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