From 2301654c4e9f6053d22bfe94a13f466bb6415178 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 30 Sep 2023 19:06:05 +0300 Subject: [PATCH] Kernel: Rework RamInode API RamInode is now a general RamInode with no data. RamFileInode is now a inode for regular files. This is much cleaner and more intuitive since there is no reason for most non-regular inodes to hold data Vector. --- kernel/include/kernel/FS/RamFS/Inode.h | 40 ++++--- kernel/kernel/Device/Device.cpp | 2 +- kernel/kernel/FS/DevFS/FileSystem.cpp | 2 +- kernel/kernel/FS/RamFS/Inode.cpp | 139 ++++++++++++++----------- kernel/kernel/FS/VirtualFileSystem.cpp | 10 +- kernel/kernel/Terminal/TTY.cpp | 2 +- 6 files changed, 110 insertions(+), 85 deletions(-) diff --git a/kernel/include/kernel/FS/RamFS/Inode.h b/kernel/include/kernel/FS/RamFS/Inode.h index afaec62840..eca6852071 100644 --- a/kernel/include/kernel/FS/RamFS/Inode.h +++ b/kernel/include/kernel/FS/RamFS/Inode.h @@ -12,7 +12,6 @@ namespace Kernel class RamInode : public Inode { public: - static BAN::ErrorOr> create(RamFileSystem&, mode_t, uid_t, gid_t); virtual ~RamInode() = default; virtual ino_t ino() const override { return m_inode_info.ino; } @@ -31,16 +30,10 @@ namespace Kernel void add_link() { m_inode_info.nlink++; } - protected: - RamInode(RamFileSystem& fs, mode_t, uid_t, gid_t); - - virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; - virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override; - virtual BAN::ErrorOr truncate_impl(size_t) override; - protected: struct FullInodeInfo { + FullInodeInfo(RamFileSystem&, mode_t, uid_t, gid_t); ino_t ino; mode_t mode; nlink_t nlink; @@ -56,16 +49,36 @@ namespace Kernel dev_t rdev; }; + RamInode(RamFileSystem& fs, const FullInodeInfo& inode_info) + : m_fs(fs) + , m_inode_info(inode_info) + {} + protected: RamFileSystem& m_fs; FullInodeInfo m_inode_info; + }; + class RamFileInode : public RamInode + { + public: + static BAN::ErrorOr> create(RamFileSystem&, mode_t, uid_t, gid_t); + ~RamFileInode() = default; + + protected: + RamFileInode(RamFileSystem&, const FullInodeInfo&); + + virtual BAN::ErrorOr read_impl(off_t, void*, size_t) override; + virtual BAN::ErrorOr write_impl(off_t, const void*, size_t) override; + virtual BAN::ErrorOr truncate_impl(size_t) override; + + private: BAN::Vector m_data; friend class RamFileSystem; }; - class RamDirectoryInode final : public RamInode + class RamDirectoryInode : public RamInode { public: static BAN::ErrorOr> create(RamFileSystem&, ino_t parent, mode_t, uid_t, gid_t); @@ -74,13 +87,12 @@ namespace Kernel BAN::ErrorOr add_inode(BAN::StringView, BAN::RefPtr); protected: + RamDirectoryInode(RamFileSystem&, const FullInodeInfo&, ino_t parent); + virtual BAN::ErrorOr> find_inode_impl(BAN::StringView) override; virtual BAN::ErrorOr list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) override; virtual BAN::ErrorOr create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override; - private: - RamDirectoryInode(RamFileSystem&, ino_t parent, mode_t, uid_t, gid_t); - private: static constexpr size_t m_name_max = NAME_MAX; struct Entry @@ -92,7 +104,7 @@ namespace Kernel private: BAN::Vector m_entries; - ino_t m_parent; + const ino_t m_parent; friend class RamFileSystem; }; @@ -111,7 +123,7 @@ namespace Kernel virtual BAN::ErrorOr link_target_impl() override; private: - RamSymlinkInode(RamFileSystem&, mode_t, uid_t, gid_t); + RamSymlinkInode(RamFileSystem&, const FullInodeInfo&, BAN::String&&); private: BAN::String m_target; diff --git a/kernel/kernel/Device/Device.cpp b/kernel/kernel/Device/Device.cpp index f79ac05b2f..1971a1dedd 100644 --- a/kernel/kernel/Device/Device.cpp +++ b/kernel/kernel/Device/Device.cpp @@ -5,7 +5,7 @@ namespace Kernel { Device::Device(mode_t mode, uid_t uid, gid_t gid) - : RamInode(DevFileSystem::get(), mode, uid, gid) + : RamInode(DevFileSystem::get(), FullInodeInfo(DevFileSystem::get(), mode, uid, gid)) { } } \ No newline at end of file diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index db4d64ac47..b41b09b2eb 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -19,7 +19,7 @@ namespace Kernel s_instance = new DevFileSystem(1024 * 1024); ASSERT(s_instance); - auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, Inode::Mode::IFDIR | 0755, 0, 0)); + auto root_inode = MUST(RamDirectoryInode::create(*s_instance, 0, 0755, 0, 0)); MUST(s_instance->set_root_inode(root_inode)); s_instance->add_device("null", MUST(NullDevice::create(0666, 0, 0))); diff --git a/kernel/kernel/FS/RamFS/Inode.cpp b/kernel/kernel/FS/RamFS/Inode.cpp index 84e2d851d3..f53146279e 100644 --- a/kernel/kernel/FS/RamFS/Inode.cpp +++ b/kernel/kernel/FS/RamFS/Inode.cpp @@ -5,42 +5,51 @@ namespace Kernel { - /* - - RAM INODE - - */ - - BAN::ErrorOr> RamInode::create(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) - { - ASSERT(Mode(mode).ifreg()); - auto* ram_inode = new RamInode(fs, mode, uid, gid); - if (ram_inode == nullptr) - return BAN::Error::from_errno(ENOMEM); - return BAN::RefPtr::adopt(ram_inode); - } - - RamInode::RamInode(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) - : m_fs(fs) + RamInode::FullInodeInfo::FullInodeInfo(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) { timespec current_time = SystemTimer::get().real_time(); - m_inode_info.ino = fs.next_ino(); - m_inode_info.mode = mode; - m_inode_info.nlink = 1; - m_inode_info.uid = uid; - m_inode_info.gid = gid; - m_inode_info.size = 0; - m_inode_info.atime = current_time; - m_inode_info.mtime = current_time; - m_inode_info.ctime = current_time; - m_inode_info.blksize = fs.blksize(); - m_inode_info.blocks = 0; - m_inode_info.dev = 0; - m_inode_info.rdev = 0; + this->ino = fs.next_ino(); + this->mode = mode; + this->nlink = 1; + this->uid = uid; + this->gid = gid; + this->size = 0; + this->atime = current_time; + this->mtime = current_time; + this->ctime = current_time; + this->blksize = fs.blksize(); + this->blocks = 0; + + // TODO + this->dev = 0; + this->rdev = 0; } - BAN::ErrorOr RamInode::read_impl(off_t offset, void* buffer, size_t bytes) + /* + + RAM FILE INODE + + */ + + BAN::ErrorOr> RamFileInode::create(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) + { + FullInodeInfo inode_info(fs, mode, uid, gid); + + auto* ram_inode = new RamFileInode(fs, inode_info); + if (ram_inode == nullptr) + return BAN::Error::from_errno(ENOMEM); + return BAN::RefPtr::adopt(ram_inode); + } + + RamFileInode::RamFileInode(RamFileSystem& fs, const FullInodeInfo& inode_info) + : RamInode(fs, inode_info) + { + ASSERT((m_inode_info.mode & Inode::Mode::TYPE_MASK) == 0); + m_inode_info.mode |= Inode::Mode::IFREG; + } + + BAN::ErrorOr RamFileInode::read_impl(off_t offset, void* buffer, size_t bytes) { ASSERT(offset >= 0); if (offset >= size()) @@ -50,7 +59,7 @@ namespace Kernel return to_copy; } - BAN::ErrorOr RamInode::write_impl(off_t offset, const void* buffer, size_t bytes) + BAN::ErrorOr RamFileInode::write_impl(off_t offset, const void* buffer, size_t bytes) { ASSERT(offset >= 0); if (offset + bytes > (size_t)size()) @@ -59,7 +68,7 @@ namespace Kernel return bytes; } - BAN::ErrorOr RamInode::truncate_impl(size_t new_size) + BAN::ErrorOr RamFileInode::truncate_impl(size_t new_size) { TRY(m_data.resize(new_size, 0)); m_inode_info.size = m_data.size(); @@ -75,30 +84,32 @@ namespace Kernel BAN::ErrorOr> RamDirectoryInode::create(RamFileSystem& fs, ino_t parent, mode_t mode, uid_t uid, gid_t gid) { - ASSERT(Mode(mode).ifdir()); - auto* ram_inode = new RamDirectoryInode(fs, parent, mode, uid, gid); + FullInodeInfo inode_info(fs, mode, uid, gid); + + // "." links to this + inode_info.nlink++; + + // ".." links to this or parent + if (parent) + TRY(fs.get_inode(parent))->add_link(); + else + { + inode_info.nlink++; + parent = inode_info.ino; + } + + auto* ram_inode = new RamDirectoryInode(fs, inode_info, parent); if (ram_inode == nullptr) return BAN::Error::from_errno(ENOMEM); return BAN::RefPtr::adopt(ram_inode); } - RamDirectoryInode::RamDirectoryInode(RamFileSystem& fs, ino_t parent, mode_t mode, uid_t uid, gid_t gid) - : RamInode(fs, mode, uid, gid) + RamDirectoryInode::RamDirectoryInode(RamFileSystem& fs, const FullInodeInfo& inode_info, ino_t parent) + : RamInode(fs, inode_info) + , m_parent(parent) { - // "." links to this - m_inode_info.nlink++; - - // ".." links to this, if there is no parent - if (parent == 0) - { - m_inode_info.nlink++; - m_parent = ino(); - } - else - { - MUST(fs.get_inode(parent))->add_link(); - m_parent = parent; - } + ASSERT((m_inode_info.mode & Inode::Mode::TYPE_MASK) == 0); + m_inode_info.mode |= Inode::Mode::IFDIR; } BAN::ErrorOr> RamDirectoryInode::find_inode_impl(BAN::StringView name) @@ -181,7 +192,7 @@ namespace Kernel { BAN::RefPtr inode; if (Mode(mode).ifreg()) - inode = TRY(RamInode::create(m_fs, mode, uid, gid)); + inode = TRY(RamFileInode::create(m_fs, mode, uid, gid)); else if (Mode(mode).ifdir()) inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode, uid, gid)); else @@ -222,20 +233,26 @@ namespace Kernel */ - BAN::ErrorOr> RamSymlinkInode::create(RamFileSystem& fs, BAN::StringView target, mode_t mode, uid_t uid, gid_t gid) + BAN::ErrorOr> RamSymlinkInode::create(RamFileSystem& fs, BAN::StringView target_sv, mode_t mode, uid_t uid, gid_t gid) { - ASSERT(Mode(mode).iflnk()); - auto* ram_inode = new RamSymlinkInode(fs, mode, uid, gid); + FullInodeInfo inode_info(fs, mode, uid, gid); + + BAN::String target_str; + TRY(target_str.append(target_sv)); + + auto* ram_inode = new RamSymlinkInode(fs, inode_info, BAN::move(target_str)); if (ram_inode == nullptr) return BAN::Error::from_errno(ENOMEM); - auto ref_ptr = BAN::RefPtr::adopt(ram_inode); - TRY(ref_ptr->set_link_target(target)); - return ref_ptr; + return BAN::RefPtr::adopt(ram_inode); } - RamSymlinkInode::RamSymlinkInode(RamFileSystem& fs, mode_t mode, uid_t uid, gid_t gid) - : RamInode(fs, mode, uid, gid) - { } + RamSymlinkInode::RamSymlinkInode(RamFileSystem& fs, const FullInodeInfo& inode_info, BAN::String&& target) + : RamInode(fs, inode_info) + , m_target(BAN::move(target)) + { + ASSERT((m_inode_info.mode & Inode::Mode::TYPE_MASK) == 0); + m_inode_info.mode |= Inode::Mode::IFLNK; + } BAN::ErrorOr RamSymlinkInode::link_target_impl() { diff --git a/kernel/kernel/FS/VirtualFileSystem.cpp b/kernel/kernel/FS/VirtualFileSystem.cpp index f5fd20806b..de82f939be 100644 --- a/kernel/kernel/FS/VirtualFileSystem.cpp +++ b/kernel/kernel/FS/VirtualFileSystem.cpp @@ -26,14 +26,10 @@ namespace Kernel s_instance->m_root_fs = MUST(Ext2FS::create(*(Partition*)partition_inode.ptr())); Credentials root_creds { 0, 0, 0, 0 }; - MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev")); + MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev"sv)); - mode_t tmpfs_mode = Inode::Mode::IFDIR - | Inode::Mode::IRUSR | Inode::Mode::IWUSR | Inode::Mode::IXUSR - | Inode::Mode::IRGRP | Inode::Mode::IWGRP | Inode::Mode::IXGRP - | Inode::Mode::IROTH | Inode::Mode::IWOTH | Inode::Mode::IXOTH; - auto* tmpfs = MUST(RamFileSystem::create(1024 * 1024, tmpfs_mode, 0, 0)); - MUST(s_instance->mount(root_creds, tmpfs, "/tmp")); + auto* tmpfs = MUST(RamFileSystem::create(1024 * 1024, 0777, 0, 0)); + MUST(s_instance->mount(root_creds, tmpfs, "/tmp"sv)); } VirtualFileSystem& VirtualFileSystem::get() diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index cdd9f3f6e0..2453bd6e2a 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -32,7 +32,7 @@ namespace Kernel if (inode_or_error.is_error()) { if (inode_or_error.error().get_error_code() == ENOENT) - DevFileSystem::get().add_device("tty"sv, MUST(RamSymlinkInode::create(DevFileSystem::get(), s_tty->name(), S_IFLNK | 0666, 0, 0))); + DevFileSystem::get().add_device("tty"sv, MUST(RamSymlinkInode::create(DevFileSystem::get(), s_tty->name(), 0666, 0, 0))); else dwarnln("{}", inode_or_error.error()); return;