-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode_manager.cc
425 lines (379 loc) · 11.1 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
#include "inode_manager.h"
#include <time.h>
#include <assert.h>
#include <iostream>
#include <string>
#define MIN(a, b) ((a)<(b) ? (a) : (b))
#define MAX(a, b) ((a)>(b) ? (a) : (b))
//#define DEBUG
// disk layer -----------------------------------------
disk::disk() {
// 初始化disk 全部置0
bzero(blocks, sizeof(blocks));
}
void
disk::read_block(blockid_t id, char *buf) {
//#ifdef DEBUG
// printf("%s blockID\n", blocks[id]);
//#endif
#ifdef DEBUG
printf("\tim read_block, blockid %d\n", id);
#endif
memcpy(buf, blocks[id], BLOCK_SIZE);
}
void
disk::write_block(blockid_t id, const char *buf) {
#ifdef DEBUG
printf("\tim write block, blockid %d\n", id);
#endif
bzero(blocks[id], BLOCK_SIZE);
memcpy(blocks[id], buf, BLOCK_SIZE);
}
// block layer -----------------------------------------
// 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.
*/
// find a free block
if(!lastAllocBlock) {
lastAllocBlock = DATABLOCK;
}
blockid_t blockid = this->lastAllocBlock;
#ifdef DEBUG
printf("\tim alloc_block, block_id %d\n", blockid);
#endif
for (int i = 0; i < BLOCK_NUM - DATABLOCK; ++i) {
if (this->using_blocks[blockid] == 0) {
using_blocks[blockid] = 1;
lastAllocBlock = blockid + 1;
return blockid;
}
blockid = (blockid + 1) > BLOCK_NUM ? (DATABLOCK) : blockid + 1;
}
// no free block
return -1;
}
void
block_manager::free_block(uint32_t id) {
#ifdef DEBUG
printf("\tim free_block, blockid %d\n", id);
#endif
/*
* your code goes here.
* note: you should unmark the corresponding bit in the block bitmap when free.
*/
using_blocks[id] = 0;
}
// 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
for (int i = 0; i < DATABLOCK - 1; ++i) {
this->using_blocks[i] = 1;
}
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);
}
bool
block_manager::is_free_block(blockid_t blockid) {
return !using_blocks[blockid];
}
// inode layer -----------------------------------------
inode_manager::inode_manager() {
bm = new block_manager();
uint32_t root_dir = alloc_inode(extent_protocol::T_DIR);
if (root_dir != 1) {
printf("\tim: error! alloc first inode %d, should be 1\n", root_dir);
exit(0);
}
}
/* 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().
*/
#ifdef DEBUG
printf("\tim alloc inode, type: %d\n", type);
#endif
int inum = last_fit_inum;
inode_t *inode;
int count = 0;
while (count < INODE_NUM) {
inode = get_inode(inum);
if (inode != NULL) {
inum = (inum + 1) % INODE_NUM;
free(inode);
continue;
} else {
inode_t *ino;
ino = (inode_t *) malloc(sizeof(inode_t));
bzero(ino, sizeof(inode_t));
ino->type = type;
ino->size = 0;
unsigned int t = time(NULL);
ino->atime = t;
ino->mtime = t;
ino->ctime = t;
put_inode(inum, ino);
free(ino);
last_fit_inum = inum;
return inum;
}
}
// if count == INODE_NUM then alloc failed
assert(count != INODE_NUM);
return 1;
}
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.
*/
#ifdef DEBUG
printf("\tim free_inode, inum: %d\n", inum);
#endif
inode_t *inode = get_inode(inum);
if (!inode) {
return;
}
inode->type = 0;
inode->size = 0;
uint32_t t = time(NULL);
inode->atime = t;
inode->mtime = t;
put_inode(inum, inode);
free(inode);
return;
}
/* Return an inode structure by inum, NULL otherwise.
* Caller should release the memory. */
struct inode *
inode_manager::get_inode(uint32_t inum) {
inode_t *ino;
/*
* your code goes here.
*/
#ifdef DEBUG
printf("\tim get_inode, inum: %d\n", inum);
#endif
// boundary check
if (inum < 0 || inum >= INODE_NUM) {
return NULL;
}
// block is the smallest unit can be read from inode File System
char buf[BLOCK_SIZE];
this->bm->read_block(IBLOCK(inum, this->bm->sb.nblocks), buf);
inode_t *inode = (inode_t *) buf + inum % IPB;
ino = (inode_t *) malloc(sizeof(inode_t));
*ino = *inode;
return ino->type == 0 ? NULL : ino;
}
void
inode_manager::put_inode(uint32_t inum, struct inode *ino) {
#ifdef DEBUG
printf("\tim put_inode, inum: %d\n", inum);
#endif
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);
}
/**
* @param inode : 需要分配新的 block 的 inode
* @param index : inode 新增加的 block 对应的序号
*/
void
inode_manager::alloc_block_by_index(inode_t *inode, uint32_t index) {
blockid_t blockid = this->bm->alloc_block();
if (blockid != -1) {
if (index < NDIRECT) {
inode->blocks[index] = blockid;
return;
} else {
if (inode->blocks[NDIRECT] == 0) {
inode->blocks[NDIRECT] = this->bm->alloc_block();
}
char blockid_buf[BLOCK_SIZE];
this->bm->read_block(inode->blocks[NDIRECT], blockid_buf);
((blockid_t *) blockid_buf)[index - NDIRECT] = this->bm->alloc_block();
this->bm->write_block(inode->blocks[NDIRECT], blockid_buf);
}
}
}
/**
* @param inode : 需要释放 block 的 inode
* @param index : inode 需要释放的 block 对应的序号
*/
void
inode_manager::free_block_by_index(inode_t *inode, uint32_t index) {
if (index < NDIRECT) {
this->bm->free_block(inode->blocks[index]);
return;
}
this->bm->free_block(get_blockid_by_index(inode, index));
}
blockid_t
inode_manager::get_blockid_by_index(inode_t *inode, uint32_t index) {
if (index < NDIRECT) {
return inode->blocks[index];
} else {
if (inode->blocks[NDIRECT] == 0) {
return 0;
}
char buf[BLOCK_SIZE];
this->bm->read_block(inode->blocks[NDIRECT], buf);
return ((blockid_t *) buf)[index - NDIRECT];
}
}
/* 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
*/
#ifdef DEBUG
printf("\tim read_file, inum: %d\n", inum);
#endif
inode_t *inode = this->get_inode(inum);
if (!inode)
return;
char buf[BLOCK_SIZE];
*size = inode->size;
*buf_out = (char *) malloc(*size);
int block_count = inode->size == 0 ? 0: (inode->size - 1) / BLOCK_SIZE + 1;
for (int i = 0; i < block_count; ++i) {
this->bm->read_block(this->get_blockid_by_index(inode, i), buf);
if (i == block_count - 1 && (inode->size) % BLOCK_SIZE) { // 对最后一个特殊处理
memcpy(*buf_out + BLOCK_SIZE * i, buf, (inode->size) % BLOCK_SIZE);
} else {
memcpy(*buf_out + BLOCK_SIZE * i, buf, BLOCK_SIZE);
}
}
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
*/
#ifdef DEBUG
printf("\tim write_file inum: %d\n", inum);
#endif
inode_t *inode = this->get_inode(inum);
if (!inode)
return;
int block_size_before = inode->size == 0 ? 0 : ((inode->size - 1) / BLOCK_SIZE + 1);
int block_size_after = size == 0 ? 0 : ((size - 1) / BLOCK_SIZE + 1);
int min_block = MIN(block_size_before, block_size_after);
int max_block = MAX(block_size_after, block_size_before);
#ifdef DEBUG
printf("bef: %d, after: %d\n", block_size_before, block_size_after);
#endif
if (block_size_after > block_size_before) {
// 需要分配新的block
for (int i = min_block; i < max_block; ++i) {
this->alloc_block_by_index(inode, i);
}
} else {
// 需要 free 多出的block
for (int i = min_block; i < max_block; ++i) {
this->free_block_by_index(inode, i);
}
}
char temp[BLOCK_SIZE];
for (int i = 0; i < block_size_after; ++i) {
if (i == block_size_after - 1 && size % BLOCK_SIZE) {
memcpy(temp, buf + i * BLOCK_SIZE, size % BLOCK_SIZE);
//#ifdef DEBUG
// printf("%d ", this->get_blockid_by_index(inode, i));
//#endif
this->bm->write_block(this->get_blockid_by_index(inode, i), temp);
} else {
//#ifdef DEBUG
// printf("%d ", this->get_blockid_by_index(inode, i));
//#endif
this->bm->write_block(this->get_blockid_by_index(inode, i), buf + i * BLOCK_SIZE);
}
}
inode->size = size;
uint32_t t = (unsigned int) time(NULL);
inode->atime = t;
inode->ctime = t;
inode->mtime = t;
this->put_inode(inum, inode);
free(inode);
}
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
*/
#ifdef DEBUG
printf("\tim get_attr inum: %d\n", inum);
#endif
inode_t *inode = this->get_inode(inum);
if(!inode) {
return;
}
a.type = inode->type;
a.ctime = inode->ctime;
a.mtime = inode->mtime;
a.atime = inode->atime;
a.size = inode->size;
}
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
*/
#ifdef DEBUG
printf("\tim remove_file inum: %d\n", inum);
#endif
inode_t *inode = get_inode(inum);
if (!inode) {
printf("this inode is not allocated yet\n");
return;
}
int count_of_block = inode->size == 0 ? 0 : (inode->size - 1) / BLOCK_SIZE + 1;
for (int i = 0; i < count_of_block; ++i) {
free_block_by_index(inode, i);
}
this->bm->free_block(inode->blocks[NDIRECT]);
free(inode);
free_inode(inum);
return;
}