xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/spa.h (revision f78cdc34af236a6199dd9e21376f4a46348c0d56)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26  * Copyright 2013 Saso Kiselkov. All rights reserved.
27  * Copyright (c) 2014 Integros [integros.com]
28  * Copyright 2017 Joyent, Inc.
29  * Copyright (c) 2017 Datto Inc.
30  */
31 
32 #ifndef _SYS_SPA_H
33 #define	_SYS_SPA_H
34 
35 #include <sys/avl.h>
36 #include <sys/zfs_context.h>
37 #include <sys/nvpair.h>
38 #include <sys/sysevent.h>
39 #include <sys/sysmacros.h>
40 #include <sys/types.h>
41 #include <sys/fs/zfs.h>
42 #include <sys/dmu.h>
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 /*
49  * Forward references that lots of things need.
50  */
51 typedef struct spa spa_t;
52 typedef struct vdev vdev_t;
53 typedef struct metaslab metaslab_t;
54 typedef struct metaslab_group metaslab_group_t;
55 typedef struct metaslab_class metaslab_class_t;
56 typedef struct zio zio_t;
57 typedef struct zilog zilog_t;
58 typedef struct spa_aux_vdev spa_aux_vdev_t;
59 typedef struct ddt ddt_t;
60 typedef struct ddt_entry ddt_entry_t;
61 struct dsl_pool;
62 struct dsl_dataset;
63 
64 /*
65  * General-purpose 32-bit and 64-bit bitfield encodings.
66  */
67 #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
68 #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
69 #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
70 #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
71 
72 #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
73 #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
74 
75 #define	BF32_SET(x, low, len, val) do { \
76 	ASSERT3U(val, <, 1U << (len)); \
77 	ASSERT3U(low + len, <=, 32); \
78 	(x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
79 _NOTE(CONSTCOND) } while (0)
80 
81 #define	BF64_SET(x, low, len, val) do { \
82 	ASSERT3U(val, <, 1ULL << (len)); \
83 	ASSERT3U(low + len, <=, 64); \
84 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
85 _NOTE(CONSTCOND) } while (0)
86 
87 #define	BF32_GET_SB(x, low, len, shift, bias)	\
88 	((BF32_GET(x, low, len) + (bias)) << (shift))
89 #define	BF64_GET_SB(x, low, len, shift, bias)	\
90 	((BF64_GET(x, low, len) + (bias)) << (shift))
91 
92 #define	BF32_SET_SB(x, low, len, shift, bias, val) do { \
93 	ASSERT(IS_P2ALIGNED(val, 1U << shift)); \
94 	ASSERT3S((val) >> (shift), >=, bias); \
95 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
96 _NOTE(CONSTCOND) } while (0)
97 #define	BF64_SET_SB(x, low, len, shift, bias, val) do { \
98 	ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \
99 	ASSERT3S((val) >> (shift), >=, bias); \
100 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
101 _NOTE(CONSTCOND) } while (0)
102 
103 /*
104  * We currently support block sizes from 512 bytes to 16MB.
105  * The benefits of larger blocks, and thus larger IO, need to be weighed
106  * against the cost of COWing a giant block to modify one byte, and the
107  * large latency of reading or writing a large block.
108  *
109  * Note that although blocks up to 16MB are supported, the recordsize
110  * property can not be set larger than zfs_max_recordsize (default 1MB).
111  * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
112  *
113  * Note that although the LSIZE field of the blkptr_t can store sizes up
114  * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
115  * 32MB - 512 bytes.  Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
116  */
117 #define	SPA_MINBLOCKSHIFT	9
118 #define	SPA_OLD_MAXBLOCKSHIFT	17
119 #define	SPA_MAXBLOCKSHIFT	24
120 #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
121 #define	SPA_OLD_MAXBLOCKSIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)
122 #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
123 
124 /*
125  * Size of block to hold the configuration data (a packed nvlist)
126  */
127 #define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
128 
129 /*
130  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
131  * The ASIZE encoding should be at least 64 times larger (6 more bits)
132  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
133  * overhead, three DVAs per bp, plus one more bit in case we do anything
134  * else that expands the ASIZE.
135  */
136 #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
137 #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
138 #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
139 
140 #define	SPA_COMPRESSBITS	7
141 #define	SPA_VDEVBITS		24
142 
143 /*
144  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
145  * The members of the dva_t should be considered opaque outside the SPA.
146  */
147 typedef struct dva {
148 	uint64_t	dva_word[2];
149 } dva_t;
150 
151 /*
152  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
153  */
154 typedef struct zio_cksum {
155 	uint64_t	zc_word[4];
156 } zio_cksum_t;
157 
158 /*
159  * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
160  * secret and is suitable for use in MAC algorithms as the key.
161  */
162 typedef struct zio_cksum_salt {
163 	uint8_t		zcs_bytes[32];
164 } zio_cksum_salt_t;
165 
166 /*
167  * Each block is described by its DVAs, time of birth, checksum, etc.
168  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
169  *
170  *	64	56	48	40	32	24	16	8	0
171  *	+-------+-------+-------+-------+-------+-------+-------+-------+
172  * 0	|  pad  |	  vdev1         | GRID  |	  ASIZE		|
173  *	+-------+-------+-------+-------+-------+-------+-------+-------+
174  * 1	|G|			 offset1				|
175  *	+-------+-------+-------+-------+-------+-------+-------+-------+
176  * 2	|  pad  |	  vdev2         | GRID  |	  ASIZE		|
177  *	+-------+-------+-------+-------+-------+-------+-------+-------+
178  * 3	|G|			 offset2				|
179  *	+-------+-------+-------+-------+-------+-------+-------+-------+
180  * 4	|  pad  |	  vdev3         | GRID  |	  ASIZE		|
181  *	+-------+-------+-------+-------+-------+-------+-------+-------+
182  * 5	|G|			 offset3				|
183  *	+-------+-------+-------+-------+-------+-------+-------+-------+
184  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
185  *	+-------+-------+-------+-------+-------+-------+-------+-------+
186  * 7	|			padding					|
187  *	+-------+-------+-------+-------+-------+-------+-------+-------+
188  * 8	|			padding					|
189  *	+-------+-------+-------+-------+-------+-------+-------+-------+
190  * 9	|			physical birth txg			|
191  *	+-------+-------+-------+-------+-------+-------+-------+-------+
192  * a	|			logical birth txg			|
193  *	+-------+-------+-------+-------+-------+-------+-------+-------+
194  * b	|			fill count				|
195  *	+-------+-------+-------+-------+-------+-------+-------+-------+
196  * c	|			checksum[0]				|
197  *	+-------+-------+-------+-------+-------+-------+-------+-------+
198  * d	|			checksum[1]				|
199  *	+-------+-------+-------+-------+-------+-------+-------+-------+
200  * e	|			checksum[2]				|
201  *	+-------+-------+-------+-------+-------+-------+-------+-------+
202  * f	|			checksum[3]				|
203  *	+-------+-------+-------+-------+-------+-------+-------+-------+
204  *
205  * Legend:
206  *
207  * vdev		virtual device ID
208  * offset	offset into virtual device
209  * LSIZE	logical size
210  * PSIZE	physical size (after compression)
211  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
212  * GRID		RAID-Z layout information (reserved for future use)
213  * cksum	checksum function
214  * comp		compression function
215  * G		gang block indicator
216  * B		byteorder (endianness)
217  * D		dedup
218  * X		encryption (on version 30, which is not supported)
219  * E		blkptr_t contains embedded data (see below)
220  * lvl		level of indirection
221  * type		DMU object type
222  * phys birth	txg when dva[0] was written; zero if same as logical birth txg
223  *              note that typically all the dva's would be written in this
224  *              txg, but they could be different if they were moved by
225  *              device removal.
226  * log. birth	transaction group in which the block was logically born
227  * fill count	number of non-zero blocks under this bp
228  * checksum[4]	256-bit checksum of the data this bp describes
229  */
230 
231 /*
232  * "Embedded" blkptr_t's don't actually point to a block, instead they
233  * have a data payload embedded in the blkptr_t itself.  See the comment
234  * in blkptr.c for more details.
235  *
236  * The blkptr_t is laid out as follows:
237  *
238  *	64	56	48	40	32	24	16	8	0
239  *	+-------+-------+-------+-------+-------+-------+-------+-------+
240  * 0	|      payload                                                  |
241  * 1	|      payload                                                  |
242  * 2	|      payload                                                  |
243  * 3	|      payload                                                  |
244  * 4	|      payload                                                  |
245  * 5	|      payload                                                  |
246  *	+-------+-------+-------+-------+-------+-------+-------+-------+
247  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
248  *	+-------+-------+-------+-------+-------+-------+-------+-------+
249  * 7	|      payload                                                  |
250  * 8	|      payload                                                  |
251  * 9	|      payload                                                  |
252  *	+-------+-------+-------+-------+-------+-------+-------+-------+
253  * a	|			logical birth txg			|
254  *	+-------+-------+-------+-------+-------+-------+-------+-------+
255  * b	|      payload                                                  |
256  * c	|      payload                                                  |
257  * d	|      payload                                                  |
258  * e	|      payload                                                  |
259  * f	|      payload                                                  |
260  *	+-------+-------+-------+-------+-------+-------+-------+-------+
261  *
262  * Legend:
263  *
264  * payload		contains the embedded data
265  * B (byteorder)	byteorder (endianness)
266  * D (dedup)		padding (set to zero)
267  * X			encryption (set to zero; see above)
268  * E (embedded)		set to one
269  * lvl			indirection level
270  * type			DMU object type
271  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
272  * comp			compression function of payload
273  * PSIZE		size of payload after compression, in bytes
274  * LSIZE		logical size of payload, in bytes
275  *			note that 25 bits is enough to store the largest
276  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
277  * log. birth		transaction group in which the block was logically born
278  *
279  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
280  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
281  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
282  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
283  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
284  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
285  * other macros, as they assert that they are only used on BP's of the correct
286  * "embedded-ness".
287  */
288 
289 #define	BPE_GET_ETYPE(bp)	\
290 	(ASSERT(BP_IS_EMBEDDED(bp)), \
291 	BF64_GET((bp)->blk_prop, 40, 8))
292 #define	BPE_SET_ETYPE(bp, t)	do { \
293 	ASSERT(BP_IS_EMBEDDED(bp)); \
294 	BF64_SET((bp)->blk_prop, 40, 8, t); \
295 _NOTE(CONSTCOND) } while (0)
296 
297 #define	BPE_GET_LSIZE(bp)	\
298 	(ASSERT(BP_IS_EMBEDDED(bp)), \
299 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
300 #define	BPE_SET_LSIZE(bp, x)	do { \
301 	ASSERT(BP_IS_EMBEDDED(bp)); \
302 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
303 _NOTE(CONSTCOND) } while (0)
304 
305 #define	BPE_GET_PSIZE(bp)	\
306 	(ASSERT(BP_IS_EMBEDDED(bp)), \
307 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
308 #define	BPE_SET_PSIZE(bp, x)	do { \
309 	ASSERT(BP_IS_EMBEDDED(bp)); \
310 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
311 _NOTE(CONSTCOND) } while (0)
312 
313 typedef enum bp_embedded_type {
314 	BP_EMBEDDED_TYPE_DATA,
315 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
316 	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
317 } bp_embedded_type_t;
318 
319 #define	BPE_NUM_WORDS 14
320 #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
321 #define	BPE_IS_PAYLOADWORD(bp, wp) \
322 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
323 
324 #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
325 #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
326 #define	SPA_SYNC_MIN_VDEVS 3		/* min vdevs to update during sync */
327 
328 /*
329  * A block is a hole when it has either 1) never been written to, or
330  * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
331  * without physically allocating disk space. Holes are represented in the
332  * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
333  * done through the BP_IS_HOLE macro. For holes, the logical size, level,
334  * DMU object type, and birth times are all also stored for holes that
335  * were written to at some point (i.e. were punched after having been filled).
336  */
337 typedef struct blkptr {
338 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
339 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
340 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
341 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
342 	uint64_t	blk_birth;	/* transaction group at birth	    */
343 	uint64_t	blk_fill;	/* fill count			    */
344 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
345 } blkptr_t;
346 
347 /*
348  * Macros to get and set fields in a bp or DVA.
349  */
350 #define	DVA_GET_ASIZE(dva)	\
351 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
352 #define	DVA_SET_ASIZE(dva, x)	\
353 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
354 	SPA_MINBLOCKSHIFT, 0, x)
355 
356 #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
357 #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
358 
359 #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, SPA_VDEVBITS)
360 #define	DVA_SET_VDEV(dva, x)	\
361 	BF64_SET((dva)->dva_word[0], 32, SPA_VDEVBITS, x)
362 
363 #define	DVA_GET_OFFSET(dva)	\
364 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
365 #define	DVA_SET_OFFSET(dva, x)	\
366 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
367 
368 #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
369 #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
370 
371 #define	BP_GET_LSIZE(bp)	\
372 	(BP_IS_EMBEDDED(bp) ?	\
373 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
374 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
375 #define	BP_SET_LSIZE(bp, x)	do { \
376 	ASSERT(!BP_IS_EMBEDDED(bp)); \
377 	BF64_SET_SB((bp)->blk_prop, \
378 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
379 _NOTE(CONSTCOND) } while (0)
380 
381 #define	BP_GET_PSIZE(bp)	\
382 	(BP_IS_EMBEDDED(bp) ? 0 : \
383 	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
384 #define	BP_SET_PSIZE(bp, x)	do { \
385 	ASSERT(!BP_IS_EMBEDDED(bp)); \
386 	BF64_SET_SB((bp)->blk_prop, \
387 	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
388 _NOTE(CONSTCOND) } while (0)
389 
390 #define	BP_GET_COMPRESS(bp)		\
391 	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
392 #define	BP_SET_COMPRESS(bp, x)		\
393 	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
394 
395 #define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
396 #define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
397 
398 #define	BP_GET_CHECKSUM(bp)		\
399 	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
400 	BF64_GET((bp)->blk_prop, 40, 8))
401 #define	BP_SET_CHECKSUM(bp, x)		do { \
402 	ASSERT(!BP_IS_EMBEDDED(bp)); \
403 	BF64_SET((bp)->blk_prop, 40, 8, x); \
404 _NOTE(CONSTCOND) } while (0)
405 
406 #define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
407 #define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
408 
409 #define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
410 #define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
411 
412 #define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
413 #define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
414 
415 #define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
416 #define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
417 
418 #define	BP_PHYSICAL_BIRTH(bp)		\
419 	(BP_IS_EMBEDDED(bp) ? 0 : \
420 	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
421 
422 #define	BP_SET_BIRTH(bp, logical, physical)	\
423 {						\
424 	ASSERT(!BP_IS_EMBEDDED(bp));		\
425 	(bp)->blk_birth = (logical);		\
426 	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
427 }
428 
429 #define	BP_GET_FILL(bp) (BP_IS_EMBEDDED(bp) ? 1 : (bp)->blk_fill)
430 
431 #define	BP_IS_METADATA(bp)	\
432 	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
433 
434 #define	BP_GET_ASIZE(bp)	\
435 	(BP_IS_EMBEDDED(bp) ? 0 : \
436 	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
437 	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
438 	DVA_GET_ASIZE(&(bp)->blk_dva[2]))
439 
440 #define	BP_GET_UCSIZE(bp)	\
441 	(BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
442 
443 #define	BP_GET_NDVAS(bp)	\
444 	(BP_IS_EMBEDDED(bp) ? 0 : \
445 	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
446 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
447 	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
448 
449 #define	BP_COUNT_GANG(bp)	\
450 	(BP_IS_EMBEDDED(bp) ? 0 : \
451 	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
452 	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
453 	DVA_GET_GANG(&(bp)->blk_dva[2])))
454 
455 #define	DVA_EQUAL(dva1, dva2)	\
456 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
457 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
458 
459 #define	BP_EQUAL(bp1, bp2)	\
460 	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
461 	(bp1)->blk_birth == (bp2)->blk_birth &&			\
462 	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
463 	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
464 	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
465 
466 #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
467 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
468 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
469 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
470 	((zc1).zc_word[3] - (zc2).zc_word[3])))
471 
472 #define	ZIO_CHECKSUM_IS_ZERO(zc) \
473 	(0 == ((zc)->zc_word[0] | (zc)->zc_word[1] | \
474 	(zc)->zc_word[2] | (zc)->zc_word[3]))
475 
476 #define	ZIO_CHECKSUM_BSWAP(zcp)					\
477 {								\
478 	(zcp)->zc_word[0] = BSWAP_64((zcp)->zc_word[0]);	\
479 	(zcp)->zc_word[1] = BSWAP_64((zcp)->zc_word[1]);	\
480 	(zcp)->zc_word[2] = BSWAP_64((zcp)->zc_word[2]);	\
481 	(zcp)->zc_word[3] = BSWAP_64((zcp)->zc_word[3]);	\
482 }
483 
484 
485 #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
486 
487 #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
488 {						\
489 	(zcp)->zc_word[0] = w0;			\
490 	(zcp)->zc_word[1] = w1;			\
491 	(zcp)->zc_word[2] = w2;			\
492 	(zcp)->zc_word[3] = w3;			\
493 }
494 
495 #define	BP_IDENTITY(bp)		(ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
496 #define	BP_IS_GANG(bp)		\
497 	(BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
498 #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&	\
499 				(dva)->dva_word[1] == 0ULL)
500 #define	BP_IS_HOLE(bp) \
501 	(!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
502 
503 /* BP_IS_RAIDZ(bp) assumes no block compression */
504 #define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
505 				BP_GET_PSIZE(bp))
506 
507 #define	BP_ZERO(bp)				\
508 {						\
509 	(bp)->blk_dva[0].dva_word[0] = 0;	\
510 	(bp)->blk_dva[0].dva_word[1] = 0;	\
511 	(bp)->blk_dva[1].dva_word[0] = 0;	\
512 	(bp)->blk_dva[1].dva_word[1] = 0;	\
513 	(bp)->blk_dva[2].dva_word[0] = 0;	\
514 	(bp)->blk_dva[2].dva_word[1] = 0;	\
515 	(bp)->blk_prop = 0;			\
516 	(bp)->blk_pad[0] = 0;			\
517 	(bp)->blk_pad[1] = 0;			\
518 	(bp)->blk_phys_birth = 0;		\
519 	(bp)->blk_birth = 0;			\
520 	(bp)->blk_fill = 0;			\
521 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
522 }
523 
524 #ifdef _BIG_ENDIAN
525 #define	ZFS_HOST_BYTEORDER	(0ULL)
526 #else
527 #define	ZFS_HOST_BYTEORDER	(1ULL)
528 #endif
529 
530 #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
531 
532 #define	BP_SPRINTF_LEN	320
533 
534 /*
535  * This macro allows code sharing between zfs, libzpool, and mdb.
536  * 'func' is either snprintf() or mdb_snprintf().
537  * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
538  */
539 #define	SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
540 {									\
541 	static const char *copyname[] =					\
542 	    { "zero", "single", "double", "triple" };			\
543 	int len = 0;							\
544 	int copies = 0;							\
545 									\
546 	if (bp == NULL) {						\
547 		len += func(buf + len, size - len, "<NULL>");		\
548 	} else if (BP_IS_HOLE(bp)) {					\
549 		len += func(buf + len, size - len,			\
550 		    "HOLE [L%llu %s] "					\
551 		    "size=%llxL birth=%lluL",				\
552 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
553 		    type,						\
554 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
555 		    (u_longlong_t)bp->blk_birth);			\
556 	} else if (BP_IS_EMBEDDED(bp)) {				\
557 		len = func(buf + len, size - len,			\
558 		    "EMBEDDED [L%llu %s] et=%u %s "			\
559 		    "size=%llxL/%llxP birth=%lluL",			\
560 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
561 		    type,						\
562 		    (int)BPE_GET_ETYPE(bp),				\
563 		    compress,						\
564 		    (u_longlong_t)BPE_GET_LSIZE(bp),			\
565 		    (u_longlong_t)BPE_GET_PSIZE(bp),			\
566 		    (u_longlong_t)bp->blk_birth);			\
567 	} else {							\
568 		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
569 			const dva_t *dva = &bp->blk_dva[d];		\
570 			if (DVA_IS_VALID(dva))				\
571 				copies++;				\
572 			len += func(buf + len, size - len,		\
573 			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
574 			    (u_longlong_t)DVA_GET_VDEV(dva),		\
575 			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
576 			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
577 			    ws);					\
578 		}							\
579 		if (BP_IS_GANG(bp) &&					\
580 		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
581 		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
582 			copies--;					\
583 		len += func(buf + len, size - len,			\
584 		    "[L%llu %s] %s %s %s %s %s %s%c"			\
585 		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
586 		    "cksum=%llx:%llx:%llx:%llx",			\
587 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
588 		    type,						\
589 		    checksum,						\
590 		    compress,						\
591 		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
592 		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
593 		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
594 		    copyname[copies],					\
595 		    ws,							\
596 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
597 		    (u_longlong_t)BP_GET_PSIZE(bp),			\
598 		    (u_longlong_t)bp->blk_birth,			\
599 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp),		\
600 		    (u_longlong_t)BP_GET_FILL(bp),			\
601 		    ws,							\
602 		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
603 		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
604 		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
605 		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
606 	}								\
607 	ASSERT(len < size);						\
608 }
609 
610 #define	BP_GET_BUFC_TYPE(bp)						\
611 	(BP_IS_METADATA(bp) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
612 
613 typedef enum spa_import_type {
614 	SPA_IMPORT_EXISTING,
615 	SPA_IMPORT_ASSEMBLE
616 } spa_import_type_t;
617 
618 /* state manipulation functions */
619 extern int spa_open(const char *pool, spa_t **, void *tag);
620 extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
621     nvlist_t *policy, nvlist_t **config);
622 extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
623     size_t buflen);
624 extern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
625     nvlist_t *zplprops);
626 extern int spa_import_rootpool(char *devpath, char *devid);
627 extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props,
628     uint64_t flags);
629 extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
630 extern int spa_destroy(char *pool);
631 extern int spa_checkpoint(const char *pool);
632 extern int spa_checkpoint_discard(const char *pool);
633 extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
634     boolean_t hardforce);
635 extern int spa_reset(char *pool);
636 extern void spa_async_request(spa_t *spa, int flag);
637 extern void spa_async_unrequest(spa_t *spa, int flag);
638 extern void spa_async_suspend(spa_t *spa);
639 extern void spa_async_resume(spa_t *spa);
640 extern spa_t *spa_inject_addref(char *pool);
641 extern void spa_inject_delref(spa_t *spa);
642 extern void spa_scan_stat_init(spa_t *spa);
643 extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
644 
645 #define	SPA_ASYNC_CONFIG_UPDATE	0x01
646 #define	SPA_ASYNC_REMOVE	0x02
647 #define	SPA_ASYNC_PROBE		0x04
648 #define	SPA_ASYNC_RESILVER_DONE	0x08
649 #define	SPA_ASYNC_RESILVER	0x10
650 #define	SPA_ASYNC_AUTOEXPAND	0x20
651 #define	SPA_ASYNC_REMOVE_DONE	0x40
652 #define	SPA_ASYNC_REMOVE_STOP	0x80
653 
654 /*
655  * Controls the behavior of spa_vdev_remove().
656  */
657 #define	SPA_REMOVE_UNSPARE	0x01
658 #define	SPA_REMOVE_DONE		0x02
659 
660 /* device manipulation */
661 extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
662 extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
663     int replacing);
664 extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
665     int replace_done);
666 extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
667 extern boolean_t spa_vdev_remove_active(spa_t *spa);
668 extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
669 extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
670 extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
671     nvlist_t *props, boolean_t exp);
672 
673 /* spare state (which is global across all pools) */
674 extern void spa_spare_add(vdev_t *vd);
675 extern void spa_spare_remove(vdev_t *vd);
676 extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
677 extern void spa_spare_activate(vdev_t *vd);
678 
679 /* L2ARC state (which is global across all pools) */
680 extern void spa_l2cache_add(vdev_t *vd);
681 extern void spa_l2cache_remove(vdev_t *vd);
682 extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
683 extern void spa_l2cache_activate(vdev_t *vd);
684 extern void spa_l2cache_drop(spa_t *spa);
685 
686 /* scanning */
687 extern int spa_scan(spa_t *spa, pool_scan_func_t func);
688 extern int spa_scan_stop(spa_t *spa);
689 extern int spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t flag);
690 
691 /* spa syncing */
692 extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
693 extern void spa_sync_allpools(void);
694 
695 /* spa namespace global mutex */
696 extern kmutex_t spa_namespace_lock;
697 
698 /*
699  * SPA configuration functions in spa_config.c
700  */
701 
702 #define	SPA_CONFIG_UPDATE_POOL	0
703 #define	SPA_CONFIG_UPDATE_VDEVS	1
704 
705 extern void spa_write_cachefile(spa_t *, boolean_t, boolean_t);
706 extern void spa_config_load(void);
707 extern nvlist_t *spa_all_configs(uint64_t *);
708 extern void spa_config_set(spa_t *spa, nvlist_t *config);
709 extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
710     int getstats);
711 extern void spa_config_update(spa_t *spa, int what);
712 
713 /*
714  * Miscellaneous SPA routines in spa_misc.c
715  */
716 
717 /* Namespace manipulation */
718 extern spa_t *spa_lookup(const char *name);
719 extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
720 extern void spa_remove(spa_t *spa);
721 extern spa_t *spa_next(spa_t *prev);
722 
723 /* Refcount functions */
724 extern void spa_open_ref(spa_t *spa, void *tag);
725 extern void spa_close(spa_t *spa, void *tag);
726 extern void spa_async_close(spa_t *spa, void *tag);
727 extern boolean_t spa_refcount_zero(spa_t *spa);
728 
729 #define	SCL_NONE	0x00
730 #define	SCL_CONFIG	0x01
731 #define	SCL_STATE	0x02
732 #define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
733 #define	SCL_ALLOC	0x08
734 #define	SCL_ZIO		0x10
735 #define	SCL_FREE	0x20
736 #define	SCL_VDEV	0x40
737 #define	SCL_LOCKS	7
738 #define	SCL_ALL		((1 << SCL_LOCKS) - 1)
739 #define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
740 
741 /* Pool configuration locks */
742 extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
743 extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
744 extern void spa_config_exit(spa_t *spa, int locks, void *tag);
745 extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
746 
747 /* Pool vdev add/remove lock */
748 extern uint64_t spa_vdev_enter(spa_t *spa);
749 extern uint64_t spa_vdev_config_enter(spa_t *spa);
750 extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
751     int error, char *tag);
752 extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
753 
754 /* Pool vdev state change lock */
755 extern void spa_vdev_state_enter(spa_t *spa, int oplock);
756 extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
757 
758 /* Log state */
759 typedef enum spa_log_state {
760 	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
761 	SPA_LOG_MISSING,	/* missing log(s) */
762 	SPA_LOG_CLEAR,		/* clear the log(s) */
763 	SPA_LOG_GOOD,		/* log(s) are good */
764 } spa_log_state_t;
765 
766 extern spa_log_state_t spa_get_log_state(spa_t *spa);
767 extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
768 extern int spa_reset_logs(spa_t *spa);
769 
770 /* Log claim callback */
771 extern void spa_claim_notify(zio_t *zio);
772 
773 /* Accessor functions */
774 extern boolean_t spa_shutting_down(spa_t *spa);
775 extern struct dsl_pool *spa_get_dsl(spa_t *spa);
776 extern boolean_t spa_is_initializing(spa_t *spa);
777 extern boolean_t spa_indirect_vdevs_loaded(spa_t *spa);
778 extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
779 extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
780 extern void spa_altroot(spa_t *, char *, size_t);
781 extern int spa_sync_pass(spa_t *spa);
782 extern char *spa_name(spa_t *spa);
783 extern uint64_t spa_guid(spa_t *spa);
784 extern uint64_t spa_load_guid(spa_t *spa);
785 extern uint64_t spa_last_synced_txg(spa_t *spa);
786 extern uint64_t spa_first_txg(spa_t *spa);
787 extern uint64_t spa_syncing_txg(spa_t *spa);
788 extern uint64_t spa_final_dirty_txg(spa_t *spa);
789 extern uint64_t spa_version(spa_t *spa);
790 extern pool_state_t spa_state(spa_t *spa);
791 extern spa_load_state_t spa_load_state(spa_t *spa);
792 extern uint64_t spa_freeze_txg(spa_t *spa);
793 extern uint64_t spa_get_worst_case_asize(spa_t *spa, uint64_t lsize);
794 extern uint64_t spa_get_dspace(spa_t *spa);
795 extern uint64_t spa_get_checkpoint_space(spa_t *spa);
796 extern uint64_t spa_get_slop_space(spa_t *spa);
797 extern void spa_update_dspace(spa_t *spa);
798 extern uint64_t spa_version(spa_t *spa);
799 extern boolean_t spa_deflate(spa_t *spa);
800 extern metaslab_class_t *spa_normal_class(spa_t *spa);
801 extern metaslab_class_t *spa_log_class(spa_t *spa);
802 extern void spa_evicting_os_register(spa_t *, objset_t *os);
803 extern void spa_evicting_os_deregister(spa_t *, objset_t *os);
804 extern void spa_evicting_os_wait(spa_t *spa);
805 extern int spa_max_replication(spa_t *spa);
806 extern int spa_prev_software_version(spa_t *spa);
807 extern int spa_busy(void);
808 extern uint8_t spa_get_failmode(spa_t *spa);
809 extern boolean_t spa_suspended(spa_t *spa);
810 extern uint64_t spa_bootfs(spa_t *spa);
811 extern uint64_t spa_delegation(spa_t *spa);
812 extern objset_t *spa_meta_objset(spa_t *spa);
813 extern uint64_t spa_deadman_synctime(spa_t *spa);
814 
815 /* Miscellaneous support routines */
816 extern void spa_load_failed(spa_t *spa, const char *fmt, ...);
817 extern void spa_load_note(spa_t *spa, const char *fmt, ...);
818 extern void spa_activate_mos_feature(spa_t *spa, const char *feature,
819     dmu_tx_t *tx);
820 extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
821 extern int spa_rename(const char *oldname, const char *newname);
822 extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
823 extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
824 extern char *spa_strdup(const char *);
825 extern void spa_strfree(char *);
826 extern uint64_t spa_get_random(uint64_t range);
827 extern uint64_t spa_generate_guid(spa_t *spa);
828 extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
829 extern void spa_freeze(spa_t *spa);
830 extern int spa_change_guid(spa_t *spa);
831 extern void spa_upgrade(spa_t *spa, uint64_t version);
832 extern void spa_evict_all(void);
833 extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
834     boolean_t l2cache);
835 extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
836 extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
837 extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
838 extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
839 extern boolean_t spa_has_slogs(spa_t *spa);
840 extern boolean_t spa_is_root(spa_t *spa);
841 extern boolean_t spa_writeable(spa_t *spa);
842 extern boolean_t spa_has_pending_synctask(spa_t *spa);
843 extern int spa_maxblocksize(spa_t *spa);
844 extern boolean_t spa_has_checkpoint(spa_t *spa);
845 extern boolean_t spa_importing_readonly_checkpoint(spa_t *spa);
846 extern boolean_t spa_suspend_async_destroy(spa_t *spa);
847 extern uint64_t spa_min_claim_txg(spa_t *spa);
848 extern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp);
849 extern boolean_t zfs_dva_valid(spa_t *spa, const dva_t *dva,
850     const blkptr_t *bp);
851 typedef void (*spa_remap_cb_t)(uint64_t vdev, uint64_t offset, uint64_t size,
852     void *arg);
853 extern boolean_t spa_remap_blkptr(spa_t *spa, blkptr_t *bp,
854     spa_remap_cb_t callback, void *arg);
855 extern uint64_t spa_get_last_removal_txg(spa_t *spa);
856 extern boolean_t spa_trust_config(spa_t *spa);
857 extern uint64_t spa_missing_tvds_allowed(spa_t *spa);
858 extern void spa_set_missing_tvds(spa_t *spa, uint64_t missing);
859 extern boolean_t spa_top_vdevs_spacemap_addressable(spa_t *spa);
860 
861 extern int spa_mode(spa_t *spa);
862 extern uint64_t zfs_strtonum(const char *str, char **nptr);
863 
864 extern char *spa_his_ievent_table[];
865 
866 extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
867 extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
868     char *his_buf);
869 extern int spa_history_log(spa_t *spa, const char *his_buf);
870 extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
871 extern void spa_history_log_version(spa_t *spa, const char *operation);
872 extern void spa_history_log_internal(spa_t *spa, const char *operation,
873     dmu_tx_t *tx, const char *fmt, ...);
874 extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
875     dmu_tx_t *tx, const char *fmt, ...);
876 extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
877     dmu_tx_t *tx, const char *fmt, ...);
878 
879 /* error handling */
880 struct zbookmark_phys;
881 extern void spa_log_error(spa_t *spa, zio_t *zio);
882 extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
883     zio_t *zio, uint64_t stateoroffset, uint64_t length);
884 extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
885 extern void zfs_post_state_change(spa_t *spa, vdev_t *vd);
886 extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
887 extern uint64_t spa_get_errlog_size(spa_t *spa);
888 extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
889 extern void spa_errlog_rotate(spa_t *spa);
890 extern void spa_errlog_drain(spa_t *spa);
891 extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
892 extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
893 
894 /* vdev cache */
895 extern void vdev_cache_stat_init(void);
896 extern void vdev_cache_stat_fini(void);
897 
898 /* Initialization and termination */
899 extern void spa_init(int flags);
900 extern void spa_fini(void);
901 extern void spa_boot_init(void);
902 
903 /* properties */
904 extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
905 extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
906 extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
907 extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
908 
909 /* asynchronous event notification */
910 extern void spa_event_notify(spa_t *spa, vdev_t *vdev, nvlist_t *hist_nvl,
911     const char *name);
912 extern sysevent_t *spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl,
913     const char *name);
914 extern void spa_event_post(sysevent_t *ev);
915 extern void spa_event_discard(sysevent_t *ev);
916 
917 #ifdef ZFS_DEBUG
918 #define	dprintf_bp(bp, fmt, ...) do {				\
919 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
920 	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
921 	snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));	\
922 	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
923 	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
924 	} \
925 _NOTE(CONSTCOND) } while (0)
926 #else
927 #define	dprintf_bp(bp, fmt, ...)
928 #endif
929 
930 extern boolean_t spa_debug_enabled(spa_t *spa);
931 #define	spa_dbgmsg(spa, ...)			\
932 {						\
933 	if (spa_debug_enabled(spa))		\
934 		zfs_dbgmsg(__VA_ARGS__);	\
935 }
936 
937 extern int spa_mode_global;			/* mode, e.g. FREAD | FWRITE */
938 
939 #ifdef	__cplusplus
940 }
941 #endif
942 
943 #endif	/* _SYS_SPA_H */
944