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