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