From 9d55cf1d8024de62da80a497e650d57ecf710b3b Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 25 Sep 2023 20:31:40 +0300 Subject: [PATCH] Kernel: Ext2 inodes are now stored in cache This allows faster inode access and ensures working inodes when opened in multiple places. --- kernel/include/kernel/FS/Ext2/FileSystem.h | 5 +++++ kernel/kernel/FS/Ext2/Inode.cpp | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/kernel/include/kernel/FS/Ext2/FileSystem.h b/kernel/include/kernel/FS/Ext2/FileSystem.h index 79a3276bde..1d1622718a 100644 --- a/kernel/include/kernel/FS/Ext2/FileSystem.h +++ b/kernel/include/kernel/FS/Ext2/FileSystem.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -65,6 +66,8 @@ namespace Kernel BAN::ErrorOr reserve_free_block(uint32_t primary_bgd); + BAN::HashMap>& inode_cache() { return m_inode_cache; } + const Ext2::Superblock& superblock() const { return m_superblock; } struct BlockLocation @@ -104,6 +107,8 @@ namespace Kernel BAN::RefPtr m_root_inode; BAN::Vector m_superblock_backups; + BAN::HashMap> m_inode_cache; + BlockBufferManager m_buffer_manager; Ext2::Superblock m_superblock; diff --git a/kernel/kernel/FS/Ext2/Inode.cpp b/kernel/kernel/FS/Ext2/Inode.cpp index 31fdefbc74..ade4789c46 100644 --- a/kernel/kernel/FS/Ext2/Inode.cpp +++ b/kernel/kernel/FS/Ext2/Inode.cpp @@ -23,6 +23,9 @@ namespace Kernel BAN::ErrorOr> Ext2Inode::create(Ext2FS& fs, uint32_t inode_ino) { + if (fs.inode_cache().contains(inode_ino)) + return fs.inode_cache()[inode_ino]; + auto inode_location = fs.locate_inode(inode_ino); auto block_buffer = fs.get_block_buffer(); @@ -30,10 +33,12 @@ namespace Kernel auto& inode = *(Ext2::Inode*)(block_buffer.data() + inode_location.offset); - Ext2Inode* result = new Ext2Inode(fs, inode, inode_ino); - if (result == nullptr) + Ext2Inode* result_ptr = new Ext2Inode(fs, inode, inode_ino); + if (result_ptr == nullptr) return BAN::Error::from_errno(ENOMEM); - return BAN::RefPtr::adopt(result); + auto result = BAN::RefPtr::adopt(result_ptr); + TRY(fs.inode_cache().insert(inode_ino, result)); + return result; } #define VERIFY_AND_READ_BLOCK(expr) do { const uint32_t block_index = expr; ASSERT(block_index); m_fs.read_block(block_index, block_buffer); } while (false)