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