summaryrefslogtreecommitdiff
path: root/kernel/fs/ext2.c
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-06-29 13:42:39 +0200
committerAnton Kling <anton@kling.gg>2024-06-29 14:42:41 +0200
commit385161fb8cc28614804d81f2a034aa92979bbc3f (patch)
treed1dfdc71779df408a9b90eb217a183354b733f67 /kernel/fs/ext2.c
parentf61ce73b5a480fc6b8ded0db86882fe304f17d77 (diff)
Ext2: Bug fix
Off by one error in block/inode lookup that caused roughly two days of debugging
Diffstat (limited to 'kernel/fs/ext2.c')
-rw-r--r--kernel/fs/ext2.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/kernel/fs/ext2.c b/kernel/fs/ext2.c
index e755884..e7f783a 100644
--- a/kernel/fs/ext2.c
+++ b/kernel/fs/ext2.c
@@ -348,7 +348,7 @@ int get_block(inode_t *inode, u32 i) {
int get_free_block(int allocate) {
bgdt_t block_group;
- u8 bitmap[BLOCK_SIZE];
+ u8 bitmap[(superblock->num_blocks_in_group) / 8];
assert(0 < superblock->num_blocks_unallocated);
for (u32 g = 0; g < num_block_groups(); g++) {
get_group_descriptor(g, &block_group);
@@ -357,26 +357,28 @@ int get_free_block(int allocate) {
continue;
}
- ext2_read_block(block_group.block_usage_bitmap, bitmap, BLOCK_SIZE, 0);
+ ext2_read_block(block_group.block_usage_bitmap, bitmap,
+ (superblock->num_blocks_in_group) / 8, 0);
for (u32 i = 0; i < superblock->num_blocks_in_group; i++) {
if (!(bitmap[i >> 3] & (1 << (i % 8)))) {
if (allocate) {
bitmap[i >> 3] |= (1 << (i % 8));
- ext2_write_block(block_group.block_usage_bitmap, bitmap, BLOCK_SIZE,
- 0);
+ ext2_write_block(block_group.block_usage_bitmap, bitmap,
+ superblock->num_blocks_in_group / 8, 0);
block_group.num_unallocated_blocks_in_group--;
write_group_descriptor(g, &block_group);
superblock->num_blocks_unallocated--;
raw_vfs_pwrite(mount_fd, superblock, 2 * SECTOR_SIZE, 0);
- // TODO: Temporary due to other code not being able to handle
- // offsets deep into files.
+ // TODO: Temporary due to other code not
+ // being able to handle offsets deep into
+ // files.
char buffer[block_byte_size];
memset(buffer, 0, block_byte_size);
- ext2_write_block(i + g * superblock->num_blocks_in_group + 1, buffer,
+ ext2_write_block(i + g * superblock->num_blocks_in_group, buffer,
block_byte_size, 0);
}
- return i + g * superblock->num_blocks_in_group + 1;
+ return i + g * superblock->num_blocks_in_group;
}
}
}
@@ -393,14 +395,14 @@ int get_free_inode(int allocate) {
continue;
}
- u8 bitmap[BLOCK_SIZE];
- ext2_read_block(block_group.inode_usage_bitmap, bitmap, BLOCK_SIZE, 0);
+ u8 bitmap[block_byte_size];
+ ext2_read_block(block_group.inode_usage_bitmap, bitmap, block_byte_size, 0);
for (u32 i = 0; i < superblock->num_inodes_in_group; i++) {
if (!(bitmap[i / 8] & (1 << (i % 8)))) {
if (allocate) {
bitmap[i / 8] |= (1 << (i % 8));
- ext2_write_block(block_group.inode_usage_bitmap, bitmap, BLOCK_SIZE,
- 0);
+ ext2_write_block(block_group.inode_usage_bitmap, bitmap,
+ block_byte_size, 0);
block_group.num_unallocated_inodes_in_group--;
write_group_descriptor(g, &block_group);
superblock->num_inodes_unallocated--;