Linux 2.2.0
[davej-history.git] / fs / super.c
blob7c5d2904c65f186edc2f5726aa17427c62232019
1 /*
2 * linux/fs/super.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * super.c contains code to handle: - mount structures
7 * - super-block tables.
8 * - mount system call
9 * - umount system call
11 * Added options to /proc/mounts
12 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
14 * GK 2/5/95 - Changed to support mounting the root fs via NFS
16 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
17 * Added change_root: Werner Almesberger & Hans Lermen, Feb '96
20 #include <linux/config.h>
21 #include <linux/malloc.h>
22 #include <linux/locks.h>
23 #include <linux/smp_lock.h>
24 #include <linux/fd.h>
25 #include <linux/init.h>
26 #include <linux/quotaops.h>
27 #include <linux/acct.h>
29 #include <asm/uaccess.h>
31 #include <linux/nfs_fs.h>
32 #include <linux/nfs_fs_sb.h>
33 #include <linux/nfs_mount.h>
35 #ifdef CONFIG_KMOD
36 #include <linux/kmod.h>
37 #endif
40 * We use a semaphore to synchronize all mount/umount
41 * activity - imagine the mess if we have a race between
42 * unmounting a filesystem and re-mounting it (or something
43 * else).
45 static struct semaphore mount_sem = MUTEX;
47 extern void wait_for_keypress(void);
48 extern struct file_operations * get_blkfops(unsigned int major);
50 extern int root_mountflags;
52 static int do_remount_sb(struct super_block *sb, int flags, char * data);
54 /* this is initialized in init/main.c */
55 kdev_t ROOT_DEV;
57 int nr_super_blocks = 0;
58 int max_super_blocks = NR_SUPER;
59 LIST_HEAD(super_blocks);
61 static struct file_system_type *file_systems = (struct file_system_type *) NULL;
62 struct vfsmount *vfsmntlist = (struct vfsmount *) NULL;
63 static struct vfsmount *vfsmnttail = (struct vfsmount *) NULL,
64 *mru_vfsmnt = (struct vfsmount *) NULL;
66 /*
67 * This part handles the management of the list of mounted filesystems.
69 struct vfsmount *lookup_vfsmnt(kdev_t dev)
71 struct vfsmount *lptr;
73 if (vfsmntlist == (struct vfsmount *)NULL)
74 return ((struct vfsmount *)NULL);
76 if (mru_vfsmnt != (struct vfsmount *)NULL &&
77 mru_vfsmnt->mnt_dev == dev)
78 return (mru_vfsmnt);
80 for (lptr = vfsmntlist;
81 lptr != (struct vfsmount *)NULL;
82 lptr = lptr->mnt_next)
83 if (lptr->mnt_dev == dev) {
84 mru_vfsmnt = lptr;
85 return (lptr);
88 return ((struct vfsmount *)NULL);
89 /* NOTREACHED */
92 static struct vfsmount *add_vfsmnt(struct super_block *sb,
93 const char *dev_name, const char *dir_name)
95 struct vfsmount *lptr;
96 char *tmp, *name;
98 lptr = (struct vfsmount *)kmalloc(sizeof(struct vfsmount), GFP_KERNEL);
99 if (!lptr)
100 goto out;
101 memset(lptr, 0, sizeof(struct vfsmount));
103 lptr->mnt_sb = sb;
104 lptr->mnt_dev = sb->s_dev;
105 lptr->mnt_flags = sb->s_flags;
107 sema_init(&lptr->mnt_dquot.semaphore, 1);
108 lptr->mnt_dquot.flags = 0;
110 /* N.B. Is it really OK to have a vfsmount without names? */
111 if (dev_name && !IS_ERR(tmp = getname(dev_name))) {
112 name = (char *) kmalloc(strlen(tmp)+1, GFP_KERNEL);
113 if (name) {
114 strcpy(name, tmp);
115 lptr->mnt_devname = name;
117 putname(tmp);
119 if (dir_name && !IS_ERR(tmp = getname(dir_name))) {
120 name = (char *) kmalloc(strlen(tmp)+1, GFP_KERNEL);
121 if (name) {
122 strcpy(name, tmp);
123 lptr->mnt_dirname = name;
125 putname(tmp);
128 if (vfsmntlist == (struct vfsmount *)NULL) {
129 vfsmntlist = vfsmnttail = lptr;
130 } else {
131 vfsmnttail->mnt_next = lptr;
132 vfsmnttail = lptr;
134 out:
135 return lptr;
138 static void remove_vfsmnt(kdev_t dev)
140 struct vfsmount *lptr, *tofree;
142 if (vfsmntlist == (struct vfsmount *)NULL)
143 return;
144 lptr = vfsmntlist;
145 if (lptr->mnt_dev == dev) {
146 tofree = lptr;
147 vfsmntlist = lptr->mnt_next;
148 if (vfsmnttail->mnt_dev == dev)
149 vfsmnttail = vfsmntlist;
150 } else {
151 while (lptr->mnt_next != (struct vfsmount *)NULL) {
152 if (lptr->mnt_next->mnt_dev == dev)
153 break;
154 lptr = lptr->mnt_next;
156 tofree = lptr->mnt_next;
157 if (tofree == (struct vfsmount *)NULL)
158 return;
159 lptr->mnt_next = lptr->mnt_next->mnt_next;
160 if (vfsmnttail->mnt_dev == dev)
161 vfsmnttail = lptr;
163 if (tofree == mru_vfsmnt)
164 mru_vfsmnt = NULL;
165 kfree(tofree->mnt_devname);
166 kfree(tofree->mnt_dirname);
167 kfree_s(tofree, sizeof(struct vfsmount));
170 int register_filesystem(struct file_system_type * fs)
172 struct file_system_type ** tmp;
174 if (!fs)
175 return -EINVAL;
176 if (fs->next)
177 return -EBUSY;
178 tmp = &file_systems;
179 while (*tmp) {
180 if (strcmp((*tmp)->name, fs->name) == 0)
181 return -EBUSY;
182 tmp = &(*tmp)->next;
184 *tmp = fs;
185 return 0;
188 #ifdef CONFIG_MODULES
189 int unregister_filesystem(struct file_system_type * fs)
191 struct file_system_type ** tmp;
193 tmp = &file_systems;
194 while (*tmp) {
195 if (fs == *tmp) {
196 *tmp = fs->next;
197 fs->next = NULL;
198 return 0;
200 tmp = &(*tmp)->next;
202 return -EINVAL;
204 #endif
206 static int fs_index(const char * __name)
208 struct file_system_type * tmp;
209 char * name;
210 int err, index;
212 name = getname(__name);
213 err = PTR_ERR(name);
214 if (IS_ERR(name))
215 return err;
217 index = 0;
218 for (tmp = file_systems ; tmp ; tmp = tmp->next) {
219 if (strcmp(tmp->name, name) == 0) {
220 putname(name);
221 return index;
223 index++;
225 putname(name);
226 return -EINVAL;
229 static int fs_name(unsigned int index, char * buf)
231 struct file_system_type * tmp;
232 int len;
234 tmp = file_systems;
235 while (tmp && index > 0) {
236 tmp = tmp->next;
237 index--;
239 if (!tmp)
240 return -EINVAL;
241 len = strlen(tmp->name) + 1;
242 return copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
245 static int fs_maxindex(void)
247 struct file_system_type * tmp;
248 int index;
250 index = 0;
251 for (tmp = file_systems ; tmp ; tmp = tmp->next)
252 index++;
253 return index;
257 * Whee.. Weird sysv syscall.
259 asmlinkage int sys_sysfs(int option, unsigned long arg1, unsigned long arg2)
261 int retval = -EINVAL;
263 lock_kernel();
264 switch (option) {
265 case 1:
266 retval = fs_index((const char *) arg1);
267 break;
269 case 2:
270 retval = fs_name(arg1, (char *) arg2);
271 break;
273 case 3:
274 retval = fs_maxindex();
275 break;
277 unlock_kernel();
278 return retval;
281 static struct proc_fs_info {
282 int flag;
283 char *str;
284 } fs_info[] = {
285 { MS_NOEXEC, ",noexec" },
286 { MS_NOSUID, ",nosuid" },
287 { MS_NODEV, ",nodev" },
288 { MS_SYNCHRONOUS, ",sync" },
289 { MS_MANDLOCK, ",mand" },
290 { MS_NOATIME, ",noatime" },
291 { MS_NODIRATIME, ",nodiratime" },
292 #ifdef MS_NOSUB /* Can't find this except in mount.c */
293 { MS_NOSUB, ",nosub" },
294 #endif
295 { 0, NULL }
298 static struct proc_nfs_info {
299 int flag;
300 char *str;
301 } nfs_info[] = {
302 { NFS_MOUNT_SOFT, ",soft" },
303 { NFS_MOUNT_INTR, ",intr" },
304 { NFS_MOUNT_POSIX, ",posix" },
305 { NFS_MOUNT_NOCTO, ",nocto" },
306 { NFS_MOUNT_NOAC, ",noac" },
307 { 0, NULL }
310 int get_filesystem_info( char *buf )
312 struct vfsmount *tmp = vfsmntlist;
313 struct proc_fs_info *fs_infop;
314 struct proc_nfs_info *nfs_infop;
315 struct nfs_server *nfss;
316 int len = 0;
318 while ( tmp && len < PAGE_SIZE - 160)
320 len += sprintf( buf + len, "%s %s %s %s",
321 tmp->mnt_devname, tmp->mnt_dirname, tmp->mnt_sb->s_type->name,
322 tmp->mnt_flags & MS_RDONLY ? "ro" : "rw" );
323 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
324 if (tmp->mnt_flags & fs_infop->flag) {
325 strcpy(buf + len, fs_infop->str);
326 len += strlen(fs_infop->str);
329 if (!strcmp("nfs", tmp->mnt_sb->s_type->name)) {
330 nfss = &tmp->mnt_sb->u.nfs_sb.s_server;
331 if (nfss->rsize != NFS_DEF_FILE_IO_BUFFER_SIZE) {
332 len += sprintf(buf+len, ",rsize=%d",
333 nfss->rsize);
335 if (nfss->wsize != NFS_DEF_FILE_IO_BUFFER_SIZE) {
336 len += sprintf(buf+len, ",wsize=%d",
337 nfss->wsize);
339 #if 0
340 if (nfss->timeo != 7*HZ/10) {
341 len += sprintf(buf+len, ",timeo=%d",
342 nfss->timeo*10/HZ);
344 if (nfss->retrans != 3) {
345 len += sprintf(buf+len, ",retrans=%d",
346 nfss->retrans);
348 #endif
349 if (nfss->acregmin != 3*HZ) {
350 len += sprintf(buf+len, ",acregmin=%d",
351 nfss->acregmin/HZ);
353 if (nfss->acregmax != 60*HZ) {
354 len += sprintf(buf+len, ",acregmax=%d",
355 nfss->acregmax/HZ);
357 if (nfss->acdirmin != 30*HZ) {
358 len += sprintf(buf+len, ",acdirmin=%d",
359 nfss->acdirmin/HZ);
361 if (nfss->acdirmax != 60*HZ) {
362 len += sprintf(buf+len, ",acdirmax=%d",
363 nfss->acdirmax/HZ);
365 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
366 if (nfss->flags & nfs_infop->flag) {
367 strcpy(buf + len, nfs_infop->str);
368 len += strlen(nfs_infop->str);
371 len += sprintf(buf+len, ",addr=%s",
372 nfss->hostname);
374 len += sprintf( buf + len, " 0 0\n" );
375 tmp = tmp->mnt_next;
378 return len;
381 int get_filesystem_list(char * buf)
383 int len = 0;
384 struct file_system_type * tmp;
386 tmp = file_systems;
387 while (tmp && len < PAGE_SIZE - 80) {
388 len += sprintf(buf+len, "%s\t%s\n",
389 (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
390 tmp->name);
391 tmp = tmp->next;
393 return len;
396 struct file_system_type *get_fs_type(const char *name)
398 struct file_system_type * fs = file_systems;
400 if (!name)
401 return fs;
402 for (fs = file_systems; fs && strcmp(fs->name, name); fs = fs->next)
404 #ifdef CONFIG_KMOD
405 if (!fs && (request_module(name) == 0)) {
406 for (fs = file_systems; fs && strcmp(fs->name, name); fs = fs->next)
409 #endif
411 return fs;
414 void __wait_on_super(struct super_block * sb)
416 struct wait_queue wait = { current, NULL };
418 add_wait_queue(&sb->s_wait, &wait);
419 repeat:
420 current->state = TASK_UNINTERRUPTIBLE;
421 if (sb->s_lock) {
422 schedule();
423 goto repeat;
425 remove_wait_queue(&sb->s_wait, &wait);
426 current->state = TASK_RUNNING;
430 * Note: check the dirty flag before waiting, so we don't
431 * hold up the sync while mounting a device. (The newly
432 * mounted device won't need syncing.)
434 void sync_supers(kdev_t dev)
436 struct super_block * sb;
438 for (sb = sb_entry(super_blocks.next);
439 sb != sb_entry(&super_blocks);
440 sb = sb_entry(sb->s_list.next)) {
441 if (!sb->s_dev)
442 continue;
443 if (dev && sb->s_dev != dev)
444 continue;
445 if (!sb->s_dirt)
446 continue;
447 /* N.B. Should lock the superblock while writing */
448 wait_on_super(sb);
449 if (!sb->s_dev || !sb->s_dirt)
450 continue;
451 if (dev && (dev != sb->s_dev))
452 continue;
453 if (sb->s_op && sb->s_op->write_super)
454 sb->s_op->write_super(sb);
458 struct super_block * get_super(kdev_t dev)
460 struct super_block * s;
462 if (!dev)
463 return NULL;
464 restart:
465 s = sb_entry(super_blocks.next);
466 while (s != sb_entry(&super_blocks))
467 if (s->s_dev == dev) {
468 wait_on_super(s);
469 if (s->s_dev == dev)
470 return s;
471 goto restart;
472 } else
473 s = sb_entry(s->s_list.next);
474 return NULL;
477 asmlinkage int sys_ustat(dev_t dev, struct ustat * ubuf)
479 struct super_block *s;
480 struct ustat tmp;
481 struct statfs sbuf;
482 mm_segment_t old_fs;
483 int err = -EINVAL;
485 lock_kernel();
486 s = get_super(to_kdev_t(dev));
487 if (s == NULL)
488 goto out;
489 err = -ENOSYS;
490 if (!(s->s_op->statfs))
491 goto out;
493 old_fs = get_fs();
494 set_fs(get_ds());
495 s->s_op->statfs(s,&sbuf,sizeof(struct statfs));
496 set_fs(old_fs);
498 memset(&tmp,0,sizeof(struct ustat));
499 tmp.f_tfree = sbuf.f_bfree;
500 tmp.f_tinode = sbuf.f_ffree;
502 err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
503 out:
504 unlock_kernel();
505 return err;
509 * Find a super_block with no device assigned.
511 static struct super_block *get_empty_super(void)
513 struct super_block *s;
515 for (s = sb_entry(super_blocks.next);
516 s != sb_entry(&super_blocks);
517 s = sb_entry(s->s_list.next)) {
518 if (s->s_dev)
519 continue;
520 if (!s->s_lock)
521 return s;
522 printk("VFS: empty superblock %p locked!\n", s);
524 /* Need a new one... */
525 if (nr_super_blocks >= max_super_blocks)
526 return NULL;
527 s = kmalloc(sizeof(struct super_block), GFP_USER);
528 if (s) {
529 nr_super_blocks++;
530 memset(s, 0, sizeof(struct super_block));
531 INIT_LIST_HEAD(&s->s_dirty);
532 list_add (&s->s_list, super_blocks.prev);
534 return s;
537 static struct super_block * read_super(kdev_t dev,const char *name,int flags,
538 void *data, int silent)
540 struct super_block * s;
541 struct file_system_type *type;
543 if (!dev)
544 goto out_null;
545 check_disk_change(dev);
546 s = get_super(dev);
547 if (s)
548 goto out;
550 type = get_fs_type(name);
551 if (!type) {
552 printk("VFS: on device %s: get_fs_type(%s) failed\n",
553 kdevname(dev), name);
554 goto out;
556 s = get_empty_super();
557 if (!s)
558 goto out;
559 s->s_dev = dev;
560 s->s_flags = flags;
561 s->s_dirt = 0;
562 /* N.B. Should lock superblock now ... */
563 if (!type->read_super(s, data, silent))
564 goto out_fail;
565 s->s_dev = dev; /* N.B. why do this again?? */
566 s->s_rd_only = 0;
567 s->s_type = type;
568 out:
569 return s;
571 /* N.B. s_dev should be cleared in type->read_super */
572 out_fail:
573 s->s_dev = 0;
574 out_null:
575 s = NULL;
576 goto out;
580 * Unnamed block devices are dummy devices used by virtual
581 * filesystems which don't use real block-devices. -- jrs
584 static unsigned int unnamed_dev_in_use[256/(8*sizeof(unsigned int))] = { 0, };
586 kdev_t get_unnamed_dev(void)
588 int i;
590 for (i = 1; i < 256; i++) {
591 if (!test_and_set_bit(i,unnamed_dev_in_use))
592 return MKDEV(UNNAMED_MAJOR, i);
594 return 0;
597 void put_unnamed_dev(kdev_t dev)
599 if (!dev || MAJOR(dev) != UNNAMED_MAJOR)
600 return;
601 if (test_and_clear_bit(MINOR(dev), unnamed_dev_in_use))
602 return;
603 printk("VFS: put_unnamed_dev: freeing unused device %s\n",
604 kdevname(dev));
607 static int d_umount(struct super_block * sb)
609 struct dentry * root = sb->s_root;
610 struct dentry * covered = root->d_covers;
612 if (root->d_count != 1)
613 return -EBUSY;
615 if (root->d_inode->i_state)
616 return -EBUSY;
618 sb->s_root = NULL;
620 if (covered != root) {
621 root->d_covers = root;
622 covered->d_mounts = covered;
623 dput(covered);
625 dput(root);
626 return 0;
629 static void d_mount(struct dentry *covered, struct dentry *dentry)
631 if (covered->d_mounts != covered) {
632 printk("VFS: mount - already mounted\n");
633 return;
635 covered->d_mounts = dentry;
636 dentry->d_covers = covered;
639 static int do_umount(kdev_t dev, int unmount_root, int flags)
641 struct super_block * sb;
642 int retval;
644 retval = -ENOENT;
645 sb = get_super(dev);
646 if (!sb || !sb->s_root)
647 goto out;
650 * Before checking whether the filesystem is still busy,
651 * make sure the kernel doesn't hold any quota files open
652 * on the device. If the umount fails, too bad -- there
653 * are no quotas running any more. Just turn them on again.
655 DQUOT_OFF(dev);
656 acct_auto_close(dev);
659 * If we may have to abort operations to get out of this
660 * mount, and they will themselves hold resources we must
661 * allow the fs to do things. In the Unix tradition of
662 * 'Gee thats tricky lets do it in userspace' the umount_begin
663 * might fail to complete on the first run through as other tasks
664 * must return, and the like. Thats for the mount program to worry
665 * about for the moment.
668 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
669 sb->s_op->umount_begin(sb);
672 * Shrink dcache, then fsync. This guarantees that if the
673 * filesystem is quiescent at this point, then (a) only the
674 * root entry should be in use and (b) that root entry is
675 * clean.
677 shrink_dcache_sb(sb);
678 fsync_dev(dev);
680 if (dev==ROOT_DEV && !unmount_root) {
682 * Special case for "unmounting" root ...
683 * we just try to remount it readonly.
685 retval = 0;
686 if (!(sb->s_flags & MS_RDONLY))
687 retval = do_remount_sb(sb, MS_RDONLY, 0);
688 return retval;
691 retval = d_umount(sb);
692 if (retval)
693 goto out;
695 if (sb->s_op) {
696 if (sb->s_op->write_super && sb->s_dirt)
697 sb->s_op->write_super(sb);
700 lock_super(sb);
701 if (sb->s_op) {
702 if (sb->s_op->put_super)
703 sb->s_op->put_super(sb);
706 /* Forget any remaining inodes */
707 if (invalidate_inodes(sb)) {
708 printk("VFS: Busy inodes after unmount. "
709 "Self-destruct in 5 seconds. Have a nice day...\n");
712 sb->s_dev = 0; /* Free the superblock */
713 unlock_super(sb);
715 remove_vfsmnt(dev);
716 out:
717 return retval;
720 static int umount_dev(kdev_t dev, int flags)
722 int retval;
723 struct inode * inode = get_empty_inode();
725 retval = -ENOMEM;
726 if (!inode)
727 goto out;
729 inode->i_rdev = dev;
730 retval = -ENXIO;
731 if (MAJOR(dev) >= MAX_BLKDEV)
732 goto out_iput;
734 fsync_dev(dev);
736 down(&mount_sem);
738 retval = do_umount(dev, 0, flags);
739 if (!retval) {
740 fsync_dev(dev);
741 if (dev != ROOT_DEV) {
742 blkdev_release(inode);
743 put_unnamed_dev(dev);
747 up(&mount_sem);
748 out_iput:
749 iput(inode);
750 out:
751 return retval;
755 * Now umount can handle mount points as well as block devices.
756 * This is important for filesystems which use unnamed block devices.
758 * There is a little kludge here with the dummy_inode. The current
759 * vfs release functions only use the r_dev field in the inode so
760 * we give them the info they need without using a real inode.
761 * If any other fields are ever needed by any block device release
762 * functions, they should be faked here. -- jrs
764 * We now support a flag for forced unmount like the other 'big iron'
765 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
768 asmlinkage int sys_umount(char * name, int flags)
770 struct dentry * dentry;
771 int retval;
773 if (!capable(CAP_SYS_ADMIN))
774 return -EPERM;
776 lock_kernel();
777 dentry = namei(name);
778 retval = PTR_ERR(dentry);
779 if (!IS_ERR(dentry)) {
780 struct inode * inode = dentry->d_inode;
781 kdev_t dev = inode->i_rdev;
783 retval = 0;
784 if (S_ISBLK(inode->i_mode)) {
785 if (IS_NODEV(inode))
786 retval = -EACCES;
787 } else {
788 struct super_block *sb = inode->i_sb;
789 retval = -EINVAL;
790 if (sb && inode == sb->s_root->d_inode) {
791 dev = sb->s_dev;
792 retval = 0;
795 dput(dentry);
797 if (!retval)
798 retval = umount_dev(dev, flags);
800 unlock_kernel();
801 return retval;
805 * The 2.0 compatible umount. No flags.
808 asmlinkage int sys_oldumount(char * name)
810 return sys_umount(name,0);
814 * Check whether we can mount the specified device.
816 int fs_may_mount(kdev_t dev)
818 struct super_block * sb = get_super(dev);
819 int busy;
821 busy = sb && sb->s_root &&
822 (sb->s_root->d_count != 1 || sb->s_root->d_covers != sb->s_root);
823 return !busy;
827 * do_mount() does the actual mounting after sys_mount has done the ugly
828 * parameter parsing. When enough time has gone by, and everything uses the
829 * new mount() parameters, sys_mount() can then be cleaned up.
831 * We cannot mount a filesystem if it has active, used, or dirty inodes.
832 * We also have to flush all inode-data for this device, as the new mount
833 * might need new info.
835 * [21-Mar-97] T.Schoebel-Theuer: Now this can be overridden when
836 * supplying a leading "!" before the dir_name, allowing "stacks" of
837 * mounted filesystems. The stacking will only influence any pathname lookups
838 * _after_ the mount, but open file descriptors or working directories that
839 * are now covered remain valid. For example, when you overmount /home, any
840 * process with old cwd /home/joe will continue to use the old versions,
841 * as long as relative paths are used, but absolute paths like /home/joe/xxx
842 * will go to the new "top of stack" version. In general, crossing a
843 * mount point will always go to the top of stack element.
844 * Anyone using this new feature must know what he/she is doing.
847 int do_mount(kdev_t dev, const char * dev_name, const char * dir_name, const char * type, int flags, void * data)
849 struct dentry * dir_d;
850 struct super_block * sb;
851 struct vfsmount *vfsmnt;
852 int error;
854 error = -EACCES;
855 if (!(flags & MS_RDONLY) && dev && is_read_only(dev))
856 goto out;
859 * Do the lookup first to force automounting.
861 dir_d = namei(dir_name);
862 error = PTR_ERR(dir_d);
863 if (IS_ERR(dir_d))
864 goto out;
866 down(&mount_sem);
867 error = -ENOTDIR;
868 if (!S_ISDIR(dir_d->d_inode->i_mode))
869 goto dput_and_out;
871 error = -EBUSY;
872 if (dir_d->d_covers != dir_d)
873 goto dput_and_out;
876 * Note: If the superblock already exists,
877 * read_super just does a get_super().
879 error = -EINVAL;
880 sb = read_super(dev, type, flags, data, 0);
881 if (!sb)
882 goto dput_and_out;
885 * We may have slept while reading the super block,
886 * so we check afterwards whether it's safe to mount.
888 error = -EBUSY;
889 if (!fs_may_mount(dev))
890 goto dput_and_out;
892 error = -ENOMEM;
893 vfsmnt = add_vfsmnt(sb, dev_name, dir_name);
894 if (vfsmnt) {
895 d_mount(dget(dir_d), sb->s_root);
896 error = 0;
899 dput_and_out:
900 dput(dir_d);
901 up(&mount_sem);
902 out:
903 return error;
908 * Alters the mount flags of a mounted file system. Only the mount point
909 * is used as a reference - file system type and the device are ignored.
910 * FS-specific mount options can't be altered by remounting.
913 static int do_remount_sb(struct super_block *sb, int flags, char *data)
915 int retval;
916 struct vfsmount *vfsmnt;
919 * Invalidate the inodes, as some mount options may be changed.
920 * N.B. If we are changing media, we should check the return
921 * from invalidate_inodes ... can't allow _any_ open files.
923 invalidate_inodes(sb);
925 if (!(flags & MS_RDONLY) && sb->s_dev && is_read_only(sb->s_dev))
926 return -EACCES;
927 /*flags |= MS_RDONLY;*/
928 /* If we are remounting RDONLY, make sure there are no rw files open */
929 if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY))
930 if (!fs_may_remount_ro(sb))
931 return -EBUSY;
932 if (sb->s_op && sb->s_op->remount_fs) {
933 retval = sb->s_op->remount_fs(sb, &flags, data);
934 if (retval)
935 return retval;
937 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
938 vfsmnt = lookup_vfsmnt(sb->s_dev);
939 if (vfsmnt)
940 vfsmnt->mnt_flags = sb->s_flags;
941 return 0;
944 static int do_remount(const char *dir,int flags,char *data)
946 struct dentry *dentry;
947 int retval;
949 dentry = namei(dir);
950 retval = PTR_ERR(dentry);
951 if (!IS_ERR(dentry)) {
952 struct super_block * sb = dentry->d_inode->i_sb;
954 retval = -EINVAL;
955 if (dentry == sb->s_root) {
957 * Shrink the dcache and sync the device.
959 shrink_dcache_sb(sb);
960 fsync_dev(sb->s_dev);
961 if (flags & MS_RDONLY)
962 acct_auto_close(sb->s_dev);
963 retval = do_remount_sb(sb, flags, data);
965 dput(dentry);
967 return retval;
970 static int copy_mount_options (const void * data, unsigned long *where)
972 int i;
973 unsigned long page;
974 struct vm_area_struct * vma;
976 *where = 0;
977 if (!data)
978 return 0;
980 vma = find_vma(current->mm, (unsigned long) data);
981 if (!vma || (unsigned long) data < vma->vm_start)
982 return -EFAULT;
983 if (!(vma->vm_flags & VM_READ))
984 return -EFAULT;
985 i = vma->vm_end - (unsigned long) data;
986 if (PAGE_SIZE <= (unsigned long) i)
987 i = PAGE_SIZE-1;
988 if (!(page = __get_free_page(GFP_KERNEL))) {
989 return -ENOMEM;
991 if (copy_from_user((void *) page,data,i)) {
992 free_page(page);
993 return -EFAULT;
995 *where = page;
996 return 0;
1000 * Flags is a 16-bit value that allows up to 16 non-fs dependent flags to
1001 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1003 * data is a (void *) that can point to any structure up to
1004 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1005 * information (or be NULL).
1007 * NOTE! As old versions of mount() didn't use this setup, the flags
1008 * have to have a special 16-bit magic number in the high word:
1009 * 0xC0ED. If this magic word isn't present, the flags and data info
1010 * aren't used, as the syscall assumes we are talking to an older
1011 * version that didn't understand them.
1013 asmlinkage int sys_mount(char * dev_name, char * dir_name, char * type,
1014 unsigned long new_flags, void * data)
1016 struct file_system_type * fstype;
1017 struct dentry * dentry = NULL;
1018 struct inode * inode = NULL;
1019 kdev_t dev;
1020 int retval = -EPERM;
1021 unsigned long flags = 0;
1022 unsigned long page = 0;
1023 struct file dummy; /* allows read-write or read-only flag */
1025 lock_kernel();
1026 if (!capable(CAP_SYS_ADMIN))
1027 goto out;
1028 if ((new_flags &
1029 (MS_MGC_MSK | MS_REMOUNT)) == (MS_MGC_VAL | MS_REMOUNT)) {
1030 retval = copy_mount_options (data, &page);
1031 if (retval < 0)
1032 goto out;
1033 retval = do_remount(dir_name,
1034 new_flags & ~MS_MGC_MSK & ~MS_REMOUNT,
1035 (char *) page);
1036 free_page(page);
1037 goto out;
1040 retval = copy_mount_options (type, &page);
1041 if (retval < 0)
1042 goto out;
1043 fstype = get_fs_type((char *) page);
1044 free_page(page);
1045 retval = -ENODEV;
1046 if (!fstype)
1047 goto out;
1049 memset(&dummy, 0, sizeof(dummy));
1050 if (fstype->fs_flags & FS_REQUIRES_DEV) {
1051 dentry = namei(dev_name);
1052 retval = PTR_ERR(dentry);
1053 if (IS_ERR(dentry))
1054 goto out;
1056 inode = dentry->d_inode;
1057 retval = -ENOTBLK;
1058 if (!S_ISBLK(inode->i_mode))
1059 goto dput_and_out;
1061 retval = -EACCES;
1062 if (IS_NODEV(inode))
1063 goto dput_and_out;
1065 dev = inode->i_rdev;
1066 retval = -ENXIO;
1067 if (MAJOR(dev) >= MAX_BLKDEV)
1068 goto dput_and_out;
1070 retval = -ENOTBLK;
1071 dummy.f_op = get_blkfops(MAJOR(dev));
1072 if (!dummy.f_op)
1073 goto dput_and_out;
1075 if (dummy.f_op->open) {
1076 dummy.f_dentry = dentry;
1077 dummy.f_mode = (new_flags & MS_RDONLY) ? 1 : 3;
1078 retval = dummy.f_op->open(inode, &dummy);
1079 if (retval)
1080 goto dput_and_out;
1083 } else {
1084 retval = -EMFILE;
1085 if (!(dev = get_unnamed_dev()))
1086 goto out;
1089 page = 0;
1090 if ((new_flags & MS_MGC_MSK) == MS_MGC_VAL) {
1091 flags = new_flags & ~MS_MGC_MSK;
1092 retval = copy_mount_options(data, &page);
1093 if (retval < 0)
1094 goto clean_up;
1096 retval = do_mount(dev, dev_name, dir_name, fstype->name, flags,
1097 (void *) page);
1098 free_page(page);
1099 if (retval)
1100 goto clean_up;
1102 dput_and_out:
1103 dput(dentry);
1104 out:
1105 unlock_kernel();
1106 return retval;
1108 clean_up:
1109 if (dummy.f_op) {
1110 if (dummy.f_op->release)
1111 dummy.f_op->release(inode, NULL);
1112 } else
1113 put_unnamed_dev(dev);
1114 goto dput_and_out;
1117 void __init mount_root(void)
1119 struct file_system_type * fs_type;
1120 struct super_block * sb;
1121 struct vfsmount *vfsmnt;
1122 struct inode * d_inode = NULL;
1123 struct file filp;
1124 int retval;
1126 #ifdef CONFIG_ROOT_NFS
1127 if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
1128 ROOT_DEV = 0;
1129 if ((fs_type = get_fs_type("nfs"))) {
1130 sb = get_empty_super(); /* "can't fail" */
1131 sb->s_dev = get_unnamed_dev();
1132 sb->s_flags = root_mountflags;
1133 vfsmnt = add_vfsmnt(sb, "/dev/root", "/");
1134 if (vfsmnt) {
1135 if (nfs_root_mount(sb) >= 0) {
1136 sb->s_dirt = 0;
1137 sb->s_type = fs_type;
1138 current->fs->root = dget(sb->s_root);
1139 current->fs->pwd = dget(sb->s_root);
1140 ROOT_DEV = sb->s_dev;
1141 printk (KERN_NOTICE "VFS: Mounted root (NFS filesystem)%s.\n", (sb->s_flags & MS_RDONLY) ? " readonly" : "");
1142 return;
1144 remove_vfsmnt(sb->s_dev);
1146 put_unnamed_dev(sb->s_dev);
1147 sb->s_dev = 0;
1149 if (!ROOT_DEV) {
1150 printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
1151 ROOT_DEV = MKDEV(FLOPPY_MAJOR, 0);
1154 #endif
1156 #ifdef CONFIG_BLK_DEV_FD
1157 if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
1158 floppy_eject();
1159 #ifndef CONFIG_BLK_DEV_RAM
1160 printk(KERN_NOTICE "(Warning, this kernel has no ramdisk support)\n");
1161 #endif
1162 printk(KERN_NOTICE "VFS: Insert root floppy and press ENTER\n");
1163 wait_for_keypress();
1165 #endif
1167 memset(&filp, 0, sizeof(filp));
1168 d_inode = get_empty_inode();
1169 d_inode->i_rdev = ROOT_DEV;
1170 filp.f_dentry = NULL;
1171 if ( root_mountflags & MS_RDONLY)
1172 filp.f_mode = 1; /* read only */
1173 else
1174 filp.f_mode = 3; /* read write */
1175 retval = blkdev_open(d_inode, &filp);
1176 if (retval == -EROFS) {
1177 root_mountflags |= MS_RDONLY;
1178 filp.f_mode = 1;
1179 retval = blkdev_open(d_inode, &filp);
1181 iput(d_inode);
1182 if (retval)
1184 * Allow the user to distinguish between failed open
1185 * and bad superblock on root device.
1187 printk("VFS: Cannot open root device %s\n",
1188 kdevname(ROOT_DEV));
1189 else for (fs_type = file_systems ; fs_type ; fs_type = fs_type->next) {
1190 if (!(fs_type->fs_flags & FS_REQUIRES_DEV))
1191 continue;
1192 sb = read_super(ROOT_DEV,fs_type->name,root_mountflags,NULL,1);
1193 if (sb) {
1194 sb->s_flags = root_mountflags;
1195 current->fs->root = dget(sb->s_root);
1196 current->fs->pwd = dget(sb->s_root);
1197 printk ("VFS: Mounted root (%s filesystem)%s.\n",
1198 fs_type->name,
1199 (sb->s_flags & MS_RDONLY) ? " readonly" : "");
1200 vfsmnt = add_vfsmnt(sb, "/dev/root", "/");
1201 if (vfsmnt)
1202 return;
1203 panic("VFS: add_vfsmnt failed for root fs");
1206 panic("VFS: Unable to mount root fs on %s",
1207 kdevname(ROOT_DEV));
1211 #ifdef CONFIG_BLK_DEV_INITRD
1213 int __init change_root(kdev_t new_root_dev,const char *put_old)
1215 kdev_t old_root_dev;
1216 struct vfsmount *vfsmnt;
1217 struct dentry *old_root,*old_pwd,*dir_d = NULL;
1218 int error;
1220 old_root = current->fs->root;
1221 old_pwd = current->fs->pwd;
1222 old_root_dev = ROOT_DEV;
1223 if (!fs_may_mount(new_root_dev)) {
1224 printk(KERN_CRIT "New root is busy. Staying in initrd.\n");
1225 return -EBUSY;
1227 ROOT_DEV = new_root_dev;
1228 mount_root();
1229 dput(old_root);
1230 dput(old_pwd);
1231 #if 1
1232 shrink_dcache();
1233 printk("change_root: old root has d_count=%d\n", old_root->d_count);
1234 #endif
1236 * Get the new mount directory
1238 dir_d = lookup_dentry(put_old, NULL, 1);
1239 if (IS_ERR(dir_d)) {
1240 error = PTR_ERR(dir_d);
1241 } else if (!dir_d->d_inode) {
1242 dput(dir_d);
1243 error = -ENOENT;
1244 } else {
1245 error = 0;
1247 if (!error && dir_d->d_covers != dir_d) {
1248 dput(dir_d);
1249 error = -EBUSY;
1251 if (!error && !S_ISDIR(dir_d->d_inode->i_mode)) {
1252 dput(dir_d);
1253 error = -ENOTDIR;
1255 if (error) {
1256 int umount_error;
1258 printk(KERN_NOTICE "Trying to unmount old root ... ");
1259 umount_error = do_umount(old_root_dev,1, 0);
1260 if (!umount_error) {
1261 printk("okay\n");
1262 invalidate_buffers(old_root_dev);
1263 return 0;
1265 printk(KERN_ERR "error %d\n",umount_error);
1266 return error;
1268 remove_vfsmnt(old_root_dev);
1269 vfsmnt = add_vfsmnt(old_root->d_sb, "/dev/root.old", put_old);
1270 if (vfsmnt) {
1271 d_mount(dir_d,old_root);
1272 return 0;
1274 printk(KERN_CRIT "Trouble: add_vfsmnt failed\n");
1275 return -ENOMEM;
1278 #endif