xref: /haiku/src/add-ons/kernel/file_systems/xfs/Extent.h (revision 553f3f2309e87d95345220463fa865d5fd8cf28d)
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 DIR2_FREE_TAG 0xffff
16 #define XFS_DIR2_DATA_FD_COUNT 3
17 #define EXTENT_REC_SIZE		128
18 #define MASK(n) ((1UL << n) - 1)
19 #define FSBLOCKS_TO_AGNO(n, volume) (n >> volume->AgBlocksLog())
20 #define FSBLOCKS_TO_AGBLOCKNO(n, volume) (n & MASK(volume->AgBlocksLog()))
21 
22 
23 
24 // xfs_exntst_t
25 enum ExtentState {
26 	XFS_EXT_NORM,
27 	XFS_EXT_UNWRITTEN,
28 	XFS_EXT_INVALID
29 };
30 
31 
32 // xfs_dir2_data_free_t
33 struct FreeRegion {
34 			uint16				offset;
35 			uint16				length;
36 };
37 
38 
39 // xfs_dir2_data_hdr_t
40 struct ExtentDataHeader {
41 			uint32				magic;
42 			FreeRegion			bestfree[XFS_DIR2_DATA_FD_COUNT];
43 };
44 
45 
46 // xfs_dir2_data_entry_t
47 struct ExtentDataEntry {
48 			xfs_ino_t			inumber;
49 			uint8				namelen;
50 			uint8				name[];
51 
52 // Followed by a file type (8bit) if applicable and a 16bit tag
53 // tag is the offset from start of the block
54 };
55 
56 
57 // xfs_dir2_data_unused_t
58 struct ExtentUnusedEntry {
59 			uint16				freetag;
60 				// takes the value 0xffff
61 			uint16				length;
62 				// freetag+length overrides the inumber of an entry
63 			uint16				tag;
64 };
65 
66 
67 // xfs_dir2_leaf_entry_t
68 struct ExtentLeafEntry {
69 			uint32				hashval;
70 			uint32				address;
71 				// offset into block after >> 3
72 };
73 
74 
75 // xfs_dir2_block_tail_t
76 struct ExtentBlockTail {
77 			uint32				count;
78 				// # of entries in leaf
79 			uint32				stale;
80 				// # of free leaf entries
81 };
82 
83 
84 struct ExtentMapEntry {
85 			xfs_fileoff_t		br_startoff;
86 				// logical file block offset
87 			xfs_fsblock_t		br_startblock;
88 				// absolute block number
89 			xfs_filblks_t		br_blockcount;
90 				// # of blocks
91 			uint8				br_state;
92 				// state of the extent
93 };
94 
95 
96 class Extent
97 {
98 public:
99 								Extent(Inode* inode);
100 								~Extent();
101 			status_t			Init();
102 			bool				IsBlockType();
103 			void				FillMapEntry(void* pointerToMap);
104 			status_t			FillBlockBuffer();
105 			ExtentBlockTail*	BlockTail();
106 			ExtentLeafEntry*	BlockFirstLeaf(ExtentBlockTail* tail);
107 			xfs_ino_t			GetIno();
108 			uint32				GetOffsetFromAddress(uint32 address);
109 			int					EntrySize(int len) const;
110 			status_t			GetNext(char* name, size_t* length,
111 									xfs_ino_t* ino);
112 			status_t			Lookup(const char* name, size_t length,
113 									xfs_ino_t* id);
114 private:
115 			Inode*				fInode;
116 			ExtentMapEntry*		fMap;
117 			uint32				fOffset;
118 			char*				fBlockBuffer;
119 				// This isn't inode data. It holds the directory block.
120 };
121 
122 
123 #endif
124