1 /* 2 * Copyright 2022, Raghav Sharma, raghavself28@gmail.com 3 * Copyright 2020, Shubham Bhagat, shubhambhagat111@yahoo.com 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 #ifndef _LEAFDIRECTORY_H_ 7 #define _LEAFDIRECTORY_H_ 8 9 10 #include "Directory.h" 11 #include "Extent.h" 12 #include "Inode.h" 13 #include "system_dependencies.h" 14 15 16 #define V4_DATA_HEADER_MAGIC 0x58443244 17 #define V5_DATA_HEADER_MAGIC 0x58444433 18 19 #define V4_LEAF_HEADER_MAGIC 0xd2f1 20 #define V5_LEAF_HEADER_MAGIC 0x3df1 21 22 23 enum ContentType { DATA, LEAF }; 24 25 26 // This class will act as interface for V4 and V5 leaf header 27 class ExtentLeafHeader { 28 public: 29 30 virtual ~ExtentLeafHeader() = 0; 31 virtual uint16 Magic() = 0; 32 virtual uint64 Blockno() = 0; 33 virtual uint64 Lsn() = 0; 34 virtual uint64 Owner() = 0; 35 virtual uuid_t* Uuid() = 0; 36 virtual uint16 Count() = 0; 37 virtual uint32 Forw() = 0; 38 static uint32 ExpectedMagic(int8 WhichDirectory, 39 Inode* inode); 40 static uint32 CRCOffset(); 41 static ExtentLeafHeader* Create(Inode* inode, const char* buffer); 42 static uint32 Size(Inode* inode); 43 44 }; 45 46 47 //xfs_dir_leaf_hdr_t 48 class ExtentLeafHeaderV4 : public ExtentLeafHeader { 49 public: 50 51 ExtentLeafHeaderV4(const char* buffer); 52 ~ExtentLeafHeaderV4(); 53 void SwapEndian(); 54 uint16 Magic(); 55 uint64 Blockno(); 56 uint64 Lsn(); 57 uint64 Owner(); 58 uuid_t* Uuid(); 59 uint16 Count(); 60 uint32 Forw(); 61 62 BlockInfo info; 63 private: 64 uint16 count; 65 uint16 stale; 66 }; 67 68 69 // xfs_dir3_leaf_hdr_t 70 class ExtentLeafHeaderV5 : public ExtentLeafHeader { 71 public: 72 73 ExtentLeafHeaderV5(const char* buffer); 74 ~ExtentLeafHeaderV5(); 75 void SwapEndian(); 76 uint16 Magic(); 77 uint64 Blockno(); 78 uint64 Lsn(); 79 uint64 Owner(); 80 uuid_t* Uuid(); 81 uint16 Count(); 82 uint32 Forw(); 83 84 BlockInfoV5 info; 85 private: 86 uint16 count; 87 uint16 stale; 88 uint32 pad; 89 }; 90 91 #define XFS_LEAF_CRC_OFF offsetof(ExtentLeafHeaderV5, info.crc) 92 #define XFS_LEAF_V5_VPTR_OFF offsetof(ExtentLeafHeaderV5, info.forw) 93 #define XFS_LEAF_V4_VPTR_OFF offsetof(ExtentLeafHeaderV4, info.forw) 94 95 96 // xfs_dir2_leaf_tail_t 97 struct ExtentLeafTail { 98 uint32 bestcount; 99 // # of best free entries 100 }; 101 102 103 class LeafDirectory : public DirectoryIterator { 104 public: 105 LeafDirectory(Inode* inode); 106 ~LeafDirectory(); 107 status_t Init(); 108 bool IsLeafType(); 109 void FillMapEntry(int num, ExtentMapEntry* map); 110 status_t FillBuffer(int type, char* buffer, 111 int howManyBlocksFurthur); 112 void SearchAndFillDataMap(uint64 blockNo); 113 ExtentLeafEntry* FirstLeaf(); 114 xfs_ino_t GetIno(); 115 uint32 GetOffsetFromAddress(uint32 address); 116 int EntrySize(int len) const; 117 status_t GetNext(char* name, size_t* length, 118 xfs_ino_t* ino); 119 status_t Lookup(const char* name, size_t length, 120 xfs_ino_t* id); 121 private: 122 Inode* fInode; 123 ExtentMapEntry* fDataMap; 124 ExtentMapEntry* fLeafMap; 125 uint32 fOffset; 126 char* fDataBuffer; 127 // This isn't inode data. It holds the directory block. 128 char* fLeafBuffer; 129 uint32 fCurBlockNumber; 130 }; 131 132 #endif 133