This repository has been archived by the owner on Jan 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinode_manager.cc
402 lines (357 loc) · 10.2 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
#include "inode_manager.h"
// disk layer -----------------------------------------
disk::disk()
{
bzero(blocks, sizeof(blocks));
}
void disk::read_block(blockid_t id, char *buf)
{
/*
*your lab1 code goes here.
*if id 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 (id < 0 || id > BLOCK_NUM || buf == NULL) return;
memcpy(buf, blocks[id], BLOCK_SIZE);
}
void disk::write_block(blockid_t id, const char *buf)
{
/*
*your lab1 code goes here.
*hint: just like read_block
*/
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.
* hint: use macro IBLOCK and BBLOCK.
use bit operation.
remind yourself of the layout of disk.
*/
blockid_t id = IBLOCK(INODE_NUM, BLOCK_NUM);
char tmp[BLOCK_SIZE];
while (id < BLOCK_NUM) {
d->read_block(BBLOCK(id), tmp);
uint32_t num = id % BLOCK_SIZE;
uint32_t* buf = &((uint32_t *)tmp)[num/8];
if (!(*buf & (1 << num))) {
*buf |= (1 << num);
d->write_block(BBLOCK(id), tmp);
break;
}
id++;
}
return id;
}
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 tmp[BLOCK_SIZE];
d->read_block(BBLOCK(id), tmp);
uint32_t num = id % BLOCK_SIZE;
uint32_t* buf = &((uint32_t *)tmp)[num/8];
*buf &= ~(1 << num);
d->write_block(BBLOCK(id), tmp);
}
// 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);
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().
* if you get some heap memory, do not forget to free it.
*/
uint32_t inum = 1;
while (inum < INODE_NUM) {
struct inode *ino = get_inode(inum);
if (ino) {
free(ino);
inum++;
} else {
struct inode buf;
buf.type = type;
buf.atime = time(0);
buf.mtime = time(0);
buf.ctime = time(0);
buf.size = 0;
put_inode(inum, &buf);
return inum;
}
}
return 0;
}
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.
* do not forget to free memory if necessary.
*/
struct inode *ino = get_inode(inum);
if (ino) {
ino->type = 0;
ino->atime = time(0);
ino->mtime = time(0);
ino->size = 0;
put_inode(inum, ino);
}
free(ino);
}
/* 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
*/
struct inode *ino = get_inode(inum);
if (!ino) return;
ino->atime = time(0);
*size = ino->size;
uint32_t nblocks = 0;
// nblocks = ⌈(*size) / BLOCK_SIZE⌉
if (*size) nblocks = (*size) / BLOCK_SIZE + !((*size) % BLOCK_SIZE == 0);
*buf_out = (char *)malloc(nblocks * BLOCK_SIZE);
for (uint32_t i = 0; i < MIN(nblocks, NDIRECT); i++) {
bm->read_block(ino->blocks[i], *buf_out + i * BLOCK_SIZE);
}
if (nblocks > NDIRECT) {
blockid_t tmp[NINDIRECT];
bm->read_block(ino->blocks[NDIRECT], (char *)tmp);
for (uint32_t i = 0; i < nblocks - NDIRECT; i++) {
bm->read_block(tmp[i], *buf_out + NDIRECT * BLOCK_SIZE + i * BLOCK_SIZE);
}
}
free(ino);
}
/* 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.
* you should free some blocks if necessary.
*/
struct inode *ino = get_inode(inum);
if (!ino) return;
uint32_t noldBlocks = 0;
uint32_t nnewBlocks = 0;
// noldBlocks = ⌈(ino->size) / BLOCK_SIZE⌉
if (ino->size) noldBlocks = (ino->size) / BLOCK_SIZE + !((ino->size) % BLOCK_SIZE == 0);
// nnewBlocks = ⌈size / BLOCK_SIZE⌉
if (size) nnewBlocks = size / BLOCK_SIZE + !(size % BLOCK_SIZE == 0);
if (nnewBlocks <= noldBlocks) {
// nnewBlocks <= noldBlocks
// free useless blocks
if (noldBlocks <= NDIRECT) {
// nnewBlocks <= noldBlocks <= NDIRECT
for (uint32_t i = nnewBlocks; i < noldBlocks; i++) {
bm->free_block(ino->blocks[i]);
}
} else if (nnewBlocks > NDIRECT) {
// NDIRECT < nnewBlocks <= noldBlocks
blockid_t tmp[NINDIRECT];
bm->read_block(ino->blocks[NDIRECT], (char *)tmp);
for (uint32_t i = nnewBlocks; i < noldBlocks; i++) {
bm->free_block(tmp[i-NDIRECT]);
}
} else {
// nnewBlocks < NDIRECT < noldBlocks
for (uint32_t i = nnewBlocks; i < NDIRECT; i++) {
bm->free_block(ino->blocks[i]);
}
blockid_t tmp[NINDIRECT];
bm->read_block(ino->blocks[NDIRECT], (char *)tmp);
for (uint32_t i = 0; i < noldBlocks - NDIRECT; i++) {
bm->free_block(tmp[i]);
}
bm->free_block(ino->blocks[NDIRECT]);
}
} else {
// nnewBlocks > noldBlocks
// alloc new blocks
if (nnewBlocks <= NDIRECT) {
// noldBlocks <= nnewBlocks <= NDIRECT
for (uint32_t i = noldBlocks; i < nnewBlocks; i++) {
ino->blocks[i] = bm->alloc_block();
}
} else if (noldBlocks > NDIRECT) {
// NDIRECT < noldBlocks <= nnewBlocks
blockid_t tmp[NINDIRECT];
bm->read_block(ino->blocks[NDIRECT], (char *)tmp);
for (uint32_t i = noldBlocks; i < nnewBlocks; i++) {
tmp[i-NDIRECT] = bm->alloc_block();
}
bm->write_block(ino->blocks[NDIRECT], (char *)tmp);
} else {
// noldBlocks < NDIRECT < nnewBlocks
for (uint32_t i = noldBlocks; i < NDIRECT; i++) {
ino->blocks[i] = bm->alloc_block();
}
blockid_t tmp[NINDIRECT];
ino->blocks[NDIRECT] = bm->alloc_block();
for (uint32_t i = 0; i < nnewBlocks - NDIRECT; i++) {
tmp[i] = bm->alloc_block();
}
bm->write_block(ino->blocks[NDIRECT], (char *)tmp);
}
}
char tmp[BLOCK_SIZE];
char _tmp[BLOCK_SIZE];
uint32_t ptr = 0;
for (uint32_t i = 0; i < NDIRECT && ptr < size; i++) {
if (size - ptr > BLOCK_SIZE) {
bm->write_block(ino->blocks[i], buf + ptr);
ptr += BLOCK_SIZE;
} else {
uint32_t len = size - ptr;
memcpy(tmp, buf + ptr, len);
bm->write_block(ino->blocks[i], tmp);
ptr += len;
}
}
if (ptr < size) {
bm->read_block(ino->blocks[NDIRECT], _tmp);
for (uint32_t i = 0; i < NINDIRECT && ptr < size; i++) {
blockid_t id = *((blockid_t *)_tmp + i);
if (size - ptr > BLOCK_SIZE) {
bm->write_block(id, buf + ptr);
ptr += BLOCK_SIZE;
} else {
uint32_t len = size - ptr;
memcpy(tmp, buf + ptr, len);
bm->write_block(id, tmp);
ptr += len;
}
}
}
ino->size = size;
ino->mtime = time(0);
ino->ctime = time(0);
put_inode(inum, ino);
free(ino);
}
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
*/
struct inode *ino = get_inode(inum);
if (ino) {
a.type = ino->type;
a.atime = ino->atime;
a.mtime = ino->mtime;
a.ctime = ino->ctime;
a.size = ino->size;
free(ino);
}
}
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
* do not forget to free memory if necessary.
*/
struct inode *ino = get_inode(inum);
uint32_t nblocks = 0;
// nblocks = ⌈(ino->size) / BLOCK_SIZE⌉
if (ino->size) nblocks = (ino->size) / BLOCK_SIZE + !((ino->size) % BLOCK_SIZE == 0);
for (uint32_t i = 0; i < MIN(nblocks, NDIRECT); i++){
bm->free_block(ino->blocks[i]);
}
if (nblocks > NDIRECT) {
blockid_t tmp[NINDIRECT];
bm->read_block(ino->blocks[NDIRECT], (char *)tmp);
for (uint32_t i = 0; i < nblocks - NDIRECT; i++) {
bm->free_block(tmp[i]);
}
bm->free_block(ino->blocks[NDIRECT]);
}
free_inode(inum);
free(ino);
}