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