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