forked from wtakuo/opfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newfs.c
117 lines (98 loc) · 2.92 KB
/
newfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* opfs: a simple utility for creating empty xv6 file system images
* Copyright (c) 2015-2019 Takuo Watanabe
*/
/* usage: newfs img_file size ninodes nlog
* size : total # of blocks
* ninodes : # of inodes
* nlog : # of log blocks
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <setjmp.h>
#include <stdarg.h>
#include <assert.h>
#include "libfs.h"
int setupfs(img_t img, uint size, uint ninodes, uint nlog) {
uint niblocks = ninodes / IPB + 1;
uint nmblocks = size / (BSIZE * 8) + 1;
uint nblocks = size - (2 + nlog + niblocks + nmblocks);
uint logstart = 2;
uint inodestart = logstart + nlog;
uint bmapstart = inodestart + niblocks;
uint dstart = bmapstart + nmblocks;
printf("# of blocks: %u\n", size);
printf("# of inodes: %u\n", ninodes);
printf("# of log blocks: %u\n", nlog);
printf("# of inode blocks: %u\n", niblocks);
printf("# of bitmap blocks: %u\n", nmblocks);
printf("# of data blocks: %u\n", nblocks);
// clear all blocks
memset((uchar *)img, 0, BSIZE * size);
// setup superblock
struct superblock sblk = {
FSMAGIC,
size, nblocks, ninodes, nlog, logstart, inodestart, bmapstart
};
memmove(img[1], (uchar *)&sblk, sizeof(sblk));
// setup initial bitmap
for (uint b = 0; b < dstart; b += BPB) {
uchar *bp = img[BBLOCK(b, SBLKS(img))];
for (int bi = 0; bi < BPB && b + bi < dstart; bi++) {
int m = 1 << (bi % 8);
bp[bi / 8] |= m;
}
}
// setup root directory
root_inode = ialloc(img, T_DIR);
assert(geti(img, root_inode) == root_inode_number);
daddent(img, root_inode, ".", root_inode);
daddent(img, root_inode, "..", root_inode);
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
if (argc != 5) {
fprintf(stderr, "usage: %s file size ninodes nlog\n", argv[0]);
return EXIT_FAILURE;
}
char *file = argv[1];
uint size = atoi(argv[2]);
uint ninodes = atoi(argv[3]);
uint nlog = atoi(argv[4]);
int fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
perror(file);
return EXIT_FAILURE;
}
size_t img_size = BSIZE * size;
lseek(fd, img_size - 1, SEEK_SET);
char c = 0;
if (write(fd, &c, 1) < 0) {
perror(file);
return EXIT_FAILURE;
}
img_t img = mmap(NULL, img_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (img == MAP_FAILED) {
perror(file);
close(fd);
return EXIT_FAILURE;
}
int status = EXIT_FAILURE;
if (setjmp(fatal_exception_buf) == 0)
status = setupfs(img, size, ninodes, nlog);
munmap(img, img_size);
close(fd);
return status;
}
/* For Emacs
* Local Variables: ***
* c-file-style: "gnu" ***
* c-basic-offset: 4 ***
* End: ***
*/