1 /* 2 * Copyright 2020, Shubham Bhagat, shubhambhagat111@yahoo.com 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #ifndef _NODE_H_ 6 #define _NODE_H_ 7 8 9 #include "Extent.h" 10 #include "LeafDirectory.h" 11 12 13 #define XFS_DIR2_LEAFN_MAGIC (0xd2ff) 14 #define XFS_DIR3_LEAFN_MAGIC (0x3dff) 15 #define XFS_DA_NODE_MAGIC (0xfebe) 16 #define XFS_DA3_NODE_MAGIC (0x3ebe) 17 18 19 class NodeHeader { 20 public: 21 22 virtual ~NodeHeader() = 0; 23 virtual uint16 Magic() = 0; 24 virtual uint64 Blockno() = 0; 25 virtual uint64 Lsn() = 0; 26 virtual uint64 Owner() = 0; 27 virtual uuid_t* Uuid() = 0; 28 virtual uint16 Count() = 0; 29 static uint32 ExpectedMagic(int8 WhichDirectory, 30 Inode* inode); 31 static uint32 CRCOffset(); 32 static NodeHeader* Create(Inode* inode, const char* buffer); 33 static uint32 Size(Inode* inode); 34 }; 35 36 37 //xfs_da_node_hdr 38 class NodeHeaderV4 : public NodeHeader { 39 public: 40 41 NodeHeaderV4(const char* buffer); 42 ~NodeHeaderV4(); 43 void SwapEndian(); 44 uint16 Magic(); 45 uint64 Blockno(); 46 uint64 Lsn(); 47 uint64 Owner(); 48 uuid_t* Uuid(); 49 uint16 Count(); 50 51 BlockInfo info; 52 private: 53 uint16 count; 54 uint16 level; 55 }; 56 57 58 class NodeHeaderV5 : public NodeHeader { 59 public: 60 61 NodeHeaderV5(const char* buffer); 62 ~NodeHeaderV5(); 63 void SwapEndian(); 64 uint16 Magic(); 65 uint64 Blockno(); 66 uint64 Lsn(); 67 uint64 Owner(); 68 uuid_t* Uuid(); 69 uint16 Count(); 70 71 BlockInfoV5 info; 72 private: 73 uint16 count; 74 uint16 level; 75 uint32 pad32; 76 }; 77 78 #define XFS_NODE_CRC_OFF offsetof(NodeHeaderV5, info.crc) 79 #define XFS_NODE_V5_VPTR_OFF offsetof(NodeHeaderV5, info.forw) 80 #define XFS_NODE_V4_VPTR_OFF offsetof(NodeHeaderV4, info.forw) 81 82 83 //xfs_da_node_entry 84 struct NodeEntry { 85 uint32 hashval; 86 uint32 before; 87 }; 88 89 90 class NodeDirectory { 91 public: 92 NodeDirectory(Inode* inode); 93 ~NodeDirectory(); 94 status_t Init(); 95 bool IsNodeType(); 96 void FillMapEntry(int num, ExtentMapEntry* map); 97 status_t FillBuffer(int type, char* buffer, 98 int howManyBlocksFurther); 99 void SearchAndFillDataMap(uint64 blockNo); 100 status_t FindHashInNode(uint32 hashVal, uint32* rightMapOffset); 101 uint32 GetOffsetFromAddress(uint32 address); 102 xfs_extnum_t FirstLeafMapIndex(); 103 int EntrySize(int len) const; 104 status_t GetNext(char* name, size_t* length, 105 xfs_ino_t* ino); 106 status_t Lookup(const char* name, size_t length, 107 xfs_ino_t* id); 108 private: 109 Inode* fInode; 110 ExtentMapEntry* fDataMap; 111 ExtentMapEntry* fLeafMap; 112 uint32 fOffset; 113 char* fDataBuffer; 114 // This isn't inode data. It holds the directory block. 115 char* fLeafBuffer; 116 uint32 fCurBlockNumber; 117 uint8 fCurLeafMapNumber; 118 uint8 fCurLeafBufferNumber; 119 xfs_extnum_t fFirstLeafMapIndex; 120 }; 121 122 #endif