From 58c6d30f6a29420356b349888b2509b44cadc8c9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 18 Jul 2021 01:29:54 +0200 Subject: [PATCH] Kernel/Ext2FS: Cache the root inode in a member variable We often get queried for the root inode, and it will always be cached in memory anyway, so let's make Ext2FS::root_inode() fast by keeping the root inode in a dedicated member variable. --- Kernel/FileSystem/Ext2FileSystem.cpp | 11 +++++++++-- Kernel/FileSystem/Ext2FileSystem.h | 3 ++- Kernel/FileSystem/FileSystem.h | 2 +- Kernel/FileSystem/Mount.h | 1 + 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 1966a0de14..b5e4d58813 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -145,12 +145,18 @@ bool Ext2FS::initialize() } } + m_root_inode = static_ptr_cast(get_inode({ fsid(), EXT2_ROOT_INO })); + if (!m_root_inode) { + dbgln("Ext2FS: failed to acquire root inode"); + return false; + } + return true; } NonnullRefPtr Ext2FS::root_inode() const { - return *get_inode({ fsid(), EXT2_ROOT_INO }); + return *m_root_inode; } bool Ext2FS::find_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset) const @@ -1778,7 +1784,7 @@ unsigned Ext2FS::free_inode_count() const return super_block().s_free_inodes_count; } -KResult Ext2FS::prepare_to_unmount() const +KResult Ext2FS::prepare_to_unmount() { MutexLocker locker(m_lock); @@ -1788,6 +1794,7 @@ KResult Ext2FS::prepare_to_unmount() const } m_inode_cache.clear(); + m_root_inode = nullptr; return KSuccess; } } diff --git a/Kernel/FileSystem/Ext2FileSystem.h b/Kernel/FileSystem/Ext2FileSystem.h index 65cc380efb..c71eefca76 100644 --- a/Kernel/FileSystem/Ext2FileSystem.h +++ b/Kernel/FileSystem/Ext2FileSystem.h @@ -99,7 +99,7 @@ public: virtual unsigned total_inode_count() const override; virtual unsigned free_inode_count() const override; - virtual KResult prepare_to_unmount() const override; + virtual KResult prepare_to_unmount() override; virtual bool supports_watchers() const override { return true; } @@ -183,6 +183,7 @@ private: KResult update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter); Vector> m_cached_bitmaps; + RefPtr m_root_inode; }; inline Ext2FS& Ext2FSInode::fs() diff --git a/Kernel/FileSystem/FileSystem.h b/Kernel/FileSystem/FileSystem.h index 0693b46b80..512a0c165f 100644 --- a/Kernel/FileSystem/FileSystem.h +++ b/Kernel/FileSystem/FileSystem.h @@ -43,7 +43,7 @@ public: virtual unsigned total_inode_count() const { return 0; } virtual unsigned free_inode_count() const { return 0; } - virtual KResult prepare_to_unmount() const { return KSuccess; } + virtual KResult prepare_to_unmount() { return KSuccess; } struct DirectoryEntryView { DirectoryEntryView(const StringView& name, InodeIdentifier, u8 file_type); diff --git a/Kernel/FileSystem/Mount.h b/Kernel/FileSystem/Mount.h index e4a8782971..57790ad043 100644 --- a/Kernel/FileSystem/Mount.h +++ b/Kernel/FileSystem/Mount.h @@ -23,6 +23,7 @@ public: Inode& guest() { return *m_guest; } FileSystem const& guest_fs() const { return *m_guest_fs; } + FileSystem& guest_fs() { return *m_guest_fs; } String absolute_path() const;