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