From 3a8b5b405cde53887ba1038bd231e9bc00fdffb9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 2 Nov 2019 11:49:11 +0100 Subject: [PATCH] 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.) --- Kernel/FileSystem/Ext2FileSystem.cpp | 47 +++++++--------------------- Kernel/FileSystem/Ext2FileSystem.h | 7 ++--- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 8b4022c818..45ca9ed01c 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -46,28 +46,11 @@ Ext2FS::~Ext2FS() { } -ByteBuffer Ext2FS::read_super_block() const +bool Ext2FS::flush_super_block() { LOCKER(m_lock); - auto buffer = ByteBuffer::create_uninitialized(1024); - bool success = device().read_block(2, buffer.data()); + bool success = device().write_blocks(2, 1, (const u8*)&m_super_block); 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; } @@ -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); } -const ext2_super_block& Ext2FS::super_block() const -{ - if (!m_cached_super_block) - m_cached_super_block = read_super_block(); - return *reinterpret_cast(m_cached_super_block.data()); -} - const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const { // 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 success = const_cast(device()).read_blocks(2, 1, (u8*)&m_super_block); + ASSERT(success); + auto& super_block = this->super_block(); #ifdef EXT2_DEBUG 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() { if (m_super_block_dirty) { - write_super_block(super_block()); + flush_super_block(); m_super_block_dirty = false; } if (m_block_group_descriptors_dirty) { @@ -1193,14 +1172,13 @@ bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state) ASSERT(success); // Update superblock - auto& sb = *reinterpret_cast(m_cached_super_block.data()); #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 if (new_state) - --sb.s_free_inodes_count; + --m_super_block.s_free_inodes_count; else - ++sb.s_free_inodes_count; + ++m_super_block.s_free_inodes_count; m_super_block_dirty = true; // Update BGD @@ -1257,14 +1235,13 @@ bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state) ASSERT(success); // Update superblock - auto& sb = *reinterpret_cast(m_cached_super_block.data()); #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 if (new_state) - --sb.s_free_blocks_count; + --m_super_block.s_free_blocks_count; else - ++sb.s_free_blocks_count; + ++m_super_block.s_free_blocks_count; m_super_block_dirty = true; // Update BGD diff --git a/Kernel/FileSystem/Ext2FileSystem.h b/Kernel/FileSystem/Ext2FileSystem.h index c98b0a98b7..97f669f244 100644 --- a/Kernel/FileSystem/Ext2FileSystem.h +++ b/Kernel/FileSystem/Ext2FileSystem.h @@ -78,7 +78,7 @@ private: typedef unsigned InodeIndex; explicit Ext2FS(NonnullRefPtr&&); - 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; void flush_block_group_descriptor_table(); unsigned first_block_of_group(unsigned groupIndex) const; @@ -90,8 +90,7 @@ private: bool write_ext2_inode(InodeIndex, const ext2_inode&); bool read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset, u8* buffer) const; - ByteBuffer read_super_block() const; - bool write_super_block(const ext2_super_block&); + bool flush_super_block(); virtual const char* class_name() const override; virtual InodeIdentifier root_inode() const override; @@ -129,7 +128,7 @@ private: 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 HashMap> m_inode_cache;