1 /* 2 * Copyright 2020, Shubham Bhagat, shubhambhagat111@yahoo.com 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #ifndef _EXTENT_H_ 6 #define _EXTENT_H_ 7 8 9 #include "Inode.h" 10 #include "system_dependencies.h" 11 12 13 #define DIR2_BLOCK_HEADER_MAGIC 0x58443242 14 // for v4 system 15 #define XFS_DIR2_DATA_FD_COUNT 3 16 #define EXTENT_REC_SIZE 128 17 #define MASK(n) ((1UL << n) - 1) 18 19 20 // xfs_exntst_t 21 enum ExtentState { 22 XFS_EXT_NORM, 23 XFS_EXT_UNWRITTEN, 24 XFS_EXT_INVALID 25 }; 26 27 28 // xfs_dir2_data_free_t 29 struct FreeRegion { 30 uint16 offset; 31 uint16 length; 32 }; 33 34 35 // xfs_dir2_data_hdr_t 36 struct ExtentDataHeader { 37 uint32 magic; 38 FreeRegion bestfree[XFS_DIR2_DATA_FD_COUNT]; 39 }; 40 41 42 // xfs_dir2_data_entry_t 43 struct ExtentDataEntry { 44 xfs_ino_t inumber; 45 uint8 namelen; 46 uint8 name[]; 47 48 // Followed by a file type (8bit) if applicable and a 16bit tag 49 // tag is the offset from start of the block 50 }; 51 52 53 // xfs_dir2_data_unused_t 54 struct ExtentUnusedEntry { 55 uint16 freetag; 56 // takes the value 0xffff 57 uint16 length; 58 // freetag+length overrides the inumber of an entry 59 uint16 tag; 60 }; 61 62 63 // xfs_dir2_leaf_entry_t 64 struct ExtentLeafEntry { 65 uint32 hashval; 66 uint32 address; 67 // offset into block after >> 3 68 }; 69 70 71 // xfs_dir2_block_tail_t 72 struct ExtentBlockTail { 73 uint32 count; 74 // # of entries in leaf 75 uint32 stale; 76 // # of free leaf entries 77 }; 78 79 80 struct ExtentMapEntry { 81 xfs_fileoff_t br_startoff; 82 // logical file block offset 83 xfs_fsblock_t br_startblock; 84 // absolute block number 85 xfs_filblks_t br_blockcount; 86 // # of blocks 87 uint8 br_state; 88 // state of the extent 89 }; 90 91 92 class Extent 93 { 94 public: 95 Extent(Inode* inode); 96 ~Extent(); 97 status_t Init(); 98 bool BlockType(); 99 void FillMapEntry(void* pointerToMap); 100 ExtentDataHeader* BlockHeader(); 101 ExtentBlockTail* BlockTail(ExtentDataHeader* header); 102 ExtentLeafEntry* BlockFirstLeaf(ExtentBlockTail* tail); 103 xfs_ino_t GetIno(); 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* fMap; 111 char* fBlockBuffer; 112 // This isn't inode data. It holds the directory block. 113 }; 114 115 116 #endif 117