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