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