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> 151da177e4SLinus Torvalds #include <linux/namei.h> 161da177e4SLinus Torvalds #include <linux/security.h> 1773cd49ecSMiklos Szeredi #include <linux/idr.h> 18d10577a8SAl Viro #include <linux/acct.h> /* acct_auto_close_mnt */ 19d10577a8SAl Viro #include <linux/ramfs.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> 2307b20889SRam Pai #include "pnode.h" 24948730b0SAdrian Bunk #include "internal.h" 251da177e4SLinus Torvalds 2613f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head)) 2713f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT) 2813f14b4dSEric Dumazet 295addc5ddSAl Viro static int event; 3073cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 31719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 3299b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 33f21f6220SAl Viro static int mnt_id_start = 0; 34f21f6220SAl Viro static int mnt_group_start = 1; 355addc5ddSAl Viro 36fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly; 37e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 38390c6843SRam Pai static struct rw_semaphore namespace_sem; 391da177e4SLinus Torvalds 40f87fd4c2SMiklos Szeredi /* /sys/fs */ 4100d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 4200d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 43f87fd4c2SMiklos Szeredi 4499b7db7bSNick Piggin /* 4599b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 4699b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 4799b7db7bSNick Piggin * up the tree. 4899b7db7bSNick Piggin * 4999b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 5099b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 5199b7db7bSNick Piggin */ 5299b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock); 5399b7db7bSNick Piggin 541da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 571da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 5813f14b4dSEric Dumazet tmp = tmp + (tmp >> HASH_SHIFT); 5913f14b4dSEric Dumazet return tmp & (HASH_SIZE - 1); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 623d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) 633d733633SDave Hansen 6499b7db7bSNick Piggin /* 6599b7db7bSNick Piggin * allocation is serialized by namespace_sem, but we need the spinlock to 6699b7db7bSNick Piggin * serialize with freeing. 6799b7db7bSNick Piggin */ 68b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 6973cd49ecSMiklos Szeredi { 7073cd49ecSMiklos Szeredi int res; 7173cd49ecSMiklos Szeredi 7273cd49ecSMiklos Szeredi retry: 7373cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 7499b7db7bSNick Piggin spin_lock(&mnt_id_lock); 7515169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 76f21f6220SAl Viro if (!res) 7715169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 7899b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 7973cd49ecSMiklos Szeredi if (res == -EAGAIN) 8073cd49ecSMiklos Szeredi goto retry; 8173cd49ecSMiklos Szeredi 8273cd49ecSMiklos Szeredi return res; 8373cd49ecSMiklos Szeredi } 8473cd49ecSMiklos Szeredi 85b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 8673cd49ecSMiklos Szeredi { 8715169fe7SAl Viro int id = mnt->mnt_id; 8899b7db7bSNick Piggin spin_lock(&mnt_id_lock); 89f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 90f21f6220SAl Viro if (mnt_id_start > id) 91f21f6220SAl Viro mnt_id_start = id; 9299b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 9373cd49ecSMiklos Szeredi } 9473cd49ecSMiklos Szeredi 95719f5d7fSMiklos Szeredi /* 96719f5d7fSMiklos Szeredi * Allocate a new peer group ID 97719f5d7fSMiklos Szeredi * 98719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 99719f5d7fSMiklos Szeredi */ 1004b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 101719f5d7fSMiklos Szeredi { 102f21f6220SAl Viro int res; 103f21f6220SAl Viro 104719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 105719f5d7fSMiklos Szeredi return -ENOMEM; 106719f5d7fSMiklos Szeredi 107f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 108f21f6220SAl Viro mnt_group_start, 10915169fe7SAl Viro &mnt->mnt_group_id); 110f21f6220SAl Viro if (!res) 11115169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 112f21f6220SAl Viro 113f21f6220SAl Viro return res; 114719f5d7fSMiklos Szeredi } 115719f5d7fSMiklos Szeredi 116719f5d7fSMiklos Szeredi /* 117719f5d7fSMiklos Szeredi * Release a peer group ID 118719f5d7fSMiklos Szeredi */ 1194b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 120719f5d7fSMiklos Szeredi { 12115169fe7SAl Viro int id = mnt->mnt_group_id; 122f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 123f21f6220SAl Viro if (mnt_group_start > id) 124f21f6220SAl Viro mnt_group_start = id; 12515169fe7SAl Viro mnt->mnt_group_id = 0; 126719f5d7fSMiklos Szeredi } 127719f5d7fSMiklos Szeredi 128b3e19d92SNick Piggin /* 129b3e19d92SNick Piggin * vfsmount lock must be held for read 130b3e19d92SNick Piggin */ 13183adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 132b3e19d92SNick Piggin { 133b3e19d92SNick Piggin #ifdef CONFIG_SMP 13468e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 135b3e19d92SNick Piggin #else 136b3e19d92SNick Piggin preempt_disable(); 13768e8a9feSAl Viro mnt->mnt_count += n; 138b3e19d92SNick Piggin preempt_enable(); 139b3e19d92SNick Piggin #endif 140b3e19d92SNick Piggin } 141b3e19d92SNick Piggin 142b3e19d92SNick Piggin /* 143b3e19d92SNick Piggin * vfsmount lock must be held for write 144b3e19d92SNick Piggin */ 14583adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 146b3e19d92SNick Piggin { 147b3e19d92SNick Piggin #ifdef CONFIG_SMP 148f03c6599SAl Viro unsigned int count = 0; 149b3e19d92SNick Piggin int cpu; 150b3e19d92SNick Piggin 151b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 15268e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 153b3e19d92SNick Piggin } 154b3e19d92SNick Piggin 155b3e19d92SNick Piggin return count; 156b3e19d92SNick Piggin #else 15768e8a9feSAl Viro return mnt->mnt_count; 158b3e19d92SNick Piggin #endif 159b3e19d92SNick Piggin } 160b3e19d92SNick Piggin 161b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1621da177e4SLinus Torvalds { 163c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 164c63181e6SAl Viro if (mnt) { 16573cd49ecSMiklos Szeredi int err; 16673cd49ecSMiklos Szeredi 167c63181e6SAl Viro err = mnt_alloc_id(mnt); 16888b38782SLi Zefan if (err) 16988b38782SLi Zefan goto out_free_cache; 17088b38782SLi Zefan 17188b38782SLi Zefan if (name) { 172c63181e6SAl Viro mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 173c63181e6SAl Viro if (!mnt->mnt_devname) 17488b38782SLi Zefan goto out_free_id; 17573cd49ecSMiklos Szeredi } 17673cd49ecSMiklos Szeredi 177b3e19d92SNick Piggin #ifdef CONFIG_SMP 178c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 179c63181e6SAl Viro if (!mnt->mnt_pcp) 180b3e19d92SNick Piggin goto out_free_devname; 181b3e19d92SNick Piggin 182c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 183b3e19d92SNick Piggin #else 184c63181e6SAl Viro mnt->mnt_count = 1; 185c63181e6SAl Viro mnt->mnt_writers = 0; 186b3e19d92SNick Piggin #endif 187b3e19d92SNick Piggin 188c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_hash); 189c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 190c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 191c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 192c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 193c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 194c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 195c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 1962504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 1972504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 1982504c5d6SAndreas Gruenbacher #endif 1991da177e4SLinus Torvalds } 200c63181e6SAl Viro return mnt; 20188b38782SLi Zefan 202d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 203d3ef3d73Snpiggin@suse.de out_free_devname: 204c63181e6SAl Viro kfree(mnt->mnt_devname); 205d3ef3d73Snpiggin@suse.de #endif 20688b38782SLi Zefan out_free_id: 207c63181e6SAl Viro mnt_free_id(mnt); 20888b38782SLi Zefan out_free_cache: 209c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 21088b38782SLi Zefan return NULL; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2138366025eSDave Hansen /* 2148366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2158366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2168366025eSDave Hansen * We must keep track of when those operations start 2178366025eSDave Hansen * (for permission checks) and when they end, so that 2188366025eSDave Hansen * we can determine when writes are able to occur to 2198366025eSDave Hansen * a filesystem. 2208366025eSDave Hansen */ 2213d733633SDave Hansen /* 2223d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2233d733633SDave Hansen * @mnt: the mount to check for its write status 2243d733633SDave Hansen * 2253d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2263d733633SDave Hansen * It does not guarantee that the filesystem will stay 2273d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2283d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2293d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2303d733633SDave Hansen * r/w. 2313d733633SDave Hansen */ 2323d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2333d733633SDave Hansen { 2342e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2352e4b7fcdSDave Hansen return 1; 2362e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2372e4b7fcdSDave Hansen return 1; 2382e4b7fcdSDave Hansen return 0; 2393d733633SDave Hansen } 2403d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2413d733633SDave Hansen 24283adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2433d733633SDave Hansen { 244d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 24568e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 246d3ef3d73Snpiggin@suse.de #else 24768e8a9feSAl Viro mnt->mnt_writers++; 248d3ef3d73Snpiggin@suse.de #endif 2493d733633SDave Hansen } 2503d733633SDave Hansen 25183adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2523d733633SDave Hansen { 253d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 25468e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 255d3ef3d73Snpiggin@suse.de #else 25668e8a9feSAl Viro mnt->mnt_writers--; 257d3ef3d73Snpiggin@suse.de #endif 258d3ef3d73Snpiggin@suse.de } 259d3ef3d73Snpiggin@suse.de 26083adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 261d3ef3d73Snpiggin@suse.de { 262d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 263d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2643d733633SDave Hansen int cpu; 2653d733633SDave Hansen 2663d733633SDave Hansen for_each_possible_cpu(cpu) { 26768e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2683d733633SDave Hansen } 2693d733633SDave Hansen 270d3ef3d73Snpiggin@suse.de return count; 271d3ef3d73Snpiggin@suse.de #else 272d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 273d3ef3d73Snpiggin@suse.de #endif 2743d733633SDave Hansen } 2753d733633SDave Hansen 2764ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2774ed5e82fSMiklos Szeredi { 2784ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2794ed5e82fSMiklos Szeredi return 1; 2804ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2814ed5e82fSMiklos Szeredi smp_rmb(); 2824ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2834ed5e82fSMiklos Szeredi } 2844ed5e82fSMiklos Szeredi 2853d733633SDave Hansen /* 286eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 287eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 288eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 289eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 2903d733633SDave Hansen */ 2918366025eSDave Hansen /** 292eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 29383adc753SAl Viro * @m: the mount on which to take a write 2948366025eSDave Hansen * 295eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 296eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 297eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 298eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 299eb04c282SJan Kara * called. This is effectively a refcount. 3008366025eSDave Hansen */ 301eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3028366025eSDave Hansen { 30383adc753SAl Viro struct mount *mnt = real_mount(m); 3043d733633SDave Hansen int ret = 0; 3053d733633SDave Hansen 306d3ef3d73Snpiggin@suse.de preempt_disable(); 307c6653a83SNick Piggin mnt_inc_writers(mnt); 308d3ef3d73Snpiggin@suse.de /* 309c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 310d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 311d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 312d3ef3d73Snpiggin@suse.de */ 313d3ef3d73Snpiggin@suse.de smp_mb(); 31483adc753SAl Viro while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 315d3ef3d73Snpiggin@suse.de cpu_relax(); 316d3ef3d73Snpiggin@suse.de /* 317d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 318d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 319d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 320d3ef3d73Snpiggin@suse.de */ 321d3ef3d73Snpiggin@suse.de smp_rmb(); 3224ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 323c6653a83SNick Piggin mnt_dec_writers(mnt); 3243d733633SDave Hansen ret = -EROFS; 3253d733633SDave Hansen } 326d3ef3d73Snpiggin@suse.de preempt_enable(); 327eb04c282SJan Kara 328eb04c282SJan Kara return ret; 329eb04c282SJan Kara } 330eb04c282SJan Kara 331eb04c282SJan Kara /** 332eb04c282SJan Kara * mnt_want_write - get write access to a mount 333eb04c282SJan Kara * @m: the mount on which to take a write 334eb04c282SJan Kara * 335eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 336eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 337eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 338eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 339eb04c282SJan Kara */ 340eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 341eb04c282SJan Kara { 342eb04c282SJan Kara int ret; 343eb04c282SJan Kara 344eb04c282SJan Kara sb_start_write(m->mnt_sb); 345eb04c282SJan Kara ret = __mnt_want_write(m); 346eb04c282SJan Kara if (ret) 347eb04c282SJan Kara sb_end_write(m->mnt_sb); 3483d733633SDave Hansen return ret; 3498366025eSDave Hansen } 3508366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3518366025eSDave Hansen 3528366025eSDave Hansen /** 35396029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 35496029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 35596029c4eSnpiggin@suse.de * 35696029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 35796029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 35896029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 35996029c4eSnpiggin@suse.de * on it. This allows some optimisation. 36096029c4eSnpiggin@suse.de * 36196029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 36296029c4eSnpiggin@suse.de * drop the reference. 36396029c4eSnpiggin@suse.de */ 36496029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 36596029c4eSnpiggin@suse.de { 36696029c4eSnpiggin@suse.de /* superblock may be r/o */ 36796029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 36896029c4eSnpiggin@suse.de return -EROFS; 36996029c4eSnpiggin@suse.de preempt_disable(); 37083adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 37196029c4eSnpiggin@suse.de preempt_enable(); 37296029c4eSnpiggin@suse.de return 0; 37396029c4eSnpiggin@suse.de } 37496029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 37596029c4eSnpiggin@suse.de 37696029c4eSnpiggin@suse.de /** 377eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 378eb04c282SJan Kara * @file: the file who's mount on which to take a write 379eb04c282SJan Kara * 380eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 381eb04c282SJan Kara * do some optimisations if the file is open for write already 382eb04c282SJan Kara */ 383eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 384eb04c282SJan Kara { 385eb04c282SJan Kara struct inode *inode = file->f_dentry->d_inode; 386eb04c282SJan Kara 387eb04c282SJan Kara if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 388eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 389eb04c282SJan Kara else 390eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 391eb04c282SJan Kara } 392eb04c282SJan Kara 393eb04c282SJan Kara /** 39496029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 39596029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 39696029c4eSnpiggin@suse.de * 39796029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 39896029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 39996029c4eSnpiggin@suse.de */ 40096029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 40196029c4eSnpiggin@suse.de { 402eb04c282SJan Kara int ret; 403eb04c282SJan Kara 404eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 405eb04c282SJan Kara ret = __mnt_want_write_file(file); 406eb04c282SJan Kara if (ret) 407eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 408eb04c282SJan Kara return ret; 40996029c4eSnpiggin@suse.de } 41096029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 41196029c4eSnpiggin@suse.de 41296029c4eSnpiggin@suse.de /** 413eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4148366025eSDave Hansen * @mnt: the mount on which to give up write access 4158366025eSDave Hansen * 4168366025eSDave Hansen * Tells the low-level filesystem that we are done 4178366025eSDave Hansen * performing writes to it. Must be matched with 418eb04c282SJan Kara * __mnt_want_write() call above. 4198366025eSDave Hansen */ 420eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4218366025eSDave Hansen { 422d3ef3d73Snpiggin@suse.de preempt_disable(); 42383adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 424d3ef3d73Snpiggin@suse.de preempt_enable(); 4258366025eSDave Hansen } 426eb04c282SJan Kara 427eb04c282SJan Kara /** 428eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 429eb04c282SJan Kara * @mnt: the mount on which to give up write access 430eb04c282SJan Kara * 431eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 432eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 433eb04c282SJan Kara * mnt_want_write() call above. 434eb04c282SJan Kara */ 435eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 436eb04c282SJan Kara { 437eb04c282SJan Kara __mnt_drop_write(mnt); 438eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 439eb04c282SJan Kara } 4408366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4418366025eSDave Hansen 442eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 443eb04c282SJan Kara { 444eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 445eb04c282SJan Kara } 446eb04c282SJan Kara 4472a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4482a79f17eSAl Viro { 4492a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4502a79f17eSAl Viro } 4512a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4522a79f17eSAl Viro 45383adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4548366025eSDave Hansen { 4553d733633SDave Hansen int ret = 0; 4563d733633SDave Hansen 457962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 45883adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 459d3ef3d73Snpiggin@suse.de /* 460d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 461d3ef3d73Snpiggin@suse.de * should be visible before we do. 462d3ef3d73Snpiggin@suse.de */ 463d3ef3d73Snpiggin@suse.de smp_mb(); 464d3ef3d73Snpiggin@suse.de 465d3ef3d73Snpiggin@suse.de /* 466d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 467d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 468d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 469d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 470d3ef3d73Snpiggin@suse.de * 471d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 472d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 473d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 474d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 475d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 476d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 477d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 478d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 479d3ef3d73Snpiggin@suse.de * we're counting up here. 480d3ef3d73Snpiggin@suse.de */ 481c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 482d3ef3d73Snpiggin@suse.de ret = -EBUSY; 483d3ef3d73Snpiggin@suse.de else 48483adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 485d3ef3d73Snpiggin@suse.de /* 486d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 487d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 488d3ef3d73Snpiggin@suse.de */ 489d3ef3d73Snpiggin@suse.de smp_wmb(); 49083adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 491962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 4923d733633SDave Hansen return ret; 4933d733633SDave Hansen } 4948366025eSDave Hansen 49583adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 4962e4b7fcdSDave Hansen { 497962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 49883adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 499962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 5002e4b7fcdSDave Hansen } 5012e4b7fcdSDave Hansen 5024ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5034ed5e82fSMiklos Szeredi { 5044ed5e82fSMiklos Szeredi struct mount *mnt; 5054ed5e82fSMiklos Szeredi int err = 0; 5064ed5e82fSMiklos Szeredi 5078e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5088e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5098e8b8796SMiklos Szeredi return -EBUSY; 5108e8b8796SMiklos Szeredi 511962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 5124ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5134ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5144ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5154ed5e82fSMiklos Szeredi smp_mb(); 5164ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5174ed5e82fSMiklos Szeredi err = -EBUSY; 5184ed5e82fSMiklos Szeredi break; 5194ed5e82fSMiklos Szeredi } 5204ed5e82fSMiklos Szeredi } 5214ed5e82fSMiklos Szeredi } 5228e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5238e8b8796SMiklos Szeredi err = -EBUSY; 5248e8b8796SMiklos Szeredi 5254ed5e82fSMiklos Szeredi if (!err) { 5264ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5274ed5e82fSMiklos Szeredi smp_wmb(); 5284ed5e82fSMiklos Szeredi } 5294ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5304ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5314ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5324ed5e82fSMiklos Szeredi } 533962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 5344ed5e82fSMiklos Szeredi 5354ed5e82fSMiklos Szeredi return err; 5364ed5e82fSMiklos Szeredi } 5374ed5e82fSMiklos Szeredi 538b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5391da177e4SLinus Torvalds { 54052ba1621SAl Viro kfree(mnt->mnt_devname); 54173cd49ecSMiklos Szeredi mnt_free_id(mnt); 542d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 54368e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 544d3ef3d73Snpiggin@suse.de #endif 545b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* 549a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 550a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 55199b7db7bSNick Piggin * vfsmount_lock must be held for read or write. 5521da177e4SLinus Torvalds */ 553c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 554a05964f3SRam Pai int dir) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 5571da177e4SLinus Torvalds struct list_head *tmp = head; 558c7105365SAl Viro struct mount *p, *found = NULL; 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds for (;;) { 561a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 5621da177e4SLinus Torvalds p = NULL; 5631da177e4SLinus Torvalds if (tmp == head) 5641da177e4SLinus Torvalds break; 5651b8e5564SAl Viro p = list_entry(tmp, struct mount, mnt_hash); 566a73324daSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) { 567a05964f3SRam Pai found = p; 5681da177e4SLinus Torvalds break; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds return found; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 574a05964f3SRam Pai /* 575f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 576f015f126SDavid Howells * 577f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 578f015f126SDavid Howells * following mounts: 579f015f126SDavid Howells * 580f015f126SDavid Howells * mount /dev/sda1 /mnt 581f015f126SDavid Howells * mount /dev/sda2 /mnt 582f015f126SDavid Howells * mount /dev/sda3 /mnt 583f015f126SDavid Howells * 584f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 585f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 586f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 587f015f126SDavid Howells * 588f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 589a05964f3SRam Pai */ 5901c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 591a05964f3SRam Pai { 592c7105365SAl Viro struct mount *child_mnt; 59399b7db7bSNick Piggin 594962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 595c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 596c7105365SAl Viro if (child_mnt) { 597c7105365SAl Viro mnt_add_count(child_mnt, 1); 598962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 599c7105365SAl Viro return &child_mnt->mnt; 600c7105365SAl Viro } else { 601962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 602c7105365SAl Viro return NULL; 603c7105365SAl Viro } 604a05964f3SRam Pai } 605a05964f3SRam Pai 606143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 6071da177e4SLinus Torvalds { 6086b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 61199b7db7bSNick Piggin /* 61299b7db7bSNick Piggin * vfsmount lock must be held for write 61399b7db7bSNick Piggin */ 6146b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 6155addc5ddSAl Viro { 6165addc5ddSAl Viro if (ns) { 6175addc5ddSAl Viro ns->event = ++event; 6185addc5ddSAl Viro wake_up_interruptible(&ns->poll); 6195addc5ddSAl Viro } 6205addc5ddSAl Viro } 6215addc5ddSAl Viro 62299b7db7bSNick Piggin /* 62399b7db7bSNick Piggin * vfsmount lock must be held for write 62499b7db7bSNick Piggin */ 6256b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 6265addc5ddSAl Viro { 6275addc5ddSAl Viro if (ns && ns->event != event) { 6285addc5ddSAl Viro ns->event = event; 6295addc5ddSAl Viro wake_up_interruptible(&ns->poll); 6305addc5ddSAl Viro } 6315addc5ddSAl Viro } 6325addc5ddSAl Viro 63399b7db7bSNick Piggin /* 6345f57cbccSNick Piggin * Clear dentry's mounted state if it has no remaining mounts. 6355f57cbccSNick Piggin * vfsmount_lock must be held for write. 6365f57cbccSNick Piggin */ 637aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry) 6385f57cbccSNick Piggin { 6395f57cbccSNick Piggin unsigned u; 6405f57cbccSNick Piggin 6415f57cbccSNick Piggin for (u = 0; u < HASH_SIZE; u++) { 642d5e50f74SAl Viro struct mount *p; 6435f57cbccSNick Piggin 6441b8e5564SAl Viro list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { 645a73324daSAl Viro if (p->mnt_mountpoint == dentry) 6465f57cbccSNick Piggin return; 6475f57cbccSNick Piggin } 6485f57cbccSNick Piggin } 6495f57cbccSNick Piggin spin_lock(&dentry->d_lock); 6505f57cbccSNick Piggin dentry->d_flags &= ~DCACHE_MOUNTED; 6515f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 6525f57cbccSNick Piggin } 6535f57cbccSNick Piggin 6545f57cbccSNick Piggin /* 65599b7db7bSNick Piggin * vfsmount lock must be held for write 65699b7db7bSNick Piggin */ 657419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 6581da177e4SLinus Torvalds { 659a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 6600714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 6610714a533SAl Viro mnt->mnt_parent = mnt; 662a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 6636b41d536SAl Viro list_del_init(&mnt->mnt_child); 6641b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 665aa0a4cf0SAl Viro dentry_reset_mounted(old_path->dentry); 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 66899b7db7bSNick Piggin /* 66999b7db7bSNick Piggin * vfsmount lock must be held for write 67099b7db7bSNick Piggin */ 67114cf1fa8SAl Viro void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry, 67244d964d6SAl Viro struct mount *child_mnt) 673b90fa9aeSRam Pai { 6743a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 675a73324daSAl Viro child_mnt->mnt_mountpoint = dget(dentry); 6763a2393d7SAl Viro child_mnt->mnt_parent = mnt; 6775f57cbccSNick Piggin spin_lock(&dentry->d_lock); 6785f57cbccSNick Piggin dentry->d_flags |= DCACHE_MOUNTED; 6795f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 680b90fa9aeSRam Pai } 681b90fa9aeSRam Pai 68299b7db7bSNick Piggin /* 68399b7db7bSNick Piggin * vfsmount lock must be held for write 68499b7db7bSNick Piggin */ 685419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path) 6861da177e4SLinus Torvalds { 68714cf1fa8SAl Viro mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt); 6881b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 6891a390689SAl Viro hash(path->mnt, path->dentry)); 6906b41d536SAl Viro list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts); 691b90fa9aeSRam Pai } 692b90fa9aeSRam Pai 693b90fa9aeSRam Pai /* 69499b7db7bSNick Piggin * vfsmount lock must be held for write 695b90fa9aeSRam Pai */ 6964b2619a5SAl Viro static void commit_tree(struct mount *mnt) 697b90fa9aeSRam Pai { 6980714a533SAl Viro struct mount *parent = mnt->mnt_parent; 69983adc753SAl Viro struct mount *m; 700b90fa9aeSRam Pai LIST_HEAD(head); 701143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 702b90fa9aeSRam Pai 7030714a533SAl Viro BUG_ON(parent == mnt); 704b90fa9aeSRam Pai 7051a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 706f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 707143c8c91SAl Viro m->mnt_ns = n; 708f03c6599SAl Viro 709b90fa9aeSRam Pai list_splice(&head, n->list.prev); 710b90fa9aeSRam Pai 7111b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 712a73324daSAl Viro hash(&parent->mnt, mnt->mnt_mountpoint)); 7136b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 7146b3286edSKirill Korotaev touch_mnt_namespace(n); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 717909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 7181da177e4SLinus Torvalds { 7196b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 7206b41d536SAl Viro if (next == &p->mnt_mounts) { 7211da177e4SLinus Torvalds while (1) { 722909b0a88SAl Viro if (p == root) 7231da177e4SLinus Torvalds return NULL; 7246b41d536SAl Viro next = p->mnt_child.next; 7256b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 7261da177e4SLinus Torvalds break; 7270714a533SAl Viro p = p->mnt_parent; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds } 7306b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 733315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 7349676f0c6SRam Pai { 7356b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 7366b41d536SAl Viro while (prev != &p->mnt_mounts) { 7376b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 7386b41d536SAl Viro prev = p->mnt_mounts.prev; 7399676f0c6SRam Pai } 7409676f0c6SRam Pai return p; 7419676f0c6SRam Pai } 7429676f0c6SRam Pai 7439d412a43SAl Viro struct vfsmount * 7449d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 7459d412a43SAl Viro { 746b105e270SAl Viro struct mount *mnt; 7479d412a43SAl Viro struct dentry *root; 7489d412a43SAl Viro 7499d412a43SAl Viro if (!type) 7509d412a43SAl Viro return ERR_PTR(-ENODEV); 7519d412a43SAl Viro 7529d412a43SAl Viro mnt = alloc_vfsmnt(name); 7539d412a43SAl Viro if (!mnt) 7549d412a43SAl Viro return ERR_PTR(-ENOMEM); 7559d412a43SAl Viro 7569d412a43SAl Viro if (flags & MS_KERNMOUNT) 757b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 7589d412a43SAl Viro 7599d412a43SAl Viro root = mount_fs(type, flags, name, data); 7609d412a43SAl Viro if (IS_ERR(root)) { 7619d412a43SAl Viro free_vfsmnt(mnt); 7629d412a43SAl Viro return ERR_CAST(root); 7639d412a43SAl Viro } 7649d412a43SAl Viro 765b105e270SAl Viro mnt->mnt.mnt_root = root; 766b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 767a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7680714a533SAl Viro mnt->mnt_parent = mnt; 769962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 77039f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 771962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 772b105e270SAl Viro return &mnt->mnt; 7739d412a43SAl Viro } 7749d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 7759d412a43SAl Viro 77687129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 77736341f64SRam Pai int flag) 7781da177e4SLinus Torvalds { 77987129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 780be34d1a3SDavid Howells struct mount *mnt; 781be34d1a3SDavid Howells int err; 7821da177e4SLinus Torvalds 783be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 784be34d1a3SDavid Howells if (!mnt) 785be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 786be34d1a3SDavid Howells 787719f5d7fSMiklos Szeredi if (flag & (CL_SLAVE | CL_PRIVATE)) 78815169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 789719f5d7fSMiklos Szeredi else 79015169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 791719f5d7fSMiklos Szeredi 79215169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 793be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 794719f5d7fSMiklos Szeredi if (err) 795719f5d7fSMiklos Szeredi goto out_free; 796719f5d7fSMiklos Szeredi } 797719f5d7fSMiklos Szeredi 79887129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 7991da177e4SLinus Torvalds atomic_inc(&sb->s_active); 800b105e270SAl Viro mnt->mnt.mnt_sb = sb; 801b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 802a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8030714a533SAl Viro mnt->mnt_parent = mnt; 804962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 80539f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 806962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 807b90fa9aeSRam Pai 8085afe0022SRam Pai if (flag & CL_SLAVE) { 8096776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 81032301920SAl Viro mnt->mnt_master = old; 811fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 8128aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 813fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 8146776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 815d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 8166776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 817d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 8185afe0022SRam Pai } 819b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 8200f0afb1dSAl Viro set_mnt_shared(mnt); 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 8231da177e4SLinus Torvalds * as the original if that was on one */ 82436341f64SRam Pai if (flag & CL_EXPIRE) { 8256776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 8266776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 8271da177e4SLinus Torvalds } 828be34d1a3SDavid Howells 829cb338d06SAl Viro return mnt; 830719f5d7fSMiklos Szeredi 831719f5d7fSMiklos Szeredi out_free: 832719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 833be34d1a3SDavid Howells return ERR_PTR(err); 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 83683adc753SAl Viro static inline void mntfree(struct mount *mnt) 8371da177e4SLinus Torvalds { 83883adc753SAl Viro struct vfsmount *m = &mnt->mnt; 83983adc753SAl Viro struct super_block *sb = m->mnt_sb; 840b3e19d92SNick Piggin 8413d733633SDave Hansen /* 8423d733633SDave Hansen * This probably indicates that somebody messed 8433d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 8443d733633SDave Hansen * happens, the filesystem was probably unable 8453d733633SDave Hansen * to make r/w->r/o transitions. 8463d733633SDave Hansen */ 847d3ef3d73Snpiggin@suse.de /* 848b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 849b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 850d3ef3d73Snpiggin@suse.de */ 851c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 85283adc753SAl Viro fsnotify_vfsmount_delete(m); 85383adc753SAl Viro dput(m->mnt_root); 85483adc753SAl Viro free_vfsmnt(mnt); 8551da177e4SLinus Torvalds deactivate_super(sb); 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 858900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 8597b7b1aceSAl Viro { 860b3e19d92SNick Piggin put_again: 861f03c6599SAl Viro #ifdef CONFIG_SMP 862962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 863f7a99c5bSAl Viro if (likely(mnt->mnt_ns)) { 864f7a99c5bSAl Viro /* shouldn't be the last one */ 865aa9c0e07SAl Viro mnt_add_count(mnt, -1); 866962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 86799b7db7bSNick Piggin return; 868b3e19d92SNick Piggin } 869962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 870b3e19d92SNick Piggin 871962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 872aa9c0e07SAl Viro mnt_add_count(mnt, -1); 873b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 874962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 87599b7db7bSNick Piggin return; 87699b7db7bSNick Piggin } 877b3e19d92SNick Piggin #else 878aa9c0e07SAl Viro mnt_add_count(mnt, -1); 879b3e19d92SNick Piggin if (likely(mnt_get_count(mnt))) 880b3e19d92SNick Piggin return; 881962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 882f03c6599SAl Viro #endif 883863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 884863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 885863d684fSAl Viro mnt->mnt_pinned = 0; 886962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 887900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 888b3e19d92SNick Piggin goto put_again; 889b3e19d92SNick Piggin } 890962830dfSAndi Kleen 89139f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 892962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 893b3e19d92SNick Piggin mntfree(mnt); 894b3e19d92SNick Piggin } 895b3e19d92SNick Piggin 896b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 897b3e19d92SNick Piggin { 898b3e19d92SNick Piggin if (mnt) { 899863d684fSAl Viro struct mount *m = real_mount(mnt); 900b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 901863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 902863d684fSAl Viro m->mnt_expiry_mark = 0; 903863d684fSAl Viro mntput_no_expire(m); 904b3e19d92SNick Piggin } 905b3e19d92SNick Piggin } 906b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 907b3e19d92SNick Piggin 908b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 909b3e19d92SNick Piggin { 910b3e19d92SNick Piggin if (mnt) 91183adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 912b3e19d92SNick Piggin return mnt; 913b3e19d92SNick Piggin } 914b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 915b3e19d92SNick Piggin 9167b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 9177b7b1aceSAl Viro { 918962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 919863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 920962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 9217b7b1aceSAl Viro } 9227b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 9237b7b1aceSAl Viro 924863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 9257b7b1aceSAl Viro { 926863d684fSAl Viro struct mount *mnt = real_mount(m); 927962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 9287b7b1aceSAl Viro if (mnt->mnt_pinned) { 929863d684fSAl Viro mnt_add_count(mnt, 1); 9307b7b1aceSAl Viro mnt->mnt_pinned--; 9317b7b1aceSAl Viro } 932962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 9337b7b1aceSAl Viro } 9347b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 9351da177e4SLinus Torvalds 936b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 937b3b304a2SMiklos Szeredi { 938b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 939b3b304a2SMiklos Szeredi } 940b3b304a2SMiklos Szeredi 941b3b304a2SMiklos Szeredi /* 942b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 943b3b304a2SMiklos Szeredi * implement more complex mount option showing. 944b3b304a2SMiklos Szeredi * 945b3b304a2SMiklos Szeredi * See also save_mount_options(). 946b3b304a2SMiklos Szeredi */ 94734c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 948b3b304a2SMiklos Szeredi { 9492a32cebdSAl Viro const char *options; 9502a32cebdSAl Viro 9512a32cebdSAl Viro rcu_read_lock(); 95234c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 953b3b304a2SMiklos Szeredi 954b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 955b3b304a2SMiklos Szeredi seq_putc(m, ','); 956b3b304a2SMiklos Szeredi mangle(m, options); 957b3b304a2SMiklos Szeredi } 9582a32cebdSAl Viro rcu_read_unlock(); 959b3b304a2SMiklos Szeredi 960b3b304a2SMiklos Szeredi return 0; 961b3b304a2SMiklos Szeredi } 962b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 963b3b304a2SMiklos Szeredi 964b3b304a2SMiklos Szeredi /* 965b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 966b3b304a2SMiklos Szeredi * called from the fill_super() callback. 967b3b304a2SMiklos Szeredi * 968b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 969b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 970b3b304a2SMiklos Szeredi * remount fails. 971b3b304a2SMiklos Szeredi * 972b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 973b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 974b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 975b3b304a2SMiklos Szeredi * any more. 976b3b304a2SMiklos Szeredi */ 977b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 978b3b304a2SMiklos Szeredi { 9792a32cebdSAl Viro BUG_ON(sb->s_options); 9802a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 981b3b304a2SMiklos Szeredi } 982b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 983b3b304a2SMiklos Szeredi 9842a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 9852a32cebdSAl Viro { 9862a32cebdSAl Viro char *old = sb->s_options; 9872a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 9882a32cebdSAl Viro if (old) { 9892a32cebdSAl Viro synchronize_rcu(); 9902a32cebdSAl Viro kfree(old); 9912a32cebdSAl Viro } 9922a32cebdSAl Viro } 9932a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 9942a32cebdSAl Viro 995a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 9960226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 9971da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 9981da177e4SLinus Torvalds { 9996ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10001da177e4SLinus Torvalds 1001390c6843SRam Pai down_read(&namespace_sem); 1002a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 10061da177e4SLinus Torvalds { 10076ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1008b0765fb8SPavel Emelianov 1009a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 10131da177e4SLinus Torvalds { 1014390c6843SRam Pai up_read(&namespace_sem); 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 10170226f492SAl Viro static int m_show(struct seq_file *m, void *v) 10189f5596afSAl Viro { 10196ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10201a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 10210226f492SAl Viro return p->show(m, &r->mnt); 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 1024a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 10251da177e4SLinus Torvalds .start = m_start, 10261da177e4SLinus Torvalds .next = m_next, 10271da177e4SLinus Torvalds .stop = m_stop, 10280226f492SAl Viro .show = m_show, 1029b4629fe2SChuck Lever }; 1030a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1031b4629fe2SChuck Lever 10321da177e4SLinus Torvalds /** 10331da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 10341da177e4SLinus Torvalds * @mnt: root of mount tree 10351da177e4SLinus Torvalds * 10361da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 10371da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 10381da177e4SLinus Torvalds * busy. 10391da177e4SLinus Torvalds */ 1040909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 10411da177e4SLinus Torvalds { 1042909b0a88SAl Viro struct mount *mnt = real_mount(m); 104336341f64SRam Pai int actual_refs = 0; 104436341f64SRam Pai int minimum_refs = 0; 1045315fc83eSAl Viro struct mount *p; 1046909b0a88SAl Viro BUG_ON(!m); 10471da177e4SLinus Torvalds 1048b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1049962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1050909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 105183adc753SAl Viro actual_refs += mnt_get_count(p); 10521da177e4SLinus Torvalds minimum_refs += 2; 10531da177e4SLinus Torvalds } 1054962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (actual_refs > minimum_refs) 10571da177e4SLinus Torvalds return 0; 1058e3474a8eSIan Kent 1059e3474a8eSIan Kent return 1; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /** 10651da177e4SLinus Torvalds * may_umount - check if a mount point is busy 10661da177e4SLinus Torvalds * @mnt: root of mount 10671da177e4SLinus Torvalds * 10681da177e4SLinus Torvalds * This is called to check if a mount point has any 10691da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 10701da177e4SLinus Torvalds * mount has sub mounts this will return busy 10711da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 10721da177e4SLinus Torvalds * 10731da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 10741da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 10751da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 10761da177e4SLinus Torvalds */ 10771da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 10781da177e4SLinus Torvalds { 1079e3474a8eSIan Kent int ret = 1; 10808ad08d8aSAl Viro down_read(&namespace_sem); 1081962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 10821ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1083e3474a8eSIan Kent ret = 0; 1084962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 10858ad08d8aSAl Viro up_read(&namespace_sem); 1086a05964f3SRam Pai return ret; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 10901da177e4SLinus Torvalds 1091b90fa9aeSRam Pai void release_mounts(struct list_head *head) 10921da177e4SLinus Torvalds { 1093d5e50f74SAl Viro struct mount *mnt; 109470fbcdf4SRam Pai while (!list_empty(head)) { 10951b8e5564SAl Viro mnt = list_first_entry(head, struct mount, mnt_hash); 10961b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 1097676da58dSAl Viro if (mnt_has_parent(mnt)) { 109870fbcdf4SRam Pai struct dentry *dentry; 1099863d684fSAl Viro struct mount *m; 110099b7db7bSNick Piggin 1101962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1102a73324daSAl Viro dentry = mnt->mnt_mountpoint; 1103863d684fSAl Viro m = mnt->mnt_parent; 1104a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 11050714a533SAl Viro mnt->mnt_parent = mnt; 11067c4b93d8SAl Viro m->mnt_ghosts--; 1107962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 110870fbcdf4SRam Pai dput(dentry); 1109863d684fSAl Viro mntput(&m->mnt); 11101da177e4SLinus Torvalds } 1111d5e50f74SAl Viro mntput(&mnt->mnt); 111270fbcdf4SRam Pai } 111370fbcdf4SRam Pai } 111470fbcdf4SRam Pai 111599b7db7bSNick Piggin /* 111699b7db7bSNick Piggin * vfsmount lock must be held for write 111799b7db7bSNick Piggin * namespace_sem must be held for write 111899b7db7bSNick Piggin */ 1119761d5c38SAl Viro void umount_tree(struct mount *mnt, int propagate, struct list_head *kill) 112070fbcdf4SRam Pai { 11217b8a53fdSAl Viro LIST_HEAD(tmp_list); 1122315fc83eSAl Viro struct mount *p; 112370fbcdf4SRam Pai 1124909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 11251b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 112670fbcdf4SRam Pai 1127a05964f3SRam Pai if (propagate) 11287b8a53fdSAl Viro propagate_umount(&tmp_list); 1129a05964f3SRam Pai 11301b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 11316776db3dSAl Viro list_del_init(&p->mnt_expire); 11321a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1133143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 113463d37a84SAl Viro p->mnt_ns = NULL; 11356b41d536SAl Viro list_del_init(&p->mnt_child); 1136676da58dSAl Viro if (mnt_has_parent(p)) { 1137863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 1138a73324daSAl Viro dentry_reset_mounted(p->mnt_mountpoint); 11397c4b93d8SAl Viro } 11400f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 11411da177e4SLinus Torvalds } 11427b8a53fdSAl Viro list_splice(&tmp_list, kill); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 1145692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1146c35038beSAl Viro 11471ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 11481da177e4SLinus Torvalds { 11491ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 11501da177e4SLinus Torvalds int retval; 115170fbcdf4SRam Pai LIST_HEAD(umount_list); 11521da177e4SLinus Torvalds 11531ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 11541da177e4SLinus Torvalds if (retval) 11551da177e4SLinus Torvalds return retval; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* 11581da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 11591da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 11601da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 11611da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 11621da177e4SLinus Torvalds */ 11631da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 11641ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 11651da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 11661da177e4SLinus Torvalds return -EINVAL; 11671da177e4SLinus Torvalds 1168b3e19d92SNick Piggin /* 1169b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1170b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1171b3e19d92SNick Piggin */ 1172962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 117383adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1174962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11751da177e4SLinus Torvalds return -EBUSY; 1176b3e19d92SNick Piggin } 1177962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11781da177e4SLinus Torvalds 1179863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 11801da177e4SLinus Torvalds return -EAGAIN; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds /* 11841da177e4SLinus Torvalds * If we may have to abort operations to get out of this 11851da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 11861da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 11871da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 11881da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 11891da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 11901da177e4SLinus Torvalds * about for the moment. 11911da177e4SLinus Torvalds */ 11921da177e4SLinus Torvalds 119342faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 119442faad99SAl Viro sb->s_op->umount_begin(sb); 119542faad99SAl Viro } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds /* 11981da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 11991da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 12001da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 12011da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 12021da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 12031da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 12041da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 12051da177e4SLinus Torvalds */ 12061ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 12071da177e4SLinus Torvalds /* 12081da177e4SLinus Torvalds * Special case for "unmounting" root ... 12091da177e4SLinus Torvalds * we just try to remount it readonly. 12101da177e4SLinus Torvalds */ 12111da177e4SLinus Torvalds down_write(&sb->s_umount); 12124aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 12131da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 12141da177e4SLinus Torvalds up_write(&sb->s_umount); 12151da177e4SLinus Torvalds return retval; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 1218390c6843SRam Pai down_write(&namespace_sem); 1219962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 12205addc5ddSAl Viro event++; 12211da177e4SLinus Torvalds 1222c35038beSAl Viro if (!(flags & MNT_DETACH)) 12231ab59738SAl Viro shrink_submounts(mnt, &umount_list); 1224c35038beSAl Viro 12251da177e4SLinus Torvalds retval = -EBUSY; 1226a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 12271a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 12281ab59738SAl Viro umount_tree(mnt, 1, &umount_list); 12291da177e4SLinus Torvalds retval = 0; 12301da177e4SLinus Torvalds } 1231962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1232390c6843SRam Pai up_write(&namespace_sem); 123370fbcdf4SRam Pai release_mounts(&umount_list); 12341da177e4SLinus Torvalds return retval; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds /* 12381da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 12391da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 12401da177e4SLinus Torvalds * 12411da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 12421da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 12431da177e4SLinus Torvalds */ 12441da177e4SLinus Torvalds 1245bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 12461da177e4SLinus Torvalds { 12472d8f3038SAl Viro struct path path; 1248900148dcSAl Viro struct mount *mnt; 12491da177e4SLinus Torvalds int retval; 1250db1f05bbSMiklos Szeredi int lookup_flags = 0; 12511da177e4SLinus Torvalds 1252db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1253db1f05bbSMiklos Szeredi return -EINVAL; 1254db1f05bbSMiklos Szeredi 1255db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1256db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1257db1f05bbSMiklos Szeredi 1258db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 12591da177e4SLinus Torvalds if (retval) 12601da177e4SLinus Torvalds goto out; 1261900148dcSAl Viro mnt = real_mount(path.mnt); 12621da177e4SLinus Torvalds retval = -EINVAL; 12632d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 12641da177e4SLinus Torvalds goto dput_and_out; 1265143c8c91SAl Viro if (!check_mnt(mnt)) 12661da177e4SLinus Torvalds goto dput_and_out; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds retval = -EPERM; 12691da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 12701da177e4SLinus Torvalds goto dput_and_out; 12711da177e4SLinus Torvalds 1272900148dcSAl Viro retval = do_umount(mnt, flags); 12731da177e4SLinus Torvalds dput_and_out: 1274429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 12752d8f3038SAl Viro dput(path.dentry); 1276900148dcSAl Viro mntput_no_expire(mnt); 12771da177e4SLinus Torvalds out: 12781da177e4SLinus Torvalds return retval; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds /* 12841da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 12851da177e4SLinus Torvalds */ 1286bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 12871da177e4SLinus Torvalds { 12881da177e4SLinus Torvalds return sys_umount(name, 0); 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds #endif 12921da177e4SLinus Torvalds 12932d92ab3cSAl Viro static int mount_is_safe(struct path *path) 12941da177e4SLinus Torvalds { 12951da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 12961da177e4SLinus Torvalds return 0; 12971da177e4SLinus Torvalds return -EPERM; 12981da177e4SLinus Torvalds #ifdef notyet 12992d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 13001da177e4SLinus Torvalds return -EPERM; 13012d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1302da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 13031da177e4SLinus Torvalds return -EPERM; 13041da177e4SLinus Torvalds } 13052d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 13061da177e4SLinus Torvalds return -EPERM; 13071da177e4SLinus Torvalds return 0; 13081da177e4SLinus Torvalds #endif 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 131187129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 131236341f64SRam Pai int flag) 13131da177e4SLinus Torvalds { 1314a73324daSAl Viro struct mount *res, *p, *q, *r; 13151a390689SAl Viro struct path path; 13161da177e4SLinus Torvalds 1317fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 1318be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 13199676f0c6SRam Pai 132036341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1321be34d1a3SDavid Howells if (IS_ERR(q)) 1322be34d1a3SDavid Howells return q; 1323be34d1a3SDavid Howells 1324a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds p = mnt; 13276b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1328315fc83eSAl Viro struct mount *s; 13297ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 13301da177e4SLinus Torvalds continue; 13311da177e4SLinus Torvalds 1332909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 1333fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 13349676f0c6SRam Pai s = skip_mnt_tree(s); 13359676f0c6SRam Pai continue; 13369676f0c6SRam Pai } 13370714a533SAl Viro while (p != s->mnt_parent) { 13380714a533SAl Viro p = p->mnt_parent; 13390714a533SAl Viro q = q->mnt_parent; 13401da177e4SLinus Torvalds } 134187129cc0SAl Viro p = s; 1342cb338d06SAl Viro path.mnt = &q->mnt; 1343a73324daSAl Viro path.dentry = p->mnt_mountpoint; 134487129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1345be34d1a3SDavid Howells if (IS_ERR(q)) 1346be34d1a3SDavid Howells goto out; 1347962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 13481a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 1349cb338d06SAl Viro attach_mnt(q, &path); 1350962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds return res; 1354be34d1a3SDavid Howells out: 13551da177e4SLinus Torvalds if (res) { 135670fbcdf4SRam Pai LIST_HEAD(umount_list); 1357962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1358761d5c38SAl Viro umount_tree(res, 0, &umount_list); 1359962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 136070fbcdf4SRam Pai release_mounts(&umount_list); 13611da177e4SLinus Torvalds } 1362be34d1a3SDavid Howells return q; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 1365be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1366be34d1a3SDavid Howells 1367589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 13688aec0809SAl Viro { 1369cb338d06SAl Viro struct mount *tree; 13701a60a280SAl Viro down_write(&namespace_sem); 137187129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 137287129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 13731a60a280SAl Viro up_write(&namespace_sem); 1374be34d1a3SDavid Howells if (IS_ERR(tree)) 1375be34d1a3SDavid Howells return NULL; 1376be34d1a3SDavid Howells return &tree->mnt; 13778aec0809SAl Viro } 13788aec0809SAl Viro 13798aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 13808aec0809SAl Viro { 13818aec0809SAl Viro LIST_HEAD(umount_list); 13821a60a280SAl Viro down_write(&namespace_sem); 1383962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1384761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 1385962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 13861a60a280SAl Viro up_write(&namespace_sem); 13878aec0809SAl Viro release_mounts(&umount_list); 13888aec0809SAl Viro } 13898aec0809SAl Viro 13901f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 13911f707137SAl Viro struct vfsmount *root) 13921f707137SAl Viro { 13931a4eeaf2SAl Viro struct mount *mnt; 13941f707137SAl Viro int res = f(root, arg); 13951f707137SAl Viro if (res) 13961f707137SAl Viro return res; 13971a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 13981a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 13991f707137SAl Viro if (res) 14001f707137SAl Viro return res; 14011f707137SAl Viro } 14021f707137SAl Viro return 0; 14031f707137SAl Viro } 14041f707137SAl Viro 14054b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1406719f5d7fSMiklos Szeredi { 1407315fc83eSAl Viro struct mount *p; 1408719f5d7fSMiklos Szeredi 1409909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1410fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 14114b8b21f4SAl Viro mnt_release_group_id(p); 1412719f5d7fSMiklos Szeredi } 1413719f5d7fSMiklos Szeredi } 1414719f5d7fSMiklos Szeredi 14154b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1416719f5d7fSMiklos Szeredi { 1417315fc83eSAl Viro struct mount *p; 1418719f5d7fSMiklos Szeredi 1419909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1420fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 14214b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1422719f5d7fSMiklos Szeredi if (err) { 14234b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1424719f5d7fSMiklos Szeredi return err; 1425719f5d7fSMiklos Szeredi } 1426719f5d7fSMiklos Szeredi } 1427719f5d7fSMiklos Szeredi } 1428719f5d7fSMiklos Szeredi 1429719f5d7fSMiklos Szeredi return 0; 1430719f5d7fSMiklos Szeredi } 1431719f5d7fSMiklos Szeredi 1432b90fa9aeSRam Pai /* 1433b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1434b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 143521444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 143621444403SRam Pai * store the parent mount and mountpoint dentry. 143721444403SRam Pai * (done when source_mnt is moved) 1438b90fa9aeSRam Pai * 1439b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1440b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 14419676f0c6SRam Pai * --------------------------------------------------------------------------- 1442b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 14439676f0c6SRam Pai * |************************************************************************** 14449676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14459676f0c6SRam Pai * | dest | | | | | 14469676f0c6SRam Pai * | | | | | | | 14479676f0c6SRam Pai * | v | | | | | 14489676f0c6SRam Pai * |************************************************************************** 14499676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 14505afe0022SRam Pai * | | | | | | 14519676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 14529676f0c6SRam Pai * *************************************************************************** 1453b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1454b90fa9aeSRam Pai * destination mount. 1455b90fa9aeSRam Pai * 1456b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1457b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1458b90fa9aeSRam Pai * the peer group of the source mount. 1459b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1460b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1461b90fa9aeSRam Pai * mount. 14625afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 14635afe0022SRam Pai * of the destination mount and the cloned mount is made slave 14645afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 14655afe0022SRam Pai * is marked as 'shared and slave'. 14665afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 14675afe0022SRam Pai * source mount. 14685afe0022SRam Pai * 14699676f0c6SRam Pai * --------------------------------------------------------------------------- 147021444403SRam Pai * | MOVE MOUNT OPERATION | 14719676f0c6SRam Pai * |************************************************************************** 14729676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14739676f0c6SRam Pai * | dest | | | | | 14749676f0c6SRam Pai * | | | | | | | 14759676f0c6SRam Pai * | v | | | | | 14769676f0c6SRam Pai * |************************************************************************** 14779676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 14785afe0022SRam Pai * | | | | | | 14799676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 14809676f0c6SRam Pai * *************************************************************************** 14815afe0022SRam Pai * 14825afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 14835afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 148421444403SRam Pai * (+*) the mount is moved to the destination. 14855afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 14865afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 14875afe0022SRam Pai * the mount is marked as 'shared and slave'. 14885afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1489b90fa9aeSRam Pai * 1490b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1491b90fa9aeSRam Pai * applied to each mount in the tree. 1492b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1493b90fa9aeSRam Pai * in allocations. 1494b90fa9aeSRam Pai */ 14950fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 14961a390689SAl Viro struct path *path, struct path *parent_path) 1497b90fa9aeSRam Pai { 1498b90fa9aeSRam Pai LIST_HEAD(tree_list); 1499a8d56d8eSAl Viro struct mount *dest_mnt = real_mount(path->mnt); 15001a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1501315fc83eSAl Viro struct mount *child, *p; 1502719f5d7fSMiklos Szeredi int err; 1503b90fa9aeSRam Pai 1504fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 15050fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1506719f5d7fSMiklos Szeredi if (err) 1507719f5d7fSMiklos Szeredi goto out; 1508719f5d7fSMiklos Szeredi } 1509a8d56d8eSAl Viro err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1510719f5d7fSMiklos Szeredi if (err) 1511719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1512b90fa9aeSRam Pai 1513962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1514df1a1ad2SAl Viro 1515fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1516909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 15170f0afb1dSAl Viro set_mnt_shared(p); 1518b90fa9aeSRam Pai } 15191a390689SAl Viro if (parent_path) { 15200fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 15210fb54e50SAl Viro attach_mnt(source_mnt, path); 1522143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 152321444403SRam Pai } else { 152414cf1fa8SAl Viro mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 15250fb54e50SAl Viro commit_tree(source_mnt); 152621444403SRam Pai } 1527b90fa9aeSRam Pai 15281b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 15291b8e5564SAl Viro list_del_init(&child->mnt_hash); 15304b2619a5SAl Viro commit_tree(child); 1531b90fa9aeSRam Pai } 1532962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 153399b7db7bSNick Piggin 1534b90fa9aeSRam Pai return 0; 1535719f5d7fSMiklos Szeredi 1536719f5d7fSMiklos Szeredi out_cleanup_ids: 1537fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 15380fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1539719f5d7fSMiklos Szeredi out: 1540719f5d7fSMiklos Szeredi return err; 1541b90fa9aeSRam Pai } 1542b90fa9aeSRam Pai 1543b12cea91SAl Viro static int lock_mount(struct path *path) 1544b12cea91SAl Viro { 1545b12cea91SAl Viro struct vfsmount *mnt; 1546b12cea91SAl Viro retry: 1547b12cea91SAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1548b12cea91SAl Viro if (unlikely(cant_mount(path->dentry))) { 1549b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1550b12cea91SAl Viro return -ENOENT; 1551b12cea91SAl Viro } 1552b12cea91SAl Viro down_write(&namespace_sem); 1553b12cea91SAl Viro mnt = lookup_mnt(path); 1554b12cea91SAl Viro if (likely(!mnt)) 1555b12cea91SAl Viro return 0; 1556b12cea91SAl Viro up_write(&namespace_sem); 1557b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1558b12cea91SAl Viro path_put(path); 1559b12cea91SAl Viro path->mnt = mnt; 1560b12cea91SAl Viro path->dentry = dget(mnt->mnt_root); 1561b12cea91SAl Viro goto retry; 1562b12cea91SAl Viro } 1563b12cea91SAl Viro 1564b12cea91SAl Viro static void unlock_mount(struct path *path) 1565b12cea91SAl Viro { 1566b12cea91SAl Viro up_write(&namespace_sem); 1567b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1568b12cea91SAl Viro } 1569b12cea91SAl Viro 157095bc5f25SAl Viro static int graft_tree(struct mount *mnt, struct path *path) 15711da177e4SLinus Torvalds { 157295bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 15731da177e4SLinus Torvalds return -EINVAL; 15741da177e4SLinus Torvalds 15758c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 157695bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 15771da177e4SLinus Torvalds return -ENOTDIR; 15781da177e4SLinus Torvalds 1579b12cea91SAl Viro if (d_unlinked(path->dentry)) 1580b12cea91SAl Viro return -ENOENT; 15811da177e4SLinus Torvalds 158295bc5f25SAl Viro return attach_recursive_mnt(mnt, path, NULL); 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds /* 15867a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 15877a2e8a8fSValerie Aurora */ 15887a2e8a8fSValerie Aurora 15897a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 15907a2e8a8fSValerie Aurora { 15917c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 15927a2e8a8fSValerie Aurora 15937a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 15947a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 15957a2e8a8fSValerie Aurora return 0; 15967a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 15977a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 15987a2e8a8fSValerie Aurora return 0; 15997a2e8a8fSValerie Aurora return type; 16007a2e8a8fSValerie Aurora } 16017a2e8a8fSValerie Aurora 16027a2e8a8fSValerie Aurora /* 160307b20889SRam Pai * recursively change the type of the mountpoint. 160407b20889SRam Pai */ 16050a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 160607b20889SRam Pai { 1607315fc83eSAl Viro struct mount *m; 16084b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 160907b20889SRam Pai int recurse = flag & MS_REC; 16107a2e8a8fSValerie Aurora int type; 1611719f5d7fSMiklos Szeredi int err = 0; 161207b20889SRam Pai 1613ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1614ee6f9582SMiklos Szeredi return -EPERM; 1615ee6f9582SMiklos Szeredi 16162d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 161707b20889SRam Pai return -EINVAL; 161807b20889SRam Pai 16197a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 16207a2e8a8fSValerie Aurora if (!type) 16217a2e8a8fSValerie Aurora return -EINVAL; 16227a2e8a8fSValerie Aurora 162307b20889SRam Pai down_write(&namespace_sem); 1624719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1625719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1626719f5d7fSMiklos Szeredi if (err) 1627719f5d7fSMiklos Szeredi goto out_unlock; 1628719f5d7fSMiklos Szeredi } 1629719f5d7fSMiklos Szeredi 1630962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1631909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 16320f0afb1dSAl Viro change_mnt_propagation(m, type); 1633962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1634719f5d7fSMiklos Szeredi 1635719f5d7fSMiklos Szeredi out_unlock: 163607b20889SRam Pai up_write(&namespace_sem); 1637719f5d7fSMiklos Szeredi return err; 163807b20889SRam Pai } 163907b20889SRam Pai 164007b20889SRam Pai /* 16411da177e4SLinus Torvalds * do loopback mount. 16421da177e4SLinus Torvalds */ 16430a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name, 16442dafe1c4SEric Sandeen int recurse) 16451da177e4SLinus Torvalds { 1646b12cea91SAl Viro LIST_HEAD(umount_list); 16472d92ab3cSAl Viro struct path old_path; 164887129cc0SAl Viro struct mount *mnt = NULL, *old; 16492d92ab3cSAl Viro int err = mount_is_safe(path); 16501da177e4SLinus Torvalds if (err) 16511da177e4SLinus Torvalds return err; 16521da177e4SLinus Torvalds if (!old_name || !*old_name) 16531da177e4SLinus Torvalds return -EINVAL; 1654815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 16551da177e4SLinus Torvalds if (err) 16561da177e4SLinus Torvalds return err; 16571da177e4SLinus Torvalds 1658b12cea91SAl Viro err = lock_mount(path); 1659b12cea91SAl Viro if (err) 16609676f0c6SRam Pai goto out; 16619676f0c6SRam Pai 166287129cc0SAl Viro old = real_mount(old_path.mnt); 166387129cc0SAl Viro 1664b12cea91SAl Viro err = -EINVAL; 1665fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1666b12cea91SAl Viro goto out2; 1667b12cea91SAl Viro 1668143c8c91SAl Viro if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old)) 1669b12cea91SAl Viro goto out2; 1670ccd48bc7SAl Viro 16711da177e4SLinus Torvalds if (recurse) 167287129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 16731da177e4SLinus Torvalds else 167487129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 16751da177e4SLinus Torvalds 1676be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1677be34d1a3SDavid Howells err = PTR_ERR(mnt); 1678be34d1a3SDavid Howells goto out; 1679be34d1a3SDavid Howells } 1680ccd48bc7SAl Viro 168195bc5f25SAl Viro err = graft_tree(mnt, path); 16821da177e4SLinus Torvalds if (err) { 1683962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1684761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 1685962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 16865b83d2c5SRam Pai } 1687b12cea91SAl Viro out2: 1688b12cea91SAl Viro unlock_mount(path); 1689b12cea91SAl Viro release_mounts(&umount_list); 1690ccd48bc7SAl Viro out: 16912d92ab3cSAl Viro path_put(&old_path); 16921da177e4SLinus Torvalds return err; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 16952e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 16962e4b7fcdSDave Hansen { 16972e4b7fcdSDave Hansen int error = 0; 16982e4b7fcdSDave Hansen int readonly_request = 0; 16992e4b7fcdSDave Hansen 17002e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 17012e4b7fcdSDave Hansen readonly_request = 1; 17022e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 17032e4b7fcdSDave Hansen return 0; 17042e4b7fcdSDave Hansen 17052e4b7fcdSDave Hansen if (readonly_request) 170683adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 17072e4b7fcdSDave Hansen else 170883adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 17092e4b7fcdSDave Hansen return error; 17102e4b7fcdSDave Hansen } 17112e4b7fcdSDave Hansen 17121da177e4SLinus Torvalds /* 17131da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 17141da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 17151da177e4SLinus Torvalds * on it - tough luck. 17161da177e4SLinus Torvalds */ 17170a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 17181da177e4SLinus Torvalds void *data) 17191da177e4SLinus Torvalds { 17201da177e4SLinus Torvalds int err; 17212d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1722143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17251da177e4SLinus Torvalds return -EPERM; 17261da177e4SLinus Torvalds 1727143c8c91SAl Viro if (!check_mnt(mnt)) 17281da177e4SLinus Torvalds return -EINVAL; 17291da177e4SLinus Torvalds 17302d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 17311da177e4SLinus Torvalds return -EINVAL; 17321da177e4SLinus Torvalds 1733ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1734ff36fe2cSEric Paris if (err) 1735ff36fe2cSEric Paris return err; 1736ff36fe2cSEric Paris 17371da177e4SLinus Torvalds down_write(&sb->s_umount); 17382e4b7fcdSDave Hansen if (flags & MS_BIND) 17392d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 17404aa98cf7SAl Viro else 17411da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 17427b43a79fSAl Viro if (!err) { 1743962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1744143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1745143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1746962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 17477b43a79fSAl Viro } 17481da177e4SLinus Torvalds up_write(&sb->s_umount); 17490e55a7ccSDan Williams if (!err) { 1750962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1751143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1752962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 17530e55a7ccSDan Williams } 17541da177e4SLinus Torvalds return err; 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds 1757cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 17589676f0c6SRam Pai { 1759315fc83eSAl Viro struct mount *p; 1760909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1761fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 17629676f0c6SRam Pai return 1; 17639676f0c6SRam Pai } 17649676f0c6SRam Pai return 0; 17659676f0c6SRam Pai } 17669676f0c6SRam Pai 17670a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name) 17681da177e4SLinus Torvalds { 17692d92ab3cSAl Viro struct path old_path, parent_path; 1770676da58dSAl Viro struct mount *p; 17710fb54e50SAl Viro struct mount *old; 17721da177e4SLinus Torvalds int err = 0; 17731da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17741da177e4SLinus Torvalds return -EPERM; 17751da177e4SLinus Torvalds if (!old_name || !*old_name) 17761da177e4SLinus Torvalds return -EINVAL; 17772d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 17781da177e4SLinus Torvalds if (err) 17791da177e4SLinus Torvalds return err; 17801da177e4SLinus Torvalds 1781b12cea91SAl Viro err = lock_mount(path); 1782cc53ce53SDavid Howells if (err < 0) 1783cc53ce53SDavid Howells goto out; 1784cc53ce53SDavid Howells 1785143c8c91SAl Viro old = real_mount(old_path.mnt); 1786fc7be130SAl Viro p = real_mount(path->mnt); 1787143c8c91SAl Viro 17881da177e4SLinus Torvalds err = -EINVAL; 1789fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 17901da177e4SLinus Torvalds goto out1; 17911da177e4SLinus Torvalds 1792f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 179321444403SRam Pai goto out1; 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds err = -EINVAL; 17962d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 179721444403SRam Pai goto out1; 17981da177e4SLinus Torvalds 1799676da58dSAl Viro if (!mnt_has_parent(old)) 180021444403SRam Pai goto out1; 18011da177e4SLinus Torvalds 18022d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 18032d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 180421444403SRam Pai goto out1; 180521444403SRam Pai /* 180621444403SRam Pai * Don't move a mount residing in a shared parent. 180721444403SRam Pai */ 1808fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 180921444403SRam Pai goto out1; 18109676f0c6SRam Pai /* 18119676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 18129676f0c6SRam Pai * mount which is shared. 18139676f0c6SRam Pai */ 1814fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 18159676f0c6SRam Pai goto out1; 18161da177e4SLinus Torvalds err = -ELOOP; 1817fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1818676da58dSAl Viro if (p == old) 181921444403SRam Pai goto out1; 18201da177e4SLinus Torvalds 18210fb54e50SAl Viro err = attach_recursive_mnt(old, path, &parent_path); 18224ac91378SJan Blunck if (err) 182321444403SRam Pai goto out1; 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 18261da177e4SLinus Torvalds * automatically */ 18276776db3dSAl Viro list_del_init(&old->mnt_expire); 18281da177e4SLinus Torvalds out1: 1829b12cea91SAl Viro unlock_mount(path); 18301da177e4SLinus Torvalds out: 18311da177e4SLinus Torvalds if (!err) 18321a390689SAl Viro path_put(&parent_path); 18332d92ab3cSAl Viro path_put(&old_path); 18341da177e4SLinus Torvalds return err; 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 18379d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 18389d412a43SAl Viro { 18399d412a43SAl Viro int err; 18409d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 18419d412a43SAl Viro if (subtype) { 18429d412a43SAl Viro subtype++; 18439d412a43SAl Viro err = -EINVAL; 18449d412a43SAl Viro if (!subtype[0]) 18459d412a43SAl Viro goto err; 18469d412a43SAl Viro } else 18479d412a43SAl Viro subtype = ""; 18489d412a43SAl Viro 18499d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 18509d412a43SAl Viro err = -ENOMEM; 18519d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 18529d412a43SAl Viro goto err; 18539d412a43SAl Viro return mnt; 18549d412a43SAl Viro 18559d412a43SAl Viro err: 18569d412a43SAl Viro mntput(mnt); 18579d412a43SAl Viro return ERR_PTR(err); 18589d412a43SAl Viro } 18599d412a43SAl Viro 186079e801a9SAl Viro static struct vfsmount * 18619d412a43SAl Viro do_kern_mount(const char *fstype, int flags, const char *name, void *data) 18629d412a43SAl Viro { 18639d412a43SAl Viro struct file_system_type *type = get_fs_type(fstype); 18649d412a43SAl Viro struct vfsmount *mnt; 18659d412a43SAl Viro if (!type) 18669d412a43SAl Viro return ERR_PTR(-ENODEV); 18679d412a43SAl Viro mnt = vfs_kern_mount(type, flags, name, data); 18689d412a43SAl Viro if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 18699d412a43SAl Viro !mnt->mnt_sb->s_subtype) 18709d412a43SAl Viro mnt = fs_set_subtype(mnt, fstype); 18719d412a43SAl Viro put_filesystem(type); 18729d412a43SAl Viro return mnt; 18739d412a43SAl Viro } 18749d412a43SAl Viro 18759d412a43SAl Viro /* 18769d412a43SAl Viro * add a mount into a namespace's mount tree 18779d412a43SAl Viro */ 187895bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 18799d412a43SAl Viro { 18809d412a43SAl Viro int err; 18819d412a43SAl Viro 18829d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 18839d412a43SAl Viro 1884b12cea91SAl Viro err = lock_mount(path); 1885b12cea91SAl Viro if (err) 1886b12cea91SAl Viro return err; 18879d412a43SAl Viro 18889d412a43SAl Viro err = -EINVAL; 1889143c8c91SAl Viro if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt))) 18909d412a43SAl Viro goto unlock; 18919d412a43SAl Viro 18929d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 18939d412a43SAl Viro err = -EBUSY; 189495bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 18959d412a43SAl Viro path->mnt->mnt_root == path->dentry) 18969d412a43SAl Viro goto unlock; 18979d412a43SAl Viro 18989d412a43SAl Viro err = -EINVAL; 189995bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 19009d412a43SAl Viro goto unlock; 19019d412a43SAl Viro 190295bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 19039d412a43SAl Viro err = graft_tree(newmnt, path); 19049d412a43SAl Viro 19059d412a43SAl Viro unlock: 1906b12cea91SAl Viro unlock_mount(path); 19079d412a43SAl Viro return err; 19089d412a43SAl Viro } 1909b1e75df4SAl Viro 19101da177e4SLinus Torvalds /* 19111da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 19121da177e4SLinus Torvalds * namespace's tree 19131da177e4SLinus Torvalds */ 19140a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags, 19151da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 19161da177e4SLinus Torvalds { 19171da177e4SLinus Torvalds struct vfsmount *mnt; 191815f9a3f3SAl Viro int err; 19191da177e4SLinus Torvalds 1920eca6f534SVegard Nossum if (!type) 19211da177e4SLinus Torvalds return -EINVAL; 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds /* we need capabilities... */ 19241da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 19251da177e4SLinus Torvalds return -EPERM; 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 19281da177e4SLinus Torvalds if (IS_ERR(mnt)) 19291da177e4SLinus Torvalds return PTR_ERR(mnt); 19301da177e4SLinus Torvalds 193195bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 193215f9a3f3SAl Viro if (err) 193315f9a3f3SAl Viro mntput(mnt); 193415f9a3f3SAl Viro return err; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 193719a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 193819a167afSAl Viro { 19396776db3dSAl Viro struct mount *mnt = real_mount(m); 194019a167afSAl Viro int err; 194119a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 194219a167afSAl Viro * expired before we get a chance to add it 194319a167afSAl Viro */ 19446776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 194519a167afSAl Viro 194619a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 194719a167afSAl Viro m->mnt_root == path->dentry) { 1948b1e75df4SAl Viro err = -ELOOP; 1949b1e75df4SAl Viro goto fail; 195019a167afSAl Viro } 195119a167afSAl Viro 195295bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 1953b1e75df4SAl Viro if (!err) 1954b1e75df4SAl Viro return 0; 1955b1e75df4SAl Viro fail: 1956b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 19576776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 1958b1e75df4SAl Viro down_write(&namespace_sem); 1959962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 19606776db3dSAl Viro list_del_init(&mnt->mnt_expire); 1961962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1962b1e75df4SAl Viro up_write(&namespace_sem); 196319a167afSAl Viro } 1964b1e75df4SAl Viro mntput(m); 1965b1e75df4SAl Viro mntput(m); 196619a167afSAl Viro return err; 196719a167afSAl Viro } 196819a167afSAl Viro 1969ea5b778aSDavid Howells /** 1970ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 1971ea5b778aSDavid Howells * @mnt: The mount to list. 1972ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 1973ea5b778aSDavid Howells */ 1974ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 1975ea5b778aSDavid Howells { 1976ea5b778aSDavid Howells down_write(&namespace_sem); 1977962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1978ea5b778aSDavid Howells 19796776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 1980ea5b778aSDavid Howells 1981962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1982ea5b778aSDavid Howells up_write(&namespace_sem); 1983ea5b778aSDavid Howells } 1984ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 1985ea5b778aSDavid Howells 1986ea5b778aSDavid Howells /* 19871da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 19881da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 19891da177e4SLinus Torvalds * here 19901da177e4SLinus Torvalds */ 19911da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 19921da177e4SLinus Torvalds { 1993761d5c38SAl Viro struct mount *mnt, *next; 19941da177e4SLinus Torvalds LIST_HEAD(graveyard); 1995bcc5c7d2SAl Viro LIST_HEAD(umounts); 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds if (list_empty(mounts)) 19981da177e4SLinus Torvalds return; 19991da177e4SLinus Torvalds 2000bcc5c7d2SAl Viro down_write(&namespace_sem); 2001962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 20041da177e4SLinus Torvalds * following criteria: 20051da177e4SLinus Torvalds * - only referenced by its parent vfsmount 20061da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 20071da177e4SLinus Torvalds * cleared by mntput()) 20081da177e4SLinus Torvalds */ 20096776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2010863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 20111ab59738SAl Viro propagate_mount_busy(mnt, 1)) 20121da177e4SLinus Torvalds continue; 20136776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 20141da177e4SLinus Torvalds } 2015bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 20166776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2017143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2018bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 2019bcc5c7d2SAl Viro } 2020962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2021bcc5c7d2SAl Viro up_write(&namespace_sem); 2022bcc5c7d2SAl Viro 2023bcc5c7d2SAl Viro release_mounts(&umounts); 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 20271da177e4SLinus Torvalds 20281da177e4SLinus Torvalds /* 20295528f911STrond Myklebust * Ripoff of 'select_parent()' 20305528f911STrond Myklebust * 20315528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 20325528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 20335528f911STrond Myklebust */ 2034692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 20355528f911STrond Myklebust { 2036692afc31SAl Viro struct mount *this_parent = parent; 20375528f911STrond Myklebust struct list_head *next; 20385528f911STrond Myklebust int found = 0; 20395528f911STrond Myklebust 20405528f911STrond Myklebust repeat: 20416b41d536SAl Viro next = this_parent->mnt_mounts.next; 20425528f911STrond Myklebust resume: 20436b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 20445528f911STrond Myklebust struct list_head *tmp = next; 20456b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 20465528f911STrond Myklebust 20475528f911STrond Myklebust next = tmp->next; 2048692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 20495528f911STrond Myklebust continue; 20505528f911STrond Myklebust /* 20515528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 20525528f911STrond Myklebust */ 20536b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 20545528f911STrond Myklebust this_parent = mnt; 20555528f911STrond Myklebust goto repeat; 20565528f911STrond Myklebust } 20575528f911STrond Myklebust 20581ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 20596776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 20605528f911STrond Myklebust found++; 20615528f911STrond Myklebust } 20625528f911STrond Myklebust } 20635528f911STrond Myklebust /* 20645528f911STrond Myklebust * All done at this level ... ascend and resume the search 20655528f911STrond Myklebust */ 20665528f911STrond Myklebust if (this_parent != parent) { 20676b41d536SAl Viro next = this_parent->mnt_child.next; 20680714a533SAl Viro this_parent = this_parent->mnt_parent; 20695528f911STrond Myklebust goto resume; 20705528f911STrond Myklebust } 20715528f911STrond Myklebust return found; 20725528f911STrond Myklebust } 20735528f911STrond Myklebust 20745528f911STrond Myklebust /* 20755528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 20765528f911STrond Myklebust * submounts of a specific parent mountpoint 207799b7db7bSNick Piggin * 207899b7db7bSNick Piggin * vfsmount_lock must be held for write 20795528f911STrond Myklebust */ 2080692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 20815528f911STrond Myklebust { 20825528f911STrond Myklebust LIST_HEAD(graveyard); 2083761d5c38SAl Viro struct mount *m; 20845528f911STrond Myklebust 20855528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2086c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2087bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2088761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 20896776db3dSAl Viro mnt_expire); 2090143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2091afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 2092bcc5c7d2SAl Viro } 2093bcc5c7d2SAl Viro } 20945528f911STrond Myklebust } 20955528f911STrond Myklebust 20965528f911STrond Myklebust /* 20971da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 20981da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 20991da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 21001da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 21011da177e4SLinus Torvalds */ 2102b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2103b58fed8bSRam Pai unsigned long n) 21041da177e4SLinus Torvalds { 21051da177e4SLinus Torvalds char *t = to; 21061da177e4SLinus Torvalds const char __user *f = from; 21071da177e4SLinus Torvalds char c; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 21101da177e4SLinus Torvalds return n; 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds while (n) { 21131da177e4SLinus Torvalds if (__get_user(c, f)) { 21141da177e4SLinus Torvalds memset(t, 0, n); 21151da177e4SLinus Torvalds break; 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds *t++ = c; 21181da177e4SLinus Torvalds f++; 21191da177e4SLinus Torvalds n--; 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds return n; 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 21251da177e4SLinus Torvalds { 21261da177e4SLinus Torvalds int i; 21271da177e4SLinus Torvalds unsigned long page; 21281da177e4SLinus Torvalds unsigned long size; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds *where = 0; 21311da177e4SLinus Torvalds if (!data) 21321da177e4SLinus Torvalds return 0; 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 21351da177e4SLinus Torvalds return -ENOMEM; 21361da177e4SLinus Torvalds 21371da177e4SLinus Torvalds /* We only care that *some* data at the address the user 21381da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 21391da177e4SLinus Torvalds * the remainder of the page. 21401da177e4SLinus Torvalds */ 21411da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 21421da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 21431da177e4SLinus Torvalds if (size > PAGE_SIZE) 21441da177e4SLinus Torvalds size = PAGE_SIZE; 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 21471da177e4SLinus Torvalds if (!i) { 21481da177e4SLinus Torvalds free_page(page); 21491da177e4SLinus Torvalds return -EFAULT; 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds if (i != PAGE_SIZE) 21521da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 21531da177e4SLinus Torvalds *where = page; 21541da177e4SLinus Torvalds return 0; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds 2157eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2158eca6f534SVegard Nossum { 2159eca6f534SVegard Nossum char *tmp; 2160eca6f534SVegard Nossum 2161eca6f534SVegard Nossum if (!data) { 2162eca6f534SVegard Nossum *where = NULL; 2163eca6f534SVegard Nossum return 0; 2164eca6f534SVegard Nossum } 2165eca6f534SVegard Nossum 2166eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2167eca6f534SVegard Nossum if (IS_ERR(tmp)) 2168eca6f534SVegard Nossum return PTR_ERR(tmp); 2169eca6f534SVegard Nossum 2170eca6f534SVegard Nossum *where = tmp; 2171eca6f534SVegard Nossum return 0; 2172eca6f534SVegard Nossum } 2173eca6f534SVegard Nossum 21741da177e4SLinus Torvalds /* 21751da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 21761da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 21771da177e4SLinus Torvalds * 21781da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 21791da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 21801da177e4SLinus Torvalds * information (or be NULL). 21811da177e4SLinus Torvalds * 21821da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 21831da177e4SLinus Torvalds * When the flags word was introduced its top half was required 21841da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 21851da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 21861da177e4SLinus Torvalds * and must be discarded. 21871da177e4SLinus Torvalds */ 21881da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 21891da177e4SLinus Torvalds unsigned long flags, void *data_page) 21901da177e4SLinus Torvalds { 21912d92ab3cSAl Viro struct path path; 21921da177e4SLinus Torvalds int retval = 0; 21931da177e4SLinus Torvalds int mnt_flags = 0; 21941da177e4SLinus Torvalds 21951da177e4SLinus Torvalds /* Discard magic */ 21961da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 21971da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds /* Basic sanity checks */ 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 22021da177e4SLinus Torvalds return -EINVAL; 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds if (data_page) 22051da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 22061da177e4SLinus Torvalds 2207a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2208a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2209a27ab9f2STetsuo Handa if (retval) 2210a27ab9f2STetsuo Handa return retval; 2211a27ab9f2STetsuo Handa 2212a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2213a27ab9f2STetsuo Handa type_page, flags, data_page); 2214a27ab9f2STetsuo Handa if (retval) 2215a27ab9f2STetsuo Handa goto dput_out; 2216a27ab9f2STetsuo Handa 2217613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2218613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 22190a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 22200a1c01c9SMatthew Garrett 22211da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 22221da177e4SLinus Torvalds if (flags & MS_NOSUID) 22231da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 22241da177e4SLinus Torvalds if (flags & MS_NODEV) 22251da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 22261da177e4SLinus Torvalds if (flags & MS_NOEXEC) 22271da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2228fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2229fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2230fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2231fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2232d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2233d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 22342e4b7fcdSDave Hansen if (flags & MS_RDONLY) 22352e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2236fc33a7bbSChristoph Hellwig 22377a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2238d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2239d0adde57SMatthew Garrett MS_STRICTATIME); 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds if (flags & MS_REMOUNT) 22422d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 22431da177e4SLinus Torvalds data_page); 22441da177e4SLinus Torvalds else if (flags & MS_BIND) 22452d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 22469676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 22472d92ab3cSAl Viro retval = do_change_type(&path, flags); 22481da177e4SLinus Torvalds else if (flags & MS_MOVE) 22492d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 22501da177e4SLinus Torvalds else 22512d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 22521da177e4SLinus Torvalds dev_name, data_page); 22531da177e4SLinus Torvalds dput_out: 22542d92ab3cSAl Viro path_put(&path); 22551da177e4SLinus Torvalds return retval; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 2258cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void) 2259cf8d2c11STrond Myklebust { 2260cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2261cf8d2c11STrond Myklebust 2262cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2263cf8d2c11STrond Myklebust if (!new_ns) 2264cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2265cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2266cf8d2c11STrond Myklebust new_ns->root = NULL; 2267cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2268cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2269cf8d2c11STrond Myklebust new_ns->event = 0; 2270cf8d2c11STrond Myklebust return new_ns; 2271cf8d2c11STrond Myklebust } 2272cf8d2c11STrond Myklebust 2273741a2951SJANAK DESAI /* 2274741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2275741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2276741a2951SJANAK DESAI */ 2277e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 22786b3286edSKirill Korotaev struct fs_struct *fs) 22791da177e4SLinus Torvalds { 22806b3286edSKirill Korotaev struct mnt_namespace *new_ns; 22817f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2282315fc83eSAl Viro struct mount *p, *q; 2283be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2284cb338d06SAl Viro struct mount *new; 22851da177e4SLinus Torvalds 2286cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2287cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2288cf8d2c11STrond Myklebust return new_ns; 22891da177e4SLinus Torvalds 2290390c6843SRam Pai down_write(&namespace_sem); 22911da177e4SLinus Torvalds /* First pass: copy the tree topology */ 2292909b0a88SAl Viro new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE); 2293be34d1a3SDavid Howells if (IS_ERR(new)) { 2294390c6843SRam Pai up_write(&namespace_sem); 22951da177e4SLinus Torvalds kfree(new_ns); 2296be34d1a3SDavid Howells return ERR_CAST(new); 22971da177e4SLinus Torvalds } 2298be08d6d2SAl Viro new_ns->root = new; 2299962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 23001a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 2301962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds /* 23041da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 23051da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 23061da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 23071da177e4SLinus Torvalds */ 2308909b0a88SAl Viro p = old; 2309cb338d06SAl Viro q = new; 23101da177e4SLinus Torvalds while (p) { 2311143c8c91SAl Viro q->mnt_ns = new_ns; 23121da177e4SLinus Torvalds if (fs) { 2313315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2314315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 2315315fc83eSAl Viro rootmnt = &p->mnt; 23161da177e4SLinus Torvalds } 2317315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2318315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 2319315fc83eSAl Viro pwdmnt = &p->mnt; 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds } 2322909b0a88SAl Viro p = next_mnt(p, old); 2323909b0a88SAl Viro q = next_mnt(q, new); 23241da177e4SLinus Torvalds } 2325390c6843SRam Pai up_write(&namespace_sem); 23261da177e4SLinus Torvalds 23271da177e4SLinus Torvalds if (rootmnt) 2328f03c6599SAl Viro mntput(rootmnt); 23291da177e4SLinus Torvalds if (pwdmnt) 2330f03c6599SAl Viro mntput(pwdmnt); 23311da177e4SLinus Torvalds 2332741a2951SJANAK DESAI return new_ns; 2333741a2951SJANAK DESAI } 2334741a2951SJANAK DESAI 2335213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2336e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 2337741a2951SJANAK DESAI { 23386b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2339741a2951SJANAK DESAI 2340e3222c4eSBadari Pulavarty BUG_ON(!ns); 23416b3286edSKirill Korotaev get_mnt_ns(ns); 2342741a2951SJANAK DESAI 2343741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2344e3222c4eSBadari Pulavarty return ns; 2345741a2951SJANAK DESAI 2346e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 2347741a2951SJANAK DESAI 23486b3286edSKirill Korotaev put_mnt_ns(ns); 2349e3222c4eSBadari Pulavarty return new_ns; 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds 2352cf8d2c11STrond Myklebust /** 2353cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2354cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2355cf8d2c11STrond Myklebust */ 23561a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2357cf8d2c11STrond Myklebust { 23581a4eeaf2SAl Viro struct mnt_namespace *new_ns = alloc_mnt_ns(); 2359cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 23601a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 23611a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2362be08d6d2SAl Viro new_ns->root = mnt; 23631a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2364c1334495SAl Viro } else { 23651a4eeaf2SAl Viro mntput(m); 2366cf8d2c11STrond Myklebust } 2367cf8d2c11STrond Myklebust return new_ns; 2368cf8d2c11STrond Myklebust } 2369cf8d2c11STrond Myklebust 2370ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2371ea441d11SAl Viro { 2372ea441d11SAl Viro struct mnt_namespace *ns; 2373d31da0f0SAl Viro struct super_block *s; 2374ea441d11SAl Viro struct path path; 2375ea441d11SAl Viro int err; 2376ea441d11SAl Viro 2377ea441d11SAl Viro ns = create_mnt_ns(mnt); 2378ea441d11SAl Viro if (IS_ERR(ns)) 2379ea441d11SAl Viro return ERR_CAST(ns); 2380ea441d11SAl Viro 2381ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2382ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2383ea441d11SAl Viro 2384ea441d11SAl Viro put_mnt_ns(ns); 2385ea441d11SAl Viro 2386ea441d11SAl Viro if (err) 2387ea441d11SAl Viro return ERR_PTR(err); 2388ea441d11SAl Viro 2389ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2390d31da0f0SAl Viro s = path.mnt->mnt_sb; 2391d31da0f0SAl Viro atomic_inc(&s->s_active); 2392ea441d11SAl Viro mntput(path.mnt); 2393ea441d11SAl Viro /* lock the sucker */ 2394d31da0f0SAl Viro down_write(&s->s_umount); 2395ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2396ea441d11SAl Viro return path.dentry; 2397ea441d11SAl Viro } 2398ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2399ea441d11SAl Viro 2400bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2401bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 24021da177e4SLinus Torvalds { 2403eca6f534SVegard Nossum int ret; 2404eca6f534SVegard Nossum char *kernel_type; 2405eca6f534SVegard Nossum char *kernel_dir; 2406eca6f534SVegard Nossum char *kernel_dev; 24071da177e4SLinus Torvalds unsigned long data_page; 24081da177e4SLinus Torvalds 2409eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2410eca6f534SVegard Nossum if (ret < 0) 2411eca6f534SVegard Nossum goto out_type; 24121da177e4SLinus Torvalds 2413eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2414eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2415eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2416eca6f534SVegard Nossum goto out_dir; 2417eca6f534SVegard Nossum } 24181da177e4SLinus Torvalds 2419eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2420eca6f534SVegard Nossum if (ret < 0) 2421eca6f534SVegard Nossum goto out_dev; 24221da177e4SLinus Torvalds 2423eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2424eca6f534SVegard Nossum if (ret < 0) 2425eca6f534SVegard Nossum goto out_data; 24261da177e4SLinus Torvalds 2427eca6f534SVegard Nossum ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, 2428eca6f534SVegard Nossum (void *) data_page); 2429eca6f534SVegard Nossum 24301da177e4SLinus Torvalds free_page(data_page); 2431eca6f534SVegard Nossum out_data: 2432eca6f534SVegard Nossum kfree(kernel_dev); 2433eca6f534SVegard Nossum out_dev: 2434eca6f534SVegard Nossum putname(kernel_dir); 2435eca6f534SVegard Nossum out_dir: 2436eca6f534SVegard Nossum kfree(kernel_type); 2437eca6f534SVegard Nossum out_type: 2438eca6f534SVegard Nossum return ret; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds /* 2442afac7cbaSAl Viro * Return true if path is reachable from root 2443afac7cbaSAl Viro * 2444afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2445afac7cbaSAl Viro */ 2446643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2447afac7cbaSAl Viro const struct path *root) 2448afac7cbaSAl Viro { 2449643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2450a73324daSAl Viro dentry = mnt->mnt_mountpoint; 24510714a533SAl Viro mnt = mnt->mnt_parent; 2452afac7cbaSAl Viro } 2453643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2454afac7cbaSAl Viro } 2455afac7cbaSAl Viro 2456afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2457afac7cbaSAl Viro { 2458afac7cbaSAl Viro int res; 2459962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 2460643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2461962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 2462afac7cbaSAl Viro return res; 2463afac7cbaSAl Viro } 2464afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2465afac7cbaSAl Viro 2466afac7cbaSAl Viro /* 24671da177e4SLinus Torvalds * pivot_root Semantics: 24681da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 24691da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 24701da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 24711da177e4SLinus Torvalds * 24721da177e4SLinus Torvalds * Restrictions: 24731da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 24741da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 24751da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 24761da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 24771da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 24781da177e4SLinus Torvalds * 24794a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 24804a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 24814a0d11faSNeil Brown * in this situation. 24824a0d11faSNeil Brown * 24831da177e4SLinus Torvalds * Notes: 24841da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 24851da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 24861da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 24871da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 24881da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 24891da177e4SLinus Torvalds * first. 24901da177e4SLinus Torvalds */ 24913480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 24923480b257SHeiko Carstens const char __user *, put_old) 24931da177e4SLinus Torvalds { 24942d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 2495419148daSAl Viro struct mount *new_mnt, *root_mnt; 24961da177e4SLinus Torvalds int error; 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 24991da177e4SLinus Torvalds return -EPERM; 25001da177e4SLinus Torvalds 25012d8f3038SAl Viro error = user_path_dir(new_root, &new); 25021da177e4SLinus Torvalds if (error) 25031da177e4SLinus Torvalds goto out0; 25041da177e4SLinus Torvalds 25052d8f3038SAl Viro error = user_path_dir(put_old, &old); 25061da177e4SLinus Torvalds if (error) 25071da177e4SLinus Torvalds goto out1; 25081da177e4SLinus Torvalds 25092d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2510b12cea91SAl Viro if (error) 2511b12cea91SAl Viro goto out2; 25121da177e4SLinus Torvalds 2513f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2514b12cea91SAl Viro error = lock_mount(&old); 2515b12cea91SAl Viro if (error) 2516b12cea91SAl Viro goto out3; 2517b12cea91SAl Viro 25181da177e4SLinus Torvalds error = -EINVAL; 2519419148daSAl Viro new_mnt = real_mount(new.mnt); 2520419148daSAl Viro root_mnt = real_mount(root.mnt); 2521fc7be130SAl Viro if (IS_MNT_SHARED(real_mount(old.mnt)) || 2522fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2523fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2524b12cea91SAl Viro goto out4; 2525143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2526b12cea91SAl Viro goto out4; 25271da177e4SLinus Torvalds error = -ENOENT; 2528f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2529b12cea91SAl Viro goto out4; 2530f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 2531b12cea91SAl Viro goto out4; 25321da177e4SLinus Torvalds error = -EBUSY; 25332d8f3038SAl Viro if (new.mnt == root.mnt || 25342d8f3038SAl Viro old.mnt == root.mnt) 2535b12cea91SAl Viro goto out4; /* loop, on the same file system */ 25361da177e4SLinus Torvalds error = -EINVAL; 25378c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2538b12cea91SAl Viro goto out4; /* not a mountpoint */ 2539676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2540b12cea91SAl Viro goto out4; /* not attached */ 25412d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2542b12cea91SAl Viro goto out4; /* not a mountpoint */ 2543676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2544b12cea91SAl Viro goto out4; /* not attached */ 25454ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 2546643822b4SAl Viro if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new)) 2547b12cea91SAl Viro goto out4; 2548962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2549419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2550419148daSAl Viro detach_mnt(root_mnt, &root_parent); 25514ac91378SJan Blunck /* mount old root on put_old */ 2552419148daSAl Viro attach_mnt(root_mnt, &old); 25534ac91378SJan Blunck /* mount new_root on / */ 2554419148daSAl Viro attach_mnt(new_mnt, &root_parent); 25556b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2556962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 25572d8f3038SAl Viro chroot_fs_refs(&root, &new); 25581da177e4SLinus Torvalds error = 0; 2559b12cea91SAl Viro out4: 2560b12cea91SAl Viro unlock_mount(&old); 2561b12cea91SAl Viro if (!error) { 25621a390689SAl Viro path_put(&root_parent); 25631a390689SAl Viro path_put(&parent_path); 2564b12cea91SAl Viro } 2565b12cea91SAl Viro out3: 25668c3ee42eSAl Viro path_put(&root); 2567b12cea91SAl Viro out2: 25682d8f3038SAl Viro path_put(&old); 25691da177e4SLinus Torvalds out1: 25702d8f3038SAl Viro path_put(&new); 25711da177e4SLinus Torvalds out0: 25721da177e4SLinus Torvalds return error; 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds static void __init init_mount_tree(void) 25761da177e4SLinus Torvalds { 25771da177e4SLinus Torvalds struct vfsmount *mnt; 25786b3286edSKirill Korotaev struct mnt_namespace *ns; 2579ac748a09SJan Blunck struct path root; 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 25821da177e4SLinus Torvalds if (IS_ERR(mnt)) 25831da177e4SLinus Torvalds panic("Can't create rootfs"); 2584b3e19d92SNick Piggin 25853b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 25863b22edc5STrond Myklebust if (IS_ERR(ns)) 25871da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 25881da177e4SLinus Torvalds 25896b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 25906b3286edSKirill Korotaev get_mnt_ns(ns); 25911da177e4SLinus Torvalds 2592be08d6d2SAl Viro root.mnt = mnt; 2593be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2594ac748a09SJan Blunck 2595ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2596ac748a09SJan Blunck set_fs_root(current->fs, &root); 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 259974bf17cfSDenis Cheng void __init mnt_init(void) 26001da177e4SLinus Torvalds { 260113f14b4dSEric Dumazet unsigned u; 260215a67dd8SRandy Dunlap int err; 26031da177e4SLinus Torvalds 2604390c6843SRam Pai init_rwsem(&namespace_sem); 2605390c6843SRam Pai 26067d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 260720c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 26081da177e4SLinus Torvalds 2609b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds if (!mount_hashtable) 26121da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 26131da177e4SLinus Torvalds 261480cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 26151da177e4SLinus Torvalds 261613f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 261713f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 26181da177e4SLinus Torvalds 2619962830dfSAndi Kleen br_lock_init(&vfsmount_lock); 262099b7db7bSNick Piggin 262115a67dd8SRandy Dunlap err = sysfs_init(); 262215a67dd8SRandy Dunlap if (err) 262315a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 26248e24eea7SHarvey Harrison __func__, err); 262500d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 262600d26666SGreg Kroah-Hartman if (!fs_kobj) 26278e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 26281da177e4SLinus Torvalds init_rootfs(); 26291da177e4SLinus Torvalds init_mount_tree(); 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 2632616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 26331da177e4SLinus Torvalds { 263470fbcdf4SRam Pai LIST_HEAD(umount_list); 2635616511d0STrond Myklebust 2636d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2637616511d0STrond Myklebust return; 2638390c6843SRam Pai down_write(&namespace_sem); 2639962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2640be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 2641962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2642390c6843SRam Pai up_write(&namespace_sem); 264370fbcdf4SRam Pai release_mounts(&umount_list); 26446b3286edSKirill Korotaev kfree(ns); 26451da177e4SLinus Torvalds } 26469d412a43SAl Viro 26479d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 26489d412a43SAl Viro { 2649423e0ab0STim Chen struct vfsmount *mnt; 2650423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2651423e0ab0STim Chen if (!IS_ERR(mnt)) { 2652423e0ab0STim Chen /* 2653423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2654423e0ab0STim Chen * we unmount before file sys is unregistered 2655423e0ab0STim Chen */ 2656f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2657423e0ab0STim Chen } 2658423e0ab0STim Chen return mnt; 26599d412a43SAl Viro } 26609d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2661423e0ab0STim Chen 2662423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2663423e0ab0STim Chen { 2664423e0ab0STim Chen /* release long term mount so mount point can be released */ 2665423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2666f7a99c5bSAl Viro br_write_lock(&vfsmount_lock); 2667f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 2668f7a99c5bSAl Viro br_write_unlock(&vfsmount_lock); 2669423e0ab0STim Chen mntput(mnt); 2670423e0ab0STim Chen } 2671423e0ab0STim Chen } 2672423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 267302125a82SAl Viro 267402125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 267502125a82SAl Viro { 2676143c8c91SAl Viro return check_mnt(real_mount(mnt)); 267702125a82SAl Viro } 2678