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