xref: /freebsd/sys/cddl/boot/zfs/zfsimpl.h (revision 3e8eb5c7f4909209c042403ddee340b2ee7003a5)
1 /*-
2  * Copyright (c) 2002 McAfee, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and McAfee Research,, the Security Research Division of
7  * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
8  * part of the DARPA CHATS research program
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /*
32  * CDDL HEADER START
33  *
34  * The contents of this file are subject to the terms of the
35  * Common Development and Distribution License (the "License").
36  * You may not use this file except in compliance with the License.
37  *
38  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
39  * or http://www.opensolaris.org/os/licensing.
40  * See the License for the specific language governing permissions
41  * and limitations under the License.
42  *
43  * When distributing Covered Code, include this CDDL HEADER in each
44  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45  * If applicable, add the following below this CDDL HEADER, with the
46  * fields enclosed by brackets "[]" replaced with your own identifying
47  * information: Portions Copyright [yyyy] [name of copyright owner]
48  *
49  * CDDL HEADER END
50  */
51 /*
52  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
53  * Use is subject to license terms.
54  */
55 /*
56  * Copyright 2013 by Saso Kiselkov. All rights reserved.
57  */
58 /*
59  * Copyright (c) 2020 by Delphix. All rights reserved.
60  */
61 
62 #include <sys/queue.h>
63 
64 #ifndef _ZFSIMPL_H_
65 #define	_ZFSIMPL_H_
66 
67 #define	MAXNAMELEN	256
68 
69 #define _NOTE(s)
70 
71 /*
72  * AVL comparator helpers
73  */
74 #define	AVL_ISIGN(a)	(((a) > 0) - ((a) < 0))
75 #define	AVL_CMP(a, b)	(((a) > (b)) - ((a) < (b)))
76 #define	AVL_PCMP(a, b)	\
77 	(((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b)))
78 
79 typedef enum { B_FALSE, B_TRUE } boolean_t;
80 
81 /* CRC64 table */
82 #define	ZFS_CRC64_POLY	0xC96C5795D7870F42ULL	/* ECMA-182, reflected form */
83 
84 /*
85  * Macros for various sorts of alignment and rounding when the alignment
86  * is known to be a power of 2.
87  */
88 #define	P2ALIGN(x, align)		((x) & -(align))
89 #define	P2PHASE(x, align)		((x) & ((align) - 1))
90 #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
91 #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
92 #define	P2END(x, align)			(-(~(x) & -(align)))
93 #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
94 #define	P2BOUNDARY(off, len, align)	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
95 
96 /*
97  * General-purpose 32-bit and 64-bit bitfield encodings.
98  */
99 #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
100 #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
101 #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
102 #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
103 
104 #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
105 #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
106 
107 #define	BF32_SET(x, low, len, val)	\
108 	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
109 #define	BF64_SET(x, low, len, val)	\
110 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
111 
112 #define	BF32_GET_SB(x, low, len, shift, bias)	\
113 	((BF32_GET(x, low, len) + (bias)) << (shift))
114 #define	BF64_GET_SB(x, low, len, shift, bias)	\
115 	((BF64_GET(x, low, len) + (bias)) << (shift))
116 
117 #define	BF32_SET_SB(x, low, len, shift, bias, val)	\
118 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
119 #define	BF64_SET_SB(x, low, len, shift, bias, val)	\
120 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
121 
122 /*
123  * Macros to reverse byte order
124  */
125 #define	BSWAP_8(x)	((x) & 0xff)
126 #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
127 #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
128 #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
129 
130 #define	SPA_MINBLOCKSHIFT	9
131 #define	SPA_OLDMAXBLOCKSHIFT	17
132 #define	SPA_MAXBLOCKSHIFT	24
133 #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
134 #define	SPA_OLDMAXBLOCKSIZE	(1ULL << SPA_OLDMAXBLOCKSHIFT)
135 #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
136 
137 /*
138  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
139  * The ASIZE encoding should be at least 64 times larger (6 more bits)
140  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
141  * overhead, three DVAs per bp, plus one more bit in case we do anything
142  * else that expands the ASIZE.
143  */
144 #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
145 #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
146 #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
147 
148 /*
149  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
150  * The members of the dva_t should be considered opaque outside the SPA.
151  */
152 typedef struct dva {
153 	uint64_t	dva_word[2];
154 } dva_t;
155 
156 /*
157  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
158  */
159 typedef struct zio_cksum {
160 	uint64_t	zc_word[4];
161 } zio_cksum_t;
162 
163 /*
164  * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
165  * secret and is suitable for use in MAC algorithms as the key.
166  */
167 typedef struct zio_cksum_salt {
168 	uint8_t		zcs_bytes[32];
169 } zio_cksum_salt_t;
170 
171 /*
172  * Each block is described by its DVAs, time of birth, checksum, etc.
173  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
174  *
175  *	64	56	48	40	32	24	16	8	0
176  *	+-------+-------+-------+-------+-------+-------+-------+-------+
177  * 0	|		vdev1		| GRID  |	  ASIZE		|
178  *	+-------+-------+-------+-------+-------+-------+-------+-------+
179  * 1	|G|			 offset1				|
180  *	+-------+-------+-------+-------+-------+-------+-------+-------+
181  * 2	|		vdev2		| GRID  |	  ASIZE		|
182  *	+-------+-------+-------+-------+-------+-------+-------+-------+
183  * 3	|G|			 offset2				|
184  *	+-------+-------+-------+-------+-------+-------+-------+-------+
185  * 4	|		vdev3		| GRID  |	  ASIZE		|
186  *	+-------+-------+-------+-------+-------+-------+-------+-------+
187  * 5	|G|			 offset3				|
188  *	+-------+-------+-------+-------+-------+-------+-------+-------+
189  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
190  *	+-------+-------+-------+-------+-------+-------+-------+-------+
191  * 7	|			padding					|
192  *	+-------+-------+-------+-------+-------+-------+-------+-------+
193  * 8	|			padding					|
194  *	+-------+-------+-------+-------+-------+-------+-------+-------+
195  * 9	|			physical birth txg			|
196  *	+-------+-------+-------+-------+-------+-------+-------+-------+
197  * a	|			logical birth txg			|
198  *	+-------+-------+-------+-------+-------+-------+-------+-------+
199  * b	|			fill count				|
200  *	+-------+-------+-------+-------+-------+-------+-------+-------+
201  * c	|			checksum[0]				|
202  *	+-------+-------+-------+-------+-------+-------+-------+-------+
203  * d	|			checksum[1]				|
204  *	+-------+-------+-------+-------+-------+-------+-------+-------+
205  * e	|			checksum[2]				|
206  *	+-------+-------+-------+-------+-------+-------+-------+-------+
207  * f	|			checksum[3]				|
208  *	+-------+-------+-------+-------+-------+-------+-------+-------+
209  *
210  * Legend:
211  *
212  * vdev		virtual device ID
213  * offset	offset into virtual device
214  * LSIZE	logical size
215  * PSIZE	physical size (after compression)
216  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
217  * GRID		RAID-Z layout information (reserved for future use)
218  * cksum	checksum function
219  * comp		compression function
220  * G		gang block indicator
221  * B		byteorder (endianness)
222  * D		dedup
223  * X		encryption (on version 30, which is not supported)
224  * E		blkptr_t contains embedded data (see below)
225  * lvl		level of indirection
226  * type		DMU object type
227  * phys birth	txg of block allocation; zero if same as logical birth txg
228  * log. birth	transaction group in which the block was logically born
229  * fill count	number of non-zero blocks under this bp
230  * checksum[4]	256-bit checksum of the data this bp describes
231  */
232 
233 /*
234  * "Embedded" blkptr_t's don't actually point to a block, instead they
235  * have a data payload embedded in the blkptr_t itself.  See the comment
236  * in blkptr.c for more details.
237  *
238  * The blkptr_t is laid out as follows:
239  *
240  *	64	56	48	40	32	24	16	8	0
241  *	+-------+-------+-------+-------+-------+-------+-------+-------+
242  * 0	|      payload                                                  |
243  * 1	|      payload                                                  |
244  * 2	|      payload                                                  |
245  * 3	|      payload                                                  |
246  * 4	|      payload                                                  |
247  * 5	|      payload                                                  |
248  *	+-------+-------+-------+-------+-------+-------+-------+-------+
249  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
250  *	+-------+-------+-------+-------+-------+-------+-------+-------+
251  * 7	|      payload                                                  |
252  * 8	|      payload                                                  |
253  * 9	|      payload                                                  |
254  *	+-------+-------+-------+-------+-------+-------+-------+-------+
255  * a	|			logical birth txg			|
256  *	+-------+-------+-------+-------+-------+-------+-------+-------+
257  * b	|      payload                                                  |
258  * c	|      payload                                                  |
259  * d	|      payload                                                  |
260  * e	|      payload                                                  |
261  * f	|      payload                                                  |
262  *	+-------+-------+-------+-------+-------+-------+-------+-------+
263  *
264  * Legend:
265  *
266  * payload		contains the embedded data
267  * B (byteorder)	byteorder (endianness)
268  * D (dedup)		padding (set to zero)
269  * X			encryption (set to zero; see above)
270  * E (embedded)		set to one
271  * lvl			indirection level
272  * type			DMU object type
273  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
274  * comp			compression function of payload
275  * PSIZE		size of payload after compression, in bytes
276  * LSIZE		logical size of payload, in bytes
277  *			note that 25 bits is enough to store the largest
278  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
279  * log. birth		transaction group in which the block was logically born
280  *
281  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
282  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
283  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
284  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
285  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
286  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
287  * other macros, as they assert that they are only used on BP's of the correct
288  * "embedded-ness".
289  */
290 
291 #define	BPE_GET_ETYPE(bp)	\
292 	(ASSERT(BP_IS_EMBEDDED(bp)), \
293 	BF64_GET((bp)->blk_prop, 40, 8))
294 #define	BPE_SET_ETYPE(bp, t)	do { \
295 	ASSERT(BP_IS_EMBEDDED(bp)); \
296 	BF64_SET((bp)->blk_prop, 40, 8, t); \
297 _NOTE(CONSTCOND) } while (0)
298 
299 #define	BPE_GET_LSIZE(bp)	\
300 	(ASSERT(BP_IS_EMBEDDED(bp)), \
301 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
302 #define	BPE_SET_LSIZE(bp, x)	do { \
303 	ASSERT(BP_IS_EMBEDDED(bp)); \
304 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
305 _NOTE(CONSTCOND) } while (0)
306 
307 #define	BPE_GET_PSIZE(bp)	\
308 	(ASSERT(BP_IS_EMBEDDED(bp)), \
309 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
310 #define	BPE_SET_PSIZE(bp, x)	do { \
311 	ASSERT(BP_IS_EMBEDDED(bp)); \
312 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
313 _NOTE(CONSTCOND) } while (0)
314 
315 typedef enum bp_embedded_type {
316 	BP_EMBEDDED_TYPE_DATA,
317 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
318 	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
319 } bp_embedded_type_t;
320 
321 #define	BPE_NUM_WORDS 14
322 #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
323 #define	BPE_IS_PAYLOADWORD(bp, wp) \
324 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
325 
326 #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
327 #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
328 
329 typedef struct blkptr {
330 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
331 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
332 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
333 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
334 	uint64_t	blk_birth;	/* transaction group at birth	    */
335 	uint64_t	blk_fill;	/* fill count			    */
336 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
337 } blkptr_t;
338 
339 /*
340  * Macros to get and set fields in a bp or DVA.
341  */
342 #define	DVA_GET_ASIZE(dva)	\
343 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
344 #define	DVA_SET_ASIZE(dva, x)	\
345 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
346 	SPA_MINBLOCKSHIFT, 0, x)
347 
348 #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
349 #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
350 
351 #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
352 #define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
353 
354 #define	DVA_GET_OFFSET(dva)	\
355 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
356 #define	DVA_SET_OFFSET(dva, x)	\
357 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
358 
359 #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
360 #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
361 
362 #define	BP_GET_LSIZE(bp)	\
363 	(BP_IS_EMBEDDED(bp) ?	\
364 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
365 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
366 #define	BP_SET_LSIZE(bp, x)	do { \
367 	ASSERT(!BP_IS_EMBEDDED(bp)); \
368 	BF64_SET_SB((bp)->blk_prop, \
369 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
370 _NOTE(CONSTCOND) } while (0)
371 
372 #define	BP_GET_PSIZE(bp)	\
373 	BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)
374 #define	BP_SET_PSIZE(bp, x)	\
375 	BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x)
376 
377 #define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 7)
378 #define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 7, x)
379 
380 #define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
381 #define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
382 
383 #define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
384 #define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
385 
386 #define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
387 #define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
388 
389 #define	BP_IS_EMBEDDED(bp)	BF64_GET((bp)->blk_prop, 39, 1)
390 
391 #define	BP_GET_DEDUP(bp)	BF64_GET((bp)->blk_prop, 62, 1)
392 #define	BP_SET_DEDUP(bp, x)	BF64_SET((bp)->blk_prop, 62, 1, x)
393 
394 #define	BP_GET_BYTEORDER(bp)	BF64_GET((bp)->blk_prop, 63, 1)
395 #define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
396 
397 #define	BP_PHYSICAL_BIRTH(bp)		\
398 	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
399 
400 #define	BP_SET_BIRTH(bp, logical, physical)	\
401 {						\
402 	ASSERT(!BP_IS_EMBEDDED(bp));		\
403 	(bp)->blk_birth = (logical);		\
404 	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
405 }
406 
407 #define	BP_GET_FILL(bp)				\
408 	((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill)
409 
410 #define	BP_SET_FILL(bp, fill)			\
411 {						\
412 	(bp)->blk_fill = fill;			\
413 }
414 
415 #define	BP_GET_ASIZE(bp)	\
416 	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
417 		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
418 
419 #define	BP_GET_UCSIZE(bp) \
420 	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
421 	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
422 
423 #define	BP_GET_NDVAS(bp)	\
424 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
425 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
426 	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
427 
428 #define	DVA_EQUAL(dva1, dva2)	\
429 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
430 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
431 
432 #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
433 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
434 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
435 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
436 	((zc1).zc_word[3] - (zc2).zc_word[3])))
437 
438 
439 #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
440 
441 #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
442 {						\
443 	(zcp)->zc_word[0] = w0;			\
444 	(zcp)->zc_word[1] = w1;			\
445 	(zcp)->zc_word[2] = w2;			\
446 	(zcp)->zc_word[3] = w3;			\
447 }
448 
449 #define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
450 #define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
451 #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&  \
452 	(dva)->dva_word[1] == 0ULL)
453 #define	BP_IS_HOLE(bp)		DVA_IS_EMPTY(BP_IDENTITY(bp))
454 #define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
455 
456 #define	BP_ZERO(bp)				\
457 {						\
458 	(bp)->blk_dva[0].dva_word[0] = 0;	\
459 	(bp)->blk_dva[0].dva_word[1] = 0;	\
460 	(bp)->blk_dva[1].dva_word[0] = 0;	\
461 	(bp)->blk_dva[1].dva_word[1] = 0;	\
462 	(bp)->blk_dva[2].dva_word[0] = 0;	\
463 	(bp)->blk_dva[2].dva_word[1] = 0;	\
464 	(bp)->blk_prop = 0;			\
465 	(bp)->blk_pad[0] = 0;			\
466 	(bp)->blk_pad[1] = 0;			\
467 	(bp)->blk_phys_birth = 0;		\
468 	(bp)->blk_birth = 0;			\
469 	(bp)->blk_fill = 0;			\
470 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
471 }
472 
473 #if BYTE_ORDER == _BIG_ENDIAN
474 #define	ZFS_HOST_BYTEORDER	(0ULL)
475 #else
476 #define	ZFS_HOST_BYTEORDER	(1ULL)
477 #endif
478 
479 #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
480 #define	BPE_NUM_WORDS 14
481 #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
482 #define	BPE_IS_PAYLOADWORD(bp, wp) \
483 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
484 
485 /*
486  * Embedded checksum
487  */
488 #define	ZEC_MAGIC	0x210da7ab10c7a11ULL
489 
490 typedef struct zio_eck {
491 	uint64_t	zec_magic;	/* for validation, endianness	*/
492 	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
493 } zio_eck_t;
494 
495 /*
496  * Gang block headers are self-checksumming and contain an array
497  * of block pointers.
498  */
499 #define	SPA_GANGBLOCKSIZE	SPA_MINBLOCKSIZE
500 #define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
501 	sizeof (zio_eck_t)) / sizeof (blkptr_t))
502 #define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
503 	sizeof (zio_eck_t) - \
504 	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
505 	sizeof (uint64_t))
506 
507 typedef struct zio_gbh {
508 	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
509 	uint64_t		zg_filler[SPA_GBH_FILLER];
510 	zio_eck_t		zg_tail;
511 } zio_gbh_phys_t;
512 
513 #define	VDEV_RAIDZ_MAXPARITY	3
514 
515 #define	VDEV_PAD_SIZE		(8 << 10)
516 /* 2 padding areas (vl_pad1 and vl_be) to skip */
517 #define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
518 #define	VDEV_PHYS_SIZE		(112 << 10)
519 #define	VDEV_UBERBLOCK_RING	(128 << 10)
520 
521 /*
522  * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
523  * ring when MMP is enabled.
524  */
525 #define	MMP_BLOCKS_PER_LABEL	1
526 
527 /* The largest uberblock we support is 8k. */
528 #define	MAX_UBERBLOCK_SHIFT	(13)
529 #define	VDEV_UBERBLOCK_SHIFT(vd)	\
530 	MIN(MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT), MAX_UBERBLOCK_SHIFT)
531 #define	VDEV_UBERBLOCK_COUNT(vd)	\
532 	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
533 #define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
534 	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
535 #define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
536 
537 typedef struct vdev_phys {
538 	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
539 	zio_eck_t	vp_zbt;
540 } vdev_phys_t;
541 
542 typedef enum vbe_vers {
543 	/* The bootenv file is stored as ascii text in the envblock */
544 	VB_RAW = 0,
545 
546 	/*
547 	 * The bootenv file is converted to an nvlist and then packed into the
548 	 * envblock.
549 	 */
550 	VB_NVLIST = 1
551 } vbe_vers_t;
552 
553 typedef struct vdev_boot_envblock {
554 	uint64_t	vbe_version;
555 	char		vbe_bootenv[VDEV_PAD_SIZE - sizeof (uint64_t) -
556 			sizeof (zio_eck_t)];
557  	zio_eck_t	vbe_zbt;
558 } vdev_boot_envblock_t;
559 
560 _Static_assert(sizeof (vdev_boot_envblock_t) == VDEV_PAD_SIZE,
561     "bad size for vdev_boot_envblock_t");
562 
563 typedef struct vdev_label {
564 	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K  */
565 	vdev_boot_envblock_t	vl_be;				/*  8K  */
566 	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
567 	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
568 } vdev_label_t;							/* 256K total */
569 
570 /*
571  * vdev_dirty() flags
572  */
573 #define	VDD_METASLAB	0x01
574 #define	VDD_DTL		0x02
575 
576 /*
577  * Size and offset of embedded boot loader region on each label.
578  * The total size of the first two labels plus the boot area is 4MB.
579  */
580 #define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
581 #define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M	*/
582 
583 /*
584  * Size of label regions at the start and end of each leaf device.
585  */
586 #define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
587 #define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
588 #define	VDEV_LABELS		4
589 
590 enum zio_checksum {
591 	ZIO_CHECKSUM_INHERIT = 0,
592 	ZIO_CHECKSUM_ON,
593 	ZIO_CHECKSUM_OFF,
594 	ZIO_CHECKSUM_LABEL,
595 	ZIO_CHECKSUM_GANG_HEADER,
596 	ZIO_CHECKSUM_ZILOG,
597 	ZIO_CHECKSUM_FLETCHER_2,
598 	ZIO_CHECKSUM_FLETCHER_4,
599 	ZIO_CHECKSUM_SHA256,
600 	ZIO_CHECKSUM_ZILOG2,
601 	ZIO_CHECKSUM_NOPARITY,
602 	ZIO_CHECKSUM_SHA512,
603 	ZIO_CHECKSUM_SKEIN,
604 	ZIO_CHECKSUM_EDONR,
605 	ZIO_CHECKSUM_FUNCTIONS
606 };
607 
608 #define	ZIO_CHECKSUM_ON_VALUE	ZIO_CHECKSUM_FLETCHER_4
609 #define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
610 
611 enum zio_compress {
612 	ZIO_COMPRESS_INHERIT = 0,
613 	ZIO_COMPRESS_ON,
614 	ZIO_COMPRESS_OFF,
615 	ZIO_COMPRESS_LZJB,
616 	ZIO_COMPRESS_EMPTY,
617 	ZIO_COMPRESS_GZIP_1,
618 	ZIO_COMPRESS_GZIP_2,
619 	ZIO_COMPRESS_GZIP_3,
620 	ZIO_COMPRESS_GZIP_4,
621 	ZIO_COMPRESS_GZIP_5,
622 	ZIO_COMPRESS_GZIP_6,
623 	ZIO_COMPRESS_GZIP_7,
624 	ZIO_COMPRESS_GZIP_8,
625 	ZIO_COMPRESS_GZIP_9,
626 	ZIO_COMPRESS_ZLE,
627 	ZIO_COMPRESS_LZ4,
628 	ZIO_COMPRESS_ZSTD,
629 	ZIO_COMPRESS_FUNCTIONS
630 };
631 
632 enum zio_zstd_levels {
633 	ZIO_ZSTD_LEVEL_INHERIT = 0,
634 	ZIO_ZSTD_LEVEL_1,
635 #define	ZIO_ZSTD_LEVEL_MIN	ZIO_ZSTD_LEVEL_1
636 	ZIO_ZSTD_LEVEL_2,
637 	ZIO_ZSTD_LEVEL_3,
638 #define	ZIO_ZSTD_LEVEL_DEFAULT	ZIO_ZSTD_LEVEL_3
639 	ZIO_ZSTD_LEVEL_4,
640 	ZIO_ZSTD_LEVEL_5,
641 	ZIO_ZSTD_LEVEL_6,
642 	ZIO_ZSTD_LEVEL_7,
643 	ZIO_ZSTD_LEVEL_8,
644 	ZIO_ZSTD_LEVEL_9,
645 	ZIO_ZSTD_LEVEL_10,
646 	ZIO_ZSTD_LEVEL_11,
647 	ZIO_ZSTD_LEVEL_12,
648 	ZIO_ZSTD_LEVEL_13,
649 	ZIO_ZSTD_LEVEL_14,
650 	ZIO_ZSTD_LEVEL_15,
651 	ZIO_ZSTD_LEVEL_16,
652 	ZIO_ZSTD_LEVEL_17,
653 	ZIO_ZSTD_LEVEL_18,
654 	ZIO_ZSTD_LEVEL_19,
655 #define	ZIO_ZSTD_LEVEL_MAX	ZIO_ZSTD_LEVEL_19
656 	ZIO_ZSTD_LEVEL_RESERVE = 101, /* Leave room for new positive levels */
657 	ZIO_ZSTD_LEVEL_FAST, /* Fast levels are negative */
658 	ZIO_ZSTD_LEVEL_FAST_1,
659 #define	ZIO_ZSTD_LEVEL_FAST_DEFAULT	ZIO_ZSTD_LEVEL_FAST_1
660 	ZIO_ZSTD_LEVEL_FAST_2,
661 	ZIO_ZSTD_LEVEL_FAST_3,
662 	ZIO_ZSTD_LEVEL_FAST_4,
663 	ZIO_ZSTD_LEVEL_FAST_5,
664 	ZIO_ZSTD_LEVEL_FAST_6,
665 	ZIO_ZSTD_LEVEL_FAST_7,
666 	ZIO_ZSTD_LEVEL_FAST_8,
667 	ZIO_ZSTD_LEVEL_FAST_9,
668 	ZIO_ZSTD_LEVEL_FAST_10,
669 	ZIO_ZSTD_LEVEL_FAST_20,
670 	ZIO_ZSTD_LEVEL_FAST_30,
671 	ZIO_ZSTD_LEVEL_FAST_40,
672 	ZIO_ZSTD_LEVEL_FAST_50,
673 	ZIO_ZSTD_LEVEL_FAST_60,
674 	ZIO_ZSTD_LEVEL_FAST_70,
675 	ZIO_ZSTD_LEVEL_FAST_80,
676 	ZIO_ZSTD_LEVEL_FAST_90,
677 	ZIO_ZSTD_LEVEL_FAST_100,
678 	ZIO_ZSTD_LEVEL_FAST_500,
679 	ZIO_ZSTD_LEVEL_FAST_1000,
680 #define	ZIO_ZSTD_LEVEL_FAST_MAX	ZIO_ZSTD_LEVEL_FAST_1000
681 	ZIO_ZSTD_LEVEL_AUTO = 251, /* Reserved for future use */
682 	ZIO_ZSTD_LEVEL_LEVELS
683 };
684 
685 #define	ZIO_COMPRESS_ON_VALUE	ZIO_COMPRESS_LZJB
686 #define	ZIO_COMPRESS_DEFAULT	ZIO_COMPRESS_OFF
687 
688 /*
689  * On-disk version number.
690  */
691 #define	SPA_VERSION_1			1ULL
692 #define	SPA_VERSION_2			2ULL
693 #define	SPA_VERSION_3			3ULL
694 #define	SPA_VERSION_4			4ULL
695 #define	SPA_VERSION_5			5ULL
696 #define	SPA_VERSION_6			6ULL
697 #define	SPA_VERSION_7			7ULL
698 #define	SPA_VERSION_8			8ULL
699 #define	SPA_VERSION_9			9ULL
700 #define	SPA_VERSION_10			10ULL
701 #define	SPA_VERSION_11			11ULL
702 #define	SPA_VERSION_12			12ULL
703 #define	SPA_VERSION_13			13ULL
704 #define	SPA_VERSION_14			14ULL
705 #define	SPA_VERSION_15			15ULL
706 #define	SPA_VERSION_16			16ULL
707 #define	SPA_VERSION_17			17ULL
708 #define	SPA_VERSION_18			18ULL
709 #define	SPA_VERSION_19			19ULL
710 #define	SPA_VERSION_20			20ULL
711 #define	SPA_VERSION_21			21ULL
712 #define	SPA_VERSION_22			22ULL
713 #define	SPA_VERSION_23			23ULL
714 #define	SPA_VERSION_24			24ULL
715 #define	SPA_VERSION_25			25ULL
716 #define	SPA_VERSION_26			26ULL
717 #define	SPA_VERSION_27			27ULL
718 #define	SPA_VERSION_28			28ULL
719 #define	SPA_VERSION_5000		5000ULL
720 
721 /*
722  * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
723  * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
724  * and do the appropriate changes.  Also bump the version number in
725  * usr/src/grub/capability.
726  */
727 #define	SPA_VERSION			SPA_VERSION_5000
728 #define	SPA_VERSION_STRING		"5000"
729 
730 /*
731  * Symbolic names for the changes that caused a SPA_VERSION switch.
732  * Used in the code when checking for presence or absence of a feature.
733  * Feel free to define multiple symbolic names for each version if there
734  * were multiple changes to on-disk structures during that version.
735  *
736  * NOTE: When checking the current SPA_VERSION in your code, be sure
737  *       to use spa_version() since it reports the version of the
738  *       last synced uberblock.  Checking the in-flight version can
739  *       be dangerous in some cases.
740  */
741 #define	SPA_VERSION_INITIAL		SPA_VERSION_1
742 #define	SPA_VERSION_DITTO_BLOCKS	SPA_VERSION_2
743 #define	SPA_VERSION_SPARES		SPA_VERSION_3
744 #define	SPA_VERSION_RAID6		SPA_VERSION_3
745 #define	SPA_VERSION_BPLIST_ACCOUNT	SPA_VERSION_3
746 #define	SPA_VERSION_RAIDZ_DEFLATE	SPA_VERSION_3
747 #define	SPA_VERSION_DNODE_BYTES		SPA_VERSION_3
748 #define	SPA_VERSION_ZPOOL_HISTORY	SPA_VERSION_4
749 #define	SPA_VERSION_GZIP_COMPRESSION	SPA_VERSION_5
750 #define	SPA_VERSION_BOOTFS		SPA_VERSION_6
751 #define	SPA_VERSION_SLOGS		SPA_VERSION_7
752 #define	SPA_VERSION_DELEGATED_PERMS	SPA_VERSION_8
753 #define	SPA_VERSION_FUID		SPA_VERSION_9
754 #define	SPA_VERSION_REFRESERVATION	SPA_VERSION_9
755 #define	SPA_VERSION_REFQUOTA		SPA_VERSION_9
756 #define	SPA_VERSION_UNIQUE_ACCURATE	SPA_VERSION_9
757 #define	SPA_VERSION_L2CACHE		SPA_VERSION_10
758 #define	SPA_VERSION_NEXT_CLONES		SPA_VERSION_11
759 #define	SPA_VERSION_ORIGIN		SPA_VERSION_11
760 #define	SPA_VERSION_DSL_SCRUB		SPA_VERSION_11
761 #define	SPA_VERSION_SNAP_PROPS		SPA_VERSION_12
762 #define	SPA_VERSION_USED_BREAKDOWN	SPA_VERSION_13
763 #define	SPA_VERSION_PASSTHROUGH_X	SPA_VERSION_14
764 #define SPA_VERSION_USERSPACE		SPA_VERSION_15
765 #define	SPA_VERSION_STMF_PROP		SPA_VERSION_16
766 #define	SPA_VERSION_RAIDZ3		SPA_VERSION_17
767 #define	SPA_VERSION_USERREFS		SPA_VERSION_18
768 #define	SPA_VERSION_HOLES		SPA_VERSION_19
769 #define	SPA_VERSION_ZLE_COMPRESSION	SPA_VERSION_20
770 #define	SPA_VERSION_DEDUP		SPA_VERSION_21
771 #define	SPA_VERSION_RECVD_PROPS		SPA_VERSION_22
772 #define	SPA_VERSION_SLIM_ZIL		SPA_VERSION_23
773 #define	SPA_VERSION_SA			SPA_VERSION_24
774 #define	SPA_VERSION_SCAN		SPA_VERSION_25
775 #define	SPA_VERSION_DIR_CLONES		SPA_VERSION_26
776 #define	SPA_VERSION_DEADLISTS		SPA_VERSION_26
777 #define	SPA_VERSION_FAST_SNAP		SPA_VERSION_27
778 #define	SPA_VERSION_MULTI_REPLACE	SPA_VERSION_28
779 #define	SPA_VERSION_BEFORE_FEATURES	SPA_VERSION_28
780 #define	SPA_VERSION_FEATURES		SPA_VERSION_5000
781 
782 #define	SPA_VERSION_IS_SUPPORTED(v) \
783 	(((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
784 	((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
785 
786 /*
787  * The following are configuration names used in the nvlist describing a pool's
788  * configuration.
789  */
790 #define	ZPOOL_CONFIG_VERSION		"version"
791 #define	ZPOOL_CONFIG_POOL_NAME		"name"
792 #define	ZPOOL_CONFIG_POOL_STATE		"state"
793 #define	ZPOOL_CONFIG_POOL_TXG		"txg"
794 #define	ZPOOL_CONFIG_POOL_GUID		"pool_guid"
795 #define	ZPOOL_CONFIG_CREATE_TXG		"create_txg"
796 #define	ZPOOL_CONFIG_TOP_GUID		"top_guid"
797 #define	ZPOOL_CONFIG_VDEV_TREE		"vdev_tree"
798 #define	ZPOOL_CONFIG_TYPE		"type"
799 #define	ZPOOL_CONFIG_CHILDREN		"children"
800 #define	ZPOOL_CONFIG_ID			"id"
801 #define	ZPOOL_CONFIG_GUID		"guid"
802 #define	ZPOOL_CONFIG_INDIRECT_OBJECT	"com.delphix:indirect_object"
803 #define	ZPOOL_CONFIG_INDIRECT_BIRTHS	"com.delphix:indirect_births"
804 #define	ZPOOL_CONFIG_PREV_INDIRECT_VDEV	"com.delphix:prev_indirect_vdev"
805 #define	ZPOOL_CONFIG_PATH		"path"
806 #define	ZPOOL_CONFIG_DEVID		"devid"
807 #define	ZPOOL_CONFIG_METASLAB_ARRAY	"metaslab_array"
808 #define	ZPOOL_CONFIG_METASLAB_SHIFT	"metaslab_shift"
809 #define	ZPOOL_CONFIG_ASHIFT		"ashift"
810 #define	ZPOOL_CONFIG_ASIZE		"asize"
811 #define	ZPOOL_CONFIG_DTL		"DTL"
812 #define	ZPOOL_CONFIG_STATS		"stats"
813 #define	ZPOOL_CONFIG_WHOLE_DISK		"whole_disk"
814 #define	ZPOOL_CONFIG_ERRCOUNT		"error_count"
815 #define	ZPOOL_CONFIG_NOT_PRESENT	"not_present"
816 #define	ZPOOL_CONFIG_SPARES		"spares"
817 #define	ZPOOL_CONFIG_IS_SPARE		"is_spare"
818 #define	ZPOOL_CONFIG_NPARITY		"nparity"
819 #define	ZPOOL_CONFIG_HOSTID		"hostid"
820 #define	ZPOOL_CONFIG_HOSTNAME		"hostname"
821 #define	ZPOOL_CONFIG_IS_LOG		"is_log"
822 #define	ZPOOL_CONFIG_TIMESTAMP		"timestamp" /* not stored on disk */
823 #define	ZPOOL_CONFIG_FEATURES_FOR_READ	"features_for_read"
824 #define	ZPOOL_CONFIG_VDEV_CHILDREN	"vdev_children"
825 
826 /*
827  * The persistent vdev state is stored as separate values rather than a single
828  * 'vdev_state' entry.  This is because a device can be in multiple states, such
829  * as offline and degraded.
830  */
831 #define	ZPOOL_CONFIG_OFFLINE            "offline"
832 #define	ZPOOL_CONFIG_FAULTED            "faulted"
833 #define	ZPOOL_CONFIG_DEGRADED           "degraded"
834 #define	ZPOOL_CONFIG_REMOVED            "removed"
835 #define	ZPOOL_CONFIG_FRU		"fru"
836 #define	ZPOOL_CONFIG_AUX_STATE		"aux_state"
837 
838 #define	VDEV_TYPE_ROOT			"root"
839 #define	VDEV_TYPE_MIRROR		"mirror"
840 #define	VDEV_TYPE_REPLACING		"replacing"
841 #define	VDEV_TYPE_RAIDZ			"raidz"
842 #define	VDEV_TYPE_DISK			"disk"
843 #define	VDEV_TYPE_FILE			"file"
844 #define	VDEV_TYPE_MISSING		"missing"
845 #define	VDEV_TYPE_HOLE			"hole"
846 #define	VDEV_TYPE_SPARE			"spare"
847 #define	VDEV_TYPE_LOG			"log"
848 #define	VDEV_TYPE_L2CACHE		"l2cache"
849 #define	VDEV_TYPE_INDIRECT		"indirect"
850 
851 /*
852  * This is needed in userland to report the minimum necessary device size.
853  */
854 #define	SPA_MINDEVSIZE		(64ULL << 20)
855 
856 /*
857  * The location of the pool configuration repository, shared between kernel and
858  * userland.
859  */
860 #define	ZPOOL_CACHE		"/boot/zfs/zpool.cache"
861 
862 /*
863  * vdev states are ordered from least to most healthy.
864  * A vdev that's CANT_OPEN or below is considered unusable.
865  */
866 typedef enum vdev_state {
867 	VDEV_STATE_UNKNOWN = 0,	/* Uninitialized vdev			*/
868 	VDEV_STATE_CLOSED,	/* Not currently open			*/
869 	VDEV_STATE_OFFLINE,	/* Not allowed to open			*/
870 	VDEV_STATE_REMOVED,	/* Explicitly removed from system	*/
871 	VDEV_STATE_CANT_OPEN,	/* Tried to open, but failed		*/
872 	VDEV_STATE_FAULTED,	/* External request to fault device	*/
873 	VDEV_STATE_DEGRADED,	/* Replicated vdev with unhealthy kids	*/
874 	VDEV_STATE_HEALTHY	/* Presumed good			*/
875 } vdev_state_t;
876 
877 /*
878  * vdev aux states.  When a vdev is in the CANT_OPEN state, the aux field
879  * of the vdev stats structure uses these constants to distinguish why.
880  */
881 typedef enum vdev_aux {
882 	VDEV_AUX_NONE,		/* no error				*/
883 	VDEV_AUX_OPEN_FAILED,	/* ldi_open_*() or vn_open() failed	*/
884 	VDEV_AUX_CORRUPT_DATA,	/* bad label or disk contents		*/
885 	VDEV_AUX_NO_REPLICAS,	/* insufficient number of replicas	*/
886 	VDEV_AUX_BAD_GUID_SUM,	/* vdev guid sum doesn't match		*/
887 	VDEV_AUX_TOO_SMALL,	/* vdev size is too small		*/
888 	VDEV_AUX_BAD_LABEL,	/* the label is OK but invalid		*/
889 	VDEV_AUX_VERSION_NEWER,	/* on-disk version is too new		*/
890 	VDEV_AUX_VERSION_OLDER,	/* on-disk version is too old		*/
891 	VDEV_AUX_SPARED		/* hot spare used in another pool	*/
892 } vdev_aux_t;
893 
894 /*
895  * pool state.  The following states are written to disk as part of the normal
896  * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE.  The remaining states are
897  * software abstractions used at various levels to communicate pool state.
898  */
899 typedef enum pool_state {
900 	POOL_STATE_ACTIVE = 0,		/* In active use		*/
901 	POOL_STATE_EXPORTED,		/* Explicitly exported		*/
902 	POOL_STATE_DESTROYED,		/* Explicitly destroyed		*/
903 	POOL_STATE_SPARE,		/* Reserved for hot spare use	*/
904 	POOL_STATE_UNINITIALIZED,	/* Internal spa_t state		*/
905 	POOL_STATE_UNAVAIL,		/* Internal libzfs state	*/
906 	POOL_STATE_POTENTIALLY_ACTIVE	/* Internal libzfs state	*/
907 } pool_state_t;
908 
909 /*
910  * The uberblock version is incremented whenever an incompatible on-disk
911  * format change is made to the SPA, DMU, or ZAP.
912  *
913  * Note: the first two fields should never be moved.  When a storage pool
914  * is opened, the uberblock must be read off the disk before the version
915  * can be checked.  If the ub_version field is moved, we may not detect
916  * version mismatch.  If the ub_magic field is moved, applications that
917  * expect the magic number in the first word won't work.
918  */
919 #define	UBERBLOCK_MAGIC		0x00bab10c		/* oo-ba-bloc!	*/
920 #define	UBERBLOCK_SHIFT		10			/* up to 1K	*/
921 
922 #define	MMP_MAGIC		0xa11cea11		/* all-see-all  */
923 
924 #define	MMP_INTERVAL_VALID_BIT	0x01
925 #define	MMP_SEQ_VALID_BIT	0x02
926 #define	MMP_FAIL_INT_VALID_BIT	0x04
927 
928 #define	MMP_VALID(ubp)		(ubp->ub_magic == UBERBLOCK_MAGIC && \
929 				    ubp->ub_mmp_magic == MMP_MAGIC)
930 #define	MMP_INTERVAL_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
931 				    MMP_INTERVAL_VALID_BIT))
932 #define	MMP_SEQ_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
933 				    MMP_SEQ_VALID_BIT))
934 #define	MMP_FAIL_INT_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
935 				    MMP_FAIL_INT_VALID_BIT))
936 
937 #define	MMP_INTERVAL(ubp)	((ubp->ub_mmp_config & 0x00000000FFFFFF00) \
938 				    >> 8)
939 #define	MMP_SEQ(ubp)		((ubp->ub_mmp_config & 0x0000FFFF00000000) \
940 				    >> 32)
941 #define	MMP_FAIL_INT(ubp)	((ubp->ub_mmp_config & 0xFFFF000000000000) \
942 				    >> 48)
943 
944 typedef struct uberblock {
945 	uint64_t	ub_magic;	/* UBERBLOCK_MAGIC		*/
946 	uint64_t	ub_version;	/* SPA_VERSION			*/
947 	uint64_t	ub_txg;		/* txg of last sync		*/
948 	uint64_t	ub_guid_sum;	/* sum of all vdev guids	*/
949 	uint64_t	ub_timestamp;	/* UTC time of last sync	*/
950 	blkptr_t	ub_rootbp;	/* MOS objset_phys_t		*/
951 	/* highest SPA_VERSION supported by software that wrote this txg */
952 	uint64_t	ub_software_version;
953 	/* Maybe missing in uberblocks we read, but always written */
954 	uint64_t	ub_mmp_magic;
955 	/*
956 	 * If ub_mmp_delay == 0 and ub_mmp_magic is valid, MMP is off.
957 	 * Otherwise, nanosec since last MMP write.
958 	 */
959 	uint64_t	ub_mmp_delay;
960 
961 	/*
962 	 * The ub_mmp_config contains the multihost write interval, multihost
963 	 * fail intervals, sequence number for sub-second granularity, and
964 	 * valid bit mask.  This layout is as follows:
965 	 *
966 	 *   64      56      48      40      32      24      16      8       0
967 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
968 	 * 0 | Fail Intervals|      Seq      |   Write Interval (ms) | VALID |
969 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
970 	 *
971 	 * This allows a write_interval of (2^24/1000)s, over 4.5 hours
972 	 *
973 	 * VALID Bits:
974 	 * - 0x01 - Write Interval (ms)
975 	 * - 0x02 - Sequence number exists
976 	 * - 0x04 - Fail Intervals
977 	 * - 0xf8 - Reserved
978 	 */
979 	uint64_t	ub_mmp_config;
980 
981 	/*
982 	 * ub_checkpoint_txg indicates two things about the current uberblock:
983 	 *
984 	 * 1] If it is not zero then this uberblock is a checkpoint. If it is
985 	 *    zero, then this uberblock is not a checkpoint.
986 	 *
987 	 * 2] On checkpointed uberblocks, the value of ub_checkpoint_txg is
988 	 *    the ub_txg that the uberblock had at the time we moved it to
989 	 *    the MOS config.
990 	 *
991 	 * The field is set when we checkpoint the uberblock and continues to
992 	 * hold that value even after we've rewound (unlike the ub_txg that
993 	 * is reset to a higher value).
994 	 *
995 	 * Besides checks used to determine whether we are reopening the
996 	 * pool from a checkpointed uberblock [see spa_ld_select_uberblock()],
997 	 * the value of the field is used to determine which ZIL blocks have
998 	 * been allocated according to the ms_sm when we are rewinding to a
999 	 * checkpoint. Specifically, if blk_birth > ub_checkpoint_txg, then
1000 	 * the ZIL block is not allocated [see uses of spa_min_claim_txg()].
1001 	 */
1002 	uint64_t	ub_checkpoint_txg;
1003 } uberblock_t;
1004 
1005 /*
1006  * Flags.
1007  */
1008 #define	DNODE_MUST_BE_ALLOCATED	1
1009 #define	DNODE_MUST_BE_FREE	2
1010 
1011 /*
1012  * Fixed constants.
1013  */
1014 #define	DNODE_SHIFT		9	/* 512 bytes */
1015 #define	DN_MIN_INDBLKSHIFT	12	/* 4k */
1016 #define	DN_MAX_INDBLKSHIFT	17	/* 128k */
1017 #define	DNODE_BLOCK_SHIFT	14	/* 16k */
1018 #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
1019 #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
1020 #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
1021 
1022 /*
1023  * Derived constants.
1024  */
1025 #define	DNODE_MIN_SIZE		(1 << DNODE_SHIFT)
1026 #define	DNODE_MAX_SIZE		(1 << DNODE_BLOCK_SHIFT)
1027 #define	DNODE_BLOCK_SIZE	(1 << DNODE_BLOCK_SHIFT)
1028 #define	DNODE_MIN_SLOTS		(DNODE_MIN_SIZE >> DNODE_SHIFT)
1029 #define	DNODE_MAX_SLOTS		(DNODE_MAX_SIZE >> DNODE_SHIFT)
1030 #define	DN_BONUS_SIZE(dnsize)	((dnsize) - DNODE_CORE_SIZE - \
1031 	(1 << SPA_BLKPTRSHIFT))
1032 #define	DN_SLOTS_TO_BONUSLEN(slots)	DN_BONUS_SIZE((slots) << DNODE_SHIFT)
1033 #define	DN_OLD_MAX_BONUSLEN		(DN_BONUS_SIZE(DNODE_MIN_SIZE))
1034 #define	DN_MAX_NBLKPTR		((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> \
1035 	SPA_BLKPTRSHIFT)
1036 #define	DN_MAX_OBJECT		(1ULL << DN_MAX_OBJECT_SHIFT)
1037 #define	DN_ZERO_BONUSLEN	(DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
1038 
1039 #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
1040 #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
1041 #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
1042 
1043 /* The +2 here is a cheesy way to round up */
1044 #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
1045 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
1046 
1047 #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
1048 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
1049 
1050 #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
1051 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
1052 
1053 #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
1054 
1055 /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
1056 #define	DNODE_FLAG_USED_BYTES		(1<<0)
1057 #define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
1058 
1059 /* Does dnode have a SA spill blkptr in bonus? */
1060 #define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
1061 
1062 typedef struct dnode_phys {
1063 	uint8_t dn_type;		/* dmu_object_type_t */
1064 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
1065 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
1066 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
1067 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
1068 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
1069 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
1070 	uint8_t dn_flags;		/* DNODE_FLAG_* */
1071 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
1072 	uint16_t dn_bonuslen;		/* length of dn_bonus */
1073 	uint8_t dn_extra_slots;		/* # of subsequent slots consumed */
1074 	uint8_t dn_pad2[3];
1075 
1076 	/* accounting is protected by dn_dirty_mtx */
1077 	uint64_t dn_maxblkid;		/* largest allocated block ID */
1078 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
1079 
1080 	uint64_t dn_pad3[4];
1081 
1082 	/*
1083 	 * The tail region is 448 bytes for a 512 byte dnode, and
1084 	 * correspondingly larger for larger dnode sizes. The spill
1085 	 * block pointer, when present, is always at the end of the tail
1086 	 * region. There are three ways this space may be used, using
1087 	 * a 512 byte dnode for this diagram:
1088 	 *
1089 	 * 0       64      128     192     256     320     384     448 (offset)
1090 	 * +---------------+---------------+---------------+-------+
1091 	 * | dn_blkptr[0]  | dn_blkptr[1]  | dn_blkptr[2]  | /     |
1092 	 * +---------------+---------------+---------------+-------+
1093 	 * | dn_blkptr[0]  | dn_bonus[0..319]                      |
1094 	 * +---------------+-----------------------+---------------+
1095 	 * | dn_blkptr[0]  | dn_bonus[0..191]      | dn_spill      |
1096 	 * +---------------+-----------------------+---------------+
1097 	 */
1098 	union {
1099 		blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
1100 		struct {
1101 			blkptr_t __dn_ignore1;
1102 			uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
1103 		};
1104 		struct {
1105 			blkptr_t __dn_ignore2;
1106 			uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
1107 			    sizeof (blkptr_t)];
1108 			blkptr_t dn_spill;
1109 		};
1110 	};
1111 } dnode_phys_t;
1112 
1113 #define	DN_SPILL_BLKPTR(dnp)	(blkptr_t *)((char *)(dnp) + \
1114 	(((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT))
1115 
1116 typedef enum dmu_object_byteswap {
1117 	DMU_BSWAP_UINT8,
1118 	DMU_BSWAP_UINT16,
1119 	DMU_BSWAP_UINT32,
1120 	DMU_BSWAP_UINT64,
1121 	DMU_BSWAP_ZAP,
1122 	DMU_BSWAP_DNODE,
1123 	DMU_BSWAP_OBJSET,
1124 	DMU_BSWAP_ZNODE,
1125 	DMU_BSWAP_OLDACL,
1126 	DMU_BSWAP_ACL,
1127 	/*
1128 	 * Allocating a new byteswap type number makes the on-disk format
1129 	 * incompatible with any other format that uses the same number.
1130 	 *
1131 	 * Data can usually be structured to work with one of the
1132 	 * DMU_BSWAP_UINT* or DMU_BSWAP_ZAP types.
1133 	 */
1134 	DMU_BSWAP_NUMFUNCS
1135 } dmu_object_byteswap_t;
1136 
1137 #define	DMU_OT_NEWTYPE 0x80
1138 #define	DMU_OT_METADATA 0x40
1139 #define	DMU_OT_BYTESWAP_MASK 0x3f
1140 
1141 /*
1142  * Defines a uint8_t object type. Object types specify if the data
1143  * in the object is metadata (boolean) and how to byteswap the data
1144  * (dmu_object_byteswap_t).
1145  */
1146 #define	DMU_OT(byteswap, metadata) \
1147 	(DMU_OT_NEWTYPE | \
1148 	((metadata) ? DMU_OT_METADATA : 0) | \
1149 	((byteswap) & DMU_OT_BYTESWAP_MASK))
1150 
1151 typedef enum dmu_object_type {
1152 	DMU_OT_NONE,
1153 	/* general: */
1154 	DMU_OT_OBJECT_DIRECTORY,	/* ZAP */
1155 	DMU_OT_OBJECT_ARRAY,		/* UINT64 */
1156 	DMU_OT_PACKED_NVLIST,		/* UINT8 (XDR by nvlist_pack/unpack) */
1157 	DMU_OT_PACKED_NVLIST_SIZE,	/* UINT64 */
1158 	DMU_OT_BPOBJ,			/* UINT64 */
1159 	DMU_OT_BPOBJ_HDR,		/* UINT64 */
1160 	/* spa: */
1161 	DMU_OT_SPACE_MAP_HEADER,	/* UINT64 */
1162 	DMU_OT_SPACE_MAP,		/* UINT64 */
1163 	/* zil: */
1164 	DMU_OT_INTENT_LOG,		/* UINT64 */
1165 	/* dmu: */
1166 	DMU_OT_DNODE,			/* DNODE */
1167 	DMU_OT_OBJSET,			/* OBJSET */
1168 	/* dsl: */
1169 	DMU_OT_DSL_DIR,			/* UINT64 */
1170 	DMU_OT_DSL_DIR_CHILD_MAP,	/* ZAP */
1171 	DMU_OT_DSL_DS_SNAP_MAP,		/* ZAP */
1172 	DMU_OT_DSL_PROPS,		/* ZAP */
1173 	DMU_OT_DSL_DATASET,		/* UINT64 */
1174 	/* zpl: */
1175 	DMU_OT_ZNODE,			/* ZNODE */
1176 	DMU_OT_OLDACL,			/* Old ACL */
1177 	DMU_OT_PLAIN_FILE_CONTENTS,	/* UINT8 */
1178 	DMU_OT_DIRECTORY_CONTENTS,	/* ZAP */
1179 	DMU_OT_MASTER_NODE,		/* ZAP */
1180 	DMU_OT_UNLINKED_SET,		/* ZAP */
1181 	/* zvol: */
1182 	DMU_OT_ZVOL,			/* UINT8 */
1183 	DMU_OT_ZVOL_PROP,		/* ZAP */
1184 	/* other; for testing only! */
1185 	DMU_OT_PLAIN_OTHER,		/* UINT8 */
1186 	DMU_OT_UINT64_OTHER,		/* UINT64 */
1187 	DMU_OT_ZAP_OTHER,		/* ZAP */
1188 	/* new object types: */
1189 	DMU_OT_ERROR_LOG,		/* ZAP */
1190 	DMU_OT_SPA_HISTORY,		/* UINT8 */
1191 	DMU_OT_SPA_HISTORY_OFFSETS,	/* spa_his_phys_t */
1192 	DMU_OT_POOL_PROPS,		/* ZAP */
1193 	DMU_OT_DSL_PERMS,		/* ZAP */
1194 	DMU_OT_ACL,			/* ACL */
1195 	DMU_OT_SYSACL,			/* SYSACL */
1196 	DMU_OT_FUID,			/* FUID table (Packed NVLIST UINT8) */
1197 	DMU_OT_FUID_SIZE,		/* FUID table size UINT64 */
1198 	DMU_OT_NEXT_CLONES,		/* ZAP */
1199 	DMU_OT_SCAN_QUEUE,		/* ZAP */
1200 	DMU_OT_USERGROUP_USED,		/* ZAP */
1201 	DMU_OT_USERGROUP_QUOTA,		/* ZAP */
1202 	DMU_OT_USERREFS,		/* ZAP */
1203 	DMU_OT_DDT_ZAP,			/* ZAP */
1204 	DMU_OT_DDT_STATS,		/* ZAP */
1205 	DMU_OT_SA,			/* System attr */
1206 	DMU_OT_SA_MASTER_NODE,		/* ZAP */
1207 	DMU_OT_SA_ATTR_REGISTRATION,	/* ZAP */
1208 	DMU_OT_SA_ATTR_LAYOUTS,		/* ZAP */
1209 	DMU_OT_SCAN_XLATE,		/* ZAP */
1210 	DMU_OT_DEDUP,			/* fake dedup BP from ddt_bp_create() */
1211 	DMU_OT_DEADLIST,		/* ZAP */
1212 	DMU_OT_DEADLIST_HDR,		/* UINT64 */
1213 	DMU_OT_DSL_CLONES,		/* ZAP */
1214 	DMU_OT_BPOBJ_SUBOBJ,		/* UINT64 */
1215 	DMU_OT_NUMTYPES,
1216 
1217 	/*
1218 	 * Names for valid types declared with DMU_OT().
1219 	 */
1220 	DMU_OTN_UINT8_DATA = DMU_OT(DMU_BSWAP_UINT8, B_FALSE),
1221 	DMU_OTN_UINT8_METADATA = DMU_OT(DMU_BSWAP_UINT8, B_TRUE),
1222 	DMU_OTN_UINT16_DATA = DMU_OT(DMU_BSWAP_UINT16, B_FALSE),
1223 	DMU_OTN_UINT16_METADATA = DMU_OT(DMU_BSWAP_UINT16, B_TRUE),
1224 	DMU_OTN_UINT32_DATA = DMU_OT(DMU_BSWAP_UINT32, B_FALSE),
1225 	DMU_OTN_UINT32_METADATA = DMU_OT(DMU_BSWAP_UINT32, B_TRUE),
1226 	DMU_OTN_UINT64_DATA = DMU_OT(DMU_BSWAP_UINT64, B_FALSE),
1227 	DMU_OTN_UINT64_METADATA = DMU_OT(DMU_BSWAP_UINT64, B_TRUE),
1228 	DMU_OTN_ZAP_DATA = DMU_OT(DMU_BSWAP_ZAP, B_FALSE),
1229 	DMU_OTN_ZAP_METADATA = DMU_OT(DMU_BSWAP_ZAP, B_TRUE)
1230 } dmu_object_type_t;
1231 
1232 typedef enum dmu_objset_type {
1233 	DMU_OST_NONE,
1234 	DMU_OST_META,
1235 	DMU_OST_ZFS,
1236 	DMU_OST_ZVOL,
1237 	DMU_OST_OTHER,			/* For testing only! */
1238 	DMU_OST_ANY,			/* Be careful! */
1239 	DMU_OST_NUMTYPES
1240 } dmu_objset_type_t;
1241 
1242 #define	ZAP_MAXVALUELEN	(1024 * 8)
1243 
1244 /*
1245  * header for all bonus and spill buffers.
1246  * The header has a fixed portion with a variable number
1247  * of "lengths" depending on the number of variable sized
1248  * attribues which are determined by the "layout number"
1249  */
1250 
1251 #define	SA_MAGIC	0x2F505A  /* ZFS SA */
1252 typedef struct sa_hdr_phys {
1253 	uint32_t sa_magic;
1254 	uint16_t sa_layout_info;  /* Encoded with hdrsize and layout number */
1255 	uint16_t sa_lengths[1];	/* optional sizes for variable length attrs */
1256 	/* ... Data follows the lengths.  */
1257 } sa_hdr_phys_t;
1258 
1259 /*
1260  * sa_hdr_phys -> sa_layout_info
1261  *
1262  * 16      10       0
1263  * +--------+-------+
1264  * | hdrsz  |layout |
1265  * +--------+-------+
1266  *
1267  * Bits 0-10 are the layout number
1268  * Bits 11-16 are the size of the header.
1269  * The hdrsize is the number * 8
1270  *
1271  * For example.
1272  * hdrsz of 1 ==> 8 byte header
1273  *          2 ==> 16 byte header
1274  *
1275  */
1276 
1277 #define	SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10)
1278 #define	SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 16, 3, 0)
1279 #define	SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \
1280 { \
1281 	BF32_SET_SB(x, 10, 6, 3, 0, size); \
1282 	BF32_SET(x, 0, 10, num); \
1283 }
1284 
1285 #define	SA_ATTR_BSWAP(x)	BF32_GET(x, 16, 8)
1286 #define	SA_ATTR_LENGTH(x)	BF32_GET(x, 24, 16)
1287 #define	SA_ATTR_NUM(x)		BF32_GET(x, 0, 16)
1288 #define	SA_ATTR_ENCODE(x, attr, length, bswap) \
1289 { \
1290 	BF64_SET(x, 24, 16, length); \
1291 	BF64_SET(x, 16, 8, bswap); \
1292 	BF64_SET(x, 0, 16, attr); \
1293 }
1294 
1295 #define	SA_MODE_OFFSET		0
1296 #define	SA_SIZE_OFFSET		8
1297 #define	SA_GEN_OFFSET		16
1298 #define	SA_UID_OFFSET		24
1299 #define	SA_GID_OFFSET		32
1300 #define	SA_PARENT_OFFSET	40
1301 #define	SA_SYMLINK_OFFSET	160
1302 
1303 #define	SA_REGISTRY	"REGISTRY"
1304 #define	SA_LAYOUTS	"LAYOUTS"
1305 
1306 typedef enum sa_bswap_type {
1307 	SA_UINT64_ARRAY,
1308 	SA_UINT32_ARRAY,
1309 	SA_UINT16_ARRAY,
1310 	SA_UINT8_ARRAY,
1311 	SA_ACL,
1312 } sa_bswap_type_t;
1313 
1314 typedef uint16_t	sa_attr_type_t;
1315 
1316 #define	ZIO_OBJSET_MAC_LEN		32
1317 
1318 /*
1319  * Intent log header - this on disk structure holds fields to manage
1320  * the log.  All fields are 64 bit to easily handle cross architectures.
1321  */
1322 typedef struct zil_header {
1323 	uint64_t zh_claim_txg;	/* txg in which log blocks were claimed */
1324 	uint64_t zh_replay_seq;	/* highest replayed sequence number */
1325 	blkptr_t zh_log;	/* log chain */
1326 	uint64_t zh_claim_seq;	/* highest claimed sequence number */
1327 	uint64_t zh_pad[5];
1328 } zil_header_t;
1329 
1330 #define	OBJSET_PHYS_SIZE_V2 2048
1331 #define	OBJSET_PHYS_SIZE_V3 4096
1332 
1333 typedef struct objset_phys {
1334 	dnode_phys_t os_meta_dnode;
1335 	zil_header_t os_zil_header;
1336 	uint64_t os_type;
1337 	uint64_t os_flags;
1338 	uint8_t os_portable_mac[ZIO_OBJSET_MAC_LEN];
1339 	uint8_t os_local_mac[ZIO_OBJSET_MAC_LEN];
1340 	char os_pad0[OBJSET_PHYS_SIZE_V2 - sizeof (dnode_phys_t)*3 -
1341 		sizeof (zil_header_t) - sizeof (uint64_t)*2 -
1342 		2*ZIO_OBJSET_MAC_LEN];
1343 	dnode_phys_t os_userused_dnode;
1344 	dnode_phys_t os_groupused_dnode;
1345 	dnode_phys_t os_projectused_dnode;
1346 	char os_pad1[OBJSET_PHYS_SIZE_V3 - OBJSET_PHYS_SIZE_V2 -
1347 	    sizeof (dnode_phys_t)];
1348 } objset_phys_t;
1349 
1350 typedef struct space_map_phys {
1351 	/* object number: not needed but kept for backwards compatibility */
1352 	uint64_t	smp_object;
1353 
1354 	/* length of the object in bytes */
1355 	uint64_t	smp_length;
1356 
1357 	/* space allocated from the map */
1358 	int64_t		smp_alloc;
1359 } space_map_phys_t;
1360 
1361 typedef enum {
1362 	SM_ALLOC,
1363 	SM_FREE
1364 } maptype_t;
1365 
1366 /* one-word entry constants */
1367 #define	SM_DEBUG_PREFIX	2
1368 #define	SM_OFFSET_BITS	47
1369 #define	SM_RUN_BITS	15
1370 
1371 /* two-word entry constants */
1372 #define	SM2_PREFIX	3
1373 #define	SM2_OFFSET_BITS	63
1374 #define	SM2_RUN_BITS	36
1375 
1376 #define	SM_PREFIX_DECODE(x)	BF64_DECODE(x, 62, 2)
1377 #define	SM_PREFIX_ENCODE(x)	BF64_ENCODE(x, 62, 2)
1378 
1379 #define	SM_DEBUG_ACTION_DECODE(x)	BF64_DECODE(x, 60, 2)
1380 #define	SM_DEBUG_ACTION_ENCODE(x)	BF64_ENCODE(x, 60, 2)
1381 #define	SM_DEBUG_SYNCPASS_DECODE(x)	BF64_DECODE(x, 50, 10)
1382 #define	SM_DEBUG_SYNCPASS_ENCODE(x)	BF64_ENCODE(x, 50, 10)
1383 #define	SM_DEBUG_TXG_DECODE(x)		BF64_DECODE(x, 0, 50)
1384 #define	SM_DEBUG_TXG_ENCODE(x)		BF64_ENCODE(x, 0, 50)
1385 
1386 #define	SM_OFFSET_DECODE(x)	BF64_DECODE(x, 16, SM_OFFSET_BITS)
1387 #define	SM_OFFSET_ENCODE(x)	BF64_ENCODE(x, 16, SM_OFFSET_BITS)
1388 #define	SM_TYPE_DECODE(x)	BF64_DECODE(x, 15, 1)
1389 #define	SM_TYPE_ENCODE(x)	BF64_ENCODE(x, 15, 1)
1390 #define	SM_RUN_DECODE(x)	(BF64_DECODE(x, 0, SM_RUN_BITS) + 1)
1391 #define	SM_RUN_ENCODE(x)	BF64_ENCODE((x) - 1, 0, SM_RUN_BITS)
1392 #define	SM_RUN_MAX		SM_RUN_DECODE(~0ULL)
1393 #define	SM_OFFSET_MAX		SM_OFFSET_DECODE(~0ULL)
1394 
1395 #define	SM2_RUN_DECODE(x)	(BF64_DECODE(x, 24, SM2_RUN_BITS) + 1)
1396 #define	SM2_RUN_ENCODE(x)	BF64_ENCODE((x) - 1, 24, SM2_RUN_BITS)
1397 #define	SM2_VDEV_DECODE(x)	BF64_DECODE(x, 0, 24)
1398 #define	SM2_VDEV_ENCODE(x)	BF64_ENCODE(x, 0, 24)
1399 #define	SM2_TYPE_DECODE(x)	BF64_DECODE(x, SM2_OFFSET_BITS, 1)
1400 #define	SM2_TYPE_ENCODE(x)	BF64_ENCODE(x, SM2_OFFSET_BITS, 1)
1401 #define	SM2_OFFSET_DECODE(x)	BF64_DECODE(x, 0, SM2_OFFSET_BITS)
1402 #define	SM2_OFFSET_ENCODE(x)	BF64_ENCODE(x, 0, SM2_OFFSET_BITS)
1403 #define	SM2_RUN_MAX		SM2_RUN_DECODE(~0ULL)
1404 #define	SM2_OFFSET_MAX		SM2_OFFSET_DECODE(~0ULL)
1405 
1406 typedef struct dsl_dir_phys {
1407 	uint64_t dd_creation_time; /* not actually used */
1408 	uint64_t dd_head_dataset_obj;
1409 	uint64_t dd_parent_obj;
1410 	uint64_t dd_clone_parent_obj;
1411 	uint64_t dd_child_dir_zapobj;
1412 	/*
1413 	 * how much space our children are accounting for; for leaf
1414 	 * datasets, == physical space used by fs + snaps
1415 	 */
1416 	uint64_t dd_used_bytes;
1417 	uint64_t dd_compressed_bytes;
1418 	uint64_t dd_uncompressed_bytes;
1419 	/* Administrative quota setting */
1420 	uint64_t dd_quota;
1421 	/* Administrative reservation setting */
1422 	uint64_t dd_reserved;
1423 	uint64_t dd_props_zapobj;
1424 	uint64_t dd_pad[7];
1425 	uint64_t dd_clones;
1426 	uint64_t dd_pad1[13]; /* pad out to 256 bytes for good measure */
1427 } dsl_dir_phys_t;
1428 
1429 typedef struct dsl_dataset_phys {
1430 	uint64_t ds_dir_obj;
1431 	uint64_t ds_prev_snap_obj;
1432 	uint64_t ds_prev_snap_txg;
1433 	uint64_t ds_next_snap_obj;
1434 	uint64_t ds_snapnames_zapobj;	/* zap obj of snaps; ==0 for snaps */
1435 	uint64_t ds_num_children;	/* clone/snap children; ==0 for head */
1436 	uint64_t ds_creation_time;	/* seconds since 1970 */
1437 	uint64_t ds_creation_txg;
1438 	uint64_t ds_deadlist_obj;
1439 	uint64_t ds_used_bytes;
1440 	uint64_t ds_compressed_bytes;
1441 	uint64_t ds_uncompressed_bytes;
1442 	uint64_t ds_unique_bytes;	/* only relevant to snapshots */
1443 	/*
1444 	 * The ds_fsid_guid is a 56-bit ID that can change to avoid
1445 	 * collisions.  The ds_guid is a 64-bit ID that will never
1446 	 * change, so there is a small probability that it will collide.
1447 	 */
1448 	uint64_t ds_fsid_guid;
1449 	uint64_t ds_guid;
1450 	uint64_t ds_flags;
1451 	blkptr_t ds_bp;
1452 	uint64_t ds_pad[8]; /* pad out to 320 bytes for good measure */
1453 } dsl_dataset_phys_t;
1454 
1455 typedef struct dsl_deadlist_phys {
1456 	uint64_t dl_used;
1457 	uint64_t dl_comp;
1458 	uint64_t dl_uncomp;
1459 	uint64_t dl_pad[37]; /* pad out to 320b for future expansion */
1460 } dsl_deadlist_phys_t;
1461 
1462 #define	BPOBJ_SIZE_V2	(6 * sizeof (uint64_t))
1463 
1464 typedef struct bpobj_phys {
1465 	uint64_t	bpo_num_blkptrs;
1466 	uint64_t	bpo_bytes;
1467 	uint64_t	bpo_comp;
1468 	uint64_t	bpo_uncomp;
1469 	uint64_t	bpo_subobjs;
1470 	uint64_t	bpo_num_subobjs;
1471 	uint64_t	bpo_num_freed;
1472 } bpobj_phys_t;
1473 
1474 /*
1475  * The names of zap entries in the DIRECTORY_OBJECT of the MOS.
1476  */
1477 #define	DMU_POOL_DIRECTORY_OBJECT	1
1478 #define	DMU_POOL_CONFIG			"config"
1479 #define	DMU_POOL_FEATURES_FOR_READ	"features_for_read"
1480 #define	DMU_POOL_FEATURES_FOR_WRITE	"features_for_write"
1481 #define	DMU_POOL_FEATURE_DESCRIPTIONS	"feature_descriptions"
1482 #define	DMU_POOL_ROOT_DATASET		"root_dataset"
1483 #define	DMU_POOL_SYNC_BPLIST		"sync_bplist"
1484 #define	DMU_POOL_ERRLOG_SCRUB		"errlog_scrub"
1485 #define	DMU_POOL_ERRLOG_LAST		"errlog_last"
1486 #define	DMU_POOL_SPARES			"spares"
1487 #define	DMU_POOL_DEFLATE		"deflate"
1488 #define	DMU_POOL_HISTORY		"history"
1489 #define	DMU_POOL_PROPS			"pool_props"
1490 #define	DMU_POOL_FREE_BPOBJ		"free_bpobj"
1491 #define	DMU_POOL_BPTREE_OBJ		"bptree_obj"
1492 #define	DMU_POOL_EMPTY_BPOBJ		"empty_bpobj"
1493 #define	DMU_POOL_TMP_USERREFS		"tmp_userrefs"
1494 #define	DMU_POOL_CHECKSUM_SALT		"org.illumos:checksum_salt"
1495 #define	DMU_POOL_REMOVING		"com.delphix:removing"
1496 #define	DMU_POOL_OBSOLETE_BPOBJ		"com.delphix:obsolete_bpobj"
1497 #define	DMU_POOL_CONDENSING_INDIRECT	"com.delphix:condensing_indirect"
1498 #define	DMU_POOL_ZPOOL_CHECKPOINT       "com.delphix:zpool_checkpoint"
1499 
1500 #define	ZAP_MAGIC 0x2F52AB2ABULL
1501 
1502 #define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_block_shift)
1503 
1504 #define	ZAP_MAXCD		(uint32_t)(-1)
1505 #define	ZAP_HASHBITS		28
1506 #define	MZAP_ENT_LEN		64
1507 #define	MZAP_ENT_MAX		\
1508 	((MZAP_MAX_BLKSZ - sizeof(mzap_phys_t)) / sizeof(mzap_ent_phys_t) + 1)
1509 #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
1510 #define	MZAP_MAX_BLKSZ		SPA_OLDMAXBLOCKSIZE
1511 
1512 typedef struct mzap_ent_phys {
1513 	uint64_t mze_value;
1514 	uint32_t mze_cd;
1515 	uint16_t mze_pad;	/* in case we want to chain them someday */
1516 	char mze_name[MZAP_NAME_LEN];
1517 } mzap_ent_phys_t;
1518 
1519 typedef struct mzap_phys {
1520 	uint64_t mz_block_type;	/* ZBT_MICRO */
1521 	uint64_t mz_salt;
1522 	uint64_t mz_normflags;
1523 	uint64_t mz_pad[5];
1524 	mzap_ent_phys_t mz_chunk[1];
1525 	/* actually variable size depending on block size */
1526 } mzap_phys_t;
1527 
1528 /*
1529  * The (fat) zap is stored in one object. It is an array of
1530  * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
1531  *
1532  * ptrtbl fits in first block:
1533  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
1534  *
1535  * ptrtbl too big for first block:
1536  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
1537  *
1538  */
1539 
1540 #define	ZBT_LEAF		((1ULL << 63) + 0)
1541 #define	ZBT_HEADER		((1ULL << 63) + 1)
1542 #define	ZBT_MICRO		((1ULL << 63) + 3)
1543 /* any other values are ptrtbl blocks */
1544 
1545 /*
1546  * the embedded pointer table takes up half a block:
1547  * block size / entry size (2^3) / 2
1548  */
1549 #define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
1550 
1551 /*
1552  * The embedded pointer table starts half-way through the block.  Since
1553  * the pointer table itself is half the block, it starts at (64-bit)
1554  * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
1555  */
1556 #define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
1557 	((uint64_t *)(zap)->zap_phys) \
1558 	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
1559 
1560 #define	ZAP_HASH_IDX(hash, n)	(((n) == 0) ? 0 : ((hash) >> (64 - (n))))
1561 
1562 /*
1563  * TAKE NOTE:
1564  * If zap_phys_t is modified, zap_byteswap() must be modified.
1565  */
1566 typedef struct zap_phys {
1567 	uint64_t zap_block_type;	/* ZBT_HEADER */
1568 	uint64_t zap_magic;		/* ZAP_MAGIC */
1569 
1570 	struct zap_table_phys {
1571 		uint64_t zt_blk;	/* starting block number */
1572 		uint64_t zt_numblks;	/* number of blocks */
1573 		uint64_t zt_shift;	/* bits to index it */
1574 		uint64_t zt_nextblk;	/* next (larger) copy start block */
1575 		uint64_t zt_blks_copied; /* number source blocks copied */
1576 	} zap_ptrtbl;
1577 
1578 	uint64_t zap_freeblk;		/* the next free block */
1579 	uint64_t zap_num_leafs;		/* number of leafs */
1580 	uint64_t zap_num_entries;	/* number of entries */
1581 	uint64_t zap_salt;		/* salt to stir into hash function */
1582 	uint64_t zap_normflags;		/* flags for u8_textprep_str() */
1583 	uint64_t zap_flags;		/* zap_flags_t */
1584 	/*
1585 	 * This structure is followed by padding, and then the embedded
1586 	 * pointer table.  The embedded pointer table takes up second
1587 	 * half of the block.  It is accessed using the
1588 	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
1589 	 */
1590 } zap_phys_t;
1591 
1592 typedef struct zap_table_phys zap_table_phys_t;
1593 
1594 struct spa;
1595 typedef struct fat_zap {
1596 	int zap_block_shift;			/* block size shift */
1597 	zap_phys_t *zap_phys;
1598 	const struct spa *zap_spa;
1599 	const dnode_phys_t *zap_dnode;
1600 } fat_zap_t;
1601 
1602 #define	ZAP_LEAF_MAGIC 0x2AB1EAF
1603 
1604 /* chunk size = 24 bytes */
1605 #define	ZAP_LEAF_CHUNKSIZE 24
1606 
1607 /*
1608  * The amount of space available for chunks is:
1609  * block size (1<<l->l_bs) - hash entry size (2) * number of hash
1610  * entries - header space (2*chunksize)
1611  */
1612 #define	ZAP_LEAF_NUMCHUNKS(l) \
1613 	(((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
1614 	ZAP_LEAF_CHUNKSIZE - 2)
1615 
1616 /*
1617  * The amount of space within the chunk available for the array is:
1618  * chunk size - space for type (1) - space for next pointer (2)
1619  */
1620 #define	ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
1621 
1622 #define	ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
1623 	(((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
1624 
1625 /*
1626  * Low water mark:  when there are only this many chunks free, start
1627  * growing the ptrtbl.  Ideally, this should be larger than a
1628  * "reasonably-sized" entry.  20 chunks is more than enough for the
1629  * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
1630  * while still being only around 3% for 16k blocks.
1631  */
1632 #define	ZAP_LEAF_LOW_WATER (20)
1633 
1634 /*
1635  * The leaf hash table has block size / 2^5 (32) number of entries,
1636  * which should be more than enough for the maximum number of entries,
1637  * which is less than block size / CHUNKSIZE (24) / minimum number of
1638  * chunks per entry (3).
1639  */
1640 #define	ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
1641 #define	ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
1642 
1643 /*
1644  * The chunks start immediately after the hash table.  The end of the
1645  * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
1646  * chunk_t.
1647  */
1648 #define	ZAP_LEAF_CHUNK(l, idx) \
1649 	((zap_leaf_chunk_t *)(void *) \
1650 	((l)->l_phys->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
1651 #define	ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
1652 
1653 #define	ZAP_LEAF_HASH(l, h) \
1654 	((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
1655 	((h) >> \
1656 	(64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
1657 #define	ZAP_LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[ZAP_LEAF_HASH(l, h)])
1658 
1659 typedef enum zap_chunk_type {
1660 	ZAP_CHUNK_FREE = 253,
1661 	ZAP_CHUNK_ENTRY = 252,
1662 	ZAP_CHUNK_ARRAY = 251,
1663 	ZAP_CHUNK_TYPE_MAX = 250
1664 } zap_chunk_type_t;
1665 
1666 /*
1667  * TAKE NOTE:
1668  * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
1669  */
1670 typedef struct zap_leaf_phys {
1671 	struct zap_leaf_header {
1672 		uint64_t lh_block_type;		/* ZBT_LEAF */
1673 		uint64_t lh_pad1;
1674 		uint64_t lh_prefix;		/* hash prefix of this leaf */
1675 		uint32_t lh_magic;		/* ZAP_LEAF_MAGIC */
1676 		uint16_t lh_nfree;		/* number free chunks */
1677 		uint16_t lh_nentries;		/* number of entries */
1678 		uint16_t lh_prefix_len;		/* num bits used to id this */
1679 
1680 /* above is accessable to zap, below is zap_leaf private */
1681 
1682 		uint16_t lh_freelist;		/* chunk head of free list */
1683 		uint8_t lh_pad2[12];
1684 	} l_hdr; /* 2 24-byte chunks */
1685 
1686 	/*
1687 	 * The header is followed by a hash table with
1688 	 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries.  The hash table is
1689 	 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
1690 	 * zap_leaf_chunk structures.  These structures are accessed
1691 	 * with the ZAP_LEAF_CHUNK() macro.
1692 	 */
1693 
1694 	uint16_t l_hash[1];
1695 } zap_leaf_phys_t;
1696 
1697 typedef union zap_leaf_chunk {
1698 	struct zap_leaf_entry {
1699 		uint8_t le_type; 		/* always ZAP_CHUNK_ENTRY */
1700 		uint8_t le_value_intlen;	/* size of ints */
1701 		uint16_t le_next;		/* next entry in hash chain */
1702 		uint16_t le_name_chunk;		/* first chunk of the name */
1703 		uint16_t le_name_numints;	/* bytes in name, incl null */
1704 		uint16_t le_value_chunk;	/* first chunk of the value */
1705 		uint16_t le_value_numints;	/* value length in ints */
1706 		uint32_t le_cd;			/* collision differentiator */
1707 		uint64_t le_hash;		/* hash value of the name */
1708 	} l_entry;
1709 	struct zap_leaf_array {
1710 		uint8_t la_type;		/* always ZAP_CHUNK_ARRAY */
1711 		uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
1712 		uint16_t la_next;		/* next blk or CHAIN_END */
1713 	} l_array;
1714 	struct zap_leaf_free {
1715 		uint8_t lf_type;		/* always ZAP_CHUNK_FREE */
1716 		uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
1717 		uint16_t lf_next;	/* next in free list, or CHAIN_END */
1718 	} l_free;
1719 } zap_leaf_chunk_t;
1720 
1721 typedef struct zap_leaf {
1722 	int l_bs;			/* block size shift */
1723 	zap_leaf_phys_t *l_phys;
1724 } zap_leaf_t;
1725 
1726 #define	ZAP_MAXNAMELEN 256
1727 #define	ZAP_MAXVALUELEN (1024 * 8)
1728 
1729 #define	ACE_READ_DATA		0x00000001	/* file: read data */
1730 #define	ACE_LIST_DIRECTORY	0x00000001	/* dir: list files */
1731 #define	ACE_WRITE_DATA		0x00000002	/* file: write data */
1732 #define	ACE_ADD_FILE		0x00000002	/* dir: create file */
1733 #define	ACE_APPEND_DATA		0x00000004	/* file: append data */
1734 #define	ACE_ADD_SUBDIRECTORY	0x00000004	/* dir: create subdir */
1735 #define	ACE_READ_NAMED_ATTRS	0x00000008	/* FILE_READ_EA */
1736 #define	ACE_WRITE_NAMED_ATTRS	0x00000010	/* FILE_WRITE_EA */
1737 #define	ACE_EXECUTE		0x00000020	/* file: execute */
1738 #define	ACE_TRAVERSE		0x00000020	/* dir: lookup name */
1739 #define	ACE_DELETE_CHILD	0x00000040	/* dir: unlink child */
1740 #define	ACE_READ_ATTRIBUTES	0x00000080	/* (all) stat, etc. */
1741 #define	ACE_WRITE_ATTRIBUTES	0x00000100	/* (all) utimes, etc. */
1742 #define	ACE_DELETE		0x00010000	/* (all) unlink self */
1743 #define	ACE_READ_ACL		0x00020000	/* (all) getsecattr */
1744 #define	ACE_WRITE_ACL		0x00040000	/* (all) setsecattr */
1745 #define	ACE_WRITE_OWNER		0x00080000	/* (all) chown */
1746 #define	ACE_SYNCHRONIZE		0x00100000	/* (all) */
1747 
1748 #define	ACE_FILE_INHERIT_ACE		0x0001
1749 #define	ACE_DIRECTORY_INHERIT_ACE	0x0002
1750 #define	ACE_NO_PROPAGATE_INHERIT_ACE	0x0004
1751 #define	ACE_INHERIT_ONLY_ACE		0x0008
1752 #define	ACE_SUCCESSFUL_ACCESS_ACE_FLAG	0x0010
1753 #define	ACE_FAILED_ACCESS_ACE_FLAG	0x0020
1754 #define	ACE_IDENTIFIER_GROUP		0x0040
1755 #define	ACE_INHERITED_ACE		0x0080
1756 #define	ACE_OWNER			0x1000
1757 #define	ACE_GROUP			0x2000
1758 #define	ACE_EVERYONE			0x4000
1759 
1760 #define	ACE_ACCESS_ALLOWED_ACE_TYPE	0x0000
1761 #define	ACE_ACCESS_DENIED_ACE_TYPE	0x0001
1762 #define	ACE_SYSTEM_AUDIT_ACE_TYPE	0x0002
1763 #define	ACE_SYSTEM_ALARM_ACE_TYPE	0x0003
1764 
1765 typedef struct zfs_ace_hdr {
1766 	uint16_t z_type;
1767 	uint16_t z_flags;
1768 	uint32_t z_access_mask;
1769 } zfs_ace_hdr_t;
1770 
1771 /*
1772  * Define special zfs pflags
1773  */
1774 #define	ZFS_XATTR		0x1		/* is an extended attribute */
1775 #define	ZFS_INHERIT_ACE		0x2		/* ace has inheritable ACEs */
1776 #define	ZFS_ACL_TRIVIAL		0x4		/* files ACL is trivial */
1777 #define	ZFS_ACL_OBJ_ACE		0x8		/* ACL has CMPLX Object ACE */
1778 #define	ZFS_ACL_PROTECTED	0x10		/* ACL protected */
1779 #define	ZFS_ACL_DEFAULTED	0x20		/* ACL should be defaulted */
1780 #define	ZFS_ACL_AUTO_INHERIT	0x40		/* ACL should be inherited */
1781 #define	ZFS_BONUS_SCANSTAMP	0x80		/* Scanstamp in bonus area */
1782 #define	ZFS_NO_EXECS_DENIED	0x100		/* exec was given to everyone */
1783 
1784 #define	ZFS_READONLY		0x0000000100000000ull
1785 #define	ZFS_HIDDEN		0x0000000200000000ull
1786 #define	ZFS_SYSTEM		0x0000000400000000ull
1787 #define	ZFS_ARCHIVE		0x0000000800000000ull
1788 #define	ZFS_IMMUTABLE		0x0000001000000000ull
1789 #define	ZFS_NOUNLINK		0x0000002000000000ull
1790 #define	ZFS_APPENDONLY		0x0000004000000000ull
1791 #define	ZFS_NODUMP		0x0000008000000000ull
1792 #define	ZFS_OPAQUE		0x0000010000000000ull
1793 #define	ZFS_AV_QUARANTINED	0x0000020000000000ull
1794 #define	ZFS_AV_MODIFIED		0x0000040000000000ull
1795 #define	ZFS_REPARSE		0x0000080000000000ull
1796 #define	ZFS_OFFLINE		0x0000100000000000ull
1797 #define	ZFS_SPARSE		0x0000200000000000ull
1798 
1799 #define	MASTER_NODE_OBJ	1
1800 
1801 /*
1802  * special attributes for master node.
1803  */
1804 
1805 #define	ZFS_FSID		"FSID"
1806 #define	ZFS_UNLINKED_SET	"DELETE_QUEUE"
1807 #define	ZFS_ROOT_OBJ		"ROOT"
1808 #define	ZPL_VERSION_OBJ		"VERSION"
1809 #define	ZFS_PROP_BLOCKPERPAGE	"BLOCKPERPAGE"
1810 #define	ZFS_PROP_NOGROWBLOCKS	"NOGROWBLOCKS"
1811 #define	ZFS_SA_ATTRS		"SA_ATTRS"
1812 
1813 #define	ZFS_FLAG_BLOCKPERPAGE	0x1
1814 #define	ZFS_FLAG_NOGROWBLOCKS	0x2
1815 
1816 /*
1817  * ZPL version - rev'd whenever an incompatible on-disk format change
1818  * occurs.  Independent of SPA/DMU/ZAP versioning.
1819  */
1820 
1821 #define	ZPL_VERSION		1ULL
1822 
1823 /*
1824  * The directory entry has the type (currently unused on Solaris) in the
1825  * top 4 bits, and the object number in the low 48 bits.  The "middle"
1826  * 12 bits are unused.
1827  */
1828 #define	ZFS_DIRENT_TYPE(de) BF64_GET(de, 60, 4)
1829 #define	ZFS_DIRENT_OBJ(de) BF64_GET(de, 0, 48)
1830 #define	ZFS_DIRENT_MAKE(type, obj) (((uint64_t)type << 60) | obj)
1831 
1832 typedef struct ace {
1833 	uid_t		a_who;		/* uid or gid */
1834 	uint32_t	a_access_mask;	/* read,write,... */
1835 	uint16_t	a_flags;	/* see below */
1836 	uint16_t	a_type;		/* allow or deny */
1837 } ace_t;
1838 
1839 #define ACE_SLOT_CNT	6
1840 
1841 typedef struct zfs_znode_acl {
1842 	uint64_t	z_acl_extern_obj;	  /* ext acl pieces */
1843 	uint32_t	z_acl_count;		  /* Number of ACEs */
1844 	uint16_t	z_acl_version;		  /* acl version */
1845 	uint16_t	z_acl_pad;		  /* pad */
1846 	ace_t		z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */
1847 } zfs_znode_acl_t;
1848 
1849 /*
1850  * This is the persistent portion of the znode.  It is stored
1851  * in the "bonus buffer" of the file.  Short symbolic links
1852  * are also stored in the bonus buffer.
1853  */
1854 typedef struct znode_phys {
1855 	uint64_t zp_atime[2];		/*  0 - last file access time */
1856 	uint64_t zp_mtime[2];		/* 16 - last file modification time */
1857 	uint64_t zp_ctime[2];		/* 32 - last file change time */
1858 	uint64_t zp_crtime[2];		/* 48 - creation time */
1859 	uint64_t zp_gen;		/* 64 - generation (txg of creation) */
1860 	uint64_t zp_mode;		/* 72 - file mode bits */
1861 	uint64_t zp_size;		/* 80 - size of file */
1862 	uint64_t zp_parent;		/* 88 - directory parent (`..') */
1863 	uint64_t zp_links;		/* 96 - number of links to file */
1864 	uint64_t zp_xattr;		/* 104 - DMU object for xattrs */
1865 	uint64_t zp_rdev;		/* 112 - dev_t for VBLK & VCHR files */
1866 	uint64_t zp_flags;		/* 120 - persistent flags */
1867 	uint64_t zp_uid;		/* 128 - file owner */
1868 	uint64_t zp_gid;		/* 136 - owning group */
1869 	uint64_t zp_pad[4];		/* 144 - future */
1870 	zfs_znode_acl_t zp_acl;		/* 176 - 263 ACL */
1871 	/*
1872 	 * Data may pad out any remaining bytes in the znode buffer, eg:
1873 	 *
1874 	 * |<---------------------- dnode_phys (512) ------------------------>|
1875 	 * |<-- dnode (192) --->|<----------- "bonus" buffer (320) ---------->|
1876 	 *			|<---- znode (264) ---->|<---- data (56) ---->|
1877 	 *
1878 	 * At present, we only use this space to store symbolic links.
1879 	 */
1880 } znode_phys_t;
1881 
1882 /*
1883  * In-core vdev representation.
1884  */
1885 struct vdev;
1886 struct spa;
1887 typedef int vdev_phys_read_t(struct vdev *, void *, off_t, void *, size_t);
1888 typedef int vdev_phys_write_t(struct vdev *, off_t, void *, size_t);
1889 typedef int vdev_read_t(struct vdev *, const blkptr_t *, void *, off_t, size_t);
1890 
1891 typedef STAILQ_HEAD(vdev_list, vdev) vdev_list_t;
1892 
1893 typedef struct vdev_indirect_mapping_entry_phys {
1894 	/*
1895 	 * Decode with DVA_MAPPING_* macros.
1896 	 * Contains:
1897 	 *   the source offset (low 63 bits)
1898 	 *   the one-bit "mark", used for garbage collection (by zdb)
1899 	 */
1900 	uint64_t vimep_src;
1901 
1902 	/*
1903 	 * Note: the DVA's asize is 24 bits, and can thus store ranges
1904 	 * up to 8GB.
1905 	 */
1906 	dva_t	vimep_dst;
1907 } vdev_indirect_mapping_entry_phys_t;
1908 
1909 #define	DVA_MAPPING_GET_SRC_OFFSET(vimep)	\
1910 	BF64_GET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0)
1911 #define	DVA_MAPPING_SET_SRC_OFFSET(vimep, x)	\
1912 	BF64_SET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0, x)
1913 
1914 /*
1915  * This is stored in the bonus buffer of the mapping object, see comment of
1916  * vdev_indirect_config for more details.
1917  */
1918 typedef struct vdev_indirect_mapping_phys {
1919 	uint64_t	vimp_max_offset;
1920 	uint64_t	vimp_bytes_mapped;
1921 	uint64_t	vimp_num_entries; /* number of v_i_m_entry_phys_t's */
1922 
1923 	/*
1924 	 * For each entry in the mapping object, this object contains an
1925 	 * entry representing the number of bytes of that mapping entry
1926 	 * that were no longer in use by the pool at the time this indirect
1927 	 * vdev was last condensed.
1928 	 */
1929 	uint64_t	vimp_counts_object;
1930 } vdev_indirect_mapping_phys_t;
1931 
1932 #define	VDEV_INDIRECT_MAPPING_SIZE_V0	(3 * sizeof (uint64_t))
1933 
1934 typedef struct vdev_indirect_mapping {
1935 	uint64_t	vim_object;
1936 	boolean_t	vim_havecounts;
1937 
1938 	/* vim_entries segment offset currently in memory. */
1939 	uint64_t	vim_entry_offset;
1940 	/* vim_entries segment size. */
1941 	size_t		vim_num_entries;
1942 
1943 	/* Needed by dnode_read() */
1944 	const void	*vim_spa;
1945 	dnode_phys_t	*vim_dn;
1946 
1947 	/*
1948 	 * An ordered array of mapping entries, sorted by source offset.
1949 	 * Note that vim_entries is needed during a removal (and contains
1950 	 * mappings that have been synced to disk so far) to handle frees
1951 	 * from the removing device.
1952 	 */
1953 	vdev_indirect_mapping_entry_phys_t *vim_entries;
1954 	objset_phys_t	*vim_objset;
1955 	vdev_indirect_mapping_phys_t	*vim_phys;
1956 } vdev_indirect_mapping_t;
1957 
1958 /*
1959  * On-disk indirect vdev state.
1960  *
1961  * An indirect vdev is described exclusively in the MOS config of a pool.
1962  * The config for an indirect vdev includes several fields, which are
1963  * accessed in memory by a vdev_indirect_config_t.
1964  */
1965 typedef struct vdev_indirect_config {
1966 	/*
1967 	 * Object (in MOS) which contains the indirect mapping. This object
1968 	 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
1969 	 * vimep_src. The bonus buffer for this object is a
1970 	 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
1971 	 * removal is initiated.
1972 	 *
1973 	 * Note that this object can be empty if none of the data on the vdev
1974 	 * has been copied yet.
1975 	 */
1976 	uint64_t	vic_mapping_object;
1977 
1978 	/*
1979 	 * Object (in MOS) which contains the birth times for the mapping
1980 	 * entries. This object contains an array of
1981 	 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
1982 	 * buffer for this object is a vdev_indirect_birth_phys_t. This object
1983 	 * is allocated when a vdev removal is initiated.
1984 	 *
1985 	 * Note that this object can be empty if none of the vdev has yet been
1986 	 * copied.
1987 	 */
1988 	uint64_t	vic_births_object;
1989 
1990 /*
1991  * This is the vdev ID which was removed previous to this vdev, or
1992  * UINT64_MAX if there are no previously removed vdevs.
1993  */
1994 	uint64_t	vic_prev_indirect_vdev;
1995 } vdev_indirect_config_t;
1996 
1997 typedef struct vdev {
1998 	STAILQ_ENTRY(vdev) v_childlink;	/* link in parent's child list */
1999 	STAILQ_ENTRY(vdev) v_alllink;	/* link in global vdev list */
2000 	vdev_list_t	v_children;	/* children of this vdev */
2001 	const char	*v_name;	/* vdev name */
2002 	uint64_t	v_guid;		/* vdev guid */
2003 	uint64_t	v_id;		/* index in parent */
2004 	uint64_t	v_psize;	/* physical device capacity */
2005 	int		v_ashift;	/* offset to block shift */
2006 	int		v_nparity;	/* # parity for raidz */
2007 	struct vdev	*v_top;		/* parent vdev */
2008 	size_t		v_nchildren;	/* # children */
2009 	vdev_state_t	v_state;	/* current state */
2010 	vdev_phys_read_t *v_phys_read;	/* read from raw leaf vdev */
2011 	vdev_phys_write_t *v_phys_write; /* write to raw leaf vdev */
2012 	vdev_read_t	*v_read;	/* read from vdev */
2013 	void		*v_priv;	/* data for read/write function */
2014 	boolean_t	v_islog;
2015 	struct spa	*v_spa;		/* link to spa */
2016 	/*
2017 	 * Values stored in the config for an indirect or removing vdev.
2018 	 */
2019 	vdev_indirect_config_t vdev_indirect_config;
2020 	vdev_indirect_mapping_t *v_mapping;
2021 } vdev_t;
2022 
2023 /*
2024  * In-core pool representation.
2025  */
2026 typedef STAILQ_HEAD(spa_list, spa) spa_list_t;
2027 
2028 typedef struct spa {
2029 	STAILQ_ENTRY(spa) spa_link;	/* link in global pool list */
2030 	char		*spa_name;	/* pool name */
2031 	uint64_t	spa_guid;	/* pool guid */
2032 	uint64_t	spa_txg;	/* most recent transaction */
2033 	struct uberblock *spa_uberblock;	/* best uberblock so far */
2034 	vdev_t		*spa_root_vdev;	/* toplevel vdev container */
2035 	objset_phys_t	*spa_mos;	/* MOS for this pool */
2036 	zio_cksum_salt_t spa_cksum_salt;	/* secret salt for cksum */
2037 	void		*spa_cksum_tmpls[ZIO_CHECKSUM_FUNCTIONS];
2038 	boolean_t	spa_with_log;	/* this pool has log */
2039 
2040 	struct uberblock spa_uberblock_master;	/* best uberblock so far */
2041 	objset_phys_t	spa_mos_master;		/* MOS for this pool */
2042 	struct uberblock spa_uberblock_checkpoint; /* checkpoint uberblock */
2043 	objset_phys_t	spa_mos_checkpoint;	/* Checkpoint MOS */
2044 	void		*spa_bootenv;		/* bootenv from pool label */
2045 } spa_t;
2046 
2047 /* IO related arguments. */
2048 typedef struct zio {
2049 	spa_t		*io_spa;
2050 	blkptr_t	*io_bp;
2051 	void		*io_data;
2052 	uint64_t	io_size;
2053 	uint64_t	io_offset;
2054 
2055 	/* Stuff for the vdev stack */
2056 	vdev_t		*io_vd;
2057 	void		*io_vsd;
2058 
2059 	int		io_error;
2060 } zio_t;
2061 
2062 extern void decode_embedded_bp_compressed(const blkptr_t *, void *);
2063 
2064 #endif /* _ZFSIMPL_H_ */
2065