-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext2-create.c
570 lines (476 loc) · 15.6 KB
/
ext2-create.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef int16_t i16;
typedef int32_t i32;
#define BLOCK_SIZE 1024
#define BLOCK_OFFSET(i) (i * BLOCK_SIZE)
#define NUM_BLOCKS 1024
#define NUM_INODES 128
#define LOST_AND_FOUND_INO 11
#define HELLO_WORLD_INO 12
#define HELLO_INO 13
#define LAST_INO HELLO_INO
#define SUPERBLOCK_BLOCKNO 1
#define BLOCK_GROUP_DESCRIPTOR_BLOCKNO 2
#define BLOCK_BITMAP_BLOCKNO 3
#define INODE_BITMAP_BLOCKNO 4
#define INODE_TABLE_BLOCKNO 5
#define ROOT_DIR_BLOCKNO 21
#define LOST_AND_FOUND_DIR_BLOCKNO 22
#define HELLO_WORLD_FILE_BLOCKNO 23
#define LAST_BLOCK HELLO_WORLD_FILE_BLOCKNO
#define NUM_FREE_BLOCKS (NUM_BLOCKS - LAST_BLOCK - 1)
#define NUM_FREE_INODES (NUM_INODES - LAST_INO)
#define EXT2_SUPER_MAGIC 0xEF53
/* http://www.nongnu.org/ext2-doc/ext2.html */
/* http://www.science.smith.edu/~nhowe/262/oldlabs/ext2.html */
#define EXT2_BAD_INO 1
#define EXT2_ROOT_INO 2
#define EXT2_GOOD_OLD_FIRST_INO 11
#define EXT2_GOOD_OLD_REV 0
#define EXT2_S_IFSOCK 0xC000
#define EXT2_S_IFLNK 0xA000
#define EXT2_S_IFREG 0x8000
#define EXT2_S_IFBLK 0x6000
#define EXT2_S_IFDIR 0x4000
#define EXT2_S_IFCHR 0x2000
#define EXT2_S_IFIFO 0x1000
#define EXT2_S_ISUID 0x0800
#define EXT2_S_ISGID 0x0400
#define EXT2_S_ISVTX 0x0200
#define EXT2_S_IRUSR 0x0100
#define EXT2_S_IWUSR 0x0080
#define EXT2_S_IXUSR 0x0040
#define EXT2_S_IRGRP 0x0020
#define EXT2_S_IWGRP 0x0010
#define EXT2_S_IXGRP 0x0008
#define EXT2_S_IROTH 0x0004
#define EXT2_S_IWOTH 0x0002
#define EXT2_S_IXOTH 0x0001
#define EXT2_NDIR_BLOCKS 12
#define EXT2_IND_BLOCK EXT2_NDIR_BLOCKS
#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1)
#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
#define EXT2_NAME_LEN 255
struct ext2_superblock {
u32 s_inodes_count;
u32 s_blocks_count;
u32 s_r_blocks_count;
u32 s_free_blocks_count;
u32 s_free_inodes_count;
u32 s_first_data_block;
u32 s_log_block_size;
i32 s_log_frag_size;
u32 s_blocks_per_group;
u32 s_frags_per_group;
u32 s_inodes_per_group;
u32 s_mtime;
u32 s_wtime;
u16 s_mnt_count;
i16 s_max_mnt_count;
u16 s_magic;
u16 s_state;
u16 s_errors;
u16 s_minor_rev_level;
u32 s_lastcheck;
u32 s_checkinterval;
u32 s_creator_os;
u32 s_rev_level;
u16 s_def_resuid;
u16 s_def_resgid;
u32 s_pad[5];
u8 s_uuid[16];
u8 s_volume_name[16];
u32 s_reserved[229];
};
struct ext2_block_group_descriptor
{
u32 bg_block_bitmap;
u32 bg_inode_bitmap;
u32 bg_inode_table;
u16 bg_free_blocks_count;
u16 bg_free_inodes_count;
u16 bg_used_dirs_count;
u16 bg_pad;
u32 bg_reserved[3];
};
struct ext2_inode {
u16 i_mode;
u16 i_uid;
u32 i_size;
u32 i_atime;
u32 i_ctime;
u32 i_mtime;
u32 i_dtime;
u16 i_gid;
u16 i_links_count;
u32 i_blocks;
u32 i_flags;
u32 i_reserved1;
u32 i_block[EXT2_N_BLOCKS];
u32 i_version;
u32 i_file_acl;
u32 i_dir_acl;
u32 i_faddr;
u8 i_frag;
u8 i_fsize;
u16 i_pad1;
u32 i_reserved2[2];
};
struct ext2_dir_entry {
u32 inode;
u16 rec_len;
u16 name_len;
u8 name[EXT2_NAME_LEN];
};
#define errno_exit(str) \
do { int err = errno; perror(str); exit(err); } while (0)
#define dir_entry_set(entry, inode_num, str) \
do { \
char *s = str; \
size_t len = strlen(s); \
entry.inode = inode_num; \
entry.name_len = len; \
memcpy(&entry.name, s, len); \
if ((len % 4) != 0) { \
entry.rec_len = 12 + len / 4 * 4; \
} \
else { \
entry.rec_len = 8 + len; \
} \
} while (0)
#define dir_entry_write(entry, fd) \
do { \
size_t size = entry.rec_len; \
if (write(fd, &entry, size) != size) { \
errno_exit("write"); \
} \
} while (0)
u32 get_current_time() {
time_t t = time(NULL);
if (t == ((time_t) -1)) {
errno_exit("time");
}
return t;
}
void write_superblock(int fd) {
off_t off = lseek(fd, BLOCK_OFFSET(1), SEEK_SET);
if (off == -1) {
errno_exit("lseek");
}
u32 current_time = get_current_time();
struct ext2_superblock superblock = {0};
// TODO It's all yours
// TODO finish the superblock number setting
superblock.s_inodes_count = NUM_INODES;
superblock.s_blocks_count = NUM_BLOCKS;
superblock.s_r_blocks_count = 0;
superblock.s_free_blocks_count = NUM_FREE_BLOCKS;
superblock.s_free_inodes_count = NUM_FREE_INODES;
superblock.s_first_data_block = SUPERBLOCK_BLOCKNO; /* First Data Block */
superblock.s_log_block_size = 0;
superblock.s_log_frag_size = 0;
superblock.s_blocks_per_group = NUM_BLOCKS*8;
superblock.s_frags_per_group = NUM_BLOCKS*8;
superblock.s_inodes_per_group = NUM_INODES; /* 1024 */
superblock.s_mtime = 0; /* Mount time */
superblock.s_wtime = current_time; /* Write time */
superblock.s_mnt_count = 0; /* Number of times mounted so far */
superblock.s_max_mnt_count = -1; /* Make this unlimited */
superblock.s_magic = EXT2_SUPER_MAGIC; /* ext2 Signature */
superblock.s_state = 1; /* File system is clean */
superblock.s_errors = 1; /* Ignore the error (continue on) */
superblock.s_minor_rev_level = 0; /* Leave this as 0 */
superblock.s_lastcheck = current_time; /* Last check time */
superblock.s_checkinterval = 1; /* Force checks by making them every 1 second */
superblock.s_creator_os = 0; /* Linux */
superblock.s_rev_level = 0; /* Leave this as 0 */
superblock.s_def_resuid = 0; /* root */
superblock.s_def_resgid = 0; /* root */
/* You can leave everything below this line the same, delete this
comment when you're done the lab */
superblock.s_uuid[0] = 0x5A;
superblock.s_uuid[1] = 0x1E;
superblock.s_uuid[2] = 0xAB;
superblock.s_uuid[3] = 0x1E;
superblock.s_uuid[4] = 0x13;
superblock.s_uuid[5] = 0x37;
superblock.s_uuid[6] = 0x13;
superblock.s_uuid[7] = 0x37;
superblock.s_uuid[8] = 0x13;
superblock.s_uuid[9] = 0x37;
superblock.s_uuid[10] = 0xC0;
superblock.s_uuid[11] = 0xFF;
superblock.s_uuid[12] = 0xEE;
superblock.s_uuid[13] = 0xC0;
superblock.s_uuid[14] = 0xFF;
superblock.s_uuid[15] = 0xEE;
memcpy(&superblock.s_volume_name, "cs111-base", 10);
ssize_t size = sizeof(superblock);
if (write(fd, &superblock, size) != size) {
errno_exit("write");
}
}
void write_block_group_descriptor_table(int fd) {
off_t off = lseek(fd, BLOCK_OFFSET(BLOCK_GROUP_DESCRIPTOR_BLOCKNO), SEEK_SET);
if (off == -1) {
errno_exit("lseek");
}
struct ext2_block_group_descriptor block_group_descriptor = {0};
// TODO It's all yours
// TODO finish the block group descriptor number setting
block_group_descriptor.bg_block_bitmap = BLOCK_BITMAP_BLOCKNO;
block_group_descriptor.bg_inode_bitmap = INODE_BITMAP_BLOCKNO;
block_group_descriptor.bg_inode_table = INODE_TABLE_BLOCKNO;
block_group_descriptor.bg_free_blocks_count = NUM_FREE_BLOCKS;
block_group_descriptor.bg_free_inodes_count = NUM_FREE_INODES;
block_group_descriptor.bg_used_dirs_count = 2;
ssize_t size = sizeof(block_group_descriptor);
if (write(fd, &block_group_descriptor, size) != size) {
errno_exit("write");
}
}
void write_block_bitmap(int fd)
{
off_t off = lseek(fd, BLOCK_OFFSET(BLOCK_BITMAP_BLOCKNO), SEEK_SET);
if (off == -1)
{
errno_exit("lseek");
}
// TODO It's all yours
u8 map_value[BLOCK_SIZE] = {0};
for (int i = 0; i < LAST_BLOCK; i+= 8) {
map_value[i / 8] = -1;
}
if (LAST_BLOCK % 8 != 0) {// we have a remainder
int remainder = LAST_BLOCK % 8;
u8 mask = -1;
mask >>= (8 - remainder);
map_value[LAST_BLOCK / 8] = mask;
}
map_value[ NUM_BLOCKS / 8 - 1] = 1 << 7;
for (int i = NUM_BLOCKS / 8; i < BLOCK_SIZE; ++i) {
map_value[i] = -1;
}
if (write(fd, map_value, BLOCK_SIZE) != BLOCK_SIZE)
{
errno_exit("write");
}
}
void write_inode_bitmap(int fd)
{
off_t off = lseek(fd, BLOCK_OFFSET(INODE_BITMAP_BLOCKNO), SEEK_SET);
if (off == -1)
{
errno_exit("lseek");
}
// TODO It's all yours
u8 map_value[BLOCK_SIZE] = { 0 };
for (int i = 0; i < LAST_INO; i+= 8) {
map_value[i / 8] = -1;
}
if (LAST_INO % 8 != 0) {// we have a remainder
int remainder = LAST_INO % 8;
u8 mask = -1;
mask >>= (8 - remainder);
map_value[LAST_INO / 8] = mask;
}
for (int i = NUM_INODES / 8; i < BLOCK_SIZE; ++i) {
map_value[i] = -1;
}
if (write(fd, map_value, BLOCK_SIZE) != BLOCK_SIZE)
{
errno_exit("write");
}
}
void write_inode(int fd, u32 index, struct ext2_inode *inode) {
off_t off = BLOCK_OFFSET(INODE_TABLE_BLOCKNO)
+ (index - 1) * sizeof(struct ext2_inode);
off = lseek(fd, off, SEEK_SET);
if (off == -1) {
errno_exit("lseek");
}
ssize_t size = sizeof(struct ext2_inode);
if (write(fd, inode, size) != size) {
errno_exit("write");
}
}
void write_inode_table(int fd) {
u32 current_time = get_current_time();
struct ext2_inode lost_and_found_inode = {0};
lost_and_found_inode.i_mode = EXT2_S_IFDIR
| EXT2_S_IRUSR
| EXT2_S_IWUSR
| EXT2_S_IXUSR
| EXT2_S_IRGRP
| EXT2_S_IXGRP
| EXT2_S_IROTH
| EXT2_S_IXOTH;
lost_and_found_inode.i_uid = 0;
lost_and_found_inode.i_size = 1024;
lost_and_found_inode.i_atime = current_time;
lost_and_found_inode.i_ctime = current_time;
lost_and_found_inode.i_mtime = current_time;
lost_and_found_inode.i_dtime = 0;
lost_and_found_inode.i_gid = 0;
lost_and_found_inode.i_links_count = 2;
lost_and_found_inode.i_blocks = 2; /* These are oddly 512 blocks */
lost_and_found_inode.i_block[0] = LOST_AND_FOUND_DIR_BLOCKNO;
write_inode(fd, LOST_AND_FOUND_INO, &lost_and_found_inode);
// TODO It's all yours
// TODO finish the inode entries for the other files
struct ext2_inode root_inode = { 0};
root_inode.i_mode =EXT2_S_IFDIR | EXT2_S_IWUSR | EXT2_S_IXUSR | EXT2_S_IRUSR | EXT2_S_IRGRP | EXT2_S_IXGRP | EXT2_S_IROTH | EXT2_S_IXOTH ;
root_inode.i_uid = 0;
root_inode.i_gid = 0;
root_inode.i_size = 1024;
root_inode.i_atime = current_time;
root_inode.i_ctime = current_time;
root_inode.i_mtime = current_time;
root_inode.i_dtime = 0;
root_inode.i_links_count = 3;
root_inode.i_blocks = 2;
root_inode.i_block[0] = ROOT_DIR_BLOCKNO;
write_inode(fd, EXT2_ROOT_INO, &root_inode);
struct ext2_inode hello_world_inode = {0};
hello_world_inode.i_mode = EXT2_S_IFREG | EXT2_S_IRUSR | EXT2_S_IWUSR | EXT2_S_IRGRP | EXT2_S_IROTH;
hello_world_inode.i_uid = 1000;
hello_world_inode.i_size = 12;
hello_world_inode.i_atime = current_time;
hello_world_inode.i_ctime = current_time;
hello_world_inode.i_mtime = current_time;
hello_world_inode.i_dtime = 0;
hello_world_inode.i_gid = 1000;
hello_world_inode.i_links_count = 1;
hello_world_inode.i_blocks = 2;
hello_world_inode.i_block[0] = HELLO_WORLD_FILE_BLOCKNO;
write_inode(fd, HELLO_WORLD_INO, &hello_world_inode);
struct ext2_inode hello_inode = {0};
hello_inode.i_mode = EXT2_S_IFLNK | EXT2_S_IRUSR | EXT2_S_IWUSR | EXT2_S_IRGRP | EXT2_S_IROTH;
hello_inode.i_uid = 1000;
hello_inode.i_gid = 1000;
hello_inode.i_size = 11;
hello_inode.i_atime = current_time;
hello_inode.i_ctime = current_time;
hello_inode.i_mtime = current_time;
hello_inode.i_dtime = 0;
hello_inode.i_links_count = 1;
memcpy(hello_inode.i_block, "hello-world", hello_inode.i_size);
hello_inode.i_blocks = 0;
write_inode(fd, HELLO_INO, &hello_inode);
}
void write_root_dir_block(int fd)
{
off_t off = BLOCK_OFFSET(ROOT_DIR_BLOCKNO);
off = lseek(fd, off, SEEK_SET);
if (off == -1) {
errno_exit("lseek");
}
ssize_t bytes_remaining = BLOCK_SIZE;
struct ext2_dir_entry current_entry = {0};
dir_entry_set(current_entry, EXT2_ROOT_INO, ".");
dir_entry_write(current_entry, fd);
bytes_remaining -= current_entry.rec_len;
struct ext2_dir_entry parent_entry = {0};
dir_entry_set(parent_entry, EXT2_ROOT_INO, "..");
dir_entry_write(parent_entry, fd);
bytes_remaining -= parent_entry.rec_len;
struct ext2_dir_entry child_entry = {0};
dir_entry_set(child_entry, LOST_AND_FOUND_INO, "lost+found");
dir_entry_write(child_entry, fd);
bytes_remaining -= child_entry.rec_len;
struct ext2_dir_entry child_entry2 = {0};
dir_entry_set(child_entry2, HELLO_WORLD_INO, "hello-world");
dir_entry_write(child_entry2, fd);
bytes_remaining -= child_entry2.rec_len;
struct ext2_dir_entry child_entry3 = {0};
dir_entry_set(child_entry3, HELLO_INO, "hello");
dir_entry_write(child_entry3, fd);
bytes_remaining -= child_entry3.rec_len;
if (bytes_remaining > 0) {
struct ext2_dir_entry fill_entry = {0};
fill_entry.rec_len = bytes_remaining;
dir_entry_write(fill_entry, fd);
}
}
void write_lost_and_found_dir_block(int fd) {
off_t off = BLOCK_OFFSET(LOST_AND_FOUND_DIR_BLOCKNO);
off = lseek(fd, off, SEEK_SET);
if (off == -1) {
errno_exit("lseek");
}
ssize_t bytes_remaining = BLOCK_SIZE;
struct ext2_dir_entry current_entry = {0};
dir_entry_set(current_entry, LOST_AND_FOUND_INO, ".");
dir_entry_write(current_entry, fd);
bytes_remaining -= current_entry.rec_len;
struct ext2_dir_entry parent_entry = {0};
dir_entry_set(parent_entry, EXT2_ROOT_INO, "..");
dir_entry_write(parent_entry, fd);
bytes_remaining -= parent_entry.rec_len;
struct ext2_dir_entry fill_entry = {0};
fill_entry.rec_len = bytes_remaining;
dir_entry_write(fill_entry, fd);
}
void write_hello_world_file_block(int fd)
{
off_t off = BLOCK_OFFSET(HELLO_WORLD_FILE_BLOCKNO);
off = lseek(fd, off, SEEK_SET);
if (off == -1) {
errno_exit("lseek");
}
ssize_t bytes_remaining = BLOCK_SIZE;
const char *str = "Hello world\n";
ssize_t str_size = strlen(str); // Get the length of the string
if (write(fd, str, str_size) != str_size) {
exit(errno);
}
bytes_remaining -= str_size;
struct ext2_dir_entry current_entry = {0};
dir_entry_set(current_entry, HELLO_WORLD_INO, ".");
dir_entry_write(current_entry, fd);
bytes_remaining -= current_entry.rec_len;
struct ext2_dir_entry parent_entry = {0};
dir_entry_set(parent_entry, EXT2_ROOT_INO, "..");
dir_entry_write(parent_entry, fd);
bytes_remaining -= parent_entry.rec_len;
struct ext2_dir_entry fill_entry = {0};
fill_entry.rec_len = bytes_remaining;
dir_entry_write(fill_entry, fd);
}
int main(int argc, char *argv[]) {
int fd = open("cs111-base.img", O_CREAT | O_WRONLY, 0666);
if (fd == -1) {
errno_exit("open");
}
if (ftruncate(fd, 0)) {
errno_exit("ftruncate");
}
if (ftruncate(fd, NUM_BLOCKS * BLOCK_SIZE)) {
errno_exit("ftruncate");
}
write_superblock(fd);
write_block_group_descriptor_table(fd);
write_block_bitmap(fd);
write_inode_bitmap(fd);
write_inode_table(fd);
write_root_dir_block(fd);
write_lost_and_found_dir_block(fd);
write_hello_world_file_block(fd);
if (close(fd)) {
errno_exit("close");
}
return 0;
}