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