-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmkfs.c
413 lines (362 loc) · 10.8 KB
/
mkfs.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/stat.h>
typedef struct stat __st;
#define stat xv6_stat // avoid clash with host struct stat
#include "types.h"
#include "fs.h"
#include "stat.h"
#include "param.h"
//#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
int nblocks = 8124;
int nlog = LOGSIZE;
int ninodes = 200;
int size = 8192;
int fsfd;
struct superblock sb;
char zeroes[512];
uint freeblock;
uint usedblocks;
uint bitblocks;
uint freeinode = 1;
void balloc(int);
void wsect(uint, void*);
void winode(uint, struct dinode*);
void rinode(uint inum, struct dinode *ip);
void rsect(uint sec, void *buf);
uint ialloc(ushort type, int mode, int uid, int gid);
void iappend(uint inum, void *p, int n);
// convert to intel byte order
ushort
xshort(ushort x)
{
ushort y;
uchar *a = (uchar*)&y;
a[0] = x;
a[1] = x >> 8;
return y;
}
uint
xint(uint x)
{
uint y;
uchar *a = (uchar*)&y;
a[0] = x;
a[1] = x >> 8;
a[2] = x >> 16;
a[3] = x >> 24;
return y;
}
struct disk_file
{
const char* original_filename;
const char* xv6_filename;
int mode;
int uid;
int gid;
};
#define DEFAULT_DIR S_IFDIR | 0755
#define DEFAULT_FILE S_IFREG | 0644
struct disk_file files[] =
{
{"", "etc", DEFAULT_DIR, 0, 0},
{"passwd_file", "etc/passwd", S_IFREG | S_IRUGO | S_IWUSR, 0, 0},
{"group_file", "etc/group", S_IFREG | S_IRUGO | S_IWUSR, 0, 0},
{"", "root", S_IFDIR | S_IRWXU | S_IRGRP | S_IXGRP, 0, 0},
{"", "home", DEFAULT_DIR, 0, 0},
{"", "home/user", DEFAULT_DIR, 1, 1},
{"README", "home/user/README",DEFAULT_FILE, 1, 1},
{"", "bin", DEFAULT_DIR, 0, 0},
{"_cat", "bin/cat", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_echo", "bin/echo", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_grep", "bin/grep", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_init", "bin/init", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_kill", "bin/kill", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_ln", "bin/ln", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_ls", "bin/ls", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_mkdir", "bin/mkdir", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_rm", "bin/rm", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_sh", "bin/sh", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_wc", "bin/wc", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_zombie", "bin/zombie", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_helloworld", "bin/helloworld", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_mkfifo", "bin/mkfifo", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_passwd", "bin/passwd", S_ISUID | S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_login", "bin/login", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_id", "bin/id", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_chmod", "bin/chmod", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_chown", "bin/chown", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_useradd", "bin/useradd", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_ps", "bin/ps", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_chroot", "bin/chroot", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"", "test", DEFAULT_DIR, 0, 0},
{"_usertests", "test/usertests", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_stressfs", "test/stressfs", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_forktest", "test/forktest", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_mmap_test", "test/mmap_test", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_mmap_pp", "test/mmap_pp", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_thread_test","test/thread_test", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"", "test/chroot", DEFAULT_DIR, 0, 0},
{"", "test/chroot/bin", DEFAULT_DIR, 0, 0},
{"_sh", "test/chroot/bin/sh", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_ls", "test/chroot/bin/ls", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
{"_mkdir", "test/chroot/bin/mkdir", S_IFREG | S_IRUGO | S_IWUSR | S_IXUGO, 0, 0},
};
int added_inode_numbers[NELEM(files)];
int
main(int argc, char *argv[])
{
int i, cc, fd;
uint rootino, inum, off;
struct dirent de;
char buf[512];
struct dinode din;
static_assert(sizeof(int) == 4, "Integers must be 4 bytes!");
if(argc < 2){
fprintf(stderr, "Usage: mkfs fs.img files...\n");
exit(1);
}
assert((512 % sizeof(struct dinode)) == 0);
assert((512 % sizeof(struct dirent)) == 0);
fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
if(fsfd < 0){
perror(argv[1]);
exit(1);
}
sb.size = xint(size);
sb.nblocks = xint(nblocks); // so whole disk is size sectors
sb.ninodes = xint(ninodes);
sb.nlog = xint(nlog);
bitblocks = size/(512*4) + 1;
usedblocks = ninodes / IPB + 3 + bitblocks;
freeblock = usedblocks;
printf("used %d (bit %d ninode %zu) free %u log %u total %d\n", usedblocks,
bitblocks, ninodes/IPB + 1, freeblock, nlog, nblocks+usedblocks+nlog);
assert(nblocks + usedblocks + nlog == size);
for(i = 0; i < nblocks + usedblocks + nlog; i++)
wsect(i, zeroes);
memset(buf, 0, sizeof(buf));
memmove(buf, &sb, sizeof(sb));
wsect(1, buf);
rootino = ialloc(T_DIR, 0755, 0, 0);
assert(rootino == ROOTINO);
bzero(&de, sizeof(de));
de.inum = xshort(rootino);
strcpy(de.name, ".");
iappend(rootino, &de, sizeof(de));
bzero(&de, sizeof(de));
de.inum = xshort(rootino);
strcpy(de.name, "..");
iappend(rootino, &de, sizeof(de));
for(i = 0; i < NELEM(files); i++){
if((fd = open(files[i].original_filename, 0)) < 0 &&
strlen(files[i].original_filename) > 0){
perror(files[i].original_filename);
exit(1);
}
int dp = -1;
const char* last_slash = strrchr(files[i].xv6_filename, '/');
// Find parent inode
if (last_slash) {
int parent_path_length = last_slash - files[i].xv6_filename;
for (int j = 0; j < i; ++j) {
if (strncmp(files[j].xv6_filename, files[i].xv6_filename,
parent_path_length) == 0 &&
(files[j].xv6_filename[parent_path_length] == 0 ||
(files[j].xv6_filename[parent_path_length] == '/' &&
files[j].xv6_filename[parent_path_length + 1] == 0))) {
dp = added_inode_numbers[j];
break;
}
}
if (last_slash == files[i].xv6_filename) {
dp = rootino;
}
if (dp < 0) {
perror("Could not find parent");
printf("Filename: %s\n", files[i].xv6_filename);
exit(1);
}
}
else {
dp = rootino;
last_slash = files[i].xv6_filename - 1;
}
inum = ialloc(0, files[i].mode, files[i].uid, files[i].gid);
bzero(&de, sizeof(de));
de.inum = xshort(inum);
strncpy(de.name, last_slash + 1, DIRSIZ);
iappend(dp, &de, sizeof(de));
if (S_ISDIR(files[i].mode)) {
bzero(&de, sizeof(de));
de.inum = xshort(inum);
strcpy(de.name, ".");
iappend(inum, &de, sizeof(de));
bzero(&de, sizeof(de));
de.inum = xshort(dp);
strcpy(de.name, "..");
iappend(inum, &de, sizeof(de));
}
added_inode_numbers[i] = inum;
if (strlen(files[i].original_filename) != 0) {
while((cc = read(fd, buf, sizeof(buf))) > 0)
iappend(inum, buf, cc);
close(fd);
}
}
// fix size of root inode dir
rinode(rootino, &din);
off = xint(din.size);
off = ((off/BSIZE) + 1) * BSIZE;
din.size = xint(off);
winode(rootino, &din);
balloc(usedblocks);
exit(0);
}
void
wsect(uint sec, void *buf)
{
if(lseek(fsfd, sec * 512L, 0) != sec * 512L){
perror("lseek");
exit(1);
}
if(write(fsfd, buf, 512) != 512){
perror("write");
exit(1);
}
}
uint
i2b(uint inum)
{
return (inum / IPB) + 2;
}
void
winode(uint inum, struct dinode *ip)
{
char buf[512];
uint bn;
struct dinode *dip;
bn = i2b(inum);
rsect(bn, buf);
dip = ((struct dinode*)buf) + (inum % IPB);
*dip = *ip;
wsect(bn, buf);
}
void
rinode(uint inum, struct dinode *ip)
{
char buf[512];
uint bn;
struct dinode *dip;
bn = i2b(inum);
rsect(bn, buf);
dip = ((struct dinode*)buf) + (inum % IPB);
*ip = *dip;
}
void
rsect(uint sec, void *buf)
{
if(lseek(fsfd, sec * 512L, 0) != sec * 512L){
perror("lseek");
exit(1);
}
if(read(fsfd, buf, 512) != 512){
perror("read");
exit(1);
}
}
int
type_to_mode(short type)
{
switch(type){
case T_DIR:
return 0040000;
case T_FILE:
return 0100000;
case T_DEV:
return 0020000;
case T_PIPE:
return 0010000;
}
return 0;
}
uint
ialloc(ushort type, int mode, int uid, int gid)
{
uint inum = freeinode++;
struct dinode din;
bzero(&din, sizeof(din));
din.mode = xint(type_to_mode(type) | mode);
din.nlink = xshort(1);
din.size = xint(0);
din.uid = uid;
din.gid = gid;
winode(inum, &din);
return inum;
}
void
balloc(int used)
{
uchar buf[512];
int i;
printf("balloc: first %d blocks have been allocated\n", used);
assert(used < 512*8);
bzero(buf, 512);
for(i = 0; i < used; i++){
buf[i/8] = buf[i/8] | (0x1 << (i%8));
}
printf("balloc: write bitmap block at sector %zu\n", ninodes/IPB + 3);
wsect(ninodes / IPB + 3, buf);
}
#define min(a, b) ((a) < (b) ? (a) : (b))
void
iappend(uint inum, void *xp, int n)
{
char *p = (char*)xp;
uint fbn, off, n1;
struct dinode din;
char buf[512];
uint indirect[NINDIRECT];
uint x;
rinode(inum, &din);
off = xint(din.size);
while(n > 0){
fbn = off / 512;
assert(fbn < MAXFILE);
if(fbn < NDIRECT){
if(xint(din.addrs[fbn]) == 0){
din.addrs[fbn] = xint(freeblock++);
usedblocks++;
}
x = xint(din.addrs[fbn]);
} else {
if(xint(din.addrs[NDIRECT]) == 0){
// printf("allocate indirect block\n");
din.addrs[NDIRECT] = xint(freeblock++);
usedblocks++;
}
// printf("read indirect block\n");
rsect(xint(din.addrs[NDIRECT]), (char*)indirect);
if(indirect[fbn - NDIRECT] == 0){
indirect[fbn - NDIRECT] = xint(freeblock++);
usedblocks++;
wsect(xint(din.addrs[NDIRECT]), (char*)indirect);
}
x = xint(indirect[fbn-NDIRECT]);
}
n1 = min(n, (fbn + 1) * 512 - off);
rsect(x, buf);
bcopy(p, buf + off - (fbn * 512), n1);
wsect(x, buf);
n -= n1;
off += n1;
p += n1;
}
din.size = xint(off);
winode(inum, &din);
}