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