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