1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 22:15:07 +00:00

Ext2FS: Tidy up code related to the Ext2 super block a bit

Store the cached super block as an ext2_super_block member instead of
caching it in a ByteBuffer and using a casting helper everywhere.

This patch also combines reading/writing of the super block into a
single disk device operation (instead of two.)
This commit is contained in:
Andreas Kling 2019-11-02 11:49:11 +01:00
parent e4b7786b66
commit 3a8b5b405c
2 changed files with 15 additions and 39 deletions

View file

@ -46,28 +46,11 @@ Ext2FS::~Ext2FS()
{ {
} }
ByteBuffer Ext2FS::read_super_block() const bool Ext2FS::flush_super_block()
{ {
LOCKER(m_lock); LOCKER(m_lock);
auto buffer = ByteBuffer::create_uninitialized(1024); bool success = device().write_blocks(2, 1, (const u8*)&m_super_block);
bool success = device().read_block(2, buffer.data());
ASSERT(success); ASSERT(success);
success = device().read_block(3, buffer.offset_pointer(512));
ASSERT(success);
return buffer;
}
bool Ext2FS::write_super_block(const ext2_super_block& sb)
{
LOCKER(m_lock);
const u8* raw = (const u8*)&sb;
bool success;
success = device().write_block(2, raw);
ASSERT(success);
success = device().write_block(3, raw + 512);
ASSERT(success);
// FIXME: This is an ugly way to refresh the superblock cache. :-|
super_block();
return true; return true;
} }
@ -76,13 +59,6 @@ unsigned Ext2FS::first_block_of_group(GroupIndex group_index) const
return super_block().s_first_data_block + (group_index * super_block().s_blocks_per_group); return super_block().s_first_data_block + (group_index * super_block().s_blocks_per_group);
} }
const ext2_super_block& Ext2FS::super_block() const
{
if (!m_cached_super_block)
m_cached_super_block = read_super_block();
return *reinterpret_cast<ext2_super_block*>(m_cached_super_block.data());
}
const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const
{ {
// FIXME: Should this fail gracefully somehow? // FIXME: Should this fail gracefully somehow?
@ -104,6 +80,9 @@ const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const
bool Ext2FS::initialize() bool Ext2FS::initialize()
{ {
bool success = const_cast<DiskDevice&>(device()).read_blocks(2, 1, (u8*)&m_super_block);
ASSERT(success);
auto& super_block = this->super_block(); auto& super_block = this->super_block();
#ifdef EXT2_DEBUG #ifdef EXT2_DEBUG
kprintf("ext2fs: super block magic: %x (super block size: %u)\n", super_block.s_magic, sizeof(ext2_super_block)); kprintf("ext2fs: super block magic: %x (super block size: %u)\n", super_block.s_magic, sizeof(ext2_super_block));
@ -516,7 +495,7 @@ void Ext2FS::flush_block_group_descriptor_table()
void Ext2FS::flush_writes() void Ext2FS::flush_writes()
{ {
if (m_super_block_dirty) { if (m_super_block_dirty) {
write_super_block(super_block()); flush_super_block();
m_super_block_dirty = false; m_super_block_dirty = false;
} }
if (m_block_group_descriptors_dirty) { if (m_block_group_descriptors_dirty) {
@ -1193,14 +1172,13 @@ bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
ASSERT(success); ASSERT(success);
// Update superblock // Update superblock
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.data());
#ifdef EXT2_DEBUG #ifdef EXT2_DEBUG
dbgprintf("Ext2FS: superblock free inode count %u -> %u\n", sb.s_free_inodes_count, sb.s_free_inodes_count - 1); dbgprintf("Ext2FS: superblock free inode count %u -> %u\n", m_super_block.s_free_inodes_count, m_super_block.s_free_inodes_count - 1);
#endif #endif
if (new_state) if (new_state)
--sb.s_free_inodes_count; --m_super_block.s_free_inodes_count;
else else
++sb.s_free_inodes_count; ++m_super_block.s_free_inodes_count;
m_super_block_dirty = true; m_super_block_dirty = true;
// Update BGD // Update BGD
@ -1257,14 +1235,13 @@ bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
ASSERT(success); ASSERT(success);
// Update superblock // Update superblock
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.data());
#ifdef EXT2_DEBUG #ifdef EXT2_DEBUG
dbgprintf("Ext2FS: superblock free block count %u -> %u\n", sb.s_free_blocks_count, sb.s_free_blocks_count - 1); dbgprintf("Ext2FS: superblock free block count %u -> %u\n", m_super_block.s_free_blocks_count, m_super_block.s_free_blocks_count - 1);
#endif #endif
if (new_state) if (new_state)
--sb.s_free_blocks_count; --m_super_block.s_free_blocks_count;
else else
++sb.s_free_blocks_count; ++m_super_block.s_free_blocks_count;
m_super_block_dirty = true; m_super_block_dirty = true;
// Update BGD // Update BGD

View file

@ -78,7 +78,7 @@ private:
typedef unsigned InodeIndex; typedef unsigned InodeIndex;
explicit Ext2FS(NonnullRefPtr<DiskDevice>&&); explicit Ext2FS(NonnullRefPtr<DiskDevice>&&);
const ext2_super_block& super_block() const; const ext2_super_block& super_block() const { return m_super_block; }
const ext2_group_desc& group_descriptor(unsigned groupIndex) const; const ext2_group_desc& group_descriptor(unsigned groupIndex) const;
void flush_block_group_descriptor_table(); void flush_block_group_descriptor_table();
unsigned first_block_of_group(unsigned groupIndex) const; unsigned first_block_of_group(unsigned groupIndex) const;
@ -90,8 +90,7 @@ private:
bool write_ext2_inode(InodeIndex, const ext2_inode&); bool write_ext2_inode(InodeIndex, const ext2_inode&);
bool read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset, u8* buffer) const; bool read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset, u8* buffer) const;
ByteBuffer read_super_block() const; bool flush_super_block();
bool write_super_block(const ext2_super_block&);
virtual const char* class_name() const override; virtual const char* class_name() const override;
virtual InodeIdentifier root_inode() const override; virtual InodeIdentifier root_inode() const override;
@ -129,7 +128,7 @@ private:
unsigned m_block_group_count { 0 }; unsigned m_block_group_count { 0 };
mutable ByteBuffer m_cached_super_block; mutable ext2_super_block m_super_block;
mutable ByteBuffer m_cached_group_descriptor_table; mutable ByteBuffer m_cached_group_descriptor_table;
mutable HashMap<BlockIndex, RefPtr<Ext2FSInode>> m_inode_cache; mutable HashMap<BlockIndex, RefPtr<Ext2FSInode>> m_inode_cache;