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