zfsimpl.h revision 199767f8919635c4928607450d9e0abb932109ce
1199767fToomas 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
62199767fToomas Soome#define	MAXNAMELEN	256
63199767fToomas Soome
64199767fToomas Soome#define _NOTE(s)
65199767fToomas Soome
66199767fToomas Soometypedef enum { B_FALSE, B_TRUE } boolean_t;
67199767fToomas Soome
68199767fToomas Soome/* CRC64 table */
69199767fToomas Soome#define	ZFS_CRC64_POLY	0xC96C5795D7870F42ULL	/* ECMA-182, reflected form */
70199767fToomas Soome
71199767fToomas Soome/*
72199767fToomas Soome * Macros for various sorts of alignment and rounding when the alignment
73199767fToomas Soome * is known to be a power of 2.
74199767fToomas Soome */
75199767fToomas Soome#define	P2ALIGN(x, align)		((x) & -(align))
76199767fToomas Soome#define	P2PHASE(x, align)		((x) & ((align) - 1))
77199767fToomas Soome#define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
78199767fToomas Soome#define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
79199767fToomas Soome#define	P2END(x, align)			(-(~(x) & -(align)))
80199767fToomas Soome#define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
81199767fToomas Soome#define	P2BOUNDARY(off, len, align)	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
82199767fToomas Soome
83199767fToomas Soome/*
84199767fToomas Soome * General-purpose 32-bit and 64-bit bitfield encodings.
85199767fToomas Soome */
86199767fToomas Soome#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
87199767fToomas Soome#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
88199767fToomas Soome#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
89199767fToomas Soome#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
90199767fToomas Soome
91199767fToomas Soome#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
92199767fToomas Soome#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
93199767fToomas Soome
94199767fToomas Soome#define	BF32_SET(x, low, len, val)	\
95199767fToomas Soome	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
96199767fToomas Soome#define	BF64_SET(x, low, len, val)	\
97199767fToomas Soome	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
98199767fToomas Soome
99199767fToomas Soome#define	BF32_GET_SB(x, low, len, shift, bias)	\
100199767fToomas Soome	((BF32_GET(x, low, len) + (bias)) << (shift))
101199767fToomas Soome#define	BF64_GET_SB(x, low, len, shift, bias)	\
102199767fToomas Soome	((BF64_GET(x, low, len) + (bias)) << (shift))
103199767fToomas Soome
104199767fToomas Soome#define	BF32_SET_SB(x, low, len, shift, bias, val)	\
105199767fToomas Soome	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
106199767fToomas Soome#define	BF64_SET_SB(x, low, len, shift, bias, val)	\
107199767fToomas Soome	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
108199767fToomas Soome
109199767fToomas Soome/*
110199767fToomas Soome * Macros to reverse byte order
111199767fToomas Soome */
112199767fToomas Soome#define	BSWAP_8(x)	((x) & 0xff)
113199767fToomas Soome#define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
114199767fToomas Soome#define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
115199767fToomas Soome#define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
116199767fToomas Soome
117199767fToomas Soome#define	SPA_MINBLOCKSHIFT	9
118199767fToomas Soome#define	SPA_OLDMAXBLOCKSHIFT	17
119199767fToomas Soome#define	SPA_MAXBLOCKSHIFT	24
120199767fToomas Soome#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
122199767fToomas Soome#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
123199767fToomas Soome
124199767fToomas Soome/*
125199767fToomas Soome * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
126199767fToomas Soome * The ASIZE encoding should be at least 64 times larger (6 more bits)
127199767fToomas Soome * to support up to 4-way RAID-Z mirror mode with worst-case gang block
128199767fToomas Soome * overhead, three DVAs per bp, plus one more bit in case we do anything
129199767fToomas Soome * else that expands the ASIZE.
130199767fToomas Soome */
131199767fToomas Soome#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
132199767fToomas Soome#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
133199767fToomas Soome#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
134199767fToomas Soome
135199767fToomas Soome/*
136199767fToomas Soome * All SPA data is represented by 128-bit data virtual addresses (DVAs).
137199767fToomas Soome * The members of the dva_t should be considered opaque outside the SPA.
138199767fToomas Soome */
139199767fToomas Soometypedef struct dva {
140199767fToomas Soome	uint64_t	dva_word[2];
141199767fToomas Soome} dva_t;
142199767fToomas Soome
143199767fToomas Soome/*
144199767fToomas Soome * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
145199767fToomas Soome */
146199767fToomas Soometypedef struct zio_cksum {
147199767fToomas Soome	uint64_t	zc_word[4];
148199767fToomas Soome} zio_cksum_t;
149199767fToomas Soome
150199767fToomas Soome/*
151199767fToomas Soome * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
152199767fToomas Soome * secret and is suitable for use in MAC algorithms as the key.
153199767fToomas Soome */
154199767fToomas Soometypedef struct zio_cksum_salt {
155199767fToomas Soome	uint8_t		zcs_bytes[32];
156199767fToomas Soome} zio_cksum_salt_t;
157199767fToomas Soome
158199767fToomas Soome/*
159199767fToomas Soome * Each block is described by its DVAs, time of birth, checksum, etc.
160199767fToomas Soome * The word-by-word, bit-by-bit layout of the blkptr is as follows:
161199767fToomas Soome *
162199767fToomas Soome *	64	56	48	40	32	24	16	8	0
163199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
164199767fToomas Soome * 0	|		vdev1		| GRID  |	  ASIZE		|
165199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
166199767fToomas Soome * 1	|G|			 offset1				|
167199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
168199767fToomas Soome * 2	|		vdev2		| GRID  |	  ASIZE		|
169199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
170199767fToomas Soome * 3	|G|			 offset2				|
171199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
172199767fToomas Soome * 4	|		vdev3		| GRID  |	  ASIZE		|
173199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
174199767fToomas Soome * 5	|G|			 offset3				|
175199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
176199767fToomas Soome * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
177199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
178199767fToomas Soome * 7	|			padding					|
179199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
180199767fToomas Soome * 8	|			padding					|
181199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
182199767fToomas Soome * 9	|			physical birth txg			|
183199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
184199767fToomas Soome * a	|			logical birth txg			|
185199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
186199767fToomas Soome * b	|			fill count				|
187199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
188199767fToomas Soome * c	|			checksum[0]				|
189199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
190199767fToomas Soome * d	|			checksum[1]				|
191199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
192199767fToomas Soome * e	|			checksum[2]				|
193199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
194199767fToomas Soome * f	|			checksum[3]				|
195199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
196199767fToomas Soome *
197199767fToomas Soome * Legend:
198199767fToomas Soome *
199199767fToomas Soome * vdev		virtual device ID
200199767fToomas Soome * offset	offset into virtual device
201199767fToomas Soome * LSIZE	logical size
202199767fToomas Soome * PSIZE	physical size (after compression)
203199767fToomas Soome * ASIZE	allocated size (including RAID-Z parity and gang block headers)
204199767fToomas Soome * GRID		RAID-Z layout information (reserved for future use)
205199767fToomas Soome * cksum	checksum function
206199767fToomas Soome * comp		compression function
207199767fToomas Soome * G		gang block indicator
208199767fToomas Soome * B		byteorder (endianness)
209199767fToomas Soome * D		dedup
210199767fToomas Soome * X		encryption (on version 30, which is not supported)
211199767fToomas Soome * E		blkptr_t contains embedded data (see below)
212199767fToomas Soome * lvl		level of indirection
213199767fToomas Soome * type		DMU object type
214199767fToomas Soome * phys birth	txg of block allocation; zero if same as logical birth txg
215199767fToomas Soome * log. birth	transaction group in which the block was logically born
216199767fToomas Soome * fill count	number of non-zero blocks under this bp
217199767fToomas Soome * checksum[4]	256-bit checksum of the data this bp describes
218199767fToomas Soome */
219199767fToomas Soome
220199767fToomas Soome/*
221199767fToomas Soome * "Embedded" blkptr_t's don't actually point to a block, instead they
222199767fToomas Soome * have a data payload embedded in the blkptr_t itself.  See the comment
223199767fToomas Soome * in blkptr.c for more details.
224199767fToomas Soome *
225199767fToomas Soome * The blkptr_t is laid out as follows:
226199767fToomas Soome *
227199767fToomas Soome *	64	56	48	40	32	24	16	8	0
228199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
229199767fToomas Soome * 0	|      payload                                                  |
230199767fToomas Soome * 1	|      payload                                                  |
231199767fToomas Soome * 2	|      payload                                                  |
232199767fToomas Soome * 3	|      payload                                                  |
233199767fToomas Soome * 4	|      payload                                                  |
234199767fToomas Soome * 5	|      payload                                                  |
235199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
236199767fToomas Soome * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
237199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
238199767fToomas Soome * 7	|      payload                                                  |
239199767fToomas Soome * 8	|      payload                                                  |
240199767fToomas Soome * 9	|      payload                                                  |
241199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
242199767fToomas Soome * a	|			logical birth txg			|
243199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
244199767fToomas Soome * b	|      payload                                                  |
245199767fToomas Soome * c	|      payload                                                  |
246199767fToomas Soome * d	|      payload                                                  |
247199767fToomas Soome * e	|      payload                                                  |
248199767fToomas Soome * f	|      payload                                                  |
249199767fToomas Soome *	+-------+-------+-------+-------+-------+-------+-------+-------+
250199767fToomas Soome *
251199767fToomas Soome * Legend:
252199767fToomas Soome *
253199767fToomas Soome * payload		contains the embedded data
254199767fToomas Soome * B (byteorder)	byteorder (endianness)
255199767fToomas Soome * D (dedup)		padding (set to zero)
256199767fToomas Soome * X			encryption (set to zero; see above)
257199767fToomas Soome * E (embedded)		set to one
258199767fToomas Soome * lvl			indirection level
259199767fToomas Soome * type			DMU object type
260199767fToomas Soome * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
261199767fToomas Soome * comp			compression function of payload
262199767fToomas Soome * PSIZE		size of payload after compression, in bytes
263199767fToomas Soome * LSIZE		logical size of payload, in bytes
264199767fToomas Soome *			note that 25 bits is enough to store the largest
265199767fToomas Soome *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
266199767fToomas Soome * log. birth		transaction group in which the block was logically born
267199767fToomas Soome *
268199767fToomas Soome * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
269199767fToomas Soome * bp's they are stored in units of SPA_MINBLOCKSHIFT.
270199767fToomas Soome * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
271199767fToomas Soome * The B, D, X, lvl, type, and comp fields are stored the same as with normal
272199767fToomas Soome * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
273199767fToomas Soome * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
274199767fToomas Soome * other macros, as they assert that they are only used on BP's of the correct
275199767fToomas Soome * "embedded-ness".
276199767fToomas Soome */
277199767fToomas Soome
278199767fToomas Soome#define	BPE_GET_ETYPE(bp)	\
279199767fToomas Soome	(ASSERT(BP_IS_EMBEDDED(bp)), \
280199767fToomas Soome	BF64_GET((bp)->blk_prop, 40, 8))
281199767fToomas Soome#define	BPE_SET_ETYPE(bp, t)	do { \
282199767fToomas Soome	ASSERT(BP_IS_EMBEDDED(bp)); \
283199767fToomas Soome	BF64_SET((bp)->blk_prop, 40, 8, t); \
284199767fToomas Soome_NOTE(CONSTCOND) } while (0)
285199767fToomas Soome
286199767fToomas Soome#define	BPE_GET_LSIZE(bp)	\
287199767fToomas Soome	(ASSERT(BP_IS_EMBEDDED(bp)), \
288199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
289199767fToomas Soome#define	BPE_SET_LSIZE(bp, x)	do { \
290199767fToomas Soome	ASSERT(BP_IS_EMBEDDED(bp)); \
291199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
292199767fToomas Soome_NOTE(CONSTCOND) } while (0)
293199767fToomas Soome
294199767fToomas Soome#define	BPE_GET_PSIZE(bp)	\
295199767fToomas Soome	(ASSERT(BP_IS_EMBEDDED(bp)), \
296199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
297199767fToomas Soome#define	BPE_SET_PSIZE(bp, x)	do { \
298199767fToomas Soome	ASSERT(BP_IS_EMBEDDED(bp)); \
299199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
300199767fToomas Soome_NOTE(CONSTCOND) } while (0)
301199767fToomas Soome
302199767fToomas Soometypedef enum bp_embedded_type {
303199767fToomas Soome	BP_EMBEDDED_TYPE_DATA,
304199767fToomas Soome	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
306199767fToomas Soome} bp_embedded_type_t;
307199767fToomas Soome
308199767fToomas Soome#define	BPE_NUM_WORDS 14
309199767fToomas Soome#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
310199767fToomas Soome#define	BPE_IS_PAYLOADWORD(bp, wp) \
311199767fToomas Soome	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
312199767fToomas Soome
313199767fToomas Soome#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
314199767fToomas Soome#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
315199767fToomas Soome
316199767fToomas Soometypedef struct blkptr {
317199767fToomas Soome	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
318199767fToomas Soome	uint64_t	blk_prop;	/* size, compression, type, etc	    */
319199767fToomas Soome	uint64_t	blk_pad[2];	/* Extra space for the future	    */
320199767fToomas Soome	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
321199767fToomas Soome	uint64_t	blk_birth;	/* transaction group at birth	    */
322199767fToomas Soome	uint64_t	blk_fill;	/* fill count			    */
323199767fToomas Soome	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
324199767fToomas Soome} blkptr_t;
325199767fToomas Soome
326199767fToomas Soome/*
327199767fToomas Soome * Macros to get and set fields in a bp or DVA.
328199767fToomas Soome */
329199767fToomas Soome#define	DVA_GET_ASIZE(dva)	\
330199767fToomas Soome	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
331199767fToomas Soome#define	DVA_SET_ASIZE(dva, x)	\
332199767fToomas Soome	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
333199767fToomas Soome	SPA_MINBLOCKSHIFT, 0, x)
334199767fToomas Soome
335199767fToomas Soome#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
336199767fToomas Soome#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
337199767fToomas Soome
338199767fToomas Soome#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
339199767fToomas Soome#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
340199767fToomas Soome
341199767fToomas Soome#define	DVA_GET_OFFSET(dva)	\
342199767fToomas Soome	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
343199767fToomas Soome#define	DVA_SET_OFFSET(dva, x)	\
344199767fToomas Soome	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
345199767fToomas Soome
346199767fToomas Soome#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
347199767fToomas Soome#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
348199767fToomas Soome
349199767fToomas Soome#define	BP_GET_LSIZE(bp)	\
350199767fToomas Soome	(BP_IS_EMBEDDED(bp) ?	\
351199767fToomas Soome	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
352199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
353199767fToomas Soome#define	BP_SET_LSIZE(bp, x)	do { \
354199767fToomas Soome	ASSERT(!BP_IS_EMBEDDED(bp)); \
355199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, \
356199767fToomas Soome	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
357199767fToomas Soome_NOTE(CONSTCOND) } while (0)
358199767fToomas Soome
359199767fToomas Soome#define	BP_GET_PSIZE(bp)	\
360199767fToomas Soome	BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)
361199767fToomas Soome#define	BP_SET_PSIZE(bp, x)	\
362199767fToomas Soome	BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x)
363199767fToomas Soome
364199767fToomas Soome#define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 7)
365199767fToomas Soome#define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 7, x)
366199767fToomas Soome
367199767fToomas Soome#define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
368199767fToomas Soome#define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
369199767fToomas Soome
370199767fToomas Soome#define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
371199767fToomas Soome#define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
372199767fToomas Soome
373199767fToomas Soome#define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
374199767fToomas Soome#define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
375199767fToomas Soome
376199767fToomas Soome#define	BP_IS_EMBEDDED(bp)	BF64_GET((bp)->blk_prop, 39, 1)
377199767fToomas Soome
378199767fToomas Soome#define	BP_GET_DEDUP(bp)	BF64_GET((bp)->blk_prop, 62, 1)
379199767fToomas Soome#define	BP_SET_DEDUP(bp, x)	BF64_SET((bp)->blk_prop, 62, 1, x)
380199767fToomas Soome
381199767fToomas Soome#define	BP_GET_BYTEORDER(bp)	BF64_GET((bp)->blk_prop, 63, 1)
382199767fToomas Soome#define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
383199767fToomas Soome
384199767fToomas Soome#define	BP_PHYSICAL_BIRTH(bp)		\
385199767fToomas Soome	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
386199767fToomas Soome
387199767fToomas Soome#define	BP_GET_ASIZE(bp)	\
388199767fToomas Soome	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
389199767fToomas Soome		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
390199767fToomas Soome
391199767fToomas Soome#define	BP_GET_UCSIZE(bp) \
392199767fToomas Soome	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
393199767fToomas Soome	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
394199767fToomas Soome
395199767fToomas Soome#define	BP_GET_NDVAS(bp)	\
396199767fToomas Soome	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
397199767fToomas Soome	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
398199767fToomas Soome	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
399199767fToomas Soome
400199767fToomas Soome#define	DVA_EQUAL(dva1, dva2)	\
401199767fToomas Soome	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
402199767fToomas Soome	(dva1)->dva_word[0] == (dva2)->dva_word[0])
403199767fToomas Soome
404199767fToomas Soome#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
405199767fToomas Soome	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
406199767fToomas Soome	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
407199767fToomas Soome	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
408199767fToomas Soome	((zc1).zc_word[3] - (zc2).zc_word[3])))
409199767fToomas Soome
410199767fToomas Soome
411199767fToomas Soome#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
412199767fToomas Soome
413199767fToomas Soome#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
414199767fToomas Soome{						\
415199767fToomas Soome	(zcp)->zc_word[0] = w0;			\
416199767fToomas Soome	(zcp)->zc_word[1] = w1;			\
417199767fToomas Soome	(zcp)->zc_word[2] = w2;			\
418199767fToomas Soome	(zcp)->zc_word[3] = w3;			\
419199767fToomas Soome}
420199767fToomas Soome
421199767fToomas Soome#define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
422199767fToomas Soome#define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
423199767fToomas Soome#define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&  \
424199767fToomas Soome	(dva)->dva_word[1] == 0ULL)
425199767fToomas Soome#define	BP_IS_HOLE(bp)		DVA_IS_EMPTY(BP_IDENTITY(bp))
426199767fToomas Soome#define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
427199767fToomas Soome
428199767fToomas Soome#define	BP_ZERO(bp)				\
429199767fToomas Soome{						\
430199767fToomas Soome	(bp)->blk_dva[0].dva_word[0] = 0;	\
431199767fToomas Soome	(bp)->blk_dva[0].dva_word[1] = 0;	\
432199767fToomas Soome	(bp)->blk_dva[1].dva_word[0] = 0;	\
433199767fToomas Soome	(bp)->blk_dva[1].dva_word[1] = 0;	\
434199767fToomas Soome	(bp)->blk_dva[2].dva_word[0] = 0;	\
435199767fToomas Soome	(bp)->blk_dva[2].dva_word[1] = 0;	\
436199767fToomas Soome	(bp)->blk_prop = 0;			\
437199767fToomas Soome	(bp)->blk_pad[0] = 0;			\
438199767fToomas Soome	(bp)->blk_pad[1] = 0;			\
439199767fToomas Soome	(bp)->blk_phys_birth = 0;		\
440199767fToomas Soome	(bp)->blk_birth = 0;			\
441199767fToomas Soome	(bp)->blk_fill = 0;			\
442199767fToomas Soome	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
443199767fToomas Soome}
444199767fToomas Soome
445199767fToomas Soome#define	BPE_NUM_WORDS 14
446199767fToomas Soome#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
447199767fToomas Soome#define	BPE_IS_PAYLOADWORD(bp, wp) \
448199767fToomas Soome	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
449199767fToomas Soome
450199767fToomas Soome/*
451199767fToomas Soome * Embedded checksum
452199767fToomas Soome */
453199767fToomas Soome#define	ZEC_MAGIC	0x210da7ab10c7a11ULL
454199767fToomas Soome
455199767fToomas Soometypedef struct zio_eck {
456199767fToomas Soome	uint64_t	zec_magic;	/* for validation, endianness	*/
457199767fToomas Soome	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
458199767fToomas Soome} zio_eck_t;
459199767fToomas Soome
460199767fToomas Soome/*
461199767fToomas Soome * Gang block headers are self-checksumming and contain an array
462199767fToomas Soome * of block pointers.
463199767fToomas Soome */
465199767fToomas Soome#define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
466199767fToomas Soome	sizeof (zio_eck_t)) / sizeof (blkptr_t))
467199767fToomas Soome#define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
468199767fToomas Soome	sizeof (zio_eck_t) - \
469199767fToomas Soome	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
470199767fToomas Soome	sizeof (uint64_t))
471199767fToomas Soome
472199767fToomas Soometypedef struct zio_gbh {
473199767fToomas Soome	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
474199767fToomas Soome	uint64_t		zg_filler[SPA_GBH_FILLER];
475199767fToomas Soome	zio_eck_t		zg_tail;
476199767fToomas Soome} zio_gbh_phys_t;
477199767fToomas Soome
478199767fToomas Soome#define	VDEV_RAIDZ_MAXPARITY	3
479199767fToomas Soome
480199767fToomas Soome#define	VDEV_PAD_SIZE		(8 << 10)
481199767fToomas Soome/* 2 padding areas (vl_pad1 and vl_pad2) to skip */
482199767fToomas Soome#define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
483199767fToomas Soome#define	VDEV_PHYS_SIZE		(112 << 10)
484199767fToomas Soome#define	VDEV_UBERBLOCK_RING	(128 << 10)
485199767fToomas Soome
486199767fToomas Soome#define	VDEV_UBERBLOCK_SHIFT(vd)	\
487199767fToomas Soome	MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT)
488199767fToomas Soome#define	VDEV_UBERBLOCK_COUNT(vd)	\
490199767fToomas Soome#define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
491199767fToomas Soome	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
492199767fToomas Soome#define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
493199767fToomas Soome
494199767fToomas Soometypedef struct vdev_phys {
495199767fToomas Soome	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
496199767fToomas Soome	zio_eck_t	vp_zbt;
497199767fToomas Soome} vdev_phys_t;
498199767fToomas Soome
499199767fToomas Soometypedef struct vdev_label {
500199767fToomas Soome	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K  */
501199767fToomas Soome	char		vl_pad2[VDEV_PAD_SIZE];			/*  8K  */
502199767fToomas Soome	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
503199767fToomas Soome	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
504199767fToomas Soome} vdev_label_t;							/* 256K total */
505199767fToomas Soome
506199767fToomas Soome/*
507199767fToomas Soome * vdev_dirty() flags
508199767fToomas Soome */
509199767fToomas Soome#define	VDD_METASLAB	0x01
510199767fToomas Soome#define	VDD_DTL		0x02
511199767fToomas Soome
512199767fToomas Soome/*
513199767fToomas Soome * Size and offset of embedded boot loader region on each label.
514199767fToomas Soome * The total size of the first two labels plus the boot area is 4MB.
515199767fToomas Soome */
516199767fToomas Soome#define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
517199767fToomas Soome#define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M	*/
518199767fToomas Soome
519199767fToomas Soome/*
520199767fToomas Soome * Size of label regions at the start and end of each leaf device.
521199767fToomas Soome */
522199767fToomas Soome#define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
523199767fToomas Soome#define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
524199767fToomas Soome#define	VDEV_LABELS		4
525199767fToomas Soome
526199767fToomas Soomeenum zio_checksum {
527199767fToomas Soome	ZIO_CHECKSUM_INHERIT = 0,
528199767fToomas Soome	ZIO_CHECKSUM_ON,
529199767fToomas Soome	ZIO_CHECKSUM_OFF,
530199767fToomas Soome	ZIO_CHECKSUM_LABEL,
531199767fToomas Soome	ZIO_CHECKSUM_GANG_HEADER,
532199767fToomas Soome	ZIO_CHECKSUM_ZILOG,
533199767fToomas Soome	ZIO_CHECKSUM_FLETCHER_2,
534199767fToomas Soome	ZIO_CHECKSUM_FLETCHER_4,
535199767fToomas Soome	ZIO_CHECKSUM_SHA256,
536199767fToomas Soome	ZIO_CHECKSUM_ZILOG2,
537199767fToomas Soome	ZIO_CHECKSUM_NOPARITY,
538199767fToomas Soome	ZIO_CHECKSUM_SHA512,
539199767fToomas Soome	ZIO_CHECKSUM_SKEIN,
540199767fToomas Soome	ZIO_CHECKSUM_EDONR,
541199767fToomas Soome	ZIO_CHECKSUM_FUNCTIONS
542199767fToomas Soome};
543199767fToomas Soome
545199767fToomas Soome#define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
546199767fToomas Soome
547199767fToomas Soomeenum zio_compress {
548199767fToomas Soome	ZIO_COMPRESS_INHERIT = 0,
549199767fToomas Soome	ZIO_COMPRESS_ON,
550199767fToomas Soome	ZIO_COMPRESS_OFF,
551199767fToomas Soome	ZIO_COMPRESS_LZJB,
552199767fToomas Soome	ZIO_COMPRESS_EMPTY,
553199767fToomas Soome	ZIO_COMPRESS_GZIP_1,
554199767fToomas Soome	ZIO_COMPRESS_GZIP_2,
555199767fToomas Soome	ZIO_COMPRESS_GZIP_3,
556199767fToomas Soome	ZIO_COMPRESS_GZIP_4,
557199767fToomas Soome	ZIO_COMPRESS_GZIP_5,
558199767fToomas Soome	ZIO_COMPRESS_GZIP_6,
559199767fToomas Soome	ZIO_COMPRESS_GZIP_7,
560199767fToomas Soome	ZIO_COMPRESS_GZIP_8,
561199767fToomas Soome	ZIO_COMPRESS_GZIP_9,
562199767fToomas Soome	ZIO_COMPRESS_ZLE,
563199767fToomas Soome	ZIO_COMPRESS_LZ4,
564199767fToomas Soome	ZIO_COMPRESS_FUNCTIONS
565199767fToomas Soome};
566199767fToomas Soome
569199767fToomas Soome
570199767fToomas Soome/* nvlist pack encoding */
571199767fToomas Soome#define	NV_ENCODE_NATIVE	0
572199767fToomas Soome#define	NV_ENCODE_XDR		1
573199767fToomas Soome
574199767fToomas Soometypedef enum {
575199767fToomas Soome	DATA_TYPE_UNKNOWN = 0,
576199767fToomas Soome	DATA_TYPE_BOOLEAN,
577199767fToomas Soome	DATA_TYPE_BYTE,
578199767fToomas Soome	DATA_TYPE_INT16,
579199767fToomas Soome	DATA_TYPE_UINT16,
580199767fToomas Soome	DATA_TYPE_INT32,
581199767fToomas Soome	DATA_TYPE_UINT32,
582199767fToomas Soome	DATA_TYPE_INT64,
583199767fToomas Soome	DATA_TYPE_UINT64,
584199767fToomas Soome	DATA_TYPE_STRING,
585199767fToomas Soome	DATA_TYPE_BYTE_ARRAY,
586199767fToomas Soome	DATA_TYPE_INT16_ARRAY,
587199767fToomas Soome	DATA_TYPE_UINT16_ARRAY,
588199767fToomas Soome	DATA_TYPE_INT32_ARRAY,
589199767fToomas Soome	DATA_TYPE_UINT32_ARRAY,
590199767fToomas Soome	DATA_TYPE_INT64_ARRAY,
591199767fToomas Soome	DATA_TYPE_UINT64_ARRAY,
592199767fToomas Soome	DATA_TYPE_STRING_ARRAY,
593199767fToomas Soome	DATA_TYPE_HRTIME,
594199767fToomas Soome	DATA_TYPE_NVLIST,
595199767fToomas Soome	DATA_TYPE_NVLIST_ARRAY,
596199767fToomas Soome	DATA_TYPE_BOOLEAN_VALUE,
597199767fToomas Soome	DATA_TYPE_INT8,
598199767fToomas Soome	DATA_TYPE_UINT8,
599199767fToomas Soome	DATA_TYPE_BOOLEAN_ARRAY,
600199767fToomas Soome	DATA_TYPE_INT8_ARRAY,
601199767fToomas Soome	DATA_TYPE_UINT8_ARRAY
602199767fToomas Soome} data_type_t;
603199767fToomas Soome
604199767fToomas Soome/*
605199767fToomas Soome * On-disk version number.
606199767fToomas Soome */
607199767fToomas Soome#define	SPA_VERSION_1			1ULL
608199767fToomas Soome#define	SPA_VERSION_2			2ULL
609199767fToomas Soome#define	SPA_VERSION_3			3ULL
610199767fToomas Soome#define	SPA_VERSION_4			4ULL
611199767fToomas Soome#define	SPA_VERSION_5			5ULL
612199767fToomas Soome#define	SPA_VERSION_6			6ULL
613199767fToomas Soome#define	SPA_VERSION_7			7ULL
614199767fToomas Soome#define	SPA_VERSION_8			8ULL
615199767fToomas Soome#define	SPA_VERSION_9			9ULL
616199767fToomas Soome#define	SPA_VERSION_10			10ULL
617199767fToomas Soome#define	SPA_VERSION_11			11ULL
618199767fToomas Soome#define	SPA_VERSION_12			12ULL
619199767fToomas Soome#define	SPA_VERSION_13			13ULL
620199767fToomas Soome#define	SPA_VERSION_14			14ULL
621199767fToomas Soome#define	SPA_VERSION_15			15ULL
622199767fToomas Soome#define	SPA_VERSION_16			16ULL
623199767fToomas Soome#define	SPA_VERSION_17			17ULL
624199767fToomas Soome#define	SPA_VERSION_18			18ULL
625199767fToomas Soome#define	SPA_VERSION_19			19ULL
626199767fToomas Soome#define	SPA_VERSION_20			20ULL
627199767fToomas Soome#define	SPA_VERSION_21			21ULL
628199767fToomas Soome#define	SPA_VERSION_22			22ULL
629199767fToomas Soome#define	SPA_VERSION_23			23ULL
630199767fToomas Soome#define	SPA_VERSION_24			24ULL
631199767fToomas Soome#define	SPA_VERSION_25			25ULL
632199767fToomas Soome#define	SPA_VERSION_26			26ULL
633199767fToomas Soome#define	SPA_VERSION_27			27ULL
634199767fToomas Soome#define	SPA_VERSION_28			28ULL
635199767fToomas Soome#define	SPA_VERSION_5000		5000ULL
636199767fToomas Soome
637199767fToomas Soome/*
638199767fToomas Soome * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
639199767fToomas Soome * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
640199767fToomas Soome * and do the appropriate changes.  Also bump the version number in
641199767fToomas Soome * usr/src/grub/capability.
642199767fToomas Soome */
643199767fToomas Soome#define	SPA_VERSION			SPA_VERSION_5000
644199767fToomas Soome#define	SPA_VERSION_STRING		"5000"
645199767fToomas Soome
646199767fToomas Soome/*
647199767fToomas Soome * Symbolic names for the changes that caused a SPA_VERSION switch.
648199767fToomas Soome * Used in the code when checking for presence or absence of a feature.
649199767fToomas Soome * Feel free to define multiple symbolic names for each version if there
650199767fToomas Soome * were multiple changes to on-disk structures during that version.
651199767fToomas Soome *
652199767fToomas Soome * NOTE: When checking the current SPA_VERSION in your code, be sure
653199767fToomas Soome *       to use spa_version() since it reports the version of the
654199767fToomas Soome *       last synced uberblock.  Checking the in-flight version can
655199767fToomas Soome *       be dangerous in some cases.
656199767fToomas Soome */
657199767fToomas Soome#define	SPA_VERSION_INITIAL		SPA_VERSION_1
658199767fToomas Soome#define	SPA_VERSION_DITTO_BLOCKS	SPA_VERSION_2
659199767fToomas Soome#define	SPA_VERSION_SPARES		SPA_VERSION_3
660199767fToomas Soome#define	SPA_VERSION_RAID6		SPA_VERSION_3
661199767fToomas Soome#define	SPA_VERSION_BPLIST_ACCOUNT	SPA_VERSION_3
662199767fToomas Soome#define	SPA_VERSION_RAIDZ_DEFLATE	SPA_VERSION_3
663199767fToomas Soome#define	SPA_VERSION_DNODE_BYTES		SPA_VERSION_3
664199767fToomas Soome#define	SPA_VERSION_ZPOOL_HISTORY	SPA_VERSION_4
666199767fToomas Soome#define	SPA_VERSION_BOOTFS		SPA_VERSION_6
667199767fToomas Soome#define	SPA_VERSION_SLOGS		SPA_VERSION_7
669199767fToomas Soome#define	SPA_VERSION_FUID		SPA_VERSION_9
671199767fToomas Soome#define	SPA_VERSION_REFQUOTA		SPA_VERSION_9
673199767fToomas Soome#define	SPA_VERSION_L2CACHE		SPA_VERSION_10
674199767fToomas Soome#define	SPA_VERSION_NEXT_CLONES		SPA_VERSION_11
675199767fToomas Soome#define	SPA_VERSION_ORIGIN		SPA_VERSION_11
676199767fToomas Soome#define	SPA_VERSION_DSL_SCRUB		SPA_VERSION_11
677199767fToomas Soome#define	SPA_VERSION_SNAP_PROPS		SPA_VERSION_12
678199767fToomas Soome#define	SPA_VERSION_USED_BREAKDOWN	SPA_VERSION_13
679199767fToomas Soome#define	SPA_VERSION_PASSTHROUGH_X	SPA_VERSION_14
680199767fToomas Soome#define SPA_VERSION_USERSPACE		SPA_VERSION_15
681199767fToomas Soome#define	SPA_VERSION_STMF_PROP		SPA_VERSION_16
682199767fToomas Soome#define	SPA_VERSION_RAIDZ3		SPA_VERSION_17
683199767fToomas Soome#define	SPA_VERSION_USERREFS		SPA_VERSION_18
684199767fToomas Soome#define	SPA_VERSION_HOLES		SPA_VERSION_19
685199767fToomas Soome#define	SPA_VERSION_ZLE_COMPRESSION	SPA_VERSION_20
686199767fToomas Soome#define	SPA_VERSION_DEDUP		SPA_VERSION_21
687199767fToomas Soome#define	SPA_VERSION_RECVD_PROPS		SPA_VERSION_22
688199767fToomas Soome#define	SPA_VERSION_SLIM_ZIL		SPA_VERSION_23
689199767fToomas Soome#define	SPA_VERSION_SA			SPA_VERSION_24
690199767fToomas Soome#define	SPA_VERSION_SCAN		SPA_VERSION_25
691199767fToomas Soome#define	SPA_VERSION_DIR_CLONES		SPA_VERSION_26
692199767fToomas Soome#define	SPA_VERSION_DEADLISTS		SPA_VERSION_26
693199767fToomas Soome#define	SPA_VERSION_FAST_SNAP		SPA_VERSION_27
694199767fToomas Soome#define	SPA_VERSION_MULTI_REPLACE	SPA_VERSION_28
695199767fToomas Soome#define	SPA_VERSION_BEFORE_FEATURES	SPA_VERSION_28
696199767fToomas Soome#define	SPA_VERSION_FEATURES		SPA_VERSION_5000
697199767fToomas Soome
698199767fToomas Soome#define	SPA_VERSION_IS_SUPPORTED(v) \
699199767fToomas Soome	(((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
700199767fToomas Soome	((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
701199767fToomas Soome
702199767fToomas Soome/*
703199767fToomas Soome * The following are configuration names used in the nvlist describing a pool's
704199767fToomas Soome * configuration.
705199767fToomas Soome */
706199767fToomas Soome#define	ZPOOL_CONFIG_VERSION		"version"
707199767fToomas Soome#define	ZPOOL_CONFIG_POOL_NAME		"name"
708199767fToomas Soome#define	ZPOOL_CONFIG_POOL_STATE		"state"
709199767fToomas Soome#define	ZPOOL_CONFIG_POOL_TXG		"txg"
710199767fToomas Soome#define	ZPOOL_CONFIG_POOL_GUID		"pool_guid"
711199767fToomas Soome#define	ZPOOL_CONFIG_CREATE_TXG		"create_txg"
712199767fToomas Soome#define	ZPOOL_CONFIG_TOP_GUID		"top_guid"
713199767fToomas Soome#define	ZPOOL_CONFIG_VDEV_TREE		"vdev_tree"
714199767fToomas Soome#define	ZPOOL_CONFIG_TYPE		"type"
715199767fToomas Soome#define	ZPOOL_CONFIG_CHILDREN		"children"
716199767fToomas Soome#define	ZPOOL_CONFIG_ID			"id"
717199767fToomas Soome#define	ZPOOL_CONFIG_GUID		"guid"
718199767fToomas Soome#define	ZPOOL_CONFIG_PATH		"path"
719199767fToomas Soome#define	ZPOOL_CONFIG_DEVID		"devid"
720199767fToomas Soome#define	ZPOOL_CONFIG_PHYS_PATH		"phys_path"
721199767fToomas Soome#define	ZPOOL_CONFIG_METASLAB_ARRAY	"metaslab_array"
722199767fToomas Soome#define	ZPOOL_CONFIG_METASLAB_SHIFT	"metaslab_shift"
723199767fToomas Soome#define	ZPOOL_CONFIG_ASHIFT		"ashift"
724199767fToomas Soome#define	ZPOOL_CONFIG_ASIZE		"asize"
725199767fToomas Soome#define	ZPOOL_CONFIG_DTL		"DTL"
726199767fToomas Soome#define	ZPOOL_CONFIG_STATS		"stats"
727199767fToomas Soome#define	ZPOOL_CONFIG_WHOLE_DISK		"whole_disk"
728199767fToomas Soome#define	ZPOOL_CONFIG_ERRCOUNT		"error_count"
729199767fToomas Soome#define	ZPOOL_CONFIG_NOT_PRESENT	"not_present"
730199767fToomas Soome#define	ZPOOL_CONFIG_SPARES		"spares"
731199767fToomas Soome#define	ZPOOL_CONFIG_IS_SPARE		"is_spare"
732199767fToomas Soome#define	ZPOOL_CONFIG_NPARITY		"nparity"
733199767fToomas Soome#define	ZPOOL_CONFIG_HOSTID		"hostid"
734199767fToomas Soome#define	ZPOOL_CONFIG_HOSTNAME		"hostname"
735199767fToomas Soome#define	ZPOOL_CONFIG_IS_LOG		"is_log"
736199767fToomas Soome#define	ZPOOL_CONFIG_TIMESTAMP		"timestamp" /* not stored on disk */
737199767fToomas Soome#define	ZPOOL_CONFIG_FEATURES_FOR_READ	"features_for_read"
738199767fToomas Soome
739199767fToomas Soome/*
740199767fToomas Soome * The persistent vdev state is stored as separate values rather than a single
741199767fToomas Soome * 'vdev_state' entry.  This is because a device can be in multiple states, such
742199767fToomas Soome * as offline and degraded.
743199767fToomas Soome */
744199767fToomas Soome#define	ZPOOL_CONFIG_OFFLINE            "offline"
745199767fToomas Soome#define	ZPOOL_CONFIG_FAULTED            "faulted"
746199767fToomas Soome#define	ZPOOL_CONFIG_DEGRADED           "degraded"
747199767fToomas Soome#define	ZPOOL_CONFIG_REMOVED            "removed"
748199767fToomas Soome#define	ZPOOL_CONFIG_FRU		"fru"
749199767fToomas Soome#define	ZPOOL_CONFIG_AUX_STATE		"aux_state"
750199767fToomas Soome
751199767fToomas Soome#define	VDEV_TYPE_ROOT			"root"
752199767fToomas Soome#define	VDEV_TYPE_MIRROR		"mirror"
753199767fToomas Soome#define	VDEV_TYPE_REPLACING		"replacing"
754199767fToomas Soome#define	VDEV_TYPE_RAIDZ			"raidz"
755199767fToomas Soome#define	VDEV_TYPE_DISK			"disk"
756199767fToomas Soome#define	VDEV_TYPE_FILE			"file"
757199767fToomas Soome#define	VDEV_TYPE_MISSING		"missing"
758199767fToomas Soome#define	VDEV_TYPE_HOLE			"hole"
759199767fToomas Soome#define	VDEV_TYPE_SPARE			"spare"
760199767fToomas Soome#define	VDEV_TYPE_LOG			"log"
761199767fToomas Soome#define	VDEV_TYPE_L2CACHE		"l2cache"
762199767fToomas Soome
763199767fToomas Soome/*
764199767fToomas Soome * This is needed in userland to report the minimum necessary device size.
765199767fToomas Soome */
766199767fToomas Soome#define	SPA_MINDEVSIZE		(64ULL << 20)
767199767fToomas Soome
768199767fToomas Soome/*
769199767fToomas Soome * The location of the pool configuration repository, shared between kernel and
770199767fToomas Soome * userland.
771199767fToomas Soome */
772199767fToomas Soome#define	ZPOOL_CACHE		"/boot/zfs/zpool.cache"
773199767fToomas Soome
774199767fToomas Soome/*
775199767fToomas Soome * vdev states are ordered from least to most healthy.
776199767fToomas Soome * A vdev that's CANT_OPEN or below is considered unusable.
777199767fToomas Soome */
778199767fToomas Soometypedef enum vdev_state {
779199767fToomas Soome	VDEV_STATE_UNKNOWN = 0,	/* Uninitialized vdev			*/
780199767fToomas Soome	VDEV_STATE_CLOSED,	/* Not currently open			*/
781199767fToomas Soome	VDEV_STATE_OFFLINE,	/* Not allowed to open			*/
782199767fToomas Soome	VDEV_STATE_REMOVED,	/* Explicitly removed from system	*/
783199767fToomas Soome	VDEV_STATE_CANT_OPEN,	/* Tried to open, but failed		*/
784199767fToomas Soome	VDEV_STATE_FAULTED,	/* External request to fault device	*/
785199767fToomas Soome	VDEV_STATE_DEGRADED,	/* Replicated vdev with unhealthy kids	*/
786199767fToomas Soome	VDEV_STATE_HEALTHY	/* Presumed good			*/
787199767fToomas Soome} vdev_state_t;
788199767fToomas Soome
789199767fToomas Soome/*
790199767fToomas Soome * vdev aux states.  When a vdev is in the CANT_OPEN state, the aux field
791199767fToomas Soome * of the vdev stats structure uses these constants to distinguish why.
792199767fToomas Soome */
793199767fToomas Soometypedef enum vdev_aux {
794199767fToomas Soome	VDEV_AUX_NONE,		/* no error				*/
795199767fToomas Soome	VDEV_AUX_OPEN_FAILED,	/* ldi_open_*() or vn_open() failed	*/
796199767fToomas Soome	VDEV_AUX_CORRUPT_DATA,	/* bad label or disk contents		*/
797199767fToomas Soome	VDEV_AUX_NO_REPLICAS,	/* insufficient number of replicas	*/
798199767fToomas Soome	VDEV_AUX_BAD_GUID_SUM,	/* vdev guid sum doesn't match		*/
799199767fToomas Soome	VDEV_AUX_TOO_SMALL,	/* vdev size is too small		*/
800199767fToomas Soome	VDEV_AUX_BAD_LABEL,	/* the label is OK but invalid		*/
801199767fToomas Soome	VDEV_AUX_VERSION_NEWER,	/* on-disk version is too new		*/
802199767fToomas Soome	VDEV_AUX_VERSION_OLDER,	/* on-disk version is too old		*/
803199767fToomas Soome	VDEV_AUX_SPARED		/* hot spare used in another pool	*/
804199767fToomas Soome} vdev_aux_t;
805199767fToomas Soome
806199767fToomas Soome/*
807199767fToomas Soome * pool state.  The following states are written to disk as part of the normal
808199767fToomas Soome * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE.  The remaining states are
809199767fToomas Soome * software abstractions used at various levels to communicate pool state.
810199767fToomas Soome */
811199767fToomas Soometypedef enum pool_state {
812199767fToomas Soome	POOL_STATE_ACTIVE = 0,		/* In active use		*/
813199767fToomas Soome	POOL_STATE_EXPORTED,		/* Explicitly exported		*/
814199767fToomas Soome	POOL_STATE_DESTROYED,		/* Explicitly destroyed		*/
815199767fToomas Soome	POOL_STATE_SPARE,		/* Reserved for hot spare use	*/
816199767fToomas Soome	POOL_STATE_UNINITIALIZED,	/* Internal spa_t state		*/
817199767fToomas Soome	POOL_STATE_UNAVAIL,		/* Internal libzfs state	*/
818199767fToomas Soome	POOL_STATE_POTENTIALLY_ACTIVE	/* Internal libzfs state	*/
819199767fToomas Soome} pool_state_t;
820199767fToomas Soome
821199767fToomas Soome/*
822199767fToomas Soome * The uberblock version is incremented whenever an incompatible on-disk
823199767fToomas Soome * format change is made to the SPA, DMU, or ZAP.
824199767fToomas Soome *
825199767fToomas Soome * Note: the first two fields should never be moved.  When a storage pool
826199767fToomas Soome * is opened, the uberblock must be read off the disk before the version
827199767fToomas Soome * can be checked.  If the ub_version field is moved, we may not detect
828199767fToomas Soome * version mismatch.  If the ub_magic field is moved, applications that
829199767fToomas Soome * expect the magic number in the first word won't work.
830199767fToomas Soome */
831199767fToomas Soome#define	UBERBLOCK_MAGIC		0x00bab10c		/* oo-ba-bloc!	*/
832199767fToomas Soome#define	UBERBLOCK_SHIFT		10			/* up to 1K	*/
833199767fToomas Soome
834199767fToomas Soomestruct uberblock {
835199767fToomas Soome	uint64_t	ub_magic;	/* UBERBLOCK_MAGIC		*/
836199767fToomas Soome	uint64_t	ub_version;	/* SPA_VERSION			*/
837199767fToomas Soome	uint64_t	ub_txg;		/* txg of last sync		*/
838199767fToomas Soome	uint64_t	ub_guid_sum;	/* sum of all vdev guids	*/
839199767fToomas Soome	uint64_t	ub_timestamp;	/* UTC time of last sync	*/
840199767fToomas Soome	blkptr_t	ub_rootbp;	/* MOS objset_phys_t		*/
841199767fToomas Soome};
842199767fToomas Soome
843199767fToomas Soome/*
844199767fToomas Soome * Flags.
845199767fToomas Soome */
846199767fToomas Soome#define	DNODE_MUST_BE_ALLOCATED	1
847199767fToomas Soome#define	DNODE_MUST_BE_FREE	2
848199767fToomas Soome
849199767fToomas Soome/*
850199767fToomas Soome * Fixed constants.
851199767fToomas Soome */
852199767fToomas Soome#define	DNODE_SHIFT		9	/* 512 bytes */
853199767fToomas Soome#define	DN_MIN_INDBLKSHIFT	12	/* 4k */
854199767fToomas Soome#define	DN_MAX_INDBLKSHIFT	14	/* 16k */
855199767fToomas Soome#define	DNODE_BLOCK_SHIFT	14	/* 16k */
856199767fToomas Soome#define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
857199767fToomas Soome#define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
858199767fToomas Soome#define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
859199767fToomas Soome
860199767fToomas Soome/*
861199767fToomas Soome * Derived constants.
862199767fToomas Soome */
863199767fToomas Soome#define	DNODE_SIZE	(1 << DNODE_SHIFT)
866199767fToomas Soome#define	DN_MAX_OBJECT	(1ULL << DN_MAX_OBJECT_SHIFT)
867199767fToomas Soome
869199767fToomas Soome#define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
871199767fToomas Soome
872199767fToomas Soome/* The +2 here is a cheesy way to round up */
873199767fToomas Soome#define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
875199767fToomas Soome
876199767fToomas Soome#define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
877199767fToomas Soome	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
878199767fToomas Soome
879199767fToomas Soome#define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
880199767fToomas Soome	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
881199767fToomas Soome
882199767fToomas Soome#define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
883199767fToomas Soome
884199767fToomas Soome/* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
885199767fToomas Soome#define	DNODE_FLAG_USED_BYTES		(1<<0)
886199767fToomas Soome#define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
887199767fToomas Soome
888199767fToomas Soome/* Does dnode have a SA spill blkptr in bonus? */
889199767fToomas Soome#define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
890199767fToomas Soome
891199767fToomas Soometypedef struct dnode_phys {
892199767fToomas Soome	uint8_t dn_type;		/* dmu_object_type_t */
893199767fToomas Soome	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
894199767fToomas Soome	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
895199767fToomas Soome	uint8_t dn_nblkptr;		/* length of dn_blkptr */
896199767fToomas Soome	uint8_t dn_bonustype;		/* type of data in bonus buffer */
897199767fToomas Soome	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
898199767fToomas Soome	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
899199767fToomas Soome	uint8_t dn_flags;		/* DNODE_FLAG_* */
900199767fToomas Soome	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
901199767fToomas Soome	uint16_t dn_bonuslen;		/* length of dn_bonus */
902199767fToomas Soome	uint8_t dn_pad2[4];
903199767fToomas Soome
904199767fToomas Soome	/* accounting is protected by dn_dirty_mtx */
905199767fToomas Soome	uint64_t dn_maxblkid;		/* largest allocated block ID */
906199767fToomas Soome	uint64_t dn_used;		/* bytes (or sectors) of disk space */
907199767fToomas Soome
908199767fToomas Soome	uint64_t dn_pad3[4];
909199767fToomas Soome
910199767fToomas Soome	blkptr_t dn_blkptr[1];
911199767fToomas Soome	uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)];
912199767fToomas Soome	blkptr_t dn_spill;
913199767fToomas Soome} dnode_phys_t;
914199767fToomas Soome
915199767fToomas Soometypedef enum dmu_object_byteswap {
916199767fToomas Soome	DMU_BSWAP_UINT8,
917199767fToomas Soome	DMU_BSWAP_UINT16,
918199767fToomas Soome	DMU_BSWAP_UINT32,
919199767fToomas Soome	DMU_BSWAP_UINT64,
920199767fToomas Soome	DMU_BSWAP_ZAP,
921199767fToomas Soome	DMU_BSWAP_DNODE,
922199767fToomas Soome	DMU_BSWAP_OBJSET,
923199767fToomas Soome	DMU_BSWAP_ZNODE,
924199767fToomas Soome	DMU_BSWAP_OLDACL,
925199767fToomas Soome	DMU_BSWAP_ACL,
926199767fToomas Soome	/*
927199767fToomas Soome	 * Allocating a new byteswap type number makes the on-disk format
928199767fToomas Soome	 * incompatible with any other format that uses the same number.
929199767fToomas Soome	 *
930199767fToomas Soome	 * Data can usually be structured to work with one of the
931199767fToomas Soome	 * DMU_BSWAP_UINT* or DMU_BSWAP_ZAP types.
932199767fToomas Soome	 */
933199767fToomas Soome	DMU_BSWAP_NUMFUNCS
934199767fToomas Soome} dmu_object_byteswap_t;
935199767fToomas Soome
936199767fToomas Soome#define	DMU_OT_NEWTYPE 0x80
937199767fToomas Soome#define	DMU_OT_METADATA 0x40
938199767fToomas Soome#define	DMU_OT_BYTESWAP_MASK 0x3f
939199767fToomas Soome
940199767fToomas Soome/*
941199767fToomas Soome * Defines a uint8_t object type. Object types specify if the data
942199767fToomas Soome * in the object is metadata (boolean) and how to byteswap the data
943199767fToomas Soome * (dmu_object_byteswap_t).
944199767fToomas Soome */
945199767fToomas Soome#define	DMU_OT(byteswap, metadata) \
946199767fToomas Soome	(DMU_OT_NEWTYPE | \
947199767fToomas Soome	((metadata) ? DMU_OT_METADATA : 0) | \
948199767fToomas Soome	((byteswap) & DMU_OT_BYTESWAP_MASK))
949199767fToomas Soome
950199767fToomas Soometypedef enum dmu_object_type {
951199767fToomas Soome	DMU_OT_NONE,
952199767fToomas Soome	/* general: */
953199767fToomas Soome	DMU_OT_OBJECT_DIRECTORY,	/* ZAP */
954199767fToomas Soome	DMU_OT_OBJECT_ARRAY,		/* UINT64 */
955199767fToomas Soome	DMU_OT_PACKED_NVLIST,		/* UINT8 (XDR by nvlist_pack/unpack) */
956199767fToomas Soome	DMU_OT_PACKED_NVLIST_SIZE,	/* UINT64 */
957199767fToomas Soome	DMU_OT_BPLIST,			/* UINT64 */
958199767fToomas Soome	DMU_OT_BPLIST_HDR,		/* UINT64 */
959199767fToomas Soome	/* spa: */
960199767fToomas Soome	DMU_OT_SPACE_MAP_HEADER,	/* UINT64 */
961199767fToomas Soome	DMU_OT_SPACE_MAP,		/* UINT64 */
962199767fToomas Soome	/* zil: */
963199767fToomas Soome	DMU_OT_INTENT_LOG,		/* UINT64 */
964199767fToomas Soome	/* dmu: */
965199767fToomas Soome	DMU_OT_DNODE,			/* DNODE */
966199767fToomas Soome	DMU_OT_OBJSET,			/* OBJSET */
967199767fToomas Soome	/* dsl: */
968199767fToomas Soome	DMU_OT_DSL_DIR,			/* UINT64 */
969199767fToomas Soome	DMU_OT_DSL_DIR_CHILD_MAP,	/* ZAP */
970199767fToomas Soome	DMU_OT_DSL_DS_SNAP_MAP,		/* ZAP */
971199767fToomas Soome	DMU_OT_DSL_PROPS,		/* ZAP */
972199767fToomas Soome	DMU_OT_DSL_DATASET,		/* UINT64 */
973199767fToomas Soome	/* zpl: */
974199767fToomas Soome	DMU_OT_ZNODE,			/* ZNODE */
975199767fToomas Soome	DMU_OT_OLDACL,			/* Old ACL */
976199767fToomas Soome	DMU_OT_PLAIN_FILE_CONTENTS,	/* UINT8 */
977199767fToomas Soome	DMU_OT_DIRECTORY_CONTENTS,	/* ZAP */
978199767fToomas Soome	DMU_OT_MASTER_NODE,		/* ZAP */
979199767fToomas Soome	DMU_OT_UNLINKED_SET,		/* ZAP */
980199767fToomas Soome	/* zvol: */
981199767fToomas Soome	DMU_OT_ZVOL,			/* UINT8 */
982199767fToomas Soome	DMU_OT_ZVOL_PROP,		/* ZAP */
983199767fToomas Soome	/* other; for testing only! */
984199767fToomas Soome	DMU_OT_PLAIN_OTHER,		/* UINT8 */
985199767fToomas Soome	DMU_OT_UINT64_OTHER,		/* UINT64 */
986199767fToomas Soome	DMU_OT_ZAP_OTHER,		/* ZAP */
987199767fToomas Soome	/* new object types: */
988199767fToomas Soome	DMU_OT_ERROR_LOG,		/* ZAP */
989199767fToomas Soome	DMU_OT_SPA_HISTORY,		/* UINT8 */
990199767fToomas Soome	DMU_OT_SPA_HISTORY_OFFSETS,	/* spa_his_phys_t */
991199767fToomas Soome	DMU_OT_POOL_PROPS,		/* ZAP */
992199767fToomas Soome	DMU_OT_DSL_PERMS,		/* ZAP */
993199767fToomas Soome	DMU_OT_ACL,			/* ACL */
994199767fToomas Soome	DMU_OT_SYSACL,			/* SYSACL */
995199767fToomas Soome	DMU_OT_FUID,			/* FUID table (Packed NVLIST UINT8) */
996199767fToomas Soome	DMU_OT_FUID_SIZE,		/* FUID table size UINT64 */
997199767fToomas Soome	DMU_OT_NEXT_CLONES,		/* ZAP */
998199767fToomas Soome	DMU_OT_SCAN_QUEUE,		/* ZAP */
999199767fToomas Soome	DMU_OT_USERGROUP_USED,		/* ZAP */
1000199767fToomas Soome	DMU_OT_USERGROUP_QUOTA,		/* ZAP */
1001199767fToomas Soome	DMU_OT_USERREFS,		/* ZAP */
1002199767fToomas Soome	DMU_OT_DDT_ZAP,			/* ZAP */
1003199767fToomas Soome	DMU_OT_DDT_STATS,		/* ZAP */
1004199767fToomas Soome	DMU_OT_SA,			/* System attr */
1005199767fToomas Soome	DMU_OT_SA_MASTER_NODE,		/* ZAP */
1006199767fToomas Soome	DMU_OT_SA_ATTR_REGISTRATION,	/* ZAP */
1007199767fToomas Soome	DMU_OT_SA_ATTR_LAYOUTS,		/* ZAP */
1008199767fToomas Soome	DMU_OT_SCAN_XLATE,		/* ZAP */
1009199767fToomas Soome	DMU_OT_DEDUP,			/* fake dedup BP from ddt_bp_create() */
1010199767fToomas Soome	DMU_OT_NUMTYPES,
1011199767fToomas Soome
1012199767fToomas Soome	/*
1013199767fToomas Soome	 * Names for valid types declared with DMU_OT().
1014199767fToomas Soome	 */
1015199767fToomas Soome	DMU_OTN_UINT8_DATA = DMU_OT(DMU_BSWAP_UINT8, B_FALSE),
1017199767fToomas Soome	DMU_OTN_UINT16_DATA = DMU_OT(DMU_BSWAP_UINT16, B_FALSE),