diff options
| author | Marcel Braun <marcel.braun@ost.ch> | 2026-05-16 14:20:38 +0200 |
|---|---|---|
| committer | Marcel Braun <marcel.braun@ost.ch> | 2026-05-16 14:20:38 +0200 |
| commit | 106e9731aaf856f940592c02953e49a496555822 (patch) | |
| tree | f3916a9865d03ebb574bac7d5496f6ec85d638ed /kernel/include | |
| parent | d22812dbf54a9fd8ecd558a94bf4ee789caf8011 (diff) | |
| parent | 5b40e4a28307eed814adb46188c3f6783651d286 (diff) | |
| download | kernel-106e9731aaf856f940592c02953e49a496555822.tar.xz kernel-106e9731aaf856f940592c02953e49a496555822.zip | |
Merge branch 'mount-reference-count' into 'develop-BA-FS26'
Mount reference count
See merge request teachos/kernel!37
Diffstat (limited to 'kernel/include')
| -rw-r--r-- | kernel/include/kernel/filesystem/dentry.hpp | 11 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/mount.hpp | 26 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/mount_table.hpp | 7 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/open_file_descriptor.hpp | 20 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/rootfs/filesystem.hpp | 2 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/rootfs/inode.hpp | 26 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/vfs.hpp | 29 |
7 files changed, 75 insertions, 46 deletions
diff --git a/kernel/include/kernel/filesystem/dentry.hpp b/kernel/include/kernel/filesystem/dentry.hpp index 7eef693..925768a 100644 --- a/kernel/include/kernel/filesystem/dentry.hpp +++ b/kernel/include/kernel/filesystem/dentry.hpp @@ -24,8 +24,7 @@ namespace kernel::filesystem */ enum class dentry_flags : uint32_t { - is_mount_point = 1 << 0, - is_mount_root = 1 << 1, + is_mount_point = 1 << 0 }; /** @@ -62,12 +61,6 @@ namespace kernel::filesystem [[nodiscard]] auto get_absolute_path() const -> kstd::string; /** - @brief traverse parent dentries until dentry with is_mount_root flag is found. - @return The found dentry. - */ - [[nodiscard]] auto find_mount_root_dentry() const -> kstd::shared_ptr<dentry>; - - /** @brief Add a @p child dentry. @param child The child dentry to add. */ @@ -104,7 +97,7 @@ namespace kernel::filesystem kstd::shared_ptr<dentry> m_parent; kstd::vector<kstd::shared_ptr<dentry>> m_children; kstd::shared_ptr<inode> m_inode; - uint32_t m_flags{0}; + uint32_t m_flags; }; } // namespace kernel::filesystem diff --git a/kernel/include/kernel/filesystem/mount.hpp b/kernel/include/kernel/filesystem/mount.hpp index f920891..5d8ea69 100644 --- a/kernel/include/kernel/filesystem/mount.hpp +++ b/kernel/include/kernel/filesystem/mount.hpp @@ -8,6 +8,7 @@ #include <kstd/string> #include <atomic> +#include <cstddef> namespace kernel::filesystem { @@ -54,12 +55,35 @@ namespace kernel::filesystem */ [[nodiscard]] auto get_parent_mount() const -> kstd::shared_ptr<mount> const &; + /** + @brief Increment the reference count for this mount. + */ + auto increment_ref_count() -> void; + + /** + @brief Decrement the reference count for this mount. + @return True if the reference count reached zero, false otherwise. + */ + auto decrement_ref_count() -> bool; + + /** + @brief Check if the mount is ready to be unmounted. + @return True if the mount is ready to be unmounted, false otherwise. + */ + [[nodiscard]] auto is_ready_to_unmount() const -> bool; + + /** + @brief Get the current reference count for this mount. + @return The current reference count. + */ + [[nodiscard]] auto get_ref_count() const -> size_t; + private: kstd::shared_ptr<dentry> m_mount_dentry; kstd::shared_ptr<dentry> m_root_dentry; kstd::shared_ptr<filesystem> m_filesystem{}; kstd::shared_ptr<mount> m_parent_mount{}; - std::atomic_uint32_t m_ref_count{0}; // TODO BA-FS26 + std::atomic_size_t m_ref_count; }; } // namespace kernel::filesystem diff --git a/kernel/include/kernel/filesystem/mount_table.hpp b/kernel/include/kernel/filesystem/mount_table.hpp index 59b9503..4f2d1b7 100644 --- a/kernel/include/kernel/filesystem/mount_table.hpp +++ b/kernel/include/kernel/filesystem/mount_table.hpp @@ -22,7 +22,8 @@ namespace kernel::filesystem { removed = 0, has_child_mounts = -1, - mount_not_found = -2 + mount_not_found = -2, + cannot_be_unmounted = -3 }; /** @@ -43,10 +44,12 @@ namespace kernel::filesystem @param path The path to match against the mount paths in the table. @return A pointer to the mount with the exact matching path, or a null pointer if no mount matches the path. */ - [[nodiscard]] auto find_exact_mount(std::string_view path) const -> kstd::shared_ptr<mount>; + [[nodiscard]] auto find_mount(std::string_view path) const -> kstd::shared_ptr<mount>; private: [[nodiscard]] auto has_child_mounts(kstd::shared_ptr<mount> const & parent_mount) const -> bool; + [[nodiscard]] auto find_mount_iterator(std::string_view path) const + -> kstd::vector<kstd::shared_ptr<mount>>::const_iterator; kstd::vector<kstd::shared_ptr<mount>> m_mounts; }; diff --git a/kernel/include/kernel/filesystem/open_file_descriptor.hpp b/kernel/include/kernel/filesystem/open_file_descriptor.hpp index 036dcf0..7ca7350 100644 --- a/kernel/include/kernel/filesystem/open_file_descriptor.hpp +++ b/kernel/include/kernel/filesystem/open_file_descriptor.hpp @@ -1,7 +1,7 @@ #ifndef TEACH_OS_KERNEL_FILESYSTEM_OPEN_FILE_DESCRIPTOR_HPP #define TEACH_OS_KERNEL_FILESYSTEM_OPEN_FILE_DESCRIPTOR_HPP -#include <kernel/filesystem/inode.hpp> +#include <kernel/filesystem/dentry.hpp> #include <kstd/memory> @@ -11,15 +11,15 @@ namespace kernel::filesystem { /** @brief Represents an open file descriptor in the filesystem. This class encapsulates the state of an open file, - including a reference to the associated inode and the current file offset. + including a reference to the associated dentry and the current file offset. */ struct open_file_descriptor { /** - @brief Constructs an open file descriptor for the given @p inode. - @param inode The inode to associate with the open file descriptor. + @brief Constructs an open file descriptor for the given @p dentry. + @param dentry The dentry to associate with the open file descriptor. */ - explicit open_file_descriptor(kstd::shared_ptr<inode> const & inode); + explicit open_file_descriptor(kstd::shared_ptr<dentry> const & dentry); /** @brief Destructor for the open file descriptor. @@ -50,10 +50,16 @@ namespace kernel::filesystem @brief Returns the current file offset for this open file descriptor. @return The current file offset in bytes. */ - [[nodiscard]] auto offset() const -> size_t; + [[nodiscard]] auto get_offset() const -> size_t; + + /** + @brief Return a reference to the dentry associated with this open file descriptor. + @return A reference to the associated dentry. + */ + [[nodiscard]] auto get_dentry() const -> kstd::shared_ptr<dentry> const &; private: - kstd::shared_ptr<inode> m_inode; + kstd::shared_ptr<dentry> m_dentry; size_t m_offset; }; diff --git a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp index cc778d8..f99440b 100644 --- a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp +++ b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp @@ -31,7 +31,7 @@ namespace kernel::filesystem::rootfs @brief Looks up an inode by @p name within a @p parent directory. @param parent The parent directory inode. @param name The name of the inode to look up. - @return A pointer to the found inode, or a null pointer if not found. + @return Always returns nullptr. */ auto lookup(kstd::shared_ptr<kernel::filesystem::inode> const & parent, std::string_view name) -> kstd::shared_ptr<kernel::filesystem::inode> override; diff --git a/kernel/include/kernel/filesystem/rootfs/inode.hpp b/kernel/include/kernel/filesystem/rootfs/inode.hpp index 58035ea..2671207 100644 --- a/kernel/include/kernel/filesystem/rootfs/inode.hpp +++ b/kernel/include/kernel/filesystem/rootfs/inode.hpp @@ -8,16 +8,10 @@ #include <kstd/vector> #include <cstddef> -#include <string_view> -#include <utility> - namespace kernel::filesystem::rootfs { /** - @brief Represents an inode in the rootfs filesystem. This inode represents a directory in the root filesystem and - maintains a list of child inodes corresponding to files and subdirectories within the root directory. The rootfs inode - provides methods for reading and writing data (which are no-ops for the root directory), as well as adding and looking - up child inodes by name. + @brief Represents an inode in the rootfs filesystem. */ struct inode : kernel::filesystem::inode { @@ -40,26 +34,10 @@ namespace kernel::filesystem::rootfs auto write(void const * buffer, size_t offset, size_t size) -> size_t override; /** - @brief Adds a child inode to the rootfs directory inode with the specified @p name. - @param name The name of the child inode. - */ - auto add_child(std::string_view name) -> void; - - /** - @brief Looks up a child inode by @p name. - @param name The name of the child inode to look up. - @return A pointer to the found child inode, or a null pointer if not found. - */ - auto lookup_child(std::string_view name) -> kstd::shared_ptr<inode>; - - /** @brief Check if this inode represents a directory. - @return returns true, since this inode represents the root directory in the rootfs filesystem. + @return returns true, since this inode represents the / directory in the rootfs filesystem. */ [[nodiscard]] auto is_directory() const -> bool override; - - private: - kstd::vector<std::pair<kstd::string, kstd::shared_ptr<inode>>> m_children; }; } // namespace kernel::filesystem::rootfs diff --git a/kernel/include/kernel/filesystem/vfs.hpp b/kernel/include/kernel/filesystem/vfs.hpp index b5053a2..0058d04 100644 --- a/kernel/include/kernel/filesystem/vfs.hpp +++ b/kernel/include/kernel/filesystem/vfs.hpp @@ -1,14 +1,16 @@ #ifndef TEACH_OS_KERNEL_FILESYSTEM_VFS_HPP #define TEACH_OS_KERNEL_FILESYSTEM_VFS_HPP -#include "kernel/filesystem/devfs/filesystem.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 { @@ -31,6 +33,7 @@ namespace kernel::filesystem mount_point_not_found = -3, unmount_failed = -4, invalid_filesystem = -5, + close_failed = -6 }; /** @@ -59,6 +62,13 @@ namespace kernel::filesystem 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. @@ -77,8 +87,23 @@ namespace kernel::filesystem vfs() = default; auto init_internal() -> void; + /** + * 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) + -> std::pair<kstd::shared_ptr<dentry>, kstd::shared_ptr<mount>>; [[nodiscard]] auto resolve_path(std::string_view path) -> kstd::shared_ptr<dentry>; - auto do_mount_internal(kstd::shared_ptr<dentry> const & mount_point_dentry, kstd::shared_ptr<filesystem> const & fs) + [[nodiscard]] auto find_mount(std::string_view path) -> 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) -> void; auto graft_persistent_device_fs(kstd::shared_ptr<devfs::filesystem> const & device_fs) -> void; |
