]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/namespace.c
[PATCH] beginning of the shared-subtree proper
[mv-sheeva.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/config.h>
12 #include <linux/syscalls.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/init.h>
17 #include <linux/quotaops.h>
18 #include <linux/acct.h>
19 #include <linux/module.h>
20 #include <linux/seq_file.h>
21 #include <linux/namespace.h>
22 #include <linux/namei.h>
23 #include <linux/security.h>
24 #include <linux/mount.h>
25 #include <asm/uaccess.h>
26 #include <asm/unistd.h>
27 #include "pnode.h"
28
29 extern int __init init_rootfs(void);
30
31 #define CL_EXPIRE       0x01
32
33 #ifdef CONFIG_SYSFS
34 extern int __init sysfs_init(void);
35 #else
36 static inline int sysfs_init(void)
37 {
38         return 0;
39 }
40 #endif
41
42 /* spinlock for vfsmount related operations, inplace of dcache_lock */
43 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
44
45 static int event;
46
47 static struct list_head *mount_hashtable;
48 static int hash_mask __read_mostly, hash_bits __read_mostly;
49 static kmem_cache_t *mnt_cache;
50 static struct rw_semaphore namespace_sem;
51
52 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
53 {
54         unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
55         tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
56         tmp = tmp + (tmp >> hash_bits);
57         return tmp & hash_mask;
58 }
59
60 struct vfsmount *alloc_vfsmnt(const char *name)
61 {
62         struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
63         if (mnt) {
64                 memset(mnt, 0, sizeof(struct vfsmount));
65                 atomic_set(&mnt->mnt_count, 1);
66                 INIT_LIST_HEAD(&mnt->mnt_hash);
67                 INIT_LIST_HEAD(&mnt->mnt_child);
68                 INIT_LIST_HEAD(&mnt->mnt_mounts);
69                 INIT_LIST_HEAD(&mnt->mnt_list);
70                 INIT_LIST_HEAD(&mnt->mnt_expire);
71                 if (name) {
72                         int size = strlen(name) + 1;
73                         char *newname = kmalloc(size, GFP_KERNEL);
74                         if (newname) {
75                                 memcpy(newname, name, size);
76                                 mnt->mnt_devname = newname;
77                         }
78                 }
79         }
80         return mnt;
81 }
82
83 void free_vfsmnt(struct vfsmount *mnt)
84 {
85         kfree(mnt->mnt_devname);
86         kmem_cache_free(mnt_cache, mnt);
87 }
88
89 /*
90  * Now, lookup_mnt increments the ref count before returning
91  * the vfsmount struct.
92  */
93 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
94 {
95         struct list_head *head = mount_hashtable + hash(mnt, dentry);
96         struct list_head *tmp = head;
97         struct vfsmount *p, *found = NULL;
98
99         spin_lock(&vfsmount_lock);
100         for (;;) {
101                 tmp = tmp->next;
102                 p = NULL;
103                 if (tmp == head)
104                         break;
105                 p = list_entry(tmp, struct vfsmount, mnt_hash);
106                 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
107                         found = mntget(p);
108                         break;
109                 }
110         }
111         spin_unlock(&vfsmount_lock);
112         return found;
113 }
114
115 static inline int check_mnt(struct vfsmount *mnt)
116 {
117         return mnt->mnt_namespace == current->namespace;
118 }
119
120 static void touch_namespace(struct namespace *ns)
121 {
122         if (ns) {
123                 ns->event = ++event;
124                 wake_up_interruptible(&ns->poll);
125         }
126 }
127
128 static void __touch_namespace(struct namespace *ns)
129 {
130         if (ns && ns->event != event) {
131                 ns->event = event;
132                 wake_up_interruptible(&ns->poll);
133         }
134 }
135
136 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
137 {
138         old_nd->dentry = mnt->mnt_mountpoint;
139         old_nd->mnt = mnt->mnt_parent;
140         mnt->mnt_parent = mnt;
141         mnt->mnt_mountpoint = mnt->mnt_root;
142         list_del_init(&mnt->mnt_child);
143         list_del_init(&mnt->mnt_hash);
144         old_nd->dentry->d_mounted--;
145 }
146
147 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
148 {
149         mnt->mnt_parent = mntget(nd->mnt);
150         mnt->mnt_mountpoint = dget(nd->dentry);
151         list_add(&mnt->mnt_hash, mount_hashtable + hash(nd->mnt, nd->dentry));
152         list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
153         nd->dentry->d_mounted++;
154 }
155
156 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
157 {
158         struct list_head *next = p->mnt_mounts.next;
159         if (next == &p->mnt_mounts) {
160                 while (1) {
161                         if (p == root)
162                                 return NULL;
163                         next = p->mnt_child.next;
164                         if (next != &p->mnt_parent->mnt_mounts)
165                                 break;
166                         p = p->mnt_parent;
167                 }
168         }
169         return list_entry(next, struct vfsmount, mnt_child);
170 }
171
172 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
173                                         int flag)
174 {
175         struct super_block *sb = old->mnt_sb;
176         struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
177
178         if (mnt) {
179                 mnt->mnt_flags = old->mnt_flags;
180                 atomic_inc(&sb->s_active);
181                 mnt->mnt_sb = sb;
182                 mnt->mnt_root = dget(root);
183                 mnt->mnt_mountpoint = mnt->mnt_root;
184                 mnt->mnt_parent = mnt;
185                 mnt->mnt_namespace = current->namespace;
186
187                 /* stick the duplicate mount on the same expiry list
188                  * as the original if that was on one */
189                 if (flag & CL_EXPIRE) {
190                         spin_lock(&vfsmount_lock);
191                         if (!list_empty(&old->mnt_expire))
192                                 list_add(&mnt->mnt_expire, &old->mnt_expire);
193                         spin_unlock(&vfsmount_lock);
194                 }
195         }
196         return mnt;
197 }
198
199 static inline void __mntput(struct vfsmount *mnt)
200 {
201         struct super_block *sb = mnt->mnt_sb;
202         dput(mnt->mnt_root);
203         free_vfsmnt(mnt);
204         deactivate_super(sb);
205 }
206
207 void mntput_no_expire(struct vfsmount *mnt)
208 {
209 repeat:
210         if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
211                 if (likely(!mnt->mnt_pinned)) {
212                         spin_unlock(&vfsmount_lock);
213                         __mntput(mnt);
214                         return;
215                 }
216                 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
217                 mnt->mnt_pinned = 0;
218                 spin_unlock(&vfsmount_lock);
219                 acct_auto_close_mnt(mnt);
220                 security_sb_umount_close(mnt);
221                 goto repeat;
222         }
223 }
224
225 EXPORT_SYMBOL(mntput_no_expire);
226
227 void mnt_pin(struct vfsmount *mnt)
228 {
229         spin_lock(&vfsmount_lock);
230         mnt->mnt_pinned++;
231         spin_unlock(&vfsmount_lock);
232 }
233
234 EXPORT_SYMBOL(mnt_pin);
235
236 void mnt_unpin(struct vfsmount *mnt)
237 {
238         spin_lock(&vfsmount_lock);
239         if (mnt->mnt_pinned) {
240                 atomic_inc(&mnt->mnt_count);
241                 mnt->mnt_pinned--;
242         }
243         spin_unlock(&vfsmount_lock);
244 }
245
246 EXPORT_SYMBOL(mnt_unpin);
247
248 /* iterator */
249 static void *m_start(struct seq_file *m, loff_t *pos)
250 {
251         struct namespace *n = m->private;
252         struct list_head *p;
253         loff_t l = *pos;
254
255         down_read(&namespace_sem);
256         list_for_each(p, &n->list)
257                 if (!l--)
258                         return list_entry(p, struct vfsmount, mnt_list);
259         return NULL;
260 }
261
262 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
263 {
264         struct namespace *n = m->private;
265         struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
266         (*pos)++;
267         return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
268 }
269
270 static void m_stop(struct seq_file *m, void *v)
271 {
272         up_read(&namespace_sem);
273 }
274
275 static inline void mangle(struct seq_file *m, const char *s)
276 {
277         seq_escape(m, s, " \t\n\\");
278 }
279
280 static int show_vfsmnt(struct seq_file *m, void *v)
281 {
282         struct vfsmount *mnt = v;
283         int err = 0;
284         static struct proc_fs_info {
285                 int flag;
286                 char *str;
287         } fs_info[] = {
288                 { MS_SYNCHRONOUS, ",sync" },
289                 { MS_DIRSYNC, ",dirsync" },
290                 { MS_MANDLOCK, ",mand" },
291                 { MS_NOATIME, ",noatime" },
292                 { MS_NODIRATIME, ",nodiratime" },
293                 { 0, NULL }
294         };
295         static struct proc_fs_info mnt_info[] = {
296                 { MNT_NOSUID, ",nosuid" },
297                 { MNT_NODEV, ",nodev" },
298                 { MNT_NOEXEC, ",noexec" },
299                 { 0, NULL }
300         };
301         struct proc_fs_info *fs_infop;
302
303         mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
304         seq_putc(m, ' ');
305         seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
306         seq_putc(m, ' ');
307         mangle(m, mnt->mnt_sb->s_type->name);
308         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
309         for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
310                 if (mnt->mnt_sb->s_flags & fs_infop->flag)
311                         seq_puts(m, fs_infop->str);
312         }
313         for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
314                 if (mnt->mnt_flags & fs_infop->flag)
315                         seq_puts(m, fs_infop->str);
316         }
317         if (mnt->mnt_sb->s_op->show_options)
318                 err = mnt->mnt_sb->s_op->show_options(m, mnt);
319         seq_puts(m, " 0 0\n");
320         return err;
321 }
322
323 struct seq_operations mounts_op = {
324         .start  = m_start,
325         .next   = m_next,
326         .stop   = m_stop,
327         .show   = show_vfsmnt
328 };
329
330 /**
331  * may_umount_tree - check if a mount tree is busy
332  * @mnt: root of mount tree
333  *
334  * This is called to check if a tree of mounts has any
335  * open files, pwds, chroots or sub mounts that are
336  * busy.
337  */
338 int may_umount_tree(struct vfsmount *mnt)
339 {
340         int actual_refs = 0;
341         int minimum_refs = 0;
342         struct vfsmount *p;
343
344         spin_lock(&vfsmount_lock);
345         for (p = mnt; p; p = next_mnt(p, mnt)) {
346                 actual_refs += atomic_read(&p->mnt_count);
347                 minimum_refs += 2;
348         }
349         spin_unlock(&vfsmount_lock);
350
351         if (actual_refs > minimum_refs)
352                 return -EBUSY;
353
354         return 0;
355 }
356
357 EXPORT_SYMBOL(may_umount_tree);
358
359 /**
360  * may_umount - check if a mount point is busy
361  * @mnt: root of mount
362  *
363  * This is called to check if a mount point has any
364  * open files, pwds, chroots or sub mounts. If the
365  * mount has sub mounts this will return busy
366  * regardless of whether the sub mounts are busy.
367  *
368  * Doesn't take quota and stuff into account. IOW, in some cases it will
369  * give false negatives. The main reason why it's here is that we need
370  * a non-destructive way to look for easily umountable filesystems.
371  */
372 int may_umount(struct vfsmount *mnt)
373 {
374         if (atomic_read(&mnt->mnt_count) > 2)
375                 return -EBUSY;
376         return 0;
377 }
378
379 EXPORT_SYMBOL(may_umount);
380
381 static void release_mounts(struct list_head *head)
382 {
383         struct vfsmount *mnt;
384         while(!list_empty(head)) {
385                 mnt = list_entry(head->next, struct vfsmount, mnt_hash);
386                 list_del_init(&mnt->mnt_hash);
387                 if (mnt->mnt_parent != mnt) {
388                         struct dentry *dentry;
389                         struct vfsmount *m;
390                         spin_lock(&vfsmount_lock);
391                         dentry = mnt->mnt_mountpoint;
392                         m = mnt->mnt_parent;
393                         mnt->mnt_mountpoint = mnt->mnt_root;
394                         mnt->mnt_parent = mnt;
395                         spin_unlock(&vfsmount_lock);
396                         dput(dentry);
397                         mntput(m);
398                 }
399                 mntput(mnt);
400         }
401 }
402
403 static void umount_tree(struct vfsmount *mnt, struct list_head *kill)
404 {
405         struct vfsmount *p;
406
407         for (p = mnt; p; p = next_mnt(p, mnt)) {
408                 list_del(&p->mnt_hash);
409                 list_add(&p->mnt_hash, kill);
410         }
411
412         list_for_each_entry(p, kill, mnt_hash) {
413                 list_del_init(&p->mnt_expire);
414                 list_del_init(&p->mnt_list);
415                 __touch_namespace(p->mnt_namespace);
416                 p->mnt_namespace = NULL;
417                 list_del_init(&p->mnt_child);
418                 if (p->mnt_parent != p)
419                         mnt->mnt_mountpoint->d_mounted--;
420         }
421 }
422
423 static int do_umount(struct vfsmount *mnt, int flags)
424 {
425         struct super_block *sb = mnt->mnt_sb;
426         int retval;
427         LIST_HEAD(umount_list);
428
429         retval = security_sb_umount(mnt, flags);
430         if (retval)
431                 return retval;
432
433         /*
434          * Allow userspace to request a mountpoint be expired rather than
435          * unmounting unconditionally. Unmount only happens if:
436          *  (1) the mark is already set (the mark is cleared by mntput())
437          *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
438          */
439         if (flags & MNT_EXPIRE) {
440                 if (mnt == current->fs->rootmnt ||
441                     flags & (MNT_FORCE | MNT_DETACH))
442                         return -EINVAL;
443
444                 if (atomic_read(&mnt->mnt_count) != 2)
445                         return -EBUSY;
446
447                 if (!xchg(&mnt->mnt_expiry_mark, 1))
448                         return -EAGAIN;
449         }
450
451         /*
452          * If we may have to abort operations to get out of this
453          * mount, and they will themselves hold resources we must
454          * allow the fs to do things. In the Unix tradition of
455          * 'Gee thats tricky lets do it in userspace' the umount_begin
456          * might fail to complete on the first run through as other tasks
457          * must return, and the like. Thats for the mount program to worry
458          * about for the moment.
459          */
460
461         lock_kernel();
462         if ((flags & MNT_FORCE) && sb->s_op->umount_begin)
463                 sb->s_op->umount_begin(sb);
464         unlock_kernel();
465
466         /*
467          * No sense to grab the lock for this test, but test itself looks
468          * somewhat bogus. Suggestions for better replacement?
469          * Ho-hum... In principle, we might treat that as umount + switch
470          * to rootfs. GC would eventually take care of the old vfsmount.
471          * Actually it makes sense, especially if rootfs would contain a
472          * /reboot - static binary that would close all descriptors and
473          * call reboot(9). Then init(8) could umount root and exec /reboot.
474          */
475         if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
476                 /*
477                  * Special case for "unmounting" root ...
478                  * we just try to remount it readonly.
479                  */
480                 down_write(&sb->s_umount);
481                 if (!(sb->s_flags & MS_RDONLY)) {
482                         lock_kernel();
483                         DQUOT_OFF(sb);
484                         retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
485                         unlock_kernel();
486                 }
487                 up_write(&sb->s_umount);
488                 return retval;
489         }
490
491         down_write(&namespace_sem);
492         spin_lock(&vfsmount_lock);
493         event++;
494
495         retval = -EBUSY;
496         if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
497                 if (!list_empty(&mnt->mnt_list))
498                         umount_tree(mnt, &umount_list);
499                 retval = 0;
500         }
501         spin_unlock(&vfsmount_lock);
502         if (retval)
503                 security_sb_umount_busy(mnt);
504         up_write(&namespace_sem);
505         release_mounts(&umount_list);
506         return retval;
507 }
508
509 /*
510  * Now umount can handle mount points as well as block devices.
511  * This is important for filesystems which use unnamed block devices.
512  *
513  * We now support a flag for forced unmount like the other 'big iron'
514  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
515  */
516
517 asmlinkage long sys_umount(char __user * name, int flags)
518 {
519         struct nameidata nd;
520         int retval;
521
522         retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
523         if (retval)
524                 goto out;
525         retval = -EINVAL;
526         if (nd.dentry != nd.mnt->mnt_root)
527                 goto dput_and_out;
528         if (!check_mnt(nd.mnt))
529                 goto dput_and_out;
530
531         retval = -EPERM;
532         if (!capable(CAP_SYS_ADMIN))
533                 goto dput_and_out;
534
535         retval = do_umount(nd.mnt, flags);
536 dput_and_out:
537         path_release_on_umount(&nd);
538 out:
539         return retval;
540 }
541
542 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
543
544 /*
545  *      The 2.0 compatible umount. No flags.
546  */
547 asmlinkage long sys_oldumount(char __user * name)
548 {
549         return sys_umount(name, 0);
550 }
551
552 #endif
553
554 static int mount_is_safe(struct nameidata *nd)
555 {
556         if (capable(CAP_SYS_ADMIN))
557                 return 0;
558         return -EPERM;
559 #ifdef notyet
560         if (S_ISLNK(nd->dentry->d_inode->i_mode))
561                 return -EPERM;
562         if (nd->dentry->d_inode->i_mode & S_ISVTX) {
563                 if (current->uid != nd->dentry->d_inode->i_uid)
564                         return -EPERM;
565         }
566         if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
567                 return -EPERM;
568         return 0;
569 #endif
570 }
571
572 static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
573 {
574         while (1) {
575                 if (d == dentry)
576                         return 1;
577                 if (d == NULL || d == d->d_parent)
578                         return 0;
579                 d = d->d_parent;
580         }
581 }
582
583 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
584                                         int flag)
585 {
586         struct vfsmount *res, *p, *q, *r, *s;
587         struct nameidata nd;
588
589         res = q = clone_mnt(mnt, dentry, flag);
590         if (!q)
591                 goto Enomem;
592         q->mnt_mountpoint = mnt->mnt_mountpoint;
593
594         p = mnt;
595         list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
596                 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
597                         continue;
598
599                 for (s = r; s; s = next_mnt(s, r)) {
600                         while (p != s->mnt_parent) {
601                                 p = p->mnt_parent;
602                                 q = q->mnt_parent;
603                         }
604                         p = s;
605                         nd.mnt = q;
606                         nd.dentry = p->mnt_mountpoint;
607                         q = clone_mnt(p, p->mnt_root, flag);
608                         if (!q)
609                                 goto Enomem;
610                         spin_lock(&vfsmount_lock);
611                         list_add_tail(&q->mnt_list, &res->mnt_list);
612                         attach_mnt(q, &nd);
613                         spin_unlock(&vfsmount_lock);
614                 }
615         }
616         return res;
617 Enomem:
618         if (res) {
619                 LIST_HEAD(umount_list);
620                 spin_lock(&vfsmount_lock);
621                 umount_tree(res, &umount_list);
622                 spin_unlock(&vfsmount_lock);
623                 release_mounts(&umount_list);
624         }
625         return NULL;
626 }
627
628 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
629 {
630         int err;
631         if (mnt->mnt_sb->s_flags & MS_NOUSER)
632                 return -EINVAL;
633
634         if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
635               S_ISDIR(mnt->mnt_root->d_inode->i_mode))
636                 return -ENOTDIR;
637
638         err = -ENOENT;
639         down(&nd->dentry->d_inode->i_sem);
640         if (IS_DEADDIR(nd->dentry->d_inode))
641                 goto out_unlock;
642
643         err = security_sb_check_sb(mnt, nd);
644         if (err)
645                 goto out_unlock;
646
647         err = -ENOENT;
648         spin_lock(&vfsmount_lock);
649         if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
650                 struct list_head head;
651
652                 attach_mnt(mnt, nd);
653                 list_add_tail(&head, &mnt->mnt_list);
654                 list_splice(&head, current->namespace->list.prev);
655                 err = 0;
656                 touch_namespace(current->namespace);
657         }
658         spin_unlock(&vfsmount_lock);
659 out_unlock:
660         up(&nd->dentry->d_inode->i_sem);
661         if (!err)
662                 security_sb_post_addmount(mnt, nd);
663         return err;
664 }
665
666 /*
667  * recursively change the type of the mountpoint.
668  */
669 static int do_change_type(struct nameidata *nd, int flag)
670 {
671         struct vfsmount *m, *mnt = nd->mnt;
672         int recurse = flag & MS_REC;
673         int type = flag & ~MS_REC;
674
675         if (nd->dentry != nd->mnt->mnt_root)
676                 return -EINVAL;
677
678         down_write(&namespace_sem);
679         spin_lock(&vfsmount_lock);
680         for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
681                 change_mnt_propagation(m, type);
682         spin_unlock(&vfsmount_lock);
683         up_write(&namespace_sem);
684         return 0;
685 }
686
687 /*
688  * do loopback mount.
689  */
690 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
691 {
692         struct nameidata old_nd;
693         struct vfsmount *mnt = NULL;
694         int err = mount_is_safe(nd);
695         if (err)
696                 return err;
697         if (!old_name || !*old_name)
698                 return -EINVAL;
699         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
700         if (err)
701                 return err;
702
703         down_write(&namespace_sem);
704         err = -EINVAL;
705         if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
706                 goto out;
707
708         err = -ENOMEM;
709         if (recurse)
710                 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
711         else
712                 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
713
714         if (!mnt)
715                 goto out;
716
717         err = graft_tree(mnt, nd);
718         if (err) {
719                 LIST_HEAD(umount_list);
720                 spin_lock(&vfsmount_lock);
721                 umount_tree(mnt, &umount_list);
722                 spin_unlock(&vfsmount_lock);
723                 release_mounts(&umount_list);
724         }
725
726 out:
727         up_write(&namespace_sem);
728         path_release(&old_nd);
729         return err;
730 }
731
732 /*
733  * change filesystem flags. dir should be a physical root of filesystem.
734  * If you've mounted a non-root directory somewhere and want to do remount
735  * on it - tough luck.
736  */
737 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
738                       void *data)
739 {
740         int err;
741         struct super_block *sb = nd->mnt->mnt_sb;
742
743         if (!capable(CAP_SYS_ADMIN))
744                 return -EPERM;
745
746         if (!check_mnt(nd->mnt))
747                 return -EINVAL;
748
749         if (nd->dentry != nd->mnt->mnt_root)
750                 return -EINVAL;
751
752         down_write(&sb->s_umount);
753         err = do_remount_sb(sb, flags, data, 0);
754         if (!err)
755                 nd->mnt->mnt_flags = mnt_flags;
756         up_write(&sb->s_umount);
757         if (!err)
758                 security_sb_post_remount(nd->mnt, flags, data);
759         return err;
760 }
761
762 static int do_move_mount(struct nameidata *nd, char *old_name)
763 {
764         struct nameidata old_nd, parent_nd;
765         struct vfsmount *p;
766         int err = 0;
767         if (!capable(CAP_SYS_ADMIN))
768                 return -EPERM;
769         if (!old_name || !*old_name)
770                 return -EINVAL;
771         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
772         if (err)
773                 return err;
774
775         down_write(&namespace_sem);
776         while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
777                 ;
778         err = -EINVAL;
779         if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
780                 goto out;
781
782         err = -ENOENT;
783         down(&nd->dentry->d_inode->i_sem);
784         if (IS_DEADDIR(nd->dentry->d_inode))
785                 goto out1;
786
787         spin_lock(&vfsmount_lock);
788         if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
789                 goto out2;
790
791         err = -EINVAL;
792         if (old_nd.dentry != old_nd.mnt->mnt_root)
793                 goto out2;
794
795         if (old_nd.mnt == old_nd.mnt->mnt_parent)
796                 goto out2;
797
798         if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
799               S_ISDIR(old_nd.dentry->d_inode->i_mode))
800                 goto out2;
801
802         err = -ELOOP;
803         for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
804                 if (p == old_nd.mnt)
805                         goto out2;
806         err = 0;
807
808         detach_mnt(old_nd.mnt, &parent_nd);
809         attach_mnt(old_nd.mnt, nd);
810         touch_namespace(current->namespace);
811
812         /* if the mount is moved, it should no longer be expire
813          * automatically */
814         list_del_init(&old_nd.mnt->mnt_expire);
815 out2:
816         spin_unlock(&vfsmount_lock);
817 out1:
818         up(&nd->dentry->d_inode->i_sem);
819 out:
820         up_write(&namespace_sem);
821         if (!err)
822                 path_release(&parent_nd);
823         path_release(&old_nd);
824         return err;
825 }
826
827 /*
828  * create a new mount for userspace and request it to be added into the
829  * namespace's tree
830  */
831 static int do_new_mount(struct nameidata *nd, char *type, int flags,
832                         int mnt_flags, char *name, void *data)
833 {
834         struct vfsmount *mnt;
835
836         if (!type || !memchr(type, 0, PAGE_SIZE))
837                 return -EINVAL;
838
839         /* we need capabilities... */
840         if (!capable(CAP_SYS_ADMIN))
841                 return -EPERM;
842
843         mnt = do_kern_mount(type, flags, name, data);
844         if (IS_ERR(mnt))
845                 return PTR_ERR(mnt);
846
847         return do_add_mount(mnt, nd, mnt_flags, NULL);
848 }
849
850 /*
851  * add a mount into a namespace's mount tree
852  * - provide the option of adding the new mount to an expiration list
853  */
854 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
855                  int mnt_flags, struct list_head *fslist)
856 {
857         int err;
858
859         down_write(&namespace_sem);
860         /* Something was mounted here while we slept */
861         while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
862                 ;
863         err = -EINVAL;
864         if (!check_mnt(nd->mnt))
865                 goto unlock;
866
867         /* Refuse the same filesystem on the same mount point */
868         err = -EBUSY;
869         if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
870             nd->mnt->mnt_root == nd->dentry)
871                 goto unlock;
872
873         err = -EINVAL;
874         if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
875                 goto unlock;
876
877         newmnt->mnt_flags = mnt_flags;
878         if ((err = graft_tree(newmnt, nd)))
879                 goto unlock;
880
881         if (fslist) {
882                 /* add to the specified expiration list */
883                 spin_lock(&vfsmount_lock);
884                 list_add_tail(&newmnt->mnt_expire, fslist);
885                 spin_unlock(&vfsmount_lock);
886         }
887         up_write(&namespace_sem);
888         return 0;
889
890 unlock:
891         up_write(&namespace_sem);
892         mntput(newmnt);
893         return err;
894 }
895
896 EXPORT_SYMBOL_GPL(do_add_mount);
897
898 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
899                                 struct list_head *umounts)
900 {
901         spin_lock(&vfsmount_lock);
902
903         /*
904          * Check if mount is still attached, if not, let whoever holds it deal
905          * with the sucker
906          */
907         if (mnt->mnt_parent == mnt) {
908                 spin_unlock(&vfsmount_lock);
909                 return;
910         }
911
912         /*
913          * Check that it is still dead: the count should now be 2 - as
914          * contributed by the vfsmount parent and the mntget above
915          */
916         if (atomic_read(&mnt->mnt_count) == 2) {
917                 /* delete from the namespace */
918                 touch_namespace(mnt->mnt_namespace);
919                 list_del_init(&mnt->mnt_list);
920                 mnt->mnt_namespace = NULL;
921                 umount_tree(mnt, umounts);
922                 spin_unlock(&vfsmount_lock);
923         } else {
924                 /*
925                  * Someone brought it back to life whilst we didn't have any
926                  * locks held so return it to the expiration list
927                  */
928                 list_add_tail(&mnt->mnt_expire, mounts);
929                 spin_unlock(&vfsmount_lock);
930         }
931 }
932
933 /*
934  * process a list of expirable mountpoints with the intent of discarding any
935  * mountpoints that aren't in use and haven't been touched since last we came
936  * here
937  */
938 void mark_mounts_for_expiry(struct list_head *mounts)
939 {
940         struct namespace *namespace;
941         struct vfsmount *mnt, *next;
942         LIST_HEAD(graveyard);
943
944         if (list_empty(mounts))
945                 return;
946
947         spin_lock(&vfsmount_lock);
948
949         /* extract from the expiration list every vfsmount that matches the
950          * following criteria:
951          * - only referenced by its parent vfsmount
952          * - still marked for expiry (marked on the last call here; marks are
953          *   cleared by mntput())
954          */
955         list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
956                 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
957                     atomic_read(&mnt->mnt_count) != 1)
958                         continue;
959
960                 mntget(mnt);
961                 list_move(&mnt->mnt_expire, &graveyard);
962         }
963
964         /*
965          * go through the vfsmounts we've just consigned to the graveyard to
966          * - check that they're still dead
967          * - delete the vfsmount from the appropriate namespace under lock
968          * - dispose of the corpse
969          */
970         while (!list_empty(&graveyard)) {
971                 LIST_HEAD(umounts);
972                 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
973                 list_del_init(&mnt->mnt_expire);
974
975                 /* don't do anything if the namespace is dead - all the
976                  * vfsmounts from it are going away anyway */
977                 namespace = mnt->mnt_namespace;
978                 if (!namespace || !namespace->root)
979                         continue;
980                 get_namespace(namespace);
981
982                 spin_unlock(&vfsmount_lock);
983                 down_write(&namespace_sem);
984                 expire_mount(mnt, mounts, &umounts);
985                 up_write(&namespace_sem);
986                 release_mounts(&umounts);
987                 mntput(mnt);
988                 put_namespace(namespace);
989                 spin_lock(&vfsmount_lock);
990         }
991
992         spin_unlock(&vfsmount_lock);
993 }
994
995 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
996
997 /*
998  * Some copy_from_user() implementations do not return the exact number of
999  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
1000  * Note that this function differs from copy_from_user() in that it will oops
1001  * on bad values of `to', rather than returning a short copy.
1002  */
1003 static long exact_copy_from_user(void *to, const void __user * from,
1004                                  unsigned long n)
1005 {
1006         char *t = to;
1007         const char __user *f = from;
1008         char c;
1009
1010         if (!access_ok(VERIFY_READ, from, n))
1011                 return n;
1012
1013         while (n) {
1014                 if (__get_user(c, f)) {
1015                         memset(t, 0, n);
1016                         break;
1017                 }
1018                 *t++ = c;
1019                 f++;
1020                 n--;
1021         }
1022         return n;
1023 }
1024
1025 int copy_mount_options(const void __user * data, unsigned long *where)
1026 {
1027         int i;
1028         unsigned long page;
1029         unsigned long size;
1030
1031         *where = 0;
1032         if (!data)
1033                 return 0;
1034
1035         if (!(page = __get_free_page(GFP_KERNEL)))
1036                 return -ENOMEM;
1037
1038         /* We only care that *some* data at the address the user
1039          * gave us is valid.  Just in case, we'll zero
1040          * the remainder of the page.
1041          */
1042         /* copy_from_user cannot cross TASK_SIZE ! */
1043         size = TASK_SIZE - (unsigned long)data;
1044         if (size > PAGE_SIZE)
1045                 size = PAGE_SIZE;
1046
1047         i = size - exact_copy_from_user((void *)page, data, size);
1048         if (!i) {
1049                 free_page(page);
1050                 return -EFAULT;
1051         }
1052         if (i != PAGE_SIZE)
1053                 memset((char *)page + i, 0, PAGE_SIZE - i);
1054         *where = page;
1055         return 0;
1056 }
1057
1058 /*
1059  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1060  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1061  *
1062  * data is a (void *) that can point to any structure up to
1063  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1064  * information (or be NULL).
1065  *
1066  * Pre-0.97 versions of mount() didn't have a flags word.
1067  * When the flags word was introduced its top half was required
1068  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1069  * Therefore, if this magic number is present, it carries no information
1070  * and must be discarded.
1071  */
1072 long do_mount(char *dev_name, char *dir_name, char *type_page,
1073                   unsigned long flags, void *data_page)
1074 {
1075         struct nameidata nd;
1076         int retval = 0;
1077         int mnt_flags = 0;
1078
1079         /* Discard magic */
1080         if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1081                 flags &= ~MS_MGC_MSK;
1082
1083         /* Basic sanity checks */
1084
1085         if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1086                 return -EINVAL;
1087         if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1088                 return -EINVAL;
1089
1090         if (data_page)
1091                 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1092
1093         /* Separate the per-mountpoint flags */
1094         if (flags & MS_NOSUID)
1095                 mnt_flags |= MNT_NOSUID;
1096         if (flags & MS_NODEV)
1097                 mnt_flags |= MNT_NODEV;
1098         if (flags & MS_NOEXEC)
1099                 mnt_flags |= MNT_NOEXEC;
1100         flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE);
1101
1102         /* ... and get the mountpoint */
1103         retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1104         if (retval)
1105                 return retval;
1106
1107         retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1108         if (retval)
1109                 goto dput_out;
1110
1111         if (flags & MS_REMOUNT)
1112                 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1113                                     data_page);
1114         else if (flags & MS_BIND)
1115                 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1116         else if (flags & MS_PRIVATE)
1117                 retval = do_change_type(&nd, flags);
1118         else if (flags & MS_MOVE)
1119                 retval = do_move_mount(&nd, dev_name);
1120         else
1121                 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1122                                       dev_name, data_page);
1123 dput_out:
1124         path_release(&nd);
1125         return retval;
1126 }
1127
1128 int copy_namespace(int flags, struct task_struct *tsk)
1129 {
1130         struct namespace *namespace = tsk->namespace;
1131         struct namespace *new_ns;
1132         struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1133         struct fs_struct *fs = tsk->fs;
1134         struct vfsmount *p, *q;
1135
1136         if (!namespace)
1137                 return 0;
1138
1139         get_namespace(namespace);
1140
1141         if (!(flags & CLONE_NEWNS))
1142                 return 0;
1143
1144         if (!capable(CAP_SYS_ADMIN)) {
1145                 put_namespace(namespace);
1146                 return -EPERM;
1147         }
1148
1149         new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1150         if (!new_ns)
1151                 goto out;
1152
1153         atomic_set(&new_ns->count, 1);
1154         INIT_LIST_HEAD(&new_ns->list);
1155         init_waitqueue_head(&new_ns->poll);
1156         new_ns->event = 0;
1157
1158         down_write(&namespace_sem);
1159         /* First pass: copy the tree topology */
1160         new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
1161                                         CL_EXPIRE);
1162         if (!new_ns->root) {
1163                 up_write(&namespace_sem);
1164                 kfree(new_ns);
1165                 goto out;
1166         }
1167         spin_lock(&vfsmount_lock);
1168         list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1169         spin_unlock(&vfsmount_lock);
1170
1171         /*
1172          * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1173          * as belonging to new namespace.  We have already acquired a private
1174          * fs_struct, so tsk->fs->lock is not needed.
1175          */
1176         p = namespace->root;
1177         q = new_ns->root;
1178         while (p) {
1179                 q->mnt_namespace = new_ns;
1180                 if (fs) {
1181                         if (p == fs->rootmnt) {
1182                                 rootmnt = p;
1183                                 fs->rootmnt = mntget(q);
1184                         }
1185                         if (p == fs->pwdmnt) {
1186                                 pwdmnt = p;
1187                                 fs->pwdmnt = mntget(q);
1188                         }
1189                         if (p == fs->altrootmnt) {
1190                                 altrootmnt = p;
1191                                 fs->altrootmnt = mntget(q);
1192                         }
1193                 }
1194                 p = next_mnt(p, namespace->root);
1195                 q = next_mnt(q, new_ns->root);
1196         }
1197         up_write(&namespace_sem);
1198
1199         tsk->namespace = new_ns;
1200
1201         if (rootmnt)
1202                 mntput(rootmnt);
1203         if (pwdmnt)
1204                 mntput(pwdmnt);
1205         if (altrootmnt)
1206                 mntput(altrootmnt);
1207
1208         put_namespace(namespace);
1209         return 0;
1210
1211 out:
1212         put_namespace(namespace);
1213         return -ENOMEM;
1214 }
1215
1216 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1217                           char __user * type, unsigned long flags,
1218                           void __user * data)
1219 {
1220         int retval;
1221         unsigned long data_page;
1222         unsigned long type_page;
1223         unsigned long dev_page;
1224         char *dir_page;
1225
1226         retval = copy_mount_options(type, &type_page);
1227         if (retval < 0)
1228                 return retval;
1229
1230         dir_page = getname(dir_name);
1231         retval = PTR_ERR(dir_page);
1232         if (IS_ERR(dir_page))
1233                 goto out1;
1234
1235         retval = copy_mount_options(dev_name, &dev_page);
1236         if (retval < 0)
1237                 goto out2;
1238
1239         retval = copy_mount_options(data, &data_page);
1240         if (retval < 0)
1241                 goto out3;
1242
1243         lock_kernel();
1244         retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1245                           flags, (void *)data_page);
1246         unlock_kernel();
1247         free_page(data_page);
1248
1249 out3:
1250         free_page(dev_page);
1251 out2:
1252         putname(dir_page);
1253 out1:
1254         free_page(type_page);
1255         return retval;
1256 }
1257
1258 /*
1259  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1260  * It can block. Requires the big lock held.
1261  */
1262 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1263                  struct dentry *dentry)
1264 {
1265         struct dentry *old_root;
1266         struct vfsmount *old_rootmnt;
1267         write_lock(&fs->lock);
1268         old_root = fs->root;
1269         old_rootmnt = fs->rootmnt;
1270         fs->rootmnt = mntget(mnt);
1271         fs->root = dget(dentry);
1272         write_unlock(&fs->lock);
1273         if (old_root) {
1274                 dput(old_root);
1275                 mntput(old_rootmnt);
1276         }
1277 }
1278
1279 /*
1280  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1281  * It can block. Requires the big lock held.
1282  */
1283 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1284                 struct dentry *dentry)
1285 {
1286         struct dentry *old_pwd;
1287         struct vfsmount *old_pwdmnt;
1288
1289         write_lock(&fs->lock);
1290         old_pwd = fs->pwd;
1291         old_pwdmnt = fs->pwdmnt;
1292         fs->pwdmnt = mntget(mnt);
1293         fs->pwd = dget(dentry);
1294         write_unlock(&fs->lock);
1295
1296         if (old_pwd) {
1297                 dput(old_pwd);
1298                 mntput(old_pwdmnt);
1299         }
1300 }
1301
1302 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1303 {
1304         struct task_struct *g, *p;
1305         struct fs_struct *fs;
1306
1307         read_lock(&tasklist_lock);
1308         do_each_thread(g, p) {
1309                 task_lock(p);
1310                 fs = p->fs;
1311                 if (fs) {
1312                         atomic_inc(&fs->count);
1313                         task_unlock(p);
1314                         if (fs->root == old_nd->dentry
1315                             && fs->rootmnt == old_nd->mnt)
1316                                 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1317                         if (fs->pwd == old_nd->dentry
1318                             && fs->pwdmnt == old_nd->mnt)
1319                                 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1320                         put_fs_struct(fs);
1321                 } else
1322                         task_unlock(p);
1323         } while_each_thread(g, p);
1324         read_unlock(&tasklist_lock);
1325 }
1326
1327 /*
1328  * pivot_root Semantics:
1329  * Moves the root file system of the current process to the directory put_old,
1330  * makes new_root as the new root file system of the current process, and sets
1331  * root/cwd of all processes which had them on the current root to new_root.
1332  *
1333  * Restrictions:
1334  * The new_root and put_old must be directories, and  must not be on the
1335  * same file  system as the current process root. The put_old  must  be
1336  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
1337  * pointed to by put_old must yield the same directory as new_root. No other
1338  * file system may be mounted on put_old. After all, new_root is a mountpoint.
1339  *
1340  * Notes:
1341  *  - we don't move root/cwd if they are not at the root (reason: if something
1342  *    cared enough to change them, it's probably wrong to force them elsewhere)
1343  *  - it's okay to pick a root that isn't the root of a file system, e.g.
1344  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1345  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1346  *    first.
1347  */
1348 asmlinkage long sys_pivot_root(const char __user * new_root,
1349                                const char __user * put_old)
1350 {
1351         struct vfsmount *tmp;
1352         struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1353         int error;
1354
1355         if (!capable(CAP_SYS_ADMIN))
1356                 return -EPERM;
1357
1358         lock_kernel();
1359
1360         error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1361                             &new_nd);
1362         if (error)
1363                 goto out0;
1364         error = -EINVAL;
1365         if (!check_mnt(new_nd.mnt))
1366                 goto out1;
1367
1368         error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1369         if (error)
1370                 goto out1;
1371
1372         error = security_sb_pivotroot(&old_nd, &new_nd);
1373         if (error) {
1374                 path_release(&old_nd);
1375                 goto out1;
1376         }
1377
1378         read_lock(&current->fs->lock);
1379         user_nd.mnt = mntget(current->fs->rootmnt);
1380         user_nd.dentry = dget(current->fs->root);
1381         read_unlock(&current->fs->lock);
1382         down_write(&namespace_sem);
1383         down(&old_nd.dentry->d_inode->i_sem);
1384         error = -EINVAL;
1385         if (!check_mnt(user_nd.mnt))
1386                 goto out2;
1387         error = -ENOENT;
1388         if (IS_DEADDIR(new_nd.dentry->d_inode))
1389                 goto out2;
1390         if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1391                 goto out2;
1392         if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1393                 goto out2;
1394         error = -EBUSY;
1395         if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1396                 goto out2; /* loop, on the same file system  */
1397         error = -EINVAL;
1398         if (user_nd.mnt->mnt_root != user_nd.dentry)
1399                 goto out2; /* not a mountpoint */
1400         if (user_nd.mnt->mnt_parent == user_nd.mnt)
1401                 goto out2; /* not attached */
1402         if (new_nd.mnt->mnt_root != new_nd.dentry)
1403                 goto out2; /* not a mountpoint */
1404         if (new_nd.mnt->mnt_parent == new_nd.mnt)
1405                 goto out2; /* not attached */
1406         tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1407         spin_lock(&vfsmount_lock);
1408         if (tmp != new_nd.mnt) {
1409                 for (;;) {
1410                         if (tmp->mnt_parent == tmp)
1411                                 goto out3; /* already mounted on put_old */
1412                         if (tmp->mnt_parent == new_nd.mnt)
1413                                 break;
1414                         tmp = tmp->mnt_parent;
1415                 }
1416                 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1417                         goto out3;
1418         } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1419                 goto out3;
1420         detach_mnt(new_nd.mnt, &parent_nd);
1421         detach_mnt(user_nd.mnt, &root_parent);
1422         attach_mnt(user_nd.mnt, &old_nd);     /* mount old root on put_old */
1423         attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1424         touch_namespace(current->namespace);
1425         spin_unlock(&vfsmount_lock);
1426         chroot_fs_refs(&user_nd, &new_nd);
1427         security_sb_post_pivotroot(&user_nd, &new_nd);
1428         error = 0;
1429         path_release(&root_parent);
1430         path_release(&parent_nd);
1431 out2:
1432         up(&old_nd.dentry->d_inode->i_sem);
1433         up_write(&namespace_sem);
1434         path_release(&user_nd);
1435         path_release(&old_nd);
1436 out1:
1437         path_release(&new_nd);
1438 out0:
1439         unlock_kernel();
1440         return error;
1441 out3:
1442         spin_unlock(&vfsmount_lock);
1443         goto out2;
1444 }
1445
1446 static void __init init_mount_tree(void)
1447 {
1448         struct vfsmount *mnt;
1449         struct namespace *namespace;
1450         struct task_struct *g, *p;
1451
1452         mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1453         if (IS_ERR(mnt))
1454                 panic("Can't create rootfs");
1455         namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1456         if (!namespace)
1457                 panic("Can't allocate initial namespace");
1458         atomic_set(&namespace->count, 1);
1459         INIT_LIST_HEAD(&namespace->list);
1460         init_waitqueue_head(&namespace->poll);
1461         namespace->event = 0;
1462         list_add(&mnt->mnt_list, &namespace->list);
1463         namespace->root = mnt;
1464         mnt->mnt_namespace = namespace;
1465
1466         init_task.namespace = namespace;
1467         read_lock(&tasklist_lock);
1468         do_each_thread(g, p) {
1469                 get_namespace(namespace);
1470                 p->namespace = namespace;
1471         } while_each_thread(g, p);
1472         read_unlock(&tasklist_lock);
1473
1474         set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1475         set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1476 }
1477
1478 void __init mnt_init(unsigned long mempages)
1479 {
1480         struct list_head *d;
1481         unsigned int nr_hash;
1482         int i;
1483
1484         init_rwsem(&namespace_sem);
1485
1486         mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1487                         0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
1488
1489         mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1490
1491         if (!mount_hashtable)
1492                 panic("Failed to allocate mount hash table\n");
1493
1494         /*
1495          * Find the power-of-two list-heads that can fit into the allocation..
1496          * We don't guarantee that "sizeof(struct list_head)" is necessarily
1497          * a power-of-two.
1498          */
1499         nr_hash = PAGE_SIZE / sizeof(struct list_head);
1500         hash_bits = 0;
1501         do {
1502                 hash_bits++;
1503         } while ((nr_hash >> hash_bits) != 0);
1504         hash_bits--;
1505
1506         /*
1507          * Re-calculate the actual number of entries and the mask
1508          * from the number of bits we can fit.
1509          */
1510         nr_hash = 1UL << hash_bits;
1511         hash_mask = nr_hash - 1;
1512
1513         printk("Mount-cache hash table entries: %d\n", nr_hash);
1514
1515         /* And initialize the newly allocated array */
1516         d = mount_hashtable;
1517         i = nr_hash;
1518         do {
1519                 INIT_LIST_HEAD(d);
1520                 d++;
1521                 i--;
1522         } while (i);
1523         sysfs_init();
1524         init_rootfs();
1525         init_mount_tree();
1526 }
1527
1528 void __put_namespace(struct namespace *namespace)
1529 {
1530         struct vfsmount *root = namespace->root;
1531         LIST_HEAD(umount_list);
1532         namespace->root = NULL;
1533         spin_unlock(&vfsmount_lock);
1534         down_write(&namespace_sem);
1535         spin_lock(&vfsmount_lock);
1536         umount_tree(root, &umount_list);
1537         spin_unlock(&vfsmount_lock);
1538         up_write(&namespace_sem);
1539         release_mounts(&umount_list);
1540         kfree(namespace);
1541 }