Ext2FileSystem.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Ext2FileSystem;
  11. class Ext2Inode final : public CoreInode {
  12. friend class Ext2FileSystem;
  13. public:
  14. virtual ~Ext2Inode() 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 Unix::ssize_t read_bytes(Unix::off_t, Unix::size_t, byte* buffer, FileDescriptor*) override;
  20. virtual void populate_metadata() const override;
  21. virtual bool traverse_as_directory(Function<bool(const FileSystem::DirectoryEntry&)>) override;
  22. Ext2FileSystem& fs();
  23. const Ext2FileSystem& fs() const;
  24. Ext2Inode(Ext2FileSystem&, unsigned index, const ext2_inode&);
  25. SpinLock m_lock;
  26. Vector<unsigned> m_block_list;
  27. ext2_inode m_raw_inode;
  28. };
  29. class Ext2FileSystem final : public DiskBackedFileSystem {
  30. friend class Ext2Inode;
  31. public:
  32. static RetainPtr<Ext2FileSystem> create(RetainPtr<DiskDevice>&&);
  33. virtual ~Ext2FileSystem() override;
  34. virtual bool initialize() override;
  35. private:
  36. typedef unsigned BlockIndex;
  37. typedef unsigned GroupIndex;
  38. typedef unsigned InodeIndex;
  39. class CachedExt2Inode;
  40. class CachedExt2InodeImpl;
  41. explicit Ext2FileSystem(RetainPtr<DiskDevice>&&);
  42. const ext2_super_block& superBlock() const;
  43. const ext2_group_desc& blockGroupDescriptor(unsigned groupIndex) const;
  44. unsigned firstBlockOfGroup(unsigned groupIndex) const;
  45. unsigned inodesPerBlock() const;
  46. unsigned inodesPerGroup() const;
  47. unsigned blocksPerGroup() const;
  48. unsigned inodeSize() const;
  49. CachedExt2Inode lookupExt2Inode(unsigned) const;
  50. bool writeExt2Inode(unsigned, const ext2_inode&);
  51. ByteBuffer readBlockContainingInode(unsigned inode, unsigned& blockIndex, unsigned& offset) const;
  52. ByteBuffer readSuperBlock() const;
  53. bool writeSuperBlock(const ext2_super_block&);
  54. virtual const char* className() const override;
  55. virtual InodeIdentifier rootInode() const override;
  56. virtual bool writeInode(InodeIdentifier, const ByteBuffer&) override;
  57. virtual bool enumerateDirectoryInode(InodeIdentifier, Function<bool(const DirectoryEntry&)>) const override;
  58. virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
  59. virtual bool setModificationTime(InodeIdentifier, dword timestamp) override;
  60. virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override;
  61. virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor*) const override;
  62. virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;
  63. virtual InodeIdentifier findParentOfInode(InodeIdentifier) const override;
  64. virtual RetainPtr<CoreInode> get_inode(InodeIdentifier) const override;
  65. bool isDirectoryInode(unsigned) const;
  66. unsigned allocateInode(unsigned preferredGroup, unsigned expectedSize);
  67. Vector<BlockIndex> allocateBlocks(unsigned group, unsigned count);
  68. unsigned groupIndexFromInode(unsigned) const;
  69. Vector<unsigned> blockListForInode(const ext2_inode&) const;
  70. void dumpBlockBitmap(unsigned groupIndex) const;
  71. void dumpInodeBitmap(unsigned groupIndex) const;
  72. template<typename F> void traverseInodeBitmap(unsigned groupIndex, F) const;
  73. template<typename F> void traverseBlockBitmap(unsigned groupIndex, F) const;
  74. bool addInodeToDirectory(unsigned directoryInode, unsigned inode, const String& name, byte fileType);
  75. bool writeDirectoryInode(unsigned directoryInode, Vector<DirectoryEntry>&&);
  76. bool setInodeAllocationState(unsigned inode, bool);
  77. bool setBlockAllocationState(GroupIndex, BlockIndex, bool);
  78. bool modifyLinkCount(InodeIndex, int delta);
  79. unsigned m_blockGroupCount { 0 };
  80. mutable ByteBuffer m_cachedSuperBlock;
  81. mutable ByteBuffer m_cachedBlockGroupDescriptorTable;
  82. mutable SpinLock m_inodeCacheLock;
  83. mutable HashMap<unsigned, RetainPtr<CachedExt2InodeImpl>> m_inodeCache;
  84. mutable SpinLock m_inode_cache_lock;
  85. mutable HashMap<BlockIndex, RetainPtr<Ext2Inode>> m_inode_cache;
  86. };
  87. inline Ext2FileSystem& Ext2Inode::fs()
  88. {
  89. return static_cast<Ext2FileSystem&>(CoreInode::fs());
  90. }
  91. inline const Ext2FileSystem& Ext2Inode::fs() const
  92. {
  93. return static_cast<const Ext2FileSystem&>(CoreInode::fs());
  94. }