xref: /illumos-gate/usr/src/boot/sys/cddl/boot/zfs/zfsimpl.h (revision b713c91e)
108487eeaSToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 2002 McAfee, Inc.
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * This software was developed for the FreeBSD Project by Marshall
6199767f8SToomas Soome  * Kirk McKusick and McAfee Research,, the Security Research Division of
7199767f8SToomas Soome  * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
8199767f8SToomas Soome  * part of the DARPA CHATS research program
9199767f8SToomas Soome  *
10199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome  * modification, are permitted provided that the following conditions
12199767f8SToomas Soome  * are met:
13199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
18199767f8SToomas Soome  *
19199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29199767f8SToomas Soome  * SUCH DAMAGE.
30199767f8SToomas Soome  */
31199767f8SToomas Soome /*
32199767f8SToomas Soome  * CDDL HEADER START
33199767f8SToomas Soome  *
34199767f8SToomas Soome  * The contents of this file are subject to the terms of the
35199767f8SToomas Soome  * Common Development and Distribution License (the "License").
36199767f8SToomas Soome  * You may not use this file except in compliance with the License.
37199767f8SToomas Soome  *
38199767f8SToomas Soome  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
39199767f8SToomas Soome  * or http://www.opensolaris.org/os/licensing.
40199767f8SToomas Soome  * See the License for the specific language governing permissions
41199767f8SToomas Soome  * and limitations under the License.
42199767f8SToomas Soome  *
43199767f8SToomas Soome  * When distributing Covered Code, include this CDDL HEADER in each
44199767f8SToomas Soome  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45199767f8SToomas Soome  * If applicable, add the following below this CDDL HEADER, with the
46199767f8SToomas Soome  * fields enclosed by brackets "[]" replaced with your own identifying
47199767f8SToomas Soome  * information: Portions Copyright [yyyy] [name of copyright owner]
48199767f8SToomas Soome  *
49199767f8SToomas Soome  * CDDL HEADER END
50199767f8SToomas Soome  */
51199767f8SToomas Soome /*
52199767f8SToomas Soome  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
53199767f8SToomas Soome  * Use is subject to license terms.
54199767f8SToomas Soome  */
55199767f8SToomas Soome /*
56199767f8SToomas Soome  * Copyright 2013 by Saso Kiselkov. All rights reserved.
57199767f8SToomas Soome  */
58199767f8SToomas Soome /*
59199767f8SToomas Soome  * Copyright (c) 2013 by Delphix. All rights reserved.
60199767f8SToomas Soome  */
61199767f8SToomas Soome 
62*b713c91eSToomas Soome #ifndef _ZFSIMPL_H_
63*b713c91eSToomas Soome #define	_ZFSIMPL_H_
64*b713c91eSToomas Soome 
65*b713c91eSToomas Soome #include <sys/queue.h>
66*b713c91eSToomas Soome #include <sys/list.h>
67*b713c91eSToomas Soome #include <bootstrap.h>
6808487eeaSToomas Soome 
69199767f8SToomas Soome #define	MAXNAMELEN	256
70199767f8SToomas Soome 
71199767f8SToomas Soome #define _NOTE(s)
72199767f8SToomas Soome 
737dcf02b3SToomas Soome /*
747dcf02b3SToomas Soome  * AVL comparator helpers
757dcf02b3SToomas Soome  */
767dcf02b3SToomas Soome #define	AVL_ISIGN(a)	(((a) > 0) - ((a) < 0))
777dcf02b3SToomas Soome #define	AVL_CMP(a, b)	(((a) > (b)) - ((a) < (b)))
787dcf02b3SToomas Soome #define	AVL_PCMP(a, b)	\
797dcf02b3SToomas Soome 	(((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b)))
807dcf02b3SToomas Soome 
81199767f8SToomas Soome /* CRC64 table */
82199767f8SToomas Soome #define	ZFS_CRC64_POLY	0xC96C5795D7870F42ULL	/* ECMA-182, reflected form */
83199767f8SToomas Soome 
84199767f8SToomas Soome /*
85199767f8SToomas Soome  * Macros for various sorts of alignment and rounding when the alignment
86199767f8SToomas Soome  * is known to be a power of 2.
87199767f8SToomas Soome  */
88199767f8SToomas Soome #define	P2ALIGN(x, align)		((x) & -(align))
89199767f8SToomas Soome #define	P2PHASE(x, align)		((x) & ((align) - 1))
90199767f8SToomas Soome #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
91199767f8SToomas Soome #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
92199767f8SToomas Soome #define	P2END(x, align)			(-(~(x) & -(align)))
93199767f8SToomas Soome #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
9408487eeaSToomas Soome #define	P2BOUNDARY(off, len, align)	\
9508487eeaSToomas Soome 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
96199767f8SToomas Soome 
97199767f8SToomas Soome /*
98199767f8SToomas Soome  * General-purpose 32-bit and 64-bit bitfield encodings.
99199767f8SToomas Soome  */
100199767f8SToomas Soome #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
101199767f8SToomas Soome #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
102199767f8SToomas Soome #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
103199767f8SToomas Soome #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
104199767f8SToomas Soome 
105199767f8SToomas Soome #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
106199767f8SToomas Soome #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
107199767f8SToomas Soome 
108199767f8SToomas Soome #define	BF32_SET(x, low, len, val)	\
109199767f8SToomas Soome 	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
110199767f8SToomas Soome #define	BF64_SET(x, low, len, val)	\
111199767f8SToomas Soome 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
112199767f8SToomas Soome 
113199767f8SToomas Soome #define	BF32_GET_SB(x, low, len, shift, bias)	\
114199767f8SToomas Soome 	((BF32_GET(x, low, len) + (bias)) << (shift))
115199767f8SToomas Soome #define	BF64_GET_SB(x, low, len, shift, bias)	\
116199767f8SToomas Soome 	((BF64_GET(x, low, len) + (bias)) << (shift))
117199767f8SToomas Soome 
118199767f8SToomas Soome #define	BF32_SET_SB(x, low, len, shift, bias, val)	\
119199767f8SToomas Soome 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
120199767f8SToomas Soome #define	BF64_SET_SB(x, low, len, shift, bias, val)	\
121199767f8SToomas Soome 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
122199767f8SToomas Soome 
123199767f8SToomas Soome /*
124199767f8SToomas Soome  * Macros to reverse byte order
125199767f8SToomas Soome  */
126199767f8SToomas Soome #define	BSWAP_8(x)	((x) & 0xff)
127199767f8SToomas Soome #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
128199767f8SToomas Soome #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
129199767f8SToomas Soome #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
130199767f8SToomas Soome 
131199767f8SToomas Soome #define	SPA_MINBLOCKSHIFT	9
132199767f8SToomas Soome #define	SPA_OLDMAXBLOCKSHIFT	17
133199767f8SToomas Soome #define	SPA_MAXBLOCKSHIFT	24
134199767f8SToomas Soome #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
135199767f8SToomas Soome #define	SPA_OLDMAXBLOCKSIZE	(1ULL << SPA_OLDMAXBLOCKSHIFT)
136199767f8SToomas Soome #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
137199767f8SToomas Soome 
138199767f8SToomas Soome /*
139199767f8SToomas Soome  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
140199767f8SToomas Soome  * The ASIZE encoding should be at least 64 times larger (6 more bits)
141199767f8SToomas Soome  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
142199767f8SToomas Soome  * overhead, three DVAs per bp, plus one more bit in case we do anything
143199767f8SToomas Soome  * else that expands the ASIZE.
144199767f8SToomas Soome  */
145199767f8SToomas Soome #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
146199767f8SToomas Soome #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
147199767f8SToomas Soome #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
148199767f8SToomas Soome 
149199767f8SToomas Soome /*
150199767f8SToomas Soome  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
151199767f8SToomas Soome  * The members of the dva_t should be considered opaque outside the SPA.
152199767f8SToomas Soome  */
153199767f8SToomas Soome typedef struct dva {
154199767f8SToomas Soome 	uint64_t	dva_word[2];
155199767f8SToomas Soome } dva_t;
156199767f8SToomas Soome 
157199767f8SToomas Soome /*
158199767f8SToomas Soome  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
159199767f8SToomas Soome  */
160199767f8SToomas Soome typedef struct zio_cksum {
161199767f8SToomas Soome 	uint64_t	zc_word[4];
162199767f8SToomas Soome } zio_cksum_t;
163199767f8SToomas Soome 
164199767f8SToomas Soome /*
165199767f8SToomas Soome  * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
166199767f8SToomas Soome  * secret and is suitable for use in MAC algorithms as the key.
167199767f8SToomas Soome  */
168199767f8SToomas Soome typedef struct zio_cksum_salt {
169199767f8SToomas Soome 	uint8_t		zcs_bytes[32];
170199767f8SToomas Soome } zio_cksum_salt_t;
171199767f8SToomas Soome 
172199767f8SToomas Soome /*
173199767f8SToomas Soome  * Each block is described by its DVAs, time of birth, checksum, etc.
174199767f8SToomas Soome  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
175199767f8SToomas Soome  *
176199767f8SToomas Soome  *	64	56	48	40	32	24	16	8	0
177199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
178199767f8SToomas Soome  * 0	|		vdev1		| GRID  |	  ASIZE		|
179199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
180199767f8SToomas Soome  * 1	|G|			 offset1				|
181199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
182199767f8SToomas Soome  * 2	|		vdev2		| GRID  |	  ASIZE		|
183199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
184199767f8SToomas Soome  * 3	|G|			 offset2				|
185199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
186199767f8SToomas Soome  * 4	|		vdev3		| GRID  |	  ASIZE		|
187199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
188199767f8SToomas Soome  * 5	|G|			 offset3				|
189199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
190199767f8SToomas Soome  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
191199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
192199767f8SToomas Soome  * 7	|			padding					|
193199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
194199767f8SToomas Soome  * 8	|			padding					|
195199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
196199767f8SToomas Soome  * 9	|			physical birth txg			|
197199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
198199767f8SToomas Soome  * a	|			logical birth txg			|
199199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
200199767f8SToomas Soome  * b	|			fill count				|
201199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
202199767f8SToomas Soome  * c	|			checksum[0]				|
203199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
204199767f8SToomas Soome  * d	|			checksum[1]				|
205199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
206199767f8SToomas Soome  * e	|			checksum[2]				|
207199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
208199767f8SToomas Soome  * f	|			checksum[3]				|
209199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
210199767f8SToomas Soome  *
211199767f8SToomas Soome  * Legend:
212199767f8SToomas Soome  *
213199767f8SToomas Soome  * vdev		virtual device ID
214199767f8SToomas Soome  * offset	offset into virtual device
215199767f8SToomas Soome  * LSIZE	logical size
216199767f8SToomas Soome  * PSIZE	physical size (after compression)
217199767f8SToomas Soome  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
218199767f8SToomas Soome  * GRID		RAID-Z layout information (reserved for future use)
219199767f8SToomas Soome  * cksum	checksum function
220199767f8SToomas Soome  * comp		compression function
221199767f8SToomas Soome  * G		gang block indicator
222199767f8SToomas Soome  * B		byteorder (endianness)
223199767f8SToomas Soome  * D		dedup
224199767f8SToomas Soome  * X		encryption (on version 30, which is not supported)
225199767f8SToomas Soome  * E		blkptr_t contains embedded data (see below)
226199767f8SToomas Soome  * lvl		level of indirection
227199767f8SToomas Soome  * type		DMU object type
228199767f8SToomas Soome  * phys birth	txg of block allocation; zero if same as logical birth txg
229199767f8SToomas Soome  * log. birth	transaction group in which the block was logically born
230199767f8SToomas Soome  * fill count	number of non-zero blocks under this bp
231199767f8SToomas Soome  * checksum[4]	256-bit checksum of the data this bp describes
232199767f8SToomas Soome  */
233199767f8SToomas Soome 
234199767f8SToomas Soome /*
235199767f8SToomas Soome  * "Embedded" blkptr_t's don't actually point to a block, instead they
236199767f8SToomas Soome  * have a data payload embedded in the blkptr_t itself.  See the comment
237199767f8SToomas Soome  * in blkptr.c for more details.
238199767f8SToomas Soome  *
239199767f8SToomas Soome  * The blkptr_t is laid out as follows:
240199767f8SToomas Soome  *
241199767f8SToomas Soome  *	64	56	48	40	32	24	16	8	0
242199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
243199767f8SToomas Soome  * 0	|      payload                                                  |
244199767f8SToomas Soome  * 1	|      payload                                                  |
245199767f8SToomas Soome  * 2	|      payload                                                  |
246199767f8SToomas Soome  * 3	|      payload                                                  |
247199767f8SToomas Soome  * 4	|      payload                                                  |
248199767f8SToomas Soome  * 5	|      payload                                                  |
249199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
250199767f8SToomas Soome  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
251199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
252199767f8SToomas Soome  * 7	|      payload                                                  |
253199767f8SToomas Soome  * 8	|      payload                                                  |
254199767f8SToomas Soome  * 9	|      payload                                                  |
255199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
256199767f8SToomas Soome  * a	|			logical birth txg			|
257199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
258199767f8SToomas Soome  * b	|      payload                                                  |
259199767f8SToomas Soome  * c	|      payload                                                  |
260199767f8SToomas Soome  * d	|      payload                                                  |
261199767f8SToomas Soome  * e	|      payload                                                  |
262199767f8SToomas Soome  * f	|      payload                                                  |
263199767f8SToomas Soome  *	+-------+-------+-------+-------+-------+-------+-------+-------+
264199767f8SToomas Soome  *
265199767f8SToomas Soome  * Legend:
266199767f8SToomas Soome  *
267199767f8SToomas Soome  * payload		contains the embedded data
268199767f8SToomas Soome  * B (byteorder)	byteorder (endianness)
269199767f8SToomas Soome  * D (dedup)		padding (set to zero)
270199767f8SToomas Soome  * X			encryption (set to zero; see above)
271199767f8SToomas Soome  * E (embedded)		set to one
272199767f8SToomas Soome  * lvl			indirection level
273199767f8SToomas Soome  * type			DMU object type
274199767f8SToomas Soome  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
275199767f8SToomas Soome  * comp			compression function of payload
276199767f8SToomas Soome  * PSIZE		size of payload after compression, in bytes
277199767f8SToomas Soome  * LSIZE		logical size of payload, in bytes
278199767f8SToomas Soome  *			note that 25 bits is enough to store the largest
279199767f8SToomas Soome  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
280199767f8SToomas Soome  * log. birth		transaction group in which the block was logically born
281199767f8SToomas Soome  *
282199767f8SToomas Soome  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
283199767f8SToomas Soome  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
284199767f8SToomas Soome  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
285199767f8SToomas Soome  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
286199767f8SToomas Soome  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
287199767f8SToomas Soome  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
288199767f8SToomas Soome  * other macros, as they assert that they are only used on BP's of the correct
289199767f8SToomas Soome  * "embedded-ness".
290199767f8SToomas Soome  */
291199767f8SToomas Soome 
292199767f8SToomas Soome #define	BPE_GET_ETYPE(bp)	\
293199767f8SToomas Soome 	(ASSERT(BP_IS_EMBEDDED(bp)), \
294199767f8SToomas Soome 	BF64_GET((bp)->blk_prop, 40, 8))
295199767f8SToomas Soome #define	BPE_SET_ETYPE(bp, t)	do { \
296199767f8SToomas Soome 	ASSERT(BP_IS_EMBEDDED(bp)); \
297199767f8SToomas Soome 	BF64_SET((bp)->blk_prop, 40, 8, t); \
298199767f8SToomas Soome _NOTE(CONSTCOND) } while (0)
299199767f8SToomas Soome 
300199767f8SToomas Soome #define	BPE_GET_LSIZE(bp)	\
301199767f8SToomas Soome 	(ASSERT(BP_IS_EMBEDDED(bp)), \
302199767f8SToomas Soome 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
303199767f8SToomas Soome #define	BPE_SET_LSIZE(bp, x)	do { \
304199767f8SToomas Soome 	ASSERT(BP_IS_EMBEDDED(bp)); \
305199767f8SToomas Soome 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
306199767f8SToomas Soome _NOTE(CONSTCOND) } while (0)
307199767f8SToomas Soome 
308199767f8SToomas Soome #define	BPE_GET_PSIZE(bp)	\
309199767f8SToomas Soome 	(ASSERT(BP_IS_EMBEDDED(bp)), \
310199767f8SToomas Soome 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
311199767f8SToomas Soome #define	BPE_SET_PSIZE(bp, x)	do { \
312199767f8SToomas Soome 	ASSERT(BP_IS_EMBEDDED(bp)); \
313199767f8SToomas Soome 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
314199767f8SToomas Soome _NOTE(CONSTCOND) } while (0)
315199767f8SToomas Soome 
316199767f8SToomas Soome typedef enum bp_embedded_type {
317199767f8SToomas Soome 	BP_EMBEDDED_TYPE_DATA,
318199767f8SToomas Soome 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
319199767f8SToomas Soome 	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
320199767f8SToomas Soome } bp_embedded_type_t;
321199767f8SToomas Soome 
322199767f8SToomas Soome #define	BPE_NUM_WORDS 14
323199767f8SToomas Soome #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
324199767f8SToomas Soome #define	BPE_IS_PAYLOADWORD(bp, wp) \
325199767f8SToomas Soome 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
326199767f8SToomas Soome 
327199767f8SToomas Soome #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
328199767f8SToomas Soome #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
329199767f8SToomas Soome 
330199767f8SToomas Soome typedef struct blkptr {
331199767f8SToomas Soome 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
332199767f8SToomas Soome 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
333199767f8SToomas Soome 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
334199767f8SToomas Soome 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
335199767f8SToomas Soome 	uint64_t	blk_birth;	/* transaction group at birth	    */
336199767f8SToomas Soome 	uint64_t	blk_fill;	/* fill count			    */
337199767f8SToomas Soome 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
338199767f8SToomas Soome } blkptr_t;
339199767f8SToomas Soome 
340199767f8SToomas Soome /*
341199767f8SToomas Soome  * Macros to get and set fields in a bp or DVA.
342199767f8SToomas Soome  */
343199767f8SToomas Soome #define	DVA_GET_ASIZE(dva)	\
344199767f8SToomas Soome 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
345199767f8SToomas Soome #define	DVA_SET_ASIZE(dva, x)	\
346199767f8SToomas Soome 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
347199767f8SToomas Soome 	SPA_MINBLOCKSHIFT, 0, x)
348199767f8SToomas Soome 
349199767f8SToomas Soome #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
350199767f8SToomas Soome #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
351199767f8SToomas Soome 
352199767f8SToomas Soome #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
353199767f8SToomas Soome #define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
354199767f8SToomas Soome 
355199767f8SToomas Soome #define	DVA_GET_OFFSET(dva)	\
356199767f8SToomas Soome 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
357199767f8SToomas Soome #define	DVA_SET_OFFSET(dva, x)	\
358199767f8SToomas Soome 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
359199767f8SToomas Soome 
360199767f8SToomas Soome #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
361199767f8SToomas Soome #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
362199767f8SToomas Soome 
363199767f8SToomas Soome #define	BP_GET_LSIZE(bp)	\
364199767f8SToomas Soome 	(BP_IS_EMBEDDED(bp) ?	\
365199767f8SToomas Soome 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
366199767f8SToomas Soome 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
367199767f8SToomas Soome #define	BP_SET_LSIZE(bp, x)	do { \
368199767f8SToomas Soome 	ASSERT(!BP_IS_EMBEDDED(bp)); \
369199767f8SToomas Soome 	BF64_SET_SB((bp)->blk_prop, \
370199767f8SToomas Soome 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
371199767f8SToomas Soome _NOTE(CONSTCOND) } while (0)
372199767f8SToomas Soome 
373199767f8SToomas Soome #define	BP_GET_PSIZE(bp)	\
374199767f8SToomas Soome 	BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)
375199767f8SToomas Soome #define	BP_SET_PSIZE(bp, x)	\
376199767f8SToomas Soome 	BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x)
377199767f8SToomas Soome 
378199767f8SToomas Soome #define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 7)
379199767f8SToomas Soome #define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 7, x)
380199767f8SToomas Soome 
381199767f8SToomas Soome #define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
382199767f8SToomas Soome #define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
383199767f8SToomas Soome 
384199767f8SToomas Soome #define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
385199767f8SToomas Soome #define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
386199767f8SToomas Soome 
387199767f8SToomas Soome #define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
388199767f8SToomas Soome #define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
389199767f8SToomas Soome 
390199767f8SToomas Soome #define	BP_IS_EMBEDDED(bp)	BF64_GET((bp)->blk_prop, 39, 1)
391199767f8SToomas Soome 
392199767f8SToomas Soome #define	BP_GET_DEDUP(bp)	BF64_GET((bp)->blk_prop, 62, 1)
393199767f8SToomas Soome #define	BP_SET_DEDUP(bp, x)	BF64_SET((bp)->blk_prop, 62, 1, x)
394199767f8SToomas Soome 
395199767f8SToomas Soome #define	BP_GET_BYTEORDER(bp)	BF64_GET((bp)->blk_prop, 63, 1)
396199767f8SToomas Soome #define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
397199767f8SToomas Soome 
398199767f8SToomas Soome #define	BP_PHYSICAL_BIRTH(bp)		\
399199767f8SToomas Soome 	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
400199767f8SToomas Soome 
401199767f8SToomas Soome #define	BP_GET_ASIZE(bp)	\
402199767f8SToomas Soome 	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
403199767f8SToomas Soome 		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
404199767f8SToomas Soome 
405199767f8SToomas Soome #define	BP_GET_UCSIZE(bp) \
406199767f8SToomas Soome 	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
407199767f8SToomas Soome 	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
408199767f8SToomas Soome 
409199767f8SToomas Soome #define	BP_GET_NDVAS(bp)	\
410199767f8SToomas Soome 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
411199767f8SToomas Soome 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
412199767f8SToomas Soome 	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
413199767f8SToomas Soome 
414199767f8SToomas Soome #define	DVA_EQUAL(dva1, dva2)	\
415199767f8SToomas Soome 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
416199767f8SToomas Soome 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
417199767f8SToomas Soome 
418199767f8SToomas Soome #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
419199767f8SToomas Soome 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
420199767f8SToomas Soome 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
421199767f8SToomas Soome 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
422199767f8SToomas Soome 	((zc1).zc_word[3] - (zc2).zc_word[3])))
423199767f8SToomas Soome 
424199767f8SToomas Soome 
425199767f8SToomas Soome #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
426199767f8SToomas Soome 
427199767f8SToomas Soome #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
428199767f8SToomas Soome {						\
429199767f8SToomas Soome 	(zcp)->zc_word[0] = w0;			\
430199767f8SToomas Soome 	(zcp)->zc_word[1] = w1;			\
431199767f8SToomas Soome 	(zcp)->zc_word[2] = w2;			\
432199767f8SToomas Soome 	(zcp)->zc_word[3] = w3;			\
433199767f8SToomas Soome }
434199767f8SToomas Soome 
435199767f8SToomas Soome #define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
436199767f8SToomas Soome #define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
437199767f8SToomas Soome #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&  \
438199767f8SToomas Soome 	(dva)->dva_word[1] == 0ULL)
439199767f8SToomas Soome #define	BP_IS_HOLE(bp)		DVA_IS_EMPTY(BP_IDENTITY(bp))
440199767f8SToomas Soome #define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
441199767f8SToomas Soome 
442199767f8SToomas Soome #define	BP_ZERO(bp)				\
443199767f8SToomas Soome {						\
444199767f8SToomas Soome 	(bp)->blk_dva[0].dva_word[0] = 0;	\
445199767f8SToomas Soome 	(bp)->blk_dva[0].dva_word[1] = 0;	\
446199767f8SToomas Soome 	(bp)->blk_dva[1].dva_word[0] = 0;	\
447199767f8SToomas Soome 	(bp)->blk_dva[1].dva_word[1] = 0;	\
448199767f8SToomas Soome 	(bp)->blk_dva[2].dva_word[0] = 0;	\
449199767f8SToomas Soome 	(bp)->blk_dva[2].dva_word[1] = 0;	\
450199767f8SToomas Soome 	(bp)->blk_prop = 0;			\
451199767f8SToomas Soome 	(bp)->blk_pad[0] = 0;			\
452199767f8SToomas Soome 	(bp)->blk_pad[1] = 0;			\
453199767f8SToomas Soome 	(bp)->blk_phys_birth = 0;		\
454199767f8SToomas Soome 	(bp)->blk_birth = 0;			\
455199767f8SToomas Soome 	(bp)->blk_fill = 0;			\
456199767f8SToomas Soome 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
457199767f8SToomas Soome }
458199767f8SToomas Soome 
459ece0bc84SToomas Soome #if BYTE_ORDER == _BIG_ENDIAN
460ece0bc84SToomas Soome #define	ZFS_HOST_BYTEORDER	(0ULL)
461ece0bc84SToomas Soome #else
462ece0bc84SToomas Soome #define	ZFS_HOST_BYTEORDER	(1ULL)
463ece0bc84SToomas Soome #endif
464ece0bc84SToomas Soome 
465ece0bc84SToomas Soome #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
466199767f8SToomas Soome #define	BPE_NUM_WORDS 14
467199767f8SToomas Soome #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
468199767f8SToomas Soome #define	BPE_IS_PAYLOADWORD(bp, wp) \
469199767f8SToomas Soome 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
470199767f8SToomas Soome 
471199767f8SToomas Soome /*
472199767f8SToomas Soome  * Embedded checksum
473199767f8SToomas Soome  */
474199767f8SToomas Soome #define	ZEC_MAGIC	0x210da7ab10c7a11ULL
475199767f8SToomas Soome 
476199767f8SToomas Soome typedef struct zio_eck {
477199767f8SToomas Soome 	uint64_t	zec_magic;	/* for validation, endianness	*/
478199767f8SToomas Soome 	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
479199767f8SToomas Soome } zio_eck_t;
480199767f8SToomas Soome 
481199767f8SToomas Soome /*
482199767f8SToomas Soome  * Gang block headers are self-checksumming and contain an array
483199767f8SToomas Soome  * of block pointers.
484199767f8SToomas Soome  */
485199767f8SToomas Soome #define	SPA_GANGBLOCKSIZE	SPA_MINBLOCKSIZE
486199767f8SToomas Soome #define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
487199767f8SToomas Soome 	sizeof (zio_eck_t)) / sizeof (blkptr_t))
488199767f8SToomas Soome #define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
489199767f8SToomas Soome 	sizeof (zio_eck_t) - \
490199767f8SToomas Soome 	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
491199767f8SToomas Soome 	sizeof (uint64_t))
492199767f8SToomas Soome 
493199767f8SToomas Soome typedef struct zio_gbh {
494199767f8SToomas Soome 	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
495199767f8SToomas Soome 	uint64_t		zg_filler[SPA_GBH_FILLER];
496199767f8SToomas Soome 	zio_eck_t		zg_tail;
497199767f8SToomas Soome } zio_gbh_phys_t;
498199767f8SToomas Soome 
499199767f8SToomas Soome #define	VDEV_RAIDZ_MAXPARITY	3
500199767f8SToomas Soome 
501199767f8SToomas Soome #define	VDEV_PAD_SIZE		(8 << 10)
502*b713c91eSToomas Soome /* 2 padding areas (vl_pad1 and vl_be) to skip */
503199767f8SToomas Soome #define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
504199767f8SToomas Soome #define	VDEV_PHYS_SIZE		(112 << 10)
505199767f8SToomas Soome #define	VDEV_UBERBLOCK_RING	(128 << 10)
506199767f8SToomas Soome 
5077dcf02b3SToomas Soome /*
5087dcf02b3SToomas Soome  * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
5097dcf02b3SToomas Soome  * ring when MMP is enabled.
5107dcf02b3SToomas Soome  */
5117dcf02b3SToomas Soome #define	MMP_BLOCKS_PER_LABEL	1
5127dcf02b3SToomas Soome 
5137dcf02b3SToomas Soome /* The largest uberblock we support is 8k. */
5147dcf02b3SToomas Soome #define	MAX_UBERBLOCK_SHIFT	(13)
515199767f8SToomas Soome #define	VDEV_UBERBLOCK_SHIFT(vd)	\
5167dcf02b3SToomas Soome 	MIN(MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT), MAX_UBERBLOCK_SHIFT)
517199767f8SToomas Soome #define	VDEV_UBERBLOCK_COUNT(vd)	\
518199767f8SToomas Soome 	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
519199767f8SToomas Soome #define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
520199767f8SToomas Soome 	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
521199767f8SToomas Soome #define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
522199767f8SToomas Soome 
523199767f8SToomas Soome typedef struct vdev_phys {
524199767f8SToomas Soome 	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
525199767f8SToomas Soome 	zio_eck_t	vp_zbt;
526199767f8SToomas Soome } vdev_phys_t;
527199767f8SToomas Soome 
528*b713c91eSToomas Soome typedef enum vbe_vers {
529*b713c91eSToomas Soome 	/* The bootenv file is stored as ascii text in the envblock */
530*b713c91eSToomas Soome 	VB_RAW = 0,
531*b713c91eSToomas Soome 
532*b713c91eSToomas Soome 	/*
533*b713c91eSToomas Soome 	 * The bootenv file is converted to an nvlist and then packed into the
534*b713c91eSToomas Soome 	 * envblock.
535*b713c91eSToomas Soome 	 */
536*b713c91eSToomas Soome 	VB_NVLIST = 1
537*b713c91eSToomas Soome } vbe_vers_t;
538*b713c91eSToomas Soome 
539*b713c91eSToomas Soome typedef struct vdev_boot_envblock {
540*b713c91eSToomas Soome 	uint64_t	vbe_version;
541*b713c91eSToomas Soome 	char		vbe_bootenv[VDEV_PAD_SIZE - sizeof (uint64_t) -
542*b713c91eSToomas Soome 			sizeof (zio_eck_t)];
543*b713c91eSToomas Soome 	zio_eck_t	vbe_zbt;
544*b713c91eSToomas Soome } vdev_boot_envblock_t;
545*b713c91eSToomas Soome 
546*b713c91eSToomas Soome CTASSERT(sizeof (vdev_boot_envblock_t) == VDEV_PAD_SIZE);
547*b713c91eSToomas Soome 
548199767f8SToomas Soome typedef struct vdev_label {
549199767f8SToomas Soome 	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K  */
550*b713c91eSToomas Soome 	vdev_boot_envblock_t vl_be;				/*  8K  */
551199767f8SToomas Soome 	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
552199767f8SToomas Soome 	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
553199767f8SToomas Soome } vdev_label_t;							/* 256K total */
554199767f8SToomas Soome 
555199767f8SToomas Soome /*
556199767f8SToomas Soome  * vdev_dirty() flags
557199767f8SToomas Soome  */
558199767f8SToomas Soome #define	VDD_METASLAB	0x01
559199767f8SToomas Soome #define	VDD_DTL		0x02
560199767f8SToomas Soome 
561199767f8SToomas Soome /*
562199767f8SToomas Soome  * Size and offset of embedded boot loader region on each label.
563199767f8SToomas Soome  * The total size of the first two labels plus the boot area is 4MB.
564199767f8SToomas Soome  */
565199767f8SToomas Soome #define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
566199767f8SToomas Soome #define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M	*/
567199767f8SToomas Soome 
568199767f8SToomas Soome /*
569199767f8SToomas Soome  * Size of label regions at the start and end of each leaf device.
570199767f8SToomas Soome  */
571199767f8SToomas Soome #define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
572199767f8SToomas Soome #define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
573199767f8SToomas Soome #define	VDEV_LABELS		4
574199767f8SToomas Soome 
575199767f8SToomas Soome enum zio_checksum {
576199767f8SToomas Soome 	ZIO_CHECKSUM_INHERIT = 0,
577199767f8SToomas Soome 	ZIO_CHECKSUM_ON,
578199767f8SToomas Soome 	ZIO_CHECKSUM_OFF,
579199767f8SToomas Soome 	ZIO_CHECKSUM_LABEL,
580199767f8SToomas Soome 	ZIO_CHECKSUM_GANG_HEADER,
581199767f8SToomas Soome 	ZIO_CHECKSUM_ZILOG,
582199767f8SToomas Soome 	ZIO_CHECKSUM_FLETCHER_2,
583199767f8SToomas Soome 	ZIO_CHECKSUM_FLETCHER_4,
584199767f8SToomas Soome 	ZIO_CHECKSUM_SHA256,
585199767f8SToomas Soome 	ZIO_CHECKSUM_ZILOG2,
586199767f8SToomas Soome 	ZIO_CHECKSUM_NOPARITY,
587199767f8SToomas Soome 	ZIO_CHECKSUM_SHA512,
588199767f8SToomas Soome 	ZIO_CHECKSUM_SKEIN,
589199767f8SToomas Soome 	ZIO_CHECKSUM_EDONR,
590199767f8SToomas Soome 	ZIO_CHECKSUM_FUNCTIONS
591199767f8SToomas Soome };
592199767f8SToomas Soome 
593199767f8SToomas Soome #define	ZIO_CHECKSUM_ON_VALUE	ZIO_CHECKSUM_FLETCHER_4
594199767f8SToomas Soome #define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
595199767f8SToomas Soome 
596199767f8SToomas Soome enum zio_compress {
597199767f8SToomas Soome 	ZIO_COMPRESS_INHERIT = 0,
598199767f8SToomas Soome 	ZIO_COMPRESS_ON,
599199767f8SToomas Soome 	ZIO_COMPRESS_OFF,
600199767f8SToomas Soome 	ZIO_COMPRESS_LZJB,
601199767f8SToomas Soome 	ZIO_COMPRESS_EMPTY,
602199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_1,
603199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_2,
604199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_3,
605199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_4,
606199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_5,
607199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_6,
608199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_7,
609199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_8,
610199767f8SToomas Soome 	ZIO_COMPRESS_GZIP_9,
611199767f8SToomas Soome 	ZIO_COMPRESS_ZLE,
612199767f8SToomas Soome 	ZIO_COMPRESS_LZ4,
613199767f8SToomas Soome 	ZIO_COMPRESS_FUNCTIONS
614199767f8SToomas Soome };
615199767f8SToomas Soome 
616199767f8SToomas Soome #define	ZIO_COMPRESS_ON_VALUE	ZIO_COMPRESS_LZJB
617199767f8SToomas Soome #define	ZIO_COMPRESS_DEFAULT	ZIO_COMPRESS_OFF
618199767f8SToomas Soome 
619199767f8SToomas Soome /* nvlist pack encoding */
620199767f8SToomas Soome #define	NV_ENCODE_NATIVE	0
621199767f8SToomas Soome #define	NV_ENCODE_XDR		1
622199767f8SToomas Soome 
623199767f8SToomas Soome typedef enum {
624199767f8SToomas Soome 	DATA_TYPE_UNKNOWN = 0,
625199767f8SToomas Soome 	DATA_TYPE_BOOLEAN,
626199767f8SToomas Soome 	DATA_TYPE_BYTE,
627199767f8SToomas Soome 	DATA_TYPE_INT16,
628199767f8SToomas Soome 	DATA_TYPE_UINT16,
629199767f8SToomas Soome 	DATA_TYPE_INT32,
630199767f8SToomas Soome 	DATA_TYPE_UINT32,
631199767f8SToomas Soome 	DATA_TYPE_INT64,
632199767f8SToomas Soome 	DATA_TYPE_UINT64,
633199767f8SToomas Soome 	DATA_TYPE_STRING,
634199767f8SToomas Soome 	DATA_TYPE_BYTE_ARRAY,
635199767f8SToomas Soome 	DATA_TYPE_INT16_ARRAY,
636199767f8SToomas Soome 	DATA_TYPE_UINT16_ARRAY,
637199767f8SToomas Soome 	DATA_TYPE_INT32_ARRAY,
638199767f8SToomas Soome 	DATA_TYPE_UINT32_ARRAY,
639199767f8SToomas Soome 	DATA_TYPE_INT64_ARRAY,
640199767f8SToomas Soome 	DATA_TYPE_UINT64_ARRAY,
641199767f8SToomas Soome 	DATA_TYPE_STRING_ARRAY,
642199767f8SToomas Soome 	DATA_TYPE_HRTIME,
643199767f8SToomas Soome 	DATA_TYPE_NVLIST,
644199767f8SToomas Soome 	DATA_TYPE_NVLIST_ARRAY,
645199767f8SToomas Soome 	DATA_TYPE_BOOLEAN_VALUE,
646199767f8SToomas Soome 	DATA_TYPE_INT8,
647199767f8SToomas Soome 	DATA_TYPE_UINT8,
648199767f8SToomas Soome 	DATA_TYPE_BOOLEAN_ARRAY,
649199767f8SToomas Soome 	DATA_TYPE_INT8_ARRAY,
650199767f8SToomas Soome 	DATA_TYPE_UINT8_ARRAY
651199767f8SToomas Soome } data_type_t;
652199767f8SToomas Soome 
653199767f8SToomas Soome /*
654199767f8SToomas Soome  * On-disk version number.
655199767f8SToomas Soome  */
656199767f8SToomas Soome #define	SPA_VERSION_1			1ULL
657199767f8SToomas Soome #define	SPA_VERSION_2			2ULL
658199767f8SToomas Soome #define	SPA_VERSION_3			3ULL
659199767f8SToomas Soome #define	SPA_VERSION_4			4ULL
660199767f8SToomas Soome #define	SPA_VERSION_5			5ULL
661199767f8SToomas Soome #define	SPA_VERSION_6			6ULL
662199767f8SToomas Soome #define	SPA_VERSION_7			7ULL
663199767f8SToomas Soome #define	SPA_VERSION_8			8ULL
664199767f8SToomas Soome #define	SPA_VERSION_9			9ULL
665199767f8SToomas Soome #define	SPA_VERSION_10			10ULL
666199767f8SToomas Soome #define	SPA_VERSION_11			11ULL
667199767f8SToomas Soome #define	SPA_VERSION_12			12ULL
668199767f8SToomas Soome #define	SPA_VERSION_13			13ULL
669199767f8SToomas Soome #define	SPA_VERSION_14			14ULL
670199767f8SToomas Soome #define	SPA_VERSION_15			15ULL
671199767f8SToomas Soome #define	SPA_VERSION_16			16ULL
672199767f8SToomas Soome #define	SPA_VERSION_17			17ULL
673199767f8SToomas Soome #define	SPA_VERSION_18			18ULL
674199767f8SToomas Soome #define	SPA_VERSION_19			19ULL
675199767f8SToomas Soome #define	SPA_VERSION_20			20ULL
676199767f8SToomas Soome #define	SPA_VERSION_21			21ULL
677199767f8SToomas Soome #define	SPA_VERSION_22			22ULL
678199767f8SToomas Soome #define	SPA_VERSION_23			23ULL
679199767f8SToomas Soome #define	SPA_VERSION_24			24ULL
680199767f8SToomas Soome #define	SPA_VERSION_25			25ULL
681199767f8SToomas Soome #define	SPA_VERSION_26			26ULL
682199767f8SToomas Soome #define	SPA_VERSION_27			27ULL
683199767f8SToomas Soome #define	SPA_VERSION_28			28ULL
684199767f8SToomas Soome #define	SPA_VERSION_5000		5000ULL
685199767f8SToomas Soome 
686199767f8SToomas Soome /*
687199767f8SToomas Soome  * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
688199767f8SToomas Soome  * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
689199767f8SToomas Soome  * and do the appropriate changes.  Also bump the version number in
690199767f8SToomas Soome  * usr/src/grub/capability.
691199767f8SToomas Soome  */
692199767f8SToomas Soome #define	SPA_VERSION			SPA_VERSION_5000
693199767f8SToomas Soome #define	SPA_VERSION_STRING		"5000"
694199767f8SToomas Soome 
695199767f8SToomas Soome /*
696199767f8SToomas Soome  * Symbolic names for the changes that caused a SPA_VERSION switch.
697199767f8SToomas Soome  * Used in the code when checking for presence or absence of a feature.
698199767f8SToomas Soome  * Feel free to define multiple symbolic names for each version if there
699199767f8SToomas Soome  * were multiple changes to on-disk structures during that version.
700199767f8SToomas Soome  *
701199767f8SToomas Soome  * NOTE: When checking the current SPA_VERSION in your code, be sure
702199767f8SToomas Soome  *       to use spa_version() since it reports the version of the
703199767f8SToomas Soome  *       last synced uberblock.  Checking the in-flight version can
704199767f8SToomas Soome  *       be dangerous in some cases.
705199767f8SToomas Soome  */
706199767f8SToomas Soome #define	SPA_VERSION_INITIAL		SPA_VERSION_1
707199767f8SToomas Soome #define	SPA_VERSION_DITTO_BLOCKS	SPA_VERSION_2
708199767f8SToomas Soome #define	SPA_VERSION_SPARES		SPA_VERSION_3
709199767f8SToomas Soome #define	SPA_VERSION_RAID6		SPA_VERSION_3
710199767f8SToomas Soome #define	SPA_VERSION_BPLIST_ACCOUNT	SPA_VERSION_3
711199767f8SToomas Soome #define	SPA_VERSION_RAIDZ_DEFLATE	SPA_VERSION_3
712199767f8SToomas Soome #define	SPA_VERSION_DNODE_BYTES		SPA_VERSION_3
713199767f8SToomas Soome #define	SPA_VERSION_ZPOOL_HISTORY	SPA_VERSION_4
714199767f8SToomas Soome #define	SPA_VERSION_GZIP_COMPRESSION	SPA_VERSION_5
715199767f8SToomas Soome #define	SPA_VERSION_BOOTFS		SPA_VERSION_6
716199767f8SToomas Soome #define	SPA_VERSION_SLOGS		SPA_VERSION_7
717199767f8SToomas Soome #define	SPA_VERSION_DELEGATED_PERMS	SPA_VERSION_8
718199767f8SToomas Soome #define	SPA_VERSION_FUID		SPA_VERSION_9
719199767f8SToomas Soome #define	SPA_VERSION_REFRESERVATION	SPA_VERSION_9
720199767f8SToomas Soome #define	SPA_VERSION_REFQUOTA		SPA_VERSION_9
721199767f8SToomas Soome #define	SPA_VERSION_UNIQUE_ACCURATE	SPA_VERSION_9
722199767f8SToomas Soome #define	SPA_VERSION_L2CACHE		SPA_VERSION_10
723199767f8SToomas Soome #define	SPA_VERSION_NEXT_CLONES		SPA_VERSION_11
724199767f8SToomas Soome #define	SPA_VERSION_ORIGIN		SPA_VERSION_11
725199767f8SToomas Soome #define	SPA_VERSION_DSL_SCRUB		SPA_VERSION_11
726199767f8SToomas Soome #define	SPA_VERSION_SNAP_PROPS		SPA_VERSION_12
727199767f8SToomas Soome #define	SPA_VERSION_USED_BREAKDOWN	SPA_VERSION_13
728199767f8SToomas Soome #define	SPA_VERSION_PASSTHROUGH_X	SPA_VERSION_14
72908487eeaSToomas Soome #define	SPA_VERSION_USERSPACE		SPA_VERSION_15
730199767f8SToomas Soome #define	SPA_VERSION_STMF_PROP		SPA_VERSION_16
731199767f8SToomas Soome #define	SPA_VERSION_RAIDZ3		SPA_VERSION_17
732199767f8SToomas Soome #define	SPA_VERSION_USERREFS		SPA_VERSION_18
733199767f8SToomas Soome #define	SPA_VERSION_HOLES		SPA_VERSION_19
734199767f8SToomas Soome #define	SPA_VERSION_ZLE_COMPRESSION	SPA_VERSION_20
735199767f8SToomas Soome #define	SPA_VERSION_DEDUP		SPA_VERSION_21
736199767f8SToomas Soome #define	SPA_VERSION_RECVD_PROPS		SPA_VERSION_22
737199767f8SToomas Soome #define	SPA_VERSION_SLIM_ZIL		SPA_VERSION_23
738199767f8SToomas Soome #define	SPA_VERSION_SA			SPA_VERSION_24
739199767f8SToomas Soome #define	SPA_VERSION_SCAN		SPA_VERSION_25
740199767f8SToomas Soome #define	SPA_VERSION_DIR_CLONES		SPA_VERSION_26
741199767f8SToomas Soome #define	SPA_VERSION_DEADLISTS		SPA_VERSION_26
742199767f8SToomas Soome #define	SPA_VERSION_FAST_SNAP		SPA_VERSION_27
743199767f8SToomas Soome #define	SPA_VERSION_MULTI_REPLACE	SPA_VERSION_28
744199767f8SToomas Soome #define	SPA_VERSION_BEFORE_FEATURES	SPA_VERSION_28
745199767f8SToomas Soome #define	SPA_VERSION_FEATURES		SPA_VERSION_5000
746199767f8SToomas Soome 
747199767f8SToomas Soome #define	SPA_VERSION_IS_SUPPORTED(v) \
748199767f8SToomas Soome 	(((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
749199767f8SToomas Soome 	((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
750199767f8SToomas Soome 
751199767f8SToomas Soome /*
752199767f8SToomas Soome  * The following are configuration names used in the nvlist describing a pool's
753199767f8SToomas Soome  * configuration.
754199767f8SToomas Soome  */
755199767f8SToomas Soome #define	ZPOOL_CONFIG_VERSION		"version"
756199767f8SToomas Soome #define	ZPOOL_CONFIG_POOL_NAME		"name"
757199767f8SToomas Soome #define	ZPOOL_CONFIG_POOL_STATE		"state"
758199767f8SToomas Soome #define	ZPOOL_CONFIG_POOL_TXG		"txg"
759199767f8SToomas Soome #define	ZPOOL_CONFIG_POOL_GUID		"pool_guid"
760199767f8SToomas Soome #define	ZPOOL_CONFIG_CREATE_TXG		"create_txg"
761199767f8SToomas Soome #define	ZPOOL_CONFIG_TOP_GUID		"top_guid"
762199767f8SToomas Soome #define	ZPOOL_CONFIG_VDEV_TREE		"vdev_tree"
763199767f8SToomas Soome #define	ZPOOL_CONFIG_TYPE		"type"
764199767f8SToomas Soome #define	ZPOOL_CONFIG_CHILDREN		"children"
765199767f8SToomas Soome #define	ZPOOL_CONFIG_ID			"id"
766199767f8SToomas Soome #define	ZPOOL_CONFIG_GUID		"guid"
767c023f651SToomas Soome #define	ZPOOL_CONFIG_INDIRECT_OBJECT	"com.delphix:indirect_object"
768c023f651SToomas Soome #define	ZPOOL_CONFIG_INDIRECT_BIRTHS	"com.delphix:indirect_births"
769c023f651SToomas Soome #define	ZPOOL_CONFIG_PREV_INDIRECT_VDEV	"com.delphix:prev_indirect_vdev"
770199767f8SToomas Soome #define	ZPOOL_CONFIG_PATH		"path"
771199767f8SToomas Soome #define	ZPOOL_CONFIG_DEVID		"devid"
772199767f8SToomas Soome #define	ZPOOL_CONFIG_PHYS_PATH		"phys_path"
773199767f8SToomas Soome #define	ZPOOL_CONFIG_METASLAB_ARRAY	"metaslab_array"
774199767f8SToomas Soome #define	ZPOOL_CONFIG_METASLAB_SHIFT	"metaslab_shift"
775199767f8SToomas Soome #define	ZPOOL_CONFIG_ASHIFT		"ashift"
776199767f8SToomas Soome #define	ZPOOL_CONFIG_ASIZE		"asize"
777199767f8SToomas Soome #define	ZPOOL_CONFIG_DTL		"DTL"
778199767f8SToomas Soome #define	ZPOOL_CONFIG_STATS		"stats"
779199767f8SToomas Soome #define	ZPOOL_CONFIG_WHOLE_DISK		"whole_disk"
780199767f8SToomas Soome #define	ZPOOL_CONFIG_ERRCOUNT		"error_count"
781199767f8SToomas Soome #define	ZPOOL_CONFIG_NOT_PRESENT	"not_present"
782199767f8SToomas Soome #define	ZPOOL_CONFIG_SPARES		"spares"
783199767f8SToomas Soome #define	ZPOOL_CONFIG_IS_SPARE		"is_spare"
784199767f8SToomas Soome #define	ZPOOL_CONFIG_NPARITY		"nparity"
785199767f8SToomas Soome #define	ZPOOL_CONFIG_HOSTID		"hostid"
786199767f8SToomas Soome #define	ZPOOL_CONFIG_HOSTNAME		"hostname"
787199767f8SToomas Soome #define	ZPOOL_CONFIG_IS_LOG		"is_log"
788199767f8SToomas Soome #define	ZPOOL_CONFIG_TIMESTAMP		"timestamp" /* not stored on disk */
789199767f8SToomas Soome #define	ZPOOL_CONFIG_FEATURES_FOR_READ	"features_for_read"
790da9bf005SToomas Soome #define	ZPOOL_CONFIG_VDEV_CHILDREN	"vdev_children"
791199767f8SToomas Soome 
792199767f8SToomas Soome /*
793199767f8SToomas Soome  * The persistent vdev state is stored as separate values rather than a single
794199767f8SToomas Soome  * 'vdev_state' entry.  This is because a device can be in multiple states, such
795199767f8SToomas Soome  * as offline and degraded.
796199767f8SToomas Soome  */
79708487eeaSToomas Soome #define	ZPOOL_CONFIG_OFFLINE		"offline"
79808487eeaSToomas Soome #define	ZPOOL_CONFIG_FAULTED		"faulted"
79908487eeaSToomas Soome #define	ZPOOL_CONFIG_DEGRADED		"degraded"