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