xref: /openbmc/u-boot/include/zfs/spa.h (revision 29b103c7)
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 /*
8  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
9  */
10 
11 #ifndef ZFS_SPA_HEADER
12 #define	ZFS_SPA_HEADER 1
13 
14 
15 /*
16  * General-purpose 32-bit and 64-bit bitfield encodings.
17  */
18 #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
19 #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
20 #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
21 #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
22 
23 #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
24 #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
25 
26 #define	BF32_SET(x, low, len, val)						\
27 	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
28 #define	BF64_SET(x, low, len, val)						\
29 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
30 
31 #define	BF32_GET_SB(x, low, len, shift, bias)		\
32 	((BF32_GET(x, low, len) + (bias)) << (shift))
33 #define	BF64_GET_SB(x, low, len, shift, bias)		\
34 	((BF64_GET(x, low, len) + (bias)) << (shift))
35 
36 #define	BF32_SET_SB(x, low, len, shift, bias, val)		\
37 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
38 #define	BF64_SET_SB(x, low, len, shift, bias, val)		\
39 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
40 
41 /*
42  * We currently support nine block sizes, from 512 bytes to 128K.
43  * We could go higher, but the benefits are near-zero and the cost
44  * of COWing a giant block to modify one byte would become excessive.
45  */
46 #define	SPA_MINBLOCKSHIFT	9
47 #define	SPA_MAXBLOCKSHIFT	17
48 #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
49 #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
50 
51 #define	SPA_BLOCKSIZES		(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
52 
53 /*
54  * Size of block to hold the configuration data (a packed nvlist)
55  */
56 #define	SPA_CONFIG_BLOCKSIZE	(1 << 14)
57 
58 /*
59  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
60  * The ASIZE encoding should be at least 64 times larger (6 more bits)
61  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
62  * overhead, three DVAs per bp, plus one more bit in case we do anything
63  * else that expands the ASIZE.
64  */
65 #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
66 #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
67 #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
68 
69 /*
70  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
71  * The members of the dva_t should be considered opaque outside the SPA.
72  */
73 typedef struct dva {
74 	uint64_t	dva_word[2];
75 } dva_t;
76 
77 /*
78  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
79  */
80 typedef struct zio_cksum {
81 	uint64_t	zc_word[4];
82 } zio_cksum_t;
83 
84 /*
85  * Each block is described by its DVAs, time of birth, checksum, etc.
86  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
87  *
88  *	64	56	48	40	32	24	16	8	0
89  *	+-------+-------+-------+-------+-------+-------+-------+-------+
90  * 0	|		vdev1		| GRID	|	  ASIZE		|
91  *	+-------+-------+-------+-------+-------+-------+-------+-------+
92  * 1	|G|			 offset1				|
93  *	+-------+-------+-------+-------+-------+-------+-------+-------+
94  * 2	|		vdev2		| GRID	|	  ASIZE		|
95  *	+-------+-------+-------+-------+-------+-------+-------+-------+
96  * 3	|G|			 offset2				|
97  *	+-------+-------+-------+-------+-------+-------+-------+-------+
98  * 4	|		vdev3		| GRID	|	  ASIZE		|
99  *	+-------+-------+-------+-------+-------+-------+-------+-------+
100  * 5	|G|			 offset3				|
101  *	+-------+-------+-------+-------+-------+-------+-------+-------+
102  * 6	|BDX|lvl| type	| cksum | comp	|	  PSIZE	|	  LSIZE	|
103  *	+-------+-------+-------+-------+-------+-------+-------+-------+
104  * 7	|			padding					|
105  *	+-------+-------+-------+-------+-------+-------+-------+-------+
106  * 8	|			padding					|
107  *	+-------+-------+-------+-------+-------+-------+-------+-------+
108  * 9	|			physical birth txg			|
109  *	+-------+-------+-------+-------+-------+-------+-------+-------+
110  * a	|			logical birth txg			|
111  *	+-------+-------+-------+-------+-------+-------+-------+-------+
112  * b	|			fill count				|
113  *	+-------+-------+-------+-------+-------+-------+-------+-------+
114  * c	|			checksum[0]				|
115  *	+-------+-------+-------+-------+-------+-------+-------+-------+
116  * d	|			checksum[1]				|
117  *	+-------+-------+-------+-------+-------+-------+-------+-------+
118  * e	|			checksum[2]				|
119  *	+-------+-------+-------+-------+-------+-------+-------+-------+
120  * f	|			checksum[3]				|
121  *	+-------+-------+-------+-------+-------+-------+-------+-------+
122  *
123  * Legend:
124  *
125  * vdev		virtual device ID
126  * offset	offset into virtual device
127  * LSIZE	logical size
128  * PSIZE	physical size (after compression)
129  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
130  * GRID		RAID-Z layout information (reserved for future use)
131  * cksum	checksum function
132  * comp		compression function
133  * G		gang block indicator
134  * B		byteorder (endianness)
135  * D		dedup
136  * X		unused
137  * lvl		level of indirection
138  * type		DMU object type
139  * phys birth	txg of block allocation; zero if same as logical birth txg
140  * log. birth	transaction group in which the block was logically born
141  * fill count	number of non-zero blocks under this bp
142  * checksum[4]	256-bit checksum of the data this bp describes
143  */
144 #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
145 #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
146 
147 typedef struct blkptr {
148 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
149 	uint64_t	blk_prop;	/* size, compression, type, etc		*/
150 	uint64_t	blk_pad[2];	/* Extra space for the future		*/
151 	uint64_t	blk_phys_birth;	/* txg when block was allocated		*/
152 	uint64_t	blk_birth;	/* transaction group at birth		*/
153 	uint64_t	blk_fill;	/* fill count				*/
154 	zio_cksum_t	blk_cksum;	/* 256-bit checksum			*/
155 } blkptr_t;
156 
157 /*
158  * Macros to get and set fields in a bp or DVA.
159  */
160 #define	DVA_GET_ASIZE(dva)											\
161 	BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
162 #define	DVA_SET_ASIZE(dva, x)										\
163 	BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
164 
165 #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
166 #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
167 
168 #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
169 #define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
170 
171 #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
172 #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
173 
174 #define	BP_GET_LSIZE(bp)										\
175 	BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
176 #define	BP_SET_LSIZE(bp, x)										\
177 	BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
178 
179 #define	BP_GET_COMPRESS(bp)		BF64_GET((bp)->blk_prop, 32, 8)
180 #define	BP_SET_COMPRESS(bp, x)		BF64_SET((bp)->blk_prop, 32, 8, x)
181 
182 #define	BP_GET_CHECKSUM(bp)		BF64_GET((bp)->blk_prop, 40, 8)
183 #define	BP_SET_CHECKSUM(bp, x)		BF64_SET((bp)->blk_prop, 40, 8, x)
184 
185 #define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
186 #define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
187 
188 #define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
189 #define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
190 
191 #define	BP_GET_PROP_BIT_61(bp)		BF64_GET((bp)->blk_prop, 61, 1)
192 #define	BP_SET_PROP_BIT_61(bp, x)	BF64_SET((bp)->blk_prop, 61, 1, x)
193 
194 #define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
195 #define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
196 
197 #define	BP_GET_BYTEORDER(bp)		(0 - BF64_GET((bp)->blk_prop, 63, 1))
198 #define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
199 
200 #define	BP_PHYSICAL_BIRTH(bp)										\
201 	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
202 
203 #define	BP_SET_BIRTH(bp, logical, physical)								\
204 	{																	\
205 		(bp)->blk_birth = (logical);									\
206 		(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
207 	}
208 
209 #define	BP_GET_ASIZE(bp)												\
210 	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
211 	 DVA_GET_ASIZE(&(bp)->blk_dva[2]))
212 
213 #define	BP_GET_UCSIZE(bp)												\
214 	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ?	\
215 	 BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
216 
217 #define	BP_GET_NDVAS(bp)						\
218 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) +		\
219 	 !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) +		\
220 	 !!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
221 
222 #define	BP_COUNT_GANG(bp)						\
223 	(DVA_GET_GANG(&(bp)->blk_dva[0]) +			\
224 	 DVA_GET_GANG(&(bp)->blk_dva[1]) +			\
225 	 DVA_GET_GANG(&(bp)->blk_dva[2]))
226 
227 #define	DVA_EQUAL(dva1, dva2)						\
228 	((dva1)->dva_word[1] == (dva2)->dva_word[1] &&	\
229 	 (dva1)->dva_word[0] == (dva2)->dva_word[0])
230 
231 #define	BP_EQUAL(bp1, bp2)									\
232 	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
233 	 DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
234 	 DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
235 	 DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
236 
237 #define	ZIO_CHECKSUM_EQUAL(zc1, zc2)				\
238 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) |	\
239 		   ((zc1).zc_word[1] - (zc2).zc_word[1]) |	\
240 		   ((zc1).zc_word[2] - (zc2).zc_word[2]) |	\
241 		   ((zc1).zc_word[3] - (zc2).zc_word[3])))
242 
243 #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
244 
245 #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
246 	{											\
247 		(zcp)->zc_word[0] = w0;					\
248 		(zcp)->zc_word[1] = w1;					\
249 		(zcp)->zc_word[2] = w2;					\
250 		(zcp)->zc_word[3] = w3;					\
251 	}
252 
253 #define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
254 #define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
255 #define	BP_IS_HOLE(bp)		((bp)->blk_birth == 0)
256 
257 /* BP_IS_RAIDZ(bp) assumes no block compression */
258 #define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
259 							 BP_GET_PSIZE(bp))
260 
261 #define	BP_ZERO(bp)										\
262 	{													\
263 		(bp)->blk_dva[0].dva_word[0] = 0;				\
264 		(bp)->blk_dva[0].dva_word[1] = 0;				\
265 		(bp)->blk_dva[1].dva_word[0] = 0;				\
266 		(bp)->blk_dva[1].dva_word[1] = 0;				\
267 		(bp)->blk_dva[2].dva_word[0] = 0;				\
268 		(bp)->blk_dva[2].dva_word[1] = 0;				\
269 		(bp)->blk_prop = 0;								\
270 		(bp)->blk_pad[0] = 0;							\
271 		(bp)->blk_pad[1] = 0;							\
272 		(bp)->blk_phys_birth = 0;						\
273 		(bp)->blk_birth = 0;							\
274 		(bp)->blk_fill = 0;								\
275 		ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
276 	}
277 
278 #define	BP_SPRINTF_LEN	320
279 
280 #endif	/* ! ZFS_SPA_HEADER */
281