aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/vfs.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/src/filesystem/vfs.cpp')
-rw-r--r--kernel/src/filesystem/vfs.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/kernel/src/filesystem/vfs.cpp b/kernel/src/filesystem/vfs.cpp
new file mode 100644
index 0000000..2123fc7
--- /dev/null
+++ b/kernel/src/filesystem/vfs.cpp
@@ -0,0 +1,123 @@
+#include "kernel/filesystem/vfs.hpp"
+
+#include "kapi/system.hpp"
+
+#include "kernel/devices/device.hpp"
+#include "kernel/devices/storage/storage_management.hpp"
+#include "kernel/filesystem/custody.hpp"
+#include "kernel/filesystem/device_file.hpp"
+#include "kernel/filesystem/ext2/ext2_filesystem.hpp"
+#include "kernel/filesystem/inode.hpp"
+#include "kernel/filesystem/inode_file.hpp"
+#include "kernel/filesystem/mount.hpp"
+#include "kernel/filesystem/open_file_description.hpp"
+
+#include <kstd/cstring>
+#include <kstd/memory>
+
+#include <algorithm>
+#include <optional>
+#include <string_view>
+
+namespace filesystem
+{
+ namespace
+ {
+ constinit auto static active_vfs = std::optional<vfs>{};
+ } // namespace
+
+ auto vfs::init() -> void
+ {
+ if (active_vfs)
+ {
+ kapi::system::panic("[FILESYSTEM] vfs has already been initialized.");
+ }
+
+ active_vfs.emplace(vfs{});
+
+ auto storage_mgmt = devices::storage::storage_management::get();
+ if (auto boot_device = storage_mgmt.determine_boot_device())
+ {
+ active_vfs->m_root_fs = kstd::make_shared<ext2::ext2_filesystem>();
+ if (active_vfs->m_root_fs->mount(boot_device) != 0)
+ {
+ kapi::system::panic("[FILESYSTEM] Failed to mount root filesystem.");
+ }
+
+ active_vfs->m_root_mount = mount{"/", active_vfs->m_root_fs};
+
+ std::ranges::for_each(storage_mgmt.all_controllers(), [&](auto controller) {
+ std::ranges::for_each(controller->all_devices(), [&](auto device) { active_vfs->make_device_node(device); });
+ });
+ }
+ else
+ {
+ // TODO BA-FS26 ?? what when no boot_device == no modules loaded??
+ }
+ }
+
+ auto vfs::get() -> vfs &
+ {
+ if (!active_vfs)
+ {
+ kapi::system::panic("[FILESYSTEM] vfs has not been initialized.");
+ }
+
+ return *active_vfs;
+ }
+
+ auto vfs::open(std::string_view path) -> std::optional<open_file_description>
+ {
+ if (auto custody = resolve_path(path))
+ {
+ auto node = custody->get_inode();
+ if (node->is_device())
+ {
+ auto current_device_file = kstd::make_shared<device_file>(node->backing_device());
+ current_device_file->open();
+ return open_file_description{current_device_file};
+ }
+
+ auto current_inode_file = kstd::make_shared<inode_file>(node);
+ current_inode_file->open();
+ return open_file_description{current_inode_file};
+ }
+
+ return std::nullopt;
+ }
+
+ auto vfs::make_device_node(kstd::shared_ptr<devices::device> const & device) -> void
+ {
+ if (!device)
+ {
+ kapi::system::panic("[FILESYSTEM] make_device_node called with null device.");
+ }
+
+ m_device_nodes.push_back(device_node_entry{device->name(), kstd::make_shared<inode>(device)});
+ }
+
+ auto vfs::resolve_path(std::string_view path) -> std::optional<custody>
+ {
+ // TODO BA-FS26 implement real path resolution with mounts and directories etc.
+ // For now, just support device nodes at /dev/<device_name>.
+
+ constexpr auto device_prefix = std::string_view{"/dev/"};
+ if (path.starts_with(device_prefix))
+ {
+ 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;
+ }
+
+ return std::nullopt;
+ }
+
+} // namespace filesystem \ No newline at end of file