Ext2FileSystem.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/FileBackedFileSystem.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. class Ext2FSInode final : public Inode {
  40. friend class Ext2FS;
  41. public:
  42. virtual ~Ext2FSInode() override;
  43. size_t size() const { return m_raw_inode.i_size; }
  44. bool is_symlink() const { return Kernel::is_symlink(m_raw_inode.i_mode); }
  45. bool is_directory() const { return Kernel::is_directory(m_raw_inode.i_mode); }
  46. // ^Inode (RefCounted magic)
  47. virtual void one_ref_left() override;
  48. private:
  49. // ^Inode
  50. virtual ssize_t read_bytes(off_t, ssize_t, u8* buffer, FileDescription*) const override;
  51. virtual InodeMetadata metadata() const override;
  52. virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) const override;
  53. virtual RefPtr<Inode> lookup(StringView name) override;
  54. virtual void flush_metadata() override;
  55. virtual ssize_t write_bytes(off_t, ssize_t, const u8* data, FileDescription*) override;
  56. virtual KResult add_child(InodeIdentifier child_id, const StringView& name, mode_t) override;
  57. virtual KResult remove_child(const StringView& name) override;
  58. virtual int set_atime(time_t) override;
  59. virtual int set_ctime(time_t) override;
  60. virtual int set_mtime(time_t) override;
  61. virtual KResult increment_link_count() override;
  62. virtual KResult decrement_link_count() override;
  63. virtual size_t directory_entry_count() const override;
  64. virtual KResult chmod(mode_t) override;
  65. virtual KResult chown(uid_t, gid_t) override;
  66. virtual KResult truncate(u64) override;
  67. bool write_directory(const Vector<FS::DirectoryEntry>&);
  68. void populate_lookup_cache() const;
  69. KResult resize(u64);
  70. Ext2FS& fs();
  71. const Ext2FS& fs() const;
  72. Ext2FSInode(Ext2FS&, unsigned index);
  73. mutable Vector<unsigned> m_block_list;
  74. mutable HashMap<String, unsigned> m_lookup_cache;
  75. ext2_inode m_raw_inode;
  76. };
  77. class Ext2FS final : public FileBackedFS {
  78. friend class Ext2FSInode;
  79. public:
  80. static NonnullRefPtr<Ext2FS> create(FileDescription&);
  81. virtual ~Ext2FS() override;
  82. virtual bool initialize() override;
  83. virtual unsigned total_block_count() const override;
  84. virtual unsigned free_block_count() const override;
  85. virtual unsigned total_inode_count() const override;
  86. virtual unsigned free_inode_count() const override;
  87. virtual KResult prepare_to_unmount() const override;
  88. virtual bool supports_watchers() const override { return true; }
  89. private:
  90. typedef unsigned BlockIndex;
  91. typedef unsigned GroupIndex;
  92. typedef unsigned InodeIndex;
  93. explicit Ext2FS(FileDescription&);
  94. const ext2_super_block& super_block() const { return m_super_block; }
  95. const ext2_group_desc& group_descriptor(GroupIndex) const;
  96. ext2_group_desc* block_group_descriptors() { return (ext2_group_desc*)m_cached_group_descriptor_table.value().data(); }
  97. const ext2_group_desc* block_group_descriptors() const { return (const ext2_group_desc*)m_cached_group_descriptor_table.value().data(); }
  98. void flush_block_group_descriptor_table();
  99. unsigned inodes_per_block() const;
  100. unsigned inodes_per_group() const;
  101. unsigned blocks_per_group() const;
  102. unsigned inode_size() const;
  103. bool write_ext2_inode(InodeIndex, const ext2_inode&);
  104. bool read_block_containing_inode(InodeIndex inode, BlockIndex& block_index, unsigned& offset, u8* buffer) const;
  105. bool flush_super_block();
  106. virtual const char* class_name() const override;
  107. virtual InodeIdentifier root_inode() const override;
  108. virtual KResultOr<NonnullRefPtr<Inode>> create_inode(InodeIdentifier parent_id, const String& name, mode_t, off_t size, dev_t, uid_t, gid_t) override;
  109. virtual KResult create_directory(InodeIdentifier parent_inode, const String& name, mode_t, uid_t, gid_t) override;
  110. virtual RefPtr<Inode> get_inode(InodeIdentifier) const override;
  111. virtual void flush_writes() override;
  112. BlockIndex first_block_index() const;
  113. InodeIndex find_a_free_inode(GroupIndex preferred_group, off_t expected_size);
  114. Vector<BlockIndex> allocate_blocks(GroupIndex preferred_group_index, size_t count);
  115. GroupIndex group_index_from_inode(InodeIndex) const;
  116. GroupIndex group_index_from_block_index(BlockIndex) const;
  117. Vector<BlockIndex> block_list_for_inode_impl(const ext2_inode&, bool include_block_list_blocks = false) const;
  118. Vector<BlockIndex> block_list_for_inode(const ext2_inode&, bool include_block_list_blocks = false) const;
  119. bool write_block_list_for_inode(InodeIndex, ext2_inode&, const Vector<BlockIndex>&);
  120. bool get_inode_allocation_state(InodeIndex) const;
  121. bool set_inode_allocation_state(InodeIndex, bool);
  122. bool set_block_allocation_state(BlockIndex, bool);
  123. void uncache_inode(InodeIndex);
  124. void free_inode(Ext2FSInode&);
  125. struct BlockListShape {
  126. unsigned direct_blocks { 0 };
  127. unsigned indirect_blocks { 0 };
  128. unsigned doubly_indirect_blocks { 0 };
  129. unsigned triply_indirect_blocks { 0 };
  130. unsigned meta_blocks { 0 };
  131. };
  132. BlockListShape compute_block_list_shape(unsigned blocks);
  133. unsigned m_block_group_count { 0 };
  134. mutable ext2_super_block m_super_block;
  135. mutable Optional<KBuffer> m_cached_group_descriptor_table;
  136. mutable HashMap<InodeIndex, RefPtr<Ext2FSInode>> m_inode_cache;
  137. bool m_super_block_dirty { false };
  138. bool m_block_group_descriptors_dirty { false };
  139. struct CachedBitmap {
  140. CachedBitmap(BlockIndex bi, KBuffer&& buf)
  141. : bitmap_block_index(bi)
  142. , buffer(move(buf))
  143. {
  144. }
  145. BlockIndex bitmap_block_index { 0 };
  146. bool dirty { false };
  147. KBuffer buffer;
  148. Bitmap bitmap(u32 blocks_per_group) { return Bitmap::wrap(buffer.data(), blocks_per_group); }
  149. };
  150. CachedBitmap& get_bitmap_block(BlockIndex);
  151. Vector<OwnPtr<CachedBitmap>> m_cached_bitmaps;
  152. };
  153. inline Ext2FS& Ext2FSInode::fs()
  154. {
  155. return static_cast<Ext2FS&>(Inode::fs());
  156. }
  157. inline const Ext2FS& Ext2FSInode::fs() const
  158. {
  159. return static_cast<const Ext2FS&>(Inode::fs());
  160. }
  161. }