aboutsummaryrefslogtreecommitdiff
path: root/kernel/kernel/filesystem/vfs.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/kernel/filesystem/vfs.hpp')
-rw-r--r--kernel/kernel/filesystem/vfs.hpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/kernel/kernel/filesystem/vfs.hpp b/kernel/kernel/filesystem/vfs.hpp
new file mode 100644
index 00000000..ddc9a9bc
--- /dev/null
+++ b/kernel/kernel/filesystem/vfs.hpp
@@ -0,0 +1,113 @@
+#ifndef TEACH_OS_KERNEL_FILESYSTEM_VFS_HPP
+#define TEACH_OS_KERNEL_FILESYSTEM_VFS_HPP
+
+#include <kernel/filesystem/dentry.hpp>
+#include <kernel/filesystem/devfs/filesystem.hpp>
+#include <kernel/filesystem/filesystem.hpp>
+#include <kernel/filesystem/mount.hpp>
+#include <kernel/filesystem/mount_table.hpp>
+
+#include <kstd/memory>
+
+#include <string_view>
+#include <utility>
+
+namespace kernel::filesystem
+{
+ /**
+ @brief The virtual filesystem (VFS) is responsible for managing mounted filesystems and providing a unified interface
+ for file operations across different filesystem types. The VFS maintains a mount table to keep track of mounted
+ filesystems and their associated mount points. It provides methods for opening files by path, which involvesresolving
+ the path to the appropriate mounted filesystem and delegating the file operation to that filesystem's implementation.
+ */
+ struct vfs
+ {
+ /**
+ @brief Results for VFS operations.
+ */
+ enum class operation_result : int
+ {
+ success = 0,
+ invalid_path = -1,
+ non_existent_path = -2,
+ mount_point_not_found = -3,
+ unmount_failed = -4,
+ invalid_filesystem = -5
+ };
+
+ vfs();
+
+ /**
+ @brief Initialize the virtual filesystem.
+ @warning Panics if the VFS has already been initialized.
+ */
+ auto static init() -> void;
+
+ /**
+ @brief Get the singleton instance of the virtual filesystem.
+ @return A reference to the VFS instance.
+ @warning Panics if the VFS has not been initialized yet.
+ */
+ auto static get() -> vfs &;
+
+ /**
+ @brief Destructor for the VFS.
+ */
+ ~vfs() = default;
+
+ /**
+ @brief Open a file by its @p path. This method resolves the path and returns the corresponding dentry.
+ @param path The path to the file to open.
+ @return A shared pointer to the dentry or a null pointer if the file could not be opened.
+ */
+ auto open(std::string_view path) -> kstd::shared_ptr<dentry>;
+
+ /**
+ @brief Close a file by its associated @p path.
+ @param path The path to the file to close.
+ @return The result of the close operation.
+ */
+ auto close(std::string_view path) -> operation_result;
+
+ /**
+ @brief Mount a @p source path to a specific @p target path.
+ @param source The source of the filesystem to mount.
+ @param target The path where the filesystem should be mounted.
+ @return The result of the mount operation.
+ */
+ auto do_mount(std::string_view source, std::string_view target) -> operation_result;
+
+ /**
+ @brief Unmount the filesystem mounted at the specified @p path.
+ @param path The path where the filesystem is mounted.
+ @return The result of the unmount operation.
+ */
+ auto unmount(std::string_view path) -> operation_result;
+
+ private:
+ /**
+ * Note: Resolving a dentry requires traversing mount points; since the
+ * associated 'mount' object is discovered as a byproduct of this
+ * traversal, we return it alongside the dentry to avoid redundant
+ * lookups in callers that require mount context.
+ *
+ * If only one component is needed, the convenience wrappers can be used:
+ * - resolve_path() for the dentry only.
+ * - find_mount() for the mount context only.
+ */
+ [[nodiscard]] auto resolve_path_internal(std::string_view path) const
+ -> std::pair<kstd::shared_ptr<dentry>, kstd::shared_ptr<mount>>;
+ [[nodiscard]] auto resolve_path(std::string_view path) const -> kstd::shared_ptr<dentry>;
+ [[nodiscard]] auto find_mount(std::string_view path) const -> kstd::shared_ptr<mount>;
+
+ auto do_mount_internal(kstd::shared_ptr<dentry> const & mount_point_dentry,
+ kstd::shared_ptr<mount> const & parent_mount, kstd::shared_ptr<filesystem> const & fs,
+ kstd::shared_ptr<mount> const & source_mount = nullptr) -> void;
+
+ auto graft_persistent_device_fs(kstd::shared_ptr<devfs::filesystem> const & device_fs) -> void;
+
+ mount_table m_mount_table{};
+ };
+} // namespace kernel::filesystem
+
+#endif \ No newline at end of file