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