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 { 417496ad9aaSAl Viro struct inode *inode = file_inode(file); 418eb04c282SJan Kara 419eb04c282SJan Kara if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 420eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 421eb04c282SJan Kara else 422eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 423eb04c282SJan Kara } 424eb04c282SJan Kara 425eb04c282SJan Kara /** 42696029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 42796029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 42896029c4eSnpiggin@suse.de * 42996029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 43096029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 43196029c4eSnpiggin@suse.de */ 43296029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 43396029c4eSnpiggin@suse.de { 434eb04c282SJan Kara int ret; 435eb04c282SJan Kara 436eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 437eb04c282SJan Kara ret = __mnt_want_write_file(file); 438eb04c282SJan Kara if (ret) 439eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 440eb04c282SJan Kara return ret; 44196029c4eSnpiggin@suse.de } 44296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 44396029c4eSnpiggin@suse.de 44496029c4eSnpiggin@suse.de /** 445eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4468366025eSDave Hansen * @mnt: the mount on which to give up write access 4478366025eSDave Hansen * 4488366025eSDave Hansen * Tells the low-level filesystem that we are done 4498366025eSDave Hansen * performing writes to it. Must be matched with 450eb04c282SJan Kara * __mnt_want_write() call above. 4518366025eSDave Hansen */ 452eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4538366025eSDave Hansen { 454d3ef3d73Snpiggin@suse.de preempt_disable(); 45583adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 456d3ef3d73Snpiggin@suse.de preempt_enable(); 4578366025eSDave Hansen } 458eb04c282SJan Kara 459eb04c282SJan Kara /** 460eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 461eb04c282SJan Kara * @mnt: the mount on which to give up write access 462eb04c282SJan Kara * 463eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 464eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 465eb04c282SJan Kara * mnt_want_write() call above. 466eb04c282SJan Kara */ 467eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 468eb04c282SJan Kara { 469eb04c282SJan Kara __mnt_drop_write(mnt); 470eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 471eb04c282SJan Kara } 4728366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4738366025eSDave Hansen 474eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 475eb04c282SJan Kara { 476eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 477eb04c282SJan Kara } 478eb04c282SJan Kara 4792a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4802a79f17eSAl Viro { 4812a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4822a79f17eSAl Viro } 4832a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4842a79f17eSAl Viro 48583adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4868366025eSDave Hansen { 4873d733633SDave Hansen int ret = 0; 4883d733633SDave Hansen 489719ea2fbSAl Viro lock_mount_hash(); 49083adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 491d3ef3d73Snpiggin@suse.de /* 492d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 493d3ef3d73Snpiggin@suse.de * should be visible before we do. 494d3ef3d73Snpiggin@suse.de */ 495d3ef3d73Snpiggin@suse.de smp_mb(); 496d3ef3d73Snpiggin@suse.de 497d3ef3d73Snpiggin@suse.de /* 498d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 499d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 500d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 501d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 502d3ef3d73Snpiggin@suse.de * 503d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 504d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 505d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 506d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 507d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 508d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 509d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 510d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 511d3ef3d73Snpiggin@suse.de * we're counting up here. 512d3ef3d73Snpiggin@suse.de */ 513c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 514d3ef3d73Snpiggin@suse.de ret = -EBUSY; 515d3ef3d73Snpiggin@suse.de else 51683adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 517d3ef3d73Snpiggin@suse.de /* 518d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 519d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 520d3ef3d73Snpiggin@suse.de */ 521d3ef3d73Snpiggin@suse.de smp_wmb(); 52283adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 523719ea2fbSAl Viro unlock_mount_hash(); 5243d733633SDave Hansen return ret; 5253d733633SDave Hansen } 5268366025eSDave Hansen 52783adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5282e4b7fcdSDave Hansen { 529719ea2fbSAl Viro lock_mount_hash(); 53083adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 531719ea2fbSAl Viro unlock_mount_hash(); 5322e4b7fcdSDave Hansen } 5332e4b7fcdSDave Hansen 5344ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5354ed5e82fSMiklos Szeredi { 5364ed5e82fSMiklos Szeredi struct mount *mnt; 5374ed5e82fSMiklos Szeredi int err = 0; 5384ed5e82fSMiklos Szeredi 5398e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5408e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5418e8b8796SMiklos Szeredi return -EBUSY; 5428e8b8796SMiklos Szeredi 543719ea2fbSAl Viro lock_mount_hash(); 5444ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5454ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5464ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5474ed5e82fSMiklos Szeredi smp_mb(); 5484ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5494ed5e82fSMiklos Szeredi err = -EBUSY; 5504ed5e82fSMiklos Szeredi break; 5514ed5e82fSMiklos Szeredi } 5524ed5e82fSMiklos Szeredi } 5534ed5e82fSMiklos Szeredi } 5548e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5558e8b8796SMiklos Szeredi err = -EBUSY; 5568e8b8796SMiklos Szeredi 5574ed5e82fSMiklos Szeredi if (!err) { 5584ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5594ed5e82fSMiklos Szeredi smp_wmb(); 5604ed5e82fSMiklos Szeredi } 5614ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5624ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5634ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5644ed5e82fSMiklos Szeredi } 565719ea2fbSAl Viro unlock_mount_hash(); 5664ed5e82fSMiklos Szeredi 5674ed5e82fSMiklos Szeredi return err; 5684ed5e82fSMiklos Szeredi } 5694ed5e82fSMiklos Szeredi 570b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5711da177e4SLinus Torvalds { 57252ba1621SAl Viro kfree(mnt->mnt_devname); 57373cd49ecSMiklos Szeredi mnt_free_id(mnt); 574d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 57568e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 576d3ef3d73Snpiggin@suse.de #endif 577b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 58048a066e7SAl Viro /* call under rcu_read_lock */ 58148a066e7SAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 58248a066e7SAl Viro { 58348a066e7SAl Viro struct mount *mnt; 58448a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 58548a066e7SAl Viro return false; 58648a066e7SAl Viro if (bastard == NULL) 58748a066e7SAl Viro return true; 58848a066e7SAl Viro mnt = real_mount(bastard); 58948a066e7SAl Viro mnt_add_count(mnt, 1); 59048a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 59148a066e7SAl Viro return true; 59248a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 59348a066e7SAl Viro mnt_add_count(mnt, -1); 59448a066e7SAl Viro return false; 59548a066e7SAl Viro } 59648a066e7SAl Viro rcu_read_unlock(); 59748a066e7SAl Viro mntput(bastard); 59848a066e7SAl Viro rcu_read_lock(); 59948a066e7SAl Viro return false; 60048a066e7SAl Viro } 60148a066e7SAl Viro 6021da177e4SLinus Torvalds /* 603474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 60448a066e7SAl Viro * call under rcu_read_lock() 6051da177e4SLinus Torvalds */ 606474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6071da177e4SLinus Torvalds { 60838129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 609474279dcSAl Viro struct mount *p; 6101da177e4SLinus Torvalds 61138129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 612474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 613474279dcSAl Viro return p; 614474279dcSAl Viro return NULL; 6151da177e4SLinus Torvalds } 616474279dcSAl Viro 617474279dcSAl Viro /* 618474279dcSAl Viro * find the last mount at @dentry on vfsmount @mnt. 61948a066e7SAl Viro * mount_lock must be held. 620474279dcSAl Viro */ 621474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry) 622474279dcSAl Viro { 62338129a13SAl Viro struct mount *p, *res; 62438129a13SAl Viro res = p = __lookup_mnt(mnt, dentry); 62538129a13SAl Viro if (!p) 62638129a13SAl Viro goto out; 62738129a13SAl Viro hlist_for_each_entry_continue(p, mnt_hash) { 6281d6a32acSAl Viro if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry) 6291d6a32acSAl Viro break; 6301d6a32acSAl Viro res = p; 6311d6a32acSAl Viro } 63238129a13SAl Viro out: 6331d6a32acSAl Viro return res; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 636a05964f3SRam Pai /* 637f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 638f015f126SDavid Howells * 639f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 640f015f126SDavid Howells * following mounts: 641f015f126SDavid Howells * 642f015f126SDavid Howells * mount /dev/sda1 /mnt 643f015f126SDavid Howells * mount /dev/sda2 /mnt 644f015f126SDavid Howells * mount /dev/sda3 /mnt 645f015f126SDavid Howells * 646f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 647f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 648f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 649f015f126SDavid Howells * 650f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 651a05964f3SRam Pai */ 6521c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 653a05964f3SRam Pai { 654c7105365SAl Viro struct mount *child_mnt; 65548a066e7SAl Viro struct vfsmount *m; 65648a066e7SAl Viro unsigned seq; 65799b7db7bSNick Piggin 65848a066e7SAl Viro rcu_read_lock(); 65948a066e7SAl Viro do { 66048a066e7SAl Viro seq = read_seqbegin(&mount_lock); 661474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 66248a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 66348a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 66448a066e7SAl Viro rcu_read_unlock(); 66548a066e7SAl Viro return m; 666a05964f3SRam Pai } 667a05964f3SRam Pai 66884d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry) 66984d17192SAl Viro { 6700818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 67184d17192SAl Viro struct mountpoint *mp; 672eed81007SMiklos Szeredi int ret; 67384d17192SAl Viro 6740818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 67584d17192SAl Viro if (mp->m_dentry == dentry) { 67684d17192SAl Viro /* might be worth a WARN_ON() */ 67784d17192SAl Viro if (d_unlinked(dentry)) 67884d17192SAl Viro return ERR_PTR(-ENOENT); 67984d17192SAl Viro mp->m_count++; 68084d17192SAl Viro return mp; 68184d17192SAl Viro } 68284d17192SAl Viro } 68384d17192SAl Viro 68484d17192SAl Viro mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 68584d17192SAl Viro if (!mp) 68684d17192SAl Viro return ERR_PTR(-ENOMEM); 68784d17192SAl Viro 688eed81007SMiklos Szeredi ret = d_set_mounted(dentry); 689eed81007SMiklos Szeredi if (ret) { 69084d17192SAl Viro kfree(mp); 691eed81007SMiklos Szeredi return ERR_PTR(ret); 69284d17192SAl Viro } 693eed81007SMiklos Szeredi 69484d17192SAl Viro mp->m_dentry = dentry; 69584d17192SAl Viro mp->m_count = 1; 6960818bf27SAl Viro hlist_add_head(&mp->m_hash, chain); 69784d17192SAl Viro return mp; 69884d17192SAl Viro } 69984d17192SAl Viro 70084d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 70184d17192SAl Viro { 70284d17192SAl Viro if (!--mp->m_count) { 70384d17192SAl Viro struct dentry *dentry = mp->m_dentry; 70484d17192SAl Viro spin_lock(&dentry->d_lock); 70584d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 70684d17192SAl Viro spin_unlock(&dentry->d_lock); 7070818bf27SAl Viro hlist_del(&mp->m_hash); 70884d17192SAl Viro kfree(mp); 70984d17192SAl Viro } 71084d17192SAl Viro } 71184d17192SAl Viro 712143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7131da177e4SLinus Torvalds { 7146b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 71799b7db7bSNick Piggin /* 71899b7db7bSNick Piggin * vfsmount lock must be held for write 71999b7db7bSNick Piggin */ 7206b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7215addc5ddSAl Viro { 7225addc5ddSAl Viro if (ns) { 7235addc5ddSAl Viro ns->event = ++event; 7245addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7255addc5ddSAl Viro } 7265addc5ddSAl Viro } 7275addc5ddSAl Viro 72899b7db7bSNick Piggin /* 72999b7db7bSNick Piggin * vfsmount lock must be held for write 73099b7db7bSNick Piggin */ 7316b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7325addc5ddSAl Viro { 7335addc5ddSAl Viro if (ns && ns->event != event) { 7345addc5ddSAl Viro ns->event = event; 7355addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7365addc5ddSAl Viro } 7375addc5ddSAl Viro } 7385addc5ddSAl Viro 73999b7db7bSNick Piggin /* 74099b7db7bSNick Piggin * vfsmount lock must be held for write 74199b7db7bSNick Piggin */ 742419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 7431da177e4SLinus Torvalds { 744a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 7450714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 7460714a533SAl Viro mnt->mnt_parent = mnt; 747a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7486b41d536SAl Viro list_del_init(&mnt->mnt_child); 74938129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 75084d17192SAl Viro put_mountpoint(mnt->mnt_mp); 75184d17192SAl Viro mnt->mnt_mp = NULL; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 75499b7db7bSNick Piggin /* 75599b7db7bSNick Piggin * vfsmount lock must be held for write 75699b7db7bSNick Piggin */ 75784d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 75884d17192SAl Viro struct mountpoint *mp, 75944d964d6SAl Viro struct mount *child_mnt) 760b90fa9aeSRam Pai { 76184d17192SAl Viro mp->m_count++; 7623a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 76384d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 7643a2393d7SAl Viro child_mnt->mnt_parent = mnt; 76584d17192SAl Viro child_mnt->mnt_mp = mp; 766b90fa9aeSRam Pai } 767b90fa9aeSRam Pai 76899b7db7bSNick Piggin /* 76999b7db7bSNick Piggin * vfsmount lock must be held for write 77099b7db7bSNick Piggin */ 77184d17192SAl Viro static void attach_mnt(struct mount *mnt, 77284d17192SAl Viro struct mount *parent, 77384d17192SAl Viro struct mountpoint *mp) 7741da177e4SLinus Torvalds { 77584d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 77638129a13SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry)); 77784d17192SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 778b90fa9aeSRam Pai } 779b90fa9aeSRam Pai 780b90fa9aeSRam Pai /* 78199b7db7bSNick Piggin * vfsmount lock must be held for write 782b90fa9aeSRam Pai */ 7831d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows) 784b90fa9aeSRam Pai { 7850714a533SAl Viro struct mount *parent = mnt->mnt_parent; 78683adc753SAl Viro struct mount *m; 787b90fa9aeSRam Pai LIST_HEAD(head); 788143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 789b90fa9aeSRam Pai 7900714a533SAl Viro BUG_ON(parent == mnt); 791b90fa9aeSRam Pai 7921a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 793f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 794143c8c91SAl Viro m->mnt_ns = n; 795f03c6599SAl Viro 796b90fa9aeSRam Pai list_splice(&head, n->list.prev); 797b90fa9aeSRam Pai 7981d6a32acSAl Viro if (shadows) 79938129a13SAl Viro hlist_add_after_rcu(&shadows->mnt_hash, &mnt->mnt_hash); 8001d6a32acSAl Viro else 80138129a13SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, 8020818bf27SAl Viro m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8036b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8046b3286edSKirill Korotaev touch_mnt_namespace(n); 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 807909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 8081da177e4SLinus Torvalds { 8096b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 8106b41d536SAl Viro if (next == &p->mnt_mounts) { 8111da177e4SLinus Torvalds while (1) { 812909b0a88SAl Viro if (p == root) 8131da177e4SLinus Torvalds return NULL; 8146b41d536SAl Viro next = p->mnt_child.next; 8156b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 8161da177e4SLinus Torvalds break; 8170714a533SAl Viro p = p->mnt_parent; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds } 8206b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 823315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 8249676f0c6SRam Pai { 8256b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 8266b41d536SAl Viro while (prev != &p->mnt_mounts) { 8276b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 8286b41d536SAl Viro prev = p->mnt_mounts.prev; 8299676f0c6SRam Pai } 8309676f0c6SRam Pai return p; 8319676f0c6SRam Pai } 8329676f0c6SRam Pai 8339d412a43SAl Viro struct vfsmount * 8349d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 8359d412a43SAl Viro { 836b105e270SAl Viro struct mount *mnt; 8379d412a43SAl Viro struct dentry *root; 8389d412a43SAl Viro 8399d412a43SAl Viro if (!type) 8409d412a43SAl Viro return ERR_PTR(-ENODEV); 8419d412a43SAl Viro 8429d412a43SAl Viro mnt = alloc_vfsmnt(name); 8439d412a43SAl Viro if (!mnt) 8449d412a43SAl Viro return ERR_PTR(-ENOMEM); 8459d412a43SAl Viro 8469d412a43SAl Viro if (flags & MS_KERNMOUNT) 847b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 8489d412a43SAl Viro 8499d412a43SAl Viro root = mount_fs(type, flags, name, data); 8509d412a43SAl Viro if (IS_ERR(root)) { 8519d412a43SAl Viro free_vfsmnt(mnt); 8529d412a43SAl Viro return ERR_CAST(root); 8539d412a43SAl Viro } 8549d412a43SAl Viro 855b105e270SAl Viro mnt->mnt.mnt_root = root; 856b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 857a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8580714a533SAl Viro mnt->mnt_parent = mnt; 859719ea2fbSAl Viro lock_mount_hash(); 86039f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 861719ea2fbSAl Viro unlock_mount_hash(); 862b105e270SAl Viro return &mnt->mnt; 8639d412a43SAl Viro } 8649d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 8659d412a43SAl Viro 86687129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 86736341f64SRam Pai int flag) 8681da177e4SLinus Torvalds { 86987129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 870be34d1a3SDavid Howells struct mount *mnt; 871be34d1a3SDavid Howells int err; 8721da177e4SLinus Torvalds 873be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 874be34d1a3SDavid Howells if (!mnt) 875be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 876be34d1a3SDavid Howells 8777a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 87815169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 879719f5d7fSMiklos Szeredi else 88015169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 881719f5d7fSMiklos Szeredi 88215169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 883be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 884719f5d7fSMiklos Szeredi if (err) 885719f5d7fSMiklos Szeredi goto out_free; 886719f5d7fSMiklos Szeredi } 887719f5d7fSMiklos Szeredi 888f2ebb3a9SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED); 889132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 890132c94e3SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY)) 891132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 892132c94e3SEric W. Biederman 8935ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 8945ff9d8a6SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire)) 8955ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 8965ff9d8a6SEric W. Biederman 8971da177e4SLinus Torvalds atomic_inc(&sb->s_active); 898b105e270SAl Viro mnt->mnt.mnt_sb = sb; 899b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 900a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9010714a533SAl Viro mnt->mnt_parent = mnt; 902719ea2fbSAl Viro lock_mount_hash(); 90339f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 904719ea2fbSAl Viro unlock_mount_hash(); 905b90fa9aeSRam Pai 9067a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 9077a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 9086776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 90932301920SAl Viro mnt->mnt_master = old; 910fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 9118aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 912fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 9136776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 914d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 9156776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 916d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 9175afe0022SRam Pai } 918b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 9190f0afb1dSAl Viro set_mnt_shared(mnt); 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 9221da177e4SLinus Torvalds * as the original if that was on one */ 92336341f64SRam Pai if (flag & CL_EXPIRE) { 9246776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 9256776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 9261da177e4SLinus Torvalds } 927be34d1a3SDavid Howells 928cb338d06SAl Viro return mnt; 929719f5d7fSMiklos Szeredi 930719f5d7fSMiklos Szeredi out_free: 931719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 932be34d1a3SDavid Howells return ERR_PTR(err); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 93548a066e7SAl Viro static void delayed_free(struct rcu_head *head) 93648a066e7SAl Viro { 93748a066e7SAl Viro struct mount *mnt = container_of(head, struct mount, mnt_rcu); 93848a066e7SAl Viro kfree(mnt->mnt_devname); 93948a066e7SAl Viro #ifdef CONFIG_SMP 94048a066e7SAl Viro free_percpu(mnt->mnt_pcp); 94148a066e7SAl Viro #endif 94248a066e7SAl Viro kmem_cache_free(mnt_cache, mnt); 94348a066e7SAl Viro } 94448a066e7SAl Viro 945900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 9467b7b1aceSAl Viro { 947b3e19d92SNick Piggin put_again: 94848a066e7SAl Viro rcu_read_lock(); 949aa9c0e07SAl Viro mnt_add_count(mnt, -1); 95048a066e7SAl Viro if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */ 95148a066e7SAl Viro rcu_read_unlock(); 95299b7db7bSNick Piggin return; 953b3e19d92SNick Piggin } 954719ea2fbSAl Viro lock_mount_hash(); 955b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 95648a066e7SAl Viro rcu_read_unlock(); 957719ea2fbSAl Viro unlock_mount_hash(); 95899b7db7bSNick Piggin return; 95999b7db7bSNick Piggin } 960863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 961863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 962863d684fSAl Viro mnt->mnt_pinned = 0; 96348a066e7SAl Viro rcu_read_unlock(); 964719ea2fbSAl Viro unlock_mount_hash(); 965900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 966b3e19d92SNick Piggin goto put_again; 967b3e19d92SNick Piggin } 96848a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 96948a066e7SAl Viro rcu_read_unlock(); 97048a066e7SAl Viro unlock_mount_hash(); 97148a066e7SAl Viro return; 97248a066e7SAl Viro } 97348a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 97448a066e7SAl Viro rcu_read_unlock(); 975962830dfSAndi Kleen 97639f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 977719ea2fbSAl Viro unlock_mount_hash(); 978649a795aSAl Viro 979649a795aSAl Viro /* 980649a795aSAl Viro * This probably indicates that somebody messed 981649a795aSAl Viro * up a mnt_want/drop_write() pair. If this 982649a795aSAl Viro * happens, the filesystem was probably unable 983649a795aSAl Viro * to make r/w->r/o transitions. 984649a795aSAl Viro */ 985649a795aSAl Viro /* 986649a795aSAl Viro * The locking used to deal with mnt_count decrement provides barriers, 987649a795aSAl Viro * so mnt_get_writers() below is safe. 988649a795aSAl Viro */ 989649a795aSAl Viro WARN_ON(mnt_get_writers(mnt)); 990649a795aSAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 991649a795aSAl Viro dput(mnt->mnt.mnt_root); 992649a795aSAl Viro deactivate_super(mnt->mnt.mnt_sb); 99348a066e7SAl Viro mnt_free_id(mnt); 99448a066e7SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free); 995b3e19d92SNick Piggin } 996b3e19d92SNick Piggin 997b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 998b3e19d92SNick Piggin { 999b3e19d92SNick Piggin if (mnt) { 1000863d684fSAl Viro struct mount *m = real_mount(mnt); 1001b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1002863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1003863d684fSAl Viro m->mnt_expiry_mark = 0; 1004863d684fSAl Viro mntput_no_expire(m); 1005b3e19d92SNick Piggin } 1006b3e19d92SNick Piggin } 1007b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1008b3e19d92SNick Piggin 1009b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1010b3e19d92SNick Piggin { 1011b3e19d92SNick Piggin if (mnt) 101283adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1013b3e19d92SNick Piggin return mnt; 1014b3e19d92SNick Piggin } 1015b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1016b3e19d92SNick Piggin 10177b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 10187b7b1aceSAl Viro { 1019719ea2fbSAl Viro lock_mount_hash(); 1020863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 1021719ea2fbSAl Viro unlock_mount_hash(); 10227b7b1aceSAl Viro } 10237b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 10247b7b1aceSAl Viro 1025863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 10267b7b1aceSAl Viro { 1027863d684fSAl Viro struct mount *mnt = real_mount(m); 1028719ea2fbSAl Viro lock_mount_hash(); 10297b7b1aceSAl Viro if (mnt->mnt_pinned) { 1030863d684fSAl Viro mnt_add_count(mnt, 1); 10317b7b1aceSAl Viro mnt->mnt_pinned--; 10327b7b1aceSAl Viro } 1033719ea2fbSAl Viro unlock_mount_hash(); 10347b7b1aceSAl Viro } 10357b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 10361da177e4SLinus Torvalds 1037b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 1038b3b304a2SMiklos Szeredi { 1039b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 1040b3b304a2SMiklos Szeredi } 1041b3b304a2SMiklos Szeredi 1042b3b304a2SMiklos Szeredi /* 1043b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 1044b3b304a2SMiklos Szeredi * implement more complex mount option showing. 1045b3b304a2SMiklos Szeredi * 1046b3b304a2SMiklos Szeredi * See also save_mount_options(). 1047b3b304a2SMiklos Szeredi */ 104834c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 1049b3b304a2SMiklos Szeredi { 10502a32cebdSAl Viro const char *options; 10512a32cebdSAl Viro 10522a32cebdSAl Viro rcu_read_lock(); 105334c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 1054b3b304a2SMiklos Szeredi 1055b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 1056b3b304a2SMiklos Szeredi seq_putc(m, ','); 1057b3b304a2SMiklos Szeredi mangle(m, options); 1058b3b304a2SMiklos Szeredi } 10592a32cebdSAl Viro rcu_read_unlock(); 1060b3b304a2SMiklos Szeredi 1061b3b304a2SMiklos Szeredi return 0; 1062b3b304a2SMiklos Szeredi } 1063b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 1064b3b304a2SMiklos Szeredi 1065b3b304a2SMiklos Szeredi /* 1066b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 1067b3b304a2SMiklos Szeredi * called from the fill_super() callback. 1068b3b304a2SMiklos Szeredi * 1069b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 1070b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 1071b3b304a2SMiklos Szeredi * remount fails. 1072b3b304a2SMiklos Szeredi * 1073b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 1074b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 1075b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 1076b3b304a2SMiklos Szeredi * any more. 1077b3b304a2SMiklos Szeredi */ 1078b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 1079b3b304a2SMiklos Szeredi { 10802a32cebdSAl Viro BUG_ON(sb->s_options); 10812a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 1082b3b304a2SMiklos Szeredi } 1083b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 1084b3b304a2SMiklos Szeredi 10852a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 10862a32cebdSAl Viro { 10872a32cebdSAl Viro char *old = sb->s_options; 10882a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 10892a32cebdSAl Viro if (old) { 10902a32cebdSAl Viro synchronize_rcu(); 10912a32cebdSAl Viro kfree(old); 10922a32cebdSAl Viro } 10932a32cebdSAl Viro } 10942a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 10952a32cebdSAl Viro 1096a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 10970226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 10981da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 10991da177e4SLinus Torvalds { 11006ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 11011da177e4SLinus Torvalds 1102390c6843SRam Pai down_read(&namespace_sem); 1103c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1104c7999c36SAl Viro void *v = p->cached_mount; 1105c7999c36SAl Viro if (*pos == p->cached_index) 1106c7999c36SAl Viro return v; 1107c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1108c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1109c7999c36SAl Viro return p->cached_mount = v; 1110c7999c36SAl Viro } 1111c7999c36SAl Viro } 1112c7999c36SAl Viro 1113c7999c36SAl Viro p->cached_event = p->ns->event; 1114c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1115c7999c36SAl Viro p->cached_index = *pos; 1116c7999c36SAl Viro return p->cached_mount; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 11201da177e4SLinus Torvalds { 11216ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1122b0765fb8SPavel Emelianov 1123c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1124c7999c36SAl Viro p->cached_index = *pos; 1125c7999c36SAl Viro return p->cached_mount; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 11291da177e4SLinus Torvalds { 1130390c6843SRam Pai up_read(&namespace_sem); 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 11330226f492SAl Viro static int m_show(struct seq_file *m, void *v) 11349f5596afSAl Viro { 11356ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 11361a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 11370226f492SAl Viro return p->show(m, &r->mnt); 11381da177e4SLinus Torvalds } 11391da177e4SLinus Torvalds 1140a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 11411da177e4SLinus Torvalds .start = m_start, 11421da177e4SLinus Torvalds .next = m_next, 11431da177e4SLinus Torvalds .stop = m_stop, 11440226f492SAl Viro .show = m_show, 1145b4629fe2SChuck Lever }; 1146a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1147b4629fe2SChuck Lever 11481da177e4SLinus Torvalds /** 11491da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 11501da177e4SLinus Torvalds * @mnt: root of mount tree 11511da177e4SLinus Torvalds * 11521da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 11531da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 11541da177e4SLinus Torvalds * busy. 11551da177e4SLinus Torvalds */ 1156909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 11571da177e4SLinus Torvalds { 1158909b0a88SAl Viro struct mount *mnt = real_mount(m); 115936341f64SRam Pai int actual_refs = 0; 116036341f64SRam Pai int minimum_refs = 0; 1161315fc83eSAl Viro struct mount *p; 1162909b0a88SAl Viro BUG_ON(!m); 11631da177e4SLinus Torvalds 1164b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1165719ea2fbSAl Viro lock_mount_hash(); 1166909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 116783adc753SAl Viro actual_refs += mnt_get_count(p); 11681da177e4SLinus Torvalds minimum_refs += 2; 11691da177e4SLinus Torvalds } 1170719ea2fbSAl Viro unlock_mount_hash(); 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds if (actual_refs > minimum_refs) 11731da177e4SLinus Torvalds return 0; 1174e3474a8eSIan Kent 1175e3474a8eSIan Kent return 1; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds /** 11811da177e4SLinus Torvalds * may_umount - check if a mount point is busy 11821da177e4SLinus Torvalds * @mnt: root of mount 11831da177e4SLinus Torvalds * 11841da177e4SLinus Torvalds * This is called to check if a mount point has any 11851da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 11861da177e4SLinus Torvalds * mount has sub mounts this will return busy 11871da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 11881da177e4SLinus Torvalds * 11891da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 11901da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 11911da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 11921da177e4SLinus Torvalds */ 11931da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 11941da177e4SLinus Torvalds { 1195e3474a8eSIan Kent int ret = 1; 11968ad08d8aSAl Viro down_read(&namespace_sem); 1197719ea2fbSAl Viro lock_mount_hash(); 11981ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1199e3474a8eSIan Kent ret = 0; 1200719ea2fbSAl Viro unlock_mount_hash(); 12018ad08d8aSAl Viro up_read(&namespace_sem); 1202a05964f3SRam Pai return ret; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 12061da177e4SLinus Torvalds 120738129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1208e3197d83SAl Viro 120997216be0SAl Viro static void namespace_unlock(void) 12101da177e4SLinus Torvalds { 1211d5e50f74SAl Viro struct mount *mnt; 121238129a13SAl Viro struct hlist_head head = unmounted; 121397216be0SAl Viro 121438129a13SAl Viro if (likely(hlist_empty(&head))) { 121597216be0SAl Viro up_write(&namespace_sem); 121697216be0SAl Viro return; 121797216be0SAl Viro } 121897216be0SAl Viro 121938129a13SAl Viro head.first->pprev = &head.first; 122038129a13SAl Viro INIT_HLIST_HEAD(&unmounted); 122138129a13SAl Viro 122297216be0SAl Viro up_write(&namespace_sem); 122397216be0SAl Viro 122448a066e7SAl Viro synchronize_rcu(); 122548a066e7SAl Viro 122638129a13SAl Viro while (!hlist_empty(&head)) { 122738129a13SAl Viro mnt = hlist_entry(head.first, struct mount, mnt_hash); 122838129a13SAl Viro hlist_del_init(&mnt->mnt_hash); 1229aba809cfSAl Viro if (mnt->mnt_ex_mountpoint.mnt) 1230aba809cfSAl Viro path_put(&mnt->mnt_ex_mountpoint); 1231d5e50f74SAl Viro mntput(&mnt->mnt); 123270fbcdf4SRam Pai } 123370fbcdf4SRam Pai } 123470fbcdf4SRam Pai 123597216be0SAl Viro static inline void namespace_lock(void) 1236e3197d83SAl Viro { 123797216be0SAl Viro down_write(&namespace_sem); 1238e3197d83SAl Viro } 1239e3197d83SAl Viro 124099b7db7bSNick Piggin /* 124148a066e7SAl Viro * mount_lock must be held 124299b7db7bSNick Piggin * namespace_sem must be held for write 124348a066e7SAl Viro * how = 0 => just this tree, don't propagate 124448a066e7SAl Viro * how = 1 => propagate; we know that nobody else has reference to any victims 124548a066e7SAl Viro * how = 2 => lazy umount 124699b7db7bSNick Piggin */ 124748a066e7SAl Viro void umount_tree(struct mount *mnt, int how) 124870fbcdf4SRam Pai { 124938129a13SAl Viro HLIST_HEAD(tmp_list); 1250315fc83eSAl Viro struct mount *p; 125138129a13SAl Viro struct mount *last = NULL; 125270fbcdf4SRam Pai 125338129a13SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 125438129a13SAl Viro hlist_del_init_rcu(&p->mnt_hash); 125538129a13SAl Viro hlist_add_head(&p->mnt_hash, &tmp_list); 125638129a13SAl Viro } 125770fbcdf4SRam Pai 125848a066e7SAl Viro if (how) 12597b8a53fdSAl Viro propagate_umount(&tmp_list); 1260a05964f3SRam Pai 126138129a13SAl Viro hlist_for_each_entry(p, &tmp_list, mnt_hash) { 12626776db3dSAl Viro list_del_init(&p->mnt_expire); 12631a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1264143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 126563d37a84SAl Viro p->mnt_ns = NULL; 126648a066e7SAl Viro if (how < 2) 126748a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 12686b41d536SAl Viro list_del_init(&p->mnt_child); 1269676da58dSAl Viro if (mnt_has_parent(p)) { 127084d17192SAl Viro put_mountpoint(p->mnt_mp); 1271aba809cfSAl Viro /* move the reference to mountpoint into ->mnt_ex_mountpoint */ 1272aba809cfSAl Viro p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint; 1273aba809cfSAl Viro p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt; 1274aba809cfSAl Viro p->mnt_mountpoint = p->mnt.mnt_root; 1275aba809cfSAl Viro p->mnt_parent = p; 127684d17192SAl Viro p->mnt_mp = NULL; 12777c4b93d8SAl Viro } 12780f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 127938129a13SAl Viro last = p; 12801da177e4SLinus Torvalds } 128138129a13SAl Viro if (last) { 128238129a13SAl Viro last->mnt_hash.next = unmounted.first; 128338129a13SAl Viro unmounted.first = tmp_list.first; 128438129a13SAl Viro unmounted.first->pprev = &unmounted.first; 128538129a13SAl Viro } 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 1288b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1289c35038beSAl Viro 12901ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 12911da177e4SLinus Torvalds { 12921ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 12931da177e4SLinus Torvalds int retval; 12941da177e4SLinus Torvalds 12951ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 12961da177e4SLinus Torvalds if (retval) 12971da177e4SLinus Torvalds return retval; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* 13001da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 13011da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 13021da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 13031da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 13041da177e4SLinus Torvalds */ 13051da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 13061ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 13071da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 13081da177e4SLinus Torvalds return -EINVAL; 13091da177e4SLinus Torvalds 1310b3e19d92SNick Piggin /* 1311b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1312b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1313b3e19d92SNick Piggin */ 1314719ea2fbSAl Viro lock_mount_hash(); 131583adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1316719ea2fbSAl Viro unlock_mount_hash(); 13171da177e4SLinus Torvalds return -EBUSY; 1318b3e19d92SNick Piggin } 1319719ea2fbSAl Viro unlock_mount_hash(); 13201da177e4SLinus Torvalds 1321863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 13221da177e4SLinus Torvalds return -EAGAIN; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds /* 13261da177e4SLinus Torvalds * If we may have to abort operations to get out of this 13271da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 13281da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 13291da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 13301da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 13311da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 13321da177e4SLinus Torvalds * about for the moment. 13331da177e4SLinus Torvalds */ 13341da177e4SLinus Torvalds 133542faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 133642faad99SAl Viro sb->s_op->umount_begin(sb); 133742faad99SAl Viro } 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds /* 13401da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 13411da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 13421da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 13431da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 13441da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 13451da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 13461da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 13471da177e4SLinus Torvalds */ 13481ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 13491da177e4SLinus Torvalds /* 13501da177e4SLinus Torvalds * Special case for "unmounting" root ... 13511da177e4SLinus Torvalds * we just try to remount it readonly. 13521da177e4SLinus Torvalds */ 13531da177e4SLinus Torvalds down_write(&sb->s_umount); 13544aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 13551da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 13561da177e4SLinus Torvalds up_write(&sb->s_umount); 13571da177e4SLinus Torvalds return retval; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 136097216be0SAl Viro namespace_lock(); 1361719ea2fbSAl Viro lock_mount_hash(); 13625addc5ddSAl Viro event++; 13631da177e4SLinus Torvalds 136448a066e7SAl Viro if (flags & MNT_DETACH) { 136548a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 136648a066e7SAl Viro umount_tree(mnt, 2); 136748a066e7SAl Viro retval = 0; 136848a066e7SAl Viro } else { 1369b54b9be7SAl Viro shrink_submounts(mnt); 13701da177e4SLinus Torvalds retval = -EBUSY; 137148a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 13721a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1373328e6d90SAl Viro umount_tree(mnt, 1); 13741da177e4SLinus Torvalds retval = 0; 13751da177e4SLinus Torvalds } 137648a066e7SAl Viro } 1377719ea2fbSAl Viro unlock_mount_hash(); 1378e3197d83SAl Viro namespace_unlock(); 13791da177e4SLinus Torvalds return retval; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* 13839b40bc90SAl Viro * Is the caller allowed to modify his namespace? 13849b40bc90SAl Viro */ 13859b40bc90SAl Viro static inline bool may_mount(void) 13869b40bc90SAl Viro { 13879b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 13889b40bc90SAl Viro } 13899b40bc90SAl Viro 13909b40bc90SAl Viro /* 13911da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 13921da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 13931da177e4SLinus Torvalds * 13941da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 13951da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 13961da177e4SLinus Torvalds */ 13971da177e4SLinus Torvalds 1398bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 13991da177e4SLinus Torvalds { 14002d8f3038SAl Viro struct path path; 1401900148dcSAl Viro struct mount *mnt; 14021da177e4SLinus Torvalds int retval; 1403db1f05bbSMiklos Szeredi int lookup_flags = 0; 14041da177e4SLinus Torvalds 1405db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1406db1f05bbSMiklos Szeredi return -EINVAL; 1407db1f05bbSMiklos Szeredi 14089b40bc90SAl Viro if (!may_mount()) 14099b40bc90SAl Viro return -EPERM; 14109b40bc90SAl Viro 1411db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1412db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1413db1f05bbSMiklos Szeredi 1414197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 14151da177e4SLinus Torvalds if (retval) 14161da177e4SLinus Torvalds goto out; 1417900148dcSAl Viro mnt = real_mount(path.mnt); 14181da177e4SLinus Torvalds retval = -EINVAL; 14192d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 14201da177e4SLinus Torvalds goto dput_and_out; 1421143c8c91SAl Viro if (!check_mnt(mnt)) 14221da177e4SLinus Torvalds goto dput_and_out; 14235ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 14245ff9d8a6SEric W. Biederman goto dput_and_out; 14251da177e4SLinus Torvalds 1426900148dcSAl Viro retval = do_umount(mnt, flags); 14271da177e4SLinus Torvalds dput_and_out: 1428429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 14292d8f3038SAl Viro dput(path.dentry); 1430900148dcSAl Viro mntput_no_expire(mnt); 14311da177e4SLinus Torvalds out: 14321da177e4SLinus Torvalds return retval; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds /* 14381da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 14391da177e4SLinus Torvalds */ 1440bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds return sys_umount(name, 0); 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds #endif 14461da177e4SLinus Torvalds 14474ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 14488823c079SEric W. Biederman { 14494ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 14504ce5d2b1SEric W. Biederman struct inode *inode = dentry->d_inode; 14510bb80f24SDavid Howells struct proc_ns *ei; 14528823c079SEric W. Biederman 14538823c079SEric W. Biederman if (!proc_ns_inode(inode)) 14548823c079SEric W. Biederman return false; 14558823c079SEric W. Biederman 14560bb80f24SDavid Howells ei = get_proc_ns(inode); 14578823c079SEric W. Biederman if (ei->ns_ops != &mntns_operations) 14588823c079SEric W. Biederman return false; 14598823c079SEric W. Biederman 14604ce5d2b1SEric W. Biederman return true; 14614ce5d2b1SEric W. Biederman } 14624ce5d2b1SEric W. Biederman 14634ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 14644ce5d2b1SEric W. Biederman { 14654ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 14664ce5d2b1SEric W. Biederman * mount namespace loop? 14674ce5d2b1SEric W. Biederman */ 14684ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 14694ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 14704ce5d2b1SEric W. Biederman return false; 14714ce5d2b1SEric W. Biederman 14724ce5d2b1SEric W. Biederman mnt_ns = get_proc_ns(dentry->d_inode)->ns; 14738823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 14748823c079SEric W. Biederman } 14758823c079SEric W. Biederman 147687129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 147736341f64SRam Pai int flag) 14781da177e4SLinus Torvalds { 147984d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 14801da177e4SLinus Torvalds 14814ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 14824ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 14834ce5d2b1SEric W. Biederman 14844ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1485be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 14869676f0c6SRam Pai 148736341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1488be34d1a3SDavid Howells if (IS_ERR(q)) 1489be34d1a3SDavid Howells return q; 1490be34d1a3SDavid Howells 14915ff9d8a6SEric W. Biederman q->mnt.mnt_flags &= ~MNT_LOCKED; 1492a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds p = mnt; 14956b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1496315fc83eSAl Viro struct mount *s; 14977ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 14981da177e4SLinus Torvalds continue; 14991da177e4SLinus Torvalds 1500909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 15014ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 15024ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 15034ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 15044ce5d2b1SEric W. Biederman continue; 15054ce5d2b1SEric W. Biederman } 15064ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 15074ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 15089676f0c6SRam Pai s = skip_mnt_tree(s); 15099676f0c6SRam Pai continue; 15109676f0c6SRam Pai } 15110714a533SAl Viro while (p != s->mnt_parent) { 15120714a533SAl Viro p = p->mnt_parent; 15130714a533SAl Viro q = q->mnt_parent; 15141da177e4SLinus Torvalds } 151587129cc0SAl Viro p = s; 151684d17192SAl Viro parent = q; 151787129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1518be34d1a3SDavid Howells if (IS_ERR(q)) 1519be34d1a3SDavid Howells goto out; 1520719ea2fbSAl Viro lock_mount_hash(); 15211a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 152284d17192SAl Viro attach_mnt(q, parent, p->mnt_mp); 1523719ea2fbSAl Viro unlock_mount_hash(); 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds return res; 1527be34d1a3SDavid Howells out: 15281da177e4SLinus Torvalds if (res) { 1529719ea2fbSAl Viro lock_mount_hash(); 1530328e6d90SAl Viro umount_tree(res, 0); 1531719ea2fbSAl Viro unlock_mount_hash(); 15321da177e4SLinus Torvalds } 1533be34d1a3SDavid Howells return q; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 1536be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1537be34d1a3SDavid Howells 1538589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 15398aec0809SAl Viro { 1540cb338d06SAl Viro struct mount *tree; 154197216be0SAl Viro namespace_lock(); 154287129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 154387129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1544328e6d90SAl Viro namespace_unlock(); 1545be34d1a3SDavid Howells if (IS_ERR(tree)) 154652e220d3SDan Carpenter return ERR_CAST(tree); 1547be34d1a3SDavid Howells return &tree->mnt; 15488aec0809SAl Viro } 15498aec0809SAl Viro 15508aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 15518aec0809SAl Viro { 155297216be0SAl Viro namespace_lock(); 1553719ea2fbSAl Viro lock_mount_hash(); 1554328e6d90SAl Viro umount_tree(real_mount(mnt), 0); 1555719ea2fbSAl Viro unlock_mount_hash(); 15563ab6abeeSAl Viro namespace_unlock(); 15578aec0809SAl Viro } 15588aec0809SAl Viro 15591f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 15601f707137SAl Viro struct vfsmount *root) 15611f707137SAl Viro { 15621a4eeaf2SAl Viro struct mount *mnt; 15631f707137SAl Viro int res = f(root, arg); 15641f707137SAl Viro if (res) 15651f707137SAl Viro return res; 15661a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 15671a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 15681f707137SAl Viro if (res) 15691f707137SAl Viro return res; 15701f707137SAl Viro } 15711f707137SAl Viro return 0; 15721f707137SAl Viro } 15731f707137SAl Viro 15744b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1575719f5d7fSMiklos Szeredi { 1576315fc83eSAl Viro struct mount *p; 1577719f5d7fSMiklos Szeredi 1578909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1579fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 15804b8b21f4SAl Viro mnt_release_group_id(p); 1581719f5d7fSMiklos Szeredi } 1582719f5d7fSMiklos Szeredi } 1583719f5d7fSMiklos Szeredi 15844b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1585719f5d7fSMiklos Szeredi { 1586315fc83eSAl Viro struct mount *p; 1587719f5d7fSMiklos Szeredi 1588909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1589fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 15904b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1591719f5d7fSMiklos Szeredi if (err) { 15924b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1593719f5d7fSMiklos Szeredi return err; 1594719f5d7fSMiklos Szeredi } 1595719f5d7fSMiklos Szeredi } 1596719f5d7fSMiklos Szeredi } 1597719f5d7fSMiklos Szeredi 1598719f5d7fSMiklos Szeredi return 0; 1599719f5d7fSMiklos Szeredi } 1600719f5d7fSMiklos Szeredi 1601b90fa9aeSRam Pai /* 1602b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1603b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 160421444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 160521444403SRam Pai * store the parent mount and mountpoint dentry. 160621444403SRam Pai * (done when source_mnt is moved) 1607b90fa9aeSRam Pai * 1608b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1609b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 16109676f0c6SRam Pai * --------------------------------------------------------------------------- 1611b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 16129676f0c6SRam Pai * |************************************************************************** 16139676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 16149676f0c6SRam Pai * | dest | | | | | 16159676f0c6SRam Pai * | | | | | | | 16169676f0c6SRam Pai * | v | | | | | 16179676f0c6SRam Pai * |************************************************************************** 16189676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 16195afe0022SRam Pai * | | | | | | 16209676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 16219676f0c6SRam Pai * *************************************************************************** 1622b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1623b90fa9aeSRam Pai * destination mount. 1624b90fa9aeSRam Pai * 1625b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1626b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1627b90fa9aeSRam Pai * the peer group of the source mount. 1628b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1629b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1630b90fa9aeSRam Pai * mount. 16315afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 16325afe0022SRam Pai * of the destination mount and the cloned mount is made slave 16335afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 16345afe0022SRam Pai * is marked as 'shared and slave'. 16355afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 16365afe0022SRam Pai * source mount. 16375afe0022SRam Pai * 16389676f0c6SRam Pai * --------------------------------------------------------------------------- 163921444403SRam Pai * | MOVE MOUNT OPERATION | 16409676f0c6SRam Pai * |************************************************************************** 16419676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 16429676f0c6SRam Pai * | dest | | | | | 16439676f0c6SRam Pai * | | | | | | | 16449676f0c6SRam Pai * | v | | | | | 16459676f0c6SRam Pai * |************************************************************************** 16469676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 16475afe0022SRam Pai * | | | | | | 16489676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 16499676f0c6SRam Pai * *************************************************************************** 16505afe0022SRam Pai * 16515afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 16525afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 165321444403SRam Pai * (+*) the mount is moved to the destination. 16545afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 16555afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 16565afe0022SRam Pai * the mount is marked as 'shared and slave'. 16575afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1658b90fa9aeSRam Pai * 1659b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1660b90fa9aeSRam Pai * applied to each mount in the tree. 1661b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1662b90fa9aeSRam Pai * in allocations. 1663b90fa9aeSRam Pai */ 16640fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 166584d17192SAl Viro struct mount *dest_mnt, 166684d17192SAl Viro struct mountpoint *dest_mp, 166784d17192SAl Viro struct path *parent_path) 1668b90fa9aeSRam Pai { 166938129a13SAl Viro HLIST_HEAD(tree_list); 1670315fc83eSAl Viro struct mount *child, *p; 167138129a13SAl Viro struct hlist_node *n; 1672719f5d7fSMiklos Szeredi int err; 1673b90fa9aeSRam Pai 1674fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 16750fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1676719f5d7fSMiklos Szeredi if (err) 1677719f5d7fSMiklos Szeredi goto out; 167884d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1679f2ebb3a9SAl Viro lock_mount_hash(); 1680719f5d7fSMiklos Szeredi if (err) 1681719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1682909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 16830f0afb1dSAl Viro set_mnt_shared(p); 16840b1b901bSAl Viro } else { 16850b1b901bSAl Viro lock_mount_hash(); 1686b90fa9aeSRam Pai } 16871a390689SAl Viro if (parent_path) { 16880fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 168984d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1690143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 169121444403SRam Pai } else { 169284d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 16931d6a32acSAl Viro commit_tree(source_mnt, NULL); 169421444403SRam Pai } 1695b90fa9aeSRam Pai 169638129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 16971d6a32acSAl Viro struct mount *q; 169838129a13SAl Viro hlist_del_init(&child->mnt_hash); 16991d6a32acSAl Viro q = __lookup_mnt_last(&child->mnt_parent->mnt, 17001d6a32acSAl Viro child->mnt_mountpoint); 17011d6a32acSAl Viro commit_tree(child, q); 1702b90fa9aeSRam Pai } 1703719ea2fbSAl Viro unlock_mount_hash(); 170499b7db7bSNick Piggin 1705b90fa9aeSRam Pai return 0; 1706719f5d7fSMiklos Szeredi 1707719f5d7fSMiklos Szeredi out_cleanup_ids: 1708f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 1709f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 1710f2ebb3a9SAl Viro umount_tree(child, 0); 1711f2ebb3a9SAl Viro } 1712f2ebb3a9SAl Viro unlock_mount_hash(); 17130fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1714719f5d7fSMiklos Szeredi out: 1715719f5d7fSMiklos Szeredi return err; 1716b90fa9aeSRam Pai } 1717b90fa9aeSRam Pai 171884d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 1719b12cea91SAl Viro { 1720b12cea91SAl Viro struct vfsmount *mnt; 172184d17192SAl Viro struct dentry *dentry = path->dentry; 1722b12cea91SAl Viro retry: 172384d17192SAl Viro mutex_lock(&dentry->d_inode->i_mutex); 172484d17192SAl Viro if (unlikely(cant_mount(dentry))) { 172584d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 172684d17192SAl Viro return ERR_PTR(-ENOENT); 1727b12cea91SAl Viro } 172897216be0SAl Viro namespace_lock(); 1729b12cea91SAl Viro mnt = lookup_mnt(path); 173084d17192SAl Viro if (likely(!mnt)) { 173184d17192SAl Viro struct mountpoint *mp = new_mountpoint(dentry); 173284d17192SAl Viro if (IS_ERR(mp)) { 173397216be0SAl Viro namespace_unlock(); 173484d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 173584d17192SAl Viro return mp; 173684d17192SAl Viro } 173784d17192SAl Viro return mp; 173884d17192SAl Viro } 173997216be0SAl Viro namespace_unlock(); 1740b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1741b12cea91SAl Viro path_put(path); 1742b12cea91SAl Viro path->mnt = mnt; 174384d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 1744b12cea91SAl Viro goto retry; 1745b12cea91SAl Viro } 1746b12cea91SAl Viro 174784d17192SAl Viro static void unlock_mount(struct mountpoint *where) 1748b12cea91SAl Viro { 174984d17192SAl Viro struct dentry *dentry = where->m_dentry; 175084d17192SAl Viro put_mountpoint(where); 1751328e6d90SAl Viro namespace_unlock(); 175284d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 1753b12cea91SAl Viro } 1754b12cea91SAl Viro 175584d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 17561da177e4SLinus Torvalds { 175795bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 17581da177e4SLinus Torvalds return -EINVAL; 17591da177e4SLinus Torvalds 176084d17192SAl Viro if (S_ISDIR(mp->m_dentry->d_inode->i_mode) != 176195bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 17621da177e4SLinus Torvalds return -ENOTDIR; 17631da177e4SLinus Torvalds 176484d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds /* 17687a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 17697a2e8a8fSValerie Aurora */ 17707a2e8a8fSValerie Aurora 17717a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 17727a2e8a8fSValerie Aurora { 17737c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 17747a2e8a8fSValerie Aurora 17757a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 17767a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 17777a2e8a8fSValerie Aurora return 0; 17787a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 17797a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 17807a2e8a8fSValerie Aurora return 0; 17817a2e8a8fSValerie Aurora return type; 17827a2e8a8fSValerie Aurora } 17837a2e8a8fSValerie Aurora 17847a2e8a8fSValerie Aurora /* 178507b20889SRam Pai * recursively change the type of the mountpoint. 178607b20889SRam Pai */ 17870a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 178807b20889SRam Pai { 1789315fc83eSAl Viro struct mount *m; 17904b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 179107b20889SRam Pai int recurse = flag & MS_REC; 17927a2e8a8fSValerie Aurora int type; 1793719f5d7fSMiklos Szeredi int err = 0; 179407b20889SRam Pai 17952d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 179607b20889SRam Pai return -EINVAL; 179707b20889SRam Pai 17987a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 17997a2e8a8fSValerie Aurora if (!type) 18007a2e8a8fSValerie Aurora return -EINVAL; 18017a2e8a8fSValerie Aurora 180297216be0SAl Viro namespace_lock(); 1803719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1804719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1805719f5d7fSMiklos Szeredi if (err) 1806719f5d7fSMiklos Szeredi goto out_unlock; 1807719f5d7fSMiklos Szeredi } 1808719f5d7fSMiklos Szeredi 1809719ea2fbSAl Viro lock_mount_hash(); 1810909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 18110f0afb1dSAl Viro change_mnt_propagation(m, type); 1812719ea2fbSAl Viro unlock_mount_hash(); 1813719f5d7fSMiklos Szeredi 1814719f5d7fSMiklos Szeredi out_unlock: 181597216be0SAl Viro namespace_unlock(); 1816719f5d7fSMiklos Szeredi return err; 181707b20889SRam Pai } 181807b20889SRam Pai 18195ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 18205ff9d8a6SEric W. Biederman { 18215ff9d8a6SEric W. Biederman struct mount *child; 18225ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 18235ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 18245ff9d8a6SEric W. Biederman continue; 18255ff9d8a6SEric W. Biederman 18265ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 18275ff9d8a6SEric W. Biederman return true; 18285ff9d8a6SEric W. Biederman } 18295ff9d8a6SEric W. Biederman return false; 18305ff9d8a6SEric W. Biederman } 18315ff9d8a6SEric W. Biederman 183207b20889SRam Pai /* 18331da177e4SLinus Torvalds * do loopback mount. 18341da177e4SLinus Torvalds */ 1835808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 18362dafe1c4SEric Sandeen int recurse) 18371da177e4SLinus Torvalds { 18382d92ab3cSAl Viro struct path old_path; 183984d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 184084d17192SAl Viro struct mountpoint *mp; 184157eccb83SAl Viro int err; 18421da177e4SLinus Torvalds if (!old_name || !*old_name) 18431da177e4SLinus Torvalds return -EINVAL; 1844815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 18451da177e4SLinus Torvalds if (err) 18461da177e4SLinus Torvalds return err; 18471da177e4SLinus Torvalds 18488823c079SEric W. Biederman err = -EINVAL; 18494ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 18508823c079SEric W. Biederman goto out; 18518823c079SEric W. Biederman 185284d17192SAl Viro mp = lock_mount(path); 185384d17192SAl Viro err = PTR_ERR(mp); 185484d17192SAl Viro if (IS_ERR(mp)) 18559676f0c6SRam Pai goto out; 18569676f0c6SRam Pai 185787129cc0SAl Viro old = real_mount(old_path.mnt); 185884d17192SAl Viro parent = real_mount(path->mnt); 185987129cc0SAl Viro 1860b12cea91SAl Viro err = -EINVAL; 1861fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1862b12cea91SAl Viro goto out2; 1863b12cea91SAl Viro 186484d17192SAl Viro if (!check_mnt(parent) || !check_mnt(old)) 1865b12cea91SAl Viro goto out2; 1866ccd48bc7SAl Viro 18675ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 18685ff9d8a6SEric W. Biederman goto out2; 18695ff9d8a6SEric W. Biederman 18701da177e4SLinus Torvalds if (recurse) 18714ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 18721da177e4SLinus Torvalds else 187387129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 18741da177e4SLinus Torvalds 1875be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1876be34d1a3SDavid Howells err = PTR_ERR(mnt); 1877e9c5d8a5SAndrey Vagin goto out2; 1878be34d1a3SDavid Howells } 1879ccd48bc7SAl Viro 18805ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 18815ff9d8a6SEric W. Biederman 188284d17192SAl Viro err = graft_tree(mnt, parent, mp); 18831da177e4SLinus Torvalds if (err) { 1884719ea2fbSAl Viro lock_mount_hash(); 1885328e6d90SAl Viro umount_tree(mnt, 0); 1886719ea2fbSAl Viro unlock_mount_hash(); 18875b83d2c5SRam Pai } 1888b12cea91SAl Viro out2: 188984d17192SAl Viro unlock_mount(mp); 1890ccd48bc7SAl Viro out: 18912d92ab3cSAl Viro path_put(&old_path); 18921da177e4SLinus Torvalds return err; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 18952e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 18962e4b7fcdSDave Hansen { 18972e4b7fcdSDave Hansen int error = 0; 18982e4b7fcdSDave Hansen int readonly_request = 0; 18992e4b7fcdSDave Hansen 19002e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 19012e4b7fcdSDave Hansen readonly_request = 1; 19022e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 19032e4b7fcdSDave Hansen return 0; 19042e4b7fcdSDave Hansen 190590563b19SEric W. Biederman if (mnt->mnt_flags & MNT_LOCK_READONLY) 190690563b19SEric W. Biederman return -EPERM; 190790563b19SEric W. Biederman 19082e4b7fcdSDave Hansen if (readonly_request) 190983adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 19102e4b7fcdSDave Hansen else 191183adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 19122e4b7fcdSDave Hansen return error; 19132e4b7fcdSDave Hansen } 19142e4b7fcdSDave Hansen 19151da177e4SLinus Torvalds /* 19161da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 19171da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 19181da177e4SLinus Torvalds * on it - tough luck. 19191da177e4SLinus Torvalds */ 19200a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 19211da177e4SLinus Torvalds void *data) 19221da177e4SLinus Torvalds { 19231da177e4SLinus Torvalds int err; 19242d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1925143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 19261da177e4SLinus Torvalds 1927143c8c91SAl Viro if (!check_mnt(mnt)) 19281da177e4SLinus Torvalds return -EINVAL; 19291da177e4SLinus Torvalds 19302d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 19311da177e4SLinus Torvalds return -EINVAL; 19321da177e4SLinus Torvalds 1933ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1934ff36fe2cSEric Paris if (err) 1935ff36fe2cSEric Paris return err; 1936ff36fe2cSEric Paris 19371da177e4SLinus Torvalds down_write(&sb->s_umount); 19382e4b7fcdSDave Hansen if (flags & MS_BIND) 19392d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 194057eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 194157eccb83SAl Viro err = -EPERM; 19424aa98cf7SAl Viro else 19431da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 19447b43a79fSAl Viro if (!err) { 1945719ea2fbSAl Viro lock_mount_hash(); 1946143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1947143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1948143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1949719ea2fbSAl Viro unlock_mount_hash(); 19500e55a7ccSDan Williams } 19516339dab8SAl Viro up_write(&sb->s_umount); 19521da177e4SLinus Torvalds return err; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds 1955cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 19569676f0c6SRam Pai { 1957315fc83eSAl Viro struct mount *p; 1958909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1959fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 19609676f0c6SRam Pai return 1; 19619676f0c6SRam Pai } 19629676f0c6SRam Pai return 0; 19639676f0c6SRam Pai } 19649676f0c6SRam Pai 1965808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 19661da177e4SLinus Torvalds { 19672d92ab3cSAl Viro struct path old_path, parent_path; 1968676da58dSAl Viro struct mount *p; 19690fb54e50SAl Viro struct mount *old; 197084d17192SAl Viro struct mountpoint *mp; 197157eccb83SAl Viro int err; 19721da177e4SLinus Torvalds if (!old_name || !*old_name) 19731da177e4SLinus Torvalds return -EINVAL; 19742d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 19751da177e4SLinus Torvalds if (err) 19761da177e4SLinus Torvalds return err; 19771da177e4SLinus Torvalds 197884d17192SAl Viro mp = lock_mount(path); 197984d17192SAl Viro err = PTR_ERR(mp); 198084d17192SAl Viro if (IS_ERR(mp)) 1981cc53ce53SDavid Howells goto out; 1982cc53ce53SDavid Howells 1983143c8c91SAl Viro old = real_mount(old_path.mnt); 1984fc7be130SAl Viro p = real_mount(path->mnt); 1985143c8c91SAl Viro 19861da177e4SLinus Torvalds err = -EINVAL; 1987fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 19881da177e4SLinus Torvalds goto out1; 19891da177e4SLinus Torvalds 19905ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 19915ff9d8a6SEric W. Biederman goto out1; 19925ff9d8a6SEric W. Biederman 19931da177e4SLinus Torvalds err = -EINVAL; 19942d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 199521444403SRam Pai goto out1; 19961da177e4SLinus Torvalds 1997676da58dSAl Viro if (!mnt_has_parent(old)) 199821444403SRam Pai goto out1; 19991da177e4SLinus Torvalds 20002d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 20012d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 200221444403SRam Pai goto out1; 200321444403SRam Pai /* 200421444403SRam Pai * Don't move a mount residing in a shared parent. 200521444403SRam Pai */ 2006fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 200721444403SRam Pai goto out1; 20089676f0c6SRam Pai /* 20099676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 20109676f0c6SRam Pai * mount which is shared. 20119676f0c6SRam Pai */ 2012fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 20139676f0c6SRam Pai goto out1; 20141da177e4SLinus Torvalds err = -ELOOP; 2015fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2016676da58dSAl Viro if (p == old) 201721444403SRam Pai goto out1; 20181da177e4SLinus Torvalds 201984d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 20204ac91378SJan Blunck if (err) 202121444403SRam Pai goto out1; 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 20241da177e4SLinus Torvalds * automatically */ 20256776db3dSAl Viro list_del_init(&old->mnt_expire); 20261da177e4SLinus Torvalds out1: 202784d17192SAl Viro unlock_mount(mp); 20281da177e4SLinus Torvalds out: 20291da177e4SLinus Torvalds if (!err) 20301a390689SAl Viro path_put(&parent_path); 20312d92ab3cSAl Viro path_put(&old_path); 20321da177e4SLinus Torvalds return err; 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds 20359d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 20369d412a43SAl Viro { 20379d412a43SAl Viro int err; 20389d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 20399d412a43SAl Viro if (subtype) { 20409d412a43SAl Viro subtype++; 20419d412a43SAl Viro err = -EINVAL; 20429d412a43SAl Viro if (!subtype[0]) 20439d412a43SAl Viro goto err; 20449d412a43SAl Viro } else 20459d412a43SAl Viro subtype = ""; 20469d412a43SAl Viro 20479d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 20489d412a43SAl Viro err = -ENOMEM; 20499d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 20509d412a43SAl Viro goto err; 20519d412a43SAl Viro return mnt; 20529d412a43SAl Viro 20539d412a43SAl Viro err: 20549d412a43SAl Viro mntput(mnt); 20559d412a43SAl Viro return ERR_PTR(err); 20569d412a43SAl Viro } 20579d412a43SAl Viro 20589d412a43SAl Viro /* 20599d412a43SAl Viro * add a mount into a namespace's mount tree 20609d412a43SAl Viro */ 206195bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 20629d412a43SAl Viro { 206384d17192SAl Viro struct mountpoint *mp; 206484d17192SAl Viro struct mount *parent; 20659d412a43SAl Viro int err; 20669d412a43SAl Viro 2067f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 20689d412a43SAl Viro 206984d17192SAl Viro mp = lock_mount(path); 207084d17192SAl Viro if (IS_ERR(mp)) 207184d17192SAl Viro return PTR_ERR(mp); 20729d412a43SAl Viro 207384d17192SAl Viro parent = real_mount(path->mnt); 20749d412a43SAl Viro err = -EINVAL; 207584d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2076156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2077156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 20789d412a43SAl Viro goto unlock; 2079156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 208084d17192SAl Viro if (!parent->mnt_ns) 2081156cacb1SAl Viro goto unlock; 2082156cacb1SAl Viro } 20839d412a43SAl Viro 20849d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 20859d412a43SAl Viro err = -EBUSY; 208695bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 20879d412a43SAl Viro path->mnt->mnt_root == path->dentry) 20889d412a43SAl Viro goto unlock; 20899d412a43SAl Viro 20909d412a43SAl Viro err = -EINVAL; 209195bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 20929d412a43SAl Viro goto unlock; 20939d412a43SAl Viro 209495bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 209584d17192SAl Viro err = graft_tree(newmnt, parent, mp); 20969d412a43SAl Viro 20979d412a43SAl Viro unlock: 209884d17192SAl Viro unlock_mount(mp); 20999d412a43SAl Viro return err; 21009d412a43SAl Viro } 2101b1e75df4SAl Viro 21021da177e4SLinus Torvalds /* 21031da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 21041da177e4SLinus Torvalds * namespace's tree 21051da177e4SLinus Torvalds */ 21060c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 2107808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 21081da177e4SLinus Torvalds { 21090c55cfc4SEric W. Biederman struct file_system_type *type; 21109b40bc90SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 21111da177e4SLinus Torvalds struct vfsmount *mnt; 211215f9a3f3SAl Viro int err; 21131da177e4SLinus Torvalds 21140c55cfc4SEric W. Biederman if (!fstype) 21151da177e4SLinus Torvalds return -EINVAL; 21161da177e4SLinus Torvalds 21170c55cfc4SEric W. Biederman type = get_fs_type(fstype); 21180c55cfc4SEric W. Biederman if (!type) 21190c55cfc4SEric W. Biederman return -ENODEV; 21200c55cfc4SEric W. Biederman 21210c55cfc4SEric W. Biederman if (user_ns != &init_user_ns) { 21220c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_MOUNT)) { 21230c55cfc4SEric W. Biederman put_filesystem(type); 21240c55cfc4SEric W. Biederman return -EPERM; 21250c55cfc4SEric W. Biederman } 21260c55cfc4SEric W. Biederman /* Only in special cases allow devices from mounts 21270c55cfc4SEric W. Biederman * created outside the initial user namespace. 21280c55cfc4SEric W. Biederman */ 21290c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) { 21300c55cfc4SEric W. Biederman flags |= MS_NODEV; 21310c55cfc4SEric W. Biederman mnt_flags |= MNT_NODEV; 21320c55cfc4SEric W. Biederman } 21330c55cfc4SEric W. Biederman } 21340c55cfc4SEric W. Biederman 21350c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 21360c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 21370c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 21380c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 21390c55cfc4SEric W. Biederman 21400c55cfc4SEric W. Biederman put_filesystem(type); 21411da177e4SLinus Torvalds if (IS_ERR(mnt)) 21421da177e4SLinus Torvalds return PTR_ERR(mnt); 21431da177e4SLinus Torvalds 214495bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 214515f9a3f3SAl Viro if (err) 214615f9a3f3SAl Viro mntput(mnt); 214715f9a3f3SAl Viro return err; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 215019a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 215119a167afSAl Viro { 21526776db3dSAl Viro struct mount *mnt = real_mount(m); 215319a167afSAl Viro int err; 215419a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 215519a167afSAl Viro * expired before we get a chance to add it 215619a167afSAl Viro */ 21576776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 215819a167afSAl Viro 215919a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 216019a167afSAl Viro m->mnt_root == path->dentry) { 2161b1e75df4SAl Viro err = -ELOOP; 2162b1e75df4SAl Viro goto fail; 216319a167afSAl Viro } 216419a167afSAl Viro 216595bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2166b1e75df4SAl Viro if (!err) 2167b1e75df4SAl Viro return 0; 2168b1e75df4SAl Viro fail: 2169b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 21706776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 217197216be0SAl Viro namespace_lock(); 21726776db3dSAl Viro list_del_init(&mnt->mnt_expire); 217397216be0SAl Viro namespace_unlock(); 217419a167afSAl Viro } 2175b1e75df4SAl Viro mntput(m); 2176b1e75df4SAl Viro mntput(m); 217719a167afSAl Viro return err; 217819a167afSAl Viro } 217919a167afSAl Viro 2180ea5b778aSDavid Howells /** 2181ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2182ea5b778aSDavid Howells * @mnt: The mount to list. 2183ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2184ea5b778aSDavid Howells */ 2185ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2186ea5b778aSDavid Howells { 218797216be0SAl Viro namespace_lock(); 2188ea5b778aSDavid Howells 21896776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2190ea5b778aSDavid Howells 219197216be0SAl Viro namespace_unlock(); 2192ea5b778aSDavid Howells } 2193ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2194ea5b778aSDavid Howells 2195ea5b778aSDavid Howells /* 21961da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 21971da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 21981da177e4SLinus Torvalds * here 21991da177e4SLinus Torvalds */ 22001da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 22011da177e4SLinus Torvalds { 2202761d5c38SAl Viro struct mount *mnt, *next; 22031da177e4SLinus Torvalds LIST_HEAD(graveyard); 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds if (list_empty(mounts)) 22061da177e4SLinus Torvalds return; 22071da177e4SLinus Torvalds 220897216be0SAl Viro namespace_lock(); 2209719ea2fbSAl Viro lock_mount_hash(); 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 22121da177e4SLinus Torvalds * following criteria: 22131da177e4SLinus Torvalds * - only referenced by its parent vfsmount 22141da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 22151da177e4SLinus Torvalds * cleared by mntput()) 22161da177e4SLinus Torvalds */ 22176776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2218863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 22191ab59738SAl Viro propagate_mount_busy(mnt, 1)) 22201da177e4SLinus Torvalds continue; 22216776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 22221da177e4SLinus Torvalds } 2223bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 22246776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2225143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2226328e6d90SAl Viro umount_tree(mnt, 1); 2227bcc5c7d2SAl Viro } 2228719ea2fbSAl Viro unlock_mount_hash(); 22293ab6abeeSAl Viro namespace_unlock(); 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds /* 22355528f911STrond Myklebust * Ripoff of 'select_parent()' 22365528f911STrond Myklebust * 22375528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 22385528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 22395528f911STrond Myklebust */ 2240692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 22415528f911STrond Myklebust { 2242692afc31SAl Viro struct mount *this_parent = parent; 22435528f911STrond Myklebust struct list_head *next; 22445528f911STrond Myklebust int found = 0; 22455528f911STrond Myklebust 22465528f911STrond Myklebust repeat: 22476b41d536SAl Viro next = this_parent->mnt_mounts.next; 22485528f911STrond Myklebust resume: 22496b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 22505528f911STrond Myklebust struct list_head *tmp = next; 22516b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 22525528f911STrond Myklebust 22535528f911STrond Myklebust next = tmp->next; 2254692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 22555528f911STrond Myklebust continue; 22565528f911STrond Myklebust /* 22575528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 22585528f911STrond Myklebust */ 22596b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 22605528f911STrond Myklebust this_parent = mnt; 22615528f911STrond Myklebust goto repeat; 22625528f911STrond Myklebust } 22635528f911STrond Myklebust 22641ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 22656776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 22665528f911STrond Myklebust found++; 22675528f911STrond Myklebust } 22685528f911STrond Myklebust } 22695528f911STrond Myklebust /* 22705528f911STrond Myklebust * All done at this level ... ascend and resume the search 22715528f911STrond Myklebust */ 22725528f911STrond Myklebust if (this_parent != parent) { 22736b41d536SAl Viro next = this_parent->mnt_child.next; 22740714a533SAl Viro this_parent = this_parent->mnt_parent; 22755528f911STrond Myklebust goto resume; 22765528f911STrond Myklebust } 22775528f911STrond Myklebust return found; 22785528f911STrond Myklebust } 22795528f911STrond Myklebust 22805528f911STrond Myklebust /* 22815528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 22825528f911STrond Myklebust * submounts of a specific parent mountpoint 228399b7db7bSNick Piggin * 228448a066e7SAl Viro * mount_lock must be held for write 22855528f911STrond Myklebust */ 2286b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 22875528f911STrond Myklebust { 22885528f911STrond Myklebust LIST_HEAD(graveyard); 2289761d5c38SAl Viro struct mount *m; 22905528f911STrond Myklebust 22915528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2292c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2293bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2294761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 22956776db3dSAl Viro mnt_expire); 2296143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2297328e6d90SAl Viro umount_tree(m, 1); 2298bcc5c7d2SAl Viro } 2299bcc5c7d2SAl Viro } 23005528f911STrond Myklebust } 23015528f911STrond Myklebust 23025528f911STrond Myklebust /* 23031da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 23041da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 23051da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 23061da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 23071da177e4SLinus Torvalds */ 2308b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2309b58fed8bSRam Pai unsigned long n) 23101da177e4SLinus Torvalds { 23111da177e4SLinus Torvalds char *t = to; 23121da177e4SLinus Torvalds const char __user *f = from; 23131da177e4SLinus Torvalds char c; 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 23161da177e4SLinus Torvalds return n; 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds while (n) { 23191da177e4SLinus Torvalds if (__get_user(c, f)) { 23201da177e4SLinus Torvalds memset(t, 0, n); 23211da177e4SLinus Torvalds break; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds *t++ = c; 23241da177e4SLinus Torvalds f++; 23251da177e4SLinus Torvalds n--; 23261da177e4SLinus Torvalds } 23271da177e4SLinus Torvalds return n; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 23311da177e4SLinus Torvalds { 23321da177e4SLinus Torvalds int i; 23331da177e4SLinus Torvalds unsigned long page; 23341da177e4SLinus Torvalds unsigned long size; 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds *where = 0; 23371da177e4SLinus Torvalds if (!data) 23381da177e4SLinus Torvalds return 0; 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 23411da177e4SLinus Torvalds return -ENOMEM; 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds /* We only care that *some* data at the address the user 23441da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 23451da177e4SLinus Torvalds * the remainder of the page. 23461da177e4SLinus Torvalds */ 23471da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 23481da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 23491da177e4SLinus Torvalds if (size > PAGE_SIZE) 23501da177e4SLinus Torvalds size = PAGE_SIZE; 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 23531da177e4SLinus Torvalds if (!i) { 23541da177e4SLinus Torvalds free_page(page); 23551da177e4SLinus Torvalds return -EFAULT; 23561da177e4SLinus Torvalds } 23571da177e4SLinus Torvalds if (i != PAGE_SIZE) 23581da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 23591da177e4SLinus Torvalds *where = page; 23601da177e4SLinus Torvalds return 0; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 2363eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2364eca6f534SVegard Nossum { 2365eca6f534SVegard Nossum char *tmp; 2366eca6f534SVegard Nossum 2367eca6f534SVegard Nossum if (!data) { 2368eca6f534SVegard Nossum *where = NULL; 2369eca6f534SVegard Nossum return 0; 2370eca6f534SVegard Nossum } 2371eca6f534SVegard Nossum 2372eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2373eca6f534SVegard Nossum if (IS_ERR(tmp)) 2374eca6f534SVegard Nossum return PTR_ERR(tmp); 2375eca6f534SVegard Nossum 2376eca6f534SVegard Nossum *where = tmp; 2377eca6f534SVegard Nossum return 0; 2378eca6f534SVegard Nossum } 2379eca6f534SVegard Nossum 23801da177e4SLinus Torvalds /* 23811da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 23821da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 23831da177e4SLinus Torvalds * 23841da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 23851da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 23861da177e4SLinus Torvalds * information (or be NULL). 23871da177e4SLinus Torvalds * 23881da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 23891da177e4SLinus Torvalds * When the flags word was introduced its top half was required 23901da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 23911da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 23921da177e4SLinus Torvalds * and must be discarded. 23931da177e4SLinus Torvalds */ 2394808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name, 2395808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 23961da177e4SLinus Torvalds { 23972d92ab3cSAl Viro struct path path; 23981da177e4SLinus Torvalds int retval = 0; 23991da177e4SLinus Torvalds int mnt_flags = 0; 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds /* Discard magic */ 24021da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 24031da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds /* Basic sanity checks */ 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 24081da177e4SLinus Torvalds return -EINVAL; 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds if (data_page) 24111da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 24121da177e4SLinus Torvalds 2413a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2414a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2415a27ab9f2STetsuo Handa if (retval) 2416a27ab9f2STetsuo Handa return retval; 2417a27ab9f2STetsuo Handa 2418a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2419a27ab9f2STetsuo Handa type_page, flags, data_page); 24200d5cadb8SAl Viro if (!retval && !may_mount()) 24210d5cadb8SAl Viro retval = -EPERM; 2422a27ab9f2STetsuo Handa if (retval) 2423a27ab9f2STetsuo Handa goto dput_out; 2424a27ab9f2STetsuo Handa 2425613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2426613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 24270a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 24280a1c01c9SMatthew Garrett 24291da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 24301da177e4SLinus Torvalds if (flags & MS_NOSUID) 24311da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 24321da177e4SLinus Torvalds if (flags & MS_NODEV) 24331da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 24341da177e4SLinus Torvalds if (flags & MS_NOEXEC) 24351da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2436fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2437fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2438fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2439fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2440d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2441d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 24422e4b7fcdSDave Hansen if (flags & MS_RDONLY) 24432e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2444fc33a7bbSChristoph Hellwig 24457a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2446d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2447d0adde57SMatthew Garrett MS_STRICTATIME); 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds if (flags & MS_REMOUNT) 24502d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 24511da177e4SLinus Torvalds data_page); 24521da177e4SLinus Torvalds else if (flags & MS_BIND) 24532d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 24549676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 24552d92ab3cSAl Viro retval = do_change_type(&path, flags); 24561da177e4SLinus Torvalds else if (flags & MS_MOVE) 24572d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 24581da177e4SLinus Torvalds else 24592d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 24601da177e4SLinus Torvalds dev_name, data_page); 24611da177e4SLinus Torvalds dput_out: 24622d92ab3cSAl Viro path_put(&path); 24631da177e4SLinus Torvalds return retval; 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds 2466771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2467771b1371SEric W. Biederman { 246898f842e6SEric W. Biederman proc_free_inum(ns->proc_inum); 2469771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2470771b1371SEric W. Biederman kfree(ns); 2471771b1371SEric W. Biederman } 2472771b1371SEric W. Biederman 24738823c079SEric W. Biederman /* 24748823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 24758823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 24768823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 24778823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 24788823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 24798823c079SEric W. Biederman */ 24808823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 24818823c079SEric W. Biederman 2482771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2483cf8d2c11STrond Myklebust { 2484cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 248598f842e6SEric W. Biederman int ret; 2486cf8d2c11STrond Myklebust 2487cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2488cf8d2c11STrond Myklebust if (!new_ns) 2489cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 249098f842e6SEric W. Biederman ret = proc_alloc_inum(&new_ns->proc_inum); 249198f842e6SEric W. Biederman if (ret) { 249298f842e6SEric W. Biederman kfree(new_ns); 249398f842e6SEric W. Biederman return ERR_PTR(ret); 249498f842e6SEric W. Biederman } 24958823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2496cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2497cf8d2c11STrond Myklebust new_ns->root = NULL; 2498cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2499cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2500cf8d2c11STrond Myklebust new_ns->event = 0; 2501771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2502cf8d2c11STrond Myklebust return new_ns; 2503cf8d2c11STrond Myklebust } 2504cf8d2c11STrond Myklebust 25059559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 25069559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 25071da177e4SLinus Torvalds { 25086b3286edSKirill Korotaev struct mnt_namespace *new_ns; 25097f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2510315fc83eSAl Viro struct mount *p, *q; 25119559f689SAl Viro struct mount *old; 2512cb338d06SAl Viro struct mount *new; 25137a472ef4SEric W. Biederman int copy_flags; 25141da177e4SLinus Torvalds 25159559f689SAl Viro BUG_ON(!ns); 25169559f689SAl Viro 25179559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 25189559f689SAl Viro get_mnt_ns(ns); 25199559f689SAl Viro return ns; 25209559f689SAl Viro } 25219559f689SAl Viro 25229559f689SAl Viro old = ns->root; 25239559f689SAl Viro 2524771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2525cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2526cf8d2c11STrond Myklebust return new_ns; 25271da177e4SLinus Torvalds 252897216be0SAl Viro namespace_lock(); 25291da177e4SLinus Torvalds /* First pass: copy the tree topology */ 25304ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 25319559f689SAl Viro if (user_ns != ns->user_ns) 2532132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 25337a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2534be34d1a3SDavid Howells if (IS_ERR(new)) { 2535328e6d90SAl Viro namespace_unlock(); 2536771b1371SEric W. Biederman free_mnt_ns(new_ns); 2537be34d1a3SDavid Howells return ERR_CAST(new); 25381da177e4SLinus Torvalds } 2539be08d6d2SAl Viro new_ns->root = new; 25401a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds /* 25431da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 25441da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 25451da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 25461da177e4SLinus Torvalds */ 2547909b0a88SAl Viro p = old; 2548cb338d06SAl Viro q = new; 25491da177e4SLinus Torvalds while (p) { 2550143c8c91SAl Viro q->mnt_ns = new_ns; 25519559f689SAl Viro if (new_fs) { 25529559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 25539559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2554315fc83eSAl Viro rootmnt = &p->mnt; 25551da177e4SLinus Torvalds } 25569559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 25579559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2558315fc83eSAl Viro pwdmnt = &p->mnt; 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds } 2561909b0a88SAl Viro p = next_mnt(p, old); 2562909b0a88SAl Viro q = next_mnt(q, new); 25634ce5d2b1SEric W. Biederman if (!q) 25644ce5d2b1SEric W. Biederman break; 25654ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 25664ce5d2b1SEric W. Biederman p = next_mnt(p, old); 25671da177e4SLinus Torvalds } 2568328e6d90SAl Viro namespace_unlock(); 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds if (rootmnt) 2571f03c6599SAl Viro mntput(rootmnt); 25721da177e4SLinus Torvalds if (pwdmnt) 2573f03c6599SAl Viro mntput(pwdmnt); 25741da177e4SLinus Torvalds 2575741a2951SJANAK DESAI return new_ns; 2576741a2951SJANAK DESAI } 2577741a2951SJANAK DESAI 2578cf8d2c11STrond Myklebust /** 2579cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2580cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2581cf8d2c11STrond Myklebust */ 25821a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2583cf8d2c11STrond Myklebust { 2584771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2585cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 25861a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 25871a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2588be08d6d2SAl Viro new_ns->root = mnt; 2589b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2590c1334495SAl Viro } else { 25911a4eeaf2SAl Viro mntput(m); 2592cf8d2c11STrond Myklebust } 2593cf8d2c11STrond Myklebust return new_ns; 2594cf8d2c11STrond Myklebust } 2595cf8d2c11STrond Myklebust 2596ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2597ea441d11SAl Viro { 2598ea441d11SAl Viro struct mnt_namespace *ns; 2599d31da0f0SAl Viro struct super_block *s; 2600ea441d11SAl Viro struct path path; 2601ea441d11SAl Viro int err; 2602ea441d11SAl Viro 2603ea441d11SAl Viro ns = create_mnt_ns(mnt); 2604ea441d11SAl Viro if (IS_ERR(ns)) 2605ea441d11SAl Viro return ERR_CAST(ns); 2606ea441d11SAl Viro 2607ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2608ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2609ea441d11SAl Viro 2610ea441d11SAl Viro put_mnt_ns(ns); 2611ea441d11SAl Viro 2612ea441d11SAl Viro if (err) 2613ea441d11SAl Viro return ERR_PTR(err); 2614ea441d11SAl Viro 2615ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2616d31da0f0SAl Viro s = path.mnt->mnt_sb; 2617d31da0f0SAl Viro atomic_inc(&s->s_active); 2618ea441d11SAl Viro mntput(path.mnt); 2619ea441d11SAl Viro /* lock the sucker */ 2620d31da0f0SAl Viro down_write(&s->s_umount); 2621ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2622ea441d11SAl Viro return path.dentry; 2623ea441d11SAl Viro } 2624ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2625ea441d11SAl Viro 2626bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2627bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 26281da177e4SLinus Torvalds { 2629eca6f534SVegard Nossum int ret; 2630eca6f534SVegard Nossum char *kernel_type; 263191a27b2aSJeff Layton struct filename *kernel_dir; 2632eca6f534SVegard Nossum char *kernel_dev; 26331da177e4SLinus Torvalds unsigned long data_page; 26341da177e4SLinus Torvalds 2635eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2636eca6f534SVegard Nossum if (ret < 0) 2637eca6f534SVegard Nossum goto out_type; 26381da177e4SLinus Torvalds 2639eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2640eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2641eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2642eca6f534SVegard Nossum goto out_dir; 2643eca6f534SVegard Nossum } 26441da177e4SLinus Torvalds 2645eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2646eca6f534SVegard Nossum if (ret < 0) 2647eca6f534SVegard Nossum goto out_dev; 26481da177e4SLinus Torvalds 2649eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2650eca6f534SVegard Nossum if (ret < 0) 2651eca6f534SVegard Nossum goto out_data; 26521da177e4SLinus Torvalds 265391a27b2aSJeff Layton ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags, 2654eca6f534SVegard Nossum (void *) data_page); 2655eca6f534SVegard Nossum 26561da177e4SLinus Torvalds free_page(data_page); 2657eca6f534SVegard Nossum out_data: 2658eca6f534SVegard Nossum kfree(kernel_dev); 2659eca6f534SVegard Nossum out_dev: 2660eca6f534SVegard Nossum putname(kernel_dir); 2661eca6f534SVegard Nossum out_dir: 2662eca6f534SVegard Nossum kfree(kernel_type); 2663eca6f534SVegard Nossum out_type: 2664eca6f534SVegard Nossum return ret; 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds /* 2668afac7cbaSAl Viro * Return true if path is reachable from root 2669afac7cbaSAl Viro * 267048a066e7SAl Viro * namespace_sem or mount_lock is held 2671afac7cbaSAl Viro */ 2672643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2673afac7cbaSAl Viro const struct path *root) 2674afac7cbaSAl Viro { 2675643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2676a73324daSAl Viro dentry = mnt->mnt_mountpoint; 26770714a533SAl Viro mnt = mnt->mnt_parent; 2678afac7cbaSAl Viro } 2679643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2680afac7cbaSAl Viro } 2681afac7cbaSAl Viro 2682afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2683afac7cbaSAl Viro { 2684afac7cbaSAl Viro int res; 268548a066e7SAl Viro read_seqlock_excl(&mount_lock); 2686643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 268748a066e7SAl Viro read_sequnlock_excl(&mount_lock); 2688afac7cbaSAl Viro return res; 2689afac7cbaSAl Viro } 2690afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2691afac7cbaSAl Viro 2692afac7cbaSAl Viro /* 26931da177e4SLinus Torvalds * pivot_root Semantics: 26941da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 26951da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 26961da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 26971da177e4SLinus Torvalds * 26981da177e4SLinus Torvalds * Restrictions: 26991da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 27001da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 27011da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 27021da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 27031da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 27041da177e4SLinus Torvalds * 27054a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 27064a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 27074a0d11faSNeil Brown * in this situation. 27084a0d11faSNeil Brown * 27091da177e4SLinus Torvalds * Notes: 27101da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 27111da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 27121da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 27131da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 27141da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 27151da177e4SLinus Torvalds * first. 27161da177e4SLinus Torvalds */ 27173480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 27183480b257SHeiko Carstens const char __user *, put_old) 27191da177e4SLinus Torvalds { 27202d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 272184d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 272284d17192SAl Viro struct mountpoint *old_mp, *root_mp; 27231da177e4SLinus Torvalds int error; 27241da177e4SLinus Torvalds 27259b40bc90SAl Viro if (!may_mount()) 27261da177e4SLinus Torvalds return -EPERM; 27271da177e4SLinus Torvalds 27282d8f3038SAl Viro error = user_path_dir(new_root, &new); 27291da177e4SLinus Torvalds if (error) 27301da177e4SLinus Torvalds goto out0; 27311da177e4SLinus Torvalds 27322d8f3038SAl Viro error = user_path_dir(put_old, &old); 27331da177e4SLinus Torvalds if (error) 27341da177e4SLinus Torvalds goto out1; 27351da177e4SLinus Torvalds 27362d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2737b12cea91SAl Viro if (error) 2738b12cea91SAl Viro goto out2; 27391da177e4SLinus Torvalds 2740f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 274184d17192SAl Viro old_mp = lock_mount(&old); 274284d17192SAl Viro error = PTR_ERR(old_mp); 274384d17192SAl Viro if (IS_ERR(old_mp)) 2744b12cea91SAl Viro goto out3; 2745b12cea91SAl Viro 27461da177e4SLinus Torvalds error = -EINVAL; 2747419148daSAl Viro new_mnt = real_mount(new.mnt); 2748419148daSAl Viro root_mnt = real_mount(root.mnt); 274984d17192SAl Viro old_mnt = real_mount(old.mnt); 275084d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 2751fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2752fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2753b12cea91SAl Viro goto out4; 2754143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2755b12cea91SAl Viro goto out4; 27565ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 27575ff9d8a6SEric W. Biederman goto out4; 27581da177e4SLinus Torvalds error = -ENOENT; 2759f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2760b12cea91SAl Viro goto out4; 27611da177e4SLinus Torvalds error = -EBUSY; 276284d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 2763b12cea91SAl Viro goto out4; /* loop, on the same file system */ 27641da177e4SLinus Torvalds error = -EINVAL; 27658c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2766b12cea91SAl Viro goto out4; /* not a mountpoint */ 2767676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2768b12cea91SAl Viro goto out4; /* not attached */ 276984d17192SAl Viro root_mp = root_mnt->mnt_mp; 27702d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2771b12cea91SAl Viro goto out4; /* not a mountpoint */ 2772676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2773b12cea91SAl Viro goto out4; /* not attached */ 27744ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 277584d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 2776b12cea91SAl Viro goto out4; 277784d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 2778719ea2fbSAl Viro lock_mount_hash(); 2779419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2780419148daSAl Viro detach_mnt(root_mnt, &root_parent); 27815ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 27825ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 27835ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 27845ff9d8a6SEric W. Biederman } 27854ac91378SJan Blunck /* mount old root on put_old */ 278684d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 27874ac91378SJan Blunck /* mount new_root on / */ 278884d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 27896b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2790719ea2fbSAl Viro unlock_mount_hash(); 27912d8f3038SAl Viro chroot_fs_refs(&root, &new); 279284d17192SAl Viro put_mountpoint(root_mp); 27931da177e4SLinus Torvalds error = 0; 2794b12cea91SAl Viro out4: 279584d17192SAl Viro unlock_mount(old_mp); 2796b12cea91SAl Viro if (!error) { 27971a390689SAl Viro path_put(&root_parent); 27981a390689SAl Viro path_put(&parent_path); 2799b12cea91SAl Viro } 2800b12cea91SAl Viro out3: 28018c3ee42eSAl Viro path_put(&root); 2802b12cea91SAl Viro out2: 28032d8f3038SAl Viro path_put(&old); 28041da177e4SLinus Torvalds out1: 28052d8f3038SAl Viro path_put(&new); 28061da177e4SLinus Torvalds out0: 28071da177e4SLinus Torvalds return error; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds static void __init init_mount_tree(void) 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds struct vfsmount *mnt; 28136b3286edSKirill Korotaev struct mnt_namespace *ns; 2814ac748a09SJan Blunck struct path root; 28150c55cfc4SEric W. Biederman struct file_system_type *type; 28161da177e4SLinus Torvalds 28170c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 28180c55cfc4SEric W. Biederman if (!type) 28190c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 28200c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 28210c55cfc4SEric W. Biederman put_filesystem(type); 28221da177e4SLinus Torvalds if (IS_ERR(mnt)) 28231da177e4SLinus Torvalds panic("Can't create rootfs"); 2824b3e19d92SNick Piggin 28253b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 28263b22edc5STrond Myklebust if (IS_ERR(ns)) 28271da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 28281da177e4SLinus Torvalds 28296b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 28306b3286edSKirill Korotaev get_mnt_ns(ns); 28311da177e4SLinus Torvalds 2832be08d6d2SAl Viro root.mnt = mnt; 2833be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2834ac748a09SJan Blunck 2835ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2836ac748a09SJan Blunck set_fs_root(current->fs, &root); 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 283974bf17cfSDenis Cheng void __init mnt_init(void) 28401da177e4SLinus Torvalds { 284113f14b4dSEric Dumazet unsigned u; 284215a67dd8SRandy Dunlap int err; 28431da177e4SLinus Torvalds 28447d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 284520c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 28461da177e4SLinus Torvalds 28470818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 284838129a13SAl Viro sizeof(struct hlist_head), 28490818bf27SAl Viro mhash_entries, 19, 28500818bf27SAl Viro 0, 28510818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 28520818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 28530818bf27SAl Viro sizeof(struct hlist_head), 28540818bf27SAl Viro mphash_entries, 19, 28550818bf27SAl Viro 0, 28560818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 28571da177e4SLinus Torvalds 285884d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 28591da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 28601da177e4SLinus Torvalds 28610818bf27SAl Viro for (u = 0; u <= m_hash_mask; u++) 286238129a13SAl Viro INIT_HLIST_HEAD(&mount_hashtable[u]); 28630818bf27SAl Viro for (u = 0; u <= mp_hash_mask; u++) 28640818bf27SAl Viro INIT_HLIST_HEAD(&mountpoint_hashtable[u]); 28651da177e4SLinus Torvalds 28664b93dc9bSTejun Heo kernfs_init(); 28674b93dc9bSTejun Heo 286815a67dd8SRandy Dunlap err = sysfs_init(); 286915a67dd8SRandy Dunlap if (err) 287015a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 28718e24eea7SHarvey Harrison __func__, err); 287200d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 287300d26666SGreg Kroah-Hartman if (!fs_kobj) 28748e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 28751da177e4SLinus Torvalds init_rootfs(); 28761da177e4SLinus Torvalds init_mount_tree(); 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds 2879616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 28801da177e4SLinus Torvalds { 2881d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2882616511d0STrond Myklebust return; 28837b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 2884771b1371SEric W. Biederman free_mnt_ns(ns); 28851da177e4SLinus Torvalds } 28869d412a43SAl Viro 28879d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 28889d412a43SAl Viro { 2889423e0ab0STim Chen struct vfsmount *mnt; 2890423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2891423e0ab0STim Chen if (!IS_ERR(mnt)) { 2892423e0ab0STim Chen /* 2893423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2894423e0ab0STim Chen * we unmount before file sys is unregistered 2895423e0ab0STim Chen */ 2896f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2897423e0ab0STim Chen } 2898423e0ab0STim Chen return mnt; 28999d412a43SAl Viro } 29009d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2901423e0ab0STim Chen 2902423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2903423e0ab0STim Chen { 2904423e0ab0STim Chen /* release long term mount so mount point can be released */ 2905423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2906f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 290748a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 2908423e0ab0STim Chen mntput(mnt); 2909423e0ab0STim Chen } 2910423e0ab0STim Chen } 2911423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 291202125a82SAl Viro 291302125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 291402125a82SAl Viro { 2915143c8c91SAl Viro return check_mnt(real_mount(mnt)); 291602125a82SAl Viro } 29178823c079SEric W. Biederman 29183151527eSEric W. Biederman bool current_chrooted(void) 29193151527eSEric W. Biederman { 29203151527eSEric W. Biederman /* Does the current process have a non-standard root */ 29213151527eSEric W. Biederman struct path ns_root; 29223151527eSEric W. Biederman struct path fs_root; 29233151527eSEric W. Biederman bool chrooted; 29243151527eSEric W. Biederman 29253151527eSEric W. Biederman /* Find the namespace root */ 29263151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 29273151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 29283151527eSEric W. Biederman path_get(&ns_root); 29293151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 29303151527eSEric W. Biederman ; 29313151527eSEric W. Biederman 29323151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 29333151527eSEric W. Biederman 29343151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 29353151527eSEric W. Biederman 29363151527eSEric W. Biederman path_put(&fs_root); 29373151527eSEric W. Biederman path_put(&ns_root); 29383151527eSEric W. Biederman 29393151527eSEric W. Biederman return chrooted; 29403151527eSEric W. Biederman } 29413151527eSEric W. Biederman 2942e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type) 294387a8ebd6SEric W. Biederman { 294487a8ebd6SEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 294587a8ebd6SEric W. Biederman struct mount *mnt; 2946e51db735SEric W. Biederman bool visible = false; 294787a8ebd6SEric W. Biederman 2948e51db735SEric W. Biederman if (unlikely(!ns)) 2949e51db735SEric W. Biederman return false; 2950e51db735SEric W. Biederman 295144bb4385SAl Viro down_read(&namespace_sem); 295287a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 2953e51db735SEric W. Biederman struct mount *child; 2954e51db735SEric W. Biederman if (mnt->mnt.mnt_sb->s_type != type) 2955e51db735SEric W. Biederman continue; 2956e51db735SEric W. Biederman 2957e51db735SEric W. Biederman /* This mount is not fully visible if there are any child mounts 2958e51db735SEric W. Biederman * that cover anything except for empty directories. 2959e51db735SEric W. Biederman */ 2960e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 2961e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 2962e51db735SEric W. Biederman if (!S_ISDIR(inode->i_mode)) 2963e51db735SEric W. Biederman goto next; 296441301ae7SEric W. Biederman if (inode->i_nlink > 2) 2965e51db735SEric W. Biederman goto next; 296687a8ebd6SEric W. Biederman } 2967e51db735SEric W. Biederman visible = true; 2968e51db735SEric W. Biederman goto found; 2969e51db735SEric W. Biederman next: ; 297087a8ebd6SEric W. Biederman } 2971e51db735SEric W. Biederman found: 297244bb4385SAl Viro up_read(&namespace_sem); 2973e51db735SEric W. Biederman return visible; 297487a8ebd6SEric W. Biederman } 297587a8ebd6SEric W. Biederman 29768823c079SEric W. Biederman static void *mntns_get(struct task_struct *task) 29778823c079SEric W. Biederman { 29788823c079SEric W. Biederman struct mnt_namespace *ns = NULL; 29798823c079SEric W. Biederman struct nsproxy *nsproxy; 29808823c079SEric W. Biederman 29818823c079SEric W. Biederman rcu_read_lock(); 29828823c079SEric W. Biederman nsproxy = task_nsproxy(task); 29838823c079SEric W. Biederman if (nsproxy) { 29848823c079SEric W. Biederman ns = nsproxy->mnt_ns; 29858823c079SEric W. Biederman get_mnt_ns(ns); 29868823c079SEric W. Biederman } 29878823c079SEric W. Biederman rcu_read_unlock(); 29888823c079SEric W. Biederman 29898823c079SEric W. Biederman return ns; 29908823c079SEric W. Biederman } 29918823c079SEric W. Biederman 29928823c079SEric W. Biederman static void mntns_put(void *ns) 29938823c079SEric W. Biederman { 29948823c079SEric W. Biederman put_mnt_ns(ns); 29958823c079SEric W. Biederman } 29968823c079SEric W. Biederman 29978823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns) 29988823c079SEric W. Biederman { 29998823c079SEric W. Biederman struct fs_struct *fs = current->fs; 30008823c079SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 30018823c079SEric W. Biederman struct path root; 30028823c079SEric W. Biederman 30030c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3004c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3005c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3006ae11e0f1SZhao Hongjiang return -EPERM; 30078823c079SEric W. Biederman 30088823c079SEric W. Biederman if (fs->users != 1) 30098823c079SEric W. Biederman return -EINVAL; 30108823c079SEric W. Biederman 30118823c079SEric W. Biederman get_mnt_ns(mnt_ns); 30128823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 30138823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 30148823c079SEric W. Biederman 30158823c079SEric W. Biederman /* Find the root */ 30168823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 30178823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 30188823c079SEric W. Biederman path_get(&root); 30198823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 30208823c079SEric W. Biederman ; 30218823c079SEric W. Biederman 30228823c079SEric W. Biederman /* Update the pwd and root */ 30238823c079SEric W. Biederman set_fs_pwd(fs, &root); 30248823c079SEric W. Biederman set_fs_root(fs, &root); 30258823c079SEric W. Biederman 30268823c079SEric W. Biederman path_put(&root); 30278823c079SEric W. Biederman return 0; 30288823c079SEric W. Biederman } 30298823c079SEric W. Biederman 303098f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns) 303198f842e6SEric W. Biederman { 303298f842e6SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 303398f842e6SEric W. Biederman return mnt_ns->proc_inum; 303498f842e6SEric W. Biederman } 303598f842e6SEric W. Biederman 30368823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 30378823c079SEric W. Biederman .name = "mnt", 30388823c079SEric W. Biederman .type = CLONE_NEWNS, 30398823c079SEric W. Biederman .get = mntns_get, 30408823c079SEric W. Biederman .put = mntns_put, 30418823c079SEric W. Biederman .install = mntns_install, 304298f842e6SEric W. Biederman .inum = mntns_inum, 30438823c079SEric W. Biederman }; 3044