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