xref: /haiku/src/add-ons/kernel/file_systems/xfs/xfs.cpp (revision 6f80a9801fedbe7355c4360bd204ba746ec3ec2d)
1 /*
2 * Copyright 2020, Shubham Bhagat, shubhambhagat111@yahoo.com
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5 
6 #include "xfs.h"
7 
8 
9 uint8
10 XfsSuperBlock::Flags() const
11 {
12 	return sb_flags;
13 }
14 
15 
16 bool
17 XfsSuperBlock::IsValid() const
18 {
19 	if (sb_magicnum != XFS_SB_MAGIC)
20 		return false;
21 
22 	if (BASICBLOCKSIZE > sb_blocksize) {
23 		ERROR("Basic block is less than 512 bytes!");
24 		return false;
25 	}
26 
27 	// Checking version 4 file system
28 	if ((Version() & 0x000f) > 4) {
29 		ERROR("XFS version 5 or later is not yet supported");
30 		return false;
31 	}
32 
33 	return true;
34 }
35 
36 
37 uint16
38 XfsSuperBlock::Version() const
39 {
40 	return sb_versionnum;
41 }
42 
43 
44 uint32
45 XfsSuperBlock::Features2() const
46 {
47 	return sb_features2;
48 }
49 
50 
51 uint32
52 XfsSuperBlock::BlockSize() const
53 {
54 	return sb_blocksize;
55 }
56 
57 
58 uint32
59 XfsSuperBlock::DirBlockLog() const
60 {
61 	return sb_dirblklog;
62 }
63 
64 
65 uint8
66 XfsSuperBlock::BlockLog() const
67 {
68 	return sb_blocklog;
69 }
70 
71 
72 uint32
73 XfsSuperBlock::DirBlockSize() const
74 {
75 	return BlockSize() * (1 << sb_dirblklog);
76 }
77 
78 
79 uint8
80 XfsSuperBlock::AgInodeBits() const
81 {
82 	return AgBlocksLog() + InodesPerBlkLog();
83 }
84 
85 
86 uint8
87 XfsSuperBlock::InodesPerBlkLog() const
88 {
89 	return sb_inopblog;
90 }
91 
92 
93 uint8
94 XfsSuperBlock::AgBlocksLog() const
95 {
96 	return sb_agblklog;
97 }
98 
99 
100 uint32
101 XfsSuperBlock::Size() const
102 {
103 	return XFS_SB_MAXSIZE;
104 }
105 
106 
107 uint16
108 XfsSuperBlock::InodeSize() const
109 {
110 	return sb_inodesize;
111 }
112 
113 
114 xfs_rfsblock_t
115 XfsSuperBlock::TotalBlocks() const
116 {
117 	return sb_dblocks;
118 }
119 
120 
121 xfs_rfsblock_t
122 XfsSuperBlock::TotalBlocksWithLog() const
123 {
124 	return TotalBlocks() + sb_logblocks;
125 }
126 
127 
128 uint64
129 XfsSuperBlock::FreeBlocks() const
130 {
131 	return sb_fdblocks;
132 }
133 
134 
135 uint64
136 XfsSuperBlock::UsedBlocks() const
137 {
138 	return TotalBlocks() - FreeBlocks();
139 }
140 
141 
142 const char*
143 XfsSuperBlock::Name() const
144 {
145 	return sb_fname;
146 }
147 
148 
149 xfs_ino_t
150 XfsSuperBlock::Root() const
151 {
152 	return sb_rootino;
153 }
154 
155 
156 xfs_agnumber_t
157 XfsSuperBlock::AgCount() const
158 {
159 	return sb_agcount;
160 }
161 
162 
163 xfs_agblock_t
164 XfsSuperBlock::AgBlocks() const
165 {
166 	return sb_agblocks;
167 }
168 
169 
170 void
171 XfsSuperBlock::SwapEndian()
172 {
173 	sb_magicnum		=	B_BENDIAN_TO_HOST_INT32(sb_magicnum);
174 	sb_blocksize	=	B_BENDIAN_TO_HOST_INT32(sb_blocksize);
175 	sb_dblocks		=	B_BENDIAN_TO_HOST_INT64(sb_dblocks);
176 	sb_rblocks		=	B_BENDIAN_TO_HOST_INT64(sb_rblocks);
177 	sb_rextents		=	B_BENDIAN_TO_HOST_INT64(sb_rextents);
178 	sb_logstart		=	B_BENDIAN_TO_HOST_INT64(sb_logstart);
179 	sb_rootino		=	B_BENDIAN_TO_HOST_INT64(sb_rootino);
180 	sb_rbmino		=	B_BENDIAN_TO_HOST_INT64(sb_rbmino);
181 	sb_rsumino		=	B_BENDIAN_TO_HOST_INT64(sb_rsumino);
182 	sb_rextsize		=	B_BENDIAN_TO_HOST_INT32(sb_rextsize);
183 	sb_agblocks		=	B_BENDIAN_TO_HOST_INT32(sb_agblocks);
184 	sb_agcount		=	B_BENDIAN_TO_HOST_INT32(sb_agcount);
185 	sb_rbmblocks	=	B_BENDIAN_TO_HOST_INT32(sb_rbmblocks);
186 	sb_logblocks	=	B_BENDIAN_TO_HOST_INT32(sb_logblocks);
187 	sb_versionnum	=	B_BENDIAN_TO_HOST_INT16(sb_versionnum);
188 	sb_sectsize		=	B_BENDIAN_TO_HOST_INT16(sb_sectsize);
189 	sb_inodesize	=	B_BENDIAN_TO_HOST_INT16(sb_inodesize);
190 	sb_inopblock	=	B_BENDIAN_TO_HOST_INT16(sb_inopblock);
191 	sb_icount		=	B_BENDIAN_TO_HOST_INT64(sb_icount);
192 	sb_ifree		=	B_BENDIAN_TO_HOST_INT64(sb_ifree);
193 	sb_fdblocks		=	B_BENDIAN_TO_HOST_INT64(sb_fdblocks);
194 	sb_frextents	=	B_BENDIAN_TO_HOST_INT64(sb_frextents);
195 	sb_uquotino		=	B_BENDIAN_TO_HOST_INT64(sb_uquotino);
196 	sb_gquotino		=	B_BENDIAN_TO_HOST_INT64(sb_gquotino);
197 	sb_qflags		=	B_BENDIAN_TO_HOST_INT16(sb_qflags);
198 	sb_inoalignmt	=	B_BENDIAN_TO_HOST_INT32(sb_inoalignmt);
199 	sb_unit			=	B_BENDIAN_TO_HOST_INT32(sb_unit);
200 	sb_width		=	B_BENDIAN_TO_HOST_INT32(sb_width);
201 	sb_logsectsize	=	B_BENDIAN_TO_HOST_INT16(sb_logsectsize);
202 	sb_logsunit		=	B_BENDIAN_TO_HOST_INT32(sb_logsunit);
203 	sb_features2	=	B_BENDIAN_TO_HOST_INT32(sb_features2);
204 }
205