diff options
| -rw-r--r-- | kernel/kernel/vfs.cpp | 20 | ||||
| -rw-r--r-- | kernel/kernel/vfs/mount_table.cpp | 12 | ||||
| -rw-r--r-- | kernel/kernel/vfs/mount_table.hpp | 15 | ||||
| -rw-r--r-- | kernel/kernel/vfs/mount_table.tests.cpp | 21 |
4 files changed, 21 insertions, 47 deletions
diff --git a/kernel/kernel/vfs.cpp b/kernel/kernel/vfs.cpp index 1f996fb4..5f729406 100644 --- a/kernel/kernel/vfs.cpp +++ b/kernel/kernel/vfs.cpp @@ -220,25 +220,7 @@ namespace kernel::vfs return kstd::failure(errc::invalid_path); } - auto remove_result = m_mount_table.remove_mount(path); - if (remove_result == mount_table::operation_result::removed) - { - return kstd::success(); - } - else if (remove_result == mount_table::operation_result::mount_not_found) - { - return kstd::failure(errc::mount_point_not_found); - } - else if (remove_result == mount_table::operation_result::cannot_be_unmounted) - { - return kstd::failure(errc::mount_busy); - } - else if (remove_result == mount_table::operation_result::has_child_mounts) - { - return kstd::failure(errc::has_child_mounts); - } - - return kstd::failure(errc::unmount_failed); + return m_mount_table.remove_mount(path); } auto vfs::mkdir(std::string_view path) -> kstd::result<void> diff --git a/kernel/kernel/vfs/mount_table.cpp b/kernel/kernel/vfs/mount_table.cpp index 2b494040..1609bc36 100644 --- a/kernel/kernel/vfs/mount_table.cpp +++ b/kernel/kernel/vfs/mount_table.cpp @@ -1,9 +1,11 @@ #include <kernel/vfs/mount_table.hpp> #include <kernel/vfs/dentry.hpp> +#include <kernel/vfs/error.hpp> #include <kernel/vfs/mount.hpp> #include <kstd/memory.hpp> +#include <kstd/result.hpp> #include <kstd/vector.hpp> #include <algorithm> @@ -33,22 +35,22 @@ namespace kernel::vfs } } - auto mount_table::remove_mount(std::string_view path) -> operation_result + auto mount_table::remove_mount(std::string_view path) -> kstd::result<void> { auto mount_it = find_mount_iterator(path); if (mount_it == m_mounts.end()) { - return operation_result::mount_not_found; + return kstd::failure(errc::not_mounted); } auto const & mount = *mount_it; if (!mount->is_ready_to_unmount()) { - return operation_result::cannot_be_unmounted; + return kstd::failure(errc::mount_busy); } if (has_child_mounts(mount)) { - return operation_result::has_child_mounts; + return kstd::failure(errc::has_child_mounts); } if (auto source_mount = mount->source_mount()) @@ -62,7 +64,7 @@ namespace kernel::vfs } m_mounts.erase(mount_it); - return operation_result::removed; + return kstd::success(); } auto mount_table::find_mount(std::string_view path) const -> kstd::shared_ptr<mount> diff --git a/kernel/kernel/vfs/mount_table.hpp b/kernel/kernel/vfs/mount_table.hpp index 86810a16..cb44c533 100644 --- a/kernel/kernel/vfs/mount_table.hpp +++ b/kernel/kernel/vfs/mount_table.hpp @@ -4,6 +4,7 @@ #include <kernel/vfs/mount.hpp> #include <kstd/memory.hpp> +#include <kstd/result.hpp> #include <kstd/vector.hpp> #include <string_view> @@ -13,15 +14,6 @@ namespace kernel::vfs //! A table managing all mounted filesystems. struct mount_table { - //! Results for mount table operations. - enum class operation_result : int - { - removed = 0, - has_child_mounts = -1, - mount_not_found = -2, - cannot_be_unmounted = -3 - }; - //! Add a mount to the table. //! //! @param mount The mount to add. @@ -30,9 +22,8 @@ namespace kernel::vfs //! Remove the topmost mount at the given @p path. //! //! @param path The mount path to remove. - //! @return The result of the removal operation. - [[nodiscard]] auto remove_mount(std::string_view path) - -> operation_result; // TODO: replace return type with kstd::result + //! @return Nothing on success, an error otherwise. + [[nodiscard]] auto remove_mount(std::string_view path) -> kstd::result<void>; //! Find the mount with the exact mount path matching the given path. //! diff --git a/kernel/kernel/vfs/mount_table.tests.cpp b/kernel/kernel/vfs/mount_table.tests.cpp index e8f3ec4a..52ca9313 100644 --- a/kernel/kernel/vfs/mount_table.tests.cpp +++ b/kernel/kernel/vfs/mount_table.tests.cpp @@ -2,8 +2,7 @@ #include <kernel/test_support/filesystems/filesystem.hpp> #include <kernel/test_support/filesystems/inode.hpp> -#include <kernel/vfs/dentry.hpp> -#include <kernel/vfs/mount.hpp> +#include <kernel/vfs.hpp> #include <kstd/memory.hpp> #include <kstd/print.hpp> @@ -21,8 +20,8 @@ SCENARIO("Mount table construction", "[filesystem][mount_table]") THEN("removing any mount returns mount_not_found") { - REQUIRE(table.remove_mount("/") == kernel::vfs::mount_table::operation_result::mount_not_found); - REQUIRE(table.remove_mount("/any/path") == kernel::vfs::mount_table::operation_result::mount_not_found); + REQUIRE(table.remove_mount("/").error() == kernel::vfs::errc::not_mounted); + REQUIRE(table.remove_mount("/any/path").error() == kernel::vfs::errc::not_mounted); } } } @@ -70,13 +69,13 @@ SCENARIO("Adding, finding and removing mounts in the mount table", "[filesystem] THEN("removing a mount that has no child mounts succeeds") { - REQUIRE(table.remove_mount("/mnt") == kernel::vfs::mount_table::operation_result::removed); + REQUIRE(table.remove_mount("/mnt")); REQUIRE_FALSE(root_dentry2->has_flag(kernel::vfs::dentry::dentry_flags::is_mount_point)); } THEN("removing a mount that does not exist returns mount_not_found") { - REQUIRE(table.remove_mount("/nonexistent") == kernel::vfs::mount_table::operation_result::mount_not_found); + REQUIRE(table.remove_mount("/nonexistent").error() == kernel::vfs::errc::not_mounted); } } @@ -108,7 +107,7 @@ SCENARIO("Adding, finding and removing mounts in the mount table", "[filesystem] THEN("removing the topmost mount with the same path succeeds") { - REQUIRE(table.remove_mount("/") == kernel::vfs::mount_table::operation_result::removed); + REQUIRE(table.remove_mount("/")); REQUIRE_FALSE(root_dentry2->has_flag(kernel::vfs::dentry::dentry_flags::is_mount_point)); } } @@ -144,13 +143,13 @@ SCENARIO("Adding, finding and removing mounts in the mount table", "[filesystem] THEN("removing a mount with child mounts returns has_child_mounts") { - REQUIRE(table.remove_mount("/") == kernel::vfs::mount_table::operation_result::has_child_mounts); - REQUIRE(table.remove_mount("/mnt") == kernel::vfs::mount_table::operation_result::has_child_mounts); + REQUIRE(table.remove_mount("/").error() == kernel::vfs::errc::has_child_mounts); + REQUIRE(table.remove_mount("/mnt").error() == kernel::vfs::errc::has_child_mounts); } THEN("removing a leaf mount succeeds") { - REQUIRE(table.remove_mount("/mnt/submnt") == kernel::vfs::mount_table::operation_result::removed); + REQUIRE(table.remove_mount("/mnt/submnt")); REQUIRE_FALSE(root_dentry3->has_flag(kernel::vfs::dentry::dentry_flags::is_mount_point)); } } @@ -177,7 +176,7 @@ SCENARIO("Mount reference counting", "[filesystem][mount_table]") table.add_mount(*mount); REQUIRE((*source_mount)->ref_count() == 1); - REQUIRE(table.remove_mount("/") == kernel::vfs::mount_table::operation_result::removed); + REQUIRE(table.remove_mount("/")); REQUIRE((*source_mount)->ref_count() == 0); } } |
