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