kern

annotate src/fs.c @ 90:7ff2b4971216

started work on the filesystem
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 13:44:15 +0200
parents 2f555c81ae67
children 083849df660b
rev   line source
nuclear@90 1 /* This code is used by the kernel AND by userspace filesystem-related tools.
nuclear@90 2 * The kernel-specific parts are conditionally compiled in #ifdef KERNEL blocks
nuclear@90 3 * the rest of the code should be independent.
nuclear@90 4 */
nuclear@88 5 #include <stdio.h>
nuclear@89 6 #include <stdlib.h>
nuclear@89 7 #include <assert.h>
nuclear@90 8 #include <errno.h>
nuclear@88 9 #include "fs.h"
nuclear@90 10 #include "part.h"
nuclear@90 11
nuclear@90 12 #ifdef KERNEL
nuclear@88 13 #include "ata.h"
nuclear@88 14 #include "panic.h"
nuclear@90 15 #endif
nuclear@89 16
nuclear@89 17 struct filesys {
nuclear@89 18 int dev;
nuclear@89 19 struct partition part;
nuclear@89 20
nuclear@89 21 struct superblock *sb;
nuclear@90 22
nuclear@90 23 struct filesys *next;
nuclear@89 24 };
nuclear@89 25
nuclear@89 26 static int find_rootfs(struct filesys *fs);
nuclear@90 27 static int readblock(int dev, uint32_t blk, void *buf);
nuclear@90 28 static int writeblock(int dev, uint32_t blk, void *buf);
nuclear@88 29
nuclear@88 30 /* root device & partition */
nuclear@90 31 static struct filesys *fslist;
nuclear@90 32
nuclear@90 33 int sys_mount(char *mtpt, char *devname, unsigned int flags)
nuclear@90 34 {
nuclear@90 35 if(strcmp(mtpt, "/") != 0) {
nuclear@90 36 printf("mount: only root can be mounted at the moment\n");
nuclear@90 37 return -EBUG;
nuclear@90 38 }
nuclear@90 39
nuclear@90 40 /* mounting root filesystem */
nuclear@90 41 if(fslist) {
nuclear@90 42
nuclear@90 43 }
nuclear@88 44
nuclear@88 45 void init_fs(void)
nuclear@88 46 {
nuclear@89 47 root.sb = malloc(512);
nuclear@89 48 assert(root.sb);
nuclear@89 49
nuclear@90 50 #ifdef KERNEL
nuclear@89 51 if(find_rootfs(&root) == -1) {
nuclear@88 52 panic("can't find root filesystem\n");
nuclear@88 53 }
nuclear@90 54 #endif
nuclear@88 55 }
nuclear@88 56
nuclear@90 57
nuclear@90 58 #ifdef KERNEL
nuclear@89 59 #define PART_TYPE 0xcc
nuclear@89 60 static int find_rootfs(struct filesys *fs)
nuclear@88 61 {
nuclear@88 62 int i, num_dev, partid;
nuclear@88 63 struct partition *plist, *p;
nuclear@88 64
nuclear@88 65 num_dev = ata_num_devices();
nuclear@88 66 for(i=0; i<num_dev; i++) {
nuclear@88 67 plist = p = get_part_list(i);
nuclear@88 68
nuclear@88 69 partid = 0;
nuclear@88 70 while(p) {
nuclear@88 71 if(get_part_type(p) == PART_TYPE) {
nuclear@89 72 /* found the correct partition, now read the superblock
nuclear@89 73 * and make sure it's got the correct magic id
nuclear@89 74 */
nuclear@90 75 readblock(i, p->start_sect / 2 + 1, fs->sb);
nuclear@89 76
nuclear@89 77 if(fs->sb->magic == MAGIC) {
nuclear@89 78 printf("found root ata%dp%d\n", i, partid);
nuclear@89 79 fs->dev = i;
nuclear@89 80 fs->part = *p;
nuclear@89 81 return 0;
nuclear@89 82 }
nuclear@88 83 }
nuclear@88 84 p = p->next;
nuclear@88 85 partid++;
nuclear@88 86 }
nuclear@88 87 free_part_list(plist);
nuclear@88 88 }
nuclear@88 89 return -1;
nuclear@88 90 }
nuclear@90 91
nuclear@90 92 #define NSECT (BLKSZ / 512)
nuclear@90 93
nuclear@90 94 static int readblock(struct block_device *bdev, uint32_t blk, void *buf)
nuclear@90 95 {
nuclear@90 96 return blk_read(bdev, blk, buf);
nuclear@90 97 }
nuclear@90 98
nuclear@90 99 static int writeblock(struct block_device *bdev, uint32_t blk, void *buf)
nuclear@90 100 {
nuclear@90 101 return blk_write(bdev, blk, buf);
nuclear@90 102 }
nuclear@90 103 #else
nuclear@90 104
nuclear@90 105 /* if this is compiled as part of the user-space tools instead of the kernel
nuclear@90 106 * forward the call to a user read/write block function supplied by the app.
nuclear@90 107 */
nuclear@90 108 int user_readblock(uint32_t, void*);
nuclear@90 109 int user_writeblock(uint32_t, void*);
nuclear@90 110
nuclear@90 111 static int readblock(struct block_device *bdev, uint32_t blk, void *buf)
nuclear@90 112 {
nuclear@90 113 return user_readblock(blk, buf);
nuclear@90 114 }
nuclear@90 115
nuclear@90 116 static int writeblock(struct block_device *bdev, uint32_t blk, void *buf)
nuclear@90 117 {
nuclear@90 118 return user_writeblock(blk, buf);
nuclear@90 119 }
nuclear@90 120 #endif /* KERNEL */