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