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