aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-08-28 20:05:24 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-08-28 20:05:24 +0200
commit662f5870a878ed29821fd82751629a8a0dd9041f (patch)
treead7607aad41a4eba483c5ca380d269cb4fc296b4
parentd3125dee241cd68ace6069537d36d32fc709b945 (diff)
downloadkernel-662f5870a878ed29821fd82751629a8a0dd9041f.tar.xz
kernel-662f5870a878ed29821fd82751629a8a0dd9041f.zip
kernel/fs: ext2: fix block allocation on 1 KiB fs
When computing the actual block numbers for a filesystem, care must be taken to account for which block is the actual first data block. For 1 KiB block size filesystems, that block is actually block 1, not 0. This is because the first 1 KiB on any ext2 volume is reserved for bootloader data. Complicating matters, the bitmaps don't take this into account. They essentially reflect a logical view, describing which data blocks are already allocated. On a 1 KiB filesystem, this effectively means that bit 0 of the allocation bitmap references physical block 1. Luckily, we don't need to make that determination based on the block size at all. The superblock already carries the number of the first data block. That means one can simply add that number, which is 1 in the 1 KiB block size case and 0 otherwise, to the found block index. Interestingly, this was already caught by accident when locating the authoritative block group descriptor (BGD) table. A factor of two was multiplied into the calculation in the case of a 1 KiB block size. This factor arises because the primary BGD table follows the primary superblock. Since the superblock is located in physical block 0 in all cases except for a 1 KiB block size, the BGD table generally lands in block 1. This implies an offset of 1 block size from the volume start. In the 1 KiB case, the BGD table lands in block number 2, effectively at an offset of 2 blocks from the start of the volume. This changeset makes that calculation explicit in the BGD table locator code as well. This clarifies the previously obscure factor of 2.
-rw-r--r--kernel/kernel/filesystems/ext2/filesystem.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/kernel/kernel/filesystems/ext2/filesystem.cpp b/kernel/kernel/filesystems/ext2/filesystem.cpp
index 384dfc77..7eaeeba2 100644
--- a/kernel/kernel/filesystems/ext2/filesystem.cpp
+++ b/kernel/kernel/filesystems/ext2/filesystem.cpp
@@ -568,7 +568,7 @@ namespace kernel::filesystems::ext2
//! @return The filesystem offset of the block group descriptor table.
[[nodiscard]] auto block_group_descriptor_table_offset(driver_state const & state) -> kstd::bytes
{
- return block_size(state) == 1024_B ? 2 * block_size(state) : block_size(state);
+ return (state.superblock.first_data_block + 1) * block_size(state);
}
//! The set of "incompatible" Extended Filesystem features supported by this driver.
@@ -894,7 +894,7 @@ namespace kernel::filesystems::ext2
{
bitmap_set(block_bitmap, i);
++claimed;
- allocated_blocks.push_back(i + group_index * superblock.blocks_per_group);
+ allocated_blocks.push_back(i + group_index * superblock.blocks_per_group + superblock.first_data_block);
}
}