-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinode_manager.cc
More file actions
349 lines (283 loc) · 6.83 KB
/
inode_manager.cc
File metadata and controls
349 lines (283 loc) · 6.83 KB
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
#include "inode_manager.h"
#include <cstring>
#include <ctime>
// disk layer -----------------------------------------
disk::disk()
{
bzero(blocks, sizeof(blocks));
}
void
disk::read_block(blockid_t id, char *buf)
{
memcpy(buf, blocks[id], BLOCK_SIZE);
}
void
disk::write_block(blockid_t id, const char *buf)
{
memcpy(blocks[id], buf, BLOCK_SIZE);
}
// block layer -----------------------------------------
#define FIRST_DATA_BLOCK (IBLOCK(INODE_NUM - 1, BLOCK_NUM) + 1)
// Allocate a free disk block.
blockid_t
block_manager::alloc_block()
{
int mask;
char buf[BLOCK_SIZE];
blockid_t id, ret;
ret = 0;
for (id = FIRST_DATA_BLOCK; id < BLOCK_NUM && ret == 0; ++id) {
mask = 1 << (id % BPB % 8);
read_block(BBLOCK(id), buf);
if ((buf[id % BPB / 8] & mask) == 0) {
buf[id % BPB / 8] |= mask;
write_block(BBLOCK(id), buf);
ret = id;
}
}
return ret;
}
void
block_manager::free_block(uint32_t id)
{
int mask;
char buf[BLOCK_SIZE];
mask = 1 << (id % BPB % 8);
read_block(BBLOCK(id), buf);
buf[id % BPB / 8] &= ~mask;
write_block(BBLOCK(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;
}
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)
{
uint32_t inum, ret;
inode_t *ino;
ret = 0;
for (inum = 1; inum < INODE_NUM && ret == 0; ++inum) {
ino = get_inode(inum);
if (ino == NULL) {
ino = (inode_t *) malloc(sizeof(inode_t));
ino->type = type;
ino->size = 0;
ino->atime = ino->mtime = ino->ctime = (unsigned) time(NULL);
put_inode(inum, ino);
ret = inum;
}
free(ino);
}
return ret;
}
void
inode_manager::free_inode(uint32_t inum)
{
inode_t *ino;
ino = get_inode(inum);
if (ino == NULL)
return;
ino->type = 0;
put_inode(inum, ino);
free(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))
#define FILE_BLOCK_NUM(size) ((size) / BLOCK_SIZE + ((size) % BLOCK_SIZE != 0 ? 1 : 0))
/* 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)
{
char *ptr;
inode_t *ino;
blockid_t id;
unsigned i, n;
char buf[BLOCK_SIZE];
blockid_t ids[NINDIRECT];
ino = get_inode(inum);
if (ino == NULL) {
*buf_out = NULL;
*size = 0;
return;
}
n = FILE_BLOCK_NUM(ino->size);
*size = ino->size;
*buf_out = ptr = (char *) malloc(n * BLOCK_SIZE);
if (n > NDIRECT)
bm->read_block(ino->blocks[NDIRECT], (char *) ids);
for (i = 0; i < n; ++i) {
id = i < NDIRECT ? ino->blocks[i] : ids[i - NDIRECT];
bm->read_block(id, buf);
memcpy(ptr, buf, BLOCK_SIZE);
ptr += BLOCK_SIZE;
}
ino->atime = (unsigned) time(NULL);
put_inode(inum, ino);
free(ino);
return;
}
/* alloc/free blocks if needed */
void
inode_manager::write_file(uint32_t inum, const char *buf, int size)
{
char *buf_ext;
const char *ptr;
inode_t *ino;
unsigned i, j, n, m, total_alloc;
blockid_t id, ids[NINDIRECT], alloc_ids[MAXFILE];
ino = get_inode(inum);
if (ino == NULL)
return;
n = FILE_BLOCK_NUM(ino->size);
m = FILE_BLOCK_NUM(size);
if (n > NDIRECT)
bm->read_block(ino->blocks[NDIRECT], (char *) ids);
total_alloc = 0;
if (m > n)
total_alloc += m - n;
if (n <= NDIRECT && m > NDIRECT)
++total_alloc;
for (i = 0; i < total_alloc; ++i) {
id = bm->alloc_block();
if (id == 0) {
for (j = 0; j < i; ++j)
bm->free_block(alloc_ids[j]);
return;
}
alloc_ids[i] = id;
}
j = 0;
if (n <= NDIRECT && m > NDIRECT)
ino->blocks[NDIRECT] = alloc_ids[j++];
for (i = n; i < m; ++i)
if (i < NDIRECT)
ino->blocks[i] = alloc_ids[j++];
else
ids[i - NDIRECT] = alloc_ids[j++];
buf_ext = (char *) malloc(m * BLOCK_SIZE);
memcpy(buf_ext, buf, size);
ptr = buf_ext;
for (i = 0; i < m; ++i) {
id = i < NDIRECT ? ino->blocks[i] : ids[i - NDIRECT];
bm->write_block(id, ptr);
ptr += BLOCK_SIZE;
}
free(buf_ext);
for (i = m; i < n; ++i) {
id = i < NDIRECT ? ino->blocks[i] : ids[i - NDIRECT];
bm->free_block(id);
}
ino->size = size;
ino->atime = ino->ctime = ino->mtime = (unsigned) time(NULL);
put_inode(inum, ino);
if (m > NDIRECT)
bm->write_block(ino->blocks[NDIRECT], (char *) ids);
else if (n > NDIRECT)
bm->free_block(ino->blocks[NDIRECT]);
free(ino);
return;
}
void
inode_manager::getattr(uint32_t inum, extent_protocol::attr &a)
{
inode_t * ino;
ino = get_inode(inum);
if (ino != NULL) {
a.type = ino->type;
a.size = ino->size;
a.atime = ino->atime;
a.mtime = ino->mtime;
a.ctime = ino->ctime;
free(ino);
}
return;
}
void
inode_manager::remove_file(uint32_t inum)
{
inode_t *ino;
unsigned i, n;
blockid_t id, ids[NINDIRECT];
ino = get_inode(inum);
if (ino == NULL)
return;
n = FILE_BLOCK_NUM(ino->size);
if (n > NDIRECT)
bm->read_block(ino->blocks[NDIRECT], (char *) ids);
for (i = 0; i < n; ++i) {
id = i < NDIRECT ? ino->blocks[i] : ids[i - NDIRECT];
bm->free_block(id);
}
if (n > NDIRECT)
bm->free_block(ino->blocks[NDIRECT]);
free_inode(inum);
free(ino);
return;
}