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