kern

view src/fs_sys.c @ 93:083849df660b

split the system call implementations out of fs.c into fs_sys.c
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 11 Dec 2011 10:17:58 +0200
parents
children
line source
1 /* implementation of the filesystem-related syscalls */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <errno.h>
8 #include "fs.h"
9 #include "part.h"
10 #include "panic.h"
11 #include "bdev.h"
13 static dev_t find_rootfs(void);
15 /* list of mounted filesystems
16 * XXX currently only one, the root filesystem
17 */
18 static struct filesys *fslist;
21 int sys_mount(char *mtpt, char *devname, unsigned int flags)
22 {
23 dev_t dev;
24 int err;
25 struct filesys *fs;
27 if(strcmp(mtpt, "/") != 0) {
28 printf("only root can be mounted at the moment\n");
29 return -EBUG;
30 }
32 /* mounting root filesystem */
33 if(fslist) {
34 printf("root already mounted\n");
35 return -EBUSY;
36 }
38 if(devname) {
39 dev = bdev_by_name(devname);
40 } else {
41 /* try to autodetect it */
42 dev = find_rootfs();
43 }
44 if(!dev) {
45 err = -ENOENT;
46 goto rootfail;
47 }
49 if(!(fs = malloc(sizeof *fslist))) {
50 err = -ENOMEM;
51 goto rootfail;
52 }
53 if((err = openfs(fs, dev)) != 0) {
54 free(fs);
55 goto rootfail;
56 }
58 fslist = fs;
59 return 0;
61 rootfail:
62 panic("failed to mount root filesystem: %d\n", -err);
63 return err; /* unreachable */
64 }
66 #define PART_TYPE 0xcc
67 static dev_t find_rootfs(void)
68 {
69 dev_t dev = 0;
70 int i, num_dev, partid;
71 struct partition *plist, *p;
72 struct superblock *sb = malloc(BLKSZ);
73 char name[16];
75 assert(sb);
77 num_dev = ata_num_devices();
78 for(i=0; i<num_dev; i++) {
79 plist = p = get_part_list(i);
81 partid = 0;
82 while(p) {
83 if(get_part_type(p) == PART_TYPE) {
84 /* found the correct partition, now read the superblock
85 * and make sure it's got the correct magic id
86 */
87 blk_read(i, p->start_sect / 2 + 1, BLKSZ, sb);
89 if(sb->magic == MAGIC) {
90 sprintf(name, "ata%dp%d", i, partid);
91 printf("found root: %s\n", name);
92 dev = bdev_by_name(name);
93 break;
94 }
95 }
96 p = p->next;
97 partid++;
98 }
99 free_part_list(plist);
100 if(dev) break;
101 }
103 free(sb);
104 return dev;
105 }