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