aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kernel/include/kernel/filesystem/mount_table.hpp2
-rw-r--r--kernel/include/kernel/filesystem/vfs.hpp3
-rw-r--r--kernel/src/filesystem/mount_table.cpp7
-rw-r--r--kernel/src/filesystem/vfs.cpp53
-rw-r--r--kernel/src/main.cpp1
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<mount>);
+
+ [[nodiscard]] auto get_root_mount() const -> kstd::shared_ptr<mount>;
auto find_mount_by_dentry(kstd::shared_ptr<dentry> const & dentry) -> kstd::shared_ptr<mount>;
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<devices::device> const & device) -> void;
[[nodiscard]] auto resolve_path(std::string_view path) -> kstd::shared_ptr<dentry>;
- kstd::shared_ptr<filesystem> m_root_fs;
- kstd::shared_ptr<dentry> 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<mount>
+ {
+ 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<dentry> const & dentry) -> kstd::shared_ptr<mount>
{
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<ext2::ext2_filesystem>();
- m_root_fs->mount(boot_device);
+ auto root_fs = kstd::make_shared<ext2::ext2_filesystem>();
+ root_fs->mount(boot_device);
- m_root_dentry = kstd::make_shared<dentry>(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<dentry>(nullptr, root_fs->root_inode(), "/");
+ root_fs->set_root_dentry(root_dentry);
+
+ m_mount_table.add_mount(kstd::make_shared<mount>(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<dentry>(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<dentry>(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 <kstd/memory>
#include <kstd/os/error.hpp>
#include <kstd/print>
-#include <kstd/string>
#include <kstd/vector>
#include <algorithm>