-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode_manager.cc
454 lines (347 loc) · 9.76 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "inode_manager.h"
// disk layer -----------------------------------------
disk::disk() {
bzero(blocks, sizeof(blocks));
}
disk::disk(disk *copy) {
memcpy(blocks, copy->blocks, sizeof(blocks));
}
void disk::read_block(uint32_t id, char *buf) {
if (id < BLOCK_NUM && buf) {
memcpy(buf, blocks[id], BLOCK_SIZE);
}
}
void disk::write_block(uint32_t id, const char *buf) {
if (id < BLOCK_NUM && buf) {
memcpy(blocks[id], buf, BLOCK_SIZE);
}
}
template <class T>
diskcache<T>::diskcache(block_manager *to_bm, uint32_t to_id, bool read, bool write) {
to_bm->direct_access(this);
id = to_id;
if (read) {
d->read_block(id, buf);
}
do_write = write;
}
template <class T>
diskcache<T>::diskcache(disk *to_d, uint32_t to_id, bool read, bool write) {
d = to_d;
id = to_id;
if (read) {
d->read_block(id, buf);
}
do_write = write;
}
template <class T>
diskcache<T>::~diskcache() {
if (do_write) {
d->write_block(id, buf);
}
}
// block layer -----------------------------------------
void block_manager::lock_block(uint32_t id) {
diskcache<struct superblock> sb(d, 0, true, true);
if (id >= sb->nblocks) {
return;
}
uint32_t g = U32MAP_GLOBAL(id);
uint32_t l = U32MAP_LOCAL(id);
uint32_t p = U32MAP_POS(id);
diskcache<struct mapblock> mb(d, g + 1, true, true);
// set 0
mb->map[l] &= ~(1 << p);
if (!mb->map[l]) {
uint32_t l_l = U32MAP_LOCAL(l);
uint32_t l_p = U32MAP_POS(l);
sb->metamap[g][l_l] &= ~(1 << l_p);
}
}
void block_manager::unlock_block(uint32_t id) {
diskcache<struct superblock> sb(d, 0, true, true);
if (id >= sb->nblocks) {
return;
}
uint32_t g = U32MAP_GLOBAL(id);
uint32_t l = U32MAP_LOCAL(id);
uint32_t p = U32MAP_POS(id);
diskcache<struct mapblock> mb(d, g + 1, true, true);
// set 1
mb->map[l] |= 1 << p;
if (mb->map[l]) {
uint32_t l_l = U32MAP_LOCAL(l);
uint32_t l_p = U32MAP_POS(l);
sb->metamap[g][l_l] |= 1 << l_p;
}
}
uint32_t de_bruijn_pos(uint32_t value) {
static const uint32_t map[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
static const uint32_t dnum = 0x077CB531;
return map[uint32_t((value & -value) * dnum) >> 27];
}
uint32_t block_manager::pick_free_block() {
diskcache<struct superblock> sb(d, 0, true, true);
uint32_t g = sb->metamap_g;
do {
uint32_t l_l = sb->metamap_l_l;
do {
if (sb->metamap[g][l_l]) {
uint32_t l_p = de_bruijn_pos(sb->metamap[g][l_l]);
uint32_t l = U32MAP(0, l_l, l_p);
diskcache<struct mapblock> mb(d, g + 1, true, true);
uint32_t p = de_bruijn_pos(mb->map[l]);
// set 0
mb->map[l] &= ~(1 << p);
if (!mb->map[l]) {
sb->metamap[g][l_l] &= ~(1 << l_p);
}
sb->metamap_g = g;
sb->metamap_l_l = l_l;
return U32MAP(g, l, p);
}
l_l = (l_l + 1) % (U32MAP_TOTAL / 32);
} while (l_l != sb->metamap_l_l);
g = (g + 1) % sb->nmaps;
} while (g != sb->metamap_g);
return U32FILL; // disk is full
}
// Allocate a free disk block.
uint32_t block_manager::alloc_block(bool check) {
uint32_t id = pick_free_block();
if (check && id == uint32_t(U32FILL)) {
throw 1; // ?
}
return id;
}
void block_manager::free_block(uint32_t id) {
unlock_block(id);
}
block_manager::block_manager() {
d = new disk();
diskcache<struct superblock> sb(d, 0, false, true);
// format the disk
sb->size = BLOCK_SIZE * BLOCK_NUM;
sb->nblocks = BLOCK_NUM;
sb->nmaps = MAP_NUM;
sb->metamap_g = 0;
sb->metamap_l_l = 0;
for (uint32_t g = 0; g < sb->nmaps; ++g) {
diskcache<struct mapblock> mb(d, g + 1, false, true);
for (uint32_t l_l = 0; l_l < U32MAP_TOTAL / 32; ++l_l) {
for (uint32_t l_p = 0; l_p < 32; ++l_p) {
mb->map[U32MAP(0, l_l, l_p)] = U32FILL;
}
sb->metamap[g][l_l] = U32FILL;
}
}
// lock sb and mb
diskcache<struct mapblock> mmb(d, 1, true, true);
mmb->map[0] &= ~((1 << (1 + sb->nmaps)) - 1);
}
block_manager::block_manager(block_manager *copy) {
d = new disk(copy->d);
}
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);
}
template <class T>
void block_manager::direct_access(diskcache<T> *dc) {
dc->d = d;
}
// inode layer -----------------------------------------
uint32_t inode_chk1(uint32_t a, uint32_t b) {
return (~a * 0xDEADBEEF) ^ (b * 0xDEADCAFE) ^ ((a + ~b) * 23333 + (a + b) * 33333);
}
uint32_t inode_chk2(uint32_t a, uint32_t b) {
return (a * 0xC5E1ab01) ^ (~b * 0xF1303704) ^ ((~a + b) * 51303 + (~a + ~b) * 79084);
}
inode_manager::inode_manager() {
bm = new block_manager();
root_id = alloc_inode(extent_protocol::T_DIR);
}
uint32_t inode_manager::alloc_inum(uint32_t block_id) {
return block_id - 8;
}
uint32_t inode_manager::addr_inum(uint32_t inum) {
return inum + 8;
}
bool inode_manager::chk_inum(uint32_t inum) {
diskcache<struct inode> ni(bm, addr_inum(inum), true, false);
if (ni->chk1 != inode_chk1(inum, ni->rtag)) {
return false;
}
if (ni->chk2 != inode_chk2(inum, ni->rtag)) {
return false;
}
return true;
}
void inode_manager::free_inum(uint32_t inum) {
//
}
/* Create a new file.
* Return its inum. */
uint32_t inode_manager::alloc_inode(uint32_t type) {
uint32_t inum = alloc_inum(bm->alloc_block());
diskcache<struct inode> ni(bm, addr_inum(inum), false, true);
ni->njnode = 0;
ni->nknode = 0;
ni->rtag = rand() ^ (rand() << 16);
ni->chk1 = inode_chk1(inum, ni->rtag);
ni->chk2 = inode_chk2(inum, ni->rtag);
ni->attr.type = type;
ni->attr.atime = time(0);
ni->attr.mtime = time(0);
ni->attr.ctime = time(0);
ni->attr.size = 0;
return inum;
}
void inode_manager::free_inode(uint32_t inum) {
if (!chk_inum(inum)) {
return;
}
bm->free_block(addr_inum(inum));
free_inum(inum);
}
/* 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) {
if (!chk_inum(inum)) {
return;
}
diskcache<struct inode> ni(bm, addr_inum(inum), true, true);
ni->attr.atime = time(0);
*size = ni->attr.size;
char *begin = (char *) malloc(ni->attr.size);
char *end = begin + ni->attr.size;
*buf_out = begin;
memcpy(begin, ni->data, NDATA_MIXED_TRUNC(end - begin));
begin += NDATA_MIXED;
uint32_t j = 0;
uint32_t k = 0;
for (; j < ni->njnode; ++j) {
diskcache<struct jnode> nj(bm, ni->map[j], true, false);
memcpy(begin, nj->data, NDATA_MIXED_TRUNC(end - begin));
begin += NDATA_MIXED;
for (; k < ni->nknode && k < (j + 1) * NMAP_J; ++k) {
diskcache<struct knode> nk(bm, nj->map[k % NMAP_J], true, false);
memcpy(begin, nk->data, NDATA_FULL_TRUNC(end - begin));
begin += NDATA_FULL;
}
}
}
/* alloc/free blocks if needed */
void inode_manager::write_file(uint32_t inum, const char *buf, int size) {
if (!buf) {
return;
}
if (!chk_inum(inum)) {
return;
}
diskcache<struct inode> ni(bm, addr_inum(inum), true, true);
ni->attr.atime = time(0);
ni->attr.mtime = time(0);
ni->attr.size = size;
const char *begin = buf;
const char *end = begin + size;
memcpy(ni->data, begin, NDATA_MIXED_TRUNC(end - begin));
begin += NDATA_MIXED;
uint32_t j = 0;
uint32_t k = 0;
uint32_t jdel = 0;
uint32_t kdel = 0;
for (; j < ni->njnode || begin < end; ++j) {
if (begin < end) {
if (j == ni->njnode) {
if (j == NMAP_I) {
throw 2; // ?
} else {
ni->map[j] = bm->alloc_block();
++(ni->njnode);
}
}
diskcache<struct jnode> nj(bm, ni->map[j], true, true);
memcpy(nj->data, begin, NDATA_MIXED_TRUNC(end - begin));
begin += NDATA_MIXED;
for (; k < ni->nknode || begin < end; ++k) {
if (k == (j + 1) * NMAP_J) {
break;
}
if (begin < end) {
if (k == ni->nknode) {
nj->map[k % NMAP_J] = bm->alloc_block();
++(ni->nknode);
}
diskcache<struct knode> nk(bm, nj->map[k % NMAP_J], false, true);
memcpy(nk->data, begin, NDATA_FULL_TRUNC(end - begin));
begin += NDATA_FULL;
} else {
++kdel;
bm->free_block(nj->map[k % NMAP_J]);
}
}
} else {
diskcache<struct jnode> nj(bm, ni->map[j], true, false);
for (; k < ni->nknode && k < (ni->njnode + 1) * NMAP_J; ++k) {
++kdel;
bm->free_block(nj->map[k % NMAP_J]);
}
++jdel;
bm->free_block(ni->map[j]);
}
}
ni->njnode -= jdel;
ni->nknode -= kdel;
}
void inode_manager::getattr(uint32_t inum, extent_protocol::attr &a) {
if (!chk_inum(inum)) {
return;
}
diskcache<struct inode> ni(bm, addr_inum(inum), true, false);
a = ni->attr;
}
void inode_manager::remove_file(uint32_t inum) {
if (!chk_inum(inum)) {
return;
}
diskcache<struct inode> ni(bm, addr_inum(inum), true, true);
uint32_t j = 0;
uint32_t k = 0;
for (; j < ni->njnode; ++j) {
diskcache<struct jnode> nj(bm, ni->map[j], true, false);
for (; k < ni->nknode && k < (ni->njnode + 1) * NMAP_J; ++k) {
bm->free_block(nj->map[k % NMAP_J]);
}
bm->free_block(ni->map[j]);
}
free_inode(inum);
ni->njnode = 0;
ni->nknode = 0;
ni->rtag = 0;
ni->chk1 = 0;
ni->chk2 = 0;
}
void inode_manager::version_commit() {
bm_before.push_back(bm);
bm = new block_manager(bm);
}
void inode_manager::version_prev() {
if (!bm_before.empty()) {
bm_after.push_back(bm);
bm = bm_before.back();
bm_before.pop_back();
}
}
void inode_manager::version_next() {
if (!bm_after.empty()) {
bm_before.push_back(bm);
bm = bm_after.back();
bm_after.pop_back();
}
}