-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinode_manager.cc
389 lines (331 loc) · 10.4 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
#include "inode_manager.h"
// disk layer -----------------------------------------
disk::disk() : blocks() {
bzero(blocks, sizeof(blocks));
}
void
disk::read_block(uint bnum, char *buf) {
/*
*your code goes here.
*if bnum is smaller than 0 or larger than BLOCK_NUM
*or buf is null, just return.
*put the content of target block into buf.
*hint: use memcpy
*/
if (!buf || bnum < 0 || bnum >= BLOCK_NUM)
return;
memcpy(buf, blocks[bnum], BLOCK_SIZE);
}
void
disk::write_block(uint bnum, const char *buf) {
/*
*your code goes here.
*hint: just like read_block
*/
if (!buf || bnum < 0 || bnum >= BLOCK_NUM)
return;
memcpy(blocks[bnum], buf, BLOCK_SIZE);
}
// block layer -----------------------------------------
// Allocate a free disk block.
uint
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.
*/
char buf[BLOCK_SIZE], mask;
uint bnum;
// scan through blocks
for (bnum = 0; bnum < BLOCK_NUM; bnum += BPB) {
read_block(BBLOCK(bnum), buf);
for (uint off = 0; off < BPB; ++off) {
mask = (char) (1 << (off % 8));
if ((buf[off / 8] & mask) == 0) {
buf[off / 8] |= mask;
write_block(BBLOCK(bnum), buf);
return bnum + off;
}
}
}
return 0;
}
void
block_manager::free_block(uint bnum) {
/*
* your code goes here.
* note: you should unmark the corresponding bit in the block bitmap when free.
*/
if (bnum <= BBLOCK(BLOCK_NUM))
return;
char buf[BLOCK_SIZE];
read_block(BBLOCK(bnum), buf);
// set free bit
uint offset = bnum % BPB;
buf[offset / 8] &= ~(1 << (offset % 8));
write_block(BBLOCK(bnum), buf);
}
// The layout of disk should be like this:
// |<-sb->|<-free block bitmap->|<-inode table->|<-data->|
block_manager::block_manager() : sb() {
d = new disk();
// format the disk
sb.size = BLOCK_SIZE * BLOCK_NUM;
sb.nblocks = BLOCK_NUM;
sb.ninodes = INODE_NUM;
// alloc disk
for (uint bnum = 0; bnum < BLOCK_NUM / BPB
+ INODE_NUM / IPB
+ 2; ++bnum)
alloc_block();
}
void
block_manager::read_block(uint bnum, char *buf) {
d->read_block(bnum, buf);
}
void
block_manager::write_block(uint bnum, const char *buf) {
d->write_block(bnum, buf);
}
// inode layer -----------------------------------------
inode_manager::inode_manager() {
bm = new block_manager();
uint 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. */
uint
inode_manager::alloc_inode(uint 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().
* if you get some heap memory, do not forget to free it.
*/
char buf[BLOCK_SIZE];
// find a free block
uint inum, off;
for (inum = 1; inum < bm->sb.ninodes; ++inum) {
off = (inum - 1) % IPB;
if (!off)
bm->read_block(IBLOCK(inum, bm->sb.nblocks), buf);
inode_t *inode = (inode_t *) buf + off;
if (!inode->type) {
inode->type = (short) type;
inode->ctime = (uint) std::time(0);
bm->write_block(IBLOCK(inum, bm->sb.nblocks), buf);
return inum;
}
}
return 1;
}
void
inode_manager::free_inode(uint 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.
* do not forget to free memory if necessary.
*/
inode_t *inode = get_inode(inum);
if (!inode->type)
return;
else {
memset(inode, 0, sizeof(inode_t));
put_inode(inum, inode);
free(inode);
}
}
/* Return an inode structure by inum, 0 otherwise.
* Caller should release the memory. */
struct inode *
inode_manager::get_inode(uint inum) {
struct inode *inode, *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 0;
}
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 0;
}
inode = (struct inode *) malloc(sizeof(struct inode));
*inode = *ino_disk;
return inode;
}
void
inode_manager::put_inode(uint inum, struct inode *inode) {
char buf[BLOCK_SIZE];
struct inode *ino_disk;
printf("\tim: put_inode %d\n", inum);
if (inode == 0)
return;
bm->read_block(IBLOCK(inum, bm->sb.nblocks), buf);
ino_disk = (struct inode *) buf + inum % IPB;
*ino_disk = *inode;
bm->write_block(IBLOCK(inum, bm->sb.nblocks), buf);
}
/* Get all the data of a file by inum.
* Return alloced data, should be freed by caller. */
void
inode_manager::read_file(uint inum, char **buf_out, int *size) {
/*
* your code goes here.
* note: read blocks related to inode number inum,
* and copy them to buf_out
*/
inode_t *inode = get_inode(inum);
if (!inode)
return;
uint fsize = inode->size, bnum, rsize = 0, toread = 0;
char *res = (char *) malloc(fsize), buf[BLOCK_SIZE];
for (bnum = 0; rsize < fsize && bnum < NDIRECT; ++bnum) {
toread = MIN(BLOCK_SIZE, fsize - rsize);
if (rsize + BLOCK_SIZE < fsize)
bm->read_block(inode->blocks[bnum], res + rsize);
else {
bm->read_block(inode->blocks[bnum], buf);
memcpy(res + rsize, buf, (uint) fsize - rsize);
}
rsize += toread;
}
if (rsize < fsize) {
uint indnum[NINDIRECT];
bm->read_block(inode->blocks[NDIRECT], (char *) indnum);
for (bnum = 0; rsize < fsize && bnum < NINDIRECT; ++bnum) {
toread = MIN(BLOCK_SIZE, fsize - rsize);
if (rsize + BLOCK_SIZE < fsize)
bm->read_block(indnum[bnum], res + rsize);
else {
bm->read_block(indnum[bnum], buf);
memcpy(res + rsize, buf, (uint) fsize - rsize);
}
rsize += toread;
}
}
*size = (int) fsize;
*buf_out = res;
uint now = (uint) std::time(0);
inode->atime = now;
inode->ctime = now;
put_inode(inum, inode);
free(inode);
}
/* alloc/free blocks if needed */
void
inode_manager::write_file(uint 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.
* you should free some blocks if necessary.
*/
char block[BLOCK_SIZE], indnum[BLOCK_SIZE];
inode_t *inode = get_inode(inum);
uint osize = BLOCKS(inode->size), nsize = BLOCKS(size);
// free all blocks
for (uint bnum = 0; bnum < MIN(osize, NDIRECT); ++bnum)
bm->free_block(inode->blocks[bnum]);
if (osize > NDIRECT) {
uint ind = osize - NDIRECT;
bm->read_block(inode->blocks[NDIRECT], indnum);
for (uint bnum = 0; bnum < ind; ++bnum)
bm->free_block(*((uint *) indnum + bnum));
bm->free_block(inode->blocks[NDIRECT]);
}
// alloc new blocks
for (uint bnum = 0; bnum < MIN(nsize, NDIRECT); ++bnum)
inode->blocks[bnum] = bm->alloc_block();
if (nsize > NDIRECT) {
inode->blocks[NDIRECT] = bm->alloc_block();
bzero(indnum, BLOCK_SIZE);
uint ind = nsize - NDIRECT;
for (uint bnum = 0; bnum < ind; ++bnum)
*((uint *) indnum + bnum) = bm->alloc_block();
bm->write_block(inode->blocks[NDIRECT], indnum);
}
// write blocks
int rsize = 0, towrite;
uint bnum;
for (bnum = 0; rsize < size && bnum < NDIRECT; ++bnum) {
towrite = MIN(BLOCK_SIZE, size - rsize);
if (size - rsize > BLOCK_SIZE)
bm->write_block(inode->blocks[bnum], buf + rsize);
else {
memcpy(block, buf + rsize, (uint) size - rsize);
bm->write_block(inode->blocks[bnum], block);
}
rsize += towrite;
}
if (rsize < size) {
bm->read_block(inode->blocks[NDIRECT], indnum);
for (bnum = 0; rsize < size && bnum < NINDIRECT; ++bnum) {
uint ind = *((uint *) indnum + bnum);
towrite = MIN(BLOCK_SIZE, size - rsize);
if (size - rsize > BLOCK_SIZE)
bm->write_block(ind, buf + rsize);
else {
memcpy(block, buf + rsize, (uint) size - rsize);
bm->write_block(ind, block);
}
rsize += towrite;
}
}
time_t now = std::time(0);
inode->size = (uint) size;
inode->mtime = (uint) now;
inode->ctime = (uint) now;
put_inode(inum, inode);
free(inode);
}
void
inode_manager::getattr(uint 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
*/
inode_t *inode = get_inode(inum);
if (inode) {
a.type = (uint) inode->type;
a.atime = inode->atime;
a.mtime = inode->mtime;
a.ctime = inode->ctime;
a.size = inode->size;
free(inode);
}
}
void
inode_manager::remove_file(uint inum) {
/*
* your code goes here
* note: you need to consider about both the data block and inode of the file
* do not forget to free memory if necessary.
*/
inode_t *inode = get_inode(inum);
if (!inode)
return;
uint bnum = BLOCKS(inode->size);
if (bnum > NDIRECT) {
uint indnum[NINDIRECT];
bm->read_block(inode->blocks[NDIRECT], (char *) indnum);
for (uint i = 0; i < bnum - NDIRECT; ++i)
bm->free_block(indnum[i]);
bm->free_block(inode->blocks[NDIRECT]);
}
for (uint i = 0; i < MIN(bnum, NDIRECT); ++i)
bm->free_block(inode->blocks[i]);
free_inode(inum);
free(inode);
}