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