Ext2FileSystem.h 4.2 KB

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