-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInodeManager.cpp
610 lines (572 loc) · 24.7 KB
/
InodeManager.cpp
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#include "InodeManager.h"
#include <cassert>
#include <cstring>
#ifndef DEPLOY
#include <iostream>
#endif
InodeManager::InodeManager(std::shared_ptr<SuperBlockManager> sbm,
std::shared_ptr<BlockManager> bm)
: sbm_(sbm), bm_(bm) {}
bool InodeManager::getIdleInode(uint32_t* iid) {
auto sb = sbm_->readSuperBlock();
auto inode_per_tbl = (1024 << sb.s_log_block_size_) / sizeof(inode);
auto i_tbl_size =
(sb.s_inodes_per_group_ + inode_per_tbl - 1) / inode_per_tbl;
for (uint32_t group = 0; group < bm_->bgd_.size(); group++) {
auto bitmap_bid = bm_->bgd_[group].bg_inode_bitmap_;
auto bitmap = bm_->readBlock(bitmap_bid);
for (uint32_t i = 0; i < sb.s_inodes_per_group_; i++) {
int byteIndex = i / 8;
int bitIndex = i % 8;
if (((bitmap.s_[byteIndex] >> bitIndex) & 1) == 0) {
bitmap.s_[byteIndex] |= 1 << bitIndex;
bm_->writeBlock(bitmap, bitmap_bid);
*iid = group * sb.s_inodes_per_group_ + i + 1;
return true;
}
}
}
return false;
}
uint32_t InodeManager::new_inode(const inode& in) {
uint32_t iid;
assert(getIdleInode(&iid));
auto sb = sbm_->readSuperBlock();
int block_group = (iid - 1) / sb.s_inodes_per_group_;
int local_inode_index = (iid - 1) % sb.s_inodes_per_group_;
// edit inode table
auto inode_per_blk = (1024 << sb.s_log_block_size_) / sizeof(inode);
auto inode_tbl_bid = bm_->bgd_[block_group].bg_inode_table_ +
local_inode_index / inode_per_blk;
auto local_inode_index_in_block = local_inode_index % inode_per_blk;
auto inode_tbl_block = bm_->readBlock(inode_tbl_bid);
auto inode_tbl = reinterpret_cast<inode_table*>(inode_tbl_block.s_.get());
inode_tbl->inodes_[local_inode_index_in_block] = in;
bm_->writeBlock(inode_tbl_block, inode_tbl_bid);
return iid;
}
bool InodeManager::del_inode(uint32_t iid) {
auto sb = sbm_->readSuperBlock();
int block_group = (iid - 1) / sb.s_inodes_per_group_;
int local_inode_index = (iid - 1) % sb.s_inodes_per_group_;
auto bitmap_bid = bm_->bgd_[block_group].bg_inode_bitmap_;
auto inode_map_block = bm_->readBlock(bitmap_bid);
int byteIndex = local_inode_index / 8;
int bitIndex = local_inode_index % 8;
assert(inode_map_block.s_[byteIndex] & (1 << bitIndex));
inode_map_block.s_[byteIndex] &= ~(1 << bitIndex);
bm_->writeBlock(inode_map_block, bitmap_bid);
return true;
}
inode InodeManager::read_inode(uint32_t iid) const {
auto sb = sbm_->readSuperBlock();
int block_group = (iid - 1) / sb.s_inodes_per_group_;
int local_inode_index = (iid - 1) % sb.s_inodes_per_group_;
auto inode_per_blk = (1024 << sb.s_log_block_size_) / sizeof(inode);
auto i_tbl_bid = bm_->bgd_[block_group].bg_inode_table_ +
local_inode_index / inode_per_blk;
auto local_inode_index_in_block = local_inode_index % inode_per_blk;
auto i_tbl = bm_->readBlock(i_tbl_bid);
auto itbl = reinterpret_cast<inode_table*>(i_tbl.s_.get());
return itbl->inodes_[local_inode_index_in_block];
}
bool InodeManager::write_inode(const inode& in, uint32_t iid) {
auto sb = sbm_->readSuperBlock();
int block_group = (iid - 1) / sb.s_inodes_per_group_;
int local_inode_index = (iid - 1) % sb.s_inodes_per_group_;
auto inode_per_blk = (1024 << sb.s_log_block_size_) / sizeof(inode);
auto i_tbl_bid = bm_->bgd_[block_group].bg_inode_table_ +
local_inode_index / inode_per_blk;
auto local_inode_index_in_block = local_inode_index % inode_per_blk;
auto i_tbl = bm_->readBlock(i_tbl_bid);
auto itbl = reinterpret_cast<inode_table*>(i_tbl.s_.get());
itbl->inodes_[local_inode_index_in_block] = in;
bm_->writeBlock(i_tbl, i_tbl_bid);
return true;
}
size_t InodeManager::write_inode_data(uint32_t iid, const void* src,
size_t offset, size_t size) const {
auto block_size = 1024 << sbm_->readSuperBlock().s_log_block_size_;
auto in = read_inode(iid);
size_t written = 0;
while (written < size) {
size_t cur = offset + written;
size_t next_boundary = (cur + block_size) / block_size * block_size;
assert(next_boundary >= offset);
written +=
write_inode_data_helper(in, (char*)src + written, cur,
std::min(size - written, next_boundary - cur));
}
assert(written == size);
return written;
}
size_t InodeManager::read_inode_data(uint32_t iid, void* dst, size_t offset,
size_t size) const {
auto block_size = 1024 << sbm_->readSuperBlock().s_log_block_size_;
auto in = read_inode(iid);
size_t readed = 0;
while (readed < size) {
size_t cur = offset + readed;
size_t next_boundary = (cur + block_size) / block_size * block_size;
assert(next_boundary >= offset);
readed +=
read_inode_data_helper(in, (char*)dst + readed, cur,
std::min(size - readed, next_boundary - cur));
}
assert(readed == size);
return readed;
}
size_t InodeManager::read_inode_data_helper(const inode& in, void* dst,
size_t offset, size_t size) const {
auto slbs = sbm_->readSuperBlock().s_log_block_size_;
auto block_size = 1024 << slbs;
auto n_entries = block_size / sizeof(uint32_t);
assert(size + (offset % block_size) <= block_size);
int used_block = in.i_blocks_ / (2 << slbs);
size_t bid = offset / block_size;
size_t local_offset = offset % block_size;
if (bid < NDIRECT_BLOCK) {
assert(in.i_block_[bid]);
assert(bm_->state(in.i_block_[bid]));
auto block = bm_->readBlock(in.i_block_[bid]);
size_t read_size = std::min(size, block_size - local_offset);
memcpy(dst, block.s_.get() + local_offset, read_size);
return read_size;
} else if (bid < NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries) {
assert(in.i_block_[NDIRECT_BLOCK]);
assert(bm_->state(in.i_block_[NDIRECT_BLOCK]));
auto b1 = bm_->readBlock(in.i_block_[NDIRECT_BLOCK]);
auto local_bid = bid - NDIRECT_BLOCK;
auto bid1 = local_bid;
assert(*((uint32_t*)b1.s_.get() + bid1));
assert(bm_->state(*((uint32_t*)b1.s_.get() + bid1)));
auto block = bm_->readBlock(*((uint32_t*)b1.s_.get() + bid1));
size_t read_size = std::min(size, block_size - local_offset);
memcpy(dst, block.s_.get() + local_offset, read_size);
return read_size;
} else if (bid < NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries) {
assert(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK]);
assert(bm_->state(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK]));
auto b1 = bm_->readBlock(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK]);
auto local_bid = bid - NDIRECT_BLOCK - N1INDIRECT_BLOCK * n_entries;
auto bid1 = local_bid / n_entries;
auto bid2 = local_bid % n_entries;
assert(*((uint32_t*)b1.s_.get() + bid1));
assert(bm_->state(*((uint32_t*)b1.s_.get() + bid1)));
auto b2 = bm_->readBlock(*((uint32_t*)b1.s_.get() + bid1));
assert(*((uint32_t*)b2.s_.get() + bid2));
assert(bm_->state(*((uint32_t*)b2.s_.get() + bid2)));
auto block = bm_->readBlock(*((uint32_t*)b2.s_.get() + bid2));
size_t read_size = std::min(size, block_size - local_offset);
memcpy(dst, block.s_.get() + local_offset, read_size);
return read_size;
} else if (bid < NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries +
N3INDIRECT_BLOCK * n_entries * n_entries * n_entries) {
assert(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK]);
assert(bm_->state(
in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK]));
auto b1 = bm_->readBlock(
in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK]);
auto local_bid = bid - NDIRECT_BLOCK - N1INDIRECT_BLOCK * n_entries -
N2INDIRECT_BLOCK * n_entries * n_entries;
auto bid1 = local_bid / n_entries / n_entries;
auto bid2 = local_bid / n_entries % n_entries;
auto bid3 = local_bid % n_entries;
assert(*((uint32_t*)b1.s_.get() + bid1));
assert(bm_->state(*((uint32_t*)b1.s_.get() + bid1)));
auto b2 = bm_->readBlock(*((uint32_t*)b1.s_.get() + bid1));
assert(*((uint32_t*)b2.s_.get() + bid2));
assert(bm_->state(*((uint32_t*)b2.s_.get() + bid2)));
auto b3 = bm_->readBlock(*((uint32_t*)b2.s_.get() + bid2));
assert(*((uint32_t*)b3.s_.get() + bid3));
assert(bm_->state(*((uint32_t*)b3.s_.get() + bid3)));
auto block = bm_->readBlock(*((uint32_t*)b3.s_.get() + bid3));
size_t read_size = std::min(size, block_size - local_offset);
memcpy(dst, block.s_.get() + local_offset, read_size);
return read_size;
} else {
assert(0);
}
}
size_t InodeManager::write_inode_data_helper(const inode& in, void* src,
size_t offset, size_t size) const {
auto slbs = sbm_->readSuperBlock().s_log_block_size_;
auto block_size = 1024 << slbs;
auto n_entries = block_size / sizeof(uint32_t);
assert(size + (offset % block_size) <= block_size);
int used_block = in.i_blocks_ / (2 << slbs);
size_t bid = offset / block_size;
size_t local_offset = offset % block_size;
if (bid < NDIRECT_BLOCK) {
assert(in.i_block_[bid]);
assert(bm_->state(in.i_block_[bid]));
auto block = bm_->readBlock(in.i_block_[bid]);
size_t write_size = std::min(size, block_size - local_offset);
memcpy(block.s_.get() + local_offset, src, write_size);
bm_->writeBlock(block, in.i_block_[bid]);
return write_size;
} else if (bid < NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries) {
assert(in.i_block_[NDIRECT_BLOCK]);
assert(bm_->state(in.i_block_[NDIRECT_BLOCK]));
auto b1 = bm_->readBlock(in.i_block_[NDIRECT_BLOCK]);
auto local_bid = bid - NDIRECT_BLOCK;
auto bid1 = local_bid;
assert(*((uint32_t*)b1.s_.get() + bid1));
assert(bm_->state(*((uint32_t*)b1.s_.get() + bid1)));
auto block = bm_->readBlock(*((uint32_t*)b1.s_.get() + bid1));
size_t write_size = std::min(size, block_size - local_offset);
memcpy(block.s_.get() + local_offset, src, write_size);
bm_->writeBlock(block, *((uint32_t*)b1.s_.get() + bid1));
return write_size;
} else if (bid < NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries) {
assert(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK]);
assert(bm_->state(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK]));
auto b1 = bm_->readBlock(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK]);
auto local_bid = bid - NDIRECT_BLOCK - N1INDIRECT_BLOCK * n_entries;
auto bid1 = local_bid / n_entries;
auto bid2 = local_bid % n_entries;
assert(*((uint32_t*)b1.s_.get() + bid1));
assert(bm_->state(*((uint32_t*)b1.s_.get() + bid1)));
auto b2 = bm_->readBlock(*((uint32_t*)b1.s_.get() + bid1));
assert(*((uint32_t*)b2.s_.get() + bid2));
assert(bm_->state(*((uint32_t*)b2.s_.get() + bid2)));
auto block = bm_->readBlock(*((uint32_t*)b2.s_.get() + bid2));
size_t write_size = std::min(size, block_size - local_offset);
memcpy(block.s_.get() + local_offset, src, write_size);
bm_->writeBlock(block, *((uint32_t*)b2.s_.get() + bid2));
return write_size;
} else if (bid < NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries +
N3INDIRECT_BLOCK * n_entries * n_entries * n_entries) {
assert(in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK]);
assert(bm_->state(
in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK]));
auto b1 = bm_->readBlock(
in.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK]);
auto local_bid = bid - NDIRECT_BLOCK - N1INDIRECT_BLOCK * n_entries -
N2INDIRECT_BLOCK * n_entries * n_entries;
auto bid1 = local_bid / n_entries / n_entries;
auto bid2 = local_bid / n_entries % n_entries;
auto bid3 = local_bid % n_entries;
assert(*((uint32_t*)b1.s_.get() + bid1));
assert(bm_->state(*((uint32_t*)b1.s_.get() + bid1)));
auto b2 = bm_->readBlock(*((uint32_t*)b1.s_.get() + bid1));
assert(*((uint32_t*)b2.s_.get() + bid2));
assert(bm_->state(*((uint32_t*)b2.s_.get() + bid2)));
auto b3 = bm_->readBlock(*((uint32_t*)b2.s_.get() + bid2));
assert(*((uint32_t*)b3.s_.get() + bid3));
assert(bm_->state(*((uint32_t*)b3.s_.get() + bid3)));
auto block = bm_->readBlock(*((uint32_t*)b3.s_.get() + bid3));
size_t write_size = std::min(size, block_size - local_offset);
memcpy(block.s_.get() + local_offset, src, write_size);
bm_->writeBlock(block, *((uint32_t*)b3.s_.get() + bid3));
return write_size;
} else {
assert(0);
}
}
bool InodeManager::find_next(inode in, const std::string& dir, uint32_t* ret) {
for (auto it = dentry_begin(&in); it != dentry_end(&in); ++it) {
if (it.cur_dentry_name() == dir) {
*ret = it.cur_dentry().inode_;
return true;
}
}
return false;
}
bool InodeManager::dir_add_dentry(uint32_t dst, uint32_t src,
const std::string& name, uint8_t type) {
auto in = read_inode(dst);
assert(in.i_mode_ & EXT2_S_IFDIR);
auto block_size = 1024 << sbm_->readSuperBlock().s_log_block_size_;
// Construct dentry
assert(name.size() < 256);
dentry d = {src, static_cast<uint16_t>(UPPER4(sizeof(dentry) + name.size())),
static_cast<uint8_t>(name.size()), type};
if (in.i_size_ % block_size + d.rec_len_ > block_size) {
// modify the rec len of the last dentry
auto prev = dentry_begin(&in);
for (auto it = dentry_begin(&in); it != dentry_end(&in); ++it) {
prev = it;
}
auto prev_d = prev.cur_dentry();
auto new_rec_len = block_size - prev.offset_ % block_size;
auto new_i_size = in.i_size_ + new_rec_len - prev_d.rec_len_;
prev_d.rec_len_ = new_rec_len;
write_inode_data(dst, &prev_d, prev.offset_, sizeof(dentry));
resize(dst, new_i_size);
in = read_inode(dst);
}
auto write_offset = in.i_size_;
resize(dst, in.i_size_ + d.rec_len_);
write_inode_data(dst, &d, write_offset, sizeof(dentry));
write_inode_data(dst, name.c_str(), write_offset + sizeof(dentry),
name.size());
return true;
}
bool InodeManager::dir_del_dentry(uint32_t dst, const std::string& name) {
auto in = read_inode(dst);
assert(in.i_mode_ & EXT2_S_IFDIR);
auto prev = dentry_begin(&in);
for (auto it = dentry_begin(&in); it != dentry_end(&in); ++it) {
if (it.cur_dentry_name() == name) {
assert(it != dentry_begin(&in));
auto prev_dentry = prev.cur_dentry();
auto cur_dentry = it.cur_dentry();
prev_dentry.rec_len_ += cur_dentry.rec_len_;
write_inode_data(dst, &prev_dentry, prev.offset_, sizeof(dentry));
return true;
}
prev = it;
}
assert(0);
}
bool InodeManager::dir_empty(uint32_t dst) {
auto in = read_inode(dst);
assert(in.i_mode_ & EXT2_S_IFDIR);
if (in.i_links_count_ == 2) {
// only . and ..
return true;
}
for (auto it = dentry_begin(&in); it != dentry_end(&in); ++it) {
if (it.cur_dentry_name() != "." && it.cur_dentry_name() != "..") {
return false;
}
}
return true;
}
void InodeManager::resize(int iid, uint32_t size) {
auto inode = read_inode(iid);
auto slbs = sbm_->readSuperBlock().s_log_block_size_;
auto block_size = 1024 << slbs;
auto n_entries = block_size / sizeof(uint32_t);
int used_block = inode.i_blocks_ / (2 << slbs);
int after_block = (size + block_size - 1) / block_size;
if (used_block == after_block) {
inode.i_size_ = size;
write_inode(inode, iid);
return;
} else if (used_block > after_block) {
// Free direct blocks
for (int i = after_block; i < used_block && i < NDIRECT_BLOCK; ++i) {
assert(inode.i_block_[i]);
bm_->tagBlock(inode.i_block_[i], 0);
inode.i_block_[i] = 0;
}
// Free indirect blocks
if (used_block > NDIRECT_BLOCK) {
unsigned int start =
(after_block > NDIRECT_BLOCK) ? after_block - NDIRECT_BLOCK : 0;
unsigned int end =
used_block - NDIRECT_BLOCK > N1INDIRECT_BLOCK * n_entries
? N1INDIRECT_BLOCK * n_entries
: used_block - NDIRECT_BLOCK;
if (free_indirect_blocks(inode.i_block_[NDIRECT_BLOCK], 1, start, end))
inode.i_block_[NDIRECT_BLOCK] = 0;
}
// Free double indirect blocks
if (used_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries) {
unsigned int start =
after_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries
? after_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries)
: 0;
unsigned int end =
used_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries) >
N2INDIRECT_BLOCK * n_entries * n_entries
? N2INDIRECT_BLOCK * n_entries * n_entries
: used_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries);
if (free_indirect_blocks(inode.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK],
2, start, end))
inode.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK] = 0;
}
// Free triple indirect blocks
if (used_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries) {
unsigned int start =
after_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries
? after_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries)
: 0;
unsigned int end =
used_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries) >
N3INDIRECT_BLOCK * n_entries * n_entries * n_entries
? N3INDIRECT_BLOCK * n_entries * n_entries * n_entries
: used_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries);
if (free_indirect_blocks(inode.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK +
N2INDIRECT_BLOCK],
3, start, end))
inode.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK] = 0;
}
} else {
// for (int i = used_block; i < after_block; i++) {
// allocate_data(inode, i);
// }
// Allocate direct blocks
for (int i = used_block; i < after_block && i < NDIRECT_BLOCK; ++i) {
allocate_indirect_blocks(&inode.i_block_[i], 0, 0, 1);
}
// Allocate indirect blocks
if (after_block > NDIRECT_BLOCK) {
unsigned int start =
(used_block > NDIRECT_BLOCK) ? used_block - NDIRECT_BLOCK : 0;
unsigned int end =
after_block - NDIRECT_BLOCK > N1INDIRECT_BLOCK * n_entries
? N1INDIRECT_BLOCK * n_entries
: after_block - NDIRECT_BLOCK;
allocate_indirect_blocks(&inode.i_block_[NDIRECT_BLOCK], 1, start, end);
}
// Allocate double indirect blocks
if (after_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries) {
unsigned int start =
used_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries
? used_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries)
: 0;
unsigned int end =
after_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries) >
N2INDIRECT_BLOCK * n_entries * n_entries
? N2INDIRECT_BLOCK * n_entries * n_entries
: after_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries);
allocate_indirect_blocks(
&inode.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK], 2, start, end);
}
// Allocate triple indirect blocks
if (after_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries) {
unsigned int start =
used_block > NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries
? used_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries)
: 0;
unsigned int end =
after_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries) >
N3INDIRECT_BLOCK * n_entries * n_entries * n_entries
? N3INDIRECT_BLOCK * n_entries * n_entries * n_entries
: after_block - (NDIRECT_BLOCK + N1INDIRECT_BLOCK * n_entries +
N2INDIRECT_BLOCK * n_entries * n_entries);
allocate_indirect_blocks(
&inode.i_block_[NDIRECT_BLOCK + N1INDIRECT_BLOCK + N2INDIRECT_BLOCK],
3, start, end);
}
}
inode.i_size_ = size;
inode.i_blocks_ = (size + block_size - 1) / block_size * (2 << slbs);
write_inode(inode, iid);
}
void InodeManager::allocate_indirect_blocks(uint32_t* dst, int level,
size_t start, size_t end) {
assert(level >= 0);
if (*dst)
assert(bm_->state(*dst));
else
*dst = bm_->getIdleBlock();
if (level == 0) return;
auto n_entries =
(1024 << sbm_->readSuperBlock().s_log_block_size_) / sizeof(uint32_t);
int level_entries = 1;
for (int i = 1; i < level; ++i) level_entries *= n_entries;
size_t start_index = start / level_entries;
size_t end_index = (end - 1) / level_entries;
unsigned int sub_start, sub_end;
auto block = bm_->readBlock(*dst);
for (size_t i = start_index; i <= end_index; ++i) {
auto entry = (uint32_t*)block.s_.get() + i;
sub_start = (i == start_index) ? start % level_entries : 0;
sub_end = (i == end_index) ? end % level_entries : level_entries;
allocate_indirect_blocks(entry, level - 1, sub_start, sub_end);
}
bm_->writeBlock(block, *dst);
}
bool InodeManager::free_indirect_blocks(uint32_t bid, int level, size_t start,
size_t end) {
auto block = bm_->readBlock(bid);
auto n_entries =
(1024 << sbm_->readSuperBlock().s_log_block_size_) / sizeof(uint32_t);
if (level == 1) {
for (size_t i = start; i < end; i++) {
auto entry = *((uint32_t*)block.s_.get() + i);
assert(entry);
bm_->tagBlock(entry, 0);
*((uint32_t*)block.s_.get() + i) = 0;
}
if (start == 0) {
bm_->tagBlock(bid, 0);
return true;
}
return false;
}
int level_entries = 1;
for (int i = 1; i < level; ++i) {
level_entries *= n_entries;
}
size_t start_index = start / level_entries;
size_t end_index = (end - 1) / level_entries;
unsigned int sub_start, sub_end;
for (size_t i = start_index; i < end_index; ++i) {
auto entry = *((uint32_t*)block.s_.get() + i);
assert(entry);
sub_start = (i == start_index) ? start % level_entries : 0;
sub_end = (i == end_index) ? end % level_entries : level_entries;
if (free_indirect_blocks(entry, level - 1, sub_start, sub_end))
*((uint32_t*)block.s_.get() + i) = 0;
}
bm_->writeBlock(block, bid);
if (start_index == 0) {
bm_->tagBlock(bid, 0);
return true;
}
return false;
}
InodeManager::dentry_iterator::dentry_iterator(inode* in, InodeManager* im,
size_t offset)
: dinode_(in), im_(im), offset_(offset) {}
dentry InodeManager::dentry_iterator::cur_dentry() const {
assert(offset_ != dinode_->i_size_);
dentry ret;
auto read_size =
im_->read_inode_data_helper(*dinode_, &ret, offset_, sizeof(dentry));
assert(read_size == sizeof(dentry));
return ret;
}
std::string InodeManager::dentry_iterator::cur_dentry_name() const {
assert(offset_ != dinode_->i_size_);
auto dentry = cur_dentry();
char name[256];
auto read_size = im_->read_inode_data_helper(
*dinode_, name, offset_ + sizeof(dentry), dentry.name_len_);
assert(read_size == dentry.name_len_);
return std::string(name, dentry.name_len_);
}
InodeManager::dentry_iterator& InodeManager::dentry_iterator::operator++() {
assert(offset_ != dinode_->i_size_);
auto dentry = cur_dentry();
offset_ += dentry.rec_len_;
return *this;
}
InodeManager::dentry_iterator InodeManager::dentry_begin(inode* in) {
return InodeManager::dentry_iterator(in, this, 0);
}
InodeManager::dentry_iterator InodeManager::dentry_end(inode* in) {
return InodeManager::dentry_iterator(in, this, in->i_size_);
}
bool operator==(const InodeManager::dentry_iterator& lhs,
const InodeManager::dentry_iterator& rhs) {
if (memcmp(lhs.dinode_, rhs.dinode_, sizeof(inode)) != 0) {
return false;
}
return lhs.offset_ == rhs.offset_;
}
bool operator!=(const InodeManager::dentry_iterator& lhs,
const InodeManager::dentry_iterator& rhs) {
if (memcmp(lhs.dinode_, rhs.dinode_, sizeof(inode)) != 0) {
return true;
}
return lhs.offset_ != rhs.offset_;
}