diff options
Diffstat (limited to 'kernel/kernel/filesystems/ext2/filesystem.hpp')
| -rw-r--r-- | kernel/kernel/filesystems/ext2/filesystem.hpp | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/kernel/kernel/filesystems/ext2/filesystem.hpp b/kernel/kernel/filesystems/ext2/filesystem.hpp new file mode 100644 index 00000000..cdf552c9 --- /dev/null +++ b/kernel/kernel/filesystems/ext2/filesystem.hpp @@ -0,0 +1,164 @@ +#ifndef TEACHOS_KERNEL_FILESYSTEMS_EXT2_FILESYSTEM_HPP +#define TEACHOS_KERNEL_FILESYSTEMS_EXT2_FILESYSTEM_HPP + +#include <kernel/filesystems/ext2/block_group_descriptor.hpp> +#include <kernel/filesystems/ext2/directory_iterator.hpp> +#include <kernel/filesystems/ext2/inode.hpp> +#include <kernel/filesystems/ext2/linked_directory_entry.hpp> +#include <kernel/filesystems/ext2/mount_state.hpp> +#include <kernel/filesystems/ext2/superblock.hpp> +#include <kernel/filesystems/ext2/write_batch.hpp> +#include <kernel/vfs/filesystem.hpp> +#include <kernel/vfs/inode.hpp> + +#include <kapi/filesystem.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/system_error.hpp> +#include <kstd/units.hpp> +#include <kstd/vector.hpp> + +#include <cstddef> +#include <cstdint> +#include <optional> +#include <span> +#include <string_view> + +namespace kernel::filesystems::ext2 +{ + //! Constants related to the ext2 filesystem. + namespace constants + { + constexpr kstd::bytes inline base_block_size = kstd::bytes{1024}; + constexpr kstd::bytes inline superblock_offset = base_block_size; + constexpr uint16_t inline magic_number = 0xEF53; + + constexpr uint32_t inline good_old_revision = 0; + constexpr uint32_t inline dynamic_revision = 1; + + constexpr uint32_t inline root_inode_number = 2; + + constexpr size_t inline direct_block_count = 12; + constexpr size_t inline singly_indirect_block_index = direct_block_count; + constexpr size_t inline doubly_indirect_block_index = singly_indirect_block_index + 1; + constexpr size_t inline triply_indirect_block_index = doubly_indirect_block_index + 1; + + constexpr uint16_t inline mode_mask = 0xF000; + constexpr uint16_t inline mode_regular = 0x8000; + constexpr uint16_t inline mode_directory = 0x4000; + constexpr uint16_t inline mode_symbolic_link = 0xA000; + constexpr uint16_t inline mode_block_device = 0x6000; + constexpr uint16_t inline mode_character_device = 0x2000; + constexpr uint16_t inline mode_fifo = 0x1000; + constexpr uint16_t inline mode_socket = 0xC000; + + } // namespace constants + + //! The Second Extended Filesystem (ext2) + struct filesystem final : kernel::vfs::filesystem + { + //! @name Driver Interface Implementation + //! @{ + + [[nodiscard]] auto probe(inode_ptr const & backing_inode) const -> kstd::result<std::uint32_t> override; + + auto mount(inode_ptr const & backing_inode) -> kstd::result<mount_result> override; + + [[nodiscard]] auto lookup(inode_ptr const & parent, std::string_view name, driver_data_ptr driver_data) const + -> kstd::result<inode_ptr> override; + + [[nodiscard]] auto create_inode(inode_ptr const & parent, std::string_view name, kapi::filesystem::file_type type, + driver_data_ptr driver_data, + std::optional<kapi::filesystem::device_number> raw_device = std::nullopt) + -> kstd::result<inode_ptr> override; + + //! @} + }; + + //! Get the size of a block in the filesystem. + //! + //! @param state The driver state to operate on. + //! @return The size of a block in bytes. + [[nodiscard]] auto block_size(mount_state const & state) -> kstd::bytes; + + //! Get the number of blocks allocated to an inode. + //! + //! @param inode The inode whose block count to query. + //! @param state The driver state to operate on. + //! @return The number of blocks allocated to the inode. + [[nodiscard]] auto block_count(inode const & inode, mount_state const & state) -> uint32_t; + + //! Determine the size of an inode's data on disk. + //! + //! @param inode The inode whose data size to determine. + //! @param state The driver state to operate on. + //! @return The size of the data referenced by the inode. + [[nodiscard]] auto data_size(inode const & inode, mount_state const & state) -> kstd::bytes; + + //! Set the size of an inode's data on disk. + //! + //! @param inode The inode whose data size to set. + //! @param new_size The size to set. + //! @param state The driver state to operate on. + auto set_data_size(inode & inode, kstd::bytes new_size, mount_state const & state) -> void; + + //! Perform the actual read of the requested data. + //! + //! @param inode The inode whose data to read. + //! @param buffer The buffer to read into. + //! @param offset The offset inside the inode's data to start at. + //! @param state The driver state to operate on. + //! @return The number of bytes read on success, an error otherwise. + [[nodiscard]] auto do_read(inode const & inode, std::span<std::byte> buffer, kstd::bytes offset, + mount_state const & state) -> kstd::result<kstd::bytes>; + + //! Allocate a specified number of blocks. + //! + //! @param count The number of blocks to allocate. + //! @param state The state to operate on. + //! @param batch The active write batch. + //! @return A vector of the allocated block numbers. + auto allocate_blocks(size_t count, mount_state & state, write_batch & batch) -> kstd::result<kstd::vector<uint32_t>>; + + //! Update the number of blocks allocated to an inode. + //! + //! @param data The inode data. + //! @param state The state to operate on. + //! @param delta The change in the number of blocks. + auto update_inode_block_count(inode_data & data, uint32_t delta, mount_state & state) -> void; + + //! Write an inode to the backing inode. + //! + //! @param inode_number The number of the inode to write. + //! @param state The state to operate on. + //! @param data The inode data to write. + auto write_inode(uint32_t inode_number, inode_data const & data, mount_state & state) -> kstd::result<void>; + + //! Get the block number associated with the nth block of an inode. + //! + //! @param index The index of the block within the inode. + //! @param inode The inode. + //! @param state The state to operate on. + //! @return The global block number on success, an error otherwise. + [[nodiscard]] auto inode_block_number(size_t index, inode const & inode, mount_state const & state) + -> kstd::result<std::size_t>; + + //! Write a global block number to an inode block index. + //! + //! @param block_index The index of the block within the inode. + //! @param data The inode data. + //! @param global_block_number The global block number to write. + //! @param state The state to operate on. + //! @param batch The active write batch. + auto write_global_block_number_to_inode_block_index(size_t block_index, inode_data & data, + uint32_t global_block_number, mount_state & state, + write_batch & batch) -> kstd::result<void>; + + [[nodiscard]] auto write_block_group_descriptor(block_group_descriptor const & block_group_descriptor, + size_t block_group_descriptor_index, mount_state const & state) + -> kstd::result<void>; + [[nodiscard]] auto write_superblock(mount_state & state) -> kstd::result<void>; +} // namespace kernel::filesystems::ext2 + +#endif |
