forked from sysprog21/simplefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkfs.c
318 lines (275 loc) · 8.4 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
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <unistd.h>
#include "simplefs.h"
struct superblock {
struct simplefs_sb_info info;
char padding[4064]; /* Padding to match block size */
};
/* Returns ceil(a/b) */
static inline uint32_t idiv_ceil(uint32_t a, uint32_t b)
{
uint32_t ret = a / b;
if (a % b)
return ret + 1;
return ret;
}
static struct superblock *write_superblock(int fd, struct stat *fstats)
{
struct superblock *sb = malloc(sizeof(struct superblock));
if (!sb)
return NULL;
uint32_t nr_blocks = fstats->st_size / SIMPLEFS_BLOCK_SIZE;
uint32_t nr_inodes = nr_blocks;
uint32_t mod = nr_inodes % SIMPLEFS_INODES_PER_BLOCK;
if (mod)
nr_inodes += SIMPLEFS_INODES_PER_BLOCK - mod;
uint32_t nr_istore_blocks = idiv_ceil(nr_inodes, SIMPLEFS_INODES_PER_BLOCK);
uint32_t nr_ifree_blocks = idiv_ceil(nr_inodes, SIMPLEFS_BLOCK_SIZE * 8);
uint32_t nr_bfree_blocks = idiv_ceil(nr_blocks, SIMPLEFS_BLOCK_SIZE * 8);
uint32_t nr_data_blocks =
nr_blocks - 1 - nr_istore_blocks - nr_ifree_blocks - nr_bfree_blocks;
memset(sb, 0, sizeof(struct superblock));
sb->info = (struct simplefs_sb_info){
.magic = htole32(SIMPLEFS_MAGIC),
.nr_blocks = htole32(nr_blocks),
.nr_inodes = htole32(nr_inodes),
.nr_istore_blocks = htole32(nr_istore_blocks),
.nr_ifree_blocks = htole32(nr_ifree_blocks),
.nr_bfree_blocks = htole32(nr_bfree_blocks),
.nr_free_inodes = htole32(nr_inodes - 1),
.nr_free_blocks = htole32(nr_data_blocks - 1),
};
int ret = write(fd, sb, sizeof(struct superblock));
if (ret != sizeof(struct superblock)) {
free(sb);
return NULL;
}
printf(
"Superblock: (%ld)\n"
"\tmagic=%#x\n"
"\tnr_blocks=%u\n"
"\tnr_inodes=%u (istore=%u blocks)\n"
"\tnr_ifree_blocks=%u\n"
"\tnr_bfree_blocks=%u\n"
"\tnr_free_inodes=%u\n"
"\tnr_free_blocks=%u\n",
sizeof(struct superblock), sb->info.magic, sb->info.nr_blocks,
sb->info.nr_inodes, sb->info.nr_istore_blocks, sb->info.nr_ifree_blocks,
sb->info.nr_bfree_blocks, sb->info.nr_free_inodes,
sb->info.nr_free_blocks);
return sb;
}
static int write_inode_store(int fd, struct superblock *sb)
{
/* Allocate a zeroed block for inode store */
char *block = malloc(SIMPLEFS_BLOCK_SIZE);
if (!block)
return -1;
memset(block, 0, SIMPLEFS_BLOCK_SIZE);
/* Root inode (inode 0) */
struct simplefs_inode *inode = (struct simplefs_inode *) block;
uint32_t first_data_block = 1 + le32toh(sb->info.nr_bfree_blocks) +
le32toh(sb->info.nr_ifree_blocks) +
le32toh(sb->info.nr_istore_blocks);
inode->i_mode = htole32(S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR |
S_IWGRP | S_IXUSR | S_IXGRP | S_IXOTH);
inode->i_uid = 0;
inode->i_gid = 0;
inode->i_size = htole32(SIMPLEFS_BLOCK_SIZE);
inode->i_ctime = inode->i_atime = inode->i_mtime = htole32(0);
inode->i_blocks = htole32(1);
inode->i_nlink = htole32(2);
inode->dir_block = htole32(first_data_block);
int ret = write(fd, block, SIMPLEFS_BLOCK_SIZE);
if (ret != SIMPLEFS_BLOCK_SIZE) {
ret = -1;
goto end;
}
/* Reset inode store blocks to zero */
memset(block, 0, SIMPLEFS_BLOCK_SIZE);
uint32_t i;
for (i = 1; i < sb->info.nr_istore_blocks; i++) {
ret = write(fd, block, SIMPLEFS_BLOCK_SIZE);
if (ret != SIMPLEFS_BLOCK_SIZE) {
ret = -1;
goto end;
}
}
ret = 0;
printf(
"Inode store: wrote %d blocks\n"
"\tinode size = %ld B\n",
i, sizeof(struct simplefs_inode));
end:
free(block);
return ret;
}
static int write_ifree_blocks(int fd, struct superblock *sb)
{
char *block = malloc(SIMPLEFS_BLOCK_SIZE);
if (!block)
return -1;
uint64_t *ifree = (uint64_t *) block;
/* Set all bits to 1 */
memset(ifree, 0xff, SIMPLEFS_BLOCK_SIZE);
/* First ifree block, containing first used inode */
ifree[0] = htole64(0xfffffffffffffffe);
int ret = write(fd, ifree, SIMPLEFS_BLOCK_SIZE);
if (ret != SIMPLEFS_BLOCK_SIZE) {
ret = -1;
goto end;
}
/* All ifree blocks except the one containing 2 first inodes */
ifree[0] = 0xffffffffffffffff;
uint32_t i;
for (i = 1; i < le32toh(sb->info.nr_ifree_blocks); i++) {
ret = write(fd, ifree, SIMPLEFS_BLOCK_SIZE);
if (ret != SIMPLEFS_BLOCK_SIZE) {
ret = -1;
goto end;
}
}
ret = 0;
printf("Ifree blocks: wrote %d blocks\n", i);
end:
free(block);
return ret;
}
static int write_bfree_blocks(int fd, struct superblock *sb)
{
uint32_t nr_used = le32toh(sb->info.nr_istore_blocks) +
le32toh(sb->info.nr_ifree_blocks) +
le32toh(sb->info.nr_bfree_blocks) + 2;
char *block = malloc(SIMPLEFS_BLOCK_SIZE);
if (!block)
return -1;
uint64_t *bfree = (uint64_t *) block;
/*
* First blocks (incl. sb + istore + ifree + bfree + 1 used block)
* we suppose it won't go further than the first block
*/
memset(bfree, 0xff, SIMPLEFS_BLOCK_SIZE);
uint32_t i = 0;
while (nr_used) {
uint64_t line = 0xffffffffffffffff;
for (uint64_t mask = 0x1; mask; mask <<= 1) {
line &= ~mask;
nr_used--;
if (!nr_used)
break;
}
bfree[i] = htole64(line);
i++;
}
int ret = write(fd, bfree, SIMPLEFS_BLOCK_SIZE);
if (ret != SIMPLEFS_BLOCK_SIZE) {
ret = -1;
goto end;
}
/* other blocks */
memset(bfree, 0xff, SIMPLEFS_BLOCK_SIZE);
for (i = 1; i < le32toh(sb->info.nr_bfree_blocks); i++) {
ret = write(fd, bfree, SIMPLEFS_BLOCK_SIZE);
if (ret != SIMPLEFS_BLOCK_SIZE) {
ret = -1;
goto end;
}
}
ret = 0;
printf("Bfree blocks: wrote %d blocks\n", i);
end:
free(block);
return ret;
}
static int write_data_blocks(int fd, struct superblock *sb)
{
/* FIXME: unimplemented */
return 0;
}
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s disk\n", argv[0]);
return EXIT_FAILURE;
}
/* Open disk image */
int fd = open(argv[1], O_RDWR);
if (fd == -1) {
perror("open():");
return EXIT_FAILURE;
}
/* Get image size */
struct stat stat_buf;
int ret = fstat(fd, &stat_buf);
if (ret) {
perror("fstat():");
ret = EXIT_FAILURE;
goto fclose;
}
/* Get block device size */
if ((stat_buf.st_mode & S_IFMT) == S_IFBLK) {
long int blk_size = 0;
ret = ioctl(fd, BLKGETSIZE64, &blk_size);
if (ret != 0) {
perror("BLKGETSIZE64:");
ret = EXIT_FAILURE;
goto fclose;
}
stat_buf.st_size = blk_size;
}
/* Check if image is large enough */
long int min_size = 100 * SIMPLEFS_BLOCK_SIZE;
if (stat_buf.st_size <= min_size) {
fprintf(stderr, "File is not large enough (size=%ld, min size=%ld)\n",
stat_buf.st_size, min_size);
ret = EXIT_FAILURE;
goto fclose;
}
/* Write superblock (block 0) */
struct superblock *sb = write_superblock(fd, &stat_buf);
if (!sb) {
perror("write_superblock():");
ret = EXIT_FAILURE;
goto fclose;
}
/* Write inode store blocks (from block 1) */
ret = write_inode_store(fd, sb);
if (ret) {
perror("write_inode_store():");
ret = EXIT_FAILURE;
goto free_sb;
}
/* Write inode free bitmap blocks */
ret = write_ifree_blocks(fd, sb);
if (ret) {
perror("write_ifree_blocks()");
ret = EXIT_FAILURE;
goto free_sb;
}
/* Write block free bitmap blocks */
ret = write_bfree_blocks(fd, sb);
if (ret) {
perror("write_bfree_blocks()");
ret = EXIT_FAILURE;
goto free_sb;
}
/* Write data blocks */
ret = write_data_blocks(fd, sb);
if (ret) {
perror("write_data_blocks():");
ret = EXIT_FAILURE;
goto free_sb;
}
free_sb:
free(sb);
fclose:
close(fd);
return ret;
}