kern
view src/fs.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 | 7ff2b4971216 |
children | b3351d018ac6 |
line source
1 /* This code is used by the kernel AND by userspace filesystem-related tools.
2 * The kernel-specific parts are conditionally compiled in #ifdef KERNEL blocks
3 * the rest of the code should be independent.
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 #include "fs.h"
9 #include "bdev.h"
12 int openfs(struct filesys *fs, dev_t dev);
13 static int read_superblock(struct block_device *bdev, struct superblock *sb);
16 int openfs(struct filesys *fs, dev_t dev)
17 {
18 int res;
19 struct block_device *bdev;
20 struct superblock *sb = 0;
22 if(!(bdev = blk_open(dev))) {
23 return -ENOENT;
24 }
26 /* read the superblock */
27 if(!(sb = malloc(BLKSZ))) {
28 res = -ENOMEM;
29 goto done;
30 }
31 if((res = read_superblock(bdev, sb)) != 0) {
32 goto done;
33 }
38 done:
39 blk_close(bdev);
40 free(sb);
41 return res;
42 }
44 static int read_superblock(struct block_device *bdev, struct superblock *sb)
45 {
46 /* read superblock and verify */
47 if(blk_read(bdev, 1, 1, sb) == -1) {
48 printf("failed to read superblock\n");
49 return -EIO;
50 }
51 if(sb->magic != MAGIC) {
52 printf("invalid magic\n");
53 return -EINVAL;
54 }
55 if(sb->ver > FS_VER) {
56 printf("invalid version: %d\n", sb->ver);
57 return -EINVAL;
58 }
59 if(sb->blksize != BLKSZ) {
60 printf("invalid block size: %d\n", sb->blksize);
61 return -EINVAL;
62 }
64 /* allocate and populate in-memory bitmaps */
65 if(!(sb->ibm = malloc(sb->ibm_count * sb->blksize))) {
66 return -ENOMEM;
67 }
68 if(blk_read(bdev, sb->ibm_start, sb->ibm_count, sb->ibm) == -1) {
69 printf("failed to read inode bitmap\n");
70 free(sb->ibm);
71 return -EIO;
72 }
73 if(!(sb->bm = malloc(sb->bm_count * sb->blksize))) {
74 free(sb->ibm);
75 return -ENOMEM;
76 }
77 if(blk_read(bdev, sb->bm_start, sb->bm_count, sb->bm) == -1) {
78 printf("failed to read block bitmap\n");
79 free(sb->ibm);
80 free(sb->bm);
81 return -EIO;
82 }
84 return 0;
85 }