Kernel: Ext2 inodes are now stored in cache

This allows faster inode access and ensures working inodes when
opened in multiple places.
This commit is contained in:
Bananymous 2023-09-25 20:31:40 +03:00
parent b62186441b
commit 55d30a7cc3
2 changed files with 13 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <BAN/HashMap.h>
#include <kernel/Storage/StorageDevice.h>
#include <kernel/FS/FileSystem.h>
#include <kernel/FS/Ext2/Inode.h>
@ -65,6 +66,8 @@ namespace Kernel
BAN::ErrorOr<uint32_t> reserve_free_block(uint32_t primary_bgd);
BAN::HashMap<ino_t, BAN::RefPtr<Inode>>& inode_cache() { return m_inode_cache; }
const Ext2::Superblock& superblock() const { return m_superblock; }
struct BlockLocation
@ -104,6 +107,8 @@ namespace Kernel
BAN::RefPtr<Inode> m_root_inode;
BAN::Vector<uint32_t> m_superblock_backups;
BAN::HashMap<ino_t, BAN::RefPtr<Inode>> m_inode_cache;
BlockBufferManager m_buffer_manager;
Ext2::Superblock m_superblock;

View File

@ -23,6 +23,9 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<Inode>> 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<Inode>::adopt(result);
auto result = BAN::RefPtr<Inode>::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)