11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namespace.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright Al Viro 2000, 2001 51da177e4SLinus Torvalds * Released under GPL v2. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Based on code from fs/super.c, copyright Linus Torvalds and others. 81da177e4SLinus Torvalds * Heavily rewritten. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/syscalls.h> 12d10577a8SAl Viro #include <linux/export.h> 1316f7e0feSRandy Dunlap #include <linux/capability.h> 146b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 15771b1371SEric W. Biederman #include <linux/user_namespace.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 171da177e4SLinus Torvalds #include <linux/security.h> 1873cd49ecSMiklos Szeredi #include <linux/idr.h> 1957f150a5SRob Landley #include <linux/init.h> /* init_rootfs */ 20d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 21d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 22d10577a8SAl Viro #include <linux/uaccess.h> 230bb80f24SDavid Howells #include <linux/proc_ns.h> 2420b4fb48SLinus Torvalds #include <linux/magic.h> 250818bf27SAl Viro #include <linux/bootmem.h> 2607b20889SRam Pai #include "pnode.h" 27948730b0SAdrian Bunk #include "internal.h" 281da177e4SLinus Torvalds 290818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 300818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 310818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 320818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 330818bf27SAl Viro 340818bf27SAl Viro static __initdata unsigned long mhash_entries; 350818bf27SAl Viro static int __init set_mhash_entries(char *str) 360818bf27SAl Viro { 370818bf27SAl Viro if (!str) 380818bf27SAl Viro return 0; 390818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 400818bf27SAl Viro return 1; 410818bf27SAl Viro } 420818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 430818bf27SAl Viro 440818bf27SAl Viro static __initdata unsigned long mphash_entries; 450818bf27SAl Viro static int __init set_mphash_entries(char *str) 460818bf27SAl Viro { 470818bf27SAl Viro if (!str) 480818bf27SAl Viro return 0; 490818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 500818bf27SAl Viro return 1; 510818bf27SAl Viro } 520818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 5313f14b4dSEric Dumazet 54c7999c36SAl Viro static u64 event; 5573cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 56719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 5799b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 58f21f6220SAl Viro static int mnt_id_start = 0; 59f21f6220SAl Viro static int mnt_group_start = 1; 605addc5ddSAl Viro 6138129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 620818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 63e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 6459aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 651da177e4SLinus Torvalds 66f87fd4c2SMiklos Szeredi /* /sys/fs */ 6700d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 6800d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 69f87fd4c2SMiklos Szeredi 7099b7db7bSNick Piggin /* 7199b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 7299b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 7399b7db7bSNick Piggin * up the tree. 7499b7db7bSNick Piggin * 7599b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 7699b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 7799b7db7bSNick Piggin */ 7848a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 7999b7db7bSNick Piggin 8038129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 831da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 840818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 850818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 860818bf27SAl Viro } 870818bf27SAl Viro 880818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 890818bf27SAl Viro { 900818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 910818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 920818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds 9599b7db7bSNick Piggin /* 9699b7db7bSNick Piggin * allocation is serialized by namespace_sem, but we need the spinlock to 9799b7db7bSNick Piggin * serialize with freeing. 9899b7db7bSNick Piggin */ 99b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 10073cd49ecSMiklos Szeredi { 10173cd49ecSMiklos Szeredi int res; 10273cd49ecSMiklos Szeredi 10373cd49ecSMiklos Szeredi retry: 10473cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 10599b7db7bSNick Piggin spin_lock(&mnt_id_lock); 10615169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 107f21f6220SAl Viro if (!res) 10815169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 10999b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 11073cd49ecSMiklos Szeredi if (res == -EAGAIN) 11173cd49ecSMiklos Szeredi goto retry; 11273cd49ecSMiklos Szeredi 11373cd49ecSMiklos Szeredi return res; 11473cd49ecSMiklos Szeredi } 11573cd49ecSMiklos Szeredi 116b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 11773cd49ecSMiklos Szeredi { 11815169fe7SAl Viro int id = mnt->mnt_id; 11999b7db7bSNick Piggin spin_lock(&mnt_id_lock); 120f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 121f21f6220SAl Viro if (mnt_id_start > id) 122f21f6220SAl Viro mnt_id_start = id; 12399b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 12473cd49ecSMiklos Szeredi } 12573cd49ecSMiklos Szeredi 126719f5d7fSMiklos Szeredi /* 127719f5d7fSMiklos Szeredi * Allocate a new peer group ID 128719f5d7fSMiklos Szeredi * 129719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 130719f5d7fSMiklos Szeredi */ 1314b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 132719f5d7fSMiklos Szeredi { 133f21f6220SAl Viro int res; 134f21f6220SAl Viro 135719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 136719f5d7fSMiklos Szeredi return -ENOMEM; 137719f5d7fSMiklos Szeredi 138f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 139f21f6220SAl Viro mnt_group_start, 14015169fe7SAl Viro &mnt->mnt_group_id); 141f21f6220SAl Viro if (!res) 14215169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 143f21f6220SAl Viro 144f21f6220SAl Viro return res; 145719f5d7fSMiklos Szeredi } 146719f5d7fSMiklos Szeredi 147719f5d7fSMiklos Szeredi /* 148719f5d7fSMiklos Szeredi * Release a peer group ID 149719f5d7fSMiklos Szeredi */ 1504b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 151719f5d7fSMiklos Szeredi { 15215169fe7SAl Viro int id = mnt->mnt_group_id; 153f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 154f21f6220SAl Viro if (mnt_group_start > id) 155f21f6220SAl Viro mnt_group_start = id; 15615169fe7SAl Viro mnt->mnt_group_id = 0; 157719f5d7fSMiklos Szeredi } 158719f5d7fSMiklos Szeredi 159b3e19d92SNick Piggin /* 160b3e19d92SNick Piggin * vfsmount lock must be held for read 161b3e19d92SNick Piggin */ 16283adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 163b3e19d92SNick Piggin { 164b3e19d92SNick Piggin #ifdef CONFIG_SMP 16568e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 166b3e19d92SNick Piggin #else 167b3e19d92SNick Piggin preempt_disable(); 16868e8a9feSAl Viro mnt->mnt_count += n; 169b3e19d92SNick Piggin preempt_enable(); 170b3e19d92SNick Piggin #endif 171b3e19d92SNick Piggin } 172b3e19d92SNick Piggin 173b3e19d92SNick Piggin /* 174b3e19d92SNick Piggin * vfsmount lock must be held for write 175b3e19d92SNick Piggin */ 17683adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 177b3e19d92SNick Piggin { 178b3e19d92SNick Piggin #ifdef CONFIG_SMP 179f03c6599SAl Viro unsigned int count = 0; 180b3e19d92SNick Piggin int cpu; 181b3e19d92SNick Piggin 182b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 18368e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 184b3e19d92SNick Piggin } 185b3e19d92SNick Piggin 186b3e19d92SNick Piggin return count; 187b3e19d92SNick Piggin #else 18868e8a9feSAl Viro return mnt->mnt_count; 189b3e19d92SNick Piggin #endif 190b3e19d92SNick Piggin } 191b3e19d92SNick Piggin 192b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1931da177e4SLinus Torvalds { 194c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 195c63181e6SAl Viro if (mnt) { 19673cd49ecSMiklos Szeredi int err; 19773cd49ecSMiklos Szeredi 198c63181e6SAl Viro err = mnt_alloc_id(mnt); 19988b38782SLi Zefan if (err) 20088b38782SLi Zefan goto out_free_cache; 20188b38782SLi Zefan 20288b38782SLi Zefan if (name) { 203c63181e6SAl Viro mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 204c63181e6SAl Viro if (!mnt->mnt_devname) 20588b38782SLi Zefan goto out_free_id; 20673cd49ecSMiklos Szeredi } 20773cd49ecSMiklos Szeredi 208b3e19d92SNick Piggin #ifdef CONFIG_SMP 209c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 210c63181e6SAl Viro if (!mnt->mnt_pcp) 211b3e19d92SNick Piggin goto out_free_devname; 212b3e19d92SNick Piggin 213c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 214b3e19d92SNick Piggin #else 215c63181e6SAl Viro mnt->mnt_count = 1; 216c63181e6SAl Viro mnt->mnt_writers = 0; 217b3e19d92SNick Piggin #endif 218b3e19d92SNick Piggin 21938129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 220c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 221c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 222c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 223c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 224c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 225c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 226c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2272504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 2282504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 2292504c5d6SAndreas Gruenbacher #endif 2301da177e4SLinus Torvalds } 231c63181e6SAl Viro return mnt; 23288b38782SLi Zefan 233d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 234d3ef3d73Snpiggin@suse.de out_free_devname: 235c63181e6SAl Viro kfree(mnt->mnt_devname); 236d3ef3d73Snpiggin@suse.de #endif 23788b38782SLi Zefan out_free_id: 238c63181e6SAl Viro mnt_free_id(mnt); 23988b38782SLi Zefan out_free_cache: 240c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 24188b38782SLi Zefan return NULL; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2448366025eSDave Hansen /* 2458366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2468366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2478366025eSDave Hansen * We must keep track of when those operations start 2488366025eSDave Hansen * (for permission checks) and when they end, so that 2498366025eSDave Hansen * we can determine when writes are able to occur to 2508366025eSDave Hansen * a filesystem. 2518366025eSDave Hansen */ 2523d733633SDave Hansen /* 2533d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2543d733633SDave Hansen * @mnt: the mount to check for its write status 2553d733633SDave Hansen * 2563d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2573d733633SDave Hansen * It does not guarantee that the filesystem will stay 2583d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2593d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2603d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2613d733633SDave Hansen * r/w. 2623d733633SDave Hansen */ 2633d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2643d733633SDave Hansen { 2652e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2662e4b7fcdSDave Hansen return 1; 2672e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2682e4b7fcdSDave Hansen return 1; 2692e4b7fcdSDave Hansen return 0; 2703d733633SDave Hansen } 2713d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2723d733633SDave Hansen 27383adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2743d733633SDave Hansen { 275d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 27668e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 277d3ef3d73Snpiggin@suse.de #else 27868e8a9feSAl Viro mnt->mnt_writers++; 279d3ef3d73Snpiggin@suse.de #endif 2803d733633SDave Hansen } 2813d733633SDave Hansen 28283adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2833d733633SDave Hansen { 284d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 28568e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 286d3ef3d73Snpiggin@suse.de #else 28768e8a9feSAl Viro mnt->mnt_writers--; 288d3ef3d73Snpiggin@suse.de #endif 289d3ef3d73Snpiggin@suse.de } 290d3ef3d73Snpiggin@suse.de 29183adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 292d3ef3d73Snpiggin@suse.de { 293d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 294d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2953d733633SDave Hansen int cpu; 2963d733633SDave Hansen 2973d733633SDave Hansen for_each_possible_cpu(cpu) { 29868e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2993d733633SDave Hansen } 3003d733633SDave Hansen 301d3ef3d73Snpiggin@suse.de return count; 302d3ef3d73Snpiggin@suse.de #else 303d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 304d3ef3d73Snpiggin@suse.de #endif 3053d733633SDave Hansen } 3063d733633SDave Hansen 3074ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 3084ed5e82fSMiklos Szeredi { 3094ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3104ed5e82fSMiklos Szeredi return 1; 3114ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3124ed5e82fSMiklos Szeredi smp_rmb(); 3134ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3144ed5e82fSMiklos Szeredi } 3154ed5e82fSMiklos Szeredi 3163d733633SDave Hansen /* 317eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 318eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 319eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 320eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3213d733633SDave Hansen */ 3228366025eSDave Hansen /** 323eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 32483adc753SAl Viro * @m: the mount on which to take a write 3258366025eSDave Hansen * 326eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 327eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 328eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 329eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 330eb04c282SJan Kara * called. This is effectively a refcount. 3318366025eSDave Hansen */ 332eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3338366025eSDave Hansen { 33483adc753SAl Viro struct mount *mnt = real_mount(m); 3353d733633SDave Hansen int ret = 0; 3363d733633SDave Hansen 337d3ef3d73Snpiggin@suse.de preempt_disable(); 338c6653a83SNick Piggin mnt_inc_writers(mnt); 339d3ef3d73Snpiggin@suse.de /* 340c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 341d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 342d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 343d3ef3d73Snpiggin@suse.de */ 344d3ef3d73Snpiggin@suse.de smp_mb(); 3451e75529eSMiao Xie while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 346d3ef3d73Snpiggin@suse.de cpu_relax(); 347d3ef3d73Snpiggin@suse.de /* 348d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 349d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 350d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 351d3ef3d73Snpiggin@suse.de */ 352d3ef3d73Snpiggin@suse.de smp_rmb(); 3534ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 354c6653a83SNick Piggin mnt_dec_writers(mnt); 3553d733633SDave Hansen ret = -EROFS; 3563d733633SDave Hansen } 357d3ef3d73Snpiggin@suse.de preempt_enable(); 358eb04c282SJan Kara 359eb04c282SJan Kara return ret; 360eb04c282SJan Kara } 361eb04c282SJan Kara 362eb04c282SJan Kara /** 363eb04c282SJan Kara * mnt_want_write - get write access to a mount 364eb04c282SJan Kara * @m: the mount on which to take a write 365eb04c282SJan Kara * 366eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 367eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 368eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 369eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 370eb04c282SJan Kara */ 371eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 372eb04c282SJan Kara { 373eb04c282SJan Kara int ret; 374eb04c282SJan Kara 375eb04c282SJan Kara sb_start_write(m->mnt_sb); 376eb04c282SJan Kara ret = __mnt_want_write(m); 377eb04c282SJan Kara if (ret) 378eb04c282SJan Kara sb_end_write(m->mnt_sb); 3793d733633SDave Hansen return ret; 3808366025eSDave Hansen } 3818366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3828366025eSDave Hansen 3838366025eSDave Hansen /** 38496029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 38596029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 38696029c4eSnpiggin@suse.de * 38796029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 38896029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 38996029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 39096029c4eSnpiggin@suse.de * on it. This allows some optimisation. 39196029c4eSnpiggin@suse.de * 39296029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 39396029c4eSnpiggin@suse.de * drop the reference. 39496029c4eSnpiggin@suse.de */ 39596029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 39696029c4eSnpiggin@suse.de { 39796029c4eSnpiggin@suse.de /* superblock may be r/o */ 39896029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 39996029c4eSnpiggin@suse.de return -EROFS; 40096029c4eSnpiggin@suse.de preempt_disable(); 40183adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 40296029c4eSnpiggin@suse.de preempt_enable(); 40396029c4eSnpiggin@suse.de return 0; 40496029c4eSnpiggin@suse.de } 40596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 40696029c4eSnpiggin@suse.de 40796029c4eSnpiggin@suse.de /** 408eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 409eb04c282SJan Kara * @file: the file who's mount on which to take a write 410eb04c282SJan Kara * 411eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 412eb04c282SJan Kara * do some optimisations if the file is open for write already 413eb04c282SJan Kara */ 414eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 415eb04c282SJan Kara { 41683f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 417eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 418eb04c282SJan Kara else 419eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 420eb04c282SJan Kara } 421eb04c282SJan Kara 422eb04c282SJan Kara /** 42396029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 42496029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 42596029c4eSnpiggin@suse.de * 42696029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 42796029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 42896029c4eSnpiggin@suse.de */ 42996029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 43096029c4eSnpiggin@suse.de { 431eb04c282SJan Kara int ret; 432eb04c282SJan Kara 433eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 434eb04c282SJan Kara ret = __mnt_want_write_file(file); 435eb04c282SJan Kara if (ret) 436eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 437eb04c282SJan Kara return ret; 43896029c4eSnpiggin@suse.de } 43996029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 44096029c4eSnpiggin@suse.de 44196029c4eSnpiggin@suse.de /** 442eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4438366025eSDave Hansen * @mnt: the mount on which to give up write access 4448366025eSDave Hansen * 4458366025eSDave Hansen * Tells the low-level filesystem that we are done 4468366025eSDave Hansen * performing writes to it. Must be matched with 447eb04c282SJan Kara * __mnt_want_write() call above. 4488366025eSDave Hansen */ 449eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4508366025eSDave Hansen { 451d3ef3d73Snpiggin@suse.de preempt_disable(); 45283adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 453d3ef3d73Snpiggin@suse.de preempt_enable(); 4548366025eSDave Hansen } 455eb04c282SJan Kara 456eb04c282SJan Kara /** 457eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 458eb04c282SJan Kara * @mnt: the mount on which to give up write access 459eb04c282SJan Kara * 460eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 461eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 462eb04c282SJan Kara * mnt_want_write() call above. 463eb04c282SJan Kara */ 464eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 465eb04c282SJan Kara { 466eb04c282SJan Kara __mnt_drop_write(mnt); 467eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 468eb04c282SJan Kara } 4698366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4708366025eSDave Hansen 471eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 472eb04c282SJan Kara { 473eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 474eb04c282SJan Kara } 475eb04c282SJan Kara 4762a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4772a79f17eSAl Viro { 4782a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4792a79f17eSAl Viro } 4802a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4812a79f17eSAl Viro 48283adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4838366025eSDave Hansen { 4843d733633SDave Hansen int ret = 0; 4853d733633SDave Hansen 486719ea2fbSAl Viro lock_mount_hash(); 48783adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 488d3ef3d73Snpiggin@suse.de /* 489d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 490d3ef3d73Snpiggin@suse.de * should be visible before we do. 491d3ef3d73Snpiggin@suse.de */ 492d3ef3d73Snpiggin@suse.de smp_mb(); 493d3ef3d73Snpiggin@suse.de 494d3ef3d73Snpiggin@suse.de /* 495d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 496d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 497d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 498d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 499d3ef3d73Snpiggin@suse.de * 500d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 501d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 502d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 503d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 504d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 505d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 506d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 507d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 508d3ef3d73Snpiggin@suse.de * we're counting up here. 509d3ef3d73Snpiggin@suse.de */ 510c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 511d3ef3d73Snpiggin@suse.de ret = -EBUSY; 512d3ef3d73Snpiggin@suse.de else 51383adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 514d3ef3d73Snpiggin@suse.de /* 515d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 516d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 517d3ef3d73Snpiggin@suse.de */ 518d3ef3d73Snpiggin@suse.de smp_wmb(); 51983adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 520719ea2fbSAl Viro unlock_mount_hash(); 5213d733633SDave Hansen return ret; 5223d733633SDave Hansen } 5238366025eSDave Hansen 52483adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5252e4b7fcdSDave Hansen { 526719ea2fbSAl Viro lock_mount_hash(); 52783adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 528719ea2fbSAl Viro unlock_mount_hash(); 5292e4b7fcdSDave Hansen } 5302e4b7fcdSDave Hansen 5314ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5324ed5e82fSMiklos Szeredi { 5334ed5e82fSMiklos Szeredi struct mount *mnt; 5344ed5e82fSMiklos Szeredi int err = 0; 5354ed5e82fSMiklos Szeredi 5368e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5378e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5388e8b8796SMiklos Szeredi return -EBUSY; 5398e8b8796SMiklos Szeredi 540719ea2fbSAl Viro lock_mount_hash(); 5414ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5424ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5434ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5444ed5e82fSMiklos Szeredi smp_mb(); 5454ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5464ed5e82fSMiklos Szeredi err = -EBUSY; 5474ed5e82fSMiklos Szeredi break; 5484ed5e82fSMiklos Szeredi } 5494ed5e82fSMiklos Szeredi } 5504ed5e82fSMiklos Szeredi } 5518e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5528e8b8796SMiklos Szeredi err = -EBUSY; 5538e8b8796SMiklos Szeredi 5544ed5e82fSMiklos Szeredi if (!err) { 5554ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5564ed5e82fSMiklos Szeredi smp_wmb(); 5574ed5e82fSMiklos Szeredi } 5584ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5594ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5604ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5614ed5e82fSMiklos Szeredi } 562719ea2fbSAl Viro unlock_mount_hash(); 5634ed5e82fSMiklos Szeredi 5644ed5e82fSMiklos Szeredi return err; 5654ed5e82fSMiklos Szeredi } 5664ed5e82fSMiklos Szeredi 567b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5681da177e4SLinus Torvalds { 56952ba1621SAl Viro kfree(mnt->mnt_devname); 570d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 57168e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 572d3ef3d73Snpiggin@suse.de #endif 573b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5768ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5778ffcb32eSDavid Howells { 5788ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5798ffcb32eSDavid Howells } 5808ffcb32eSDavid Howells 58148a066e7SAl Viro /* call under rcu_read_lock */ 58248a066e7SAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 58348a066e7SAl Viro { 58448a066e7SAl Viro struct mount *mnt; 58548a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 58648a066e7SAl Viro return false; 58748a066e7SAl Viro if (bastard == NULL) 58848a066e7SAl Viro return true; 58948a066e7SAl Viro mnt = real_mount(bastard); 59048a066e7SAl Viro mnt_add_count(mnt, 1); 59148a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 59248a066e7SAl Viro return true; 59348a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 59448a066e7SAl Viro mnt_add_count(mnt, -1); 59548a066e7SAl Viro return false; 59648a066e7SAl Viro } 59748a066e7SAl Viro rcu_read_unlock(); 59848a066e7SAl Viro mntput(bastard); 59948a066e7SAl Viro rcu_read_lock(); 60048a066e7SAl Viro return false; 60148a066e7SAl Viro } 60248a066e7SAl Viro 6031da177e4SLinus Torvalds /* 604474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 60548a066e7SAl Viro * call under rcu_read_lock() 6061da177e4SLinus Torvalds */ 607474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6081da177e4SLinus Torvalds { 60938129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 610474279dcSAl Viro struct mount *p; 6111da177e4SLinus Torvalds 61238129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 613474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 614474279dcSAl Viro return p; 615474279dcSAl Viro return NULL; 6161da177e4SLinus Torvalds } 617474279dcSAl Viro 618474279dcSAl Viro /* 619474279dcSAl Viro * find the last mount at @dentry on vfsmount @mnt. 62048a066e7SAl Viro * mount_lock must be held. 621474279dcSAl Viro */ 622474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry) 623474279dcSAl Viro { 62438129a13SAl Viro struct mount *p, *res; 62538129a13SAl Viro res = p = __lookup_mnt(mnt, dentry); 62638129a13SAl Viro if (!p) 62738129a13SAl Viro goto out; 62838129a13SAl Viro hlist_for_each_entry_continue(p, mnt_hash) { 6291d6a32acSAl Viro if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry) 6301d6a32acSAl Viro break; 6311d6a32acSAl Viro res = p; 6321d6a32acSAl Viro } 63338129a13SAl Viro out: 6341d6a32acSAl Viro return res; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 637a05964f3SRam Pai /* 638f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 639f015f126SDavid Howells * 640f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 641f015f126SDavid Howells * following mounts: 642f015f126SDavid Howells * 643f015f126SDavid Howells * mount /dev/sda1 /mnt 644f015f126SDavid Howells * mount /dev/sda2 /mnt 645f015f126SDavid Howells * mount /dev/sda3 /mnt 646f015f126SDavid Howells * 647f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 648f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 649f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 650f015f126SDavid Howells * 651f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 652a05964f3SRam Pai */ 6531c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 654a05964f3SRam Pai { 655c7105365SAl Viro struct mount *child_mnt; 65648a066e7SAl Viro struct vfsmount *m; 65748a066e7SAl Viro unsigned seq; 65899b7db7bSNick Piggin 65948a066e7SAl Viro rcu_read_lock(); 66048a066e7SAl Viro do { 66148a066e7SAl Viro seq = read_seqbegin(&mount_lock); 662474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 66348a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 66448a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 66548a066e7SAl Viro rcu_read_unlock(); 66648a066e7SAl Viro return m; 667a05964f3SRam Pai } 668a05964f3SRam Pai 66984d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry) 67084d17192SAl Viro { 6710818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 67284d17192SAl Viro struct mountpoint *mp; 673eed81007SMiklos Szeredi int ret; 67484d17192SAl Viro 6750818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 67684d17192SAl Viro if (mp->m_dentry == dentry) { 67784d17192SAl Viro /* might be worth a WARN_ON() */ 67884d17192SAl Viro if (d_unlinked(dentry)) 67984d17192SAl Viro return ERR_PTR(-ENOENT); 68084d17192SAl Viro mp->m_count++; 68184d17192SAl Viro return mp; 68284d17192SAl Viro } 68384d17192SAl Viro } 68484d17192SAl Viro 68584d17192SAl Viro mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 68684d17192SAl Viro if (!mp) 68784d17192SAl Viro return ERR_PTR(-ENOMEM); 68884d17192SAl Viro 689eed81007SMiklos Szeredi ret = d_set_mounted(dentry); 690eed81007SMiklos Szeredi if (ret) { 69184d17192SAl Viro kfree(mp); 692eed81007SMiklos Szeredi return ERR_PTR(ret); 69384d17192SAl Viro } 694eed81007SMiklos Szeredi 69584d17192SAl Viro mp->m_dentry = dentry; 69684d17192SAl Viro mp->m_count = 1; 6970818bf27SAl Viro hlist_add_head(&mp->m_hash, chain); 69884d17192SAl Viro return mp; 69984d17192SAl Viro } 70084d17192SAl Viro 70184d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 70284d17192SAl Viro { 70384d17192SAl Viro if (!--mp->m_count) { 70484d17192SAl Viro struct dentry *dentry = mp->m_dentry; 70584d17192SAl Viro spin_lock(&dentry->d_lock); 70684d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 70784d17192SAl Viro spin_unlock(&dentry->d_lock); 7080818bf27SAl Viro hlist_del(&mp->m_hash); 70984d17192SAl Viro kfree(mp); 71084d17192SAl Viro } 71184d17192SAl Viro } 71284d17192SAl Viro 713143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7141da177e4SLinus Torvalds { 7156b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds 71899b7db7bSNick Piggin /* 71999b7db7bSNick Piggin * vfsmount lock must be held for write 72099b7db7bSNick Piggin */ 7216b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7225addc5ddSAl Viro { 7235addc5ddSAl Viro if (ns) { 7245addc5ddSAl Viro ns->event = ++event; 7255addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7265addc5ddSAl Viro } 7275addc5ddSAl Viro } 7285addc5ddSAl Viro 72999b7db7bSNick Piggin /* 73099b7db7bSNick Piggin * vfsmount lock must be held for write 73199b7db7bSNick Piggin */ 7326b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7335addc5ddSAl Viro { 7345addc5ddSAl Viro if (ns && ns->event != event) { 7355addc5ddSAl Viro ns->event = event; 7365addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7375addc5ddSAl Viro } 7385addc5ddSAl Viro } 7395addc5ddSAl Viro 74099b7db7bSNick Piggin /* 74199b7db7bSNick Piggin * vfsmount lock must be held for write 74299b7db7bSNick Piggin */ 743419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 7441da177e4SLinus Torvalds { 745a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 7460714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 7470714a533SAl Viro mnt->mnt_parent = mnt; 748a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7496b41d536SAl Viro list_del_init(&mnt->mnt_child); 75038129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 75184d17192SAl Viro put_mountpoint(mnt->mnt_mp); 75284d17192SAl Viro mnt->mnt_mp = NULL; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 75599b7db7bSNick Piggin /* 75699b7db7bSNick Piggin * vfsmount lock must be held for write 75799b7db7bSNick Piggin */ 75884d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 75984d17192SAl Viro struct mountpoint *mp, 76044d964d6SAl Viro struct mount *child_mnt) 761b90fa9aeSRam Pai { 76284d17192SAl Viro mp->m_count++; 7633a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 76484d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 7653a2393d7SAl Viro child_mnt->mnt_parent = mnt; 76684d17192SAl Viro child_mnt->mnt_mp = mp; 767b90fa9aeSRam Pai } 768b90fa9aeSRam Pai 76999b7db7bSNick Piggin /* 77099b7db7bSNick Piggin * vfsmount lock must be held for write 77199b7db7bSNick Piggin */ 77284d17192SAl Viro static void attach_mnt(struct mount *mnt, 77384d17192SAl Viro struct mount *parent, 77484d17192SAl Viro struct mountpoint *mp) 7751da177e4SLinus Torvalds { 77684d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 77738129a13SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry)); 77884d17192SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 779b90fa9aeSRam Pai } 780b90fa9aeSRam Pai 78112a5b529SAl Viro static void attach_shadowed(struct mount *mnt, 78212a5b529SAl Viro struct mount *parent, 78312a5b529SAl Viro struct mount *shadows) 78412a5b529SAl Viro { 78512a5b529SAl Viro if (shadows) { 786f6f99332SLinus Torvalds hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash); 78712a5b529SAl Viro list_add(&mnt->mnt_child, &shadows->mnt_child); 78812a5b529SAl Viro } else { 78912a5b529SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, 79012a5b529SAl Viro m_hash(&parent->mnt, mnt->mnt_mountpoint)); 79112a5b529SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 79212a5b529SAl Viro } 79312a5b529SAl Viro } 79412a5b529SAl Viro 795b90fa9aeSRam Pai /* 79699b7db7bSNick Piggin * vfsmount lock must be held for write 797b90fa9aeSRam Pai */ 7981d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows) 799b90fa9aeSRam Pai { 8000714a533SAl Viro struct mount *parent = mnt->mnt_parent; 80183adc753SAl Viro struct mount *m; 802b90fa9aeSRam Pai LIST_HEAD(head); 803143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 804b90fa9aeSRam Pai 8050714a533SAl Viro BUG_ON(parent == mnt); 806b90fa9aeSRam Pai 8071a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 808f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 809143c8c91SAl Viro m->mnt_ns = n; 810f03c6599SAl Viro 811b90fa9aeSRam Pai list_splice(&head, n->list.prev); 812b90fa9aeSRam Pai 81312a5b529SAl Viro attach_shadowed(mnt, parent, shadows); 8146b3286edSKirill Korotaev touch_mnt_namespace(n); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 817909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 8181da177e4SLinus Torvalds { 8196b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 8206b41d536SAl Viro if (next == &p->mnt_mounts) { 8211da177e4SLinus Torvalds while (1) { 822909b0a88SAl Viro if (p == root) 8231da177e4SLinus Torvalds return NULL; 8246b41d536SAl Viro next = p->mnt_child.next; 8256b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 8261da177e4SLinus Torvalds break; 8270714a533SAl Viro p = p->mnt_parent; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds } 8306b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 833315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 8349676f0c6SRam Pai { 8356b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 8366b41d536SAl Viro while (prev != &p->mnt_mounts) { 8376b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 8386b41d536SAl Viro prev = p->mnt_mounts.prev; 8399676f0c6SRam Pai } 8409676f0c6SRam Pai return p; 8419676f0c6SRam Pai } 8429676f0c6SRam Pai 8439d412a43SAl Viro struct vfsmount * 8449d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 8459d412a43SAl Viro { 846b105e270SAl Viro struct mount *mnt; 8479d412a43SAl Viro struct dentry *root; 8489d412a43SAl Viro 8499d412a43SAl Viro if (!type) 8509d412a43SAl Viro return ERR_PTR(-ENODEV); 8519d412a43SAl Viro 8529d412a43SAl Viro mnt = alloc_vfsmnt(name); 8539d412a43SAl Viro if (!mnt) 8549d412a43SAl Viro return ERR_PTR(-ENOMEM); 8559d412a43SAl Viro 8569d412a43SAl Viro if (flags & MS_KERNMOUNT) 857b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 8589d412a43SAl Viro 8599d412a43SAl Viro root = mount_fs(type, flags, name, data); 8609d412a43SAl Viro if (IS_ERR(root)) { 8618ffcb32eSDavid Howells mnt_free_id(mnt); 8629d412a43SAl Viro free_vfsmnt(mnt); 8639d412a43SAl Viro return ERR_CAST(root); 8649d412a43SAl Viro } 8659d412a43SAl Viro 866b105e270SAl Viro mnt->mnt.mnt_root = root; 867b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 868a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8690714a533SAl Viro mnt->mnt_parent = mnt; 870719ea2fbSAl Viro lock_mount_hash(); 87139f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 872719ea2fbSAl Viro unlock_mount_hash(); 873b105e270SAl Viro return &mnt->mnt; 8749d412a43SAl Viro } 8759d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 8769d412a43SAl Viro 87787129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 87836341f64SRam Pai int flag) 8791da177e4SLinus Torvalds { 88087129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 881be34d1a3SDavid Howells struct mount *mnt; 882be34d1a3SDavid Howells int err; 8831da177e4SLinus Torvalds 884be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 885be34d1a3SDavid Howells if (!mnt) 886be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 887be34d1a3SDavid Howells 8887a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 88915169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 890719f5d7fSMiklos Szeredi else 89115169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 892719f5d7fSMiklos Szeredi 89315169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 894be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 895719f5d7fSMiklos Szeredi if (err) 896719f5d7fSMiklos Szeredi goto out_free; 897719f5d7fSMiklos Szeredi } 898719f5d7fSMiklos Szeredi 899f2ebb3a9SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED); 900132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 9019566d674SEric W. Biederman if (flag & CL_UNPRIVILEGED) { 9029566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_ATIME; 9039566d674SEric W. Biederman 9049566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_READONLY) 905132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 906132c94e3SEric W. Biederman 9079566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NODEV) 9089566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NODEV; 9099566d674SEric W. Biederman 9109566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOSUID) 9119566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID; 9129566d674SEric W. Biederman 9139566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOEXEC) 9149566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC; 9159566d674SEric W. Biederman } 9169566d674SEric W. Biederman 9175ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 9185ff9d8a6SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire)) 9195ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 9205ff9d8a6SEric W. Biederman 9211da177e4SLinus Torvalds atomic_inc(&sb->s_active); 922b105e270SAl Viro mnt->mnt.mnt_sb = sb; 923b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 924a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9250714a533SAl Viro mnt->mnt_parent = mnt; 926719ea2fbSAl Viro lock_mount_hash(); 92739f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 928719ea2fbSAl Viro unlock_mount_hash(); 929b90fa9aeSRam Pai 9307a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 9317a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 9326776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 93332301920SAl Viro mnt->mnt_master = old; 934fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 9358aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 936fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 9376776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 938d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 9396776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 940d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 9415afe0022SRam Pai } 942b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 9430f0afb1dSAl Viro set_mnt_shared(mnt); 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 9461da177e4SLinus Torvalds * as the original if that was on one */ 94736341f64SRam Pai if (flag & CL_EXPIRE) { 9486776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 9496776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 9501da177e4SLinus Torvalds } 951be34d1a3SDavid Howells 952cb338d06SAl Viro return mnt; 953719f5d7fSMiklos Szeredi 954719f5d7fSMiklos Szeredi out_free: 9558ffcb32eSDavid Howells mnt_free_id(mnt); 956719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 957be34d1a3SDavid Howells return ERR_PTR(err); 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 960900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 9617b7b1aceSAl Viro { 96248a066e7SAl Viro rcu_read_lock(); 963aa9c0e07SAl Viro mnt_add_count(mnt, -1); 96448a066e7SAl Viro if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */ 96548a066e7SAl Viro rcu_read_unlock(); 96699b7db7bSNick Piggin return; 967b3e19d92SNick Piggin } 968719ea2fbSAl Viro lock_mount_hash(); 969b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 97048a066e7SAl Viro rcu_read_unlock(); 971719ea2fbSAl Viro unlock_mount_hash(); 97299b7db7bSNick Piggin return; 97399b7db7bSNick Piggin } 97448a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 97548a066e7SAl Viro rcu_read_unlock(); 97648a066e7SAl Viro unlock_mount_hash(); 97748a066e7SAl Viro return; 97848a066e7SAl Viro } 97948a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 98048a066e7SAl Viro rcu_read_unlock(); 981962830dfSAndi Kleen 98239f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 983719ea2fbSAl Viro unlock_mount_hash(); 984649a795aSAl Viro 985649a795aSAl Viro /* 986649a795aSAl Viro * This probably indicates that somebody messed 987649a795aSAl Viro * up a mnt_want/drop_write() pair. If this 988649a795aSAl Viro * happens, the filesystem was probably unable 989649a795aSAl Viro * to make r/w->r/o transitions. 990649a795aSAl Viro */ 991649a795aSAl Viro /* 992649a795aSAl Viro * The locking used to deal with mnt_count decrement provides barriers, 993649a795aSAl Viro * so mnt_get_writers() below is safe. 994649a795aSAl Viro */ 995649a795aSAl Viro WARN_ON(mnt_get_writers(mnt)); 9963064c356SAl Viro if (unlikely(mnt->mnt_pins.first)) 9973064c356SAl Viro mnt_pin_kill(mnt); 998649a795aSAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 999649a795aSAl Viro dput(mnt->mnt.mnt_root); 1000649a795aSAl Viro deactivate_super(mnt->mnt.mnt_sb); 100148a066e7SAl Viro mnt_free_id(mnt); 10028ffcb32eSDavid Howells call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 1003b3e19d92SNick Piggin } 1004b3e19d92SNick Piggin 1005b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1006b3e19d92SNick Piggin { 1007b3e19d92SNick Piggin if (mnt) { 1008863d684fSAl Viro struct mount *m = real_mount(mnt); 1009b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1010863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1011863d684fSAl Viro m->mnt_expiry_mark = 0; 1012863d684fSAl Viro mntput_no_expire(m); 1013b3e19d92SNick Piggin } 1014b3e19d92SNick Piggin } 1015b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1016b3e19d92SNick Piggin 1017b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1018b3e19d92SNick Piggin { 1019b3e19d92SNick Piggin if (mnt) 102083adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1021b3e19d92SNick Piggin return mnt; 1022b3e19d92SNick Piggin } 1023b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1024b3e19d92SNick Piggin 10253064c356SAl Viro struct vfsmount *mnt_clone_internal(struct path *path) 10267b7b1aceSAl Viro { 10273064c356SAl Viro struct mount *p; 10283064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 10293064c356SAl Viro if (IS_ERR(p)) 10303064c356SAl Viro return ERR_CAST(p); 10313064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 10323064c356SAl Viro return &p->mnt; 10337b7b1aceSAl Viro } 10341da177e4SLinus Torvalds 1035b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 1036b3b304a2SMiklos Szeredi { 1037b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 1038b3b304a2SMiklos Szeredi } 1039b3b304a2SMiklos Szeredi 1040b3b304a2SMiklos Szeredi /* 1041b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 1042b3b304a2SMiklos Szeredi * implement more complex mount option showing. 1043b3b304a2SMiklos Szeredi * 1044b3b304a2SMiklos Szeredi * See also save_mount_options(). 1045b3b304a2SMiklos Szeredi */ 104634c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 1047b3b304a2SMiklos Szeredi { 10482a32cebdSAl Viro const char *options; 10492a32cebdSAl Viro 10502a32cebdSAl Viro rcu_read_lock(); 105134c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 1052b3b304a2SMiklos Szeredi 1053b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 1054b3b304a2SMiklos Szeredi seq_putc(m, ','); 1055b3b304a2SMiklos Szeredi mangle(m, options); 1056b3b304a2SMiklos Szeredi } 10572a32cebdSAl Viro rcu_read_unlock(); 1058b3b304a2SMiklos Szeredi 1059b3b304a2SMiklos Szeredi return 0; 1060b3b304a2SMiklos Szeredi } 1061b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 1062b3b304a2SMiklos Szeredi 1063b3b304a2SMiklos Szeredi /* 1064b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 1065b3b304a2SMiklos Szeredi * called from the fill_super() callback. 1066b3b304a2SMiklos Szeredi * 1067b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 1068b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 1069b3b304a2SMiklos Szeredi * remount fails. 1070b3b304a2SMiklos Szeredi * 1071b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 1072b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 1073b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 1074b3b304a2SMiklos Szeredi * any more. 1075b3b304a2SMiklos Szeredi */ 1076b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 1077b3b304a2SMiklos Szeredi { 10782a32cebdSAl Viro BUG_ON(sb->s_options); 10792a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 1080b3b304a2SMiklos Szeredi } 1081b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 1082b3b304a2SMiklos Szeredi 10832a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 10842a32cebdSAl Viro { 10852a32cebdSAl Viro char *old = sb->s_options; 10862a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 10872a32cebdSAl Viro if (old) { 10882a32cebdSAl Viro synchronize_rcu(); 10892a32cebdSAl Viro kfree(old); 10902a32cebdSAl Viro } 10912a32cebdSAl Viro } 10922a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 10932a32cebdSAl Viro 1094a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 10950226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 10961da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 10971da177e4SLinus Torvalds { 10986ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10991da177e4SLinus Torvalds 1100390c6843SRam Pai down_read(&namespace_sem); 1101c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1102c7999c36SAl Viro void *v = p->cached_mount; 1103c7999c36SAl Viro if (*pos == p->cached_index) 1104c7999c36SAl Viro return v; 1105c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1106c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1107c7999c36SAl Viro return p->cached_mount = v; 1108c7999c36SAl Viro } 1109c7999c36SAl Viro } 1110c7999c36SAl Viro 1111c7999c36SAl Viro p->cached_event = p->ns->event; 1112c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1113c7999c36SAl Viro p->cached_index = *pos; 1114c7999c36SAl Viro return p->cached_mount; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 11181da177e4SLinus Torvalds { 11196ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1120b0765fb8SPavel Emelianov 1121c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1122c7999c36SAl Viro p->cached_index = *pos; 1123c7999c36SAl Viro return p->cached_mount; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 11271da177e4SLinus Torvalds { 1128390c6843SRam Pai up_read(&namespace_sem); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11310226f492SAl Viro static int m_show(struct seq_file *m, void *v) 11329f5596afSAl Viro { 11336ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 11341a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 11350226f492SAl Viro return p->show(m, &r->mnt); 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 1138a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 11391da177e4SLinus Torvalds .start = m_start, 11401da177e4SLinus Torvalds .next = m_next, 11411da177e4SLinus Torvalds .stop = m_stop, 11420226f492SAl Viro .show = m_show, 1143b4629fe2SChuck Lever }; 1144a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1145b4629fe2SChuck Lever 11461da177e4SLinus Torvalds /** 11471da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 11481da177e4SLinus Torvalds * @mnt: root of mount tree 11491da177e4SLinus Torvalds * 11501da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 11511da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 11521da177e4SLinus Torvalds * busy. 11531da177e4SLinus Torvalds */ 1154909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 11551da177e4SLinus Torvalds { 1156909b0a88SAl Viro struct mount *mnt = real_mount(m); 115736341f64SRam Pai int actual_refs = 0; 115836341f64SRam Pai int minimum_refs = 0; 1159315fc83eSAl Viro struct mount *p; 1160909b0a88SAl Viro BUG_ON(!m); 11611da177e4SLinus Torvalds 1162b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1163719ea2fbSAl Viro lock_mount_hash(); 1164909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 116583adc753SAl Viro actual_refs += mnt_get_count(p); 11661da177e4SLinus Torvalds minimum_refs += 2; 11671da177e4SLinus Torvalds } 1168719ea2fbSAl Viro unlock_mount_hash(); 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds if (actual_refs > minimum_refs) 11711da177e4SLinus Torvalds return 0; 1172e3474a8eSIan Kent 1173e3474a8eSIan Kent return 1; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds /** 11791da177e4SLinus Torvalds * may_umount - check if a mount point is busy 11801da177e4SLinus Torvalds * @mnt: root of mount 11811da177e4SLinus Torvalds * 11821da177e4SLinus Torvalds * This is called to check if a mount point has any 11831da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 11841da177e4SLinus Torvalds * mount has sub mounts this will return busy 11851da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 11861da177e4SLinus Torvalds * 11871da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 11881da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 11891da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 11921da177e4SLinus Torvalds { 1193e3474a8eSIan Kent int ret = 1; 11948ad08d8aSAl Viro down_read(&namespace_sem); 1195719ea2fbSAl Viro lock_mount_hash(); 11961ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1197e3474a8eSIan Kent ret = 0; 1198719ea2fbSAl Viro unlock_mount_hash(); 11998ad08d8aSAl Viro up_read(&namespace_sem); 1200a05964f3SRam Pai return ret; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 12041da177e4SLinus Torvalds 120538129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1206e3197d83SAl Viro 120797216be0SAl Viro static void namespace_unlock(void) 12081da177e4SLinus Torvalds { 1209d5e50f74SAl Viro struct mount *mnt; 121038129a13SAl Viro struct hlist_head head = unmounted; 121197216be0SAl Viro 121238129a13SAl Viro if (likely(hlist_empty(&head))) { 121397216be0SAl Viro up_write(&namespace_sem); 121497216be0SAl Viro return; 121597216be0SAl Viro } 121697216be0SAl Viro 121738129a13SAl Viro head.first->pprev = &head.first; 121838129a13SAl Viro INIT_HLIST_HEAD(&unmounted); 121938129a13SAl Viro 122081b6b061SAl Viro /* undo decrements we'd done in umount_tree() */ 122181b6b061SAl Viro hlist_for_each_entry(mnt, &head, mnt_hash) 122281b6b061SAl Viro if (mnt->mnt_ex_mountpoint.mnt) 122381b6b061SAl Viro mntget(mnt->mnt_ex_mountpoint.mnt); 122481b6b061SAl Viro 122597216be0SAl Viro up_write(&namespace_sem); 122697216be0SAl Viro 122748a066e7SAl Viro synchronize_rcu(); 122848a066e7SAl Viro 122938129a13SAl Viro while (!hlist_empty(&head)) { 123038129a13SAl Viro mnt = hlist_entry(head.first, struct mount, mnt_hash); 123138129a13SAl Viro hlist_del_init(&mnt->mnt_hash); 1232aba809cfSAl Viro if (mnt->mnt_ex_mountpoint.mnt) 1233aba809cfSAl Viro path_put(&mnt->mnt_ex_mountpoint); 1234d5e50f74SAl Viro mntput(&mnt->mnt); 123570fbcdf4SRam Pai } 123670fbcdf4SRam Pai } 123770fbcdf4SRam Pai 123897216be0SAl Viro static inline void namespace_lock(void) 1239e3197d83SAl Viro { 124097216be0SAl Viro down_write(&namespace_sem); 1241e3197d83SAl Viro } 1242e3197d83SAl Viro 124399b7db7bSNick Piggin /* 124448a066e7SAl Viro * mount_lock must be held 124599b7db7bSNick Piggin * namespace_sem must be held for write 124648a066e7SAl Viro * how = 0 => just this tree, don't propagate 124748a066e7SAl Viro * how = 1 => propagate; we know that nobody else has reference to any victims 124848a066e7SAl Viro * how = 2 => lazy umount 124999b7db7bSNick Piggin */ 125048a066e7SAl Viro void umount_tree(struct mount *mnt, int how) 125170fbcdf4SRam Pai { 125238129a13SAl Viro HLIST_HEAD(tmp_list); 1253315fc83eSAl Viro struct mount *p; 125438129a13SAl Viro struct mount *last = NULL; 125570fbcdf4SRam Pai 125638129a13SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 125738129a13SAl Viro hlist_del_init_rcu(&p->mnt_hash); 125838129a13SAl Viro hlist_add_head(&p->mnt_hash, &tmp_list); 125938129a13SAl Viro } 126070fbcdf4SRam Pai 126188b368f2SAl Viro hlist_for_each_entry(p, &tmp_list, mnt_hash) 126288b368f2SAl Viro list_del_init(&p->mnt_child); 126388b368f2SAl Viro 126448a066e7SAl Viro if (how) 12657b8a53fdSAl Viro propagate_umount(&tmp_list); 1266a05964f3SRam Pai 126738129a13SAl Viro hlist_for_each_entry(p, &tmp_list, mnt_hash) { 12686776db3dSAl Viro list_del_init(&p->mnt_expire); 12691a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1270143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 127163d37a84SAl Viro p->mnt_ns = NULL; 127248a066e7SAl Viro if (how < 2) 127348a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 1274676da58dSAl Viro if (mnt_has_parent(p)) { 127584d17192SAl Viro put_mountpoint(p->mnt_mp); 127681b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1277aba809cfSAl Viro /* move the reference to mountpoint into ->mnt_ex_mountpoint */ 1278aba809cfSAl Viro p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint; 1279aba809cfSAl Viro p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt; 1280aba809cfSAl Viro p->mnt_mountpoint = p->mnt.mnt_root; 1281aba809cfSAl Viro p->mnt_parent = p; 128284d17192SAl Viro p->mnt_mp = NULL; 12837c4b93d8SAl Viro } 12840f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 128538129a13SAl Viro last = p; 12861da177e4SLinus Torvalds } 128738129a13SAl Viro if (last) { 128838129a13SAl Viro last->mnt_hash.next = unmounted.first; 128938129a13SAl Viro unmounted.first = tmp_list.first; 129038129a13SAl Viro unmounted.first->pprev = &unmounted.first; 129138129a13SAl Viro } 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 1294b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1295c35038beSAl Viro 12961ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 12971da177e4SLinus Torvalds { 12981ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 12991da177e4SLinus Torvalds int retval; 13001da177e4SLinus Torvalds 13011ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 13021da177e4SLinus Torvalds if (retval) 13031da177e4SLinus Torvalds return retval; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* 13061da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 13071da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 13081da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 13091da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 13101da177e4SLinus Torvalds */ 13111da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 13121ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 13131da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 13141da177e4SLinus Torvalds return -EINVAL; 13151da177e4SLinus Torvalds 1316b3e19d92SNick Piggin /* 1317b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1318b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1319b3e19d92SNick Piggin */ 1320719ea2fbSAl Viro lock_mount_hash(); 132183adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1322719ea2fbSAl Viro unlock_mount_hash(); 13231da177e4SLinus Torvalds return -EBUSY; 1324b3e19d92SNick Piggin } 1325719ea2fbSAl Viro unlock_mount_hash(); 13261da177e4SLinus Torvalds 1327863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 13281da177e4SLinus Torvalds return -EAGAIN; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* 13321da177e4SLinus Torvalds * If we may have to abort operations to get out of this 13331da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 13341da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 13351da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 13361da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 13371da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 13381da177e4SLinus Torvalds * about for the moment. 13391da177e4SLinus Torvalds */ 13401da177e4SLinus Torvalds 134142faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 134242faad99SAl Viro sb->s_op->umount_begin(sb); 134342faad99SAl Viro } 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /* 13461da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 13471da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 13481da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 13491da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 13501da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 13511da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 13521da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 13531da177e4SLinus Torvalds */ 13541ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 13551da177e4SLinus Torvalds /* 13561da177e4SLinus Torvalds * Special case for "unmounting" root ... 13571da177e4SLinus Torvalds * we just try to remount it readonly. 13581da177e4SLinus Torvalds */ 13591da177e4SLinus Torvalds down_write(&sb->s_umount); 13604aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 13611da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 13621da177e4SLinus Torvalds up_write(&sb->s_umount); 13631da177e4SLinus Torvalds return retval; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 136697216be0SAl Viro namespace_lock(); 1367719ea2fbSAl Viro lock_mount_hash(); 13685addc5ddSAl Viro event++; 13691da177e4SLinus Torvalds 137048a066e7SAl Viro if (flags & MNT_DETACH) { 137148a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 137248a066e7SAl Viro umount_tree(mnt, 2); 137348a066e7SAl Viro retval = 0; 137448a066e7SAl Viro } else { 1375b54b9be7SAl Viro shrink_submounts(mnt); 13761da177e4SLinus Torvalds retval = -EBUSY; 137748a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 13781a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1379328e6d90SAl Viro umount_tree(mnt, 1); 13801da177e4SLinus Torvalds retval = 0; 13811da177e4SLinus Torvalds } 138248a066e7SAl Viro } 1383719ea2fbSAl Viro unlock_mount_hash(); 1384e3197d83SAl Viro namespace_unlock(); 13851da177e4SLinus Torvalds return retval; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds /* 13899b40bc90SAl Viro * Is the caller allowed to modify his namespace? 13909b40bc90SAl Viro */ 13919b40bc90SAl Viro static inline bool may_mount(void) 13929b40bc90SAl Viro { 13939b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 13949b40bc90SAl Viro } 13959b40bc90SAl Viro 13969b40bc90SAl Viro /* 13971da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 13981da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 13991da177e4SLinus Torvalds * 14001da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 14011da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 14021da177e4SLinus Torvalds */ 14031da177e4SLinus Torvalds 1404bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 14051da177e4SLinus Torvalds { 14062d8f3038SAl Viro struct path path; 1407900148dcSAl Viro struct mount *mnt; 14081da177e4SLinus Torvalds int retval; 1409db1f05bbSMiklos Szeredi int lookup_flags = 0; 14101da177e4SLinus Torvalds 1411db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1412db1f05bbSMiklos Szeredi return -EINVAL; 1413db1f05bbSMiklos Szeredi 14149b40bc90SAl Viro if (!may_mount()) 14159b40bc90SAl Viro return -EPERM; 14169b40bc90SAl Viro 1417db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1418db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1419db1f05bbSMiklos Szeredi 1420197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 14211da177e4SLinus Torvalds if (retval) 14221da177e4SLinus Torvalds goto out; 1423900148dcSAl Viro mnt = real_mount(path.mnt); 14241da177e4SLinus Torvalds retval = -EINVAL; 14252d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 14261da177e4SLinus Torvalds goto dput_and_out; 1427143c8c91SAl Viro if (!check_mnt(mnt)) 14281da177e4SLinus Torvalds goto dput_and_out; 14295ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 14305ff9d8a6SEric W. Biederman goto dput_and_out; 14311da177e4SLinus Torvalds 1432900148dcSAl Viro retval = do_umount(mnt, flags); 14331da177e4SLinus Torvalds dput_and_out: 1434429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 14352d8f3038SAl Viro dput(path.dentry); 1436900148dcSAl Viro mntput_no_expire(mnt); 14371da177e4SLinus Torvalds out: 14381da177e4SLinus Torvalds return retval; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* 14441da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 14451da177e4SLinus Torvalds */ 1446bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 14471da177e4SLinus Torvalds { 14481da177e4SLinus Torvalds return sys_umount(name, 0); 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds #endif 14521da177e4SLinus Torvalds 14534ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 14548823c079SEric W. Biederman { 14554ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 14564ce5d2b1SEric W. Biederman struct inode *inode = dentry->d_inode; 14570bb80f24SDavid Howells struct proc_ns *ei; 14588823c079SEric W. Biederman 14598823c079SEric W. Biederman if (!proc_ns_inode(inode)) 14608823c079SEric W. Biederman return false; 14618823c079SEric W. Biederman 14620bb80f24SDavid Howells ei = get_proc_ns(inode); 14638823c079SEric W. Biederman if (ei->ns_ops != &mntns_operations) 14648823c079SEric W. Biederman return false; 14658823c079SEric W. Biederman 14664ce5d2b1SEric W. Biederman return true; 14674ce5d2b1SEric W. Biederman } 14684ce5d2b1SEric W. Biederman 14694ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 14704ce5d2b1SEric W. Biederman { 14714ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 14724ce5d2b1SEric W. Biederman * mount namespace loop? 14734ce5d2b1SEric W. Biederman */ 14744ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 14754ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 14764ce5d2b1SEric W. Biederman return false; 14774ce5d2b1SEric W. Biederman 14784ce5d2b1SEric W. Biederman mnt_ns = get_proc_ns(dentry->d_inode)->ns; 14798823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 14808823c079SEric W. Biederman } 14818823c079SEric W. Biederman 148287129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 148336341f64SRam Pai int flag) 14841da177e4SLinus Torvalds { 148584d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 14861da177e4SLinus Torvalds 14874ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 14884ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 14894ce5d2b1SEric W. Biederman 14904ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1491be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 14929676f0c6SRam Pai 149336341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1494be34d1a3SDavid Howells if (IS_ERR(q)) 1495be34d1a3SDavid Howells return q; 1496be34d1a3SDavid Howells 14975ff9d8a6SEric W. Biederman q->mnt.mnt_flags &= ~MNT_LOCKED; 1498a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds p = mnt; 15016b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1502315fc83eSAl Viro struct mount *s; 15037ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 15041da177e4SLinus Torvalds continue; 15051da177e4SLinus Torvalds 1506909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 150712a5b529SAl Viro struct mount *t = NULL; 15084ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 15094ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 15104ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 15114ce5d2b1SEric W. Biederman continue; 15124ce5d2b1SEric W. Biederman } 15134ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 15144ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 15159676f0c6SRam Pai s = skip_mnt_tree(s); 15169676f0c6SRam Pai continue; 15179676f0c6SRam Pai } 15180714a533SAl Viro while (p != s->mnt_parent) { 15190714a533SAl Viro p = p->mnt_parent; 15200714a533SAl Viro q = q->mnt_parent; 15211da177e4SLinus Torvalds } 152287129cc0SAl Viro p = s; 152384d17192SAl Viro parent = q; 152487129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1525be34d1a3SDavid Howells if (IS_ERR(q)) 1526be34d1a3SDavid Howells goto out; 1527719ea2fbSAl Viro lock_mount_hash(); 15281a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 152912a5b529SAl Viro mnt_set_mountpoint(parent, p->mnt_mp, q); 153012a5b529SAl Viro if (!list_empty(&parent->mnt_mounts)) { 153112a5b529SAl Viro t = list_last_entry(&parent->mnt_mounts, 153212a5b529SAl Viro struct mount, mnt_child); 153312a5b529SAl Viro if (t->mnt_mp != p->mnt_mp) 153412a5b529SAl Viro t = NULL; 153512a5b529SAl Viro } 153612a5b529SAl Viro attach_shadowed(q, parent, t); 1537719ea2fbSAl Viro unlock_mount_hash(); 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds return res; 1541be34d1a3SDavid Howells out: 15421da177e4SLinus Torvalds if (res) { 1543719ea2fbSAl Viro lock_mount_hash(); 1544328e6d90SAl Viro umount_tree(res, 0); 1545719ea2fbSAl Viro unlock_mount_hash(); 15461da177e4SLinus Torvalds } 1547be34d1a3SDavid Howells return q; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 1550be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1551be34d1a3SDavid Howells 1552589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 15538aec0809SAl Viro { 1554cb338d06SAl Viro struct mount *tree; 155597216be0SAl Viro namespace_lock(); 155687129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 155787129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1558328e6d90SAl Viro namespace_unlock(); 1559be34d1a3SDavid Howells if (IS_ERR(tree)) 156052e220d3SDan Carpenter return ERR_CAST(tree); 1561be34d1a3SDavid Howells return &tree->mnt; 15628aec0809SAl Viro } 15638aec0809SAl Viro 15648aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 15658aec0809SAl Viro { 156697216be0SAl Viro namespace_lock(); 1567719ea2fbSAl Viro lock_mount_hash(); 1568328e6d90SAl Viro umount_tree(real_mount(mnt), 0); 1569719ea2fbSAl Viro unlock_mount_hash(); 15703ab6abeeSAl Viro namespace_unlock(); 15718aec0809SAl Viro } 15728aec0809SAl Viro 15731f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 15741f707137SAl Viro struct vfsmount *root) 15751f707137SAl Viro { 15761a4eeaf2SAl Viro struct mount *mnt; 15771f707137SAl Viro int res = f(root, arg); 15781f707137SAl Viro if (res) 15791f707137SAl Viro return res; 15801a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 15811a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 15821f707137SAl Viro if (res) 15831f707137SAl Viro return res; 15841f707137SAl Viro } 15851f707137SAl Viro return 0; 15861f707137SAl Viro } 15871f707137SAl Viro 15884b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1589719f5d7fSMiklos Szeredi { 1590315fc83eSAl Viro struct mount *p; 1591719f5d7fSMiklos Szeredi 1592909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1593fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 15944b8b21f4SAl Viro mnt_release_group_id(p); 1595719f5d7fSMiklos Szeredi } 1596719f5d7fSMiklos Szeredi } 1597719f5d7fSMiklos Szeredi 15984b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1599719f5d7fSMiklos Szeredi { 1600315fc83eSAl Viro struct mount *p; 1601719f5d7fSMiklos Szeredi 1602909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1603fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 16044b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1605719f5d7fSMiklos Szeredi if (err) { 16064b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1607719f5d7fSMiklos Szeredi return err; 1608719f5d7fSMiklos Szeredi } 1609719f5d7fSMiklos Szeredi } 1610719f5d7fSMiklos Szeredi } 1611719f5d7fSMiklos Szeredi 1612719f5d7fSMiklos Szeredi return 0; 1613719f5d7fSMiklos Szeredi } 1614719f5d7fSMiklos Szeredi 1615b90fa9aeSRam Pai /* 1616b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1617b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 161821444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 161921444403SRam Pai * store the parent mount and mountpoint dentry. 162021444403SRam Pai * (done when source_mnt is moved) 1621b90fa9aeSRam Pai * 1622b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1623b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 16249676f0c6SRam Pai * --------------------------------------------------------------------------- 1625b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 16269676f0c6SRam Pai * |************************************************************************** 16279676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 16289676f0c6SRam Pai * | dest | | | | | 16299676f0c6SRam Pai * | | | | | | | 16309676f0c6SRam Pai * | v | | | | | 16319676f0c6SRam Pai * |************************************************************************** 16329676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 16335afe0022SRam Pai * | | | | | | 16349676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 16359676f0c6SRam Pai * *************************************************************************** 1636b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1637b90fa9aeSRam Pai * destination mount. 1638b90fa9aeSRam Pai * 1639b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1640b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1641b90fa9aeSRam Pai * the peer group of the source mount. 1642b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1643b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1644b90fa9aeSRam Pai * mount. 16455afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 16465afe0022SRam Pai * of the destination mount and the cloned mount is made slave 16475afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 16485afe0022SRam Pai * is marked as 'shared and slave'. 16495afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 16505afe0022SRam Pai * source mount. 16515afe0022SRam Pai * 16529676f0c6SRam Pai * --------------------------------------------------------------------------- 165321444403SRam Pai * | MOVE MOUNT OPERATION | 16549676f0c6SRam Pai * |************************************************************************** 16559676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 16569676f0c6SRam Pai * | dest | | | | | 16579676f0c6SRam Pai * | | | | | | | 16589676f0c6SRam Pai * | v | | | | | 16599676f0c6SRam Pai * |************************************************************************** 16609676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 16615afe0022SRam Pai * | | | | | | 16629676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 16639676f0c6SRam Pai * *************************************************************************** 16645afe0022SRam Pai * 16655afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 16665afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 166721444403SRam Pai * (+*) the mount is moved to the destination. 16685afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 16695afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 16705afe0022SRam Pai * the mount is marked as 'shared and slave'. 16715afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1672b90fa9aeSRam Pai * 1673b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1674b90fa9aeSRam Pai * applied to each mount in the tree. 1675b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1676b90fa9aeSRam Pai * in allocations. 1677b90fa9aeSRam Pai */ 16780fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 167984d17192SAl Viro struct mount *dest_mnt, 168084d17192SAl Viro struct mountpoint *dest_mp, 168184d17192SAl Viro struct path *parent_path) 1682b90fa9aeSRam Pai { 168338129a13SAl Viro HLIST_HEAD(tree_list); 1684315fc83eSAl Viro struct mount *child, *p; 168538129a13SAl Viro struct hlist_node *n; 1686719f5d7fSMiklos Szeredi int err; 1687b90fa9aeSRam Pai 1688fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 16890fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1690719f5d7fSMiklos Szeredi if (err) 1691719f5d7fSMiklos Szeredi goto out; 169284d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1693f2ebb3a9SAl Viro lock_mount_hash(); 1694719f5d7fSMiklos Szeredi if (err) 1695719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1696909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 16970f0afb1dSAl Viro set_mnt_shared(p); 16980b1b901bSAl Viro } else { 16990b1b901bSAl Viro lock_mount_hash(); 1700b90fa9aeSRam Pai } 17011a390689SAl Viro if (parent_path) { 17020fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 170384d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1704143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 170521444403SRam Pai } else { 170684d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 17071d6a32acSAl Viro commit_tree(source_mnt, NULL); 170821444403SRam Pai } 1709b90fa9aeSRam Pai 171038129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 17111d6a32acSAl Viro struct mount *q; 171238129a13SAl Viro hlist_del_init(&child->mnt_hash); 17131d6a32acSAl Viro q = __lookup_mnt_last(&child->mnt_parent->mnt, 17141d6a32acSAl Viro child->mnt_mountpoint); 17151d6a32acSAl Viro commit_tree(child, q); 1716b90fa9aeSRam Pai } 1717719ea2fbSAl Viro unlock_mount_hash(); 171899b7db7bSNick Piggin 1719b90fa9aeSRam Pai return 0; 1720719f5d7fSMiklos Szeredi 1721719f5d7fSMiklos Szeredi out_cleanup_ids: 1722f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 1723f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 1724f2ebb3a9SAl Viro umount_tree(child, 0); 1725f2ebb3a9SAl Viro } 1726f2ebb3a9SAl Viro unlock_mount_hash(); 17270fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1728719f5d7fSMiklos Szeredi out: 1729719f5d7fSMiklos Szeredi return err; 1730b90fa9aeSRam Pai } 1731b90fa9aeSRam Pai 173284d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 1733b12cea91SAl Viro { 1734b12cea91SAl Viro struct vfsmount *mnt; 173584d17192SAl Viro struct dentry *dentry = path->dentry; 1736b12cea91SAl Viro retry: 173784d17192SAl Viro mutex_lock(&dentry->d_inode->i_mutex); 173884d17192SAl Viro if (unlikely(cant_mount(dentry))) { 173984d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 174084d17192SAl Viro return ERR_PTR(-ENOENT); 1741b12cea91SAl Viro } 174297216be0SAl Viro namespace_lock(); 1743b12cea91SAl Viro mnt = lookup_mnt(path); 174484d17192SAl Viro if (likely(!mnt)) { 174584d17192SAl Viro struct mountpoint *mp = new_mountpoint(dentry); 174684d17192SAl Viro if (IS_ERR(mp)) { 174797216be0SAl Viro namespace_unlock(); 174884d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 174984d17192SAl Viro return mp; 175084d17192SAl Viro } 175184d17192SAl Viro return mp; 175284d17192SAl Viro } 175397216be0SAl Viro namespace_unlock(); 1754b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1755b12cea91SAl Viro path_put(path); 1756b12cea91SAl Viro path->mnt = mnt; 175784d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 1758b12cea91SAl Viro goto retry; 1759b12cea91SAl Viro } 1760b12cea91SAl Viro 176184d17192SAl Viro static void unlock_mount(struct mountpoint *where) 1762b12cea91SAl Viro { 176384d17192SAl Viro struct dentry *dentry = where->m_dentry; 176484d17192SAl Viro put_mountpoint(where); 1765328e6d90SAl Viro namespace_unlock(); 176684d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 1767b12cea91SAl Viro } 1768b12cea91SAl Viro 176984d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 17701da177e4SLinus Torvalds { 177195bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 17721da177e4SLinus Torvalds return -EINVAL; 17731da177e4SLinus Torvalds 177484d17192SAl Viro if (S_ISDIR(mp->m_dentry->d_inode->i_mode) != 177595bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 17761da177e4SLinus Torvalds return -ENOTDIR; 17771da177e4SLinus Torvalds 177884d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds /* 17827a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 17837a2e8a8fSValerie Aurora */ 17847a2e8a8fSValerie Aurora 17857a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 17867a2e8a8fSValerie Aurora { 17877c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 17887a2e8a8fSValerie Aurora 17897a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 17907a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 17917a2e8a8fSValerie Aurora return 0; 17927a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 17937a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 17947a2e8a8fSValerie Aurora return 0; 17957a2e8a8fSValerie Aurora return type; 17967a2e8a8fSValerie Aurora } 17977a2e8a8fSValerie Aurora 17987a2e8a8fSValerie Aurora /* 179907b20889SRam Pai * recursively change the type of the mountpoint. 180007b20889SRam Pai */ 18010a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 180207b20889SRam Pai { 1803315fc83eSAl Viro struct mount *m; 18044b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 180507b20889SRam Pai int recurse = flag & MS_REC; 18067a2e8a8fSValerie Aurora int type; 1807719f5d7fSMiklos Szeredi int err = 0; 180807b20889SRam Pai 18092d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 181007b20889SRam Pai return -EINVAL; 181107b20889SRam Pai 18127a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 18137a2e8a8fSValerie Aurora if (!type) 18147a2e8a8fSValerie Aurora return -EINVAL; 18157a2e8a8fSValerie Aurora 181697216be0SAl Viro namespace_lock(); 1817719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1818719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1819719f5d7fSMiklos Szeredi if (err) 1820719f5d7fSMiklos Szeredi goto out_unlock; 1821719f5d7fSMiklos Szeredi } 1822719f5d7fSMiklos Szeredi 1823719ea2fbSAl Viro lock_mount_hash(); 1824909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 18250f0afb1dSAl Viro change_mnt_propagation(m, type); 1826719ea2fbSAl Viro unlock_mount_hash(); 1827719f5d7fSMiklos Szeredi 1828719f5d7fSMiklos Szeredi out_unlock: 182997216be0SAl Viro namespace_unlock(); 1830719f5d7fSMiklos Szeredi return err; 183107b20889SRam Pai } 183207b20889SRam Pai 18335ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 18345ff9d8a6SEric W. Biederman { 18355ff9d8a6SEric W. Biederman struct mount *child; 18365ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 18375ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 18385ff9d8a6SEric W. Biederman continue; 18395ff9d8a6SEric W. Biederman 18405ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 18415ff9d8a6SEric W. Biederman return true; 18425ff9d8a6SEric W. Biederman } 18435ff9d8a6SEric W. Biederman return false; 18445ff9d8a6SEric W. Biederman } 18455ff9d8a6SEric W. Biederman 184607b20889SRam Pai /* 18471da177e4SLinus Torvalds * do loopback mount. 18481da177e4SLinus Torvalds */ 1849808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 18502dafe1c4SEric Sandeen int recurse) 18511da177e4SLinus Torvalds { 18522d92ab3cSAl Viro struct path old_path; 185384d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 185484d17192SAl Viro struct mountpoint *mp; 185557eccb83SAl Viro int err; 18561da177e4SLinus Torvalds if (!old_name || !*old_name) 18571da177e4SLinus Torvalds return -EINVAL; 1858815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 18591da177e4SLinus Torvalds if (err) 18601da177e4SLinus Torvalds return err; 18611da177e4SLinus Torvalds 18628823c079SEric W. Biederman err = -EINVAL; 18634ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 18648823c079SEric W. Biederman goto out; 18658823c079SEric W. Biederman 186684d17192SAl Viro mp = lock_mount(path); 186784d17192SAl Viro err = PTR_ERR(mp); 186884d17192SAl Viro if (IS_ERR(mp)) 18699676f0c6SRam Pai goto out; 18709676f0c6SRam Pai 187187129cc0SAl Viro old = real_mount(old_path.mnt); 187284d17192SAl Viro parent = real_mount(path->mnt); 187387129cc0SAl Viro 1874b12cea91SAl Viro err = -EINVAL; 1875fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1876b12cea91SAl Viro goto out2; 1877b12cea91SAl Viro 187884d17192SAl Viro if (!check_mnt(parent) || !check_mnt(old)) 1879b12cea91SAl Viro goto out2; 1880ccd48bc7SAl Viro 18815ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 18825ff9d8a6SEric W. Biederman goto out2; 18835ff9d8a6SEric W. Biederman 18841da177e4SLinus Torvalds if (recurse) 18854ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 18861da177e4SLinus Torvalds else 188787129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 18881da177e4SLinus Torvalds 1889be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1890be34d1a3SDavid Howells err = PTR_ERR(mnt); 1891e9c5d8a5SAndrey Vagin goto out2; 1892be34d1a3SDavid Howells } 1893ccd48bc7SAl Viro 18945ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 18955ff9d8a6SEric W. Biederman 189684d17192SAl Viro err = graft_tree(mnt, parent, mp); 18971da177e4SLinus Torvalds if (err) { 1898719ea2fbSAl Viro lock_mount_hash(); 1899328e6d90SAl Viro umount_tree(mnt, 0); 1900719ea2fbSAl Viro unlock_mount_hash(); 19015b83d2c5SRam Pai } 1902b12cea91SAl Viro out2: 190384d17192SAl Viro unlock_mount(mp); 1904ccd48bc7SAl Viro out: 19052d92ab3cSAl Viro path_put(&old_path); 19061da177e4SLinus Torvalds return err; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 19092e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 19102e4b7fcdSDave Hansen { 19112e4b7fcdSDave Hansen int error = 0; 19122e4b7fcdSDave Hansen int readonly_request = 0; 19132e4b7fcdSDave Hansen 19142e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 19152e4b7fcdSDave Hansen readonly_request = 1; 19162e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 19172e4b7fcdSDave Hansen return 0; 19182e4b7fcdSDave Hansen 19192e4b7fcdSDave Hansen if (readonly_request) 192083adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 19212e4b7fcdSDave Hansen else 192283adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 19232e4b7fcdSDave Hansen return error; 19242e4b7fcdSDave Hansen } 19252e4b7fcdSDave Hansen 19261da177e4SLinus Torvalds /* 19271da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 19281da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 19291da177e4SLinus Torvalds * on it - tough luck. 19301da177e4SLinus Torvalds */ 19310a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 19321da177e4SLinus Torvalds void *data) 19331da177e4SLinus Torvalds { 19341da177e4SLinus Torvalds int err; 19352d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1936143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 19371da177e4SLinus Torvalds 1938143c8c91SAl Viro if (!check_mnt(mnt)) 19391da177e4SLinus Torvalds return -EINVAL; 19401da177e4SLinus Torvalds 19412d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 19421da177e4SLinus Torvalds return -EINVAL; 19431da177e4SLinus Torvalds 194407b64558SEric W. Biederman /* Don't allow changing of locked mnt flags. 194507b64558SEric W. Biederman * 194607b64558SEric W. Biederman * No locks need to be held here while testing the various 194707b64558SEric W. Biederman * MNT_LOCK flags because those flags can never be cleared 194807b64558SEric W. Biederman * once they are set. 194907b64558SEric W. Biederman */ 195007b64558SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) && 195107b64558SEric W. Biederman !(mnt_flags & MNT_READONLY)) { 195207b64558SEric W. Biederman return -EPERM; 195307b64558SEric W. Biederman } 19549566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) && 19559566d674SEric W. Biederman !(mnt_flags & MNT_NODEV)) { 19569566d674SEric W. Biederman return -EPERM; 19579566d674SEric W. Biederman } 19589566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) && 19599566d674SEric W. Biederman !(mnt_flags & MNT_NOSUID)) { 19609566d674SEric W. Biederman return -EPERM; 19619566d674SEric W. Biederman } 19629566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) && 19639566d674SEric W. Biederman !(mnt_flags & MNT_NOEXEC)) { 19649566d674SEric W. Biederman return -EPERM; 19659566d674SEric W. Biederman } 19669566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) && 19679566d674SEric W. Biederman ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) { 19689566d674SEric W. Biederman return -EPERM; 19699566d674SEric W. Biederman } 19709566d674SEric W. Biederman 1971ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1972ff36fe2cSEric Paris if (err) 1973ff36fe2cSEric Paris return err; 1974ff36fe2cSEric Paris 19751da177e4SLinus Torvalds down_write(&sb->s_umount); 19762e4b7fcdSDave Hansen if (flags & MS_BIND) 19772d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 197857eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 197957eccb83SAl Viro err = -EPERM; 19804aa98cf7SAl Viro else 19811da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 19827b43a79fSAl Viro if (!err) { 1983719ea2fbSAl Viro lock_mount_hash(); 1984a6138db8SEric W. Biederman mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 1985143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1986143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1987719ea2fbSAl Viro unlock_mount_hash(); 19880e55a7ccSDan Williams } 19896339dab8SAl Viro up_write(&sb->s_umount); 19901da177e4SLinus Torvalds return err; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 1993cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 19949676f0c6SRam Pai { 1995315fc83eSAl Viro struct mount *p; 1996909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1997fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 19989676f0c6SRam Pai return 1; 19999676f0c6SRam Pai } 20009676f0c6SRam Pai return 0; 20019676f0c6SRam Pai } 20029676f0c6SRam Pai 2003808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 20041da177e4SLinus Torvalds { 20052d92ab3cSAl Viro struct path old_path, parent_path; 2006676da58dSAl Viro struct mount *p; 20070fb54e50SAl Viro struct mount *old; 200884d17192SAl Viro struct mountpoint *mp; 200957eccb83SAl Viro int err; 20101da177e4SLinus Torvalds if (!old_name || !*old_name) 20111da177e4SLinus Torvalds return -EINVAL; 20122d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 20131da177e4SLinus Torvalds if (err) 20141da177e4SLinus Torvalds return err; 20151da177e4SLinus Torvalds 201684d17192SAl Viro mp = lock_mount(path); 201784d17192SAl Viro err = PTR_ERR(mp); 201884d17192SAl Viro if (IS_ERR(mp)) 2019cc53ce53SDavid Howells goto out; 2020cc53ce53SDavid Howells 2021143c8c91SAl Viro old = real_mount(old_path.mnt); 2022fc7be130SAl Viro p = real_mount(path->mnt); 2023143c8c91SAl Viro 20241da177e4SLinus Torvalds err = -EINVAL; 2025fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 20261da177e4SLinus Torvalds goto out1; 20271da177e4SLinus Torvalds 20285ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 20295ff9d8a6SEric W. Biederman goto out1; 20305ff9d8a6SEric W. Biederman 20311da177e4SLinus Torvalds err = -EINVAL; 20322d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 203321444403SRam Pai goto out1; 20341da177e4SLinus Torvalds 2035676da58dSAl Viro if (!mnt_has_parent(old)) 203621444403SRam Pai goto out1; 20371da177e4SLinus Torvalds 20382d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 20392d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 204021444403SRam Pai goto out1; 204121444403SRam Pai /* 204221444403SRam Pai * Don't move a mount residing in a shared parent. 204321444403SRam Pai */ 2044fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 204521444403SRam Pai goto out1; 20469676f0c6SRam Pai /* 20479676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 20489676f0c6SRam Pai * mount which is shared. 20499676f0c6SRam Pai */ 2050fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 20519676f0c6SRam Pai goto out1; 20521da177e4SLinus Torvalds err = -ELOOP; 2053fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2054676da58dSAl Viro if (p == old) 205521444403SRam Pai goto out1; 20561da177e4SLinus Torvalds 205784d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 20584ac91378SJan Blunck if (err) 205921444403SRam Pai goto out1; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 20621da177e4SLinus Torvalds * automatically */ 20636776db3dSAl Viro list_del_init(&old->mnt_expire); 20641da177e4SLinus Torvalds out1: 206584d17192SAl Viro unlock_mount(mp); 20661da177e4SLinus Torvalds out: 20671da177e4SLinus Torvalds if (!err) 20681a390689SAl Viro path_put(&parent_path); 20692d92ab3cSAl Viro path_put(&old_path); 20701da177e4SLinus Torvalds return err; 20711da177e4SLinus Torvalds } 20721da177e4SLinus Torvalds 20739d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 20749d412a43SAl Viro { 20759d412a43SAl Viro int err; 20769d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 20779d412a43SAl Viro if (subtype) { 20789d412a43SAl Viro subtype++; 20799d412a43SAl Viro err = -EINVAL; 20809d412a43SAl Viro if (!subtype[0]) 20819d412a43SAl Viro goto err; 20829d412a43SAl Viro } else 20839d412a43SAl Viro subtype = ""; 20849d412a43SAl Viro 20859d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 20869d412a43SAl Viro err = -ENOMEM; 20879d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 20889d412a43SAl Viro goto err; 20899d412a43SAl Viro return mnt; 20909d412a43SAl Viro 20919d412a43SAl Viro err: 20929d412a43SAl Viro mntput(mnt); 20939d412a43SAl Viro return ERR_PTR(err); 20949d412a43SAl Viro } 20959d412a43SAl Viro 20969d412a43SAl Viro /* 20979d412a43SAl Viro * add a mount into a namespace's mount tree 20989d412a43SAl Viro */ 209995bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 21009d412a43SAl Viro { 210184d17192SAl Viro struct mountpoint *mp; 210284d17192SAl Viro struct mount *parent; 21039d412a43SAl Viro int err; 21049d412a43SAl Viro 2105f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 21069d412a43SAl Viro 210784d17192SAl Viro mp = lock_mount(path); 210884d17192SAl Viro if (IS_ERR(mp)) 210984d17192SAl Viro return PTR_ERR(mp); 21109d412a43SAl Viro 211184d17192SAl Viro parent = real_mount(path->mnt); 21129d412a43SAl Viro err = -EINVAL; 211384d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2114156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2115156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 21169d412a43SAl Viro goto unlock; 2117156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 211884d17192SAl Viro if (!parent->mnt_ns) 2119156cacb1SAl Viro goto unlock; 2120156cacb1SAl Viro } 21219d412a43SAl Viro 21229d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 21239d412a43SAl Viro err = -EBUSY; 212495bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 21259d412a43SAl Viro path->mnt->mnt_root == path->dentry) 21269d412a43SAl Viro goto unlock; 21279d412a43SAl Viro 21289d412a43SAl Viro err = -EINVAL; 212995bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 21309d412a43SAl Viro goto unlock; 21319d412a43SAl Viro 213295bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 213384d17192SAl Viro err = graft_tree(newmnt, parent, mp); 21349d412a43SAl Viro 21359d412a43SAl Viro unlock: 213684d17192SAl Viro unlock_mount(mp); 21379d412a43SAl Viro return err; 21389d412a43SAl Viro } 2139b1e75df4SAl Viro 21401da177e4SLinus Torvalds /* 21411da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 21421da177e4SLinus Torvalds * namespace's tree 21431da177e4SLinus Torvalds */ 21440c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 2145808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 21461da177e4SLinus Torvalds { 21470c55cfc4SEric W. Biederman struct file_system_type *type; 21489b40bc90SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 21491da177e4SLinus Torvalds struct vfsmount *mnt; 215015f9a3f3SAl Viro int err; 21511da177e4SLinus Torvalds 21520c55cfc4SEric W. Biederman if (!fstype) 21531da177e4SLinus Torvalds return -EINVAL; 21541da177e4SLinus Torvalds 21550c55cfc4SEric W. Biederman type = get_fs_type(fstype); 21560c55cfc4SEric W. Biederman if (!type) 21570c55cfc4SEric W. Biederman return -ENODEV; 21580c55cfc4SEric W. Biederman 21590c55cfc4SEric W. Biederman if (user_ns != &init_user_ns) { 21600c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_MOUNT)) { 21610c55cfc4SEric W. Biederman put_filesystem(type); 21620c55cfc4SEric W. Biederman return -EPERM; 21630c55cfc4SEric W. Biederman } 21640c55cfc4SEric W. Biederman /* Only in special cases allow devices from mounts 21650c55cfc4SEric W. Biederman * created outside the initial user namespace. 21660c55cfc4SEric W. Biederman */ 21670c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) { 21680c55cfc4SEric W. Biederman flags |= MS_NODEV; 21699566d674SEric W. Biederman mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV; 21700c55cfc4SEric W. Biederman } 21710c55cfc4SEric W. Biederman } 21720c55cfc4SEric W. Biederman 21730c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 21740c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 21750c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 21760c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 21770c55cfc4SEric W. Biederman 21780c55cfc4SEric W. Biederman put_filesystem(type); 21791da177e4SLinus Torvalds if (IS_ERR(mnt)) 21801da177e4SLinus Torvalds return PTR_ERR(mnt); 21811da177e4SLinus Torvalds 218295bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 218315f9a3f3SAl Viro if (err) 218415f9a3f3SAl Viro mntput(mnt); 218515f9a3f3SAl Viro return err; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 218819a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 218919a167afSAl Viro { 21906776db3dSAl Viro struct mount *mnt = real_mount(m); 219119a167afSAl Viro int err; 219219a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 219319a167afSAl Viro * expired before we get a chance to add it 219419a167afSAl Viro */ 21956776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 219619a167afSAl Viro 219719a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 219819a167afSAl Viro m->mnt_root == path->dentry) { 2199b1e75df4SAl Viro err = -ELOOP; 2200b1e75df4SAl Viro goto fail; 220119a167afSAl Viro } 220219a167afSAl Viro 220395bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2204b1e75df4SAl Viro if (!err) 2205b1e75df4SAl Viro return 0; 2206b1e75df4SAl Viro fail: 2207b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 22086776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 220997216be0SAl Viro namespace_lock(); 22106776db3dSAl Viro list_del_init(&mnt->mnt_expire); 221197216be0SAl Viro namespace_unlock(); 221219a167afSAl Viro } 2213b1e75df4SAl Viro mntput(m); 2214b1e75df4SAl Viro mntput(m); 221519a167afSAl Viro return err; 221619a167afSAl Viro } 221719a167afSAl Viro 2218ea5b778aSDavid Howells /** 2219ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2220ea5b778aSDavid Howells * @mnt: The mount to list. 2221ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2222ea5b778aSDavid Howells */ 2223ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2224ea5b778aSDavid Howells { 222597216be0SAl Viro namespace_lock(); 2226ea5b778aSDavid Howells 22276776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2228ea5b778aSDavid Howells 222997216be0SAl Viro namespace_unlock(); 2230ea5b778aSDavid Howells } 2231ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2232ea5b778aSDavid Howells 2233ea5b778aSDavid Howells /* 22341da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 22351da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 22361da177e4SLinus Torvalds * here 22371da177e4SLinus Torvalds */ 22381da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 22391da177e4SLinus Torvalds { 2240761d5c38SAl Viro struct mount *mnt, *next; 22411da177e4SLinus Torvalds LIST_HEAD(graveyard); 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds if (list_empty(mounts)) 22441da177e4SLinus Torvalds return; 22451da177e4SLinus Torvalds 224697216be0SAl Viro namespace_lock(); 2247719ea2fbSAl Viro lock_mount_hash(); 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 22501da177e4SLinus Torvalds * following criteria: 22511da177e4SLinus Torvalds * - only referenced by its parent vfsmount 22521da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 22531da177e4SLinus Torvalds * cleared by mntput()) 22541da177e4SLinus Torvalds */ 22556776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2256863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 22571ab59738SAl Viro propagate_mount_busy(mnt, 1)) 22581da177e4SLinus Torvalds continue; 22596776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 22601da177e4SLinus Torvalds } 2261bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 22626776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2263143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2264328e6d90SAl Viro umount_tree(mnt, 1); 2265bcc5c7d2SAl Viro } 2266719ea2fbSAl Viro unlock_mount_hash(); 22673ab6abeeSAl Viro namespace_unlock(); 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds /* 22735528f911STrond Myklebust * Ripoff of 'select_parent()' 22745528f911STrond Myklebust * 22755528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 22765528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 22775528f911STrond Myklebust */ 2278692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 22795528f911STrond Myklebust { 2280692afc31SAl Viro struct mount *this_parent = parent; 22815528f911STrond Myklebust struct list_head *next; 22825528f911STrond Myklebust int found = 0; 22835528f911STrond Myklebust 22845528f911STrond Myklebust repeat: 22856b41d536SAl Viro next = this_parent->mnt_mounts.next; 22865528f911STrond Myklebust resume: 22876b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 22885528f911STrond Myklebust struct list_head *tmp = next; 22896b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 22905528f911STrond Myklebust 22915528f911STrond Myklebust next = tmp->next; 2292692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 22935528f911STrond Myklebust continue; 22945528f911STrond Myklebust /* 22955528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 22965528f911STrond Myklebust */ 22976b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 22985528f911STrond Myklebust this_parent = mnt; 22995528f911STrond Myklebust goto repeat; 23005528f911STrond Myklebust } 23015528f911STrond Myklebust 23021ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 23036776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 23045528f911STrond Myklebust found++; 23055528f911STrond Myklebust } 23065528f911STrond Myklebust } 23075528f911STrond Myklebust /* 23085528f911STrond Myklebust * All done at this level ... ascend and resume the search 23095528f911STrond Myklebust */ 23105528f911STrond Myklebust if (this_parent != parent) { 23116b41d536SAl Viro next = this_parent->mnt_child.next; 23120714a533SAl Viro this_parent = this_parent->mnt_parent; 23135528f911STrond Myklebust goto resume; 23145528f911STrond Myklebust } 23155528f911STrond Myklebust return found; 23165528f911STrond Myklebust } 23175528f911STrond Myklebust 23185528f911STrond Myklebust /* 23195528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 23205528f911STrond Myklebust * submounts of a specific parent mountpoint 232199b7db7bSNick Piggin * 232248a066e7SAl Viro * mount_lock must be held for write 23235528f911STrond Myklebust */ 2324b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 23255528f911STrond Myklebust { 23265528f911STrond Myklebust LIST_HEAD(graveyard); 2327761d5c38SAl Viro struct mount *m; 23285528f911STrond Myklebust 23295528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2330c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2331bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2332761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 23336776db3dSAl Viro mnt_expire); 2334143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2335328e6d90SAl Viro umount_tree(m, 1); 2336bcc5c7d2SAl Viro } 2337bcc5c7d2SAl Viro } 23385528f911STrond Myklebust } 23395528f911STrond Myklebust 23405528f911STrond Myklebust /* 23411da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 23421da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 23431da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 23441da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 23451da177e4SLinus Torvalds */ 2346b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2347b58fed8bSRam Pai unsigned long n) 23481da177e4SLinus Torvalds { 23491da177e4SLinus Torvalds char *t = to; 23501da177e4SLinus Torvalds const char __user *f = from; 23511da177e4SLinus Torvalds char c; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 23541da177e4SLinus Torvalds return n; 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds while (n) { 23571da177e4SLinus Torvalds if (__get_user(c, f)) { 23581da177e4SLinus Torvalds memset(t, 0, n); 23591da177e4SLinus Torvalds break; 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds *t++ = c; 23621da177e4SLinus Torvalds f++; 23631da177e4SLinus Torvalds n--; 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds return n; 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 23691da177e4SLinus Torvalds { 23701da177e4SLinus Torvalds int i; 23711da177e4SLinus Torvalds unsigned long page; 23721da177e4SLinus Torvalds unsigned long size; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds *where = 0; 23751da177e4SLinus Torvalds if (!data) 23761da177e4SLinus Torvalds return 0; 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 23791da177e4SLinus Torvalds return -ENOMEM; 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds /* We only care that *some* data at the address the user 23821da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 23831da177e4SLinus Torvalds * the remainder of the page. 23841da177e4SLinus Torvalds */ 23851da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 23861da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 23871da177e4SLinus Torvalds if (size > PAGE_SIZE) 23881da177e4SLinus Torvalds size = PAGE_SIZE; 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 23911da177e4SLinus Torvalds if (!i) { 23921da177e4SLinus Torvalds free_page(page); 23931da177e4SLinus Torvalds return -EFAULT; 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds if (i != PAGE_SIZE) 23961da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 23971da177e4SLinus Torvalds *where = page; 23981da177e4SLinus Torvalds return 0; 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 2401eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2402eca6f534SVegard Nossum { 2403eca6f534SVegard Nossum char *tmp; 2404eca6f534SVegard Nossum 2405eca6f534SVegard Nossum if (!data) { 2406eca6f534SVegard Nossum *where = NULL; 2407eca6f534SVegard Nossum return 0; 2408eca6f534SVegard Nossum } 2409eca6f534SVegard Nossum 2410eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2411eca6f534SVegard Nossum if (IS_ERR(tmp)) 2412eca6f534SVegard Nossum return PTR_ERR(tmp); 2413eca6f534SVegard Nossum 2414eca6f534SVegard Nossum *where = tmp; 2415eca6f534SVegard Nossum return 0; 2416eca6f534SVegard Nossum } 2417eca6f534SVegard Nossum 24181da177e4SLinus Torvalds /* 24191da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 24201da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 24211da177e4SLinus Torvalds * 24221da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 24231da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 24241da177e4SLinus Torvalds * information (or be NULL). 24251da177e4SLinus Torvalds * 24261da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 24271da177e4SLinus Torvalds * When the flags word was introduced its top half was required 24281da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 24291da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 24301da177e4SLinus Torvalds * and must be discarded. 24311da177e4SLinus Torvalds */ 2432808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name, 2433808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 24341da177e4SLinus Torvalds { 24352d92ab3cSAl Viro struct path path; 24361da177e4SLinus Torvalds int retval = 0; 24371da177e4SLinus Torvalds int mnt_flags = 0; 24381da177e4SLinus Torvalds 24391da177e4SLinus Torvalds /* Discard magic */ 24401da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 24411da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds /* Basic sanity checks */ 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 24461da177e4SLinus Torvalds return -EINVAL; 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds if (data_page) 24491da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 24501da177e4SLinus Torvalds 2451a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2452a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2453a27ab9f2STetsuo Handa if (retval) 2454a27ab9f2STetsuo Handa return retval; 2455a27ab9f2STetsuo Handa 2456a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2457a27ab9f2STetsuo Handa type_page, flags, data_page); 24580d5cadb8SAl Viro if (!retval && !may_mount()) 24590d5cadb8SAl Viro retval = -EPERM; 2460a27ab9f2STetsuo Handa if (retval) 2461a27ab9f2STetsuo Handa goto dput_out; 2462a27ab9f2STetsuo Handa 2463613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2464613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 24650a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 24660a1c01c9SMatthew Garrett 24671da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 24681da177e4SLinus Torvalds if (flags & MS_NOSUID) 24691da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 24701da177e4SLinus Torvalds if (flags & MS_NODEV) 24711da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 24721da177e4SLinus Torvalds if (flags & MS_NOEXEC) 24731da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2474fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2475fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2476fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2477fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2478d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2479d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 24802e4b7fcdSDave Hansen if (flags & MS_RDONLY) 24812e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2482fc33a7bbSChristoph Hellwig 2483ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 2484ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 2485ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 2486ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 2487ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 2488ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 2489ffbc6f0eSEric W. Biederman } 2490ffbc6f0eSEric W. Biederman 24917a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2492d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2493d0adde57SMatthew Garrett MS_STRICTATIME); 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds if (flags & MS_REMOUNT) 24962d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 24971da177e4SLinus Torvalds data_page); 24981da177e4SLinus Torvalds else if (flags & MS_BIND) 24992d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 25009676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 25012d92ab3cSAl Viro retval = do_change_type(&path, flags); 25021da177e4SLinus Torvalds else if (flags & MS_MOVE) 25032d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 25041da177e4SLinus Torvalds else 25052d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 25061da177e4SLinus Torvalds dev_name, data_page); 25071da177e4SLinus Torvalds dput_out: 25082d92ab3cSAl Viro path_put(&path); 25091da177e4SLinus Torvalds return retval; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 2512771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2513771b1371SEric W. Biederman { 251498f842e6SEric W. Biederman proc_free_inum(ns->proc_inum); 2515771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2516771b1371SEric W. Biederman kfree(ns); 2517771b1371SEric W. Biederman } 2518771b1371SEric W. Biederman 25198823c079SEric W. Biederman /* 25208823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 25218823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 25228823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 25238823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 25248823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 25258823c079SEric W. Biederman */ 25268823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 25278823c079SEric W. Biederman 2528771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2529cf8d2c11STrond Myklebust { 2530cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 253198f842e6SEric W. Biederman int ret; 2532cf8d2c11STrond Myklebust 2533cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2534cf8d2c11STrond Myklebust if (!new_ns) 2535cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 253698f842e6SEric W. Biederman ret = proc_alloc_inum(&new_ns->proc_inum); 253798f842e6SEric W. Biederman if (ret) { 253898f842e6SEric W. Biederman kfree(new_ns); 253998f842e6SEric W. Biederman return ERR_PTR(ret); 254098f842e6SEric W. Biederman } 25418823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2542cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2543cf8d2c11STrond Myklebust new_ns->root = NULL; 2544cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2545cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2546cf8d2c11STrond Myklebust new_ns->event = 0; 2547771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2548cf8d2c11STrond Myklebust return new_ns; 2549cf8d2c11STrond Myklebust } 2550cf8d2c11STrond Myklebust 25519559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 25529559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 25531da177e4SLinus Torvalds { 25546b3286edSKirill Korotaev struct mnt_namespace *new_ns; 25557f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2556315fc83eSAl Viro struct mount *p, *q; 25579559f689SAl Viro struct mount *old; 2558cb338d06SAl Viro struct mount *new; 25597a472ef4SEric W. Biederman int copy_flags; 25601da177e4SLinus Torvalds 25619559f689SAl Viro BUG_ON(!ns); 25629559f689SAl Viro 25639559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 25649559f689SAl Viro get_mnt_ns(ns); 25659559f689SAl Viro return ns; 25669559f689SAl Viro } 25679559f689SAl Viro 25689559f689SAl Viro old = ns->root; 25699559f689SAl Viro 2570771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2571cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2572cf8d2c11STrond Myklebust return new_ns; 25731da177e4SLinus Torvalds 257497216be0SAl Viro namespace_lock(); 25751da177e4SLinus Torvalds /* First pass: copy the tree topology */ 25764ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 25779559f689SAl Viro if (user_ns != ns->user_ns) 2578132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 25797a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2580be34d1a3SDavid Howells if (IS_ERR(new)) { 2581328e6d90SAl Viro namespace_unlock(); 2582771b1371SEric W. Biederman free_mnt_ns(new_ns); 2583be34d1a3SDavid Howells return ERR_CAST(new); 25841da177e4SLinus Torvalds } 2585be08d6d2SAl Viro new_ns->root = new; 25861a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds /* 25891da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 25901da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 25911da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 25921da177e4SLinus Torvalds */ 2593909b0a88SAl Viro p = old; 2594cb338d06SAl Viro q = new; 25951da177e4SLinus Torvalds while (p) { 2596143c8c91SAl Viro q->mnt_ns = new_ns; 25979559f689SAl Viro if (new_fs) { 25989559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 25999559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2600315fc83eSAl Viro rootmnt = &p->mnt; 26011da177e4SLinus Torvalds } 26029559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 26039559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2604315fc83eSAl Viro pwdmnt = &p->mnt; 26051da177e4SLinus Torvalds } 26061da177e4SLinus Torvalds } 2607909b0a88SAl Viro p = next_mnt(p, old); 2608909b0a88SAl Viro q = next_mnt(q, new); 26094ce5d2b1SEric W. Biederman if (!q) 26104ce5d2b1SEric W. Biederman break; 26114ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 26124ce5d2b1SEric W. Biederman p = next_mnt(p, old); 26131da177e4SLinus Torvalds } 2614328e6d90SAl Viro namespace_unlock(); 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds if (rootmnt) 2617f03c6599SAl Viro mntput(rootmnt); 26181da177e4SLinus Torvalds if (pwdmnt) 2619f03c6599SAl Viro mntput(pwdmnt); 26201da177e4SLinus Torvalds 2621741a2951SJANAK DESAI return new_ns; 2622741a2951SJANAK DESAI } 2623741a2951SJANAK DESAI 2624cf8d2c11STrond Myklebust /** 2625cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2626cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2627cf8d2c11STrond Myklebust */ 26281a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2629cf8d2c11STrond Myklebust { 2630771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2631cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 26321a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 26331a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2634be08d6d2SAl Viro new_ns->root = mnt; 2635b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2636c1334495SAl Viro } else { 26371a4eeaf2SAl Viro mntput(m); 2638cf8d2c11STrond Myklebust } 2639cf8d2c11STrond Myklebust return new_ns; 2640cf8d2c11STrond Myklebust } 2641cf8d2c11STrond Myklebust 2642ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2643ea441d11SAl Viro { 2644ea441d11SAl Viro struct mnt_namespace *ns; 2645d31da0f0SAl Viro struct super_block *s; 2646ea441d11SAl Viro struct path path; 2647ea441d11SAl Viro int err; 2648ea441d11SAl Viro 2649ea441d11SAl Viro ns = create_mnt_ns(mnt); 2650ea441d11SAl Viro if (IS_ERR(ns)) 2651ea441d11SAl Viro return ERR_CAST(ns); 2652ea441d11SAl Viro 2653ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2654ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2655ea441d11SAl Viro 2656ea441d11SAl Viro put_mnt_ns(ns); 2657ea441d11SAl Viro 2658ea441d11SAl Viro if (err) 2659ea441d11SAl Viro return ERR_PTR(err); 2660ea441d11SAl Viro 2661ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2662d31da0f0SAl Viro s = path.mnt->mnt_sb; 2663d31da0f0SAl Viro atomic_inc(&s->s_active); 2664ea441d11SAl Viro mntput(path.mnt); 2665ea441d11SAl Viro /* lock the sucker */ 2666d31da0f0SAl Viro down_write(&s->s_umount); 2667ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2668ea441d11SAl Viro return path.dentry; 2669ea441d11SAl Viro } 2670ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2671ea441d11SAl Viro 2672bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2673bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 26741da177e4SLinus Torvalds { 2675eca6f534SVegard Nossum int ret; 2676eca6f534SVegard Nossum char *kernel_type; 267791a27b2aSJeff Layton struct filename *kernel_dir; 2678eca6f534SVegard Nossum char *kernel_dev; 26791da177e4SLinus Torvalds unsigned long data_page; 26801da177e4SLinus Torvalds 2681eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2682eca6f534SVegard Nossum if (ret < 0) 2683eca6f534SVegard Nossum goto out_type; 26841da177e4SLinus Torvalds 2685eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2686eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2687eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2688eca6f534SVegard Nossum goto out_dir; 2689eca6f534SVegard Nossum } 26901da177e4SLinus Torvalds 2691eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2692eca6f534SVegard Nossum if (ret < 0) 2693eca6f534SVegard Nossum goto out_dev; 26941da177e4SLinus Torvalds 2695eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2696eca6f534SVegard Nossum if (ret < 0) 2697eca6f534SVegard Nossum goto out_data; 26981da177e4SLinus Torvalds 269991a27b2aSJeff Layton ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags, 2700eca6f534SVegard Nossum (void *) data_page); 2701eca6f534SVegard Nossum 27021da177e4SLinus Torvalds free_page(data_page); 2703eca6f534SVegard Nossum out_data: 2704eca6f534SVegard Nossum kfree(kernel_dev); 2705eca6f534SVegard Nossum out_dev: 2706eca6f534SVegard Nossum putname(kernel_dir); 2707eca6f534SVegard Nossum out_dir: 2708eca6f534SVegard Nossum kfree(kernel_type); 2709eca6f534SVegard Nossum out_type: 2710eca6f534SVegard Nossum return ret; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds /* 2714afac7cbaSAl Viro * Return true if path is reachable from root 2715afac7cbaSAl Viro * 271648a066e7SAl Viro * namespace_sem or mount_lock is held 2717afac7cbaSAl Viro */ 2718643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2719afac7cbaSAl Viro const struct path *root) 2720afac7cbaSAl Viro { 2721643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2722a73324daSAl Viro dentry = mnt->mnt_mountpoint; 27230714a533SAl Viro mnt = mnt->mnt_parent; 2724afac7cbaSAl Viro } 2725643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2726afac7cbaSAl Viro } 2727afac7cbaSAl Viro 2728afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2729afac7cbaSAl Viro { 2730afac7cbaSAl Viro int res; 273148a066e7SAl Viro read_seqlock_excl(&mount_lock); 2732643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 273348a066e7SAl Viro read_sequnlock_excl(&mount_lock); 2734afac7cbaSAl Viro return res; 2735afac7cbaSAl Viro } 2736afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2737afac7cbaSAl Viro 2738afac7cbaSAl Viro /* 27391da177e4SLinus Torvalds * pivot_root Semantics: 27401da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 27411da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 27421da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 27431da177e4SLinus Torvalds * 27441da177e4SLinus Torvalds * Restrictions: 27451da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 27461da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 27471da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 27481da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 27491da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 27501da177e4SLinus Torvalds * 27514a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 27524a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 27534a0d11faSNeil Brown * in this situation. 27544a0d11faSNeil Brown * 27551da177e4SLinus Torvalds * Notes: 27561da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 27571da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 27581da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 27591da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 27601da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 27611da177e4SLinus Torvalds * first. 27621da177e4SLinus Torvalds */ 27633480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 27643480b257SHeiko Carstens const char __user *, put_old) 27651da177e4SLinus Torvalds { 27662d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 276784d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 276884d17192SAl Viro struct mountpoint *old_mp, *root_mp; 27691da177e4SLinus Torvalds int error; 27701da177e4SLinus Torvalds 27719b40bc90SAl Viro if (!may_mount()) 27721da177e4SLinus Torvalds return -EPERM; 27731da177e4SLinus Torvalds 27742d8f3038SAl Viro error = user_path_dir(new_root, &new); 27751da177e4SLinus Torvalds if (error) 27761da177e4SLinus Torvalds goto out0; 27771da177e4SLinus Torvalds 27782d8f3038SAl Viro error = user_path_dir(put_old, &old); 27791da177e4SLinus Torvalds if (error) 27801da177e4SLinus Torvalds goto out1; 27811da177e4SLinus Torvalds 27822d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2783b12cea91SAl Viro if (error) 2784b12cea91SAl Viro goto out2; 27851da177e4SLinus Torvalds 2786f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 278784d17192SAl Viro old_mp = lock_mount(&old); 278884d17192SAl Viro error = PTR_ERR(old_mp); 278984d17192SAl Viro if (IS_ERR(old_mp)) 2790b12cea91SAl Viro goto out3; 2791b12cea91SAl Viro 27921da177e4SLinus Torvalds error = -EINVAL; 2793419148daSAl Viro new_mnt = real_mount(new.mnt); 2794419148daSAl Viro root_mnt = real_mount(root.mnt); 279584d17192SAl Viro old_mnt = real_mount(old.mnt); 279684d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 2797fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2798fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2799b12cea91SAl Viro goto out4; 2800143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2801b12cea91SAl Viro goto out4; 28025ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 28035ff9d8a6SEric W. Biederman goto out4; 28041da177e4SLinus Torvalds error = -ENOENT; 2805f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2806b12cea91SAl Viro goto out4; 28071da177e4SLinus Torvalds error = -EBUSY; 280884d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 2809b12cea91SAl Viro goto out4; /* loop, on the same file system */ 28101da177e4SLinus Torvalds error = -EINVAL; 28118c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2812b12cea91SAl Viro goto out4; /* not a mountpoint */ 2813676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2814b12cea91SAl Viro goto out4; /* not attached */ 281584d17192SAl Viro root_mp = root_mnt->mnt_mp; 28162d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2817b12cea91SAl Viro goto out4; /* not a mountpoint */ 2818676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2819b12cea91SAl Viro goto out4; /* not attached */ 28204ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 282184d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 2822b12cea91SAl Viro goto out4; 28230d082601SEric W. Biederman /* make certain new is below the root */ 28240d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 28250d082601SEric W. Biederman goto out4; 282684d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 2827719ea2fbSAl Viro lock_mount_hash(); 2828419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2829419148daSAl Viro detach_mnt(root_mnt, &root_parent); 28305ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 28315ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 28325ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 28335ff9d8a6SEric W. Biederman } 28344ac91378SJan Blunck /* mount old root on put_old */ 283584d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 28364ac91378SJan Blunck /* mount new_root on / */ 283784d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 28386b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2839719ea2fbSAl Viro unlock_mount_hash(); 28402d8f3038SAl Viro chroot_fs_refs(&root, &new); 284184d17192SAl Viro put_mountpoint(root_mp); 28421da177e4SLinus Torvalds error = 0; 2843b12cea91SAl Viro out4: 284484d17192SAl Viro unlock_mount(old_mp); 2845b12cea91SAl Viro if (!error) { 28461a390689SAl Viro path_put(&root_parent); 28471a390689SAl Viro path_put(&parent_path); 2848b12cea91SAl Viro } 2849b12cea91SAl Viro out3: 28508c3ee42eSAl Viro path_put(&root); 2851b12cea91SAl Viro out2: 28522d8f3038SAl Viro path_put(&old); 28531da177e4SLinus Torvalds out1: 28542d8f3038SAl Viro path_put(&new); 28551da177e4SLinus Torvalds out0: 28561da177e4SLinus Torvalds return error; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds static void __init init_mount_tree(void) 28601da177e4SLinus Torvalds { 28611da177e4SLinus Torvalds struct vfsmount *mnt; 28626b3286edSKirill Korotaev struct mnt_namespace *ns; 2863ac748a09SJan Blunck struct path root; 28640c55cfc4SEric W. Biederman struct file_system_type *type; 28651da177e4SLinus Torvalds 28660c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 28670c55cfc4SEric W. Biederman if (!type) 28680c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 28690c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 28700c55cfc4SEric W. Biederman put_filesystem(type); 28711da177e4SLinus Torvalds if (IS_ERR(mnt)) 28721da177e4SLinus Torvalds panic("Can't create rootfs"); 2873b3e19d92SNick Piggin 28743b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 28753b22edc5STrond Myklebust if (IS_ERR(ns)) 28761da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 28771da177e4SLinus Torvalds 28786b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 28796b3286edSKirill Korotaev get_mnt_ns(ns); 28801da177e4SLinus Torvalds 2881be08d6d2SAl Viro root.mnt = mnt; 2882be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2883ac748a09SJan Blunck 2884ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2885ac748a09SJan Blunck set_fs_root(current->fs, &root); 28861da177e4SLinus Torvalds } 28871da177e4SLinus Torvalds 288874bf17cfSDenis Cheng void __init mnt_init(void) 28891da177e4SLinus Torvalds { 289013f14b4dSEric Dumazet unsigned u; 289115a67dd8SRandy Dunlap int err; 28921da177e4SLinus Torvalds 28937d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 289420c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 28951da177e4SLinus Torvalds 28960818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 289738129a13SAl Viro sizeof(struct hlist_head), 28980818bf27SAl Viro mhash_entries, 19, 28990818bf27SAl Viro 0, 29000818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 29010818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 29020818bf27SAl Viro sizeof(struct hlist_head), 29030818bf27SAl Viro mphash_entries, 19, 29040818bf27SAl Viro 0, 29050818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 29061da177e4SLinus Torvalds 290784d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 29081da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 29091da177e4SLinus Torvalds 29100818bf27SAl Viro for (u = 0; u <= m_hash_mask; u++) 291138129a13SAl Viro INIT_HLIST_HEAD(&mount_hashtable[u]); 29120818bf27SAl Viro for (u = 0; u <= mp_hash_mask; u++) 29130818bf27SAl Viro INIT_HLIST_HEAD(&mountpoint_hashtable[u]); 29141da177e4SLinus Torvalds 29154b93dc9bSTejun Heo kernfs_init(); 29164b93dc9bSTejun Heo 291715a67dd8SRandy Dunlap err = sysfs_init(); 291815a67dd8SRandy Dunlap if (err) 291915a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 29208e24eea7SHarvey Harrison __func__, err); 292100d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 292200d26666SGreg Kroah-Hartman if (!fs_kobj) 29238e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 29241da177e4SLinus Torvalds init_rootfs(); 29251da177e4SLinus Torvalds init_mount_tree(); 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 2928616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 29291da177e4SLinus Torvalds { 2930d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2931616511d0STrond Myklebust return; 29327b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 2933771b1371SEric W. Biederman free_mnt_ns(ns); 29341da177e4SLinus Torvalds } 29359d412a43SAl Viro 29369d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 29379d412a43SAl Viro { 2938423e0ab0STim Chen struct vfsmount *mnt; 2939423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2940423e0ab0STim Chen if (!IS_ERR(mnt)) { 2941423e0ab0STim Chen /* 2942423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2943423e0ab0STim Chen * we unmount before file sys is unregistered 2944423e0ab0STim Chen */ 2945f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2946423e0ab0STim Chen } 2947423e0ab0STim Chen return mnt; 29489d412a43SAl Viro } 29499d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2950423e0ab0STim Chen 2951423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2952423e0ab0STim Chen { 2953423e0ab0STim Chen /* release long term mount so mount point can be released */ 2954423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2955f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 295648a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 2957423e0ab0STim Chen mntput(mnt); 2958423e0ab0STim Chen } 2959423e0ab0STim Chen } 2960423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 296102125a82SAl Viro 296202125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 296302125a82SAl Viro { 2964143c8c91SAl Viro return check_mnt(real_mount(mnt)); 296502125a82SAl Viro } 29668823c079SEric W. Biederman 29673151527eSEric W. Biederman bool current_chrooted(void) 29683151527eSEric W. Biederman { 29693151527eSEric W. Biederman /* Does the current process have a non-standard root */ 29703151527eSEric W. Biederman struct path ns_root; 29713151527eSEric W. Biederman struct path fs_root; 29723151527eSEric W. Biederman bool chrooted; 29733151527eSEric W. Biederman 29743151527eSEric W. Biederman /* Find the namespace root */ 29753151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 29763151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 29773151527eSEric W. Biederman path_get(&ns_root); 29783151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 29793151527eSEric W. Biederman ; 29803151527eSEric W. Biederman 29813151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 29823151527eSEric W. Biederman 29833151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 29843151527eSEric W. Biederman 29853151527eSEric W. Biederman path_put(&fs_root); 29863151527eSEric W. Biederman path_put(&ns_root); 29873151527eSEric W. Biederman 29883151527eSEric W. Biederman return chrooted; 29893151527eSEric W. Biederman } 29903151527eSEric W. Biederman 2991e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type) 299287a8ebd6SEric W. Biederman { 299387a8ebd6SEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 299487a8ebd6SEric W. Biederman struct mount *mnt; 2995e51db735SEric W. Biederman bool visible = false; 299687a8ebd6SEric W. Biederman 2997e51db735SEric W. Biederman if (unlikely(!ns)) 2998e51db735SEric W. Biederman return false; 2999e51db735SEric W. Biederman 300044bb4385SAl Viro down_read(&namespace_sem); 300187a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3002e51db735SEric W. Biederman struct mount *child; 3003e51db735SEric W. Biederman if (mnt->mnt.mnt_sb->s_type != type) 3004e51db735SEric W. Biederman continue; 3005e51db735SEric W. Biederman 3006e51db735SEric W. Biederman /* This mount is not fully visible if there are any child mounts 3007e51db735SEric W. Biederman * that cover anything except for empty directories. 3008e51db735SEric W. Biederman */ 3009e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3010e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3011e51db735SEric W. Biederman if (!S_ISDIR(inode->i_mode)) 3012e51db735SEric W. Biederman goto next; 301341301ae7SEric W. Biederman if (inode->i_nlink > 2) 3014e51db735SEric W. Biederman goto next; 301587a8ebd6SEric W. Biederman } 3016e51db735SEric W. Biederman visible = true; 3017e51db735SEric W. Biederman goto found; 3018e51db735SEric W. Biederman next: ; 301987a8ebd6SEric W. Biederman } 3020e51db735SEric W. Biederman found: 302144bb4385SAl Viro up_read(&namespace_sem); 3022e51db735SEric W. Biederman return visible; 302387a8ebd6SEric W. Biederman } 302487a8ebd6SEric W. Biederman 30258823c079SEric W. Biederman static void *mntns_get(struct task_struct *task) 30268823c079SEric W. Biederman { 30278823c079SEric W. Biederman struct mnt_namespace *ns = NULL; 30288823c079SEric W. Biederman struct nsproxy *nsproxy; 30298823c079SEric W. Biederman 3030728dba3aSEric W. Biederman task_lock(task); 3031728dba3aSEric W. Biederman nsproxy = task->nsproxy; 30328823c079SEric W. Biederman if (nsproxy) { 30338823c079SEric W. Biederman ns = nsproxy->mnt_ns; 30348823c079SEric W. Biederman get_mnt_ns(ns); 30358823c079SEric W. Biederman } 3036728dba3aSEric W. Biederman task_unlock(task); 30378823c079SEric W. Biederman 30388823c079SEric W. Biederman return ns; 30398823c079SEric W. Biederman } 30408823c079SEric W. Biederman 30418823c079SEric W. Biederman static void mntns_put(void *ns) 30428823c079SEric W. Biederman { 30438823c079SEric W. Biederman put_mnt_ns(ns); 30448823c079SEric W. Biederman } 30458823c079SEric W. Biederman 30468823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns) 30478823c079SEric W. Biederman { 30488823c079SEric W. Biederman struct fs_struct *fs = current->fs; 30498823c079SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 30508823c079SEric W. Biederman struct path root; 30518823c079SEric W. Biederman 30520c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3053c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3054c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3055ae11e0f1SZhao Hongjiang return -EPERM; 30568823c079SEric W. Biederman 30578823c079SEric W. Biederman if (fs->users != 1) 30588823c079SEric W. Biederman return -EINVAL; 30598823c079SEric W. Biederman 30608823c079SEric W. Biederman get_mnt_ns(mnt_ns); 30618823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 30628823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 30638823c079SEric W. Biederman 30648823c079SEric W. Biederman /* Find the root */ 30658823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 30668823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 30678823c079SEric W. Biederman path_get(&root); 30688823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 30698823c079SEric W. Biederman ; 30708823c079SEric W. Biederman 30718823c079SEric W. Biederman /* Update the pwd and root */ 30728823c079SEric W. Biederman set_fs_pwd(fs, &root); 30738823c079SEric W. Biederman set_fs_root(fs, &root); 30748823c079SEric W. Biederman 30758823c079SEric W. Biederman path_put(&root); 30768823c079SEric W. Biederman return 0; 30778823c079SEric W. Biederman } 30788823c079SEric W. Biederman 307998f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns) 308098f842e6SEric W. Biederman { 308198f842e6SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 308298f842e6SEric W. Biederman return mnt_ns->proc_inum; 308398f842e6SEric W. Biederman } 308498f842e6SEric W. Biederman 30858823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 30868823c079SEric W. Biederman .name = "mnt", 30878823c079SEric W. Biederman .type = CLONE_NEWNS, 30888823c079SEric W. Biederman .get = mntns_get, 30898823c079SEric W. Biederman .put = mntns_put, 30908823c079SEric W. Biederman .install = mntns_install, 309198f842e6SEric W. Biederman .inum = mntns_inum, 30928823c079SEric W. Biederman }; 3093