]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/namespace.c
[patch 3/7] vfs: mountinfo: add mount ID
[net-next-2.6.git] / fs / namespace.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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>
15a67dd8 16#include <linux/kernel.h>
1da177e4
LT
17#include <linux/quotaops.h>
18#include <linux/acct.h>
16f7e0fe 19#include <linux/capability.h>
3d733633 20#include <linux/cpumask.h>
1da177e4 21#include <linux/module.h>
f20a9ead 22#include <linux/sysfs.h>
1da177e4 23#include <linux/seq_file.h>
6b3286ed 24#include <linux/mnt_namespace.h>
1da177e4
LT
25#include <linux/namei.h>
26#include <linux/security.h>
27#include <linux/mount.h>
07f3f05c 28#include <linux/ramfs.h>
13f14b4d 29#include <linux/log2.h>
73cd49ec 30#include <linux/idr.h>
1da177e4
LT
31#include <asm/uaccess.h>
32#include <asm/unistd.h>
07b20889 33#include "pnode.h"
948730b0 34#include "internal.h"
1da177e4 35
13f14b4d
ED
36#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
37#define HASH_SIZE (1UL << HASH_SHIFT)
38
1da177e4 39/* spinlock for vfsmount related operations, inplace of dcache_lock */
5addc5dd
AV
40__cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
41
42static int event;
73cd49ec 43static DEFINE_IDA(mnt_id_ida);
1da177e4 44
fa3536cc 45static struct list_head *mount_hashtable __read_mostly;
e18b890b 46static struct kmem_cache *mnt_cache __read_mostly;
390c6843 47static struct rw_semaphore namespace_sem;
1da177e4 48
f87fd4c2 49/* /sys/fs */
00d26666
GKH
50struct kobject *fs_kobj;
51EXPORT_SYMBOL_GPL(fs_kobj);
f87fd4c2 52
1da177e4
LT
53static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
54{
b58fed8b
RP
55 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
56 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
13f14b4d
ED
57 tmp = tmp + (tmp >> HASH_SHIFT);
58 return tmp & (HASH_SIZE - 1);
1da177e4
LT
59}
60
3d733633
DH
61#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
62
73cd49ec
MS
63/* allocation is serialized by namespace_sem */
64static int mnt_alloc_id(struct vfsmount *mnt)
65{
66 int res;
67
68retry:
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
79static 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
1da177e4
LT
86struct vfsmount *alloc_vfsmnt(const char *name)
87{
c3762229 88 struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
1da177e4 89 if (mnt) {
73cd49ec
MS
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
b58fed8b 98 atomic_set(&mnt->mnt_count, 1);
1da177e4
LT
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);
55e700b9 103 INIT_LIST_HEAD(&mnt->mnt_expire);
03e06e68 104 INIT_LIST_HEAD(&mnt->mnt_share);
a58b0eb8
RP
105 INIT_LIST_HEAD(&mnt->mnt_slave_list);
106 INIT_LIST_HEAD(&mnt->mnt_slave);
3d733633 107 atomic_set(&mnt->__mnt_writers, 0);
1da177e4 108 if (name) {
b58fed8b 109 int size = strlen(name) + 1;
1da177e4
LT
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
3d733633
DH
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 */
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 */
139int __mnt_is_readonly(struct vfsmount *mnt)
140{
2e4b7fcd
DH
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;
3d733633
DH
146}
147EXPORT_SYMBOL_GPL(__mnt_is_readonly);
148
149struct 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;
159static DEFINE_PER_CPU(struct mnt_writer, mnt_writers);
160
161static 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}
172fs_initcall(init_mnt_writers);
173
174static 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
185static 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 */
201static 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
8366025e
DH
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 */
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 */
228int mnt_want_write(struct vfsmount *mnt)
229{
3d733633
DH
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++;
241out:
242 spin_unlock(&cpu_writer->lock);
243 put_cpu_var(mnt_writers);
244 return ret;
8366025e
DH
245}
246EXPORT_SYMBOL_GPL(mnt_want_write);
247
3d733633
DH
248static 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 */
268static 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
8366025e
DH
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 */
309void mnt_drop_write(struct vfsmount *mnt)
310{
3d733633
DH
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);
8366025e
DH
342}
343EXPORT_SYMBOL_GPL(mnt_drop_write);
344
2e4b7fcd 345static int mnt_make_readonly(struct vfsmount *mnt)
8366025e 346{
3d733633
DH
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 /*
2e4b7fcd
DH
358 * nobody can do a successful mnt_want_write() with all
359 * of the counts in MNT_DENIED_WRITE and the locks held.
3d733633 360 */
2e4b7fcd
DH
361 spin_lock(&vfsmount_lock);
362 if (!ret)
363 mnt->mnt_flags |= MNT_READONLY;
364 spin_unlock(&vfsmount_lock);
3d733633
DH
365out:
366 unlock_mnt_writers();
367 return ret;
8366025e 368}
8366025e 369
2e4b7fcd
DH
370static 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
454e2398
DH
377int 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
384EXPORT_SYMBOL(simple_set_mnt);
385
1da177e4
LT
386void free_vfsmnt(struct vfsmount *mnt)
387{
388 kfree(mnt->mnt_devname);
73cd49ec 389 mnt_free_id(mnt);
1da177e4
LT
390 kmem_cache_free(mnt_cache, mnt);
391}
392
393/*
a05964f3
RP
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.
1da177e4 396 */
a05964f3
RP
397struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
398 int dir)
1da177e4 399{
b58fed8b
RP
400 struct list_head *head = mount_hashtable + hash(mnt, dentry);
401 struct list_head *tmp = head;
1da177e4
LT
402 struct vfsmount *p, *found = NULL;
403
1da177e4 404 for (;;) {
a05964f3 405 tmp = dir ? tmp->next : tmp->prev;
1da177e4
LT
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) {
a05964f3 411 found = p;
1da177e4
LT
412 break;
413 }
414 }
1da177e4
LT
415 return found;
416}
417
a05964f3
RP
418/*
419 * lookup_mnt increments the ref count before returning
420 * the vfsmount struct.
421 */
422struct 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
1da177e4
LT
432static inline int check_mnt(struct vfsmount *mnt)
433{
6b3286ed 434 return mnt->mnt_ns == current->nsproxy->mnt_ns;
1da177e4
LT
435}
436
6b3286ed 437static void touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
438{
439 if (ns) {
440 ns->event = ++event;
441 wake_up_interruptible(&ns->poll);
442 }
443}
444
6b3286ed 445static void __touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
446{
447 if (ns && ns->event != event) {
448 ns->event = event;
449 wake_up_interruptible(&ns->poll);
450 }
451}
452
1a390689 453static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
1da177e4 454{
1a390689
AV
455 old_path->dentry = mnt->mnt_mountpoint;
456 old_path->mnt = mnt->mnt_parent;
1da177e4
LT
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);
1a390689 461 old_path->dentry->d_mounted--;
1da177e4
LT
462}
463
b90fa9ae
RP
464void 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
1a390689 472static void attach_mnt(struct vfsmount *mnt, struct path *path)
1da177e4 473{
1a390689 474 mnt_set_mountpoint(path->mnt, path->dentry, mnt);
b90fa9ae 475 list_add_tail(&mnt->mnt_hash, mount_hashtable +
1a390689
AV
476 hash(path->mnt, path->dentry));
477 list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
b90fa9ae
RP
478}
479
480/*
481 * the caller must hold vfsmount_lock
482 */
483static void commit_tree(struct vfsmount *mnt)
484{
485 struct vfsmount *parent = mnt->mnt_parent;
486 struct vfsmount *m;
487 LIST_HEAD(head);
6b3286ed 488 struct mnt_namespace *n = parent->mnt_ns;
b90fa9ae
RP
489
490 BUG_ON(parent == mnt);
491
492 list_add_tail(&head, &mnt->mnt_list);
493 list_for_each_entry(m, &head, mnt_list)
6b3286ed 494 m->mnt_ns = n;
b90fa9ae
RP
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);
6b3286ed 500 touch_mnt_namespace(n);
1da177e4
LT
501}
502
503static 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
9676f0c6
RP
519static 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
36341f64
RP
529static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
530 int flag)
1da177e4
LT
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;
b90fa9ae 542
5afe0022
RP
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);
8aec0809 547 } else if (!(flag & CL_PRIVATE)) {
5afe0022
RP
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 }
b90fa9ae
RP
554 if (flag & CL_MAKE_SHARED)
555 set_mnt_shared(mnt);
1da177e4
LT
556
557 /* stick the duplicate mount on the same expiry list
558 * as the original if that was on one */
36341f64 559 if (flag & CL_EXPIRE) {
36341f64
RP
560 if (!list_empty(&old->mnt_expire))
561 list_add(&mnt->mnt_expire, &old->mnt_expire);
36341f64 562 }
1da177e4
LT
563 }
564 return mnt;
565}
566
7b7b1ace 567static inline void __mntput(struct vfsmount *mnt)
1da177e4 568{
3d733633 569 int cpu;
1da177e4 570 struct super_block *sb = mnt->mnt_sb;
3d733633
DH
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));
1da177e4
LT
599 dput(mnt->mnt_root);
600 free_vfsmnt(mnt);
601 deactivate_super(sb);
602}
603
7b7b1ace
AV
604void mntput_no_expire(struct vfsmount *mnt)
605{
606repeat:
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
622EXPORT_SYMBOL(mntput_no_expire);
623
624void mnt_pin(struct vfsmount *mnt)
625{
626 spin_lock(&vfsmount_lock);
627 mnt->mnt_pinned++;
628 spin_unlock(&vfsmount_lock);
629}
630
631EXPORT_SYMBOL(mnt_pin);
632
633void 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
643EXPORT_SYMBOL(mnt_unpin);
1da177e4 644
b3b304a2
MS
645static 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 */
656int 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}
667EXPORT_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 */
682void save_mount_options(struct super_block *sb, char *options)
683{
684 kfree(sb->s_options);
685 sb->s_options = kstrdup(options, GFP_KERNEL);
686}
687EXPORT_SYMBOL(save_mount_options);
688
1da177e4
LT
689/* iterator */
690static void *m_start(struct seq_file *m, loff_t *pos)
691{
6b3286ed 692 struct mnt_namespace *n = m->private;
1da177e4 693
390c6843 694 down_read(&namespace_sem);
b0765fb8 695 return seq_list_start(&n->list, *pos);
1da177e4
LT
696}
697
698static void *m_next(struct seq_file *m, void *v, loff_t *pos)
699{
6b3286ed 700 struct mnt_namespace *n = m->private;
b0765fb8
PE
701
702 return seq_list_next(v, &n->list, pos);
1da177e4
LT
703}
704
705static void m_stop(struct seq_file *m, void *v)
706{
390c6843 707 up_read(&namespace_sem);
1da177e4
LT
708}
709
1da177e4
LT
710static int show_vfsmnt(struct seq_file *m, void *v)
711{
b0765fb8 712 struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
1da177e4
LT
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" },
1da177e4
LT
721 { 0, NULL }
722 };
723 static struct proc_fs_info mnt_info[] = {
724 { MNT_NOSUID, ",nosuid" },
725 { MNT_NODEV, ",nodev" },
726 { MNT_NOEXEC, ",noexec" },
fc33a7bb
CH
727 { MNT_NOATIME, ",noatime" },
728 { MNT_NODIRATIME, ",nodiratime" },
47ae32d6 729 { MNT_RELATIME, ",relatime" },
1da177e4
LT
730 { 0, NULL }
731 };
732 struct proc_fs_info *fs_infop;
c32c2f63 733 struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
1da177e4
LT
734
735 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
736 seq_putc(m, ' ');
c32c2f63 737 seq_path(m, &mnt_path, " \t\n\\");
1da177e4
LT
738 seq_putc(m, ' ');
739 mangle(m, mnt->mnt_sb->s_type->name);
79c0b2df
MS
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 }
2e4b7fcd 744 seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
1da177e4
LT
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
759struct seq_operations mounts_op = {
760 .start = m_start,
761 .next = m_next,
762 .stop = m_stop,
763 .show = show_vfsmnt
764};
765
b4629fe2
CL
766static int show_vfsstat(struct seq_file *m, void *v)
767{
b0765fb8 768 struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
c32c2f63 769 struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
b4629fe2
CL
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 ");
c32c2f63 781 seq_path(m, &mnt_path, " \t\n\\");
b4629fe2
CL
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
798struct seq_operations mountstats_op = {
799 .start = m_start,
800 .next = m_next,
801 .stop = m_stop,
802 .show = show_vfsstat,
803};
804
1da177e4
LT
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 */
813int may_umount_tree(struct vfsmount *mnt)
814{
36341f64
RP
815 int actual_refs = 0;
816 int minimum_refs = 0;
817 struct vfsmount *p;
1da177e4
LT
818
819 spin_lock(&vfsmount_lock);
36341f64 820 for (p = mnt; p; p = next_mnt(p, mnt)) {
1da177e4
LT
821 actual_refs += atomic_read(&p->mnt_count);
822 minimum_refs += 2;
1da177e4
LT
823 }
824 spin_unlock(&vfsmount_lock);
825
826 if (actual_refs > minimum_refs)
e3474a8e 827 return 0;
1da177e4 828
e3474a8e 829 return 1;
1da177e4
LT
830}
831
832EXPORT_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 */
847int may_umount(struct vfsmount *mnt)
848{
e3474a8e 849 int ret = 1;
a05964f3
RP
850 spin_lock(&vfsmount_lock);
851 if (propagate_mount_busy(mnt, 2))
e3474a8e 852 ret = 0;
a05964f3
RP
853 spin_unlock(&vfsmount_lock);
854 return ret;
1da177e4
LT
855}
856
857EXPORT_SYMBOL(may_umount);
858
b90fa9ae 859void release_mounts(struct list_head *head)
70fbcdf4
RP
860{
861 struct vfsmount *mnt;
bf066c7d 862 while (!list_empty(head)) {
b5e61818 863 mnt = list_first_entry(head, struct vfsmount, mnt_hash);
70fbcdf4
RP
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;
7c4b93d8 873 m->mnt_ghosts--;
70fbcdf4
RP
874 spin_unlock(&vfsmount_lock);
875 dput(dentry);
876 mntput(m);
877 }
878 mntput(mnt);
879 }
880}
881
a05964f3 882void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
1da177e4
LT
883{
884 struct vfsmount *p;
1da177e4 885
1bfba4e8
AM
886 for (p = mnt; p; p = next_mnt(p, mnt))
887 list_move(&p->mnt_hash, kill);
1da177e4 888
a05964f3
RP
889 if (propagate)
890 propagate_umount(kill);
891
70fbcdf4
RP
892 list_for_each_entry(p, kill, mnt_hash) {
893 list_del_init(&p->mnt_expire);
894 list_del_init(&p->mnt_list);
6b3286ed
KK
895 __touch_mnt_namespace(p->mnt_ns);
896 p->mnt_ns = NULL;
70fbcdf4 897 list_del_init(&p->mnt_child);
7c4b93d8
AV
898 if (p->mnt_parent != p) {
899 p->mnt_parent->mnt_ghosts++;
f30ac319 900 p->mnt_mountpoint->d_mounted--;
7c4b93d8 901 }
a05964f3 902 change_mnt_propagation(p, MS_PRIVATE);
1da177e4
LT
903 }
904}
905
c35038be
AV
906static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
907
1da177e4
LT
908static int do_umount(struct vfsmount *mnt, int flags)
909{
b58fed8b 910 struct super_block *sb = mnt->mnt_sb;
1da177e4 911 int retval;
70fbcdf4 912 LIST_HEAD(umount_list);
1da177e4
LT
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) {
6ac08c39 925 if (mnt == current->fs->root.mnt ||
1da177e4
LT
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();
8b512d9a
TM
947 if (sb->s_op->umount_begin)
948 sb->s_op->umount_begin(mnt, flags);
1da177e4
LT
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 */
6ac08c39 960 if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1da177e4
LT
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
390c6843 976 down_write(&namespace_sem);
1da177e4 977 spin_lock(&vfsmount_lock);
5addc5dd 978 event++;
1da177e4 979
c35038be
AV
980 if (!(flags & MNT_DETACH))
981 shrink_submounts(mnt, &umount_list);
982
1da177e4 983 retval = -EBUSY;
a05964f3 984 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
1da177e4 985 if (!list_empty(&mnt->mnt_list))
a05964f3 986 umount_tree(mnt, 1, &umount_list);
1da177e4
LT
987 retval = 0;
988 }
989 spin_unlock(&vfsmount_lock);
990 if (retval)
991 security_sb_umount_busy(mnt);
390c6843 992 up_write(&namespace_sem);
70fbcdf4 993 release_mounts(&umount_list);
1da177e4
LT
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
1005asmlinkage 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;
4ac91378 1014 if (nd.path.dentry != nd.path.mnt->mnt_root)
1da177e4 1015 goto dput_and_out;
4ac91378 1016 if (!check_mnt(nd.path.mnt))
1da177e4
LT
1017 goto dput_and_out;
1018
1019 retval = -EPERM;
1020 if (!capable(CAP_SYS_ADMIN))
1021 goto dput_and_out;
1022
4ac91378 1023 retval = do_umount(nd.path.mnt, flags);
1da177e4 1024dput_and_out:
429731b1 1025 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
4ac91378
JB
1026 dput(nd.path.dentry);
1027 mntput_no_expire(nd.path.mnt);
1da177e4
LT
1028out:
1029 return retval;
1030}
1031
1032#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1033
1034/*
b58fed8b 1035 * The 2.0 compatible umount. No flags.
1da177e4 1036 */
1da177e4
LT
1037asmlinkage long sys_oldumount(char __user * name)
1038{
b58fed8b 1039 return sys_umount(name, 0);
1da177e4
LT
1040}
1041
1042#endif
1043
1044static int mount_is_safe(struct nameidata *nd)
1045{
1046 if (capable(CAP_SYS_ADMIN))
1047 return 0;
1048 return -EPERM;
1049#ifdef notyet
4ac91378 1050 if (S_ISLNK(nd->path.dentry->d_inode->i_mode))
1da177e4 1051 return -EPERM;
4ac91378
JB
1052 if (nd->path.dentry->d_inode->i_mode & S_ISVTX) {
1053 if (current->uid != nd->path.dentry->d_inode->i_uid)
1da177e4
LT
1054 return -EPERM;
1055 }
e4543edd 1056 if (vfs_permission(nd, MAY_WRITE))
1da177e4
LT
1057 return -EPERM;
1058 return 0;
1059#endif
1060}
1061
b58fed8b 1062static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
1da177e4
LT
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
b90fa9ae 1073struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
36341f64 1074 int flag)
1da177e4
LT
1075{
1076 struct vfsmount *res, *p, *q, *r, *s;
1a390689 1077 struct path path;
1da177e4 1078
9676f0c6
RP
1079 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
1080 return NULL;
1081
36341f64 1082 res = q = clone_mnt(mnt, dentry, flag);
1da177e4
LT
1083 if (!q)
1084 goto Enomem;
1085 q->mnt_mountpoint = mnt->mnt_mountpoint;
1086
1087 p = mnt;
fdadd65f 1088 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1da177e4
LT
1089 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
1090 continue;
1091
1092 for (s = r; s; s = next_mnt(s, r)) {
9676f0c6
RP
1093 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
1094 s = skip_mnt_tree(s);
1095 continue;
1096 }
1da177e4
LT
1097 while (p != s->mnt_parent) {
1098 p = p->mnt_parent;
1099 q = q->mnt_parent;
1100 }
1101 p = s;
1a390689
AV
1102 path.mnt = q;
1103 path.dentry = p->mnt_mountpoint;
36341f64 1104 q = clone_mnt(p, p->mnt_root, flag);
1da177e4
LT
1105 if (!q)
1106 goto Enomem;
1107 spin_lock(&vfsmount_lock);
1108 list_add_tail(&q->mnt_list, &res->mnt_list);
1a390689 1109 attach_mnt(q, &path);
1da177e4
LT
1110 spin_unlock(&vfsmount_lock);
1111 }
1112 }
1113 return res;
b58fed8b 1114Enomem:
1da177e4 1115 if (res) {
70fbcdf4 1116 LIST_HEAD(umount_list);
1da177e4 1117 spin_lock(&vfsmount_lock);
a05964f3 1118 umount_tree(res, 0, &umount_list);
1da177e4 1119 spin_unlock(&vfsmount_lock);
70fbcdf4 1120 release_mounts(&umount_list);
1da177e4
LT
1121 }
1122 return NULL;
1123}
1124
8aec0809
AV
1125struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry)
1126{
1127 struct vfsmount *tree;
1a60a280 1128 down_write(&namespace_sem);
8aec0809 1129 tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE);
1a60a280 1130 up_write(&namespace_sem);
8aec0809
AV
1131 return tree;
1132}
1133
1134void drop_collected_mounts(struct vfsmount *mnt)
1135{
1136 LIST_HEAD(umount_list);
1a60a280 1137 down_write(&namespace_sem);
8aec0809
AV
1138 spin_lock(&vfsmount_lock);
1139 umount_tree(mnt, 0, &umount_list);
1140 spin_unlock(&vfsmount_lock);
1a60a280 1141 up_write(&namespace_sem);
8aec0809
AV
1142 release_mounts(&umount_list);
1143}
1144
b90fa9ae
RP
1145/*
1146 * @source_mnt : mount tree to be attached
21444403
RP
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)
b90fa9ae
RP
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.
9676f0c6
RP
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 * ***************************************************************************
b90fa9ae
RP
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.
5afe0022
RP
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 *
9676f0c6
RP
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 * ***************************************************************************
5afe0022
RP
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.
21444403 1197 * (+*) the mount is moved to the destination.
5afe0022
RP
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.
b90fa9ae
RP
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 */
1208static int attach_recursive_mnt(struct vfsmount *source_mnt,
1a390689 1209 struct path *path, struct path *parent_path)
b90fa9ae
RP
1210{
1211 LIST_HEAD(tree_list);
1a390689
AV
1212 struct vfsmount *dest_mnt = path->mnt;
1213 struct dentry *dest_dentry = path->dentry;
b90fa9ae
RP
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);
1a390689
AV
1225 if (parent_path) {
1226 detach_mnt(source_mnt, parent_path);
1227 attach_mnt(source_mnt, path);
6b3286ed 1228 touch_mnt_namespace(current->nsproxy->mnt_ns);
21444403
RP
1229 } else {
1230 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1231 commit_tree(source_mnt);
1232 }
b90fa9ae
RP
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
8c3ee42e 1242static int graft_tree(struct vfsmount *mnt, struct path *path)
1da177e4
LT
1243{
1244 int err;
1245 if (mnt->mnt_sb->s_flags & MS_NOUSER)
1246 return -EINVAL;
1247
8c3ee42e 1248 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1da177e4
LT
1249 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
1250 return -ENOTDIR;
1251
1252 err = -ENOENT;
8c3ee42e
AV
1253 mutex_lock(&path->dentry->d_inode->i_mutex);
1254 if (IS_DEADDIR(path->dentry->d_inode))
1da177e4
LT
1255 goto out_unlock;
1256
8c3ee42e 1257 err = security_sb_check_sb(mnt, path);
1da177e4
LT
1258 if (err)
1259 goto out_unlock;
1260
1261 err = -ENOENT;
8c3ee42e
AV
1262 if (IS_ROOT(path->dentry) || !d_unhashed(path->dentry))
1263 err = attach_recursive_mnt(mnt, path, NULL);
1da177e4 1264out_unlock:
8c3ee42e 1265 mutex_unlock(&path->dentry->d_inode->i_mutex);
1da177e4 1266 if (!err)
8c3ee42e 1267 security_sb_post_addmount(mnt, path);
1da177e4
LT
1268 return err;
1269}
1270
07b20889
RP
1271/*
1272 * recursively change the type of the mountpoint.
2dafe1c4 1273 * noinline this do_mount helper to save do_mount stack space.
07b20889 1274 */
2dafe1c4 1275static noinline int do_change_type(struct nameidata *nd, int flag)
07b20889 1276{
4ac91378 1277 struct vfsmount *m, *mnt = nd->path.mnt;
07b20889
RP
1278 int recurse = flag & MS_REC;
1279 int type = flag & ~MS_REC;
1280
ee6f9582
MS
1281 if (!capable(CAP_SYS_ADMIN))
1282 return -EPERM;
1283
4ac91378 1284 if (nd->path.dentry != nd->path.mnt->mnt_root)
07b20889
RP
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
1da177e4
LT
1296/*
1297 * do loopback mount.
2dafe1c4 1298 * noinline this do_mount helper to save do_mount stack space.
1da177e4 1299 */
2dafe1c4
ES
1300static noinline int do_loopback(struct nameidata *nd, char *old_name,
1301 int recurse)
1da177e4
LT
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
390c6843 1314 down_write(&namespace_sem);
1da177e4 1315 err = -EINVAL;
4ac91378
JB
1316 if (IS_MNT_UNBINDABLE(old_nd.path.mnt))
1317 goto out;
9676f0c6 1318
4ac91378 1319 if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
ccd48bc7 1320 goto out;
1da177e4 1321
ccd48bc7
AV
1322 err = -ENOMEM;
1323 if (recurse)
4ac91378 1324 mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0);
ccd48bc7 1325 else
4ac91378 1326 mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0);
ccd48bc7
AV
1327
1328 if (!mnt)
1329 goto out;
1330
8c3ee42e 1331 err = graft_tree(mnt, &nd->path);
ccd48bc7 1332 if (err) {
70fbcdf4 1333 LIST_HEAD(umount_list);
1da177e4 1334 spin_lock(&vfsmount_lock);
a05964f3 1335 umount_tree(mnt, 0, &umount_list);
1da177e4 1336 spin_unlock(&vfsmount_lock);
70fbcdf4 1337 release_mounts(&umount_list);
5b83d2c5 1338 }
1da177e4 1339
ccd48bc7 1340out:
390c6843 1341 up_write(&namespace_sem);
1d957f9b 1342 path_put(&old_nd.path);
1da177e4
LT
1343 return err;
1344}
1345
2e4b7fcd
DH
1346static 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
1da177e4
LT
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.
2dafe1c4 1367 * noinline this do_mount helper to save do_mount stack space.
1da177e4 1368 */
2dafe1c4 1369static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags,
1da177e4
LT
1370 void *data)
1371{
1372 int err;
4ac91378 1373 struct super_block *sb = nd->path.mnt->mnt_sb;
1da177e4
LT
1374
1375 if (!capable(CAP_SYS_ADMIN))
1376 return -EPERM;
1377
4ac91378 1378 if (!check_mnt(nd->path.mnt))
1da177e4
LT
1379 return -EINVAL;
1380
4ac91378 1381 if (nd->path.dentry != nd->path.mnt->mnt_root)
1da177e4
LT
1382 return -EINVAL;
1383
1384 down_write(&sb->s_umount);
2e4b7fcd
DH
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);
1da177e4 1389 if (!err)
4ac91378 1390 nd->path.mnt->mnt_flags = mnt_flags;
1da177e4
LT
1391 up_write(&sb->s_umount);
1392 if (!err)
4ac91378 1393 security_sb_post_remount(nd->path.mnt, flags, data);
1da177e4
LT
1394 return err;
1395}
1396
9676f0c6
RP
1397static 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
2dafe1c4
ES
1407/*
1408 * noinline this do_mount helper to save do_mount stack space.
1409 */
1410static noinline int do_move_mount(struct nameidata *nd, char *old_name)
1da177e4 1411{
1a390689
AV
1412 struct nameidata old_nd;
1413 struct path parent_path;
1da177e4
LT
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
390c6843 1424 down_write(&namespace_sem);
4ac91378
JB
1425 while (d_mountpoint(nd->path.dentry) &&
1426 follow_down(&nd->path.mnt, &nd->path.dentry))
1da177e4
LT
1427 ;
1428 err = -EINVAL;
4ac91378 1429 if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
1da177e4
LT
1430 goto out;
1431
1432 err = -ENOENT;
4ac91378
JB
1433 mutex_lock(&nd->path.dentry->d_inode->i_mutex);
1434 if (IS_DEADDIR(nd->path.dentry->d_inode))
1da177e4
LT
1435 goto out1;
1436
4ac91378 1437 if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry))
21444403 1438 goto out1;
1da177e4
LT
1439
1440 err = -EINVAL;
4ac91378 1441 if (old_nd.path.dentry != old_nd.path.mnt->mnt_root)
21444403 1442 goto out1;
1da177e4 1443
4ac91378 1444 if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent)
21444403 1445 goto out1;
1da177e4 1446
4ac91378
JB
1447 if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
1448 S_ISDIR(old_nd.path.dentry->d_inode->i_mode))
21444403
RP
1449 goto out1;
1450 /*
1451 * Don't move a mount residing in a shared parent.
1452 */
4ac91378
JB
1453 if (old_nd.path.mnt->mnt_parent &&
1454 IS_MNT_SHARED(old_nd.path.mnt->mnt_parent))
21444403 1455 goto out1;
9676f0c6
RP
1456 /*
1457 * Don't move a mount tree containing unbindable mounts to a destination
1458 * mount which is shared.
1459 */
4ac91378
JB
1460 if (IS_MNT_SHARED(nd->path.mnt) &&
1461 tree_contains_unbindable(old_nd.path.mnt))
9676f0c6 1462 goto out1;
1da177e4 1463 err = -ELOOP;
4ac91378
JB
1464 for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent)
1465 if (p == old_nd.path.mnt)
21444403 1466 goto out1;
1da177e4 1467
1a390689 1468 err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path);
4ac91378 1469 if (err)
21444403 1470 goto out1;
1da177e4
LT
1471
1472 /* if the mount is moved, it should no longer be expire
1473 * automatically */
4ac91378 1474 list_del_init(&old_nd.path.mnt->mnt_expire);
1da177e4 1475out1:
4ac91378 1476 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
1da177e4 1477out:
390c6843 1478 up_write(&namespace_sem);
1da177e4 1479 if (!err)
1a390689 1480 path_put(&parent_path);
1d957f9b 1481 path_put(&old_nd.path);
1da177e4
LT
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
2dafe1c4 1488 * noinline this do_mount helper to save do_mount stack space.
1da177e4 1489 */
2dafe1c4 1490static noinline int do_new_mount(struct nameidata *nd, char *type, int flags,
1da177e4
LT
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 */
1513int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
1514 int mnt_flags, struct list_head *fslist)
1515{
1516 int err;
1517
390c6843 1518 down_write(&namespace_sem);
1da177e4 1519 /* Something was mounted here while we slept */
4ac91378
JB
1520 while (d_mountpoint(nd->path.dentry) &&
1521 follow_down(&nd->path.mnt, &nd->path.dentry))
1da177e4
LT
1522 ;
1523 err = -EINVAL;
4ac91378 1524 if (!check_mnt(nd->path.mnt))
1da177e4
LT
1525 goto unlock;
1526
1527 /* Refuse the same filesystem on the same mount point */
1528 err = -EBUSY;
4ac91378
JB
1529 if (nd->path.mnt->mnt_sb == newmnt->mnt_sb &&
1530 nd->path.mnt->mnt_root == nd->path.dentry)
1da177e4
LT
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;
8c3ee42e 1538 if ((err = graft_tree(newmnt, &nd->path)))
5b83d2c5 1539 goto unlock;
1da177e4 1540
6758f953 1541 if (fslist) /* add to the specified expiration list */
55e700b9 1542 list_add_tail(&newmnt->mnt_expire, fslist);
6758f953 1543
390c6843 1544 up_write(&namespace_sem);
5b83d2c5 1545 return 0;
1da177e4
LT
1546
1547unlock:
390c6843 1548 up_write(&namespace_sem);
1da177e4
LT
1549 mntput(newmnt);
1550 return err;
1551}
1552
1553EXPORT_SYMBOL_GPL(do_add_mount);
1554
1555/*
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 */
1560void mark_mounts_for_expiry(struct list_head *mounts)
1561{
1da177e4
LT
1562 struct vfsmount *mnt, *next;
1563 LIST_HEAD(graveyard);
bcc5c7d2 1564 LIST_HEAD(umounts);
1da177e4
LT
1565
1566 if (list_empty(mounts))
1567 return;
1568
bcc5c7d2 1569 down_write(&namespace_sem);
1da177e4
LT
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 */
55e700b9 1578 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1da177e4 1579 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
bcc5c7d2 1580 propagate_mount_busy(mnt, 1))
1da177e4 1581 continue;
55e700b9 1582 list_move(&mnt->mnt_expire, &graveyard);
1da177e4 1583 }
bcc5c7d2
AV
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 }
5528f911 1589 spin_unlock(&vfsmount_lock);
bcc5c7d2
AV
1590 up_write(&namespace_sem);
1591
1592 release_mounts(&umounts);
5528f911
TM
1593}
1594
1595EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1596
1597/*
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 */
1603static 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
1609repeat:
1610 next = this_parent->mnt_mounts.next;
1611resume:
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))
1da177e4 1618 continue;
5528f911
TM
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 }
1da177e4 1626
5528f911 1627 if (!propagate_mount_busy(mnt, 1)) {
5528f911
TM
1628 list_move_tail(&mnt->mnt_expire, graveyard);
1629 found++;
1630 }
1da177e4 1631 }
5528f911
TM
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 */
c35038be 1647static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
5528f911
TM
1648{
1649 LIST_HEAD(graveyard);
c35038be 1650 struct vfsmount *m;
5528f911 1651
5528f911 1652 /* extract submounts of 'mountpoint' from the expiration list */
c35038be 1653 while (select_submounts(mnt, &graveyard)) {
bcc5c7d2 1654 while (!list_empty(&graveyard)) {
c35038be 1655 m = list_first_entry(&graveyard, struct vfsmount,
bcc5c7d2
AV
1656 mnt_expire);
1657 touch_mnt_namespace(mnt->mnt_ns);
c35038be 1658 umount_tree(mnt, 1, umounts);
bcc5c7d2
AV
1659 }
1660 }
1da177e4
LT
1661}
1662
1da177e4
LT
1663/*
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 */
b58fed8b
RP
1669static long exact_copy_from_user(void *to, const void __user * from,
1670 unsigned long n)
1da177e4
LT
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
b58fed8b 1691int copy_mount_options(const void __user * data, unsigned long *where)
1da177e4
LT
1692{
1693 int i;
1694 unsigned long page;
1695 unsigned long size;
b58fed8b 1696
1da177e4
LT
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) {
b58fed8b 1715 free_page(page);
1da177e4
LT
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 */
b58fed8b 1738long do_mount(char *dev_name, char *dir_name, char *type_page,
1da177e4
LT
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;
fc33a7bb
CH
1766 if (flags & MS_NOATIME)
1767 mnt_flags |= MNT_NOATIME;
1768 if (flags & MS_NODIRATIME)
1769 mnt_flags |= MNT_NODIRATIME;
47ae32d6
VH
1770 if (flags & MS_RELATIME)
1771 mnt_flags |= MNT_RELATIME;
2e4b7fcd
DH
1772 if (flags & MS_RDONLY)
1773 mnt_flags |= MNT_READONLY;
fc33a7bb
CH
1774
1775 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
8bf9725c 1776 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
1da177e4
LT
1777
1778 /* ... and get the mountpoint */
1779 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1780 if (retval)
1781 return retval;
1782
b5266eb4
AV
1783 retval = security_sb_mount(dev_name, &nd.path,
1784 type_page, flags, data_page);
1da177e4
LT
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)
eee391a6 1792 retval = do_loopback(&nd, dev_name, flags & MS_REC);
9676f0c6 1793 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
07b20889 1794 retval = do_change_type(&nd, flags);
1da177e4
LT
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);
1800dput_out:
1d957f9b 1801 path_put(&nd.path);
1da177e4
LT
1802 return retval;
1803}
1804
741a2951
JD
1805/*
1806 * Allocate a new namespace structure and populate it with contents
1807 * copied from the namespace of the passed in task structure.
1808 */
e3222c4e 1809static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
6b3286ed 1810 struct fs_struct *fs)
1da177e4 1811{
6b3286ed 1812 struct mnt_namespace *new_ns;
1da177e4 1813 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1da177e4
LT
1814 struct vfsmount *p, *q;
1815
6b3286ed 1816 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
1da177e4 1817 if (!new_ns)
467e9f4b 1818 return ERR_PTR(-ENOMEM);
1da177e4
LT
1819
1820 atomic_set(&new_ns->count, 1);
1da177e4 1821 INIT_LIST_HEAD(&new_ns->list);
5addc5dd
AV
1822 init_waitqueue_head(&new_ns->poll);
1823 new_ns->event = 0;
1da177e4 1824
390c6843 1825 down_write(&namespace_sem);
1da177e4 1826 /* First pass: copy the tree topology */
6b3286ed 1827 new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
9676f0c6 1828 CL_COPY_ALL | CL_EXPIRE);
1da177e4 1829 if (!new_ns->root) {
390c6843 1830 up_write(&namespace_sem);
1da177e4 1831 kfree(new_ns);
467e9f4b 1832 return ERR_PTR(-ENOMEM);;
1da177e4
LT
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 */
6b3286ed 1843 p = mnt_ns->root;
1da177e4
LT
1844 q = new_ns->root;
1845 while (p) {
6b3286ed 1846 q->mnt_ns = new_ns;
1da177e4 1847 if (fs) {
6ac08c39 1848 if (p == fs->root.mnt) {
1da177e4 1849 rootmnt = p;
6ac08c39 1850 fs->root.mnt = mntget(q);
1da177e4 1851 }
6ac08c39 1852 if (p == fs->pwd.mnt) {
1da177e4 1853 pwdmnt = p;
6ac08c39 1854 fs->pwd.mnt = mntget(q);
1da177e4 1855 }
6ac08c39 1856 if (p == fs->altroot.mnt) {
1da177e4 1857 altrootmnt = p;
6ac08c39 1858 fs->altroot.mnt = mntget(q);
1da177e4
LT
1859 }
1860 }
6b3286ed 1861 p = next_mnt(p, mnt_ns->root);
1da177e4
LT
1862 q = next_mnt(q, new_ns->root);
1863 }
390c6843 1864 up_write(&namespace_sem);
1da177e4 1865
1da177e4
LT
1866 if (rootmnt)
1867 mntput(rootmnt);
1868 if (pwdmnt)
1869 mntput(pwdmnt);
1870 if (altrootmnt)
1871 mntput(altrootmnt);
1872
741a2951
JD
1873 return new_ns;
1874}
1875
213dd266 1876struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
e3222c4e 1877 struct fs_struct *new_fs)
741a2951 1878{
6b3286ed 1879 struct mnt_namespace *new_ns;
741a2951 1880
e3222c4e 1881 BUG_ON(!ns);
6b3286ed 1882 get_mnt_ns(ns);
741a2951
JD
1883
1884 if (!(flags & CLONE_NEWNS))
e3222c4e 1885 return ns;
741a2951 1886
e3222c4e 1887 new_ns = dup_mnt_ns(ns, new_fs);
741a2951 1888
6b3286ed 1889 put_mnt_ns(ns);
e3222c4e 1890 return new_ns;
1da177e4
LT
1891}
1892
1893asmlinkage 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
b58fed8b 1903 retval = copy_mount_options(type, &type_page);
1da177e4
LT
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
b58fed8b 1912 retval = copy_mount_options(dev_name, &dev_page);
1da177e4
LT
1913 if (retval < 0)
1914 goto out2;
1915
b58fed8b 1916 retval = copy_mount_options(data, &data_page);
1da177e4
LT
1917 if (retval < 0)
1918 goto out3;
1919
1920 lock_kernel();
b58fed8b
RP
1921 retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1922 flags, (void *)data_page);
1da177e4
LT
1923 unlock_kernel();
1924 free_page(data_page);
1925
1926out3:
1927 free_page(dev_page);
1928out2:
1929 putname(dir_page);
1930out1:
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 */
ac748a09 1939void set_fs_root(struct fs_struct *fs, struct path *path)
1da177e4 1940{
6ac08c39
JB
1941 struct path old_root;
1942
1da177e4
LT
1943 write_lock(&fs->lock);
1944 old_root = fs->root;
ac748a09
JB
1945 fs->root = *path;
1946 path_get(path);
1da177e4 1947 write_unlock(&fs->lock);
6ac08c39
JB
1948 if (old_root.dentry)
1949 path_put(&old_root);
1da177e4
LT
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 */
ac748a09 1956void set_fs_pwd(struct fs_struct *fs, struct path *path)
1da177e4 1957{
6ac08c39 1958 struct path old_pwd;
1da177e4
LT
1959
1960 write_lock(&fs->lock);
1961 old_pwd = fs->pwd;
ac748a09
JB
1962 fs->pwd = *path;
1963 path_get(path);
1da177e4
LT
1964 write_unlock(&fs->lock);
1965
6ac08c39
JB
1966 if (old_pwd.dentry)
1967 path_put(&old_pwd);
1da177e4
LT
1968}
1969
1a390689 1970static void chroot_fs_refs(struct path *old_root, struct path *new_root)
1da177e4
LT
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);
1a390689
AV
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);
1da177e4
LT
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 *
4a0d11fa
NB
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 *
1da177e4
LT
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 */
b58fed8b
RP
2020asmlinkage long sys_pivot_root(const char __user * new_root,
2021 const char __user * put_old)
1da177e4
LT
2022{
2023 struct vfsmount *tmp;
8c3ee42e
AV
2024 struct nameidata new_nd, old_nd;
2025 struct path parent_path, root_parent, root;
1da177e4
LT
2026 int error;
2027
2028 if (!capable(CAP_SYS_ADMIN))
2029 return -EPERM;
2030
b58fed8b
RP
2031 error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
2032 &new_nd);
1da177e4
LT
2033 if (error)
2034 goto out0;
2035 error = -EINVAL;
4ac91378 2036 if (!check_mnt(new_nd.path.mnt))
1da177e4
LT
2037 goto out1;
2038
b58fed8b 2039 error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1da177e4
LT
2040 if (error)
2041 goto out1;
2042
b5266eb4 2043 error = security_sb_pivotroot(&old_nd.path, &new_nd.path);
1da177e4 2044 if (error) {
1d957f9b 2045 path_put(&old_nd.path);
1da177e4
LT
2046 goto out1;
2047 }
2048
2049 read_lock(&current->fs->lock);
8c3ee42e 2050 root = current->fs->root;
6ac08c39 2051 path_get(&current->fs->root);
1da177e4 2052 read_unlock(&current->fs->lock);
390c6843 2053 down_write(&namespace_sem);
4ac91378 2054 mutex_lock(&old_nd.path.dentry->d_inode->i_mutex);
1da177e4 2055 error = -EINVAL;
4ac91378
JB
2056 if (IS_MNT_SHARED(old_nd.path.mnt) ||
2057 IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) ||
8c3ee42e 2058 IS_MNT_SHARED(root.mnt->mnt_parent))
21444403 2059 goto out2;
8c3ee42e 2060 if (!check_mnt(root.mnt))
1da177e4
LT
2061 goto out2;
2062 error = -ENOENT;
4ac91378 2063 if (IS_DEADDIR(new_nd.path.dentry->d_inode))
1da177e4 2064 goto out2;
4ac91378 2065 if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry))
1da177e4 2066 goto out2;
4ac91378 2067 if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry))
1da177e4
LT
2068 goto out2;
2069 error = -EBUSY;
8c3ee42e
AV
2070 if (new_nd.path.mnt == root.mnt ||
2071 old_nd.path.mnt == root.mnt)
1da177e4
LT
2072 goto out2; /* loop, on the same file system */
2073 error = -EINVAL;
8c3ee42e 2074 if (root.mnt->mnt_root != root.dentry)
1da177e4 2075 goto out2; /* not a mountpoint */
8c3ee42e 2076 if (root.mnt->mnt_parent == root.mnt)
0bb6fcc1 2077 goto out2; /* not attached */
4ac91378 2078 if (new_nd.path.mnt->mnt_root != new_nd.path.dentry)
1da177e4 2079 goto out2; /* not a mountpoint */
4ac91378 2080 if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt)
0bb6fcc1 2081 goto out2; /* not attached */
4ac91378
JB
2082 /* make sure we can reach put_old from new_root */
2083 tmp = old_nd.path.mnt;
1da177e4 2084 spin_lock(&vfsmount_lock);
4ac91378 2085 if (tmp != new_nd.path.mnt) {
1da177e4
LT
2086 for (;;) {
2087 if (tmp->mnt_parent == tmp)
2088 goto out3; /* already mounted on put_old */
4ac91378 2089 if (tmp->mnt_parent == new_nd.path.mnt)
1da177e4
LT
2090 break;
2091 tmp = tmp->mnt_parent;
2092 }
4ac91378 2093 if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry))
1da177e4 2094 goto out3;
4ac91378 2095 } else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry))
1da177e4 2096 goto out3;
1a390689 2097 detach_mnt(new_nd.path.mnt, &parent_path);
8c3ee42e 2098 detach_mnt(root.mnt, &root_parent);
4ac91378 2099 /* mount old root on put_old */
8c3ee42e 2100 attach_mnt(root.mnt, &old_nd.path);
4ac91378
JB
2101 /* mount new_root on / */
2102 attach_mnt(new_nd.path.mnt, &root_parent);
6b3286ed 2103 touch_mnt_namespace(current->nsproxy->mnt_ns);
1da177e4 2104 spin_unlock(&vfsmount_lock);
8c3ee42e
AV
2105 chroot_fs_refs(&root, &new_nd.path);
2106 security_sb_post_pivotroot(&root, &new_nd.path);
1da177e4 2107 error = 0;
1a390689
AV
2108 path_put(&root_parent);
2109 path_put(&parent_path);
1da177e4 2110out2:
4ac91378 2111 mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex);
390c6843 2112 up_write(&namespace_sem);
8c3ee42e 2113 path_put(&root);
1d957f9b 2114 path_put(&old_nd.path);
1da177e4 2115out1:
1d957f9b 2116 path_put(&new_nd.path);
1da177e4 2117out0:
1da177e4
LT
2118 return error;
2119out3:
2120 spin_unlock(&vfsmount_lock);
2121 goto out2;
2122}
2123
2124static void __init init_mount_tree(void)
2125{
2126 struct vfsmount *mnt;
6b3286ed 2127 struct mnt_namespace *ns;
ac748a09 2128 struct path root;
1da177e4
LT
2129
2130 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
2131 if (IS_ERR(mnt))
2132 panic("Can't create rootfs");
6b3286ed
KK
2133 ns = kmalloc(sizeof(*ns), GFP_KERNEL);
2134 if (!ns)
1da177e4 2135 panic("Can't allocate initial namespace");
6b3286ed
KK
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;
2143
2144 init_task.nsproxy->mnt_ns = ns;
2145 get_mnt_ns(ns);
2146
ac748a09
JB
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);
1da177e4
LT
2152}
2153
74bf17cf 2154void __init mnt_init(void)
1da177e4 2155{
13f14b4d 2156 unsigned u;
15a67dd8 2157 int err;
1da177e4 2158
390c6843
RP
2159 init_rwsem(&namespace_sem);
2160
1da177e4 2161 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
20c2df83 2162 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1da177e4 2163
b58fed8b 2164 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1da177e4
LT
2165
2166 if (!mount_hashtable)
2167 panic("Failed to allocate mount hash table\n");
2168
13f14b4d
ED
2169 printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
2170
2171 for (u = 0; u < HASH_SIZE; u++)
2172 INIT_LIST_HEAD(&mount_hashtable[u]);
1da177e4 2173
15a67dd8
RD
2174 err = sysfs_init();
2175 if (err)
2176 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
2177 __FUNCTION__, err);
00d26666
GKH
2178 fs_kobj = kobject_create_and_add("fs", NULL);
2179 if (!fs_kobj)
2180 printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__);
1da177e4
LT
2181 init_rootfs();
2182 init_mount_tree();
2183}
2184
6b3286ed 2185void __put_mnt_ns(struct mnt_namespace *ns)
1da177e4 2186{
6b3286ed 2187 struct vfsmount *root = ns->root;
70fbcdf4 2188 LIST_HEAD(umount_list);
6b3286ed 2189 ns->root = NULL;
1ce88cf4 2190 spin_unlock(&vfsmount_lock);
390c6843 2191 down_write(&namespace_sem);
1da177e4 2192 spin_lock(&vfsmount_lock);
a05964f3 2193 umount_tree(root, 0, &umount_list);
1da177e4 2194 spin_unlock(&vfsmount_lock);
390c6843 2195 up_write(&namespace_sem);
70fbcdf4 2196 release_mounts(&umount_list);
6b3286ed 2197 kfree(ns);
1da177e4 2198}