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 555addc5ddSAl Viro static int 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 62fa3536ccSEric Dumazet static struct list_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 810818bf27SAl Viro static inline struct list_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 220c63181e6SAl Viro INIT_LIST_HEAD(&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 { 6080818bf27SAl Viro struct list_head *head = m_hash(mnt, dentry); 609474279dcSAl Viro struct mount *p; 6101da177e4SLinus Torvalds 61148a066e7SAl Viro list_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 { 6230818bf27SAl Viro struct list_head *head = m_hash(mnt, dentry); 624474279dcSAl Viro struct mount *p; 625474279dcSAl Viro 626474279dcSAl Viro list_for_each_entry_reverse(p, head, mnt_hash) 627474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 628474279dcSAl Viro return p; 629474279dcSAl Viro return NULL; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 632a05964f3SRam Pai /* 633f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 634f015f126SDavid Howells * 635f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 636f015f126SDavid Howells * following mounts: 637f015f126SDavid Howells * 638f015f126SDavid Howells * mount /dev/sda1 /mnt 639f015f126SDavid Howells * mount /dev/sda2 /mnt 640f015f126SDavid Howells * mount /dev/sda3 /mnt 641f015f126SDavid Howells * 642f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 643f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 644f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 645f015f126SDavid Howells * 646f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 647a05964f3SRam Pai */ 6481c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 649a05964f3SRam Pai { 650c7105365SAl Viro struct mount *child_mnt; 65148a066e7SAl Viro struct vfsmount *m; 65248a066e7SAl Viro unsigned seq; 65399b7db7bSNick Piggin 65448a066e7SAl Viro rcu_read_lock(); 65548a066e7SAl Viro do { 65648a066e7SAl Viro seq = read_seqbegin(&mount_lock); 657474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 65848a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 65948a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 66048a066e7SAl Viro rcu_read_unlock(); 66148a066e7SAl Viro return m; 662a05964f3SRam Pai } 663a05964f3SRam Pai 66484d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry) 66584d17192SAl Viro { 6660818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 66784d17192SAl Viro struct mountpoint *mp; 668eed81007SMiklos Szeredi int ret; 66984d17192SAl Viro 6700818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 67184d17192SAl Viro if (mp->m_dentry == dentry) { 67284d17192SAl Viro /* might be worth a WARN_ON() */ 67384d17192SAl Viro if (d_unlinked(dentry)) 67484d17192SAl Viro return ERR_PTR(-ENOENT); 67584d17192SAl Viro mp->m_count++; 67684d17192SAl Viro return mp; 67784d17192SAl Viro } 67884d17192SAl Viro } 67984d17192SAl Viro 68084d17192SAl Viro mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 68184d17192SAl Viro if (!mp) 68284d17192SAl Viro return ERR_PTR(-ENOMEM); 68384d17192SAl Viro 684eed81007SMiklos Szeredi ret = d_set_mounted(dentry); 685eed81007SMiklos Szeredi if (ret) { 68684d17192SAl Viro kfree(mp); 687eed81007SMiklos Szeredi return ERR_PTR(ret); 68884d17192SAl Viro } 689eed81007SMiklos Szeredi 69084d17192SAl Viro mp->m_dentry = dentry; 69184d17192SAl Viro mp->m_count = 1; 6920818bf27SAl Viro hlist_add_head(&mp->m_hash, chain); 69384d17192SAl Viro return mp; 69484d17192SAl Viro } 69584d17192SAl Viro 69684d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 69784d17192SAl Viro { 69884d17192SAl Viro if (!--mp->m_count) { 69984d17192SAl Viro struct dentry *dentry = mp->m_dentry; 70084d17192SAl Viro spin_lock(&dentry->d_lock); 70184d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 70284d17192SAl Viro spin_unlock(&dentry->d_lock); 7030818bf27SAl Viro hlist_del(&mp->m_hash); 70484d17192SAl Viro kfree(mp); 70584d17192SAl Viro } 70684d17192SAl Viro } 70784d17192SAl Viro 708143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7091da177e4SLinus Torvalds { 7106b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 71399b7db7bSNick Piggin /* 71499b7db7bSNick Piggin * vfsmount lock must be held for write 71599b7db7bSNick Piggin */ 7166b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7175addc5ddSAl Viro { 7185addc5ddSAl Viro if (ns) { 7195addc5ddSAl Viro ns->event = ++event; 7205addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7215addc5ddSAl Viro } 7225addc5ddSAl Viro } 7235addc5ddSAl Viro 72499b7db7bSNick Piggin /* 72599b7db7bSNick Piggin * vfsmount lock must be held for write 72699b7db7bSNick Piggin */ 7276b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7285addc5ddSAl Viro { 7295addc5ddSAl Viro if (ns && ns->event != event) { 7305addc5ddSAl Viro ns->event = event; 7315addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7325addc5ddSAl Viro } 7335addc5ddSAl Viro } 7345addc5ddSAl Viro 73599b7db7bSNick Piggin /* 73699b7db7bSNick Piggin * vfsmount lock must be held for write 73799b7db7bSNick Piggin */ 738419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 7391da177e4SLinus Torvalds { 740a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 7410714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 7420714a533SAl Viro mnt->mnt_parent = mnt; 743a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7446b41d536SAl Viro list_del_init(&mnt->mnt_child); 7451b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 74684d17192SAl Viro put_mountpoint(mnt->mnt_mp); 74784d17192SAl Viro mnt->mnt_mp = NULL; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 75099b7db7bSNick Piggin /* 75199b7db7bSNick Piggin * vfsmount lock must be held for write 75299b7db7bSNick Piggin */ 75384d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 75484d17192SAl Viro struct mountpoint *mp, 75544d964d6SAl Viro struct mount *child_mnt) 756b90fa9aeSRam Pai { 75784d17192SAl Viro mp->m_count++; 7583a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 75984d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 7603a2393d7SAl Viro child_mnt->mnt_parent = mnt; 76184d17192SAl Viro child_mnt->mnt_mp = mp; 762b90fa9aeSRam Pai } 763b90fa9aeSRam Pai 76499b7db7bSNick Piggin /* 76599b7db7bSNick Piggin * vfsmount lock must be held for write 76699b7db7bSNick Piggin */ 76784d17192SAl Viro static void attach_mnt(struct mount *mnt, 76884d17192SAl Viro struct mount *parent, 76984d17192SAl Viro struct mountpoint *mp) 7701da177e4SLinus Torvalds { 77184d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 7720818bf27SAl Viro list_add_tail(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry)); 77384d17192SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 774b90fa9aeSRam Pai } 775b90fa9aeSRam Pai 776b90fa9aeSRam Pai /* 77799b7db7bSNick Piggin * vfsmount lock must be held for write 778b90fa9aeSRam Pai */ 7794b2619a5SAl Viro static void commit_tree(struct mount *mnt) 780b90fa9aeSRam Pai { 7810714a533SAl Viro struct mount *parent = mnt->mnt_parent; 78283adc753SAl Viro struct mount *m; 783b90fa9aeSRam Pai LIST_HEAD(head); 784143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 785b90fa9aeSRam Pai 7860714a533SAl Viro BUG_ON(parent == mnt); 787b90fa9aeSRam Pai 7881a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 789f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 790143c8c91SAl Viro m->mnt_ns = n; 791f03c6599SAl Viro 792b90fa9aeSRam Pai list_splice(&head, n->list.prev); 793b90fa9aeSRam Pai 7940818bf27SAl Viro list_add_tail(&mnt->mnt_hash, 7950818bf27SAl Viro m_hash(&parent->mnt, mnt->mnt_mountpoint)); 7966b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 7976b3286edSKirill Korotaev touch_mnt_namespace(n); 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 800909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 8011da177e4SLinus Torvalds { 8026b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 8036b41d536SAl Viro if (next == &p->mnt_mounts) { 8041da177e4SLinus Torvalds while (1) { 805909b0a88SAl Viro if (p == root) 8061da177e4SLinus Torvalds return NULL; 8076b41d536SAl Viro next = p->mnt_child.next; 8086b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 8091da177e4SLinus Torvalds break; 8100714a533SAl Viro p = p->mnt_parent; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds } 8136b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 816315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 8179676f0c6SRam Pai { 8186b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 8196b41d536SAl Viro while (prev != &p->mnt_mounts) { 8206b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 8216b41d536SAl Viro prev = p->mnt_mounts.prev; 8229676f0c6SRam Pai } 8239676f0c6SRam Pai return p; 8249676f0c6SRam Pai } 8259676f0c6SRam Pai 8269d412a43SAl Viro struct vfsmount * 8279d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 8289d412a43SAl Viro { 829b105e270SAl Viro struct mount *mnt; 8309d412a43SAl Viro struct dentry *root; 8319d412a43SAl Viro 8329d412a43SAl Viro if (!type) 8339d412a43SAl Viro return ERR_PTR(-ENODEV); 8349d412a43SAl Viro 8359d412a43SAl Viro mnt = alloc_vfsmnt(name); 8369d412a43SAl Viro if (!mnt) 8379d412a43SAl Viro return ERR_PTR(-ENOMEM); 8389d412a43SAl Viro 8399d412a43SAl Viro if (flags & MS_KERNMOUNT) 840b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 8419d412a43SAl Viro 8429d412a43SAl Viro root = mount_fs(type, flags, name, data); 8439d412a43SAl Viro if (IS_ERR(root)) { 8449d412a43SAl Viro free_vfsmnt(mnt); 8459d412a43SAl Viro return ERR_CAST(root); 8469d412a43SAl Viro } 8479d412a43SAl Viro 848b105e270SAl Viro mnt->mnt.mnt_root = root; 849b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 850a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8510714a533SAl Viro mnt->mnt_parent = mnt; 852719ea2fbSAl Viro lock_mount_hash(); 85339f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 854719ea2fbSAl Viro unlock_mount_hash(); 855b105e270SAl Viro return &mnt->mnt; 8569d412a43SAl Viro } 8579d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 8589d412a43SAl Viro 85987129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 86036341f64SRam Pai int flag) 8611da177e4SLinus Torvalds { 86287129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 863be34d1a3SDavid Howells struct mount *mnt; 864be34d1a3SDavid Howells int err; 8651da177e4SLinus Torvalds 866be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 867be34d1a3SDavid Howells if (!mnt) 868be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 869be34d1a3SDavid Howells 8707a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 87115169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 872719f5d7fSMiklos Szeredi else 87315169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 874719f5d7fSMiklos Szeredi 87515169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 876be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 877719f5d7fSMiklos Szeredi if (err) 878719f5d7fSMiklos Szeredi goto out_free; 879719f5d7fSMiklos Szeredi } 880719f5d7fSMiklos Szeredi 88187129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 882132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 883132c94e3SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY)) 884132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 885132c94e3SEric W. Biederman 8865ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 8875ff9d8a6SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire)) 8885ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 8895ff9d8a6SEric W. Biederman 8901da177e4SLinus Torvalds atomic_inc(&sb->s_active); 891b105e270SAl Viro mnt->mnt.mnt_sb = sb; 892b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 893a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8940714a533SAl Viro mnt->mnt_parent = mnt; 895719ea2fbSAl Viro lock_mount_hash(); 89639f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 897719ea2fbSAl Viro unlock_mount_hash(); 898b90fa9aeSRam Pai 8997a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 9007a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 9016776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 90232301920SAl Viro mnt->mnt_master = old; 903fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 9048aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 905fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 9066776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 907d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 9086776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 909d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 9105afe0022SRam Pai } 911b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 9120f0afb1dSAl Viro set_mnt_shared(mnt); 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 9151da177e4SLinus Torvalds * as the original if that was on one */ 91636341f64SRam Pai if (flag & CL_EXPIRE) { 9176776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 9186776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 9191da177e4SLinus Torvalds } 920be34d1a3SDavid Howells 921cb338d06SAl Viro return mnt; 922719f5d7fSMiklos Szeredi 923719f5d7fSMiklos Szeredi out_free: 924719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 925be34d1a3SDavid Howells return ERR_PTR(err); 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 92848a066e7SAl Viro static void delayed_free(struct rcu_head *head) 92948a066e7SAl Viro { 93048a066e7SAl Viro struct mount *mnt = container_of(head, struct mount, mnt_rcu); 93148a066e7SAl Viro kfree(mnt->mnt_devname); 93248a066e7SAl Viro #ifdef CONFIG_SMP 93348a066e7SAl Viro free_percpu(mnt->mnt_pcp); 93448a066e7SAl Viro #endif 93548a066e7SAl Viro kmem_cache_free(mnt_cache, mnt); 93648a066e7SAl Viro } 93748a066e7SAl Viro 938900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 9397b7b1aceSAl Viro { 940b3e19d92SNick Piggin put_again: 94148a066e7SAl Viro rcu_read_lock(); 942aa9c0e07SAl Viro mnt_add_count(mnt, -1); 94348a066e7SAl Viro if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */ 94448a066e7SAl Viro rcu_read_unlock(); 94599b7db7bSNick Piggin return; 946b3e19d92SNick Piggin } 947719ea2fbSAl Viro lock_mount_hash(); 948b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 94948a066e7SAl Viro rcu_read_unlock(); 950719ea2fbSAl Viro unlock_mount_hash(); 95199b7db7bSNick Piggin return; 95299b7db7bSNick Piggin } 953863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 954863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 955863d684fSAl Viro mnt->mnt_pinned = 0; 95648a066e7SAl Viro rcu_read_unlock(); 957719ea2fbSAl Viro unlock_mount_hash(); 958900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 959b3e19d92SNick Piggin goto put_again; 960b3e19d92SNick Piggin } 96148a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 96248a066e7SAl Viro rcu_read_unlock(); 96348a066e7SAl Viro unlock_mount_hash(); 96448a066e7SAl Viro return; 96548a066e7SAl Viro } 96648a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 96748a066e7SAl Viro rcu_read_unlock(); 968962830dfSAndi Kleen 96939f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 970719ea2fbSAl Viro unlock_mount_hash(); 971649a795aSAl Viro 972649a795aSAl Viro /* 973649a795aSAl Viro * This probably indicates that somebody messed 974649a795aSAl Viro * up a mnt_want/drop_write() pair. If this 975649a795aSAl Viro * happens, the filesystem was probably unable 976649a795aSAl Viro * to make r/w->r/o transitions. 977649a795aSAl Viro */ 978649a795aSAl Viro /* 979649a795aSAl Viro * The locking used to deal with mnt_count decrement provides barriers, 980649a795aSAl Viro * so mnt_get_writers() below is safe. 981649a795aSAl Viro */ 982649a795aSAl Viro WARN_ON(mnt_get_writers(mnt)); 983649a795aSAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 984649a795aSAl Viro dput(mnt->mnt.mnt_root); 985649a795aSAl Viro deactivate_super(mnt->mnt.mnt_sb); 98648a066e7SAl Viro mnt_free_id(mnt); 98748a066e7SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free); 988b3e19d92SNick Piggin } 989b3e19d92SNick Piggin 990b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 991b3e19d92SNick Piggin { 992b3e19d92SNick Piggin if (mnt) { 993863d684fSAl Viro struct mount *m = real_mount(mnt); 994b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 995863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 996863d684fSAl Viro m->mnt_expiry_mark = 0; 997863d684fSAl Viro mntput_no_expire(m); 998b3e19d92SNick Piggin } 999b3e19d92SNick Piggin } 1000b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1001b3e19d92SNick Piggin 1002b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1003b3e19d92SNick Piggin { 1004b3e19d92SNick Piggin if (mnt) 100583adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1006b3e19d92SNick Piggin return mnt; 1007b3e19d92SNick Piggin } 1008b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1009b3e19d92SNick Piggin 10107b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 10117b7b1aceSAl Viro { 1012719ea2fbSAl Viro lock_mount_hash(); 1013863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 1014719ea2fbSAl Viro unlock_mount_hash(); 10157b7b1aceSAl Viro } 10167b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 10177b7b1aceSAl Viro 1018863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 10197b7b1aceSAl Viro { 1020863d684fSAl Viro struct mount *mnt = real_mount(m); 1021719ea2fbSAl Viro lock_mount_hash(); 10227b7b1aceSAl Viro if (mnt->mnt_pinned) { 1023863d684fSAl Viro mnt_add_count(mnt, 1); 10247b7b1aceSAl Viro mnt->mnt_pinned--; 10257b7b1aceSAl Viro } 1026719ea2fbSAl Viro unlock_mount_hash(); 10277b7b1aceSAl Viro } 10287b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 10291da177e4SLinus Torvalds 1030b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 1031b3b304a2SMiklos Szeredi { 1032b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 1033b3b304a2SMiklos Szeredi } 1034b3b304a2SMiklos Szeredi 1035b3b304a2SMiklos Szeredi /* 1036b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 1037b3b304a2SMiklos Szeredi * implement more complex mount option showing. 1038b3b304a2SMiklos Szeredi * 1039b3b304a2SMiklos Szeredi * See also save_mount_options(). 1040b3b304a2SMiklos Szeredi */ 104134c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 1042b3b304a2SMiklos Szeredi { 10432a32cebdSAl Viro const char *options; 10442a32cebdSAl Viro 10452a32cebdSAl Viro rcu_read_lock(); 104634c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 1047b3b304a2SMiklos Szeredi 1048b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 1049b3b304a2SMiklos Szeredi seq_putc(m, ','); 1050b3b304a2SMiklos Szeredi mangle(m, options); 1051b3b304a2SMiklos Szeredi } 10522a32cebdSAl Viro rcu_read_unlock(); 1053b3b304a2SMiklos Szeredi 1054b3b304a2SMiklos Szeredi return 0; 1055b3b304a2SMiklos Szeredi } 1056b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 1057b3b304a2SMiklos Szeredi 1058b3b304a2SMiklos Szeredi /* 1059b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 1060b3b304a2SMiklos Szeredi * called from the fill_super() callback. 1061b3b304a2SMiklos Szeredi * 1062b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 1063b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 1064b3b304a2SMiklos Szeredi * remount fails. 1065b3b304a2SMiklos Szeredi * 1066b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 1067b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 1068b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 1069b3b304a2SMiklos Szeredi * any more. 1070b3b304a2SMiklos Szeredi */ 1071b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 1072b3b304a2SMiklos Szeredi { 10732a32cebdSAl Viro BUG_ON(sb->s_options); 10742a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 1075b3b304a2SMiklos Szeredi } 1076b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 1077b3b304a2SMiklos Szeredi 10782a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 10792a32cebdSAl Viro { 10802a32cebdSAl Viro char *old = sb->s_options; 10812a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 10822a32cebdSAl Viro if (old) { 10832a32cebdSAl Viro synchronize_rcu(); 10842a32cebdSAl Viro kfree(old); 10852a32cebdSAl Viro } 10862a32cebdSAl Viro } 10872a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 10882a32cebdSAl Viro 1089a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 10900226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 10911da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 10921da177e4SLinus Torvalds { 10936ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10941da177e4SLinus Torvalds 1095390c6843SRam Pai down_read(&namespace_sem); 1096a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 11001da177e4SLinus Torvalds { 11016ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1102b0765fb8SPavel Emelianov 1103a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 11071da177e4SLinus Torvalds { 1108390c6843SRam Pai up_read(&namespace_sem); 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11110226f492SAl Viro static int m_show(struct seq_file *m, void *v) 11129f5596afSAl Viro { 11136ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 11141a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 11150226f492SAl Viro return p->show(m, &r->mnt); 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 1118a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 11191da177e4SLinus Torvalds .start = m_start, 11201da177e4SLinus Torvalds .next = m_next, 11211da177e4SLinus Torvalds .stop = m_stop, 11220226f492SAl Viro .show = m_show, 1123b4629fe2SChuck Lever }; 1124a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1125b4629fe2SChuck Lever 11261da177e4SLinus Torvalds /** 11271da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 11281da177e4SLinus Torvalds * @mnt: root of mount tree 11291da177e4SLinus Torvalds * 11301da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 11311da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 11321da177e4SLinus Torvalds * busy. 11331da177e4SLinus Torvalds */ 1134909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 11351da177e4SLinus Torvalds { 1136909b0a88SAl Viro struct mount *mnt = real_mount(m); 113736341f64SRam Pai int actual_refs = 0; 113836341f64SRam Pai int minimum_refs = 0; 1139315fc83eSAl Viro struct mount *p; 1140909b0a88SAl Viro BUG_ON(!m); 11411da177e4SLinus Torvalds 1142b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1143719ea2fbSAl Viro lock_mount_hash(); 1144909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 114583adc753SAl Viro actual_refs += mnt_get_count(p); 11461da177e4SLinus Torvalds minimum_refs += 2; 11471da177e4SLinus Torvalds } 1148719ea2fbSAl Viro unlock_mount_hash(); 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds if (actual_refs > minimum_refs) 11511da177e4SLinus Torvalds return 0; 1152e3474a8eSIan Kent 1153e3474a8eSIan Kent return 1; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds /** 11591da177e4SLinus Torvalds * may_umount - check if a mount point is busy 11601da177e4SLinus Torvalds * @mnt: root of mount 11611da177e4SLinus Torvalds * 11621da177e4SLinus Torvalds * This is called to check if a mount point has any 11631da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 11641da177e4SLinus Torvalds * mount has sub mounts this will return busy 11651da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 11661da177e4SLinus Torvalds * 11671da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 11681da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 11691da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 11701da177e4SLinus Torvalds */ 11711da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 11721da177e4SLinus Torvalds { 1173e3474a8eSIan Kent int ret = 1; 11748ad08d8aSAl Viro down_read(&namespace_sem); 1175719ea2fbSAl Viro lock_mount_hash(); 11761ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1177e3474a8eSIan Kent ret = 0; 1178719ea2fbSAl Viro unlock_mount_hash(); 11798ad08d8aSAl Viro up_read(&namespace_sem); 1180a05964f3SRam Pai return ret; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 11841da177e4SLinus Torvalds 1185e3197d83SAl Viro static LIST_HEAD(unmounted); /* protected by namespace_sem */ 1186e3197d83SAl Viro 118797216be0SAl Viro static void namespace_unlock(void) 11881da177e4SLinus Torvalds { 1189d5e50f74SAl Viro struct mount *mnt; 119097216be0SAl Viro LIST_HEAD(head); 119197216be0SAl Viro 119297216be0SAl Viro if (likely(list_empty(&unmounted))) { 119397216be0SAl Viro up_write(&namespace_sem); 119497216be0SAl Viro return; 119597216be0SAl Viro } 119697216be0SAl Viro 119797216be0SAl Viro list_splice_init(&unmounted, &head); 119897216be0SAl Viro up_write(&namespace_sem); 119997216be0SAl Viro 120048a066e7SAl Viro synchronize_rcu(); 120148a066e7SAl Viro 120297216be0SAl Viro while (!list_empty(&head)) { 120397216be0SAl Viro mnt = list_first_entry(&head, struct mount, mnt_hash); 12041b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 1205aba809cfSAl Viro if (mnt->mnt_ex_mountpoint.mnt) 1206aba809cfSAl Viro path_put(&mnt->mnt_ex_mountpoint); 1207d5e50f74SAl Viro mntput(&mnt->mnt); 120870fbcdf4SRam Pai } 120970fbcdf4SRam Pai } 121070fbcdf4SRam Pai 121197216be0SAl Viro static inline void namespace_lock(void) 1212e3197d83SAl Viro { 121397216be0SAl Viro down_write(&namespace_sem); 1214e3197d83SAl Viro } 1215e3197d83SAl Viro 121699b7db7bSNick Piggin /* 121748a066e7SAl Viro * mount_lock must be held 121899b7db7bSNick Piggin * namespace_sem must be held for write 121948a066e7SAl Viro * how = 0 => just this tree, don't propagate 122048a066e7SAl Viro * how = 1 => propagate; we know that nobody else has reference to any victims 122148a066e7SAl Viro * how = 2 => lazy umount 122299b7db7bSNick Piggin */ 122348a066e7SAl Viro void umount_tree(struct mount *mnt, int how) 122470fbcdf4SRam Pai { 12257b8a53fdSAl Viro LIST_HEAD(tmp_list); 1226315fc83eSAl Viro struct mount *p; 122770fbcdf4SRam Pai 1228909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 12291b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 123070fbcdf4SRam Pai 123148a066e7SAl Viro if (how) 12327b8a53fdSAl Viro propagate_umount(&tmp_list); 1233a05964f3SRam Pai 12341b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 12356776db3dSAl Viro list_del_init(&p->mnt_expire); 12361a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1237143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 123863d37a84SAl Viro p->mnt_ns = NULL; 123948a066e7SAl Viro if (how < 2) 124048a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 12416b41d536SAl Viro list_del_init(&p->mnt_child); 1242676da58dSAl Viro if (mnt_has_parent(p)) { 124384d17192SAl Viro put_mountpoint(p->mnt_mp); 1244aba809cfSAl Viro /* move the reference to mountpoint into ->mnt_ex_mountpoint */ 1245aba809cfSAl Viro p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint; 1246aba809cfSAl Viro p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt; 1247aba809cfSAl Viro p->mnt_mountpoint = p->mnt.mnt_root; 1248aba809cfSAl Viro p->mnt_parent = p; 124984d17192SAl Viro p->mnt_mp = NULL; 12507c4b93d8SAl Viro } 12510f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 12521da177e4SLinus Torvalds } 1253328e6d90SAl Viro list_splice(&tmp_list, &unmounted); 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 1256b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1257c35038beSAl Viro 12581ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 12591da177e4SLinus Torvalds { 12601ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 12611da177e4SLinus Torvalds int retval; 12621da177e4SLinus Torvalds 12631ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 12641da177e4SLinus Torvalds if (retval) 12651da177e4SLinus Torvalds return retval; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds /* 12681da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 12691da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 12701da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 12711da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 12721da177e4SLinus Torvalds */ 12731da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 12741ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 12751da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 12761da177e4SLinus Torvalds return -EINVAL; 12771da177e4SLinus Torvalds 1278b3e19d92SNick Piggin /* 1279b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1280b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1281b3e19d92SNick Piggin */ 1282719ea2fbSAl Viro lock_mount_hash(); 128383adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1284719ea2fbSAl Viro unlock_mount_hash(); 12851da177e4SLinus Torvalds return -EBUSY; 1286b3e19d92SNick Piggin } 1287719ea2fbSAl Viro unlock_mount_hash(); 12881da177e4SLinus Torvalds 1289863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 12901da177e4SLinus Torvalds return -EAGAIN; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds /* 12941da177e4SLinus Torvalds * If we may have to abort operations to get out of this 12951da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 12961da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 12971da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 12981da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 12991da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 13001da177e4SLinus Torvalds * about for the moment. 13011da177e4SLinus Torvalds */ 13021da177e4SLinus Torvalds 130342faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 130442faad99SAl Viro sb->s_op->umount_begin(sb); 130542faad99SAl Viro } 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /* 13081da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 13091da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 13101da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 13111da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 13121da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 13131da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 13141da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 13151da177e4SLinus Torvalds */ 13161ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 13171da177e4SLinus Torvalds /* 13181da177e4SLinus Torvalds * Special case for "unmounting" root ... 13191da177e4SLinus Torvalds * we just try to remount it readonly. 13201da177e4SLinus Torvalds */ 13211da177e4SLinus Torvalds down_write(&sb->s_umount); 13224aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 13231da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 13241da177e4SLinus Torvalds up_write(&sb->s_umount); 13251da177e4SLinus Torvalds return retval; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 132897216be0SAl Viro namespace_lock(); 1329719ea2fbSAl Viro lock_mount_hash(); 13305addc5ddSAl Viro event++; 13311da177e4SLinus Torvalds 133248a066e7SAl Viro if (flags & MNT_DETACH) { 133348a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 133448a066e7SAl Viro umount_tree(mnt, 2); 133548a066e7SAl Viro retval = 0; 133648a066e7SAl Viro } else { 1337b54b9be7SAl Viro shrink_submounts(mnt); 13381da177e4SLinus Torvalds retval = -EBUSY; 133948a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 13401a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1341328e6d90SAl Viro umount_tree(mnt, 1); 13421da177e4SLinus Torvalds retval = 0; 13431da177e4SLinus Torvalds } 134448a066e7SAl Viro } 1345719ea2fbSAl Viro unlock_mount_hash(); 1346e3197d83SAl Viro namespace_unlock(); 13471da177e4SLinus Torvalds return retval; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds /* 13519b40bc90SAl Viro * Is the caller allowed to modify his namespace? 13529b40bc90SAl Viro */ 13539b40bc90SAl Viro static inline bool may_mount(void) 13549b40bc90SAl Viro { 13559b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 13569b40bc90SAl Viro } 13579b40bc90SAl Viro 13589b40bc90SAl Viro /* 13591da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 13601da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 13611da177e4SLinus Torvalds * 13621da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 13631da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 13641da177e4SLinus Torvalds */ 13651da177e4SLinus Torvalds 1366bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 13671da177e4SLinus Torvalds { 13682d8f3038SAl Viro struct path path; 1369900148dcSAl Viro struct mount *mnt; 13701da177e4SLinus Torvalds int retval; 1371db1f05bbSMiklos Szeredi int lookup_flags = 0; 13721da177e4SLinus Torvalds 1373db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1374db1f05bbSMiklos Szeredi return -EINVAL; 1375db1f05bbSMiklos Szeredi 13769b40bc90SAl Viro if (!may_mount()) 13779b40bc90SAl Viro return -EPERM; 13789b40bc90SAl Viro 1379db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1380db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1381db1f05bbSMiklos Szeredi 1382197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 13831da177e4SLinus Torvalds if (retval) 13841da177e4SLinus Torvalds goto out; 1385900148dcSAl Viro mnt = real_mount(path.mnt); 13861da177e4SLinus Torvalds retval = -EINVAL; 13872d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 13881da177e4SLinus Torvalds goto dput_and_out; 1389143c8c91SAl Viro if (!check_mnt(mnt)) 13901da177e4SLinus Torvalds goto dput_and_out; 13915ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 13925ff9d8a6SEric W. Biederman goto dput_and_out; 13931da177e4SLinus Torvalds 1394900148dcSAl Viro retval = do_umount(mnt, flags); 13951da177e4SLinus Torvalds dput_and_out: 1396429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 13972d8f3038SAl Viro dput(path.dentry); 1398900148dcSAl Viro mntput_no_expire(mnt); 13991da177e4SLinus Torvalds out: 14001da177e4SLinus Torvalds return retval; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds /* 14061da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 14071da177e4SLinus Torvalds */ 1408bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 14091da177e4SLinus Torvalds { 14101da177e4SLinus Torvalds return sys_umount(name, 0); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds #endif 14141da177e4SLinus Torvalds 14154ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 14168823c079SEric W. Biederman { 14174ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 14184ce5d2b1SEric W. Biederman struct inode *inode = dentry->d_inode; 14190bb80f24SDavid Howells struct proc_ns *ei; 14208823c079SEric W. Biederman 14218823c079SEric W. Biederman if (!proc_ns_inode(inode)) 14228823c079SEric W. Biederman return false; 14238823c079SEric W. Biederman 14240bb80f24SDavid Howells ei = get_proc_ns(inode); 14258823c079SEric W. Biederman if (ei->ns_ops != &mntns_operations) 14268823c079SEric W. Biederman return false; 14278823c079SEric W. Biederman 14284ce5d2b1SEric W. Biederman return true; 14294ce5d2b1SEric W. Biederman } 14304ce5d2b1SEric W. Biederman 14314ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 14324ce5d2b1SEric W. Biederman { 14334ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 14344ce5d2b1SEric W. Biederman * mount namespace loop? 14354ce5d2b1SEric W. Biederman */ 14364ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 14374ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 14384ce5d2b1SEric W. Biederman return false; 14394ce5d2b1SEric W. Biederman 14404ce5d2b1SEric W. Biederman mnt_ns = get_proc_ns(dentry->d_inode)->ns; 14418823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 14428823c079SEric W. Biederman } 14438823c079SEric W. Biederman 144487129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 144536341f64SRam Pai int flag) 14461da177e4SLinus Torvalds { 144784d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 14481da177e4SLinus Torvalds 14494ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 14504ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 14514ce5d2b1SEric W. Biederman 14524ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1453be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 14549676f0c6SRam Pai 145536341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1456be34d1a3SDavid Howells if (IS_ERR(q)) 1457be34d1a3SDavid Howells return q; 1458be34d1a3SDavid Howells 14595ff9d8a6SEric W. Biederman q->mnt.mnt_flags &= ~MNT_LOCKED; 1460a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds p = mnt; 14636b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1464315fc83eSAl Viro struct mount *s; 14657ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 14661da177e4SLinus Torvalds continue; 14671da177e4SLinus Torvalds 1468909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 14694ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 14704ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 14714ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 14724ce5d2b1SEric W. Biederman continue; 14734ce5d2b1SEric W. Biederman } 14744ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 14754ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 14769676f0c6SRam Pai s = skip_mnt_tree(s); 14779676f0c6SRam Pai continue; 14789676f0c6SRam Pai } 14790714a533SAl Viro while (p != s->mnt_parent) { 14800714a533SAl Viro p = p->mnt_parent; 14810714a533SAl Viro q = q->mnt_parent; 14821da177e4SLinus Torvalds } 148387129cc0SAl Viro p = s; 148484d17192SAl Viro parent = q; 148587129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1486be34d1a3SDavid Howells if (IS_ERR(q)) 1487be34d1a3SDavid Howells goto out; 1488719ea2fbSAl Viro lock_mount_hash(); 14891a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 149084d17192SAl Viro attach_mnt(q, parent, p->mnt_mp); 1491719ea2fbSAl Viro unlock_mount_hash(); 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds return res; 1495be34d1a3SDavid Howells out: 14961da177e4SLinus Torvalds if (res) { 1497719ea2fbSAl Viro lock_mount_hash(); 1498328e6d90SAl Viro umount_tree(res, 0); 1499719ea2fbSAl Viro unlock_mount_hash(); 15001da177e4SLinus Torvalds } 1501be34d1a3SDavid Howells return q; 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds 1504be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1505be34d1a3SDavid Howells 1506589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 15078aec0809SAl Viro { 1508cb338d06SAl Viro struct mount *tree; 150997216be0SAl Viro namespace_lock(); 151087129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 151187129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1512328e6d90SAl Viro namespace_unlock(); 1513be34d1a3SDavid Howells if (IS_ERR(tree)) 151452e220d3SDan Carpenter return ERR_CAST(tree); 1515be34d1a3SDavid Howells return &tree->mnt; 15168aec0809SAl Viro } 15178aec0809SAl Viro 15188aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 15198aec0809SAl Viro { 152097216be0SAl Viro namespace_lock(); 1521719ea2fbSAl Viro lock_mount_hash(); 1522328e6d90SAl Viro umount_tree(real_mount(mnt), 0); 1523719ea2fbSAl Viro unlock_mount_hash(); 15243ab6abeeSAl Viro namespace_unlock(); 15258aec0809SAl Viro } 15268aec0809SAl Viro 15271f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 15281f707137SAl Viro struct vfsmount *root) 15291f707137SAl Viro { 15301a4eeaf2SAl Viro struct mount *mnt; 15311f707137SAl Viro int res = f(root, arg); 15321f707137SAl Viro if (res) 15331f707137SAl Viro return res; 15341a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 15351a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 15361f707137SAl Viro if (res) 15371f707137SAl Viro return res; 15381f707137SAl Viro } 15391f707137SAl Viro return 0; 15401f707137SAl Viro } 15411f707137SAl Viro 15424b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1543719f5d7fSMiklos Szeredi { 1544315fc83eSAl Viro struct mount *p; 1545719f5d7fSMiklos Szeredi 1546909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1547fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 15484b8b21f4SAl Viro mnt_release_group_id(p); 1549719f5d7fSMiklos Szeredi } 1550719f5d7fSMiklos Szeredi } 1551719f5d7fSMiklos Szeredi 15524b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1553719f5d7fSMiklos Szeredi { 1554315fc83eSAl Viro struct mount *p; 1555719f5d7fSMiklos Szeredi 1556909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1557fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 15584b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1559719f5d7fSMiklos Szeredi if (err) { 15604b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1561719f5d7fSMiklos Szeredi return err; 1562719f5d7fSMiklos Szeredi } 1563719f5d7fSMiklos Szeredi } 1564719f5d7fSMiklos Szeredi } 1565719f5d7fSMiklos Szeredi 1566719f5d7fSMiklos Szeredi return 0; 1567719f5d7fSMiklos Szeredi } 1568719f5d7fSMiklos Szeredi 1569b90fa9aeSRam Pai /* 1570b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1571b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 157221444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 157321444403SRam Pai * store the parent mount and mountpoint dentry. 157421444403SRam Pai * (done when source_mnt is moved) 1575b90fa9aeSRam Pai * 1576b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1577b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 15789676f0c6SRam Pai * --------------------------------------------------------------------------- 1579b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 15809676f0c6SRam Pai * |************************************************************************** 15819676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 15829676f0c6SRam Pai * | dest | | | | | 15839676f0c6SRam Pai * | | | | | | | 15849676f0c6SRam Pai * | v | | | | | 15859676f0c6SRam Pai * |************************************************************************** 15869676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 15875afe0022SRam Pai * | | | | | | 15889676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 15899676f0c6SRam Pai * *************************************************************************** 1590b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1591b90fa9aeSRam Pai * destination mount. 1592b90fa9aeSRam Pai * 1593b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1594b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1595b90fa9aeSRam Pai * the peer group of the source mount. 1596b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1597b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1598b90fa9aeSRam Pai * mount. 15995afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 16005afe0022SRam Pai * of the destination mount and the cloned mount is made slave 16015afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 16025afe0022SRam Pai * is marked as 'shared and slave'. 16035afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 16045afe0022SRam Pai * source mount. 16055afe0022SRam Pai * 16069676f0c6SRam Pai * --------------------------------------------------------------------------- 160721444403SRam Pai * | MOVE MOUNT OPERATION | 16089676f0c6SRam Pai * |************************************************************************** 16099676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 16109676f0c6SRam Pai * | dest | | | | | 16119676f0c6SRam Pai * | | | | | | | 16129676f0c6SRam Pai * | v | | | | | 16139676f0c6SRam Pai * |************************************************************************** 16149676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 16155afe0022SRam Pai * | | | | | | 16169676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 16179676f0c6SRam Pai * *************************************************************************** 16185afe0022SRam Pai * 16195afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 16205afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 162121444403SRam Pai * (+*) the mount is moved to the destination. 16225afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 16235afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 16245afe0022SRam Pai * the mount is marked as 'shared and slave'. 16255afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1626b90fa9aeSRam Pai * 1627b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1628b90fa9aeSRam Pai * applied to each mount in the tree. 1629b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1630b90fa9aeSRam Pai * in allocations. 1631b90fa9aeSRam Pai */ 16320fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 163384d17192SAl Viro struct mount *dest_mnt, 163484d17192SAl Viro struct mountpoint *dest_mp, 163584d17192SAl Viro struct path *parent_path) 1636b90fa9aeSRam Pai { 1637b90fa9aeSRam Pai LIST_HEAD(tree_list); 1638315fc83eSAl Viro struct mount *child, *p; 1639719f5d7fSMiklos Szeredi int err; 1640b90fa9aeSRam Pai 1641fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 16420fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1643719f5d7fSMiklos Szeredi if (err) 1644719f5d7fSMiklos Szeredi goto out; 1645719f5d7fSMiklos Szeredi } 164684d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1647719f5d7fSMiklos Szeredi if (err) 1648719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1649b90fa9aeSRam Pai 1650719ea2fbSAl Viro lock_mount_hash(); 1651df1a1ad2SAl Viro 1652fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1653909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 16540f0afb1dSAl Viro set_mnt_shared(p); 1655b90fa9aeSRam Pai } 16561a390689SAl Viro if (parent_path) { 16570fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 165884d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1659143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 166021444403SRam Pai } else { 166184d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 16620fb54e50SAl Viro commit_tree(source_mnt); 166321444403SRam Pai } 1664b90fa9aeSRam Pai 16651b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 16661b8e5564SAl Viro list_del_init(&child->mnt_hash); 16674b2619a5SAl Viro commit_tree(child); 1668b90fa9aeSRam Pai } 1669719ea2fbSAl Viro unlock_mount_hash(); 167099b7db7bSNick Piggin 1671b90fa9aeSRam Pai return 0; 1672719f5d7fSMiklos Szeredi 1673719f5d7fSMiklos Szeredi out_cleanup_ids: 1674fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 16750fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1676719f5d7fSMiklos Szeredi out: 1677719f5d7fSMiklos Szeredi return err; 1678b90fa9aeSRam Pai } 1679b90fa9aeSRam Pai 168084d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 1681b12cea91SAl Viro { 1682b12cea91SAl Viro struct vfsmount *mnt; 168384d17192SAl Viro struct dentry *dentry = path->dentry; 1684b12cea91SAl Viro retry: 168584d17192SAl Viro mutex_lock(&dentry->d_inode->i_mutex); 168684d17192SAl Viro if (unlikely(cant_mount(dentry))) { 168784d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 168884d17192SAl Viro return ERR_PTR(-ENOENT); 1689b12cea91SAl Viro } 169097216be0SAl Viro namespace_lock(); 1691b12cea91SAl Viro mnt = lookup_mnt(path); 169284d17192SAl Viro if (likely(!mnt)) { 169384d17192SAl Viro struct mountpoint *mp = new_mountpoint(dentry); 169484d17192SAl Viro if (IS_ERR(mp)) { 169597216be0SAl Viro namespace_unlock(); 169684d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 169784d17192SAl Viro return mp; 169884d17192SAl Viro } 169984d17192SAl Viro return mp; 170084d17192SAl Viro } 170197216be0SAl Viro namespace_unlock(); 1702b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1703b12cea91SAl Viro path_put(path); 1704b12cea91SAl Viro path->mnt = mnt; 170584d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 1706b12cea91SAl Viro goto retry; 1707b12cea91SAl Viro } 1708b12cea91SAl Viro 170984d17192SAl Viro static void unlock_mount(struct mountpoint *where) 1710b12cea91SAl Viro { 171184d17192SAl Viro struct dentry *dentry = where->m_dentry; 171284d17192SAl Viro put_mountpoint(where); 1713328e6d90SAl Viro namespace_unlock(); 171484d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 1715b12cea91SAl Viro } 1716b12cea91SAl Viro 171784d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 17181da177e4SLinus Torvalds { 171995bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 17201da177e4SLinus Torvalds return -EINVAL; 17211da177e4SLinus Torvalds 172284d17192SAl Viro if (S_ISDIR(mp->m_dentry->d_inode->i_mode) != 172395bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 17241da177e4SLinus Torvalds return -ENOTDIR; 17251da177e4SLinus Torvalds 172684d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds /* 17307a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 17317a2e8a8fSValerie Aurora */ 17327a2e8a8fSValerie Aurora 17337a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 17347a2e8a8fSValerie Aurora { 17357c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 17367a2e8a8fSValerie Aurora 17377a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 17387a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 17397a2e8a8fSValerie Aurora return 0; 17407a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 17417a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 17427a2e8a8fSValerie Aurora return 0; 17437a2e8a8fSValerie Aurora return type; 17447a2e8a8fSValerie Aurora } 17457a2e8a8fSValerie Aurora 17467a2e8a8fSValerie Aurora /* 174707b20889SRam Pai * recursively change the type of the mountpoint. 174807b20889SRam Pai */ 17490a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 175007b20889SRam Pai { 1751315fc83eSAl Viro struct mount *m; 17524b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 175307b20889SRam Pai int recurse = flag & MS_REC; 17547a2e8a8fSValerie Aurora int type; 1755719f5d7fSMiklos Szeredi int err = 0; 175607b20889SRam Pai 17572d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 175807b20889SRam Pai return -EINVAL; 175907b20889SRam Pai 17607a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 17617a2e8a8fSValerie Aurora if (!type) 17627a2e8a8fSValerie Aurora return -EINVAL; 17637a2e8a8fSValerie Aurora 176497216be0SAl Viro namespace_lock(); 1765719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1766719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1767719f5d7fSMiklos Szeredi if (err) 1768719f5d7fSMiklos Szeredi goto out_unlock; 1769719f5d7fSMiklos Szeredi } 1770719f5d7fSMiklos Szeredi 1771719ea2fbSAl Viro lock_mount_hash(); 1772909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 17730f0afb1dSAl Viro change_mnt_propagation(m, type); 1774719ea2fbSAl Viro unlock_mount_hash(); 1775719f5d7fSMiklos Szeredi 1776719f5d7fSMiklos Szeredi out_unlock: 177797216be0SAl Viro namespace_unlock(); 1778719f5d7fSMiklos Szeredi return err; 177907b20889SRam Pai } 178007b20889SRam Pai 17815ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 17825ff9d8a6SEric W. Biederman { 17835ff9d8a6SEric W. Biederman struct mount *child; 17845ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 17855ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 17865ff9d8a6SEric W. Biederman continue; 17875ff9d8a6SEric W. Biederman 17885ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 17895ff9d8a6SEric W. Biederman return true; 17905ff9d8a6SEric W. Biederman } 17915ff9d8a6SEric W. Biederman return false; 17925ff9d8a6SEric W. Biederman } 17935ff9d8a6SEric W. Biederman 179407b20889SRam Pai /* 17951da177e4SLinus Torvalds * do loopback mount. 17961da177e4SLinus Torvalds */ 1797808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 17982dafe1c4SEric Sandeen int recurse) 17991da177e4SLinus Torvalds { 18002d92ab3cSAl Viro struct path old_path; 180184d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 180284d17192SAl Viro struct mountpoint *mp; 180357eccb83SAl Viro int err; 18041da177e4SLinus Torvalds if (!old_name || !*old_name) 18051da177e4SLinus Torvalds return -EINVAL; 1806815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 18071da177e4SLinus Torvalds if (err) 18081da177e4SLinus Torvalds return err; 18091da177e4SLinus Torvalds 18108823c079SEric W. Biederman err = -EINVAL; 18114ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 18128823c079SEric W. Biederman goto out; 18138823c079SEric W. Biederman 181484d17192SAl Viro mp = lock_mount(path); 181584d17192SAl Viro err = PTR_ERR(mp); 181684d17192SAl Viro if (IS_ERR(mp)) 18179676f0c6SRam Pai goto out; 18189676f0c6SRam Pai 181987129cc0SAl Viro old = real_mount(old_path.mnt); 182084d17192SAl Viro parent = real_mount(path->mnt); 182187129cc0SAl Viro 1822b12cea91SAl Viro err = -EINVAL; 1823fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1824b12cea91SAl Viro goto out2; 1825b12cea91SAl Viro 182684d17192SAl Viro if (!check_mnt(parent) || !check_mnt(old)) 1827b12cea91SAl Viro goto out2; 1828ccd48bc7SAl Viro 18295ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 18305ff9d8a6SEric W. Biederman goto out2; 18315ff9d8a6SEric W. Biederman 18321da177e4SLinus Torvalds if (recurse) 18334ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 18341da177e4SLinus Torvalds else 183587129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 18361da177e4SLinus Torvalds 1837be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1838be34d1a3SDavid Howells err = PTR_ERR(mnt); 1839e9c5d8a5SAndrey Vagin goto out2; 1840be34d1a3SDavid Howells } 1841ccd48bc7SAl Viro 18425ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 18435ff9d8a6SEric W. Biederman 184484d17192SAl Viro err = graft_tree(mnt, parent, mp); 18451da177e4SLinus Torvalds if (err) { 1846719ea2fbSAl Viro lock_mount_hash(); 1847328e6d90SAl Viro umount_tree(mnt, 0); 1848719ea2fbSAl Viro unlock_mount_hash(); 18495b83d2c5SRam Pai } 1850b12cea91SAl Viro out2: 185184d17192SAl Viro unlock_mount(mp); 1852ccd48bc7SAl Viro out: 18532d92ab3cSAl Viro path_put(&old_path); 18541da177e4SLinus Torvalds return err; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds 18572e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 18582e4b7fcdSDave Hansen { 18592e4b7fcdSDave Hansen int error = 0; 18602e4b7fcdSDave Hansen int readonly_request = 0; 18612e4b7fcdSDave Hansen 18622e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 18632e4b7fcdSDave Hansen readonly_request = 1; 18642e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 18652e4b7fcdSDave Hansen return 0; 18662e4b7fcdSDave Hansen 186790563b19SEric W. Biederman if (mnt->mnt_flags & MNT_LOCK_READONLY) 186890563b19SEric W. Biederman return -EPERM; 186990563b19SEric W. Biederman 18702e4b7fcdSDave Hansen if (readonly_request) 187183adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 18722e4b7fcdSDave Hansen else 187383adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 18742e4b7fcdSDave Hansen return error; 18752e4b7fcdSDave Hansen } 18762e4b7fcdSDave Hansen 18771da177e4SLinus Torvalds /* 18781da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 18791da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 18801da177e4SLinus Torvalds * on it - tough luck. 18811da177e4SLinus Torvalds */ 18820a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 18831da177e4SLinus Torvalds void *data) 18841da177e4SLinus Torvalds { 18851da177e4SLinus Torvalds int err; 18862d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1887143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 18881da177e4SLinus Torvalds 1889143c8c91SAl Viro if (!check_mnt(mnt)) 18901da177e4SLinus Torvalds return -EINVAL; 18911da177e4SLinus Torvalds 18922d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 18931da177e4SLinus Torvalds return -EINVAL; 18941da177e4SLinus Torvalds 1895ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1896ff36fe2cSEric Paris if (err) 1897ff36fe2cSEric Paris return err; 1898ff36fe2cSEric Paris 18991da177e4SLinus Torvalds down_write(&sb->s_umount); 19002e4b7fcdSDave Hansen if (flags & MS_BIND) 19012d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 190257eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 190357eccb83SAl Viro err = -EPERM; 19044aa98cf7SAl Viro else 19051da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 19067b43a79fSAl Viro if (!err) { 1907719ea2fbSAl Viro lock_mount_hash(); 1908143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1909143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1910143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1911719ea2fbSAl Viro unlock_mount_hash(); 19120e55a7ccSDan Williams } 19136339dab8SAl Viro up_write(&sb->s_umount); 19141da177e4SLinus Torvalds return err; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds 1917cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 19189676f0c6SRam Pai { 1919315fc83eSAl Viro struct mount *p; 1920909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1921fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 19229676f0c6SRam Pai return 1; 19239676f0c6SRam Pai } 19249676f0c6SRam Pai return 0; 19259676f0c6SRam Pai } 19269676f0c6SRam Pai 1927808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 19281da177e4SLinus Torvalds { 19292d92ab3cSAl Viro struct path old_path, parent_path; 1930676da58dSAl Viro struct mount *p; 19310fb54e50SAl Viro struct mount *old; 193284d17192SAl Viro struct mountpoint *mp; 193357eccb83SAl Viro int err; 19341da177e4SLinus Torvalds if (!old_name || !*old_name) 19351da177e4SLinus Torvalds return -EINVAL; 19362d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 19371da177e4SLinus Torvalds if (err) 19381da177e4SLinus Torvalds return err; 19391da177e4SLinus Torvalds 194084d17192SAl Viro mp = lock_mount(path); 194184d17192SAl Viro err = PTR_ERR(mp); 194284d17192SAl Viro if (IS_ERR(mp)) 1943cc53ce53SDavid Howells goto out; 1944cc53ce53SDavid Howells 1945143c8c91SAl Viro old = real_mount(old_path.mnt); 1946fc7be130SAl Viro p = real_mount(path->mnt); 1947143c8c91SAl Viro 19481da177e4SLinus Torvalds err = -EINVAL; 1949fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 19501da177e4SLinus Torvalds goto out1; 19511da177e4SLinus Torvalds 19525ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 19535ff9d8a6SEric W. Biederman goto out1; 19545ff9d8a6SEric W. Biederman 19551da177e4SLinus Torvalds err = -EINVAL; 19562d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 195721444403SRam Pai goto out1; 19581da177e4SLinus Torvalds 1959676da58dSAl Viro if (!mnt_has_parent(old)) 196021444403SRam Pai goto out1; 19611da177e4SLinus Torvalds 19622d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 19632d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 196421444403SRam Pai goto out1; 196521444403SRam Pai /* 196621444403SRam Pai * Don't move a mount residing in a shared parent. 196721444403SRam Pai */ 1968fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 196921444403SRam Pai goto out1; 19709676f0c6SRam Pai /* 19719676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 19729676f0c6SRam Pai * mount which is shared. 19739676f0c6SRam Pai */ 1974fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 19759676f0c6SRam Pai goto out1; 19761da177e4SLinus Torvalds err = -ELOOP; 1977fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1978676da58dSAl Viro if (p == old) 197921444403SRam Pai goto out1; 19801da177e4SLinus Torvalds 198184d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 19824ac91378SJan Blunck if (err) 198321444403SRam Pai goto out1; 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 19861da177e4SLinus Torvalds * automatically */ 19876776db3dSAl Viro list_del_init(&old->mnt_expire); 19881da177e4SLinus Torvalds out1: 198984d17192SAl Viro unlock_mount(mp); 19901da177e4SLinus Torvalds out: 19911da177e4SLinus Torvalds if (!err) 19921a390689SAl Viro path_put(&parent_path); 19932d92ab3cSAl Viro path_put(&old_path); 19941da177e4SLinus Torvalds return err; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 19979d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 19989d412a43SAl Viro { 19999d412a43SAl Viro int err; 20009d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 20019d412a43SAl Viro if (subtype) { 20029d412a43SAl Viro subtype++; 20039d412a43SAl Viro err = -EINVAL; 20049d412a43SAl Viro if (!subtype[0]) 20059d412a43SAl Viro goto err; 20069d412a43SAl Viro } else 20079d412a43SAl Viro subtype = ""; 20089d412a43SAl Viro 20099d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 20109d412a43SAl Viro err = -ENOMEM; 20119d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 20129d412a43SAl Viro goto err; 20139d412a43SAl Viro return mnt; 20149d412a43SAl Viro 20159d412a43SAl Viro err: 20169d412a43SAl Viro mntput(mnt); 20179d412a43SAl Viro return ERR_PTR(err); 20189d412a43SAl Viro } 20199d412a43SAl Viro 20209d412a43SAl Viro /* 20219d412a43SAl Viro * add a mount into a namespace's mount tree 20229d412a43SAl Viro */ 202395bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 20249d412a43SAl Viro { 202584d17192SAl Viro struct mountpoint *mp; 202684d17192SAl Viro struct mount *parent; 20279d412a43SAl Viro int err; 20289d412a43SAl Viro 202948a066e7SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL | MNT_DOOMED | MNT_SYNC_UMOUNT); 20309d412a43SAl Viro 203184d17192SAl Viro mp = lock_mount(path); 203284d17192SAl Viro if (IS_ERR(mp)) 203384d17192SAl Viro return PTR_ERR(mp); 20349d412a43SAl Viro 203584d17192SAl Viro parent = real_mount(path->mnt); 20369d412a43SAl Viro err = -EINVAL; 203784d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2038156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2039156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 20409d412a43SAl Viro goto unlock; 2041156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 204284d17192SAl Viro if (!parent->mnt_ns) 2043156cacb1SAl Viro goto unlock; 2044156cacb1SAl Viro } 20459d412a43SAl Viro 20469d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 20479d412a43SAl Viro err = -EBUSY; 204895bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 20499d412a43SAl Viro path->mnt->mnt_root == path->dentry) 20509d412a43SAl Viro goto unlock; 20519d412a43SAl Viro 20529d412a43SAl Viro err = -EINVAL; 205395bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 20549d412a43SAl Viro goto unlock; 20559d412a43SAl Viro 205695bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 205784d17192SAl Viro err = graft_tree(newmnt, parent, mp); 20589d412a43SAl Viro 20599d412a43SAl Viro unlock: 206084d17192SAl Viro unlock_mount(mp); 20619d412a43SAl Viro return err; 20629d412a43SAl Viro } 2063b1e75df4SAl Viro 20641da177e4SLinus Torvalds /* 20651da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 20661da177e4SLinus Torvalds * namespace's tree 20671da177e4SLinus Torvalds */ 20680c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 2069808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 20701da177e4SLinus Torvalds { 20710c55cfc4SEric W. Biederman struct file_system_type *type; 20729b40bc90SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 20731da177e4SLinus Torvalds struct vfsmount *mnt; 207415f9a3f3SAl Viro int err; 20751da177e4SLinus Torvalds 20760c55cfc4SEric W. Biederman if (!fstype) 20771da177e4SLinus Torvalds return -EINVAL; 20781da177e4SLinus Torvalds 20790c55cfc4SEric W. Biederman type = get_fs_type(fstype); 20800c55cfc4SEric W. Biederman if (!type) 20810c55cfc4SEric W. Biederman return -ENODEV; 20820c55cfc4SEric W. Biederman 20830c55cfc4SEric W. Biederman if (user_ns != &init_user_ns) { 20840c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_MOUNT)) { 20850c55cfc4SEric W. Biederman put_filesystem(type); 20860c55cfc4SEric W. Biederman return -EPERM; 20870c55cfc4SEric W. Biederman } 20880c55cfc4SEric W. Biederman /* Only in special cases allow devices from mounts 20890c55cfc4SEric W. Biederman * created outside the initial user namespace. 20900c55cfc4SEric W. Biederman */ 20910c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) { 20920c55cfc4SEric W. Biederman flags |= MS_NODEV; 20930c55cfc4SEric W. Biederman mnt_flags |= MNT_NODEV; 20940c55cfc4SEric W. Biederman } 20950c55cfc4SEric W. Biederman } 20960c55cfc4SEric W. Biederman 20970c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 20980c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 20990c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 21000c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 21010c55cfc4SEric W. Biederman 21020c55cfc4SEric W. Biederman put_filesystem(type); 21031da177e4SLinus Torvalds if (IS_ERR(mnt)) 21041da177e4SLinus Torvalds return PTR_ERR(mnt); 21051da177e4SLinus Torvalds 210695bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 210715f9a3f3SAl Viro if (err) 210815f9a3f3SAl Viro mntput(mnt); 210915f9a3f3SAl Viro return err; 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 211219a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 211319a167afSAl Viro { 21146776db3dSAl Viro struct mount *mnt = real_mount(m); 211519a167afSAl Viro int err; 211619a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 211719a167afSAl Viro * expired before we get a chance to add it 211819a167afSAl Viro */ 21196776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 212019a167afSAl Viro 212119a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 212219a167afSAl Viro m->mnt_root == path->dentry) { 2123b1e75df4SAl Viro err = -ELOOP; 2124b1e75df4SAl Viro goto fail; 212519a167afSAl Viro } 212619a167afSAl Viro 212795bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2128b1e75df4SAl Viro if (!err) 2129b1e75df4SAl Viro return 0; 2130b1e75df4SAl Viro fail: 2131b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 21326776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 213397216be0SAl Viro namespace_lock(); 21346776db3dSAl Viro list_del_init(&mnt->mnt_expire); 213597216be0SAl Viro namespace_unlock(); 213619a167afSAl Viro } 2137b1e75df4SAl Viro mntput(m); 2138b1e75df4SAl Viro mntput(m); 213919a167afSAl Viro return err; 214019a167afSAl Viro } 214119a167afSAl Viro 2142ea5b778aSDavid Howells /** 2143ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2144ea5b778aSDavid Howells * @mnt: The mount to list. 2145ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2146ea5b778aSDavid Howells */ 2147ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2148ea5b778aSDavid Howells { 214997216be0SAl Viro namespace_lock(); 2150ea5b778aSDavid Howells 21516776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2152ea5b778aSDavid Howells 215397216be0SAl Viro namespace_unlock(); 2154ea5b778aSDavid Howells } 2155ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2156ea5b778aSDavid Howells 2157ea5b778aSDavid Howells /* 21581da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 21591da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 21601da177e4SLinus Torvalds * here 21611da177e4SLinus Torvalds */ 21621da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 21631da177e4SLinus Torvalds { 2164761d5c38SAl Viro struct mount *mnt, *next; 21651da177e4SLinus Torvalds LIST_HEAD(graveyard); 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds if (list_empty(mounts)) 21681da177e4SLinus Torvalds return; 21691da177e4SLinus Torvalds 217097216be0SAl Viro namespace_lock(); 2171719ea2fbSAl Viro lock_mount_hash(); 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 21741da177e4SLinus Torvalds * following criteria: 21751da177e4SLinus Torvalds * - only referenced by its parent vfsmount 21761da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 21771da177e4SLinus Torvalds * cleared by mntput()) 21781da177e4SLinus Torvalds */ 21796776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2180863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 21811ab59738SAl Viro propagate_mount_busy(mnt, 1)) 21821da177e4SLinus Torvalds continue; 21836776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 21841da177e4SLinus Torvalds } 2185bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 21866776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2187143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2188328e6d90SAl Viro umount_tree(mnt, 1); 2189bcc5c7d2SAl Viro } 2190719ea2fbSAl Viro unlock_mount_hash(); 21913ab6abeeSAl Viro namespace_unlock(); 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds /* 21975528f911STrond Myklebust * Ripoff of 'select_parent()' 21985528f911STrond Myklebust * 21995528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 22005528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 22015528f911STrond Myklebust */ 2202692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 22035528f911STrond Myklebust { 2204692afc31SAl Viro struct mount *this_parent = parent; 22055528f911STrond Myklebust struct list_head *next; 22065528f911STrond Myklebust int found = 0; 22075528f911STrond Myklebust 22085528f911STrond Myklebust repeat: 22096b41d536SAl Viro next = this_parent->mnt_mounts.next; 22105528f911STrond Myklebust resume: 22116b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 22125528f911STrond Myklebust struct list_head *tmp = next; 22136b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 22145528f911STrond Myklebust 22155528f911STrond Myklebust next = tmp->next; 2216692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 22175528f911STrond Myklebust continue; 22185528f911STrond Myklebust /* 22195528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 22205528f911STrond Myklebust */ 22216b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 22225528f911STrond Myklebust this_parent = mnt; 22235528f911STrond Myklebust goto repeat; 22245528f911STrond Myklebust } 22255528f911STrond Myklebust 22261ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 22276776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 22285528f911STrond Myklebust found++; 22295528f911STrond Myklebust } 22305528f911STrond Myklebust } 22315528f911STrond Myklebust /* 22325528f911STrond Myklebust * All done at this level ... ascend and resume the search 22335528f911STrond Myklebust */ 22345528f911STrond Myklebust if (this_parent != parent) { 22356b41d536SAl Viro next = this_parent->mnt_child.next; 22360714a533SAl Viro this_parent = this_parent->mnt_parent; 22375528f911STrond Myklebust goto resume; 22385528f911STrond Myklebust } 22395528f911STrond Myklebust return found; 22405528f911STrond Myklebust } 22415528f911STrond Myklebust 22425528f911STrond Myklebust /* 22435528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 22445528f911STrond Myklebust * submounts of a specific parent mountpoint 224599b7db7bSNick Piggin * 224648a066e7SAl Viro * mount_lock must be held for write 22475528f911STrond Myklebust */ 2248b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 22495528f911STrond Myklebust { 22505528f911STrond Myklebust LIST_HEAD(graveyard); 2251761d5c38SAl Viro struct mount *m; 22525528f911STrond Myklebust 22535528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2254c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2255bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2256761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 22576776db3dSAl Viro mnt_expire); 2258143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2259328e6d90SAl Viro umount_tree(m, 1); 2260bcc5c7d2SAl Viro } 2261bcc5c7d2SAl Viro } 22625528f911STrond Myklebust } 22635528f911STrond Myklebust 22645528f911STrond Myklebust /* 22651da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 22661da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 22671da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 22681da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 22691da177e4SLinus Torvalds */ 2270b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2271b58fed8bSRam Pai unsigned long n) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds char *t = to; 22741da177e4SLinus Torvalds const char __user *f = from; 22751da177e4SLinus Torvalds char c; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 22781da177e4SLinus Torvalds return n; 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds while (n) { 22811da177e4SLinus Torvalds if (__get_user(c, f)) { 22821da177e4SLinus Torvalds memset(t, 0, n); 22831da177e4SLinus Torvalds break; 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds *t++ = c; 22861da177e4SLinus Torvalds f++; 22871da177e4SLinus Torvalds n--; 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds return n; 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds 22921da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 22931da177e4SLinus Torvalds { 22941da177e4SLinus Torvalds int i; 22951da177e4SLinus Torvalds unsigned long page; 22961da177e4SLinus Torvalds unsigned long size; 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds *where = 0; 22991da177e4SLinus Torvalds if (!data) 23001da177e4SLinus Torvalds return 0; 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 23031da177e4SLinus Torvalds return -ENOMEM; 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds /* We only care that *some* data at the address the user 23061da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 23071da177e4SLinus Torvalds * the remainder of the page. 23081da177e4SLinus Torvalds */ 23091da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 23101da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 23111da177e4SLinus Torvalds if (size > PAGE_SIZE) 23121da177e4SLinus Torvalds size = PAGE_SIZE; 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 23151da177e4SLinus Torvalds if (!i) { 23161da177e4SLinus Torvalds free_page(page); 23171da177e4SLinus Torvalds return -EFAULT; 23181da177e4SLinus Torvalds } 23191da177e4SLinus Torvalds if (i != PAGE_SIZE) 23201da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 23211da177e4SLinus Torvalds *where = page; 23221da177e4SLinus Torvalds return 0; 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds 2325eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2326eca6f534SVegard Nossum { 2327eca6f534SVegard Nossum char *tmp; 2328eca6f534SVegard Nossum 2329eca6f534SVegard Nossum if (!data) { 2330eca6f534SVegard Nossum *where = NULL; 2331eca6f534SVegard Nossum return 0; 2332eca6f534SVegard Nossum } 2333eca6f534SVegard Nossum 2334eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2335eca6f534SVegard Nossum if (IS_ERR(tmp)) 2336eca6f534SVegard Nossum return PTR_ERR(tmp); 2337eca6f534SVegard Nossum 2338eca6f534SVegard Nossum *where = tmp; 2339eca6f534SVegard Nossum return 0; 2340eca6f534SVegard Nossum } 2341eca6f534SVegard Nossum 23421da177e4SLinus Torvalds /* 23431da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 23441da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 23451da177e4SLinus Torvalds * 23461da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 23471da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 23481da177e4SLinus Torvalds * information (or be NULL). 23491da177e4SLinus Torvalds * 23501da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 23511da177e4SLinus Torvalds * When the flags word was introduced its top half was required 23521da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 23531da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 23541da177e4SLinus Torvalds * and must be discarded. 23551da177e4SLinus Torvalds */ 2356808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name, 2357808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 23581da177e4SLinus Torvalds { 23592d92ab3cSAl Viro struct path path; 23601da177e4SLinus Torvalds int retval = 0; 23611da177e4SLinus Torvalds int mnt_flags = 0; 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds /* Discard magic */ 23641da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 23651da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds /* Basic sanity checks */ 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 23701da177e4SLinus Torvalds return -EINVAL; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds if (data_page) 23731da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 23741da177e4SLinus Torvalds 2375a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2376a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2377a27ab9f2STetsuo Handa if (retval) 2378a27ab9f2STetsuo Handa return retval; 2379a27ab9f2STetsuo Handa 2380a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2381a27ab9f2STetsuo Handa type_page, flags, data_page); 23820d5cadb8SAl Viro if (!retval && !may_mount()) 23830d5cadb8SAl Viro retval = -EPERM; 2384a27ab9f2STetsuo Handa if (retval) 2385a27ab9f2STetsuo Handa goto dput_out; 2386a27ab9f2STetsuo Handa 2387613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2388613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 23890a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 23900a1c01c9SMatthew Garrett 23911da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 23921da177e4SLinus Torvalds if (flags & MS_NOSUID) 23931da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 23941da177e4SLinus Torvalds if (flags & MS_NODEV) 23951da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 23961da177e4SLinus Torvalds if (flags & MS_NOEXEC) 23971da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2398fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2399fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2400fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2401fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2402d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2403d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 24042e4b7fcdSDave Hansen if (flags & MS_RDONLY) 24052e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2406fc33a7bbSChristoph Hellwig 24077a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2408d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2409d0adde57SMatthew Garrett MS_STRICTATIME); 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds if (flags & MS_REMOUNT) 24122d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 24131da177e4SLinus Torvalds data_page); 24141da177e4SLinus Torvalds else if (flags & MS_BIND) 24152d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 24169676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 24172d92ab3cSAl Viro retval = do_change_type(&path, flags); 24181da177e4SLinus Torvalds else if (flags & MS_MOVE) 24192d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 24201da177e4SLinus Torvalds else 24212d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 24221da177e4SLinus Torvalds dev_name, data_page); 24231da177e4SLinus Torvalds dput_out: 24242d92ab3cSAl Viro path_put(&path); 24251da177e4SLinus Torvalds return retval; 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds 2428771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2429771b1371SEric W. Biederman { 243098f842e6SEric W. Biederman proc_free_inum(ns->proc_inum); 2431771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2432771b1371SEric W. Biederman kfree(ns); 2433771b1371SEric W. Biederman } 2434771b1371SEric W. Biederman 24358823c079SEric W. Biederman /* 24368823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 24378823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 24388823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 24398823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 24408823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 24418823c079SEric W. Biederman */ 24428823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 24438823c079SEric W. Biederman 2444771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2445cf8d2c11STrond Myklebust { 2446cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 244798f842e6SEric W. Biederman int ret; 2448cf8d2c11STrond Myklebust 2449cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2450cf8d2c11STrond Myklebust if (!new_ns) 2451cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 245298f842e6SEric W. Biederman ret = proc_alloc_inum(&new_ns->proc_inum); 245398f842e6SEric W. Biederman if (ret) { 245498f842e6SEric W. Biederman kfree(new_ns); 245598f842e6SEric W. Biederman return ERR_PTR(ret); 245698f842e6SEric W. Biederman } 24578823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2458cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2459cf8d2c11STrond Myklebust new_ns->root = NULL; 2460cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2461cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2462cf8d2c11STrond Myklebust new_ns->event = 0; 2463771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2464cf8d2c11STrond Myklebust return new_ns; 2465cf8d2c11STrond Myklebust } 2466cf8d2c11STrond Myklebust 24679559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 24689559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 24691da177e4SLinus Torvalds { 24706b3286edSKirill Korotaev struct mnt_namespace *new_ns; 24717f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2472315fc83eSAl Viro struct mount *p, *q; 24739559f689SAl Viro struct mount *old; 2474cb338d06SAl Viro struct mount *new; 24757a472ef4SEric W. Biederman int copy_flags; 24761da177e4SLinus Torvalds 24779559f689SAl Viro BUG_ON(!ns); 24789559f689SAl Viro 24799559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 24809559f689SAl Viro get_mnt_ns(ns); 24819559f689SAl Viro return ns; 24829559f689SAl Viro } 24839559f689SAl Viro 24849559f689SAl Viro old = ns->root; 24859559f689SAl Viro 2486771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2487cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2488cf8d2c11STrond Myklebust return new_ns; 24891da177e4SLinus Torvalds 249097216be0SAl Viro namespace_lock(); 24911da177e4SLinus Torvalds /* First pass: copy the tree topology */ 24924ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 24939559f689SAl Viro if (user_ns != ns->user_ns) 2494132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 24957a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2496be34d1a3SDavid Howells if (IS_ERR(new)) { 2497328e6d90SAl Viro namespace_unlock(); 2498771b1371SEric W. Biederman free_mnt_ns(new_ns); 2499be34d1a3SDavid Howells return ERR_CAST(new); 25001da177e4SLinus Torvalds } 2501be08d6d2SAl Viro new_ns->root = new; 25021a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds /* 25051da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 25061da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 25071da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 25081da177e4SLinus Torvalds */ 2509909b0a88SAl Viro p = old; 2510cb338d06SAl Viro q = new; 25111da177e4SLinus Torvalds while (p) { 2512143c8c91SAl Viro q->mnt_ns = new_ns; 25139559f689SAl Viro if (new_fs) { 25149559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 25159559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2516315fc83eSAl Viro rootmnt = &p->mnt; 25171da177e4SLinus Torvalds } 25189559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 25199559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2520315fc83eSAl Viro pwdmnt = &p->mnt; 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds } 2523909b0a88SAl Viro p = next_mnt(p, old); 2524909b0a88SAl Viro q = next_mnt(q, new); 25254ce5d2b1SEric W. Biederman if (!q) 25264ce5d2b1SEric W. Biederman break; 25274ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 25284ce5d2b1SEric W. Biederman p = next_mnt(p, old); 25291da177e4SLinus Torvalds } 2530328e6d90SAl Viro namespace_unlock(); 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds if (rootmnt) 2533f03c6599SAl Viro mntput(rootmnt); 25341da177e4SLinus Torvalds if (pwdmnt) 2535f03c6599SAl Viro mntput(pwdmnt); 25361da177e4SLinus Torvalds 2537741a2951SJANAK DESAI return new_ns; 2538741a2951SJANAK DESAI } 2539741a2951SJANAK DESAI 2540cf8d2c11STrond Myklebust /** 2541cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2542cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2543cf8d2c11STrond Myklebust */ 25441a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2545cf8d2c11STrond Myklebust { 2546771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2547cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 25481a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 25491a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2550be08d6d2SAl Viro new_ns->root = mnt; 2551b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2552c1334495SAl Viro } else { 25531a4eeaf2SAl Viro mntput(m); 2554cf8d2c11STrond Myklebust } 2555cf8d2c11STrond Myklebust return new_ns; 2556cf8d2c11STrond Myklebust } 2557cf8d2c11STrond Myklebust 2558ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2559ea441d11SAl Viro { 2560ea441d11SAl Viro struct mnt_namespace *ns; 2561d31da0f0SAl Viro struct super_block *s; 2562ea441d11SAl Viro struct path path; 2563ea441d11SAl Viro int err; 2564ea441d11SAl Viro 2565ea441d11SAl Viro ns = create_mnt_ns(mnt); 2566ea441d11SAl Viro if (IS_ERR(ns)) 2567ea441d11SAl Viro return ERR_CAST(ns); 2568ea441d11SAl Viro 2569ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2570ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2571ea441d11SAl Viro 2572ea441d11SAl Viro put_mnt_ns(ns); 2573ea441d11SAl Viro 2574ea441d11SAl Viro if (err) 2575ea441d11SAl Viro return ERR_PTR(err); 2576ea441d11SAl Viro 2577ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2578d31da0f0SAl Viro s = path.mnt->mnt_sb; 2579d31da0f0SAl Viro atomic_inc(&s->s_active); 2580ea441d11SAl Viro mntput(path.mnt); 2581ea441d11SAl Viro /* lock the sucker */ 2582d31da0f0SAl Viro down_write(&s->s_umount); 2583ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2584ea441d11SAl Viro return path.dentry; 2585ea441d11SAl Viro } 2586ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2587ea441d11SAl Viro 2588bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2589bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 25901da177e4SLinus Torvalds { 2591eca6f534SVegard Nossum int ret; 2592eca6f534SVegard Nossum char *kernel_type; 259391a27b2aSJeff Layton struct filename *kernel_dir; 2594eca6f534SVegard Nossum char *kernel_dev; 25951da177e4SLinus Torvalds unsigned long data_page; 25961da177e4SLinus Torvalds 2597eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2598eca6f534SVegard Nossum if (ret < 0) 2599eca6f534SVegard Nossum goto out_type; 26001da177e4SLinus Torvalds 2601eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2602eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2603eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2604eca6f534SVegard Nossum goto out_dir; 2605eca6f534SVegard Nossum } 26061da177e4SLinus Torvalds 2607eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2608eca6f534SVegard Nossum if (ret < 0) 2609eca6f534SVegard Nossum goto out_dev; 26101da177e4SLinus Torvalds 2611eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2612eca6f534SVegard Nossum if (ret < 0) 2613eca6f534SVegard Nossum goto out_data; 26141da177e4SLinus Torvalds 261591a27b2aSJeff Layton ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags, 2616eca6f534SVegard Nossum (void *) data_page); 2617eca6f534SVegard Nossum 26181da177e4SLinus Torvalds free_page(data_page); 2619eca6f534SVegard Nossum out_data: 2620eca6f534SVegard Nossum kfree(kernel_dev); 2621eca6f534SVegard Nossum out_dev: 2622eca6f534SVegard Nossum putname(kernel_dir); 2623eca6f534SVegard Nossum out_dir: 2624eca6f534SVegard Nossum kfree(kernel_type); 2625eca6f534SVegard Nossum out_type: 2626eca6f534SVegard Nossum return ret; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds /* 2630afac7cbaSAl Viro * Return true if path is reachable from root 2631afac7cbaSAl Viro * 263248a066e7SAl Viro * namespace_sem or mount_lock is held 2633afac7cbaSAl Viro */ 2634643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2635afac7cbaSAl Viro const struct path *root) 2636afac7cbaSAl Viro { 2637643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2638a73324daSAl Viro dentry = mnt->mnt_mountpoint; 26390714a533SAl Viro mnt = mnt->mnt_parent; 2640afac7cbaSAl Viro } 2641643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2642afac7cbaSAl Viro } 2643afac7cbaSAl Viro 2644afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2645afac7cbaSAl Viro { 2646afac7cbaSAl Viro int res; 264748a066e7SAl Viro read_seqlock_excl(&mount_lock); 2648643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 264948a066e7SAl Viro read_sequnlock_excl(&mount_lock); 2650afac7cbaSAl Viro return res; 2651afac7cbaSAl Viro } 2652afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2653afac7cbaSAl Viro 2654afac7cbaSAl Viro /* 26551da177e4SLinus Torvalds * pivot_root Semantics: 26561da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 26571da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 26581da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 26591da177e4SLinus Torvalds * 26601da177e4SLinus Torvalds * Restrictions: 26611da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 26621da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 26631da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 26641da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 26651da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 26661da177e4SLinus Torvalds * 26674a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 26684a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 26694a0d11faSNeil Brown * in this situation. 26704a0d11faSNeil Brown * 26711da177e4SLinus Torvalds * Notes: 26721da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 26731da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 26741da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 26751da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 26761da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 26771da177e4SLinus Torvalds * first. 26781da177e4SLinus Torvalds */ 26793480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 26803480b257SHeiko Carstens const char __user *, put_old) 26811da177e4SLinus Torvalds { 26822d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 268384d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 268484d17192SAl Viro struct mountpoint *old_mp, *root_mp; 26851da177e4SLinus Torvalds int error; 26861da177e4SLinus Torvalds 26879b40bc90SAl Viro if (!may_mount()) 26881da177e4SLinus Torvalds return -EPERM; 26891da177e4SLinus Torvalds 26902d8f3038SAl Viro error = user_path_dir(new_root, &new); 26911da177e4SLinus Torvalds if (error) 26921da177e4SLinus Torvalds goto out0; 26931da177e4SLinus Torvalds 26942d8f3038SAl Viro error = user_path_dir(put_old, &old); 26951da177e4SLinus Torvalds if (error) 26961da177e4SLinus Torvalds goto out1; 26971da177e4SLinus Torvalds 26982d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2699b12cea91SAl Viro if (error) 2700b12cea91SAl Viro goto out2; 27011da177e4SLinus Torvalds 2702f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 270384d17192SAl Viro old_mp = lock_mount(&old); 270484d17192SAl Viro error = PTR_ERR(old_mp); 270584d17192SAl Viro if (IS_ERR(old_mp)) 2706b12cea91SAl Viro goto out3; 2707b12cea91SAl Viro 27081da177e4SLinus Torvalds error = -EINVAL; 2709419148daSAl Viro new_mnt = real_mount(new.mnt); 2710419148daSAl Viro root_mnt = real_mount(root.mnt); 271184d17192SAl Viro old_mnt = real_mount(old.mnt); 271284d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 2713fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2714fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2715b12cea91SAl Viro goto out4; 2716143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2717b12cea91SAl Viro goto out4; 27185ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 27195ff9d8a6SEric W. Biederman goto out4; 27201da177e4SLinus Torvalds error = -ENOENT; 2721f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2722b12cea91SAl Viro goto out4; 27231da177e4SLinus Torvalds error = -EBUSY; 272484d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 2725b12cea91SAl Viro goto out4; /* loop, on the same file system */ 27261da177e4SLinus Torvalds error = -EINVAL; 27278c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2728b12cea91SAl Viro goto out4; /* not a mountpoint */ 2729676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2730b12cea91SAl Viro goto out4; /* not attached */ 273184d17192SAl Viro root_mp = root_mnt->mnt_mp; 27322d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2733b12cea91SAl Viro goto out4; /* not a mountpoint */ 2734676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2735b12cea91SAl Viro goto out4; /* not attached */ 27364ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 273784d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 2738b12cea91SAl Viro goto out4; 273984d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 2740719ea2fbSAl Viro lock_mount_hash(); 2741419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2742419148daSAl Viro detach_mnt(root_mnt, &root_parent); 27435ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 27445ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 27455ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 27465ff9d8a6SEric W. Biederman } 27474ac91378SJan Blunck /* mount old root on put_old */ 274884d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 27494ac91378SJan Blunck /* mount new_root on / */ 275084d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 27516b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2752719ea2fbSAl Viro unlock_mount_hash(); 27532d8f3038SAl Viro chroot_fs_refs(&root, &new); 275484d17192SAl Viro put_mountpoint(root_mp); 27551da177e4SLinus Torvalds error = 0; 2756b12cea91SAl Viro out4: 275784d17192SAl Viro unlock_mount(old_mp); 2758b12cea91SAl Viro if (!error) { 27591a390689SAl Viro path_put(&root_parent); 27601a390689SAl Viro path_put(&parent_path); 2761b12cea91SAl Viro } 2762b12cea91SAl Viro out3: 27638c3ee42eSAl Viro path_put(&root); 2764b12cea91SAl Viro out2: 27652d8f3038SAl Viro path_put(&old); 27661da177e4SLinus Torvalds out1: 27672d8f3038SAl Viro path_put(&new); 27681da177e4SLinus Torvalds out0: 27691da177e4SLinus Torvalds return error; 27701da177e4SLinus Torvalds } 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds static void __init init_mount_tree(void) 27731da177e4SLinus Torvalds { 27741da177e4SLinus Torvalds struct vfsmount *mnt; 27756b3286edSKirill Korotaev struct mnt_namespace *ns; 2776ac748a09SJan Blunck struct path root; 27770c55cfc4SEric W. Biederman struct file_system_type *type; 27781da177e4SLinus Torvalds 27790c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 27800c55cfc4SEric W. Biederman if (!type) 27810c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 27820c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 27830c55cfc4SEric W. Biederman put_filesystem(type); 27841da177e4SLinus Torvalds if (IS_ERR(mnt)) 27851da177e4SLinus Torvalds panic("Can't create rootfs"); 2786b3e19d92SNick Piggin 27873b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 27883b22edc5STrond Myklebust if (IS_ERR(ns)) 27891da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 27901da177e4SLinus Torvalds 27916b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 27926b3286edSKirill Korotaev get_mnt_ns(ns); 27931da177e4SLinus Torvalds 2794be08d6d2SAl Viro root.mnt = mnt; 2795be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2796ac748a09SJan Blunck 2797ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2798ac748a09SJan Blunck set_fs_root(current->fs, &root); 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds 280174bf17cfSDenis Cheng void __init mnt_init(void) 28021da177e4SLinus Torvalds { 280313f14b4dSEric Dumazet unsigned u; 280415a67dd8SRandy Dunlap int err; 28051da177e4SLinus Torvalds 28067d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 280720c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 28081da177e4SLinus Torvalds 28090818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 28100818bf27SAl Viro sizeof(struct list_head), 28110818bf27SAl Viro mhash_entries, 19, 28120818bf27SAl Viro 0, 28130818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 28140818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 28150818bf27SAl Viro sizeof(struct hlist_head), 28160818bf27SAl Viro mphash_entries, 19, 28170818bf27SAl Viro 0, 28180818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 28191da177e4SLinus Torvalds 282084d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 28211da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 28221da177e4SLinus Torvalds 28230818bf27SAl Viro for (u = 0; u <= m_hash_mask; u++) 282413f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 28250818bf27SAl Viro for (u = 0; u <= mp_hash_mask; u++) 28260818bf27SAl Viro INIT_HLIST_HEAD(&mountpoint_hashtable[u]); 28271da177e4SLinus Torvalds 28284b93dc9bSTejun Heo kernfs_init(); 28294b93dc9bSTejun Heo 283015a67dd8SRandy Dunlap err = sysfs_init(); 283115a67dd8SRandy Dunlap if (err) 283215a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 28338e24eea7SHarvey Harrison __func__, err); 283400d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 283500d26666SGreg Kroah-Hartman if (!fs_kobj) 28368e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 28371da177e4SLinus Torvalds init_rootfs(); 28381da177e4SLinus Torvalds init_mount_tree(); 28391da177e4SLinus Torvalds } 28401da177e4SLinus Torvalds 2841616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 28421da177e4SLinus Torvalds { 2843d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2844616511d0STrond Myklebust return; 28457b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 2846771b1371SEric W. Biederman free_mnt_ns(ns); 28471da177e4SLinus Torvalds } 28489d412a43SAl Viro 28499d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 28509d412a43SAl Viro { 2851423e0ab0STim Chen struct vfsmount *mnt; 2852423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2853423e0ab0STim Chen if (!IS_ERR(mnt)) { 2854423e0ab0STim Chen /* 2855423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2856423e0ab0STim Chen * we unmount before file sys is unregistered 2857423e0ab0STim Chen */ 2858f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2859423e0ab0STim Chen } 2860423e0ab0STim Chen return mnt; 28619d412a43SAl Viro } 28629d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2863423e0ab0STim Chen 2864423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2865423e0ab0STim Chen { 2866423e0ab0STim Chen /* release long term mount so mount point can be released */ 2867423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2868f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 286948a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 2870423e0ab0STim Chen mntput(mnt); 2871423e0ab0STim Chen } 2872423e0ab0STim Chen } 2873423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 287402125a82SAl Viro 287502125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 287602125a82SAl Viro { 2877143c8c91SAl Viro return check_mnt(real_mount(mnt)); 287802125a82SAl Viro } 28798823c079SEric W. Biederman 28803151527eSEric W. Biederman bool current_chrooted(void) 28813151527eSEric W. Biederman { 28823151527eSEric W. Biederman /* Does the current process have a non-standard root */ 28833151527eSEric W. Biederman struct path ns_root; 28843151527eSEric W. Biederman struct path fs_root; 28853151527eSEric W. Biederman bool chrooted; 28863151527eSEric W. Biederman 28873151527eSEric W. Biederman /* Find the namespace root */ 28883151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 28893151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 28903151527eSEric W. Biederman path_get(&ns_root); 28913151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 28923151527eSEric W. Biederman ; 28933151527eSEric W. Biederman 28943151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 28953151527eSEric W. Biederman 28963151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 28973151527eSEric W. Biederman 28983151527eSEric W. Biederman path_put(&fs_root); 28993151527eSEric W. Biederman path_put(&ns_root); 29003151527eSEric W. Biederman 29013151527eSEric W. Biederman return chrooted; 29023151527eSEric W. Biederman } 29033151527eSEric W. Biederman 2904e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type) 290587a8ebd6SEric W. Biederman { 290687a8ebd6SEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 290787a8ebd6SEric W. Biederman struct mount *mnt; 2908e51db735SEric W. Biederman bool visible = false; 290987a8ebd6SEric W. Biederman 2910e51db735SEric W. Biederman if (unlikely(!ns)) 2911e51db735SEric W. Biederman return false; 2912e51db735SEric W. Biederman 291344bb4385SAl Viro down_read(&namespace_sem); 291487a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 2915e51db735SEric W. Biederman struct mount *child; 2916e51db735SEric W. Biederman if (mnt->mnt.mnt_sb->s_type != type) 2917e51db735SEric W. Biederman continue; 2918e51db735SEric W. Biederman 2919e51db735SEric W. Biederman /* This mount is not fully visible if there are any child mounts 2920e51db735SEric W. Biederman * that cover anything except for empty directories. 2921e51db735SEric W. Biederman */ 2922e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 2923e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 2924e51db735SEric W. Biederman if (!S_ISDIR(inode->i_mode)) 2925e51db735SEric W. Biederman goto next; 292641301ae7SEric W. Biederman if (inode->i_nlink > 2) 2927e51db735SEric W. Biederman goto next; 292887a8ebd6SEric W. Biederman } 2929e51db735SEric W. Biederman visible = true; 2930e51db735SEric W. Biederman goto found; 2931e51db735SEric W. Biederman next: ; 293287a8ebd6SEric W. Biederman } 2933e51db735SEric W. Biederman found: 293444bb4385SAl Viro up_read(&namespace_sem); 2935e51db735SEric W. Biederman return visible; 293687a8ebd6SEric W. Biederman } 293787a8ebd6SEric W. Biederman 29388823c079SEric W. Biederman static void *mntns_get(struct task_struct *task) 29398823c079SEric W. Biederman { 29408823c079SEric W. Biederman struct mnt_namespace *ns = NULL; 29418823c079SEric W. Biederman struct nsproxy *nsproxy; 29428823c079SEric W. Biederman 29438823c079SEric W. Biederman rcu_read_lock(); 29448823c079SEric W. Biederman nsproxy = task_nsproxy(task); 29458823c079SEric W. Biederman if (nsproxy) { 29468823c079SEric W. Biederman ns = nsproxy->mnt_ns; 29478823c079SEric W. Biederman get_mnt_ns(ns); 29488823c079SEric W. Biederman } 29498823c079SEric W. Biederman rcu_read_unlock(); 29508823c079SEric W. Biederman 29518823c079SEric W. Biederman return ns; 29528823c079SEric W. Biederman } 29538823c079SEric W. Biederman 29548823c079SEric W. Biederman static void mntns_put(void *ns) 29558823c079SEric W. Biederman { 29568823c079SEric W. Biederman put_mnt_ns(ns); 29578823c079SEric W. Biederman } 29588823c079SEric W. Biederman 29598823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns) 29608823c079SEric W. Biederman { 29618823c079SEric W. Biederman struct fs_struct *fs = current->fs; 29628823c079SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 29638823c079SEric W. Biederman struct path root; 29648823c079SEric W. Biederman 29650c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 2966c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 2967c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 2968ae11e0f1SZhao Hongjiang return -EPERM; 29698823c079SEric W. Biederman 29708823c079SEric W. Biederman if (fs->users != 1) 29718823c079SEric W. Biederman return -EINVAL; 29728823c079SEric W. Biederman 29738823c079SEric W. Biederman get_mnt_ns(mnt_ns); 29748823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 29758823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 29768823c079SEric W. Biederman 29778823c079SEric W. Biederman /* Find the root */ 29788823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 29798823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 29808823c079SEric W. Biederman path_get(&root); 29818823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 29828823c079SEric W. Biederman ; 29838823c079SEric W. Biederman 29848823c079SEric W. Biederman /* Update the pwd and root */ 29858823c079SEric W. Biederman set_fs_pwd(fs, &root); 29868823c079SEric W. Biederman set_fs_root(fs, &root); 29878823c079SEric W. Biederman 29888823c079SEric W. Biederman path_put(&root); 29898823c079SEric W. Biederman return 0; 29908823c079SEric W. Biederman } 29918823c079SEric W. Biederman 299298f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns) 299398f842e6SEric W. Biederman { 299498f842e6SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 299598f842e6SEric W. Biederman return mnt_ns->proc_inum; 299698f842e6SEric W. Biederman } 299798f842e6SEric W. Biederman 29988823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 29998823c079SEric W. Biederman .name = "mnt", 30008823c079SEric W. Biederman .type = CLONE_NEWNS, 30018823c079SEric W. Biederman .get = mntns_get, 30028823c079SEric W. Biederman .put = mntns_put, 30038823c079SEric W. Biederman .install = mntns_install, 300498f842e6SEric W. Biederman .inum = mntns_inum, 30058823c079SEric W. Biederman }; 3006