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