mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-02 09:18:52 +00:00
Ext2FS: Simplify block bitmap stuff.
Block bitmaps are only ever one block in size. I don't know why I thought otherwise, but use this info to simplify the code. :^)
This commit is contained in:
parent
58240fdb33
commit
1bf37db9a9
Notes:
sideshowbarker
2024-07-19 14:37:09 +09:00
Author: https://github.com/awesomekling
Commit: 1bf37db9a9
3 changed files with 23 additions and 45 deletions
|
@ -846,26 +846,6 @@ void Ext2FS::traverse_inode_bitmap(unsigned group_index, F callback) const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
void Ext2FS::traverse_block_bitmap(GroupIndex group_index, F callback) const
|
|
||||||
{
|
|
||||||
ASSERT(group_index <= m_block_group_count);
|
|
||||||
auto& bgd = group_descriptor(group_index);
|
|
||||||
|
|
||||||
int blocks_in_group = min(blocks_per_group(), super_block().s_blocks_count);
|
|
||||||
int block_count = ceil_div(blocks_in_group, 8u);
|
|
||||||
BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group();
|
|
||||||
int bits_per_block = block_size() * 8;
|
|
||||||
|
|
||||||
for (int i = 0; i < block_count; ++i) {
|
|
||||||
auto block = read_block(bgd.bg_block_bitmap + i);
|
|
||||||
ASSERT(block);
|
|
||||||
bool should_continue = callback(first_block_in_group + (i * bits_per_block) + 1, Bitmap::wrap(block.pointer(), blocks_in_group));
|
|
||||||
if (!should_continue)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
|
bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
|
||||||
{
|
{
|
||||||
LOCKER(m_lock);
|
LOCKER(m_lock);
|
||||||
|
@ -880,31 +860,34 @@ bool Ext2FS::write_ext2_inode(unsigned inode, const ext2_inode& e2inode)
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(GroupIndex group, int count)
|
Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(GroupIndex group_index, int count)
|
||||||
{
|
{
|
||||||
LOCKER(m_lock);
|
LOCKER(m_lock);
|
||||||
dbgprintf("Ext2FS: allocate_blocks(group: %u, count: %u)\n", group, count);
|
dbgprintf("Ext2FS: allocate_blocks(group: %u, count: %u)\n", group_index, count);
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
return { };
|
return { };
|
||||||
|
|
||||||
auto& bgd = group_descriptor(group);
|
auto& bgd = group_descriptor(group_index);
|
||||||
if (bgd.bg_free_blocks_count < count) {
|
if (bgd.bg_free_blocks_count < count) {
|
||||||
kprintf("Ext2FS: allocate_blocks can't allocate out of group %u, wanted %u but only %u available\n", group, count, bgd.bg_free_blocks_count);
|
kprintf("Ext2FS: allocate_blocks can't allocate out of group %u, wanted %u but only %u available\n", group_index, count, bgd.bg_free_blocks_count);
|
||||||
return { };
|
return { };
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Implement a scan that finds consecutive blocks if possible.
|
// FIXME: Implement a scan that finds consecutive blocks if possible.
|
||||||
Vector<BlockIndex> blocks;
|
Vector<BlockIndex> blocks;
|
||||||
traverse_block_bitmap(group, [&blocks, count] (unsigned first_block_in_bitmap, const Bitmap& bitmap) {
|
auto bitmap_block = read_block(bgd.bg_block_bitmap);
|
||||||
for (int i = 0; i < bitmap.size(); ++i) {
|
int blocks_in_group = min(blocks_per_group(), super_block().s_blocks_count);
|
||||||
if (!bitmap.get(i)) {
|
auto block_bitmap = Bitmap::wrap(bitmap_block.pointer(), blocks_in_group);
|
||||||
blocks.append(first_block_in_bitmap + i);
|
BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group() + 1;
|
||||||
if (blocks.size() == count)
|
for (int i = 0; i < block_bitmap.size(); ++i) {
|
||||||
return false;
|
if (!block_bitmap.get(i)) {
|
||||||
}
|
blocks.append(first_block_in_group + i);
|
||||||
|
if (blocks.size() == count)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return true;
|
}
|
||||||
});
|
|
||||||
|
ASSERT(blocks.size() == count);
|
||||||
dbgprintf("Ext2FS: allocate_block found these blocks:\n");
|
dbgprintf("Ext2FS: allocate_block found these blocks:\n");
|
||||||
for (auto& bi : blocks) {
|
for (auto& bi : blocks) {
|
||||||
dbgprintf(" > %u\n", bi);
|
dbgprintf(" > %u\n", bi);
|
||||||
|
@ -994,9 +977,8 @@ bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
|
||||||
auto& bgd = group_descriptor(group_index);
|
auto& bgd = group_descriptor(group_index);
|
||||||
unsigned index_in_group = index - ((group_index - 1) * inodes_per_group());
|
unsigned index_in_group = index - ((group_index - 1) * inodes_per_group());
|
||||||
unsigned inodes_per_bitmap_block = block_size() * 8;
|
unsigned inodes_per_bitmap_block = block_size() * 8;
|
||||||
unsigned bitmap_block_index = (index_in_group - 1) / inodes_per_bitmap_block;
|
|
||||||
unsigned bit_index = (index_in_group - 1) % inodes_per_bitmap_block;
|
unsigned bit_index = (index_in_group - 1) % inodes_per_bitmap_block;
|
||||||
auto block = read_block(bgd.bg_inode_bitmap + bitmap_block_index);
|
auto block = read_block(bgd.bg_inode_bitmap);
|
||||||
ASSERT(block);
|
ASSERT(block);
|
||||||
auto bitmap = Bitmap::wrap(block.pointer(), inodes_per_bitmap_block);
|
auto bitmap = Bitmap::wrap(block.pointer(), inodes_per_bitmap_block);
|
||||||
return bitmap.get(bit_index);
|
return bitmap.get(bit_index);
|
||||||
|
@ -1009,9 +991,8 @@ bool Ext2FS::set_inode_allocation_state(unsigned index, bool newState)
|
||||||
auto& bgd = group_descriptor(group_index);
|
auto& bgd = group_descriptor(group_index);
|
||||||
unsigned index_in_group = index - ((group_index - 1) * inodes_per_group());
|
unsigned index_in_group = index - ((group_index - 1) * inodes_per_group());
|
||||||
unsigned inodes_per_bitmap_block = block_size() * 8;
|
unsigned inodes_per_bitmap_block = block_size() * 8;
|
||||||
unsigned bitmap_block_index = (index_in_group - 1) / inodes_per_bitmap_block;
|
|
||||||
unsigned bit_index = (index_in_group - 1) % inodes_per_bitmap_block;
|
unsigned bit_index = (index_in_group - 1) % inodes_per_bitmap_block;
|
||||||
auto block = read_block(bgd.bg_inode_bitmap + bitmap_block_index);
|
auto block = read_block(bgd.bg_inode_bitmap);
|
||||||
ASSERT(block);
|
ASSERT(block);
|
||||||
auto bitmap = Bitmap::wrap(block.pointer(), inodes_per_bitmap_block);
|
auto bitmap = Bitmap::wrap(block.pointer(), inodes_per_bitmap_block);
|
||||||
bool current_state = bitmap.get(bit_index);
|
bool current_state = bitmap.get(bit_index);
|
||||||
|
@ -1021,7 +1002,7 @@ bool Ext2FS::set_inode_allocation_state(unsigned index, bool newState)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
bitmap.set(bit_index, newState);
|
bitmap.set(bit_index, newState);
|
||||||
bool success = write_block(bgd.bg_inode_bitmap + bitmap_block_index, block);
|
bool success = write_block(bgd.bg_inode_bitmap, block);
|
||||||
ASSERT(success);
|
ASSERT(success);
|
||||||
|
|
||||||
// Update superblock
|
// Update superblock
|
||||||
|
@ -1053,14 +1034,12 @@ bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
|
||||||
auto& bgd = group_descriptor(group_index);
|
auto& bgd = group_descriptor(group_index);
|
||||||
BlockIndex index_in_group = block_index - ((group_index - 1) * blocks_per_group());
|
BlockIndex index_in_group = block_index - ((group_index - 1) * blocks_per_group());
|
||||||
unsigned blocks_per_bitmap_block = block_size() * 8;
|
unsigned blocks_per_bitmap_block = block_size() * 8;
|
||||||
unsigned bitmap_block_index = (index_in_group - 1) / blocks_per_bitmap_block;
|
|
||||||
unsigned bit_index = (index_in_group - 1) % blocks_per_bitmap_block;
|
unsigned bit_index = (index_in_group - 1) % blocks_per_bitmap_block;
|
||||||
dbgprintf(" index_in_group: %u\n", index_in_group);
|
dbgprintf(" index_in_group: %u\n", index_in_group);
|
||||||
dbgprintf(" blocks_per_bitmap_block: %u\n", blocks_per_bitmap_block);
|
dbgprintf(" blocks_per_bitmap_block: %u\n", blocks_per_bitmap_block);
|
||||||
dbgprintf(" bitmap_block_index: %u\n", bitmap_block_index);
|
|
||||||
dbgprintf(" bit_index: %u\n", bit_index);
|
dbgprintf(" bit_index: %u\n", bit_index);
|
||||||
dbgprintf(" read_block(%u + %u = %u)\n", bgd.bg_block_bitmap, bitmap_block_index, bgd.bg_block_bitmap + bitmap_block_index);
|
dbgprintf(" read_block(%u)\n", bgd.bg_block_bitmap);
|
||||||
auto block = read_block(bgd.bg_block_bitmap + bitmap_block_index);
|
auto block = read_block(bgd.bg_block_bitmap);
|
||||||
ASSERT(block);
|
ASSERT(block);
|
||||||
auto bitmap = Bitmap::wrap(block.pointer(), blocks_per_bitmap_block);
|
auto bitmap = Bitmap::wrap(block.pointer(), blocks_per_bitmap_block);
|
||||||
bool current_state = bitmap.get(bit_index);
|
bool current_state = bitmap.get(bit_index);
|
||||||
|
@ -1070,7 +1049,7 @@ bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
bitmap.set(bit_index, new_state);
|
bitmap.set(bit_index, new_state);
|
||||||
bool success = write_block(bgd.bg_block_bitmap + bitmap_block_index, block);
|
bool success = write_block(bgd.bg_block_bitmap, block);
|
||||||
ASSERT(success);
|
ASSERT(success);
|
||||||
|
|
||||||
// Update superblock
|
// Update superblock
|
||||||
|
|
|
@ -107,7 +107,6 @@ private:
|
||||||
void dump_inode_bitmap(GroupIndex) const;
|
void dump_inode_bitmap(GroupIndex) const;
|
||||||
|
|
||||||
template<typename F> void traverse_inode_bitmap(GroupIndex, F) const;
|
template<typename F> void traverse_inode_bitmap(GroupIndex, F) const;
|
||||||
template<typename F> void traverse_block_bitmap(GroupIndex, F) const;
|
|
||||||
|
|
||||||
bool add_inode_to_directory(InodeIndex parent, InodeIndex child, const String& name, byte file_type, int& error);
|
bool add_inode_to_directory(InodeIndex parent, InodeIndex child, const String& name, byte file_type, int& error);
|
||||||
bool write_directory_inode(InodeIndex, Vector<DirectoryEntry>&&);
|
bool write_directory_inode(InodeIndex, Vector<DirectoryEntry>&&);
|
||||||
|
|
|
@ -5,7 +5,7 @@ fi
|
||||||
rm -vf _fs_contents.lock
|
rm -vf _fs_contents.lock
|
||||||
rm -vf _fs_contents
|
rm -vf _fs_contents
|
||||||
dd if=/dev/zero of=_fs_contents bs=1M count=512
|
dd if=/dev/zero of=_fs_contents bs=1M count=512
|
||||||
mke2fs _fs_contents
|
mke2fs -I 128 _fs_contents
|
||||||
chown 1000:1000 _fs_contents
|
chown 1000:1000 _fs_contents
|
||||||
mkdir -vp mnt
|
mkdir -vp mnt
|
||||||
mount -o loop _fs_contents mnt/
|
mount -o loop _fs_contents mnt/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue