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