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