-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode_manager.cc
463 lines (433 loc) · 11.7 KB
/
inode_manager.cc
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
#include "inode_manager.h"
#include <cstring>
#include <algorithm>
#include <exception>
#include <memory>
#include <cstdlib>
#include <time.h>
#include <assert.h>
// disk layer -----------------------------------------
disk::disk()
{
bzero(blocks, sizeof(blocks));
}
void
disk::read_block(blockid_t id, char *buf)
{
if(id >= BLOCK_NUM) {
return;
}
memcpy(buf, blocks[id], BLOCK_SIZE);
}
void
disk::write_block(blockid_t id, const char *buf)
{
// judge the range
if(id >= BLOCK_NUM) {
return;
}
// TODO: it is not safe
memcpy(blocks[id], buf, BLOCK_SIZE);
}
// block layer -----------------------------------------
// judge whether it is a valid and using block
bool block_manager::is_valid_block(uint32_t id) {
if(id >= BLOCK_NUM) {
return false;
}
return using_blocks[id] == 1;
}
// Allocate a free disk block.
blockid_t
block_manager::alloc_block()
{
/*
* your code goes here.
* note: you should mark the corresponding bit in block bitmap when alloc.
* you need to think about which block you can start to be allocated.
*/
uint32_t last_inode_block = IBLOCK(INODE_NUM, sb.nblocks);
for(int i = last_inode_block + 1; i < BLOCK_NUM; ++i) {
if(!using_blocks.count(i) || using_blocks[i] == 0 ) {
using_blocks[i] = 1;
return i;
}
}
// an error occur
return 0;
}
void
block_manager::free_block(uint32_t id)
{
/*
* your code goes here.
* note: you should unmark the corresponding bit in the block bitmap when free.
*/
if(id >= BLOCK_NUM)
return;
char buf[BLOCK_SIZE] = {0};
write_block(id, buf);
assert(using_blocks[id] == 1);
using_blocks[id] = 0;
return;
}
// The layout of disk should be like this:
// |<-sb->|<-free block bitmap->|<-inode table->|<-data->|
block_manager::block_manager()
{
d = new disk();
// format the disk
sb.size = BLOCK_SIZE * BLOCK_NUM;
sb.nblocks = BLOCK_NUM;
sb.ninodes = INODE_NUM;
}
void
block_manager::read_block(uint32_t id, char *buf)
{
d->read_block(id, buf);
}
void
block_manager::write_block(uint32_t id, const char *buf)
{
d->write_block(id, buf);
}
// inode layer -----------------------------------------
inode_manager::inode_manager()
{
bm = new block_manager();
uint32_t root_dir = alloc_inode(extent_protocol::T_DIR);
_last_alloced = 0;
if (root_dir != 1) {
exit(0);
}
}
/* get the indirect block which point to other blocks */
uint32_t* inode_manager::get_indirect_blocks(struct inode *node, int &block_count) {
char *buf = new char[BLOCK_SIZE];
bm->read_block(node->blocks[NDIRECT], buf);
block_count = strlen(buf) / sizeof(uint32_t);
uint32_t *blocks = (uint32_t *)buf;
return blocks;
}
void inode_manager::free_indirect_blocks(struct inode *node, int begin_block) {
if(node == nullptr) {
return;
}
int block_count = node->size >= BLOCK_SIZE * NDIRECT ? (node->size - 1) / BLOCK_SIZE + 1 - NDIRECT : 0;
char *blocks = new char[BLOCK_SIZE];
bm->read_block(node->blocks[NDIRECT], blocks);
for(int i = begin_block; i < block_count; ++i) {
bm->free_block(*((uint32_t *)blocks + i));
*((uint32_t *)blocks + i) = 0;
}
if(begin_block == 0) {
bm->free_block(node->blocks[NDIRECT]);
node->blocks[NDIRECT] = 0;
} else {
bm->write_block(node->blocks[NDIRECT], blocks);
}
delete []blocks;
}
/* Create a new file.
* Return its inum. */
uint32_t
inode_manager::alloc_inode(uint32_t type)
{
/*
* your code goes here.
* note: the normal inode block should begin from the 2nd inode block.
* the 1st is used for root_dir, see inode_manager::inode_manager().
*/
// create the new block
struct inode *new_node = new inode();
bzero(new_node, sizeof(struct inode));
new_node->atime = new_node->ctime = new_node->mtime = time(NULL);
new_node->type = type;
memset(new_node->blocks, 0, sizeof(blockid_t)*(NDIRECT+1));
for(int i = 1; i <= INODE_NUM; ++i) {
uint32_t to_alloc = i + _last_alloced >= INODE_NUM ? (i + _last_alloced) % INODE_NUM + 1 : i + _last_alloced;
if(get_inode(to_alloc) == nullptr) {
if(to_alloc == 1) {
printf("create a root inode!\n");
}
put_inode(to_alloc, new_node);
_last_alloced = to_alloc;
delete new_node;
return to_alloc;
}
}
// an error occur
delete new_node;
return 0;
}
void
inode_manager::free_inode(uint32_t inum)
{
/*
* your code goes here.
* note: you need to check if the inode is already a freed one;
* if not, clear it, and remember to write back to disk.
*/
// remember to write back to disk.
// find the inode
if(inum > INODE_NUM) {
return;
}
struct inode *node = get_inode(inum);
// the inode is already a freed one
if(node == nullptr) {
return;
}
// clear it, and write back to disk.
char buf[BLOCK_SIZE] = {0};
bm->write_block(IBLOCK(inum, bm->sb.nblocks), buf);
delete node;
return;
}
/* Return an inode structure by inum, NULL otherwise.
* Caller should release the memory. */
struct inode*
inode_manager::get_inode(uint32_t inum)
{
if(inum > INODE_NUM)
return nullptr;
struct inode *ino, *ino_disk;
/*
* your code goes here.
*/
char buf[BLOCK_SIZE] = {0};
bm->read_block(IBLOCK(inum, bm->sb.nblocks), buf);
ino = (struct inode*)buf + inum % IPB;
if(ino->type == 0) {
return nullptr;
}
ino_disk = new inode_t();
*ino_disk = *ino;
return ino_disk;
}
void
inode_manager::put_inode(uint32_t inum, struct inode *ino)
{
char buf[BLOCK_SIZE];
struct inode *ino_disk;
if (ino == NULL)
return;
bm->read_block(IBLOCK(inum, bm->sb.nblocks), buf);
ino_disk = (struct inode*)buf + inum%IPB;
*ino_disk = *ino;
bm->write_block(IBLOCK(inum, bm->sb.nblocks), buf);
}
#define MIN(a,b) ((a)<(b) ? (a) : (b))
/* Get all the data of a file by inum.
* Return alloced data, should be freed by caller. */
void
inode_manager::read_file(uint32_t inum, char **buf_out, int *size)
{
/*
* your code goes here.
* note: read blocks related to inode number inum,
* and copy them to buf_out
*/
if(inum > INODE_NUM) {
*size = 0;
return;
}
struct inode *node = get_inode(inum);
// varify the node
if(node == nullptr) {
return;
}
//!!! first alloc space for buf_out
int block_count = node->size / BLOCK_SIZE;
if(node->size % BLOCK_SIZE != 0 || block_count == 0) {
++block_count;
}
int remain_size = node->size % BLOCK_SIZE;
*buf_out = (char *)malloc(block_count * BLOCK_SIZE);
*size = node->size;
int i = 0;
for(;i < NDIRECT; ++i) {
uint32_t block_id;
block_id = node->blocks[i];
if(bm->is_valid_block(block_id)) {
char buf[BLOCK_SIZE] = {0};
bm->read_block(block_id, buf);
// add to buf_out
if(i == (block_count - 1) && remain_size) {
memcpy(*buf_out + i * BLOCK_SIZE, buf, remain_size);
break;
}
memcpy(*buf_out + i * BLOCK_SIZE, buf, BLOCK_SIZE);
}
}
if(i == NDIRECT && bm->is_valid_block(node->blocks[NDIRECT])) {
char indirect_blocks[BLOCK_SIZE] = {0};
bm->read_block(node->blocks[NDIRECT], indirect_blocks);
for(i = 0; i < block_count - NDIRECT; ++i) {
if(bm->is_valid_block(*((uint32_t *)indirect_blocks + i))) {
char indirect_buf[BLOCK_SIZE] = {0};
bm->read_block(*((uint32_t *)indirect_blocks + i), indirect_buf);
if(i + NDIRECT == block_count - 1 && remain_size) {
memcpy(*buf_out + (i + NDIRECT) * BLOCK_SIZE, indirect_buf, remain_size);
break;
}
memcpy(*buf_out + (i + NDIRECT) * BLOCK_SIZE, indirect_buf, BLOCK_SIZE);
}
}
}
node->atime = time(NULL);
put_inode(inum, node);
return;
}
/* alloc/free blocks if needed */
void
inode_manager::write_file(uint32_t inum, const char *buf, int size)
{
/*
* your code goes here.
* note: write buf to blocks of inode inum.
* you need to consider the situation when the size of buf
* is larger or smaller than the size of original inode
*/
struct inode *node = get_inode(inum);
if(node == nullptr) {
return;
}
int pre_block_num = node->size == 0 ? 1 : (node->size - 1) / BLOCK_SIZE + 1;
// !!! there maybe '\0' in buf !!!
char arr_for_remain[BLOCK_SIZE] = {0};
int i = 0, pos = 0;
// handle direct blocks
while(pos < size && i < NDIRECT) {
if(!bm->is_valid_block(node->blocks[i])) {
// has to alloc new
uint32_t new_block_id = bm->alloc_block();
node->blocks[i] = new_block_id;
}
if(pos + BLOCK_SIZE > size) {
memcpy(arr_for_remain, buf + pos, size - pos);
bm->write_block(node->blocks[i], arr_for_remain);
}
bm->write_block(node->blocks[i], buf + pos);
pos += BLOCK_SIZE;
++i;
}
// handle indirect blocks
if(pos < size && i == NDIRECT) {
char indirect_blocks[BLOCK_SIZE] = {0};
if(bm->is_valid_block(node->blocks[NDIRECT])) {
bm->read_block(node->blocks[NDIRECT], indirect_blocks);
} else {
uint32_t new_block_id = bm->alloc_block();
node->blocks[NDIRECT] = new_block_id;
}
i = 0;
int indirect_block_count = BLOCK_SIZE / sizeof(uint32_t);
while(pos < size && i < indirect_block_count) {
if(!bm->is_valid_block(*((uint32_t *)indirect_blocks + i))) {
// has alloc new
uint32_t new_block_id = bm->alloc_block();
*((uint32_t *)indirect_blocks + i) = new_block_id;
}
if(pos + BLOCK_SIZE > size) {
memcpy(arr_for_remain, buf + pos, size - pos);
bm->write_block(*((uint32_t *)indirect_blocks + i), arr_for_remain);
break;
}
bm->write_block(*((uint32_t *)indirect_blocks + i), buf + pos);
pos += BLOCK_SIZE;
++i;
}
// change the indirect block
bm->write_block(node->blocks[NDIRECT], indirect_blocks);
}
// free block if needed
int used_block_num = (size - 1) / BLOCK_SIZE + 1;
i = used_block_num;
while(i < NDIRECT && i < pre_block_num) {
if(bm->is_valid_block(node->blocks[i])) {
bm->free_block(node->blocks[i]);
node->blocks[i] = 0;
++i;
} else {
break;
}
}
// free the indirect block
if(i >= NDIRECT && pre_block_num > NDIRECT) {
free_indirect_blocks(node, i - NDIRECT);
if(i - NDIRECT == 0) {
node->blocks[NDIRECT] = 0;
}
}
// change the inode information and save
node->size = size;
node->atime = time(NULL);
node->ctime = time(NULL);
node->mtime = time(NULL);
put_inode(inum, node);
return;
}
void
inode_manager::get_attr(uint32_t inum, extent_protocol::attr &a)
{
/*
* your code goes here.
* note: get the attributes of inode inum.
* you can refer to "struct attr" in extent_protocol.h
*/
if(inum > INODE_NUM)
return;
struct inode* node = get_inode(inum);
if(node == nullptr)
return;
a.type = node->type;
a.atime = node->atime;
a.ctime = node->ctime;
a.mtime = node->mtime;
a.size = node->size;
return;
}
void
inode_manager::remove_file(uint32_t inum)
{
/*
* your code goes here
* note: you need to consider about both the data block and inode of the file
*/
// find the inode
if(inum > INODE_NUM)
return;
struct inode *node = get_inode(inum);
if(node == nullptr) {
return;
}
// free the data block
unsigned int free_size = 0;
int i = 0;
for(i = 0; i < NDIRECT && free_size < node->size; ++i) {
if(bm->is_valid_block(node->blocks[i])) {
bm->free_block(node->blocks[i]);
free_size += BLOCK_SIZE;
} else {
break;
}
}
if(free_size < node->size) {
// TODO: free indirect block
int indirect_block_num = 0;
uint32_t* indirect_blocks = get_indirect_blocks(node, indirect_block_num);
for(i = 0; i < indirect_block_num && free_size < node->size; ++i) {
if(bm->is_valid_block(indirect_blocks[i])) {
bm->free_block(indirect_blocks[i]);
free_size += BLOCK_SIZE;
} else {
break;
}
}
bm->free_block(node->blocks[NDIRECT]);
}
free_inode(inum);
delete node;
return;
}