Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/namespace.c |
| 3 | * |
| 4 | * (C) Copyright Al Viro 2000, 2001 |
| 5 | * Released under GPL v2. |
| 6 | * |
| 7 | * Based on code from fs/super.c, copyright Linus Torvalds and others. |
| 8 | * Heavily rewritten. |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/syscalls.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/sched.h> |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/percpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/init.h> |
Randy Dunlap | 15a67dd | 2006-09-29 01:58:57 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/acct.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 19 | #include <linux/capability.h> |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 20 | #include <linux/cpumask.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
Andrew Morton | f20a9ea | 2006-08-14 22:43:23 -0700 | [diff] [blame] | 22 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/seq_file.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 24 | #include <linux/mnt_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/namei.h> |
Alexey Dobriyan | b43f3cb | 2009-07-08 01:54:37 +0400 | [diff] [blame] | 26 | #include <linux/nsproxy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/security.h> |
| 28 | #include <linux/mount.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 29 | #include <linux/ramfs.h> |
Eric Dumazet | 13f14b4 | 2008-02-06 01:37:57 -0800 | [diff] [blame] | 30 | #include <linux/log2.h> |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 31 | #include <linux/idr.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 32 | #include <linux/fs_struct.h> |
Andreas Gruenbacher | 2504c5d | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 33 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/uaccess.h> |
| 35 | #include <asm/unistd.h> |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 36 | #include "pnode.h" |
Adrian Bunk | 948730b | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 37 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Eric Dumazet | 13f14b4 | 2008-02-06 01:37:57 -0800 | [diff] [blame] | 39 | #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head)) |
| 40 | #define HASH_SIZE (1UL << HASH_SHIFT) |
| 41 | |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 42 | static int event; |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 43 | static DEFINE_IDA(mnt_id_ida); |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 44 | static DEFINE_IDA(mnt_group_ida); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 45 | static DEFINE_SPINLOCK(mnt_id_lock); |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 46 | static int mnt_id_start = 0; |
| 47 | static int mnt_group_start = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Eric Dumazet | fa3536c | 2006-03-26 01:37:24 -0800 | [diff] [blame] | 49 | static struct list_head *mount_hashtable __read_mostly; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 50 | static struct kmem_cache *mnt_cache __read_mostly; |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 51 | static struct rw_semaphore namespace_sem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Miklos Szeredi | f87fd4c | 2006-01-16 22:14:23 -0800 | [diff] [blame] | 53 | /* /sys/fs */ |
Greg Kroah-Hartman | 00d2666 | 2007-10-29 14:17:23 -0600 | [diff] [blame] | 54 | struct kobject *fs_kobj; |
| 55 | EXPORT_SYMBOL_GPL(fs_kobj); |
Miklos Szeredi | f87fd4c | 2006-01-16 22:14:23 -0800 | [diff] [blame] | 56 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 57 | /* |
| 58 | * vfsmount lock may be taken for read to prevent changes to the |
| 59 | * vfsmount hash, ie. during mountpoint lookups or walking back |
| 60 | * up the tree. |
| 61 | * |
| 62 | * It should be taken for write in all cases where the vfsmount |
| 63 | * tree or hash is modified or when a vfsmount structure is modified. |
| 64 | */ |
| 65 | DEFINE_BRLOCK(vfsmount_lock); |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) |
| 68 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 69 | unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); |
| 70 | tmp += ((unsigned long)dentry / L1_CACHE_BYTES); |
Eric Dumazet | 13f14b4 | 2008-02-06 01:37:57 -0800 | [diff] [blame] | 71 | tmp = tmp + (tmp >> HASH_SHIFT); |
| 72 | return tmp & (HASH_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 75 | #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) |
| 76 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 77 | /* |
| 78 | * allocation is serialized by namespace_sem, but we need the spinlock to |
| 79 | * serialize with freeing. |
| 80 | */ |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 81 | static int mnt_alloc_id(struct vfsmount *mnt) |
| 82 | { |
| 83 | int res; |
| 84 | |
| 85 | retry: |
| 86 | ida_pre_get(&mnt_id_ida, GFP_KERNEL); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 87 | spin_lock(&mnt_id_lock); |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 88 | res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); |
| 89 | if (!res) |
| 90 | mnt_id_start = mnt->mnt_id + 1; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 91 | spin_unlock(&mnt_id_lock); |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 92 | if (res == -EAGAIN) |
| 93 | goto retry; |
| 94 | |
| 95 | return res; |
| 96 | } |
| 97 | |
| 98 | static void mnt_free_id(struct vfsmount *mnt) |
| 99 | { |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 100 | int id = mnt->mnt_id; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 101 | spin_lock(&mnt_id_lock); |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 102 | ida_remove(&mnt_id_ida, id); |
| 103 | if (mnt_id_start > id) |
| 104 | mnt_id_start = id; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 105 | spin_unlock(&mnt_id_lock); |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 108 | /* |
| 109 | * Allocate a new peer group ID |
| 110 | * |
| 111 | * mnt_group_ida is protected by namespace_sem |
| 112 | */ |
| 113 | static int mnt_alloc_group_id(struct vfsmount *mnt) |
| 114 | { |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 115 | int res; |
| 116 | |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 117 | if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) |
| 118 | return -ENOMEM; |
| 119 | |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 120 | res = ida_get_new_above(&mnt_group_ida, |
| 121 | mnt_group_start, |
| 122 | &mnt->mnt_group_id); |
| 123 | if (!res) |
| 124 | mnt_group_start = mnt->mnt_group_id + 1; |
| 125 | |
| 126 | return res; |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Release a peer group ID |
| 131 | */ |
| 132 | void mnt_release_group_id(struct vfsmount *mnt) |
| 133 | { |
Al Viro | f21f622 | 2009-06-24 03:12:00 -0400 | [diff] [blame] | 134 | int id = mnt->mnt_group_id; |
| 135 | ida_remove(&mnt_group_ida, id); |
| 136 | if (mnt_group_start > id) |
| 137 | mnt_group_start = id; |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 138 | mnt->mnt_group_id = 0; |
| 139 | } |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct vfsmount *alloc_vfsmnt(const char *name) |
| 142 | { |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 143 | struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (mnt) { |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 145 | int err; |
| 146 | |
| 147 | err = mnt_alloc_id(mnt); |
Li Zefan | 88b3878 | 2008-07-21 18:06:36 +0800 | [diff] [blame] | 148 | if (err) |
| 149 | goto out_free_cache; |
| 150 | |
| 151 | if (name) { |
| 152 | mnt->mnt_devname = kstrdup(name, GFP_KERNEL); |
| 153 | if (!mnt->mnt_devname) |
| 154 | goto out_free_id; |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 157 | atomic_set(&mnt->mnt_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | INIT_LIST_HEAD(&mnt->mnt_hash); |
| 159 | INIT_LIST_HEAD(&mnt->mnt_child); |
| 160 | INIT_LIST_HEAD(&mnt->mnt_mounts); |
| 161 | INIT_LIST_HEAD(&mnt->mnt_list); |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 162 | INIT_LIST_HEAD(&mnt->mnt_expire); |
Ram Pai | 03e06e6 | 2005-11-07 17:19:33 -0500 | [diff] [blame] | 163 | INIT_LIST_HEAD(&mnt->mnt_share); |
Ram Pai | a58b0eb | 2005-11-07 17:20:48 -0500 | [diff] [blame] | 164 | INIT_LIST_HEAD(&mnt->mnt_slave_list); |
| 165 | INIT_LIST_HEAD(&mnt->mnt_slave); |
Andreas Gruenbacher | 2504c5d | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 166 | #ifdef CONFIG_FSNOTIFY |
| 167 | INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); |
| 168 | #endif |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 169 | #ifdef CONFIG_SMP |
| 170 | mnt->mnt_writers = alloc_percpu(int); |
| 171 | if (!mnt->mnt_writers) |
| 172 | goto out_free_devname; |
| 173 | #else |
| 174 | mnt->mnt_writers = 0; |
| 175 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | return mnt; |
Li Zefan | 88b3878 | 2008-07-21 18:06:36 +0800 | [diff] [blame] | 178 | |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 179 | #ifdef CONFIG_SMP |
| 180 | out_free_devname: |
| 181 | kfree(mnt->mnt_devname); |
| 182 | #endif |
Li Zefan | 88b3878 | 2008-07-21 18:06:36 +0800 | [diff] [blame] | 183 | out_free_id: |
| 184 | mnt_free_id(mnt); |
| 185 | out_free_cache: |
| 186 | kmem_cache_free(mnt_cache, mnt); |
| 187 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 190 | /* |
| 191 | * Most r/o checks on a fs are for operations that take |
| 192 | * discrete amounts of time, like a write() or unlink(). |
| 193 | * We must keep track of when those operations start |
| 194 | * (for permission checks) and when they end, so that |
| 195 | * we can determine when writes are able to occur to |
| 196 | * a filesystem. |
| 197 | */ |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 198 | /* |
| 199 | * __mnt_is_readonly: check whether a mount is read-only |
| 200 | * @mnt: the mount to check for its write status |
| 201 | * |
| 202 | * This shouldn't be used directly ouside of the VFS. |
| 203 | * It does not guarantee that the filesystem will stay |
| 204 | * r/w, just that it is right *now*. This can not and |
| 205 | * should not be used in place of IS_RDONLY(inode). |
| 206 | * mnt_want/drop_write() will _keep_ the filesystem |
| 207 | * r/w. |
| 208 | */ |
| 209 | int __mnt_is_readonly(struct vfsmount *mnt) |
| 210 | { |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 211 | if (mnt->mnt_flags & MNT_READONLY) |
| 212 | return 1; |
| 213 | if (mnt->mnt_sb->s_flags & MS_RDONLY) |
| 214 | return 1; |
| 215 | return 0; |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 216 | } |
| 217 | EXPORT_SYMBOL_GPL(__mnt_is_readonly); |
| 218 | |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 219 | static inline void mnt_inc_writers(struct vfsmount *mnt) |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 220 | { |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 221 | #ifdef CONFIG_SMP |
| 222 | (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++; |
| 223 | #else |
| 224 | mnt->mnt_writers++; |
| 225 | #endif |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 226 | } |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 227 | |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 228 | static inline void mnt_dec_writers(struct vfsmount *mnt) |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 229 | { |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 230 | #ifdef CONFIG_SMP |
| 231 | (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--; |
| 232 | #else |
| 233 | mnt->mnt_writers--; |
| 234 | #endif |
| 235 | } |
| 236 | |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 237 | static unsigned int mnt_get_writers(struct vfsmount *mnt) |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 238 | { |
| 239 | #ifdef CONFIG_SMP |
| 240 | unsigned int count = 0; |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 241 | int cpu; |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 242 | |
| 243 | for_each_possible_cpu(cpu) { |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 244 | count += *per_cpu_ptr(mnt->mnt_writers, cpu); |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 245 | } |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 246 | |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 247 | return count; |
| 248 | #else |
| 249 | return mnt->mnt_writers; |
| 250 | #endif |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Most r/o checks on a fs are for operations that take |
| 255 | * discrete amounts of time, like a write() or unlink(). |
| 256 | * We must keep track of when those operations start |
| 257 | * (for permission checks) and when they end, so that |
| 258 | * we can determine when writes are able to occur to |
| 259 | * a filesystem. |
| 260 | */ |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 261 | /** |
| 262 | * mnt_want_write - get write access to a mount |
| 263 | * @mnt: the mount on which to take a write |
| 264 | * |
| 265 | * This tells the low-level filesystem that a write is |
| 266 | * about to be performed to it, and makes sure that |
| 267 | * writes are allowed before returning success. When |
| 268 | * the write operation is finished, mnt_drop_write() |
| 269 | * must be called. This is effectively a refcount. |
| 270 | */ |
| 271 | int mnt_want_write(struct vfsmount *mnt) |
| 272 | { |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 273 | int ret = 0; |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 274 | |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 275 | preempt_disable(); |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 276 | mnt_inc_writers(mnt); |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 277 | /* |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 278 | * The store to mnt_inc_writers must be visible before we pass |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 279 | * MNT_WRITE_HOLD loop below, so that the slowpath can see our |
| 280 | * incremented count after it has set MNT_WRITE_HOLD. |
| 281 | */ |
| 282 | smp_mb(); |
| 283 | while (mnt->mnt_flags & MNT_WRITE_HOLD) |
| 284 | cpu_relax(); |
| 285 | /* |
| 286 | * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will |
| 287 | * be set to match its requirements. So we must not load that until |
| 288 | * MNT_WRITE_HOLD is cleared. |
| 289 | */ |
| 290 | smp_rmb(); |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 291 | if (__mnt_is_readonly(mnt)) { |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 292 | mnt_dec_writers(mnt); |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 293 | ret = -EROFS; |
| 294 | goto out; |
| 295 | } |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 296 | out: |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 297 | preempt_enable(); |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 298 | return ret; |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 299 | } |
| 300 | EXPORT_SYMBOL_GPL(mnt_want_write); |
| 301 | |
| 302 | /** |
npiggin@suse.de | 96029c4 | 2009-04-26 20:25:55 +1000 | [diff] [blame] | 303 | * mnt_clone_write - get write access to a mount |
| 304 | * @mnt: the mount on which to take a write |
| 305 | * |
| 306 | * This is effectively like mnt_want_write, except |
| 307 | * it must only be used to take an extra write reference |
| 308 | * on a mountpoint that we already know has a write reference |
| 309 | * on it. This allows some optimisation. |
| 310 | * |
| 311 | * After finished, mnt_drop_write must be called as usual to |
| 312 | * drop the reference. |
| 313 | */ |
| 314 | int mnt_clone_write(struct vfsmount *mnt) |
| 315 | { |
| 316 | /* superblock may be r/o */ |
| 317 | if (__mnt_is_readonly(mnt)) |
| 318 | return -EROFS; |
| 319 | preempt_disable(); |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 320 | mnt_inc_writers(mnt); |
npiggin@suse.de | 96029c4 | 2009-04-26 20:25:55 +1000 | [diff] [blame] | 321 | preempt_enable(); |
| 322 | return 0; |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(mnt_clone_write); |
| 325 | |
| 326 | /** |
| 327 | * mnt_want_write_file - get write access to a file's mount |
| 328 | * @file: the file who's mount on which to take a write |
| 329 | * |
| 330 | * This is like mnt_want_write, but it takes a file and can |
| 331 | * do some optimisations if the file is open for write already |
| 332 | */ |
| 333 | int mnt_want_write_file(struct file *file) |
| 334 | { |
OGAWA Hirofumi | 2d8dd38 | 2009-08-06 15:07:39 -0700 | [diff] [blame] | 335 | struct inode *inode = file->f_dentry->d_inode; |
| 336 | if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) |
npiggin@suse.de | 96029c4 | 2009-04-26 20:25:55 +1000 | [diff] [blame] | 337 | return mnt_want_write(file->f_path.mnt); |
| 338 | else |
| 339 | return mnt_clone_write(file->f_path.mnt); |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(mnt_want_write_file); |
| 342 | |
| 343 | /** |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 344 | * mnt_drop_write - give up write access to a mount |
| 345 | * @mnt: the mount on which to give up write access |
| 346 | * |
| 347 | * Tells the low-level filesystem that we are done |
| 348 | * performing writes to it. Must be matched with |
| 349 | * mnt_want_write() call above. |
| 350 | */ |
| 351 | void mnt_drop_write(struct vfsmount *mnt) |
| 352 | { |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 353 | preempt_disable(); |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 354 | mnt_dec_writers(mnt); |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 355 | preempt_enable(); |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 356 | } |
| 357 | EXPORT_SYMBOL_GPL(mnt_drop_write); |
| 358 | |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 359 | static int mnt_make_readonly(struct vfsmount *mnt) |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 360 | { |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 361 | int ret = 0; |
| 362 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 363 | br_write_lock(vfsmount_lock); |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 364 | mnt->mnt_flags |= MNT_WRITE_HOLD; |
| 365 | /* |
| 366 | * After storing MNT_WRITE_HOLD, we'll read the counters. This store |
| 367 | * should be visible before we do. |
| 368 | */ |
| 369 | smp_mb(); |
| 370 | |
| 371 | /* |
| 372 | * With writers on hold, if this value is zero, then there are |
| 373 | * definitely no active writers (although held writers may subsequently |
| 374 | * increment the count, they'll have to wait, and decrement it after |
| 375 | * seeing MNT_READONLY). |
| 376 | * |
| 377 | * It is OK to have counter incremented on one CPU and decremented on |
| 378 | * another: the sum will add up correctly. The danger would be when we |
| 379 | * sum up each counter, if we read a counter before it is incremented, |
| 380 | * but then read another CPU's count which it has been subsequently |
| 381 | * decremented from -- we would see more decrements than we should. |
| 382 | * MNT_WRITE_HOLD protects against this scenario, because |
| 383 | * mnt_want_write first increments count, then smp_mb, then spins on |
| 384 | * MNT_WRITE_HOLD, so it can't be decremented by another CPU while |
| 385 | * we're counting up here. |
| 386 | */ |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 387 | if (mnt_get_writers(mnt) > 0) |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 388 | ret = -EBUSY; |
| 389 | else |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 390 | mnt->mnt_flags |= MNT_READONLY; |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 391 | /* |
| 392 | * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers |
| 393 | * that become unheld will see MNT_READONLY. |
| 394 | */ |
| 395 | smp_wmb(); |
| 396 | mnt->mnt_flags &= ~MNT_WRITE_HOLD; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 397 | br_write_unlock(vfsmount_lock); |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 398 | return ret; |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 399 | } |
Dave Hansen | 8366025 | 2008-02-15 14:37:30 -0800 | [diff] [blame] | 400 | |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 401 | static void __mnt_unmake_readonly(struct vfsmount *mnt) |
| 402 | { |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 403 | br_write_lock(vfsmount_lock); |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 404 | mnt->mnt_flags &= ~MNT_READONLY; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 405 | br_write_unlock(vfsmount_lock); |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Sukadev Bhattiprolu | a3ec947 | 2009-03-04 12:06:34 -0800 | [diff] [blame] | 408 | void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb) |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 409 | { |
| 410 | mnt->mnt_sb = sb; |
| 411 | mnt->mnt_root = dget(sb->s_root); |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | EXPORT_SYMBOL(simple_set_mnt); |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | void free_vfsmnt(struct vfsmount *mnt) |
| 417 | { |
| 418 | kfree(mnt->mnt_devname); |
Miklos Szeredi | 73cd49e | 2008-03-26 22:11:34 +0100 | [diff] [blame] | 419 | mnt_free_id(mnt); |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 420 | #ifdef CONFIG_SMP |
| 421 | free_percpu(mnt->mnt_writers); |
| 422 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | kmem_cache_free(mnt_cache, mnt); |
| 424 | } |
| 425 | |
| 426 | /* |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 427 | * find the first or last mount at @dentry on vfsmount @mnt depending on |
| 428 | * @dir. If @dir is set return the first mount else return the last mount. |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 429 | * vfsmount_lock must be held for read or write. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | */ |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 431 | struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, |
| 432 | int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 434 | struct list_head *head = mount_hashtable + hash(mnt, dentry); |
| 435 | struct list_head *tmp = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | struct vfsmount *p, *found = NULL; |
| 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | for (;;) { |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 439 | tmp = dir ? tmp->next : tmp->prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | p = NULL; |
| 441 | if (tmp == head) |
| 442 | break; |
| 443 | p = list_entry(tmp, struct vfsmount, mnt_hash); |
| 444 | if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) { |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 445 | found = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return found; |
| 450 | } |
| 451 | |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 452 | /* |
| 453 | * lookup_mnt increments the ref count before returning |
| 454 | * the vfsmount struct. |
| 455 | */ |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 456 | struct vfsmount *lookup_mnt(struct path *path) |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 457 | { |
| 458 | struct vfsmount *child_mnt; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 459 | |
| 460 | br_read_lock(vfsmount_lock); |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 461 | if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1))) |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 462 | mntget(child_mnt); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 463 | br_read_unlock(vfsmount_lock); |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 464 | return child_mnt; |
| 465 | } |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | static inline int check_mnt(struct vfsmount *mnt) |
| 468 | { |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 469 | return mnt->mnt_ns == current->nsproxy->mnt_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 472 | /* |
| 473 | * vfsmount lock must be held for write |
| 474 | */ |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 475 | static void touch_mnt_namespace(struct mnt_namespace *ns) |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 476 | { |
| 477 | if (ns) { |
| 478 | ns->event = ++event; |
| 479 | wake_up_interruptible(&ns->poll); |
| 480 | } |
| 481 | } |
| 482 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 483 | /* |
| 484 | * vfsmount lock must be held for write |
| 485 | */ |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 486 | static void __touch_mnt_namespace(struct mnt_namespace *ns) |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 487 | { |
| 488 | if (ns && ns->event != event) { |
| 489 | ns->event = event; |
| 490 | wake_up_interruptible(&ns->poll); |
| 491 | } |
| 492 | } |
| 493 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 494 | /* |
Nick Piggin | 5f57cbc | 2011-01-07 17:49:54 +1100 | [diff] [blame] | 495 | * Clear dentry's mounted state if it has no remaining mounts. |
| 496 | * vfsmount_lock must be held for write. |
| 497 | */ |
| 498 | static void dentry_reset_mounted(struct vfsmount *mnt, struct dentry *dentry) |
| 499 | { |
| 500 | unsigned u; |
| 501 | |
| 502 | for (u = 0; u < HASH_SIZE; u++) { |
| 503 | struct vfsmount *p; |
| 504 | |
| 505 | list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { |
| 506 | if (p->mnt_mountpoint == dentry) |
| 507 | return; |
| 508 | } |
| 509 | } |
| 510 | spin_lock(&dentry->d_lock); |
| 511 | dentry->d_flags &= ~DCACHE_MOUNTED; |
| 512 | spin_unlock(&dentry->d_lock); |
| 513 | } |
| 514 | |
| 515 | /* |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 516 | * vfsmount lock must be held for write |
| 517 | */ |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 518 | static void detach_mnt(struct vfsmount *mnt, struct path *old_path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | { |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 520 | old_path->dentry = mnt->mnt_mountpoint; |
| 521 | old_path->mnt = mnt->mnt_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | mnt->mnt_parent = mnt; |
| 523 | mnt->mnt_mountpoint = mnt->mnt_root; |
| 524 | list_del_init(&mnt->mnt_child); |
| 525 | list_del_init(&mnt->mnt_hash); |
Nick Piggin | 5f57cbc | 2011-01-07 17:49:54 +1100 | [diff] [blame] | 526 | dentry_reset_mounted(old_path->mnt, old_path->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 529 | /* |
| 530 | * vfsmount lock must be held for write |
| 531 | */ |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 532 | void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry, |
| 533 | struct vfsmount *child_mnt) |
| 534 | { |
| 535 | child_mnt->mnt_parent = mntget(mnt); |
| 536 | child_mnt->mnt_mountpoint = dget(dentry); |
Nick Piggin | 5f57cbc | 2011-01-07 17:49:54 +1100 | [diff] [blame] | 537 | spin_lock(&dentry->d_lock); |
| 538 | dentry->d_flags |= DCACHE_MOUNTED; |
| 539 | spin_unlock(&dentry->d_lock); |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 540 | } |
| 541 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 542 | /* |
| 543 | * vfsmount lock must be held for write |
| 544 | */ |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 545 | static void attach_mnt(struct vfsmount *mnt, struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 547 | mnt_set_mountpoint(path->mnt, path->dentry, mnt); |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 548 | list_add_tail(&mnt->mnt_hash, mount_hashtable + |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 549 | hash(path->mnt, path->dentry)); |
| 550 | list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts); |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 554 | * vfsmount lock must be held for write |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 555 | */ |
| 556 | static void commit_tree(struct vfsmount *mnt) |
| 557 | { |
| 558 | struct vfsmount *parent = mnt->mnt_parent; |
| 559 | struct vfsmount *m; |
| 560 | LIST_HEAD(head); |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 561 | struct mnt_namespace *n = parent->mnt_ns; |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 562 | |
| 563 | BUG_ON(parent == mnt); |
| 564 | |
| 565 | list_add_tail(&head, &mnt->mnt_list); |
| 566 | list_for_each_entry(m, &head, mnt_list) |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 567 | m->mnt_ns = n; |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 568 | list_splice(&head, n->list.prev); |
| 569 | |
| 570 | list_add_tail(&mnt->mnt_hash, mount_hashtable + |
| 571 | hash(parent, mnt->mnt_mountpoint)); |
| 572 | list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 573 | touch_mnt_namespace(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root) |
| 577 | { |
| 578 | struct list_head *next = p->mnt_mounts.next; |
| 579 | if (next == &p->mnt_mounts) { |
| 580 | while (1) { |
| 581 | if (p == root) |
| 582 | return NULL; |
| 583 | next = p->mnt_child.next; |
| 584 | if (next != &p->mnt_parent->mnt_mounts) |
| 585 | break; |
| 586 | p = p->mnt_parent; |
| 587 | } |
| 588 | } |
| 589 | return list_entry(next, struct vfsmount, mnt_child); |
| 590 | } |
| 591 | |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 592 | static struct vfsmount *skip_mnt_tree(struct vfsmount *p) |
| 593 | { |
| 594 | struct list_head *prev = p->mnt_mounts.prev; |
| 595 | while (prev != &p->mnt_mounts) { |
| 596 | p = list_entry(prev, struct vfsmount, mnt_child); |
| 597 | prev = p->mnt_mounts.prev; |
| 598 | } |
| 599 | return p; |
| 600 | } |
| 601 | |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 602 | static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root, |
| 603 | int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
| 605 | struct super_block *sb = old->mnt_sb; |
| 606 | struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname); |
| 607 | |
| 608 | if (mnt) { |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 609 | if (flag & (CL_SLAVE | CL_PRIVATE)) |
| 610 | mnt->mnt_group_id = 0; /* not a peer of original */ |
| 611 | else |
| 612 | mnt->mnt_group_id = old->mnt_group_id; |
| 613 | |
| 614 | if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { |
| 615 | int err = mnt_alloc_group_id(mnt); |
| 616 | if (err) |
| 617 | goto out_free; |
| 618 | } |
| 619 | |
Miklos Szeredi | be1a16a | 2010-10-05 12:31:09 +0200 | [diff] [blame] | 620 | mnt->mnt_flags = old->mnt_flags & ~MNT_WRITE_HOLD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | atomic_inc(&sb->s_active); |
| 622 | mnt->mnt_sb = sb; |
| 623 | mnt->mnt_root = dget(root); |
| 624 | mnt->mnt_mountpoint = mnt->mnt_root; |
| 625 | mnt->mnt_parent = mnt; |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 626 | |
Ram Pai | 5afe002 | 2005-11-07 17:21:01 -0500 | [diff] [blame] | 627 | if (flag & CL_SLAVE) { |
| 628 | list_add(&mnt->mnt_slave, &old->mnt_slave_list); |
| 629 | mnt->mnt_master = old; |
| 630 | CLEAR_MNT_SHARED(mnt); |
Al Viro | 8aec080 | 2007-06-07 12:20:32 -0400 | [diff] [blame] | 631 | } else if (!(flag & CL_PRIVATE)) { |
Al Viro | 796a6b5 | 2010-01-16 13:28:47 -0500 | [diff] [blame] | 632 | if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) |
Ram Pai | 5afe002 | 2005-11-07 17:21:01 -0500 | [diff] [blame] | 633 | list_add(&mnt->mnt_share, &old->mnt_share); |
| 634 | if (IS_MNT_SLAVE(old)) |
| 635 | list_add(&mnt->mnt_slave, &old->mnt_slave); |
| 636 | mnt->mnt_master = old->mnt_master; |
| 637 | } |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 638 | if (flag & CL_MAKE_SHARED) |
| 639 | set_mnt_shared(mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | /* stick the duplicate mount on the same expiry list |
| 642 | * as the original if that was on one */ |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 643 | if (flag & CL_EXPIRE) { |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 644 | if (!list_empty(&old->mnt_expire)) |
| 645 | list_add(&mnt->mnt_expire, &old->mnt_expire); |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | return mnt; |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 649 | |
| 650 | out_free: |
| 651 | free_vfsmnt(mnt); |
| 652 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 655 | static inline void __mntput(struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | struct super_block *sb = mnt->mnt_sb; |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 658 | /* |
Dave Hansen | 3d73363 | 2008-02-15 14:37:59 -0800 | [diff] [blame] | 659 | * This probably indicates that somebody messed |
| 660 | * up a mnt_want/drop_write() pair. If this |
| 661 | * happens, the filesystem was probably unable |
| 662 | * to make r/w->r/o transitions. |
| 663 | */ |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 664 | /* |
| 665 | * atomic_dec_and_lock() used to deal with ->mnt_count decrements |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 666 | * provides barriers, so mnt_get_writers() below is safe. AV |
npiggin@suse.de | d3ef3d7 | 2009-04-26 20:25:54 +1000 | [diff] [blame] | 667 | */ |
Nick Piggin | c6653a8 | 2011-01-07 17:50:10 +1100 | [diff] [blame^] | 668 | WARN_ON(mnt_get_writers(mnt)); |
Andreas Gruenbacher | ca9c726 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 669 | fsnotify_vfsmount_delete(mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | dput(mnt->mnt_root); |
| 671 | free_vfsmnt(mnt); |
| 672 | deactivate_super(sb); |
| 673 | } |
| 674 | |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 675 | void mntput_no_expire(struct vfsmount *mnt) |
| 676 | { |
| 677 | repeat: |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 678 | if (atomic_add_unless(&mnt->mnt_count, -1, 1)) |
| 679 | return; |
| 680 | br_write_lock(vfsmount_lock); |
| 681 | if (!atomic_dec_and_test(&mnt->mnt_count)) { |
| 682 | br_write_unlock(vfsmount_lock); |
| 683 | return; |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 684 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 685 | if (likely(!mnt->mnt_pinned)) { |
| 686 | br_write_unlock(vfsmount_lock); |
| 687 | __mntput(mnt); |
| 688 | return; |
| 689 | } |
| 690 | atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count); |
| 691 | mnt->mnt_pinned = 0; |
| 692 | br_write_unlock(vfsmount_lock); |
| 693 | acct_auto_close_mnt(mnt); |
| 694 | goto repeat; |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 695 | } |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 696 | EXPORT_SYMBOL(mntput_no_expire); |
| 697 | |
| 698 | void mnt_pin(struct vfsmount *mnt) |
| 699 | { |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 700 | br_write_lock(vfsmount_lock); |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 701 | mnt->mnt_pinned++; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 702 | br_write_unlock(vfsmount_lock); |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | EXPORT_SYMBOL(mnt_pin); |
| 706 | |
| 707 | void mnt_unpin(struct vfsmount *mnt) |
| 708 | { |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 709 | br_write_lock(vfsmount_lock); |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 710 | if (mnt->mnt_pinned) { |
| 711 | atomic_inc(&mnt->mnt_count); |
| 712 | mnt->mnt_pinned--; |
| 713 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 714 | br_write_unlock(vfsmount_lock); |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | EXPORT_SYMBOL(mnt_unpin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
Miklos Szeredi | b3b304a | 2008-02-08 04:21:35 -0800 | [diff] [blame] | 719 | static inline void mangle(struct seq_file *m, const char *s) |
| 720 | { |
| 721 | seq_escape(m, s, " \t\n\\"); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Simple .show_options callback for filesystems which don't want to |
| 726 | * implement more complex mount option showing. |
| 727 | * |
| 728 | * See also save_mount_options(). |
| 729 | */ |
| 730 | int generic_show_options(struct seq_file *m, struct vfsmount *mnt) |
| 731 | { |
Al Viro | 2a32ceb | 2009-05-08 16:05:57 -0400 | [diff] [blame] | 732 | const char *options; |
| 733 | |
| 734 | rcu_read_lock(); |
| 735 | options = rcu_dereference(mnt->mnt_sb->s_options); |
Miklos Szeredi | b3b304a | 2008-02-08 04:21:35 -0800 | [diff] [blame] | 736 | |
| 737 | if (options != NULL && options[0]) { |
| 738 | seq_putc(m, ','); |
| 739 | mangle(m, options); |
| 740 | } |
Al Viro | 2a32ceb | 2009-05-08 16:05:57 -0400 | [diff] [blame] | 741 | rcu_read_unlock(); |
Miklos Szeredi | b3b304a | 2008-02-08 04:21:35 -0800 | [diff] [blame] | 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | EXPORT_SYMBOL(generic_show_options); |
| 746 | |
| 747 | /* |
| 748 | * If filesystem uses generic_show_options(), this function should be |
| 749 | * called from the fill_super() callback. |
| 750 | * |
| 751 | * The .remount_fs callback usually needs to be handled in a special |
| 752 | * way, to make sure, that previous options are not overwritten if the |
| 753 | * remount fails. |
| 754 | * |
| 755 | * Also note, that if the filesystem's .remount_fs function doesn't |
| 756 | * reset all options to their default value, but changes only newly |
| 757 | * given options, then the displayed options will not reflect reality |
| 758 | * any more. |
| 759 | */ |
| 760 | void save_mount_options(struct super_block *sb, char *options) |
| 761 | { |
Al Viro | 2a32ceb | 2009-05-08 16:05:57 -0400 | [diff] [blame] | 762 | BUG_ON(sb->s_options); |
| 763 | rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); |
Miklos Szeredi | b3b304a | 2008-02-08 04:21:35 -0800 | [diff] [blame] | 764 | } |
| 765 | EXPORT_SYMBOL(save_mount_options); |
| 766 | |
Al Viro | 2a32ceb | 2009-05-08 16:05:57 -0400 | [diff] [blame] | 767 | void replace_mount_options(struct super_block *sb, char *options) |
| 768 | { |
| 769 | char *old = sb->s_options; |
| 770 | rcu_assign_pointer(sb->s_options, options); |
| 771 | if (old) { |
| 772 | synchronize_rcu(); |
| 773 | kfree(old); |
| 774 | } |
| 775 | } |
| 776 | EXPORT_SYMBOL(replace_mount_options); |
| 777 | |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 778 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | /* iterator */ |
| 780 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 781 | { |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 782 | struct proc_mounts *p = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 784 | down_read(&namespace_sem); |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 785 | return seq_list_start(&p->ns->list, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) |
| 789 | { |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 790 | struct proc_mounts *p = m->private; |
Pavel Emelianov | b0765fb | 2007-07-15 23:39:55 -0700 | [diff] [blame] | 791 | |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 792 | return seq_list_next(v, &p->ns->list, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static void m_stop(struct seq_file *m, void *v) |
| 796 | { |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 797 | up_read(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Al Viro | 9f5596a | 2010-02-05 00:40:25 -0500 | [diff] [blame] | 800 | int mnt_had_events(struct proc_mounts *p) |
| 801 | { |
| 802 | struct mnt_namespace *ns = p->ns; |
| 803 | int res = 0; |
| 804 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 805 | br_read_lock(vfsmount_lock); |
Al Viro | 9f5596a | 2010-02-05 00:40:25 -0500 | [diff] [blame] | 806 | if (p->event != ns->event) { |
| 807 | p->event = ns->event; |
| 808 | res = 1; |
| 809 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 810 | br_read_unlock(vfsmount_lock); |
Al Viro | 9f5596a | 2010-02-05 00:40:25 -0500 | [diff] [blame] | 811 | |
| 812 | return res; |
| 813 | } |
| 814 | |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 815 | struct proc_fs_info { |
| 816 | int flag; |
| 817 | const char *str; |
| 818 | }; |
| 819 | |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 820 | static int show_sb_opts(struct seq_file *m, struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 822 | static const struct proc_fs_info fs_info[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { MS_SYNCHRONOUS, ",sync" }, |
| 824 | { MS_DIRSYNC, ",dirsync" }, |
| 825 | { MS_MANDLOCK, ",mand" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { 0, NULL } |
| 827 | }; |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 828 | const struct proc_fs_info *fs_infop; |
| 829 | |
| 830 | for (fs_infop = fs_info; fs_infop->flag; fs_infop++) { |
| 831 | if (sb->s_flags & fs_infop->flag) |
| 832 | seq_puts(m, fs_infop->str); |
| 833 | } |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 834 | |
| 835 | return security_sb_show_options(m, sb); |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt) |
| 839 | { |
| 840 | static const struct proc_fs_info mnt_info[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | { MNT_NOSUID, ",nosuid" }, |
| 842 | { MNT_NODEV, ",nodev" }, |
| 843 | { MNT_NOEXEC, ",noexec" }, |
Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 844 | { MNT_NOATIME, ",noatime" }, |
| 845 | { MNT_NODIRATIME, ",nodiratime" }, |
Valerie Henson | 47ae32d | 2006-12-13 00:34:34 -0800 | [diff] [blame] | 846 | { MNT_RELATIME, ",relatime" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { 0, NULL } |
| 848 | }; |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 849 | const struct proc_fs_info *fs_infop; |
| 850 | |
| 851 | for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) { |
| 852 | if (mnt->mnt_flags & fs_infop->flag) |
| 853 | seq_puts(m, fs_infop->str); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | static void show_type(struct seq_file *m, struct super_block *sb) |
| 858 | { |
| 859 | mangle(m, sb->s_type->name); |
| 860 | if (sb->s_subtype && sb->s_subtype[0]) { |
| 861 | seq_putc(m, '.'); |
| 862 | mangle(m, sb->s_subtype); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | static int show_vfsmnt(struct seq_file *m, void *v) |
| 867 | { |
| 868 | struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); |
| 869 | int err = 0; |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 870 | struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none"); |
| 873 | seq_putc(m, ' '); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 874 | seq_path(m, &mnt_path, " \t\n\\"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | seq_putc(m, ' '); |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 876 | show_type(m, mnt->mnt_sb); |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 877 | seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw"); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 878 | err = show_sb_opts(m, mnt->mnt_sb); |
| 879 | if (err) |
| 880 | goto out; |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 881 | show_mnt_opts(m, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | if (mnt->mnt_sb->s_op->show_options) |
| 883 | err = mnt->mnt_sb->s_op->show_options(m, mnt); |
| 884 | seq_puts(m, " 0 0\n"); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 885 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | return err; |
| 887 | } |
| 888 | |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 889 | const struct seq_operations mounts_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | .start = m_start, |
| 891 | .next = m_next, |
| 892 | .stop = m_stop, |
| 893 | .show = show_vfsmnt |
| 894 | }; |
| 895 | |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 896 | static int show_mountinfo(struct seq_file *m, void *v) |
| 897 | { |
| 898 | struct proc_mounts *p = m->private; |
| 899 | struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); |
| 900 | struct super_block *sb = mnt->mnt_sb; |
| 901 | struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; |
| 902 | struct path root = p->root; |
| 903 | int err = 0; |
| 904 | |
| 905 | seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id, |
| 906 | MAJOR(sb->s_dev), MINOR(sb->s_dev)); |
| 907 | seq_dentry(m, mnt->mnt_root, " \t\n\\"); |
| 908 | seq_putc(m, ' '); |
| 909 | seq_path_root(m, &mnt_path, &root, " \t\n\\"); |
| 910 | if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) { |
| 911 | /* |
| 912 | * Mountpoint is outside root, discard that one. Ugly, |
| 913 | * but less so than trying to do that in iterator in a |
| 914 | * race-free way (due to renames). |
| 915 | */ |
| 916 | return SEQ_SKIP; |
| 917 | } |
| 918 | seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw"); |
| 919 | show_mnt_opts(m, mnt); |
| 920 | |
| 921 | /* Tagged fields ("foo:X" or "bar") */ |
| 922 | if (IS_MNT_SHARED(mnt)) |
| 923 | seq_printf(m, " shared:%i", mnt->mnt_group_id); |
Miklos Szeredi | 97e7e0f | 2008-03-27 13:06:26 +0100 | [diff] [blame] | 924 | if (IS_MNT_SLAVE(mnt)) { |
| 925 | int master = mnt->mnt_master->mnt_group_id; |
| 926 | int dom = get_dominating_id(mnt, &p->root); |
| 927 | seq_printf(m, " master:%i", master); |
| 928 | if (dom && dom != master) |
| 929 | seq_printf(m, " propagate_from:%i", dom); |
| 930 | } |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 931 | if (IS_MNT_UNBINDABLE(mnt)) |
| 932 | seq_puts(m, " unbindable"); |
| 933 | |
| 934 | /* Filesystem specific data */ |
| 935 | seq_puts(m, " - "); |
| 936 | show_type(m, sb); |
| 937 | seq_putc(m, ' '); |
| 938 | mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none"); |
| 939 | seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw"); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 940 | err = show_sb_opts(m, sb); |
| 941 | if (err) |
| 942 | goto out; |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 943 | if (sb->s_op->show_options) |
| 944 | err = sb->s_op->show_options(m, mnt); |
| 945 | seq_putc(m, '\n'); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 946 | out: |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 947 | return err; |
| 948 | } |
| 949 | |
| 950 | const struct seq_operations mountinfo_op = { |
| 951 | .start = m_start, |
| 952 | .next = m_next, |
| 953 | .stop = m_stop, |
| 954 | .show = show_mountinfo, |
| 955 | }; |
| 956 | |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 957 | static int show_vfsstat(struct seq_file *m, void *v) |
| 958 | { |
Pavel Emelianov | b0765fb | 2007-07-15 23:39:55 -0700 | [diff] [blame] | 959 | struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 960 | struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 961 | int err = 0; |
| 962 | |
| 963 | /* device */ |
| 964 | if (mnt->mnt_devname) { |
| 965 | seq_puts(m, "device "); |
| 966 | mangle(m, mnt->mnt_devname); |
| 967 | } else |
| 968 | seq_puts(m, "no device"); |
| 969 | |
| 970 | /* mount point */ |
| 971 | seq_puts(m, " mounted on "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 972 | seq_path(m, &mnt_path, " \t\n\\"); |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 973 | seq_putc(m, ' '); |
| 974 | |
| 975 | /* file system type */ |
| 976 | seq_puts(m, "with fstype "); |
Ram Pai | 2d4d486 | 2008-03-27 13:06:25 +0100 | [diff] [blame] | 977 | show_type(m, mnt->mnt_sb); |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 978 | |
| 979 | /* optional statistics */ |
| 980 | if (mnt->mnt_sb->s_op->show_stats) { |
| 981 | seq_putc(m, ' '); |
| 982 | err = mnt->mnt_sb->s_op->show_stats(m, mnt); |
| 983 | } |
| 984 | |
| 985 | seq_putc(m, '\n'); |
| 986 | return err; |
| 987 | } |
| 988 | |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 989 | const struct seq_operations mountstats_op = { |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 990 | .start = m_start, |
| 991 | .next = m_next, |
| 992 | .stop = m_stop, |
| 993 | .show = show_vfsstat, |
| 994 | }; |
Miklos Szeredi | a1a2c40 | 2008-03-27 13:06:24 +0100 | [diff] [blame] | 995 | #endif /* CONFIG_PROC_FS */ |
Chuck Lever | b4629fe | 2006-03-20 13:44:12 -0500 | [diff] [blame] | 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | /** |
| 998 | * may_umount_tree - check if a mount tree is busy |
| 999 | * @mnt: root of mount tree |
| 1000 | * |
| 1001 | * This is called to check if a tree of mounts has any |
| 1002 | * open files, pwds, chroots or sub mounts that are |
| 1003 | * busy. |
| 1004 | */ |
| 1005 | int may_umount_tree(struct vfsmount *mnt) |
| 1006 | { |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 1007 | int actual_refs = 0; |
| 1008 | int minimum_refs = 0; |
| 1009 | struct vfsmount *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1011 | br_read_lock(vfsmount_lock); |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 1012 | for (p = mnt; p; p = next_mnt(p, mnt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | actual_refs += atomic_read(&p->mnt_count); |
| 1014 | minimum_refs += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1016 | br_read_unlock(vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | if (actual_refs > minimum_refs) |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 1019 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 1021 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | EXPORT_SYMBOL(may_umount_tree); |
| 1025 | |
| 1026 | /** |
| 1027 | * may_umount - check if a mount point is busy |
| 1028 | * @mnt: root of mount |
| 1029 | * |
| 1030 | * This is called to check if a mount point has any |
| 1031 | * open files, pwds, chroots or sub mounts. If the |
| 1032 | * mount has sub mounts this will return busy |
| 1033 | * regardless of whether the sub mounts are busy. |
| 1034 | * |
| 1035 | * Doesn't take quota and stuff into account. IOW, in some cases it will |
| 1036 | * give false negatives. The main reason why it's here is that we need |
| 1037 | * a non-destructive way to look for easily umountable filesystems. |
| 1038 | */ |
| 1039 | int may_umount(struct vfsmount *mnt) |
| 1040 | { |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 1041 | int ret = 1; |
Al Viro | 8ad08d8 | 2010-01-16 12:56:08 -0500 | [diff] [blame] | 1042 | down_read(&namespace_sem); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1043 | br_read_lock(vfsmount_lock); |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1044 | if (propagate_mount_busy(mnt, 2)) |
Ian Kent | e3474a8 | 2006-03-27 01:14:51 -0800 | [diff] [blame] | 1045 | ret = 0; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1046 | br_read_unlock(vfsmount_lock); |
Al Viro | 8ad08d8 | 2010-01-16 12:56:08 -0500 | [diff] [blame] | 1047 | up_read(&namespace_sem); |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1048 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | EXPORT_SYMBOL(may_umount); |
| 1052 | |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1053 | void release_mounts(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1055 | struct vfsmount *mnt; |
Miklos Szeredi | bf066c7 | 2006-01-08 01:03:19 -0800 | [diff] [blame] | 1056 | while (!list_empty(head)) { |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 1057 | mnt = list_first_entry(head, struct vfsmount, mnt_hash); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1058 | list_del_init(&mnt->mnt_hash); |
| 1059 | if (mnt->mnt_parent != mnt) { |
| 1060 | struct dentry *dentry; |
| 1061 | struct vfsmount *m; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1062 | |
| 1063 | br_write_lock(vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1064 | dentry = mnt->mnt_mountpoint; |
| 1065 | m = mnt->mnt_parent; |
| 1066 | mnt->mnt_mountpoint = mnt->mnt_root; |
| 1067 | mnt->mnt_parent = mnt; |
Al Viro | 7c4b93d | 2008-03-21 23:59:49 -0400 | [diff] [blame] | 1068 | m->mnt_ghosts--; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1069 | br_write_unlock(vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1070 | dput(dentry); |
| 1071 | mntput(m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
| 1073 | mntput(mnt); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1077 | /* |
| 1078 | * vfsmount lock must be held for write |
| 1079 | * namespace_sem must be held for write |
| 1080 | */ |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1081 | void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill) |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1082 | { |
| 1083 | struct vfsmount *p; |
| 1084 | |
Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 1085 | for (p = mnt; p; p = next_mnt(p, mnt)) |
| 1086 | list_move(&p->mnt_hash, kill); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1087 | |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1088 | if (propagate) |
| 1089 | propagate_umount(kill); |
| 1090 | |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1091 | list_for_each_entry(p, kill, mnt_hash) { |
| 1092 | list_del_init(&p->mnt_expire); |
| 1093 | list_del_init(&p->mnt_list); |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 1094 | __touch_mnt_namespace(p->mnt_ns); |
| 1095 | p->mnt_ns = NULL; |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1096 | list_del_init(&p->mnt_child); |
Al Viro | 7c4b93d | 2008-03-21 23:59:49 -0400 | [diff] [blame] | 1097 | if (p->mnt_parent != p) { |
| 1098 | p->mnt_parent->mnt_ghosts++; |
Nick Piggin | 5f57cbc | 2011-01-07 17:49:54 +1100 | [diff] [blame] | 1099 | dentry_reset_mounted(p->mnt_parent, p->mnt_mountpoint); |
Al Viro | 7c4b93d | 2008-03-21 23:59:49 -0400 | [diff] [blame] | 1100 | } |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1101 | change_mnt_propagation(p, MS_PRIVATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
Al Viro | c35038b | 2008-03-22 00:46:23 -0400 | [diff] [blame] | 1105 | static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts); |
| 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | static int do_umount(struct vfsmount *mnt, int flags) |
| 1108 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1109 | struct super_block *sb = mnt->mnt_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | int retval; |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1111 | LIST_HEAD(umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
| 1113 | retval = security_sb_umount(mnt, flags); |
| 1114 | if (retval) |
| 1115 | return retval; |
| 1116 | |
| 1117 | /* |
| 1118 | * Allow userspace to request a mountpoint be expired rather than |
| 1119 | * unmounting unconditionally. Unmount only happens if: |
| 1120 | * (1) the mark is already set (the mark is cleared by mntput()) |
| 1121 | * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] |
| 1122 | */ |
| 1123 | if (flags & MNT_EXPIRE) { |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 1124 | if (mnt == current->fs->root.mnt || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | flags & (MNT_FORCE | MNT_DETACH)) |
| 1126 | return -EINVAL; |
| 1127 | |
| 1128 | if (atomic_read(&mnt->mnt_count) != 2) |
| 1129 | return -EBUSY; |
| 1130 | |
| 1131 | if (!xchg(&mnt->mnt_expiry_mark, 1)) |
| 1132 | return -EAGAIN; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * If we may have to abort operations to get out of this |
| 1137 | * mount, and they will themselves hold resources we must |
| 1138 | * allow the fs to do things. In the Unix tradition of |
| 1139 | * 'Gee thats tricky lets do it in userspace' the umount_begin |
| 1140 | * might fail to complete on the first run through as other tasks |
| 1141 | * must return, and the like. Thats for the mount program to worry |
| 1142 | * about for the moment. |
| 1143 | */ |
| 1144 | |
Al Viro | 42faad9 | 2008-04-24 07:21:56 -0400 | [diff] [blame] | 1145 | if (flags & MNT_FORCE && sb->s_op->umount_begin) { |
Al Viro | 42faad9 | 2008-04-24 07:21:56 -0400 | [diff] [blame] | 1146 | sb->s_op->umount_begin(sb); |
Al Viro | 42faad9 | 2008-04-24 07:21:56 -0400 | [diff] [blame] | 1147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
| 1149 | /* |
| 1150 | * No sense to grab the lock for this test, but test itself looks |
| 1151 | * somewhat bogus. Suggestions for better replacement? |
| 1152 | * Ho-hum... In principle, we might treat that as umount + switch |
| 1153 | * to rootfs. GC would eventually take care of the old vfsmount. |
| 1154 | * Actually it makes sense, especially if rootfs would contain a |
| 1155 | * /reboot - static binary that would close all descriptors and |
| 1156 | * call reboot(9). Then init(8) could umount root and exec /reboot. |
| 1157 | */ |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 1158 | if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | /* |
| 1160 | * Special case for "unmounting" root ... |
| 1161 | * we just try to remount it readonly. |
| 1162 | */ |
| 1163 | down_write(&sb->s_umount); |
Al Viro | 4aa98cf | 2009-05-08 13:36:58 -0400 | [diff] [blame] | 1164 | if (!(sb->s_flags & MS_RDONLY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | up_write(&sb->s_umount); |
| 1167 | return retval; |
| 1168 | } |
| 1169 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1170 | down_write(&namespace_sem); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1171 | br_write_lock(vfsmount_lock); |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 1172 | event++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
Al Viro | c35038b | 2008-03-22 00:46:23 -0400 | [diff] [blame] | 1174 | if (!(flags & MNT_DETACH)) |
| 1175 | shrink_submounts(mnt, &umount_list); |
| 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | retval = -EBUSY; |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1178 | if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | if (!list_empty(&mnt->mnt_list)) |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1180 | umount_tree(mnt, 1, &umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | retval = 0; |
| 1182 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1183 | br_write_unlock(vfsmount_lock); |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1184 | up_write(&namespace_sem); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1185 | release_mounts(&umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return retval; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * Now umount can handle mount points as well as block devices. |
| 1191 | * This is important for filesystems which use unnamed block devices. |
| 1192 | * |
| 1193 | * We now support a flag for forced unmount like the other 'big iron' |
| 1194 | * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD |
| 1195 | */ |
| 1196 | |
Heiko Carstens | bdc480e | 2009-01-14 14:14:12 +0100 | [diff] [blame] | 1197 | SYSCALL_DEFINE2(umount, char __user *, name, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 1199 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | int retval; |
Miklos Szeredi | db1f05b | 2010-02-10 12:15:53 +0100 | [diff] [blame] | 1201 | int lookup_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
Miklos Szeredi | db1f05b | 2010-02-10 12:15:53 +0100 | [diff] [blame] | 1203 | if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) |
| 1204 | return -EINVAL; |
| 1205 | |
| 1206 | if (!(flags & UMOUNT_NOFOLLOW)) |
| 1207 | lookup_flags |= LOOKUP_FOLLOW; |
| 1208 | |
| 1209 | retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | if (retval) |
| 1211 | goto out; |
| 1212 | retval = -EINVAL; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 1213 | if (path.dentry != path.mnt->mnt_root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | goto dput_and_out; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 1215 | if (!check_mnt(path.mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | goto dput_and_out; |
| 1217 | |
| 1218 | retval = -EPERM; |
| 1219 | if (!capable(CAP_SYS_ADMIN)) |
| 1220 | goto dput_and_out; |
| 1221 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 1222 | retval = do_umount(path.mnt, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | dput_and_out: |
Jan Blunck | 429731b | 2008-02-14 19:34:31 -0800 | [diff] [blame] | 1224 | /* we mustn't call path_put() as that would clear mnt_expiry_mark */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 1225 | dput(path.dentry); |
| 1226 | mntput_no_expire(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | out: |
| 1228 | return retval; |
| 1229 | } |
| 1230 | |
| 1231 | #ifdef __ARCH_WANT_SYS_OLDUMOUNT |
| 1232 | |
| 1233 | /* |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1234 | * The 2.0 compatible umount. No flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | */ |
Heiko Carstens | bdc480e | 2009-01-14 14:14:12 +0100 | [diff] [blame] | 1236 | SYSCALL_DEFINE1(oldumount, char __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1238 | return sys_umount(name, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | #endif |
| 1242 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1243 | static int mount_is_safe(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | { |
| 1245 | if (capable(CAP_SYS_ADMIN)) |
| 1246 | return 0; |
| 1247 | return -EPERM; |
| 1248 | #ifdef notyet |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1249 | if (S_ISLNK(path->dentry->d_inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | return -EPERM; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1251 | if (path->dentry->d_inode->i_mode & S_ISVTX) { |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 1252 | if (current_uid() != path->dentry->d_inode->i_uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | return -EPERM; |
| 1254 | } |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1255 | if (inode_permission(path->dentry->d_inode, MAY_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | return -EPERM; |
| 1257 | return 0; |
| 1258 | #endif |
| 1259 | } |
| 1260 | |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1261 | struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry, |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 1262 | int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | { |
| 1264 | struct vfsmount *res, *p, *q, *r, *s; |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 1265 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1267 | if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) |
| 1268 | return NULL; |
| 1269 | |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 1270 | res = q = clone_mnt(mnt, dentry, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | if (!q) |
| 1272 | goto Enomem; |
| 1273 | q->mnt_mountpoint = mnt->mnt_mountpoint; |
| 1274 | |
| 1275 | p = mnt; |
Domen Puncer | fdadd65 | 2005-09-10 00:27:07 -0700 | [diff] [blame] | 1276 | list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { |
Jan Blunck | 7ec02ef | 2008-04-29 00:59:40 -0700 | [diff] [blame] | 1277 | if (!is_subdir(r->mnt_mountpoint, dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | continue; |
| 1279 | |
| 1280 | for (s = r; s; s = next_mnt(s, r)) { |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1281 | if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { |
| 1282 | s = skip_mnt_tree(s); |
| 1283 | continue; |
| 1284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | while (p != s->mnt_parent) { |
| 1286 | p = p->mnt_parent; |
| 1287 | q = q->mnt_parent; |
| 1288 | } |
| 1289 | p = s; |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 1290 | path.mnt = q; |
| 1291 | path.dentry = p->mnt_mountpoint; |
Ram Pai | 36341f6 | 2005-11-07 17:17:22 -0500 | [diff] [blame] | 1292 | q = clone_mnt(p, p->mnt_root, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | if (!q) |
| 1294 | goto Enomem; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1295 | br_write_lock(vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | list_add_tail(&q->mnt_list, &res->mnt_list); |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 1297 | attach_mnt(q, &path); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1298 | br_write_unlock(vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | return res; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1302 | Enomem: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | if (res) { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1304 | LIST_HEAD(umount_list); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1305 | br_write_lock(vfsmount_lock); |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1306 | umount_tree(res, 0, &umount_list); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1307 | br_write_unlock(vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1308 | release_mounts(&umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | } |
| 1310 | return NULL; |
| 1311 | } |
| 1312 | |
Al Viro | 589ff87 | 2009-04-18 03:28:19 -0400 | [diff] [blame] | 1313 | struct vfsmount *collect_mounts(struct path *path) |
Al Viro | 8aec080 | 2007-06-07 12:20:32 -0400 | [diff] [blame] | 1314 | { |
| 1315 | struct vfsmount *tree; |
Al Viro | 1a60a28 | 2008-03-22 16:19:49 -0400 | [diff] [blame] | 1316 | down_write(&namespace_sem); |
Al Viro | 589ff87 | 2009-04-18 03:28:19 -0400 | [diff] [blame] | 1317 | tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE); |
Al Viro | 1a60a28 | 2008-03-22 16:19:49 -0400 | [diff] [blame] | 1318 | up_write(&namespace_sem); |
Al Viro | 8aec080 | 2007-06-07 12:20:32 -0400 | [diff] [blame] | 1319 | return tree; |
| 1320 | } |
| 1321 | |
| 1322 | void drop_collected_mounts(struct vfsmount *mnt) |
| 1323 | { |
| 1324 | LIST_HEAD(umount_list); |
Al Viro | 1a60a28 | 2008-03-22 16:19:49 -0400 | [diff] [blame] | 1325 | down_write(&namespace_sem); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1326 | br_write_lock(vfsmount_lock); |
Al Viro | 8aec080 | 2007-06-07 12:20:32 -0400 | [diff] [blame] | 1327 | umount_tree(mnt, 0, &umount_list); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1328 | br_write_unlock(vfsmount_lock); |
Al Viro | 1a60a28 | 2008-03-22 16:19:49 -0400 | [diff] [blame] | 1329 | up_write(&namespace_sem); |
Al Viro | 8aec080 | 2007-06-07 12:20:32 -0400 | [diff] [blame] | 1330 | release_mounts(&umount_list); |
| 1331 | } |
| 1332 | |
Al Viro | 1f70713 | 2010-01-30 22:51:25 -0500 | [diff] [blame] | 1333 | int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, |
| 1334 | struct vfsmount *root) |
| 1335 | { |
| 1336 | struct vfsmount *mnt; |
| 1337 | int res = f(root, arg); |
| 1338 | if (res) |
| 1339 | return res; |
| 1340 | list_for_each_entry(mnt, &root->mnt_list, mnt_list) { |
| 1341 | res = f(mnt, arg); |
| 1342 | if (res) |
| 1343 | return res; |
| 1344 | } |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1348 | static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end) |
| 1349 | { |
| 1350 | struct vfsmount *p; |
| 1351 | |
| 1352 | for (p = mnt; p != end; p = next_mnt(p, mnt)) { |
| 1353 | if (p->mnt_group_id && !IS_MNT_SHARED(p)) |
| 1354 | mnt_release_group_id(p); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | static int invent_group_ids(struct vfsmount *mnt, bool recurse) |
| 1359 | { |
| 1360 | struct vfsmount *p; |
| 1361 | |
| 1362 | for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { |
| 1363 | if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { |
| 1364 | int err = mnt_alloc_group_id(p); |
| 1365 | if (err) { |
| 1366 | cleanup_group_ids(mnt, p); |
| 1367 | return err; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1375 | /* |
| 1376 | * @source_mnt : mount tree to be attached |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1377 | * @nd : place the mount tree @source_mnt is attached |
| 1378 | * @parent_nd : if non-null, detach the source_mnt from its parent and |
| 1379 | * store the parent mount and mountpoint dentry. |
| 1380 | * (done when source_mnt is moved) |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1381 | * |
| 1382 | * NOTE: in the table below explains the semantics when a source mount |
| 1383 | * of a given type is attached to a destination mount of a given type. |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1384 | * --------------------------------------------------------------------------- |
| 1385 | * | BIND MOUNT OPERATION | |
| 1386 | * |************************************************************************** |
| 1387 | * | source-->| shared | private | slave | unbindable | |
| 1388 | * | dest | | | | | |
| 1389 | * | | | | | | | |
| 1390 | * | v | | | | | |
| 1391 | * |************************************************************************** |
| 1392 | * | shared | shared (++) | shared (+) | shared(+++)| invalid | |
| 1393 | * | | | | | | |
| 1394 | * |non-shared| shared (+) | private | slave (*) | invalid | |
| 1395 | * *************************************************************************** |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1396 | * A bind operation clones the source mount and mounts the clone on the |
| 1397 | * destination mount. |
| 1398 | * |
| 1399 | * (++) the cloned mount is propagated to all the mounts in the propagation |
| 1400 | * tree of the destination mount and the cloned mount is added to |
| 1401 | * the peer group of the source mount. |
| 1402 | * (+) the cloned mount is created under the destination mount and is marked |
| 1403 | * as shared. The cloned mount is added to the peer group of the source |
| 1404 | * mount. |
Ram Pai | 5afe002 | 2005-11-07 17:21:01 -0500 | [diff] [blame] | 1405 | * (+++) the mount is propagated to all the mounts in the propagation tree |
| 1406 | * of the destination mount and the cloned mount is made slave |
| 1407 | * of the same master as that of the source mount. The cloned mount |
| 1408 | * is marked as 'shared and slave'. |
| 1409 | * (*) the cloned mount is made a slave of the same master as that of the |
| 1410 | * source mount. |
| 1411 | * |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1412 | * --------------------------------------------------------------------------- |
| 1413 | * | MOVE MOUNT OPERATION | |
| 1414 | * |************************************************************************** |
| 1415 | * | source-->| shared | private | slave | unbindable | |
| 1416 | * | dest | | | | | |
| 1417 | * | | | | | | | |
| 1418 | * | v | | | | | |
| 1419 | * |************************************************************************** |
| 1420 | * | shared | shared (+) | shared (+) | shared(+++) | invalid | |
| 1421 | * | | | | | | |
| 1422 | * |non-shared| shared (+*) | private | slave (*) | unbindable | |
| 1423 | * *************************************************************************** |
Ram Pai | 5afe002 | 2005-11-07 17:21:01 -0500 | [diff] [blame] | 1424 | * |
| 1425 | * (+) the mount is moved to the destination. And is then propagated to |
| 1426 | * all the mounts in the propagation tree of the destination mount. |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1427 | * (+*) the mount is moved to the destination. |
Ram Pai | 5afe002 | 2005-11-07 17:21:01 -0500 | [diff] [blame] | 1428 | * (+++) the mount is moved to the destination and is then propagated to |
| 1429 | * all the mounts belonging to the destination mount's propagation tree. |
| 1430 | * the mount is marked as 'shared and slave'. |
| 1431 | * (*) the mount continues to be a slave at the new location. |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1432 | * |
| 1433 | * if the source mount is a tree, the operations explained above is |
| 1434 | * applied to each mount in the tree. |
| 1435 | * Must be called without spinlocks held, since this function can sleep |
| 1436 | * in allocations. |
| 1437 | */ |
| 1438 | static int attach_recursive_mnt(struct vfsmount *source_mnt, |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 1439 | struct path *path, struct path *parent_path) |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1440 | { |
| 1441 | LIST_HEAD(tree_list); |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 1442 | struct vfsmount *dest_mnt = path->mnt; |
| 1443 | struct dentry *dest_dentry = path->dentry; |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1444 | struct vfsmount *child, *p; |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1445 | int err; |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1446 | |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1447 | if (IS_MNT_SHARED(dest_mnt)) { |
| 1448 | err = invent_group_ids(source_mnt, true); |
| 1449 | if (err) |
| 1450 | goto out; |
| 1451 | } |
| 1452 | err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); |
| 1453 | if (err) |
| 1454 | goto out_cleanup_ids; |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1455 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1456 | br_write_lock(vfsmount_lock); |
Al Viro | df1a1ad | 2010-01-16 12:57:40 -0500 | [diff] [blame] | 1457 | |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1458 | if (IS_MNT_SHARED(dest_mnt)) { |
| 1459 | for (p = source_mnt; p; p = next_mnt(p, source_mnt)) |
| 1460 | set_mnt_shared(p); |
| 1461 | } |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 1462 | if (parent_path) { |
| 1463 | detach_mnt(source_mnt, parent_path); |
| 1464 | attach_mnt(source_mnt, path); |
Al Viro | e5d67f0 | 2009-04-07 12:15:39 -0400 | [diff] [blame] | 1465 | touch_mnt_namespace(parent_path->mnt->mnt_ns); |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1466 | } else { |
| 1467 | mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); |
| 1468 | commit_tree(source_mnt); |
| 1469 | } |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1470 | |
| 1471 | list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { |
| 1472 | list_del_init(&child->mnt_hash); |
| 1473 | commit_tree(child); |
| 1474 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1475 | br_write_unlock(vfsmount_lock); |
| 1476 | |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1477 | return 0; |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1478 | |
| 1479 | out_cleanup_ids: |
| 1480 | if (IS_MNT_SHARED(dest_mnt)) |
| 1481 | cleanup_group_ids(source_mnt, NULL); |
| 1482 | out: |
| 1483 | return err; |
Ram Pai | b90fa9a | 2005-11-07 17:19:50 -0500 | [diff] [blame] | 1484 | } |
| 1485 | |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 1486 | static int graft_tree(struct vfsmount *mnt, struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | { |
| 1488 | int err; |
| 1489 | if (mnt->mnt_sb->s_flags & MS_NOUSER) |
| 1490 | return -EINVAL; |
| 1491 | |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 1492 | if (S_ISDIR(path->dentry->d_inode->i_mode) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | S_ISDIR(mnt->mnt_root->d_inode->i_mode)) |
| 1494 | return -ENOTDIR; |
| 1495 | |
| 1496 | err = -ENOENT; |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 1497 | mutex_lock(&path->dentry->d_inode->i_mutex); |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 1498 | if (cant_mount(path->dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | goto out_unlock; |
| 1500 | |
Alexey Dobriyan | f3da392 | 2009-05-04 03:32:03 +0400 | [diff] [blame] | 1501 | if (!d_unlinked(path->dentry)) |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 1502 | err = attach_recursive_mnt(mnt, path, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | out_unlock: |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 1504 | mutex_unlock(&path->dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | return err; |
| 1506 | } |
| 1507 | |
| 1508 | /* |
Valerie Aurora | 7a2e8a8 | 2010-08-26 11:07:22 -0700 | [diff] [blame] | 1509 | * Sanity check the flags to change_mnt_propagation. |
| 1510 | */ |
| 1511 | |
| 1512 | static int flags_to_propagation_type(int flags) |
| 1513 | { |
| 1514 | int type = flags & ~MS_REC; |
| 1515 | |
| 1516 | /* Fail if any non-propagation flags are set */ |
| 1517 | if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) |
| 1518 | return 0; |
| 1519 | /* Only one propagation flag should be set */ |
| 1520 | if (!is_power_of_2(type)) |
| 1521 | return 0; |
| 1522 | return type; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1526 | * recursively change the type of the mountpoint. |
| 1527 | */ |
Al Viro | 0a0d8a4 | 2008-08-02 00:55:27 -0400 | [diff] [blame] | 1528 | static int do_change_type(struct path *path, int flag) |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1529 | { |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1530 | struct vfsmount *m, *mnt = path->mnt; |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1531 | int recurse = flag & MS_REC; |
Valerie Aurora | 7a2e8a8 | 2010-08-26 11:07:22 -0700 | [diff] [blame] | 1532 | int type; |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1533 | int err = 0; |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1534 | |
Miklos Szeredi | ee6f958 | 2007-05-08 00:30:40 -0700 | [diff] [blame] | 1535 | if (!capable(CAP_SYS_ADMIN)) |
| 1536 | return -EPERM; |
| 1537 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1538 | if (path->dentry != path->mnt->mnt_root) |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1539 | return -EINVAL; |
| 1540 | |
Valerie Aurora | 7a2e8a8 | 2010-08-26 11:07:22 -0700 | [diff] [blame] | 1541 | type = flags_to_propagation_type(flag); |
| 1542 | if (!type) |
| 1543 | return -EINVAL; |
| 1544 | |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1545 | down_write(&namespace_sem); |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1546 | if (type == MS_SHARED) { |
| 1547 | err = invent_group_ids(mnt, recurse); |
| 1548 | if (err) |
| 1549 | goto out_unlock; |
| 1550 | } |
| 1551 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1552 | br_write_lock(vfsmount_lock); |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1553 | for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) |
| 1554 | change_mnt_propagation(m, type); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1555 | br_write_unlock(vfsmount_lock); |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1556 | |
| 1557 | out_unlock: |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1558 | up_write(&namespace_sem); |
Miklos Szeredi | 719f5d7 | 2008-03-27 13:06:23 +0100 | [diff] [blame] | 1559 | return err; |
Ram Pai | 07b2088 | 2005-11-07 17:19:07 -0500 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | * do loopback mount. |
| 1564 | */ |
Al Viro | 0a0d8a4 | 2008-08-02 00:55:27 -0400 | [diff] [blame] | 1565 | static int do_loopback(struct path *path, char *old_name, |
Eric Sandeen | 2dafe1c | 2008-02-08 04:22:12 -0800 | [diff] [blame] | 1566 | int recurse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | { |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1568 | struct path old_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | struct vfsmount *mnt = NULL; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1570 | int err = mount_is_safe(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (err) |
| 1572 | return err; |
| 1573 | if (!old_name || !*old_name) |
| 1574 | return -EINVAL; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1575 | err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | if (err) |
| 1577 | return err; |
| 1578 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1579 | down_write(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | err = -EINVAL; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1581 | if (IS_MNT_UNBINDABLE(old_path.mnt)) |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1582 | goto out; |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1583 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1584 | if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt)) |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 1585 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 1587 | err = -ENOMEM; |
| 1588 | if (recurse) |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1589 | mnt = copy_tree(old_path.mnt, old_path.dentry, 0); |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 1590 | else |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1591 | mnt = clone_mnt(old_path.mnt, old_path.dentry, 0); |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 1592 | |
| 1593 | if (!mnt) |
| 1594 | goto out; |
| 1595 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1596 | err = graft_tree(mnt, path); |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 1597 | if (err) { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1598 | LIST_HEAD(umount_list); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1599 | |
| 1600 | br_write_lock(vfsmount_lock); |
Ram Pai | a05964f | 2005-11-07 17:20:17 -0500 | [diff] [blame] | 1601 | umount_tree(mnt, 0, &umount_list); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1602 | br_write_unlock(vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 1603 | release_mounts(&umount_list); |
Ram Pai | 5b83d2c5c | 2005-11-07 17:16:29 -0500 | [diff] [blame] | 1604 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 1606 | out: |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1607 | up_write(&namespace_sem); |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1608 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | return err; |
| 1610 | } |
| 1611 | |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 1612 | static int change_mount_flags(struct vfsmount *mnt, int ms_flags) |
| 1613 | { |
| 1614 | int error = 0; |
| 1615 | int readonly_request = 0; |
| 1616 | |
| 1617 | if (ms_flags & MS_RDONLY) |
| 1618 | readonly_request = 1; |
| 1619 | if (readonly_request == __mnt_is_readonly(mnt)) |
| 1620 | return 0; |
| 1621 | |
| 1622 | if (readonly_request) |
| 1623 | error = mnt_make_readonly(mnt); |
| 1624 | else |
| 1625 | __mnt_unmake_readonly(mnt); |
| 1626 | return error; |
| 1627 | } |
| 1628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | /* |
| 1630 | * change filesystem flags. dir should be a physical root of filesystem. |
| 1631 | * If you've mounted a non-root directory somewhere and want to do remount |
| 1632 | * on it - tough luck. |
| 1633 | */ |
Al Viro | 0a0d8a4 | 2008-08-02 00:55:27 -0400 | [diff] [blame] | 1634 | static int do_remount(struct path *path, int flags, int mnt_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | void *data) |
| 1636 | { |
| 1637 | int err; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1638 | struct super_block *sb = path->mnt->mnt_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | |
| 1640 | if (!capable(CAP_SYS_ADMIN)) |
| 1641 | return -EPERM; |
| 1642 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1643 | if (!check_mnt(path->mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | return -EINVAL; |
| 1645 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1646 | if (path->dentry != path->mnt->mnt_root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | return -EINVAL; |
| 1648 | |
| 1649 | down_write(&sb->s_umount); |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 1650 | if (flags & MS_BIND) |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1651 | err = change_mount_flags(path->mnt, flags); |
Al Viro | 4aa98cf | 2009-05-08 13:36:58 -0400 | [diff] [blame] | 1652 | else |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 1653 | err = do_remount_sb(sb, flags, data, 0); |
Al Viro | 7b43a79 | 2010-01-16 13:01:26 -0500 | [diff] [blame] | 1654 | if (!err) { |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1655 | br_write_lock(vfsmount_lock); |
Valerie Aurora | 495d6c9 | 2010-01-26 14:20:47 -0500 | [diff] [blame] | 1656 | mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1657 | path->mnt->mnt_flags = mnt_flags; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1658 | br_write_unlock(vfsmount_lock); |
Al Viro | 7b43a79 | 2010-01-16 13:01:26 -0500 | [diff] [blame] | 1659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | up_write(&sb->s_umount); |
Dan Williams | 0e55a7c | 2008-09-26 19:01:20 -0700 | [diff] [blame] | 1661 | if (!err) { |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1662 | br_write_lock(vfsmount_lock); |
Dan Williams | 0e55a7c | 2008-09-26 19:01:20 -0700 | [diff] [blame] | 1663 | touch_mnt_namespace(path->mnt->mnt_ns); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1664 | br_write_unlock(vfsmount_lock); |
Dan Williams | 0e55a7c | 2008-09-26 19:01:20 -0700 | [diff] [blame] | 1665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | return err; |
| 1667 | } |
| 1668 | |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1669 | static inline int tree_contains_unbindable(struct vfsmount *mnt) |
| 1670 | { |
| 1671 | struct vfsmount *p; |
| 1672 | for (p = mnt; p; p = next_mnt(p, mnt)) { |
| 1673 | if (IS_MNT_UNBINDABLE(p)) |
| 1674 | return 1; |
| 1675 | } |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
Al Viro | 0a0d8a4 | 2008-08-02 00:55:27 -0400 | [diff] [blame] | 1679 | static int do_move_mount(struct path *path, char *old_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | { |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1681 | struct path old_path, parent_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | struct vfsmount *p; |
| 1683 | int err = 0; |
| 1684 | if (!capable(CAP_SYS_ADMIN)) |
| 1685 | return -EPERM; |
| 1686 | if (!old_name || !*old_name) |
| 1687 | return -EINVAL; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1688 | err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | if (err) |
| 1690 | return err; |
| 1691 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1692 | down_write(&namespace_sem); |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1693 | while (d_mountpoint(path->dentry) && |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1694 | follow_down(path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | ; |
| 1696 | err = -EINVAL; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1697 | if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | goto out; |
| 1699 | |
| 1700 | err = -ENOENT; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1701 | mutex_lock(&path->dentry->d_inode->i_mutex); |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 1702 | if (cant_mount(path->dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | goto out1; |
| 1704 | |
Alexey Dobriyan | f3da392 | 2009-05-04 03:32:03 +0400 | [diff] [blame] | 1705 | if (d_unlinked(path->dentry)) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1706 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
| 1708 | err = -EINVAL; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1709 | if (old_path.dentry != old_path.mnt->mnt_root) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1710 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1712 | if (old_path.mnt == old_path.mnt->mnt_parent) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1713 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1715 | if (S_ISDIR(path->dentry->d_inode->i_mode) != |
| 1716 | S_ISDIR(old_path.dentry->d_inode->i_mode)) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1717 | goto out1; |
| 1718 | /* |
| 1719 | * Don't move a mount residing in a shared parent. |
| 1720 | */ |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1721 | if (old_path.mnt->mnt_parent && |
| 1722 | IS_MNT_SHARED(old_path.mnt->mnt_parent)) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1723 | goto out1; |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1724 | /* |
| 1725 | * Don't move a mount tree containing unbindable mounts to a destination |
| 1726 | * mount which is shared. |
| 1727 | */ |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1728 | if (IS_MNT_SHARED(path->mnt) && |
| 1729 | tree_contains_unbindable(old_path.mnt)) |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 1730 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | err = -ELOOP; |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1732 | for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent) |
| 1733 | if (p == old_path.mnt) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1734 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1736 | err = attach_recursive_mnt(old_path.mnt, path, &parent_path); |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1737 | if (err) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 1738 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
| 1740 | /* if the mount is moved, it should no longer be expire |
| 1741 | * automatically */ |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1742 | list_del_init(&old_path.mnt->mnt_expire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | out1: |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1744 | mutex_unlock(&path->dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | out: |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1746 | up_write(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | if (!err) |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 1748 | path_put(&parent_path); |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1749 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | return err; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * create a new mount for userspace and request it to be added into the |
| 1755 | * namespace's tree |
| 1756 | */ |
Al Viro | 0a0d8a4 | 2008-08-02 00:55:27 -0400 | [diff] [blame] | 1757 | static int do_new_mount(struct path *path, char *type, int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | int mnt_flags, char *name, void *data) |
| 1759 | { |
| 1760 | struct vfsmount *mnt; |
| 1761 | |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 1762 | if (!type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | return -EINVAL; |
| 1764 | |
| 1765 | /* we need capabilities... */ |
| 1766 | if (!capable(CAP_SYS_ADMIN)) |
| 1767 | return -EPERM; |
| 1768 | |
| 1769 | mnt = do_kern_mount(type, flags, name, data); |
| 1770 | if (IS_ERR(mnt)) |
| 1771 | return PTR_ERR(mnt); |
| 1772 | |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 1773 | return do_add_mount(mnt, path, mnt_flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * add a mount into a namespace's mount tree |
| 1778 | * - provide the option of adding the new mount to an expiration list |
| 1779 | */ |
Al Viro | 8d66bf5 | 2008-08-01 09:05:54 -0400 | [diff] [blame] | 1780 | int do_add_mount(struct vfsmount *newmnt, struct path *path, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | int mnt_flags, struct list_head *fslist) |
| 1782 | { |
| 1783 | int err; |
| 1784 | |
Al Viro | 8089352 | 2010-02-05 09:30:46 -0500 | [diff] [blame] | 1785 | mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); |
Al Viro | 27d55f1 | 2010-01-16 13:07:36 -0500 | [diff] [blame] | 1786 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1787 | down_write(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | /* Something was mounted here while we slept */ |
Al Viro | 8d66bf5 | 2008-08-01 09:05:54 -0400 | [diff] [blame] | 1789 | while (d_mountpoint(path->dentry) && |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1790 | follow_down(path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | ; |
| 1792 | err = -EINVAL; |
Al Viro | dd5cae6 | 2009-04-07 12:21:18 -0400 | [diff] [blame] | 1793 | if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | goto unlock; |
| 1795 | |
| 1796 | /* Refuse the same filesystem on the same mount point */ |
| 1797 | err = -EBUSY; |
Al Viro | 8d66bf5 | 2008-08-01 09:05:54 -0400 | [diff] [blame] | 1798 | if (path->mnt->mnt_sb == newmnt->mnt_sb && |
| 1799 | path->mnt->mnt_root == path->dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | goto unlock; |
| 1801 | |
| 1802 | err = -EINVAL; |
| 1803 | if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode)) |
| 1804 | goto unlock; |
| 1805 | |
| 1806 | newmnt->mnt_flags = mnt_flags; |
Al Viro | 8d66bf5 | 2008-08-01 09:05:54 -0400 | [diff] [blame] | 1807 | if ((err = graft_tree(newmnt, path))) |
Ram Pai | 5b83d2c5c | 2005-11-07 17:16:29 -0500 | [diff] [blame] | 1808 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | |
Al Viro | 6758f95 | 2008-03-22 16:14:30 -0400 | [diff] [blame] | 1810 | if (fslist) /* add to the specified expiration list */ |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 1811 | list_add_tail(&newmnt->mnt_expire, fslist); |
Al Viro | 6758f95 | 2008-03-22 16:14:30 -0400 | [diff] [blame] | 1812 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1813 | up_write(&namespace_sem); |
Ram Pai | 5b83d2c5c | 2005-11-07 17:16:29 -0500 | [diff] [blame] | 1814 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | |
| 1816 | unlock: |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 1817 | up_write(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | mntput(newmnt); |
| 1819 | return err; |
| 1820 | } |
| 1821 | |
| 1822 | EXPORT_SYMBOL_GPL(do_add_mount); |
| 1823 | |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1824 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | * process a list of expirable mountpoints with the intent of discarding any |
| 1826 | * mountpoints that aren't in use and haven't been touched since last we came |
| 1827 | * here |
| 1828 | */ |
| 1829 | void mark_mounts_for_expiry(struct list_head *mounts) |
| 1830 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | struct vfsmount *mnt, *next; |
| 1832 | LIST_HEAD(graveyard); |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1833 | LIST_HEAD(umounts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | |
| 1835 | if (list_empty(mounts)) |
| 1836 | return; |
| 1837 | |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1838 | down_write(&namespace_sem); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1839 | br_write_lock(vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
| 1841 | /* extract from the expiration list every vfsmount that matches the |
| 1842 | * following criteria: |
| 1843 | * - only referenced by its parent vfsmount |
| 1844 | * - still marked for expiry (marked on the last call here; marks are |
| 1845 | * cleared by mntput()) |
| 1846 | */ |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 1847 | list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | if (!xchg(&mnt->mnt_expiry_mark, 1) || |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1849 | propagate_mount_busy(mnt, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | continue; |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 1851 | list_move(&mnt->mnt_expire, &graveyard); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | } |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1853 | while (!list_empty(&graveyard)) { |
| 1854 | mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire); |
| 1855 | touch_mnt_namespace(mnt->mnt_ns); |
| 1856 | umount_tree(mnt, 1, &umounts); |
| 1857 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1858 | br_write_unlock(vfsmount_lock); |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1859 | up_write(&namespace_sem); |
| 1860 | |
| 1861 | release_mounts(&umounts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); |
| 1865 | |
| 1866 | /* |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1867 | * Ripoff of 'select_parent()' |
| 1868 | * |
| 1869 | * search the list of submounts for a given mountpoint, and move any |
| 1870 | * shrinkable submounts to the 'graveyard' list. |
| 1871 | */ |
| 1872 | static int select_submounts(struct vfsmount *parent, struct list_head *graveyard) |
| 1873 | { |
| 1874 | struct vfsmount *this_parent = parent; |
| 1875 | struct list_head *next; |
| 1876 | int found = 0; |
| 1877 | |
| 1878 | repeat: |
| 1879 | next = this_parent->mnt_mounts.next; |
| 1880 | resume: |
| 1881 | while (next != &this_parent->mnt_mounts) { |
| 1882 | struct list_head *tmp = next; |
| 1883 | struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child); |
| 1884 | |
| 1885 | next = tmp->next; |
| 1886 | if (!(mnt->mnt_flags & MNT_SHRINKABLE)) |
| 1887 | continue; |
| 1888 | /* |
| 1889 | * Descend a level if the d_mounts list is non-empty. |
| 1890 | */ |
| 1891 | if (!list_empty(&mnt->mnt_mounts)) { |
| 1892 | this_parent = mnt; |
| 1893 | goto repeat; |
| 1894 | } |
| 1895 | |
| 1896 | if (!propagate_mount_busy(mnt, 1)) { |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1897 | list_move_tail(&mnt->mnt_expire, graveyard); |
| 1898 | found++; |
| 1899 | } |
| 1900 | } |
| 1901 | /* |
| 1902 | * All done at this level ... ascend and resume the search |
| 1903 | */ |
| 1904 | if (this_parent != parent) { |
| 1905 | next = this_parent->mnt_child.next; |
| 1906 | this_parent = this_parent->mnt_parent; |
| 1907 | goto resume; |
| 1908 | } |
| 1909 | return found; |
| 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * process a list of expirable mountpoints with the intent of discarding any |
| 1914 | * submounts of a specific parent mountpoint |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 1915 | * |
| 1916 | * vfsmount_lock must be held for write |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1917 | */ |
Al Viro | c35038b | 2008-03-22 00:46:23 -0400 | [diff] [blame] | 1918 | static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts) |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1919 | { |
| 1920 | LIST_HEAD(graveyard); |
Al Viro | c35038b | 2008-03-22 00:46:23 -0400 | [diff] [blame] | 1921 | struct vfsmount *m; |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1922 | |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1923 | /* extract submounts of 'mountpoint' from the expiration list */ |
Al Viro | c35038b | 2008-03-22 00:46:23 -0400 | [diff] [blame] | 1924 | while (select_submounts(mnt, &graveyard)) { |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1925 | while (!list_empty(&graveyard)) { |
Al Viro | c35038b | 2008-03-22 00:46:23 -0400 | [diff] [blame] | 1926 | m = list_first_entry(&graveyard, struct vfsmount, |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1927 | mnt_expire); |
Eric W. Biederman | afef80b | 2008-11-12 13:26:54 -0800 | [diff] [blame] | 1928 | touch_mnt_namespace(m->mnt_ns); |
| 1929 | umount_tree(m, 1, umounts); |
Al Viro | bcc5c7d | 2008-03-22 00:21:53 -0400 | [diff] [blame] | 1930 | } |
| 1931 | } |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1932 | } |
| 1933 | |
Trond Myklebust | 5528f911 | 2006-06-09 09:34:17 -0400 | [diff] [blame] | 1934 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | * Some copy_from_user() implementations do not return the exact number of |
| 1936 | * bytes remaining to copy on a fault. But copy_mount_options() requires that. |
| 1937 | * Note that this function differs from copy_from_user() in that it will oops |
| 1938 | * on bad values of `to', rather than returning a short copy. |
| 1939 | */ |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1940 | static long exact_copy_from_user(void *to, const void __user * from, |
| 1941 | unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | { |
| 1943 | char *t = to; |
| 1944 | const char __user *f = from; |
| 1945 | char c; |
| 1946 | |
| 1947 | if (!access_ok(VERIFY_READ, from, n)) |
| 1948 | return n; |
| 1949 | |
| 1950 | while (n) { |
| 1951 | if (__get_user(c, f)) { |
| 1952 | memset(t, 0, n); |
| 1953 | break; |
| 1954 | } |
| 1955 | *t++ = c; |
| 1956 | f++; |
| 1957 | n--; |
| 1958 | } |
| 1959 | return n; |
| 1960 | } |
| 1961 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1962 | int copy_mount_options(const void __user * data, unsigned long *where) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | { |
| 1964 | int i; |
| 1965 | unsigned long page; |
| 1966 | unsigned long size; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | *where = 0; |
| 1969 | if (!data) |
| 1970 | return 0; |
| 1971 | |
| 1972 | if (!(page = __get_free_page(GFP_KERNEL))) |
| 1973 | return -ENOMEM; |
| 1974 | |
| 1975 | /* We only care that *some* data at the address the user |
| 1976 | * gave us is valid. Just in case, we'll zero |
| 1977 | * the remainder of the page. |
| 1978 | */ |
| 1979 | /* copy_from_user cannot cross TASK_SIZE ! */ |
| 1980 | size = TASK_SIZE - (unsigned long)data; |
| 1981 | if (size > PAGE_SIZE) |
| 1982 | size = PAGE_SIZE; |
| 1983 | |
| 1984 | i = size - exact_copy_from_user((void *)page, data, size); |
| 1985 | if (!i) { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1986 | free_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | return -EFAULT; |
| 1988 | } |
| 1989 | if (i != PAGE_SIZE) |
| 1990 | memset((char *)page + i, 0, PAGE_SIZE - i); |
| 1991 | *where = page; |
| 1992 | return 0; |
| 1993 | } |
| 1994 | |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 1995 | int copy_mount_string(const void __user *data, char **where) |
| 1996 | { |
| 1997 | char *tmp; |
| 1998 | |
| 1999 | if (!data) { |
| 2000 | *where = NULL; |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | tmp = strndup_user(data, PAGE_SIZE); |
| 2005 | if (IS_ERR(tmp)) |
| 2006 | return PTR_ERR(tmp); |
| 2007 | |
| 2008 | *where = tmp; |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | /* |
| 2013 | * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to |
| 2014 | * be given to the mount() call (ie: read-only, no-dev, no-suid etc). |
| 2015 | * |
| 2016 | * data is a (void *) that can point to any structure up to |
| 2017 | * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent |
| 2018 | * information (or be NULL). |
| 2019 | * |
| 2020 | * Pre-0.97 versions of mount() didn't have a flags word. |
| 2021 | * When the flags word was introduced its top half was required |
| 2022 | * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. |
| 2023 | * Therefore, if this magic number is present, it carries no information |
| 2024 | * and must be discarded. |
| 2025 | */ |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 2026 | long do_mount(char *dev_name, char *dir_name, char *type_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | unsigned long flags, void *data_page) |
| 2028 | { |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 2029 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | int retval = 0; |
| 2031 | int mnt_flags = 0; |
| 2032 | |
| 2033 | /* Discard magic */ |
| 2034 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) |
| 2035 | flags &= ~MS_MGC_MSK; |
| 2036 | |
| 2037 | /* Basic sanity checks */ |
| 2038 | |
| 2039 | if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) |
| 2040 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
| 2042 | if (data_page) |
| 2043 | ((char *)data_page)[PAGE_SIZE - 1] = 0; |
| 2044 | |
Tetsuo Handa | a27ab9f2 | 2009-10-04 21:49:49 +0900 | [diff] [blame] | 2045 | /* ... and get the mountpoint */ |
| 2046 | retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); |
| 2047 | if (retval) |
| 2048 | return retval; |
| 2049 | |
| 2050 | retval = security_sb_mount(dev_name, &path, |
| 2051 | type_page, flags, data_page); |
| 2052 | if (retval) |
| 2053 | goto dput_out; |
| 2054 | |
Andi Kleen | 613cbe3 | 2009-04-19 18:40:43 +0200 | [diff] [blame] | 2055 | /* Default to relatime unless overriden */ |
| 2056 | if (!(flags & MS_NOATIME)) |
| 2057 | mnt_flags |= MNT_RELATIME; |
Matthew Garrett | 0a1c01c | 2009-03-26 17:53:14 +0000 | [diff] [blame] | 2058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | /* Separate the per-mountpoint flags */ |
| 2060 | if (flags & MS_NOSUID) |
| 2061 | mnt_flags |= MNT_NOSUID; |
| 2062 | if (flags & MS_NODEV) |
| 2063 | mnt_flags |= MNT_NODEV; |
| 2064 | if (flags & MS_NOEXEC) |
| 2065 | mnt_flags |= MNT_NOEXEC; |
Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 2066 | if (flags & MS_NOATIME) |
| 2067 | mnt_flags |= MNT_NOATIME; |
| 2068 | if (flags & MS_NODIRATIME) |
| 2069 | mnt_flags |= MNT_NODIRATIME; |
Matthew Garrett | d0adde5 | 2009-03-26 17:49:56 +0000 | [diff] [blame] | 2070 | if (flags & MS_STRICTATIME) |
| 2071 | mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); |
Dave Hansen | 2e4b7fc | 2008-02-15 14:38:00 -0800 | [diff] [blame] | 2072 | if (flags & MS_RDONLY) |
| 2073 | mnt_flags |= MNT_READONLY; |
Christoph Hellwig | fc33a7b | 2006-01-09 20:52:17 -0800 | [diff] [blame] | 2074 | |
Al Viro | 7a4dec5 | 2010-08-09 12:05:43 -0400 | [diff] [blame] | 2075 | flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | |
Matthew Garrett | d0adde5 | 2009-03-26 17:49:56 +0000 | [diff] [blame] | 2076 | MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | |
| 2077 | MS_STRICTATIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | if (flags & MS_REMOUNT) |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 2080 | retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | data_page); |
| 2082 | else if (flags & MS_BIND) |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 2083 | retval = do_loopback(&path, dev_name, flags & MS_REC); |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 2084 | else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 2085 | retval = do_change_type(&path, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | else if (flags & MS_MOVE) |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 2087 | retval = do_move_mount(&path, dev_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | else |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 2089 | retval = do_new_mount(&path, type_page, flags, mnt_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | dev_name, data_page); |
| 2091 | dput_out: |
Al Viro | 2d92ab3 | 2008-08-02 00:51:11 -0400 | [diff] [blame] | 2092 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | return retval; |
| 2094 | } |
| 2095 | |
Trond Myklebust | cf8d2c1 | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2096 | static struct mnt_namespace *alloc_mnt_ns(void) |
| 2097 | { |
| 2098 | struct mnt_namespace *new_ns; |
| 2099 | |
| 2100 | new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); |
| 2101 | if (!new_ns) |
| 2102 | return ERR_PTR(-ENOMEM); |
| 2103 | atomic_set(&new_ns->count, 1); |
| 2104 | new_ns->root = NULL; |
| 2105 | INIT_LIST_HEAD(&new_ns->list); |
| 2106 | init_waitqueue_head(&new_ns->poll); |
| 2107 | new_ns->event = 0; |
| 2108 | return new_ns; |
| 2109 | } |
| 2110 | |
JANAK DESAI | 741a295 | 2006-02-07 12:59:00 -0800 | [diff] [blame] | 2111 | /* |
| 2112 | * Allocate a new namespace structure and populate it with contents |
| 2113 | * copied from the namespace of the passed in task structure. |
| 2114 | */ |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 2115 | static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2116 | struct fs_struct *fs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | { |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2118 | struct mnt_namespace *new_ns; |
Al Viro | 7f2da1e | 2008-05-10 20:44:54 -0400 | [diff] [blame] | 2119 | struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | struct vfsmount *p, *q; |
| 2121 | |
Trond Myklebust | cf8d2c1 | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2122 | new_ns = alloc_mnt_ns(); |
| 2123 | if (IS_ERR(new_ns)) |
| 2124 | return new_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2126 | down_write(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | /* First pass: copy the tree topology */ |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2128 | new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root, |
Ram Pai | 9676f0c | 2005-11-07 17:21:20 -0500 | [diff] [blame] | 2129 | CL_COPY_ALL | CL_EXPIRE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | if (!new_ns->root) { |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2131 | up_write(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | kfree(new_ns); |
Julia Lawall | 5cc4a03 | 2008-12-01 14:34:51 -0800 | [diff] [blame] | 2133 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | } |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2135 | br_write_lock(vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | list_add_tail(&new_ns->list, &new_ns->root->mnt_list); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2137 | br_write_unlock(vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | |
| 2139 | /* |
| 2140 | * Second pass: switch the tsk->fs->* elements and mark new vfsmounts |
| 2141 | * as belonging to new namespace. We have already acquired a private |
| 2142 | * fs_struct, so tsk->fs->lock is not needed. |
| 2143 | */ |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2144 | p = mnt_ns->root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | q = new_ns->root; |
| 2146 | while (p) { |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2147 | q->mnt_ns = new_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | if (fs) { |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2149 | if (p == fs->root.mnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | rootmnt = p; |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2151 | fs->root.mnt = mntget(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | } |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2153 | if (p == fs->pwd.mnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | pwdmnt = p; |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 2155 | fs->pwd.mnt = mntget(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | } |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2158 | p = next_mnt(p, mnt_ns->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | q = next_mnt(q, new_ns->root); |
| 2160 | } |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2161 | up_write(&namespace_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | if (rootmnt) |
| 2164 | mntput(rootmnt); |
| 2165 | if (pwdmnt) |
| 2166 | mntput(pwdmnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
JANAK DESAI | 741a295 | 2006-02-07 12:59:00 -0800 | [diff] [blame] | 2168 | return new_ns; |
| 2169 | } |
| 2170 | |
Eric W. Biederman | 213dd26 | 2007-07-15 23:41:15 -0700 | [diff] [blame] | 2171 | struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 2172 | struct fs_struct *new_fs) |
JANAK DESAI | 741a295 | 2006-02-07 12:59:00 -0800 | [diff] [blame] | 2173 | { |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2174 | struct mnt_namespace *new_ns; |
JANAK DESAI | 741a295 | 2006-02-07 12:59:00 -0800 | [diff] [blame] | 2175 | |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 2176 | BUG_ON(!ns); |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2177 | get_mnt_ns(ns); |
JANAK DESAI | 741a295 | 2006-02-07 12:59:00 -0800 | [diff] [blame] | 2178 | |
| 2179 | if (!(flags & CLONE_NEWNS)) |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 2180 | return ns; |
JANAK DESAI | 741a295 | 2006-02-07 12:59:00 -0800 | [diff] [blame] | 2181 | |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 2182 | new_ns = dup_mnt_ns(ns, new_fs); |
JANAK DESAI | 741a295 | 2006-02-07 12:59:00 -0800 | [diff] [blame] | 2183 | |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2184 | put_mnt_ns(ns); |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 2185 | return new_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
Trond Myklebust | cf8d2c1 | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2188 | /** |
| 2189 | * create_mnt_ns - creates a private namespace and adds a root filesystem |
| 2190 | * @mnt: pointer to the new root filesystem mountpoint |
| 2191 | */ |
Linus Torvalds | a2770d8 | 2009-12-17 12:51:05 -0800 | [diff] [blame] | 2192 | struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt) |
Trond Myklebust | cf8d2c1 | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2193 | { |
| 2194 | struct mnt_namespace *new_ns; |
| 2195 | |
| 2196 | new_ns = alloc_mnt_ns(); |
| 2197 | if (!IS_ERR(new_ns)) { |
| 2198 | mnt->mnt_ns = new_ns; |
| 2199 | new_ns->root = mnt; |
| 2200 | list_add(&new_ns->list, &new_ns->root->mnt_list); |
| 2201 | } |
| 2202 | return new_ns; |
| 2203 | } |
Linus Torvalds | a2770d8 | 2009-12-17 12:51:05 -0800 | [diff] [blame] | 2204 | EXPORT_SYMBOL(create_mnt_ns); |
Trond Myklebust | cf8d2c1 | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2205 | |
Heiko Carstens | bdc480e | 2009-01-14 14:14:12 +0100 | [diff] [blame] | 2206 | SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, |
| 2207 | char __user *, type, unsigned long, flags, void __user *, data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | { |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 2209 | int ret; |
| 2210 | char *kernel_type; |
| 2211 | char *kernel_dir; |
| 2212 | char *kernel_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | unsigned long data_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 2215 | ret = copy_mount_string(type, &kernel_type); |
| 2216 | if (ret < 0) |
| 2217 | goto out_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 2219 | kernel_dir = getname(dir_name); |
| 2220 | if (IS_ERR(kernel_dir)) { |
| 2221 | ret = PTR_ERR(kernel_dir); |
| 2222 | goto out_dir; |
| 2223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 2225 | ret = copy_mount_string(dev_name, &kernel_dev); |
| 2226 | if (ret < 0) |
| 2227 | goto out_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 2229 | ret = copy_mount_options(data, &data_page); |
| 2230 | if (ret < 0) |
| 2231 | goto out_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 2233 | ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, |
| 2234 | (void *) data_page); |
| 2235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | free_page(data_page); |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 2237 | out_data: |
| 2238 | kfree(kernel_dev); |
| 2239 | out_dev: |
| 2240 | putname(kernel_dir); |
| 2241 | out_dir: |
| 2242 | kfree(kernel_type); |
| 2243 | out_type: |
| 2244 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | * pivot_root Semantics: |
| 2249 | * Moves the root file system of the current process to the directory put_old, |
| 2250 | * makes new_root as the new root file system of the current process, and sets |
| 2251 | * root/cwd of all processes which had them on the current root to new_root. |
| 2252 | * |
| 2253 | * Restrictions: |
| 2254 | * The new_root and put_old must be directories, and must not be on the |
| 2255 | * same file system as the current process root. The put_old must be |
| 2256 | * underneath new_root, i.e. adding a non-zero number of /.. to the string |
| 2257 | * pointed to by put_old must yield the same directory as new_root. No other |
| 2258 | * file system may be mounted on put_old. After all, new_root is a mountpoint. |
| 2259 | * |
Neil Brown | 4a0d11f | 2006-01-08 01:03:18 -0800 | [diff] [blame] | 2260 | * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. |
| 2261 | * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives |
| 2262 | * in this situation. |
| 2263 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | * Notes: |
| 2265 | * - we don't move root/cwd if they are not at the root (reason: if something |
| 2266 | * cared enough to change them, it's probably wrong to force them elsewhere) |
| 2267 | * - it's okay to pick a root that isn't the root of a file system, e.g. |
| 2268 | * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, |
| 2269 | * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root |
| 2270 | * first. |
| 2271 | */ |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 2272 | SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, |
| 2273 | const char __user *, put_old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | { |
| 2275 | struct vfsmount *tmp; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2276 | struct path new, old, parent_path, root_parent, root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | int error; |
| 2278 | |
| 2279 | if (!capable(CAP_SYS_ADMIN)) |
| 2280 | return -EPERM; |
| 2281 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2282 | error = user_path_dir(new_root, &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | if (error) |
| 2284 | goto out0; |
| 2285 | error = -EINVAL; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2286 | if (!check_mnt(new.mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | goto out1; |
| 2288 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2289 | error = user_path_dir(put_old, &old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | if (error) |
| 2291 | goto out1; |
| 2292 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2293 | error = security_sb_pivotroot(&old, &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | if (error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2295 | path_put(&old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | goto out1; |
| 2297 | } |
| 2298 | |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 2299 | get_fs_root(current->fs, &root); |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2300 | down_write(&namespace_sem); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2301 | mutex_lock(&old.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | error = -EINVAL; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2303 | if (IS_MNT_SHARED(old.mnt) || |
| 2304 | IS_MNT_SHARED(new.mnt->mnt_parent) || |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 2305 | IS_MNT_SHARED(root.mnt->mnt_parent)) |
Ram Pai | 2144440 | 2005-11-07 17:20:03 -0500 | [diff] [blame] | 2306 | goto out2; |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 2307 | if (!check_mnt(root.mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | goto out2; |
| 2309 | error = -ENOENT; |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 2310 | if (cant_mount(old.dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | goto out2; |
Alexey Dobriyan | f3da392 | 2009-05-04 03:32:03 +0400 | [diff] [blame] | 2312 | if (d_unlinked(new.dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | goto out2; |
Alexey Dobriyan | f3da392 | 2009-05-04 03:32:03 +0400 | [diff] [blame] | 2314 | if (d_unlinked(old.dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | goto out2; |
| 2316 | error = -EBUSY; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2317 | if (new.mnt == root.mnt || |
| 2318 | old.mnt == root.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | goto out2; /* loop, on the same file system */ |
| 2320 | error = -EINVAL; |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 2321 | if (root.mnt->mnt_root != root.dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | goto out2; /* not a mountpoint */ |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 2323 | if (root.mnt->mnt_parent == root.mnt) |
Miklos Szeredi | 0bb6fcc | 2005-09-06 15:19:36 -0700 | [diff] [blame] | 2324 | goto out2; /* not attached */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2325 | if (new.mnt->mnt_root != new.dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | goto out2; /* not a mountpoint */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2327 | if (new.mnt->mnt_parent == new.mnt) |
Miklos Szeredi | 0bb6fcc | 2005-09-06 15:19:36 -0700 | [diff] [blame] | 2328 | goto out2; /* not attached */ |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 2329 | /* make sure we can reach put_old from new_root */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2330 | tmp = old.mnt; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2331 | br_write_lock(vfsmount_lock); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2332 | if (tmp != new.mnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | for (;;) { |
| 2334 | if (tmp->mnt_parent == tmp) |
| 2335 | goto out3; /* already mounted on put_old */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2336 | if (tmp->mnt_parent == new.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | break; |
| 2338 | tmp = tmp->mnt_parent; |
| 2339 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2340 | if (!is_subdir(tmp->mnt_mountpoint, new.dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | goto out3; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2342 | } else if (!is_subdir(old.dentry, new.dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | goto out3; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2344 | detach_mnt(new.mnt, &parent_path); |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 2345 | detach_mnt(root.mnt, &root_parent); |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 2346 | /* mount old root on put_old */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2347 | attach_mnt(root.mnt, &old); |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 2348 | /* mount new_root on / */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2349 | attach_mnt(new.mnt, &root_parent); |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2350 | touch_mnt_namespace(current->nsproxy->mnt_ns); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2351 | br_write_unlock(vfsmount_lock); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2352 | chroot_fs_refs(&root, &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | error = 0; |
Al Viro | 1a39068 | 2008-03-21 20:48:19 -0400 | [diff] [blame] | 2354 | path_put(&root_parent); |
| 2355 | path_put(&parent_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | out2: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2357 | mutex_unlock(&old.dentry->d_inode->i_mutex); |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2358 | up_write(&namespace_sem); |
Al Viro | 8c3ee42 | 2008-03-22 18:00:39 -0400 | [diff] [blame] | 2359 | path_put(&root); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2360 | path_put(&old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | out1: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2362 | path_put(&new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | out0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | return error; |
| 2365 | out3: |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2366 | br_write_unlock(vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | goto out2; |
| 2368 | } |
| 2369 | |
| 2370 | static void __init init_mount_tree(void) |
| 2371 | { |
| 2372 | struct vfsmount *mnt; |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2373 | struct mnt_namespace *ns; |
Jan Blunck | ac748a0 | 2008-02-14 19:34:39 -0800 | [diff] [blame] | 2374 | struct path root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | |
| 2376 | mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); |
| 2377 | if (IS_ERR(mnt)) |
| 2378 | panic("Can't create rootfs"); |
Trond Myklebust | 3b22edc | 2009-06-23 17:29:49 -0400 | [diff] [blame] | 2379 | ns = create_mnt_ns(mnt); |
| 2380 | if (IS_ERR(ns)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | panic("Can't allocate initial namespace"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2383 | init_task.nsproxy->mnt_ns = ns; |
| 2384 | get_mnt_ns(ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | |
Jan Blunck | ac748a0 | 2008-02-14 19:34:39 -0800 | [diff] [blame] | 2386 | root.mnt = ns->root; |
| 2387 | root.dentry = ns->root->mnt_root; |
| 2388 | |
| 2389 | set_fs_pwd(current->fs, &root); |
| 2390 | set_fs_root(current->fs, &root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | } |
| 2392 | |
Denis Cheng | 74bf17c | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 2393 | void __init mnt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | { |
Eric Dumazet | 13f14b4 | 2008-02-06 01:37:57 -0800 | [diff] [blame] | 2395 | unsigned u; |
Randy Dunlap | 15a67dd | 2006-09-29 01:58:57 -0700 | [diff] [blame] | 2396 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2398 | init_rwsem(&namespace_sem); |
| 2399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2401 | 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 2403 | mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | |
| 2405 | if (!mount_hashtable) |
| 2406 | panic("Failed to allocate mount hash table\n"); |
| 2407 | |
Eric Dumazet | 13f14b4 | 2008-02-06 01:37:57 -0800 | [diff] [blame] | 2408 | printk("Mount-cache hash table entries: %lu\n", HASH_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | |
Eric Dumazet | 13f14b4 | 2008-02-06 01:37:57 -0800 | [diff] [blame] | 2410 | for (u = 0; u < HASH_SIZE; u++) |
| 2411 | INIT_LIST_HEAD(&mount_hashtable[u]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2413 | br_lock_init(vfsmount_lock); |
| 2414 | |
Randy Dunlap | 15a67dd | 2006-09-29 01:58:57 -0700 | [diff] [blame] | 2415 | err = sysfs_init(); |
| 2416 | if (err) |
| 2417 | printk(KERN_WARNING "%s: sysfs_init error: %d\n", |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 2418 | __func__, err); |
Greg Kroah-Hartman | 00d2666 | 2007-10-29 14:17:23 -0600 | [diff] [blame] | 2419 | fs_kobj = kobject_create_and_add("fs", NULL); |
| 2420 | if (!fs_kobj) |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 2421 | printk(KERN_WARNING "%s: kobj create error\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | init_rootfs(); |
| 2423 | init_mount_tree(); |
| 2424 | } |
| 2425 | |
Trond Myklebust | 616511d | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2426 | void put_mnt_ns(struct mnt_namespace *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 2428 | LIST_HEAD(umount_list); |
Trond Myklebust | 616511d | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2429 | |
Al Viro | d498b25 | 2010-02-05 02:21:06 -0500 | [diff] [blame] | 2430 | if (!atomic_dec_and_test(&ns->count)) |
Trond Myklebust | 616511d | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2431 | return; |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2432 | down_write(&namespace_sem); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2433 | br_write_lock(vfsmount_lock); |
Al Viro | d498b25 | 2010-02-05 02:21:06 -0500 | [diff] [blame] | 2434 | umount_tree(ns->root, 0, &umount_list); |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 2435 | br_write_unlock(vfsmount_lock); |
Ram Pai | 390c684 | 2005-11-07 17:17:51 -0500 | [diff] [blame] | 2436 | up_write(&namespace_sem); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame] | 2437 | release_mounts(&umount_list); |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 2438 | kfree(ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | } |
Trond Myklebust | cf8d2c1 | 2009-06-22 15:09:13 -0400 | [diff] [blame] | 2440 | EXPORT_SYMBOL(put_mnt_ns); |