108487eeToomas Soome/*
2199767fToomas Soome * Copyright (c) 2002 McAfee, Inc.
3199767fToomas Soome * All rights reserved.
4199767fToomas Soome *
5199767fToomas Soome * This software was developed for the FreeBSD Project by Marshall
6199767fToomas Soome * Kirk McKusick and McAfee Research,, the Security Research Division of
7199767fToomas Soome * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
8199767fToomas Soome * part of the DARPA CHATS research program
9199767fToomas Soome *
10199767fToomas Soome * Redistribution and use in source and binary forms, with or without
11199767fToomas Soome * modification, are permitted provided that the following conditions
12199767fToomas Soome * are met:
13199767fToomas Soome * 1. Redistributions of source code must retain the above copyright
14199767fToomas Soome *    notice, this list of conditions and the following disclaimer.
15199767fToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
16199767fToomas Soome *    notice, this list of conditions and the following disclaimer in the
17199767fToomas Soome *    documentation and/or other materials provided with the distribution.
18199767fToomas Soome *
29199767fToomas Soome * SUCH DAMAGE.
30199767fToomas Soome */
31199767fToomas Soome/*
32199767fToomas Soome * CDDL HEADER START
33199767fToomas Soome *
34199767fToomas Soome * The contents of this file are subject to the terms of the
35199767fToomas Soome * Common Development and Distribution License (the "License").
36199767fToomas Soome * You may not use this file except in compliance with the License.
37199767fToomas Soome *
38199767fToomas Soome * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
39199767fToomas Soome * or http://www.opensolaris.org/os/licensing.
40199767fToomas Soome * See the License for the specific language governing permissions
41199767fToomas Soome * and limitations under the License.
42199767fToomas Soome *
43199767fToomas Soome * When distributing Covered Code, include this CDDL HEADER in each
44199767fToomas Soome * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45199767fToomas Soome * If applicable, add the following below this CDDL HEADER, with the
46199767fToomas Soome * fields enclosed by brackets "[]" replaced with your own identifying
47199767fToomas Soome * information: Portions Copyright [yyyy] [name of copyright owner]
48199767fToomas Soome *
49199767fToomas Soome * CDDL HEADER END
50199767fToomas Soome */
51199767fToomas Soome/*
52199767fToomas Soome * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
53199767fToomas Soome * Use is subject to license terms.
54199767fToomas Soome */
55199767fToomas Soome/*
56199767fToomas Soome * Copyright 2013 by Saso Kiselkov. All rights reserved.
57199767fToomas Soome */
58199767fToomas Soome/*
59199767fToomas Soome * Copyright (c) 2013 by Delphix. All rights reserved.
60199767fToomas Soome */
61199767fToomas Soome
6208487eeToomas Soome#ifndef _ZFSIMPL_H
6308487eeToomas Soome#define	_ZFSIMPL_H
6408487eeToomas Soome
65199767fToomas Soome#define	MAXNAMELEN	256
66199767fToomas Soome
67199767fToomas Soome#define _NOTE(s)
68199767fToomas Soome
697dcf02bToomas Soome/*
707dcf02bToomas Soome * AVL comparator helpers
717dcf02bToomas Soome */
727dcf02bToomas Soome#define	AVL_ISIGN(a)	(((a) > 0) - ((a) < 0))
737dcf02bToomas Soome#define	AVL_CMP(a, b)	(((a) > (b)) - ((a) < (b)))
747dcf02bToomas Soome#define	AVL_PCMP(a, b)	\
757dcf02bToomas Soome	(((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b)))
767dcf02bToomas Soome
77199767fToomas Soome/* CRC64 table */
78199767fToomas Soome#define	ZFS_CRC64_POLY	0xC96C5795D7870F42ULL	/* ECMA-182, reflected form */
79199767fToomas Soome
80199767fToomas Soome/*
81199767fToomas Soome * Macros for various sorts of alignment and rounding when the alignment
82199767fToomas Soome * is known to be a power of 2.
83199767fToomas Soome */
84199767fToomas Soome#define	P2ALIGN(x, align)		((x) & -(align))
85199767fToomas Soome#define	P2PHASE(x, align)		((x) & ((align) - 1))
86199767fToomas Soome#define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
87199767fToomas Soome#define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
88199767fToomas Soome#define	P2END(x, align)			(-(~(x) & -(align)))
89199767fToomas Soome#define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
9008487eeToomas Soome#define	P2BOUNDARY(off, len, align)	\
9108487eeToomas Soome	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
92199767fToomas Soome
93199767fToomas Soome/*
94199767fToomas Soome * General-purpose 32-bit and 64-bit bitfield encodings.
95199767fToomas Soome */
96199767fToomas Soome#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
97199767fToomas Soome#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
98199767fToomas Soome#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
99199767fToomas Soome#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
100199767fToomas Soome
101199767fToomas Soome#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
102199767fToomas Soome#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
103199767fToomas Soome
104199767fToomas Soome#define	BF32_SET(x, low, len, val)	\
105199767fToomas Soome	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
106199767fToomas Soome#define	BF64_SET(x, low, len, val)	\
107199767fToomas Soome	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
108199767fToomas Soome
109199767fToomas Soome#define	BF32_GET_SB(x, low, len, shift, bias)	\
110199767fToomas Soome	((BF32_GET(x, low, len) + (bias)) << (shift))
111199767fToomas Soome#define	BF64_GET_SB(x, low, len, shift, bias)	\
112199767fToomas Soome	((BF64_GET(x, low, len) + (bias)) << (shift))
113199767fToomas Soome
114199767fToomas Soome#define	BF32_SET_SB(x, low, len, shift, bias, val)	\
115199767fToomas Soome	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
116199767fToomas Soome#define	BF64_SET_SB(x, low, len, shift, bias, val)	\
117199767fToomas Soome	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
118199767fToomas Soome
119199767fToomas Soome/*
120199767fToomas Soome * Macros to reverse byte order
121199767fToomas Soome */
122199767fToomas Soome#define	BSWAP_8(x)	((x) & 0xff)
123199767fToomas Soome#define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
124199767fToomas Soome#define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
125199767fToomas Soome#define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
126199767fToomas Soome
127199767fToomas Soome#define	SPA_MINBLOCKSHIFT	9
128199767fToomas Soome#define	SPA_OLDMAXBLOCKSHIFT	17
129199767fToomas Soome#define	SPA_MAXBLOCKSHIFT	24
130199767fToomas Soome#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
132199767fToomas Soome#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
133199767fToomas Soome
134199767fToomas Soome/*
135199767fToomas Soome * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
136199767fToomas Soome * The ASIZE encoding should be at least 64 times larger (6 more bits)
137199767fToomas Soome * to support up to 4-way RAID-Z mirror mode with worst-case gang block
138199767fToomas Soome * overhead, three DVAs per bp, plus one more bit in case we do anything
139199767fToomas Soome * else that expands the ASIZE.
140199767fToomas Soome */
141199767fToomas Soome#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
142199767fToomas Soome#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
143199767fToomas Soome#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
144199767fToomas Soome
145199767fToomas Soome/*
146199767fToomas Soome * All SPA data is represented by 128-bit data virtual addresses (DVAs).
147199767fToomas Soome * The members of the dva_t should be considered opaque outside the SPA.
148199767fToomas Soome */
149199767fToomas Soometypedef struct dva {
150199767fToomas Soome	uint64_t	dva_word[2];
151199767fToomas Soome} dva_t;
152199767fToomas Soome
153199767fToomas Soome/*
154199767fToomas Soome * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
155199767fToomas Soome */
156199767fToomas Soometypedef struct zio_cksum {
157199767fToomas Soome	uint64_t	zc_word[4];
158199767fToomas Soome} zio_cksum_t;
159199767fToomas Soome
160199767fToomas Soome/*
161199767fToomas Soome * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
162199767fToomas Soome * secret and is suitable for use in MAC algorithms as the key.
163199767fToomas Soome */
164199767fToomas Soometypedef struct zio_cksum_salt {
165199767fToomas Soome	uint8_t		zcs_bytes[32];
166199767fToomas Soome} zio_cksum_salt_t;
167199767fToomas Soome
168199767fToomas Soome/*
169199767fToomas Soome * Each block is described by its DVAs, time of birth, checksum, etc.
170199767fToomas Soome * The word-by-word, bit-by-bit layout of the blkptr is as follows:
171199767fToomas Soome *
172199767fToomas Soome *	64	56	48	40	32	24	16	8	0
173199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
174199767fToomas Soome * 0	|		vdev1		| GRID  |	  ASIZE		|
175199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
176199767fToomas Soome * 1	|G|			 offset1				|
177199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
178199767fToomas Soome * 2	|		vdev2		| GRID  |	  ASIZE		|
179199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
180199767fToomas Soome * 3	|G|			 offset2				|
181199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
182199767fToomas Soome * 4	|		vdev3		| GRID  |	  ASIZE		|
183199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
184199767fToomas Soome * 5	|G|			 offset3				|
185199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
186199767fToomas Soome * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
187199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
188199767fToomas Soome * 7	|			padding					|
189199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
190199767fToomas Soome * 8	|			padding					|
191199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
192199767fToomas Soome * 9	|			physical birth txg			|
193199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
194199767fToomas Soome * a	|			logical birth txg			|
195199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
196199767fToomas Soome * b	|			fill count				|
197199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
198199767fToomas Soome * c	|			checksum[0]				|
199199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
200199767fToomas Soome * d	|			checksum[1]				|
201199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
202199767fToomas Soome * e	|			checksum[2]				|
203199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
204199767fToomas Soome * f	|			checksum[3]				|
205199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
206199767fToomas Soome *
207199767fToomas Soome * Legend:
208199767fToomas Soome *
209199767fToomas Soome * vdev		virtual device ID
210199767fToomas Soome * offset	offset into virtual device
211199767fToomas Soome * LSIZE	logical size
212199767fToomas Soome * PSIZE	physical size (after compression)
213199767fToomas Soome * ASIZE	allocated size (including RAID-Z parity and gang block headers)
214199767fToomas Soome * GRID		RAID-Z layout information (reserved for future use)
215199767fToomas Soome * cksum	checksum function
216199767fToomas Soome * comp		compression function
217199767fToomas Soome * G		gang block indicator
218199767fToomas Soome * B		byteorder (endianness)
219199767fToomas Soome * D		dedup
220199767fToomas Soome * X		encryption (on version 30, which is not supported)
221199767fToomas Soome * E		blkptr_t contains embedded data (see below)
222199767fToomas Soome * lvl		level of indirection
223199767fToomas Soome * type		DMU object type
224199767fToomas Soome * phys birth	txg of block allocation; zero if same as logical birth txg
225199767fToomas Soome * log. birth	transaction group in which the block was logically born
226199767fToomas Soome * fill count	number of non-zero blocks under this bp
227199767fToomas Soome * checksum[4]	256-bit checksum of the data this bp describes
228199767fToomas Soome */
229199767fToomas Soome
230199767fToomas Soome/*
231199767fToomas Soome * "Embedded" blkptr_t's don't actually point to a block, instead they
232199767fToomas Soome * have a data payload embedded in the blkptr_t itself.  See the comment
233199767fToomas Soome * in blkptr.c for more details.
234199767fToomas Soome *
235199767fToomas Soome * The blkptr_t is laid out as follows:
236199767fToomas Soome *
237199767fToomas Soome *	64	56	48	40	32	24	16	8	0
238199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
239199767fToomas Soome * 0	|      payload                                                  |
240199767fToomas Soome * 1	|      payload                                                  |
241199767fToomas Soome * 2	|      payload                                                  |
242199767fToomas Soome * 3	|      payload                                                  |
243199767fToomas Soome * 4	|      payload                                                  |
244199767fToomas Soome * 5	|      payload                                                  |
245199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
246199767fToomas Soome * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
247199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
248199767fToomas Soome * 7	|      payload                                                  |
249199767fToomas Soome * 8	|      payload                                                  |
250199767fToomas Soome * 9	|      payload                                                  |
251199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
252199767fToomas Soome * a	|			logical birth txg			|
253199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
254199767fToomas Soome * b	|      payload                                                  |
255199767fToomas Soome * c	|      payload                                                  |
256199767fToomas Soome * d	|      payload                                                  |
257199767fToomas Soome * e	|      payload                                                  |
258199767fToomas Soome * f	|      payload                                                  |
259199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
260199767fToomas Soome *
261199767fToomas Soome * Legend:
262199767fToomas Soome *
263199767fToomas Soome * payload		contains the embedded data
264199767fToomas Soome * B (byteorder)	byteorder (endianness)
265199767fToomas Soome * D (dedup)		padding (set to zero)
266199767fToomas Soome * X			encryption (set to zero; see above)
267199767fToomas Soome * E (embedded)		set to one
268199767fToomas Soome * lvl			indirection level
269199767fToomas Soome * type			DMU object type
270199767fToomas Soome * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
271199767fToomas Soome * comp			compression function of payload
272199767fToomas Soome * PSIZE		size of payload after compression, in bytes
273199767fToomas Soome * LSIZE		logical size of payload, in bytes
274199767fToomas Soome *			note that 25 bits is enough to store the largest
275199767fToomas Soome *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
276199767fToomas Soome * log. birth		transaction group in which the block was logically born
277199767fToomas Soome *
278199767fToomas Soome * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
279199767fToomas Soome * bp's they are stored in units of SPA_MINBLOCKSHIFT.
280199767fToomas Soome * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
281199767fToomas Soome * The B, D, X, lvl, type, and comp fields are stored the same as with normal
282199767fToomas Soome * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
283199767fToomas Soome * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
284199767fToomas Soome * other macros, as they assert that they are only used on BP's of the correct
285199767fToomas Soome * "embedded-ness".
286199767fToomas Soome */
287199767fToomas Soome
288199767fToomas Soome#define	BPE_GET_ETYPE(bp)	\
289199767fToomas Soome	(ASSERT(BP_IS_EMBEDDED(bp)), \
290199767fToomas Soome	BF64_GET((bp)->blk_prop, 40, 8))
291199767fToomas Soome#define	BPE_SET_ETYPE(bp, t)	do { \
292199767fToomas Soome	ASSERT(BP_IS_EMBEDDED(bp)); \
293199767fToomas Soome	BF64_SET((bp)->blk_prop, 40, 8, t); \
294199767fToomas Soome_NOTE(CONSTCOND) } while (0)
295199767fToomas Soome
296199767fToomas Soome#define	BPE_GET_LSIZE(bp)	\
297199767fToomas Soome	(ASSERT(BP_IS_EMBEDDED(bp)), \
298199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
299199767fToomas Soome#define	BPE_SET_LSIZE(bp, x)	do { \
300199767fToomas Soome	ASSERT(BP_IS_EMBEDDED(bp)); \
301199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
302199767fToomas Soome_NOTE(CONSTCOND) } while (0)
303199767fToomas Soome
304199767fToomas Soome#define	BPE_GET_PSIZE(bp)	\
305199767fToomas Soome	(ASSERT(BP_IS_EMBEDDED(bp)), \
306199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
307199767fToomas Soome#define	BPE_SET_PSIZE(bp, x)	do { \
308199767fToomas Soome	ASSERT(BP_IS_EMBEDDED(bp)); \
309199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
310199767fToomas Soome_NOTE(CONSTCOND) } while (0)
311199767fToomas Soome
312199767fToomas Soometypedef enum bp_embedded_type {
313199767fToomas Soome	BP_EMBEDDED_TYPE_DATA,
314199767fToomas Soome	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
316199767fToomas Soome} bp_embedded_type_t;
317199767fToomas Soome
318199767fToomas Soome#define	BPE_NUM_WORDS 14
319199767fToomas Soome#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
320199767fToomas Soome#define	BPE_IS_PAYLOADWORD(bp, wp) \
321199767fToomas Soome	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
322199767fToomas Soome
323199767fToomas Soome#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
324199767fToomas Soome#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
325199767fToomas Soome
326199767fToomas Soometypedef struct blkptr {
327199767fToomas Soome	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
328199767fToomas Soome	uint64_t	blk_prop;	/* size, compression, type, etc	    */
329199767fToomas Soome	uint64_t	blk_pad[2];	/* Extra space for the future	    */
330199767fToomas Soome	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
331199767fToomas Soome	uint64_t	blk_birth;	/* transaction group at birth	    */
332199767fToomas Soome	uint64_t	blk_fill;	/* fill count			    */
333199767fToomas Soome	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
334199767fToomas Soome} blkptr_t;
335199767fToomas Soome
336199767fToomas Soome/*
337199767fToomas Soome * Macros to get and set fields in a bp or DVA.
338199767fToomas Soome */
339199767fToomas Soome#define	DVA_GET_ASIZE(dva)	\
340199767fToomas Soome	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
341199767fToomas Soome#define	DVA_SET_ASIZE(dva, x)	\
342199767fToomas Soome	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
343199767fToomas Soome	SPA_MINBLOCKSHIFT, 0, x)
344199767fToomas Soome
345199767fToomas Soome#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
346199767fToomas Soome#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
347199767fToomas Soome
348199767fToomas Soome#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
349199767fToomas Soome#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
350199767fToomas Soome
351199767fToomas Soome#define	DVA_GET_OFFSET(dva)	\
352199767fToomas Soome	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
353199767fToomas Soome#define	DVA_SET_OFFSET(dva, x)	\
354199767fToomas Soome	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
355199767fToomas Soome
356199767fToomas Soome#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
357199767fToomas Soome#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
358199767fToomas Soome
359199767fToomas Soome#define	BP_GET_LSIZE(bp)	\
360199767fToomas Soome	(BP_IS_EMBEDDED(bp) ?	\
361199767fToomas Soome	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
362199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
363199767fToomas Soome#define	BP_SET_LSIZE(bp, x)	do { \
364199767fToomas Soome	ASSERT(!BP_IS_EMBEDDED(bp)); \
365199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, \
366199767fToomas Soome	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
367199767fToomas Soome_NOTE(CONSTCOND) } while (0)
368199767fToomas Soome
369199767fToomas Soome#define	BP_GET_PSIZE(bp)	\
370199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)
371199767fToomas Soome#define	BP_SET_PSIZE(bp, x)	\
372199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x)
373199767fToomas Soome
374199767fToomas Soome#define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 7)
375199767fToomas Soome#define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 7, x)
376199767fToomas Soome
377199767fToomas Soome#define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
378199767fToomas Soome#define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
379199767fToomas Soome
380199767fToomas Soome#define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
381199767fToomas Soome#define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
382199767fToomas Soome
383199767fToomas Soome#define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
384199767fToomas Soome#define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
385199767fToomas Soome
386199767fToomas Soome#define	BP_IS_EMBEDDED(bp)	BF64_GET((bp)->blk_prop, 39, 1)
387199767fToomas Soome
388199767fToomas Soome#define	BP_GET_DEDUP(bp)	BF64_GET((bp)->blk_prop, 62, 1)
389199767fToomas Soome#define	BP_SET_DEDUP(bp, x)	BF64_SET((bp)->blk_prop, 62, 1, x)
390199767fToomas Soome
391199767fToomas Soome#define	BP_GET_BYTEORDER(bp)	BF64_GET((bp)->blk_prop, 63, 1)
392199767fToomas Soome#define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
393199767fToomas Soome
394199767fToomas Soome#define	BP_PHYSICAL_BIRTH(bp)		\
395199767fToomas Soome	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
396199767fToomas Soome
397199767fToomas Soome#define	BP_GET_ASIZE(bp)	\
398199767fToomas Soome	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
399199767fToomas Soome		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
400199767fToomas Soome
401199767fToomas Soome#define	BP_GET_UCSIZE(bp) \
402199767fToomas Soome	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
403199767fToomas Soome	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
404199767fToomas Soome
405199767fToomas Soome#define	BP_GET_NDVAS(bp)	\
406199767fToomas Soome	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
407199767fToomas Soome	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
408199767fToomas Soome	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
409199767fToomas Soome
410199767fToomas Soome#define	DVA_EQUAL(dva1, dva2)	\
411199767fToomas Soome	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
412199767fToomas Soome	(dva1)->dva_word[0] == (dva2)->dva_word[0])
413199767fToomas Soome
414199767fToomas Soome#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
415199767fToomas Soome	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
416199767fToomas Soome	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
417199767fToomas Soome	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
418199767fToomas Soome	((zc1).zc_word[3] - (zc2).zc_word[3])))
419199767fToomas Soome
420199767fToomas Soome
421199767fToomas Soome#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
422199767fToomas Soome
423199767fToomas Soome#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
424199767fToomas Soome{						\
425199767fToomas Soome	(zcp)->zc_word[0] = w0;			\
426199767fToomas Soome	(zcp)->zc_word[1] = w1;			\
427199767fToomas Soome	(zcp)->zc_word[2] = w2;			\
428199767fToomas Soome	(zcp)->zc_word[3] = w3;			\
429199767fToomas Soome}
430199767fToomas Soome
431199767fToomas Soome#define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
432199767fToomas Soome#define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
433199767fToomas Soome#define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&  \
434199767fToomas Soome	(dva)->dva_word[1] == 0ULL)
435199767fToomas Soome#define	BP_IS_HOLE(bp)		DVA_IS_EMPTY(BP_IDENTITY(bp))
436199767fToomas Soome#define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
437199767fToomas Soome
438199767fToomas Soome#define	BP_ZERO(bp)				\
439199767fToomas Soome{						\
440199767fToomas Soome	(bp)->blk_dva[0].dva_word[0] = 0;	\
441199767fToomas Soome	(bp)->blk_dva[0].dva_word[1] = 0;	\
442199767fToomas Soome	(bp)->blk_dva[1].dva_word[0] = 0;	\
443199767fToomas Soome	(bp)->blk_dva[1].dva_word[1] = 0;	\
444199767fToomas Soome	(bp)->blk_dva[2].dva_word[0] = 0;	\
445199767fToomas Soome	(bp)->blk_dva[2].dva_word[1] = 0;	\
446199767fToomas Soome	(bp)->blk_prop = 0;			\
447199767fToomas Soome	(bp)->blk_pad[0] = 0;			\
448199767fToomas Soome	(bp)->blk_pad[1] = 0;			\
449199767fToomas Soome	(bp)->blk_phys_birth = 0;		\
450199767fToomas Soome	(bp)->blk_birth = 0;			\
451199767fToomas Soome	(bp)->blk_fill = 0;			\
452199767fToomas Soome	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
453199767fToomas Soome}
454199767fToomas Soome
455ece0bc8Toomas Soome#if BYTE_ORDER == _BIG_ENDIAN
456ece0bc8Toomas Soome#define	ZFS_HOST_BYTEORDER	(0ULL)
457ece0bc8Toomas Soome#else
458ece0bc8Toomas Soome#define	ZFS_HOST_BYTEORDER	(1ULL)
459ece0bc8Toomas Soome#endif
460ece0bc8Toomas Soome
461ece0bc8Toomas Soome#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
462199767fToomas Soome#define	BPE_NUM_WORDS 14
463199767fToomas Soome#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
464199767fToomas Soome#define	BPE_IS_PAYLOADWORD(bp, wp) \
465199767fToomas Soome	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
466199767fToomas Soome
467199767fToomas Soome/*
468199767fToomas Soome * Embedded checksum
469199767fToomas Soome */
470199767fToomas Soome#define	ZEC_MAGIC	0x210da7ab10c7a11ULL
471199767fToomas Soome
472199767fToomas Soometypedef struct zio_eck {
473199767fToomas Soome	uint64_t	zec_magic;	/* for validation, endianness	*/
474199767fToomas Soome	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
475199767fToomas Soome} zio_eck_t;
476199767fToomas Soome
477199767fToomas Soome/*
478199767fToomas Soome * Gang block headers are self-checksumming and contain an array
479199767fToomas Soome * of block pointers.
480199767fToomas Soome */
482199767fToomas Soome#define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
483199767fToomas Soome	sizeof (zio_eck_t)) / sizeof (blkptr_t))
484199767fToomas Soome#define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
485199767fToomas Soome	sizeof (zio_eck_t) - \
486199767fToomas Soome	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
487199767fToomas Soome	sizeof (uint64_t))
488199767fToomas Soome
489199767fToomas Soometypedef struct zio_gbh {
490199767fToomas Soome	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
491199767fToomas Soome	uint64_t		zg_filler[SPA_GBH_FILLER];
492199767fToomas Soome	zio_eck_t		zg_tail;
493199767fToomas Soome} zio_gbh_phys_t;
494199767fToomas Soome
495199767fToomas Soome#define	VDEV_RAIDZ_MAXPARITY	3
496199767fToomas Soome
497199767fToomas Soome#define	VDEV_PAD_SIZE		(8 << 10)
498199767fToomas Soome/* 2 padding areas (vl_pad1 and vl_pad2) to skip */
499199767fToomas Soome#define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
500199767fToomas Soome#define	VDEV_PHYS_SIZE		(112 << 10)
501199767fToomas Soome#define	VDEV_UBERBLOCK_RING	(128 << 10)
502199767fToomas Soome
5037dcf02bToomas Soome/*
5047dcf02bToomas Soome * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
5057dcf02bToomas Soome * ring when MMP is enabled.
5067dcf02bToomas Soome */
5077dcf02bToomas Soome#define	MMP_BLOCKS_PER_LABEL	1
5087dcf02bToomas Soome
5097dcf02bToomas Soome/* The largest uberblock we support is 8k. */
5107dcf02bToomas Soome#define	MAX_UBERBLOCK_SHIFT	(13)
511199767fToomas Soome#define	VDEV_UBERBLOCK_SHIFT(vd)	\
5127dcf02bToomas Soome	MIN(MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT), MAX_UBERBLOCK_SHIFT)
513199767fToomas Soome#define	VDEV_UBERBLOCK_COUNT(vd)	\
515199767fToomas Soome#define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
516199767fToomas Soome	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
517199767fToomas Soome#define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
518199767fToomas Soome
519199767fToomas Soometypedef struct vdev_phys {
520199767fToomas Soome	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
521199767fToomas Soome	zio_eck_t	vp_zbt;
522199767fToomas Soome} vdev_phys_t;
523199767fToomas Soome
524199767fToomas Soometypedef struct vdev_label {
525199767fToomas Soome	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K  */
526199767fToomas Soome	char		vl_pad2[VDEV_PAD_SIZE];			/*  8K  */
527199767fToomas Soome	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
528199767fToomas Soome	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
529199767fToomas Soome} vdev_label_t;							/* 256K total */
530199767fToomas Soome
531199767fToomas Soome/*
532199767fToomas Soome * vdev_dirty() flags
533199767fToomas Soome */
534199767fToomas Soome#define	VDD_METASLAB	0x01
535199767fToomas Soome#define	VDD_DTL		0x02
536199767fToomas Soome
537199767fToomas Soome/*
538199767fToomas Soome * Size and offset of embedded boot loader region on each label.
539199767fToomas Soome * The total size of the first two labels plus the boot area is 4MB.
540199767fToomas Soome */
541199767fToomas Soome#define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
542199767fToomas Soome#define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M	*/
543199767fToomas Soome
544199767fToomas Soome/*
545199767fToomas Soome * Size of label regions at the start and end of each leaf device.
546199767fToomas Soome */
547199767fToomas Soome#define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
548199767fToomas Soome#define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
549199767fToomas Soome#define	VDEV_LABELS		4
550199767fToomas Soome
551199767fToomas Soomeenum zio_checksum {
552199767fToomas Soome	ZIO_CHECKSUM_INHERIT = 0,
553199767fToomas Soome	ZIO_CHECKSUM_ON,
554199767fToomas Soome	ZIO_CHECKSUM_OFF,
555199767fToomas Soome	ZIO_CHECKSUM_LABEL,
556199767fToomas Soome	ZIO_CHECKSUM_GANG_HEADER,
557199767fToomas Soome	ZIO_CHECKSUM_ZILOG,
558199767fToomas Soome	ZIO_CHECKSUM_FLETCHER_2,
559199767fToomas Soome	ZIO_CHECKSUM_FLETCHER_4,
560199767fToomas Soome	ZIO_CHECKSUM_SHA256,
561199767fToomas Soome	ZIO_CHECKSUM_ZILOG2,
562199767fToomas Soome	ZIO_CHECKSUM_NOPARITY,
563199767fToomas Soome	ZIO_CHECKSUM_SHA512,
564199767fToomas Soome	ZIO_CHECKSUM_SKEIN,
565199767fToomas Soome	ZIO_CHECKSUM_EDONR,
566199767fToomas Soome	ZIO_CHECKSUM_FUNCTIONS
567199767fToomas Soome};
568199767fToomas Soome
570199767fToomas Soome#define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
571199767fToomas Soome
572199767fToomas Soomeenum zio_compress {
573199767fToomas Soome	ZIO_COMPRESS_INHERIT = 0,
574199767fToomas Soome	ZIO_COMPRESS_ON,
575199767fToomas Soome	ZIO_COMPRESS_OFF,
576199767fToomas Soome	ZIO_COMPRESS_LZJB,
577199767fToomas Soome	ZIO_COMPRESS_EMPTY,
578199767fToomas Soome	ZIO_COMPRESS_GZIP_1,
579199767fToomas Soome	ZIO_COMPRESS_GZIP_2,
580199767fToomas Soome	ZIO_COMPRESS_GZIP_3,
581199767fToomas Soome	ZIO_COMPRESS_GZIP_4,
582199767fToomas Soome	ZIO_COMPRESS_GZIP_5,
583199767fToomas Soome	ZIO_COMPRESS_GZIP_6,
584199767fToomas Soome	ZIO_COMPRESS_GZIP_7,
585199767fToomas Soome	ZIO_COMPRESS_GZIP_8,
586199767fToomas Soome	ZIO_COMPRESS_GZIP_9,
587199767fToomas Soome	ZIO_COMPRESS_ZLE,
588199767fToomas Soome	ZIO_COMPRESS_LZ4,
589199767fToomas Soome	ZIO_COMPRESS_FUNCTIONS
590199767fToomas Soome};
591199767fToomas Soome
594199767fToomas Soome
595199767fToomas Soome/* nvlist pack encoding */
596199767fToomas Soome#define	NV_ENCODE_NATIVE	0
597199767fToomas Soome#define	NV_ENCODE_XDR		1
598199767fToomas Soome
599199767fToomas Soometypedef enum {
600199767fToomas Soome	DATA_TYPE_UNKNOWN = 0,
601199767fToomas Soome	DATA_TYPE_BOOLEAN,
602199767fToomas Soome	DATA_TYPE_BYTE,
603199767fToomas Soome	DATA_TYPE_INT16,
604199767fToomas Soome	DATA_TYPE_UINT16,
605199767fToomas Soome	DATA_TYPE_INT32,
606199767fToomas Soome	DATA_TYPE_UINT32,
607199767fToomas Soome	DATA_TYPE_INT64,
608199767fToomas Soome	DATA_TYPE_UINT64,
609199767fToomas Soome	DATA_TYPE_STRING,
610199767fToomas Soome	DATA_TYPE_BYTE_ARRAY,
611199767fToomas Soome	DATA_TYPE_INT16_ARRAY,
612199767fToomas Soome	DATA_TYPE_UINT16_ARRAY,
613199767fToomas Soome	DATA_TYPE_INT32_ARRAY,
614199767fToomas Soome	DATA_TYPE_UINT32_ARRAY,
615199767fToomas Soome	DATA_TYPE_INT64_ARRAY,
616199767fToomas Soome	DATA_TYPE_UINT64_ARRAY,
617199767fToomas Soome	DATA_TYPE_STRING_ARRAY,
618199767fToomas Soome	DATA_TYPE_HRTIME,
619199767fToomas Soome	DATA_TYPE_NVLIST,
620199767fToomas Soome	DATA_TYPE_NVLIST_ARRAY,
621199767fToomas Soome	DATA_TYPE_BOOLEAN_VALUE,
622199767fToomas Soome	DATA_TYPE_INT8,
623199767fToomas Soome	DATA_TYPE_UINT8,
624199767fToomas Soome	DATA_TYPE_BOOLEAN_ARRAY,
625199767fToomas Soome	DATA_TYPE_INT8_ARRAY,
626199767fToomas Soome	DATA_TYPE_UINT8_ARRAY
627199767fToomas Soome} data_type_t;
628199767fToomas Soome
629199767fToomas Soome/*
630199767fToomas Soome * On-disk version number.
631199767fToomas Soome */
632199767fToomas Soome#define	SPA_VERSION_1			1ULL
633199767fToomas Soome#define	SPA_VERSION_2			2ULL
634199767fToomas Soome#define	SPA_VERSION_3			3ULL
635199767fToomas Soome#define	SPA_VERSION_4			4ULL
636199767fToomas Soome#define	SPA_VERSION_5			5ULL
637199767fToomas Soome#define	SPA_VERSION_6			6ULL
638199767fToomas Soome#define	SPA_VERSION_7			7ULL
639199767fToomas Soome#define	SPA_VERSION_8			8ULL
640199767fToomas Soome#define	SPA_VERSION_9			9ULL
641199767fToomas Soome#define	SPA_VERSION_10			10ULL
642199767fToomas Soome#define	SPA_VERSION_11			11ULL
643199767fToomas Soome#define	SPA_VERSION_12			12ULL
644199767fToomas Soome#define	SPA_VERSION_13			13ULL
645199767fToomas Soome#define	SPA_VERSION_14			14ULL
646199767fToomas Soome#define	SPA_VERSION_15			15ULL
647199767fToomas Soome#define	SPA_VERSION_16			16ULL
648199767fToomas Soome#define	SPA_VERSION_17			17ULL
649199767fToomas Soome#define	SPA_VERSION_18			18ULL
650199767fToomas Soome#define	SPA_VERSION_19			19ULL
651199767fToomas Soome#define	SPA_VERSION_20			20ULL
652199767fToomas Soome#define	SPA_VERSION_21			21ULL
653199767fToomas Soome#define	SPA_VERSION_22			22ULL
654199767fToomas Soome#define	SPA_VERSION_23			23ULL
655199767fToomas Soome#define	SPA_VERSION_24			24ULL
656199767fToomas Soome#define	SPA_VERSION_25			25ULL
657199767fToomas Soome#define	SPA_VERSION_26			26ULL
658199767fToomas Soome#define	SPA_VERSION_27			27ULL
659199767fToomas Soome#define	SPA_VERSION_28			28ULL
660199767fToomas Soome#define	SPA_VERSION_5000		5000ULL
661199767fToomas Soome
662199767fToomas Soome/*
663199767fToomas Soome * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
664199767fToomas Soome * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
665199767fToomas Soome * and do the appropriate changes.  Also bump the version number in
666199767fToomas Soome * usr/src/grub/capability.
667199767fToomas Soome */
668199767fToomas Soome#define	SPA_VERSION			SPA_VERSION_5000
669199767fToomas Soome#define	SPA_VERSION_STRING		"5000"
670199767fToomas Soome
671199767fToomas Soome/*
672199767fToomas Soome * Symbolic names for the changes that caused a SPA_VERSION switch.
673199767fToomas Soome * Used in the code when checking for presence or absence of a feature.
674199767fToomas Soome * Feel free to define multiple symbolic names for each version if there
675199767fToomas Soome * were multiple changes to on-disk structures during that version.
676199767fToomas Soome *
677199767fToomas Soome * NOTE: When checking the current SPA_VERSION in your code, be sure
678199767fToomas Soome *       to use spa_version() since it reports the version of the
679199767fToomas Soome *       last synced uberblock.  Checking the in-flight version can
680199767fToomas Soome *       be dangerous in some cases.
681199767fToomas Soome */
682199767fToomas Soome#define	SPA_VERSION_INITIAL		SPA_VERSION_1
683199767fToomas Soome#define	SPA_VERSION_DITTO_BLOCKS	SPA_VERSION_2
684199767fToomas Soome#define	SPA_VERSION_SPARES		SPA_VERSION_3
685199767fToomas Soome#define	SPA_VERSION_RAID6		SPA_VERSION_3
686199767fToomas Soome#define	SPA_VERSION_BPLIST_ACCOUNT	SPA_VERSION_3
687199767fToomas Soome#define	SPA_VERSION_RAIDZ_DEFLATE	SPA_VERSION_3
688199767fToomas Soome#define	SPA_VERSION_DNODE_BYTES		SPA_VERSION_3
689199767fToomas Soome#define	SPA_VERSION_ZPOOL_HISTORY	SPA_VERSION_4
691199767fToomas Soome#define	SPA_VERSION_BOOTFS		SPA_VERSION_6
692199767fToomas Soome#define	SPA_VERSION_SLOGS		SPA_VERSION_7
694199767fToomas Soome#define	SPA_VERSION_FUID		SPA_VERSION_9
696199767fToomas Soome#define	SPA_VERSION_REFQUOTA		SPA_VERSION_9
698199767fToomas Soome#define	SPA_VERSION_L2CACHE		SPA_VERSION_10
699199767fToomas Soome#define	SPA_VERSION_NEXT_CLONES		SPA_VERSION_11
700199767fToomas Soome#define	SPA_VERSION_ORIGIN		SPA_VERSION_11
701199767fToomas Soome#define	SPA_VERSION_DSL_SCRUB		SPA_VERSION_11
702199767fToomas Soome#define	SPA_VERSION_SNAP_PROPS		SPA_VERSION_12
703199767fToomas Soome#define	SPA_VERSION_USED_BREAKDOWN	SPA_VERSION_13
704199767fToomas Soome#define	SPA_VERSION_PASSTHROUGH_X	SPA_VERSION_14
70508487eeToomas Soome#define	SPA_VERSION_USERSPACE		SPA_VERSION_15
706199767fToomas Soome#define	SPA_VERSION_STMF_PROP		SPA_VERSION_16
707199767fToomas Soome#define	SPA_VERSION_RAIDZ3		SPA_VERSION_17
708199767fToomas Soome#define	SPA_VERSION_USERREFS		SPA_VERSION_18
709199767fToomas Soome#define	SPA_VERSION_HOLES		SPA_VERSION_19
710199767fToomas Soome#define	SPA_VERSION_ZLE_COMPRESSION	SPA_VERSION_20
711199767fToomas Soome#define	SPA_VERSION_DEDUP		SPA_VERSION_21
712199767fToomas Soome#define	SPA_VERSION_RECVD_PROPS		SPA_VERSION_22
713199767fToomas Soome#define	SPA_VERSION_SLIM_ZIL		SPA_VERSION_23
714199767fToomas Soome#define	SPA_VERSION_SA			SPA_VERSION_24
715199767fToomas Soome#define	SPA_VERSION_SCAN		SPA_VERSION_25
716199767fToomas Soome#define	SPA_VERSION_DIR_CLONES		SPA_VERSION_26
717199767fToomas Soome#define	SPA_VERSION_DEADLISTS		SPA_VERSION_26
718199767fToomas Soome#define	SPA_VERSION_FAST_SNAP		SPA_VERSION_27
719199767fToomas Soome#define	SPA_VERSION_MULTI_REPLACE	SPA_VERSION_28
720199767fToomas Soome#define	SPA_VERSION_BEFORE_FEATURES	SPA_VERSION_28
721199767fToomas Soome#define	SPA_VERSION_FEATURES		SPA_VERSION_5000
722199767fToomas Soome
723199767fToomas Soome#define	SPA_VERSION_IS_SUPPORTED(v) \
724199767fToomas Soome	(((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
725199767fToomas Soome	((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
726199767fToomas Soome
727199767fToomas Soome/*
728199767fToomas Soome * The following are configuration names used in the nvlist describing a pool's
729199767fToomas Soome * configuration.
730199767fToomas Soome */
731199767fToomas Soome#define	ZPOOL_CONFIG_VERSION		"version"
732199767fToomas Soome#define	ZPOOL_CONFIG_POOL_NAME		"name"
733199767fToomas Soome#define	ZPOOL_CONFIG_POOL_STATE		"state"
734199767fToomas Soome#define	ZPOOL_CONFIG_POOL_TXG		"txg"
735199767fToomas Soome#define	ZPOOL_CONFIG_POOL_GUID		"pool_guid"
736199767fToomas Soome#define	ZPOOL_CONFIG_CREATE_TXG		"create_txg"
737199767fToomas Soome#define	ZPOOL_CONFIG_TOP_GUID		"top_guid"
738199767fToomas Soome#define	ZPOOL_CONFIG_VDEV_TREE		"vdev_tree"
739199767fToomas Soome#define	ZPOOL_CONFIG_TYPE		"type"
740199767fToomas Soome#define	ZPOOL_CONFIG_CHILDREN		"children"
741199767fToomas Soome#define	ZPOOL_CONFIG_ID			"id"
742199767fToomas Soome#define	ZPOOL_CONFIG_GUID		"guid"
743c023f65Toomas Soome#define	ZPOOL_CONFIG_INDIRECT_OBJECT	"com.delphix:indirect_object"
744c023f65Toomas Soome#define	ZPOOL_CONFIG_INDIRECT_BIRTHS	"com.delphix:indirect_births"
745c023f65Toomas Soome#define	ZPOOL_CONFIG_PREV_INDIRECT_VDEV	"com.delphix:prev_indirect_vdev"
746199767fToomas Soome#define	ZPOOL_CONFIG_PATH		"path"
747199767fToomas Soome#define	ZPOOL_CONFIG_DEVID		"devid"
748199767fToomas Soome#define	ZPOOL_CONFIG_PHYS_PATH		"phys_path"
749199767fToomas Soome#define	ZPOOL_CONFIG_METASLAB_ARRAY	"metaslab_array"
750199767fToomas Soome#define	ZPOOL_CONFIG_METASLAB_SHIFT	"metaslab_shift"
751199767fToomas Soome#define	ZPOOL_CONFIG_ASHIFT		"ashift"
752199767fToomas Soome#define	ZPOOL_CONFIG_ASIZE		"asize"
753199767fToomas Soome#define	ZPOOL_CONFIG_DTL		"DTL"
754199767fToomas Soome#define	ZPOOL_CONFIG_STATS		"stats"
755199767fToomas Soome#define	ZPOOL_CONFIG_WHOLE_DISK		"whole_disk"
756199767fToomas Soome#define	ZPOOL_CONFIG_ERRCOUNT		"error_count"
757199767fToomas Soome#define	ZPOOL_CONFIG_NOT_PRESENT	"not_present"
758199767fToomas Soome#define	ZPOOL_CONFIG_SPARES		"spares"
759199767fToomas Soome#define	ZPOOL_CONFIG_IS_SPARE		"is_spare"
760199767fToomas Soome#define	ZPOOL_CONFIG_NPARITY		"nparity"
761199767fToomas Soome#define	ZPOOL_CONFIG_HOSTID		"hostid"
762199767fToomas Soome#define	ZPOOL_CONFIG_HOSTNAME		"hostname"
763199767fToomas Soome#define	ZPOOL_CONFIG_IS_LOG		"is_log"
764199767fToomas Soome#define	ZPOOL_CONFIG_TIMESTAMP		"timestamp" /* not stored on disk */
765199767fToomas Soome#define	ZPOOL_CONFIG_FEATURES_FOR_READ	"features_for_read"
766da9bf00Toomas Soome#define	ZPOOL_CONFIG_VDEV_CHILDREN	"vdev_children"
767199767fToomas Soome
768199767fToomas Soome/*
769199767fToomas Soome * The persistent vdev state is stored as separate values rather than a single
770199767fToomas Soome * 'vdev_state' entry.  This is because a device can be in multiple states, such
771199767fToomas Soome * as offline and degraded.
772199767fToomas Soome */
77308487eeToomas Soome#define	ZPOOL_CONFIG_OFFLINE		"offline"
77408487eeToomas Soome#define	ZPOOL_CONFIG_FAULTED		"faulted"
77508487eeToomas Soome#define	ZPOOL_CONFIG_DEGRADED		"degraded"
77608487eeToomas Soome#define	ZPOOL_CONFIG_REMOVED		"removed"
777199767fToomas Soome#define	ZPOOL_CONFIG_FRU		"fru"
778199767fToomas Soome#define	ZPOOL_CONFIG_AUX_STATE		"aux_state"
779199767fToomas Soome
780199767fToomas Soome#define	VDEV_TYPE_ROOT			"root"
781199767fToomas Soome#define	VDEV_TYPE_MIRROR		"mirror"
782199767fToomas Soome#define	VDEV_TYPE_REPLACING		"replacing"
783199767fToomas Soome#define	VDEV_TYPE_RAIDZ			"raidz"
784199767fToomas Soome#define	VDEV_TYPE_DISK			"disk"
785199767fToomas Soome#define	VDEV_TYPE_FILE			"file"
786199767fToomas Soome#define	VDEV_TYPE_MISSING		"missing"
787199767fToomas Soome#define	VDEV_TYPE_HOLE			"hole"
788199767fToomas Soome#define	VDEV_TYPE_SPARE			"spare"
789199767fToomas Soome#define	VDEV_TYPE_LOG			"log"
790199767fToomas Soome#define	VDEV_TYPE_L2CACHE		"l2cache"
791c023f65Toomas Soome#define	VDEV_TYPE_INDIRECT		"indirect"
792199767fToomas Soome
793199767fToomas Soome/*
794199767fToomas Soome * This is needed in userland to report the minimum necessary device size.
795199767fToomas Soome */
796199767fToomas Soome#define	SPA_MINDEVSIZE		(64ULL << 20)
797199767fToomas Soome
798199767fToomas Soome/*
799199767fToomas Soome * The location of the pool configuration repository, shared between kernel and
800199767fToomas Soome * userland.
801199767fToomas Soome */
802199767fToomas Soome#define	ZPOOL_CACHE		"/boot/zfs/zpool.cache"
803199767fToomas Soome
804199767fToomas Soome/*
805199767fToomas Soome * vdev states are ordered from least to most healthy.
806199767fToomas Soome * A vdev that's CANT_OPEN or below is considered unusable.
807199767fToomas Soome */
808199767fToomas Soometypedef enum vdev_state {
809199767fToomas Soome	VDEV_STATE_UNKNOWN = 0,	/* Uninitialized vdev			*/
810199767fToomas Soome	VDEV_STATE_CLOSED,	/* Not currently open			*/
811199767fToomas Soome	VDEV_STATE_OFFLINE,	/* Not allowed to open			*/
812199767fToomas Soome	VDEV_STATE_REMOVED,	/* Explicitly removed from system	*/
813199767fToomas Soome	VDEV_STATE_CANT_OPEN,	/* Tried to open, but failed		*/
814199767fToomas Soome	VDEV_STATE_FAULTED,	/* External request to fault device	*/
815199767fToomas Soome	VDEV_STATE_DEGRADED,	/* Replicated vdev with unhealthy kids	*/
816199767fToomas Soome	VDEV_STATE_HEALTHY	/* Presumed good			*/
817199767fToomas Soome} vdev_state_t;
818199767fToomas Soome
819199767fToomas Soome/*
820199767fToomas Soome * vdev aux states.  When a vdev is in the CANT_OPEN state, the aux field
821199767fToomas Soome * of the vdev stats structure uses these constants to distinguish why.
822199767fToomas Soome */
823199767fToomas Soometypedef enum vdev_aux {
824199767fToomas Soome	VDEV_AUX_NONE,		/* no error				*/
825199767fToomas Soome	VDEV_AUX_OPEN_FAILED,	/* ldi_open_*() or vn_open() failed	*/
826199767fToomas Soome	VDEV_AUX_CORRUPT_DATA,	/* bad label or disk contents		*/
827199767fToomas Soome	VDEV_AUX_NO_REPLICAS,	/* insufficient number of replicas	*/
828199767fToomas Soome	VDEV_AUX_BAD_GUID_SUM,	/* vdev guid sum doesn't match		*/
829199767fToomas Soome	VDEV_AUX_TOO_SMALL,	/* vdev size is too small		*/
830199767fToomas Soome	VDEV_AUX_BAD_LABEL,	/* the label is OK but invalid		*/
831199767fToomas Soome	VDEV_AUX_VERSION_NEWER,	/* on-disk version is too new		*/
832199767fToomas Soome	VDEV_AUX_VERSION_OLDER,	/* on-disk version is too old		*/
833199767fToomas Soome	VDEV_AUX_SPARED		/* hot spare used in another pool	*/
834199767fToomas Soome} vdev_aux_t;
835199767fToomas Soome
836199767fToomas Soome/*
837199767fToomas Soome * pool state.  The following states are written to disk as part of the normal
838199767fToomas Soome * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE.  The remaining states are
839199767fToomas Soome * software abstractions used at various levels to communicate pool state.
840199767fToomas Soome */
841199767fToomas Soometypedef enum pool_state {
842199767fToomas Soome	POOL_STATE_ACTIVE = 0,		/* In active use		*/
843199767fToomas Soome	POOL_STATE_EXPORTED,		/* Explicitly exported		*/
844199767fToomas Soome	POOL_STATE_DESTROYED,		/* Explicitly destroyed		*/
845199767fToomas Soome	POOL_STATE_SPARE,		/* Reserved for hot spare use	*/
846199767fToomas Soome	POOL_STATE_UNINITIALIZED,	/* Internal spa_t state		*/
847199767fToomas Soome	POOL_STATE_UNAVAIL,		/* Internal libzfs state	*/
848199767fToomas Soome	POOL_STATE_POTENTIALLY_ACTIVE	/* Internal libzfs state	*/
849199767fToomas Soome} pool_state_t;
850199767fToomas Soome
851199767fToomas Soome/*
852199767fToomas Soome * The uberblock version is incremented whenever an incompatible on-disk
853199767fToomas Soome * format change is made to the SPA, DMU, or ZAP.
854199767fToomas Soome *
855199767fToomas Soome * Note: the first two fields should never be moved.  When a storage pool
856199767fToomas Soome * is opened, the uberblock must be read off the disk before the version
857199767fToomas Soome * can be checked.  If the ub_version field is moved, we may not detect
858199767fToomas Soome * version mismatch.  If the ub_magic field is moved, applications that
859199767fToomas Soome * expect the magic number in the first word won't work.
860199767fToomas Soome */
861199767fToomas Soome#define	UBERBLOCK_MAGIC		0x00bab10c		/* oo-ba-bloc!	*/
862199767fToomas Soome#define	UBERBLOCK_SHIFT		10			/* up to 1K	*/
8637dcf02bToomas Soome#define	MMP_MAGIC		0xa11cea11		/* all-see-all  */
8647dcf02bToomas Soome
8657dcf02bToomas Soome#define	MMP_INTERVAL_VALID_BIT	0x01
8667dcf02bToomas Soome#define	MMP_SEQ_VALID_BIT	0x02
8677dcf02bToomas Soome#define	MMP_FAIL_INT_VALID_BIT	0x04
8687dcf02bToomas Soome
8697dcf02bToomas Soome#define	MMP_VALID(ubp)		(ubp->ub_magic == UBERBLOCK_MAGIC && \
8707dcf02bToomas Soome				    ubp->ub_mmp_magic == MMP_MAGIC)
8717dcf02bToomas Soome#define	MMP_INTERVAL_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
8727dcf02bToomas Soome				    MMP_INTERVAL_VALID_BIT))
8737dcf02bToomas Soome#define	MMP_SEQ_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
8747dcf02bToomas Soome				    MMP_SEQ_VALID_BIT))
8757dcf02bToomas Soome#define	MMP_FAIL_INT_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
8767dcf02bToomas Soome				    MMP_FAIL_INT_VALID_BIT))
8777dcf02bToomas Soome
8787dcf02bToomas Soome#define	MMP_INTERVAL(ubp)	((ubp->ub_mmp_config & 0x00000000FFFFFF00) \
8797dcf02bToomas Soome				    >> 8)
8807dcf02bToomas Soome#define	MMP_SEQ(ubp)		((ubp->ub_mmp_config & 0x0000FFFF00000000) \
8817dcf02bToomas Soome				    >> 32)
8827dcf02bToomas Soome#define	MMP_FAIL_INT(ubp)	((ubp->ub_mmp_config & 0xFFFF000000000000) \
8837dcf02bToomas Soome				    >> 48)
8847dcf02bToomas Soome
8857dcf02bToomas Soometypedef struct uberblock {
886199767fToomas Soome	uint64_t	ub_magic;	/* UBERBLOCK_MAGIC		*/
887199767fToomas Soome	uint64_t	ub_version;	/* SPA_VERSION			*/
888199767fToomas Soome	uint64_t	ub_txg;		/* txg of last sync		*/
889199767fToomas Soome	uint64_t	ub_guid_sum;	/* sum of all vdev guids	*/
890199767fToomas Soome	uint64_t	ub_timestamp;	/* UTC time of last sync	*/
891199767fToomas Soome	blkptr_t	ub_rootbp;	/* MOS objset_phys_t		*/
8927dcf02bToomas Soome	/* highest SPA_VERSION supported by software that wrote this txg */
8937dcf02bToomas Soome	uint64_t	ub_software_version;
8947dcf02bToomas Soome	/* Maybe missing in uberblocks we read, but always written */
8957dcf02bToomas Soome	uint64_t	ub_mmp_magic;
8967dcf02bToomas Soome	/*
8977dcf02bToomas Soome	 * If ub_mmp_delay == 0 and ub_mmp_magic is valid, MMP is off.
8987dcf02bToomas Soome	 * Otherwise, nanosec since last MMP write.
8997dcf02bToomas Soome	 */
9007dcf02bToomas Soome	uint64_t	ub_mmp_delay;
9017dcf02bToomas Soome
9027dcf02bToomas Soome	/*
9037dcf02bToomas Soome	 * The ub_mmp_config contains the multihost write interval, multihost
9047dcf02bToomas Soome	 * fail intervals, sequence number for sub-second granularity, and
9057dcf02bToomas Soome	 * valid bit mask.  This layout is as follows:
9067dcf02bToomas Soome	 *
9077dcf02bToomas Soome	 *   64      56      48      40      32      24      16      8       0
9087dcf02bToomas Soome	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
9097dcf02bToomas Soome	 * 0 | Fail Intervals|      Seq      |   Write Interval (ms) | VALID |
9107dcf02bToomas Soome	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
9117dcf02bToomas Soome	 *
9127dcf02bToomas Soome	 * This allows a write_interval of (2^24/1000)s, over 4.5 hours
9137dcf02bToomas Soome	 *
9147dcf02bToomas Soome	 * VALID Bits:
9157dcf02bToomas Soome	 * - 0x01 - Write Interval (ms)
9167dcf02bToomas Soome	 * - 0x02 - Sequence number exists
9177dcf02bToomas Soome	 * - 0x04 - Fail Intervals
9187dcf02bToomas Soome	 * - 0xf8 - Reserved
9197dcf02bToomas Soome	 */
9207dcf02bToomas Soome	uint64_t	ub_mmp_config;
9217dcf02bToomas Soome
9227dcf02bToomas Soome	/*
9237dcf02bToomas Soome	 * ub_checkpoint_txg indicates two things about the current uberblock:
9247dcf02bToomas Soome	 *
9257dcf02bToomas Soome	 * 1] If it is not zero then this uberblock is a checkpoint. If it is
9267dcf02bToomas Soome	 *    zero, then this uberblock is not a checkpoint.
9277dcf02bToomas Soome	 *
9287dcf02bToomas Soome	 * 2] On checkpointed uberblocks, the value of ub_checkpoint_txg is
9297dcf02bToomas Soome	 *    the ub_txg that the uberblock had at the time we moved it to
9307dcf02bToomas Soome	 *    the MOS config.
9317dcf02bToomas Soome	 *
9327dcf02bToomas Soome	 * The field is set when we checkpoint the uberblock and continues to
9337dcf02bToomas Soome	 * hold that value even after we've rewound (unlike the ub_txg that
9347dcf02bToomas Soome	 * is reset to a higher value).
9357dcf02bToomas Soome	 *
9367dcf02bToomas Soome	 * Besides checks used to determine whether we are reopening the
9377dcf02bToomas Soome	 * pool from a checkpointed uberblock [see spa_ld_select_uberblock()],
9387dcf02bToomas Soome	 * the value of the field is used to determine which ZIL blocks have
9397dcf02bToomas Soome	 * been allocated according to the ms_sm when we are rewinding to a
9407dcf02bToomas Soome	 * checkpoint. Specifically, if blk_birth > ub_checkpoint_txg, then
9417dcf02bToomas Soome	 * the ZIL block is not allocated [see uses of spa_min_claim_txg()].
9427dcf02bToomas Soome	 */
9437dcf02bToomas Soome	uint64_t	ub_checkpoint_txg;
9447dcf02bToomas Soome} uberblock_t;
945199767fToomas Soome
946199767fToomas Soome/*
947199767fToomas Soome * Flags.
948199767fToomas Soome */
949199767fToomas Soome#define	DNODE_MUST_BE_ALLOCATED	1
950199767fToomas Soome#define	DNODE_MUST_BE_FREE	2
951199767fToomas Soome
952199767fToomas Soome/*
953199767fToomas Soome * Fixed constants.
954199767fToomas Soome */
955199767fToomas Soome#define	DNODE_SHIFT		9	/* 512 bytes */
956199767fToomas Soome#define	DN_MIN_INDBLKSHIFT	12	/* 4k */
957c023f65Toomas Soome#define	DN_MAX_INDBLKSHIFT	17	/* 128k */
958199767fToomas Soome#define	DNODE_BLOCK_SHIFT	14	/* 16k */
959199767fToomas Soome#define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
960199767fToomas Soome#define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
961199767fToomas Soome#define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
962199767fToomas Soome
963199767fToomas Soome/*
964199767fToomas Soome * Derived constants.
965199767fToomas Soome */
966f905073Toomas Soome#define	DNODE_MIN_SIZE		(1 << DNODE_SHIFT)
967f905073Toomas Soome#define	DNODE_MAX_SIZE		(1 << DNODE_BLOCK_SHIFT)
968f905073Toomas Soome#define	DNODE_BLOCK_SIZE	(1 << DNODE_BLOCK_SHIFT)
969f905073Toomas Soome#define	DNODE_MIN_SLOTS		(DNODE_MIN_SIZE >> DNODE_SHIFT)
970f905073Toomas Soome#define	DNODE_MAX_SLOTS		(DNODE_MAX_SIZE >> DNODE_SHIFT)
971f905073Toomas Soome#define	DN_BONUS_SIZE(dnsize)	((dnsize) - DNODE_CORE_SIZE - \
972f905073Toomas Soome	(1 << SPA_BLKPTRSHIFT))
973f905073Toomas Soome#define	DN_SLOTS_TO_BONUSLEN(slots)	DN_BONUS_SIZE((slots) << DNODE_SHIFT)
974f905073Toomas Soome#define	DN_OLD_MAX_BONUSLEN		(DN_BONUS_SIZE(DNODE_MIN_SIZE))
975f905073Toomas Soome#define	DN_MAX_NBLKPTR		((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> \
976f905073Toomas Soome	SPA_BLKPTRSHIFT)
977f905073Toomas Soome#define	DN_MAX_OBJECT		(1ULL << DN_MAX_OBJECT_SHIFT)
978f905073Toomas Soome#define	DN_ZERO_BONUSLEN	(DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
979199767fToomas Soome
981199767fToomas Soome#define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
983199767fToomas Soome
984199767fToomas Soome/* The +2 here is a cheesy way to round up */
985199767fToomas Soome#define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
987199767fToomas Soome
988199767fToomas Soome#define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
989199767fToomas Soome	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
990199767fToomas Soome
991199767fToomas Soome#define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
992199767fToomas Soome	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
993199767fToomas Soome
994199767fToomas Soome#define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
995199767fToomas Soome
996199767fToomas Soome/* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
997199767fToomas Soome#define	DNODE_FLAG_USED_BYTES		(1<<0)
998199767fToomas Soome#define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
999199767fToomas Soome
1000199767fToomas Soome/* Does dnode have a SA spill blkptr in bonus? */