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