xref: /freebsd/sys/contrib/openzfs/include/sys/spa.h (revision 718519f4efc71096422fc71dab90b2a3369871ff)
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 https://opensource.org/licenses/CDDL-1.0.
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, 2024 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, Intel Corporation.
30  * Copyright (c) 2019, Allan Jude
31  * Copyright (c) 2019, Klara Inc.
32  * Copyright (c) 2019, Datto Inc.
33  */
34 
35 #ifndef _SYS_SPA_H
36 #define	_SYS_SPA_H
37 
38 #include <sys/zfs_context.h>
39 #include <sys/avl.h>
40 #include <sys/kstat.h>
41 #include <sys/nvpair.h>
42 #include <sys/types.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/spa_checksum.h>
45 #include <sys/dmu.h>
46 #include <sys/space_map.h>
47 #include <sys/bitops.h>
48 
49 #ifdef	__cplusplus
50 extern "C" {
51 #endif
52 
53 /*
54  * Forward references that lots of things need.
55  */
56 typedef struct brt_vdev brt_vdev_t;
57 typedef struct spa spa_t;
58 typedef struct vdev vdev_t;
59 typedef struct metaslab metaslab_t;
60 typedef struct metaslab_group metaslab_group_t;
61 typedef struct metaslab_class metaslab_class_t;
62 typedef struct zio zio_t;
63 typedef struct zilog zilog_t;
64 typedef struct spa_aux_vdev spa_aux_vdev_t;
65 typedef struct zbookmark_phys zbookmark_phys_t;
66 typedef struct zbookmark_err_phys zbookmark_err_phys_t;
67 
68 struct bpobj;
69 struct bplist;
70 struct dsl_pool;
71 struct dsl_dataset;
72 struct dsl_crypto_params;
73 
74 /*
75  * Alignment Shift (ashift) is an immutable, internal top-level vdev property
76  * which can only be set at vdev creation time. Physical writes are always done
77  * according to it, which makes 2^ashift the smallest possible IO on a vdev.
78  *
79  * We currently allow values ranging from 512 bytes (2^9 = 512) to 64 KiB
80  * (2^16 = 65,536).
81  */
82 #define	ASHIFT_MIN		9
83 #define	ASHIFT_MAX		16
84 
85 /*
86  * Size of block to hold the configuration data (a packed nvlist)
87  */
88 #define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
89 
90 /*
91  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
92  * The ASIZE encoding should be at least 64 times larger (6 more bits)
93  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
94  * overhead, three DVAs per bp, plus one more bit in case we do anything
95  * else that expands the ASIZE.
96  */
97 #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
98 #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
99 #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
100 
101 #define	SPA_COMPRESSBITS	7
102 #define	SPA_VDEVBITS		24
103 #define	SPA_COMPRESSMASK	((1U << SPA_COMPRESSBITS) - 1)
104 
105 /*
106  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
107  * The members of the dva_t should be considered opaque outside the SPA.
108  */
109 typedef struct dva {
110 	uint64_t	dva_word[2];
111 } dva_t;
112 
113 
114 /*
115  * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
116  * secret and is suitable for use in MAC algorithms as the key.
117  */
118 typedef struct zio_cksum_salt {
119 	uint8_t		zcs_bytes[32];
120 } zio_cksum_salt_t;
121 
122 /*
123  * Each block is described by its DVAs, time of birth, checksum, etc.
124  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
125  *
126  *	64	56	48	40	32	24	16	8	0
127  *	+-------+-------+-------+-------+-------+-------+-------+-------+
128  * 0	|  pad  |	  vdev1         | pad   |	  ASIZE		|
129  *	+-------+-------+-------+-------+-------+-------+-------+-------+
130  * 1	|G|			 offset1				|
131  *	+-------+-------+-------+-------+-------+-------+-------+-------+
132  * 2	|  pad  |	  vdev2         | pad   |	  ASIZE		|
133  *	+-------+-------+-------+-------+-------+-------+-------+-------+
134  * 3	|G|			 offset2				|
135  *	+-------+-------+-------+-------+-------+-------+-------+-------+
136  * 4	|  pad  |	  vdev3         | pad   |	  ASIZE		|
137  *	+-------+-------+-------+-------+-------+-------+-------+-------+
138  * 5	|G|			 offset3				|
139  *	+-------+-------+-------+-------+-------+-------+-------+-------+
140  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
141  *	+-------+-------+-------+-------+-------+-------+-------+-------+
142  * 7	|			padding					|
143  *	+-------+-------+-------+-------+-------+-------+-------+-------+
144  * 8	|			padding					|
145  *	+-------+-------+-------+-------+-------+-------+-------+-------+
146  * 9	|			physical birth txg			|
147  *	+-------+-------+-------+-------+-------+-------+-------+-------+
148  * a	|			logical birth txg			|
149  *	+-------+-------+-------+-------+-------+-------+-------+-------+
150  * b	|			fill count				|
151  *	+-------+-------+-------+-------+-------+-------+-------+-------+
152  * c	|			checksum[0]				|
153  *	+-------+-------+-------+-------+-------+-------+-------+-------+
154  * d	|			checksum[1]				|
155  *	+-------+-------+-------+-------+-------+-------+-------+-------+
156  * e	|			checksum[2]				|
157  *	+-------+-------+-------+-------+-------+-------+-------+-------+
158  * f	|			checksum[3]				|
159  *	+-------+-------+-------+-------+-------+-------+-------+-------+
160  *
161  * Legend:
162  *
163  * vdev		virtual device ID
164  * offset	offset into virtual device
165  * LSIZE	logical size
166  * PSIZE	physical size (after compression)
167  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
168  * cksum	checksum function
169  * comp		compression function
170  * G		gang block indicator
171  * B		byteorder (endianness)
172  * D		dedup
173  * X		encryption
174  * E		blkptr_t contains embedded data (see below)
175  * lvl		level of indirection
176  * type		DMU object type
177  * phys birth	txg when dva[0] was written; zero if same as logical birth txg
178  *              note that typically all the dva's would be written in this
179  *              txg, but they could be different if they were moved by
180  *              device removal.
181  * log. birth	transaction group in which the block was logically born
182  * fill count	number of non-zero blocks under this bp
183  * checksum[4]	256-bit checksum of the data this bp describes
184  */
185 
186 /*
187  * The blkptr_t's of encrypted blocks also need to store the encryption
188  * parameters so that the block can be decrypted. This layout is as follows:
189  *
190  *	64	56	48	40	32	24	16	8	0
191  *	+-------+-------+-------+-------+-------+-------+-------+-------+
192  * 0	|		vdev1		| pad   |	  ASIZE		|
193  *	+-------+-------+-------+-------+-------+-------+-------+-------+
194  * 1	|G|			 offset1				|
195  *	+-------+-------+-------+-------+-------+-------+-------+-------+
196  * 2	|		vdev2		| pad   |	  ASIZE		|
197  *	+-------+-------+-------+-------+-------+-------+-------+-------+
198  * 3	|G|			 offset2				|
199  *	+-------+-------+-------+-------+-------+-------+-------+-------+
200  * 4	|			salt					|
201  *	+-------+-------+-------+-------+-------+-------+-------+-------+
202  * 5	|			IV1					|
203  *	+-------+-------+-------+-------+-------+-------+-------+-------+
204  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
205  *	+-------+-------+-------+-------+-------+-------+-------+-------+
206  * 7	|			padding					|
207  *	+-------+-------+-------+-------+-------+-------+-------+-------+
208  * 8	|			padding					|
209  *	+-------+-------+-------+-------+-------+-------+-------+-------+
210  * 9	|			physical birth txg			|
211  *	+-------+-------+-------+-------+-------+-------+-------+-------+
212  * a	|			logical birth txg			|
213  *	+-------+-------+-------+-------+-------+-------+-------+-------+
214  * b	|		IV2		|	    fill count		|
215  *	+-------+-------+-------+-------+-------+-------+-------+-------+
216  * c	|			checksum[0]				|
217  *	+-------+-------+-------+-------+-------+-------+-------+-------+
218  * d	|			checksum[1]				|
219  *	+-------+-------+-------+-------+-------+-------+-------+-------+
220  * e	|			MAC[0]					|
221  *	+-------+-------+-------+-------+-------+-------+-------+-------+
222  * f	|			MAC[1]					|
223  *	+-------+-------+-------+-------+-------+-------+-------+-------+
224  *
225  * Legend:
226  *
227  * salt		Salt for generating encryption keys
228  * IV1		First 64 bits of encryption IV
229  * X		Block requires encryption handling (set to 1)
230  * E		blkptr_t contains embedded data (set to 0, see below)
231  * fill count	number of non-zero blocks under this bp (truncated to 32 bits)
232  * IV2		Last 32 bits of encryption IV
233  * checksum[2]	128-bit checksum of the data this bp describes
234  * MAC[2]	128-bit message authentication code for this data
235  *
236  * The X bit being set indicates that this block is one of 3 types. If this is
237  * a level 0 block with an encrypted object type, the block is encrypted
238  * (see BP_IS_ENCRYPTED()). If this is a level 0 block with an unencrypted
239  * object type, this block is authenticated with an HMAC (see
240  * BP_IS_AUTHENTICATED()). Otherwise (if level > 0), this bp will use the MAC
241  * words to store a checksum-of-MACs from the level below (see
242  * BP_HAS_INDIRECT_MAC_CKSUM()). For convenience in the code, BP_IS_PROTECTED()
243  * refers to both encrypted and authenticated blocks and BP_USES_CRYPT()
244  * refers to any of these 3 kinds of blocks.
245  *
246  * The additional encryption parameters are the salt, IV, and MAC which are
247  * explained in greater detail in the block comment at the top of zio_crypt.c.
248  * The MAC occupies half of the checksum space since it serves a very similar
249  * purpose: to prevent data corruption on disk. The only functional difference
250  * is that the checksum is used to detect on-disk corruption whether or not the
251  * encryption key is loaded and the MAC provides additional protection against
252  * malicious disk tampering. We use the 3rd DVA to store the salt and first
253  * 64 bits of the IV. As a result encrypted blocks can only have 2 copies
254  * maximum instead of the normal 3. The last 32 bits of the IV are stored in
255  * the upper bits of what is usually the fill count. Note that only blocks at
256  * level 0 or -2 are ever encrypted, which allows us to guarantee that these
257  * 32 bits are not trampled over by other code (see zio_crypt.c for details).
258  * The salt and IV are not used for authenticated bps or bps with an indirect
259  * MAC checksum, so these blocks can utilize all 3 DVAs and the full 64 bits
260  * for the fill count.
261  */
262 
263 /*
264  * "Embedded" blkptr_t's don't actually point to a block, instead they
265  * have a data payload embedded in the blkptr_t itself.  See the comment
266  * in blkptr.c for more details.
267  *
268  * The blkptr_t is laid out as follows:
269  *
270  *	64	56	48	40	32	24	16	8	0
271  *	+-------+-------+-------+-------+-------+-------+-------+-------+
272  * 0	|      payload                                                  |
273  * 1	|      payload                                                  |
274  * 2	|      payload                                                  |
275  * 3	|      payload                                                  |
276  * 4	|      payload                                                  |
277  * 5	|      payload                                                  |
278  *	+-------+-------+-------+-------+-------+-------+-------+-------+
279  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
280  *	+-------+-------+-------+-------+-------+-------+-------+-------+
281  * 7	|      payload                                                  |
282  * 8	|      payload                                                  |
283  * 9	|      payload                                                  |
284  *	+-------+-------+-------+-------+-------+-------+-------+-------+
285  * a	|			logical birth txg			|
286  *	+-------+-------+-------+-------+-------+-------+-------+-------+
287  * b	|      payload                                                  |
288  * c	|      payload                                                  |
289  * d	|      payload                                                  |
290  * e	|      payload                                                  |
291  * f	|      payload                                                  |
292  *	+-------+-------+-------+-------+-------+-------+-------+-------+
293  *
294  * Legend:
295  *
296  * payload		contains the embedded data
297  * B (byteorder)	byteorder (endianness)
298  * D (dedup)		padding (set to zero)
299  * X			encryption (set to zero)
300  * E (embedded)		set to one
301  * lvl			indirection level
302  * type			DMU object type
303  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
304  * comp			compression function of payload
305  * PSIZE		size of payload after compression, in bytes
306  * LSIZE		logical size of payload, in bytes
307  *			note that 25 bits is enough to store the largest
308  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
309  * log. birth		transaction group in which the block was logically born
310  *
311  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
312  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
313  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
314  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
315  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
316  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
317  * other macros, as they assert that they are only used on BP's of the correct
318  * "embedded-ness". Encrypted blkptr_t's cannot be embedded because they use
319  * the payload space for encryption parameters (see the comment above on
320  * how encryption parameters are stored).
321  */
322 
323 #define	BPE_GET_ETYPE(bp)	\
324 	(ASSERT(BP_IS_EMBEDDED(bp)), \
325 	BF64_GET((bp)->blk_prop, 40, 8))
326 #define	BPE_SET_ETYPE(bp, t)	do { \
327 	ASSERT(BP_IS_EMBEDDED(bp)); \
328 	BF64_SET((bp)->blk_prop, 40, 8, t); \
329 } while (0)
330 
331 #define	BPE_GET_LSIZE(bp)	\
332 	(ASSERT(BP_IS_EMBEDDED(bp)), \
333 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
334 #define	BPE_SET_LSIZE(bp, x)	do { \
335 	ASSERT(BP_IS_EMBEDDED(bp)); \
336 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
337 } while (0)
338 
339 #define	BPE_GET_PSIZE(bp)	\
340 	(ASSERT(BP_IS_EMBEDDED(bp)), \
341 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
342 #define	BPE_SET_PSIZE(bp, x)	do { \
343 	ASSERT(BP_IS_EMBEDDED(bp)); \
344 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
345 } while (0)
346 
347 typedef enum bp_embedded_type {
348 	BP_EMBEDDED_TYPE_DATA,
349 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for Delphix byteswap feature. */
350 	BP_EMBEDDED_TYPE_REDACTED,
351 	NUM_BP_EMBEDDED_TYPES
352 } bp_embedded_type_t;
353 
354 #define	BPE_NUM_WORDS 14
355 #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
356 #define	BPE_IS_PAYLOADWORD(bp, wp) \
357 	((wp) != &(bp)->blk_prop && (wp) != (&(bp)->blk_birth_word[1]))
358 
359 #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
360 #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
361 #define	SPA_SYNC_MIN_VDEVS 3		/* min vdevs to update during sync */
362 
363 /*
364  * A block is a hole when it has either 1) never been written to, or
365  * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
366  * without physically allocating disk space. Holes are represented in the
367  * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
368  * done through the BP_IS_HOLE macro. For holes, the logical size, level,
369  * DMU object type, and birth times are all also stored for holes that
370  * were written to at some point (i.e. were punched after having been filled).
371  */
372 typedef struct blkptr {
373 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
374 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
375 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
376 	uint64_t	blk_birth_word[2];
377 	uint64_t	blk_fill;	/* fill count			    */
378 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
379 } blkptr_t;
380 
381 /*
382  * Macros to get and set fields in a bp or DVA.
383  */
384 
385 /*
386  * Note, for gang blocks, DVA_GET_ASIZE() is the total space allocated for
387  * this gang DVA including its children BP's.  The space allocated at this
388  * DVA's vdev/offset is vdev_gang_header_asize(vdev).
389  */
390 #define	DVA_GET_ASIZE(dva)	\
391 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
392 #define	DVA_SET_ASIZE(dva, x)	\
393 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
394 	SPA_MINBLOCKSHIFT, 0, x)
395 
396 #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, SPA_VDEVBITS)
397 #define	DVA_SET_VDEV(dva, x)	\
398 	BF64_SET((dva)->dva_word[0], 32, SPA_VDEVBITS, x)
399 
400 #define	DVA_GET_OFFSET(dva)	\
401 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
402 #define	DVA_SET_OFFSET(dva, x)	\
403 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
404 
405 #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
406 #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
407 
408 #define	BP_GET_LSIZE(bp)	\
409 	(BP_IS_EMBEDDED(bp) ?	\
410 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
411 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
412 #define	BP_SET_LSIZE(bp, x)	do { \
413 	ASSERT(!BP_IS_EMBEDDED(bp)); \
414 	BF64_SET_SB((bp)->blk_prop, \
415 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
416 } while (0)
417 
418 #define	BP_GET_PSIZE(bp)	\
419 	(BP_IS_EMBEDDED(bp) ? 0 : \
420 	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
421 #define	BP_SET_PSIZE(bp, x)	do { \
422 	ASSERT(!BP_IS_EMBEDDED(bp)); \
423 	BF64_SET_SB((bp)->blk_prop, \
424 	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
425 } while (0)
426 
427 #define	BP_GET_COMPRESS(bp)		\
428 	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
429 #define	BP_SET_COMPRESS(bp, x)		\
430 	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
431 
432 #define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
433 #define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
434 
435 #define	BP_GET_CHECKSUM(bp)		\
436 	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
437 	BF64_GET((bp)->blk_prop, 40, 8))
438 #define	BP_SET_CHECKSUM(bp, x)		do { \
439 	ASSERT(!BP_IS_EMBEDDED(bp)); \
440 	BF64_SET((bp)->blk_prop, 40, 8, x); \
441 } while (0)
442 
443 #define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
444 #define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
445 
446 #define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
447 #define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
448 
449 /* encrypted, authenticated, and MAC cksum bps use the same bit */
450 #define	BP_USES_CRYPT(bp)		BF64_GET((bp)->blk_prop, 61, 1)
451 #define	BP_SET_CRYPT(bp, x)		BF64_SET((bp)->blk_prop, 61, 1, x)
452 
453 #define	BP_IS_ENCRYPTED(bp)			\
454 	(BP_USES_CRYPT(bp) &&			\
455 	BP_GET_LEVEL(bp) <= 0 &&		\
456 	DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
457 
458 #define	BP_IS_AUTHENTICATED(bp)			\
459 	(BP_USES_CRYPT(bp) &&			\
460 	BP_GET_LEVEL(bp) <= 0 &&		\
461 	!DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
462 
463 #define	BP_HAS_INDIRECT_MAC_CKSUM(bp)		\
464 	(BP_USES_CRYPT(bp) && BP_GET_LEVEL(bp) > 0)
465 
466 #define	BP_IS_PROTECTED(bp)			\
467 	(BP_IS_ENCRYPTED(bp) || BP_IS_AUTHENTICATED(bp))
468 
469 #define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
470 #define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
471 
472 #define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
473 #define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
474 
475 #define	BP_GET_FREE(bp)			BF64_GET((bp)->blk_fill, 0, 1)
476 #define	BP_SET_FREE(bp, x)		BF64_SET((bp)->blk_fill, 0, 1, x)
477 
478 #define	BP_GET_LOGICAL_BIRTH(bp)	(bp)->blk_birth_word[1]
479 #define	BP_SET_LOGICAL_BIRTH(bp, x)	((bp)->blk_birth_word[1] = (x))
480 
481 #define	BP_GET_PHYSICAL_BIRTH(bp)	(bp)->blk_birth_word[0]
482 #define	BP_SET_PHYSICAL_BIRTH(bp, x)	((bp)->blk_birth_word[0] = (x))
483 
484 #define	BP_GET_BIRTH(bp)					\
485 	(BP_IS_EMBEDDED(bp) ? 0 : 				\
486 	BP_GET_PHYSICAL_BIRTH(bp) ? BP_GET_PHYSICAL_BIRTH(bp) :	\
487 	BP_GET_LOGICAL_BIRTH(bp))
488 
489 #define	BP_SET_BIRTH(bp, logical, physical)	\
490 {						\
491 	ASSERT(!BP_IS_EMBEDDED(bp));		\
492 	BP_SET_LOGICAL_BIRTH(bp, logical);	\
493 	BP_SET_PHYSICAL_BIRTH(bp, 		\
494 	    ((logical) == (physical) ? 0 : (physical))); \
495 }
496 
497 #define	BP_GET_FILL(bp)				\
498 	((BP_IS_ENCRYPTED(bp)) ? BF64_GET((bp)->blk_fill, 0, 32) : \
499 	((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill))
500 
501 #define	BP_SET_FILL(bp, fill)			\
502 {						\
503 	if (BP_IS_ENCRYPTED(bp))			\
504 		BF64_SET((bp)->blk_fill, 0, 32, fill); \
505 	else					\
506 		(bp)->blk_fill = fill;		\
507 }
508 
509 #define	BP_GET_IV2(bp)				\
510 	(ASSERT(BP_IS_ENCRYPTED(bp)),		\
511 	BF64_GET((bp)->blk_fill, 32, 32))
512 #define	BP_SET_IV2(bp, iv2)			\
513 {						\
514 	ASSERT(BP_IS_ENCRYPTED(bp));		\
515 	BF64_SET((bp)->blk_fill, 32, 32, iv2);	\
516 }
517 
518 #define	BP_IS_METADATA(bp)	\
519 	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
520 
521 #define	BP_GET_ASIZE(bp)	\
522 	(BP_IS_EMBEDDED(bp) ? 0 : \
523 	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
524 	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
525 	(DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
526 
527 #define	BP_GET_UCSIZE(bp)	\
528 	(BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
529 
530 #define	BP_GET_NDVAS(bp)	\
531 	(BP_IS_EMBEDDED(bp) ? 0 : \
532 	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
533 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
534 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
535 
536 #define	BP_COUNT_GANG(bp)	\
537 	(BP_IS_EMBEDDED(bp) ? 0 : \
538 	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
539 	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
540 	(DVA_GET_GANG(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp))))
541 
542 #define	DVA_EQUAL(dva1, dva2)	\
543 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
544 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
545 
546 #define	BP_EQUAL(bp1, bp2)	\
547 	(BP_GET_BIRTH(bp1) == BP_GET_BIRTH(bp2) &&	\
548 	BP_GET_LOGICAL_BIRTH(bp1) == BP_GET_LOGICAL_BIRTH(bp2) &&	\
549 	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
550 	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
551 	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
552 
553 
554 #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
555 
556 #define	BP_IDENTITY(bp)		(ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
557 #define	BP_IS_GANG(bp)		\
558 	(BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
559 #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&	\
560 				(dva)->dva_word[1] == 0ULL)
561 #define	BP_IS_HOLE(bp) \
562 	(!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
563 
564 #define	BP_SET_REDACTED(bp) \
565 {							\
566 	BP_SET_EMBEDDED(bp, B_TRUE);			\
567 	BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_REDACTED);	\
568 }
569 #define	BP_IS_REDACTED(bp) \
570 	(BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_REDACTED)
571 
572 /* BP_IS_RAIDZ(bp) assumes no block compression */
573 #define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
574 				BP_GET_PSIZE(bp))
575 
576 #define	BP_ZERO_DVAS(bp)			\
577 {						\
578 	(bp)->blk_dva[0].dva_word[0] = 0;	\
579 	(bp)->blk_dva[0].dva_word[1] = 0;	\
580 	(bp)->blk_dva[1].dva_word[0] = 0;	\
581 	(bp)->blk_dva[1].dva_word[1] = 0;	\
582 	(bp)->blk_dva[2].dva_word[0] = 0;	\
583 	(bp)->blk_dva[2].dva_word[1] = 0;	\
584 }
585 
586 #define	BP_ZERO(bp)				\
587 {						\
588 	BP_ZERO_DVAS(bp);			\
589 	(bp)->blk_prop = 0;			\
590 	(bp)->blk_pad[0] = 0;			\
591 	(bp)->blk_pad[1] = 0;			\
592 	(bp)->blk_birth_word[0] = 0;		\
593 	(bp)->blk_birth_word[1] = 0;		\
594 	(bp)->blk_fill = 0;			\
595 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
596 }
597 
598 #ifdef _ZFS_BIG_ENDIAN
599 #define	ZFS_HOST_BYTEORDER	(0ULL)
600 #else
601 #define	ZFS_HOST_BYTEORDER	(1ULL)
602 #endif
603 
604 #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
605 
606 #define	BP_SPRINTF_LEN	400
607 
608 /*
609  * This macro allows code sharing between zfs, libzpool, and mdb.
610  * 'func' is either kmem_scnprintf() or mdb_snprintf().
611  * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
612  */
613 
614 #define	SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
615 {									\
616 	static const char *const copyname[] =				\
617 	    { "zero", "single", "double", "triple" };			\
618 	int len = 0;							\
619 	int copies = 0;							\
620 	const char *crypt_type;						\
621 	if (bp != NULL) {						\
622 		if (BP_IS_ENCRYPTED(bp)) {				\
623 			crypt_type = "encrypted";			\
624 			/* LINTED E_SUSPICIOUS_COMPARISON */		\
625 		} else if (BP_IS_AUTHENTICATED(bp)) {			\
626 			crypt_type = "authenticated";			\
627 		} else if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {		\
628 			crypt_type = "indirect-MAC";			\
629 		} else {						\
630 			crypt_type = "unencrypted";			\
631 		}							\
632 	}								\
633 	if (bp == NULL) {						\
634 		len += func(buf + len, size - len, "<NULL>");		\
635 	} else if (BP_IS_HOLE(bp)) {					\
636 		len += func(buf + len, size - len,			\
637 		    "HOLE [L%llu %s] "					\
638 		    "size=%llxL birth=%lluL",				\
639 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
640 		    type,						\
641 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
642 		    (u_longlong_t)BP_GET_LOGICAL_BIRTH(bp));		\
643 	} else if (BP_IS_EMBEDDED(bp)) {				\
644 		len = func(buf + len, size - len,			\
645 		    "EMBEDDED [L%llu %s] et=%u %s "			\
646 		    "size=%llxL/%llxP birth=%lluL",			\
647 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
648 		    type,						\
649 		    (int)BPE_GET_ETYPE(bp),				\
650 		    compress,						\
651 		    (u_longlong_t)BPE_GET_LSIZE(bp),			\
652 		    (u_longlong_t)BPE_GET_PSIZE(bp),			\
653 		    (u_longlong_t)BP_GET_LOGICAL_BIRTH(bp));		\
654 	} else if (BP_IS_REDACTED(bp)) {				\
655 		len += func(buf + len, size - len,			\
656 		    "REDACTED [L%llu %s] size=%llxL birth=%lluL",	\
657 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
658 		    type,						\
659 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
660 		    (u_longlong_t)BP_GET_LOGICAL_BIRTH(bp));		\
661 	} else {							\
662 		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
663 			const dva_t *dva = &bp->blk_dva[d];		\
664 			if (DVA_IS_VALID(dva))				\
665 				copies++;				\
666 			len += func(buf + len, size - len,		\
667 			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
668 			    (u_longlong_t)DVA_GET_VDEV(dva),		\
669 			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
670 			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
671 			    ws);					\
672 		}							\
673 		ASSERT3S(copies, >, 0);					\
674 		if (BP_IS_ENCRYPTED(bp)) {				\
675 			len += func(buf + len, size - len,		\
676 			    "salt=%llx iv=%llx:%llx%c",			\
677 			    (u_longlong_t)bp->blk_dva[2].dva_word[0],	\
678 			    (u_longlong_t)bp->blk_dva[2].dva_word[1],	\
679 			    (u_longlong_t)BP_GET_IV2(bp),		\
680 			    ws);					\
681 		}							\
682 		if (BP_IS_GANG(bp) &&					\
683 		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
684 		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
685 			copies--;					\
686 		len += func(buf + len, size - len,			\
687 		    "[L%llu %s] %s %s %s %s %s %s %s%c"			\
688 		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
689 		    "cksum=%016llx:%016llx:%016llx:%016llx",		\
690 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
691 		    type,						\
692 		    checksum,						\
693 		    compress,						\
694 		    crypt_type,						\
695 		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
696 		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
697 		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
698 		    copyname[copies],					\
699 		    ws,							\
700 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
701 		    (u_longlong_t)BP_GET_PSIZE(bp),			\
702 		    (u_longlong_t)BP_GET_LOGICAL_BIRTH(bp),		\
703 		    (u_longlong_t)BP_GET_BIRTH(bp),			\
704 		    (u_longlong_t)BP_GET_FILL(bp),			\
705 		    ws,							\
706 		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
707 		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
708 		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
709 		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
710 	}								\
711 	ASSERT(len < size);						\
712 }
713 
714 #define	BP_GET_BUFC_TYPE(bp)						\
715 	(BP_IS_METADATA(bp) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
716 
717 typedef enum spa_import_type {
718 	SPA_IMPORT_EXISTING,
719 	SPA_IMPORT_ASSEMBLE
720 } spa_import_type_t;
721 
722 typedef enum spa_mode {
723 	SPA_MODE_UNINIT = 0,
724 	SPA_MODE_READ = 1,
725 	SPA_MODE_WRITE = 2,
726 } spa_mode_t;
727 
728 /*
729  * Send TRIM commands in-line during normal pool operation while deleting.
730  *	OFF: no
731  *	ON: yes
732  */
733 typedef enum {
734 	SPA_AUTOTRIM_OFF = 0,	/* default */
735 	SPA_AUTOTRIM_ON,
736 } spa_autotrim_t;
737 
738 /*
739  * Reason TRIM command was issued, used internally for accounting purposes.
740  */
741 typedef enum trim_type {
742 	TRIM_TYPE_MANUAL = 0,
743 	TRIM_TYPE_AUTO = 1,
744 	TRIM_TYPE_SIMPLE = 2
745 } trim_type_t;
746 
747 /* state manipulation functions */
748 extern int spa_open(const char *pool, spa_t **, const void *tag);
749 extern int spa_open_rewind(const char *pool, spa_t **, const void *tag,
750     nvlist_t *policy, nvlist_t **config);
751 extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
752     size_t buflen);
753 extern int spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
754     nvlist_t *zplprops, struct dsl_crypto_params *dcp);
755 extern int spa_import(char *pool, nvlist_t *config, nvlist_t *props,
756     uint64_t flags);
757 extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
758 extern int spa_destroy(const char *pool);
759 extern int spa_checkpoint(const char *pool);
760 extern int spa_checkpoint_discard(const char *pool);
761 extern int spa_export(const char *pool, nvlist_t **oldconfig, boolean_t force,
762     boolean_t hardforce);
763 extern int spa_reset(const char *pool);
764 extern void spa_async_request(spa_t *spa, int flag);
765 extern void spa_async_unrequest(spa_t *spa, int flag);
766 extern void spa_async_suspend(spa_t *spa);
767 extern void spa_async_resume(spa_t *spa);
768 extern int spa_async_tasks(spa_t *spa);
769 extern spa_t *spa_inject_addref(char *pool);
770 extern void spa_inject_delref(spa_t *spa);
771 extern void spa_scan_stat_init(spa_t *spa);
772 extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
773 extern int bpobj_enqueue_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx);
774 extern int bpobj_enqueue_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx);
775 
776 #define	SPA_ASYNC_CONFIG_UPDATE			0x01
777 #define	SPA_ASYNC_REMOVE			0x02
778 #define	SPA_ASYNC_FAULT_VDEV			0x04
779 #define	SPA_ASYNC_RESILVER_DONE			0x08
780 #define	SPA_ASYNC_RESILVER			0x10
781 #define	SPA_ASYNC_AUTOEXPAND			0x20
782 #define	SPA_ASYNC_REMOVE_DONE			0x40
783 #define	SPA_ASYNC_REMOVE_STOP			0x80
784 #define	SPA_ASYNC_INITIALIZE_RESTART		0x100
785 #define	SPA_ASYNC_TRIM_RESTART			0x200
786 #define	SPA_ASYNC_AUTOTRIM_RESTART		0x400
787 #define	SPA_ASYNC_L2CACHE_REBUILD		0x800
788 #define	SPA_ASYNC_L2CACHE_TRIM			0x1000
789 #define	SPA_ASYNC_REBUILD_DONE			0x2000
790 #define	SPA_ASYNC_DETACH_SPARE			0x4000
791 
792 /* device manipulation */
793 extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot, boolean_t ashift_check);
794 extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
795     int replacing, int rebuild);
796 extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
797     int replace_done);
798 extern int spa_vdev_alloc(spa_t *spa, uint64_t guid);
799 extern int spa_vdev_noalloc(spa_t *spa, uint64_t guid);
800 extern boolean_t spa_vdev_remove_active(spa_t *spa);
801 extern int spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type,
802     nvlist_t *vdev_errlist);
803 extern int spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type,
804     uint64_t rate, boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist);
805 extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
806 extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
807 extern int spa_vdev_split_mirror(spa_t *spa, const char *newname,
808     nvlist_t *config, nvlist_t *props, boolean_t exp);
809 
810 /* spare state (which is global across all pools) */
811 extern void spa_spare_add(vdev_t *vd);
812 extern void spa_spare_remove(vdev_t *vd);
813 extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
814 extern void spa_spare_activate(vdev_t *vd);
815 
816 /* L2ARC state (which is global across all pools) */
817 extern void spa_l2cache_add(vdev_t *vd);
818 extern void spa_l2cache_remove(vdev_t *vd);
819 extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
820 extern void spa_l2cache_activate(vdev_t *vd);
821 extern void spa_l2cache_drop(spa_t *spa);
822 
823 /* scanning */
824 extern int spa_scan(spa_t *spa, pool_scan_func_t func);
825 extern int spa_scan_stop(spa_t *spa);
826 extern int spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t flag);
827 
828 /* spa syncing */
829 extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
830 extern void spa_sync_allpools(void);
831 
832 extern uint_t zfs_sync_pass_deferred_free;
833 
834 /* spa sync taskqueues */
835 taskq_t *spa_sync_tq_create(spa_t *spa, const char *name);
836 void spa_sync_tq_destroy(spa_t *spa);
837 uint_t spa_acq_allocator(spa_t *spa);
838 void spa_rel_allocator(spa_t *spa, uint_t allocator);
839 void spa_select_allocator(zio_t *zio);
840 
841 /* spa namespace global mutex */
842 extern kmutex_t spa_namespace_lock;
843 extern avl_tree_t spa_namespace_avl;
844 extern kcondvar_t spa_namespace_cv;
845 
846 /*
847  * SPA configuration functions in spa_config.c
848  */
849 
850 #define	SPA_CONFIG_UPDATE_POOL	0
851 #define	SPA_CONFIG_UPDATE_VDEVS	1
852 
853 extern void spa_write_cachefile(spa_t *, boolean_t, boolean_t, boolean_t);
854 extern void spa_config_load(void);
855 extern int spa_all_configs(uint64_t *generation, nvlist_t **pools);
856 extern void spa_config_set(spa_t *spa, nvlist_t *config);
857 extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
858     int getstats);
859 extern void spa_config_update(spa_t *spa, int what);
860 extern int spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv,
861     vdev_t *parent, uint_t id, int atype);
862 
863 
864 /*
865  * Miscellaneous SPA routines in spa_misc.c
866  */
867 
868 /* Namespace manipulation */
869 extern spa_t *spa_lookup(const char *name);
870 extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
871 extern void spa_remove(spa_t *spa);
872 extern spa_t *spa_next(spa_t *prev);
873 
874 /* Refcount functions */
875 extern void spa_open_ref(spa_t *spa, const void *tag);
876 extern void spa_close(spa_t *spa, const void *tag);
877 extern void spa_async_close(spa_t *spa, const void *tag);
878 extern boolean_t spa_refcount_zero(spa_t *spa);
879 
880 #define	SCL_NONE	0x00
881 #define	SCL_CONFIG	0x01
882 #define	SCL_STATE	0x02
883 #define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
884 #define	SCL_ALLOC	0x08
885 #define	SCL_ZIO		0x10
886 #define	SCL_FREE	0x20
887 #define	SCL_VDEV	0x40
888 #define	SCL_LOCKS	7
889 #define	SCL_ALL		((1 << SCL_LOCKS) - 1)
890 #define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
891 
892 /* Historical pool statistics */
893 typedef struct spa_history_kstat {
894 	kmutex_t		lock;
895 	uint64_t		count;
896 	uint64_t		size;
897 	kstat_t			*kstat;
898 	void			*priv;
899 	list_t			list;
900 } spa_history_kstat_t;
901 
902 typedef struct spa_history_list {
903 	uint64_t		size;
904 	procfs_list_t		procfs_list;
905 } spa_history_list_t;
906 
907 typedef struct spa_stats {
908 	spa_history_list_t	read_history;
909 	spa_history_list_t	txg_history;
910 	spa_history_kstat_t	tx_assign_histogram;
911 	spa_history_list_t	mmp_history;
912 	spa_history_kstat_t	state;		/* pool state */
913 	spa_history_kstat_t	guid;		/* pool guid */
914 	spa_history_kstat_t	iostats;
915 } spa_stats_t;
916 
917 typedef enum txg_state {
918 	TXG_STATE_BIRTH		= 0,
919 	TXG_STATE_OPEN		= 1,
920 	TXG_STATE_QUIESCED	= 2,
921 	TXG_STATE_WAIT_FOR_SYNC	= 3,
922 	TXG_STATE_SYNCED	= 4,
923 	TXG_STATE_COMMITTED	= 5,
924 } txg_state_t;
925 
926 typedef struct txg_stat {
927 	vdev_stat_t		vs1;
928 	vdev_stat_t		vs2;
929 	uint64_t		txg;
930 	uint64_t		ndirty;
931 } txg_stat_t;
932 
933 /* Assorted pool IO kstats */
934 typedef struct spa_iostats {
935 	kstat_named_t	trim_extents_written;
936 	kstat_named_t	trim_bytes_written;
937 	kstat_named_t	trim_extents_skipped;
938 	kstat_named_t	trim_bytes_skipped;
939 	kstat_named_t	trim_extents_failed;
940 	kstat_named_t	trim_bytes_failed;
941 	kstat_named_t	autotrim_extents_written;
942 	kstat_named_t	autotrim_bytes_written;
943 	kstat_named_t	autotrim_extents_skipped;
944 	kstat_named_t	autotrim_bytes_skipped;
945 	kstat_named_t	autotrim_extents_failed;
946 	kstat_named_t	autotrim_bytes_failed;
947 	kstat_named_t	simple_trim_extents_written;
948 	kstat_named_t	simple_trim_bytes_written;
949 	kstat_named_t	simple_trim_extents_skipped;
950 	kstat_named_t	simple_trim_bytes_skipped;
951 	kstat_named_t	simple_trim_extents_failed;
952 	kstat_named_t	simple_trim_bytes_failed;
953 	kstat_named_t	arc_read_count;
954 	kstat_named_t	arc_read_bytes;
955 	kstat_named_t	arc_write_count;
956 	kstat_named_t	arc_write_bytes;
957 	kstat_named_t	direct_read_count;
958 	kstat_named_t	direct_read_bytes;
959 	kstat_named_t	direct_write_count;
960 	kstat_named_t	direct_write_bytes;
961 } spa_iostats_t;
962 
963 extern void spa_stats_init(spa_t *spa);
964 extern void spa_stats_destroy(spa_t *spa);
965 extern void spa_read_history_add(spa_t *spa, const zbookmark_phys_t *zb,
966     uint32_t aflags);
967 extern void spa_txg_history_add(spa_t *spa, uint64_t txg, hrtime_t birth_time);
968 extern int spa_txg_history_set(spa_t *spa,  uint64_t txg,
969     txg_state_t completed_state, hrtime_t completed_time);
970 extern txg_stat_t *spa_txg_history_init_io(spa_t *, uint64_t,
971     struct dsl_pool *);
972 extern void spa_txg_history_fini_io(spa_t *, txg_stat_t *);
973 extern void spa_tx_assign_add_nsecs(spa_t *spa, uint64_t nsecs);
974 extern int spa_mmp_history_set_skip(spa_t *spa, uint64_t mmp_kstat_id);
975 extern int spa_mmp_history_set(spa_t *spa, uint64_t mmp_kstat_id, int io_error,
976     hrtime_t duration);
977 extern void spa_mmp_history_add(spa_t *spa, uint64_t txg, uint64_t timestamp,
978     uint64_t mmp_delay, vdev_t *vd, int label, uint64_t mmp_kstat_id,
979     int error);
980 extern void spa_iostats_trim_add(spa_t *spa, trim_type_t type,
981     uint64_t extents_written, uint64_t bytes_written,
982     uint64_t extents_skipped, uint64_t bytes_skipped,
983     uint64_t extents_failed, uint64_t bytes_failed);
984 extern void spa_iostats_read_add(spa_t *spa, uint64_t size, uint64_t iops,
985     uint32_t flags);
986 extern void spa_iostats_write_add(spa_t *spa, uint64_t size, uint64_t iops,
987     uint32_t flags);
988 extern void spa_import_progress_add(spa_t *spa);
989 extern void spa_import_progress_remove(uint64_t spa_guid);
990 extern int spa_import_progress_set_mmp_check(uint64_t pool_guid,
991     uint64_t mmp_sec_remaining);
992 extern int spa_import_progress_set_max_txg(uint64_t pool_guid,
993     uint64_t max_txg);
994 extern int spa_import_progress_set_state(uint64_t pool_guid,
995     spa_load_state_t spa_load_state);
996 extern void spa_import_progress_set_notes(spa_t *spa,
997     const char *fmt, ...) __printflike(2, 3);
998 extern void spa_import_progress_set_notes_nolog(spa_t *spa,
999     const char *fmt, ...) __printflike(2, 3);
1000 
1001 /* Pool configuration locks */
1002 extern int spa_config_tryenter(spa_t *spa, int locks, const void *tag,
1003     krw_t rw);
1004 extern void spa_config_enter(spa_t *spa, int locks, const void *tag, krw_t rw);
1005 extern void spa_config_enter_mmp(spa_t *spa, int locks, const void *tag,
1006     krw_t rw);
1007 extern void spa_config_exit(spa_t *spa, int locks, const void *tag);
1008 extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
1009 
1010 /* Pool vdev add/remove lock */
1011 extern uint64_t spa_vdev_enter(spa_t *spa);
1012 extern uint64_t spa_vdev_detach_enter(spa_t *spa, uint64_t guid);
1013 extern uint64_t spa_vdev_config_enter(spa_t *spa);
1014 extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
1015     int error, const char *tag);
1016 extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
1017 
1018 /* Pool vdev state change lock */
1019 extern void spa_vdev_state_enter(spa_t *spa, int oplock);
1020 extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
1021 
1022 /* Log state */
1023 typedef enum spa_log_state {
1024 	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
1025 	SPA_LOG_MISSING,	/* missing log(s) */
1026 	SPA_LOG_CLEAR,		/* clear the log(s) */
1027 	SPA_LOG_GOOD,		/* log(s) are good */
1028 } spa_log_state_t;
1029 
1030 extern spa_log_state_t spa_get_log_state(spa_t *spa);
1031 extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
1032 extern int spa_reset_logs(spa_t *spa);
1033 
1034 /* Log claim callback */
1035 extern void spa_claim_notify(zio_t *zio);
1036 extern void spa_deadman(void *);
1037 
1038 /* Accessor functions */
1039 extern boolean_t spa_shutting_down(spa_t *spa);
1040 extern struct dsl_pool *spa_get_dsl(spa_t *spa);
1041 extern boolean_t spa_is_initializing(spa_t *spa);
1042 extern boolean_t spa_indirect_vdevs_loaded(spa_t *spa);
1043 extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
1044 extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
1045 extern void spa_altroot(spa_t *, char *, size_t);
1046 extern uint32_t spa_sync_pass(spa_t *spa);
1047 extern char *spa_name(spa_t *spa);
1048 extern uint64_t spa_guid(spa_t *spa);
1049 extern uint64_t spa_load_guid(spa_t *spa);
1050 extern uint64_t spa_last_synced_txg(spa_t *spa);
1051 extern uint64_t spa_first_txg(spa_t *spa);
1052 extern uint64_t spa_syncing_txg(spa_t *spa);
1053 extern uint64_t spa_final_dirty_txg(spa_t *spa);
1054 extern uint64_t spa_version(spa_t *spa);
1055 extern pool_state_t spa_state(spa_t *spa);
1056 extern spa_load_state_t spa_load_state(spa_t *spa);
1057 extern uint64_t spa_freeze_txg(spa_t *spa);
1058 extern uint64_t spa_get_worst_case_asize(spa_t *spa, uint64_t lsize);
1059 extern uint64_t spa_get_dspace(spa_t *spa);
1060 extern uint64_t spa_get_checkpoint_space(spa_t *spa);
1061 extern uint64_t spa_get_slop_space(spa_t *spa);
1062 extern void spa_update_dspace(spa_t *spa);
1063 extern uint64_t spa_version(spa_t *spa);
1064 extern boolean_t spa_deflate(spa_t *spa);
1065 extern metaslab_class_t *spa_normal_class(spa_t *spa);
1066 extern metaslab_class_t *spa_log_class(spa_t *spa);
1067 extern metaslab_class_t *spa_embedded_log_class(spa_t *spa);
1068 extern metaslab_class_t *spa_special_class(spa_t *spa);
1069 extern metaslab_class_t *spa_dedup_class(spa_t *spa);
1070 extern metaslab_class_t *spa_preferred_class(spa_t *spa, const zio_t *zio);
1071 extern boolean_t spa_special_has_ddt(spa_t *spa);
1072 
1073 extern void spa_evicting_os_register(spa_t *, objset_t *os);
1074 extern void spa_evicting_os_deregister(spa_t *, objset_t *os);
1075 extern void spa_evicting_os_wait(spa_t *spa);
1076 extern int spa_max_replication(spa_t *spa);
1077 extern int spa_prev_software_version(spa_t *spa);
1078 extern uint64_t spa_get_failmode(spa_t *spa);
1079 extern uint64_t spa_get_deadman_failmode(spa_t *spa);
1080 extern void spa_set_deadman_failmode(spa_t *spa, const char *failmode);
1081 extern boolean_t spa_suspended(spa_t *spa);
1082 extern uint64_t spa_bootfs(spa_t *spa);
1083 extern uint64_t spa_delegation(spa_t *spa);
1084 extern objset_t *spa_meta_objset(spa_t *spa);
1085 extern space_map_t *spa_syncing_log_sm(spa_t *spa);
1086 extern uint64_t spa_deadman_synctime(spa_t *spa);
1087 extern uint64_t spa_deadman_ziotime(spa_t *spa);
1088 extern uint64_t spa_dirty_data(spa_t *spa);
1089 extern spa_autotrim_t spa_get_autotrim(spa_t *spa);
1090 extern int spa_get_allocator(spa_t *spa);
1091 extern void spa_set_allocator(spa_t *spa, const char *allocator);
1092 
1093 /* Miscellaneous support routines */
1094 extern void spa_load_failed(spa_t *spa, const char *fmt, ...)
1095     __attribute__((format(printf, 2, 3)));
1096 extern void spa_load_note(spa_t *spa, const char *fmt, ...)
1097     __attribute__((format(printf, 2, 3)));
1098 extern void spa_activate_mos_feature(spa_t *spa, const char *feature,
1099     dmu_tx_t *tx);
1100 extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
1101 extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
1102 extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
1103 extern char *spa_strdup(const char *);
1104 extern void spa_strfree(char *);
1105 extern uint64_t spa_generate_guid(spa_t *spa);
1106 extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
1107 extern void spa_freeze(spa_t *spa);
1108 extern int spa_change_guid(spa_t *spa, const uint64_t *guidp);
1109 extern void spa_upgrade(spa_t *spa, uint64_t version);
1110 extern void spa_evict_all(void);
1111 extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
1112     boolean_t l2cache);
1113 extern boolean_t spa_has_l2cache(spa_t *, uint64_t guid);
1114 extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
1115 extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
1116 extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
1117 extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
1118 extern boolean_t spa_has_slogs(spa_t *spa);
1119 extern boolean_t spa_is_root(spa_t *spa);
1120 extern boolean_t spa_writeable(spa_t *spa);
1121 extern boolean_t spa_has_pending_synctask(spa_t *spa);
1122 extern int spa_maxblocksize(spa_t *spa);
1123 extern int spa_maxdnodesize(spa_t *spa);
1124 extern boolean_t spa_has_checkpoint(spa_t *spa);
1125 extern boolean_t spa_importing_readonly_checkpoint(spa_t *spa);
1126 extern boolean_t spa_suspend_async_destroy(spa_t *spa);
1127 extern uint64_t spa_min_claim_txg(spa_t *spa);
1128 extern boolean_t zfs_dva_valid(spa_t *spa, const dva_t *dva,
1129     const blkptr_t *bp);
1130 typedef void (*spa_remap_cb_t)(uint64_t vdev, uint64_t offset, uint64_t size,
1131     void *arg);
1132 extern boolean_t spa_remap_blkptr(spa_t *spa, blkptr_t *bp,
1133     spa_remap_cb_t callback, void *arg);
1134 extern uint64_t spa_get_last_removal_txg(spa_t *spa);
1135 extern boolean_t spa_trust_config(spa_t *spa);
1136 extern uint64_t spa_missing_tvds_allowed(spa_t *spa);
1137 extern void spa_set_missing_tvds(spa_t *spa, uint64_t missing);
1138 extern boolean_t spa_top_vdevs_spacemap_addressable(spa_t *spa);
1139 extern uint64_t spa_total_metaslabs(spa_t *spa);
1140 extern boolean_t spa_multihost(spa_t *spa);
1141 extern uint32_t spa_get_hostid(spa_t *spa);
1142 extern void spa_activate_allocation_classes(spa_t *, dmu_tx_t *);
1143 extern boolean_t spa_livelist_delete_check(spa_t *spa);
1144 
1145 extern boolean_t spa_mmp_remote_host_activity(spa_t *spa);
1146 
1147 extern spa_mode_t spa_mode(spa_t *spa);
1148 extern uint64_t zfs_strtonum(const char *str, char **nptr);
1149 
1150 extern char *spa_his_ievent_table[];
1151 
1152 extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
1153 extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
1154     char *his_buf);
1155 extern int spa_history_log(spa_t *spa, const char *his_buf);
1156 extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
1157 extern void spa_history_log_version(spa_t *spa, const char *operation,
1158     dmu_tx_t *tx);
1159 extern void spa_history_log_internal(spa_t *spa, const char *operation,
1160     dmu_tx_t *tx, const char *fmt, ...) __printflike(4, 5);
1161 extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
1162     dmu_tx_t *tx, const char *fmt, ...)  __printflike(4, 5);
1163 extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
1164     dmu_tx_t *tx, const char *fmt, ...) __printflike(4, 5);
1165 
1166 extern const char *spa_state_to_name(spa_t *spa);
1167 
1168 /* error handling */
1169 struct zbookmark_phys;
1170 extern void spa_log_error(spa_t *spa, const zbookmark_phys_t *zb,
1171     const uint64_t birth);
1172 extern void spa_remove_error(spa_t *spa, zbookmark_phys_t *zb,
1173     uint64_t birth);
1174 extern int zfs_ereport_post(const char *clazz, spa_t *spa, vdev_t *vd,
1175     const zbookmark_phys_t *zb, zio_t *zio, uint64_t state);
1176 extern boolean_t zfs_ereport_is_valid(const char *clazz, spa_t *spa, vdev_t *vd,
1177     zio_t *zio);
1178 extern void zfs_ereport_taskq_fini(void);
1179 extern void zfs_ereport_clear(spa_t *spa, vdev_t *vd);
1180 extern nvlist_t *zfs_event_create(spa_t *spa, vdev_t *vd, const char *type,
1181     const char *name, nvlist_t *aux);
1182 extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
1183 extern void zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate);
1184 extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
1185 extern uint64_t spa_approx_errlog_size(spa_t *spa);
1186 extern int spa_get_errlog(spa_t *spa, void *uaddr, uint64_t *count);
1187 extern uint64_t spa_get_last_errlog_size(spa_t *spa);
1188 extern void spa_errlog_rotate(spa_t *spa);
1189 extern void spa_errlog_drain(spa_t *spa);
1190 extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
1191 extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
1192 extern void spa_delete_dataset_errlog(spa_t *spa, uint64_t ds, dmu_tx_t *tx);
1193 extern void spa_swap_errlog(spa_t *spa, uint64_t new_head_ds,
1194     uint64_t old_head_ds, dmu_tx_t *tx);
1195 extern void sync_error_list(spa_t *spa, avl_tree_t *t, uint64_t *obj,
1196     dmu_tx_t *tx);
1197 extern void spa_upgrade_errlog(spa_t *spa, dmu_tx_t *tx);
1198 extern int find_top_affected_fs(spa_t *spa, uint64_t head_ds,
1199     zbookmark_err_phys_t *zep, uint64_t *top_affected_fs);
1200 extern int find_birth_txg(struct dsl_dataset *ds, zbookmark_err_phys_t *zep,
1201     uint64_t *birth_txg);
1202 extern void zep_to_zb(uint64_t dataset, zbookmark_err_phys_t *zep,
1203     zbookmark_phys_t *zb);
1204 extern void name_to_errphys(char *buf, zbookmark_err_phys_t *zep);
1205 
1206 /* vdev mirror */
1207 extern void vdev_mirror_stat_init(void);
1208 extern void vdev_mirror_stat_fini(void);
1209 
1210 /* Initialization and termination */
1211 extern void spa_init(spa_mode_t mode);
1212 extern void spa_fini(void);
1213 extern void spa_boot_init(void);
1214 
1215 /* properties */
1216 extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
1217 extern int spa_prop_get(spa_t *spa, nvlist_t *nvp);
1218 extern int spa_prop_get_nvlist(spa_t *spa, char **props,
1219     unsigned int n_props, nvlist_t *outnvl);
1220 extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
1221 extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
1222 
1223 /* asynchronous event notification */
1224 extern void spa_event_notify(spa_t *spa, vdev_t *vdev, nvlist_t *hist_nvl,
1225     const char *name);
1226 extern void zfs_ereport_zvol_post(const char *subclass, const char *name,
1227     const char *device_name, const char *raw_name);
1228 
1229 /* waiting for pool activities to complete */
1230 extern int spa_wait(const char *pool, zpool_wait_activity_t activity,
1231     boolean_t *waited);
1232 extern int spa_wait_tag(const char *name, zpool_wait_activity_t activity,
1233     uint64_t tag, boolean_t *waited);
1234 extern void spa_notify_waiters(spa_t *spa);
1235 extern void spa_wake_waiters(spa_t *spa);
1236 
1237 extern void spa_import_os(spa_t *spa);
1238 extern void spa_export_os(spa_t *spa);
1239 extern void spa_activate_os(spa_t *spa);
1240 extern void spa_deactivate_os(spa_t *spa);
1241 
1242 /* module param call functions */
1243 int param_set_deadman_ziotime(ZFS_MODULE_PARAM_ARGS);
1244 int param_set_deadman_synctime(ZFS_MODULE_PARAM_ARGS);
1245 int param_set_slop_shift(ZFS_MODULE_PARAM_ARGS);
1246 int param_set_deadman_failmode(ZFS_MODULE_PARAM_ARGS);
1247 int param_set_active_allocator(ZFS_MODULE_PARAM_ARGS);
1248 
1249 #ifdef ZFS_DEBUG
1250 #define	dprintf_bp(bp, fmt, ...) do {				\
1251 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
1252 	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
1253 	snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));	\
1254 	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
1255 	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
1256 	} \
1257 } while (0)
1258 #else
1259 #define	dprintf_bp(bp, fmt, ...)
1260 #endif
1261 
1262 extern spa_mode_t spa_mode_global;
1263 extern int zfs_deadman_enabled;
1264 extern uint64_t zfs_deadman_synctime_ms;
1265 extern uint64_t zfs_deadman_ziotime_ms;
1266 extern uint64_t zfs_deadman_checktime_ms;
1267 
1268 extern kmem_cache_t *zio_buf_cache[];
1269 extern kmem_cache_t *zio_data_buf_cache[];
1270 
1271 #ifdef	__cplusplus
1272 }
1273 #endif
1274 
1275 #endif	/* _SYS_SPA_H */
1276