Files
banan-os/kernel/include/kernel/FS/Ext2/Inode.h
Bananymous 8fccb74542 Kernel: Add indirect block cache to ext2 inode
This reduces indirect block lookup by a lot :)
2026-05-25 02:14:04 +03:00

142 lines
4.6 KiB
C++

#pragma once
#include <BAN/String.h>
#include <BAN/StringView.h>
#include <kernel/FS/Ext2/Definitions.h>
#include <kernel/FS/Inode.h>
#include <kernel/Lock/RWLock.h>
namespace Kernel
{
class Ext2FS;
class Ext2Inode final : public Inode
{
public:
~Ext2Inode();
virtual const FileSystem* filesystem() const override;
private:
virtual BAN::ErrorOr<void> sync_inode(SyncType) override;
virtual BAN::ErrorOr<void> sync_data() override;
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
virtual BAN::ErrorOr<void> create_directory_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
virtual BAN::ErrorOr<void> link_inode_impl(BAN::StringView, BAN::RefPtr<Inode>) override;
virtual BAN::ErrorOr<void> rename_inode_impl(BAN::RefPtr<Inode>, BAN::StringView, BAN::StringView) override;
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override;
virtual BAN::ErrorOr<BAN::String> link_target_impl() override;
virtual BAN::ErrorOr<void> set_link_target_impl(BAN::StringView) override;
virtual BAN::ErrorOr<size_t> read_impl(off_t, BAN::ByteSpan) override;
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) override;
virtual BAN::ErrorOr<void> truncate_impl(size_t) override;
virtual bool can_read_impl() const override { return true; }
virtual bool can_write_impl() const override { return true; }
virtual bool has_error_impl() const override { return false; }
virtual bool has_hungup_impl() const override { return false; }
private:
uint32_t block_group() const;
// Returns maximum number of data blocks in use
// NOTE: the inode might have more blocks than what this suggests if it has been shrinked
uint32_t max_used_data_block_count() const { return size() / blksize(); }
BAN::ErrorOr<void> sync_inode_no_lock();
BAN::ErrorOr<bool> is_directory_empty_no_lock();
BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_no_lock(BAN::StringView);
/* needs write end of the lock when allocate is true*/
BAN::ErrorOr<BAN::Optional<uint32_t>> block_from_indirect_block_no_lock(uint32_t block, uint32_t index, uint32_t depth, bool allocate);
BAN::ErrorOr<BAN::Optional<uint32_t>> fs_block_of_data_block_index_no_lock(uint32_t data_block_index, bool allocate);
/* needs write end of the lock */
BAN::ErrorOr<void> link_inode_to_directory_no_lock(Ext2Inode&, BAN::StringView name);
BAN::ErrorOr<void> remove_inode_from_directory_no_lock(BAN::StringView name, bool cleanup_directory);
/* needs write end of the lock */
BAN::ErrorOr<void> cleanup_indirect_block_no_lock(uint32_t block, uint32_t depth);
BAN::ErrorOr<void> cleanup_default_links_no_lock();
BAN::ErrorOr<void> cleanup_data_blocks_no_lock();
BAN::ErrorOr<void> cleanup_from_fs_no_lock();
private:
Ext2Inode(Ext2FS& fs, Ext2::Inode inode, uint32_t ino);
BAN::Optional<uint32_t> block_cache_find(uint32_t block, uint32_t index) const;
void block_cache_remove(uint32_t block, uint32_t index);
void block_cache_add(uint32_t block, uint32_t index, uint32_t target);
BAN::RefPtr<Inode> dir_cache_find(BAN::StringView) const;
void dir_cache_remove(BAN::StringView);
void dir_cache_add(BAN::StringView, BAN::RefPtr<Inode>);
private:
struct ScopedSync
{
ScopedSync(Ext2Inode& inode)
: inode(inode)
{ }
~ScopedSync()
{
// TODO: there was some memcmp smarty pants stuff here.
// How do we wanna approach it?
if (auto ret = inode.sync_inode_no_lock(); ret.is_error())
dwarnln("failed to sync inode: {}", ret.error());
}
Ext2Inode& inode;
};
private:
Ext2FS& m_fs;
RWLock m_lock;
Ext2::InodeBlocks m_ext2_blocks;
// NOTE: some fields from the original disk inode
// that we do not use, but we keep for serialise.
const uint32_t m_og_dtime;
const uint32_t m_og_flags;
const uint32_t m_og_osd1;
const uint32_t m_og_generation;
const uint32_t m_og_file_acl;
const uint32_t m_og_dir_acl;
const uint32_t m_og_faddr;
const Ext2::Osd2 m_og_osd2;
struct BlockCacheEntry
{
mutable uint32_t freq;
uint32_t block;
uint32_t index;
uint32_t target;
};
mutable SpinLock m_block_cache_lock;
BAN::Array<BlockCacheEntry, 8> m_block_cache;
struct DirCacheEntry
{
mutable size_t freq { 0 };
BAN::RefPtr<Inode> inode;
size_t name_len { 0 };
char name[256];
};
static constexpr size_t dir_cache_size = 32;
mutable RWLock m_dir_cache_lock;
BAN::Vector<DirCacheEntry> m_dir_cache;
friend class Ext2FS;
friend class BAN::RefPtr<Ext2Inode>;
};
}