xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/spa.h (revision b4fb0039)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23814dcd43SSerapheim Dimitropoulos  * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2645818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
28e830fb12SKody A Kantor  * Copyright 2019 Joyent, Inc.
290c06d385Sjwpoduska  * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
30663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
3130c304d9SJoshua M. Clulow  * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
32fa9e4066Sahrens  */
33fa9e4066Sahrens 
34fa9e4066Sahrens #ifndef _SYS_SPA_H
35fa9e4066Sahrens #define	_SYS_SPA_H
36fa9e4066Sahrens 
37fa9e4066Sahrens #include <sys/avl.h>
38fa9e4066Sahrens #include <sys/zfs_context.h>
39fa9e4066Sahrens #include <sys/nvpair.h>
405cabbc6bSPrashanth Sreenivasa #include <sys/sysevent.h>
41fa9e4066Sahrens #include <sys/sysmacros.h>
42fa9e4066Sahrens #include <sys/types.h>
43fa9e4066Sahrens #include <sys/fs/zfs.h>
444ba5b961SDan Kimmel #include <sys/dmu.h>
45814dcd43SSerapheim Dimitropoulos #include <sys/space_map.h>
464d7988d6SPaul Dagnelie #include <sys/bitops.h>
47fa9e4066Sahrens 
48fa9e4066Sahrens #ifdef	__cplusplus
49fa9e4066Sahrens extern "C" {
50fa9e4066Sahrens #endif
51fa9e4066Sahrens 
52fa9e4066Sahrens /*
53fa9e4066Sahrens  * Forward references that lots of things need.
54fa9e4066Sahrens  */
55fa9e4066Sahrens typedef struct spa spa_t;
56fa9e4066Sahrens typedef struct vdev vdev_t;
57fa9e4066Sahrens typedef struct metaslab metaslab_t;
58b24ab676SJeff Bonwick typedef struct metaslab_group metaslab_group_t;
59b24ab676SJeff Bonwick typedef struct metaslab_class metaslab_class_t;
60b24ab676SJeff Bonwick typedef struct zio zio_t;
61fa9e4066Sahrens typedef struct zilog zilog_t;
62fa94a07fSbrendan typedef struct spa_aux_vdev spa_aux_vdev_t;
63b24ab676SJeff Bonwick typedef struct ddt ddt_t;
64b24ab676SJeff Bonwick typedef struct ddt_entry ddt_entry_t;
65fa9e4066Sahrens struct dsl_pool;
664445fffbSMatthew Ahrens struct dsl_dataset;
67eb633035STom Caputi struct dsl_crypto_params;
68fa9e4066Sahrens 
69fa9e4066Sahrens /*
70b5152584SMatthew Ahrens  * We currently support block sizes from 512 bytes to 16MB.
71b5152584SMatthew Ahrens  * The benefits of larger blocks, and thus larger IO, need to be weighed
72b5152584SMatthew Ahrens  * against the cost of COWing a giant block to modify one byte, and the
73b5152584SMatthew Ahrens  * large latency of reading or writing a large block.
74b5152584SMatthew Ahrens  *
75b5152584SMatthew Ahrens  * Note that although blocks up to 16MB are supported, the recordsize
76b5152584SMatthew Ahrens  * property can not be set larger than zfs_max_recordsize (default 1MB).
77b5152584SMatthew Ahrens  * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
78b5152584SMatthew Ahrens  *
79b5152584SMatthew Ahrens  * Note that although the LSIZE field of the blkptr_t can store sizes up
80b5152584SMatthew Ahrens  * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
81b5152584SMatthew Ahrens  * 32MB - 512 bytes.  Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
82fa9e4066Sahrens  */
83fa9e4066Sahrens #define	SPA_MINBLOCKSHIFT	9
84b5152584SMatthew Ahrens #define	SPA_OLD_MAXBLOCKSHIFT	17
85b5152584SMatthew Ahrens #define	SPA_MAXBLOCKSHIFT	24
86fa9e4066Sahrens #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
87b5152584SMatthew Ahrens #define	SPA_OLD_MAXBLOCKSIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)
88fa9e4066Sahrens #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
89fa9e4066Sahrens 
905711d393Sloli /*
915711d393Sloli  * Alignment Shift (ashift) is an immutable, internal top-level vdev property
925711d393Sloli  * which can only be set at vdev creation time. Physical writes are always done
935711d393Sloli  * according to it, which makes 2^ashift the smallest possible IO on a vdev.
945711d393Sloli  *
955711d393Sloli  * We currently allow values ranging from 512 bytes (2^9 = 512) to 64 KiB
965711d393Sloli  * (2^16 = 65,536).
975711d393Sloli  */
985711d393Sloli #define	ASHIFT_MIN		9
995711d393Sloli #define	ASHIFT_MAX		16
1005711d393Sloli 
101f7991ba4STim Haley /*
102f7991ba4STim Haley  * Size of block to hold the configuration data (a packed nvlist)
103f7991ba4STim Haley  */
104ad135b5dSChristopher Siden #define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
105f7991ba4STim Haley 
106fa9e4066Sahrens /*
107fa9e4066Sahrens  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
108fa9e4066Sahrens  * The ASIZE encoding should be at least 64 times larger (6 more bits)
109fa9e4066Sahrens  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
110fa9e4066Sahrens  * overhead, three DVAs per bp, plus one more bit in case we do anything
111fa9e4066Sahrens  * else that expands the ASIZE.
112fa9e4066Sahrens  */
113fa9e4066Sahrens #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
114fa9e4066Sahrens #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
115fa9e4066Sahrens #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
116fa9e4066Sahrens 
117dcbf3bd6SGeorge Wilson #define	SPA_COMPRESSBITS	7
11817f11284SSerapheim Dimitropoulos #define	SPA_VDEVBITS		24
119dcbf3bd6SGeorge Wilson 
120fa9e4066Sahrens /*
121fa9e4066Sahrens  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
122fa9e4066Sahrens  * The members of the dva_t should be considered opaque outside the SPA.
123fa9e4066Sahrens  */
124fa9e4066Sahrens typedef struct dva {
125fa9e4066Sahrens 	uint64_t	dva_word[2];
126fa9e4066Sahrens } dva_t;
127fa9e4066Sahrens 
128fa9e4066Sahrens /*
129fa9e4066Sahrens  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
130fa9e4066Sahrens  */
131fa9e4066Sahrens typedef struct zio_cksum {
132fa9e4066Sahrens 	uint64_t	zc_word[4];
133fa9e4066Sahrens } zio_cksum_t;
134fa9e4066Sahrens 
13545818ee1SMatthew Ahrens /*
13645818ee1SMatthew Ahrens  * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
13745818ee1SMatthew Ahrens  * secret and is suitable for use in MAC algorithms as the key.
13845818ee1SMatthew Ahrens  */
13945818ee1SMatthew Ahrens typedef struct zio_cksum_salt {
14045818ee1SMatthew Ahrens 	uint8_t		zcs_bytes[32];
14145818ee1SMatthew Ahrens } zio_cksum_salt_t;
14245818ee1SMatthew Ahrens 
143fa9e4066Sahrens /*
144fa9e4066Sahrens  * Each block is described by its DVAs, time of birth, checksum, etc.
145fa9e4066Sahrens  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
146fa9e4066Sahrens  *
147fa9e4066Sahrens  *	64	56	48	40	32	24	16	8	0
148fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
14917f11284SSerapheim Dimitropoulos  * 0	|  pad  |	  vdev1         | GRID  |	  ASIZE		|
150fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
151fa9e4066Sahrens  * 1	|G|			 offset1				|
152fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
15317f11284SSerapheim Dimitropoulos  * 2	|  pad  |	  vdev2         | GRID  |	  ASIZE		|
154fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
155fa9e4066Sahrens  * 3	|G|			 offset2				|
156fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
15717f11284SSerapheim Dimitropoulos  * 4	|  pad  |	  vdev3         | GRID  |	  ASIZE		|
158fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
159fa9e4066Sahrens  * 5	|G|			 offset3				|
160fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1615d7b4d43SMatthew Ahrens  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
162fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
163fa9e4066Sahrens  * 7	|			padding					|
164fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
165fa9e4066Sahrens  * 8	|			padding					|
166fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
167b24ab676SJeff Bonwick  * 9	|			physical birth txg			|
168fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
169b24ab676SJeff Bonwick  * a	|			logical birth txg			|
170fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
171fa9e4066Sahrens  * b	|			fill count				|
172fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
173fa9e4066Sahrens  * c	|			checksum[0]				|
174fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
175fa9e4066Sahrens  * d	|			checksum[1]				|
176fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
177fa9e4066Sahrens  * e	|			checksum[2]				|
178fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
179fa9e4066Sahrens  * f	|			checksum[3]				|
180fa9e4066Sahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
181fa9e4066Sahrens  *
182fa9e4066Sahrens  * Legend:
183fa9e4066Sahrens  *
184fa9e4066Sahrens  * vdev		virtual device ID
185fa9e4066Sahrens  * offset	offset into virtual device
186fa9e4066Sahrens  * LSIZE	logical size
187fa9e4066Sahrens  * PSIZE	physical size (after compression)
188fa9e4066Sahrens  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
189fa9e4066Sahrens  * GRID		RAID-Z layout information (reserved for future use)
190fa9e4066Sahrens  * cksum	checksum function
191fa9e4066Sahrens  * comp		compression function
192fa9e4066Sahrens  * G		gang block indicator
193b24ab676SJeff Bonwick  * B		byteorder (endianness)
194b24ab676SJeff Bonwick  * D		dedup
195eb633035STom Caputi  * X		encryption
1965d7b4d43SMatthew Ahrens  * E		blkptr_t contains embedded data (see below)
197fa9e4066Sahrens  * lvl		level of indirection
198b24ab676SJeff Bonwick  * type		DMU object type
1995cabbc6bSPrashanth Sreenivasa  * phys birth	txg when dva[0] was written; zero if same as logical birth txg
2005cabbc6bSPrashanth Sreenivasa  *              note that typically all the dva's would be written in this
2015cabbc6bSPrashanth Sreenivasa  *              txg, but they could be different if they were moved by
2025cabbc6bSPrashanth Sreenivasa  *              device removal.
203b24ab676SJeff Bonwick  * log. birth	transaction group in which the block was logically born
204fa9e4066Sahrens  * fill count	number of non-zero blocks under this bp
205fa9e4066Sahrens  * checksum[4]	256-bit checksum of the data this bp describes
206fa9e4066Sahrens  */
2075d7b4d43SMatthew Ahrens 
208eb633035STom Caputi /*
209eb633035STom Caputi  * The blkptr_t's of encrypted blocks also need to store the encryption
210eb633035STom Caputi  * parameters so that the block can be decrypted. This layout is as follows:
211eb633035STom Caputi  *
212eb633035STom Caputi  *	64	56	48	40	32	24	16	8	0
213eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
214eb633035STom Caputi  * 0	|		vdev1		| GRID  |	  ASIZE		|
215eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
216eb633035STom Caputi  * 1	|G|			 offset1				|
217eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
218eb633035STom Caputi  * 2	|		vdev2		| GRID  |	  ASIZE		|
219eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
220eb633035STom Caputi  * 3	|G|			 offset2				|
221eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
222eb633035STom Caputi  * 4	|			salt					|
223eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
224eb633035STom Caputi  * 5	|			IV1					|
225eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
226eb633035STom Caputi  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
227eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
228eb633035STom Caputi  * 7	|			padding					|
229eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
230eb633035STom Caputi  * 8	|			padding					|
231eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
232eb633035STom Caputi  * 9	|			physical birth txg			|
233eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
234eb633035STom Caputi  * a	|			logical birth txg			|
235eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
236eb633035STom Caputi  * b	|		IV2		|	    fill count		|
237eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
238eb633035STom Caputi  * c	|			checksum[0]				|
239eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
240eb633035STom Caputi  * d	|			checksum[1]				|
241eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
242eb633035STom Caputi  * e	|			MAC[0]					|
243eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
244eb633035STom Caputi  * f	|			MAC[1]					|
245eb633035STom Caputi  *	+-------+-------+-------+-------+-------+-------+-------+-------+
246eb633035STom Caputi  *
247eb633035STom Caputi  * Legend:
248eb633035STom Caputi  *
249eb633035STom Caputi  * salt		Salt for generating encryption keys
250eb633035STom Caputi  * IV1		First 64 bits of encryption IV
251eb633035STom Caputi  * X		Block requires encryption handling (set to 1)
252eb633035STom Caputi  * E		blkptr_t contains embedded data (set to 0, see below)
253eb633035STom Caputi  * fill count	number of non-zero blocks under this bp (truncated to 32 bits)
254eb633035STom Caputi  * IV2		Last 32 bits of encryption IV
255eb633035STom Caputi  * checksum[2]	128-bit checksum of the data this bp describes
256eb633035STom Caputi  * MAC[2]	128-bit message authentication code for this data
257eb633035STom Caputi  *
258eb633035STom Caputi  * The X bit being set indicates that this block is one of 3 types. If this is
259eb633035STom Caputi  * a level 0 block with an encrypted object type, the block is encrypted
260eb633035STom Caputi  * (see BP_IS_ENCRYPTED()). If this is a level 0 block with an unencrypted
261eb633035STom Caputi  * object type, this block is authenticated with an HMAC (see
262eb633035STom Caputi  * BP_IS_AUTHENTICATED()). Otherwise (if level > 0), this bp will use the MAC
263eb633035STom Caputi  * words to store a checksum-of-MACs from the level below (see
264eb633035STom Caputi  * BP_HAS_INDIRECT_MAC_CKSUM()). For convenience in the code, BP_IS_PROTECTED()
265eb633035STom Caputi  * refers to both encrypted and authenticated blocks and BP_USES_CRYPT()
266eb633035STom Caputi  * refers to any of these 3 kinds of blocks.
267eb633035STom Caputi  *
268eb633035STom Caputi  * The additional encryption parameters are the salt, IV, and MAC which are
269eb633035STom Caputi  * explained in greater detail in the block comment at the top of zio_crypt.c.
270eb633035STom Caputi  * The MAC occupies half of the checksum space since it serves a very similar
271eb633035STom Caputi  * purpose: to prevent data corruption on disk. The only functional difference
272eb633035STom Caputi  * is that the checksum is used to detect on-disk corruption whether or not the
273eb633035STom Caputi  * encryption key is loaded and the MAC provides additional protection against
274eb633035STom Caputi  * malicious disk tampering. We use the 3rd DVA to store the salt and first
275eb633035STom Caputi  * 64 bits of the IV. As a result encrypted blocks can only have 2 copies
276eb633035STom Caputi  * maximum instead of the normal 3. The last 32 bits of the IV are stored in
277eb633035STom Caputi  * the upper bits of what is usually the fill count. Note that only blocks at
278eb633035STom Caputi  * level 0 or -2 are ever encrypted, which allows us to guarantee that these
279eb633035STom Caputi  * 32 bits are not trampled over by other code (see zio_crypt.c for details).
280eb633035STom Caputi  * The salt and IV are not used for authenticated bps or bps with an indirect
281eb633035STom Caputi  * MAC checksum, so these blocks can utilize all 3 DVAs and the full 64 bits
282eb633035STom Caputi  * for the fill count.
283eb633035STom Caputi  */
284eb633035STom Caputi 
2855d7b4d43SMatthew Ahrens /*
2865d7b4d43SMatthew Ahrens  * "Embedded" blkptr_t's don't actually point to a block, instead they
2875d7b4d43SMatthew Ahrens  * have a data payload embedded in the blkptr_t itself.  See the comment
2885d7b4d43SMatthew Ahrens  * in blkptr.c for more details.
2895d7b4d43SMatthew Ahrens  *
2905d7b4d43SMatthew Ahrens  * The blkptr_t is laid out as follows:
2915d7b4d43SMatthew Ahrens  *
2925d7b4d43SMatthew Ahrens  *	64	56	48	40	32	24	16	8	0
2935d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2945d7b4d43SMatthew Ahrens  * 0	|      payload                                                  |
2955d7b4d43SMatthew Ahrens  * 1	|      payload                                                  |
2965d7b4d43SMatthew Ahrens  * 2	|      payload                                                  |
2975d7b4d43SMatthew Ahrens  * 3	|      payload                                                  |
2985d7b4d43SMatthew Ahrens  * 4	|      payload                                                  |
2995d7b4d43SMatthew Ahrens  * 5	|      payload                                                  |
3005d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
3015d7b4d43SMatthew Ahrens  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
3025d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
3035d7b4d43SMatthew Ahrens  * 7	|      payload                                                  |
3045d7b4d43SMatthew Ahrens  * 8	|      payload                                                  |
3055d7b4d43SMatthew Ahrens  * 9	|      payload                                                  |
3065d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
3075d7b4d43SMatthew Ahrens  * a	|			logical birth txg			|
3085d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
3095d7b4d43SMatthew Ahrens  * b	|      payload                                                  |
3105d7b4d43SMatthew Ahrens  * c	|      payload                                                  |
3115d7b4d43SMatthew Ahrens  * d	|      payload                                                  |
3125d7b4d43SMatthew Ahrens  * e	|      payload                                                  |
3135d7b4d43SMatthew Ahrens  * f	|      payload                                                  |
3145d7b4d43SMatthew Ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
3155d7b4d43SMatthew Ahrens  *
3165d7b4d43SMatthew Ahrens  * Legend:
3175d7b4d43SMatthew Ahrens  *
3185d7b4d43SMatthew Ahrens  * payload		contains the embedded data
3195d7b4d43SMatthew Ahrens  * B (byteorder)	byteorder (endianness)
3205d7b4d43SMatthew Ahrens  * D (dedup)		padding (set to zero)
3215d7b4d43SMatthew Ahrens  * X			encryption (set to zero; see above)
3225d7b4d43SMatthew Ahrens  * E (embedded)		set to one
3235d7b4d43SMatthew Ahrens  * lvl			indirection level
3245d7b4d43SMatthew Ahrens  * type			DMU object type
3255d7b4d43SMatthew Ahrens  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
3265d7b4d43SMatthew Ahrens  * comp			compression function of payload
3275d7b4d43SMatthew Ahrens  * PSIZE		size of payload after compression, in bytes
3285d7b4d43SMatthew Ahrens  * LSIZE		logical size of payload, in bytes
3295d7b4d43SMatthew Ahrens  *			note that 25 bits is enough to store the largest
3305d7b4d43SMatthew Ahrens  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
3315d7b4d43SMatthew Ahrens  * log. birth		transaction group in which the block was logically born
3325d7b4d43SMatthew Ahrens  *
3335d7b4d43SMatthew Ahrens  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
3345d7b4d43SMatthew Ahrens  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
3355d7b4d43SMatthew Ahrens  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
3365d7b4d43SMatthew Ahrens  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
3375d7b4d43SMatthew Ahrens  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
3385d7b4d43SMatthew Ahrens  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
3395d7b4d43SMatthew Ahrens  * other macros, as they assert that they are only used on BP's of the correct
340eb633035STom Caputi  * "embedded-ness". Encrypted blkptr_t's cannot be embedded because they use
341eb633035STom Caputi  * the payload space for encryption parameters (see the comment above on
342eb633035STom Caputi  * how encryption parameters are stored).
3435d7b4d43SMatthew Ahrens  */
3445d7b4d43SMatthew Ahrens 
3455d7b4d43SMatthew Ahrens #define	BPE_GET_ETYPE(bp)	\
3465d7b4d43SMatthew Ahrens 	(ASSERT(BP_IS_EMBEDDED(bp)), \
3475d7b4d43SMatthew Ahrens 	BF64_GET((bp)->blk_prop, 40, 8))
3485d7b4d43SMatthew Ahrens #define	BPE_SET_ETYPE(bp, t)	do { \
3495d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp)); \
3505d7b4d43SMatthew Ahrens 	BF64_SET((bp)->blk_prop, 40, 8, t); \
3515d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
3525d7b4d43SMatthew Ahrens 
3535d7b4d43SMatthew Ahrens #define	BPE_GET_LSIZE(bp)	\
3545d7b4d43SMatthew Ahrens 	(ASSERT(BP_IS_EMBEDDED(bp)), \
3555d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
3565d7b4d43SMatthew Ahrens #define	BPE_SET_LSIZE(bp, x)	do { \
3575d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp)); \
3585d7b4d43SMatthew Ahrens 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
3595d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
3605d7b4d43SMatthew Ahrens 
3615d7b4d43SMatthew Ahrens #define	BPE_GET_PSIZE(bp)	\
3625d7b4d43SMatthew Ahrens 	(ASSERT(BP_IS_EMBEDDED(bp)), \
3635d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
3645d7b4d43SMatthew Ahrens #define	BPE_SET_PSIZE(bp, x)	do { \
3655d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp)); \
366eb633035STom Caputi 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x);	\
3675d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
3685d7b4d43SMatthew Ahrens 
3695d7b4d43SMatthew Ahrens typedef enum bp_embedded_type {
3705d7b4d43SMatthew Ahrens 	BP_EMBEDDED_TYPE_DATA,
3715d7b4d43SMatthew Ahrens 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
3725d7b4d43SMatthew Ahrens 	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
3735d7b4d43SMatthew Ahrens } bp_embedded_type_t;
3745d7b4d43SMatthew Ahrens 
3755d7b4d43SMatthew Ahrens #define	BPE_NUM_WORDS 14
3765d7b4d43SMatthew Ahrens #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
3775d7b4d43SMatthew Ahrens #define	BPE_IS_PAYLOADWORD(bp, wp) \
3785d7b4d43SMatthew Ahrens 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
3795d7b4d43SMatthew Ahrens 
380fa9e4066Sahrens #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
381fa9e4066Sahrens #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
3826f793812SPavel Zakharov #define	SPA_SYNC_MIN_VDEVS 3		/* min vdevs to update during sync */
383fa9e4066Sahrens 
38443466aaeSMax Grossman /*
38543466aaeSMax Grossman  * A block is a hole when it has either 1) never been written to, or
38643466aaeSMax Grossman  * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
38743466aaeSMax Grossman  * without physically allocating disk space. Holes are represented in the
38843466aaeSMax Grossman  * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
38943466aaeSMax Grossman  * done through the BP_IS_HOLE macro. For holes, the logical size, level,
39043466aaeSMax Grossman  * DMU object type, and birth times are all also stored for holes that
39143466aaeSMax Grossman  * were written to at some point (i.e. were punched after having been filled).
39243466aaeSMax Grossman  */
393b24ab676SJeff Bonwick typedef struct blkptr {
394b24ab676SJeff Bonwick 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
395b24ab676SJeff Bonwick 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
396b24ab676SJeff Bonwick 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
397b24ab676SJeff Bonwick 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
398b24ab676SJeff Bonwick 	uint64_t	blk_birth;	/* transaction group at birth	    */
399b24ab676SJeff Bonwick 	uint64_t	blk_fill;	/* fill count			    */
400b24ab676SJeff Bonwick 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
401b24ab676SJeff Bonwick } blkptr_t;
402b24ab676SJeff Bonwick 
403fa9e4066Sahrens /*
404fa9e4066Sahrens  * Macros to get and set fields in a bp or DVA.
405fa9e4066Sahrens  */
406fa9e4066Sahrens #define	DVA_GET_ASIZE(dva)	\
40743466aaeSMax Grossman 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
408fa9e4066Sahrens #define	DVA_SET_ASIZE(dva, x)	\
40943466aaeSMax Grossman 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
41043466aaeSMax Grossman 	SPA_MINBLOCKSHIFT, 0, x)
411fa9e4066Sahrens 
412fa9e4066Sahrens #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
413fa9e4066Sahrens #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
414fa9e4066Sahrens 
41517f11284SSerapheim Dimitropoulos #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, SPA_VDEVBITS)
41617f11284SSerapheim Dimitropoulos #define	DVA_SET_VDEV(dva, x)	\
41717f11284SSerapheim Dimitropoulos 	BF64_SET((dva)->dva_word[0], 32, SPA_VDEVBITS, x)
418fa9e4066Sahrens 
419fa9e4066Sahrens #define	DVA_GET_OFFSET(dva)	\
420fa9e4066Sahrens 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
421fa9e4066Sahrens #define	DVA_SET_OFFSET(dva, x)	\
422fa9e4066Sahrens 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
423fa9e4066Sahrens 
424fa9e4066Sahrens #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
425fa9e4066Sahrens #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
426fa9e4066Sahrens 
427fa9e4066Sahrens #define	BP_GET_LSIZE(bp)	\
4285d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ?	\
4295d7b4d43SMatthew Ahrens 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
4305d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4315d7b4d43SMatthew Ahrens #define	BP_SET_LSIZE(bp, x)	do { \
4325d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp)); \
4335d7b4d43SMatthew Ahrens 	BF64_SET_SB((bp)->blk_prop, \
4345d7b4d43SMatthew Ahrens 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4355d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
436fa9e4066Sahrens 
437fa9e4066Sahrens #define	BP_GET_PSIZE(bp)	\
4385d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
4395d7b4d43SMatthew Ahrens 	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4405d7b4d43SMatthew Ahrens #define	BP_SET_PSIZE(bp, x)	do { \
4415d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp)); \
4425d7b4d43SMatthew Ahrens 	BF64_SET_SB((bp)->blk_prop, \
4435d7b4d43SMatthew Ahrens 	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4445d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
4455d7b4d43SMatthew Ahrens 
446dcbf3bd6SGeorge Wilson #define	BP_GET_COMPRESS(bp)		\
447dcbf3bd6SGeorge Wilson 	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
448dcbf3bd6SGeorge Wilson #define	BP_SET_COMPRESS(bp, x)		\
449dcbf3bd6SGeorge Wilson 	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
450fa9e4066Sahrens 
4515d7b4d43SMatthew Ahrens #define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
4525d7b4d43SMatthew Ahrens #define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
453b24ab676SJeff Bonwick 
4545d7b4d43SMatthew Ahrens #define	BP_GET_CHECKSUM(bp)		\
4555d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
4565d7b4d43SMatthew Ahrens 	BF64_GET((bp)->blk_prop, 40, 8))
4575d7b4d43SMatthew Ahrens #define	BP_SET_CHECKSUM(bp, x)		do { \
4585d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp)); \
4595d7b4d43SMatthew Ahrens 	BF64_SET((bp)->blk_prop, 40, 8, x); \
4605d7b4d43SMatthew Ahrens _NOTE(CONSTCOND) } while (0)
461b24ab676SJeff Bonwick 
462b24ab676SJeff Bonwick #define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
463b24ab676SJeff Bonwick #define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
464fa9e4066Sahrens 
465b24ab676SJeff Bonwick #define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
466b24ab676SJeff Bonwick #define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
467fa9e4066Sahrens 
468eb633035STom Caputi /* encrypted, authenticated, and MAC cksum bps use the same bit */
469eb633035STom Caputi #define	BP_USES_CRYPT(bp)		BF64_GET((bp)->blk_prop, 61, 1)
470eb633035STom Caputi #define	BP_SET_CRYPT(bp, x)		BF64_SET((bp)->blk_prop, 61, 1, x)
471eb633035STom Caputi 
472eb633035STom Caputi #define	BP_IS_ENCRYPTED(bp)			\
473eb633035STom Caputi 	(BP_USES_CRYPT(bp) &&			\
474eb633035STom Caputi 	BP_GET_LEVEL(bp) == 0 &&		\
475eb633035STom Caputi 	DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
476eb633035STom Caputi 
477eb633035STom Caputi #define	BP_IS_AUTHENTICATED(bp)			\
478eb633035STom Caputi 	(BP_USES_CRYPT(bp) &&			\
479eb633035STom Caputi 	BP_GET_LEVEL(bp) == 0 &&		\
480eb633035STom Caputi 	!DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
481eb633035STom Caputi 
482eb633035STom Caputi #define	BP_HAS_INDIRECT_MAC_CKSUM(bp)		\
483eb633035STom Caputi 	(BP_USES_CRYPT(bp) && BP_GET_LEVEL(bp) > 0)
484eb633035STom Caputi 
485eb633035STom Caputi #define	BP_IS_PROTECTED(bp)			\
486eb633035STom Caputi 	(BP_IS_ENCRYPTED(bp) || BP_IS_AUTHENTICATED(bp))
487eb633035STom Caputi 
488b24ab676SJeff Bonwick #define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
489b24ab676SJeff Bonwick #define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
490fa9e4066Sahrens 
49143466aaeSMax Grossman #define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
492b24ab676SJeff Bonwick #define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
493b24ab676SJeff Bonwick 
494b24ab676SJeff Bonwick #define	BP_PHYSICAL_BIRTH(bp)		\
4955d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
4965d7b4d43SMatthew Ahrens 	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
497b24ab676SJeff Bonwick 
498b24ab676SJeff Bonwick #define	BP_SET_BIRTH(bp, logical, physical)	\
499b24ab676SJeff Bonwick {						\
5005d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));		\
501b24ab676SJeff Bonwick 	(bp)->blk_birth = (logical);		\
502b24ab676SJeff Bonwick 	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
503b24ab676SJeff Bonwick }
504fa9e4066Sahrens 
505eb633035STom Caputi #define	BP_GET_FILL(bp)				\
506eb633035STom Caputi 	((BP_IS_ENCRYPTED(bp)) ? BF64_GET((bp)->blk_fill, 0, 32) : \
507eb633035STom Caputi 	((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill))
508eb633035STom Caputi 
509eb633035STom Caputi #define	BP_SET_FILL(bp, fill)			\
510eb633035STom Caputi {						\
511eb633035STom Caputi 	if (BP_IS_ENCRYPTED(bp))			\
512eb633035STom Caputi 		BF64_SET((bp)->blk_fill, 0, 32, fill); \
513eb633035STom Caputi 	else					\
514eb633035STom Caputi 		(bp)->blk_fill = fill;		\
515eb633035STom Caputi }
516eb633035STom Caputi 
517eb633035STom Caputi #define	BP_GET_IV2(bp)				\
518eb633035STom Caputi 	(ASSERT(BP_IS_ENCRYPTED(bp)),		\
519eb633035STom Caputi 	BF64_GET((bp)->blk_fill, 32, 32))
520eb633035STom Caputi #define	BP_SET_IV2(bp, iv2)			\
521eb633035STom Caputi {						\
522eb633035STom Caputi 	ASSERT(BP_IS_ENCRYPTED(bp));		\
523eb633035STom Caputi 	BF64_SET((bp)->blk_fill, 32, 32, iv2);	\
524eb633035STom Caputi }
5255d7b4d43SMatthew Ahrens 
526770499e1SDan Kimmel #define	BP_IS_METADATA(bp)	\
527770499e1SDan Kimmel 	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
528770499e1SDan Kimmel 
529fa9e4066Sahrens #define	BP_GET_ASIZE(bp)	\
5305d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
5315d7b4d43SMatthew Ahrens 	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
5325d7b4d43SMatthew Ahrens 	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
533eb633035STom Caputi 	(DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
53499653d4eSeschrock 
535770499e1SDan Kimmel #define	BP_GET_UCSIZE(bp)	\
536770499e1SDan Kimmel 	(BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
537fa9e4066Sahrens 
53844cd46caSbillm #define	BP_GET_NDVAS(bp)	\
5395d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
5405d7b4d43SMatthew Ahrens 	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
54144cd46caSbillm 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
542eb633035STom Caputi 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
54344cd46caSbillm 
54444cd46caSbillm #define	BP_COUNT_GANG(bp)	\
5455d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? 0 : \
54644cd46caSbillm 	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
54744cd46caSbillm 	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
548eb633035STom Caputi 	(DVA_GET_GANG(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp))))
54944cd46caSbillm 
550fa9e4066Sahrens #define	DVA_EQUAL(dva1, dva2)	\
551fa9e4066Sahrens 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
552fa9e4066Sahrens 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
553fa9e4066Sahrens 
554b24ab676SJeff Bonwick #define	BP_EQUAL(bp1, bp2)	\
555b24ab676SJeff Bonwick 	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
5565d7b4d43SMatthew Ahrens 	(bp1)->blk_birth == (bp2)->blk_birth &&			\
557b24ab676SJeff Bonwick 	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
558b24ab676SJeff Bonwick 	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
559b24ab676SJeff Bonwick 	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
560b24ab676SJeff Bonwick 
5616b4acc8bSahrens #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
5626b4acc8bSahrens 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
5636b4acc8bSahrens 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
5646b4acc8bSahrens 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
5656b4acc8bSahrens 	((zc1).zc_word[3] - (zc2).zc_word[3])))
5666b4acc8bSahrens 
567eb633035STom Caputi #define	ZIO_CHECKSUM_MAC_EQUAL(zc1, zc2) \
568eb633035STom Caputi 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
569eb633035STom Caputi 	((zc1).zc_word[1] - (zc2).zc_word[1])))
570eb633035STom Caputi 
57198110f08SMatthew Ahrens #define	ZIO_CHECKSUM_IS_ZERO(zc) \
57298110f08SMatthew Ahrens 	(0 == ((zc)->zc_word[0] | (zc)->zc_word[1] | \
57398110f08SMatthew Ahrens 	(zc)->zc_word[2] | (zc)->zc_word[3]))
57498110f08SMatthew Ahrens 
57598110f08SMatthew Ahrens #define	ZIO_CHECKSUM_BSWAP(zcp)					\
57698110f08SMatthew Ahrens {								\
57798110f08SMatthew Ahrens 	(zcp)->zc_word[0] = BSWAP_64((zcp)->zc_word[0]);	\
57898110f08SMatthew Ahrens 	(zcp)->zc_word[1] = BSWAP_64((zcp)->zc_word[1]);	\
57998110f08SMatthew Ahrens 	(zcp)->zc_word[2] = BSWAP_64((zcp)->zc_word[2]);	\
58098110f08SMatthew Ahrens 	(zcp)->zc_word[3] = BSWAP_64((zcp)->zc_word[3]);	\
58198110f08SMatthew Ahrens }
58298110f08SMatthew Ahrens 
58398110f08SMatthew Ahrens 
584fa9e4066Sahrens #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
585fa9e4066Sahrens 
586fa9e4066Sahrens #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
587fa9e4066Sahrens {						\
588fa9e4066Sahrens 	(zcp)->zc_word[0] = w0;			\
589fa9e4066Sahrens 	(zcp)->zc_word[1] = w1;			\
590fa9e4066Sahrens 	(zcp)->zc_word[2] = w2;			\
591fa9e4066Sahrens 	(zcp)->zc_word[3] = w3;			\
592fa9e4066Sahrens }
593fa9e4066Sahrens 
5945d7b4d43SMatthew Ahrens #define	BP_IDENTITY(bp)		(ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
5955d7b4d43SMatthew Ahrens #define	BP_IS_GANG(bp)		\
5965d7b4d43SMatthew Ahrens 	(BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
59743466aaeSMax Grossman #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&	\
59843466aaeSMax Grossman 				(dva)->dva_word[1] == 0ULL)
5995d7b4d43SMatthew Ahrens #define	BP_IS_HOLE(bp) \
6005d7b4d43SMatthew Ahrens 	(!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
601fa9e4066Sahrens 
6026e1f5caaSNeil Perrin /* BP_IS_RAIDZ(bp) assumes no block compression */
6036e1f5caaSNeil Perrin #define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
6046e1f5caaSNeil Perrin 				BP_GET_PSIZE(bp))
6056e1f5caaSNeil Perrin 
606e14bb325SJeff Bonwick #define	BP_ZERO(bp)				\
607fa9e4066Sahrens {						\
608fa9e4066Sahrens 	(bp)->blk_dva[0].dva_word[0] = 0;	\
609fa9e4066Sahrens 	(bp)->blk_dva[0].dva_word[1] = 0;	\
610fa9e4066Sahrens 	(bp)->blk_dva[1].dva_word[0] = 0;	\
611fa9e4066Sahrens 	(bp)->blk_dva[1].dva_word[1] = 0;	\
612fa9e4066Sahrens 	(bp)->blk_dva[2].dva_word[0] = 0;	\
613fa9e4066Sahrens 	(bp)->blk_dva[2].dva_word[1] = 0;	\
614fa9e4066Sahrens 	(bp)->blk_prop = 0;			\
615fa9e4066Sahrens 	(bp)->blk_pad[0] = 0;			\
616fa9e4066Sahrens 	(bp)->blk_pad[1] = 0;			\
617b24ab676SJeff Bonwick 	(bp)->blk_phys_birth = 0;		\
618e14bb325SJeff Bonwick 	(bp)->blk_birth = 0;			\
619fa9e4066Sahrens 	(bp)->blk_fill = 0;			\
620fa9e4066Sahrens 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
621fa9e4066Sahrens }
622fa9e4066Sahrens 
623fa9e4066Sahrens #ifdef _BIG_ENDIAN
624fa9e4066Sahrens #define	ZFS_HOST_BYTEORDER	(0ULL)
625fa9e4066Sahrens #else
62643466aaeSMax Grossman #define	ZFS_HOST_BYTEORDER	(1ULL)
627fa9e4066Sahrens #endif
628fa9e4066Sahrens 
629fa9e4066Sahrens #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
630fa9e4066Sahrens 
631eb633035STom Caputi #define	BP_SPRINTF_LEN	400
632fbabab8fSmaybee 
633b24ab676SJeff Bonwick /*
634b24ab676SJeff Bonwick  * This macro allows code sharing between zfs, libzpool, and mdb.
635b24ab676SJeff Bonwick  * 'func' is either snprintf() or mdb_snprintf().
636b24ab676SJeff Bonwick  * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
637b24ab676SJeff Bonwick  */
63843466aaeSMax Grossman #define	SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
639b24ab676SJeff Bonwick {									\
640b24ab676SJeff Bonwick 	static const char *copyname[] =					\
641b24ab676SJeff Bonwick 	    { "zero", "single", "double", "triple" };			\
642b24ab676SJeff Bonwick 	int len = 0;							\
643b24ab676SJeff Bonwick 	int copies = 0;							\
644eb633035STom Caputi 	const char *crypt_type;						\
645eb633035STom Caputi 	if (bp != NULL) {						\
646eb633035STom Caputi 		if (BP_IS_ENCRYPTED(bp)) {				\
647eb633035STom Caputi 			crypt_type = "encrypted";			\
648eb633035STom Caputi 		} else if (BP_IS_AUTHENTICATED(bp)) {			\
649eb633035STom Caputi 			crypt_type = "authenticated";			\
650eb633035STom Caputi 		} else if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {		\
651eb633035STom Caputi 			crypt_type = "indirect-MAC";			\
652eb633035STom Caputi 		} else {						\
653eb633035STom Caputi 			crypt_type = "unencrypted";			\
654eb633035STom Caputi 		}							\
655eb633035STom Caputi 	}								\
656b24ab676SJeff Bonwick 	if (bp == NULL) {						\
65743466aaeSMax Grossman 		len += func(buf + len, size - len, "<NULL>");		\
658b24ab676SJeff Bonwick 	} else if (BP_IS_HOLE(bp)) {					\
65970163ac5SPrakash Surya 		len += func(buf + len, size - len,			\
66070163ac5SPrakash Surya 		    "HOLE [L%llu %s] "					\
66170163ac5SPrakash Surya 		    "size=%llxL birth=%lluL",				\
66270163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
66370163ac5SPrakash Surya 		    type,						\
66470163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
66570163ac5SPrakash Surya 		    (u_longlong_t)bp->blk_birth);			\
6665d7b4d43SMatthew Ahrens 	} else if (BP_IS_EMBEDDED(bp)) {				\
6675d7b4d43SMatthew Ahrens 		len = func(buf + len, size - len,			\
6685d7b4d43SMatthew Ahrens 		    "EMBEDDED [L%llu %s] et=%u %s "			\
6695d7b4d43SMatthew Ahrens 		    "size=%llxL/%llxP birth=%lluL",			\
6705d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
6715d7b4d43SMatthew Ahrens 		    type,						\
6725d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),				\
6735d7b4d43SMatthew Ahrens 		    compress,						\
6745d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),			\
6755d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),			\
6765d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);			\
677b24ab676SJeff Bonwick 	} else {							\
678b24ab676SJeff Bonwick 		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
679b24ab676SJeff Bonwick 			const dva_t *dva = &bp->blk_dva[d];		\
680b24ab676SJeff Bonwick 			if (DVA_IS_VALID(dva))				\
681b24ab676SJeff Bonwick 				copies++;				\
682b24ab676SJeff Bonwick 			len += func(buf + len, size - len,		\
683b24ab676SJeff Bonwick 			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
684b24ab676SJeff Bonwick 			    (u_longlong_t)DVA_GET_VDEV(dva),		\
685b24ab676SJeff Bonwick 			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
686b24ab676SJeff Bonwick 			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
687b24ab676SJeff Bonwick 			    ws);					\
688b24ab676SJeff Bonwick 		}							\
689eb633035STom Caputi 		if (BP_IS_ENCRYPTED(bp)) {				\
690eb633035STom Caputi 			len += func(buf + len, size - len,		\
691eb633035STom Caputi 			    "salt=%llx iv=%llx:%llx%c",			\
692eb633035STom Caputi 			    (u_longlong_t)bp->blk_dva[2].dva_word[0],	\
693eb633035STom Caputi 			    (u_longlong_t)bp->blk_dva[2].dva_word[1],	\
694eb633035STom Caputi 			    (u_longlong_t)BP_GET_IV2(bp),		\
695eb633035STom Caputi 			    ws);					\
696eb633035STom Caputi 		}							\
697b24ab676SJeff Bonwick 		if (BP_IS_GANG(bp) &&					\
698b24ab676SJeff Bonwick 		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
699b24ab676SJeff Bonwick 		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
700b24ab676SJeff Bonwick 			copies--;					\
701b24ab676SJeff Bonwick 		len += func(buf + len, size - len,			\
702eb633035STom Caputi 		    "[L%llu %s] %s %s %s %s %s %s %s%c"			\
703b24ab676SJeff Bonwick 		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
704b24ab676SJeff Bonwick 		    "cksum=%llx:%llx:%llx:%llx",			\
705b24ab676SJeff Bonwick 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
706b24ab676SJeff Bonwick 		    type,						\
707b24ab676SJeff Bonwick 		    checksum,						\
708b24ab676SJeff Bonwick 		    compress,						\
709eb633035STom Caputi 		    crypt_type,						\
710b24ab676SJeff Bonwick 		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
711b24ab676SJeff Bonwick 		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
712b24ab676SJeff Bonwick 		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
713b24ab676SJeff Bonwick 		    copyname[copies],					\
714b24ab676SJeff Bonwick 		    ws,							\
715b24ab676SJeff Bonwick 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
716b24ab676SJeff Bonwick 		    (u_longlong_t)BP_GET_PSIZE(bp),			\
717b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_birth,			\
718b24ab676SJeff Bonwick 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp),		\
7195d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),			\
720b24ab676SJeff Bonwick 		    ws,							\
721b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
722b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
723b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
724b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
725b24ab676SJeff Bonwick 	}								\
726b24ab676SJeff Bonwick 	ASSERT(len < size);						\
727b24ab676SJeff Bonwick }
728b24ab676SJeff Bonwick 
729ad23a2dbSjohansen #define	BP_GET_BUFC_TYPE(bp)						\
730770499e1SDan Kimmel 	(BP_IS_METADATA(bp) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
731fa9e4066Sahrens 
7321195e687SMark J Musante typedef enum spa_import_type {
7331195e687SMark J Musante 	SPA_IMPORT_EXISTING,
7341195e687SMark J Musante 	SPA_IMPORT_ASSEMBLE
7351195e687SMark J Musante } spa_import_type_t;
7361195e687SMark J Musante 
737084fd14fSBrian Behlendorf /*
738084fd14fSBrian Behlendorf  * Send TRIM commands in-line during normal pool operation while deleting.
739084fd14fSBrian Behlendorf  *	OFF: no
740084fd14fSBrian Behlendorf  *	ON: yes
741084fd14fSBrian Behlendorf  */
742084fd14fSBrian Behlendorf typedef enum {
743084fd14fSBrian Behlendorf 	SPA_AUTOTRIM_OFF = 0,	/* default */
744084fd14fSBrian Behlendorf 	SPA_AUTOTRIM_ON
745084fd14fSBrian Behlendorf } spa_autotrim_t;
746084fd14fSBrian Behlendorf 
747084fd14fSBrian Behlendorf /*
748084fd14fSBrian Behlendorf  * Reason TRIM command was issued, used internally for accounting purposes.
749084fd14fSBrian Behlendorf  */
750084fd14fSBrian Behlendorf typedef enum trim_type {
751084fd14fSBrian Behlendorf 	TRIM_TYPE_MANUAL = 0,
752084fd14fSBrian Behlendorf 	TRIM_TYPE_AUTO = 1,
753084fd14fSBrian Behlendorf } trim_type_t;
754084fd14fSBrian Behlendorf 
755fa9e4066Sahrens /* state manipulation functions */
756fa9e4066Sahrens extern int spa_open(const char *pool, spa_t **, void *tag);
757468c413aSTim Haley extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
758468c413aSTim Haley     nvlist_t *policy, nvlist_t **config);
759ad135b5dSChristopher Siden extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
760ad135b5dSChristopher Siden     size_t buflen);
761eb633035STom Caputi extern int spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
762eb633035STom Caputi     nvlist_t *zplprops, struct dsl_crypto_params *dcp);
76330c304d9SJoshua M. Clulow extern int spa_import_rootpool(char *devpath, char *devid, uint64_t pool_guid,
76430c304d9SJoshua M. Clulow     uint64_t vdev_guid);
7654b964adaSGeorge Wilson extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props,
7664b964adaSGeorge Wilson     uint64_t flags);
767fa9e4066Sahrens extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
768fa9e4066Sahrens extern int spa_destroy(char *pool);
76986714001SSerapheim Dimitropoulos extern int spa_checkpoint(const char *pool);
77086714001SSerapheim Dimitropoulos extern int spa_checkpoint_discard(const char *pool);
771394ab0cbSGeorge Wilson extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
772394ab0cbSGeorge Wilson     boolean_t hardforce);
773ea8dc4b6Seschrock extern int spa_reset(char *pool);
774ea8dc4b6Seschrock extern void spa_async_request(spa_t *spa, int flag);
775088f3894Sahrens extern void spa_async_unrequest(spa_t *spa, int flag);
776ea8dc4b6Seschrock extern void spa_async_suspend(spa_t *spa);
777ea8dc4b6Seschrock extern void spa_async_resume(spa_t *spa);
7780c06d385Sjwpoduska extern int spa_async_tasks(spa_t *spa);
779ea8dc4b6Seschrock extern spa_t *spa_inject_addref(char *pool);
780ea8dc4b6Seschrock extern void spa_inject_delref(spa_t *spa);
7813f9d6ad7SLin Ling extern void spa_scan_stat_init(spa_t *spa);
7823f9d6ad7SLin Ling extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
783ea8dc4b6Seschrock 
784084fd14fSBrian Behlendorf #define	SPA_ASYNC_CONFIG_UPDATE			0x01
785084fd14fSBrian Behlendorf #define	SPA_ASYNC_REMOVE			0x02
786084fd14fSBrian Behlendorf #define	SPA_ASYNC_PROBE				0x04
787084fd14fSBrian Behlendorf #define	SPA_ASYNC_RESILVER_DONE			0x08
788084fd14fSBrian Behlendorf #define	SPA_ASYNC_RESILVER			0x10
789084fd14fSBrian Behlendorf #define	SPA_ASYNC_AUTOEXPAND			0x20
790084fd14fSBrian Behlendorf #define	SPA_ASYNC_REMOVE_DONE			0x40
791084fd14fSBrian Behlendorf #define	SPA_ASYNC_REMOVE_STOP			0x80
792084fd14fSBrian Behlendorf #define	SPA_ASYNC_INITIALIZE_RESTART		0x100
793084fd14fSBrian Behlendorf #define	SPA_ASYNC_TRIM_RESTART			0x200
794084fd14fSBrian Behlendorf #define	SPA_ASYNC_AUTOTRIM_RESTART		0x400
795f0a05239SGeorge Amanakis #define	SPA_ASYNC_L2CACHE_REBUILD		0x800
7963f9d6ad7SLin Ling 
7973f9d6ad7SLin Ling /*
7983f9d6ad7SLin Ling  * Controls the behavior of spa_vdev_remove().
7993f9d6ad7SLin Ling  */
8003f9d6ad7SLin Ling #define	SPA_REMOVE_UNSPARE	0x01
8013f9d6ad7SLin Ling #define	SPA_REMOVE_DONE		0x02
802fa9e4066Sahrens 
803fa9e4066Sahrens /* device manipulation */
804fa9e4066Sahrens extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
805ea8dc4b6Seschrock extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
806fa9e4066Sahrens     int replacing);
8078ad4d6ddSJeff Bonwick extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
8088ad4d6ddSJeff Bonwick     int replace_done);
80999653d4eSeschrock extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
8103f9d6ad7SLin Ling extern boolean_t spa_vdev_remove_active(spa_t *spa);
811084fd14fSBrian Behlendorf extern int spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type,
812084fd14fSBrian Behlendorf     nvlist_t *vdev_errlist);
813084fd14fSBrian Behlendorf extern int spa_vdev_trim(spa_t *spa, nvlist_t *nv, uint64_t cmd_type,
814084fd14fSBrian Behlendorf     uint64_t rate, boolean_t partial, boolean_t secure, nvlist_t *vdev_errlist);
815c67d9675Seschrock extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
8166809eb4eSEric Schrock extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
8171195e687SMark J Musante extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
8181195e687SMark J Musante     nvlist_t *props, boolean_t exp);
819fa9e4066Sahrens 
82099653d4eSeschrock /* spare state (which is global across all pools) */
82139c23413Seschrock extern void spa_spare_add(vdev_t *vd);
82239c23413Seschrock extern void spa_spare_remove(vdev_t *vd);
82389a89ebfSlling extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
82439c23413Seschrock extern void spa_spare_activate(vdev_t *vd);
82599653d4eSeschrock 
826e830fb12SKody A Kantor /* spare polling */
827e830fb12SKody A Kantor extern void spa_spare_poll(spa_t *spa);
828e830fb12SKody A Kantor 
829fa94a07fSbrendan /* L2ARC state (which is global across all pools) */
830fa94a07fSbrendan extern void spa_l2cache_add(vdev_t *vd);
831fa94a07fSbrendan extern void spa_l2cache_remove(vdev_t *vd);
832fa94a07fSbrendan extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
833fa94a07fSbrendan extern void spa_l2cache_activate(vdev_t *vd);
834fa94a07fSbrendan extern void spa_l2cache_drop(spa_t *spa);
835fa94a07fSbrendan 
8363f9d6ad7SLin Ling /* scanning */
8373f9d6ad7SLin Ling extern int spa_scan(spa_t *spa, pool_scan_func_t func);
8383f9d6ad7SLin Ling extern int spa_scan_stop(spa_t *spa);
8391702cce7SAlek Pinchuk extern int spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t flag);
840fa9e4066Sahrens 
841fa9e4066Sahrens /* spa syncing */
842fa9e4066Sahrens extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
843fa9e4066Sahrens extern void spa_sync_allpools(void);
844fa9e4066Sahrens 
8453a737e0dSbrendan /* spa namespace global mutex */
8463a737e0dSbrendan extern kmutex_t spa_namespace_lock;
8473a737e0dSbrendan 
848fa9e4066Sahrens /*
849fa9e4066Sahrens  * SPA configuration functions in spa_config.c
850fa9e4066Sahrens  */
8510373e76bSbonwick 
8520373e76bSbonwick #define	SPA_CONFIG_UPDATE_POOL	0
8530373e76bSbonwick #define	SPA_CONFIG_UPDATE_VDEVS	1
8540373e76bSbonwick 
8555cabbc6bSPrashanth Sreenivasa extern void spa_write_cachefile(spa_t *, boolean_t, boolean_t);
856fa9e4066Sahrens extern void spa_config_load(void);
857fa9e4066Sahrens extern nvlist_t *spa_all_configs(uint64_t *);
858fa9e4066Sahrens extern void spa_config_set(spa_t *spa, nvlist_t *config);
859fa9e4066Sahrens extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
860fa9e4066Sahrens     int getstats);
8610373e76bSbonwick extern void spa_config_update(spa_t *spa, int what);
862fa9e4066Sahrens 
863fa9e4066Sahrens /*
864fa9e4066Sahrens  * Miscellaneous SPA routines in spa_misc.c
865fa9e4066Sahrens  */
866fa9e4066Sahrens 
867fa9e4066Sahrens /* Namespace manipulation */
868fa9e4066Sahrens extern spa_t *spa_lookup(const char *name);
869468c413aSTim Haley extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
870fa9e4066Sahrens extern void spa_remove(spa_t *spa);
871fa9e4066Sahrens extern spa_t *spa_next(spa_t *prev);
872fa9e4066Sahrens 
873fa9e4066Sahrens /* Refcount functions */
874fa9e4066Sahrens extern void spa_open_ref(spa_t *spa, void *tag);
875fa9e4066Sahrens extern void spa_close(spa_t *spa, void *tag);
876bc9014e6SJustin Gibbs extern void spa_async_close(spa_t *spa, void *tag);
877fa9e4066Sahrens extern boolean_t spa_refcount_zero(spa_t *spa);
878fa9e4066Sahrens 
8798f18d1faSGeorge Wilson #define	SCL_NONE	0x00
880e14bb325SJeff Bonwick #define	SCL_CONFIG	0x01
881e14bb325SJeff Bonwick #define	SCL_STATE	0x02
882e14bb325SJeff Bonwick #define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
883e14bb325SJeff Bonwick #define	SCL_ALLOC	0x08
884e14bb325SJeff Bonwick #define	SCL_ZIO		0x10
885e14bb325SJeff Bonwick #define	SCL_FREE	0x20
886e14bb325SJeff Bonwick #define	SCL_VDEV	0x40
887e14bb325SJeff Bonwick #define	SCL_LOCKS	7
888e14bb325SJeff Bonwick #define	SCL_ALL		((1 << SCL_LOCKS) - 1)
889e14bb325SJeff Bonwick #define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
890e14bb325SJeff Bonwick 
891084fd14fSBrian Behlendorf /* Assorted pool IO kstats */
892084fd14fSBrian Behlendorf typedef struct spa_iostats {
893084fd14fSBrian Behlendorf 	kstat_named_t	trim_extents_written;
894084fd14fSBrian Behlendorf 	kstat_named_t	trim_bytes_written;
895084fd14fSBrian Behlendorf 	kstat_named_t	trim_extents_skipped;
896084fd14fSBrian Behlendorf 	kstat_named_t	trim_bytes_skipped;
897084fd14fSBrian Behlendorf 	kstat_named_t	trim_extents_failed;
898084fd14fSBrian Behlendorf 	kstat_named_t	trim_bytes_failed;
899084fd14fSBrian Behlendorf 	kstat_named_t	autotrim_extents_written;
900084fd14fSBrian Behlendorf 	kstat_named_t	autotrim_bytes_written;
901084fd14fSBrian Behlendorf 	kstat_named_t	autotrim_extents_skipped;
902084fd14fSBrian Behlendorf 	kstat_named_t	autotrim_bytes_skipped;
903084fd14fSBrian Behlendorf 	kstat_named_t	autotrim_extents_failed;
904084fd14fSBrian Behlendorf 	kstat_named_t	autotrim_bytes_failed;
905084fd14fSBrian Behlendorf } spa_iostats_t;
906084fd14fSBrian Behlendorf 
907dd50e0ccSTony Hutter extern int spa_import_progress_set_state(spa_t *, spa_load_state_t);
908dd50e0ccSTony Hutter extern int spa_import_progress_set_max_txg(spa_t *, uint64_t);
909dd50e0ccSTony Hutter extern int spa_import_progress_set_mmp_check(spa_t *, uint64_t);
910dd50e0ccSTony Hutter extern void spa_import_progress_add(spa_t *);
911dd50e0ccSTony Hutter extern void spa_import_progress_remove(spa_t *);
912dd50e0ccSTony Hutter 
913e14bb325SJeff Bonwick /* Pool configuration locks */
914e14bb325SJeff Bonwick extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
915e14bb325SJeff Bonwick extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
916e14bb325SJeff Bonwick extern void spa_config_exit(spa_t *spa, int locks, void *tag);
917e14bb325SJeff Bonwick extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
918fa9e4066Sahrens 
919fa9e4066Sahrens /* Pool vdev add/remove lock */
920fa9e4066Sahrens extern uint64_t spa_vdev_enter(spa_t *spa);
92188ecc943SGeorge Wilson extern uint64_t spa_vdev_config_enter(spa_t *spa);
92288ecc943SGeorge Wilson extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
92388ecc943SGeorge Wilson     int error, char *tag);
924fa9e4066Sahrens extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
925fa9e4066Sahrens 
926e14bb325SJeff Bonwick /* Pool vdev state change lock */
9278f18d1faSGeorge Wilson extern void spa_vdev_state_enter(spa_t *spa, int oplock);
928e14bb325SJeff Bonwick extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
929e14bb325SJeff Bonwick 
930b24ab676SJeff Bonwick /* Log state */
931b24ab676SJeff Bonwick typedef enum spa_log_state {
932b24ab676SJeff Bonwick 	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
933b24ab676SJeff Bonwick 	SPA_LOG_MISSING,	/* missing log(s) */
934b24ab676SJeff Bonwick 	SPA_LOG_CLEAR,		/* clear the log(s) */
935b24ab676SJeff Bonwick 	SPA_LOG_GOOD,		/* log(s) are good */
936b24ab676SJeff Bonwick } spa_log_state_t;
937b24ab676SJeff Bonwick 
938b24ab676SJeff Bonwick extern spa_log_state_t spa_get_log_state(spa_t *spa);
939b24ab676SJeff Bonwick extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
9405cabbc6bSPrashanth Sreenivasa extern int spa_reset_logs(spa_t *spa);
941b24ab676SJeff Bonwick 
942b24ab676SJeff Bonwick /* Log claim callback */
943b24ab676SJeff Bonwick extern void spa_claim_notify(zio_t *zio);
944b24ab676SJeff Bonwick 
945fa9e4066Sahrens /* Accessor functions */
94688b7b0f2SMatthew Ahrens extern boolean_t spa_shutting_down(spa_t *spa);
947fa9e4066Sahrens extern struct dsl_pool *spa_get_dsl(spa_t *spa);
948ad135b5dSChristopher Siden extern boolean_t spa_is_initializing(spa_t *spa);
9495cabbc6bSPrashanth Sreenivasa extern boolean_t spa_indirect_vdevs_loaded(spa_t *spa);
950fa9e4066Sahrens extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
951fa9e4066Sahrens extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
952fa9e4066Sahrens extern void spa_altroot(spa_t *, char *, size_t);
953fa9e4066Sahrens extern int spa_sync_pass(spa_t *spa);
954fa9e4066Sahrens extern char *spa_name(spa_t *spa);
955fa9e4066Sahrens extern uint64_t spa_guid(spa_t *spa);
956e9103aaeSGarrett D'Amore extern uint64_t spa_load_guid(spa_t *spa);
957fa9e4066Sahrens extern uint64_t spa_last_synced_txg(spa_t *spa);
958fa9e4066Sahrens extern uint64_t spa_first_txg(spa_t *spa);
959b24ab676SJeff Bonwick extern uint64_t spa_syncing_txg(spa_t *spa);
9603991b535SGeorge Wilson extern uint64_t spa_final_dirty_txg(spa_t *spa);
96199653d4eSeschrock extern uint64_t spa_version(spa_t *spa);
96288b7b0f2SMatthew Ahrens extern pool_state_t spa_state(spa_t *spa);
963b16da2e2SGeorge Wilson extern spa_load_state_t spa_load_state(spa_t *spa);
964fa9e4066Sahrens extern uint64_t spa_freeze_txg(spa_t *spa);
96561e255ceSMatthew Ahrens extern uint64_t spa_get_worst_case_asize(spa_t *spa, uint64_t lsize);
966485bbbf5SGeorge Wilson extern uint64_t spa_get_dspace(spa_t *spa);
96786714001SSerapheim Dimitropoulos extern uint64_t spa_get_checkpoint_space(spa_t *spa);
968c39f2c8cSChristopher Siden extern uint64_t spa_get_slop_space(spa_t *spa);
969485bbbf5SGeorge Wilson extern void spa_update_dspace(spa_t *spa);
97044cd46caSbillm extern uint64_t spa_version(spa_t *spa);
971b24ab676SJeff Bonwick extern boolean_t spa_deflate(spa_t *spa);
972b24ab676SJeff Bonwick extern metaslab_class_t *spa_normal_class(spa_t *spa);
973b24ab676SJeff Bonwick extern metaslab_class_t *spa_log_class(spa_t *spa);
974663207adSDon Brady extern metaslab_class_t *spa_special_class(spa_t *spa);
975663207adSDon Brady extern metaslab_class_t *spa_dedup_class(spa_t *spa);
976663207adSDon Brady extern metaslab_class_t *spa_preferred_class(spa_t *spa, uint64_t size,
977663207adSDon Brady     dmu_object_type_t objtype, uint_t level, uint_t special_smallblk);
978663207adSDon Brady 
979bc9014e6SJustin Gibbs extern void spa_evicting_os_register(spa_t *, objset_t *os);
980bc9014e6SJustin Gibbs extern void spa_evicting_os_deregister(spa_t *, objset_t *os);
981bc9014e6SJustin Gibbs extern void spa_evicting_os_wait(spa_t *spa);
98244cd46caSbillm extern int spa_max_replication(spa_t *spa);
9833f9d6ad7SLin Ling extern int spa_prev_software_version(spa_t *spa);
984fa9e4066Sahrens extern int spa_busy(void);
9850a4e9518Sgw extern uint8_t spa_get_failmode(spa_t *spa);
986e14bb325SJeff Bonwick extern boolean_t spa_suspended(spa_t *spa);
987b24ab676SJeff Bonwick extern uint64_t spa_bootfs(spa_t *spa);
988b24ab676SJeff Bonwick extern uint64_t spa_delegation(spa_t *spa);
989b24ab676SJeff Bonwick extern objset_t *spa_meta_objset(spa_t *spa);
990814dcd43SSerapheim Dimitropoulos extern space_map_t *spa_syncing_log_sm(spa_t *spa);
991283b8460SGeorge.Wilson extern uint64_t spa_deadman_synctime(spa_t *spa);
992abe1fd01SDon Brady extern uint64_t spa_dirty_data(spa_t *spa);
993084fd14fSBrian Behlendorf extern spa_autotrim_t spa_get_autotrim(spa_t *spa);
994fa9e4066Sahrens 
995fa9e4066Sahrens /* Miscellaneous support routines */
9963ee8c80cSPavel Zakharov extern void spa_load_failed(spa_t *spa, const char *fmt, ...);
9973ee8c80cSPavel Zakharov extern void spa_load_note(spa_t *spa, const char *fmt, ...);
99843466aaeSMax Grossman extern void spa_activate_mos_feature(spa_t *spa, const char *feature,
99943466aaeSMax Grossman     dmu_tx_t *tx);
1000ad135b5dSChristopher Siden extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
1001f9af39baSGeorge Wilson extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
1002fa9e4066Sahrens extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
1003fa9e4066Sahrens extern char *spa_strdup(const char *);
1004fa9e4066Sahrens extern void spa_strfree(char *);
1005fa9e4066Sahrens extern uint64_t spa_get_random(uint64_t range);
10061195e687SMark J Musante extern uint64_t spa_generate_guid(spa_t *spa);
100743466aaeSMax Grossman extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
1008fa9e4066Sahrens extern void spa_freeze(spa_t *spa);
1009e9103aaeSGarrett D'Amore extern int spa_change_guid(spa_t *spa);
1010990b4856Slling extern void spa_upgrade(spa_t *spa, uint64_t version);
1011fa9e4066Sahrens extern void spa_evict_all(void);
1012c5904d13Seschrock extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
1013c5904d13Seschrock     boolean_t l2cache);
101499653d4eSeschrock extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
1015b24ab676SJeff Bonwick extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
1016b24ab676SJeff Bonwick extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
1017b24ab676SJeff Bonwick extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
10186ce0521aSperrin extern boolean_t spa_has_slogs(spa_t *spa);
1019bf82a41bSeschrock extern boolean_t spa_is_root(spa_t *spa);
10208ad4d6ddSJeff Bonwick extern boolean_t spa_writeable(spa_t *spa);
102173527f44SAlex Reece extern boolean_t spa_has_pending_synctask(spa_t *spa);
1022b5152584SMatthew Ahrens extern int spa_maxblocksize(spa_t *spa);
102354811da5SToomas Soome extern int spa_maxdnodesize(spa_t *spa);
1024e0f1c0afSOlaf Faaland extern boolean_t spa_multihost(spa_t *spa);
1025e0f1c0afSOlaf Faaland extern unsigned long spa_get_hostid(void);
102686714001SSerapheim Dimitropoulos extern boolean_t spa_has_checkpoint(spa_t *spa);
102786714001SSerapheim Dimitropoulos extern boolean_t spa_importing_readonly_checkpoint(spa_t *spa);
102886714001SSerapheim Dimitropoulos extern boolean_t spa_suspend_async_destroy(spa_t *spa);
102986714001SSerapheim Dimitropoulos extern uint64_t spa_min_claim_txg(spa_t *spa);
1030f63ab3d5SMatthew Ahrens extern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp);
10316f793812SPavel Zakharov extern boolean_t zfs_dva_valid(spa_t *spa, const dva_t *dva,
10326f793812SPavel Zakharov     const blkptr_t *bp);
10335cabbc6bSPrashanth Sreenivasa typedef void (*spa_remap_cb_t)(uint64_t vdev, uint64_t offset, uint64_t size,
10345cabbc6bSPrashanth Sreenivasa     void *arg);
10355cabbc6bSPrashanth Sreenivasa extern boolean_t spa_remap_blkptr(spa_t *spa, blkptr_t *bp,
10365cabbc6bSPrashanth Sreenivasa     spa_remap_cb_t callback, void *arg);
10375cabbc6bSPrashanth Sreenivasa extern uint64_t spa_get_last_removal_txg(spa_t *spa);
10386f793812SPavel Zakharov extern boolean_t spa_trust_config(spa_t *spa);
10396f793812SPavel Zakharov extern uint64_t spa_missing_tvds_allowed(spa_t *spa);
10406f793812SPavel Zakharov extern void spa_set_missing_tvds(spa_t *spa, uint64_t missing);
104186714001SSerapheim Dimitropoulos extern boolean_t spa_top_vdevs_spacemap_addressable(spa_t *spa);
1042814dcd43SSerapheim Dimitropoulos extern uint64_t spa_total_metaslabs(spa_t *spa);
1043663207adSDon Brady extern void spa_activate_allocation_classes(spa_t *, dmu_tx_t *);
1044468c413aSTim Haley 
10458ad4d6ddSJeff Bonwick extern int spa_mode(spa_t *spa);
10464585130bSYuri Pankov extern uint64_t zfs_strtonum(const char *str, char **nptr);
1047ea8dc4b6Seschrock 
1048ecd6cf80Smarks extern char *spa_his_ievent_table[];
1049ecd6cf80Smarks 
105006eeb2adSek extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
105106eeb2adSek extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
105206eeb2adSek     char *his_buf);
10534445fffbSMatthew Ahrens extern int spa_history_log(spa_t *spa, const char *his_buf);
10544445fffbSMatthew Ahrens extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
10554445fffbSMatthew Ahrens extern void spa_history_log_version(spa_t *spa, const char *operation);
10564445fffbSMatthew Ahrens extern void spa_history_log_internal(spa_t *spa, const char *operation,
10574445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...);
10584445fffbSMatthew Ahrens extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
10594445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...);
10604445fffbSMatthew Ahrens extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
10614445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...);
106206eeb2adSek 
1063ea8dc4b6Seschrock /* error handling */
10647802d7bfSMatthew Ahrens struct zbookmark_phys;
1065eb633035STom Caputi extern void spa_log_error(spa_t *spa, const struct zbookmark_phys *zb);
1066dd50e0ccSTony Hutter extern int zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
1067eb633035STom Caputi     const struct zbookmark_phys *zb, struct zio *zio, uint64_t stateoroffset,
1068eb633035STom Caputi     uint64_t length);
1069dd50e0ccSTony Hutter extern boolean_t zfs_ereport_is_valid(const char *class, spa_t *spa, vdev_t *vd,
1070dd50e0ccSTony Hutter     zio_t *zio);
10713d7072f8Seschrock extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
1072069f55e2SEric Schrock extern void zfs_post_state_change(spa_t *spa, vdev_t *vd);
10733d7072f8Seschrock extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
1074ea8dc4b6Seschrock extern uint64_t spa_get_errlog_size(spa_t *spa);
1075ea8dc4b6Seschrock extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
1076ea8dc4b6Seschrock extern void spa_errlog_rotate(spa_t *spa);
1077ea8dc4b6Seschrock extern void spa_errlog_drain(spa_t *spa);
1078ea8dc4b6Seschrock extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
1079ea8dc4b6Seschrock extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
1080fa9e4066Sahrens 
108187db74c1Sek /* vdev cache */
108287db74c1Sek extern void vdev_cache_stat_init(void);
108387db74c1Sek extern void vdev_cache_stat_fini(void);
108487db74c1Sek 
108512a8814cSTom Caputi /* vdev mirror */
108612a8814cSTom Caputi extern void vdev_mirror_stat_init(void);
108712a8814cSTom Caputi extern void vdev_mirror_stat_fini(void);
108812a8814cSTom Caputi 
1089fa9e4066Sahrens /* Initialization and termination */
1090fa9e4066Sahrens extern void spa_init(int flags);
1091fa9e4066Sahrens extern void spa_fini(void);
10923f7978d0SAlan Somers extern void spa_boot_init(void);
1093fa9e4066Sahrens 
1094b1b8ab34Slling /* properties */
1095990b4856Slling extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
1096990b4856Slling extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
1097990b4856Slling extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
1098379c004dSEric Schrock extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
1099b1b8ab34Slling 
11003d7072f8Seschrock /* asynchronous event notification */
1101ce1577b0SDave Eddy extern void spa_event_notify(spa_t *spa, vdev_t *vdev, nvlist_t *hist_nvl,
1102ce1577b0SDave Eddy     const char *name);
11035cabbc6bSPrashanth Sreenivasa extern sysevent_t *spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl,
11045cabbc6bSPrashanth Sreenivasa     const char *name);
11055cabbc6bSPrashanth Sreenivasa extern void spa_event_post(sysevent_t *ev);
11065cabbc6bSPrashanth Sreenivasa extern void spa_event_discard(sysevent_t *ev);
1107*b4fb0039SJoshua M. Clulow extern void zfs_post_dle_sysevent(const char *);
11083d7072f8Seschrock 
1109fa9e4066Sahrens #ifdef ZFS_DEBUG
1110c0a81264Sek #define	dprintf_bp(bp, fmt, ...) do {				\
111143466aaeSMax Grossman 	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
1112c0a81264Sek 	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
111343466aaeSMax Grossman 	snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));	\
1114c0a81264Sek 	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
1115c0a81264Sek 	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
1116fa9e4066Sahrens 	} \
1117fa9e4066Sahrens _NOTE(CONSTCOND) } while (0)
1118fa9e4066Sahrens #else
1119fa9e4066Sahrens #define	dprintf_bp(bp, fmt, ...)
1120fa9e4066Sahrens #endif
1121fa9e4066Sahrens 
11228ad4d6ddSJeff Bonwick extern int spa_mode_global;			/* mode, e.g. FREAD | FWRITE */
1123fa9e4066Sahrens 
1124fa9e4066Sahrens #ifdef	__cplusplus
1125fa9e4066Sahrens }
1126fa9e4066Sahrens #endif
1127fa9e4066Sahrens 
1128fa9e4066Sahrens #endif	/* _SYS_SPA_H */
1129