vserver 1.9.3
[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 #include <linux/vserver/namespace.h>
26
27 #include <asm/uaccess.h>
28 #include <asm/unistd.h>
29
30 extern int __init init_rootfs(void);
31
32 #ifdef CONFIG_SYSFS
33 extern int __init sysfs_init(void);
34 #else
35 static inline int sysfs_init(void)
36 {
37         return 0;
38 }
39 #endif
40
41 /* spinlock for vfsmount related operations, inplace of dcache_lock */
42 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
43
44 static struct list_head *mount_hashtable;
45 static int hash_mask, hash_bits;
46 static kmem_cache_t *mnt_cache; 
47
48 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
49 {
50         unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
51         tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
52         tmp = tmp + (tmp >> hash_bits);
53         return tmp & hash_mask;
54 }
55
56 struct vfsmount *alloc_vfsmnt(const char *name)
57 {
58         struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL); 
59         if (mnt) {
60                 memset(mnt, 0, sizeof(struct vfsmount));
61                 atomic_set(&mnt->mnt_count,1);
62                 INIT_LIST_HEAD(&mnt->mnt_hash);
63                 INIT_LIST_HEAD(&mnt->mnt_child);
64                 INIT_LIST_HEAD(&mnt->mnt_mounts);
65                 INIT_LIST_HEAD(&mnt->mnt_list);
66                 INIT_LIST_HEAD(&mnt->mnt_fslink);
67                 if (name) {
68                         int size = strlen(name)+1;
69                         char *newname = kmalloc(size, GFP_KERNEL);
70                         if (newname) {
71                                 memcpy(newname, name, size);
72                                 mnt->mnt_devname = newname;
73                         }
74                 }
75         }
76         return mnt;
77 }
78
79 void free_vfsmnt(struct vfsmount *mnt)
80 {
81         kfree(mnt->mnt_devname);
82         kmem_cache_free(mnt_cache, mnt);
83 }
84
85 /*
86  * Now, lookup_mnt increments the ref count before returning
87  * the vfsmount struct.
88  */
89 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
90 {
91         struct list_head * head = mount_hashtable + hash(mnt, dentry);
92         struct list_head * tmp = head;
93         struct vfsmount *p, *found = NULL;
94
95         spin_lock(&vfsmount_lock);
96         for (;;) {
97                 tmp = tmp->next;
98                 p = NULL;
99                 if (tmp == head)
100                         break;
101                 p = list_entry(tmp, struct vfsmount, mnt_hash);
102                 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
103                         found = mntget(p);
104                         break;
105                 }
106         }
107         spin_unlock(&vfsmount_lock);
108         return found;
109 }
110
111 EXPORT_SYMBOL(lookup_mnt);
112
113 static inline int check_mnt(struct vfsmount *mnt)
114 {
115         return mnt->mnt_namespace == current->namespace;
116 }
117
118 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
119 {
120         old_nd->dentry = mnt->mnt_mountpoint;
121         old_nd->mnt = mnt->mnt_parent;
122         mnt->mnt_parent = mnt;
123         mnt->mnt_mountpoint = mnt->mnt_root;
124         list_del_init(&mnt->mnt_child);
125         list_del_init(&mnt->mnt_hash);
126         old_nd->dentry->d_mounted--;
127 }
128
129 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
130 {
131         mnt->mnt_parent = mntget(nd->mnt);
132         mnt->mnt_mountpoint = dget(nd->dentry);
133         list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
134         list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
135         nd->dentry->d_mounted++;
136 }
137
138 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
139 {
140         struct list_head *next = p->mnt_mounts.next;
141         if (next == &p->mnt_mounts) {
142                 while (1) {
143                         if (p == root)
144                                 return NULL;
145                         next = p->mnt_child.next;
146                         if (next != &p->mnt_parent->mnt_mounts)
147                                 break;
148                         p = p->mnt_parent;
149                 }
150         }
151         return list_entry(next, struct vfsmount, mnt_child);
152 }
153
154 static struct vfsmount *
155 clone_mnt(struct vfsmount *old, struct dentry *root)
156 {
157         struct super_block *sb = old->mnt_sb;
158         struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
159
160         if (mnt) {
161                 mnt->mnt_flags = old->mnt_flags;
162                 atomic_inc(&sb->s_active);
163                 mnt->mnt_sb = sb;
164                 mnt->mnt_root = dget(root);
165                 mnt->mnt_mountpoint = mnt->mnt_root;
166                 mnt->mnt_parent = mnt;
167                 mnt->mnt_namespace = old->mnt_namespace;
168
169                 /* stick the duplicate mount on the same expiry list
170                  * as the original if that was on one */
171                 spin_lock(&vfsmount_lock);
172                 if (!list_empty(&old->mnt_fslink))
173                         list_add(&mnt->mnt_fslink, &old->mnt_fslink);
174                 spin_unlock(&vfsmount_lock);
175         }
176         return mnt;
177 }
178
179 void __mntput(struct vfsmount *mnt)
180 {
181         struct super_block *sb = mnt->mnt_sb;
182         dput(mnt->mnt_root);
183         free_vfsmnt(mnt);
184         deactivate_super(sb);
185 }
186
187 EXPORT_SYMBOL(__mntput);
188
189 /* iterator */
190 static void *m_start(struct seq_file *m, loff_t *pos)
191 {
192         struct namespace *n = m->private;
193         struct list_head *p;
194         loff_t l = *pos;
195
196         down_read(&n->sem);
197         list_for_each(p, &n->list)
198                 if (!l--)
199                         return list_entry(p, struct vfsmount, mnt_list);
200         return NULL;
201 }
202
203 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
204 {
205         struct namespace *n = m->private;
206         struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
207         (*pos)++;
208         return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
209 }
210
211 static void m_stop(struct seq_file *m, void *v)
212 {
213         struct namespace *n = m->private;
214         up_read(&n->sem);
215 }
216
217 static inline void mangle(struct seq_file *m, const char *s)
218 {
219         seq_escape(m, s, " \t\n\\");
220 }
221
222 static int show_vfsmnt(struct seq_file *m, void *v)
223 {
224         struct vfsmount *mnt = v;
225         int err = 0;
226         static struct proc_fs_info {
227                 int flag;
228                 char *str;
229         } fs_info[] = {
230                 { MS_SYNCHRONOUS, ",sync" },
231                 { MS_DIRSYNC, ",dirsync" },
232                 { MS_MANDLOCK, ",mand" },
233                 { MS_NOATIME, ",noatime" },
234                 { MS_NODIRATIME, ",nodiratime" },
235                 { MS_TAGXID, ",tagxid" },
236                 { 0, NULL }
237         };
238         static struct proc_fs_info mnt_info[] = {
239                 { MNT_NOSUID, ",nosuid" },
240                 { MNT_NODEV, ",nodev" },
241                 { MNT_NOEXEC, ",noexec" },
242                 { 0, NULL }
243         };
244         struct proc_fs_info *fs_infop;
245
246         if (vx_flags(VXF_HIDE_MOUNT, 0))
247                 return 0;
248         if (!vx_check_vfsmount(current->vx_info, mnt))
249                 return 0;
250
251         mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
252         seq_putc(m, ' ');
253         seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
254         seq_putc(m, ' ');
255         mangle(m, mnt->mnt_sb->s_type->name);
256         seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
257         for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
258                 if (mnt->mnt_sb->s_flags & fs_infop->flag)
259                         seq_puts(m, fs_infop->str);
260         }
261         for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
262                 if (mnt->mnt_flags & fs_infop->flag)
263                         seq_puts(m, fs_infop->str);
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, int recurse)
651 {
652         struct nameidata old_nd;
653         struct vfsmount *mnt = NULL;
654         int err = mount_is_safe(nd);
655         if (err)
656                 return err;
657         if (!old_name || !*old_name)
658                 return -EINVAL;
659         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
660         if (err)
661                 return err;
662
663         down_write(&current->namespace->sem);
664         err = -EINVAL;
665         if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
666                 err = -ENOMEM;
667                 if (recurse)
668                         mnt = copy_tree(old_nd.mnt, old_nd.dentry);
669                 else
670                         mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
671         }
672
673         if (mnt) {
674                 /* stop bind mounts from expiring */
675                 spin_lock(&vfsmount_lock);
676                 list_del_init(&mnt->mnt_fslink);
677                 spin_unlock(&vfsmount_lock);
678
679                 err = graft_tree(mnt, nd);
680                 if (err) {
681                         spin_lock(&vfsmount_lock);
682                         umount_tree(mnt);
683                         spin_unlock(&vfsmount_lock);
684                 } else
685                         mntput(mnt);
686         }
687
688         up_write(&current->namespace->sem);
689         path_release(&old_nd);
690         return err;
691 }
692
693 /*
694  * change filesystem flags. dir should be a physical root of filesystem.
695  * If you've mounted a non-root directory somewhere and want to do remount
696  * on it - tough luck.
697  */
698
699 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
700                       void *data)
701 {
702         int err;
703         struct super_block * sb = nd->mnt->mnt_sb;
704
705         if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_REMOUNT))
706                 return -EPERM;
707
708         if (!check_mnt(nd->mnt))
709                 return -EINVAL;
710
711         if (nd->dentry != nd->mnt->mnt_root)
712                 return -EINVAL;
713
714         if (vx_ccaps(VXC_SECURE_REMOUNT))
715                 mnt_flags |= MNT_NODEV;
716         down_write(&sb->s_umount);
717         err = do_remount_sb(sb, flags, data, 0);
718         if (!err)
719                 nd->mnt->mnt_flags=mnt_flags;
720         up_write(&sb->s_umount);
721         if (!err)
722                 security_sb_post_remount(nd->mnt, flags, data);
723         return err;
724 }
725
726 static int do_move_mount(struct nameidata *nd, char *old_name)
727 {
728         struct nameidata old_nd, parent_nd;
729         struct vfsmount *p;
730         int err = 0;
731         if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
732                 return -EPERM;
733         if (!old_name || !*old_name)
734                 return -EINVAL;
735         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
736         if (err)
737                 return err;
738
739         down_write(&current->namespace->sem);
740         while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
741                 ;
742         err = -EINVAL;
743         if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
744                 goto out;
745
746         err = -ENOENT;
747         down(&nd->dentry->d_inode->i_sem);
748         if (IS_DEADDIR(nd->dentry->d_inode))
749                 goto out1;
750
751         spin_lock(&vfsmount_lock);
752         if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
753                 goto out2;
754
755         err = -EINVAL;
756         if (old_nd.dentry != old_nd.mnt->mnt_root)
757                 goto out2;
758
759         if (old_nd.mnt == old_nd.mnt->mnt_parent)
760                 goto out2;
761
762         if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
763               S_ISDIR(old_nd.dentry->d_inode->i_mode))
764                 goto out2;
765
766         err = -ELOOP;
767         for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
768                 if (p == old_nd.mnt)
769                         goto out2;
770         err = 0;
771
772         detach_mnt(old_nd.mnt, &parent_nd);
773         attach_mnt(old_nd.mnt, nd);
774
775         /* if the mount is moved, it should no longer be expire
776          * automatically */
777         list_del_init(&old_nd.mnt->mnt_fslink);
778 out2:
779         spin_unlock(&vfsmount_lock);
780 out1:
781         up(&nd->dentry->d_inode->i_sem);
782 out:
783         up_write(&current->namespace->sem);
784         if (!err)
785                 path_release(&parent_nd);
786         path_release(&old_nd);
787         return err;
788 }
789
790 /*
791  * create a new mount for userspace and request it to be added into the
792  * namespace's tree
793  */
794 static int do_new_mount(struct nameidata *nd, char *type, int flags,
795                         int mnt_flags, char *name, void *data)
796 {
797         struct vfsmount *mnt;
798
799         if (!type || !memchr(type, 0, PAGE_SIZE))
800                 return -EINVAL;
801
802         /* we need capabilities... */
803         if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
804                 return -EPERM;
805
806         mnt = do_kern_mount(type, flags, name, data);
807         if (IS_ERR(mnt))
808                 return PTR_ERR(mnt);
809
810         return do_add_mount(mnt, nd, mnt_flags, NULL);
811 }
812
813 /*
814  * add a mount into a namespace's mount tree
815  * - provide the option of adding the new mount to an expiration list
816  */
817 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
818                  int mnt_flags, struct list_head *fslist)
819 {
820         int err;
821
822         down_write(&current->namespace->sem);
823         /* Something was mounted here while we slept */
824         while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
825                 ;
826         err = -EINVAL;
827         if (!check_mnt(nd->mnt))
828                 goto unlock;
829
830         /* Refuse the same filesystem on the same mount point */
831         err = -EBUSY;
832         if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
833             nd->mnt->mnt_root == nd->dentry)
834                 goto unlock;
835
836         err = -EINVAL;
837         if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
838                 goto unlock;
839
840         newmnt->mnt_flags = mnt_flags;
841         err = graft_tree(newmnt, nd);
842
843         if (err == 0 && fslist) {
844                 /* add to the specified expiration list */
845                 spin_lock(&vfsmount_lock);
846                 list_add_tail(&newmnt->mnt_fslink, fslist);
847                 spin_unlock(&vfsmount_lock);
848         }
849
850 unlock:
851         up_write(&current->namespace->sem);
852         mntput(newmnt);
853         return err;
854 }
855
856 EXPORT_SYMBOL_GPL(do_add_mount);
857
858 /*
859  * process a list of expirable mountpoints with the intent of discarding any
860  * mountpoints that aren't in use and haven't been touched since last we came
861  * here
862  */
863 void mark_mounts_for_expiry(struct list_head *mounts)
864 {
865         struct namespace *namespace;
866         struct vfsmount *mnt, *next;
867         LIST_HEAD(graveyard);
868
869         if (list_empty(mounts))
870                 return;
871
872         spin_lock(&vfsmount_lock);
873
874         /* extract from the expiration list every vfsmount that matches the
875          * following criteria:
876          * - only referenced by its parent vfsmount
877          * - still marked for expiry (marked on the last call here; marks are
878          *   cleared by mntput())
879          */
880         list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
881                 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
882                     atomic_read(&mnt->mnt_count) != 1)
883                         continue;
884
885                 mntget(mnt);
886                 list_move(&mnt->mnt_fslink, &graveyard);
887         }
888
889         /*
890          * go through the vfsmounts we've just consigned to the graveyard to
891          * - check that they're still dead
892          * - delete the vfsmount from the appropriate namespace under lock
893          * - dispose of the corpse
894          */
895         while (!list_empty(&graveyard)) {
896                 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
897                 list_del_init(&mnt->mnt_fslink);
898
899                 /* don't do anything if the namespace is dead - all the
900                  * vfsmounts from it are going away anyway */
901                 namespace = mnt->mnt_namespace;
902                 if (!namespace || atomic_read(&namespace->count) <= 0)
903                         continue;
904                 get_namespace(namespace);
905
906                 spin_unlock(&vfsmount_lock);
907                 down_write(&namespace->sem);
908                 spin_lock(&vfsmount_lock);
909
910                 /* check that it is still dead: the count should now be 2 - as
911                  * contributed by the vfsmount parent and the mntget above */
912                 if (atomic_read(&mnt->mnt_count) == 2) {
913                         struct vfsmount *xdmnt;
914                         struct dentry *xdentry;
915
916                         /* delete from the namespace */
917                         list_del_init(&mnt->mnt_list);
918                         list_del_init(&mnt->mnt_child);
919                         list_del_init(&mnt->mnt_hash);
920                         mnt->mnt_mountpoint->d_mounted--;
921
922                         xdentry = mnt->mnt_mountpoint;
923                         mnt->mnt_mountpoint = mnt->mnt_root;
924                         xdmnt = mnt->mnt_parent;
925                         mnt->mnt_parent = mnt;
926
927                         spin_unlock(&vfsmount_lock);
928
929                         mntput(xdmnt);
930                         dput(xdentry);
931
932                         /* now lay it to rest if this was the last ref on the
933                          * superblock */
934                         if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
935                                 /* last instance - try to be smart */
936                                 lock_kernel();
937                                 DQUOT_OFF(mnt->mnt_sb);
938                                 acct_auto_close(mnt->mnt_sb);
939                                 unlock_kernel();
940                         }
941
942                         mntput(mnt);
943                 } else {
944                         /* someone brought it back to life whilst we didn't
945                          * have any locks held so return it to the expiration
946                          * list */
947                         list_add_tail(&mnt->mnt_fslink, mounts);
948                         spin_unlock(&vfsmount_lock);
949                 }
950
951                 up_write(&namespace->sem);
952
953                 mntput(mnt);
954                 put_namespace(namespace);
955
956                 spin_lock(&vfsmount_lock);
957         }
958
959         spin_unlock(&vfsmount_lock);
960 }
961
962 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
963
964 /*
965  * Some copy_from_user() implementations do not return the exact number of
966  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
967  * Note that this function differs from copy_from_user() in that it will oops
968  * on bad values of `to', rather than returning a short copy.
969  */
970 static long
971 exact_copy_from_user(void *to, const void __user *from, unsigned long n)
972 {
973         char *t = to;
974         const char __user *f = from;
975         char c;
976
977         if (!access_ok(VERIFY_READ, from, n))
978                 return n;
979
980         while (n) {
981                 if (__get_user(c, f)) {
982                         memset(t, 0, n);
983                         break;
984                 }
985                 *t++ = c;
986                 f++;
987                 n--;
988         }
989         return n;
990 }
991
992 int copy_mount_options(const void __user *data, unsigned long *where)
993 {
994         int i;
995         unsigned long page;
996         unsigned long size;
997         
998         *where = 0;
999         if (!data)
1000                 return 0;
1001
1002         if (!(page = __get_free_page(GFP_KERNEL)))
1003                 return -ENOMEM;
1004
1005         /* We only care that *some* data at the address the user
1006          * gave us is valid.  Just in case, we'll zero
1007          * the remainder of the page.
1008          */
1009         /* copy_from_user cannot cross TASK_SIZE ! */
1010         size = TASK_SIZE - (unsigned long)data;
1011         if (size > PAGE_SIZE)
1012                 size = PAGE_SIZE;
1013
1014         i = size - exact_copy_from_user((void *)page, data, size);
1015         if (!i) {
1016                 free_page(page); 
1017                 return -EFAULT;
1018         }
1019         if (i != PAGE_SIZE)
1020                 memset((char *)page + i, 0, PAGE_SIZE - i);
1021         *where = page;
1022         return 0;
1023 }
1024
1025 /*
1026  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1027  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1028  *
1029  * data is a (void *) that can point to any structure up to
1030  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1031  * information (or be NULL).
1032  *
1033  * Pre-0.97 versions of mount() didn't have a flags word.
1034  * When the flags word was introduced its top half was required
1035  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1036  * Therefore, if this magic number is present, it carries no information
1037  * and must be discarded.
1038  */
1039 long do_mount(char * dev_name, char * dir_name, char *type_page,
1040                   unsigned long flags, void *data_page)
1041 {
1042         struct nameidata nd;
1043         int retval = 0;
1044         int mnt_flags = 0;
1045
1046         /* Discard magic */
1047         if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1048                 flags &= ~MS_MGC_MSK;
1049
1050         /* Basic sanity checks */
1051
1052         if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1053                 return -EINVAL;
1054         if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1055                 return -EINVAL;
1056
1057         if (data_page)
1058                 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1059
1060         /* Separate the per-mountpoint flags */
1061         if (flags & MS_NOSUID)
1062                 mnt_flags |= MNT_NOSUID;
1063         if (flags & MS_NODEV)
1064                 mnt_flags |= MNT_NODEV;
1065         if (flags & MS_NOEXEC)
1066                 mnt_flags |= MNT_NOEXEC;
1067         flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1068
1069         if (vx_ccaps(VXC_SECURE_MOUNT))
1070                 mnt_flags |= MNT_NODEV;
1071
1072         /* ... and get the mountpoint */
1073         retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1074         if (retval)
1075                 return retval;
1076
1077         retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1078         if (retval)
1079                 goto dput_out;
1080
1081         if (flags & MS_REMOUNT)
1082                 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1083                                     data_page);
1084         else if (flags & MS_BIND)
1085                 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1086         else if (flags & MS_MOVE)
1087                 retval = do_move_mount(&nd, dev_name);
1088         else
1089                 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1090                                       dev_name, data_page);
1091 dput_out:
1092         path_release(&nd);
1093         return retval;
1094 }
1095
1096 int copy_namespace(int flags, struct task_struct *tsk)
1097 {
1098         struct namespace *namespace = tsk->namespace;
1099         struct namespace *new_ns;
1100         struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1101         struct fs_struct *fs = tsk->fs;
1102         struct vfsmount *p, *q;
1103
1104         if (!namespace)
1105                 return 0;
1106
1107         get_namespace(namespace);
1108
1109         if (!(flags & CLONE_NEWNS))
1110                 return 0;
1111
1112         if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT)) {
1113                 put_namespace(namespace);
1114                 return -EPERM;
1115         }
1116
1117         new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1118         if (!new_ns)
1119                 goto out;
1120
1121         atomic_set(&new_ns->count, 1);
1122         init_rwsem(&new_ns->sem);
1123         INIT_LIST_HEAD(&new_ns->list);
1124
1125         down_write(&tsk->namespace->sem);
1126         /* First pass: copy the tree topology */
1127         new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1128         if (!new_ns->root) {
1129                 up_write(&tsk->namespace->sem);
1130                 kfree(new_ns);
1131                 goto out;
1132         }
1133         spin_lock(&vfsmount_lock);
1134         list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1135         spin_unlock(&vfsmount_lock);
1136
1137         /*
1138          * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1139          * as belonging to new namespace.  We have already acquired a private
1140          * fs_struct, so tsk->fs->lock is not needed.
1141          */
1142         p = namespace->root;
1143         q = new_ns->root;
1144         while (p) {
1145                 q->mnt_namespace = new_ns;
1146                 if (fs) {
1147                         if (p == fs->rootmnt) {
1148                                 rootmnt = p;
1149                                 fs->rootmnt = mntget(q);
1150                         }
1151                         if (p == fs->pwdmnt) {
1152                                 pwdmnt = p;
1153                                 fs->pwdmnt = mntget(q);
1154                         }
1155                         if (p == fs->altrootmnt) {
1156                                 altrootmnt = p;
1157                                 fs->altrootmnt = mntget(q);
1158                         }
1159                 }
1160                 p = next_mnt(p, namespace->root);
1161                 q = next_mnt(q, new_ns->root);
1162         }
1163         up_write(&tsk->namespace->sem);
1164
1165         tsk->namespace = new_ns;
1166
1167         if (rootmnt)
1168                 mntput(rootmnt);
1169         if (pwdmnt)
1170                 mntput(pwdmnt);
1171         if (altrootmnt)
1172                 mntput(altrootmnt);
1173
1174         put_namespace(namespace);
1175         return 0;
1176
1177 out:
1178         put_namespace(namespace);
1179         return -ENOMEM;
1180 }
1181
1182 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1183                           char __user * type, unsigned long flags,
1184                           void __user * data)
1185 {
1186         int retval;
1187         unsigned long data_page;
1188         unsigned long type_page;
1189         unsigned long dev_page;
1190         char *dir_page;
1191
1192         retval = copy_mount_options (type, &type_page);
1193         if (retval < 0)
1194                 return retval;
1195
1196         dir_page = getname(dir_name);
1197         retval = PTR_ERR(dir_page);
1198         if (IS_ERR(dir_page))
1199                 goto out1;
1200
1201         retval = copy_mount_options (dev_name, &dev_page);
1202         if (retval < 0)
1203                 goto out2;
1204
1205         retval = copy_mount_options (data, &data_page);
1206         if (retval < 0)
1207                 goto out3;
1208
1209         lock_kernel();
1210         retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1211                           flags, (void*)data_page);
1212         unlock_kernel();
1213         free_page(data_page);
1214
1215 out3:
1216         free_page(dev_page);
1217 out2:
1218         putname(dir_page);
1219 out1:
1220         free_page(type_page);
1221         return retval;
1222 }
1223
1224 /*
1225  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1226  * It can block. Requires the big lock held.
1227  */
1228 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1229                  struct dentry *dentry)
1230 {
1231         struct dentry *old_root;
1232         struct vfsmount *old_rootmnt;
1233         write_lock(&fs->lock);
1234         old_root = fs->root;
1235         old_rootmnt = fs->rootmnt;
1236         fs->rootmnt = mntget(mnt);
1237         fs->root = dget(dentry);
1238         write_unlock(&fs->lock);
1239         if (old_root) {
1240                 dput(old_root);
1241                 mntput(old_rootmnt);
1242         }
1243 }
1244
1245 EXPORT_SYMBOL(set_fs_root);
1246
1247 /*
1248  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1249  * It can block. Requires the big lock held.
1250  */
1251 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1252                 struct dentry *dentry)
1253 {
1254         struct dentry *old_pwd;
1255         struct vfsmount *old_pwdmnt;
1256
1257         write_lock(&fs->lock);
1258         old_pwd = fs->pwd;
1259         old_pwdmnt = fs->pwdmnt;
1260         fs->pwdmnt = mntget(mnt);
1261         fs->pwd = dget(dentry);
1262         write_unlock(&fs->lock);
1263
1264         if (old_pwd) {
1265                 dput(old_pwd);
1266                 mntput(old_pwdmnt);
1267         }
1268 }
1269
1270 EXPORT_SYMBOL(set_fs_pwd);
1271
1272 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1273 {
1274         struct task_struct *g, *p;
1275         struct fs_struct *fs;
1276
1277         read_lock(&tasklist_lock);
1278         do_each_thread(g, p) {
1279                 task_lock(p);
1280                 fs = p->fs;
1281                 if (fs) {
1282                         atomic_inc(&fs->count);
1283                         task_unlock(p);
1284                         if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1285                                 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1286                         if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1287                                 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1288                         put_fs_struct(fs);
1289                 } else
1290                         task_unlock(p);
1291         } while_each_thread(g, p);
1292         read_unlock(&tasklist_lock);
1293 }
1294
1295 /*
1296  * Moves the current root to put_root, and sets root/cwd of all processes
1297  * which had them on the old root to new_root.
1298  *
1299  * Note:
1300  *  - we don't move root/cwd if they are not at the root (reason: if something
1301  *    cared enough to change them, it's probably wrong to force them elsewhere)
1302  *  - it's okay to pick a root that isn't the root of a file system, e.g.
1303  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1304  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1305  *    first.
1306  */
1307
1308 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1309 {
1310         struct vfsmount *tmp;
1311         struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1312         int error;
1313
1314         if (!capable(CAP_SYS_ADMIN))
1315                 return -EPERM;
1316
1317         lock_kernel();
1318
1319         error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1320         if (error)
1321                 goto out0;
1322         error = -EINVAL;
1323         if (!check_mnt(new_nd.mnt))
1324                 goto out1;
1325
1326         error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1327         if (error)
1328                 goto out1;
1329
1330         error = security_sb_pivotroot(&old_nd, &new_nd);
1331         if (error) {
1332                 path_release(&old_nd);
1333                 goto out1;
1334         }
1335
1336         read_lock(&current->fs->lock);
1337         user_nd.mnt = mntget(current->fs->rootmnt);
1338         user_nd.dentry = dget(current->fs->root);
1339         read_unlock(&current->fs->lock);
1340         down_write(&current->namespace->sem);
1341         down(&old_nd.dentry->d_inode->i_sem);
1342         error = -EINVAL;
1343         if (!check_mnt(user_nd.mnt))
1344                 goto out2;
1345         error = -ENOENT;
1346         if (IS_DEADDIR(new_nd.dentry->d_inode))
1347                 goto out2;
1348         if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1349                 goto out2;
1350         if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1351                 goto out2;
1352         error = -EBUSY;
1353         if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1354                 goto out2; /* loop */
1355         error = -EINVAL;
1356         if (user_nd.mnt->mnt_root != user_nd.dentry)
1357                 goto out2;
1358         if (new_nd.mnt->mnt_root != new_nd.dentry)
1359                 goto out2; /* not a mountpoint */
1360         tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1361         spin_lock(&vfsmount_lock);
1362         if (tmp != new_nd.mnt) {
1363                 for (;;) {
1364                         if (tmp->mnt_parent == tmp)
1365                                 goto out3;
1366                         if (tmp->mnt_parent == new_nd.mnt)
1367                                 break;
1368                         tmp = tmp->mnt_parent;
1369                 }
1370                 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1371                         goto out3;
1372         } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1373                 goto out3;
1374         detach_mnt(new_nd.mnt, &parent_nd);
1375         detach_mnt(user_nd.mnt, &root_parent);
1376         attach_mnt(user_nd.mnt, &old_nd);
1377         attach_mnt(new_nd.mnt, &root_parent);
1378         spin_unlock(&vfsmount_lock);
1379         chroot_fs_refs(&user_nd, &new_nd);
1380         security_sb_post_pivotroot(&user_nd, &new_nd);
1381         error = 0;
1382         path_release(&root_parent);
1383         path_release(&parent_nd);
1384 out2:
1385         up(&old_nd.dentry->d_inode->i_sem);
1386         up_write(&current->namespace->sem);
1387         path_release(&user_nd);
1388         path_release(&old_nd);
1389 out1:
1390         path_release(&new_nd);
1391 out0:
1392         unlock_kernel();
1393         return error;
1394 out3:
1395         spin_unlock(&vfsmount_lock);
1396         goto out2;
1397 }
1398
1399 static void __init init_mount_tree(void)
1400 {
1401         struct vfsmount *mnt;
1402         struct namespace *namespace;
1403         struct task_struct *g, *p;
1404
1405         mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1406         if (IS_ERR(mnt))
1407                 panic("Can't create rootfs");
1408         namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1409         if (!namespace)
1410                 panic("Can't allocate initial namespace");
1411         atomic_set(&namespace->count, 1);
1412         INIT_LIST_HEAD(&namespace->list);
1413         init_rwsem(&namespace->sem);
1414         list_add(&mnt->mnt_list, &namespace->list);
1415         namespace->root = mnt;
1416         mnt->mnt_namespace = namespace;
1417
1418         init_task.namespace = namespace;
1419         read_lock(&tasklist_lock);
1420         do_each_thread(g, p) {
1421                 get_namespace(namespace);
1422                 p->namespace = namespace;
1423         } while_each_thread(g, p);
1424         read_unlock(&tasklist_lock);
1425
1426         set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1427         set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1428 }
1429
1430 void __init mnt_init(unsigned long mempages)
1431 {
1432         struct list_head *d;
1433         unsigned long order;
1434         unsigned int nr_hash;
1435         int i;
1436
1437         mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1438                         0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1439
1440         order = 0; 
1441         mount_hashtable = (struct list_head *)
1442                 __get_free_pages(GFP_ATOMIC, order);
1443
1444         if (!mount_hashtable)
1445                 panic("Failed to allocate mount hash table\n");
1446
1447         /*
1448          * Find the power-of-two list-heads that can fit into the allocation..
1449          * We don't guarantee that "sizeof(struct list_head)" is necessarily
1450          * a power-of-two.
1451          */
1452         nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1453         hash_bits = 0;
1454         do {
1455                 hash_bits++;
1456         } while ((nr_hash >> hash_bits) != 0);
1457         hash_bits--;
1458
1459         /*
1460          * Re-calculate the actual number of entries and the mask
1461          * from the number of bits we can fit.
1462          */
1463         nr_hash = 1UL << hash_bits;
1464         hash_mask = nr_hash-1;
1465
1466         printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1467                         nr_hash, order, (PAGE_SIZE << order));
1468
1469         /* And initialize the newly allocated array */
1470         d = mount_hashtable;
1471         i = nr_hash;
1472         do {
1473                 INIT_LIST_HEAD(d);
1474                 d++;
1475                 i--;
1476         } while (i);
1477         sysfs_init();
1478         init_rootfs();
1479         init_mount_tree();
1480 }
1481
1482 void __put_namespace(struct namespace *namespace)
1483 {
1484         struct vfsmount *mnt;
1485
1486         down_write(&namespace->sem);
1487         spin_lock(&vfsmount_lock);
1488
1489         list_for_each_entry(mnt, &namespace->list, mnt_list) {
1490                 mnt->mnt_namespace = NULL;
1491         }
1492
1493         umount_tree(namespace->root);
1494         spin_unlock(&vfsmount_lock);
1495         up_write(&namespace->sem);
1496         kfree(namespace);
1497 }