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