Skip to content

Commit e2f3861

Browse files
committed
fixup! feat: add 'allocated' flag to inode data block
1 parent 93e2ef8 commit e2f3861

4 files changed

Lines changed: 34 additions & 23 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ if (TESTS)
2626
endif()
2727

2828
if (DEBUG)
29-
add_compile_options(-ggdb)
3029
set(CMAKE_BUILD_TYPE Debug)
3130
endif()
3231

src/inode.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,23 @@ ex_status ex_inode_allocate_indirect_block(struct ex_inode *inode, size_t n) {
8989

9090
debug("allocating parent inode: %zu", parent_block_no);
9191

92-
size_t address = ex_super_allocate_block();
93-
// we were unable to allocate parent block, there is nothing
94-
// we can do
95-
if (address == EX_BLOCK_INVALID_ADDRESS) {
96-
warning("unable to allocate block: %zu for inode: %zu",
92+
struct ex_inode_block block;
93+
94+
if (ex_super_allocate_data_block(&block) != OK) {
95+
warning("unable to allocate parent block: %zu for inode: (%lu)",
9796
n, inode->number);
9897
return INODE_BLOCK_ALLOCATION_FAILED;
9998
}
10099

101-
debug("parent inode is at: %zu", address);
100+
debug("parent inode is at: %zu", block.address);
102101

103-
inode->blocks[parent_block_no].address = address;
102+
inode->blocks[parent_block_no].address = block.address;
104103
inode->blocks[parent_block_no].allocated = 1;
105104
inode->nblocks++;
106105

107106
// clear the block space
108107
char empty[EX_BLOCK_SIZE] = {0};
109-
ex_device_write(address, (char *)empty, sizeof(empty));
108+
ex_device_write(block.address, (char *)empty, sizeof(empty));
110109

111110
ex_inode_flush(inode);
112111
}
@@ -132,17 +131,17 @@ ex_status ex_inode_allocate_indirect_block(struct ex_inode *inode, size_t n) {
132131

133132
debug("allocating new indirect block");
134133

135-
size_t address = ex_super_allocate_block();
136-
137-
if (address == EX_BLOCK_INVALID_ADDRESS) {
134+
struct ex_inode_block block;
135+
if (ex_super_allocate_data_block(&block) != OK) {
136+
warning("unable to allocate new indirect block: %zu", n);
138137
return INODE_BLOCK_ALLOCATION_FAILED;
139138
}
140139

141140
debug("block %zu in parent %zu is at (%zu)", block_off,
142-
parent_block_no, address);
141+
parent_block_no, block.address);
143142

144143
parent_block[block_off].allocated = 1;
145-
parent_block[block_off].address = address;
144+
parent_block[block_off].address = block.address;
146145

147146
ex_device_write(parent_block_address,
148147
(char *)parent_block,
@@ -162,17 +161,17 @@ ex_status ex_inode_allocate_blocks(struct ex_inode *inode) {
162161

163162
for (size_t i = 0; i < EX_DIRECT_BLOCKS; i++) {
164163

165-
struct ex_inode_block block;
164+
struct ex_inode_block block_;
166165

167-
if ((status = ex_super_allocate_data_block(&block)) != OK) {
166+
if ((status = ex_super_allocate_data_block(&block_)) != OK) {
168167
warning("failing to allocate nth (%lu) block", i);
169168
goto done;
170169
}
171170

172171
struct ex_data_block *block = &inode->blocks[i];
173172

174173
inode->nblocks += 1;
175-
block->address = address;
174+
block->address = block_.address;
176175
block->allocated = 1;
177176
}
178177

@@ -649,6 +648,7 @@ ex_status ex_inode_unlink(struct ex_inode *dir, const char *name) {
649648
}
650649

651650
entry->free = 1;
651+
entry->name[0] = '-';
652652

653653
ex_inode_flush(&inode);
654654
ex_dir_entry_flush(entry_address, entry);
@@ -718,6 +718,9 @@ ssize_t ex_inode_write(struct ex_inode *ino, size_t off, const char *data,
718718
size_t start_block_idx = off / EX_BLOCK_SIZE;
719719
size_t start_block_off = off % EX_BLOCK_SIZE;
720720

721+
info("start_block=%zu, start_block_off=%zu", start_block_idx, start_block_off);
722+
info("max_blocks=%zu", ex_inode_max_blocks());
723+
721724
if (start_block_idx >= ex_inode_max_blocks() ||
722725
(start_block_off + amount / EX_BLOCK_SIZE) >= ex_inode_max_blocks()) {
723726
return -1;
@@ -727,13 +730,16 @@ ssize_t ex_inode_write(struct ex_inode *ino, size_t off, const char *data,
727730
ino->size += (off + amount) - ino->size;
728731
}
729732

733+
// TODO
730734
block_address addr = ino->blocks[start_block_idx].address;
731735
char allocated = ino->blocks[start_block_idx].allocated;
732736

733737
if (!allocated) {
734-
// XXX: do allocation
738+
warning("start block (%zu) is not allocated", start_block_idx);
735739
}
736740

741+
debug("writing to %zu", addr);
742+
737743
ex_device_write(ino->address, (void *)ino, sizeof(struct ex_inode));
738744
ex_device_write(addr + start_block_off, data, amount);
739745

@@ -864,7 +870,7 @@ struct ex_inode_block ex_inode_block_iterate_indirect(struct ex_inode *inode,
864870
// XXX: this is the end, my only friend, the end
865871
struct ex_data_block block = ((struct ex_data_block *)it->indirect.data)[block_idx];
866872

867-
debug("ba: %zu, aloc: %zu", block.address, block.allocated);
873+
debug("ba: %zu, aloc: %i", block.address, (int)block.allocated);
868874

869875
// block in the indirect block is not allocated
870876
if (!block.allocated) {

test/test_mkfs_device_size.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ void test_mkfs_device_size(void) {
2323

2424
// the size of the super block an inode bitmap and a data bitmap rounded
2525
// to block size
26-
size_t expected_device_size = 3 * EX_BLOCK_SIZE;
26+
size_t expected_device_size = round_block(sizeof(struct ex_super_block));
27+
expected_device_size += round_block(super_block->bitmap.size);
28+
expected_device_size += round_block(super_block->inode_bitmap.size);
29+
2730
// size for `ninodes` inodes
2831
expected_device_size += EX_BLOCK_SIZE * ninodes;
2932
// number of data blocks for `ninodes` inodes

test/test_write_max_size.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
void test_write_max_size(void) {
1010
// create new device
1111
unlink(EX_DEVICE);
12+
1213
ex_mkfs_test_init();
1314

14-
size_t max_size = EX_DIRECT_BLOCKS * EX_BLOCK_SIZE - 1;
15-
char data[max_size];
15+
size_t max_size = ex_inode_max_blocks() * EX_BLOCK_SIZE - 1;
16+
// +1 because of EFBIG
17+
char *data = malloc(max_size + 1);
18+
g_assert(data);
1619

17-
memset(data, 'a', sizeof(data));
20+
memset(data, 'a', max_size + 1);
1821

1922
// create new file
2023
int rv = ex_create("/file", S_IRWXU, getgid(), getuid());

0 commit comments

Comments
 (0)