From 760752ef2045aaceb0393911a0919f9bc0104282 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Sun, 15 Mar 2026 20:37:14 +0100 Subject: implement first inode draft, fix make_device_node, implement first draft of resolve_path (currently with temp m_device_nodes in vfs -> has later to be deleted again, just for tests) --- kernel/filesystem/src/vfs.cpp | 77 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 13 deletions(-) (limited to 'kernel/filesystem/src/vfs.cpp') diff --git a/kernel/filesystem/src/vfs.cpp b/kernel/filesystem/src/vfs.cpp index 573994a..2ecf847 100644 --- a/kernel/filesystem/src/vfs.cpp +++ b/kernel/filesystem/src/vfs.cpp @@ -4,13 +4,17 @@ #include "devices/device.hpp" #include "devices/storage/storage_management.hpp" +#include "filesystem/custody.hpp" +#include "filesystem/device_file.hpp" #include "filesystem/ext2/ext2_filesystem.hpp" +#include "filesystem/inode.hpp" +#include "filesystem/inode_file.hpp" #include "filesystem/mount.hpp" +#include "filesystem/open_file_description.hpp" #include #include -#include #include #include @@ -23,12 +27,11 @@ namespace filesystem // TODO BA-FS26 @Felix better solution? while dynamic memory not available? // TODO BA-FS26 remove when dynamic memory available; constinit auto static root_fs = std::optional{}; - // TODO BA-FS26 use kstd::vector when available - constinit auto static filesystems = std::array, 1>{}; - // TODO BA-FS26 @Felix - // TODO BA-FS26 depends on the length of ram_disk_device name - constinit std::array device_mount_path = {'/', 'd', 'e', 'v', '/', 'x', 'x', 'x', 'x', '\0'}; + // TODO BA-FS26 remove when dynamic memory available; + constinit auto static temp_device_file = std::optional{}; + // TODO BA-FS26 remove when dynamic memory available; + constinit auto static temp_inode_file = std::optional{}; } // namespace auto vfs::init() -> void @@ -71,19 +74,67 @@ namespace filesystem return *active_vfs; } + auto vfs::open(std::string_view path) -> std::optional + { + if (auto custody = resolve_path(path)) + { + auto * node = custody->get_inode(); + if (node->is_device()) + { + temp_device_file.emplace(node->backing_device()); + temp_device_file->open(); + return open_file_description{&*temp_device_file}; + } + + temp_inode_file.emplace(node); + temp_inode_file->open(); + return open_file_description{&*temp_inode_file}; + } + + return std::nullopt; + } + auto vfs::make_device_node(devices::device * device) -> void { - auto device_name = device->name(); - kstd::libc::memcpy(&device_mount_path[5], device_name.data(), device_name.size()); + if (!device) + { + kapi::system::panic("[FILESYSTEM] make_device_node called with null device."); + } + + auto const device_name = device->name(); + + // TODO BA-FS26 this logic isn't needed anymore when kstd::vector available, just use push_back + auto const slot = std::ranges::find_if(m_device_nodes, [](auto const & entry) { return !entry.has_value(); }); + if (slot == m_device_nodes.end()) + { + kapi::system::panic("[FILESYSTEM] No free slot available for device nodes."); + } + + slot->emplace(device_node_entry{device_name, inode{device}}); + } + + auto vfs::resolve_path(std::string_view path) -> std::optional + { + // TODO BA-FS26 implement real path resolution with mounts and directories etc. + // For now, just support device nodes at /dev/. - // TODO BA-FS26 use kstd::vector and push_back when available - filesystems[0].emplace(ext2::ext2_filesystem{}); - if (filesystems[0]->mount(device) != 0) + constexpr auto device_prefix = std::string_view{"/dev/"}; + if (path.starts_with(device_prefix)) { - kapi::system::panic("[FILESYSTEM] Failed to mount device filesystem."); + auto const device_name = path.substr(device_prefix.size()); + auto entry = std::ranges::find_if(m_device_nodes, [&](auto const & device_entry) { + return device_entry.has_value() && device_entry->name == device_name; + }); + + if (entry != m_device_nodes.end()) + { + return custody{nullptr, &entry->value().node}; + } + + return std::nullopt; } - m_mounts[0] = mount{std::string_view{device_mount_path.data()}, &*filesystems[0]}; + return std::nullopt; } } // namespace filesystem \ No newline at end of file -- cgit v1.2.3