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