1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
223f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23814dcd4Serapheim Dimitropoulos * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24e9103aaGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2645818eeMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved.
27c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
28ce1577bDave Eddy * Copyright 2017 Joyent, Inc.
291702cceAlek Pinchuk * Copyright (c) 2017 Datto Inc.
30663207aDon Brady * Copyright (c) 2017, Intel Corporation.
31fa9e406ahrens */
32fa9e406ahrens
33fa9e406ahrens#ifndef _SYS_SPA_H
34fa9e406ahrens#define	_SYS_SPA_H
35fa9e406ahrens
36fa9e406ahrens#include <sys/avl.h>
37fa9e406ahrens#include <sys/zfs_context.h>
38fa9e406ahrens#include <sys/nvpair.h>
395cabbc6Prashanth Sreenivasa#include <sys/sysevent.h>
40fa9e406ahrens#include <sys/sysmacros.h>
41fa9e406ahrens#include <sys/types.h>
42fa9e406ahrens#include <sys/fs/zfs.h>
434ba5b96Dan Kimmel#include <sys/dmu.h>
44814dcd4Serapheim Dimitropoulos#include <sys/space_map.h>
454d7988dPaul Dagnelie#include <sys/bitops.h>
46fa9e406ahrens
47fa9e406ahrens#ifdef	__cplusplus
48fa9e406ahrensextern "C" {
49fa9e406ahrens#endif
50fa9e406ahrens
51fa9e406ahrens/*
52fa9e406ahrens * Forward references that lots of things need.
53fa9e406ahrens */
54fa9e406ahrenstypedef struct spa spa_t;
55fa9e406ahrenstypedef struct vdev vdev_t;
56fa9e406ahrenstypedef struct metaslab metaslab_t;
57b24ab67Jeff Bonwicktypedef struct metaslab_group metaslab_group_t;
58b24ab67Jeff Bonwicktypedef struct metaslab_class metaslab_class_t;
59b24ab67Jeff Bonwicktypedef struct zio zio_t;
60fa9e406ahrenstypedef struct zilog zilog_t;
61fa94a07brendantypedef struct spa_aux_vdev spa_aux_vdev_t;
62b24ab67Jeff Bonwicktypedef struct ddt ddt_t;
63b24ab67Jeff Bonwicktypedef struct ddt_entry ddt_entry_t;
64fa9e406ahrensstruct dsl_pool;
654445fffMatthew Ahrensstruct dsl_dataset;
66eb63303Tom Caputistruct dsl_crypto_params;
67fa9e406ahrens
68fa9e406ahrens/*
69b515258Matthew Ahrens * We currently support block sizes from 512 bytes to 16MB.
70b515258Matthew Ahrens * The benefits of larger blocks, and thus larger IO, need to be weighed
71b515258Matthew Ahrens * against the cost of COWing a giant block to modify one byte, and the
72b515258Matthew Ahrens * large latency of reading or writing a large block.
73b515258Matthew Ahrens *
74b515258Matthew Ahrens * Note that although blocks up to 16MB are supported, the recordsize
75b515258Matthew Ahrens * property can not be set larger than zfs_max_recordsize (default 1MB).
76b515258Matthew Ahrens * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
77b515258Matthew Ahrens *
78b515258Matthew Ahrens * Note that although the LSIZE field of the blkptr_t can store sizes up
79b515258Matthew Ahrens * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
80b515258Matthew Ahrens * 32MB - 512 bytes.  Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
81fa9e406ahrens */
82fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
83b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSHIFT	17
84b515258Matthew Ahrens#define	SPA_MAXBLOCKSHIFT	24
85fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
86b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)
87fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
88fa9e406ahrens
89fa9e406ahrens/*
905711d39loli * Alignment Shift (ashift) is an immutable, internal top-level vdev property
915711d39loli * which can only be set at vdev creation time. Physical writes are always done
925711d39loli * according to it, which makes 2^ashift the smallest possible IO on a vdev.
935711d39loli *
945711d39loli * We currently allow values ranging from 512 bytes (2^9 = 512) to 64 KiB
955711d39loli * (2^16 = 65,536).
965711d39loli */
975711d39loli#define	ASHIFT_MIN		9
985711d39loli#define	ASHIFT_MAX		16
995711d39loli
1005711d39loli/*
101f7991baTim Haley * Size of block to hold the configuration data (a packed nvlist)
102f7991baTim Haley */
103ad135b5Christopher Siden#define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
104f7991baTim Haley
105f7991baTim Haley/*
106fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
107fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
108fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
109fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
110fa9e406ahrens * else that expands the ASIZE.
111fa9e406ahrens */
112fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
113fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
114fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
115fa9e406ahrens
116dcbf3bdGeorge Wilson#define	SPA_COMPRESSBITS	7
11717f1128Serapheim Dimitropoulos#define	SPA_VDEVBITS		24
118dcbf3bdGeorge Wilson
119fa9e406ahrens/*
120fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
121fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
122fa9e406ahrens */
123fa9e406ahrenstypedef struct dva {
124fa9e406ahrens	uint64_t	dva_word[2];
125fa9e406ahrens} dva_t;
126fa9e406ahrens
127fa9e406ahrens/*
128fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
129fa9e406ahrens */
130fa9e406ahrenstypedef struct zio_cksum {
131fa9e406ahrens	uint64_t	zc_word[4];
132fa9e406ahrens} zio_cksum_t;
133fa9e406ahrens
134fa9e406ahrens/*
13545818eeMatthew Ahrens * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
13645818eeMatthew Ahrens * secret and is suitable for use in MAC algorithms as the key.
13745818eeMatthew Ahrens */
13845818eeMatthew Ahrenstypedef struct zio_cksum_salt {
13945818eeMatthew Ahrens	uint8_t		zcs_bytes[32];
14045818eeMatthew Ahrens} zio_cksum_salt_t;
14145818eeMatthew Ahrens
14245818eeMatthew Ahrens/*
143fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
144fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
145fa9e406ahrens *
146fa9e406ahrens *	64	56	48	40	32	24	16	8	0
147fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
14817f1128Serapheim Dimitropoulos * 0	|  pad  |	  vdev1         | GRID  |	  ASIZE		|
149fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
150fa9e406ahrens * 1	|G|			 offset1				|
151fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
15217f1128Serapheim Dimitropoulos * 2	|  pad  |	  vdev2         | GRID  |	  ASIZE		|
153fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
154fa9e406ahrens * 3	|G|			 offset2				|
155fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
15617f1128Serapheim Dimitropoulos * 4	|  pad  |	  vdev3         | GRID  |	  ASIZE		|
157fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
158fa9e406ahrens * 5	|G|			 offset3				|
159fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
1605d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
161fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
162fa9e406ahrens * 7	|			padding					|
163fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
164fa9e406ahrens * 8	|			padding					|
165fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
166b24ab67Jeff Bonwick * 9	|			physical birth txg			|
167fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
168b24ab67Jeff Bonwick * a	|			logical birth txg			|
169fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
170fa9e406ahrens * b	|			fill count				|
171fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
172fa9e406ahrens * c	|			checksum[0]				|
173fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
174fa9e406ahrens * d	|			checksum[1]				|
175fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
176fa9e406ahrens * e	|			checksum[2]				|
177fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
178fa9e406ahrens * f	|			checksum[3]				|
179fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
180fa9e406ahrens *
181fa9e406ahrens * Legend:
182fa9e406ahrens *
183fa9e406ahrens * vdev		virtual device ID
184fa9e406ahrens * offset	offset into virtual device
185fa9e406ahrens * LSIZE	logical size
186fa9e406ahrens * PSIZE	physical size (after compression)
187fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
188fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
189fa9e406ahrens * cksum	checksum function
190fa9e406ahrens * comp		compression function
191fa9e406ahrens * G		gang block indicator
192b24ab67Jeff Bonwick * B		byteorder (endianness)
193b24ab67Jeff Bonwick * D		dedup
194eb63303Tom Caputi * X		encryption
1955d7b4d4Matthew Ahrens * E		blkptr_t contains embedded data (see below)
196fa9e406ahrens * lvl		level of indirection
197b24ab67Jeff Bonwick * type		DMU object type
1985cabbc6Prashanth Sreenivasa * phys birth	txg when dva[0] was written; zero if same as logical birth txg
1995cabbc6Prashanth Sreenivasa *              note that typically all the dva's would be written in this
2005cabbc6Prashanth Sreenivasa *              txg, but they could be different if they were moved by
2015cabbc6Prashanth Sreenivasa *              device removal.
202b24ab67Jeff Bonwick * log. birth	transaction group in which the block was logically born
203fa9e406ahrens * fill count	number of non-zero blocks under this bp
204fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
205fa9e406ahrens */
2065d7b4d4Matthew Ahrens
2075d7b4d4Matthew Ahrens/*
208eb63303Tom Caputi * The blkptr_t's of encrypted blocks also need to store the encryption
209eb63303Tom Caputi * parameters so that the block can be decrypted. This layout is as follows:
210eb63303Tom Caputi *
211eb63303Tom Caputi *	64	56	48	40	32	24	16	8	0
212eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
213eb63303Tom Caputi * 0	|		vdev1		| GRID  |	  ASIZE		|
214eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
215eb63303Tom Caputi * 1	|G|			 offset1				|
216eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
217eb63303Tom Caputi * 2	|		vdev2		| GRID  |	  ASIZE		|
218eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
219eb63303Tom Caputi * 3	|G|			 offset2				|
220eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
221eb63303Tom Caputi * 4	|			salt					|
222eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
223eb63303Tom Caputi * 5	|			IV1					|
224eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
225eb63303Tom Caputi * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
226eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
227eb63303Tom Caputi * 7	|			padding					|
228eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
229eb63303Tom Caputi * 8	|			padding					|
230eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
231eb63303Tom Caputi * 9	|			physical birth txg			|
232eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
233eb63303Tom Caputi * a	|			logical birth txg			|
234eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
235eb63303Tom Caputi * b	|		IV2		|	    fill count		|
236eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
237eb63303Tom Caputi * c	|			checksum[0]				|
238eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
239eb63303Tom Caputi * d	|			checksum[1]				|
240eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
241eb63303Tom Caputi * e	|			MAC[0]					|
242eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
243eb63303Tom Caputi * f	|			MAC[1]					|
244eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
245eb63303Tom Caputi *
246eb63303Tom Caputi * Legend:
247eb63303Tom Caputi *
248eb63303Tom Caputi * salt		Salt for generating encryption keys
249eb63303Tom Caputi * IV1		First 64 bits of encryption IV
250eb63303Tom Caputi * X		Block requires encryption handling (set to 1)
251eb63303Tom Caputi * E		blkptr_t contains embedded data (set to 0, see below)
252eb63303Tom Caputi * fill count	number of non-zero blocks under this bp (truncated to 32 bits)
253eb63303Tom Caputi * IV2		Last 32 bits of encryption IV
254eb63303Tom Caputi * checksum[2]	128-bit checksum of the data this bp describes
255eb63303Tom Caputi * MAC[2]	128-bit message authentication code for this data
256eb63303Tom Caputi *
257eb63303Tom Caputi * The X bit being set indicates that this block is one of 3 types. If this is
258eb63303Tom Caputi * a level 0 block with an encrypted object type, the block is encrypted
259eb63303Tom Caputi * (see BP_IS_ENCRYPTED()). If this is a level 0 block with an unencrypted
260eb63303Tom Caputi * object type, this block is authenticated with an HMAC (see
261eb63303Tom Caputi * BP_IS_AUTHENTICATED()). Otherwise (if level > 0), this bp will use the MAC
262eb63303Tom Caputi * words to store a checksum-of-MACs from the level below (see
263eb63303Tom Caputi * BP_HAS_INDIRECT_MAC_CKSUM()). For convenience in the code, BP_IS_PROTECTED()
264eb63303Tom Caputi * refers to both encrypted and authenticated blocks and BP_USES_CRYPT()
265eb63303Tom Caputi * refers to any of these 3 kinds of blocks.
266eb63303Tom Caputi *
267eb63303Tom Caputi * The additional encryption parameters are the salt, IV, and MAC which are
268eb63303Tom Caputi * explained in greater detail in the block comment at the top of zio_crypt.c.
269eb63303Tom Caputi * The MAC occupies half of the checksum space since it serves a very similar
270eb63303Tom Caputi * purpose: to prevent data corruption on disk. The only functional difference
271eb63303Tom Caputi * is that the checksum is used to detect on-disk corruption whether or not the
272eb63303Tom Caputi * encryption key is loaded and the MAC provides additional protection against
273eb63303Tom Caputi * malicious disk tampering. We use the 3rd DVA to store the salt and first
274eb63303Tom Caputi * 64 bits of the IV. As a result encrypted blocks can only have 2 copies
275eb63303Tom Caputi * maximum instead of the normal 3. The last 32 bits of the IV are stored in
276eb63303Tom Caputi * the upper bits of what is usually the fill count. Note that only blocks at
277eb63303Tom Caputi * level 0 or -2 are ever encrypted, which allows us to guarantee that these
278eb63303Tom Caputi * 32 bits are not trampled over by other code (see zio_crypt.c for details).
279eb63303Tom Caputi * The salt and IV are not used for authenticated bps or bps with an indirect
280eb63303Tom Caputi * MAC checksum, so these blocks can utilize all 3 DVAs and the full 64 bits
281eb63303Tom Caputi * for the fill count.
282eb63303Tom Caputi */
283eb63303Tom Caputi
284eb63303Tom Caputi/*
2855d7b4d4Matthew Ahrens * "Embedded" blkptr_t's don't actually point to a block, instead they
2865d7b4d4Matthew Ahrens * have a data payload embedded in the blkptr_t itself.  See the comment
2875d7b4d4Matthew Ahrens * in blkptr.c for more details.
2885d7b4d4Matthew Ahrens *
2895d7b4d4Matthew Ahrens * The blkptr_t is laid out as follows:
2905d7b4d4Matthew Ahrens *
2915d7b4d4Matthew Ahrens *	64	56	48	40	32	24	16	8	0
2925d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2935d7b4d4Matthew Ahrens * 0	|      payload                                                  |
2945d7b4d4Matthew Ahrens * 1	|      payload                                                  |
2955d7b4d4Matthew Ahrens * 2	|      payload                                                  |
2965d7b4d4Matthew Ahrens * 3	|      payload                                                  |
2975d7b4d4Matthew Ahrens * 4	|      payload                                                  |
2985d7b4d4Matthew Ahrens * 5	|      payload                                                  |
2995d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3005d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
3015d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3025d7b4d4Matthew Ahrens * 7	|      payload                                                  |
3035d7b4d4Matthew Ahrens * 8	|      payload                                                  |
3045d7b4d4Matthew Ahrens * 9	|      payload                                                  |
3055d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3065d7b4d4Matthew Ahrens * a	|			logical birth txg			|
3075d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3085d7b4d4Matthew Ahrens * b	|      payload                                                  |
3095d7b4d4Matthew Ahrens * c	|      payload                                                  |
3105d7b4d4Matthew Ahrens * d	|      payload                                                  |
3115d7b4d4Matthew Ahrens * e	|      payload                                                  |
3125d7b4d4Matthew Ahrens * f	|      payload                                                  |
3135d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3145d7b4d4Matthew Ahrens *
3155d7b4d4Matthew Ahrens * Legend:
3165d7b4d4Matthew Ahrens *
3175d7b4d4Matthew Ahrens * payload		contains the embedded data
3185d7b4d4Matthew Ahrens * B (byteorder)	byteorder (endianness)
3195d7b4d4Matthew Ahrens * D (dedup)		padding (set to zero)
3205d7b4d4Matthew Ahrens * X			encryption (set to zero; see above)
3215d7b4d4Matthew Ahrens * E (embedded)		set to one
3225d7b4d4Matthew Ahrens * lvl			indirection level
3235d7b4d4Matthew Ahrens * type			DMU object type
3245d7b4d4Matthew Ahrens * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
3255d7b4d4Matthew Ahrens * comp			compression function of payload
3265d7b4d4Matthew Ahrens * PSIZE		size of payload after compression, in bytes
3275d7b4d4Matthew Ahrens * LSIZE		logical size of payload, in bytes
3285d7b4d4Matthew Ahrens *			note that 25 bits is enough to store the largest
3295d7b4d4Matthew Ahrens *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
3305d7b4d4Matthew Ahrens * log. birth		transaction group in which the block was logically born
3315d7b4d4Matthew Ahrens *
3325d7b4d4Matthew Ahrens * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
3335d7b4d4Matthew Ahrens * bp's they are stored in units of SPA_MINBLOCKSHIFT.
3345d7b4d4Matthew Ahrens * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
3355d7b4d4Matthew Ahrens * The B, D, X, lvl, type, and comp fields are stored the same as with normal
3365d7b4d4Matthew Ahrens * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
3375d7b4d4Matthew Ahrens * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
3385d7b4d4Matthew Ahrens * other macros, as they assert that they are only used on BP's of the correct
339eb63303Tom Caputi * "embedded-ness". Encrypted blkptr_t's cannot be embedded because they use
340eb63303Tom Caputi * the payload space for encryption parameters (see the comment above on
341eb63303Tom Caputi * how encryption parameters are stored).
3425d7b4d4Matthew Ahrens */
3435d7b4d4Matthew Ahrens
3445d7b4d4Matthew Ahrens#define	BPE_GET_ETYPE(bp)	\
3455d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3465d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
3475d7b4d4Matthew Ahrens#define	BPE_SET_ETYPE(bp, t)	do { \
3485d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3495d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, t); \
3505d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3515d7b4d4Matthew Ahrens
3525d7b4d4Matthew Ahrens#define	BPE_GET_LSIZE(bp)	\
3535d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3545d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
3555d7b4d4Matthew Ahrens#define	BPE_SET_LSIZE(bp, x)	do { \
3565d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3575d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
3585d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3595d7b4d4Matthew Ahrens
3605d7b4d4Matthew Ahrens#define	BPE_GET_PSIZE(bp)	\
3615d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3625d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
3635d7b4d4Matthew Ahrens#define	BPE_SET_PSIZE(bp, x)	do { \
3645d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
365eb63303Tom Caputi	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x);	\
3665d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3675d7b4d4Matthew Ahrens
3685d7b4d4Matthew Ahrenstypedef enum bp_embedded_type {
3695d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_DATA,
3705d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
3715d7b4d4Matthew Ahrens	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
3725d7b4d4Matthew Ahrens} bp_embedded_type_t;
3735d7b4d4Matthew Ahrens
3745d7b4d4Matthew Ahrens#define	BPE_NUM_WORDS 14
3755d7b4d4Matthew Ahrens#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
3765d7b4d4Matthew Ahrens#define	BPE_IS_PAYLOADWORD(bp, wp) \
3775d7b4d4Matthew Ahrens	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
3785d7b4d4Matthew Ahrens
379fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
380fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
3816f79381Pavel Zakharov#define	SPA_SYNC_MIN_VDEVS 3		/* min vdevs to update during sync */
382fa9e406ahrens
38343466aaMax Grossman/*
38443466aaMax Grossman * A block is a hole when it has either 1) never been written to, or
38543466aaMax Grossman * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
38643466aaMax Grossman * without physically allocating disk space. Holes are represented in the
38743466aaMax Grossman * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
38843466aaMax Grossman * done through the BP_IS_HOLE macro. For holes, the logical size, level,
38943466aaMax Grossman * DMU object type, and birth times are all also stored for holes that
39043466aaMax Grossman * were written to at some point (i.e. were punched after having been filled).
39143466aaMax Grossman */
392b24ab67Jeff Bonwicktypedef struct blkptr {
393b24ab67Jeff Bonwick	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
394b24ab67Jeff Bonwick	uint64_t	blk_prop;	/* size, compression, type, etc	    */
395b24ab67Jeff Bonwick	uint64_t	blk_pad[2];	/* Extra space for the future	    */
396b24ab67Jeff Bonwick	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
397b24ab67Jeff Bonwick	uint64_t	blk_birth;	/* transaction group at birth	    */
398b24ab67Jeff Bonwick	uint64_t	blk_fill;	/* fill count			    */
399b24ab67Jeff Bonwick	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
400b24ab67Jeff Bonwick} blkptr_t;
401b24ab67Jeff Bonwick
402fa9e406ahrens/*
403fa9e406ahrens * Macros to get and set fields in a bp or DVA.
404fa9e406ahrens */
405fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
40643466aaMax Grossman	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
407fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
40843466aaMax Grossman	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
40943466aaMax Grossman	SPA_MINBLOCKSHIFT, 0, x)
410fa9e406ahrens
411fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
412fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
413fa9e406ahrens
41417f1128Serapheim Dimitropoulos#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, SPA_VDEVBITS)
41517f1128Serapheim Dimitropoulos#define	DVA_SET_VDEV(dva, x)	\
41617f1128Serapheim Dimitropoulos	BF64_SET((dva)->dva_word[0], 32, SPA_VDEVBITS, x)
417fa9e406ahrens
418fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
419fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
420fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
421fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
422fa9e406ahrens
423fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
424fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
425fa9e406ahrens
426fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
4275d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ?	\
4285d7b4d4Matthew Ahrens	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
4295d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4305d7b4d4Matthew Ahrens#define	BP_SET_LSIZE(bp, x)	do { \
4315d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4325d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
4335d7b4d4Matthew Ahrens	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4345d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
435fa9e406ahrens
436fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
4375d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4385d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4395d7b4d4Matthew Ahrens#define	BP_SET_PSIZE(bp, x)	do { \
4405d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4415d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
4425d7b4d4Matthew Ahrens	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4435d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
4445d7b4d4Matthew Ahrens
445dcbf3bdGeorge Wilson#define	BP_GET_COMPRESS(bp)		\
446dcbf3bdGeorge Wilson	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
447dcbf3bdGeorge Wilson#define	BP_SET_COMPRESS(bp, x)		\
448dcbf3bdGeorge Wilson	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
449fa9e406ahrens
4505d7b4d4Matthew Ahrens#define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
4515d7b4d4Matthew Ahrens#define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
452b24ab67Jeff Bonwick
4535d7b4d4Matthew Ahrens#define	BP_GET_CHECKSUM(bp)		\
4545d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
4555d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
4565d7b4d4Matthew Ahrens#define	BP_SET_CHECKSUM(bp, x)		do { \
4575d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4585d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, x); \
4595d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
460b24ab67Jeff Bonwick
461b24ab67Jeff Bonwick#define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
462b24ab67Jeff Bonwick#define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
463fa9e406ahrens
464b24ab67Jeff Bonwick#define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
465b24ab67Jeff Bonwick#define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
466fa9e406ahrens
467eb63303Tom Caputi/* encrypted, authenticated, and MAC cksum bps use the same bit */
468eb63303Tom Caputi#define	BP_USES_CRYPT(bp)		BF64_GET((bp)->blk_prop, 61, 1)
469eb63303Tom Caputi#define	BP_SET_CRYPT(bp, x)		BF64_SET((bp)->blk_prop, 61, 1, x)
470eb63303Tom Caputi
471eb63303Tom Caputi#define	BP_IS_ENCRYPTED(bp)			\
472eb63303Tom Caputi	(BP_USES_CRYPT(bp) &&			\
473eb63303Tom Caputi	BP_GET_LEVEL(bp) == 0 &&		\
474eb63303Tom Caputi	DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
475eb63303Tom Caputi
476eb63303Tom Caputi#define	BP_IS_AUTHENTICATED(bp)			\
477eb63303Tom Caputi	(BP_USES_CRYPT(bp) &&			\
478eb63303Tom Caputi	BP_GET_LEVEL(bp) == 0 &&		\
479eb63303Tom Caputi	!DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
480eb63303Tom Caputi
481eb63303Tom Caputi#define	BP_HAS_INDIRECT_MAC_CKSUM(bp)		\
482eb63303Tom Caputi	(BP_USES_CRYPT(bp) && BP_GET_LEVEL(bp) > 0)
483eb63303Tom Caputi
484eb63303Tom Caputi#define	BP_IS_PROTECTED(bp)			\
485eb63303Tom Caputi	(BP_IS_ENCRYPTED(bp) || BP_IS_AUTHENTICATED(bp))
486eb63303Tom Caputi
487b24ab67Jeff Bonwick#define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
488b24ab67Jeff Bonwick#define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
489fa9e406ahrens
49043466aaMax Grossman#define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
491b24ab67Jeff Bonwick#define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
492b24ab67Jeff Bonwick
493b24ab67Jeff Bonwick#define	BP_PHYSICAL_BIRTH(bp)		\
4945d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4955d7b4d4Matthew Ahrens	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
496b24ab67Jeff Bonwick
497b24ab67Jeff Bonwick#define	BP_SET_BIRTH(bp, logical, physical)	\
498b24ab67Jeff Bonwick{						\
4995d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp));		\
500b24ab67Jeff Bonwick	(bp)->blk_birth = (logical);		\
501b24ab67Jeff Bonwick	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
502b24ab67Jeff Bonwick}
503fa9e406ahrens
504eb63303Tom Caputi#define	BP_GET_FILL(bp)				\
505eb63303Tom Caputi	((BP_IS_ENCRYPTED(bp)) ? BF64_GET((bp)->blk_fill, 0, 32) : \
506eb63303Tom Caputi	((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill))
507eb63303Tom Caputi
508eb63303Tom Caputi#define	BP_SET_FILL(bp, fill)			\
509eb63303Tom Caputi{						\
510eb63303Tom Caputi	if (BP_IS_ENCRYPTED(bp))			\
511eb63303Tom Caputi		BF64_SET((bp)->blk_fill, 0, 32, fill); \
512eb63303Tom Caputi	else					\
513eb63303Tom Caputi		(bp)->blk_fill = fill;		\
514eb63303Tom Caputi}
515eb63303Tom Caputi
516eb63303Tom Caputi#define	BP_GET_IV2(bp)				\
517eb63303Tom Caputi	(ASSERT(BP_IS_ENCRYPTED(bp)),		\
518eb63303Tom Caputi	BF64_GET((bp)->blk_fill, 32, 32))
519eb63303Tom Caputi#define	BP_SET_IV2(bp, iv2)			\
520eb63303Tom Caputi{						\
521eb63303Tom Caputi	ASSERT(BP_IS_ENCRYPTED(bp));		\
522eb63303Tom Caputi	BF64_SET((bp)->blk_fill, 32, 32, iv2);	\
523eb63303Tom Caputi}
5245d7b4d4Matthew Ahrens
525770499eDan Kimmel#define	BP_IS_METADATA(bp)	\
526770499eDan Kimmel	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
527770499eDan Kimmel
528fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
5295d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
5305d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
5315d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
532eb63303Tom Caputi	(DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
53399653d4eschrock
534770499eDan Kimmel#define	BP_GET_UCSIZE(bp)	\
535770499eDan Kimmel	(BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
536fa9e406ahrens
53744cd46cbillm#define	BP_GET_NDVAS(bp)	\
5385d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
5395d7b4d4Matthew Ahrens	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
54044cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
541eb63303Tom Caputi	(!!DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
54244cd46cbillm
54344cd46cbillm#define	BP_COUNT_GANG(bp)	\
5445d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
54544cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
54644cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
547eb63303Tom Caputi	(DVA_GET_GANG(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp))))
54844cd46cbillm
549fa9e406ahrens#define	DVA_EQUAL(dva1, dva2)	\
550fa9e406ahrens	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
551fa9e406ahrens	(dva1)->dva_word[0] == (dva2)->dva_word[0])
552fa9e406ahrens
553b24ab67Jeff Bonwick#define	BP_EQUAL(bp1, bp2)	\
554b24ab67Jeff Bonwick	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
5555d7b4d4Matthew Ahrens	(bp1)->blk_birth == (bp2)->blk_birth &&			\
556b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
557b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
558b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
559b24ab67Jeff Bonwick
5606b4acc8ahrens#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
5616b4acc8ahrens	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
5626b4acc8ahrens	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
5636b4acc8ahrens	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
5646b4acc8