aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/vfs.cpp
diff options
context:
space:
mode:
authorMarcel Braun <marcel.braun@ost.ch>2026-04-12 19:15:38 +0200
committerMarcel Braun <marcel.braun@ost.ch>2026-04-12 19:15:38 +0200
commit4d2a1d028f8ba28b655026b93124e71a12562619 (patch)
treef49deef4dd3e8728fd1000b04c0908966f37663f /kernel/src/filesystem/vfs.cpp
parent21fd1281cf19572e202d583689b99c33ec68da50 (diff)
parentcb7edbe6d4454ee5b217b522f62f4a7b92475a32 (diff)
downloadteachos-4d2a1d028f8ba28b655026b93124e71a12562619.tar.xz
teachos-4d2a1d028f8ba28b655026b93124e71a12562619.zip
Merge branch 'ext2' into 'develop-BA-FS26'HEADdevelop-BA-FS26
ext2 and tests See merge request teachos/kernel!22
Diffstat (limited to 'kernel/src/filesystem/vfs.cpp')
-rw-r--r--kernel/src/filesystem/vfs.cpp74
1 files changed, 48 insertions, 26 deletions
diff --git a/kernel/src/filesystem/vfs.cpp b/kernel/src/filesystem/vfs.cpp
index 06214d2..67d1af2 100644
--- a/kernel/src/filesystem/vfs.cpp
+++ b/kernel/src/filesystem/vfs.cpp
@@ -5,9 +5,9 @@
#include "kernel/devices/storage/management.hpp"
#include "kernel/filesystem/dentry.hpp"
#include "kernel/filesystem/devfs/filesystem.hpp"
-#include "kernel/filesystem/ext2/filesystem.hpp"
#include "kernel/filesystem/filesystem.hpp"
#include "kernel/filesystem/mount.hpp"
+#include "kernel/filesystem/mount_table.hpp"
#include "kernel/filesystem/open_file_description.hpp"
#include "kernel/filesystem/rootfs/filesystem.hpp"
@@ -17,13 +17,13 @@
#include <ranges>
#include <string_view>
-namespace kernel::filesystem
+namespace
{
- namespace
- {
- constinit auto static active_vfs = std::optional<vfs>{};
- } // namespace
+ constinit auto static active_vfs = std::optional<kernel::filesystem::vfs>{};
+} // namespace
+namespace kernel::filesystem
+{
auto vfs::init() -> void
{
if (active_vfs)
@@ -41,15 +41,16 @@ namespace kernel::filesystem
root_fs->mount(nullptr);
auto root_fs_root_dentry = kstd::make_shared<dentry>(nullptr, root_fs->root_inode());
- m_mount_table.add_mount(kstd::make_shared<mount>(nullptr, root_fs_root_dentry, root_fs, ""));
+ m_mount_table.add_mount(kstd::make_shared<mount>(nullptr, root_fs_root_dentry, root_fs, "", nullptr));
auto storage_mgmt = devices::storage::management::get();
if (auto boot_device = storage_mgmt.determine_boot_device())
{
- // TODO BA-FS26 detect fs type from boot device and load corresponding fs, for now just assume ext2
- auto boot_root_fs = kstd::make_shared<ext2::filesystem>();
- boot_root_fs->mount(boot_device);
- do_mount_internal("/", root_fs_root_dentry, boot_root_fs);
+ auto boot_root_fs = kernel::filesystem::filesystem::probe_and_mount(boot_device);
+ if (boot_root_fs)
+ {
+ do_mount_internal("/", root_fs_root_dentry, boot_root_fs);
+ }
}
auto device_fs = kstd::make_shared<devfs::filesystem>();
@@ -77,41 +78,55 @@ namespace kernel::filesystem
return nullptr;
}
- auto vfs::do_mount(std::string_view path, kstd::shared_ptr<filesystem> const & filesystem) -> int
+ auto vfs::do_mount(std::string_view path, kstd::shared_ptr<filesystem> const & filesystem) -> operation_result
{
if (!filesystem)
{
- return -1; // TODO BA-FS26 panic or errorcode?
+ return operation_result::filesystem_null;
}
- if (path.empty() || path.front() != '/')
+ if (path.empty() || path.front() != '/' || (path.size() > 1 && path.back() == '/'))
{
- return -1; // TODO BA-FS26 panic or errorcode?
+ return operation_result::invalid_path;
}
- // TODO BA-FS26 better path validation
- if ((path.size() > 1 && path.back() == '/'))
+ if (auto mount_point_dentry = resolve_path(path))
{
- return -1; // TODO BA-FS26 panic or errorcode?
+ do_mount_internal(path, mount_point_dentry, filesystem);
+ return operation_result::success;
}
- if (auto mount_point_dentry = resolve_path(path))
+ return operation_result::mount_point_not_found;
+ }
+
+ auto vfs::unmount(std::string_view path) -> operation_result
+ {
+ if (path.empty() || path.front() != '/' || (path.size() > 1 && path.back() == '/'))
{
- do_mount_internal(path, mount_point_dentry, filesystem);
- return 0;
+ return operation_result::invalid_path;
+ }
+
+ auto remove_result = m_mount_table.remove_mount(path);
+ if (remove_result == mount_table::operation_result::removed)
+ {
+ return operation_result::success;
}
- return -1;
+ if (remove_result == mount_table::operation_result::has_child_mounts)
+ {
+ return operation_result::unmount_failed;
+ }
+
+ return operation_result::mount_point_not_found;
}
auto vfs::do_mount_internal(std::string_view path, kstd::shared_ptr<dentry> const & mount_point_dentry,
kstd::shared_ptr<filesystem> const & fs) -> void
{
- // TODO BA-FS26 check if mount point is already mounted and handle it (unmount old fs, fail, etc.)
+ auto parent_mount = m_mount_table.find_longest_prefix_mount(path);
auto new_fs_root = kstd::make_shared<dentry>(mount_point_dentry, fs->root_inode());
- auto new_mount = kstd::make_shared<mount>(mount_point_dentry, new_fs_root, fs, path);
+ auto new_mount = kstd::make_shared<mount>(mount_point_dentry, new_fs_root, fs, path, parent_mount);
m_mount_table.add_mount(new_mount);
- mount_point_dentry->set_flag(dentry::dentry_flags::dcache_mounted);
}
auto vfs::resolve_path(std::string_view path) -> kstd::shared_ptr<dentry>
@@ -160,5 +175,12 @@ namespace kernel::filesystem
return current_dentry;
}
+} // namespace kernel::filesystem
-} // namespace kernel::filesystem \ No newline at end of file
+namespace kernel::tests::filesystem::vfs
+{
+ auto deinit() -> void
+ {
+ active_vfs.reset();
+ }
+} // namespace kernel::tests::filesystem::vfs