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 }; 33 34 35 //xfs_da_node_hdr 36 class NodeHeaderV4 : public NodeHeader { 37 public: 38 39 NodeHeaderV4(const char* buffer); 40 ~NodeHeaderV4(); 41 void SwapEndian(); 42 uint16 Magic(); 43 uint64 Blockno(); 44 uint64 Lsn(); 45 uint64 Owner(); 46 uuid_t* Uuid(); 47 uint16 Count(); 48 49 BlockInfo info; 50 private: 51 uint16 count; 52 uint16 level; 53 }; 54 55 56 class NodeHeaderV5 : public NodeHeader { 57 public: 58 59 NodeHeaderV5(const char* buffer); 60 ~NodeHeaderV5(); 61 void SwapEndian(); 62 uint16 Magic(); 63 uint64 Blockno(); 64 uint64 Lsn(); 65 uint64 Owner(); 66 uuid_t* Uuid(); 67 uint16 Count(); 68 69 BlockInfoV5 info; 70 private: 71 uint16 count; 72 uint16 level; 73 uint32 pad32; 74 }; 75 76 #define XFS_NODE_CRC_OFF offsetof(NodeHeaderV5, info.crc) 77 #define XFS_NODE_V5_VPTR_OFF offsetof(NodeHeaderV5, info.forw) 78 #define XFS_NODE_V4_VPTR_OFF offsetof(NodeHeaderV4, info.forw) 79 80 81 //xfs_da_node_entry 82 struct NodeEntry { 83 uint32 hashval; 84 uint32 before; 85 }; 86 87 88 class NodeDirectory { 89 public: 90 NodeDirectory(Inode* inode); 91 ~NodeDirectory(); 92 status_t Init(); 93 bool IsNodeType(); 94 void FillMapEntry(int num, ExtentMapEntry* map); 95 status_t FillBuffer(int type, char* buffer, 96 int howManyBlocksFurther); 97 void SearchAndFillDataMap(uint64 blockNo); 98 status_t FindHashInNode(uint32 hashVal, uint32* rightMapOffset); 99 uint32 GetOffsetFromAddress(uint32 address); 100 xfs_extnum_t FirstLeafMapIndex(); 101 int EntrySize(int len) const; 102 status_t GetNext(char* name, size_t* length, 103 xfs_ino_t* ino); 104 status_t Lookup(const char* name, size_t length, 105 xfs_ino_t* id); 106 private: 107 Inode* fInode; 108 ExtentMapEntry* fDataMap; 109 ExtentMapEntry* fLeafMap; 110 uint32 fOffset; 111 char* fDataBuffer; 112 // This isn't inode data. It holds the directory block. 113 char* fLeafBuffer; 114 uint32 fCurBlockNumber; 115 uint8 fCurLeafMapNumber; 116 uint8 fCurLeafBufferNumber; 117 xfs_extnum_t fFirstLeafMapIndex; 118 }; 119 120 121 NodeHeader* 122 CreateNodeHeader(Inode* inode, const char* buffer); 123 124 125 uint32 126 SizeOfNodeHeader(Inode* inode); 127 128 #endif