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