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