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