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