/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include struct ext2_group_desc; struct ext2_inode; struct ext2_super_block; namespace Kernel { class Ext2FS; struct Ext2FSDirectoryEntry; class Ext2FSInode final : public Inode { friend class Ext2FS; public: virtual ~Ext2FSInode() override; u64 size() const; bool is_symlink() const { return Kernel::is_symlink(m_raw_inode.i_mode); } bool is_directory() const { return Kernel::is_directory(m_raw_inode.i_mode); } // ^Inode (RefCounted magic) virtual void one_ref_left() override; private: // ^Inode virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual InodeMetadata metadata() const override; virtual ErrorOr traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)>) const override; virtual ErrorOr> lookup(StringView name) override; virtual ErrorOr flush_metadata() override; virtual ErrorOr write_bytes(off_t, size_t, const UserOrKernelBuffer& data, OpenFileDescription*) override; virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; virtual ErrorOr add_child(Inode& child, StringView name, mode_t) override; virtual ErrorOr remove_child(StringView name) override; virtual ErrorOr set_atime(time_t) override; virtual ErrorOr set_ctime(time_t) override; virtual ErrorOr set_mtime(time_t) override; virtual ErrorOr increment_link_count() override; virtual ErrorOr decrement_link_count() override; virtual ErrorOr chmod(mode_t) override; virtual ErrorOr chown(UserID, GroupID) override; virtual ErrorOr truncate(u64) override; virtual ErrorOr get_block_address(int) override; ErrorOr write_directory(Vector&); ErrorOr populate_lookup_cache() const; ErrorOr resize(u64); ErrorOr write_indirect_block(BlockBasedFileSystem::BlockIndex, Span); ErrorOr grow_doubly_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, Span, Vector&, unsigned&); ErrorOr shrink_doubly_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, size_t, unsigned&); ErrorOr grow_triply_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, Span, Vector&, unsigned&); ErrorOr shrink_triply_indirect_block(BlockBasedFileSystem::BlockIndex, size_t, size_t, unsigned&); ErrorOr flush_block_list(); ErrorOr> compute_block_list() const; ErrorOr> compute_block_list_with_meta_blocks() const; ErrorOr> compute_block_list_impl(bool include_block_list_blocks) const; ErrorOr> compute_block_list_impl_internal(ext2_inode const&, bool include_block_list_blocks) const; Ext2FS& fs(); const Ext2FS& fs() const; Ext2FSInode(Ext2FS&, InodeIndex); mutable Vector m_block_list; mutable HashMap m_lookup_cache; ext2_inode m_raw_inode; }; class Ext2FS final : public BlockBasedFileSystem { friend class Ext2FSInode; public: enum class FeaturesReadOnly : u32 { None = 0, FileSize64bits = 1 << 1, }; static ErrorOr> try_create(OpenFileDescription&); virtual ~Ext2FS() override; virtual ErrorOr initialize() override; virtual unsigned total_block_count() const override; virtual unsigned free_block_count() const override; virtual unsigned total_inode_count() const override; virtual unsigned free_inode_count() const override; virtual ErrorOr prepare_to_unmount() override; virtual bool supports_watchers() const override { return true; } virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const override; FeaturesReadOnly get_features_readonly() const; private: TYPEDEF_DISTINCT_ORDERED_ID(unsigned, GroupIndex); explicit Ext2FS(OpenFileDescription&); const ext2_super_block& super_block() const { return m_super_block; } const ext2_group_desc& group_descriptor(GroupIndex) const; ext2_group_desc* block_group_descriptors() { return (ext2_group_desc*)m_cached_group_descriptor_table->data(); } const ext2_group_desc* block_group_descriptors() const { return (const ext2_group_desc*)m_cached_group_descriptor_table->data(); } void flush_block_group_descriptor_table(); u64 inodes_per_block() const; u64 inodes_per_group() const; u64 blocks_per_group() const; u64 inode_size() const; ErrorOr write_ext2_inode(InodeIndex, ext2_inode const&); bool find_block_containing_inode(InodeIndex, BlockIndex& block_index, unsigned& offset) const; bool flush_super_block(); virtual StringView class_name() const override { return "Ext2FS"sv; } virtual Ext2FSInode& root_inode() override; ErrorOr> get_inode(InodeIdentifier) const; ErrorOr> create_inode(Ext2FSInode& parent_inode, StringView name, mode_t, dev_t, UserID, GroupID); ErrorOr> create_directory(Ext2FSInode& parent_inode, StringView name, mode_t, UserID, GroupID); virtual void flush_writes() override; BlockIndex first_block_index() const; ErrorOr allocate_inode(GroupIndex preferred_group = 0); ErrorOr> allocate_blocks(GroupIndex preferred_group_index, size_t count); GroupIndex group_index_from_inode(InodeIndex) const; GroupIndex group_index_from_block_index(BlockIndex) const; ErrorOr get_inode_allocation_state(InodeIndex) const; ErrorOr set_inode_allocation_state(InodeIndex, bool); ErrorOr set_block_allocation_state(BlockIndex, bool); void uncache_inode(InodeIndex); ErrorOr free_inode(Ext2FSInode&); struct BlockListShape { unsigned direct_blocks { 0 }; unsigned indirect_blocks { 0 }; unsigned doubly_indirect_blocks { 0 }; unsigned triply_indirect_blocks { 0 }; unsigned meta_blocks { 0 }; }; BlockListShape compute_block_list_shape(unsigned blocks) const; u64 m_block_group_count { 0 }; mutable ext2_super_block m_super_block; mutable OwnPtr m_cached_group_descriptor_table; mutable HashMap> m_inode_cache; bool m_super_block_dirty { false }; bool m_block_group_descriptors_dirty { false }; struct CachedBitmap { CachedBitmap(BlockIndex bi, NonnullOwnPtr buf) : bitmap_block_index(bi) , buffer(move(buf)) { } BlockIndex bitmap_block_index { 0 }; bool dirty { false }; NonnullOwnPtr buffer; Bitmap bitmap(u32 blocks_per_group) { return Bitmap { buffer->data(), blocks_per_group }; } }; ErrorOr get_bitmap_block(BlockIndex); ErrorOr update_bitmap_block(BlockIndex bitmap_block, size_t bit_index, bool new_state, u32& super_block_counter, u16& group_descriptor_counter); Vector> m_cached_bitmaps; RefPtr m_root_inode; }; inline Ext2FS& Ext2FSInode::fs() { return static_cast(Inode::fs()); } inline const Ext2FS& Ext2FSInode::fs() const { return static_cast(Inode::fs()); } }