xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/spa.h (revision 5d7b4d438c4a51eccc95e77a83a437b4d48380eb)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2343466aaeSMax Grossman  * Copyright (c) 2013 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #ifndef _SYS_SPA_H
28fa9e4066Sahrens #define	_SYS_SPA_H
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/avl.h>
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/nvpair.h>
33fa9e4066Sahrens #include <sys/sysmacros.h>
34fa9e4066Sahrens #include <sys/types.h>
35fa9e4066Sahrens #include <sys/fs/zfs.h>
36fa9e4066Sahrens 
37fa9e4066Sahrens #ifdef	__cplusplus
38fa9e4066Sahrens extern "C" {
39fa9e4066Sahrens #endif
40fa9e4066Sahrens 
41fa9e4066Sahrens /*
42fa9e4066Sahrens  * Forward references that lots of things need.
43fa9e4066Sahrens  */
44fa9e4066Sahrens typedef struct spa spa_t;
45fa9e4066Sahrens typedef struct vdev vdev_t;
46fa9e4066Sahrens typedef struct metaslab metaslab_t;
47b24ab676SJeff Bonwick typedef struct metaslab_group metaslab_group_t;
48b24ab676SJeff Bonwick typedef struct metaslab_class metaslab_class_t;
49b24ab676SJeff Bonwick typedef struct zio zio_t;
50fa9e4066Sahrens typedef struct zilog zilog_t;
51fa94a07fSbrendan typedef struct spa_aux_vdev spa_aux_vdev_t;
52b24ab676SJeff Bonwick typedef struct ddt ddt_t;
53b24ab676SJeff Bonwick typedef struct ddt_entry ddt_entry_t;
54fa9e4066Sahrens struct dsl_pool;
554445fffbSMatthew Ahrens struct dsl_dataset;
56fa9e4066Sahrens 
57fa9e4066Sahrens /*
58fa9e4066Sahrens  * General-purpose 32-bit and 64-bit bitfield encodings.
59fa9e4066Sahrens  */
60fa9e4066Sahrens #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
61fa9e4066Sahrens #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
62fa9e4066Sahrens #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
63fa9e4066Sahrens #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
64fa9e4066Sahrens 
65fa9e4066Sahrens #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
66fa9e4066Sahrens #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
67fa9e4066Sahrens 
6843466aaeSMax Grossman #define	BF32_SET(x, low, len, val) do { \
6943466aaeSMax Grossman 	ASSERT3U(val, <, 1U << (len)); \
7043466aaeSMax Grossman 	ASSERT3U(low + len, <=, 32); \
7143466aaeSMax Grossman 	(x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
7243466aaeSMax Grossman _NOTE(CONSTCOND) } while (0)
7343466aaeSMax Grossman 
7443466aaeSMax Grossman #define	BF64_SET(x, low, len, val) do { \
7543466aaeSMax Grossman 	ASSERT3U(val, <, 1ULL << (len)); \
7643466aaeSMax Grossman 	ASSERT3U(low + len, <=, 64); \
7743466aaeSMax Grossman 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
7843466aaeSMax Grossman _NOTE(CONSTCOND) } while (0)
79fa9e4066Sahrens 
80fa9e4066Sahrens #define	BF32_GET_SB(x, low, len, shift, bias)	\
81fa9e4066Sahrens 	((BF32_GET(x, low, len) + (bias)) << (shift))
82fa9e4066Sahrens #define	BF64_GET_SB(x, low, len, shift, bias)	\
83fa9e4066Sahrens 	((BF64_GET(x, low, len) + (bias)) << (shift))
84fa9e4066Sahrens 
8543466aaeSMax Grossman #define	BF32_SET_SB(x, low, len, shift, bias, val) do { \
8643466aaeSMax Grossman 	ASSERT(IS_P2ALIGNED(val, 1U << shift)); \
8743466aaeSMax Grossman 	ASSERT3S((val) >> (shift), >=, bias); \
8843466aaeSMax Grossman 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
8943466aaeSMax Grossman _NOTE(CONSTCOND) } while (0)
9043466aaeSMax Grossman #define	BF64_SET_SB(x, low, len, shift, bias, val) do { \
9143466aaeSMax Grossman 	ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \
9243466aaeSMax Grossman 	ASSERT3S((val) >> (shift), >=, bias); \
9343466aaeSMax Grossman 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
9443466aaeSMax Grossman _NOTE(CONSTCOND) } while (0)
95fa9e4066Sahrens 
96fa9e4066Sahrens /*
97fa9e4066Sahrens  * We currently support nine block sizes, from 512 bytes to 128K.
98fa9e4066Sahrens  * We could go higher, but the benefits are near-zero and the cost
99fa9e4066Sahrens  * of COWing a giant block to modify one byte would become excessive.
100fa9e4066Sahrens  */
101fa9e4066Sahrens #define	SPA_MINBLOCKSHIFT	9
102fa9e4066Sahrens #define	SPA_MAXBLOCKSHIFT	17
103fa9e4066Sahrens #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
104fa9e4066Sahrens #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
105fa9e4066Sahrens 
106fa9e4066Sahrens #define	SPA_BLOCKSIZES		(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
107fa9e4066Sahrens 
108f7991ba4STim Haley /*
109f7991ba4STim Haley  * Size of block to hold the configuration data (a packed nvlist)
110f7991ba4STim Haley  */
111ad135b5dSChristopher Siden #define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
112f7991ba4STim Haley 
113fa9e4066Sahrens /*
114fa9e4066Sahrens  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
115fa9e4066Sahrens  * The ASIZE encoding should be at least 64 times larger (6 more bits)
116fa9e4066Sahrens  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
117fa9e4066Sahrens  * overhead, three DVAs per bp, plus one more bit in case we do anything
118fa9e4066Sahrens  * else that expands the ASIZE.
119fa9e4066Sahrens  */
120fa9e4066Sahrens #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
121fa9e4066Sahrens #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
122fa9e4066Sahrens #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
123fa9e4066Sahrens 
124fa9e4066Sahrens /*
125fa9e4066Sahrens  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
126fa9e4066Sahrens  * The members of the dva_t should be considered opaque outside the SPA.
127fa9e4066Sahrens  */
128fa9e4066Sahrens typedef struct dva {
129fa9e4066Sahrens 	uint64_t	dva_word[2];
130fa9e4066Sahrens } dva_t;
131fa9e4066Sahrens 
132fa9e4066Sahrens /*
133fa9e4066Sahrens  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
134fa9e4066Sahrens  */
135fa9e4066Sahrens typedef struct zio_cksum {
136fa9e4066Sahrens 	uint64_t	zc_word[4];
137fa9e4066Sahrens } zio_cksum_t;
138fa9e4066Sahrens 
139fa9e4066Sahrens /*
140fa9e4066Sahrens  * Each block is described by its DVAs, time of birth, checksum, etc.
141fa9e4066Sahrens  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
142fa9e4066Sahrens  *
143fa9e4066Sahrens  *	64	56	48	40	32	24	16	8	0
144fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
145fa9e4066Sahrens  * 0	|		vdev1		| GRID  |	  ASIZE		|
146fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
147fa9e4066Sahrens  * 1	|G|			 offset1				|
148fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
149fa9e4066Sahrens  * 2	|		vdev2		| GRID  |	  ASIZE		|
150fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
151fa9e4066Sahrens  * 3	|G|			 offset2				|
152fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
153fa9e4066Sahrens  * 4	|		vdev3		| GRID  |	  ASIZE		|
154fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
155fa9e4066Sahrens  * 5	|G|			 offset3				|
156fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
157*5d7b4d43SMatthew Ahrens  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
158fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
159fa9e4066Sahrens  * 7	|			padding					|
160fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
161fa9e4066Sahrens  * 8	|			padding					|
162fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
163b24ab676SJeff Bonwick  * 9	|			physical birth txg			|
164fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
165b24ab676SJeff Bonwick  * a	|			logical birth txg			|
166fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
167fa9e4066Sahrens  * b	|			fill count				|
168fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
169fa9e4066Sahrens  * c	|			checksum[0]				|
170fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
171fa9e4066Sahrens  * d	|			checksum[1]				|
172fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
173fa9e4066Sahrens  * e	|			checksum[2]				|
174fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
175fa9e4066Sahrens  * f	|			checksum[3]				|
176fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
177fa9e4066Sahrens  *
178fa9e4066Sahrens  * Legend:
179fa9e4066Sahrens  *
180fa9e4066Sahrens  * vdev		virtual device ID
181fa9e4066Sahrens  * offset	offset into virtual device
182fa9e4066Sahrens  * LSIZE	logical size
183fa9e4066Sahrens  * PSIZE	physical size (after compression)
184fa9e4066Sahrens  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
185fa9e4066Sahrens  * GRID		RAID-Z layout information (reserved for future use)
186fa9e4066Sahrens  * cksum	checksum function
187fa9e4066Sahrens  * comp		compression function
188fa9e4066Sahrens  * G		gang block indicator
189b24ab676SJeff Bonwick  * B		byteorder (endianness)
190b24ab676SJeff Bonwick  * D		dedup
191*5d7b4d43SMatthew Ahrens  * X		encryption (on version 30, which is not supported)
192*5d7b4d43SMatthew Ahrens  * E		blkptr_t contains embedded data (see below)
193fa9e4066Sahrens  * lvl		level of indirection
194b24ab676SJeff Bonwick  * type		DMU object type
195b24ab676SJeff Bonwick  * phys birth	txg of block allocation; zero if same as logical birth txg
196b24ab676SJeff Bonwick  * log. birth	transaction group in which the block was logically born
197fa9e4066Sahrens  * fill count	number of non-zero blocks under this bp
198fa9e4066Sahrens  * checksum[4]	256-bit checksum of the data this bp describes
199fa9e4066Sahrens  */
200*5d7b4d43SMatthew Ahrens 
201*5d7b4d43SMatthew Ahrens /*
202*5d7b4d43SMatthew Ahrens  * "Embedded" blkptr_t's don't actually point to a block, instead they
203*5d7b4d43SMatthew Ahrens  * have a data payload embedded in the blkptr_t itself.  See the comment
204*5d7b4d43SMatthew Ahrens  * in blkptr.c for more details.
205*5d7b4d43SMatthew Ahrens  *
206*5d7b4d43SMatthew Ahrens  * The blkptr_t is laid out as follows:
207*5d7b4d43SMatthew Ahrens  *
208*5d7b4d43SMatthew Ahrens  *	64	56	48	40	32	24	16	8	0
209*5d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
210*5d7b4d43SMatthew Ahrens  * 0	|      payload                                                  |
211*5d7b4d43SMatthew Ahrens  * 1	|      payload                                                  |
212*5d7b4d43SMatthew Ahrens  * 2	|      payload                                                  |
213*5d7b4d43SMatthew Ahrens  * 3	|      payload                                                  |
214*5d7b4d43SMatthew Ahrens  * 4	|      payload                                                  |
215*5d7b4d43SMatthew Ahrens  * 5	|      payload                                                  |
216*5d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
217*5d7b4d43SMatthew Ahrens  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
218*5d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
219*5d7b4d43SMatthew Ahrens  * 7	|      payload                                                  |
220*5d7b4d43SMatthew Ahrens  * 8	|      payload                                                  |
221*5d7b4d43SMatthew Ahrens  * 9	|      payload                                                  |
222*5d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
223*5d7b4d43SMatthew Ahrens  * a	|			logical birth txg			|
224*5d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
225*5d7b4d43SMatthew Ahrens  * b	|      payload                                                  |
226*5d7b4d43SMatthew Ahrens  * c	|      payload                                                  |
227*5d7b4d43SMatthew Ahrens  * d	|      payload                                                  |
228*5d7b4d43SMatthew Ahrens  * e	|      payload                                                  |
229*5d7b4d43SMatthew Ahrens  * f	|      payload                                                  |
230*5d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
231*5d7b4d43SMatthew Ahrens  *
232*5d7b4d43SMatthew Ahrens  * Legend:
233*5d7b4d43SMatthew Ahrens  *
234*5d7b4d43SMatthew Ahrens  * payload		contains the embedded data
235*5d7b4d43SMatthew Ahrens  * B (byteorder)	byteorder (endianness)
236*5d7b4d43SMatthew Ahrens  * D (dedup)		padding (set to zero)
237*5d7b4d43SMatthew Ahrens  * X			encryption (set to zero; see above)
238*5d7b4d43SMatthew Ahrens  * E (embedded)		set to one
239*5d7b4d43SMatthew Ahrens  * lvl			indirection level
240*5d7b4d43SMatthew Ahrens  * type			DMU object type
241*5d7b4d43SMatthew Ahrens  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
242*5d7b4d43SMatthew Ahrens  * comp			compression function of payload
243*5d7b4d43SMatthew Ahrens  * PSIZE		size of payload after compression, in bytes
244*5d7b4d43SMatthew Ahrens  * LSIZE		logical size of payload, in bytes
245*5d7b4d43SMatthew Ahrens  *			note that 25 bits is enough to store the largest
246*5d7b4d43SMatthew Ahrens  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
247*5d7b4d43SMatthew Ahrens  * log. birth		transaction group in which the block was logically born
248*5d7b4d43SMatthew Ahrens  *
249*5d7b4d43SMatthew Ahrens  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
250*5d7b4d43SMatthew Ahrens  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
251*5d7b4d43SMatthew Ahrens  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
252*5d7b4d43SMatthew Ahrens  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
253*5d7b4d43SMatthew Ahrens  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
254*5d7b4d43SMatthew Ahrens  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
255*5d7b4d43SMatthew Ahrens  * other macros, as they assert that they are only used on BP's of the correct
256*5d7b4d43SMatthew Ahrens  * "embedded-ness".
257*5d7b4d43SMatthew Ahrens  */
258*5d7b4d43SMatthew Ahrens 
259*5d7b4d43SMatthew Ahrens #define	BPE_GET_ETYPE(bp)	\
260*5d7b4d43SMatthew Ahrens 	(ASSERT(BP_IS_EMBEDDED(bp)), \
261*5d7b4d43SMatthew Ahrens 	BF64_GET((bp)->blk_prop, 40, 8))
262*5d7b4d43SMatthew Ahrens #define	BPE_SET_ETYPE(bp, t)	do { \
263*5d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp)); \
264*5d7b4d43SMatthew Ahrens 	BF64_SET((bp)->blk_prop, 40, 8, t); \
265*5d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
266*5d7b4d43SMatthew Ahrens 
267*5d7b4d43SMatthew Ahrens #define	BPE_GET_LSIZE(bp)	\
268*5d7b4d43SMatthew Ahrens 	(ASSERT(BP_IS_EMBEDDED(bp)), \
269*5d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
270*5d7b4d43SMatthew Ahrens #define	BPE_SET_LSIZE(bp, x)	do { \
271*5d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp)); \
272*5d7b4d43SMatthew Ahrens 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
273*5d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
274*5d7b4d43SMatthew Ahrens 
275*5d7b4d43SMatthew Ahrens #define	BPE_GET_PSIZE(bp)	\
276*5d7b4d43SMatthew Ahrens 	(ASSERT(BP_IS_EMBEDDED(bp)), \
277*5d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
278*5d7b4d43SMatthew Ahrens #define	BPE_SET_PSIZE(bp, x)	do { \
279*5d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp)); \
280*5d7b4d43SMatthew Ahrens 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
281*5d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
282*5d7b4d43SMatthew Ahrens 
283*5d7b4d43SMatthew Ahrens typedef enum bp_embedded_type {
284*5d7b4d43SMatthew Ahrens 	BP_EMBEDDED_TYPE_DATA,
285*5d7b4d43SMatthew Ahrens 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
286*5d7b4d43SMatthew Ahrens 	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
287*5d7b4d43SMatthew Ahrens } bp_embedded_type_t;
288*5d7b4d43SMatthew Ahrens 
289*5d7b4d43SMatthew Ahrens #define	BPE_NUM_WORDS 14
290*5d7b4d43SMatthew Ahrens #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
291*5d7b4d43SMatthew Ahrens #define	BPE_IS_PAYLOADWORD(bp, wp) \
292*5d7b4d43SMatthew Ahrens 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
293*5d7b4d43SMatthew Ahrens 
294fa9e4066Sahrens #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
295fa9e4066Sahrens #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
296fa9e4066Sahrens 
29743466aaeSMax Grossman /*
29843466aaeSMax Grossman  * A block is a hole when it has either 1) never been written to, or
29943466aaeSMax Grossman  * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
30043466aaeSMax Grossman  * without physically allocating disk space. Holes are represented in the
30143466aaeSMax Grossman  * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
30243466aaeSMax Grossman  * done through the BP_IS_HOLE macro. For holes, the logical size, level,
30343466aaeSMax Grossman  * DMU object type, and birth times are all also stored for holes that
30443466aaeSMax Grossman  * were written to at some point (i.e. were punched after having been filled).
30543466aaeSMax Grossman  */
306b24ab676SJeff Bonwick typedef struct blkptr {
307b24ab676SJeff Bonwick 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
308b24ab676SJeff Bonwick 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
309b24ab676SJeff Bonwick 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
310b24ab676SJeff Bonwick 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
311b24ab676SJeff Bonwick 	uint64_t	blk_birth;	/* transaction group at birth	    */
312b24ab676SJeff Bonwick 	uint64_t	blk_fill;	/* fill count			    */
313b24ab676SJeff Bonwick 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
314b24ab676SJeff Bonwick } blkptr_t;
315b24ab676SJeff Bonwick 
316fa9e4066Sahrens /*
317fa9e4066Sahrens  * Macros to get and set fields in a bp or DVA.
318fa9e4066Sahrens  */
319fa9e4066Sahrens #define	DVA_GET_ASIZE(dva)	\
32043466aaeSMax Grossman 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
321fa9e4066Sahrens #define	DVA_SET_ASIZE(dva, x)	\
32243466aaeSMax Grossman 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
32343466aaeSMax Grossman 	SPA_MINBLOCKSHIFT, 0, x)
324fa9e4066Sahrens 
325fa9e4066Sahrens #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
326fa9e4066Sahrens #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
327fa9e4066Sahrens 
328fa9e4066Sahrens #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
329fa9e4066Sahrens #define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
330fa9e4066Sahrens 
331fa9e4066Sahrens #define	DVA_GET_OFFSET(dva)	\
332fa9e4066Sahrens 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
333fa9e4066Sahrens #define	DVA_SET_OFFSET(dva, x)	\
334fa9e4066Sahrens 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
335fa9e4066Sahrens 
336fa9e4066Sahrens #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
337fa9e4066Sahrens #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
338fa9e4066Sahrens 
339fa9e4066Sahrens #define	BP_GET_LSIZE(bp)	\
340*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ?	\
341*5d7b4d43SMatthew Ahrens 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
342*5d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
343*5d7b4d43SMatthew Ahrens #define	BP_SET_LSIZE(bp, x)	do { \
344*5d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp)); \
345*5d7b4d43SMatthew Ahrens 	BF64_SET_SB((bp)->blk_prop, \
346*5d7b4d43SMatthew Ahrens 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
347*5d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
348fa9e4066Sahrens 
349fa9e4066Sahrens #define	BP_GET_PSIZE(bp)	\
350*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
351*5d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
352*5d7b4d43SMatthew Ahrens #define	BP_SET_PSIZE(bp, x)	do { \
353*5d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp)); \
354*5d7b4d43SMatthew Ahrens 	BF64_SET_SB((bp)->blk_prop, \
355*5d7b4d43SMatthew Ahrens 	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
356*5d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
357*5d7b4d43SMatthew Ahrens 
358*5d7b4d43SMatthew Ahrens #define	BP_GET_COMPRESS(bp)		BF64_GET((bp)->blk_prop, 32, 7)
359*5d7b4d43SMatthew Ahrens #define	BP_SET_COMPRESS(bp, x)		BF64_SET((bp)->blk_prop, 32, 7, x)
360fa9e4066Sahrens 
361*5d7b4d43SMatthew Ahrens #define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
362*5d7b4d43SMatthew Ahrens #define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
363b24ab676SJeff Bonwick 
364*5d7b4d43SMatthew Ahrens #define	BP_GET_CHECKSUM(bp)		\
365*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
366*5d7b4d43SMatthew Ahrens 	BF64_GET((bp)->blk_prop, 40, 8))
367*5d7b4d43SMatthew Ahrens #define	BP_SET_CHECKSUM(bp, x)		do { \
368*5d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp)); \
369*5d7b4d43SMatthew Ahrens 	BF64_SET((bp)->blk_prop, 40, 8, x); \
370*5d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
371b24ab676SJeff Bonwick 
372b24ab676SJeff Bonwick #define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
373b24ab676SJeff Bonwick #define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
374fa9e4066Sahrens 
375b24ab676SJeff Bonwick #define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
376b24ab676SJeff Bonwick #define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
377fa9e4066Sahrens 
378b24ab676SJeff Bonwick #define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
379b24ab676SJeff Bonwick #define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
380fa9e4066Sahrens 
38143466aaeSMax Grossman #define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
382b24ab676SJeff Bonwick #define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
383b24ab676SJeff Bonwick 
384b24ab676SJeff Bonwick #define	BP_PHYSICAL_BIRTH(bp)		\
385*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
386*5d7b4d43SMatthew Ahrens 	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
387b24ab676SJeff Bonwick 
388b24ab676SJeff Bonwick #define	BP_SET_BIRTH(bp, logical, physical)	\
389b24ab676SJeff Bonwick {						\
390*5d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));		\
391b24ab676SJeff Bonwick 	(bp)->blk_birth = (logical);		\
392b24ab676SJeff Bonwick 	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
393b24ab676SJeff Bonwick }
394fa9e4066Sahrens 
395*5d7b4d43SMatthew Ahrens #define	BP_GET_FILL(bp) (BP_IS_EMBEDDED(bp) ? 1 : (bp)->blk_fill)
396*5d7b4d43SMatthew Ahrens 
397fa9e4066Sahrens #define	BP_GET_ASIZE(bp)	\
398*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
399*5d7b4d43SMatthew Ahrens 	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
400*5d7b4d43SMatthew Ahrens 	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
401*5d7b4d43SMatthew Ahrens 	DVA_GET_ASIZE(&(bp)->blk_dva[2]))
40299653d4eSeschrock 
40399653d4eSeschrock #define	BP_GET_UCSIZE(bp) \
404ad135b5dSChristopher Siden 	((BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) ? \
4053f9d6ad7SLin Ling 	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
406fa9e4066Sahrens 
40744cd46caSbillm #define	BP_GET_NDVAS(bp)	\
408*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
409*5d7b4d43SMatthew Ahrens 	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
41044cd46caSbillm 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
41144cd46caSbillm 	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
41244cd46caSbillm 
41344cd46caSbillm #define	BP_COUNT_GANG(bp)	\
414*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
41544cd46caSbillm 	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
41644cd46caSbillm 	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
417*5d7b4d43SMatthew Ahrens 	DVA_GET_GANG(&(bp)->blk_dva[2])))
41844cd46caSbillm 
419fa9e4066Sahrens #define	DVA_EQUAL(dva1, dva2)	\
420fa9e4066Sahrens 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
421fa9e4066Sahrens 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
422fa9e4066Sahrens 
423b24ab676SJeff Bonwick #define	BP_EQUAL(bp1, bp2)	\
424b24ab676SJeff Bonwick 	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
425*5d7b4d43SMatthew Ahrens 	(bp1)->blk_birth == (bp2)->blk_birth &&			\
426b24ab676SJeff Bonwick 	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
427b24ab676SJeff Bonwick 	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
428b24ab676SJeff Bonwick 	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
429b24ab676SJeff Bonwick 
4306b4acc8bSahrens #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
4316b4acc8bSahrens 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
4326b4acc8bSahrens 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
4336b4acc8bSahrens 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
4346b4acc8bSahrens 	((zc1).zc_word[3] - (zc2).zc_word[3])))
4356b4acc8bSahrens 
436fa9e4066Sahrens #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
437fa9e4066Sahrens 
438fa9e4066Sahrens #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
439fa9e4066Sahrens {						\
440fa9e4066Sahrens 	(zcp)->zc_word[0] = w0;			\
441fa9e4066Sahrens 	(zcp)->zc_word[1] = w1;			\
442fa9e4066Sahrens 	(zcp)->zc_word[2] = w2;			\
443fa9e4066Sahrens 	(zcp)->zc_word[3] = w3;			\
444fa9e4066Sahrens }
445fa9e4066Sahrens 
446*5d7b4d43SMatthew Ahrens #define	BP_IDENTITY(bp)		(ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
447*5d7b4d43SMatthew Ahrens #define	BP_IS_GANG(bp)		\
448*5d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
44943466aaeSMax Grossman #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&	\
45043466aaeSMax Grossman 				(dva)->dva_word[1] == 0ULL)
451*5d7b4d43SMatthew Ahrens #define	BP_IS_HOLE(bp) \
452*5d7b4d43SMatthew Ahrens 	(!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
453fa9e4066Sahrens 
4546e1f5caaSNeil Perrin /* BP_IS_RAIDZ(bp) assumes no block compression */
4556e1f5caaSNeil Perrin #define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
4566e1f5caaSNeil Perrin 				BP_GET_PSIZE(bp))
4576e1f5caaSNeil Perrin 
458e14bb325SJeff Bonwick #define	BP_ZERO(bp)				\
459fa9e4066Sahrens {						\
460fa9e4066Sahrens 	(bp)->blk_dva[0].dva_word[0] = 0;	\
461fa9e4066Sahrens 	(bp)->blk_dva[0].dva_word[1] = 0;	\
462fa9e4066Sahrens 	(bp)->blk_dva[1].dva_word[0] = 0;	\
463fa9e4066Sahrens 	(bp)->blk_dva[1].dva_word[1] = 0;	\
464fa9e4066Sahrens 	(bp)->blk_dva[2].dva_word[0] = 0;	\
465fa9e4066Sahrens 	(bp)->blk_dva[2].dva_word[1] = 0;	\
466fa9e4066Sahrens 	(bp)->blk_prop = 0;			\
467fa9e4066Sahrens 	(bp)->blk_pad[0] = 0;			\
468fa9e4066Sahrens 	(bp)->blk_pad[1] = 0;			\
469b24ab676SJeff Bonwick 	(bp)->blk_phys_birth = 0;		\
470e14bb325SJeff Bonwick 	(bp)->blk_birth = 0;			\
471fa9e4066Sahrens 	(bp)->blk_fill = 0;			\
472fa9e4066Sahrens 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
473fa9e4066Sahrens }
474fa9e4066Sahrens 
475fa9e4066Sahrens #ifdef _BIG_ENDIAN
476fa9e4066Sahrens #define	ZFS_HOST_BYTEORDER	(0ULL)
477fa9e4066Sahrens #else
47843466aaeSMax Grossman #define	ZFS_HOST_BYTEORDER	(1ULL)
479fa9e4066Sahrens #endif
480fa9e4066Sahrens 
481fa9e4066Sahrens #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
482fa9e4066Sahrens 
48344cd46caSbillm #define	BP_SPRINTF_LEN	320
484fbabab8fSmaybee 
485b24ab676SJeff Bonwick /*
486b24ab676SJeff Bonwick  * This macro allows code sharing between zfs, libzpool, and mdb.
487b24ab676SJeff Bonwick  * 'func' is either snprintf() or mdb_snprintf().
488b24ab676SJeff Bonwick  * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
489b24ab676SJeff Bonwick  */
49043466aaeSMax Grossman #define	SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
491b24ab676SJeff Bonwick {									\
492b24ab676SJeff Bonwick 	static const char *copyname[] =					\
493b24ab676SJeff Bonwick 	    { "zero", "single", "double", "triple" };			\
494b24ab676SJeff Bonwick 	int len = 0;							\
495b24ab676SJeff Bonwick 	int copies = 0;							\
496b24ab676SJeff Bonwick 									\
497b24ab676SJeff Bonwick 	if (bp == NULL) {						\
49843466aaeSMax Grossman 		len += func(buf + len, size - len, "<NULL>");		\
499b24ab676SJeff Bonwick 	} else if (BP_IS_HOLE(bp)) {					\
50043466aaeSMax Grossman 		len += func(buf + len, size - len, "<hole>");		\
50143466aaeSMax Grossman 		if (bp->blk_birth > 0) {				\
50243466aaeSMax Grossman 			len += func(buf + len, size - len,		\
50343466aaeSMax Grossman 			    " birth=%lluL",				\
50443466aaeSMax Grossman 			    (u_longlong_t)bp->blk_birth);		\
50543466aaeSMax Grossman 		}							\
506*5d7b4d43SMatthew Ahrens 	} else if (BP_IS_EMBEDDED(bp)) {				\
507*5d7b4d43SMatthew Ahrens 		len = func(buf + len, size - len,			\
508*5d7b4d43SMatthew Ahrens 		    "EMBEDDED [L%llu %s] et=%u %s "			\
509*5d7b4d43SMatthew Ahrens 		    "size=%llxL/%llxP birth=%lluL",			\
510*5d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
511*5d7b4d43SMatthew Ahrens 		    type,						\
512*5d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),				\
513*5d7b4d43SMatthew Ahrens 		    compress,						\
514*5d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),			\
515*5d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),			\
516*5d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);			\
517b24ab676SJeff Bonwick 	} else {							\
518b24ab676SJeff Bonwick 		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
519b24ab676SJeff Bonwick 			const dva_t *dva = &bp->blk_dva[d];		\
520b24ab676SJeff Bonwick 			if (DVA_IS_VALID(dva))				\
521b24ab676SJeff Bonwick 				copies++;				\
522b24ab676SJeff Bonwick 			len += func(buf + len, size - len,		\
523b24ab676SJeff Bonwick 			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
524b24ab676SJeff Bonwick 			    (u_longlong_t)DVA_GET_VDEV(dva),		\
525b24ab676SJeff Bonwick 			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
526b24ab676SJeff Bonwick 			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
527b24ab676SJeff Bonwick 			    ws);					\
528b24ab676SJeff Bonwick 		}							\
529b24ab676SJeff Bonwick 		if (BP_IS_GANG(bp) &&					\
530b24ab676SJeff Bonwick 		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
531b24ab676SJeff Bonwick 		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
532b24ab676SJeff Bonwick 			copies--;					\
533b24ab676SJeff Bonwick 		len += func(buf + len, size - len,			\
534b24ab676SJeff Bonwick 		    "[L%llu %s] %s %s %s %s %s %s%c"			\
535b24ab676SJeff Bonwick 		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
536b24ab676SJeff Bonwick 		    "cksum=%llx:%llx:%llx:%llx",			\
537b24ab676SJeff Bonwick 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
538b24ab676SJeff Bonwick 		    type,						\
539b24ab676SJeff Bonwick 		    checksum,						\
540b24ab676SJeff Bonwick 		    compress,						\
541b24ab676SJeff Bonwick 		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
542b24ab676SJeff Bonwick 		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
543b24ab676SJeff Bonwick 		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
544b24ab676SJeff Bonwick 		    copyname[copies],					\
545b24ab676SJeff Bonwick 		    ws,							\
546b24ab676SJeff Bonwick 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
547b24ab676SJeff Bonwick 		    (u_longlong_t)BP_GET_PSIZE(bp),			\
548b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_birth,			\
549b24ab676SJeff Bonwick 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp),		\
550*5d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),			\
551b24ab676SJeff Bonwick 		    ws,							\
552b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
553b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
554b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
555b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
556b24ab676SJeff Bonwick 	}								\
557b24ab676SJeff Bonwick 	ASSERT(len < size);						\
558b24ab676SJeff Bonwick }
559b24ab676SJeff Bonwick 
560fa9e4066Sahrens #include <sys/dmu.h>
561fa9e4066Sahrens 
562ad23a2dbSjohansen #define	BP_GET_BUFC_TYPE(bp)						\
563ad135b5dSChristopher Siden 	(((BP_GET_LEVEL(bp) > 0) || (DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))) ? \
564ad135b5dSChristopher Siden 	ARC_BUFC_METADATA : ARC_BUFC_DATA)
565fa9e4066Sahrens 
5661195e687SMark J Musante typedef enum spa_import_type {
5671195e687SMark J Musante 	SPA_IMPORT_EXISTING,
5681195e687SMark J Musante 	SPA_IMPORT_ASSEMBLE
5691195e687SMark J Musante } spa_import_type_t;
5701195e687SMark J Musante 
571fa9e4066Sahrens /* state manipulation functions */
572fa9e4066Sahrens extern int spa_open(const char *pool, spa_t **, void *tag);
573468c413aSTim Haley extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
574468c413aSTim Haley     nvlist_t *policy, nvlist_t **config);
575ad135b5dSChristopher Siden extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
576ad135b5dSChristopher Siden     size_t buflen);
577990b4856Slling extern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
5784445fffbSMatthew Ahrens     nvlist_t *zplprops);
579051aabe6Staylor extern int spa_import_rootpool(char *devpath, char *devid);
5804b964adaSGeorge Wilson extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props,
5814b964adaSGeorge Wilson     uint64_t flags);
582fa9e4066Sahrens extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
583fa9e4066Sahrens extern int spa_destroy(char *pool);
584394ab0cbSGeorge Wilson extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
585394ab0cbSGeorge Wilson     boolean_t hardforce);
586ea8dc4b6Seschrock extern int spa_reset(char *pool);
587ea8dc4b6Seschrock extern void spa_async_request(spa_t *spa, int flag);
588088f3894Sahrens extern void spa_async_unrequest(spa_t *spa, int flag);
589ea8dc4b6Seschrock extern void spa_async_suspend(spa_t *spa);
590ea8dc4b6Seschrock extern void spa_async_resume(spa_t *spa);
591ea8dc4b6Seschrock extern spa_t *spa_inject_addref(char *pool);
592ea8dc4b6Seschrock extern void spa_inject_delref(spa_t *spa);
5933f9d6ad7SLin Ling extern void spa_scan_stat_init(spa_t *spa);
5943f9d6ad7SLin Ling extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
595ea8dc4b6Seschrock 
596e14bb325SJeff Bonwick #define	SPA_ASYNC_CONFIG_UPDATE	0x01
597e14bb325SJeff Bonwick #define	SPA_ASYNC_REMOVE	0x02
598e14bb325SJeff Bonwick #define	SPA_ASYNC_PROBE		0x04
599e14bb325SJeff Bonwick #define	SPA_ASYNC_RESILVER_DONE	0x08
600e14bb325SJeff Bonwick #define	SPA_ASYNC_RESILVER	0x10
601573ca77eSGeorge Wilson #define	SPA_ASYNC_AUTOEXPAND	0x20
6023f9d6ad7SLin Ling #define	SPA_ASYNC_REMOVE_DONE	0x40
6033f9d6ad7SLin Ling #define	SPA_ASYNC_REMOVE_STOP	0x80
6043f9d6ad7SLin Ling 
6053f9d6ad7SLin Ling /*
6063f9d6ad7SLin Ling  * Controls the behavior of spa_vdev_remove().
6073f9d6ad7SLin Ling  */
6083f9d6ad7SLin Ling #define	SPA_REMOVE_UNSPARE	0x01
6093f9d6ad7SLin Ling #define	SPA_REMOVE_DONE		0x02
610fa9e4066Sahrens 
611fa9e4066Sahrens /* device manipulation */
612fa9e4066Sahrens extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
613ea8dc4b6Seschrock extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
614fa9e4066Sahrens     int replacing);
6158ad4d6ddSJeff Bonwick extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
6168ad4d6ddSJeff Bonwick     int replace_done);
61799653d4eSeschrock extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
6183f9d6ad7SLin Ling extern boolean_t spa_vdev_remove_active(spa_t *spa);
619c67d9675Seschrock extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
6206809eb4eSEric Schrock extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
6211195e687SMark J Musante extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
6221195e687SMark J Musante     nvlist_t *props, boolean_t exp);
623fa9e4066Sahrens 
62499653d4eSeschrock /* spare state (which is global across all pools) */
62539c23413Seschrock extern void spa_spare_add(vdev_t *vd);
62639c23413Seschrock extern void spa_spare_remove(vdev_t *vd);
62789a89ebfSlling extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
62839c23413Seschrock extern void spa_spare_activate(vdev_t *vd);
62999653d4eSeschrock 
630fa94a07fSbrendan /* L2ARC state (which is global across all pools) */
631fa94a07fSbrendan extern void spa_l2cache_add(vdev_t *vd);
632fa94a07fSbrendan extern void spa_l2cache_remove(vdev_t *vd);
633fa94a07fSbrendan extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
634fa94a07fSbrendan extern void spa_l2cache_activate(vdev_t *vd);
635fa94a07fSbrendan extern void spa_l2cache_drop(spa_t *spa);
636fa94a07fSbrendan 
6373f9d6ad7SLin Ling /* scanning */
6383f9d6ad7SLin Ling extern int spa_scan(spa_t *spa, pool_scan_func_t func);
6393f9d6ad7SLin Ling extern int spa_scan_stop(spa_t *spa);
640fa9e4066Sahrens 
641fa9e4066Sahrens /* spa syncing */
642fa9e4066Sahrens extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
643fa9e4066Sahrens extern void spa_sync_allpools(void);
644fa9e4066Sahrens 
6453a737e0dSbrendan /* spa namespace global mutex */
6463a737e0dSbrendan extern kmutex_t spa_namespace_lock;
6473a737e0dSbrendan 
648fa9e4066Sahrens /*
649fa9e4066Sahrens  * SPA configuration functions in spa_config.c
650fa9e4066Sahrens  */
6510373e76bSbonwick 
6520373e76bSbonwick #define	SPA_CONFIG_UPDATE_POOL	0
6530373e76bSbonwick #define	SPA_CONFIG_UPDATE_VDEVS	1
6540373e76bSbonwick 
655c5904d13Seschrock extern void spa_config_sync(spa_t *, boolean_t, boolean_t);
656fa9e4066Sahrens extern void spa_config_load(void);
657fa9e4066Sahrens extern nvlist_t *spa_all_configs(uint64_t *);
658fa9e4066Sahrens extern void spa_config_set(spa_t *spa, nvlist_t *config);
659fa9e4066Sahrens extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
660fa9e4066Sahrens     int getstats);
6610373e76bSbonwick extern void spa_config_update(spa_t *spa, int what);
662fa9e4066Sahrens 
663fa9e4066Sahrens /*
664fa9e4066Sahrens  * Miscellaneous SPA routines in spa_misc.c
665fa9e4066Sahrens  */
666fa9e4066Sahrens 
667fa9e4066Sahrens /* Namespace manipulation */
668fa9e4066Sahrens extern spa_t *spa_lookup(const char *name);
669468c413aSTim Haley extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
670fa9e4066Sahrens extern void spa_remove(spa_t *spa);
671fa9e4066Sahrens extern spa_t *spa_next(spa_t *prev);
672fa9e4066Sahrens 
673fa9e4066Sahrens /* Refcount functions */
674fa9e4066Sahrens extern void spa_open_ref(spa_t *spa, void *tag);
675fa9e4066Sahrens extern void spa_close(spa_t *spa, void *tag);
676fa9e4066Sahrens extern boolean_t spa_refcount_zero(spa_t *spa);
677fa9e4066Sahrens 
6788f18d1faSGeorge Wilson #define	SCL_NONE	0x00
679e14bb325SJeff Bonwick #define	SCL_CONFIG	0x01
680e14bb325SJeff Bonwick #define	SCL_STATE	0x02
681e14bb325SJeff Bonwick #define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
682e14bb325SJeff Bonwick #define	SCL_ALLOC	0x08
683e14bb325SJeff Bonwick #define	SCL_ZIO		0x10
684e14bb325SJeff Bonwick #define	SCL_FREE	0x20
685e14bb325SJeff Bonwick #define	SCL_VDEV	0x40
686e14bb325SJeff Bonwick #define	SCL_LOCKS	7
687e14bb325SJeff Bonwick #define	SCL_ALL		((1 << SCL_LOCKS) - 1)
688e14bb325SJeff Bonwick #define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
689e14bb325SJeff Bonwick 
690e14bb325SJeff Bonwick /* Pool configuration locks */
691e14bb325SJeff Bonwick extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
692e14bb325SJeff Bonwick extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
693e14bb325SJeff Bonwick extern void spa_config_exit(spa_t *spa, int locks, void *tag);
694e14bb325SJeff Bonwick extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
695fa9e4066Sahrens 
696fa9e4066Sahrens /* Pool vdev add/remove lock */
697fa9e4066Sahrens extern uint64_t spa_vdev_enter(spa_t *spa);
69888ecc943SGeorge Wilson extern uint64_t spa_vdev_config_enter(spa_t *spa);
69988ecc943SGeorge Wilson extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
70088ecc943SGeorge Wilson     int error, char *tag);
701fa9e4066Sahrens extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
702fa9e4066Sahrens 
703e14bb325SJeff Bonwick /* Pool vdev state change lock */
7048f18d1faSGeorge Wilson extern void spa_vdev_state_enter(spa_t *spa, int oplock);
705e14bb325SJeff Bonwick extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
706e14bb325SJeff Bonwick 
707b24ab676SJeff Bonwick /* Log state */
708b24ab676SJeff Bonwick typedef enum spa_log_state {
709b24ab676SJeff Bonwick 	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
710b24ab676SJeff Bonwick 	SPA_LOG_MISSING,	/* missing log(s) */
711b24ab676SJeff Bonwick 	SPA_LOG_CLEAR,		/* clear the log(s) */
712b24ab676SJeff Bonwick 	SPA_LOG_GOOD,		/* log(s) are good */
713b24ab676SJeff Bonwick } spa_log_state_t;
714b24ab676SJeff Bonwick 
715b24ab676SJeff Bonwick extern spa_log_state_t spa_get_log_state(spa_t *spa);
716b24ab676SJeff Bonwick extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
7171195e687SMark J Musante extern int spa_offline_log(spa_t *spa);
718b24ab676SJeff Bonwick 
719b24ab676SJeff Bonwick /* Log claim callback */
720b24ab676SJeff Bonwick extern void spa_claim_notify(zio_t *zio);
721b24ab676SJeff Bonwick 
722fa9e4066Sahrens /* Accessor functions */
72388b7b0f2SMatthew Ahrens extern boolean_t spa_shutting_down(spa_t *spa);
724fa9e4066Sahrens extern struct dsl_pool *spa_get_dsl(spa_t *spa);
725ad135b5dSChristopher Siden extern boolean_t spa_is_initializing(spa_t *spa);
726fa9e4066Sahrens extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
727fa9e4066Sahrens extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
728fa9e4066Sahrens extern void spa_altroot(spa_t *, char *, size_t);
729fa9e4066Sahrens extern int spa_sync_pass(spa_t *spa);
730fa9e4066Sahrens extern char *spa_name(spa_t *spa);
731fa9e4066Sahrens extern uint64_t spa_guid(spa_t *spa);
732e9103aaeSGarrett D'Amore extern uint64_t spa_load_guid(spa_t *spa);
733fa9e4066Sahrens extern uint64_t spa_last_synced_txg(spa_t *spa);
734fa9e4066Sahrens extern uint64_t spa_first_txg(spa_t *spa);
735b24ab676SJeff Bonwick extern uint64_t spa_syncing_txg(spa_t *spa);
73699653d4eSeschrock extern uint64_t spa_version(spa_t *spa);
73788b7b0f2SMatthew Ahrens extern pool_state_t spa_state(spa_t *spa);
738b16da2e2SGeorge Wilson extern spa_load_state_t spa_load_state(spa_t *spa);
739fa9e4066Sahrens extern uint64_t spa_freeze_txg(spa_t *spa);
740fa9e4066Sahrens extern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
741485bbbf5SGeorge Wilson extern uint64_t spa_get_dspace(spa_t *spa);
742485bbbf5SGeorge Wilson extern void spa_update_dspace(spa_t *spa);
74344cd46caSbillm extern uint64_t spa_version(spa_t *spa);
744b24ab676SJeff Bonwick extern boolean_t spa_deflate(spa_t *spa);
745b24ab676SJeff Bonwick extern metaslab_class_t *spa_normal_class(spa_t *spa);
746b24ab676SJeff Bonwick extern metaslab_class_t *spa_log_class(spa_t *spa);
74744cd46caSbillm extern int spa_max_replication(spa_t *spa);
7483f9d6ad7SLin Ling extern int spa_prev_software_version(spa_t *spa);
749fa9e4066Sahrens extern int spa_busy(void);
7500a4e9518Sgw extern uint8_t spa_get_failmode(spa_t *spa);
751e14bb325SJeff Bonwick extern boolean_t spa_suspended(spa_t *spa);
752b24ab676SJeff Bonwick extern uint64_t spa_bootfs(spa_t *spa);
753b24ab676SJeff Bonwick extern uint64_t spa_delegation(spa_t *spa);
754b24ab676SJeff Bonwick extern objset_t *spa_meta_objset(spa_t *spa);
755283b8460SGeorge.Wilson extern uint64_t spa_deadman_synctime(spa_t *spa);
756fa9e4066Sahrens 
757fa9e4066Sahrens /* Miscellaneous support routines */
75843466aaeSMax Grossman extern void spa_activate_mos_feature(spa_t *spa, const char *feature,
75943466aaeSMax Grossman     dmu_tx_t *tx);
760ad135b5dSChristopher Siden extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
761fa9e4066Sahrens extern int spa_rename(const char *oldname, const char *newname);
762f9af39baSGeorge Wilson extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
763fa9e4066Sahrens extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
764fa9e4066Sahrens extern char *spa_strdup(const char *);
765fa9e4066Sahrens extern void spa_strfree(char *);
766fa9e4066Sahrens extern uint64_t spa_get_random(uint64_t range);
7671195e687SMark J Musante extern uint64_t spa_generate_guid(spa_t *spa);
76843466aaeSMax Grossman extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
769fa9e4066Sahrens extern void spa_freeze(spa_t *spa);
770e9103aaeSGarrett D'Amore extern int spa_change_guid(spa_t *spa);
771990b4856Slling extern void spa_upgrade(spa_t *spa, uint64_t version);
772fa9e4066Sahrens extern void spa_evict_all(void);
773c5904d13Seschrock extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
774c5904d13Seschrock     boolean_t l2cache);
77599653d4eSeschrock extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
776b24ab676SJeff Bonwick extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
777b24ab676SJeff Bonwick extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
778b24ab676SJeff Bonwick extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
7796ce0521aSperrin extern boolean_t spa_has_slogs(spa_t *spa);
780bf82a41bSeschrock extern boolean_t spa_is_root(spa_t *spa);
7818ad4d6ddSJeff Bonwick extern boolean_t spa_writeable(spa_t *spa);
782468c413aSTim Haley 
7838ad4d6ddSJeff Bonwick extern int spa_mode(spa_t *spa);
784ca45db41SChris Kirby extern uint64_t strtonum(const char *str, char **nptr);
785ea8dc4b6Seschrock 
786ecd6cf80Smarks extern char *spa_his_ievent_table[];
787ecd6cf80Smarks 
78806eeb2adSek extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
78906eeb2adSek extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
79006eeb2adSek     char *his_buf);
7914445fffbSMatthew Ahrens extern int spa_history_log(spa_t *spa, const char *his_buf);
7924445fffbSMatthew Ahrens extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
7934445fffbSMatthew Ahrens extern void spa_history_log_version(spa_t *spa, const char *operation);
7944445fffbSMatthew Ahrens extern void spa_history_log_internal(spa_t *spa, const char *operation,
7954445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...);
7964445fffbSMatthew Ahrens extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
7974445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...);
7984445fffbSMatthew Ahrens extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
7994445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...);
80006eeb2adSek 
801ea8dc4b6Seschrock /* error handling */
802ea8dc4b6Seschrock struct zbookmark;
803b24ab676SJeff Bonwick extern void spa_log_error(spa_t *spa, zio_t *zio);
804ea8dc4b6Seschrock extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
805b24ab676SJeff Bonwick     zio_t *zio, uint64_t stateoroffset, uint64_t length);
8063d7072f8Seschrock extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
807069f55e2SEric Schrock extern void zfs_post_state_change(spa_t *spa, vdev_t *vd);
8083d7072f8Seschrock extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
809ea8dc4b6Seschrock extern uint64_t spa_get_errlog_size(spa_t *spa);
810ea8dc4b6Seschrock extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
811ea8dc4b6Seschrock extern void spa_errlog_rotate(spa_t *spa);
812ea8dc4b6Seschrock extern void spa_errlog_drain(spa_t *spa);
813ea8dc4b6Seschrock extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
814ea8dc4b6Seschrock extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
815fa9e4066Sahrens 
81687db74c1Sek /* vdev cache */
81787db74c1Sek extern void vdev_cache_stat_init(void);
81887db74c1Sek extern void vdev_cache_stat_fini(void);
81987db74c1Sek 
820fa9e4066Sahrens /* Initialization and termination */
821fa9e4066Sahrens extern void spa_init(int flags);
822fa9e4066Sahrens extern void spa_fini(void);
823e7cbe64fSgw extern void spa_boot_init();
824fa9e4066Sahrens 
825b1b8ab34Slling /* properties */
826990b4856Slling extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
827990b4856Slling extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
828990b4856Slling extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
829379c004dSEric Schrock extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
830b1b8ab34Slling 
8313d7072f8Seschrock /* asynchronous event notification */
8323d7072f8Seschrock extern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name);
8333d7072f8Seschrock 
834fa9e4066Sahrens #ifdef ZFS_DEBUG
835c0a81264Sek #define	dprintf_bp(bp, fmt, ...) do {				\
83643466aaeSMax Grossman 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
837c0a81264Sek 	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
83843466aaeSMax Grossman 	snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));	\
839c0a81264Sek 	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
840c0a81264Sek 	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
841fa9e4066Sahrens 	} \
842fa9e4066Sahrens _NOTE(CONSTCOND) } while (0)
843fa9e4066Sahrens #else
844fa9e4066Sahrens #define	dprintf_bp(bp, fmt, ...)
845fa9e4066Sahrens #endif
846fa9e4066Sahrens 
84709c9d376SGeorge Wilson extern boolean_t spa_debug_enabled(spa_t *spa);
84809c9d376SGeorge Wilson #define	spa_dbgmsg(spa, ...)			\
84909c9d376SGeorge Wilson {						\
85009c9d376SGeorge Wilson 	if (spa_debug_enabled(spa))		\
85109c9d376SGeorge Wilson 		zfs_dbgmsg(__VA_ARGS__);	\
85209c9d376SGeorge Wilson }
85309c9d376SGeorge Wilson 
8548ad4d6ddSJeff Bonwick extern int spa_mode_global;			/* mode, e.g. FREAD | FWRITE */
855fa9e4066Sahrens 
856fa9e4066Sahrens #ifdef	__cplusplus
857fa9e4066Sahrens }
858fa9e4066Sahrens #endif
859fa9e4066Sahrens 
860fa9e4066Sahrens #endif	/* _SYS_SPA_H */
861