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.
235cabbc6Prashanth Sreenivasa * Copyright (c) 2011, 2018 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>
44fa9e406ahrens
45fa9e406ahrens#ifdef	__cplusplus
46fa9e406ahrensextern "C" {
47fa9e406ahrens#endif
48fa9e406ahrens
49fa9e406ahrens/*
50fa9e406ahrens * Forward references that lots of things need.
51fa9e406ahrens */
52fa9e406ahrenstypedef struct spa spa_t;
53fa9e406ahrenstypedef struct vdev vdev_t;
54fa9e406ahrenstypedef struct metaslab metaslab_t;
55b24ab67Jeff Bonwicktypedef struct metaslab_group metaslab_group_t;
56b24ab67Jeff Bonwicktypedef struct metaslab_class metaslab_class_t;
57b24ab67Jeff Bonwicktypedef struct zio zio_t;
58fa9e406ahrenstypedef struct zilog zilog_t;
59fa94a07brendantypedef struct spa_aux_vdev spa_aux_vdev_t;
60b24ab67Jeff Bonwicktypedef struct ddt ddt_t;
61b24ab67Jeff Bonwicktypedef struct ddt_entry ddt_entry_t;
62fa9e406ahrensstruct dsl_pool;
634445fffMatthew Ahrensstruct dsl_dataset;
64eb63303Tom Caputistruct dsl_crypto_params;
65fa9e406ahrens
66fa9e406ahrens/*
67fa9e406ahrens * General-purpose 32-bit and 64-bit bitfield encodings.
68fa9e406ahrens */
69fa9e406ahrens#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
70fa9e406ahrens#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
71fa9e406ahrens#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
72fa9e406ahrens#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
73fa9e406ahrens
74fa9e406ahrens#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
75fa9e406ahrens#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
76fa9e406ahrens
7743466aaMax Grossman#define	BF32_SET(x, low, len, val) do { \
7843466aaMax Grossman	ASSERT3U(val, <, 1U << (len)); \
7943466aaMax Grossman	ASSERT3U(low + len, <=, 32); \
8043466aaMax Grossman	(x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
8143466aaMax Grossman_NOTE(CONSTCOND) } while (0)
8243466aaMax Grossman
8343466aaMax Grossman#define	BF64_SET(x, low, len, val) do { \
8443466aaMax Grossman	ASSERT3U(val, <, 1ULL << (len)); \
8543466aaMax Grossman	ASSERT3U(low + len, <=, 64); \
8643466aaMax Grossman	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
8743466aaMax Grossman_NOTE(CONSTCOND) } while (0)
88fa9e406ahrens
89fa9e406ahrens#define	BF32_GET_SB(x, low, len, shift, bias)	\
90fa9e406ahrens	((BF32_GET(x, low, len) + (bias)) << (shift))
91fa9e406ahrens#define	BF64_GET_SB(x, low, len, shift, bias)	\
92fa9e406ahrens	((BF64_GET(x, low, len) + (bias)) << (shift))
93fa9e406ahrens
9443466aaMax Grossman#define	BF32_SET_SB(x, low, len, shift, bias, val) do { \
9543466aaMax Grossman	ASSERT(IS_P2ALIGNED(val, 1U << shift)); \
9643466aaMax Grossman	ASSERT3S((val) >> (shift), >=, bias); \
9743466aaMax Grossman	BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
9843466aaMax Grossman_NOTE(CONSTCOND) } while (0)
9943466aaMax Grossman#define	BF64_SET_SB(x, low, len, shift, bias, val) do { \
10043466aaMax Grossman	ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \
10143466aaMax Grossman	ASSERT3S((val) >> (shift), >=, bias); \
10243466aaMax Grossman	BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
10343466aaMax Grossman_NOTE(CONSTCOND) } while (0)
104fa9e406ahrens
105fa9e406ahrens/*
106b515258Matthew Ahrens * We currently support block sizes from 512 bytes to 16MB.
107b515258Matthew Ahrens * The benefits of larger blocks, and thus larger IO, need to be weighed
108b515258Matthew Ahrens * against the cost of COWing a giant block to modify one byte, and the
109b515258Matthew Ahrens * large latency of reading or writing a large block.
110b515258Matthew Ahrens *
111b515258Matthew Ahrens * Note that although blocks up to 16MB are supported, the recordsize
112b515258Matthew Ahrens * property can not be set larger than zfs_max_recordsize (default 1MB).
113b515258Matthew Ahrens * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
114b515258Matthew Ahrens *
115b515258Matthew Ahrens * Note that although the LSIZE field of the blkptr_t can store sizes up
116b515258Matthew Ahrens * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
117b515258Matthew Ahrens * 32MB - 512 bytes.  Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
118fa9e406ahrens */
119fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
120b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSHIFT	17
121b515258Matthew Ahrens#define	SPA_MAXBLOCKSHIFT	24
122fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
123b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)
124fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
125fa9e406ahrens
126fa9e406ahrens/*
1275711d39loli * Alignment Shift (ashift) is an immutable, internal top-level vdev property
1285711d39loli * which can only be set at vdev creation time. Physical writes are always done
1295711d39loli * according to it, which makes 2^ashift the smallest possible IO on a vdev.
1305711d39loli *
1315711d39loli * We currently allow values ranging from 512 bytes (2^9 = 512) to 64 KiB
1325711d39loli * (2^16 = 65,536).
1335711d39loli */
1345711d39loli#define	ASHIFT_MIN		9
1355711d39loli#define	ASHIFT_MAX		16
1365711d39loli
1375711d39loli/*
138f7991baTim Haley * Size of block to hold the configuration data (a packed nvlist)
139f7991baTim Haley */
140ad135b5Christopher Siden#define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
141f7991baTim Haley
142f7991baTim Haley/*
143fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
144fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
145fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
146fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
147fa9e406ahrens * else that expands the ASIZE.
148fa9e406ahrens */
149fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
150fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
151fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
152fa9e406ahrens
153dcbf3bdGeorge Wilson#define	SPA_COMPRESSBITS	7
15417f1128Serapheim Dimitropoulos#define	SPA_VDEVBITS		24
155dcbf3bdGeorge Wilson
156fa9e406ahrens/*
157fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
158fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
159fa9e406ahrens */
160fa9e406ahrenstypedef struct dva {
161fa9e406ahrens	uint64_t	dva_word[2];
162fa9e406ahrens} dva_t;
163fa9e406ahrens
164fa9e406ahrens/*
165fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
166fa9e406ahrens */
167fa9e406ahrenstypedef struct zio_cksum {
168fa9e406ahrens	uint64_t	zc_word[4];
169fa9e406ahrens} zio_cksum_t;
170fa9e406ahrens
171fa9e406ahrens/*
17245818eeMatthew Ahrens * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
17345818eeMatthew Ahrens * secret and is suitable for use in MAC algorithms as the key.
17445818eeMatthew Ahrens */
17545818eeMatthew Ahrenstypedef struct zio_cksum_salt {
17645818eeMatthew Ahrens	uint8_t		zcs_bytes[32];
17745818eeMatthew Ahrens} zio_cksum_salt_t;
17845818eeMatthew Ahrens
17945818eeMatthew Ahrens/*
180fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
181fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
182fa9e406ahrens *
183fa9e406ahrens *	64	56	48	40	32	24	16	8	0
184fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
18517f1128Serapheim Dimitropoulos * 0	|  pad  |	  vdev1         | GRID  |	  ASIZE		|
186fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
187fa9e406ahrens * 1	|G|			 offset1				|
188fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
18917f1128Serapheim Dimitropoulos * 2	|  pad  |	  vdev2         | GRID  |	  ASIZE		|
190fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
191fa9e406ahrens * 3	|G|			 offset2				|
192fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
19317f1128Serapheim Dimitropoulos * 4	|  pad  |	  vdev3         | GRID  |	  ASIZE		|
194fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
195fa9e406ahrens * 5	|G|			 offset3				|
196fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
1975d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
198fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
199fa9e406ahrens * 7	|			padding					|
200fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
201fa9e406ahrens * 8	|			padding					|
202fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
203b24ab67Jeff Bonwick * 9	|			physical birth txg			|
204fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
205b24ab67Jeff Bonwick * a	|			logical birth txg			|
206fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
207fa9e406ahrens * b	|			fill count				|
208fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
209fa9e406ahrens * c	|			checksum[0]				|
210fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
211fa9e406ahrens * d	|			checksum[1]				|
212fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
213fa9e406ahrens * e	|			checksum[2]				|
214fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
215fa9e406ahrens * f	|			checksum[3]				|
216fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
217fa9e406ahrens *
218fa9e406ahrens * Legend:
219fa9e406ahrens *
220fa9e406ahrens * vdev		virtual device ID
221fa9e406ahrens * offset	offset into virtual device
222fa9e406ahrens * LSIZE	logical size
223fa9e406ahrens * PSIZE	physical size (after compression)
224fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
225fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
226fa9e406ahrens * cksum	checksum function
227fa9e406ahrens * comp		compression function
228fa9e406ahrens * G		gang block indicator
229b24ab67Jeff Bonwick * B		byteorder (endianness)
230b24ab67Jeff Bonwick * D		dedup
231eb63303Tom Caputi * X		encryption
2325d7b4d4Matthew Ahrens * E		blkptr_t contains embedded data (see below)
233fa9e406ahrens * lvl		level of indirection
234b24ab67Jeff Bonwick * type		DMU object type
2355cabbc6Prashanth Sreenivasa * phys birth	txg when dva[0] was written; zero if same as logical birth txg
2365cabbc6Prashanth Sreenivasa *              note that typically all the dva's would be written in this
2375cabbc6Prashanth Sreenivasa *              txg, but they could be different if they were moved by
2385cabbc6Prashanth Sreenivasa *              device removal.
239b24ab67Jeff Bonwick * log. birth	transaction group in which the block was logically born
240fa9e406ahrens * fill count	number of non-zero blocks under this bp
241fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
242fa9e406ahrens */
2435d7b4d4Matthew Ahrens
2445d7b4d4Matthew Ahrens/*
245eb63303Tom Caputi * The blkptr_t's of encrypted blocks also need to store the encryption
246eb63303Tom Caputi * parameters so that the block can be decrypted. This layout is as follows:
247eb63303Tom Caputi *
248eb63303Tom Caputi *	64	56	48	40	32	24	16	8	0
249eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
250eb63303Tom Caputi * 0	|		vdev1		| GRID  |	  ASIZE		|
251eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
252eb63303Tom Caputi * 1	|G|			 offset1				|
253eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
254eb63303Tom Caputi * 2	|		vdev2		| GRID  |	  ASIZE		|
255eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
256eb63303Tom Caputi * 3	|G|			 offset2				|
257eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
258eb63303Tom Caputi * 4	|			salt					|
259eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
260eb63303Tom Caputi * 5	|			IV1					|
261eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
262eb63303Tom Caputi * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
263eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
264eb63303Tom Caputi * 7	|			padding					|
265eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
266eb63303Tom Caputi * 8	|			padding					|
267eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
268eb63303Tom Caputi * 9	|			physical birth txg			|
269eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
270eb63303Tom Caputi * a	|			logical birth txg			|
271eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
272eb63303Tom Caputi * b	|		IV2		|	    fill count		|
273eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
274eb63303Tom Caputi * c	|			checksum[0]				|
275eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
276eb63303Tom Caputi * d	|			checksum[1]				|
277eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
278eb63303Tom Caputi * e	|			MAC[0]					|
279eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
280eb63303Tom Caputi * f	|			MAC[1]					|
281eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
282eb63303Tom Caputi *
283eb63303Tom Caputi * Legend:
284eb63303Tom Caputi *
285eb63303Tom Caputi * salt		Salt for generating encryption keys
286eb63303Tom Caputi * IV1		First 64 bits of encryption IV
287eb63303Tom Caputi * X		Block requires encryption handling (set to 1)
288eb63303Tom Caputi * E		blkptr_t contains embedded data (set to 0, see below)
289eb63303Tom Caputi * fill count	number of non-zero blocks under this bp (truncated to 32 bits)
290eb63303Tom Caputi * IV2		Last 32 bits of encryption IV
291eb63303Tom Caputi * checksum[2]	128-bit checksum of the data this bp describes
292eb63303Tom Caputi * MAC[2]	128-bit message authentication code for this data
293eb63303Tom Caputi *
294eb63303Tom Caputi * The X bit being set indicates that this block is one of 3 types. If this is
295eb63303Tom Caputi * a level 0 block with an encrypted object type, the block is encrypted
296eb63303Tom Caputi * (see BP_IS_ENCRYPTED()). If this is a level 0 block with an unencrypted
297eb63303Tom Caputi * object type, this block is authenticated with an HMAC (see
298eb63303Tom Caputi * BP_IS_AUTHENTICATED()). Otherwise (if level > 0), this bp will use the MAC
299eb63303Tom Caputi * words to store a checksum-of-MACs from the level below (see
300eb63303Tom Caputi * BP_HAS_INDIRECT_MAC_CKSUM()). For convenience in the code, BP_IS_PROTECTED()
301eb63303Tom Caputi * refers to both encrypted and authenticated blocks and BP_USES_CRYPT()
302eb63303Tom Caputi * refers to any of these 3 kinds of blocks.
303eb63303Tom Caputi *
304eb63303Tom Caputi * The additional encryption parameters are the salt, IV, and MAC which are
305eb63303Tom Caputi * explained in greater detail in the block comment at the top of zio_crypt.c.
306eb63303Tom Caputi * The MAC occupies half of the checksum space since it serves a very similar
307eb63303Tom Caputi * purpose: to prevent data corruption on disk. The only functional difference
308eb63303Tom Caputi * is that the checksum is used to detect on-disk corruption whether or not the
309eb63303Tom Caputi * encryption key is loaded and the MAC provides additional protection against
310eb63303Tom Caputi * malicious disk tampering. We use the 3rd DVA to store the salt and first
311eb63303Tom Caputi * 64 bits of the IV. As a result encrypted blocks can only have 2 copies
312eb63303Tom Caputi * maximum instead of the normal 3. The last 32 bits of the IV are stored in
313eb63303Tom Caputi * the upper bits of what is usually the fill count. Note that only blocks at
314eb63303Tom Caputi * level 0 or -2 are ever encrypted, which allows us to guarantee that these
315eb63303Tom Caputi * 32 bits are not trampled over by other code (see zio_crypt.c for details).
316eb63303Tom Caputi * The salt and IV are not used for authenticated bps or bps with an indirect
317eb63303Tom Caputi * MAC checksum, so these blocks can utilize all 3 DVAs and the full 64 bits
318eb63303Tom Caputi * for the fill count.
319eb63303Tom Caputi */
320eb63303Tom Caputi
321eb63303Tom Caputi/*
3225d7b4d4Matthew Ahrens * "Embedded" blkptr_t's don't actually point to a block, instead they
3235d7b4d4Matthew Ahrens * have a data payload embedded in the blkptr_t itself.  See the comment
3245d7b4d4Matthew Ahrens * in blkptr.c for more details.
3255d7b4d4Matthew Ahrens *
3265d7b4d4Matthew Ahrens * The blkptr_t is laid out as follows:
3275d7b4d4Matthew Ahrens *
3285d7b4d4Matthew Ahrens *	64	56	48	40	32	24	16	8	0
3295d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3305d7b4d4Matthew Ahrens * 0	|      payload                                                  |
3315d7b4d4Matthew Ahrens * 1	|      payload                                                  |
3325d7b4d4Matthew Ahrens * 2	|      payload                                                  |
3335d7b4d4Matthew Ahrens * 3	|      payload                                                  |
3345d7b4d4Matthew Ahrens * 4	|      payload                                                  |
3355d7b4d4Matthew Ahrens * 5	|      payload                                                  |
3365d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3375d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
3385d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3395d7b4d4Matthew Ahrens * 7	|      payload                                                  |
3405d7b4d4Matthew Ahrens * 8	|      payload                                                  |
3415d7b4d4Matthew Ahrens * 9	|      payload                                                  |
3425d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3435d7b4d4Matthew Ahrens * a	|			logical birth txg			|
3445d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3455d7b4d4Matthew Ahrens * b	|      payload                                                  |
3465d7b4d4Matthew Ahrens * c	|      payload                                                  |
3475d7b4d4Matthew Ahrens * d	|      payload                                                  |
3485d7b4d4Matthew Ahrens * e	|      payload                                                  |
3495d7b4d4Matthew Ahrens * f	|      payload                                                  |
3505d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3515d7b4d4Matthew Ahrens *
3525d7b4d4Matthew Ahrens * Legend:
3535d7b4d4Matthew Ahrens *
3545d7b4d4Matthew Ahrens * payload		contains the embedded data
3555d7b4d4Matthew Ahrens * B (byteorder)	byteorder (endianness)
3565d7b4d4Matthew Ahrens * D (dedup)		padding (set to zero)
3575d7b4d4Matthew Ahrens * X			encryption (set to zero; see above)
3585d7b4d4Matthew Ahrens * E (embedded)		set to one
3595d7b4d4Matthew Ahrens * lvl			indirection level
3605d7b4d4Matthew Ahrens * type			DMU object type
3615d7b4d4Matthew Ahrens * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
3625d7b4d4Matthew Ahrens * comp			compression function of payload
3635d7b4d4Matthew Ahrens * PSIZE		size of payload after compression, in bytes
3645d7b4d4Matthew Ahrens * LSIZE		logical size of payload, in bytes
3655d7b4d4Matthew Ahrens *			note that 25 bits is enough to store the largest
3665d7b4d4Matthew Ahrens *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
3675d7b4d4Matthew Ahrens * log. birth		transaction group in which the block was logically born
3685d7b4d4Matthew Ahrens *
3695d7b4d4Matthew Ahrens * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
3705d7b4d4Matthew Ahrens * bp's they are stored in units of SPA_MINBLOCKSHIFT.
3715d7b4d4Matthew Ahrens * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
3725d7b4d4Matthew Ahrens * The B, D, X, lvl, type, and comp fields are stored the same as with normal
3735d7b4d4Matthew Ahrens * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
3745d7b4d4Matthew Ahrens * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
3755d7b4d4Matthew Ahrens * other macros, as they assert that they are only used on BP's of the correct
376eb63303Tom Caputi * "embedded-ness". Encrypted blkptr_t's cannot be embedded because they use
377eb63303Tom Caputi * the payload space for encryption parameters (see the comment above on
378eb63303Tom Caputi * how encryption parameters are stored).
3795d7b4d4Matthew Ahrens */
3805d7b4d4Matthew Ahrens
3815d7b4d4Matthew Ahrens#define	BPE_GET_ETYPE(bp)	\
3825d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3835d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
3845d7b4d4Matthew Ahrens#define	BPE_SET_ETYPE(bp, t)	do { \
3855d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3865d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, t); \
3875d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3885d7b4d4Matthew Ahrens
3895d7b4d4Matthew Ahrens#define	BPE_GET_LSIZE(bp)	\
3905d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3915d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
3925d7b4d4Matthew Ahrens#define	BPE_SET_LSIZE(bp, x)	do { \
3935d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3945d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
3955d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3965d7b4d4Matthew Ahrens
3975d7b4d4Matthew Ahrens#define	BPE_GET_PSIZE(bp)	\
3985d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3995d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
4005d7b4d4Matthew Ahrens#define	BPE_SET_PSIZE(bp, x)	do { \
4015d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
402eb63303Tom Caputi	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x);	\
4035d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
4045d7b4d4Matthew Ahrens
4055d7b4d4Matthew Ahrenstypedef enum bp_embedded_type {
4065d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_DATA,
4075d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
4085d7b4d4Matthew Ahrens	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
4095d7b4d4Matthew Ahrens} bp_embedded_type_t;
4105d7b4d4Matthew Ahrens
4115d7b4d4Matthew Ahrens#define	BPE_NUM_WORDS 14
4125d7b4d4Matthew Ahrens#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
4135d7b4d4Matthew Ahrens#define	BPE_IS_PAYLOADWORD(bp, wp) \
4145d7b4d4Matthew Ahrens	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
4155d7b4d4Matthew Ahrens
416fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
417fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
4186f79381Pavel Zakharov#define	SPA_SYNC_MIN_VDEVS 3		/* min vdevs to update during sync */
419fa9e406ahrens
42043466aaMax Grossman/*
42143466aaMax Grossman * A block is a hole when it has either 1) never been written to, or
42243466aaMax Grossman * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
42343466aaMax Grossman * without physically allocating disk space. Holes are represented in the
42443466aaMax Grossman * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
42543466aaMax Grossman * done through the BP_IS_HOLE macro. For holes, the logical size, level,
42643466aaMax Grossman * DMU object type, and birth times are all also stored for holes that
42743466aaMax Grossman * were written to at some point (i.e. were punched after having been filled).
42843466aaMax Grossman */
429b24ab67Jeff Bonwicktypedef struct blkptr {
430b24ab67Jeff Bonwick	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
431b24ab67Jeff Bonwick	uint64_t	blk_prop;	/* size, compression, type, etc	    */
432b24ab67Jeff Bonwick	uint64_t	blk_pad[2];	/* Extra space for the future	    */
433b24ab67Jeff Bonwick	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
434b24ab67Jeff Bonwick	uint64_t	blk_birth;	/* transaction group at birth	    */
435b24ab67Jeff Bonwick	uint64_t	blk_fill;	/* fill count			    */
436b24ab67Jeff Bonwick	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
437b24ab67Jeff Bonwick} blkptr_t;
438b24ab67Jeff Bonwick
439fa9e406ahrens/*
440fa9e406ahrens * Macros to get and set fields in a bp or DVA.
441fa9e406ahrens */
442fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
44343466aaMax Grossman	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
444fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
44543466aaMax Grossman	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
44643466aaMax Grossman	SPA_MINBLOCKSHIFT, 0, x)
447fa9e406ahrens
448fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
449fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
450fa9e406ahrens
45117f1128Serapheim Dimitropoulos#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, SPA_VDEVBITS)
45217f1128Serapheim Dimitropoulos#define	DVA_SET_VDEV(dva, x)	\
45317f1128Serapheim Dimitropoulos	BF64_SET((dva)->dva_word[0], 32, SPA_VDEVBITS, x)
454fa9e406ahrens
455fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
456fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
457fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
458fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
459fa9e406ahrens
460fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
461fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
462fa9e406ahrens
463fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
4645d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ?	\
4655d7b4d4Matthew Ahrens	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
4665d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4675d7b4d4Matthew Ahrens#define	BP_SET_LSIZE(bp, x)	do { \
4685d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4695d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
4705d7b4d4Matthew Ahrens	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4715d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
472fa9e406ahrens
473fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
4745d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4755d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4765d7b4d4Matthew Ahrens#define	BP_SET_PSIZE(bp, x)	do { \
4775d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4785d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
4795d7b4d4Matthew Ahrens	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4805d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
4815d7b4d4Matthew Ahrens
482dcbf3bdGeorge Wilson#define	BP_GET_COMPRESS(bp)		\
483dcbf3bdGeorge Wilson	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
484dcbf3bdGeorge Wilson#define	BP_SET_COMPRESS(bp, x)		\
485dcbf3bdGeorge Wilson	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
486fa9e406ahrens
4875d7b4d4Matthew Ahrens#define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
4885d7b4d4Matthew Ahrens#define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
489b24ab67Jeff Bonwick
4905d7b4d4Matthew Ahrens#define	BP_GET_CHECKSUM(bp)		\
4915d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
4925d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
4935d7b4d4Matthew Ahrens#define	BP_SET_CHECKSUM(bp, x)		do { \
4945d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4955d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, x); \
4965d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
497b24ab67Jeff Bonwick
498b24ab67Jeff Bonwick#define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
499b24ab67Jeff Bonwick#define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
500fa9e406ahrens
501b24ab67Jeff Bonwick#define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
502b24ab67Jeff Bonwick#define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
503fa9e406ahrens
504eb63303Tom Caputi/* encrypted, authenticated, and MAC cksum bps use the same bit */
505eb63303Tom Caputi#define	BP_USES_CRYPT(bp)		BF64_GET((bp)->blk_prop, 61, 1)
506eb63303Tom Caputi#define	BP_SET_CRYPT(bp, x)		BF64_SET((bp)->blk_prop, 61, 1, x)
507eb63303Tom Caputi
508eb63303Tom Caputi#define	BP_IS_ENCRYPTED(bp)			\
509eb63303Tom Caputi	(BP_USES_CRYPT(bp) &&			\
510eb63303Tom Caputi	BP_GET_LEVEL(bp) == 0 &&		\
511eb63303Tom Caputi	DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
512eb63303Tom Caputi
513eb63303Tom Caputi#define	BP_IS_AUTHENTICATED(bp)			\
514eb63303Tom Caputi	(BP_USES_CRYPT(bp) &&			\
515eb63303Tom Caputi	BP_GET_LEVEL(bp) == 0 &&		\
516eb63303Tom Caputi	!DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
517eb63303Tom Caputi
518eb63303Tom Caputi#define	BP_HAS_INDIRECT_MAC_CKSUM(bp)		\
519eb63303Tom Caputi	(BP_USES_CRYPT(bp) && BP_GET_LEVEL(bp) > 0)
520eb63303Tom Caputi
521eb63303Tom Caputi#define	BP_IS_PROTECTED(bp)			\
522eb63303Tom Caputi	(BP_IS_ENCRYPTED(bp) || BP_IS_AUTHENTICATED(bp))
523eb63303Tom Caputi
524b24ab67Jeff Bonwick#define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
525b24ab67Jeff Bonwick#define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
526fa9e406ahrens
52743466aaMax Grossman#define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
528b24ab67Jeff Bonwick#define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
529b24ab67Jeff Bonwick
530b24ab67Jeff Bonwick#define	BP_PHYSICAL_BIRTH(bp)		\
5315d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
5325d7b4d4Matthew Ahrens	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
533b24ab67Jeff Bonwick
534b24ab67Jeff Bonwick#define	BP_SET_BIRTH(bp, logical, physical)	\
535b24ab67Jeff Bonwick{						\
5365d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp));		\
537b24ab67Jeff Bonwick	(bp)->blk_birth = (logical);		\
538b24ab67Jeff Bonwick	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
539b24ab67Jeff Bonwick}
540fa9e406ahrens
541eb63303Tom Caputi#define	BP_GET_FILL(bp)				\
542eb63303Tom Caputi	((BP_IS_ENCRYPTED(bp)) ? BF64_GET((bp)->blk_fill, 0, 32) : \
543eb63303Tom Caputi	((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill))
544eb63303Tom Caputi
545eb63303Tom Caputi#define	BP_SET_FILL(bp, fill)			\
546eb63303Tom Caputi{						\
547eb63303Tom Caputi	if (BP_IS_ENCRYPTED(bp))			\
548eb63303Tom Caputi		BF64_SET((bp)->blk_fill, 0, 32, fill); \
549eb63303Tom Caputi	else					\
550eb63303Tom Caputi		(bp)->blk_fill = fill;		\
551eb63303Tom Caputi}
552eb63303Tom Caputi
553eb63303Tom Caputi#define	BP_GET_IV2(bp)				\
554eb63303Tom Caputi	(ASSERT(BP_IS_ENCRYPTED(bp)),		\
555eb63303Tom Caputi	BF64_GET((bp)->blk_fill, 32, 32))
556eb63303Tom Caputi#define	BP_SET_IV2(bp, iv2)			\
557eb63303Tom Caputi{						\
558eb63303Tom Caputi	ASSERT(BP_IS_ENCRYPTED(bp));		\
559eb63303Tom Caputi	BF64_SET((bp)->blk_fill, 32, 32, iv2);	\
560eb63303Tom Caputi}
5615d7b4d4Matthew Ahrens
562770499eDan Kimmel#define	BP_IS_METADATA(bp)	\
563770499eDan Kimmel	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
564770499eDan Kimmel
565fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
5665d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
5675d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
5685d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
569