diff options
Diffstat (limited to 'kernel')
27 files changed, 792 insertions, 629 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index ca44e79b..b5486417 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -86,11 +86,13 @@ target_sources("kernel_lib" PRIVATE "kernel/filesystems/ext2/write_batch.cpp" # fat32 Filesystem + "kernel/filesystems/fat32/boot_sector.cpp" "kernel/filesystems/fat32/directory_entry.cpp" "kernel/filesystems/fat32/directory_iterator.cpp" "kernel/filesystems/fat32/error.cpp" "kernel/filesystems/fat32/file_allocation_table.cpp" "kernel/filesystems/fat32/filesystem.cpp" + "kernel/filesystems/fat32/fs_info.cpp" "kernel/filesystems/fat32/inode.cpp" "kernel/filesystems/fat32/module.cpp" diff --git a/kernel/kernel/filesystems/fat32/boot_sector.cpp b/kernel/kernel/filesystems/fat32/boot_sector.cpp new file mode 100644 index 00000000..91733fdf --- /dev/null +++ b/kernel/kernel/filesystems/fat32/boot_sector.cpp @@ -0,0 +1,79 @@ +#include <kernel/filesystems/fat32/boot_sector.hpp> + +#include <kernel/filesystems/fat32/directory_entry.hpp> +#include <kernel/filesystems/fat32/error.hpp> + +#include <kstd/result.hpp> +#include <kstd/span.hpp> +#include <kstd/units.hpp> + +#include <bit> +#include <cstdint> + +using namespace kstd::units_literals; + +namespace kernel::filesystems::fat32 +{ + boot_sector::boot_sector(boot_sector_data const & data) + : m_data(data) + {} + + auto boot_sector::cluster_count() const -> std::uint64_t + { + // is zero for FAT32 because root directory is stored in data-sector + auto const root_dir_size_fat16 = root_entries_count_16() * constants::directory_entry_size; + + // ceiling divide + auto const root_dir_sectors_fat16 = (root_dir_size_fat16 + sector_size() - 1_B) / sector_size(); + + auto const data_region_sectors = total_size_in_sectors() - (reserved_region_size_in_sectors() + + fat_region_size_in_sectors() + root_dir_sectors_fat16); + + return data_region_sectors / sectors_per_cluster(); + } + + auto boot_sector::validate() const -> kstd::result<void> + { + auto is_valid = sector_size() >= constants::min_bytes_per_sector && + sector_size() <= constants::max_bytes_per_sector && reserved_region_size_in_sectors() > 0 && + fats_count() > 0 && + fat_region_size_in_sectors() + reserved_region_size_in_sectors() < total_size_in_sectors() && + std::has_single_bit(sector_size().value) && std::has_single_bit(sectors_per_cluster()); + + if (boot_signature() != constants::boot_signature) + { + return kstd::failure(errc::invalid_boot_signature); + } + if (!is_valid) + { + return kstd::failure(errc::invalid_boot_sector); + } + + if (cluster_count() < constants::min_clusters_fat32) + { + return kstd::failure(errc::unsupported_fat_fs); + } + + return kstd::success(); + } + + auto boot_sector::create(inode_ptr const & backing_inode) -> kstd::result<boot_sector> + { + auto data = fat32::boot_sector_data{}; + + if (auto read_result = backing_inode->read(kstd::raw_bytes(data), constants::boot_sector_offset); !read_result) + { + return kstd::failure(read_result.error()); + } + + auto boot_sector = fat32::boot_sector{data}; + + if (auto result = boot_sector.validate(); !result) + { + return kstd::failure(result.error()); + } + + return kstd::success(boot_sector); + } + +} // namespace kernel::filesystems::fat32
\ No newline at end of file diff --git a/kernel/kernel/filesystems/fat32/boot_sector.hpp b/kernel/kernel/filesystems/fat32/boot_sector.hpp index b5789b92..1753f3e9 100644 --- a/kernel/kernel/filesystems/fat32/boot_sector.hpp +++ b/kernel/kernel/filesystems/fat32/boot_sector.hpp @@ -1,42 +1,52 @@ #ifndef TEACHOS_KERNEL_FILESYSTEMS_FAT32_BOOT_SECTOR_HPP #define TEACHOS_KERNEL_FILESYSTEMS_FAT32_BOOT_SECTOR_HPP +#include <kernel/vfs/filesystem.hpp> + +#include <kstd/result.hpp> +#include <kstd/units.hpp> + #include <array> #include <cstdint> namespace kernel::filesystems::fat32 { - //! The boot sector in the fat32 filesystem. - //! - //! The layout follows the "Microsoft Extensible Firmware Initiative FAT32 File System Specification" (fatgen103). - //! The sector consists of the BIOS Parameter Block (BPB) common to all FAT variants, followed by the FAT32 specific - //! extension. - // cspell:ignore fatgen - struct [[gnu::packed]] boot_sector + namespace constants + { + using namespace kstd::units_literals; + + constexpr kstd::bytes inline boot_sector_offset = 0_B; + constexpr std::uint16_t inline boot_signature = 0xAA55; + constexpr kstd::bytes inline min_bytes_per_sector = 512_B; + constexpr kstd::bytes inline max_bytes_per_sector = 4096_B; + constexpr std::uint16_t inline min_clusters_fat32 = 65525; + } // namespace constants + + struct [[gnu::packed]] boot_sector_data { // Common BIOS Parameter Block std::array<std::uint8_t, 3> jump_boot; std::array<char, 8> oem_name; std::uint16_t bytes_per_sector; std::uint8_t sectors_per_cluster; - std::uint16_t reserved_sector_count; - std::uint8_t number_of_fats; - std::uint16_t root_entry_count; - std::uint16_t total_sectors_16; + std::uint16_t reserved_region_size_in_sectors; + std::uint8_t fats_count; + std::uint16_t root_entries_count_16; + std::uint16_t total_size_in_sectors_16; std::uint8_t media; - std::uint16_t fat_size_16; + std::uint16_t fat_size_in_sectors_16; std::uint16_t sectors_per_track; - std::uint16_t number_of_heads; - std::uint32_t hidden_sectors; - std::uint32_t total_sectors_32; + std::uint16_t heads_count; + std::uint32_t hidden_region_size_in_sectors; + std::uint32_t total_size_in_sectors_32; // FAT32 Extended BIOS Parameter Block - std::uint32_t fat_size_32; + std::uint32_t fat_size_in_sectors_32; std::uint16_t extended_flags; std::uint16_t filesystem_version; - std::uint32_t root_cluster; - std::uint16_t filesystem_info_sector; - std::uint16_t backup_boot_sector; + std::uint32_t root_directory_cluster_index; + std::uint16_t fs_info_offset_in_sectors; + std::uint16_t backup_boot_sector_offset_in_sectors; std::array<std::uint8_t, 12> reserved; // NOLINT(readability-magic-numbers) std::uint8_t drive_number; std::uint8_t reserved1; @@ -47,8 +57,98 @@ namespace kernel::filesystems::fat32 std::array<std::uint8_t, 420> boot_code; // NOLINT(readability-magic-numbers) std::uint16_t boot_signature; }; + static_assert(sizeof(boot_sector_data) == 512, "The fat32 boot sector must be exactly 512 bytes in size."); + + struct boot_sector + { + using inode_ptr = kernel::vfs::filesystem::inode_ptr; + + explicit boot_sector(boot_sector_data const & data); + + auto static create(inode_ptr const & backing_inode) -> kstd::result<boot_sector>; + + [[nodiscard]] auto cluster_count() const -> std::uint64_t; + + [[nodiscard]] auto root_directory_cluster_index() const -> std::uint64_t + { + return m_data.root_directory_cluster_index; + } + + [[nodiscard]] auto fat_size() const -> kstd::bytes + { + return fat_size_in_sectors() * sector_size(); + } + + [[nodiscard]] auto fats_count() const -> std::uint64_t + { + return m_data.fats_count; + } + + [[nodiscard]] auto fs_info_offset() const -> kstd::bytes + { + return m_data.fs_info_offset_in_sectors * sector_size(); + } - static_assert(sizeof(boot_sector) == 512, "The fat32 boot sector must be exactly 512 bytes in size."); + [[nodiscard]] auto fat1_offset() const -> kstd::bytes + { + return reserved_region_size_in_sectors() * sector_size(); + } + + [[nodiscard]] auto cluster_size() const -> kstd::bytes + { + return sector_size() * sectors_per_cluster(); + } + + [[nodiscard]] auto data_region_offset() const -> kstd::bytes + { + return (fat_region_size_in_sectors() + reserved_region_size_in_sectors()) * sector_size(); + } + + private: + boot_sector_data m_data{}; + + [[nodiscard]] auto validate() const -> kstd::result<void>; + + [[nodiscard]] auto fat_size_in_sectors() const noexcept -> std::uint64_t // todo rename + { + return m_data.fat_size_in_sectors_16 != 0 ? m_data.fat_size_in_sectors_16 : m_data.fat_size_in_sectors_32; + } + + [[nodiscard]] auto total_size_in_sectors() const noexcept -> std::uint64_t + { + return m_data.total_size_in_sectors_16 != 0 ? m_data.total_size_in_sectors_16 : m_data.total_size_in_sectors_32; + } + + [[nodiscard]] auto sector_size() const -> kstd::bytes + { + return kstd::bytes{m_data.bytes_per_sector}; + } + + [[nodiscard]] auto sectors_per_cluster() const -> std::uint64_t + { + return m_data.sectors_per_cluster; + } + + [[nodiscard]] auto reserved_region_size_in_sectors() const -> std::uint64_t + { + return m_data.reserved_region_size_in_sectors; + } + + [[nodiscard]] auto fat_region_size_in_sectors() const -> std::uint64_t + { + return fats_count() * fat_size_in_sectors(); + } + + [[nodiscard]] auto root_entries_count_16() const -> std::uint64_t + { + return m_data.root_entries_count_16; + } + + [[nodiscard]] auto boot_signature() const -> std::uint16_t + { + return m_data.boot_signature; + } + }; } // namespace kernel::filesystems::fat32 #endif diff --git a/kernel/kernel/filesystems/fat32/directory_entry.cpp b/kernel/kernel/filesystems/fat32/directory_entry.cpp index 3b448962..24b43474 100644 --- a/kernel/kernel/filesystems/fat32/directory_entry.cpp +++ b/kernel/kernel/filesystems/fat32/directory_entry.cpp @@ -1,12 +1,21 @@ #include <kernel/filesystems/fat32/directory_entry.hpp> +#include <kapi/system.hpp> + +#include <kstd/result.hpp> +#include <kstd/span.hpp> #include <kstd/string.hpp> +#include <kstd/units.hpp> #include <cstdint> +#include <optional> #include <string_view> namespace kernel::filesystems::fat32 { + using kstd::bytes; + using std::optional; + namespace { constexpr auto base_name_length = 8uz; @@ -32,14 +41,64 @@ namespace kernel::filesystems::fat32 } } // namespace + directory_entry::directory_entry(directory_entry_data const & data, std::uint32_t root_directory_cluster_index, + optional<bytes> disk_offset) + : m_data(data) + , m_first_cluster(0) + , m_disk_offset(disk_offset) + { + // A ".." entry pointing to the root directory stores cluster 0 instead of the root cluster. + auto cluster_index_raw = (static_cast<std::uint32_t>(m_data.first_cluster_high) << 16) | m_data.first_cluster_low; + + m_first_cluster = short_name() == ".." && cluster_index_raw == 0 ? root_directory_cluster_index : cluster_index_raw; + } + + auto directory_entry::create(inode_ptr const & backing_inode, std::uint32_t root_directory_cluster_index, + kstd::bytes disk_offset) -> kstd::result<directory_entry> + { + auto data = directory_entry_data{}; + + if (auto read_result = backing_inode->read(kstd::raw_bytes(data), disk_offset); !read_result) + { + return kstd::failure(read_result.error()); + } + + return directory_entry{data, root_directory_cluster_index, disk_offset}; + } + + auto directory_entry::create_root(uint32_t first_cluster) -> directory_entry + { + auto data = directory_entry_data{}; + data.attributes = constants::attribute_directory; + data.first_cluster_high = static_cast<std::uint16_t>(first_cluster >> 16); + data.first_cluster_low = static_cast<std::uint16_t>(first_cluster); + return directory_entry{data, first_cluster, std::nullopt}; // todo + } + + auto directory_entry::write_first_cluster(inode_ptr const & backing_inode, std::uint32_t first_cluster) + -> kstd::result<void> + { + // todo maybe transactional + m_data.first_cluster_high = static_cast<std::uint16_t>(first_cluster >> 16); + m_data.first_cluster_low = static_cast<std::uint16_t>(first_cluster); + return write_data(backing_inode); + } + + auto directory_entry::write_file_size(inode_ptr const & backing_inode, kstd::bytes file_size) -> kstd::result<void> + { + // todo maybe transactional + m_data.file_size = file_size; + return write_data(backing_inode); + } + auto directory_entry::short_name() const -> kstd::string { - auto const raw_name = std::string_view{name.data(), name.size()}; + auto const raw_name = std::string_view{m_data.name.data(), m_data.name.size()}; auto const base_name = trim_padding(raw_name.substr(0, base_name_length)); auto const extension = trim_padding(raw_name.substr(base_name_length)); auto result = kstd::string{}; - append_component(result, base_name, (nt_reserved & constants::lowercase_base_name) != 0); + append_component(result, base_name, (m_data.nt_reserved & constants::lowercase_base_name) != 0); // A leading 0xE5 is a valid character in some code pages and is escaped as 0x05 to not mark the entry as deleted. if (!result.empty() && static_cast<std::uint8_t>(result[0]) == constants::escaped_deleted_entry_marker) @@ -50,9 +109,25 @@ namespace kernel::filesystems::fat32 if (!extension.empty()) { result.push_back('.'); - append_component(result, extension, (nt_reserved & constants::lowercase_extension) != 0); + append_component(result, extension, (m_data.nt_reserved & constants::lowercase_extension) != 0); } return result; } + + auto directory_entry::write_data(inode_ptr const & backing_inode) -> kstd::result<void> + { + if (!m_disk_offset.has_value()) + { + kapi::system::panic("[OS:FAT32] Can't write to root-directory."); // todo allowed to panic + } + + if (auto result = backing_inode->write(kstd::raw_bytes(m_data), m_disk_offset.value()); !result) + { + return kstd::failure(result.error()); + } + + return kstd::success(); + } + } // namespace kernel::filesystems::fat32 diff --git a/kernel/kernel/filesystems/fat32/directory_entry.hpp b/kernel/kernel/filesystems/fat32/directory_entry.hpp index 54b00e57..f9a538fc 100644 --- a/kernel/kernel/filesystems/fat32/directory_entry.hpp +++ b/kernel/kernel/filesystems/fat32/directory_entry.hpp @@ -1,15 +1,18 @@ #ifndef TEACHOS_KERNEL_FILESYSTEMS_FAT32_DIRECTORY_ENTRY_HPP #define TEACHOS_KERNEL_FILESYSTEMS_FAT32_DIRECTORY_ENTRY_HPP +#include <kernel/vfs/filesystem.hpp> + +#include <kstd/result.hpp> #include <kstd/string.hpp> #include <kstd/units.hpp> #include <array> #include <cstdint> +#include <optional> namespace kernel::filesystems::fat32 { - //! Constants related to fat32 directory entries. namespace constants { constexpr std::uint8_t inline attribute_read_only = 0x01; @@ -33,43 +36,72 @@ namespace kernel::filesystems::fat32 constexpr kstd::bytes inline directory_entry_size = kstd::bytes{32}; } // namespace constants - //! A short (8.3) directory entry in the fat32 filesystem. - //! - //! The layout follows the "Microsoft Extensible Firmware Initiative FAT32 File System Specification" (fatgen103). - //! The name is stored as an 8 character base name followed by a 3 character extension, both space padded. - // cspell:ignore fatgen - struct [[gnu::packed]] directory_entry + struct [[gnu::packed]] directory_entry_data { - //! Check if this entry marks the end of its directory, i.e. if it and all following entries are unused. + std::array<char, 11> name; // NOLINT(readability-magic-numbers) + std::uint8_t attributes; + std::uint8_t nt_reserved; + std::uint8_t creation_time_tenth; + std::uint16_t creation_time; + std::uint16_t creation_date; + std::uint16_t last_access_date; + std::uint16_t first_cluster_high; + std::uint16_t write_time; + std::uint16_t write_date; + std::uint16_t first_cluster_low; + std::uint32_t file_size; + }; + + static_assert(sizeof(directory_entry_data) == constants::directory_entry_size.value, + "A fat32 directory entry must be exactly 32 bytes in size."); + + struct directory_entry + { + using inode_ptr = kernel::vfs::filesystem::inode_ptr; + + directory_entry() = default; // todo currently needed because of iterator, maybe in future unnecessary + explicit directory_entry(directory_entry_data const & data, std::uint32_t root_directory_cluster_index, + std::optional<kstd::bytes> disk_offset); + + // todo maybe take driver-state as argument + auto static create(inode_ptr const & backing_inode, std::uint32_t root_directory_cluster_index, + kstd::bytes disk_offset) -> kstd::result<directory_entry>; + + auto static create_root(uint32_t first_cluster) -> directory_entry; + [[nodiscard]] constexpr auto is_end_of_directory() const noexcept -> bool { - return static_cast<std::uint8_t>(name[0]) == constants::end_of_directory_marker; + return static_cast<std::uint8_t>(m_data.name[0]) == constants::end_of_directory_marker; } - //! Check if this entry has been deleted and is free to be reused. [[nodiscard]] constexpr auto is_deleted() const noexcept -> bool { - return static_cast<std::uint8_t>(name[0]) == constants::deleted_entry_marker; + return static_cast<std::uint8_t>(m_data.name[0]) == constants::deleted_entry_marker; } - //! Check if this entry is part of a long file name. [[nodiscard]] constexpr auto is_long_name() const noexcept -> bool { - return (attributes & constants::long_name_mask) == constants::attribute_long_name; + return (m_data.attributes & constants::long_name_mask) == constants::attribute_long_name; } - //! Check if this entry holds the label of the volume. [[nodiscard]] constexpr auto is_volume_label() const noexcept -> bool { - return !is_long_name() && (attributes & constants::attribute_volume_id) != 0; + return !is_long_name() && (m_data.attributes & constants::attribute_volume_id) != 0; } - //! Get the first cluster of the data described by this entry. - //! - //! @return The first cluster, or 0 if no cluster is allocated (e.g. for empty files). [[nodiscard]] constexpr auto first_cluster() const noexcept -> std::uint32_t { - return (static_cast<std::uint32_t>(first_cluster_high) << 16) | first_cluster_low; + return m_first_cluster; + } + + auto set_first_cluster(std::uint32_t first_cluster) -> void + { + m_first_cluster = first_cluster; + } + + [[nodiscard]] constexpr auto attributes() const noexcept -> std::uint8_t + { + return m_data.attributes; } //! Get the name of this entry in its displayed form, e.g. "HELLO.TXT". @@ -78,22 +110,22 @@ namespace kernel::filesystems::fat32 //! @p nt_reserved are honored. [[nodiscard]] auto short_name() const -> kstd::string; - std::array<char, 11> name; // NOLINT(readability-magic-numbers) - std::uint8_t attributes; - std::uint8_t nt_reserved; - std::uint8_t creation_time_tenth; - std::uint16_t creation_time; - std::uint16_t creation_date; - std::uint16_t last_access_date; - std::uint16_t first_cluster_high; - std::uint16_t write_time; - std::uint16_t write_date; - std::uint16_t first_cluster_low; - std::uint32_t file_size; - }; + [[nodiscard]] auto file_size() const noexcept -> kstd::bytes + { + return kstd::bytes{m_data.file_size}; + } - static_assert(sizeof(directory_entry) == constants::directory_entry_size.value, - "A fat32 directory entry must be exactly 32 bytes in size."); + auto write_first_cluster(inode_ptr const & backing_inode, std::uint32_t first_cluster) -> kstd::result<void>; + + auto write_file_size(inode_ptr const & backing_inode, kstd::bytes file_size) -> kstd::result<void>; + + private: + directory_entry_data m_data{}; + std::uint32_t m_first_cluster{}; + std::optional<kstd::bytes> m_disk_offset{}; + + auto write_data(inode_ptr const & backing_inode) -> kstd::result<void>; + }; } // namespace kernel::filesystems::fat32 #endif diff --git a/kernel/kernel/filesystems/fat32/directory_iterator.cpp b/kernel/kernel/filesystems/fat32/directory_iterator.cpp index a179e7b1..ac586151 100644 --- a/kernel/kernel/filesystems/fat32/directory_iterator.cpp +++ b/kernel/kernel/filesystems/fat32/directory_iterator.cpp @@ -6,7 +6,6 @@ #include <kernel/filesystems/fat32/file_allocation_table.hpp> #include <kstd/memory.hpp> -#include <kstd/span.hpp> #include <kstd/system_error.hpp> #include <kstd/units.hpp> @@ -15,11 +14,11 @@ namespace kernel::filesystems::fat32 { - directory_iterator::directory_iterator(std::uint32_t first_cluster, driver_state const & state) + directory_iterator::directory_iterator(std::uint64_t first_cluster, driver_state const & state) : directory_iterator{first_cluster, kstd::bytes{0}, state} {} - directory_iterator::directory_iterator(std::uint32_t first_cluster, kstd::bytes offset, driver_state const & state) + directory_iterator::directory_iterator(std::uint64_t first_cluster, kstd::bytes offset, driver_state const & state) : m_state{&state} , m_cluster{first_cluster} , m_visited_clusters{1} @@ -31,7 +30,7 @@ namespace kernel::filesystems::fat32 return; } - for (auto clusters_to_skip = m_offset / state.cluster_size; clusters_to_skip > 0; --clusters_to_skip) + for (auto clusters_to_skip = m_offset / state.boot_sector.cluster_size(); clusters_to_skip > 0; --clusters_to_skip) { if (!advance_cluster()) { @@ -95,14 +94,16 @@ namespace kernel::filesystems::fat32 { while (m_cluster) { - auto const location = cluster_offset(*m_cluster, *m_state) + m_offset % m_state->cluster_size; + auto const location = cluster_offset(*m_cluster, *m_state) + m_offset % m_state->boot_sector.cluster_size(); - auto entry = directory_entry{}; - if (auto read_result = m_state->backing_inode->read(kstd::raw_bytes(entry), location); !read_result) + auto entry_result = directory_entry::create(m_state->backing_inode, + m_state->boot_sector.root_directory_cluster_index(), location); + if (!entry_result) { - finish(read_result.error()); + finish(entry_result.error()); return; } + auto entry = entry_result.value(); if (entry.is_end_of_directory()) { @@ -115,7 +116,7 @@ namespace kernel::filesystems::fat32 // If the chain ends right behind this entry, the entry is still reported and the next increment ends the // iteration. - if (m_offset % m_state->cluster_size == kstd::bytes{0} && !advance_cluster() && m_error) + if (m_offset % m_state->boot_sector.cluster_size() == kstd::bytes{0} && !advance_cluster() && m_error) { return; } @@ -141,7 +142,7 @@ namespace kernel::filesystems::fat32 return false; } - if (*next && ++m_visited_clusters > m_state->cluster_count) + if (*next && ++m_visited_clusters > m_state->boot_sector.cluster_count()) { finish(errc::corrupted_cluster_chain); return false; diff --git a/kernel/kernel/filesystems/fat32/directory_iterator.hpp b/kernel/kernel/filesystems/fat32/directory_iterator.hpp index 3ce9b855..d2641ec7 100644 --- a/kernel/kernel/filesystems/fat32/directory_iterator.hpp +++ b/kernel/kernel/filesystems/fat32/directory_iterator.hpp @@ -64,14 +64,14 @@ namespace kernel::filesystems::fat32 //! //! @param first_cluster The first cluster of the directory's data. //! @param state The driver state to operate on. - directory_iterator(std::uint32_t first_cluster, driver_state const & state); + directory_iterator(std::uint64_t first_cluster, driver_state const & state); //! Create an iterator positioned at the first entry at or behind a given offset. //! //! @param first_cluster The first cluster of the directory's data. //! @param offset The offset within the directory's data to start at, rounded down to an entry boundary. //! @param state The driver state to operate on. - directory_iterator(std::uint32_t first_cluster, kstd::bytes offset, driver_state const & state); + directory_iterator(std::uint64_t first_cluster, kstd::bytes offset, driver_state const & state); auto operator*() const -> reference; @@ -100,7 +100,7 @@ namespace kernel::filesystems::fat32 auto finish(kstd::error_code error = {}) -> void; kstd::observer_ptr<driver_state const> m_state{}; - std::optional<std::uint32_t> m_cluster{}; + std::optional<std::uint64_t> m_cluster{}; std::size_t m_visited_clusters{}; kstd::bytes m_offset{}; directory_record m_record{}; diff --git a/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp b/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp index c83cf315..a6711a90 100644 --- a/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp +++ b/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp @@ -1,6 +1,7 @@ #include <kernel/filesystems/fat32/directory_iterator.hpp> #include <kernel/devices/storage.hpp> +#include <kernel/filesystems/fat32/boot_sector.hpp> #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/filesystems/fat32/driver_state.hpp> #include <kernel/filesystems/fat32/error.hpp> @@ -18,7 +19,6 @@ #include <algorithm> #include <array> -#include <cstddef> #include <cstdint> #include <filesystem> #include <span> @@ -32,15 +32,16 @@ using namespace kstd::units_literals; namespace { - using kernel::filesystems::fat32::directory_entry; + using kernel::filesystems::fat32::directory_entry_data; using kernel::filesystems::fat32::directory_iterator; using kernel::tests::filesystems::fat32::make_entry; + using kernel::tests::filesystems::fat32::make_entry_data; constexpr auto entries_per_cluster = 16uz; - auto file_entry(std::string_view name) -> directory_entry + auto file_entry(std::string_view name) -> directory_entry_data { - return make_entry(name, kernel::filesystems::fat32::constants::attribute_archive); + return make_entry_data(name, kernel::filesystems::fat32::constants::attribute_archive); } auto collect_names(directory_iterator it) -> std::vector<std::string> @@ -79,22 +80,22 @@ SCENARIO("Fat32 short names are formatted for display", "[filesystem][fat32][rea THEN("the lowercase flags are honored per component") { - auto entry = make_entry("HELLO TXT", 0); - - entry.nt_reserved = constants::lowercase_base_name; + auto entry = make_entry("HELLO TXT", 0, 0, 0, constants::lowercase_base_name); REQUIRE(entry.short_name() == "hello.TXT"); - entry.nt_reserved = constants::lowercase_extension; + entry = make_entry("HELLO TXT", 0, 0, 0, constants::lowercase_extension); REQUIRE(entry.short_name() == "HELLO.txt"); - entry.nt_reserved = constants::lowercase_base_name | constants::lowercase_extension; + entry = make_entry("HELLO TXT", 0, 0, 0, constants::lowercase_base_name | constants::lowercase_extension); REQUIRE(entry.short_name() == "hello.txt"); } THEN("an escaped leading 0xE5 is restored") { - auto entry = make_entry("XAB", 0); - entry.name[0] = static_cast<char>(constants::escaped_deleted_entry_marker); + // todo + auto entry = make_entry("\xE5" + "AB", + 0); auto const name = entry.short_name(); REQUIRE(static_cast<std::uint8_t>(name[0]) == constants::deleted_entry_marker); REQUIRE(std::string_view{name.data() + 1, name.size() - 1} == "AB"); @@ -126,7 +127,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 director auto driver_state = static_pointer_cast<kernel::filesystems::fat32::driver_state>(mount->second); REQUIRE(driver_state); - auto const root_cluster = driver_state->boot_sector.root_cluster; + auto const root_cluster = driver_state->boot_sector.root_directory_cluster_index(); THEN("the volume label is skipped and only HELLO.TXT is found") { @@ -138,7 +139,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 director auto it = directory_iterator{root_cluster, *driver_state}; REQUIRE(it != directory_iterator{}); REQUIRE(it->entry.first_cluster() == 3); - REQUIRE(it->entry.file_size == 12); + REQUIRE(it->entry.file_size() == 12_B); REQUIRE(it->offset == 32_B); REQUIRE(it->location == kernel::filesystems::fat32::cluster_offset(root_cluster, *driver_state) + 32_B); REQUIRE(it->inode_number() == it->location.value); @@ -176,12 +177,12 @@ SCENARIO("Fat32 directory_iterator on a mock layout", "[filesystem][fat32][readd deleted.name[0] = static_cast<char>(constants::deleted_entry_marker); auto const entries = std::array{ - make_entry("LABEL", constants::attribute_volume_id), - make_entry("Ah\0e\0l\0l\0o", constants::attribute_long_name), + make_entry_data("LABEL", constants::attribute_volume_id), + make_entry_data("Ah\0e\0l\0l\0o", constants::attribute_long_name), file_entry("HELLO TXT"), deleted, - make_entry("SUBDIR", constants::attribute_directory, 5), - directory_entry{}, + make_entry_data("SUBDIR", constants::attribute_directory, 5), + directory_entry_data{}, file_entry("HIDDEN TXT"), }; kernel::tests::filesystems::fat32::write_directory_entries(*device, 2, entries); @@ -195,7 +196,7 @@ SCENARIO("Fat32 directory_iterator on a mock layout", "[filesystem][fat32][readd { auto it = directory_iterator{2, *driver_state}; REQUIRE(it->offset == 64_B); - REQUIRE(it->location == 1024_B + 64_B); + REQUIRE(it->location == 1536_B + 64_B); ++it; REQUIRE(it->offset == 128_B); REQUIRE(it->entry.first_cluster() == 5); @@ -227,7 +228,7 @@ SCENARIO("Fat32 directory_iterator on a mock layout", "[filesystem][fat32][readd WHEN("the directory spans two clusters") { - auto first_cluster_entries = std::array<directory_entry, entries_per_cluster>{}; + auto first_cluster_entries = std::array<directory_entry_data, entries_per_cluster>{}; for (auto i = 0uz; i < first_cluster_entries.size(); ++i) { first_cluster_entries[i] = file_entry(std::string{"FILE"} + static_cast<char>('A' + i)); @@ -263,7 +264,7 @@ SCENARIO("Fat32 directory_iterator on a mock layout", "[filesystem][fat32][readd WHEN("a full directory has no end marker") { - auto entries = std::array<directory_entry, entries_per_cluster>{}; + auto entries = std::array<directory_entry_data, entries_per_cluster>{}; std::ranges::fill(entries, file_entry("FILE")); kernel::tests::filesystems::fat32::write_directory_entries(*device, 6, entries); write_fat_entry(*device, 6, 0x0FFF'FFFF); @@ -285,10 +286,22 @@ SCENARIO("Fat32 directory_iterator on a mock layout", "[filesystem][fat32][readd REQUIRE(it.error() == errc::invalid_cluster); } + // todo WHEN("the directory's chain is cyclic") { - driver_state->cluster_count = 16; - auto entries = std::array<directory_entry, entries_per_cluster>{}; + // todo + auto data = kernel::filesystems::fat32::boot_sector_data{}; + data.bytes_per_sector = 512; + data.sectors_per_cluster = 1; + data.reserved_region_size_in_sectors = 2; + data.fats_count = 1; + data.fat_size_in_sectors_32 = 1; + data.total_size_in_sectors_32 = 18; + data.fs_info_offset_in_sectors = 1; + + driver_state->boot_sector = kernel::filesystems::fat32::boot_sector{data}; + + auto entries = std::array<directory_entry_data, entries_per_cluster>{}; std::ranges::fill(entries, file_entry("LOOP")); kernel::tests::filesystems::fat32::write_directory_entries(*device, 13, entries); kernel::tests::filesystems::fat32::write_directory_entries(*device, 14, entries); @@ -310,7 +323,7 @@ SCENARIO("Fat32 directory_iterator on a mock layout", "[filesystem][fat32][readd WHEN("the directory's chain runs into a free cluster") { - auto entries = std::array<directory_entry, entries_per_cluster>{}; + auto entries = std::array<directory_entry_data, entries_per_cluster>{}; std::ranges::fill(entries, file_entry("FILE")); kernel::tests::filesystems::fat32::write_directory_entries(*device, 8, entries); kernel::tests::filesystems::fat32::write_directory_entries(*device, 9, entries); diff --git a/kernel/kernel/filesystems/fat32/driver_state.hpp b/kernel/kernel/filesystems/fat32/driver_state.hpp index 0f25f759..faf6deb5 100644 --- a/kernel/kernel/filesystems/fat32/driver_state.hpp +++ b/kernel/kernel/filesystems/fat32/driver_state.hpp @@ -9,32 +9,16 @@ #include <kapi/tracked_mutex.hpp> #include <kstd/memory.hpp> -#include <kstd/units.hpp> - -#include <cstdint> -#include <optional> namespace kernel::filesystems::fat32 { struct driver_state : vfs::driver_state { - struct boot_sector boot_sector{}; + struct boot_sector boot_sector{boot_sector_data{}}; + struct fs_info fs_info{0, 0}; // todo does this default init make sense? kstd::shared_ptr<kernel::vfs::inode> backing_inode{}; - kstd::bytes sector_size{}; - kstd::bytes cluster_size{}; - std::uint32_t fat_start_sector{}; - std::uint32_t data_start_sector{}; - std::uint32_t cluster_count{}; - - //! The offset of the filesystem information sector, @c std::nullopt if the volume has no valid one. - std::optional<kstd::bytes> fs_info_offset{}; - //! The number of free clusters, @c constants::fs_info_unknown if not known. - std::uint32_t free_cluster_count{constants::fs_info_unknown}; - //! The cluster to start searching for a free cluster at, @c constants::fs_info_unknown if not known. - std::uint32_t next_free_cluster{constants::fs_info_unknown}; - mutable kapi::tracked_mutex lock{}; }; diff --git a/kernel/kernel/filesystems/fat32/error.cpp b/kernel/kernel/filesystems/fat32/error.cpp index 1795107e..ba60a20c 100644 --- a/kernel/kernel/filesystems/fat32/error.cpp +++ b/kernel/kernel/filesystems/fat32/error.cpp @@ -36,6 +36,8 @@ namespace kernel::filesystems::fat32 return "no free clusters"; case errc::file_too_large: return "file too large"; + case errc::invalid_fs_info_signature: + return "invalid fs-info signature"; default: return "unknown fat32 error"; }; @@ -51,6 +53,7 @@ namespace kernel::filesystems::fat32 case errc::unsupported_fat_fs: case errc::invalid_cluster: case errc::corrupted_cluster_chain: + case errc::invalid_fs_info_signature: if (condition.category() == kernel::vfs::category()) { return condition.value() == static_cast<int>(kernel::vfs::errc::invalid_filesystem); diff --git a/kernel/kernel/filesystems/fat32/error.hpp b/kernel/kernel/filesystems/fat32/error.hpp index 1cc6d584..d512e778 100644 --- a/kernel/kernel/filesystems/fat32/error.hpp +++ b/kernel/kernel/filesystems/fat32/error.hpp @@ -18,6 +18,7 @@ namespace kernel::filesystems::fat32 corrupted_cluster_chain, no_free_clusters, file_too_large, + invalid_fs_info_signature }; [[nodiscard]] auto category() noexcept -> kstd::error_category const &; diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.cpp b/kernel/kernel/filesystems/fat32/file_allocation_table.cpp index 2390c22a..b897f9f0 100644 --- a/kernel/kernel/filesystems/fat32/file_allocation_table.cpp +++ b/kernel/kernel/filesystems/fat32/file_allocation_table.cpp @@ -10,7 +10,6 @@ #include <kstd/vector.hpp> #include <algorithm> -#include <array> #include <cstddef> #include <cstdint> #include <optional> @@ -34,41 +33,21 @@ namespace kernel::filesystems::fat32 [[nodiscard]] auto fat_entry_offset(std::uint32_t cluster, std::uint32_t fat_index, driver_state const & state) -> kstd::bytes { - auto const fat_start_sector = state.fat_start_sector + fat_index * state.boot_sector.fat_size_32; - return fat_start_sector * state.sector_size + cluster * constants::fat_entry_size; - } - - //! Write the free cluster hints of the driver state to the filesystem information sector, if the volume has one. - //! - //! @param state The driver state to operate on. - //! @return Nothing on success, an error otherwise. - [[nodiscard]] auto write_fs_info_hints(driver_state const & state) -> kstd::result<void> - { - if (!state.fs_info_offset) - { - return {}; - } - - auto const hints = std::array{state.free_cluster_count, state.next_free_cluster}; - auto const hints_offset = *state.fs_info_offset + kstd::bytes{offsetof(fs_info, free_cluster_count)}; - if (auto const write_result = state.backing_inode->write(kstd::raw_bytes(hints), hints_offset); !write_result) - { - return kstd::failure(write_result.error()); - } - - return {}; + auto const fat_offset = state.boot_sector.fat1_offset() + (fat_index * state.boot_sector.fat_size()); + return fat_offset + cluster * constants::fat_entry_size; } } // namespace auto is_valid_cluster(std::uint32_t cluster, driver_state const & state) -> bool { - return cluster >= constants::first_data_cluster && cluster - constants::first_data_cluster < state.cluster_count; + return cluster >= constants::first_data_cluster && + cluster - constants::first_data_cluster < state.boot_sector.cluster_count(); } auto cluster_offset(std::uint32_t cluster, driver_state const & state) -> kstd::bytes { auto const cluster_index = cluster - constants::first_data_cluster; - return state.data_start_sector * state.sector_size + cluster_index * state.cluster_size; + return state.boot_sector.data_region_offset() + cluster_index * state.boot_sector.cluster_size(); } auto read_fat_entry(std::uint32_t cluster, driver_state const & state) -> kstd::result<fat_entry> @@ -78,7 +57,7 @@ namespace kernel::filesystems::fat32 return kstd::failure(errc::invalid_cluster); } - auto const entry_offset = state.fat_start_sector * state.sector_size + cluster * constants::fat_entry_size; + auto const entry_offset = state.boot_sector.fat1_offset() + cluster * constants::fat_entry_size; auto raw_entry = std::uint32_t{}; if (auto read_result = state.backing_inode->read(kstd::raw_bytes(raw_entry), entry_offset); !read_result) { @@ -139,7 +118,7 @@ namespace kernel::filesystems::fat32 while (current) { - if (chain.size() >= state.cluster_count) + if (chain.size() >= state.boot_sector.cluster_count()) { return kstd::failure(errc::corrupted_cluster_chain); } @@ -176,7 +155,7 @@ namespace kernel::filesystems::fat32 return kstd::failure(next.error()); } - if (*next && ++visited_clusters > state.cluster_count) + if (*next && ++visited_clusters > state.boot_sector.cluster_count()) { return kstd::failure(errc::corrupted_cluster_chain); } @@ -185,7 +164,8 @@ namespace kernel::filesystems::fat32 return {}; }; - for (auto clusters_to_skip = offset / state.cluster_size; current && clusters_to_skip > 0; --clusters_to_skip) + for (auto clusters_to_skip = offset / state.boot_sector.cluster_size(); current && clusters_to_skip > 0; + --clusters_to_skip) { if (auto advance_result = advance(); !advance_result) { @@ -194,12 +174,13 @@ namespace kernel::filesystems::fat32 } auto const requested_size = kstd::bytes{buffer.size()}; - auto in_cluster_offset = offset % state.cluster_size; + auto in_cluster_offset = offset % state.boot_sector.cluster_size(); auto bytes_read = 0_B; while (current && bytes_read < requested_size) { - auto const bytes_to_read = std::min(requested_size - bytes_read, state.cluster_size - in_cluster_offset); + auto const bytes_to_read = + std::min(requested_size - bytes_read, state.boot_sector.cluster_size() - in_cluster_offset); auto const read_offset = cluster_offset(*current, state) + in_cluster_offset; auto const read_result = @@ -236,7 +217,7 @@ namespace kernel::filesystems::fat32 return kstd::failure(errc::invalid_cluster); } - for (auto fat_index = std::uint32_t{}; fat_index < state.boot_sector.number_of_fats; ++fat_index) + for (auto fat_index = std::uint32_t{}; fat_index < state.boot_sector.fats_count(); ++fat_index) { auto const entry_offset = fat_entry_offset(cluster, fat_index, state); @@ -265,13 +246,14 @@ namespace kernel::filesystems::fat32 } // The hint is only a suggestion where to start searching, so an out of range hint is ignored. - auto const start_cluster = - is_valid_cluster(state.next_free_cluster, state) ? state.next_free_cluster : constants::first_data_cluster; + auto const start_cluster = is_valid_cluster(state.fs_info.next_free_cluster(), state) + ? state.fs_info.next_free_cluster() + : constants::first_data_cluster; - for (auto offset = std::uint32_t{}; offset < state.cluster_count; ++offset) + for (auto offset = std::uint32_t{}; offset < state.boot_sector.cluster_count(); ++offset) { auto const cluster = constants::first_data_cluster + - (start_cluster - constants::first_data_cluster + offset) % state.cluster_count; + (start_cluster - constants::first_data_cluster + offset) % state.boot_sector.cluster_count(); auto const entry = read_fat_entry(cluster, state); if (!entry) @@ -298,15 +280,18 @@ namespace kernel::filesystems::fat32 } } - state.next_free_cluster = cluster; - if (state.free_cluster_count != constants::fs_info_unknown && state.free_cluster_count > 0) + if (auto const result = state.fs_info.write_next_free_cluster(state, cluster); !result) { - --state.free_cluster_count; + return kstd::failure(result.error()); } - if (auto const write_result = write_fs_info_hints(state); !write_result) + auto free_cluster_count = state.fs_info.free_cluster_count(); + if (free_cluster_count != constants::fs_info_unknown && free_cluster_count > 0) { - return kstd::failure(write_result.error()); + if (auto const result = state.fs_info.write_free_cluster_count(state, --free_cluster_count); !result) + { + return kstd::failure(result.error()); + } } return cluster; @@ -323,7 +308,7 @@ namespace kernel::filesystems::fat32 return kstd::failure(errc::invalid_cluster); } - if (cluster_count > state.cluster_count) + if (cluster_count > state.boot_sector.cluster_count()) { return kstd::failure(errc::no_free_clusters); } @@ -380,7 +365,7 @@ namespace kernel::filesystems::fat32 }; auto current = first_cluster; - for (auto clusters_to_skip = offset / state.cluster_size; clusters_to_skip > 0; --clusters_to_skip) + for (auto clusters_to_skip = offset / state.boot_sector.cluster_size(); clusters_to_skip > 0; --clusters_to_skip) { auto const next = advance(current); if (!next) @@ -392,12 +377,13 @@ namespace kernel::filesystems::fat32 } auto const requested_size = kstd::bytes{buffer.size()}; - auto in_cluster_offset = offset % state.cluster_size; + auto in_cluster_offset = offset % state.boot_sector.cluster_size(); auto bytes_written = 0_B; while (bytes_written < requested_size) { - auto const bytes_to_write = std::min(requested_size - bytes_written, state.cluster_size - in_cluster_offset); + auto const bytes_to_write = + std::min(requested_size - bytes_written, state.boot_sector.cluster_size() - in_cluster_offset); auto const write_offset = cluster_offset(current, state) + in_cluster_offset; auto const write_result = diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.hpp b/kernel/kernel/filesystems/fat32/file_allocation_table.hpp index 43cf2de5..048939bf 100644 --- a/kernel/kernel/filesystems/fat32/file_allocation_table.hpp +++ b/kernel/kernel/filesystems/fat32/file_allocation_table.hpp @@ -14,6 +14,7 @@ namespace kernel::filesystems::fat32 { + using namespace kstd::units_literals; namespace constants { constexpr std::uint32_t inline first_data_cluster = 2; @@ -22,7 +23,7 @@ namespace kernel::filesystems::fat32 constexpr std::uint32_t inline reserved_cluster = 0x0000'0001; constexpr std::uint32_t inline bad_cluster = 0x0FFF'FFF7; constexpr std::uint32_t inline end_of_chain = 0x0FFF'FFF8; - constexpr kstd::bytes inline fat_entry_size = kstd::bytes{sizeof(std::uint32_t)}; + constexpr kstd::bytes inline fat_entry_size = 4_B; } // namespace constants enum struct fat_entry_type : std::uint8_t diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp b/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp index e6f56728..41a9169c 100644 --- a/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp +++ b/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp @@ -1,6 +1,7 @@ #include <kernel/filesystems/fat32/file_allocation_table.hpp> #include <kernel/devices/storage.hpp> +#include <kernel/filesystems/fat32/boot_sector.hpp> #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/filesystems/fat32/driver_state.hpp> #include <kernel/filesystems/fat32/error.hpp> @@ -53,8 +54,8 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 cluster THEN("the root directory occupies a single cluster") { - auto const chain = - kernel::filesystems::fat32::cluster_chain(driver_state->boot_sector.root_cluster, *driver_state); + auto const chain = kernel::filesystems::fat32::cluster_chain( + driver_state->boot_sector.root_directory_cluster_index(), *driver_state); REQUIRE(chain); REQUIRE(*chain == kstd::vector<std::uint32_t>{2}); } @@ -84,9 +85,10 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 cluster { using namespace kernel::filesystems::fat32; - auto entries = std::array<directory_entry, 3>{}; + auto entries = std::array<directory_entry_data, 3>{}; auto const buffer = std::as_writable_bytes(std::span{entries}); - auto const read_result = read_cluster_chain(driver_state->boot_sector.root_cluster, buffer, 0_B, *driver_state); + auto const read_result = + read_cluster_chain(driver_state->boot_sector.root_directory_cluster_index(), buffer, 0_B, *driver_state); REQUIRE(read_result); REQUIRE(*read_result == kstd::bytes{buffer.size()}); @@ -148,8 +150,8 @@ SCENARIO("Fat32 cluster chain traversal", "[filesystem][fat32][fat]") THEN("cluster offsets start at the data region") { - REQUIRE(kernel::filesystems::fat32::cluster_offset(2, *driver_state) == 1024_B); - REQUIRE(kernel::filesystems::fat32::cluster_offset(3, *driver_state) == 1536_B); + REQUIRE(kernel::filesystems::fat32::cluster_offset(2, *driver_state) == 1536_B); + REQUIRE(kernel::filesystems::fat32::cluster_offset(3, *driver_state) == 2048_B); } THEN("entries are classified by their value") @@ -355,7 +357,17 @@ SCENARIO("Fat32 cluster chain reading", "[filesystem][fat32][fat]") THEN("a cyclic chain is detected while reading and while skipping") { - driver_state->cluster_count = 16; + // todo + auto data = kernel::filesystems::fat32::boot_sector_data{}; + data.bytes_per_sector = 512; + data.sectors_per_cluster = 1; + data.reserved_region_size_in_sectors = 2; + data.fats_count = 1; + data.fat_size_in_sectors_32 = 1; + data.total_size_in_sectors_32 = 18; + data.fs_info_offset_in_sectors = 1; + + driver_state->boot_sector = kernel::filesystems::fat32::boot_sector{data}; write_fat_entry(*device, 13, 14); write_fat_entry(*device, 14, 13); @@ -393,7 +405,7 @@ SCENARIO("Fat32 cluster allocation on a mock layout", "[filesystem][fat32][fat][ REQUIRE(kernel::filesystems::fat32::write_fat_entry(3, 0xFFFF'FFF8, *driver_state)); auto raw_entry = std::uint32_t{}; - REQUIRE(dev_inode->read(kstd::raw_bytes(raw_entry), 512_B + 3 * constants::fat_entry_size)); + REQUIRE(dev_inode->read(kstd::raw_bytes(raw_entry), 1024_B + 3 * constants::fat_entry_size)); REQUIRE(raw_entry == 0xFFFF'FFF8); } @@ -407,7 +419,7 @@ SCENARIO("Fat32 cluster allocation on a mock layout", "[filesystem][fat32][fat][ auto const cluster = allocate_cluster(std::nullopt, *driver_state); REQUIRE(cluster == 3u); REQUIRE(next_cluster(3, *driver_state) == std::nullopt); - REQUIRE(driver_state->next_free_cluster == 3); + REQUIRE(driver_state->fs_info.next_free_cluster() == 3); } THEN("an allocated cluster is appended to the given chain") @@ -420,8 +432,19 @@ SCENARIO("Fat32 cluster allocation on a mock layout", "[filesystem][fat32][fat][ THEN("the search starts at the next free cluster hint and wraps around") { - driver_state->cluster_count = 8; - driver_state->next_free_cluster = 8; + // todo + auto data = kernel::filesystems::fat32::boot_sector_data{}; + data.bytes_per_sector = 512; + data.sectors_per_cluster = 1; + data.reserved_region_size_in_sectors = 2; + data.fats_count = 1; + data.fat_size_in_sectors_32 = 1; + data.total_size_in_sectors_32 = 10; + data.fs_info_offset_in_sectors = 1; + + driver_state->boot_sector = kernel::filesystems::fat32::boot_sector{data}; + + REQUIRE(driver_state->fs_info.write_next_free_cluster(*driver_state, 8)); write_fat_entry(*device, 8, 0x0FFF'FFFF); write_fat_entry(*device, 9, 0x0FFF'FFFF); @@ -430,14 +453,25 @@ SCENARIO("Fat32 cluster allocation on a mock layout", "[filesystem][fat32][fat][ THEN("the free cluster count is decremented if it is known") { - driver_state->free_cluster_count = 10; + auto const old = driver_state->fs_info.free_cluster_count(); REQUIRE(allocate_cluster(std::nullopt, *driver_state)); - REQUIRE(driver_state->free_cluster_count == 9); + REQUIRE(driver_state->fs_info.free_cluster_count() == old - 1); } THEN("allocation fails if no cluster is free") { - driver_state->cluster_count = 2; + // todo + auto data = kernel::filesystems::fat32::boot_sector_data{}; + data.bytes_per_sector = 512; + data.sectors_per_cluster = 1; + data.reserved_region_size_in_sectors = 2; + data.fats_count = 1; + data.fat_size_in_sectors_32 = 1; + data.total_size_in_sectors_32 = 4; + data.fs_info_offset_in_sectors = 1; + + driver_state->boot_sector = kernel::filesystems::fat32::boot_sector{data}; + write_fat_entry(*device, 3, 0x0FFF'FFFF); REQUIRE(allocate_cluster(std::nullopt, *driver_state).error() == errc::no_free_clusters); } diff --git a/kernel/kernel/filesystems/fat32/filesystem.cpp b/kernel/kernel/filesystems/fat32/filesystem.cpp index be104f20..59621022 100644 --- a/kernel/kernel/filesystems/fat32/filesystem.cpp +++ b/kernel/kernel/filesystems/fat32/filesystem.cpp @@ -15,15 +15,11 @@ #include <kstd/memory.hpp> #include <kstd/mutex.hpp> -#include <kstd/print.hpp> #include <kstd/result.hpp> -#include <kstd/span.hpp> #include <kstd/system_error.hpp> -#include <kstd/units.hpp> #include <algorithm> #include <array> -#include <bit> #include <cstddef> #include <cstdint> #include <optional> @@ -46,38 +42,6 @@ namespace kernel::filesystems::fat32 return {array.data(), array.size()}; } - // TODO: Remove later, just for demo - auto print_boot_sector(boot_sector const & boot_sector) -> void - { - kstd::println("[OS:FAT32] jump_boot: {:#04X} {:#04X} {:#04X}", boot_sector.jump_boot[0], boot_sector.jump_boot[1], - boot_sector.jump_boot[2]); - kstd::println("[OS:FAT32] oem_name: '{}'", to_string_view(boot_sector.oem_name)); - kstd::println("[OS:FAT32] bytes_per_sector: {}", boot_sector.bytes_per_sector); - kstd::println("[OS:FAT32] sectors_per_cluster: {}", boot_sector.sectors_per_cluster); - kstd::println("[OS:FAT32] reserved_sector_count: {}", boot_sector.reserved_sector_count); - kstd::println("[OS:FAT32] number_of_fats: {}", boot_sector.number_of_fats); - kstd::println("[OS:FAT32] root_entry_count: {}", boot_sector.root_entry_count); - kstd::println("[OS:FAT32] total_sectors_16: {}", boot_sector.total_sectors_16); - kstd::println("[OS:FAT32] media: {:#04X}", boot_sector.media); - kstd::println("[OS:FAT32] fat_size_16: {}", boot_sector.fat_size_16); - kstd::println("[OS:FAT32] sectors_per_track: {}", boot_sector.sectors_per_track); - kstd::println("[OS:FAT32] number_of_heads: {}", boot_sector.number_of_heads); - kstd::println("[OS:FAT32] hidden_sectors: {}", boot_sector.hidden_sectors); - kstd::println("[OS:FAT32] total_sectors_32: {}", boot_sector.total_sectors_32); - kstd::println("[OS:FAT32] fat_size_32: {}", boot_sector.fat_size_32); - kstd::println("[OS:FAT32] extended_flags: {:#06X}", boot_sector.extended_flags); - kstd::println("[OS:FAT32] filesystem_version: {:#06X}", boot_sector.filesystem_version); - kstd::println("[OS:FAT32] root_cluster: {}", boot_sector.root_cluster); - kstd::println("[OS:FAT32] filesystem_info_sector: {}", boot_sector.filesystem_info_sector); - kstd::println("[OS:FAT32] backup_boot_sector: {}", boot_sector.backup_boot_sector); - kstd::println("[OS:FAT32] drive_number: {:#04X}", boot_sector.drive_number); - kstd::println("[OS:FAT32] extended_boot_signature: {:#04X}", boot_sector.extended_boot_signature); - kstd::println("[OS:FAT32] volume_id: {:#010X}", boot_sector.volume_id); - kstd::println("[OS:FAT32] volume_label: '{}'", to_string_view(boot_sector.volume_label)); - kstd::println("[OS:FAT32] filesystem_type: '{}'", to_string_view(boot_sector.filesystem_type)); - kstd::println("[OS:FAT32] boot_signature: {:#06X}", boot_sector.boot_signature); - } - //! Compare two names, ignoring the case of ASCII letters. //! //! Short names are stored in upper case, but are matched case-insensitively, e.g. "hello.txt" finds "HELLO.TXT". @@ -89,162 +53,16 @@ namespace kernel::filesystems::fat32 return std::ranges::equal(lhs, rhs, {}, to_lower, to_lower); } - - enum struct fat_type - { - fat12, - fat16, - fat32 - }; - - //! basic boot-sector validation - //! - //! @param bs populated boot-sector - //! @return true on valid boot-sector, else false - auto validate_boot_sector(boot_sector const & bs) -> bool - { - auto const fat_sectors = bs.fat_size_16 != 0 ? std::uint64_t{bs.fat_size_16} : bs.fat_size_32; - auto const total_sectors = bs.total_sectors_16 != 0 ? std::uint64_t{bs.total_sectors_16} : bs.total_sectors_32; - - auto const fat_region_sectors = std::uint64_t{bs.number_of_fats} * fat_sectors; - - return bs.bytes_per_sector >= constants::min_bytes_per_sector && - bs.bytes_per_sector <= constants::max_bytes_per_sector && bs.reserved_sector_count > 0 && - bs.number_of_fats > 0 && fat_region_sectors + bs.reserved_sector_count < total_sectors && - std::has_single_bit(bs.bytes_per_sector) && std::has_single_bit(bs.sectors_per_cluster); - } - - //! Derive the fat-type with the variables in the boot-sector. The differentiation is made by number of clusters in - //! the data-region following microsoft specification 1.03. This function doesn't validate the provided boot_sector - //! and necessitates that it's valid. - //! - //! @param bs populated boot-sector - //! @return returns fat12, fat16 or fat32 - auto get_fat_type(boot_sector const & bs) -> fat_type - { - // is zero for FAT32 because root directory is stored in data-sector - auto const root_dir_size_fat16 = std::uint64_t{bs.root_entry_count} * 32; // todo maybe constant - - // ceiling divide - auto const root_dir_sectors_fat16 = (root_dir_size_fat16 + bs.bytes_per_sector - 1) / bs.bytes_per_sector; - - auto const fat_sectors = bs.fat_size_16 != 0 ? std::uint64_t{bs.fat_size_16} : bs.fat_size_32; - auto const total_sectors = bs.total_sectors_16 != 0 ? std::uint64_t{bs.total_sectors_16} : bs.total_sectors_32; - - auto const fat_region_sectors = bs.number_of_fats * fat_sectors; - - auto const data_region_sectors = - total_sectors - (bs.reserved_sector_count + fat_region_sectors + root_dir_sectors_fat16); - - auto const data_region_clusters = data_region_sectors / bs.sectors_per_cluster; - - if (data_region_clusters <= constants::max_clusters_fat12) - return fat_type::fat12; - - if (data_region_clusters <= constants::max_clusters_fat16) - return fat_type::fat16; - - return fat_type::fat32; - } - - //! Read the boot sector from a backing inode and check its signature. - //! - //! @param backing_inode The inode to read the boot sector from. - //! @return The boot sector on success, an error otherwise. - [[nodiscard]] auto read_boot_sector(kernel::vfs::filesystem::inode_ptr const & backing_inode) - -> kstd::result<boot_sector> - { - auto boot_sector = fat32::boot_sector{}; - if (auto read_result = backing_inode->read(kstd::raw_bytes(boot_sector), constants::boot_sector_offset); - !read_result) - { - return kstd::failure(read_result.error()); - } - - if (boot_sector.boot_signature != constants::boot_signature) - { - return kstd::failure(errc::invalid_boot_signature); - } - - if (!validate_boot_sector(boot_sector)) - { - return kstd::failure(errc::invalid_boot_sector); - } - - if (get_fat_type(boot_sector) != fat_type::fat32) - { - return kstd::failure(errc::unsupported_fat_fs); // todo custom error - } - - return boot_sector; - } - - //! Derive the location of the file allocation table and the data region from the boot sector. - //! - //! @param state The driver state whose boot sector to use and whose layout fields to fill. - auto compute_layout(driver_state & state) -> void - { - auto const & boot_sector = state.boot_sector; - - auto const bytes_per_sector = boot_sector.bytes_per_sector; - auto const sectors_per_cluster = boot_sector.sectors_per_cluster; - auto const total_sectors = boot_sector.total_sectors_32 != 0 ? std::uint64_t{boot_sector.total_sectors_32} - : std::uint64_t{boot_sector.total_sectors_16}; - auto const fat_sectors = std::uint64_t{boot_sector.number_of_fats} * boot_sector.fat_size_32; - auto const data_start_sector = boot_sector.reserved_sector_count + fat_sectors; - auto const cluster_count = (total_sectors - data_start_sector) / sectors_per_cluster; - - state.sector_size = kstd::bytes{bytes_per_sector}; - state.cluster_size = state.sector_size * sectors_per_cluster; - state.fat_start_sector = boot_sector.reserved_sector_count; - state.data_start_sector = static_cast<std::uint32_t>(data_start_sector); - state.cluster_count = static_cast<std::uint32_t>(cluster_count); - } - - //! Read the free cluster hints from the filesystem information sector, if the volume has a valid one. - //! - //! The sector is optional, so a missing or invalid sector is not an error. The hints then remain unknown. - //! - //! @param state The driver state whose boot sector to use and whose hint fields to fill. - //! @return Nothing on success, an error if the sector could not be read. - [[nodiscard]] auto read_fs_info(driver_state & state) -> kstd::result<void> - { - auto const fs_info_sector = state.boot_sector.filesystem_info_sector; - if (fs_info_sector == 0 || fs_info_sector >= state.boot_sector.reserved_sector_count) - { - return {}; - } - - auto const fs_info_offset = fs_info_sector * state.sector_size; - auto fs_info = fat32::fs_info{}; - if (auto const read_result = state.backing_inode->read(kstd::raw_bytes(fs_info), fs_info_offset); !read_result) - { - return kstd::failure(read_result.error()); - } - - if (!fs_info.is_valid()) - { - return {}; - } - - state.fs_info_offset = fs_info_offset; - state.free_cluster_count = - fs_info.free_cluster_count <= state.cluster_count ? fs_info.free_cluster_count : constants::fs_info_unknown; - state.next_free_cluster = fs_info.next_free_cluster; - return {}; - } } // namespace - auto filesystem::probe(inode_ptr const & inode) const -> kstd::result<std::uint32_t> + auto filesystem::probe(inode_ptr const & backing_inode) const -> kstd::result<std::uint32_t> { - auto const boot_sector = read_boot_sector(inode); + auto const boot_sector = boot_sector::create(backing_inode); if (!boot_sector) { return kstd::failure(boot_sector.error()); } - print_boot_sector(*boot_sector); - return 0; } @@ -258,28 +76,29 @@ namespace kernel::filesystems::fat32 auto const driver_state = kstd::make_shared<fat32::driver_state>(); driver_state->backing_inode = backing_inode; - auto const boot_sector = read_boot_sector(backing_inode); + auto const boot_sector = boot_sector::create(backing_inode); if (!boot_sector) { return kstd::failure(boot_sector.error()); } - driver_state->boot_sector = *boot_sector; + driver_state->boot_sector = *boot_sector; // todo correct?? - compute_layout(*driver_state); - - if (auto const fs_info_result = read_fs_info(*driver_state); !fs_info_result) + auto const fs_info_result = fs_info::create(backing_inode, boot_sector->fs_info_offset()); + if (!fs_info_result) { return kstd::failure(fs_info_result.error()); } - auto const root_cluster = std::uint32_t{driver_state->boot_sector.root_cluster}; + driver_state->fs_info = fs_info_result.value(); + + auto const root_cluster = driver_state->boot_sector.root_directory_cluster_index(); if (!is_valid_cluster(root_cluster, *driver_state)) { return kstd::failure(errc::invalid_cluster); } - auto root = kstd::make_shared<inode>(root_cluster, constants::attribute_directory, kstd::bytes{}, - constants::root_inode_number); + auto root_directory_entry = directory_entry::create_root(root_cluster); + auto root = kstd::make_shared<inode>(root_directory_entry, constants::root_inode_number); return mount_result{root, driver_state}; } @@ -314,14 +133,11 @@ namespace kernel::filesystems::fat32 continue; } - // A ".." entry pointing to the root directory stores cluster 0 instead of the root cluster. - if (it->entry.first_cluster() == 0 && (it->entry.attributes & constants::attribute_directory) != 0) - { - return kstd::make_shared<inode>(std::uint32_t{state->boot_sector.root_cluster}, constants::attribute_directory, - kstd::bytes{}, constants::root_inode_number); - } + auto const inode_number = it->entry.first_cluster() == state->boot_sector.root_directory_cluster_index() + ? constants::root_inode_number + : it->inode_number(); - return kstd::make_shared<inode>(*it); + return kstd::make_shared<inode>(it->entry, inode_number); } if (it.error()) diff --git a/kernel/kernel/filesystems/fat32/filesystem.hpp b/kernel/kernel/filesystems/fat32/filesystem.hpp index ffb4e3c3..69f3ade5 100644 --- a/kernel/kernel/filesystems/fat32/filesystem.hpp +++ b/kernel/kernel/filesystems/fat32/filesystem.hpp @@ -2,16 +2,10 @@ #define TEACHOS_KERNEL_FILESYSTEMS_FAT32_FILESYSTEM_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/string.hpp> -#include <kstd/system_error.hpp> -#include <kstd/units.hpp> -#include <kstd/vector.hpp> #include <cstdint> #include <optional> @@ -19,18 +13,6 @@ namespace kernel::filesystems::fat32 { - //! Constants related to the fat32 filesystem. - namespace constants - { - constexpr kstd::bytes inline boot_sector_offset = kstd::bytes{0}; - constexpr std::uint16_t inline boot_signature = 0xAA55; - constexpr std::uint16_t inline min_bytes_per_sector = 512; - constexpr std::uint16_t inline max_bytes_per_sector = 4096; - - constexpr std::uint16_t inline max_clusters_fat12 = 4084; - constexpr std::uint16_t inline max_clusters_fat16 = 65524; - } // namespace constants - //! The File Allocation Table 32 Filesystem (fat32) struct filesystem : kernel::vfs::filesystem { diff --git a/kernel/kernel/filesystems/fat32/filesystem.tests.cpp b/kernel/kernel/filesystems/fat32/filesystem.tests.cpp index a727b921..ae11de39 100644 --- a/kernel/kernel/filesystems/fat32/filesystem.tests.cpp +++ b/kernel/kernel/filesystems/fat32/filesystem.tests.cpp @@ -15,9 +15,7 @@ #include <kernel/vfs/filesystem.hpp> #include <kstd/memory.hpp> -#include <kstd/print.hpp> #include <kstd/units.hpp> -#include <kstd/vector.hpp> #include <catch2/catch_test_macros.hpp> @@ -54,16 +52,6 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 filesyst REQUIRE(root->is_directory()); REQUIRE(static_pointer_cast<kernel::filesystems::fat32::inode>(root)->first_cluster() == 2); } - - THEN("the layout is derived from the boot sector") - { - REQUIRE(driver_state); - REQUIRE(driver_state->sector_size == 512_B); - REQUIRE(driver_state->cluster_size == 512_B); - REQUIRE(driver_state->fat_start_sector == 32); - REQUIRE(driver_state->data_start_sector == 32 + 2 * 536); - REQUIRE(driver_state->cluster_count == 69632 - (32 + 2 * 536)); - } } } @@ -86,43 +74,43 @@ SCENARIO("Fat32 filesystem mount validates the boot sector", "[filesystem][fat32 auto device = kstd::make_shared<kernel::tests::devices::block_device>("mock", block_size, 18 * block_size); auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(device); auto fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); - auto boot_sector = kernel::tests::filesystems::fat32::mock_boot_sector(); + auto data = std::get<0>(kernel::tests::filesystems::fat32::mock_boot_sector()); THEN("the unmodified layout mounts") { - kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); + kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, data); REQUIRE(fs->mount(dev_inode)); } THEN("a sector size that is not a supported power of two is rejected") { - boot_sector.bytes_per_sector = 0; - kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); + data.bytes_per_sector = 0; + kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, data); REQUIRE(fs->mount(dev_inode).error() == kernel::filesystems::fat32::errc::invalid_boot_sector); - boot_sector.bytes_per_sector = 513; - kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); + data.bytes_per_sector = 513; + kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, data); REQUIRE(fs->mount(dev_inode).error() == kernel::filesystems::fat32::errc::invalid_boot_sector); } THEN("a cluster size that is not a power of two is rejected") { - boot_sector.sectors_per_cluster = 3; - kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); + data.sectors_per_cluster = 3; + kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, data); REQUIRE(fs->mount(dev_inode).error() == kernel::filesystems::fat32::errc::invalid_boot_sector); } THEN("a missing file allocation table is rejected") { - boot_sector.number_of_fats = 0; - kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); + data.fats_count = 0; + kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, data); REQUIRE(fs->mount(dev_inode).error() == kernel::filesystems::fat32::errc::invalid_boot_sector); } THEN("a data region that does not fit into the volume is rejected") { - boot_sector.total_sectors_32 = 2; - kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, boot_sector); + data.total_size_in_sectors_32 = 2; + kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device, data); REQUIRE(fs->mount(dev_inode).error() == kernel::filesystems::fat32::errc::invalid_boot_sector); } } @@ -214,7 +202,7 @@ SCENARIO("Fat32 filesystem lookup on a mock layout", "[filesystem][fat32][filesy GIVEN("a mounted minimal fat32 layout with a file and a subdirectory") { using namespace kernel::filesystems::fat32; - using kernel::tests::filesystems::fat32::make_entry; + using kernel::tests::filesystems::fat32::make_entry_data; using kernel::tests::filesystems::fat32::write_directory_entries; using kernel::tests::filesystems::fat32::write_fat_entry; @@ -223,16 +211,16 @@ SCENARIO("Fat32 filesystem lookup on a mock layout", "[filesystem][fat32][filesy write_directory_entries(*device, 2, std::array{ - make_entry("LABEL", constants::attribute_volume_id), - make_entry("HELLO TXT", constants::attribute_archive, 3, 5), - make_entry("SUBDIR", constants::attribute_directory, 4), + make_entry_data("LABEL", constants::attribute_volume_id), + make_entry_data("HELLO TXT", constants::attribute_archive, 3, 5), + make_entry_data("SUBDIR", constants::attribute_directory, 4), }); write_fat_entry(*device, 3, 0x0FFF'FFFF); write_directory_entries(*device, 4, std::array{ - make_entry(".", constants::attribute_directory, 4), - make_entry("..", constants::attribute_directory, 0), - make_entry("INNER TXT", constants::attribute_archive, 5, 7), + make_entry_data(".", constants::attribute_directory, 4), + make_entry_data("..", constants::attribute_directory, 0), + make_entry_data("INNER TXT", constants::attribute_archive, 5, 7), }); write_fat_entry(*device, 4, 0x0FFF'FFFF); write_fat_entry(*device, 5, 0x0FFF'FFFF); @@ -317,8 +305,8 @@ SCENARIO("Fat32 filesystem lookup on a mock layout", "[filesystem][fat32][filesy WHEN("the full root directory cluster is marked as free in the file allocation table") { - auto entries = std::array<directory_entry, 16>{}; - entries.fill(make_entry("FILLER TXT", constants::attribute_archive)); + auto entries = std::array<directory_entry_data, 16>{}; + entries.fill(make_entry_data("FILLER TXT", constants::attribute_archive)); write_directory_entries(*device, 2, entries); write_fat_entry(*device, 2, 0); diff --git a/kernel/kernel/filesystems/fat32/fs_info.cpp b/kernel/kernel/filesystems/fat32/fs_info.cpp new file mode 100644 index 00000000..2bb8fa90 --- /dev/null +++ b/kernel/kernel/filesystems/fat32/fs_info.cpp @@ -0,0 +1,63 @@ +#include <kernel/filesystems/fat32/fs_info.hpp> + +#include <kernel/filesystems/fat32/driver_state.hpp> +#include <kernel/filesystems/fat32/error.hpp> + +#include <kstd/result.hpp> +#include <kstd/span.hpp> +#include <kstd/units.hpp> + +#include <array> +#include <cstdint> + +namespace kernel::filesystems::fat32 +{ + auto fs_info::create(inode_ptr const & backing_inode, kstd::bytes offset) -> kstd::result<fs_info> + { + auto data = fs_info_data{}; + + if (auto result = backing_inode->read(kstd::raw_bytes(data), offset); !result) + { + return kstd::failure(result.error()); + } + + auto const is_valid = data.lead_signature == constants::fs_info_lead_signature && + data.struct_signature == constants::fs_info_struct_signature && + data.trail_signature == constants::fs_info_trail_signature; + + if (!is_valid) + { + return kstd::failure(errc::invalid_fs_info_signature); + } + + return kstd::success(fs_info{data.free_clusters_count, data.next_free_cluster}); + } + + auto fs_info::write_free_cluster_count(driver_state const & state, std::uint32_t free_cluster_count) + -> kstd::result<void> + { + m_free_clusters_count = free_cluster_count; + return write_data(state); + } + + auto fs_info::write_next_free_cluster(driver_state const & state, std::uint32_t next_free_cluster) + -> kstd::result<void> + { + m_next_free_cluster = next_free_cluster; + return write_data(state); + } + + // todo performance could be improved if just writing these two values together + auto fs_info::write_data(driver_state const & state) -> kstd::result<void> + { + std::array data{m_free_clusters_count, m_next_free_cluster}; + if (auto result = state.backing_inode->write(kstd::raw_bytes(data), + state.boot_sector.fs_info_offset() + constants::fs_info_data_offset); + !result) + { + return kstd::failure(result.error()); + } + + return kstd::success(); + } +} // namespace kernel::filesystems::fat32 diff --git a/kernel/kernel/filesystems/fat32/fs_info.hpp b/kernel/kernel/filesystems/fat32/fs_info.hpp index 754f06b1..c9058ccf 100644 --- a/kernel/kernel/filesystems/fat32/fs_info.hpp +++ b/kernel/kernel/filesystems/fat32/fs_info.hpp @@ -1,48 +1,70 @@ #ifndef TEACHOS_KERNEL_FILESYSTEMS_FAT32_FS_INFO_HPP #define TEACHOS_KERNEL_FILESYSTEMS_FAT32_FS_INFO_HPP +#include <kernel/vfs/filesystem.hpp> + +#include <kstd/result.hpp> +#include <kstd/units.hpp> + #include <array> #include <cstdint> namespace kernel::filesystems::fat32 { - //! Constants related to the fat32 filesystem information sector. namespace constants { constexpr std::uint32_t inline fs_info_lead_signature = 0x4161'5252; constexpr std::uint32_t inline fs_info_struct_signature = 0x6141'7272; constexpr std::uint32_t inline fs_info_trail_signature = 0xAA55'0000; - - //! The value of the free cluster count and next free cluster hints if they are not known. constexpr std::uint32_t inline fs_info_unknown = 0xFFFF'FFFF; + constexpr kstd::bytes inline fs_info_data_offset = kstd::bytes{488}; } // namespace constants - //! The filesystem information sector (FSInfo) in the fat32 filesystem. - //! - //! The sector holds hints about the free clusters of the volume, which spare the driver from scanning the file - //! allocation table. The hints are not guaranteed to be accurate and must be validated before use. The layout follows - //! the "Microsoft Extensible Firmware Initiative FAT32 File System Specification" (fatgen103). - // cspell:ignore fatgen - struct [[gnu::packed]] fs_info + struct [[gnu::packed]] fs_info_data { - //! Check if all signatures of this sector are valid. - [[nodiscard]] constexpr auto is_valid() const noexcept -> bool - { - return lead_signature == constants::fs_info_lead_signature && - struct_signature == constants::fs_info_struct_signature && - trail_signature == constants::fs_info_trail_signature; - } - std::uint32_t lead_signature; std::array<std::uint8_t, 480> reserved; // NOLINT(readability-magic-numbers) std::uint32_t struct_signature; - std::uint32_t free_cluster_count; + std::uint32_t free_clusters_count; std::uint32_t next_free_cluster; std::array<std::uint8_t, 12> reserved1; // NOLINT(readability-magic-numbers) std::uint32_t trail_signature; }; - static_assert(sizeof(fs_info) == 512, "The fat32 filesystem information sector must be exactly 512 bytes in size."); + static_assert(sizeof(fs_info_data) == 512, "The fat32 fs-info sector must be exactly 512 bytes in size."); + + struct driver_state; // forward-declaration + + struct fs_info + { + using inode_ptr = kernel::vfs::filesystem::inode_ptr; + + explicit fs_info(std::uint32_t free_cluster_count, std::uint32_t next_free_cluster) + : m_free_clusters_count(free_cluster_count) + , m_next_free_cluster(next_free_cluster) + {} + + auto static create(inode_ptr const & backing_inode, kstd::bytes offset) -> kstd::result<fs_info>; + + auto write_free_cluster_count(driver_state const & state, std::uint32_t free_cluster_count) -> kstd::result<void>; + auto write_next_free_cluster(driver_state const & state, std::uint32_t next_free_cluster) -> kstd::result<void>; + + [[nodiscard]] auto free_cluster_count() const noexcept -> std::uint32_t + { + return m_free_clusters_count; + } + + [[nodiscard]] auto next_free_cluster() const noexcept -> std::uint32_t + { + return m_next_free_cluster; + } + + private: + std::uint32_t m_free_clusters_count; + std::uint32_t m_next_free_cluster; + + auto write_data(driver_state const & state) -> kstd::result<void>; + }; } // namespace kernel::filesystems::fat32 #endif diff --git a/kernel/kernel/filesystems/fat32/inode.cpp b/kernel/kernel/filesystems/fat32/inode.cpp index 694c261d..556635e3 100644 --- a/kernel/kernel/filesystems/fat32/inode.cpp +++ b/kernel/kernel/filesystems/fat32/inode.cpp @@ -38,20 +38,11 @@ namespace kernel::filesystems::fat32 constexpr auto zero_fill_chunk_size = 512_B; } // namespace - inode::inode(std::uint32_t first_cluster, std::uint8_t attributes, kstd::bytes size, std::uint64_t inode_number) - : m_first_cluster(first_cluster) - , m_attributes(attributes) - , m_size(size) + inode::inode(directory_entry const & directory_entry, std::uint64_t inode_number) + : m_directory_entry(directory_entry) , m_inode_number(inode_number) {} - inode::inode(directory_record const & record) - : inode(record.entry.first_cluster(), record.entry.attributes, kstd::bytes{record.entry.file_size}, - record.inode_number()) - { - m_entry_location = record.location; - } - auto inode::read(std::span<std::byte> buffer, kstd::bytes offset) const -> kstd::result<kstd::bytes> { if (is_directory()) @@ -65,16 +56,17 @@ namespace kernel::filesystems::fat32 return kstd::failure(vfs::errc::not_mounted); } - if (offset >= m_size || buffer.empty()) + if (offset >= m_directory_entry.file_size() || buffer.empty()) { return 0_B; } // The last cluster of a file is padded up to the cluster size, so the read must be limited to the file size. - auto const requested_size = std::min(kstd::bytes{buffer.size()}, m_size - offset); + auto const requested_size = std::min(kstd::bytes{buffer.size()}, m_directory_entry.file_size() - offset); auto guard = kstd::lock_guard{state->lock}; - auto const read_result = read_cluster_chain(m_first_cluster, buffer.first(requested_size.value), offset, *state); + auto const read_result = + read_cluster_chain(m_directory_entry.first_cluster(), buffer.first(requested_size.value), offset, *state); if (!read_result) { return kstd::failure(read_result.error()); @@ -107,12 +99,6 @@ namespace kernel::filesystems::fat32 return kstd::failure(vfs::errc::not_mounted); } - // Without its directory entry, the new size of the file could not be persisted. - if (!m_entry_location) - { - return kstd::failure(vfs::errc::invalid_inode); - } - if (buffer.empty()) { return 0_B; @@ -127,7 +113,7 @@ namespace kernel::filesystems::fat32 auto guard = kstd::lock_guard{state->lock}; // An empty file owns no cluster, so the chain has to be started first. - if (m_first_cluster == 0) + if (m_directory_entry.first_cluster() == 0) { auto const first_cluster = allocate_cluster(std::nullopt, *state); if (!first_cluster) @@ -135,26 +121,30 @@ namespace kernel::filesystems::fat32 return kstd::failure(first_cluster.error()); } - m_first_cluster = *first_cluster; - if (auto const entry_result = write_directory_entry(*state); !entry_result) + m_directory_entry.set_first_cluster(first_cluster.value()); + + if (auto const entry_result = m_directory_entry.write_first_cluster(state->backing_inode, first_cluster.value()); + !entry_result) { return kstd::failure(entry_result.error()); } } - auto const cluster_count = (write_end + state->cluster_size - 1_B) / state->cluster_size; - if (auto const allocation_result = ensure_clusters_allocated(m_first_cluster, cluster_count, *state); + auto const cluster_count = + (write_end + state->boot_sector.cluster_size() - 1_B) / state->boot_sector.cluster_size(); + if (auto const allocation_result = + ensure_clusters_allocated(m_directory_entry.first_cluster(), cluster_count, *state); !allocation_result) { return kstd::failure(allocation_result.error()); } // Writing behind the end of the file leaves a gap, which must read as zeros instead of stale cluster contents. - for (auto gap_offset = m_size; gap_offset < offset;) + for (auto gap_offset = m_directory_entry.file_size(); gap_offset < offset;) { auto const zeros = std::array<std::byte, zero_fill_chunk_size.value>{}; auto const chunk = std::span{zeros}.first(std::min(offset - gap_offset, zero_fill_chunk_size).value); - auto const fill_result = write_cluster_chain(m_first_cluster, chunk, gap_offset, *state); + auto const fill_result = write_cluster_chain(m_directory_entry.first_cluster(), chunk, gap_offset, *state); if (!fill_result) { return kstd::failure(fill_result.error()); @@ -169,16 +159,15 @@ namespace kernel::filesystems::fat32 gap_offset += *fill_result; } - auto const write_result = write_cluster_chain(m_first_cluster, buffer, offset, *state); + auto const write_result = write_cluster_chain(m_directory_entry.first_cluster(), buffer, offset, *state); if (!write_result) { return kstd::failure(write_result.error()); } - if (auto const new_size = offset + *write_result; new_size > m_size) + if (auto const new_size = offset + *write_result; new_size > m_directory_entry.file_size()) { - m_size = new_size; - if (auto const entry_result = write_directory_entry(*state); !entry_result) + if (auto const entry_result = m_directory_entry.write_file_size(state->backing_inode, new_size); !entry_result) { return kstd::failure(entry_result.error()); } @@ -189,12 +178,12 @@ namespace kernel::filesystems::fat32 auto inode::is_directory() const -> bool { - return (m_attributes & constants::attribute_directory) != 0; + return (m_directory_entry.attributes() & constants::attribute_directory) != 0; } auto inode::is_regular() const -> bool { - return (m_attributes & (constants::attribute_directory | constants::attribute_volume_id)) == 0; + return (m_directory_entry.attributes() & (constants::attribute_directory | constants::attribute_volume_id)) == 0; } auto inode::status() const -> kstd::result<kapi::filesystem::file_status> @@ -205,7 +194,7 @@ namespace kernel::filesystems::fat32 return status; } - status->size = m_size; + status->size = m_directory_entry.file_size(); status->inode_number = m_inode_number; status->link_count = 1; return status; @@ -234,10 +223,10 @@ namespace kernel::filesystems::fat32 auto guard = kstd::lock_guard{state->lock}; auto count = 0uz; - auto it = directory_iterator{m_first_cluster, kstd::bytes{position.value}, *state}; + auto it = directory_iterator{m_directory_entry.first_cluster(), kstd::bytes{position.value}, *state}; for (; it != directory_iterator{}; ++it) { - auto const is_directory_entry = (it->entry.attributes & constants::attribute_directory) != 0; + auto const is_directory_entry = (it->entry.attributes() & constants::attribute_directory) != 0; entries[count++] = kapi::filesystem::directory_entry{ .name = it->entry.short_name(), .type = is_directory_entry ? kapi::filesystem::file_type::directory : kapi::filesystem::file_type::regular, @@ -261,31 +250,11 @@ namespace kernel::filesystems::fat32 auto inode::first_cluster() const -> std::uint32_t { - return m_first_cluster; + return m_directory_entry.first_cluster(); } auto inode::size() const -> kstd::bytes { - return m_size; - } - - auto inode::write_directory_entry(fat32::driver_state const & state) const -> kstd::result<void> - { - auto entry = directory_entry{}; - if (auto const read_result = state.backing_inode->read(kstd::raw_bytes(entry), *m_entry_location); !read_result) - { - return kstd::failure(read_result.error()); - } - - entry.first_cluster_high = static_cast<std::uint16_t>(m_first_cluster >> 16); - entry.first_cluster_low = static_cast<std::uint16_t>(m_first_cluster); - entry.file_size = static_cast<std::uint32_t>(m_size.value); - - if (auto const write_result = state.backing_inode->write(kstd::raw_bytes(entry), *m_entry_location); !write_result) - { - return kstd::failure(write_result.error()); - } - - return {}; + return m_directory_entry.file_size(); } } // namespace kernel::filesystems::fat32 diff --git a/kernel/kernel/filesystems/fat32/inode.hpp b/kernel/kernel/filesystems/fat32/inode.hpp index 55fc1d13..3a5a88d4 100644 --- a/kernel/kernel/filesystems/fat32/inode.hpp +++ b/kernel/kernel/filesystems/fat32/inode.hpp @@ -1,21 +1,16 @@ #ifndef TEACHOS_KERNEL_FILESYSTEMS_FAT32_INODE_HPP #define TEACHOS_KERNEL_FILESYSTEMS_FAT32_INODE_HPP -#include <kernel/filesystems/fat32/directory_iterator.hpp> -#include <kernel/filesystems/fat32/driver_state.hpp> +#include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/vfs/inode.hpp> #include <kapi/filesystem.hpp> -#include <kstd/memory.hpp> #include <kstd/result.hpp> -#include <kstd/string.hpp> #include <kstd/units.hpp> -#include <kstd/vector.hpp> #include <cstddef> #include <cstdint> -#include <optional> #include <span> #include <utility> @@ -23,22 +18,7 @@ namespace kernel::filesystems::fat32 { struct inode : kernel::vfs::inode { - //! Create a fat32 inode. - //! - //! @param first_cluster The first cluster of the inode's data. - //! @param attributes The attributes of the directory entry describing the inode. - //! @param size The size of the inode's data in bytes. - //! @param inode_number The inode number reported in the inode's status. - explicit inode(std::uint32_t first_cluster, std::uint8_t attributes, kstd::bytes size = {}, - std::uint64_t inode_number = {}); - - //! Create a fat32 inode for the file described by a directory record. - //! - //! Only inodes created from a record can be written to, since the record locates the directory entry that has to be - //! updated when the file grows. - //! - //! @param record The record describing the inode. - explicit inode(directory_record const & record); + explicit inode(directory_entry const & directory_entry, std::uint64_t inode_number); [[nodiscard]] auto read(std::span<std::byte> buffer, kstd::bytes offset) const -> kstd::result<kstd::bytes> override; @@ -56,29 +36,13 @@ namespace kernel::filesystems::fat32 std::span<kapi::filesystem::directory_entry> entries) const -> kstd::result<std::pair<std::size_t, kapi::filesystem::directory_cursor>> override; - //! Get the first cluster of the inode's data. - //! - //! @return The first cluster number. [[nodiscard]] auto first_cluster() const -> std::uint32_t; - //! Get the size of the inode's data. - //! - //! @return The size in bytes, which is always 0 for directories. [[nodiscard]] auto size() const -> kstd::bytes; private: - //! Store the first cluster and size of this inode in the directory entry describing it. - //! - //! @param state The driver state to operate on. - //! @return Nothing on success, an error otherwise. - [[nodiscard]] auto write_directory_entry(fat32::driver_state const & state) const -> kstd::result<void>; - - std::uint32_t m_first_cluster{}; - std::uint8_t m_attributes{}; - kstd::bytes m_size{}; + directory_entry m_directory_entry{}; std::uint64_t m_inode_number{}; - //! The offset of the directory entry describing this inode on the backing inode, if it has one. - std::optional<kstd::bytes> m_entry_location{}; }; } // namespace kernel::filesystems::fat32 diff --git a/kernel/kernel/filesystems/fat32/inode.tests.cpp b/kernel/kernel/filesystems/fat32/inode.tests.cpp index 2f5822a0..e3578b42 100644 --- a/kernel/kernel/filesystems/fat32/inode.tests.cpp +++ b/kernel/kernel/filesystems/fat32/inode.tests.cpp @@ -1,6 +1,7 @@ #include <kernel/filesystems/fat32/inode.hpp> #include <kernel/devices/storage.hpp> +#include <kernel/filesystems/fat32/boot_sector.hpp> #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/filesystems/fat32/directory_iterator.hpp> #include <kernel/filesystems/fat32/driver_state.hpp> @@ -79,6 +80,20 @@ namespace } return bytes; } + + auto create_inode(std::uint32_t first_cluster, std::uint8_t attributes, std::uint32_t file_size = {}, + std::uint64_t inode_number = {}) -> kernel::filesystems::fat32::inode + { + auto data = kernel::filesystems::fat32::directory_entry_data{}; + data.first_cluster_high = static_cast<std::uint16_t>(first_cluster >> 16); + data.first_cluster_low = static_cast<std::uint16_t>(first_cluster); + data.file_size = file_size; + data.attributes = attributes; + return kernel::filesystems::fat32::inode{ + kernel::filesystems::fat32::directory_entry{data, 2, 0_B}, // todo + inode_number + }; + } } // namespace SCENARIO("Fat32 inode attributes", "[filesystem][fat32][inode]") @@ -87,19 +102,20 @@ SCENARIO("Fat32 inode attributes", "[filesystem][fat32][inode]") GIVEN("a fat32 inode with the directory attribute") { - auto inode = kernel::filesystems::fat32::inode{2, constants::attribute_directory}; + auto entry = create_inode(3, constants::attribute_directory); + auto inode = kernel::filesystems::fat32::inode{entry}; THEN("it is a directory and keeps its first cluster") { REQUIRE(inode.is_directory()); REQUIRE_FALSE(inode.is_regular()); - REQUIRE(inode.first_cluster() == 2); + REQUIRE(inode.first_cluster() == 3); } } GIVEN("a fat32 inode without the directory attribute") { - auto inode = kernel::filesystems::fat32::inode{3, 0}; + auto inode = create_inode(3, 0); THEN("it is a regular file") { @@ -110,7 +126,7 @@ SCENARIO("Fat32 inode attributes", "[filesystem][fat32][inode]") GIVEN("a fat32 inode with the read only and archive attributes") { - auto inode = kernel::filesystems::fat32::inode{3, constants::attribute_read_only | constants::attribute_archive}; + auto inode = create_inode(3, constants::attribute_read_only | constants::attribute_archive); THEN("it is a regular file") { @@ -120,7 +136,7 @@ SCENARIO("Fat32 inode attributes", "[filesystem][fat32][inode]") GIVEN("a fat32 inode with the volume id attribute") { - auto inode = kernel::filesystems::fat32::inode{0, constants::attribute_volume_id}; + auto inode = create_inode(3, constants::attribute_volume_id); THEN("it is neither a directory nor a regular file") { @@ -129,16 +145,17 @@ SCENARIO("Fat32 inode attributes", "[filesystem][fat32][inode]") } } - GIVEN("a fat32 inode created from a directory record") + GIVEN("a fat32 inode created from a directory entry") { - auto record = directory_record{}; - record.entry.attributes = constants::attribute_archive; - record.entry.first_cluster_high = 0x0001; - record.entry.first_cluster_low = 0x0002; - record.entry.file_size = 1234; - record.location = 4096_B; + auto data = directory_entry_data{}; + data.attributes = constants::attribute_archive; + data.first_cluster_high = 0x0001; + data.first_cluster_low = 0x0002; + data.file_size = 1234; - auto inode = kernel::filesystems::fat32::inode{record}; + auto entry = directory_entry{data, 3, 4096_B}; + + auto inode = kernel::filesystems::fat32::inode{entry, 4096_B}; THEN("it takes its first cluster, size and inode number from the record") { @@ -163,7 +180,7 @@ SCENARIO("Fat32 inode read/write without a mount", "[filesystem][fat32][inode]") { GIVEN("a fat32 file inode that does not belong to a mount") { - auto inode = kernel::filesystems::fat32::inode{3, 0, 10_B}; + auto inode = create_inode(3, 0, 10_B); WHEN("reading from the inode") { @@ -208,8 +225,8 @@ SCENARIO("Fat32 inode reads file data on a mock layout", "[filesystem][fat32][in using namespace kernel::filesystems::fat32; auto const make_file = [&](std::uint32_t first_cluster, kstd::bytes size) { - auto inode = - kstd::make_shared<kernel::filesystems::fat32::inode>(first_cluster, constants::attribute_archive, size); + auto inode = kstd::make_shared<kernel::filesystems::fat32::inode>( + create_inode(first_cluster, constants::attribute_archive, size)); inode->set_driver_state(mount->second); return inode; }; @@ -384,22 +401,22 @@ SCENARIO("Fat32 inode lists directories on a mock layout", "[filesystem][fat32][ GIVEN("a mounted minimal fat32 layout with a populated root directory") { using namespace kernel::filesystems::fat32; - using kernel::tests::filesystems::fat32::make_entry; + using kernel::tests::filesystems::fat32::make_entry_data; auto device = kstd::make_shared<kernel::tests::devices::block_device>("mock", block_size, 18 * block_size); kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device); - auto deleted = make_entry("GONE TXT", constants::attribute_archive); + auto deleted = make_entry_data("GONE TXT", constants::attribute_archive); deleted.name[0] = static_cast<char>(constants::deleted_entry_marker); kernel::tests::filesystems::fat32::write_directory_entries( *device, 2, std::array{ - make_entry("LABEL", constants::attribute_volume_id), - make_entry("A TXT", constants::attribute_archive, 3, 1), + make_entry_data("LABEL", constants::attribute_volume_id), + make_entry_data("A TXT", constants::attribute_archive, 3, 1), deleted, - make_entry("B TXT", constants::attribute_archive, 4, 1), - make_entry("DIR", constants::attribute_directory, 5), + make_entry_data("B TXT", constants::attribute_archive, 4, 1), + make_entry_data("DIR", constants::attribute_directory, 5), }); auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(device); @@ -484,7 +501,7 @@ SCENARIO("Fat32 inode lists directories on a mock layout", "[filesystem][fat32][ THEN("listing a directory that does not belong to a mount fails") { - auto const directory = kernel::filesystems::fat32::inode{2, constants::attribute_directory}; + auto const directory = create_inode(2, constants::attribute_directory); auto entries = std::vector<kapi::filesystem::directory_entry>(1); REQUIRE(directory.read_directory(kapi::filesystem::directory_cursor{0}, entries).error() == kernel::vfs::errc::not_mounted); @@ -563,16 +580,16 @@ SCENARIO("Fat32 inode writes file data on a mock layout", "[filesystem][fat32][i GIVEN("a mounted minimal fat32 layout with a file in the root directory") { using namespace kernel::filesystems::fat32; - using kernel::tests::filesystems::fat32::make_entry; + using kernel::tests::filesystems::fat32::make_entry_data; auto device = kstd::make_shared<kernel::tests::devices::block_device>("mock", block_size, 18 * block_size); kernel::tests::filesystems::fat32::setup_mock_fat32_layout(*device); kernel::tests::filesystems::fat32::write_directory_entries( *device, 2, std::array{ - make_entry("HELLO TXT", constants::attribute_archive, 3), - make_entry("EMPTY TXT", constants::attribute_archive), - make_entry("DIR", constants::attribute_directory, 5), + make_entry_data("HELLO TXT", constants::attribute_archive, 3), + make_entry_data("EMPTY TXT", constants::attribute_archive), + make_entry_data("DIR", constants::attribute_directory, 5), }); kernel::tests::filesystems::fat32::write_fat_entry(*device, 3, 0x0FFF'FFFF); kernel::tests::filesystems::fat32::write_fat_entry(*device, 5, 0x0FFF'FFFF); @@ -744,7 +761,18 @@ SCENARIO("Fat32 inode writes file data on a mock layout", "[filesystem][fat32][i WHEN("the volume has no free cluster left") { - state->cluster_count = 4; + // todo + auto data = kernel::filesystems::fat32::boot_sector_data{}; + data.bytes_per_sector = 512; + data.sectors_per_cluster = 1; + data.reserved_region_size_in_sectors = 2; + data.fats_count = 1; + data.fat_size_in_sectors_32 = 1; + data.total_size_in_sectors_32 = 6; + data.fs_info_offset_in_sectors = 1; + + state->boot_sector = kernel::filesystems::fat32::boot_sector{data}; + kernel::tests::filesystems::fat32::write_fat_entry(*device, 4, 0x0FFF'FFFF); THEN("growing the file fails") @@ -782,19 +810,6 @@ SCENARIO("Fat32 inode writes file data on a mock layout", "[filesystem][fat32][i REQUIRE(result.error() == kernel::vfs::errc::is_a_directory); } } - - WHEN("writing to a file inode without a directory entry") - { - auto orphan = kstd::make_shared<kernel::filesystems::fat32::inode>(3, constants::attribute_archive); - orphan->set_driver_state(driver_data); - auto const result = orphan->write(as_bytes("x"), 0_B); - - THEN("the write fails because the size could not be persisted") - { - REQUIRE_FALSE(result); - REQUIRE(result.error() == kernel::vfs::errc::invalid_inode); - } - } } } @@ -825,8 +840,8 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 inode wr auto file = static_pointer_cast<kernel::filesystems::fat32::inode>(*looked_up); file->set_driver_state(driver_data); - REQUIRE(state->fs_info_offset); - auto const free_clusters_before = state->free_cluster_count; + REQUIRE(state->boot_sector.fs_info_offset()); + auto const free_clusters_before = state->fs_info.free_cluster_count(); WHEN("the file grows beyond its first cluster") { @@ -846,11 +861,11 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 inode wr REQUIRE(second); REQUIRE(*second); - auto const fat_size = state->boot_sector.fat_size_32 * state->sector_size; - for (auto fat_index = 0uz; fat_index < state->boot_sector.number_of_fats; ++fat_index) + auto const fat_size = kstd::bytes{state->boot_sector.fat_size()}; // todo bytes + for (auto fat_index = 0uz; fat_index < state->boot_sector.fats_count(); ++fat_index) { auto raw_entry = std::uint32_t{}; - auto const entry_offset = state->fat_start_sector * state->sector_size + fat_index * fat_size + + auto const entry_offset = state->boot_sector.fat1_offset() + fat_index * fat_size + file->first_cluster() * constants::fat_entry_size; REQUIRE(dev_inode->read(kstd::raw_bytes(raw_entry), entry_offset)); REQUIRE((raw_entry & constants::cluster_mask) == **second); @@ -859,10 +874,9 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 inode wr THEN("the filesystem information sector reflects the allocation") { - auto fs_info = kernel::filesystems::fat32::fs_info{}; - REQUIRE(dev_inode->read(kstd::raw_bytes(fs_info), *state->fs_info_offset)); - REQUIRE(fs_info.is_valid()); - REQUIRE(fs_info.free_cluster_count == free_clusters_before - 1); + auto fs_info = kernel::filesystems::fat32::fs_info_data{}; + REQUIRE(dev_inode->read(kstd::raw_bytes(fs_info), state->boot_sector.fs_info_offset())); + REQUIRE(fs_info.free_clusters_count == free_clusters_before - 1); REQUIRE(fs_info.next_free_cluster == *next_cluster(file->first_cluster(), *state).value()); } } diff --git a/kernel/kernel/main.cpp b/kernel/kernel/main.cpp index 4dcb9dcf..4e2b0cac 100644 --- a/kernel/kernel/main.cpp +++ b/kernel/kernel/main.cpp @@ -30,14 +30,14 @@ auto run_demo_fat32() -> void { kstd::println("[OS:DEM] Running FAT32 Demo"); - kstd::println("[OS:DEM] mount /dev/ram3 to /entrance"); - if (auto result = kapi::filesystem::mount("/dev/ram3", "/entrance")) + kstd::println("[OS:DEM] mount /dev/ram5 to /entrance"); + if (auto result = kapi::filesystem::mount("/dev/ram5", "/entrance")) { - kstd::println("[OS:DEM] mounted /dev/ram3 to /entrance"); + kstd::println("[OS:DEM] mounted /dev/ram5 to /entrance"); } else { - kapi::system::panic("[OS:DEM] failed to mount /dev/ram3 on /entrance", result.error()); + kapi::system::panic("[OS:DEM] failed to mount /dev/ram5 on /entrance", result.error()); } auto const directory = kapi::filesystem::open("/entrance"); diff --git a/kernel/kernel/test_support/filesystems/fat32.cpp b/kernel/kernel/test_support/filesystems/fat32.cpp index c320eb3e..3d131482 100644 --- a/kernel/kernel/test_support/filesystems/fat32.cpp +++ b/kernel/kernel/test_support/filesystems/fat32.cpp @@ -3,7 +3,7 @@ #include <kernel/filesystems/fat32/boot_sector.hpp> #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/filesystems/fat32/file_allocation_table.hpp> -#include <kernel/filesystems/fat32/filesystem.hpp> +#include <kernel/filesystems/fat32/fs_info.hpp> #include <kernel/test_support/devices/block_device.hpp> #include <kstd/units.hpp> @@ -15,6 +15,7 @@ #include <cstring> #include <span> #include <string_view> +#include <tuple> using namespace kstd::units_literals; @@ -23,7 +24,7 @@ namespace kernel::tests::filesystems::fat32 namespace { constexpr auto sector_size = 512_B; - constexpr std::uint16_t reserved_sector_count = 1; + constexpr std::uint16_t reserved_region_size_in_sectors = 2; constexpr std::uint32_t fat_sectors = 1; constexpr std::uint32_t data_sectors = 69632; @@ -40,30 +41,44 @@ namespace kernel::tests::filesystems::fat32 } } // namespace - auto mock_boot_sector() -> kernel::filesystems::fat32::boot_sector + auto mock_boot_sector() + -> std::tuple<kernel::filesystems::fat32::boot_sector_data, kernel::filesystems::fat32::boot_sector> { - auto boot_sector = kernel::filesystems::fat32::boot_sector{}; - boot_sector.bytes_per_sector = static_cast<std::uint16_t>(sector_size.value); - boot_sector.sectors_per_cluster = 1; - boot_sector.reserved_sector_count = reserved_sector_count; - boot_sector.number_of_fats = 1; - boot_sector.total_sectors_32 = reserved_sector_count + fat_sectors + data_sectors; - boot_sector.fat_size_32 = fat_sectors; - boot_sector.root_cluster = kernel::filesystems::fat32::constants::first_data_cluster; - boot_sector.boot_signature = kernel::filesystems::fat32::constants::boot_signature; - return boot_sector; + auto data = kernel::filesystems::fat32::boot_sector_data{}; + + data.bytes_per_sector = static_cast<std::uint16_t>(sector_size.value); + data.sectors_per_cluster = 1; + data.reserved_region_size_in_sectors = reserved_region_size_in_sectors; + data.fats_count = 1; + data.total_size_in_sectors_32 = reserved_region_size_in_sectors + fat_sectors + data_sectors; + data.fat_size_in_sectors_32 = fat_sectors; + data.root_directory_cluster_index = kernel::filesystems::fat32::constants::first_data_cluster; + data.boot_signature = kernel::filesystems::fat32::constants::boot_signature; + data.fs_info_offset_in_sectors = 1; + + auto boot_sector = kernel::filesystems::fat32::boot_sector{data}; + return {data, boot_sector}; } auto setup_mock_fat32_layout(kernel::tests::devices::block_device & device) -> void { - setup_mock_fat32_layout(device, mock_boot_sector()); + setup_mock_fat32_layout(device, std::get<0>(mock_boot_sector())); } auto setup_mock_fat32_layout(kernel::tests::devices::block_device & device, - kernel::filesystems::fat32::boot_sector const & boot_sector) -> void + kernel::filesystems::fat32::boot_sector_data const & data) -> void { - write_bytes(device, kernel::filesystems::fat32::constants::boot_sector_offset, &boot_sector, - kstd::size_of(boot_sector)); + write_bytes(device, kernel::filesystems::fat32::constants::boot_sector_offset, &data, kstd::size_of(data)); + + auto fs_info_data = kernel::filesystems::fat32::fs_info_data{}; + fs_info_data.free_clusters_count = data_sectors - 1; + fs_info_data.next_free_cluster = 2; + fs_info_data.lead_signature = kernel::filesystems::fat32::constants::fs_info_lead_signature; + fs_info_data.struct_signature = kernel::filesystems::fat32::constants::fs_info_struct_signature; + fs_info_data.trail_signature = kernel::filesystems::fat32::constants::fs_info_trail_signature; + + write_bytes(device, kstd::bytes{data.fs_info_offset_in_sectors} * data.bytes_per_sector, &fs_info_data, + kstd::size_of(fs_info_data)); write_fat_entry(device, 0, 0x0FFF'FFF8); write_fat_entry(device, 1, 0x0FFF'FFFF); @@ -74,39 +89,49 @@ namespace kernel::tests::filesystems::fat32 -> void { auto const offset = - reserved_sector_count * sector_size + cluster * kernel::filesystems::fat32::constants::fat_entry_size; + reserved_region_size_in_sectors * sector_size + cluster * kernel::filesystems::fat32::constants::fat_entry_size; write_bytes(device, offset, &value, kstd::size_of(value)); } auto write_cluster_data(kernel::tests::devices::block_device & device, std::uint32_t cluster, std::span<std::byte const> data) -> void { - auto const data_start = (reserved_sector_count + fat_sectors) * sector_size; + auto const data_start = (reserved_region_size_in_sectors + fat_sectors) * sector_size; auto const offset = data_start + (cluster - kernel::filesystems::fat32::constants::first_data_cluster) * sector_size; write_bytes(device, offset, data.data(), kstd::bytes{data.size()}); } - auto make_entry(std::string_view name, std::uint8_t attributes, std::uint32_t first_cluster, std::uint32_t file_size) + auto make_entry_data(std::string_view name, std::uint8_t attributes, std::uint32_t first_cluster, + std::uint32_t file_size, std::uint8_t nt_reserved) + -> kernel::filesystems::fat32::directory_entry_data + { + auto data = kernel::filesystems::fat32::directory_entry_data{}; + std::ranges::fill(data.name, ' '); + std::ranges::copy(name.substr(0, data.name.size()), data.name.begin()); + data.attributes = attributes; + data.first_cluster_high = static_cast<std::uint16_t>(first_cluster >> 16); + data.first_cluster_low = static_cast<std::uint16_t>(first_cluster & 0xFFFF); + data.file_size = file_size; + data.nt_reserved = nt_reserved; + return data; + } + + auto make_entry(std::string_view name, std::uint8_t attributes, std::uint32_t first_cluster, std::uint32_t file_size, + std::uint8_t nt_reserved, std::uint32_t root_dir_cluster_index, kstd::bytes disk_offset) -> kernel::filesystems::fat32::directory_entry { - auto entry = kernel::filesystems::fat32::directory_entry{}; - std::ranges::fill(entry.name, ' '); - std::ranges::copy(name.substr(0, entry.name.size()), entry.name.begin()); - entry.attributes = attributes; - entry.first_cluster_high = static_cast<std::uint16_t>(first_cluster >> 16); - entry.first_cluster_low = static_cast<std::uint16_t>(first_cluster & 0xFFFF); - entry.file_size = file_size; - return entry; + auto data = make_entry_data(name, attributes, first_cluster, file_size, nt_reserved); + return kernel::filesystems::fat32::directory_entry{data, root_dir_cluster_index, disk_offset}; } auto write_directory_entries(kernel::tests::devices::block_device & device, std::uint32_t cluster, - std::span<kernel::filesystems::fat32::directory_entry const> entries) -> void + std::span<kernel::filesystems::fat32::directory_entry_data const> entries) -> void { constexpr auto entries_per_cluster = sector_size.value / kernel::filesystems::fat32::constants::directory_entry_size.value; - auto cluster_entries = std::array<kernel::filesystems::fat32::directory_entry, entries_per_cluster>{}; + auto cluster_entries = std::array<kernel::filesystems::fat32::directory_entry_data, entries_per_cluster>{}; std::ranges::copy(entries, cluster_entries.begin()); write_cluster_data(device, cluster, std::as_bytes(std::span{cluster_entries})); } diff --git a/kernel/kernel/test_support/filesystems/fat32.hpp b/kernel/kernel/test_support/filesystems/fat32.hpp index f27392af..f111d01d 100644 --- a/kernel/kernel/test_support/filesystems/fat32.hpp +++ b/kernel/kernel/test_support/filesystems/fat32.hpp @@ -5,19 +5,23 @@ #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/test_support/devices/block_device.hpp> +#include <kstd/units.hpp> + #include <cstddef> #include <cstdint> #include <span> #include <string_view> +#include <tuple> namespace kernel::tests::filesystems::fat32 { //! The boot sector of the mock layout: 512 byte sectors, 1 sector per cluster, 1 reserved sector, a single FAT of //! 1 sector and 16 data clusters (2 to 17). The root directory starts at cluster 2. - auto mock_boot_sector() -> kernel::filesystems::fat32::boot_sector; + auto mock_boot_sector() + -> std::tuple<kernel::filesystems::fat32::boot_sector_data, kernel::filesystems::fat32::boot_sector>; auto setup_mock_fat32_layout(kernel::tests::devices::block_device & device) -> void; auto setup_mock_fat32_layout(kernel::tests::devices::block_device & device, - kernel::filesystems::fat32::boot_sector const & boot_sector) -> void; + kernel::filesystems::fat32::boot_sector_data const & boot_sector) -> void; auto write_fat_entry(kernel::tests::devices::block_device & device, std::uint32_t cluster, std::uint32_t value) -> void; auto write_cluster_data(kernel::tests::devices::block_device & device, std::uint32_t cluster, @@ -26,13 +30,18 @@ namespace kernel::tests::filesystems::fat32 //! Create a short directory entry. //! //! @param name The raw 11 character name, i.e. the space padded base name directly followed by the extension. + auto make_entry_data(std::string_view name, std::uint8_t attributes, std::uint32_t first_cluster = 0, + std::uint32_t file_size = 0, std::uint8_t nt_reserved = 0) + -> kernel::filesystems::fat32::directory_entry_data; + auto make_entry(std::string_view name, std::uint8_t attributes, std::uint32_t first_cluster = 0, - std::uint32_t file_size = 0) -> kernel::filesystems::fat32::directory_entry; + std::uint32_t file_size = 0, std::uint8_t nt_reserved = 0, std::uint32_t root_dir_cluster_index = 0, + kstd::bytes disk_offset = kstd::bytes{0}) -> kernel::filesystems::fat32::directory_entry; //! Fill a cluster of the mock layout with directory entries, terminated by an end of directory marker if they do not //! fill the whole cluster. auto write_directory_entries(kernel::tests::devices::block_device & device, std::uint32_t cluster, - std::span<kernel::filesystems::fat32::directory_entry const> entries) -> void; + std::span<kernel::filesystems::fat32::directory_entry_data const> entries) -> void; } // namespace kernel::tests::filesystems::fat32 diff --git a/kernel/kernel/test_support/filesystems/filesystem.hpp b/kernel/kernel/test_support/filesystems/filesystem.hpp index cfbfc550..46e14cdc 100644 --- a/kernel/kernel/test_support/filesystems/filesystem.hpp +++ b/kernel/kernel/test_support/filesystems/filesystem.hpp @@ -8,7 +8,6 @@ #include <kstd/memory.hpp> #include <kstd/result.hpp> -#include <kstd/system_error.hpp> #include <cstdint> #include <optional> diff --git a/kernel/kernel/test_support/filesystems/inode.hpp b/kernel/kernel/test_support/filesystems/inode.hpp index cdb489de..bf33af50 100644 --- a/kernel/kernel/test_support/filesystems/inode.hpp +++ b/kernel/kernel/test_support/filesystems/inode.hpp @@ -13,7 +13,8 @@ namespace kernel::tests::filesystems { struct inode : kernel::vfs::inode { - auto read(std::span<std::byte> buffer, kstd::bytes offset) const -> kstd::result<kstd::bytes> override; + [[nodiscard]] auto read(std::span<std::byte> buffer, kstd::bytes offset) const + -> kstd::result<kstd::bytes> override; auto write(std::span<std::byte const> buffer, kstd::bytes offset) -> kstd::result<kstd::bytes> override; [[nodiscard]] auto is_regular() const -> bool override; |
