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