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