spa.h revision 45818ee124adeaaf947698996b4f4c722afc6d1f
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.
237802d7bMatthew Ahrens * Copyright (c) 2011, 2014 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.
27fa9e406ahrens */
28fa9e406ahrens
29fa9e406ahrens#ifndef _SYS_SPA_H
30fa9e406ahrens#define	_SYS_SPA_H
31fa9e406ahrens
32fa9e406ahrens#include <sys/avl.h>
33fa9e406ahrens#include <sys/zfs_context.h>
34fa9e406ahrens#include <sys/nvpair.h>
35fa9e406ahrens#include <sys/sysmacros.h>
36fa9e406ahrens#include <sys/types.h>
37fa9e406ahrens#include <sys/fs/zfs.h>
38fa9e406ahrens
39fa9e406ahrens#ifdef	__cplusplus
40fa9e406ahrensextern "C" {
41fa9e406ahrens#endif
42fa9e406ahrens
43fa9e406ahrens/*
44fa9e406ahrens * Forward references that lots of things need.
45fa9e406ahrens */
46fa9e406ahrenstypedef struct spa spa_t;
47fa9e406ahrenstypedef struct vdev vdev_t;
48fa9e406ahrenstypedef struct metaslab metaslab_t;
49b24ab67Jeff Bonwicktypedef struct metaslab_group metaslab_group_t;
50b24ab67Jeff Bonwicktypedef struct metaslab_class metaslab_class_t;
51b24ab67Jeff Bonwicktypedef struct zio zio_t;
52fa9e406ahrenstypedef struct zilog zilog_t;
53fa94a07brendantypedef struct spa_aux_vdev spa_aux_vdev_t;
54b24ab67Jeff Bonwicktypedef struct ddt ddt_t;
55b24ab67Jeff Bonwicktypedef struct ddt_entry ddt_entry_t;
56fa9e406ahrensstruct dsl_pool;
574445fffMatthew Ahrensstruct dsl_dataset;
58fa9e406ahrens
59fa9e406ahrens/*
60fa9e406ahrens * General-purpose 32-bit and 64-bit bitfield encodings.
61fa9e406ahrens */
62fa9e406ahrens#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
63fa9e406ahrens#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
64fa9e406ahrens#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
65fa9e406ahrens#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
66fa9e406ahrens
67fa9e406ahrens#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
68fa9e406ahrens#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
69fa9e406ahrens
7043466aaMax Grossman#define	BF32_SET(x, low, len, val) do { \
7143466aaMax Grossman	ASSERT3U(val, <, 1U << (len)); \
7243466aaMax Grossman	ASSERT3U(low + len, <=, 32); \
7343466aaMax Grossman	(x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
7443466aaMax Grossman_NOTE(CONSTCOND) } while (0)
7543466aaMax Grossman
7643466aaMax Grossman#define	BF64_SET(x, low, len, val) do { \
7743466aaMax Grossman	ASSERT3U(val, <, 1ULL << (len)); \
7843466aaMax Grossman	ASSERT3U(low + len, <=, 64); \
7943466aaMax Grossman	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
8043466aaMax Grossman_NOTE(CONSTCOND) } while (0)
81fa9e406ahrens
82fa9e406ahrens#define	BF32_GET_SB(x, low, len, shift, bias)	\
83fa9e406ahrens	((BF32_GET(x, low, len) + (bias)) << (shift))
84fa9e406ahrens#define	BF64_GET_SB(x, low, len, shift, bias)	\
85fa9e406ahrens	((BF64_GET(x, low, len) + (bias)) << (shift))
86fa9e406ahrens
8743466aaMax Grossman#define	BF32_SET_SB(x, low, len, shift, bias, val) do { \
8843466aaMax Grossman	ASSERT(IS_P2ALIGNED(val, 1U << shift)); \
8943466aaMax Grossman	ASSERT3S((val) >> (shift), >=, bias); \
9043466aaMax Grossman	BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
9143466aaMax Grossman_NOTE(CONSTCOND) } while (0)
9243466aaMax Grossman#define	BF64_SET_SB(x, low, len, shift, bias, val) do { \
9343466aaMax Grossman	ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \
9443466aaMax Grossman	ASSERT3S((val) >> (shift), >=, bias); \
9543466aaMax Grossman	BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
9643466aaMax Grossman_NOTE(CONSTCOND) } while (0)
97fa9e406ahrens
98fa9e406ahrens/*
99b515258Matthew Ahrens * We currently support block sizes from 512 bytes to 16MB.
100b515258Matthew Ahrens * The benefits of larger blocks, and thus larger IO, need to be weighed
101b515258Matthew Ahrens * against the cost of COWing a giant block to modify one byte, and the
102b515258Matthew Ahrens * large latency of reading or writing a large block.
103b515258Matthew Ahrens *
104b515258Matthew Ahrens * Note that although blocks up to 16MB are supported, the recordsize
105b515258Matthew Ahrens * property can not be set larger than zfs_max_recordsize (default 1MB).
106b515258Matthew Ahrens * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
107b515258Matthew Ahrens *
108b515258Matthew Ahrens * Note that although the LSIZE field of the blkptr_t can store sizes up
109b515258Matthew Ahrens * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
110b515258Matthew Ahrens * 32MB - 512 bytes.  Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
111fa9e406ahrens */
112fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
113b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSHIFT	17
114b515258Matthew Ahrens#define	SPA_MAXBLOCKSHIFT	24
115fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
116b515258Matthew Ahrens#define	SPA_OLD_MAXBLOCKSIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)
117fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
118fa9e406ahrens
119fa9e406ahrens/*
120f7991baTim Haley * Size of block to hold the configuration data (a packed nvlist)
121f7991baTim Haley */
122ad135b5Christopher Siden#define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
123f7991baTim Haley
124f7991baTim Haley/*
125fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
126fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
127fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
128fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
129fa9e406ahrens * else that expands the ASIZE.
130fa9e406ahrens */
131fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
132fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
133fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
134fa9e406ahrens
135fa9e406ahrens/*
136fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
137fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
138fa9e406ahrens */
139fa9e406ahrenstypedef struct dva {
140fa9e406ahrens	uint64_t	dva_word[2];
141fa9e406ahrens} dva_t;
142fa9e406ahrens
143fa9e406ahrens/*
144fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
145fa9e406ahrens */
146fa9e406ahrenstypedef struct zio_cksum {
147fa9e406ahrens	uint64_t	zc_word[4];
148fa9e406ahrens} zio_cksum_t;
149fa9e406ahrens
150fa9e406ahrens/*
15145818eeMatthew Ahrens * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
15245818eeMatthew Ahrens * secret and is suitable for use in MAC algorithms as the key.
15345818eeMatthew Ahrens */
15445818eeMatthew Ahrenstypedef struct zio_cksum_salt {
15545818eeMatthew Ahrens	uint8_t		zcs_bytes[32];
15645818eeMatthew Ahrens} zio_cksum_salt_t;
15745818eeMatthew Ahrens
15845818eeMatthew Ahrens/*
159fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
160fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
161fa9e406ahrens *
162fa9e406ahrens *	64	56	48	40	32	24	16	8	0
163fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
164fa9e406ahrens * 0	|		vdev1		| GRID  |	  ASIZE		|
165fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
166fa9e406ahrens * 1	|G|			 offset1				|
167fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
168fa9e406ahrens * 2	|		vdev2		| GRID  |	  ASIZE		|
169fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
170fa9e406ahrens * 3	|G|			 offset2				|
171fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
172fa9e406ahrens * 4	|		vdev3		| GRID  |	  ASIZE		|
173fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
174fa9e406ahrens * 5	|G|			 offset3				|
175fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
1765d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
177fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
178fa9e406ahrens * 7	|			padding					|
179fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
180fa9e406ahrens * 8	|			padding					|
181fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
182b24ab67Jeff Bonwick * 9	|			physical birth txg			|
183fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
184b24ab67Jeff Bonwick * a	|			logical birth txg			|
185fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
186fa9e406ahrens * b	|			fill count				|
187fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
188fa9e406ahrens * c	|			checksum[0]				|
189fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
190fa9e406ahrens * d	|			checksum[1]				|
191fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
192fa9e406ahrens * e	|			checksum[2]				|
193fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
194fa9e406ahrens * f	|			checksum[3]				|
195fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
196fa9e406ahrens *
197fa9e406ahrens * Legend:
198fa9e406ahrens *
199fa9e406ahrens * vdev		virtual device ID
200fa9e406ahrens * offset	offset into virtual device
201fa9e406ahrens * LSIZE	logical size
202fa9e406ahrens * PSIZE	physical size (after compression)
203fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
204fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
205fa9e406ahrens * cksum	checksum function
206fa9e406ahrens * comp		compression function
207fa9e406ahrens * G		gang block indicator
208b24ab67Jeff Bonwick * B		byteorder (endianness)
209b24ab67Jeff Bonwick * D		dedup
2105d7b4d4Matthew Ahrens * X		encryption (on version 30, which is not supported)
2115d7b4d4Matthew Ahrens * E		blkptr_t contains embedded data (see below)
212fa9e406ahrens * lvl		level of indirection
213b24ab67Jeff Bonwick * type		DMU object type
214b24ab67Jeff Bonwick * phys birth	txg of block allocation; zero if same as logical birth txg
215b24ab67Jeff Bonwick * log. birth	transaction group in which the block was logically born
216fa9e406ahrens * fill count	number of non-zero blocks under this bp
217fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
218fa9e406ahrens */
2195d7b4d4Matthew Ahrens
2205d7b4d4Matthew Ahrens/*
2215d7b4d4Matthew Ahrens * "Embedded" blkptr_t's don't actually point to a block, instead they
2225d7b4d4Matthew Ahrens * have a data payload embedded in the blkptr_t itself.  See the comment
2235d7b4d4Matthew Ahrens * in blkptr.c for more details.
2245d7b4d4Matthew Ahrens *
2255d7b4d4Matthew Ahrens * The blkptr_t is laid out as follows:
2265d7b4d4Matthew Ahrens *
2275d7b4d4Matthew Ahrens *	64	56	48	40	32	24	16	8	0
2285d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2295d7b4d4Matthew Ahrens * 0	|      payload                                                  |
2305d7b4d4Matthew Ahrens * 1	|      payload                                                  |
2315d7b4d4Matthew Ahrens * 2	|      payload                                                  |
2325d7b4d4Matthew Ahrens * 3	|      payload                                                  |
2335d7b4d4Matthew Ahrens * 4	|      payload                                                  |
2345d7b4d4Matthew Ahrens * 5	|      payload                                                  |
2355d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2365d7b4d4Matthew Ahrens * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
2375d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2385d7b4d4Matthew Ahrens * 7	|      payload                                                  |
2395d7b4d4Matthew Ahrens * 8	|      payload                                                  |
2405d7b4d4Matthew Ahrens * 9	|      payload                                                  |
2415d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2425d7b4d4Matthew Ahrens * a	|			logical birth txg			|
2435d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2445d7b4d4Matthew Ahrens * b	|      payload                                                  |
2455d7b4d4Matthew Ahrens * c	|      payload                                                  |
2465d7b4d4Matthew Ahrens * d	|      payload                                                  |
2475d7b4d4Matthew Ahrens * e	|      payload                                                  |
2485d7b4d4Matthew Ahrens * f	|      payload                                                  |
2495d7b4d4Matthew Ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
2505d7b4d4Matthew Ahrens *
2515d7b4d4Matthew Ahrens * Legend:
2525d7b4d4Matthew Ahrens *
2535d7b4d4Matthew Ahrens * payload		contains the embedded data
2545d7b4d4Matthew Ahrens * B (byteorder)	byteorder (endianness)
2555d7b4d4Matthew Ahrens * D (dedup)		padding (set to zero)
2565d7b4d4Matthew Ahrens * X			encryption (set to zero; see above)
2575d7b4d4Matthew Ahrens * E (embedded)		set to one
2585d7b4d4Matthew Ahrens * lvl			indirection level
2595d7b4d4Matthew Ahrens * type			DMU object type
2605d7b4d4Matthew Ahrens * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
2615d7b4d4Matthew Ahrens * comp			compression function of payload
2625d7b4d4Matthew Ahrens * PSIZE		size of payload after compression, in bytes
2635d7b4d4Matthew Ahrens * LSIZE		logical size of payload, in bytes
2645d7b4d4Matthew Ahrens *			note that 25 bits is enough to store the largest
2655d7b4d4Matthew Ahrens *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
2665d7b4d4Matthew Ahrens * log. birth		transaction group in which the block was logically born
2675d7b4d4Matthew Ahrens *
2685d7b4d4Matthew Ahrens * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
2695d7b4d4Matthew Ahrens * bp's they are stored in units of SPA_MINBLOCKSHIFT.
2705d7b4d4Matthew Ahrens * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
2715d7b4d4Matthew Ahrens * The B, D, X, lvl, type, and comp fields are stored the same as with normal
2725d7b4d4Matthew Ahrens * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
2735d7b4d4Matthew Ahrens * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
2745d7b4d4Matthew Ahrens * other macros, as they assert that they are only used on BP's of the correct
2755d7b4d4Matthew Ahrens * "embedded-ness".
2765d7b4d4Matthew Ahrens */
2775d7b4d4Matthew Ahrens
2785d7b4d4Matthew Ahrens#define	BPE_GET_ETYPE(bp)	\
2795d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
2805d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
2815d7b4d4Matthew Ahrens#define	BPE_SET_ETYPE(bp, t)	do { \
2825d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
2835d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, t); \
2845d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
2855d7b4d4Matthew Ahrens
2865d7b4d4Matthew Ahrens#define	BPE_GET_LSIZE(bp)	\
2875d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
2885d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
2895d7b4d4Matthew Ahrens#define	BPE_SET_LSIZE(bp, x)	do { \
2905d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
2915d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
2925d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
2935d7b4d4Matthew Ahrens
2945d7b4d4Matthew Ahrens#define	BPE_GET_PSIZE(bp)	\
2955d7b4d4Matthew Ahrens	(ASSERT(BP_IS_EMBEDDED(bp)), \
2965d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
2975d7b4d4Matthew Ahrens#define	BPE_SET_PSIZE(bp, x)	do { \
2985d7b4d4Matthew Ahrens	ASSERT(BP_IS_EMBEDDED(bp)); \
2995d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
3005d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3015d7b4d4Matthew Ahrens
3025d7b4d4Matthew Ahrenstypedef enum bp_embedded_type {
3035d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_DATA,
3045d7b4d4Matthew Ahrens	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
3055d7b4d4Matthew Ahrens	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
3065d7b4d4Matthew Ahrens} bp_embedded_type_t;
3075d7b4d4Matthew Ahrens
3085d7b4d4Matthew Ahrens#define	BPE_NUM_WORDS 14
3095d7b4d4Matthew Ahrens#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
3105d7b4d4Matthew Ahrens#define	BPE_IS_PAYLOADWORD(bp, wp) \
3115d7b4d4Matthew Ahrens	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
3125d7b4d4Matthew Ahrens
313fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
314fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
315fa9e406ahrens
31643466aaMax Grossman/*
31743466aaMax Grossman * A block is a hole when it has either 1) never been written to, or
31843466aaMax Grossman * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
31943466aaMax Grossman * without physically allocating disk space. Holes are represented in the
32043466aaMax Grossman * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
32143466aaMax Grossman * done through the BP_IS_HOLE macro. For holes, the logical size, level,
32243466aaMax Grossman * DMU object type, and birth times are all also stored for holes that
32343466aaMax Grossman * were written to at some point (i.e. were punched after having been filled).
32443466aaMax Grossman */
325b24ab67Jeff Bonwicktypedef struct blkptr {
326b24ab67Jeff Bonwick	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
327b24ab67Jeff Bonwick	uint64_t	blk_prop;	/* size, compression, type, etc	    */
328b24ab67Jeff Bonwick	uint64_t	blk_pad[2];	/* Extra space for the future	    */
329b24ab67Jeff Bonwick	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
330b24ab67Jeff Bonwick	uint64_t	blk_birth;	/* transaction group at birth	    */
331b24ab67Jeff Bonwick	uint64_t	blk_fill;	/* fill count			    */
332b24ab67Jeff Bonwick	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
333b24ab67Jeff Bonwick} blkptr_t;
334b24ab67Jeff Bonwick
335fa9e406ahrens/*
336fa9e406ahrens * Macros to get and set fields in a bp or DVA.
337fa9e406ahrens */
338fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
33943466aaMax Grossman	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
340fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
34143466aaMax Grossman	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
34243466aaMax Grossman	SPA_MINBLOCKSHIFT, 0, x)
343fa9e406ahrens
344fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
345fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
346fa9e406ahrens
347fa9e406ahrens#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
348fa9e406ahrens#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
349fa9e406ahrens
350fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
351fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
352fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
353fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
354fa9e406ahrens
355fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
356fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
357fa9e406ahrens
358fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
3595d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ?	\
3605d7b4d4Matthew Ahrens	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
3615d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
3625d7b4d4Matthew Ahrens#define	BP_SET_LSIZE(bp, x)	do { \
3635d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
3645d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
3655d7b4d4Matthew Ahrens	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
3665d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
367fa9e406ahrens
368fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
3695d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
3705d7b4d4Matthew Ahrens	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
3715d7b4d4Matthew Ahrens#define	BP_SET_PSIZE(bp, x)	do { \
3725d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
3735d7b4d4Matthew Ahrens	BF64_SET_SB((bp)->blk_prop, \
3745d7b4d4Matthew Ahrens	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
3755d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
3765d7b4d4Matthew Ahrens
3775d7b4d4Matthew Ahrens#define	BP_GET_COMPRESS(bp)		BF64_GET((bp)->blk_prop, 32, 7)
3785d7b4d4Matthew Ahrens#define	BP_SET_COMPRESS(bp, x)		BF64_SET((bp)->blk_prop, 32, 7, x)
379fa9e406ahrens
3805d7b4d4Matthew Ahrens#define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
3815d7b4d4Matthew Ahrens#define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
382b24ab67Jeff Bonwick
3835d7b4d4Matthew Ahrens#define	BP_GET_CHECKSUM(bp)		\
3845d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
3855d7b4d4Matthew Ahrens	BF64_GET((bp)->blk_prop, 40, 8))
3865d7b4d4Matthew Ahrens#define	BP_SET_CHECKSUM(bp, x)		do { \
3875d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp)); \
3885d7b4d4Matthew Ahrens	BF64_SET((bp)->blk_prop, 40, 8, x); \
3895d7b4d4Matthew Ahrens_NOTE(CONSTCOND) } while (0)
390b24ab67Jeff Bonwick
391b24ab67Jeff Bonwick#define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
392b24ab67Jeff Bonwick#define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
393fa9e406ahrens
394b24ab67Jeff Bonwick#define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
395b24ab67Jeff Bonwick#define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
396fa9e406ahrens
397b24ab67Jeff Bonwick#define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
398b24ab67Jeff Bonwick#define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
399fa9e406ahrens
40043466aaMax Grossman#define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
401b24ab67Jeff Bonwick#define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
402b24ab67Jeff Bonwick
403b24ab67Jeff Bonwick#define	BP_PHYSICAL_BIRTH(bp)		\
4045d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4055d7b4d4Matthew Ahrens	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
406b24ab67Jeff Bonwick
407b24ab67Jeff Bonwick#define	BP_SET_BIRTH(bp, logical, physical)	\
408b24ab67Jeff Bonwick{						\
4095d7b4d4Matthew Ahrens	ASSERT(!BP_IS_EMBEDDED(bp));		\
410b24ab67Jeff Bonwick	(bp)->blk_birth = (logical);		\
411b24ab67Jeff Bonwick	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
412b24ab67Jeff Bonwick}
413fa9e406ahrens
4145d7b4d4Matthew Ahrens#define	BP_GET_FILL(bp) (BP_IS_EMBEDDED(bp) ? 1 : (bp)->blk_fill)
4155d7b4d4Matthew Ahrens
416fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
4175d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4185d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
4195d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
4205d7b4d4Matthew Ahrens	DVA_GET_ASIZE(&(bp)->blk_dva[2]))
42199653d4eschrock
42299653d4eschrock#define	BP_GET_UCSIZE(bp) \
423ad135b5Christopher Siden	((BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) ? \
4243f9d6adLin Ling	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
425fa9e406ahrens
42644cd46cbillm#define	BP_GET_NDVAS(bp)	\
4275d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
4285d7b4d4Matthew Ahrens	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
42944cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
43044cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
43144cd46cbillm
43244cd46cbillm#define	BP_COUNT_GANG(bp)	\
4335d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? 0 : \
43444cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
43544cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
4365d7b4d4Matthew Ahrens	DVA_GET_GANG(&(bp)->blk_dva[2])))
43744cd46cbillm
438fa9e406ahrens#define	DVA_EQUAL(dva1, dva2)	\
439fa9e406ahrens	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
440fa9e406ahrens	(dva1)->dva_word[0] == (dva2)->dva_word[0])
441fa9e406ahrens
442b24ab67Jeff Bonwick#define	BP_EQUAL(bp1, bp2)	\
443b24ab67Jeff Bonwick	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
4445d7b4d4Matthew Ahrens	(bp1)->blk_birth == (bp2)->blk_birth &&			\
445b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
446b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
447b24ab67Jeff Bonwick	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
448b24ab67Jeff Bonwick
4496b4acc8ahrens#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
4506b4acc8ahrens	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
4516b4acc8ahrens	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
4526b4acc8ahrens	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
4536b4acc8ahrens	((zc1).zc_word[3] - (zc2).zc_word[3])))
4546b4acc8ahrens
45598110f0Matthew Ahrens#define	ZIO_CHECKSUM_IS_ZERO(zc) \
45698110f0Matthew Ahrens	(0 == ((zc)->zc_word[0] | (zc)->zc_word[1] | \
45798110f0Matthew Ahrens	(zc)->zc_word[2] | (zc)->zc_word[3]))
45898110f0Matthew Ahrens
45998110f0Matthew Ahrens#define	ZIO_CHECKSUM_BSWAP(zcp)					\
46098110f0Matthew Ahrens{								\
46198110f0Matthew Ahrens	(zcp)->zc_word[0] = BSWAP_64((zcp)->zc_word[0]);	\
46298110f0Matthew Ahrens	(zcp)->zc_word[1] = BSWAP_64((zcp)->zc_word[1]);	\
46398110f0Matthew Ahrens	(zcp)->zc_word[2] = BSWAP_64((zcp)->zc_word[2]);	\
46498110f0Matthew Ahrens	(zcp)->zc_word[3] = BSWAP_64((zcp)->zc_word[3]);	\
46598110f0Matthew Ahrens}
46698110f0Matthew Ahrens
46798110f0Matthew Ahrens
468fa9e406ahrens#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
469fa9e406ahrens
470fa9e406ahrens#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
471fa9e406ahrens{						\
472fa9e406ahrens	(zcp)->zc_word[0] = w0;			\
473fa9e406ahrens	(zcp)->zc_word[1] = w1;			\
474fa9e406ahrens	(zcp)->zc_word[2] = w2;			\
475fa9e406ahrens	(zcp)->zc_word[3] = w3;			\
476fa9e406ahrens}
477fa9e406ahrens
4785d7b4d4Matthew Ahrens#define	BP_IDENTITY(bp)		(ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
4795d7b4d4Matthew Ahrens#define	BP_IS_GANG(bp)		\
4805d7b4d4Matthew Ahrens	(BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
48143466aaMax Grossman#define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&	\
48243466aaMax Grossman				(dva)->dva_word[1] == 0ULL)
4835d7b4d4Matthew Ahrens#define	BP_IS_HOLE(bp) \
4845d7b4d4Matthew Ahrens	(!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
485fa9e406ahrens
4866e1f5caNeil Perrin/* BP_IS_RAIDZ(bp) assumes no block compression */
4876e1f5caNeil Perrin#define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
4886e1f5caNeil Perrin				BP_GET_PSIZE(bp))
4896e1f5caNeil Perrin
490e14bb32Jeff Bonwick#define	BP_ZERO(bp)				\
491fa9e406ahrens{						\
492fa9e406ahrens	(bp)->blk_dva[0].dva_word[0] = 0;	\
493fa9e406ahrens	(bp)->blk_dva[0].dva_word[1] = 0;	\
494fa9e406ahrens	(bp)->blk_dva[1].dva_word[0] = 0;	\
495fa9e406ahrens	(bp)->blk_dva[1].dva_word[1] = 0;	\
496fa9e406ahrens	(bp)->blk_dva[2].dva_word[0] = 0;	\
497fa9e406ahrens	(bp)->blk_dva[2].dva_word[1] = 0;	\
498fa9e406ahrens	(bp)->blk_prop = 0;			\
499fa9e406ahrens	(bp)->blk_pad[0] = 0;			\
500fa9e406ahrens	(bp)->blk_pad[1] = 0;			\
501b24ab67Jeff Bonwick	(bp)->blk_phys_birth = 0;		\
502e14bb32Jeff Bonwick	(bp)->blk_birth = 0;			\
503fa9e406ahrens	(bp)->blk_fill = 0;			\
504fa9e406ahrens	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
505fa9e406ahrens}
506fa9e406ahrens
507fa9e406ahrens#ifdef _BIG_ENDIAN
508fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(0ULL)
509fa9e406ahrens#else
51043466aaMax Grossman#define	ZFS_HOST_BYTEORDER	(1ULL)
511fa9e406ahrens#endif
512fa9e406ahrens
513fa9e406ahrens#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
514fa9e406ahrens
51544cd46cbillm#define	BP_SPRINTF_LEN	320
516fbabab8maybee
517b24ab67Jeff Bonwick/*
518b24ab67Jeff Bonwick * This macro allows code sharing between zfs, libzpool, and mdb.
519b24ab67Jeff Bonwick * 'func' is either snprintf() or mdb_snprintf().
520b24ab67Jeff Bonwick * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
521b24ab67Jeff Bonwick */
52243466aaMax Grossman#define	SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
523b24ab67Jeff Bonwick{									\
524b24ab67Jeff Bonwick	static const char *copyname[] =					\
525b24ab67Jeff Bonwick	    { "zero", "single", "double", "triple" };			\
526b24ab67Jeff Bonwick	int len = 0;							\
527b24ab67Jeff Bonwick	int copies = 0;							\
528b24ab67Jeff Bonwick									\
529b24ab67Jeff Bonwick	if (bp == NULL) {						\
53043466aaMax Grossman		len += func(buf + len, size - len, "<NULL>");		\
531b24ab67Jeff Bonwick	} else if (BP_IS_HOLE(bp)) {					\
53270163acPrakash Surya		len += func(buf + len, size - len,			\
53370163acPrakash Surya		    "HOLE [L%llu %s] "					\
53470163acPrakash Surya		    "size=%llxL birth=%lluL",				\
53570163acPrakash Surya		    (u_longlong_t)BP_GET_LEVEL(bp),			\
53670163acPrakash Surya		    type,						\
53770163acPrakash Surya		    (u_longlong_t)BP_GET_LSIZE(bp),			\
53870163acPrakash Surya		    (u_longlong_t)bp->blk_birth);			\
5395d7b4d4Matthew Ahrens	} else if (BP_IS_EMBEDDED(bp)) {				\
5405d7b4d4Matthew Ahrens		len = func(buf + len, size - len,			\
5415d7b4d4Matthew Ahrens		    "EMBEDDED [L%llu %s] et=%u %s "			\
5425d7b4d4Matthew Ahrens		    "size=%llxL/%llxP birth=%lluL",			\
5435d7b4d4Matthew Ahrens		    (u_longlong_t)BP_GET_LEVEL(bp),			\
5445d7b4d4Matthew Ahrens		    type,						\
5455d7b4d4Matthew Ahrens		    (int)BPE_GET_ETYPE(bp),				\
5465d7b4d4Matthew Ahrens		    compress,						\
5475d7b4d4Matthew Ahrens		    (u_longlong_t)BPE_GET_LSIZE(bp),			\
5485d7b4d4Matthew Ahrens		    (u_longlong_t)BPE_GET_PSIZE(bp),			\
5495d7b4d4Matthew Ahrens		    (u_longlong_t)bp->blk_birth);			\
550b24ab67Jeff Bonwick	} else {							\
551b24ab67Jeff Bonwick		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
552b24ab67Jeff Bonwick			const dva_t *dva = &bp->blk_dva[d];		\
553b24ab67Jeff Bonwick			if (DVA_IS_VALID(dva))				\
554b24ab67Jeff Bonwick				copies++;				\
555b24ab67Jeff Bonwick			len += func(buf + len, size - len,		\
556b24ab67Jeff Bonwick			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
557b24ab67Jeff Bonwick			    (u_longlong_t)DVA_GET_VDEV(dva),		\
558b24ab67Jeff Bonwick			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
559b24ab67Jeff Bonwick			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
560b24ab67Jeff Bonwick			    ws);					\
561b24ab67Jeff Bonwick		}							\
562b24ab67Jeff Bonwick		if (BP_IS_GANG(bp) &&					\
563b24ab67Jeff Bonwick		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
564b24ab67Jeff Bonwick		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
565b24ab67Jeff Bonwick			copies--;					\
566b24ab67Jeff Bonwick		len += func(buf + len, size - len,			\
567b24ab67Jeff Bonwick		    "[L%llu %s] %s %s %s %s %s %s%c"			\
568b24ab67Jeff Bonwick		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
569b24ab67Jeff Bonwick		    "cksum=%llx:%llx:%llx:%llx",			\
570b24ab67Jeff Bonwick		    (u_longlong_t)BP_GET_LEVEL(bp),			\
571b24ab67Jeff Bonwick		    type,						\
572b24ab67Jeff Bonwick		    checksum,						\
573b24ab67Jeff Bonwick		    compress,						\
574b24ab67Jeff Bonwick		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
575b24ab67Jeff Bonwick		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
576b24ab67Jeff Bonwick		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
577b24ab67Jeff Bonwick		    copyname[copies],					\
578b24ab67Jeff Bonwick		    ws,							\
579b24ab67Jeff Bonwick		    (u_longlong_t)BP_GET_LSIZE(bp),			\
580b24ab67Jeff Bonwick		    (u_longlong_t)BP_GET_PSIZE(bp),			\
581b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_birth,			\
582b24ab67Jeff Bonwick		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp),		\
5835d7b4d4Matthew Ahrens		    (u_longlong_t)BP_GET_FILL(bp),			\
584b24ab67Jeff Bonwick		    ws,							\
585b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
586b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
587b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
588b24ab67Jeff Bonwick		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
589b24ab67Jeff Bonwick	}								\
590b24ab67Jeff Bonwick	ASSERT(len < size);						\
591b24ab67Jeff Bonwick}
592b24ab67Jeff Bonwick
593fa9e406ahrens#include <sys/dmu.h>
594fa9e406ahrens
595ad23a2djohansen#define	BP_GET_BUFC_TYPE(bp)						\
596ad135b5Christopher Siden	(((BP_GET_LEVEL(bp) > 0) || (DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))) ? \
597ad135b5Christopher Siden	ARC_BUFC_METADATA : ARC_BUFC_DATA)
598fa9e406ahrens
5991195e68Mark J Musantetypedef enum spa_import_type {
6001195e68Mark J Musante	SPA_IMPORT_EXISTING,
6011195e68Mark J Musante	SPA_IMPORT_ASSEMBLE
6021195e68Mark J Musante} spa_import_type_t;
6031195e68Mark J Musante
604fa9e406ahrens/* state manipulation functions */
605fa9e406ahrensextern int spa_open(const char *pool, spa_t **, void *tag);
606468c413Tim Haleyextern int spa_open_rewind(const char *pool, spa_t **, void *tag,
607468c413Tim Haley    nvlist_t *policy, nvlist_t **config);
608ad135b5Christopher Sidenextern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
609ad135b5Christopher Siden    size_t buflen);
610990b485llingextern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
6114445fffMatthew Ahrens    nvlist_t *zplprops);
612051aabetaylorextern int spa_import_rootpool(char *devpath, char *devid);
6134b964adGeorge Wilsonextern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props,
6144b964adGeorge Wilson    uint64_t flags);
615fa9e406ahrensextern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
616fa9e406ahrensextern int spa_destroy(char *pool);
617394ab0cGeorge Wilsonextern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
618394ab0cGeorge Wilson    boolean_t hardforce);
619ea8dc4beschrockextern int spa_reset(char *pool);
620ea8dc4beschrockextern void spa_async_request(spa_t *spa, int flag);
621088f389ahrensextern void spa_async_unrequest(spa_t *spa, int flag);
622ea8dc4beschrockextern void spa_async_suspend(spa_t *spa);
623ea8dc4beschrockextern void spa_async_resume(spa_t *spa);
624ea8dc4beschrockextern spa_t *spa_inject_addref(char *pool);
625ea8dc4beschrockextern void spa_inject_delref(spa_t *spa);
6263f9d6adLin Lingextern void spa_scan_stat_init(spa_t *spa);
6273f9d6adLin Lingextern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
628ea8dc4beschrock
629e14bb32Jeff Bonwick#define	SPA_ASYNC_CONFIG_UPDATE	0x01
630e14bb32Jeff Bonwick#define	SPA_ASYNC_REMOVE	0x02
631e14bb32Jeff Bonwick#define	SPA_ASYNC_PROBE		0x04
632e14bb32Jeff Bonwick#define	SPA_ASYNC_RESILVER_DONE	0x08
633e14bb32Jeff Bonwick#define	SPA_ASYNC_RESILVER	0x10
634573ca77George Wilson#define	SPA_ASYNC_AUTOEXPAND	0x20
6353f9d6adLin Ling#define	SPA_ASYNC_REMOVE_DONE	0x40
6363f9d6adLin Ling#define	SPA_ASYNC_REMOVE_STOP	0x80
6373f9d6adLin Ling
6383f9d6adLin Ling/*
6393f9d6adLin Ling * Controls the behavior of spa_vdev_remove().
6403f9d6adLin Ling */
6413f9d6adLin Ling#define	SPA_REMOVE_UNSPARE	0x01
6423f9d6adLin Ling#define	SPA_REMOVE_DONE		0x02
643fa9e406ahrens
644fa9e406ahrens/* device manipulation */
645fa9e406ahrensextern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
646ea8dc4beschrockextern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
647fa9e406ahrens    int replacing);
6488ad4d6dJeff Bonwickextern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
6498ad4d6dJeff Bonwick    int replace_done);
65099653d4eschrockextern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
6513f9d6adLin Lingextern boolean_t spa_vdev_remove_active(spa_t *spa);
652c67d967eschrockextern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
6536809eb4Eric Schrockextern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
6541195e68Mark J Musanteextern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
6551195e68Mark J Musante    nvlist_t *props, boolean_t exp);
656fa9e406ahrens
65799653d4eschrock/* spare state (which is global across all pools) */
65839c2341eschrockextern void spa_spare_add(vdev_t *vd);
65939c2341eschrockextern void spa_spare_remove(vdev_t *vd);
66089a89ebllingextern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
66139c2341eschrockextern void spa_spare_activate(vdev_t *vd);
66299653d4eschrock
663fa94a07brendan/* L2ARC state (which is global across all pools) */
664fa94a07brendanextern void spa_l2cache_add(vdev_t *vd);
665fa94a07brendanextern void spa_l2cache_remove(vdev_t *vd);
666fa94a07brendanextern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
667fa94a07brendanextern void spa_l2cache_activate(vdev_t *vd);
668fa94a07brendanextern void spa_l2cache_drop(spa_t *spa);
669fa94a07brendan
6703f9d6adLin Ling/* scanning */
6713f9d6adLin Lingextern int spa_scan(spa_t *spa, pool_scan_func_t func);
6723f9d6adLin Lingextern int spa_scan_stop(spa_t *spa);
673fa9e406ahrens
674fa9e406ahrens/* spa syncing */
675fa9e406ahrensextern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
676fa9e406ahrensextern void spa_sync_allpools(void);
677fa9e406ahrens
6783a737e0brendan/* spa namespace global mutex */
6793a737e0brendanextern kmutex_t spa_namespace_lock;
6803a737e0brendan
681fa9e406ahrens/*
682fa9e406ahrens * SPA configuration functions in spa_config.c
683fa9e406ahrens */
6840373e76bonwick
6850373e76bonwick#define	SPA_CONFIG_UPDATE_POOL	0
6860373e76bonwick#define	SPA_CONFIG_UPDATE_VDEVS	1
6870373e76bonwick
688c5904d1eschrockextern void spa_config_sync(spa_t *, boolean_t, boolean_t);
689fa9e406ahrensextern void spa_config_load(void);
690fa9e406ahrensextern nvlist_t *spa_all_configs(uint64_t *);
691fa9e406ahrensextern void spa_config_set(spa_t *spa, nvlist_t *config);
692fa9e406ahrensextern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
693fa9e406ahrens    int getstats);
6940373e76bonwickextern void spa_config_update(spa_t *spa, int what);
695fa9e406ahrens
696fa9e406ahrens/*
697fa9e406ahrens * Miscellaneous SPA routines in spa_misc.c
698fa9e406ahrens */
699fa9e406ahrens
700fa9e406ahrens/* Namespace manipulation */
701fa9e406ahrensextern spa_t *spa_lookup(const char *name);
702468c413Tim Haleyextern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
703fa9e406ahrensextern void spa_remove(spa_t *spa);
704fa9e406ahrensextern spa_t *spa_next(spa_t *prev);
705fa9e406ahrens
706fa9e406ahrens/* Refcount functions */
707fa9e406ahrensextern void spa_open_ref(spa_t *spa, void *tag);
708fa9e406ahrensextern void spa_close(spa_t *spa, void *tag);
709bc9014eJustin Gibbsextern void spa_async_close(spa_t *spa, void *tag);
710fa9e406ahrensextern boolean_t spa_refcount_zero(spa_t *spa);
711fa9e406ahrens
7128f18d1fGeorge Wilson#define	SCL_NONE	0x00
713e14bb32Jeff Bonwick#define	SCL_CONFIG	0x01
714e14bb32Jeff Bonwick#define	SCL_STATE	0x02
715e14bb32Jeff Bonwick#define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
716e14bb32Jeff Bonwick#define	SCL_ALLOC	0x08
717e14bb32Jeff Bonwick#define	SCL_ZIO		0x10
718e14bb32Jeff Bonwick#define	SCL_FREE	0x20
719e14bb32Jeff Bonwick#define	SCL_VDEV	0x40
720e14bb32Jeff Bonwick#define	SCL_LOCKS	7
721e14bb32Jeff Bonwick#define	SCL_ALL		((1 << SCL_LOCKS) - 1)
722e14bb32Jeff Bonwick#define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
723e14bb32Jeff Bonwick
724e14bb32Jeff Bonwick/* Pool configuration locks */
725e14bb32Jeff Bonwickextern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
726e14bb32Jeff Bonwickextern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
727e14bb32Jeff Bonwickextern void spa_config_exit(spa_t *spa, int locks, void *tag);
728e14bb32Jeff Bonwickextern int spa_config_held(spa_t *spa, int locks, krw_t rw);
729fa9e406ahrens
730fa9e406ahrens/* Pool vdev add/remove lock */
731fa9e406ahrensextern uint64_t spa_vdev_enter(spa_t *spa);
73288ecc94George Wilsonextern uint64_t spa_vdev_config_enter(spa_t *spa);
73388ecc94George Wilsonextern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
73488ecc94George Wilson    int error, char *tag);
735fa9e406ahrensextern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
736fa9e406ahrens
737e14bb32Jeff Bonwick/* Pool vdev state change lock */
7388f18d1fGeorge Wilsonextern void spa_vdev_state_enter(spa_t *spa, int oplock);
739e14bb32Jeff Bonwickextern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
740e14bb32Jeff Bonwick
741b24ab67Jeff Bonwick/* Log state */
742b24ab67Jeff Bonwicktypedef enum spa_log_state {
743b24ab67Jeff Bonwick	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
744b24ab67Jeff Bonwick	SPA_LOG_MISSING,	/* missing log(s) */
745b24ab67Jeff Bonwick	SPA_LOG_CLEAR,		/* clear the log(s) */
746b24ab67Jeff Bonwick	SPA_LOG_GOOD,		/* log(s) are good */
747b24ab67Jeff Bonwick} spa_log_state_t;
748b24ab67Jeff Bonwick
749b24ab67Jeff Bonwickextern spa_log_state_t spa_get_log_state(spa_t *spa);
750b24ab67Jeff Bonwickextern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
7511195e68Mark J Musanteextern int spa_offline_log(spa_t *spa);
752b24ab67Jeff Bonwick
753b24ab67Jeff Bonwick/* Log claim callback */
754b24ab67Jeff Bonwickextern void spa_claim_notify(zio_t *zio);
755b24ab67Jeff Bonwick
756fa9e406ahrens/* Accessor functions */
75788b7b0fMatthew Ahrensextern boolean_t spa_shutting_down(spa_t *spa);
758fa9e406ahrensextern struct dsl_pool *spa_get_dsl(spa_t *spa);
759ad135b5Christopher Sidenextern boolean_t spa_is_initializing(spa_t *spa);
760fa9e406ahrensextern blkptr_t *spa_get_rootblkptr(spa_t *spa);
761fa9e406ahrensextern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
762fa9e406ahrensextern void spa_altroot(spa_t *, char *, size_t);
763fa9e406ahrensextern int spa_sync_pass(spa_t *spa);
764fa9e406ahrensextern char *spa_name(spa_t *spa);
765fa9e406ahrensextern uint64_t spa_guid(spa_t *spa);
766e9103aaGarrett D'Amoreextern uint64_t spa_load_guid(spa_t *spa);
767fa9e406ahrensextern uint64_t spa_last_synced_txg(spa_t *spa);
768fa9e406ahrensextern uint64_t spa_first_txg(spa_t *spa);
769b24ab67Jeff Bonwickextern uint64_t spa_syncing_txg(spa_t *spa);
77099653d4eschrockextern uint64_t spa_version(spa_t *spa);
77188b7b0fMatthew Ahrensextern pool_state_t spa_state(spa_t *spa);
772b16da2eGeorge Wilsonextern spa_load_state_t spa_load_state(spa_t *spa);
773fa9e406ahrensextern uint64_t spa_freeze_txg(spa_t *spa);
774fa9e406ahrensextern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
775485bbbfGeorge Wilsonextern uint64_t spa_get_dspace(spa_t *spa);
776c39f2c8Christopher Sidenextern uint64_t spa_get_slop_space(spa_t *spa);
777485bbbfGeorge Wilsonextern void spa_update_dspace(spa_t *spa);
77844cd46cbillmextern uint64_t spa_version(spa_t *spa);
779b24ab67Jeff Bonwickextern boolean_t spa_deflate(spa_t *spa);
780b24ab67Jeff Bonwickextern metaslab_class_t *spa_normal_class(spa_t *spa);
781b24ab67Jeff Bonwickextern metaslab_class_t *spa_log_class(spa_t *spa);
782bc9014eJustin Gibbsextern void spa_evicting_os_register(spa_t *, objset_t *os);
783bc9014eJustin Gibbsextern void spa_evicting_os_deregister(spa_t *, objset_t *os);
784bc9014eJustin Gibbsextern void spa_evicting_os_wait(spa_t *spa);
78544cd46cbillmextern int spa_max_replication(spa_t *spa);
7863f9d6adLin Lingextern int spa_prev_software_version(spa_t *spa);
787fa9e406ahrensextern int spa_busy(void);
7880a4e951gwextern uint8_t spa_get_failmode(spa_t *spa);
789e14bb32Jeff Bonwickextern boolean_t spa_suspended(spa_t *spa);
790b24ab67Jeff Bonwickextern uint64_t spa_bootfs(spa_t *spa);
791b24ab67Jeff Bonwickextern uint64_t spa_delegation(spa_t *spa);
792b24ab67Jeff Bonwickextern objset_t *spa_meta_objset(spa_t *spa);
793283b846George.Wilsonextern uint64_t spa_deadman_synctime(spa_t *spa);
794fa9e406ahrens
795fa9e406ahrens/* Miscellaneous support routines */
79643466aaMax Grossmanextern void spa_activate_mos_feature(spa_t *spa, const char *feature,
79743466aaMax Grossman    dmu_tx_t *tx);
798ad135b5Christopher Sidenextern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
799fa9e406ahrensextern int spa_rename(const char *oldname, const char *newname);
800f9af39bGeorge Wilsonextern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
801fa9e406ahrensextern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
802fa9e406ahrensextern char *spa_strdup(const char *);
803fa9e406ahrensextern void spa_strfree(char *);
804fa9e406ahrensextern uint64_t spa_get_random(uint64_t range);
8051195e68Mark J Musanteextern uint64_t spa_generate_guid(spa_t *spa);
80643466aaMax Grossmanextern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
807fa9e406ahrensextern void spa_freeze(spa_t *spa);
808e9103aaGarrett D'Amoreextern int spa_change_guid(spa_t *spa);
809990b485llingextern void spa_upgrade(spa_t *spa, uint64_t version);
810fa9e406ahrensextern void spa_evict_all(void);
811c5904d1eschrockextern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
812c5904d1eschrock    boolean_t l2cache);
81399653d4eschrockextern boolean_t spa_has_spare(spa_t *, uint64_t guid);
814b24ab67Jeff Bonwickextern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
815b24ab67Jeff Bonwickextern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
816b24ab67Jeff Bonwickextern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
8176ce0521perrinextern boolean_t spa_has_slogs(spa_t *spa);
818bf82a41eschrockextern boolean_t spa_is_root(spa_t *spa);
8198ad4d6dJeff Bonwickextern boolean_t spa_writeable(spa_t *spa);
82073527f4Alex Reeceextern boolean_t spa_has_pending_synctask(spa_t *spa);
821b515258Matthew Ahrensextern int spa_maxblocksize(spa_t *spa);
822f63ab3dMatthew Ahrensextern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp);
823468c413Tim Haley
8248ad4d6dJeff Bonwickextern int spa_mode(spa_t *spa);
825ca45db4Chris Kirbyextern uint64_t strtonum(const char *str, char **nptr);
826