Ext2FileSystem.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "DiskBackedFileSystem.h"
  3. #include "UnixTypes.h"
  4. #include <AK/Buffer.h>
  5. #include <AK/OwnPtr.h>
  6. #include "ext2_fs.h"
  7. struct ext2_group_desc;
  8. struct ext2_inode;
  9. struct ext2_super_block;
  10. class Ext2FS;
  11. class Ext2FSInode final : public CoreInode {
  12. friend class Ext2FS;
  13. public:
  14. virtual ~Ext2FSInode() override;
  15. size_t size() const { return m_raw_inode.i_size; }
  16. bool is_symlink() const { return isSymbolicLink(m_raw_inode.i_mode); }
  17. private:
  18. // ^CoreInode
  19. virtual ssize_t read_bytes(Unix::off_t, size_t, byte* buffer, FileDescriptor*) override;
  20. virtual void populate_metadata() const override;
  21. virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) override;
  22. virtual InodeIdentifier lookup(const String& name) override;
  23. virtual String reverse_lookup(InodeIdentifier) override;
  24. void populate_lookup_cache();
  25. Ext2FS& fs();
  26. const Ext2FS& fs() const;
  27. Ext2FSInode(Ext2FS&, unsigned index, const ext2_inode&);
  28. SpinLock m_lock;
  29. Vector<unsigned> m_block_list;
  30. HashMap<String, unsigned> m_lookup_cache;
  31. ext2_inode m_raw_inode;
  32. };
  33. class Ext2FS final : public DiskBackedFS {
  34. friend class Ext2FSInode;
  35. public:
  36. static RetainPtr<Ext2FS> create(RetainPtr<DiskDevice>&&);
  37. virtual ~Ext2FS() override;
  38. virtual bool initialize() override;
  39. private:
  40. typedef unsigned BlockIndex;
  41. typedef unsigned GroupIndex;
  42. typedef unsigned InodeIndex;
  43. explicit Ext2FS(RetainPtr<DiskDevice>&&);
  44. const ext2_super_block& super_block() const;
  45. const ext2_group_desc& group_descriptor(unsigned groupIndex) const;
  46. unsigned first_block_of_group(unsigned groupIndex) const;
  47. unsigned inodes_per_block() const;
  48. unsigned inodes_per_group() const;
  49. unsigned blocks_per_group() const;
  50. unsigned inode_size() const;
  51. OwnPtr<ext2_inode> lookup_ext2_inode(unsigned) const;
  52. bool write_ext2_inode(unsigned, const ext2_inode&);
  53. ByteBuffer read_block_containing_inode(unsigned inode, unsigned& blockIndex, unsigned& offset) const;
  54. ByteBuffer read_super_block() const;
  55. bool write_super_block(const ext2_super_block&);
  56. virtual const char* class_name() const override;
  57. virtual InodeIdentifier root_inode() const override;
  58. virtual bool write_inode(InodeIdentifier, const ByteBuffer&) override;
  59. virtual InodeMetadata inode_metadata(InodeIdentifier) const override;
  60. virtual int set_atime_and_mtime(InodeIdentifier, dword atime, dword mtime) override;
  61. virtual InodeIdentifier create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size, int& error) override;
  62. virtual ssize_t read_inode_bytes(InodeIdentifier, Unix::off_t offset, size_t count, byte* buffer, FileDescriptor*) const override;
  63. virtual InodeIdentifier create_directory(InodeIdentifier parentInode, const String& name, Unix::mode_t, int& error) override;
  64. virtual InodeIdentifier find_parent_of_inode(InodeIdentifier) const override;
  65. virtual RetainPtr<CoreInode> get_inode(InodeIdentifier) const override;
  66. bool is_directory_inode(unsigned) const;
  67. unsigned allocate_inode(unsigned preferredGroup, unsigned expectedSize);
  68. Vector<BlockIndex> allocate_blocks(unsigned group, unsigned count);
  69. unsigned group_index_from_inode(unsigned) const;
  70. Vector<unsigned> block_list_for_inode(const ext2_inode&) const;
  71. void dump_block_bitmap(unsigned groupIndex) const;
  72. void dump_inode_bitmap(unsigned groupIndex) const;
  73. template<typename F> void traverse_inode_bitmap(unsigned groupIndex, F) const;
  74. template<typename F> void traverse_block_bitmap(unsigned groupIndex, F) const;
  75. bool add_inode_to_directory(InodeIndex parent, InodeIndex child, const String& name, byte fileType, int& error);
  76. bool write_directory_inode(unsigned directoryInode, Vector<DirectoryEntry>&&);
  77. bool set_inode_allocation_state(unsigned inode, bool);
  78. bool set_block_allocation_state(GroupIndex, BlockIndex, bool);
  79. bool modify_link_count(InodeIndex, int delta);
  80. unsigned m_blockGroupCount { 0 };
  81. mutable ByteBuffer m_cached_super_block;
  82. mutable ByteBuffer m_cached_group_descriptor_table;
  83. mutable SpinLock m_inode_cache_lock;
  84. mutable HashMap<BlockIndex, RetainPtr<Ext2FSInode>> m_inode_cache;
  85. };
  86. inline Ext2FS& Ext2FSInode::fs()
  87. {
  88. return static_cast<Ext2FS&>(CoreInode::fs());
  89. }
  90. inline const Ext2FS& Ext2FSInode::fs() const
  91. {
  92. return static_cast<const Ext2FS&>(CoreInode::fs());
  93. }