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