From 72b40ecf33fb0ef2d4232b80560642296c79399c Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 2 Apr 2026 09:49:17 +0200 Subject: automatically detect the mounted file system type by trial-and-error --- kernel/include/kernel/filesystem/filesystem.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/filesystem.hpp b/kernel/include/kernel/filesystem/filesystem.hpp index 1d86178..05f96dc 100644 --- a/kernel/include/kernel/filesystem/filesystem.hpp +++ b/kernel/include/kernel/filesystem/filesystem.hpp @@ -2,6 +2,7 @@ #define TEACH_OS_KERNEL_FILESYSTEM_FILESYSTEM_HPP #include "kapi/devices/device.hpp" + #include "kernel/filesystem/inode.hpp" #include @@ -15,7 +16,7 @@ namespace kernel::filesystem { virtual ~filesystem() = default; - virtual auto mount(kstd::shared_ptr const & device) -> int; + auto static mount(kstd::shared_ptr const & device) -> kstd::shared_ptr; virtual auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr = 0; [[nodiscard]] auto root_inode() const -> kstd::shared_ptr const &; -- cgit v1.2.3 From 93bca4c2a7c1852fc89df6965c835a7dbbdd6512 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 2 Apr 2026 09:50:14 +0200 Subject: read ext2 superblock and check the magic number --- kernel/include/kernel/filesystem/ext2/filesystem.hpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index 078da31..176d83c 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -2,20 +2,29 @@ #define TEACH_OS_KERNEL_FILESYSTEM_EXT2_FILESYSTEM_HPP #include "kapi/devices/device.hpp" + +#include "kernel/filesystem/ext2/superblock.hpp" #include "kernel/filesystem/filesystem.hpp" #include "kernel/filesystem/inode.hpp" #include +#include #include namespace kernel::filesystem::ext2 { struct filesystem : kernel::filesystem::filesystem { - auto mount(kstd::shared_ptr const & device) -> int override; + auto mount(kstd::shared_ptr const & device) -> int; auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr override; + + private: + auto get_block_size() -> size_t; + auto get_inode_size() -> size_t; + + superblock m_superblock; }; } // namespace kernel::filesystem::ext2 -- cgit v1.2.3 From 1dcf253fdf8169a3b2b71bfac68f2f25951af1a8 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 2 Apr 2026 10:04:04 +0200 Subject: fix build, refactoring --- kernel/include/kernel/filesystem/devfs/filesystem.hpp | 1 + kernel/include/kernel/filesystem/ext2/filesystem.hpp | 2 +- kernel/include/kernel/filesystem/filesystem.hpp | 4 +++- kernel/include/kernel/filesystem/rootfs/filesystem.hpp | 1 + 4 files changed, 6 insertions(+), 2 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/devfs/filesystem.hpp b/kernel/include/kernel/filesystem/devfs/filesystem.hpp index 29ae388..3edeabb 100644 --- a/kernel/include/kernel/filesystem/devfs/filesystem.hpp +++ b/kernel/include/kernel/filesystem/devfs/filesystem.hpp @@ -2,6 +2,7 @@ #define TEACH_OS_KERNEL_FILESYSTEM_DEVFS_FILESYSTEM_HPP #include "kapi/devices/device.hpp" + #include "kernel/filesystem/filesystem.hpp" #include "kernel/filesystem/inode.hpp" diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index 176d83c..f6cd17f 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -16,7 +16,7 @@ namespace kernel::filesystem::ext2 { struct filesystem : kernel::filesystem::filesystem { - auto mount(kstd::shared_ptr const & device) -> int; + auto mount(kstd::shared_ptr const & device) -> int override; auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr override; diff --git a/kernel/include/kernel/filesystem/filesystem.hpp b/kernel/include/kernel/filesystem/filesystem.hpp index 05f96dc..1c45377 100644 --- a/kernel/include/kernel/filesystem/filesystem.hpp +++ b/kernel/include/kernel/filesystem/filesystem.hpp @@ -16,7 +16,9 @@ namespace kernel::filesystem { virtual ~filesystem() = default; - auto static mount(kstd::shared_ptr const & device) -> kstd::shared_ptr; + auto static probe_and_mount(kstd::shared_ptr const & device) -> kstd::shared_ptr; + + virtual auto mount(kstd::shared_ptr const & device) -> int; virtual auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr = 0; [[nodiscard]] auto root_inode() const -> kstd::shared_ptr const &; diff --git a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp index 5632d86..7931d87 100644 --- a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp +++ b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp @@ -2,6 +2,7 @@ #define TEACH_OS_KERNEL_FILESYSTEM_ROOTFS_FILESYSTEM_HPP #include "kapi/devices/device.hpp" + #include "kernel/filesystem/filesystem.hpp" #include "kernel/filesystem/inode.hpp" -- cgit v1.2.3 From 16b854e991bb791694268d09eb696c719cdff42f Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 2 Apr 2026 11:09:13 +0200 Subject: read block_group_descriptors --- kernel/include/kernel/filesystem/ext2/filesystem.hpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index f6cd17f..59b9cba 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -3,11 +3,13 @@ #include "kapi/devices/device.hpp" +#include "kernel/filesystem/ext2/block_group_descriptor.hpp" #include "kernel/filesystem/ext2/superblock.hpp" #include "kernel/filesystem/filesystem.hpp" #include "kernel/filesystem/inode.hpp" #include +#include #include #include @@ -25,6 +27,7 @@ namespace kernel::filesystem::ext2 auto get_inode_size() -> size_t; superblock m_superblock; + kstd::vector m_block_group_descriptors; }; } // namespace kernel::filesystem::ext2 -- cgit v1.2.3 From 91db623189133cb14693ae60ee54ac293cec3b54 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 2 Apr 2026 11:33:14 +0200 Subject: remove todos --- kernel/include/kernel/filesystem/ext2/superblock.hpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/superblock.hpp b/kernel/include/kernel/filesystem/ext2/superblock.hpp index 8600b4c..e7e15f2 100644 --- a/kernel/include/kernel/filesystem/ext2/superblock.hpp +++ b/kernel/include/kernel/filesystem/ext2/superblock.hpp @@ -41,11 +41,8 @@ namespace kernel::filesystem::ext2 uint32_t feature_compat; uint32_t feature_incompat; uint32_t feature_ro_compat; - // uint8_t uuid[16]; // TODO BA-FS26 really correct? std::array uuid; - // uint8_t volume_name[16]; // TODO BA-FS26 really correct? std::array volume_name; - // uint8_t last_mounted[64]; // TODO BA-FS26 really correct? std::array last_mounted; uint32_t algorithm_usage_bitmap; @@ -55,14 +52,12 @@ namespace kernel::filesystem::ext2 uint16_t padding1; // Journaling Support - // uint8_t journal_uuid[16]; // TODO BA-FS26 really correct? std::array journal_uuid; uint32_t journal_inum; uint32_t journal_dev; uint32_t last_orphan; // Directory Indexing Support - // uint32_t hash_seed[4]; // TODO BA-FS26 really correct? std::array hash_seed; uint8_t def_hash_version; std::array padding2; -- cgit v1.2.3 From baf63039d5430c0b3b1e6235b561c12f60e97f49 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 2 Apr 2026 15:03:06 +0200 Subject: implement read_inode --- .../kernel/filesystem/ext2/block_group_descriptor.hpp | 2 +- kernel/include/kernel/filesystem/ext2/filesystem.hpp | 4 ++++ kernel/include/kernel/filesystem/ext2/inode.hpp | 19 +++++++++++-------- .../kernel/filesystem/ext2/linked_directory_entry.hpp | 2 +- kernel/include/kernel/filesystem/ext2/superblock.hpp | 2 +- 5 files changed, 18 insertions(+), 11 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp b/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp index a23c045..2ff91d9 100644 --- a/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp +++ b/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp @@ -6,7 +6,7 @@ namespace kernel::filesystem::ext2 { - struct block_group_descriptor + struct [[gnu::packed]] block_group_descriptor { uint32_t block_bitmap; uint32_t inode_bitmap; diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index 59b9cba..ccee172 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -4,6 +4,7 @@ #include "kapi/devices/device.hpp" #include "kernel/filesystem/ext2/block_group_descriptor.hpp" +#include "kernel/filesystem/ext2/inode.hpp" #include "kernel/filesystem/ext2/superblock.hpp" #include "kernel/filesystem/filesystem.hpp" #include "kernel/filesystem/inode.hpp" @@ -12,6 +13,7 @@ #include #include +#include #include namespace kernel::filesystem::ext2 @@ -23,6 +25,8 @@ namespace kernel::filesystem::ext2 -> kstd::shared_ptr override; private: + auto read_inode(uint32_t inode_number) -> kstd::shared_ptr; + auto get_block_size() -> size_t; auto get_inode_size() -> size_t; diff --git a/kernel/include/kernel/filesystem/ext2/inode.hpp b/kernel/include/kernel/filesystem/ext2/inode.hpp index 2c27c17..4284e6f 100644 --- a/kernel/include/kernel/filesystem/ext2/inode.hpp +++ b/kernel/include/kernel/filesystem/ext2/inode.hpp @@ -11,13 +11,8 @@ namespace kernel::filesystem::ext2 { - struct inode : kernel::filesystem::inode + struct [[gnu::packed]] inode_data { - inode(); - - auto read(void * buffer, size_t offset, size_t size) const -> size_t override; - auto write(void const * buffer, size_t offset, size_t size) -> size_t override; - uint16_t mode; uint16_t uid; uint32_t size; @@ -30,15 +25,23 @@ namespace kernel::filesystem::ext2 uint32_t blocks; uint32_t flags; uint32_t osd1; - // uint32_t block[15]; // TODO BA-FS26 really correct? std::array block; // NOLINT(readability-magic-numbers) uint32_t generation; uint32_t file_acl; uint32_t dir_acl; uint32_t faddr; - // uint8_t osd2[12]; // TODO BA-FS26 really correct? std::array osd2; // NOLINT(readability-magic-numbers) }; + + struct inode : kernel::filesystem::inode + { + inode(); + + auto read(void * buffer, size_t offset, size_t size) const -> size_t override; + auto write(void const * buffer, size_t offset, size_t size) -> size_t override; + + inode_data m_data{}; + }; } // namespace kernel::filesystem::ext2 #endif \ No newline at end of file diff --git a/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp b/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp index 8dd42a1..f44255a 100644 --- a/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp +++ b/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp @@ -6,7 +6,7 @@ namespace kernel::filesystem::ext2 { - struct linked_directory_entry + struct [[gnu::packed]] linked_directory_entry { uint32_t inode; uint16_t rec_len; diff --git a/kernel/include/kernel/filesystem/ext2/superblock.hpp b/kernel/include/kernel/filesystem/ext2/superblock.hpp index e7e15f2..8e57ae7 100644 --- a/kernel/include/kernel/filesystem/ext2/superblock.hpp +++ b/kernel/include/kernel/filesystem/ext2/superblock.hpp @@ -6,7 +6,7 @@ namespace kernel::filesystem::ext2 { - struct superblock + struct [[gnu::packed]] superblock { uint32_t inodes_count; uint32_t blocks_count; -- cgit v1.2.3 From 794de4a7f8dbea164d857ae9e4525536f338518d Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Thu, 2 Apr 2026 16:06:40 +0200 Subject: temporary implementation of inode kind --- kernel/include/kernel/filesystem/inode.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/inode.hpp b/kernel/include/kernel/filesystem/inode.hpp index d97b5ab..59207df 100644 --- a/kernel/include/kernel/filesystem/inode.hpp +++ b/kernel/include/kernel/filesystem/inode.hpp @@ -25,7 +25,8 @@ namespace kernel::filesystem [[nodiscard]] auto is_regular() const -> bool; [[nodiscard]] auto is_device() const -> bool; - private: + // TODO BA-FS26 improve inode_kind really needed? + public: inode_kind m_kind{inode_kind::regular}; }; } // namespace kernel::filesystem -- cgit v1.2.3 From 15ea1551e54c36ebac26f21dc156636e326298c6 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Fri, 3 Apr 2026 14:29:32 +0200 Subject: fix linked_directory_entry struct --- kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp b/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp index f44255a..76eb6f5 100644 --- a/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp +++ b/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp @@ -12,8 +12,7 @@ namespace kernel::filesystem::ext2 uint16_t rec_len; uint8_t name_len; uint8_t file_type; - uint8_t pad; - std::array name; // NOLINT(readability-magic-numbers) + std::array name; // NOLINT(readability-magic-numbers) }; } // namespace kernel::filesystem::ext2 -- cgit v1.2.3 From 7e6137b2725d5cf2b16f55678dcfb99091f03fe9 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Fri, 3 Apr 2026 14:30:20 +0200 Subject: implement map_inode_block_index_to_global_block_number and lookup --- kernel/include/kernel/filesystem/ext2/filesystem.hpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index ccee172..dea059f 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -26,9 +26,11 @@ namespace kernel::filesystem::ext2 private: auto read_inode(uint32_t inode_number) -> kstd::shared_ptr; + auto map_inode_block_index_to_global_block_number(uint32_t inode_block_index, inode_data data) -> uint32_t; auto get_block_size() -> size_t; auto get_inode_size() -> size_t; + auto get_inode_block_count(inode_data const & data) -> uint32_t; superblock m_superblock; kstd::vector m_block_group_descriptors; -- cgit v1.2.3 From fe8706422605e466427ae2727ddb98ce5cd984f6 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Fri, 3 Apr 2026 15:38:16 +0200 Subject: refactoring map_inode_block_index_to_global_block_number --- kernel/include/kernel/filesystem/ext2/filesystem.hpp | 1 + 1 file changed, 1 insertion(+) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index dea059f..9761903 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -27,6 +27,7 @@ namespace kernel::filesystem::ext2 private: auto read_inode(uint32_t inode_number) -> kstd::shared_ptr; auto map_inode_block_index_to_global_block_number(uint32_t inode_block_index, inode_data data) -> uint32_t; + auto read_block_number_at_index(uint32_t block_number, uint32_t index) -> uint32_t; auto get_block_size() -> size_t; auto get_inode_size() -> size_t; -- cgit v1.2.3 From 725116d22e850c502e6cb8d42b100da1080dfec0 Mon Sep 17 00:00:00 2001 From: Marcel Braun Date: Mon, 6 Apr 2026 10:35:45 +0200 Subject: Add file system pointer to ext2 inode --- kernel/include/kernel/filesystem/ext2/inode.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/inode.hpp b/kernel/include/kernel/filesystem/ext2/inode.hpp index 4284e6f..9318008 100644 --- a/kernel/include/kernel/filesystem/ext2/inode.hpp +++ b/kernel/include/kernel/filesystem/ext2/inode.hpp @@ -11,6 +11,8 @@ namespace kernel::filesystem::ext2 { + struct filesystem; + struct [[gnu::packed]] inode_data { uint16_t mode; @@ -35,12 +37,13 @@ namespace kernel::filesystem::ext2 struct inode : kernel::filesystem::inode { - inode(); + explicit inode(filesystem * fs); auto read(void * buffer, size_t offset, size_t size) const -> size_t override; auto write(void const * buffer, size_t offset, size_t size) -> size_t override; inode_data m_data{}; + filesystem * m_filesystem; }; } // namespace kernel::filesystem::ext2 -- cgit v1.2.3 From 4a2d4fb3ab38a64c4b10832f5a6318b7240829cc Mon Sep 17 00:00:00 2001 From: Marcel Braun Date: Mon, 6 Apr 2026 11:40:12 +0200 Subject: Implement read data in ext2 inode --- kernel/include/kernel/filesystem/ext2/filesystem.hpp | 5 +++-- kernel/include/kernel/filesystem/filesystem.hpp | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index 9761903..762f590 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -24,12 +24,13 @@ namespace kernel::filesystem::ext2 auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr override; + auto get_block_size() -> size_t; + auto map_inode_block_index_to_global_block_number(uint32_t inode_block_index, inode_data data) -> uint32_t; + private: auto read_inode(uint32_t inode_number) -> kstd::shared_ptr; - auto map_inode_block_index_to_global_block_number(uint32_t inode_block_index, inode_data data) -> uint32_t; auto read_block_number_at_index(uint32_t block_number, uint32_t index) -> uint32_t; - auto get_block_size() -> size_t; auto get_inode_size() -> size_t; auto get_inode_block_count(inode_data const & data) -> uint32_t; diff --git a/kernel/include/kernel/filesystem/filesystem.hpp b/kernel/include/kernel/filesystem/filesystem.hpp index 1c45377..0f9de9f 100644 --- a/kernel/include/kernel/filesystem/filesystem.hpp +++ b/kernel/include/kernel/filesystem/filesystem.hpp @@ -22,6 +22,7 @@ namespace kernel::filesystem virtual auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr = 0; [[nodiscard]] auto root_inode() const -> kstd::shared_ptr const &; + [[nodiscard]] auto device() const -> kstd::shared_ptr const &; protected: kstd::shared_ptr m_root_inode{}; -- cgit v1.2.3 From 2240b9a36e4a9f6f8291c9271e6aac8f5536dbd7 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Tue, 7 Apr 2026 22:13:49 +0200 Subject: refactoring --- kernel/include/kernel/filesystem/ext2/filesystem.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index 762f590..abab0a6 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -34,7 +34,7 @@ namespace kernel::filesystem::ext2 auto get_inode_size() -> size_t; auto get_inode_block_count(inode_data const & data) -> uint32_t; - superblock m_superblock; + superblock m_superblock; // TODO BA-FS26 initialize kstd::vector m_block_group_descriptors; }; } // namespace kernel::filesystem::ext2 -- cgit v1.2.3 From dd330e7a05905713acfa87ec109956bfe78f78c4 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Wed, 8 Apr 2026 09:31:32 +0200 Subject: add descriptions, some refactoring --- .../include/kernel/devices/block_device_utils.hpp | 29 +++++++++++++- kernel/include/kernel/filesystem/dentry.hpp | 46 ++++++++++++++++++++++ .../include/kernel/filesystem/devfs/filesystem.hpp | 18 +++++++++ kernel/include/kernel/filesystem/devfs/inode.hpp | 22 +++++++++++ kernel/include/kernel/filesystem/device_inode.hpp | 31 +++++++++++++++ .../filesystem/ext2/block_group_descriptor.hpp | 3 ++ .../include/kernel/filesystem/ext2/filesystem.hpp | 29 +++++++++++++- kernel/include/kernel/filesystem/ext2/inode.hpp | 27 +++++++++++++ .../filesystem/ext2/linked_directory_entry.hpp | 3 ++ .../include/kernel/filesystem/ext2/superblock.hpp | 3 ++ .../kernel/filesystem/file_descriptor_table.hpp | 34 ++++++++++++++++ kernel/include/kernel/filesystem/filesystem.hpp | 37 +++++++++++++++++ kernel/include/kernel/filesystem/inode.hpp | 44 +++++++++++++++++++++ kernel/include/kernel/filesystem/mount.hpp | 28 ++++++++++++- kernel/include/kernel/filesystem/mount_table.hpp | 16 +++++++- .../kernel/filesystem/open_file_description.hpp | 28 +++++++++++++ .../kernel/filesystem/rootfs/filesystem.hpp | 18 +++++++++ kernel/include/kernel/filesystem/rootfs/inode.hpp | 34 ++++++++++++++++ kernel/include/kernel/filesystem/vfs.hpp | 31 +++++++++++++++ 19 files changed, 476 insertions(+), 5 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/devices/block_device_utils.hpp b/kernel/include/kernel/devices/block_device_utils.hpp index 5e862ba..7b1daec 100644 --- a/kernel/include/kernel/devices/block_device_utils.hpp +++ b/kernel/include/kernel/devices/block_device_utils.hpp @@ -9,7 +9,34 @@ namespace kernel::devices::block_device_utils { - auto read(kstd::shared_ptr const & device, void * buffer, size_t offset, size_t size) -> size_t; + /** + @brief Utility functions for block devices, such as reading/writing data at specific offsets. These functions handle + the necessary logic to interact with block devices, such as calculating block boundaries and ensuring proper access + patterns. They abstract away the details of block device interactions, providing a simple interface for reading and + writing data to block devices. + */ + + /** + @brief Reads data from a @p device into a @p buffer, starting at a specific @p offset and for a given @p size. + @warning Panics if @p buffer or @p device is null. + @param device The block device to read from. + @param buffer The buffer to read data into. + @param offset The offset on the block device to start reading from. + @param size The number of bytes to read. + @return The number of bytes actually read, which may be less than the requested size. + */ + auto read(kstd::shared_ptr const & device, void * buffer, size_t offset, size_t size) + -> size_t; + + /** + @brief Writes data from a @p buffer to a @p device, starting at a specific @p offset and for a given @p size. + @warning Panics if @p buffer or @p device is null. + @param device The block device to write to. + @param buffer The buffer to write data from. + @param offset The offset on the block device to start writing to. + @param size The number of bytes to write. + @return The number of bytes actually written, which may be less than the requested size. + */ auto write(kstd::shared_ptr const & device, void const * buffer, size_t offset, size_t size) -> size_t; } // namespace kernel::devices::block_device_utils diff --git a/kernel/include/kernel/filesystem/dentry.hpp b/kernel/include/kernel/filesystem/dentry.hpp index fc85a7d..72d758f 100644 --- a/kernel/include/kernel/filesystem/dentry.hpp +++ b/kernel/include/kernel/filesystem/dentry.hpp @@ -12,23 +12,69 @@ namespace kernel::filesystem { + /** + @brief Represents a directory entry (dentry) in the filesystem. A dentry is a node in the directory tree that + represents a file or directory. It contains a reference to its parent dentry, a reference to the associated real + filesystem inode, and a list of child dentries. + */ struct dentry { + /** + @brief Flags for the dentry. + */ enum class dentry_flags : uint32_t { dcache_mounted = 1 << 15 }; + /** + @brief Create a dentry with the given @p parent, associated @p node, and optional @p name. The dentry is initialized + with the provided parent and inode, and the name is stored for lookup purposes. + */ dentry(kstd::shared_ptr const & parent, kstd::shared_ptr const & node, std::string_view name = {}); + /** + @brief Get the associated inode. + @return A reference to the associated inode. + */ [[nodiscard]] auto get_inode() const -> kstd::shared_ptr const &; + + /** + @brief Get the parent dentry. + @return A reference to the parent dentry. + */ [[nodiscard]] auto get_parent() const -> kstd::shared_ptr const &; + /** + @brief Add a @p child dentry. + @param child The child dentry to add. + */ auto add_child(kstd::shared_ptr const & child) -> void; + + /** + @brief Find a child dentry by @p name. + @param name The name of the child dentry to find. + @return A pointer to the found child dentry, or a null pointer if not found. + */ [[nodiscard]] auto find_child(std::string_view name) const -> kstd::shared_ptr; + /** + @brief Set a @p flag for the dentry. + @param flag The flag to set. + */ auto set_flag(dentry_flags flag) -> void; + + /** + @brief Unset a @p flag for the dentry. + @param flag The flag to unset. + */ auto unset_flag(dentry_flags flag) -> void; + + /** + @brief Check if the dentry has a specific @p flag. + @param flag The flag to check. + @return True if the dentry has the flag, false otherwise. + */ [[nodiscard]] auto has_flag(dentry_flags flag) const -> bool; private: diff --git a/kernel/include/kernel/filesystem/devfs/filesystem.hpp b/kernel/include/kernel/filesystem/devfs/filesystem.hpp index 3edeabb..60c39cf 100644 --- a/kernel/include/kernel/filesystem/devfs/filesystem.hpp +++ b/kernel/include/kernel/filesystem/devfs/filesystem.hpp @@ -13,9 +13,27 @@ namespace kernel::filesystem::devfs { + /** + @brief A filesystem for managing device nodes in the virtual filesystem. This filesystem provides a way to represent + devices as files in the /dev directory, allowing user-space applications to interact with devices using standard file + operations. The devfs filesystem dynamically creates inodes for devices registered in the system, enabling seamless + access to device functionality through the filesystem interface. + */ struct filesystem : kernel::filesystem::filesystem { + /** + @brief Initializes the devfs instance and builds the device inode table. + @param device Backing device passed by the generic filesystem interface (not required by devfs). + @return 0 on success, -1 on failure. + */ auto mount(kstd::shared_ptr const & device) -> int override; + + /** + @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. + */ auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr override; diff --git a/kernel/include/kernel/filesystem/devfs/inode.hpp b/kernel/include/kernel/filesystem/devfs/inode.hpp index 9c11edf..c117bd2 100644 --- a/kernel/include/kernel/filesystem/devfs/inode.hpp +++ b/kernel/include/kernel/filesystem/devfs/inode.hpp @@ -7,11 +7,33 @@ namespace kernel::filesystem::devfs { + /** + @brief Inode implementation for the devfs filesystem. + This inode represents root device node in the /dev directory. + */ struct inode : kernel::filesystem::inode { + /** + @brief Create a devfs inode. The inode is initialized with the appropriate kind (directory). + */ inode(); + /** + @brief Reads from the devfs directory inode. + @param buffer Destination buffer. + @param offset Read offset in bytes. + @param size Number of bytes requested. + @return Number of bytes read (always 0 because this inode does not expose file data). + */ auto read(void * buffer, size_t offset, size_t size) const -> size_t override; + + /** + @brief Writes to the devfs directory inode. + @param buffer Source buffer. + @param offset Write offset in bytes. + @param size Number of bytes requested. + @return Number of bytes written (always 0 because writes are not supported for this inode). + */ auto write(void const * buffer, size_t offset, size_t size) -> size_t override; }; } // namespace kernel::filesystem::devfs diff --git a/kernel/include/kernel/filesystem/device_inode.hpp b/kernel/include/kernel/filesystem/device_inode.hpp index 18a98f5..c33be2a 100644 --- a/kernel/include/kernel/filesystem/device_inode.hpp +++ b/kernel/include/kernel/filesystem/device_inode.hpp @@ -2,6 +2,7 @@ #define TEACH_OS_KERNEL_FILESYSTEM_DEVICE_INODE_HPP #include "kapi/devices/device.hpp" + #include "kernel/filesystem/inode.hpp" #include @@ -10,13 +11,43 @@ namespace kernel::filesystem { + /** + @brief Inode implementation for device inodes in the filesystem. This inode represents a device file that provides + access to a device registered in the system. The device inode allows reading from and writing to the associated + device. + */ struct device_inode : inode { + /** + @brief Create a device inode with the given @p device. + @param device The device to associate with the inode. + */ explicit device_inode(kstd::shared_ptr const & device); + /** + @brief Read data from the device inode (and in the background from the associated device) into a @p buffer, starting + at @p offset and reading @p size bytes. + @param buffer The buffer to read data into. + @param offset The offset to read from. + @param size The number of bytes to read. + @return The number of bytes read. + */ auto read(void * buffer, size_t offset, size_t size) const -> size_t override; + + /** + @brief Write data to the device inode (and in the background from the associated device) from a @p buffer, starting + at @p offset and writing @p size bytes. + @param buffer The buffer containing data to write. + @param offset The offset to write to. + @param size The number of bytes to write. + @return The number of bytes written. + */ auto write(void const * buffer, size_t offset, size_t size) -> size_t override; + /** + @brief Get the associated device. + @return A reference to the associated device. + */ [[nodiscard]] auto device() const -> kstd::shared_ptr const &; private: diff --git a/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp b/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp index 2ff91d9..7fbba3f 100644 --- a/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp +++ b/kernel/include/kernel/filesystem/ext2/block_group_descriptor.hpp @@ -6,6 +6,9 @@ namespace kernel::filesystem::ext2 { + /** + @brief Represents a block group descriptor in the ext2 filesystem. + */ struct [[gnu::packed]] block_group_descriptor { uint32_t block_bitmap; diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index abab0a6..32374dc 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -18,13 +18,40 @@ namespace kernel::filesystem::ext2 { + /** + @brief A filesystem implementation for the ext2 filesystem format. This class provides methods for mounting an ext2 + filesystem, and looking up inodes. + */ struct filesystem : kernel::filesystem::filesystem { + /** + @brief Initializes the ext2 filesystem with the given @p device. + @param device The device to mount. + @return 0 on success, negative error code on failure. + */ auto mount(kstd::shared_ptr const & device) -> int override; + + /** + @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. + */ auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr override; + /** + @brief Gets the size of a block in the filesystem. + @return The size of a block in bytes. + */ auto get_block_size() -> size_t; + + /** + @brief Maps an inode block index to a global block number. + @param inode_block_index The index of the block within the inode. + @param data The inode data. + @return The global block number. + */ auto map_inode_block_index_to_global_block_number(uint32_t inode_block_index, inode_data data) -> uint32_t; private: @@ -34,7 +61,7 @@ namespace kernel::filesystem::ext2 auto get_inode_size() -> size_t; auto get_inode_block_count(inode_data const & data) -> uint32_t; - superblock m_superblock; // TODO BA-FS26 initialize + superblock m_superblock{}; kstd::vector m_block_group_descriptors; }; } // namespace kernel::filesystem::ext2 diff --git a/kernel/include/kernel/filesystem/ext2/inode.hpp b/kernel/include/kernel/filesystem/ext2/inode.hpp index 9318008..a1645cd 100644 --- a/kernel/include/kernel/filesystem/ext2/inode.hpp +++ b/kernel/include/kernel/filesystem/ext2/inode.hpp @@ -13,6 +13,9 @@ namespace kernel::filesystem::ext2 { struct filesystem; + /** + @brief Represents the data associated with an ext2 inode. + */ struct [[gnu::packed]] inode_data { uint16_t mode; @@ -37,12 +40,36 @@ namespace kernel::filesystem::ext2 struct inode : kernel::filesystem::inode { + /** + @brief Create an ext2 inode associated with the given filesystem. + */ explicit inode(filesystem * fs); + /** + @brief Reads from the ext2 inode into a @p buffer, starting at the specified @p offset and for a given @p size. + @param buffer Destination buffer. + @param offset Read offset in bytes. + @param size Number of bytes requested. + @return Number of bytes read. + */ auto read(void * buffer, size_t offset, size_t size) const -> size_t override; + + /** + @brief Writes to the ext2 inode into a @p buffer, starting at the specified @p offset and for a given @p size. + @warning This method is not implemented yet and will panic if called. + @param buffer Source buffer. + @param offset Write offset in bytes. + @param size Number of bytes requested. + @return Number of bytes written. + */ auto write(void const * buffer, size_t offset, size_t size) -> size_t override; + /** + @brief The raw inode data as read from the disk. + */ inode_data m_data{}; + + private: filesystem * m_filesystem; }; } // namespace kernel::filesystem::ext2 diff --git a/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp b/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp index 76eb6f5..4097cbb 100644 --- a/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp +++ b/kernel/include/kernel/filesystem/ext2/linked_directory_entry.hpp @@ -6,6 +6,9 @@ namespace kernel::filesystem::ext2 { + /** + @brief Represents a linked directory entry in the ext2 filesystem. + */ struct [[gnu::packed]] linked_directory_entry { uint32_t inode; diff --git a/kernel/include/kernel/filesystem/ext2/superblock.hpp b/kernel/include/kernel/filesystem/ext2/superblock.hpp index 8e57ae7..41ad935 100644 --- a/kernel/include/kernel/filesystem/ext2/superblock.hpp +++ b/kernel/include/kernel/filesystem/ext2/superblock.hpp @@ -6,6 +6,9 @@ namespace kernel::filesystem::ext2 { + /** + @brief Represents the superblock in the ext2 filesystem. + */ struct [[gnu::packed]] superblock { uint32_t inodes_count; diff --git a/kernel/include/kernel/filesystem/file_descriptor_table.hpp b/kernel/include/kernel/filesystem/file_descriptor_table.hpp index 91e2960..5d52c19 100644 --- a/kernel/include/kernel/filesystem/file_descriptor_table.hpp +++ b/kernel/include/kernel/filesystem/file_descriptor_table.hpp @@ -8,15 +8,49 @@ namespace kernel::filesystem { + /** + @brief A table for managing file descriptors in the filesystem. This class provides methods for adding, retrieving, + and removing open file descriptions. + */ struct file_descriptor_table { + /** + @brief Initialize the global file descriptor table. This method creates the singleton instance of the file + descriptor table. + @warning Panics if called more than once. + */ auto static init() -> void; + + /** + @brief Get the global file descriptor table instance. + @return A reference to the global file descriptor table. + @warning Panics if the file descriptor table has not been initialized. + */ auto static get() -> file_descriptor_table &; + /** + @brief Destructor for the file descriptor table. + */ ~file_descriptor_table() = default; + /** + @brief Add a file to the descriptor table. + @param f The file description to add. + @return The file descriptor index assigned to the file, or -1 on failure. + */ auto add_file(kstd::shared_ptr const & f) -> int; + + /** + @brief Get a file from the descriptor table. + @param fd The file descriptor index to retrieve. + @return A pointer to the requested file description, or a null pointer if not found. + */ [[nodiscard]] auto get_file(int fd) const -> kstd::shared_ptr; + + /** + @brief Remove a file from the descriptor table. + @param fd The file descriptor index to remove. + */ auto remove_file(int fd) -> void; private: diff --git a/kernel/include/kernel/filesystem/filesystem.hpp b/kernel/include/kernel/filesystem/filesystem.hpp index 0f9de9f..f855380 100644 --- a/kernel/include/kernel/filesystem/filesystem.hpp +++ b/kernel/include/kernel/filesystem/filesystem.hpp @@ -12,16 +12,53 @@ namespace kernel::filesystem { + /** + @brief A base class for implementing filesystems in the kernel. This class provides a common interface for managing + files and directories within the virtual filesystem. + */ struct filesystem { + /** + @brief Virtual destructor for the filesystem. + */ virtual ~filesystem() = default; + /** + @brief Probes the given @p device to determine if it contains a recognizable filesystem, and if so, mounts it and + returns a pointer to the mounted filesystem instance. This method iterates through known filesystem types and + attempts to initialize it with the device until the mount was successful or all types have been tried. + @param device The device to probe and mount. + @return A pointer to the mounted filesystem instance if successful, or a null pointer if no recognizable filesystem + is found on the device. + @warning Panics if @p device is null. + */ auto static probe_and_mount(kstd::shared_ptr const & device) -> kstd::shared_ptr; + /** + @brief Initializes the filesystem with the given @p device. + @param device The device to mount. + @return 0 on success, or a negative error code on failure. + */ virtual auto mount(kstd::shared_ptr const & device) -> int; + + /** + @brief Looks up a child inode within the given @p parent inode with the specified @p name. This method must be + implemented by concrete filesystem subclasses to provide the logic for traversing the filesystem structure and + finding the requested inode. + @param parent The parent inode. + @param name The name of the child inode to look up. + @return A pointer to the requested child inode, or a null pointer if not found. + */ virtual auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr = 0; + /** + @brief Returns a reference to the root inode of the filesystem. + */ [[nodiscard]] auto root_inode() const -> kstd::shared_ptr const &; + + /** + @brief Returns a reference to the device associated with the filesystem. + */ [[nodiscard]] auto device() const -> kstd::shared_ptr const &; protected: diff --git a/kernel/include/kernel/filesystem/inode.hpp b/kernel/include/kernel/filesystem/inode.hpp index 59207df..7237184 100644 --- a/kernel/include/kernel/filesystem/inode.hpp +++ b/kernel/include/kernel/filesystem/inode.hpp @@ -5,8 +5,14 @@ namespace kernel::filesystem { + /** + @brief Represents an inode in the filesystem. + */ struct inode { + /** + @brief Represents the kind of an inode. + */ enum class inode_kind { regular, @@ -14,15 +20,53 @@ namespace kernel::filesystem device }; + /** + @brief Create an inode with the given @p kind. + @param kind The kind of the inode. + */ explicit inode(inode_kind kind); + /** + @brief Virtual destructor for the inode. + */ virtual ~inode() = default; + /** + @brief Reads from the inode into a @p buffer, starting at the specified @p offset and for a given @p size. This + method must be implemented by concrete inode subclasses. + @param buffer Destination buffer. + @param offset Read offset in bytes. + @param size Number of bytes requested. + @return Number of bytes read. + */ virtual auto read(void * buffer, size_t offset, size_t size) const -> size_t = 0; + + /** + @brief Writes to the inode into a @p buffer, starting at the specified @p offset and for a given @p size. This + method must be implemented by concrete inode subclasses. + @param buffer Source buffer. + @param offset Write offset in bytes. + @param size Number of bytes to write. + @return Number of bytes written. + */ virtual auto write(void const * buffer, size_t offset, size_t size) -> size_t = 0; + /** + @brief Returns whether the inode is a directory. + @return true if the inode is a directory, false otherwise. + */ [[nodiscard]] auto is_directory() const -> bool; + + /** + @brief Returns whether the inode is a regular file. + @return true if the inode is a regular file, false otherwise. + */ [[nodiscard]] auto is_regular() const -> bool; + + /** + @brief Returns whether the inode is a device. + @return true if the inode is a device, false otherwise. + */ [[nodiscard]] auto is_device() const -> bool; // TODO BA-FS26 improve inode_kind really needed? diff --git a/kernel/include/kernel/filesystem/mount.hpp b/kernel/include/kernel/filesystem/mount.hpp index a054750..3e3c69f 100644 --- a/kernel/include/kernel/filesystem/mount.hpp +++ b/kernel/include/kernel/filesystem/mount.hpp @@ -11,15 +11,41 @@ namespace kernel::filesystem { + /** + @brief Represents a mounted filesystem in the kernel. + */ struct mount { + /** + @brief Creates a mount for the given @p filesystem at the specified @p mount_path. The @p mount_dentry represents + the dentry where the filesystem is mounted, and the @p root_dentry represents the root dentry of the mounted + filesystem. + @param mount_dentry The dentry where the filesystem is mounted. + @param root_dentry The root dentry of the mounted filesystem. + @param fs The filesystem instance being mounted. + @param mount_path The path at which the filesystem is mounted. + */ mount(kstd::shared_ptr const & mount_dentry, kstd::shared_ptr const & root_dentry, kstd::shared_ptr const & fs, std::string_view mount_path); - [[nodiscard]] auto get_mount_dentry() const -> kstd::shared_ptr; + /** + @brief Get the dentry where the filesystem is mounted. + */ + [[nodiscard]] auto get_mount_dentry() const -> kstd::shared_ptr const &; + + /** + @brief Get the root dentry of the mounted filesystem. + */ [[nodiscard]] auto root_dentry() const -> kstd::shared_ptr const &; + /** + @brief Get the filesystem instance being mounted. + */ [[nodiscard]] auto get_filesystem() const -> kstd::shared_ptr const &; + + /** + @brief Get the path at which the filesystem is mounted. + */ [[nodiscard]] auto get_mount_path() const -> std::string_view; private: diff --git a/kernel/include/kernel/filesystem/mount_table.hpp b/kernel/include/kernel/filesystem/mount_table.hpp index 6dc2218..a8ef59e 100644 --- a/kernel/include/kernel/filesystem/mount_table.hpp +++ b/kernel/include/kernel/filesystem/mount_table.hpp @@ -10,11 +10,23 @@ namespace kernel::filesystem { + /** + @brief A table for managing mounted filesystems in the kernel. + */ struct mount_table { - public: - void add_mount(kstd::shared_ptr); + /** + @brief Adds a mount to the table. + @param mount The mount to add. + */ + void add_mount(kstd::shared_ptr const & mount); + /** + @brief Finds the mount with the longest prefix matching the given @p path. This method is used to determine which + mounted filesystem should handle a given path lookup. + @param path The path to match against the mount paths in the table. + @return A pointer to the mount with the longest matching prefix, or a null pointer if no mount matches the path. + */ [[nodiscard]] auto find_longest_prefix_mount(std::string_view path) const -> kstd::shared_ptr; private: diff --git a/kernel/include/kernel/filesystem/open_file_description.hpp b/kernel/include/kernel/filesystem/open_file_description.hpp index 45719cf..ed878a7 100644 --- a/kernel/include/kernel/filesystem/open_file_description.hpp +++ b/kernel/include/kernel/filesystem/open_file_description.hpp @@ -9,13 +9,41 @@ namespace kernel::filesystem { + /** + @brief Represents an open file description in the filesystem. This class encapsulates the state of an open file, + including a reference to the associated inode and the current file offset. + */ struct open_file_description { + /** + @brief Constructs an open file description for the given @p inode. + @param inode The inode to associate with the open file description. + */ explicit open_file_description(kstd::shared_ptr const & inode); + /** + @brief Destructor for the open file description. + */ ~open_file_description() = default; + /** + @brief Reads data from the open file description into a @p buffer, starting at the current file offset and for a + given + @p size. The file offset is advanced by the number of bytes read. + @param buffer The buffer to read data into. + @param size The number of bytes to read. + @return The number of bytes read. + */ auto read(void * buffer, size_t size) -> size_t; + + /** + @brief Writes data to the open file description from a @p buffer, starting at the current file offset and for a + given + @p size. The file offset is advanced by the number of bytes written. + @param buffer The buffer to write data from. + @param size The number of bytes to write. + @return The number of bytes written. + */ auto write(void const * buffer, size_t size) -> size_t; private: diff --git a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp index 7931d87..b7e7c6f 100644 --- a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp +++ b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp @@ -14,9 +14,27 @@ namespace kernel::filesystem::rootfs { + /** + @brief A filesystem for the root filesystem. This filesystem provides access to the root directory and its contents, + which are typically populated by the init process during system startup. The rootfs filesystem serves as the top-level + directory in the filesystem hierarchy. It is responsible for providing a stable and consistent interface to the root + directory. + */ struct filesystem : kernel::filesystem::filesystem { + /** + @brief Initializes the rootfs filesystem with the given @p device. + @param device The device to mount (not required by rootfs). + @return 0 on success, negative error code on failure. + */ auto mount(kstd::shared_ptr const & device) -> int override; + + /** + @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. + */ auto lookup(kstd::shared_ptr const & parent, std::string_view name) -> kstd::shared_ptr override; }; diff --git a/kernel/include/kernel/filesystem/rootfs/inode.hpp b/kernel/include/kernel/filesystem/rootfs/inode.hpp index 24d3e6b..469e47a 100644 --- a/kernel/include/kernel/filesystem/rootfs/inode.hpp +++ b/kernel/include/kernel/filesystem/rootfs/inode.hpp @@ -13,14 +13,48 @@ 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. + */ struct inode : kernel::filesystem::inode { + /** + @brief Create a rootfs inode. The inode is initialized with the appropriate kind (directory). + */ inode(); + /** + @brief Reads from the rootfs directory inode. + @param buffer Destination buffer. + @param offset Read offset in bytes. + @param size Number of bytes requested. + @return Number of bytes read (always 0 because this inode does not expose file data). + */ auto read(void * buffer, size_t offset, size_t size) const -> size_t override; + + /** + @brief Writes to the rootfs directory inode. + @param buffer Source buffer. + @param offset Write offset in bytes. + @param size Number of bytes requested. + @return Number of bytes written (always 0 because writes are not supported for this inode). + */ 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; private: diff --git a/kernel/include/kernel/filesystem/vfs.hpp b/kernel/include/kernel/filesystem/vfs.hpp index 5823a83..2d05765 100644 --- a/kernel/include/kernel/filesystem/vfs.hpp +++ b/kernel/include/kernel/filesystem/vfs.hpp @@ -12,14 +12,45 @@ 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 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 creates an open file description. + @param path The path to the file to open. + @return A shared pointer to the open file description or a null pointer if the file could not be opened. + */ auto open(std::string_view path) -> kstd::shared_ptr; + + /** + @brief Mount a @p filesystem at a specific @p path. + @param path The path where the filesystem should be mounted. + @param filesystem The filesystem to mount. + @return 0 on success, or a negative error code on failure. + */ auto do_mount(std::string_view path, kstd::shared_ptr const & filesystem) -> int; private: -- cgit v1.2.3 From 2793770dc6eba30b73b4a4993618d2cbe184790e Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Wed, 8 Apr 2026 15:21:10 +0200 Subject: implement unmount, improve error codes --- .../include/kernel/filesystem/devfs/filesystem.hpp | 4 ++-- .../include/kernel/filesystem/ext2/filesystem.hpp | 4 ++-- kernel/include/kernel/filesystem/filesystem.hpp | 12 +++++++++-- kernel/include/kernel/filesystem/mount.hpp | 10 +++++++++- kernel/include/kernel/filesystem/mount_table.hpp | 21 +++++++++++++++++++- .../kernel/filesystem/rootfs/filesystem.hpp | 4 ++-- kernel/include/kernel/filesystem/vfs.hpp | 23 ++++++++++++++++++++-- 7 files changed, 66 insertions(+), 12 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/kernel/filesystem/devfs/filesystem.hpp b/kernel/include/kernel/filesystem/devfs/filesystem.hpp index 60c39cf..137eca3 100644 --- a/kernel/include/kernel/filesystem/devfs/filesystem.hpp +++ b/kernel/include/kernel/filesystem/devfs/filesystem.hpp @@ -24,9 +24,9 @@ namespace kernel::filesystem::devfs /** @brief Initializes the devfs instance and builds the device inode table. @param device Backing device passed by the generic filesystem interface (not required by devfs). - @return 0 on success, -1 on failure. + @return The result of the mount operation. */ - auto mount(kstd::shared_ptr const & device) -> int override; + auto mount(kstd::shared_ptr const & device) -> operation_result override; /** @brief Looks up an inode by @p name within a @p parent directory. diff --git a/kernel/include/kernel/filesystem/ext2/filesystem.hpp b/kernel/include/kernel/filesystem/ext2/filesystem.hpp index 32374dc..65324c8 100644 --- a/kernel/include/kernel/filesystem/ext2/filesystem.hpp +++ b/kernel/include/kernel/filesystem/ext2/filesystem.hpp @@ -27,9 +27,9 @@ namespace kernel::filesystem::ext2 /** @brief Initializes the ext2 filesystem with the given @p device. @param device The device to mount. - @return 0 on success, negative error code on failure. + @return The result of the mount operation. */ - auto mount(kstd::shared_ptr const & device) -> int override; + auto mount(kstd::shared_ptr const & device) -> operation_result override; /** @brief Looks up an inode by @p name within a @p parent directory. diff --git a/kernel/include/kernel/filesystem/filesystem.hpp b/kernel/include/kernel/filesystem/filesystem.hpp index f855380..ef6929a 100644 --- a/kernel/include/kernel/filesystem/filesystem.hpp +++ b/kernel/include/kernel/filesystem/filesystem.hpp @@ -18,6 +18,14 @@ namespace kernel::filesystem */ struct filesystem { + enum class operation_result : int + { + success = 0, + invalid_magic_number = -1, + invalid_root_inode = -2, + unmount_failed = -3 + }; + /** @brief Virtual destructor for the filesystem. */ @@ -37,9 +45,9 @@ namespace kernel::filesystem /** @brief Initializes the filesystem with the given @p device. @param device The device to mount. - @return 0 on success, or a negative error code on failure. + @return The result of the mount operation. */ - virtual auto mount(kstd::shared_ptr const & device) -> int; + virtual auto mount(kstd::shared_ptr const & device) -> operation_result; /** @brief Looks up a child inode within the given @p parent inode with the specified @p name. This method must be diff --git a/kernel/include/kernel/filesystem/mount.hpp b/kernel/include/kernel/filesystem/mount.hpp index 3e3c69f..0ac6b2f 100644 --- a/kernel/include/kernel/filesystem/mount.hpp +++ b/kernel/include/kernel/filesystem/mount.hpp @@ -24,9 +24,11 @@ namespace kernel::filesystem @param root_dentry The root dentry of the mounted filesystem. @param fs The filesystem instance being mounted. @param mount_path The path at which the filesystem is mounted. + @param parent_mount The parent mount that this mount is attached beneath. */ mount(kstd::shared_ptr const & mount_dentry, kstd::shared_ptr const & root_dentry, - kstd::shared_ptr const & fs, std::string_view mount_path); + kstd::shared_ptr const & fs, std::string_view mount_path, + kstd::shared_ptr const & parent_mount); /** @brief Get the dentry where the filesystem is mounted. @@ -48,11 +50,17 @@ namespace kernel::filesystem */ [[nodiscard]] auto get_mount_path() const -> std::string_view; + /** + @brief Get the parent mount that this mount was attached beneath. + */ + [[nodiscard]] auto get_parent_mount() const -> kstd::shared_ptr const &; + private: kstd::string m_mount_path; kstd::shared_ptr m_mount_dentry; kstd::shared_ptr m_root_dentry; kstd::shared_ptr m_filesystem{}; + kstd::shared_ptr m_parent_mount{}; }; } // namespace kernel::filesystem diff --git a/kernel/include/kernel/filesystem/mount_table.hpp b/kernel/include/kernel/filesystem/mount_table.hpp index a8ef59e..a5cdde6 100644 --- a/kernel/include/kernel/filesystem/mount_table.hpp +++ b/kernel/include/kernel/filesystem/mount_table.hpp @@ -15,11 +15,28 @@ namespace kernel::filesystem */ struct mount_table { + /** + @brief Results for mount table operations. + */ + enum class operation_result : int + { + removed = 0, + has_child_mounts = -1, + mount_not_found = -2 + }; + /** @brief Adds a mount to the table. @param mount The mount to add. */ - void add_mount(kstd::shared_ptr const & mount); + auto add_mount(kstd::shared_ptr const & mount) -> void; + + /** + @brief Removes 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; /** @brief Finds the mount with the longest prefix matching the given @p path. This method is used to determine which @@ -30,6 +47,8 @@ namespace kernel::filesystem [[nodiscard]] auto find_longest_prefix_mount(std::string_view path) const -> kstd::shared_ptr; private: + [[nodiscard]] auto has_child_mounts(kstd::shared_ptr const & parent_mount) const -> bool; + kstd::vector> m_mounts; }; } // namespace kernel::filesystem diff --git a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp index b7e7c6f..0155c41 100644 --- a/kernel/include/kernel/filesystem/rootfs/filesystem.hpp +++ b/kernel/include/kernel/filesystem/rootfs/filesystem.hpp @@ -25,9 +25,9 @@ namespace kernel::filesystem::rootfs /** @brief Initializes the rootfs filesystem with the given @p device. @param device The device to mount (not required by rootfs). - @return 0 on success, negative error code on failure. + @return The result of the mount operation. */ - auto mount(kstd::shared_ptr const & device) -> int override; + auto mount(kstd::shared_ptr const & device) -> operation_result override; /** @brief Looks up an inode by @p name within a @p parent directory. diff --git a/kernel/include/kernel/filesystem/vfs.hpp b/kernel/include/kernel/filesystem/vfs.hpp index 2d05765..4dd2a83 100644 --- a/kernel/include/kernel/filesystem/vfs.hpp +++ b/kernel/include/kernel/filesystem/vfs.hpp @@ -20,6 +20,18 @@ namespace kernel::filesystem */ struct vfs { + /** + @brief Results for VFS operations. + */ + enum class operation_result : int + { + success = 0, + invalid_path = -1, + mount_point_not_found = -2, + filesystem_null = -3, + unmount_failed = -4 + }; + /** @brief Initialize the virtual filesystem. @warning Panics if the VFS has already been initialized. @@ -49,9 +61,16 @@ namespace kernel::filesystem @brief Mount a @p filesystem at a specific @p path. @param path The path where the filesystem should be mounted. @param filesystem The filesystem to mount. - @return 0 on success, or a negative error code on failure. + @return The result of the mount operation. + */ + auto do_mount(std::string_view path, kstd::shared_ptr const & filesystem) -> 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 do_mount(std::string_view path, kstd::shared_ptr const & filesystem) -> int; + auto unmount(std::string_view path) -> operation_result; private: vfs() = default; -- cgit v1.2.3 From e2c08ddb3d79f946399ca5d3bc07b4e6c4de9328 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Wed, 8 Apr 2026 16:21:33 +0200 Subject: add dentry tests --- kernel/include/kernel/filesystem/dentry.hpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to '