spa.h revision 5cabbc6b49070407fb9610cfe73d4c0e0dea3e77
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.
30fa9e406ahrens */
31fa9e406ahrens
32fa9e406ahrens#ifndef _SYS_SPA_H
33fa9e406ahrens#define	_SYS_SPA_H
34fa9e406ahrens
35fa9e406ahrens#include <sys/avl.h>
36fa9e406ahrens#include <sys/zfs_context.h>
37fa9e406ahrens#include <sys/nvpair.h>
385cabbc6Prashanth Sreenivasa#include <sys/sysevent.h>
39fa9e406ahrens#include <sys/sysmacros.h>
40fa9e406ahrens#include <sys/types.h>
41fa9e406ahrens#include <sys/fs/zfs.h>
424ba5b96Dan Kimmel#include <sys/dmu.h>
43fa9e406ahrens
44fa9e406ahrens#ifdef	__cplusplus
45fa9e406ahrensextern "C" {
46fa9e406ahrens#endif
47fa9e406ahrens
48fa9e406ahrens/*
49fa9e406ahrens * Forward references that lots of things need.
50fa9e406ahrens */
51fa9e406ahrenstypedef struct spa spa_t;
52fa9e406ahrenstypedef struct vdev vdev_t;
53fa9e406ahrenstypedef struct metaslab metaslab_t;
54b24ab67Jeff Bonwicktypedef struct metaslab_group metaslab_group_t;
55b24ab67Jeff Bonwicktypedef struct metaslab_class metaslab_class_t;
56b24ab67Jeff Bonwicktypedef struct zio zio_t;
57fa9e406ahrenstypedef struct zilog zilog_t;
58fa94a07brendantypedef struct spa_aux_vdev spa_aux_vdev_t;
59b24ab67Jeff Bonwicktypedef struct ddt ddt_t;
60b24ab67Jeff Bonwicktypedef struct ddt_entry ddt_entry_t;
61fa9e406ahrensstruct dsl_pool;
624445fffMatthew Ahrensstruct dsl_dataset;
63fa9e406ahrens
64fa9e406ahrens/*
65fa9e406ahrens * General-purpose 32-bit and 64-bit bitfield encodings.
66fa9e406ahrens */
67fa9e406ahrens#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
68fa9e406ahrens#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
69fa9e406ahrens#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
70fa9e406ahrens#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
71fa9e406ahrens
72fa9e406ahrens#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
73fa9e406ahrens#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
74fa9e406ahrens
7543466aaMax Grossman#define	BF32_SET(x, low, len, val) do { \
7643466aaMax Grossman	ASSERT3U(val, <, 1U << (len)); \
7743466aaMax Grossman	ASSERT3U(low + len, <=, 32); \
7843466aaMax Grossman	(x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
7943466aaMax Grossman_NOTE(CONSTCOND) } while (0)
8043466aaMax Grossman
8143466aaMax Grossman#define	BF64_SET(x, low, len, val) do { \
8243466aaMax Grossman	ASSERT3U(val, <, 1ULL << (len)); \
8343466aaMax Grossman	ASSERT3U(low + len, <=, 64); \
8443466aaMax Grossman	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
8543466aaMax Grossman_NOTE(CONSTCOND) } while (0)
86fa9e406ahrens
87fa9e406ahrens#define	BF32_GET_SB(x, low, len, shift, bias)	\
88fa9e406ahrens	((BF32_GET(x, low, len) + (bias)) << (shift))
89fa9e406ahrens#define	BF64_GET_SB(x, low, len, shift, bias)	\
90fa9e406ahrens	((BF64_GET(x, low, len) + (bias)) << (shift))
91fa9e406ahrens
9243466aaMax Grossman#define	BF32_SET_SB(x, low, len, shift, bias, val) do { \
9343466aaMax Grossman	ASSERT(IS_P2ALIGNED(val, 1U << shift)); \
9443466aaMax Grossman	ASSERT3S((val) >> (shift), >=, bias); \
9543466aaMax Grossman	BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
9643466aaMax Grossman_NOTE(CONSTCOND) } while (0)
9743466aaMax Grossman#define	BF64_SET_SB(x, low, len, shift, bias, val) do { \
9843466aaMax Grossman	ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \
9943466aaMax Grossman	ASSERT3S((val) >> (shift), >=, bias); \
10043466aaMax Grossman	BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
10143466aaMax Grossman_NOTE(CONSTCOND) } while (0)
102fa9e406ahrens
103fa9e406ahrens/*
104b515258Matthew Ahrens * We currently support block sizes from 512 bytes to 16MB.
105b515258Matthew Ahrens * The benefits of larger blocks, and thus larger IO, need to be weighed
106b515258Matthew Ahrens * against the cost of COWing a giant block to modify one byte, and the
107b515258Matthew Ahrens * large latency of reading or writing a large block.
108b515258Matthew Ahrens *
109b515258Matthew Ahrens * Note that although blocks up to 16MB are supported, the recordsize
110b515258Matthew Ahrens * property can not be set larger than zfs_max_recordsize (default 1MB).
111b515258Matthew Ahrens * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
112b515258Matthew Ahrens *
113b515258Matthew Ahrens * Note that although the LSIZE field of the blkptr_t can store sizes up
114b515258Matthew Ahrens * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
115b515258Matthew Ahrens * 32MB - 512 bytes.  Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
116fa9e406ahrens */
117fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
118b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSHIFT	17
119b515258Matthew Ahrens#define	SPA_MAXBLOCKSHIFT	24
120fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
121b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)
122fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
123fa9e406ahrens
124fa9e406ahrens/*
125f7991baTim Haley * Size of block to hold the configuration data (a packed nvlist)
126f7991baTim Haley */
127ad135b5Christopher Siden#define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
128f7991baTim Haley
129f7991baTim Haley/*
130fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
131fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
132fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
133fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
134fa9e406ahrens * else that expands the ASIZE.
135fa9e406ahrens */
136fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
137fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
138fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
139fa9e406ahrens
140dcbf3bdGeorge Wilson#define	SPA_COMPRESSBITS	7
141dcbf3bdGeorge Wilson
142fa9e406ahrens/*
143fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
144fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
145fa9e406ahrens */
146fa9e406ahrenstypedef struct dva {
147fa9e406ahrens	uint64_t	dva_word[2];
148fa9e406ahrens} dva_t;
149fa9e406ahrens
150fa9e406ahrens/*
151fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
152fa9e406ahrens */
153fa9e406ahrenstypedef struct zio_cksum {
154fa9e406ahrens	uint64_t	zc_word[4];
155fa9e406ahrens} zio_cksum_t;
156fa9e406ahrens
157fa9e406ahrens/*
15845818eeMatthew Ahrens * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
15945818eeMatthew Ahrens * secret and is suitable for use in MAC algorithms as the key.
16045818eeMatthew Ahrens */
16145818eeMatthew Ahrenstypedef struct zio_cksum_salt {
16245818eeMatthew Ahrens	uint8_t		zcs_bytes[32];
16345818eeMatthew Ahrens} zio_cksum_salt_t;
16445818eeMatthew Ahrens
16545818eeMatthew Ahrens/*
166fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
167fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
168fa9e406ahrens *
169fa9e406ahrens *	64	56	48	40	32	24	16	8	0
170fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
171fa9e406ahrens * 0	|		vdev1		| GRID  |	  ASIZE		|
172fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
173fa9e406ahrens * 1	|G|			 offset1				|
174fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
175fa9e406ahrens * 2	|		vdev2		| GRID  |	  ASIZE		|
176fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
177fa9e406ahrens * 3	|G|			 offset2				|
178fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
179fa9e406ahrens * 4	|		vdev3		| GRID  |	  ASIZE		|
180fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
181fa9e406ahrens * 5	|G|			 offset3				|
182fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
1835d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
184fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
185fa9e406ahrens * 7	|			padding					|
186fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
187fa9e406ahrens * 8	|			padding					|
188fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
189b24ab67Jeff Bonwick * 9	|			physical birth txg			|
190fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
191b24ab67Jeff Bonwick * a	|			logical birth txg			|
192fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
193fa9e406ahrens * b	|			fill count				|
194fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
195fa9e406ahrens * c	|			checksum[0]				|
196fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
197fa9e406ahrens * d	|			checksum[1]				|
198fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
199fa9e406ahrens * e	|			checksum[2]				|
200fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
201fa9e406ahrens * f	|			checksum[3]				|
202fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
203fa9e406ahrens *
204fa9e406ahrens * Legend:
205fa9e406ahrens *
206fa9e406ahrens * vdev		virtual device ID
207fa9e406ahrens * offset	offset into virtual device
208fa9e406ahrens * LSIZE	logical size
209fa9e406ahrens * PSIZE	physical size (after compression)
210fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
211fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
212fa9e406ahrens * cksum	checksum function
213fa9e406ahrens * comp		compression function
214fa9e406ahrens * G		gang block indicator
215b24ab67Jeff Bonwick * B		byteorder (endianness)
216b24ab67Jeff Bonwick * D		dedup
2175d7b4d4Matthew Ahrens * X		encryption (on version 30, which is not supported)
2185d7b4d4Matthew Ahrens * E		blkptr_t contains embedded data (see below)
219fa9e406ahrens * lvl		level of indirection
220b24ab67Jeff Bonwick * type		DMU object type
2215cabbc6Prashanth Sreenivasa * phys birth	txg when dva[0] was written; zero if same as logical birth txg
2225cabbc6Prashanth Sreenivasa *              note that typically all the dva's would be written in this
2235cabbc6Prashanth Sreenivasa *              txg, but they could be different if they were moved by
2245cabbc6Prashanth Sreenivasa *              device removal.
225b24ab67Jeff Bonwick * log. birth	transaction group in which the block was logically born
226fa9e406ahrens * fill count	number of non-zero blocks under this bp
227fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
228fa9e406ahrens */
2295d7b4d4Matthew Ahrens
2305d7b4d4Matthew Ahrens/*
2315d7b4d4Matthew Ahrens * "Embedded" blkptr_t's don't actually point to a block, instead they
2325d7b4d4Matthew Ahrens * have a data payload embedded in the blkptr_t itself.  See the comment
2335d7b4d4Matthew Ahrens * in blkptr.c for more details.
2345d7b4d4Matthew Ahrens *
2355d7b4d4Matthew Ahrens * The blkptr_t is laid out as follows:
2365d7b4d4Matthew Ahrens *
2375d7b4d4Matthew Ahrens *	64	56	48	40	32	24	16	8	0
2385d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2395d7b4d4Matthew Ahrens * 0	|      payload                                                  |
2405d7b4d4Matthew Ahrens * 1	|      payload                                                  |
2415d7b4d4Matthew Ahrens * 2	|      payload                                                  |
2425d7b4d4Matthew Ahrens * 3	|      payload                                                  |
2435d7b4d4Matthew Ahrens * 4	|      payload                                                  |
2445d7b4d4Matthew Ahrens * 5	|      payload                                                  |
2455d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2465d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
2475d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2485d7b4d4Matthew Ahrens * 7	|      payload                                                  |
2495d7b4d4Matthew Ahrens * 8	|      payload                                                  |
2505d7b4d4Matthew Ahrens * 9	|      payload                                                  |
2515d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2525d7b4d4Matthew Ahrens * a	|			logical birth txg			|
2535d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2545d7b4d4Matthew Ahrens * b	|      payload                                                  |
2555d7b4d4Matthew Ahrens * c	|      payload                                                  |
2565d7b4d4Matthew Ahrens * d	|      payload                                                  |
2575d7b4d4Matthew Ahrens * e	|      payload                                                  |
2585d7b4d4Matthew Ahrens * f	|      payload                                                  |
2595d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2605d7b4d4Matthew Ahrens *
2615d7b4d4Matthew Ahrens * Legend:
2625d7b4d4Matthew Ahrens *
2635d7b4d4Matthew Ahrens * payload		contains the embedded data
2645d7b4d4Matthew Ahrens * B (byteorder)	byteorder (endianness)
2655d7b4d4Matthew Ahrens * D (dedup)		padding (set to zero)
2665d7b4d4Matthew Ahrens * X			encryption (set to zero; see above)
2675d7b4d4Matthew Ahrens * E (embedded)		set to one
2685d7b4d4Matthew Ahrens * lvl			indirection level
2695d7b4d4Matthew Ahrens * type			DMU object type
2705d7b4d4Matthew Ahrens * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
2715d7b4d4Matthew Ahrens * comp			compression function of payload
2725d7b4d4Matthew Ahrens * PSIZE		size of payload after compression, in bytes
2735d7b4d4Matthew Ahrens * LSIZE		logical size of payload, in bytes
2745d7b4d4Matthew Ahrens *			note that 25 bits is enough to store the largest
2755d7b4d4Matthew Ahrens *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
2765d7b4d4Matthew Ahrens * log. birth		transaction group in which the block was logically born
2775d7b4d4Matthew Ahrens *
2785d7b4d4Matthew Ahrens * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
2795d7b4d4Matthew Ahrens * bp's they are stored in units of SPA_MINBLOCKSHIFT.
2805d7b4d4Matthew Ahrens * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
2815d7b4d4Matthew Ahrens * The B, D, X, lvl, type, and comp fields are stored the same as with normal
2825d7b4d4Matthew Ahrens * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
2835d7b4d4Matthew Ahrens * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
2845d7b4d4Matthew Ahrens * other macros, as they assert that they are only used on BP's of the correct
2855d7b4d4Matthew Ahrens * "embedded-ness".
2865d7b4d4Matthew Ahrens */
2875d7b4d4Matthew Ahrens
2885d7b4d4Matthew Ahrens#define	BPE_GET_ETYPE(bp)	\
2895d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
2905d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
2915d7b4d4Matthew Ahrens#define	BPE_SET_ETYPE(bp, t)	do { \
2925d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
2935d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, t); \
2945d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
2955d7b4d4Matthew Ahrens
2965d7b4d4Matthew Ahrens#define	BPE_GET_LSIZE(bp)	\
2975d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
2985d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
2995d7b4d4Matthew Ahrens#define	BPE_SET_LSIZE(bp, x)	do { \
3005d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3015d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
3025d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3035d7b4d4Matthew Ahrens
3045d7b4d4Matthew Ahrens#define	BPE_GET_PSIZE(bp)	\
3055d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
3065d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
3075d7b4d4Matthew Ahrens#define	BPE_SET_PSIZE(bp, x)	do { \
3085d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
3095d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
3105d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3115d7b4d4Matthew Ahrens
3125d7b4d4Matthew Ahrenstypedef enum bp_embedded_type {
3135d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_DATA,
3145d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
3155d7b4d4Matthew Ahrens	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
3165d7b4d4Matthew Ahrens} bp_embedded_type_t;
3175d7b4d4Matthew Ahrens
3185d7b4d4Matthew Ahrens#define	BPE_NUM_WORDS 14
3195d7b4d4Matthew Ahrens#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
3205d7b4d4Matthew Ahrens#define	BPE_IS_PAYLOADWORD(bp, wp) \
3215d7b4d4Matthew Ahrens	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
3225d7b4d4Matthew Ahrens
323fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
324fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
325fa9e406ahrens
32643466aaMax Grossman/*
32743466aaMax Grossman * A block is a hole when it has either 1) never been written to, or
32843466aaMax Grossman * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
32943466aaMax Grossman * without physically allocating disk space. Holes are represented in the
33043466aaMax Grossman * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
33143466aaMax Grossman * done through the BP_IS_HOLE macro. For holes, the logical size, level,
33243466aaMax Grossman * DMU object type, and birth times are all also stored for holes that
33343466aaMax Grossman * were written to at some point (i.e. were punched after having been filled).
33443466aaMax Grossman */
335b24ab67Jeff Bonwicktypedef struct blkptr {
336b24ab67Jeff Bonwick	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
337b24ab67Jeff Bonwick	uint64_t	blk_prop;	/* size, compression, type, etc	    */
338b24ab67Jeff Bonwick	uint64_t	blk_pad[2];	/* Extra space for the future	    */
339b24ab67Jeff Bonwick	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
340b24ab67Jeff Bonwick	uint64_t	blk_birth;	/* transaction group at birth	    */
341b24ab67Jeff Bonwick	uint64_t	blk_fill;	/* fill count			    */
342b24ab67Jeff Bonwick	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
343b24ab67Jeff Bonwick} blkptr_t;
344b24ab67Jeff Bonwick
345fa9e406ahrens/*
346fa9e406ahrens * Macros to get and set fields in a bp or DVA.
347fa9e406ahrens */
348fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
34943466aaMax Grossman	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
350fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
35143466aaMax Grossman	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
35243466aaMax Grossman	SPA_MINBLOCKSHIFT, 0, x)
353fa9e406ahrens
354fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
355fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
356fa9e406ahrens
357fa9e406ahrens#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
358fa9e406ahrens#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
359fa9e406ahrens
360fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
361fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
362fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
363fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
364fa9e406ahrens
365fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
366fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
367fa9e406ahrens
368fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
3695d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ?	\
3705d7b4d4Matthew Ahrens	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
3715d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
3725d7b4d4Matthew Ahrens#define	BP_SET_LSIZE(bp, x)	do { \
3735d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
3745d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
3755d7b4d4Matthew Ahrens	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
3765d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
377fa9e406ahrens
378fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
3795d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
3805d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
3815d7b4d4Matthew Ahrens#define	BP_SET_PSIZE(bp, x)	do { \
3825d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
3835d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
3845d7b4d4Matthew Ahrens	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
3855d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3865d7b4d4Matthew Ahrens
387dcbf3bdGeorge Wilson#define	BP_GET_COMPRESS(bp)		\
388dcbf3bdGeorge Wilson	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
389dcbf3bdGeorge Wilson#define	BP_SET_COMPRESS(bp, x)		\
390dcbf3bdGeorge Wilson	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
391fa9e406ahrens
3925d7b4d4Matthew Ahrens#define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
3935d7b4d4Matthew Ahrens#define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
394b24ab67Jeff Bonwick
3955d7b4d4Matthew Ahrens#define	BP_GET_CHECKSUM(bp)		\
3965d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
3975d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
3985d7b4d4Matthew Ahrens#define	BP_SET_CHECKSUM(bp, x)		do { \
3995d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
4005d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, x); \
4015d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
402b24ab67Jeff Bonwick
403b24ab67Jeff Bonwick#define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
404b24ab67Jeff Bonwick#define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
405fa9e406ahrens
406b24ab67Jeff Bonwick#define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
407b24ab67Jeff Bonwick#define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
408fa9e406ahrens
409b24ab67Jeff Bonwick#define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
410b24ab67Jeff Bonwick#define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
411fa9e406ahrens
41243466aaMax Grossman#define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
413b24ab67Jeff Bonwick#define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
414b24ab67Jeff Bonwick
415b24ab67Jeff Bonwick#define	BP_PHYSICAL_BIRTH(bp)		\
4165d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4175d7b4d4Matthew Ahrens	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
418b24ab67Jeff Bonwick
419b24ab67Jeff Bonwick#define	BP_SET_BIRTH(bp, logical, physical)	\
420b24ab67Jeff Bonwick{						\
4215d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp));		\
422b24ab67Jeff Bonwick	(bp)->blk_birth = (logical);		\
423b24ab67Jeff Bonwick	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
424b24ab67Jeff Bonwick}
425fa9e406ahrens
4265d7b4d4Matthew Ahrens#define	BP_GET_FILL(bp) (BP_IS_EMBEDDED(bp) ? 1 : (bp)->blk_fill)
4275d7b4d4Matthew Ahrens
428770499eDan Kimmel#define	BP_IS_METADATA(bp)	\
429770499eDan Kimmel	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
430770499eDan Kimmel
431fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
4325d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4335d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
4345d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
4355d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[2]))
43699653d4eschrock
437770499eDan Kimmel#define	BP_GET_UCSIZE(bp)	\
438770499eDan Kimmel	(BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
439fa9e406ahrens
44044cd46cbillm#define	BP_GET_NDVAS(bp)	\
4415d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4425d7b4d4Matthew Ahrens	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
44344cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
44444cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
44544cd46cbillm
44644cd46cbillm#define	BP_COUNT_GANG(bp)	\
4475d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
44844cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
44944cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
4505d7b4d4Matthew Ahrens	DVA_GET_GANG(&(bp)->blk_dva[2])))
45144cd46cbillm
452fa9e406ahrens#define	DVA_EQUAL(dva1, dva2)	\
453fa9e406ahrens	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
454fa9e406ahrens	(dva1)->dva_word[0] == (dva2)->dva_word[0])
455fa9e406ahrens
456b24ab67Jeff Bonwick#define	BP_EQUAL(bp1, bp2)	\
457b24ab67Jeff Bonwick	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
4585d7b4d4Matthew Ahrens	(bp1)->blk_birth == (bp2)->blk_birth &&			\
459b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
460b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
461b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
462b24ab67Jeff Bonwick
4636b4acc8ahrens#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
4646b4acc8ahrens	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
4656b4acc8ahrens	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
4666b4acc8ahrens	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
4676b4acc8ahrens	((zc1).zc_word[3] - (zc2).zc_word[3])))
4686b4acc8ahrens
46998110f0Matthew Ahrens#define	ZIO_CHECKSUM_IS_ZERO(zc) \
47098110f0Matthew Ahrens	(0 == ((zc)->zc_word[0] | (zc)->zc_word[1] | \
47198110f0Matthew Ahrens	(zc)->zc_word[2] | (zc)->zc_word[3]))
47298110f0Matthew Ahrens
47398110f0Matthew Ahrens#define	ZIO_CHECKSUM_BSWAP(zcp)					\
47498110f0Matthew Ahrens{								\
47598110f0Matthew Ahrens	(zcp)->zc_word[0] = BSWAP_64((zcp)->zc_word[0]);	\
47698110f0Matthew Ahrens	(zcp)->zc_word[1] = BSWAP_64((zcp)->zc_word[1]);	\
47798110f0Matthew Ahrens	(zcp)->zc_word[2] = BSWAP_64((zcp)->zc_word[2]);	\
47898110f0Matthew Ahrens	(zcp)->zc_word[3] = BSWAP_64((zcp)->zc_word[3]);	\
47998110f0Matthew Ahrens}
48098110f0Matthew Ahrens
48198110f0Matthew Ahrens
482fa9e406ahrens#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
483fa9e406ahrens
484fa9e406ahrens#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
485fa9e406ahrens{						\
486fa9e406ahrens	(zcp)->zc_word[0] = w0;			\
487fa9e406ahrens	(zcp)->zc_word[1] = w1;			\
488fa9e406ahrens	(zcp)->zc_word[2] = w2;			\
489fa9e406ahrens	(zcp)->zc_word[3] = w3;			\
490fa9e406ahrens}
491fa9e406ahrens
4925d7b4d4Matthew Ahrens#define	BP_IDENTITY(bp)		(ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
4935d7b4d4Matthew Ahrens#define	BP_IS_GANG(bp)		\
4945d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
49543466aaMax Grossman#define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&	\
49643466aaMax Grossman				(dva)->dva_word[1] == 0ULL)
4975d7b4d4Matthew Ahrens#define	BP_IS_HOLE(bp) \
4985d7b4d4Matthew Ahrens	(!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
499fa9e406ahrens
5006e1f5caNeil Perrin/* BP_IS_RAIDZ(bp) assumes no block compression */
5016e1f5caNeil Perrin#define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
5026e1f5caNeil Perrin				BP_GET_PSIZE(bp))
5036e1f5caNeil Perrin
504e14bb32Jeff Bonwick#define	BP_ZERO(bp)				\
505fa9e406ahrens{						\
506fa9e406ahrens	(bp)->blk_dva[0].dva_word[0] = 0;	\
507fa9e406ahrens	(bp)->blk_dva[0].dva_word[1] = 0;	\
508fa9e406ahrens	(bp)->blk_dva[1].dva_word[0] = 0;	\
509fa9e406ahrens	(bp)->blk_dva[1].dva_word[1] = 0;	\
510fa9e406ahrens	(bp)->blk_dva[2].dva_word[0] = 0;	\
511fa9e406ahrens	(bp)->blk_dva[2].dva_word[1] = 0;	\
512fa9e406ahrens	(bp)->blk_prop = 0;			\
513fa9e406ahrens	(bp)->blk_pad[0] = 0;			\
514fa9e406ahrens	(bp)->blk_pad[1] = 0;			\
515b24ab67Jeff Bonwick	(bp)->blk_phys_birth = 0;		\
516e14bb32Jeff Bonwick	(bp)->blk_birth = 0;			\
517fa9e406ahrens	(bp)->blk_fill = 0;			\
518fa9e406ahrens	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
519fa9e406ahrens}
520fa9e406ahrens
521fa9e406ahrens#ifdef _BIG_ENDIAN
522fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(0ULL)
523fa9e406ahrens#else
52443466aaMax Grossman#define	ZFS_HOST_BYTEORDER	(1ULL)
525fa9e406ahrens#endif
526fa9e406ahrens
527fa9e406ahrens#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
528fa9e406ahrens
52944cd46cbillm#define	BP_SPRINTF_LEN	320
530fbabab8maybee
531b24ab67Jeff Bonwick/*
532b24ab67Jeff Bonwick * This macro allows code sharing between zfs, libzpool, and mdb.
533b24ab67Jeff Bonwick * 'func' is either snprintf() or mdb_snprintf().
534b24ab67Jeff Bonwick * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
535b24ab67Jeff Bonwick */
53643466aaMax Grossman#define	SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
537b24ab67Jeff Bonwick{									\
538b24ab67Jeff Bonwick	static const char *copyname[] =					\
539b24ab67Jeff Bonwick	    { "zero", "single", "double", "triple" };			\
540b24ab67Jeff Bonwick	int len = 0;							\
541b24ab67Jeff Bonwick	int copies = 0;							\
542b24ab67Jeff Bonwick									\
543b24ab67Jeff Bonwick	if (bp == NULL) {						\
54443466aaMax Grossman		len += func(buf + len, size - len, "<NULL>");		\
545b24ab67Jeff Bonwick	} else if (BP_IS_HOLE(bp)) {					\
54670163acPrakash Surya		len += func(buf + len, size - len,			\
54770163acPrakash Surya		    "HOLE [L%llu %s] "					\
54870163acPrakash Surya		    "size=%llxL birth=%lluL",				\
54970163acPrakash Surya		    (u_longlong_t)BP_GET_LEVEL(bp),			\
55070163acPrakash Surya		    type,						\
55170163acPrakash Surya		    (u_longlong_t)BP_GET_LSIZE(bp),			\
55270163acPrakash Surya		    (u_longlong_t)bp->blk_birth);			\
5535d7b4d4Matthew Ahrens	} else if (BP_IS_EMBEDDED(bp)) {				\
5545d7b4d4Matthew Ahrens		len = func(buf + len, size - len,			\
5555d7b4d4Matthew Ahrens		    "EMBEDDED [L%llu %s] et=%u %s "			\
5565d7b4d4Matthew Ahrens		    "size=%llxL/%llxP birth=%lluL",			\
5575d7b4d4Matthew Ahrens		    (u_longlong_t)BP_GET_LEVEL(bp),			\
5585d7b4d4Matthew Ahrens		    type,						\
5595d7b4d4Matthew Ahrens		    (int)BPE_GET_ETYPE(bp),				\
5605d7b4d4Matthew Ahrens		    compress,						\
5615d7b4d4Matthew Ahrens		    (u_longlong_t)BPE_GET_LSIZE(bp),			\
5625d7b4d4Matthew Ahrens		    (u_longlong_t)BPE_GET_PSIZE(bp),			\
5635d7b4d4Matthew Ahrens		    (u_longlong_t)bp->blk_birth);			\
564b24ab67Jeff Bonwick	} else {							\
565b24ab67Jeff Bonwick		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
566b24ab67Jeff Bonwick			const dva_t *dva = &bp->blk_dva[d];		\
567b24ab67Jeff Bonwick			if (DVA_IS_VALID(dva))				\
568b24ab67Jeff Bonwick				copies++;				\
569b24ab67Jeff Bonwick			len += func(buf + len, size - len,		\
570b24ab67Jeff Bonwick			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
571b24ab67Jeff Bonwick			    (u_longlong_t)DVA_GET_VDEV(dva),		\
572b24ab67Jeff Bonwick			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
573b24ab67Jeff Bonwick			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
574b24ab67Jeff Bonwick			    ws);					\
575b24ab67Jeff Bonwick		}							\
576b24ab67Jeff Bonwick		if (BP_IS_GANG(bp) &&					\
577b24ab67Jeff Bonwick		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
578b24ab67Jeff Bonwick		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
579b24ab67Jeff Bonwick			copies--;					\
580b24ab67Jeff Bonwick		len += func(buf + len, size - len,			\
581b24ab67Jeff Bonwick		    "[L%llu %s] %s %s %s %s %s %s%c"			\
582b24ab67Jeff Bonwick		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
583b24ab67Jeff Bonwick		    "cksum=%llx:%llx:%llx:%llx",			\
584b24ab67Jeff Bonwick		    (u_longlong_t)BP_GET_LEVEL(bp),			\
585b24ab67Jeff Bonwick		    type,						\
586b24ab67Jeff Bonwick		    checksum,						\
587b24ab67Jeff Bonwick		    compress,						\
588b24ab67Jeff Bonwick		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
589b24ab67Jeff Bonwick		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
590b24ab67Jeff Bonwick		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
591b24ab67Jeff Bonwick		    copyname[copies],					\
592b24ab67Jeff Bonwick		    ws,							\
593b24ab67Jeff Bonwick		    (u_longlong_t)BP_GET_LSIZE(bp),			\
594b24ab67Jeff Bonwick		    (u_longlong_t)BP_GET_PSIZE(bp),			\
595b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_birth,			\
596b24ab67Jeff Bonwick		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp),		\
5975d7b4d4Matthew Ahrens		    (u_longlong_t)BP_GET_FILL(bp),			\
598b24ab67Jeff Bonwick		    ws,							\
599b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
600b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
601b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
602b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
603b24ab67Jeff Bonwick	}								\
604b24ab67Jeff Bonwick	ASSERT(len < size);						\
605b24ab67Jeff Bonwick}
606b24ab67Jeff Bonwick
607ad23a2djohansen#define	BP_GET_BUFC_TYPE(bp)						\
608770499eDan Kimmel	(BP_IS_METADATA(bp) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
609fa9e406ahrens
6101195e68Mark J Musantetypedef enum spa_import_type {
6111195e68Mark J Musante	SPA_IMPORT_EXISTING,
6121195e68Mark J Musante	SPA_IMPORT_ASSEMBLE
6131195e68Mark J Musante} spa_import_type_t;
6141195e68Mark J Musante
615fa9e406ahrens/* state manipulation functions */
616fa9e406ahrensextern int spa_open(const char *pool, spa_t **, void *tag);
617468c413Tim Haleyextern int spa_open_rewind(const char *pool, spa_t **, void *tag,
618468c413Tim Haley    nvlist_t *policy, nvlist_t **config);
619ad135b5Christopher Sidenextern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
620ad135b5Christopher Siden    size_t buflen);
621990b485llingextern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
6224445fffMatthew Ahrens    nvlist_t *zplprops);
623051aabetaylorextern int spa_import_rootpool(char *devpath, char *devid);
6244b964adGeorge Wilsonextern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props,
6254b964adGeorge Wilson    uint64_t flags);
626fa9e406ahrensextern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
627fa9e406ahrensextern int spa_destroy(char *pool);
628394ab0cGeorge Wilsonextern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
629394ab0cGeorge Wilson    boolean_t hardforce);
630ea8dc4beschrockextern int spa_reset(char *pool);
631ea8dc4beschrockextern void spa_async_request(spa_t *spa, int flag);
632088f389ahrensextern void spa_async_unrequest(spa_t *spa, int flag);
633ea8dc4beschrockextern void spa_async_suspend(spa_t *spa);
634ea8dc4beschrockextern void spa_async_resume(spa_t *spa);
635ea8dc4beschrockextern spa_t *spa_inject_addref(char *pool);
636ea8dc4beschrockextern void spa_inject_delref(spa_t *spa);
6373f9d6adLin Lingextern void spa_scan_stat_init(spa_t *spa);
6383f9d6adLin Lingextern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
639ea8dc4beschrock
640e14bb32Jeff Bonwick#define	SPA_ASYNC_CONFIG_UPDATE	0x01
641e14bb32Jeff Bonwick#define	SPA_ASYNC_REMOVE	0x02
642e14bb32Jeff Bonwick#define	SPA_ASYNC_PROBE		0x04
643e14bb32Jeff Bonwick#define	SPA_ASYNC_RESILVER_DONE	0x08
644e14bb32Jeff Bonwick#define	SPA_ASYNC_RESILVER	0x10
645573ca77George Wilson#define	SPA_ASYNC_AUTOEXPAND	0x20
6463f9d6adLin Ling#define	SPA_ASYNC_REMOVE_DONE	0x40
6473f9d6adLin Ling#define	SPA_ASYNC_REMOVE_STOP	0x80
6483f9d6adLin Ling
6493f9d6adLin Ling/*
6503f9d6adLin Ling * Controls the behavior of spa_vdev_remove().
6513f9d6adLin Ling */
6523f9d6adLin Ling#define	SPA_REMOVE_UNSPARE	0x01
6533f9d6adLin Ling#define	SPA_REMOVE_DONE		0x02
654fa9e406ahrens
655fa9e406ahrens/* device manipulation */
656fa9e406ahrensextern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
657ea8dc4beschrockextern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
658fa9e406ahrens    int replacing);
6598ad4d6dJeff Bonwickextern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
6608ad4d6dJeff Bonwick    int replace_done);
66199653d4eschrockextern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
6623f9d6adLin Lingextern boolean_t spa_vdev_remove_active(spa_t *spa);
663c67d967eschrockextern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
6646809eb4Eric Schrockextern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
6651195e68Mark J Musanteextern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
6661195e68Mark J Musante    nvlist_t *props, boolean_t exp);
667fa9e406ahrens
66899653d4eschrock/* spare state (which is global across all pools) */
66939c2341eschrockextern void spa_spare_add(vdev_t *vd);
67039c2341eschrockextern void spa_spare_remove(vdev_t *vd);
67189a89ebllingextern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
67239c2341eschrockextern void spa_spare_activate(vdev_t *vd);
67399653d4eschrock
674fa94a07brendan/* L2ARC state (which is global across all pools) */
675fa94a07brendanextern void spa_l2cache_add(vdev_t *vd);
676fa94a07brendanextern void spa_l2cache_remove(vdev_t *vd);
677fa94a07brendanextern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
678fa94a07brendanextern void spa_l2cache_activate(vdev_t *vd);
679fa94a07brendanextern void spa_l2cache_drop(spa_t *spa);
680fa94a07brendan
6813f9d6adLin Ling/* scanning */
6823f9d6adLin Lingextern int spa_scan(spa_t *spa, pool_scan_func_t func);
6833f9d6adLin Lingextern int spa_scan_stop(spa_t *spa);
6841702cceAlek Pinchukextern int spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t flag);
685fa9e406ahrens
686fa9e406ahrens/* spa syncing */
687fa9e406ahrensextern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
688fa9e406ahrensextern void spa_sync_allpools(void);
689fa9e406ahrens
6903a737e0brendan/* spa namespace global mutex */
6913a737e0brendanextern kmutex_t spa_namespace_lock;
6923a737e0brendan
693fa9e406ahrens/*
694fa9e406ahrens * SPA configuration functions in spa_config.c
695fa9e406ahrens */
6960373e76bonwick
6970373e76bonwick#define	SPA_CONFIG_UPDATE_POOL	0
6980373e76bonwick#define	SPA_CONFIG_UPDATE_VDEVS	1
6990373e76bonwick
7005cabbc6Prashanth Sreenivasaextern void spa_write_cachefile(spa_t *, boolean_t, boolean_t);
701fa9e406ahrensextern void spa_config_load(void);
702fa9e406ahrensextern nvlist_t *spa_all_configs(uint64_t *);
703fa9e406ahrensextern void spa_config_set(spa_t *spa, nvlist_t *config);
704fa9e406ahrensextern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
705fa9e406ahrens    int getstats);
7060373e76bonwickextern void spa_config_update(spa_t *spa, int what);
707fa9e406ahrens
708fa9e406ahrens/*
709fa9e406ahrens * Miscellaneous SPA routines in spa_misc.c
710fa9e406ahrens */
711fa9e406ahrens
712fa9e406ahrens/* Namespace manipulation */
713fa9e406ahrensextern spa_t *spa_lookup(const char *name);
714468c413Tim Haleyextern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
715fa9e406ahrensextern void spa_remove(spa_t *spa);
716fa9e406ahrensextern spa_t *spa_next(spa_t *prev);
717fa9e406ahrens
718fa9e406ahrens/* Refcount functions */
719fa9e406ahrensextern void spa_open_ref(spa_t *spa, void *tag);
720fa9e406ahrensextern void spa_close(spa_t *spa, void *tag);
721bc9014eJustin Gibbsextern void spa_async_close(spa_t *spa, void *tag);
722fa9e406ahrensextern boolean_t spa_refcount_zero(spa_t *spa);
723fa9e406ahrens
7248f18d1fGeorge Wilson#define	SCL_NONE	0x00
725e14bb32Jeff Bonwick#define	SCL_CONFIG	0x01
726e14bb32Jeff Bonwick#define	SCL_STATE	0x02
727e14bb32Jeff Bonwick#define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
728e14bb32Jeff Bonwick#define	SCL_ALLOC	0x08
729e14bb32Jeff Bonwick#define	SCL_ZIO		0x10
730e14bb32Jeff Bonwick#define	SCL_FREE	0x20
731e14bb32Jeff Bonwick#define	SCL_VDEV	0x40
732e14bb32Jeff Bonwick#define	SCL_LOCKS	7
733e14bb32Jeff Bonwick#define	SCL_ALL		((1 << SCL_LOCKS) - 1)
734e14bb32Jeff Bonwick#define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
735e14bb32Jeff Bonwick
736e14bb32Jeff Bonwick/* Pool configuration locks */
737e14bb32Jeff Bonwickextern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
738e14bb32Jeff Bonwickextern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
739e14bb32Jeff Bonwickextern void spa_config_exit(spa_t *spa, int locks, void *tag);
740e14bb32Jeff Bonwickextern int spa_config_held(spa_t *spa, int locks, krw_t rw);
741fa9e406ahrens
742fa9e406ahrens/* Pool vdev add/remove lock */
743fa9e406ahrensextern uint64_t spa_vdev_enter(spa_t *spa);
74488ecc94George Wilsonextern uint64_t spa_vdev_config_enter(spa_t *spa);
74588ecc94George Wilsonextern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
74688ecc94George Wilson    int error, char *tag);
747fa9e406ahrensextern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
748fa9e406ahrens
749e14bb32Jeff Bonwick/* Pool vdev state change lock */
7508f18d1fGeorge Wilsonextern void spa_vdev_state_enter(spa_t *spa, int oplock);
751e14bb32Jeff Bonwickextern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
752e14bb32Jeff Bonwick
753b24ab67Jeff Bonwick/* Log state */
754b24ab67Jeff Bonwicktypedef enum spa_log_state {
755b24ab67Jeff Bonwick	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
756b24ab67Jeff Bonwick	SPA_LOG_MISSING,	/* missing log(s) */
757b24ab67Jeff Bonwick	SPA_LOG_CLEAR,		/* clear the log(s) */
758b24ab67Jeff Bonwick	SPA_LOG_GOOD,		/* log(s) are good */
759b24ab67Jeff Bonwick} spa_log_state_t;
760b24ab67Jeff Bonwick
761b24ab67Jeff Bonwickextern spa_log_state_t spa_get_log_state(spa_t *spa);
762b24ab67Jeff Bonwickextern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
7635cabbc6Prashanth Sreenivasaextern int spa_reset_logs(spa_t *spa);
764b24ab67Jeff Bonwick
765b24ab67Jeff Bonwick/* Log claim callback */
766b24ab67Jeff Bonwickextern void spa_claim_notify(zio_t *zio);
767b24ab67Jeff Bonwick
768fa9e406ahrens/* Accessor functions */
76988b7b0fMatthew Ahrensextern boolean_t spa_shutting_down(spa_t *spa);
770fa9e406ahrensextern struct dsl_pool *spa_get_dsl(spa_t *spa);
771ad135b5Christopher Sidenextern boolean_t spa_is_initializing(spa_t *spa);
7725cabbc6Prashanth Sreenivasaextern boolean_t spa_indirect_vdevs_loaded(spa_t *spa);
773fa9e406ahrensextern blkptr_t *spa_get_rootblkptr(spa_t *spa);
774fa9e406ahrensextern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
775fa9e406ahrensextern void spa_altroot(spa_t *, char *, size_t);
776fa9e406ahrensextern int spa_sync_pass(spa_t *spa);
777fa9e406ahrensextern char *spa_name(spa_t *spa);
778fa9e406ahrensextern uint64_t spa_guid(spa_t *spa);
779e9103aaGarrett D'Amoreextern uint64_t spa_load_guid(spa_t *spa);
780fa9e406ahrensextern uint64_t spa_last_synced_txg(spa_t *spa);
781fa9e406ahrensextern uint64_t spa_first_txg(spa_t *spa);
782b24ab67Jeff Bonwickextern uint64_t spa_syncing_txg(spa_t *spa);
7833991b53George Wilsonextern uint64_t spa_final_dirty_txg(spa_t *spa);
78499653d4eschrockextern uint64_t spa_version(spa_t *spa);
78588b7b0fMatthew Ahrensextern pool_state_t spa_state(spa_t *spa);
786b16da2eGeorge Wilsonextern spa_load_state_t spa_load_state(spa_t *spa);
787fa9e406ahrensextern uint64_t spa_freeze_txg(spa_t *spa);
78861e255cMatthew Ahrensextern uint64_t spa_get_worst_case_asize(spa_t *spa, uint64_t lsize);
789485bbbfGeorge Wilsonextern uint64_t spa_get_dspace(spa_t *spa);
790c39f2c8Christopher Sidenextern uint64_t spa_get_slop_space(spa_t *spa);
791485bbbfGeorge Wilsonextern void spa_update_dspace(spa_t *spa);
79244cd46cbillmextern uint64_t spa_version(spa_t *spa);
793b24ab67Jeff Bonwickextern boolean_t spa_deflate(spa_t *spa);
794b24ab67Jeff Bonwickextern metaslab_class_t *spa_normal_class(spa_t *spa);
795b24ab67Jeff Bonwickextern metaslab_class_t *spa_log_class(spa_t *spa);
796bc9014eJustin Gibbsextern void spa_evicting_os_register(spa_t *, objset_t *os);
797bc9014eJustin Gibbsextern void spa_evicting_os_deregister(spa_t *, objset_t *os);
798bc9014eJustin Gibbsextern void spa_evicting_os_wait(spa_t *spa);
79944cd46cbillmextern int spa_max_replication(spa_t *spa);
8003f9d6adLin Lingextern int spa_prev_software_version(spa_t *spa);
801fa9e406ahrensextern int spa_busy(void);
8020a4e951gwextern uint8_t spa_get_failmode(spa_t *spa);
803e14bb32Jeff Bonwickextern boolean_t spa_suspended(spa_t *spa);
804b24ab67Jeff Bonwickextern uint64_t spa_bootfs(spa_t *spa);
805b24ab67Jeff Bonwickextern uint64_t spa_delegation(spa_t *spa);
806b24ab67Jeff Bonwickextern objset_t *spa_meta_objset(spa_t *spa);
807283b846George.Wilsonextern uint64_t spa_deadman_synctime(spa_t *spa);
808fa9e406ahrens
809fa9e406ahrens/* Miscellaneous support routines */
81043466aaMax Grossmanextern void spa_activate_mos_feature(spa_t *spa, const char *feature,
81143466aaMax Grossman    dmu_tx_t *tx);
812ad135b5Christopher Sidenextern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
813fa9e406ahrensextern int spa_rename(const char *oldname, const char *newname);
814f9af39bGeorge Wilsonextern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
815fa9e406ahrensextern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
816fa9e406ahrensextern char *spa_strdup(const char *);
817fa9e406ahrensextern void spa_strfree(char *);
818fa9e406ahrensextern uint64_t spa_get_random(uint64_t range);
8191195e68Mark J Musanteextern uint64_t spa_generate_guid(spa_t *spa);
82043466aaMax Grossmanextern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
821fa9e406ahrensextern void spa_freeze(spa_t *spa);
822e9103aaGarrett D'Amoreextern int spa_change_guid(spa_t *spa);
823990b485llingextern void spa_upgrade(spa_t *spa, uint64_t version);
824fa9e406ahrensextern void spa_evict_all(void);
825c5904d1eschrockextern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
826c5904d1eschrock    boolean_t l2cache);
82799653d4eschrockextern boolean_t spa_has_spare(spa_t *, uint64_t guid);
828