-
Notifications
You must be signed in to change notification settings - Fork 2
/
super.c
executable file
·305 lines (269 loc) · 9.06 KB
/
super.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
#include "testfs.h"
#include "super.h"
#include "inode.h"
#include "dir.h"
#include "block.h"
#include "bitmap.h"
#include "csum.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct super_block *
testfs_make_super_block(char *storage)
{
struct super_block *sb = calloc(1, sizeof(struct super_block));
if (!sb) {
EXIT("malloc");
}
sb->storage = storage;
sb->sb.inode_freemap_start = SUPER_BLOCK_SIZE;
sb->sb.block_freemap_start = sb->sb.inode_freemap_start +
INODE_FREEMAP_SIZE;
sb->sb.csum_table_start = sb->sb.block_freemap_start +
BLOCK_FREEMAP_SIZE;
sb->sb.inode_blocks_start = sb->sb.csum_table_start +
CSUM_TABLE_SIZE;
sb->sb.data_blocks_start = sb->sb.inode_blocks_start + NR_INODE_BLOCKS;
sb->sb.modification_time = 0;
testfs_write_super_block(sb);
sb->inode_hash_table = inode_hash_init();
return sb;
}
void
testfs_make_inode_freemap(struct super_block *sb)
{
zero_blocks(sb, sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE);
}
void
testfs_make_block_freemap(struct super_block *sb)
{
zero_blocks(sb, sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE);
}
void
testfs_make_csum_table(struct super_block *sb)
{
/* number of data blocks cannot exceed size of checksum table */
assert(MAX_NR_CSUMS > NR_DATA_BLOCKS);
zero_blocks(sb, sb->sb.csum_table_start, CSUM_TABLE_SIZE);
}
void
testfs_make_inode_blocks(struct super_block *sb)
{
/* dinodes should not span blocks */
assert((BLOCK_SIZE % sizeof(struct dinode)) == 0);
zero_blocks(sb, sb->sb.inode_blocks_start, NR_INODE_BLOCKS);
}
/* returns negative value on error */
int
testfs_init_super_block(const char* storage, int corrupt, struct super_block **sbp)
{
struct super_block *sb = NULL;
char block[BLOCK_SIZE];
int ret;
*sbp = NULL;
sb = malloc(sizeof(struct super_block));
if (!sb) {
return -ENOMEM;
}
sb->storage = (char*)storage;
read_blocks(sb, block, 0, 1);
memcpy(&sb->sb, block, sizeof(struct dsuper_block));
ret = bitmap_create(BLOCK_SIZE * INODE_FREEMAP_SIZE * BITS_PER_WORD,
&sb->inode_freemap);
if (ret < 0)
return ret;
read_blocks(sb, bitmap_getdata(sb->inode_freemap),
sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE);
ret = bitmap_create(BLOCK_SIZE * BLOCK_FREEMAP_SIZE * BITS_PER_WORD,
&sb->block_freemap);
if (ret < 0)
return ret;
read_blocks(sb, bitmap_getdata(sb->block_freemap),
sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE);
sb->csum_table = calloc(CSUM_TABLE_SIZE, BLOCK_SIZE);
if ( !sb->csum_table )
return -ENOMEM;
read_blocks(sb, (char *)sb->csum_table, sb->sb.csum_table_start,
CSUM_TABLE_SIZE);
sb->tx_in_progress = TX_NONE;
sb->inode_hash_table = inode_hash_init();
*sbp = sb;
return 0;
}
void
testfs_write_super_block(struct super_block *sb)
{
char block[BLOCK_SIZE] = {0};
assert(sizeof(struct dsuper_block) <= BLOCK_SIZE);
memcpy(block, &sb->sb, sizeof(struct dsuper_block));
write_blocks(sb, block, 0, 1);
}
void
testfs_close_super_block(struct super_block *sb)
{
testfs_tx_start(sb, TX_UMOUNT);
testfs_write_super_block(sb);
inode_hash_destroy(sb->inode_hash_table);
sb->inode_hash_table = NULL;
if (sb->inode_freemap) {
write_blocks(sb, bitmap_getdata(sb->inode_freemap),
sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE);
bitmap_destroy(sb->inode_freemap);
sb->inode_freemap = NULL;
}
if (sb->block_freemap) {
write_blocks(sb, bitmap_getdata(sb->block_freemap),
sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE);
bitmap_destroy(sb->block_freemap);
sb->block_freemap = NULL;
}
testfs_tx_commit(sb, TX_UMOUNT);
free(sb->csum_table);
free(sb);
}
static void
testfs_write_inode_freemap(struct super_block *sb, int inode_nr)
{
char *freemap;
int nr;
assert(sb->inode_freemap);
freemap = bitmap_getdata(sb->inode_freemap);
nr = inode_nr / (BLOCK_SIZE * BITS_PER_WORD);
write_blocks(sb, freemap + (nr * BLOCK_SIZE),
sb->sb.inode_freemap_start + nr, 1);
}
static void
testfs_write_block_freemap(struct super_block *sb, int block_nr)
{
char *freemap;
int nr;
assert(sb->block_freemap);
freemap = bitmap_getdata(sb->block_freemap);
nr = block_nr / (BLOCK_SIZE * BITS_PER_WORD);
write_blocks(sb, freemap + (nr * BLOCK_SIZE),
sb->sb.block_freemap_start + nr, 1);
}
/* return free block number or negative value */
static int
testfs_get_block_freemap(struct super_block *sb)
{
u_int32_t index;
int ret;
assert(sb->block_freemap);
ret = bitmap_alloc(sb->block_freemap, &index);
if (ret < 0)
return ret;
testfs_write_block_freemap(sb, index);
return index;
}
/* release allocated block */
static void
testfs_put_block_freemap(struct super_block *sb, int block_nr)
{
assert(sb->block_freemap);
bitmap_unmark(sb->block_freemap, block_nr);
testfs_write_block_freemap(sb, block_nr);
}
/* return free inode number or negative value */
int
testfs_get_inode_freemap(struct super_block *sb)
{
u_int32_t index;
int ret;
assert(sb->inode_freemap);
ret = bitmap_alloc(sb->inode_freemap, &index);
if (ret < 0)
return ret;
testfs_write_inode_freemap(sb, index);
return index;
}
/* release allocated inode */
void
testfs_put_inode_freemap(struct super_block *sb, int inode_nr)
{
assert(sb->inode_freemap);
bitmap_unmark(sb->inode_freemap, inode_nr);
testfs_write_inode_freemap(sb, inode_nr);
}
/* allocate a block and return its block number.
* returns negative value on error. */
int
testfs_alloc_block(struct super_block *sb, char *block)
{
int phy_block_nr;
phy_block_nr = testfs_get_block_freemap(sb);
if (phy_block_nr < 0)
return phy_block_nr;
bzero(block, BLOCK_SIZE);
return sb->sb.data_blocks_start + phy_block_nr;
}
/* free a block.
* returns negative value on error. */
int
testfs_free_block(struct super_block *sb, int block_nr)
{
zero_blocks(sb, block_nr, 1);
block_nr -= sb->sb.data_blocks_start;
assert(block_nr >= 0);
testfs_put_block_freemap(sb, block_nr);
return 0;
}
static int
testfs_checkfs(struct super_block *sb, struct bitmap *i_freemap,
struct bitmap *b_freemap, int inode_nr)
{
struct inode *in = testfs_get_inode(sb, inode_nr);
int size;
int size_roundup = ROUNDUP(testfs_inode_get_size(in), BLOCK_SIZE);
assert((testfs_inode_get_type(in) == I_FILE) ||
(testfs_inode_get_type(in) == I_DIR));
/* inode processing */
bitmap_mark(i_freemap, inode_nr);
if (testfs_inode_get_type(in) == I_DIR) {
int offset = 0;
struct tfs_dirent *d;
for (; (d = testfs_next_dirent(in, &offset)); free(d)) {
if ((d->d_inode_nr < 0) ||
(strcmp(D_NAME(d), ".") == 0) ||
(strcmp(D_NAME(d), "..") == 0))
continue;
testfs_checkfs(sb, i_freemap, b_freemap, d->d_inode_nr);
}
}
/* block processing */
size = testfs_check_inode(sb, b_freemap, in);
assert(size == size_roundup);
(void) size;
(void) size_roundup;
testfs_put_inode(in);
return 0;
}
int
cmd_checkfs(struct super_block *sb, struct context *c)
{
struct bitmap *i_freemap, *b_freemap;
int ret;
if (c->nargs != 1) {
return -EINVAL;
}
ret = bitmap_create(BLOCK_SIZE * INODE_FREEMAP_SIZE * BITS_PER_WORD,
&i_freemap);
if (ret < 0)
return ret;
ret = bitmap_create(BLOCK_SIZE * BLOCK_FREEMAP_SIZE * BITS_PER_WORD,
&b_freemap);
if (ret < 0)
return ret;
testfs_checkfs(sb, i_freemap, b_freemap, 0);
if (!bitmap_equal(sb->inode_freemap, i_freemap)) {
printf("inode freemap is not consistent\n");
}
if (!bitmap_equal(sb->block_freemap, b_freemap)) {
printf("block freemap is not consistent\n");
}
printf("nr of allocated inodes = %d\n",
bitmap_nr_allocated(sb->inode_freemap));
printf("nr of allocated blocks = %d\n",
bitmap_nr_allocated(sb->block_freemap));
return 0;
}