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