Ext2FileSystem.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Bitmap.h>
  28. #include <AK/HashMap.h>
  29. #include <Kernel/FileSystem/BlockBasedFileSystem.h>
  30. #include <Kernel/FileSystem/Inode.h>
  31. #include <Kernel/FileSystem/ext2_fs.h>
  32. #include <Kernel/KBuffer.h>
  33. #include <Kernel/UnixTypes.h>
  34. struct ext2_group_desc;
  35. struct ext2_inode;
  36. struct ext2_super_block;
  37. namespace Kernel {
  38. class Ext2FS;
  39. struct Ext2FSDirectoryEntry;
  40. class Ext2FSInode final : public Inode {
  41. friend class Ext2FS;
  42. public:
  43. virtual ~Ext2FSInode() override;
  44. size_t size() const { return m_raw_inode.i_size; }
  45. bool is_symlink() const { return Kernel::is_symlink(m_raw_inode.i_mode); }
  46. bool is_directory() const { return Kernel::is_directory(m_raw_inode.i_mode); }
  47. // ^Inode (RefCounted magic)
  48. virtual void one_ref_left() override;
  49. private:
  50. // ^Inode
  51. virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const override;
  52. virtual InodeMetadata metadata() const override;
  53. virtual KResult traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const override;
  54. virtual RefPtr<Inode> lookup(StringView name) override;
  55. virtual void flush_metadata() override;
  56. virtual ssize_t write_bytes(off_t, ssize_t, const UserOrKernelBuffer& data, FileDescription*) override;
  57. virtual KResultOr<NonnullRefPtr<Inode>> create_child(const String& name, mode_t, dev_t, uid_t, gid_t) override;
  58. virtual KResult add_child(Inode& child, const StringView& name, mode_t) override;
  59. virtual KResult remove_child(const StringView& name) override;
  60. virtual int set_atime(time_t) override;
  61. virtual int set_ctime(time_t) override;
  62. virtual int set_mtime(time_t) override;
  63. virtual KResult increment_link_count() override;
  64. virtual KResult decrement_link_count() override;
  65. virtual KResultOr<size_t> directory_entry_count() const override;
  66. virtual KResult chmod(mode_t) override;
  67. virtual KResult chown(uid_t, gid_t) override;
  68. virtual KResult truncate(u64) override;
  69. virtual KResultOr<int> get_block_address(int) override;
  70. KResult write_directory(const Vector<Ext2FSDirectoryEntry>&);
  71. bool populate_lookup_cache() const;
  72. KResult resize(u64);
  73. Ext2FS& fs();
  74. const Ext2FS& fs() const;
  75. Ext2FSInode(Ext2FS&, InodeIndex);
  76. mutable Vector<BlockBasedFS::BlockIndex> m_block_list;
  77. mutable HashMap<String, InodeIndex> m_lookup_cache;
  78. ext2_inode m_raw_inode;
  79. };
  80. class Ext2FS final : public BlockBasedFS {
  81. friend class Ext2FSInode;
  82. public:
  83. static NonnullRefPtr<Ext2FS> create(FileDescription&);
  84. virtual ~Ext2FS() override;
  85. virtual bool initialize() override;
  86. virtual unsigned total_block_count() const override;
  87. virtual unsigned free_block_count() const override;
  88. virtual unsigned total_inode_count() const override;
  89. virtual unsigned free_inode_count() const override;
  90. virtual KResult prepare_to_unmount() const override;
  91. virtual bool supports_watchers() const override { return true; }
  92. virtual u8 internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const override;
  93. private:
  94. TYPEDEF_DISTINCT_ORDERED_ID(unsigned, GroupIndex);
  95. explicit Ext2FS(FileDescription&);
  96. const ext2_super_block& super_block() const { return m_super_block; }
  97. const ext2_group_desc& group_descriptor(GroupIndex) const;
  98. ext2_group_desc* block_group_descriptors() { return (ext2_group_desc*)m_cached_group_descriptor_table->data(); }
  99. const ext2_group_desc* block_group_descriptors() const { return (const ext2_group_desc*)m_cached_group_descriptor_table->data(); }
  100. void flush_block_group_descriptor_table();
  101. unsigned inodes_per_block() const;
  102. unsigned inodes_per_group() const;
  103. unsigned blocks_per_group() const;
  104. unsigned inode_size() const;
  105. bool write_ext2_inode(InodeIndex, const ext2_inode&);
  106. bool find_block_containing_inode(InodeIndex, BlockIndex& block_index, unsigned& offset) const;
  107. bool flush_super_block();
  108. virtual const char* class_name() const override;
  109. virtual NonnullRefPtr<Inode> root_inode() const override;
  110. RefPtr<Inode> get_inode(InodeIdentifier) const;
  111. KResultOr<NonnullRefPtr<Inode>> create_inode(Ext2FSInode& parent_inode, const String& name, mode_t, dev_t, uid_t, gid_t);
  112. KResult create_directory(Ext2FSInode& parent_inode, const String& name, mode_t, uid_t, gid_t);
  113. virtual void flush_writes() override;
  114. BlockIndex first_block_index() const;
  115. InodeIndex find_a_free_inode(GroupIndex preferred_group = 0);
  116. Vector<BlockIndex> allocate_blocks(GroupIndex preferred_group_index, size_t count);
  117. GroupIndex group_index_from_inode(InodeIndex) const;
  118. GroupIndex group_index_from_block_index(BlockIndex) const;
  119. Vector<BlockIndex> block_list_for_inode_impl(const ext2_inode&, bool include_block_list_blocks = false) const;
  120. Vector<BlockIndex> block_list_for_inode(const ext2_inode&, bool include_block_list_blocks = false) const;
  121. KResult write_block_list_for_inode(InodeIndex, ext2_inode&, const Vector<BlockIndex>&);
  122. bool get_inode_allocation_state(InodeIndex) const;
  123. bool set_inode_allocation_state(InodeIndex, bool);
  124. bool set_block_allocation_state(BlockIndex, bool);
  125. void uncache_inode(InodeIndex);
  126. void free_inode(Ext2FSInode&);
  127. struct BlockListShape {
  128. unsigned direct_blocks { 0 };
  129. unsigned indirect_blocks { 0 };
  130. unsigned doubly_indirect_blocks { 0 };
  131. unsigned triply_indirect_blocks { 0 };
  132. unsigned meta_blocks { 0 };
  133. };
  134. BlockListShape compute_block_list_shape(unsigned blocks) const;
  135. unsigned m_block_group_count { 0 };
  136. mutable ext2_super_block m_super_block;
  137. mutable OwnPtr<KBuffer> m_cached_group_descriptor_table;
  138. mutable HashMap<InodeIndex, RefPtr<Ext2FSInode>> m_inode_cache;
  139. bool m_super_block_dirty { false };
  140. bool m_block_group_descriptors_dirty { false };
  141. struct CachedBitmap {
  142. CachedBitmap(BlockIndex bi, KBuffer&& buf)
  143. : bitmap_block_index(bi)
  144. , buffer(move(buf))
  145. {
  146. }
  147. BlockIndex bitmap_block_index { 0 };
  148. bool dirty { false };
  149. KBuffer buffer;
  150. Bitmap bitmap(u32 blocks_per_group) { return Bitmap::wrap(buffer.data(), blocks_per_group); }
  151. };
  152. CachedBitmap& get_bitmap_block(BlockIndex);
  153. Vector<OwnPtr<CachedBitmap>> m_cached_bitmaps;
  154. };
  155. inline Ext2FS& Ext2FSInode::fs()
  156. {
  157. return static_cast<Ext2FS&>(Inode::fs());
  158. }
  159. inline const Ext2FS& Ext2FSInode::fs() const
  160. {
  161. return static_cast<const Ext2FS&>(Inode::fs());
  162. }
  163. }