spa.h revision eb633035c80613ec93d62f90482837adaaf21a0a
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/*
127f7991baTim Haley * Size of block to hold the configuration data (a packed nvlist)
128f7991baTim Haley */
129ad135b5Christopher Siden#define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
130f7991baTim Haley
131f7991baTim Haley/*
132fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
133fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
134fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
135fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
136fa9e406ahrens * else that expands the ASIZE.
137fa9e406ahrens */
138fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
139fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
140fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
141fa9e406ahrens
142dcbf3bdGeorge Wilson#define	SPA_COMPRESSBITS	7
14317f1128Serapheim Dimitropoulos#define	SPA_VDEVBITS		24
144dcbf3bdGeorge Wilson
145fa9e406ahrens/*
146fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
147fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
148fa9e406ahrens */
149fa9e406ahrenstypedef struct dva {
150fa9e406ahrens	uint64_t	dva_word[2];
151fa9e406ahrens} dva_t;
152fa9e406ahrens
153fa9e406ahrens/*
154fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
155fa9e406ahrens */
156fa9e406ahrenstypedef struct zio_cksum {
157fa9e406ahrens	uint64_t	zc_word[4];
158fa9e406ahrens} zio_cksum_t;
159fa9e406ahrens
160fa9e406ahrens/*
16145818eeMatthew Ahrens * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
16245818eeMatthew Ahrens * secret and is suitable for use in MAC algorithms as the key.
16345818eeMatthew Ahrens */
16445818eeMatthew Ahrenstypedef struct zio_cksum_salt {
16545818eeMatthew Ahrens	uint8_t		zcs_bytes[32];
16645818eeMatthew Ahrens} zio_cksum_salt_t;
16745818eeMatthew Ahrens
16845818eeMatthew Ahrens/*
169fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
170fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
171fa9e406ahrens *
172fa9e406ahrens *	64	56	48	40	32	24	16	8	0
173fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
17417f1128Serapheim Dimitropoulos * 0	|  pad  |	  vdev1         | GRID  |	  ASIZE		|
175fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
176fa9e406ahrens * 1	|G|			 offset1				|
177fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
17817f1128Serapheim Dimitropoulos * 2	|  pad  |	  vdev2         | GRID  |	  ASIZE		|
179fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
180fa9e406ahrens * 3	|G|			 offset2				|
181fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
18217f1128Serapheim Dimitropoulos * 4	|  pad  |	  vdev3         | GRID  |	  ASIZE		|
183fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
184fa9e406ahrens * 5	|G|			 offset3				|
185fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
1865d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
187fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
188fa9e406ahrens * 7	|			padding					|
189fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
190fa9e406ahrens * 8	|			padding					|
191fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
192b24ab67Jeff Bonwick * 9	|			physical birth txg			|
193fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
194b24ab67Jeff Bonwick * a	|			logical birth txg			|
195fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
196fa9e406ahrens * b	|			fill count				|
197fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
198fa9e406ahrens * c	|			checksum[0]				|
199fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
200fa9e406ahrens * d	|			checksum[1]				|
201fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
202fa9e406ahrens * e	|			checksum[2]				|
203fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
204fa9e406ahrens * f	|			checksum[3]				|
205fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
206fa9e406ahrens *
207fa9e406ahrens * Legend:
208fa9e406ahrens *
209fa9e406ahrens * vdev		virtual device ID
210fa9e406ahrens * offset	offset into virtual device
211fa9e406ahrens * LSIZE	logical size
212fa9e406ahrens * PSIZE	physical size (after compression)
213fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
214fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
215fa9e406ahrens * cksum	checksum function
216fa9e406ahrens * comp		compression function
217fa9e406ahrens * G		gang block indicator
218b24ab67Jeff Bonwick * B		byteorder (endianness)
219b24ab67Jeff Bonwick * D		dedup
220eb63303Tom Caputi * X		encryption
2215d7b4d4Matthew Ahrens * E		blkptr_t contains embedded data (see below)
222fa9e406ahrens * lvl		level of indirection
223b24ab67Jeff Bonwick * type		DMU object type
2245cabbc6Prashanth Sreenivasa * phys birth	txg when dva[0] was written; zero if same as logical birth txg
2255cabbc6Prashanth Sreenivasa *              note that typically all the dva's would be written in this
2265cabbc6Prashanth Sreenivasa *              txg, but they could be different if they were moved by
2275cabbc6Prashanth Sreenivasa *              device removal.
228b24ab67Jeff Bonwick * log. birth	transaction group in which the block was logically born
229fa9e406ahrens * fill count	number of non-zero blocks under this bp
230fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
231fa9e406ahrens */
2325d7b4d4Matthew Ahrens
2335d7b4d4Matthew Ahrens/*
234eb63303Tom Caputi * The blkptr_t's of encrypted blocks also need to store the encryption
235eb63303Tom Caputi * parameters so that the block can be decrypted. This layout is as follows:
236eb63303Tom Caputi *
237eb63303Tom Caputi *	64	56	48	40	32	24	16	8	0
238eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
239eb63303Tom Caputi * 0	|		vdev1		| GRID  |	  ASIZE		|
240eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
241eb63303Tom Caputi * 1	|G|			 offset1				|
242eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
243eb63303Tom Caputi * 2	|		vdev2		| GRID  |	  ASIZE		|
244eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
245eb63303Tom Caputi * 3	|G|			 offset2				|
246eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
247eb63303Tom Caputi * 4	|			salt					|
248eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
249eb63303Tom Caputi * 5	|			IV1					|
250eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
251eb63303Tom Caputi * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
252eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
253eb63303Tom Caputi * 7	|			padding					|
254eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
255eb63303Tom Caputi * 8	|			padding					|
256eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
257eb63303Tom Caputi * 9	|			physical birth txg			|
258eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
259eb63303Tom Caputi * a	|			logical birth txg			|
260eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
261eb63303Tom Caputi * b	|		IV2		|	    fill count		|
262eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
263eb63303Tom Caputi * c	|			checksum[0]				|
264eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
265eb63303Tom Caputi * d	|			checksum[1]				|
266eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
267eb63303Tom Caputi * e	|			MAC[0]					|
268eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
269eb63303Tom Caputi * f	|			MAC[1]					|
270eb63303Tom Caputi *	+-------+-------+-------+-------+-------+-------+-------+-------+
271eb63303Tom Caputi *
272eb63303Tom Caputi * Legend:
273eb63303Tom Caputi *
274eb63303Tom Caputi * salt		Salt for generating encryption keys
275eb63303Tom Caputi * IV1		First 64 bits of encryption IV
276eb63303Tom Caputi * X		Block requires encryption handling (set to 1)
277eb63303Tom Caputi * E		blkptr_t contains embedded data (set to 0, see below)
278eb63303Tom Caputi * fill count	number of non-zero blocks under this bp (truncated to 32 bits)
279eb63303Tom Caputi * IV2		Last 32 bits of encryption IV
280eb63303Tom Caputi * checksum[2]	128-bit checksum of the data this bp describes
281eb63303Tom Caputi * MAC[2]	128-bit message authentication code for this data
282eb63303Tom Caputi *
283eb63303Tom Caputi * The X bit being set indicates that this block is one of 3 types. If this is
284eb63303Tom Caputi * a level 0 block with an encrypted object type, the block is encrypted
285eb63303Tom Caputi * (see BP_IS_ENCRYPTED()). If this is a level 0 block with an unencrypted
286eb63303Tom Caputi * object type, this block is authenticated with an HMAC (see
287eb63303Tom Caputi * BP_IS_AUTHENTICATED()). Otherwise (if level > 0), this bp will use the MAC
288eb63303Tom Caputi * words to store a checksum-of-MACs from the level below (see
289eb63303Tom Caputi * BP_HAS_INDIRECT_MAC_CKSUM()). For convenience in the code, BP_IS_PROTECTED()
290eb63303Tom Caputi * refers to both encrypted and authenticated blocks and BP_USES_CRYPT()
291eb63303Tom Caputi * refers to any of these 3 kinds of blocks.
292eb63303Tom Caputi *
293eb63303Tom Caputi * The additional encryption parameters are the salt, IV, and MAC which are
294eb63303Tom Caputi * explained in greater detail in the block comment at the top of zio_crypt.c.
295eb63303Tom Caputi * The MAC occupies half of the checksum space since it serves a very similar
296eb63303Tom Caputi * purpose: to prevent data corruption on disk. The only functional difference
297eb63303Tom Caputi * is that the checksum is used to detect on-disk corruption whether or not the
298eb63303Tom Caputi * encryption key is loaded and the MAC provides additional protection against
299eb63303Tom Caputi * malicious disk tampering. We use the 3rd DVA to store the salt and first
300eb63303Tom Caputi * 64 bits of the IV. As a result encrypted blocks can only have 2 copies
301eb63303Tom Caputi * maximum instead of the normal 3. The last 32 bits of the IV are stored in
302eb63303Tom Caputi * the upper bits of what is usually the fill count. Note that only blocks at
303eb63303Tom Caputi * level 0 or -2 are ever encrypted, which allows us to guarantee that these
304eb63303Tom Caputi * 32 bits are not trampled over by other code (see zio_crypt.c for details).
305eb63303Tom Caputi * The salt and IV are not used for authenticated bps or bps with an indirect
306eb63303Tom Caputi * MAC checksum, so these blocks can utilize all 3 DVAs and the full 64 bits
307eb63303Tom Caputi * for the fill count.
308eb63303Tom Caputi */
309eb63303Tom Caputi
310eb63303Tom Caputi/*
3115d7b4d4Matthew Ahrens * "Embedded" blkptr_t's don't actually point to a block, instead they
3125d7b4d4Matthew Ahrens * have a data payload embedded in the blkptr_t itself.  See the comment
3135d7b4d4Matthew Ahrens * in blkptr.c for more details.
3145d7b4d4Matthew Ahrens *
3155d7b4d4Matthew Ahrens * The blkptr_t is laid out as follows:
3165d7b4d4Matthew Ahrens *
3175d7b4d4Matthew Ahrens *	64	56	48	40	32	24	16	8	0
3185d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3195d7b4d4Matthew Ahrens * 0	|      payload                                                  |
3205d7b4d4Matthew Ahrens * 1	|      payload                                                  |
3215d7b4d4Matthew Ahrens * 2	|      payload                                                  |
3225d7b4d4Matthew Ahrens * 3	|      payload                                                  |
3235d7b4d4Matthew Ahrens * 4	|      payload                                                  |
3245d7b4d4Matthew Ahrens * 5	|      payload                                                  |
3255d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3265d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
3275d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3285d7b4d4Matthew Ahrens * 7	|      payload                                                  |
3295d7b4d4Matthew Ahrens * 8	|      payload                                                  |
3305d7b4d4Matthew Ahrens * 9	|      payload                                                  |
3315d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3325d7b4d4Matthew Ahrens * a	|			logical birth txg			|
3335d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3345d7b4d4Matthew Ahrens * b	|      payload                                                  |
3355d7b4d4Matthew Ahrens * c	|      payload                                                  |
3365d7b4d4Matthew Ahrens * d	|      payload                                                  |
3375d7b4d4Matthew Ahrens * e	|      payload                                                  |
3385d7b4d4Matthew Ahrens * f	|      payload                                                  |
3395d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
3405d7b4d4Matthew Ahrens *
3415d7b4d4Matthew Ahrens * Legend:
3425d7b4d4Matthew Ahrens *
3435d7b4d4Matthew Ahrens * payload		contains the embedded data
3445d7b4d4Matthew Ahrens * B (byteorder)	byteorder (endianness)
3455d7b4d4Matthew Ahrens * D (dedup)		padding (set to zero)
3465d7b4d4Matthew Ahrens * X			encryption (set to zero; see above)
3475d7b4d4Matthew Ahrens * E (embedded)		set to one
3485d7b4d4Matthew Ahrens * lvl			indirection level
3495d7b4d4Matthew Ahrens * type			DMU object type
3505d7b4d4Matthew Ahrens * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
3515d7b4d4Matthew Ahrens * comp			compression function of payload
3525d7b4d4Matthew Ahrens * PSIZE		size of payload after compression, in bytes
3535d7b4d4Matthew Ahrens * LSIZE		logical size of payload, in bytes
3545d7b4d4Matthew Ahrens *			note that 25 bits is enough to store the largest
3555d7b4d4Matthew Ahrens *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
3565d7b4d4Matthew Ahrens * log. birth		transaction group in which the block was logically born
3575d7b4d4Matthew Ahrens *
3585d7b4d4Matthew Ahrens * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
3595d7b4d4Matthew Ahrens * bp's they are stored in units of SPA_MINBLOCKSHIFT.
3605d7b4d4Matthew Ahrens * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
3615d7b4d4Matthew Ahrens * The B, D, X, lvl, type, and comp fields are stored the same as with normal
3625d7b4d4Matthew Ahrens * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
3635d7b4d4Matthew Ahrens * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
3645d7b4d4Matthew Ahrens * other macros, as they assert that they are only used on BP's of the correct
365eb63303Tom Caputi * "embedded-ness". Encrypted blkptr_t's cannot be embedded because they use
366eb63303Tom Caputi * the payload space for encryption parameters (see the comment above on
367eb63303Tom Caputi * how encryption parameters are stored).
3685d7b4d4Matthew Ahrens */
3695d7b4d4Matthew Ahrens
3705d7b4d4Matthew Ahrens#define	BPE_GET_ETYPE(bp)	\
3715d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3725d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
3735d7b4d4Matthew Ahrens#define	BPE_SET_ETYPE(bp, t)	do { \
3745d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3755d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, t); \
3765d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3775d7b4d4Matthew Ahrens
3785d7b4d4Matthew Ahrens#define	BPE_GET_LSIZE(bp)	\
3795d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3805d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
3815d7b4d4Matthew Ahrens#define	BPE_SET_LSIZE(bp, x)	do { \
3825d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3835d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
3845d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3855d7b4d4Matthew Ahrens
3865d7b4d4Matthew Ahrens#define	BPE_GET_PSIZE(bp)	\
3875d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3885d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
3895d7b4d4Matthew Ahrens#define	BPE_SET_PSIZE(bp, x)	do { \
3905d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
391eb63303Tom Caputi	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x);	\
3925d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3935d7b4d4Matthew Ahrens
3945d7b4d4Matthew Ahrenstypedef enum bp_embedded_type {
3955d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_DATA,
3965d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
3975d7b4d4Matthew Ahrens	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
3985d7b4d4Matthew Ahrens} bp_embedded_type_t;
3995d7b4d4Matthew Ahrens
4005d7b4d4Matthew Ahrens#define	BPE_NUM_WORDS 14
4015d7b4d4Matthew Ahrens#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
4025d7b4d4Matthew Ahrens#define	BPE_IS_PAYLOADWORD(bp, wp) \
4035d7b4d4Matthew Ahrens	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
4045d7b4d4Matthew Ahrens
405fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
406fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
4076f79381Pavel Zakharov#define	SPA_SYNC_MIN_VDEVS 3		/* min vdevs to update during sync */
408fa9e406ahrens
40943466aaMax Grossman/*
41043466aaMax Grossman * A block is a hole when it has either 1) never been written to, or
41143466aaMax Grossman * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
41243466aaMax Grossman * without physically allocating disk space. Holes are represented in the
41343466aaMax Grossman * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
41443466aaMax Grossman * done through the BP_IS_HOLE macro. For holes, the logical size, level,
41543466aaMax Grossman * DMU object type, and birth times are all also stored for holes that
41643466aaMax Grossman * were written to at some point (i.e. were punched after having been filled).
41743466aaMax Grossman */
418b24ab67Jeff Bonwicktypedef struct blkptr {
419b24ab67Jeff Bonwick	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
420b24ab67Jeff Bonwick	uint64_t	blk_prop;	/* size, compression, type, etc	    */
421b24ab67Jeff Bonwick	uint64_t	blk_pad[2];	/* Extra space for the future	    */
422b24ab67Jeff Bonwick	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
423b24ab67Jeff Bonwick	uint64_t	blk_birth;	/* transaction group at birth	    */
424b24ab67Jeff Bonwick	uint64_t	blk_fill;	/* fill count			    */
425b24ab67Jeff Bonwick	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
426b24ab67Jeff Bonwick} blkptr_t;
427b24ab67Jeff Bonwick
428fa9e406ahrens/*
429fa9e406ahrens * Macros to get and set fields in a bp or DVA.
430fa9e406ahrens */
431fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
43243466aaMax Grossman	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
433fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
43443466aaMax Grossman	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
43543466aaMax Grossman	SPA_MINBLOCKSHIFT, 0, x)
436fa9e406ahrens
437fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
438fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
439fa9e406ahrens
44017f1128Serapheim Dimitropoulos#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, SPA_VDEVBITS)
44117f1128Serapheim Dimitropoulos#define	DVA_SET_VDEV(dva, x)	\
44217f1128Serapheim Dimitropoulos	BF64_SET((dva)->dva_word[0], 32, SPA_VDEVBITS, x)
443fa9e406ahrens
444fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
445fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
446fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
447fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
448fa9e406ahrens
449fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
450fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
451fa9e406ahrens
452fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
4535d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ?	\
4545d7b4d4Matthew Ahrens	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
4555d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4565d7b4d4Matthew Ahrens#define	BP_SET_LSIZE(bp, x)	do { \
4575d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4585d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
4595d7b4d4Matthew Ahrens	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4605d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
461fa9e406ahrens
462fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
4635d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4645d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
4655d7b4d4Matthew Ahrens#define	BP_SET_PSIZE(bp, x)	do { \
4665d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4675d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
4685d7b4d4Matthew Ahrens	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
4695d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
4705d7b4d4Matthew Ahrens
471dcbf3bdGeorge Wilson#define	BP_GET_COMPRESS(bp)		\
472dcbf3bdGeorge Wilson	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
473dcbf3bdGeorge Wilson#define	BP_SET_COMPRESS(bp, x)		\
474dcbf3bdGeorge Wilson	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
475fa9e406ahrens
4765d7b4d4Matthew Ahrens#define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
4775d7b4d4Matthew Ahrens#define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
478b24ab67Jeff Bonwick
4795d7b4d4Matthew Ahrens#define	BP_GET_CHECKSUM(bp)		\
4805d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
4815d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
4825d7b4d4Matthew Ahrens#define	BP_SET_CHECKSUM(bp, x)		do { \
4835d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4845d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, x); \
4855d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
486b24ab67Jeff Bonwick
487b24ab67Jeff Bonwick#define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
488b24ab67Jeff Bonwick#define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
489fa9e406ahrens
490b24ab67Jeff Bonwick#define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
491b24ab67Jeff Bonwick#define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
492fa9e406ahrens
493eb63303Tom Caputi/* encrypted, authenticated, and MAC cksum bps use the same bit */
494eb63303Tom Caputi#define	BP_USES_CRYPT(bp)		BF64_GET((bp)->blk_prop, 61, 1)
495eb63303Tom Caputi#define	BP_SET_CRYPT(bp, x)		BF64_SET((bp)->blk_prop, 61, 1, x)
496eb63303Tom Caputi
497eb63303Tom Caputi#define	BP_IS_ENCRYPTED(bp)			\
498eb63303Tom Caputi	(BP_USES_CRYPT(bp) &&			\
499eb63303Tom Caputi	BP_GET_LEVEL(bp) == 0 &&		\
500eb63303Tom Caputi	DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
501eb63303Tom Caputi
502eb63303Tom Caputi#define	BP_IS_AUTHENTICATED(bp)			\
503eb63303Tom Caputi	(BP_USES_CRYPT(bp) &&			\
504eb63303Tom Caputi	BP_GET_LEVEL(bp) == 0 &&		\
505eb63303Tom Caputi	!DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
506eb63303Tom Caputi
507eb63303Tom Caputi#define	BP_HAS_INDIRECT_MAC_CKSUM(bp)		\
508eb63303Tom Caputi	(BP_USES_CRYPT(bp) && BP_GET_LEVEL(bp) > 0)
509eb63303Tom Caputi
510eb63303Tom Caputi#define	BP_IS_PROTECTED(bp)			\
511eb63303Tom Caputi	(BP_IS_ENCRYPTED(bp) || BP_IS_AUTHENTICATED(bp))
512eb63303Tom Caputi
513b24ab67Jeff Bonwick#define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
514b24ab67Jeff Bonwick#define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
515fa9e406ahrens
51643466aaMax Grossman#define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
517b24ab67Jeff Bonwick#define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
518b24ab67Jeff Bonwick
519b24ab67Jeff Bonwick#define	BP_PHYSICAL_BIRTH(bp)		\
5205d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
5215d7b4d4Matthew Ahrens	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
522b24ab67Jeff Bonwick
523b24ab67Jeff Bonwick#define	BP_SET_BIRTH(bp, logical, physical)	\
524b24ab67Jeff Bonwick{						\
5255d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp));		\
526b24ab67Jeff Bonwick	(bp)->blk_birth = (logical);		\
527b24ab67Jeff Bonwick	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
528b24ab67Jeff Bonwick}
529fa9e406ahrens
530eb63303Tom Caputi#define	BP_GET_FILL(bp)				\
531eb63303Tom Caputi	((BP_IS_ENCRYPTED(bp)) ? BF64_GET((bp)->blk_fill, 0, 32) : \
532eb63303Tom Caputi	((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill))
533eb63303Tom Caputi
534eb63303Tom Caputi#define	BP_SET_FILL(bp, fill)			\
535eb63303Tom Caputi{						\
536eb63303Tom Caputi	if (BP_IS_ENCRYPTED(bp))			\
537eb63303Tom Caputi		BF64_SET((bp)->blk_fill, 0, 32, fill); \
538eb63303Tom Caputi	else					\
539eb63303Tom Caputi		(bp)->blk_fill = fill;		\
540eb63303Tom Caputi}
541eb63303Tom Caputi
542eb63303Tom Caputi#define	BP_GET_IV2(bp)				\
543eb63303Tom Caputi	(ASSERT(BP_IS_ENCRYPTED(bp)),		\
544eb63303Tom Caputi	BF64_GET((bp)->blk_fill, 32, 32))
545eb63303Tom Caputi#define	BP_SET_IV2(bp, iv2)			\
546eb63303Tom Caputi{						\
547eb63303Tom Caputi	ASSERT(BP_IS_ENCRYPTED(bp));		\
548eb63303Tom Caputi	BF64_SET((bp)->blk_fill, 32, 32, iv2);	\
549eb63303Tom Caputi}
5505d7b4d4Matthew Ahrens
551770499eDan Kimmel#define	BP_IS_METADATA(bp)	\
552770499eDan Kimmel	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
553770499eDan Kimmel
554fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
5555d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
5565d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
5575d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
558eb63303Tom Caputi	(DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
55999653d4eschrock
560770499eDan Kimmel#define	BP_GET_UCSIZE(bp)	\
561770499eDan Kimmel	(BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
562fa9e406ahrens
56344cd46cbillm#define	BP_GET_NDVAS(bp)	\
5645d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
5655d7b4d4Matthew Ahrens	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
56644cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
567eb63303Tom Caputi	(!!DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
56844cd46cbillm
56944cd46cbillm#define	BP_COUNT_GANG(bp)	\
5705d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
57144cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
57244cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
573