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