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