From 76de81de1e12694bf6bec1edd3e3409092a92d09 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Tue, 24 Mar 2026 20:41:32 +0100 Subject: refactoring, add root_mount into the root_table --- kernel/include/kernel/filesystem/mount_table.hpp | 2 + kernel/include/kernel/filesystem/vfs.hpp | 3 -- kernel/src/filesystem/mount_table.cpp | 7 ++++ kernel/src/filesystem/vfs.cpp | 53 ++++++++++++++---------- kernel/src/main.cpp | 1 - 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/kernel/include/kernel/filesystem/mount_table.hpp b/kernel/include/kernel/filesystem/mount_table.hpp index 14d6d08..cf523c1 100644 --- a/kernel/include/kernel/filesystem/mount_table.hpp +++ b/kernel/include/kernel/filesystem/mount_table.hpp @@ -13,6 +13,8 @@ namespace filesystem { public: void add_mount(kstd::shared_ptr); + + [[nodiscard]] auto get_root_mount() const -> kstd::shared_ptr; auto find_mount_by_dentry(kstd::shared_ptr const & dentry) -> kstd::shared_ptr; private: diff --git a/kernel/include/kernel/filesystem/vfs.hpp b/kernel/include/kernel/filesystem/vfs.hpp index 55bf6a4..cf268a3 100644 --- a/kernel/include/kernel/filesystem/vfs.hpp +++ b/kernel/include/kernel/filesystem/vfs.hpp @@ -5,7 +5,6 @@ #include "kernel/filesystem/dentry.hpp" #include "kernel/filesystem/filesystem.hpp" #include "kernel/filesystem/inode.hpp" -#include "kernel/filesystem/mount.hpp" #include "kernel/filesystem/mount_table.hpp" #include "kernel/filesystem/open_file_description.hpp" @@ -42,8 +41,6 @@ namespace filesystem auto make_device_node(kstd::shared_ptr const & device) -> void; [[nodiscard]] auto resolve_path(std::string_view path) -> kstd::shared_ptr; - kstd::shared_ptr m_root_fs; - kstd::shared_ptr m_root_dentry; mount_table m_mount_table; }; } // namespace filesystem diff --git a/kernel/src/filesystem/mount_table.cpp b/kernel/src/filesystem/mount_table.cpp index 3176d6d..debb9ab 100644 --- a/kernel/src/filesystem/mount_table.cpp +++ b/kernel/src/filesystem/mount_table.cpp @@ -16,6 +16,13 @@ namespace filesystem m_mounts.push_back(mount); } + auto mount_table::get_root_mount() const -> kstd::shared_ptr + { + auto it = + std::ranges::find_if(m_mounts, [](auto const & mount) { return mount->get_dentry()->get_parent() == nullptr; }); + return it != m_mounts.end() ? *it : nullptr; + } + auto mount_table::find_mount_by_dentry(kstd::shared_ptr const & dentry) -> kstd::shared_ptr { auto it = std::ranges::find_if(m_mounts, [&](auto const & mount) { return mount->get_dentry() == dentry; }); diff --git a/kernel/src/filesystem/vfs.cpp b/kernel/src/filesystem/vfs.cpp index 78dec77..c8e5bc5 100644 --- a/kernel/src/filesystem/vfs.cpp +++ b/kernel/src/filesystem/vfs.cpp @@ -41,14 +41,13 @@ namespace filesystem auto storage_mgmt = devices::storage::storage_management::get(); if (auto boot_device = storage_mgmt.determine_boot_device()) { - m_root_fs = kstd::make_shared(); - m_root_fs->mount(boot_device); + auto root_fs = kstd::make_shared(); + root_fs->mount(boot_device); - m_root_dentry = kstd::make_shared(nullptr, m_root_fs->root_inode()); - // if (do_mount("/", m_root_fs) != 0) - // { - // kapi::system::panic("[FILESYSTEM] Failed to mount root filesystem."); - // } + auto root_dentry = kstd::make_shared(nullptr, root_fs->root_inode(), "/"); + root_fs->set_root_dentry(root_dentry); + + m_mount_table.add_mount(kstd::make_shared(nullptr, root_fs)); // TODO BA-FS26 use do_mount when tempdevfs is implemented -> just call /dev/ with all devices in devtempfs std::ranges::for_each(storage_mgmt.all_controllers(), [&](auto controller) { @@ -148,37 +147,47 @@ namespace filesystem } else if (!path.empty() && path.front() == '/') { + auto root_mount = m_mount_table.get_root_mount(); + if (!root_mount) + { + kapi::system::panic("[FILESYSTEM] no root mount found."); + } + + auto current_dentry = root_mount->get_dentry(); + auto current_fs = root_mount->get_filesystem(); + auto path_parts = std::views::split(path, '/') | std::views::filter([](auto const & part) { return !part.empty(); }); - auto current_parent = m_root_dentry; - auto current_fs = m_root_fs; - for (auto const & part : path_parts) { - if (auto child = current_parent->find_child(std::string_view{part})) + auto next_dentry = current_dentry->find_child(std::string_view{part}); + + if (!next_dentry) { - if (child->has_flag(dentry::dentry_flags::dcache_mounted)) // found dentry is a mounted fs + if (auto found_inode = current_fs->lookup(current_dentry->get_inode(), std::string_view{part})) { - auto found_mount = m_mount_table.find_mount_by_dentry(child); - current_fs = found_mount->get_filesystem(); - current_parent = current_fs->root_dentry(); + next_dentry = kstd::make_shared(current_dentry, found_inode, std::string_view{part}); + current_dentry->add_child(next_dentry); } else - { // found dentry is NOT a mounted fs -> continue path resolution in current fs - current_parent = child; + { + return nullptr; } } - else if (auto found_inode = m_root_fs->lookup(current_parent->get_inode(), std::string_view{part})) + + if (next_dentry->has_flag(dentry::dentry_flags::dcache_mounted)) { - auto next_dentry = kstd::make_shared(current_parent, found_inode, std::string_view{part}); - current_parent->add_child(next_dentry); - current_parent = next_dentry; + auto found_mount = m_mount_table.find_mount_by_dentry(next_dentry); + current_fs = found_mount->get_filesystem(); + current_dentry = current_fs->root_dentry(); } else { - return nullptr; + current_dentry = next_dentry; } } + + return current_dentry; } return nullptr; diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index a575be2..9571a0d 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include -- cgit v1.2.3