-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode_manager.cc
372 lines (307 loc) · 7.82 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
#include "inode_manager.h"
// disk layer -----------------------------------------
disk::disk()
{
bzero(blocks, sizeof(blocks));
}
void disk::read_block(blockid_t id, char *buf)
{
if (id < 0 || id >= BLOCK_NUM || buf == NULL)
return;
memcpy(buf, blocks[id], BLOCK_SIZE);
}
void
disk::write_block(blockid_t id, const char *buf)
{
if (id < 0 || id >= BLOCK_NUM || buf == NULL)
return;
memcpy(blocks[id], buf, BLOCK_SIZE);
}
void
disk::read_indirect_block(blockid_t id, uint32_t *buf)
{
if (id < 0 || id >= BLOCK_NUM || buf == NULL)
return;
memcpy(buf, blocks[id], BLOCK_SIZE);
}
void
disk::write_indirect_block(blockid_t id, const uint32_t *buf)
{
if (id < 0 || id >= BLOCK_NUM || buf == NULL)
return;
memcpy(blocks[id], buf, BLOCK_SIZE);
}
// block layer -----------------------------------------
// Allocate a free disk block.
blockid_t
block_manager::alloc_block()
{
/*
* your lab1 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.
*/
char buf[BLOCK_SIZE];
d->read_block(BBLOCK(++count), buf);
char* buf_bit = (char*)buf;
int byte_pos = count % BLOCK_SIZE;
int bit_pos = count % 8;
buf_bit += byte_pos;
bitset<8> char_bit(*buf_bit);
char_bit.set(bit_pos);
*buf_bit = char_bit.to_ulong();
d->write_block(BBLOCK(count), buf);
return count;
}
void
block_manager::free_block(uint32_t id)
{
/*
* your lab1 code goes here.
* note: you should unmark the corresponding bit in the block bitmap when free.
*/
char buf[BLOCK_SIZE];
d->read_block(BBLOCK(id), buf);
char* buf_bit = (char*)buf;
int byte_pos = id % BLOCK_SIZE;
int bit_pos = id % 8;
buf_bit += byte_pos;
bitset<8> char_bit(*buf_bit);
char_bit.set(bit_pos, 0);
*buf_bit = char_bit.to_ulong();
d->write_block(BBLOCK(id), buf);
bzero(buf, BLOCK_SIZE);
d->write_block(id, buf);
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;
// root, sb and a empty block between bitmap and inode table for the three
count = (BLOCK_NUM / BPB + 3) + ceil(INODE_NUM / IPB);
}
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);
}
void
block_manager::read_indirect_block(uint32_t id, uint32_t *buf)
{
d->read_indirect_block(id, buf);
}
void
block_manager::write_indirect_block(uint32_t id, const uint32_t *buf)
{
d->write_indirect_block(id, buf);
}
// inode layer -----------------------------------------
inode_manager::inode_manager()
{
count = 0;
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 lab1 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().
*/
inode* ino = (inode*)malloc(sizeof(inode));
ino->type = type;
put_inode(++count, ino);
return count;
}
void
inode_manager::free_inode(uint32_t inum)
{
/*
* your lab1 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.
*/
inode* ino = get_inode(inum);
uint32_t buf_indir[BLOCK_SIZE];
if (ino->type != 0) {
for (int i = 0; i < ceil( float(ino->size) / (float)BLOCK_SIZE ); i++ ) {
if (i < NDIRECT) {
if (ino->blocks[i] > 0) {
bm->free_block(ino->blocks[i]);
}
}
else if (i == NDIRECT) {
bm->read_indirect_block(ino->blocks[i], buf_indir);
}
if (i >= NDIRECT) {
bm->free_block(buf_indir[i - NDIRECT]);
}
}
bzero(ino, sizeof(struct inode));
put_inode(inum, ino);
}
return;
}
/* Return an inode structure by inum, NULL otherwise.
* Caller should release the memory. */
struct inode*
inode_manager::get_inode(uint32_t inum)
{
struct inode *ino, *ino_disk;
char buf[BLOCK_SIZE];
printf("\tim: get_inode %d\n", inum);
if (inum < 0 || inum >= INODE_NUM) {
printf("\tim: inum out of range\n");
return NULL;
}
bm->read_block(IBLOCK(inum, bm->sb.nblocks), buf);
// printf("%s:%d\n", __FILE__, __LINE__);
ino_disk = (struct inode*)buf + inum%IPB;
if (ino_disk->type == 0) {
printf("\tim: inode not exist\n");
return NULL;
}
ino = (struct inode*)malloc(sizeof(struct inode));
*ino = *ino_disk;
return ino;
}
void
inode_manager::put_inode(uint32_t inum, struct inode *ino)
{
char buf[BLOCK_SIZE];
struct inode *ino_disk;
printf("\tim: put_inode %d\n", inum);
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 lab1 code goes here.
* note: read blocks related to inode number inum,
* and copy them to buf_Out
*/
inode* ino = get_inode(inum);
if( ino == NULL ) return;
*buf_out = (char*)malloc(ino->size);
bzero(*buf_out, ino->size);
char* buf_read = *buf_out;
char buf_tmp[BLOCK_SIZE];
uint32_t indirId_tmp[BLOCK_SIZE];
int l = ceil((float)(ino->size)/(float)BLOCK_SIZE);
for (int i = 0; i < l; i++) {
if (i < NDIRECT) {
bm->read_block(ino->blocks[i], buf_tmp);
}
else if (i == NDIRECT) {
bm->read_indirect_block(ino->blocks[NDIRECT], indirId_tmp);
}
if (i >= NDIRECT) {
bm->read_block(indirId_tmp[i - NDIRECT], buf_tmp);
}
int read_size = MIN(BLOCK_SIZE, ino->size - *size);
memcpy(buf_read, buf_tmp, read_size);
buf_read += BLOCK_SIZE;
*size += read_size;
}
return;
}
/* alloc/free blocks if needed */
void
inode_manager::write_file(uint32_t inum, const char *buf, int size)
{
/*
* your lab1 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
*/
inode* ino = get_inode(inum);
if( ino == NULL || size < 0 || size > (int)MAXFILE * BLOCK_SIZE ) {
return;
}
char* buf_cpy = (char*)buf;
char buf_tmp[BLOCK_SIZE];
uint32_t indir[BLOCK_SIZE];
uint32_t indirId;
ino->size = size;
int l = ceil((float)size/(float)BLOCK_SIZE);
for (int i = 0; i < l; i++) {
uint32_t id;
if (i < NDIRECT) {
id = bm->alloc_block();
ino->blocks[i] = id;
}
else if (i == NDIRECT) {
indirId = bm->alloc_block();
ino->blocks[NDIRECT] = indirId;
}
if (i >= NDIRECT) {
id = bm->alloc_block();
indir[i - NDIRECT] = id;
}
memcpy(buf_tmp, buf_cpy, BLOCK_SIZE);
bm->write_block(id, buf_tmp);
buf_cpy += BLOCK_SIZE;
}
if (l > NDIRECT) {
bm->write_indirect_block(indirId, indir);
}
put_inode(inum, ino);
return;
}
void
inode_manager::getattr(uint32_t inum, extent_protocol::attr &a)
{
/*
* your lab1 code goes here.
* note: get the attributes of inode inum.
* you can refer to "struct attr" in extent_protocol.h
*/
inode* i = get_inode(inum);
if (i != NULL) {
a.type = i->type;
a.size = i->size;
a.atime = i->atime;
a.mtime = i->mtime;
a.ctime = i->ctime;
}
return;
}
void
inode_manager::remove_file(uint32_t inum)
{
/*
* your lab1 code goes here
* note: you need to consider about both the data block and inode of the file
*/
free_inode(inum);
return;
}