-
Notifications
You must be signed in to change notification settings - Fork 10
/
mkfs.c
308 lines (271 loc) · 7.2 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
/*
* HUST_fs disk layout:
* 100MB disk -> 25600 blocks
* And can write 25600 files at most.
* inode size is 128B
* block size is 4096B <=> 4K
* block0 |dummy block
* block1 |super block
* block2 |bmap block
* block3 |imap block
* block4 - block(25600/(4096/64) + 3) |inode table
* other blocks |data blocks
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <endian.h>
#include <linux/stat.h>
#include "constants.h"
static uint8_t* bmap;
static uint8_t* imap;
static uint64_t disk_size;
static uint64_t bmap_size;
static uint64_t imap_size;
static uint64_t inode_table_size;
struct HUST_fs_super_block {
uint64_t version;
uint64_t magic;
uint64_t block_size;
uint64_t inodes_count;
uint64_t free_blocks;
uint64_t blocks_count;
uint64_t bmap_block;
uint64_t imap_block;
uint64_t inode_table_block;
uint64_t data_block_number;
char padding[4016];
};
static struct HUST_fs_super_block super_block;
struct HUST_inode {
mode_t mode;//sizeof(mode_t) is 4
uint64_t inode_no;
uint64_t blocks;
uint64_t block[HUST_N_BLOCKS];
union {
uint64_t file_size;
uint64_t dir_children_count;
};
int32_t i_uid;
int32_t i_gid;
int32_t i_nlink;
int64_t i_atime;
int64_t i_mtime;
int64_t i_ctime;
char padding[112];
};
#define HUST_INODE_SIZE sizeof(struct HUST_inode)
struct HUST_dir_record
{
char filename[HUST_FILENAME_MAX_LEN];
uint64_t inode_no;
};
static off_t get_file_size(const char* path)
{
off_t ret = -1;
struct stat statbuf;
if (stat(path, &statbuf) < 0) {
return ret;
}
return statbuf.st_size;
}
static int set_bmap(uint64_t idx, int value)
{
if(!bmap) {
return -1;
}
uint64_t array_idx = idx/(sizeof(char)*8);
uint64_t off = idx%(sizeof(char)*8);
if(array_idx > bmap_size*HUST_BLOCKSIZE) {
printf("Set bmap error and idx is %llu\n", idx);
return -1;
}
if(value)
bmap[array_idx] |= (1<<off);
else
bmap[array_idx] &= ~(1<<off);
return 0;
}
static int init_disk(int fd, const char* path)
{
disk_size = get_file_size(path);
if (disk_size == -1) {
perror("Error: can not get disk size!\n");
return -1;
}
printf("Disk size id %lu\n", disk_size);
super_block.version = 1;
super_block.block_size = HUST_BLOCKSIZE;
super_block.magic = MAGIC_NUM;
super_block.blocks_count = disk_size/HUST_BLOCKSIZE;
printf("blocks count is %llu\n", super_block.blocks_count);
super_block.inodes_count = super_block.blocks_count;
super_block.free_blocks = 0;
//计算bmap
bmap_size = super_block.blocks_count/(8*HUST_BLOCKSIZE);
super_block.bmap_block = RESERVE_BLOCKS;
if (super_block.blocks_count%(8*HUST_BLOCKSIZE) != 0) {
bmap_size += 1;
}
bmap = (uint8_t *)malloc(bmap_size*HUST_BLOCKSIZE);
memset(bmap,0,bmap_size*HUST_BLOCKSIZE);
//计算imap
imap_size = super_block.inodes_count/(8*HUST_BLOCKSIZE);
super_block.imap_block = super_block.bmap_block + bmap_size;
if(super_block.inodes_count%(8*HUST_BLOCKSIZE) != 0) {
imap_size += 1;
}
imap = (uint8_t *)malloc(imap_size*HUST_BLOCKSIZE);
memset(imap,0,imap_size*HUST_BLOCKSIZE);
//计算inode_table
inode_table_size = super_block.inodes_count/(HUST_BLOCKSIZE/HUST_INODE_SIZE);
super_block.inode_table_block = super_block.imap_block + imap_size;
super_block.data_block_number = RESERVE_BLOCKS + bmap_size + imap_size + inode_table_size;
super_block.free_blocks = super_block.blocks_count - super_block.data_block_number - 1;
//设置bmap以及imap
int idx;
// plus one becase of the root dir
for (idx = 0; idx < super_block.data_block_number + 1; ++idx) {
if (set_bmap(idx, 1)) {
return -1;
}
}
return 0;
}
static int write_sb(int fd)
{
ssize_t ret;
ret = write(fd, &super_block, sizeof(super_block));
if(ret != HUST_BLOCKSIZE) {
perror("Write super block error!\n");
return -1;
}
printf("Super block written succesfully!\n");
return 0;
}
static int write_bmap(int fd)
{
ssize_t ret = -1;
ret = write(fd, bmap, bmap_size*HUST_BLOCKSIZE);
if (ret != bmap_size*HUST_BLOCKSIZE) {
perror("Write_bmap() error!\n");
return -1;
}
return 0;
}
static int write_imap(int fd)
{
memset(imap, 0, imap_size*HUST_BLOCKSIZE);
imap[0] |= 0x3;
ssize_t res = write(fd, imap, imap_size*HUST_BLOCKSIZE);
if (res != imap_size*HUST_BLOCKSIZE) {
perror("write_imap() erroe!");
return -1;
}
return 0;
}
static int write_itable(int fd)
{
uint32_t _uid = getuid();
uint32_t _gid = getgid();
ssize_t ret;
struct HUST_inode root_dir_inode;
root_dir_inode.mode = S_IFDIR;
root_dir_inode.inode_no = HUST_ROOT_INODE_NUM;
root_dir_inode.blocks = 1;
root_dir_inode.block[0] = super_block.data_block_number;
root_dir_inode.dir_children_count = 3;
root_dir_inode.i_gid = _gid;
root_dir_inode.i_uid = _uid;
root_dir_inode.i_nlink = 2;
root_dir_inode.i_atime = root_dir_inode.i_mtime = root_dir_inode.i_ctime = ((int64_t)time(NULL));
ret = write(fd, &root_dir_inode, sizeof(root_dir_inode));
if (ret != sizeof(root_dir_inode)) {
perror("write_itable error!\n");
return -1;
}
struct HUST_inode onefile_inode;
onefile_inode.mode = S_IFREG;
onefile_inode.inode_no = 1;
onefile_inode.blocks = 0;
onefile_inode.block[0] = 0;
onefile_inode.file_size = 0;
onefile_inode.i_gid = _gid;
onefile_inode.i_uid = _uid;
onefile_inode.i_nlink = 1;
onefile_inode.i_atime = onefile_inode.i_mtime = onefile_inode.i_ctime = ((int64_t)time(NULL));
ret = write(fd, &onefile_inode, sizeof(onefile_inode));
if (ret != sizeof(onefile_inode)) {
perror("write_itable error!\n");
return -1;
}
struct HUST_dir_record root_dir_c;
const char* cur_dir = ".";
const char* parent_dir = "..";
memcpy(root_dir_c.filename, cur_dir, strlen(cur_dir) + 1);
root_dir_c.inode_no = HUST_ROOT_INODE_NUM;
struct HUST_dir_record root_dir_p;
memcpy(root_dir_p.filename, parent_dir, strlen(parent_dir) + 1);
root_dir_p.inode_no = HUST_ROOT_INODE_NUM;
struct HUST_dir_record file_record;
const char* onefile = "file";
memcpy(file_record.filename, onefile, strlen(onefile) + 1);
file_record.inode_no = 1;
off_t current_off = lseek(fd, 0L, SEEK_CUR);
printf("Current seek is %lu and rootdir at %lu\n", current_off
, super_block.data_block_number*HUST_BLOCKSIZE);
if(-1 == lseek(fd, super_block.data_block_number*HUST_BLOCKSIZE, SEEK_SET)) {
perror("lseek error\n");
return -1;
}
ret = write(fd, &root_dir_c, sizeof(root_dir_c));
ret = write(fd, &root_dir_p, sizeof(root_dir_p));
ret = write(fd, &file_record, sizeof(file_record));
if (ret != sizeof(root_dir_c)) {
perror("Write error!\n");
return -1;
}
printf("Create root dir successfully!\n");
return 0;
}
static int write_dummy(int fd)
{
char dummy[HUST_BLOCKSIZE] = {0};
ssize_t res = write(fd, dummy, HUST_BLOCKSIZE);
if (res != HUST_BLOCKSIZE) {
perror("write_dummy error!");
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
int fd;
ssize_t ret;
if(argc != 2) {
printf("Usage: mkfs <device>\n");
return -1;
}
fd = open(argv[1]
, O_RDWR);
if (fd == -1) {
perror("Error opening the device");
return -1;
}
ret = 1;
init_disk(fd, argv[1]);
write_dummy(fd);
write_sb(fd);
write_bmap(fd);
write_imap(fd);
write_itable(fd);
// write_root_dir(fd);
// write_welcome_file(fd);
close(fd);
return ret;
}