zfsimpl.h revision 328866
1251881Speter/*-
2251881Speter * Copyright (c) 2002 McAfee, Inc.
3251881Speter * All rights reserved.
4251881Speter *
5251881Speter * This software was developed for the FreeBSD Project by Marshall
6251881Speter * Kirk McKusick and McAfee Research,, the Security Research Division of
7251881Speter * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
8251881Speter * part of the DARPA CHATS research program
9251881Speter *
10251881Speter * Redistribution and use in source and binary forms, with or without
11251881Speter * modification, are permitted provided that the following conditions
12251881Speter * are met:
13251881Speter * 1. Redistributions of source code must retain the above copyright
14251881Speter *    notice, this list of conditions and the following disclaimer.
15251881Speter * 2. Redistributions in binary form must reproduce the above copyright
16251881Speter *    notice, this list of conditions and the following disclaimer in the
17251881Speter *    documentation and/or other materials provided with the distribution.
18251881Speter *
19251881Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20251881Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21251881Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22251881Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23251881Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24251881Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25251881Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26251881Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27251881Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28251881Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29251881Speter * SUCH DAMAGE.
30251881Speter */
31251881Speter/*
32251881Speter * CDDL HEADER START
33251881Speter *
34251881Speter * The contents of this file are subject to the terms of the
35251881Speter * Common Development and Distribution License (the "License").
36251881Speter * You may not use this file except in compliance with the License.
37251881Speter *
38251881Speter * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
39251881Speter * or http://www.opensolaris.org/os/licensing.
40251881Speter * See the License for the specific language governing permissions
41251881Speter * and limitations under the License.
42251881Speter *
43251881Speter * When distributing Covered Code, include this CDDL HEADER in each
44251881Speter * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45251881Speter * If applicable, add the following below this CDDL HEADER, with the
46251881Speter * fields enclosed by brackets "[]" replaced with your own identifying
47251881Speter * information: Portions Copyright [yyyy] [name of copyright owner]
48251881Speter *
49251881Speter * CDDL HEADER END
50251881Speter */
51251881Speter/*
52251881Speter * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
53251881Speter * Use is subject to license terms.
54251881Speter */
55251881Speter/*
56251881Speter * Copyright 2013 by Saso Kiselkov. All rights reserved.
57251881Speter */
58251881Speter/*
59289180Speter * Copyright (c) 2013 by Delphix. All rights reserved.
60289180Speter */
61289180Speter
62289180Speter#define	MAXNAMELEN	256
63251881Speter
64251881Speter#define _NOTE(s)
65251881Speter
66251881Spetertypedef enum { B_FALSE, B_TRUE } boolean_t;
67251881Speter
68251881Speter/* CRC64 table */
69251881Speter#define	ZFS_CRC64_POLY	0xC96C5795D7870F42ULL	/* ECMA-182, reflected form */
70251881Speter
71251881Speter/*
72251881Speter * Macros for various sorts of alignment and rounding when the alignment
73251881Speter * is known to be a power of 2.
74251881Speter */
75251881Speter#define	P2ALIGN(x, align)		((x) & -(align))
76251881Speter#define	P2PHASE(x, align)		((x) & ((align) - 1))
77251881Speter#define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
78251881Speter#define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
79251881Speter#define	P2END(x, align)			(-(~(x) & -(align)))
80251881Speter#define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
81251881Speter#define	P2BOUNDARY(off, len, align)	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
82251881Speter
83251881Speter/*
84251881Speter * General-purpose 32-bit and 64-bit bitfield encodings.
85251881Speter */
86251881Speter#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
87251881Speter#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
88251881Speter#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
89251881Speter#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
90251881Speter
91251881Speter#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
92251881Speter#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
93251881Speter
94251881Speter#define	BF32_SET(x, low, len, val)	\
95251881Speter	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
96251881Speter#define	BF64_SET(x, low, len, val)	\
97251881Speter	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
98251881Speter
99251881Speter#define	BF32_GET_SB(x, low, len, shift, bias)	\
100251881Speter	((BF32_GET(x, low, len) + (bias)) << (shift))
101251881Speter#define	BF64_GET_SB(x, low, len, shift, bias)	\
102251881Speter	((BF64_GET(x, low, len) + (bias)) << (shift))
103251881Speter
104251881Speter#define	BF32_SET_SB(x, low, len, shift, bias, val)	\
105251881Speter	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
106251881Speter#define	BF64_SET_SB(x, low, len, shift, bias, val)	\
107251881Speter	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
108251881Speter
109251881Speter/*
110251881Speter * Macros to reverse byte order
111251881Speter */
112251881Speter#define	BSWAP_8(x)	((x) & 0xff)
113251881Speter#define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
114257936Speter#define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
115251881Speter#define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
116251881Speter
117251881Speter#define	SPA_MINBLOCKSHIFT	9
118289180Speter#define	SPA_OLDMAXBLOCKSHIFT	17
119289180Speter#define	SPA_MAXBLOCKSHIFT	24
120289180Speter#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
121289180Speter#define	SPA_OLDMAXBLOCKSIZE	(1ULL << SPA_OLDMAXBLOCKSHIFT)
122251881Speter#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
123251881Speter
124251881Speter/*
125251881Speter * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
126289180Speter * The ASIZE encoding should be at least 64 times larger (6 more bits)
127289180Speter * to support up to 4-way RAID-Z mirror mode with worst-case gang block
128251881Speter * overhead, three DVAs per bp, plus one more bit in case we do anything
129251881Speter * else that expands the ASIZE.
130251881Speter */
131251881Speter#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
132251881Speter#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
133251881Speter#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
134251881Speter
135251881Speter/*
136251881Speter * All SPA data is represented by 128-bit data virtual addresses (DVAs).
137251881Speter * The members of the dva_t should be considered opaque outside the SPA.
138251881Speter */
139251881Spetertypedef struct dva {
140251881Speter	uint64_t	dva_word[2];
141251881Speter} dva_t;
142251881Speter
143251881Speter/*
144251881Speter * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
145251881Speter */
146251881Spetertypedef struct zio_cksum {
147251881Speter	uint64_t	zc_word[4];
148251881Speter} zio_cksum_t;
149251881Speter
150251881Speter/*
151289180Speter * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
152251881Speter * secret and is suitable for use in MAC algorithms as the key.
153251881Speter */
154251881Spetertypedef struct zio_cksum_salt {
155251881Speter	uint8_t		zcs_bytes[32];
156251881Speter} zio_cksum_salt_t;
157289180Speter
158251881Speter/*
159251881Speter * Each block is described by its DVAs, time of birth, checksum, etc.
160251881Speter * The word-by-word, bit-by-bit layout of the blkptr is as follows:
161289180Speter *
162251881Speter *	64	56	48	40	32	24	16	8	0
163251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
164251881Speter * 0	|		vdev1		| GRID  |	  ASIZE		|
165251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
166251881Speter * 1	|G|			 offset1				|
167251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
168251881Speter * 2	|		vdev2		| GRID  |	  ASIZE		|
169251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
170251881Speter * 3	|G|			 offset2				|
171251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
172289180Speter * 4	|		vdev3		| GRID  |	  ASIZE		|
173289180Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
174289180Speter * 5	|G|			 offset3				|
175289180Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
176289180Speter * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
177251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
178251881Speter * 7	|			padding					|
179251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
180251881Speter * 8	|			padding					|
181251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
182251881Speter * 9	|			physical birth txg			|
183251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
184251881Speter * a	|			logical birth txg			|
185251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
186251881Speter * b	|			fill count				|
187251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
188251881Speter * c	|			checksum[0]				|
189251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
190251881Speter * d	|			checksum[1]				|
191289180Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
192251881Speter * e	|			checksum[2]				|
193251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
194251881Speter * f	|			checksum[3]				|
195251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
196289180Speter *
197289180Speter * Legend:
198289180Speter *
199251881Speter * vdev		virtual device ID
200251881Speter * offset	offset into virtual device
201251881Speter * LSIZE	logical size
202251881Speter * PSIZE	physical size (after compression)
203251881Speter * ASIZE	allocated size (including RAID-Z parity and gang block headers)
204289180Speter * GRID		RAID-Z layout information (reserved for future use)
205289180Speter * cksum	checksum function
206251881Speter * comp		compression function
207251881Speter * G		gang block indicator
208289180Speter * B		byteorder (endianness)
209289180Speter * D		dedup
210289180Speter * X		encryption (on version 30, which is not supported)
211289180Speter * E		blkptr_t contains embedded data (see below)
212289180Speter * lvl		level of indirection
213251881Speter * type		DMU object type
214289180Speter * phys birth	txg of block allocation; zero if same as logical birth txg
215289180Speter * log. birth	transaction group in which the block was logically born
216289180Speter * fill count	number of non-zero blocks under this bp
217251881Speter * checksum[4]	256-bit checksum of the data this bp describes
218289180Speter */
219289180Speter
220289180Speter/*
221251881Speter * "Embedded" blkptr_t's don't actually point to a block, instead they
222251881Speter * have a data payload embedded in the blkptr_t itself.  See the comment
223289180Speter * in blkptr.c for more details.
224251881Speter *
225251881Speter * The blkptr_t is laid out as follows:
226251881Speter *
227251881Speter *	64	56	48	40	32	24	16	8	0
228251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
229251881Speter * 0	|      payload                                                  |
230251881Speter * 1	|      payload                                                  |
231251881Speter * 2	|      payload                                                  |
232251881Speter * 3	|      payload                                                  |
233251881Speter * 4	|      payload                                                  |
234251881Speter * 5	|      payload                                                  |
235251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
236251881Speter * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
237251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
238289180Speter * 7	|      payload                                                  |
239251881Speter * 8	|      payload                                                  |
240251881Speter * 9	|      payload                                                  |
241289180Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
242289180Speter * a	|			logical birth txg			|
243251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
244289180Speter * b	|      payload                                                  |
245289180Speter * c	|      payload                                                  |
246251881Speter * d	|      payload                                                  |
247289180Speter * e	|      payload                                                  |
248289180Speter * f	|      payload                                                  |
249251881Speter *	+-------+-------+-------+-------+-------+-------+-------+-------+
250251881Speter *
251289180Speter * Legend:
252289180Speter *
253289180Speter * payload		contains the embedded data
254289180Speter * B (byteorder)	byteorder (endianness)
255289180Speter * D (dedup)		padding (set to zero)
256289180Speter * X			encryption (set to zero; see above)
257289180Speter * E (embedded)		set to one
258289180Speter * lvl			indirection level
259251881Speter * type			DMU object type
260289180Speter * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
261251881Speter * comp			compression function of payload
262251881Speter * PSIZE		size of payload after compression, in bytes
263289180Speter * LSIZE		logical size of payload, in bytes
264251881Speter *			note that 25 bits is enough to store the largest
265251881Speter *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
266251881Speter * log. birth		transaction group in which the block was logically born
267251881Speter *
268289180Speter * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
269289180Speter * bp's they are stored in units of SPA_MINBLOCKSHIFT.
270289180Speter * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
271289180Speter * The B, D, X, lvl, type, and comp fields are stored the same as with normal
272251881Speter * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
273251881Speter * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
274251881Speter * other macros, as they assert that they are only used on BP's of the correct
275289180Speter * "embedded-ness".
276251881Speter */
277289180Speter
278251881Speter#define	BPE_GET_ETYPE(bp)	\
279289180Speter	(ASSERT(BP_IS_EMBEDDED(bp)), \
280251881Speter	BF64_GET((bp)->blk_prop, 40, 8))
281289180Speter#define	BPE_SET_ETYPE(bp, t)	do { \
282251881Speter	ASSERT(BP_IS_EMBEDDED(bp)); \
283289180Speter	BF64_SET((bp)->blk_prop, 40, 8, t); \
284251881Speter_NOTE(CONSTCOND) } while (0)
285251881Speter
286289180Speter#define	BPE_GET_LSIZE(bp)	\
287251881Speter	(ASSERT(BP_IS_EMBEDDED(bp)), \
288289180Speter	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
289289180Speter#define	BPE_SET_LSIZE(bp, x)	do { \
290251881Speter	ASSERT(BP_IS_EMBEDDED(bp)); \
291289180Speter	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
292289180Speter_NOTE(CONSTCOND) } while (0)
293289180Speter
294289180Speter#define	BPE_GET_PSIZE(bp)	\
295289180Speter	(ASSERT(BP_IS_EMBEDDED(bp)), \
296289180Speter	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
297251881Speter#define	BPE_SET_PSIZE(bp, x)	do { \
298289180Speter	ASSERT(BP_IS_EMBEDDED(bp)); \
299289180Speter	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
300289180Speter_NOTE(CONSTCOND) } while (0)
301289180Speter
302289180Spetertypedef enum bp_embedded_type {
303289180Speter	BP_EMBEDDED_TYPE_DATA,
304289180Speter	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
305289180Speter	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
306289180Speter} bp_embedded_type_t;
307289180Speter
308289180Speter#define	BPE_NUM_WORDS 14
309289180Speter#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
310289180Speter#define	BPE_IS_PAYLOADWORD(bp, wp) \
311289180Speter	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
312289180Speter
313289180Speter#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
314289180Speter#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
315289180Speter
316289180Spetertypedef struct blkptr {
317289180Speter	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
318289180Speter	uint64_t	blk_prop;	/* size, compression, type, etc	    */
319289180Speter	uint64_t	blk_pad[2];	/* Extra space for the future	    */
320289180Speter	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
321251881Speter	uint64_t	blk_birth;	/* transaction group at birth	    */
322	uint64_t	blk_fill;	/* fill count			    */
323	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
324} blkptr_t;
325
326/*
327 * Macros to get and set fields in a bp or DVA.
328 */
329#define	DVA_GET_ASIZE(dva)	\
330	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
331#define	DVA_SET_ASIZE(dva, x)	\
332	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
333	SPA_MINBLOCKSHIFT, 0, x)
334
335#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
336#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
337
338#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
339#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
340
341#define	DVA_GET_OFFSET(dva)	\
342	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
343#define	DVA_SET_OFFSET(dva, x)	\
344	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
345
346#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
347#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
348
349#define	BP_GET_LSIZE(bp)	\
350	(BP_IS_EMBEDDED(bp) ?	\
351	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
352	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
353#define	BP_SET_LSIZE(bp, x)	do { \
354	ASSERT(!BP_IS_EMBEDDED(bp)); \
355	BF64_SET_SB((bp)->blk_prop, \
356	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
357_NOTE(CONSTCOND) } while (0)
358
359#define	BP_GET_PSIZE(bp)	\
360	BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)
361#define	BP_SET_PSIZE(bp, x)	\
362	BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x)
363
364#define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 7)
365#define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 7, x)
366
367#define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
368#define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
369
370#define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
371#define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
372
373#define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
374#define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
375
376#define	BP_IS_EMBEDDED(bp)	BF64_GET((bp)->blk_prop, 39, 1)
377
378#define	BP_GET_DEDUP(bp)	BF64_GET((bp)->blk_prop, 62, 1)
379#define	BP_SET_DEDUP(bp, x)	BF64_SET((bp)->blk_prop, 62, 1, x)
380
381#define	BP_GET_BYTEORDER(bp)	BF64_GET((bp)->blk_prop, 63, 1)
382#define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
383
384#define	BP_PHYSICAL_BIRTH(bp)		\
385	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
386
387#define	BP_GET_ASIZE(bp)	\
388	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
389		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
390
391#define	BP_GET_UCSIZE(bp) \
392	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
393	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
394
395#define	BP_GET_NDVAS(bp)	\
396	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
397	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
398	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
399
400#define	DVA_EQUAL(dva1, dva2)	\
401	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
402	(dva1)->dva_word[0] == (dva2)->dva_word[0])
403
404#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
405	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
406	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
407	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
408	((zc1).zc_word[3] - (zc2).zc_word[3])))
409
410
411#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
412
413#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
414{						\
415	(zcp)->zc_word[0] = w0;			\
416	(zcp)->zc_word[1] = w1;			\
417	(zcp)->zc_word[2] = w2;			\
418	(zcp)->zc_word[3] = w3;			\
419}
420
421#define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
422#define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
423#define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&  \
424	(dva)->dva_word[1] == 0ULL)
425#define	BP_IS_HOLE(bp)		DVA_IS_EMPTY(BP_IDENTITY(bp))
426#define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
427
428#define	BP_ZERO(bp)				\
429{						\
430	(bp)->blk_dva[0].dva_word[0] = 0;	\
431	(bp)->blk_dva[0].dva_word[1] = 0;	\
432	(bp)->blk_dva[1].dva_word[0] = 0;	\
433	(bp)->blk_dva[1].dva_word[1] = 0;	\
434	(bp)->blk_dva[2].dva_word[0] = 0;	\
435	(bp)->blk_dva[2].dva_word[1] = 0;	\
436	(bp)->blk_prop = 0;			\
437	(bp)->blk_pad[0] = 0;			\
438	(bp)->blk_pad[1] = 0;			\
439	(bp)->blk_phys_birth = 0;		\
440	(bp)->blk_birth = 0;			\
441	(bp)->blk_fill = 0;			\
442	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
443}
444
445#define	BPE_NUM_WORDS 14
446#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
447#define	BPE_IS_PAYLOADWORD(bp, wp) \
448	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
449
450/*
451 * Embedded checksum
452 */
453#define	ZEC_MAGIC	0x210da7ab10c7a11ULL
454
455typedef struct zio_eck {
456	uint64_t	zec_magic;	/* for validation, endianness	*/
457	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
458} zio_eck_t;
459
460/*
461 * Gang block headers are self-checksumming and contain an array
462 * of block pointers.
463 */
464#define	SPA_GANGBLOCKSIZE	SPA_MINBLOCKSIZE
465#define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
466	sizeof (zio_eck_t)) / sizeof (blkptr_t))
467#define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
468	sizeof (zio_eck_t) - \
469	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
470	sizeof (uint64_t))
471
472typedef struct zio_gbh {
473	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
474	uint64_t		zg_filler[SPA_GBH_FILLER];
475	zio_eck_t		zg_tail;
476} zio_gbh_phys_t;
477
478#define	VDEV_RAIDZ_MAXPARITY	3
479
480#define	VDEV_PAD_SIZE		(8 << 10)
481/* 2 padding areas (vl_pad1 and vl_pad2) to skip */
482#define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
483#define	VDEV_PHYS_SIZE		(112 << 10)
484#define	VDEV_UBERBLOCK_RING	(128 << 10)
485
486#define	VDEV_UBERBLOCK_SHIFT(vd)	\
487	MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT)
488#define	VDEV_UBERBLOCK_COUNT(vd)	\
489	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
490#define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
491	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
492#define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
493
494typedef struct vdev_phys {
495	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
496	zio_eck_t	vp_zbt;
497} vdev_phys_t;
498
499typedef struct vdev_label {
500	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K  */
501	char		vl_pad2[VDEV_PAD_SIZE];			/*  8K  */
502	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
503	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
504} vdev_label_t;							/* 256K total */
505
506/*
507 * vdev_dirty() flags
508 */
509#define	VDD_METASLAB	0x01
510#define	VDD_DTL		0x02
511
512/*
513 * Size and offset of embedded boot loader region on each label.
514 * The total size of the first two labels plus the boot area is 4MB.
515 */
516#define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
517#define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M	*/
518
519/*
520 * Size of label regions at the start and end of each leaf device.
521 */
522#define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
523#define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
524#define	VDEV_LABELS		4
525
526enum zio_checksum {
527	ZIO_CHECKSUM_INHERIT = 0,
528	ZIO_CHECKSUM_ON,
529	ZIO_CHECKSUM_OFF,
530	ZIO_CHECKSUM_LABEL,
531	ZIO_CHECKSUM_GANG_HEADER,
532	ZIO_CHECKSUM_ZILOG,
533	ZIO_CHECKSUM_FLETCHER_2,
534	ZIO_CHECKSUM_FLETCHER_4,
535	ZIO_CHECKSUM_SHA256,
536	ZIO_CHECKSUM_ZILOG2,
537	ZIO_CHECKSUM_NOPARITY,
538	ZIO_CHECKSUM_SHA512,
539	ZIO_CHECKSUM_SKEIN,
540	ZIO_CHECKSUM_EDONR,
541	ZIO_CHECKSUM_FUNCTIONS
542};
543
544#define	ZIO_CHECKSUM_ON_VALUE	ZIO_CHECKSUM_FLETCHER_4
545#define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
546
547enum zio_compress {
548	ZIO_COMPRESS_INHERIT = 0,
549	ZIO_COMPRESS_ON,
550	ZIO_COMPRESS_OFF,
551	ZIO_COMPRESS_LZJB,
552	ZIO_COMPRESS_EMPTY,
553	ZIO_COMPRESS_GZIP_1,
554	ZIO_COMPRESS_GZIP_2,
555	ZIO_COMPRESS_GZIP_3,
556	ZIO_COMPRESS_GZIP_4,
557	ZIO_COMPRESS_GZIP_5,
558	ZIO_COMPRESS_GZIP_6,
559	ZIO_COMPRESS_GZIP_7,
560	ZIO_COMPRESS_GZIP_8,
561	ZIO_COMPRESS_GZIP_9,
562	ZIO_COMPRESS_ZLE,
563	ZIO_COMPRESS_LZ4,
564	ZIO_COMPRESS_FUNCTIONS
565};
566
567#define	ZIO_COMPRESS_ON_VALUE	ZIO_COMPRESS_LZJB
568#define	ZIO_COMPRESS_DEFAULT	ZIO_COMPRESS_OFF
569
570/* nvlist pack encoding */
571#define	NV_ENCODE_NATIVE	0
572#define	NV_ENCODE_XDR		1
573
574typedef enum {
575	DATA_TYPE_UNKNOWN = 0,
576	DATA_TYPE_BOOLEAN,
577	DATA_TYPE_BYTE,
578	DATA_TYPE_INT16,
579	DATA_TYPE_UINT16,
580	DATA_TYPE_INT32,
581	DATA_TYPE_UINT32,
582	DATA_TYPE_INT64,
583	DATA_TYPE_UINT64,
584	DATA_TYPE_STRING,
585	DATA_TYPE_BYTE_ARRAY,
586	DATA_TYPE_INT16_ARRAY,
587	DATA_TYPE_UINT16_ARRAY,
588	DATA_TYPE_INT32_ARRAY,
589	DATA_TYPE_UINT32_ARRAY,
590	DATA_TYPE_INT64_ARRAY,
591	DATA_TYPE_UINT64_ARRAY,
592	DATA_TYPE_STRING_ARRAY,
593	DATA_TYPE_HRTIME,
594	DATA_TYPE_NVLIST,
595	DATA_TYPE_NVLIST_ARRAY,
596	DATA_TYPE_BOOLEAN_VALUE,
597	DATA_TYPE_INT8,
598	DATA_TYPE_UINT8,
599	DATA_TYPE_BOOLEAN_ARRAY,
600	DATA_TYPE_INT8_ARRAY,
601	DATA_TYPE_UINT8_ARRAY
602} data_type_t;
603
604/*
605 * On-disk version number.
606 */
607#define	SPA_VERSION_1			1ULL
608#define	SPA_VERSION_2			2ULL
609#define	SPA_VERSION_3			3ULL
610#define	SPA_VERSION_4			4ULL
611#define	SPA_VERSION_5			5ULL
612#define	SPA_VERSION_6			6ULL
613#define	SPA_VERSION_7			7ULL
614#define	SPA_VERSION_8			8ULL
615#define	SPA_VERSION_9			9ULL
616#define	SPA_VERSION_10			10ULL
617#define	SPA_VERSION_11			11ULL
618#define	SPA_VERSION_12			12ULL
619#define	SPA_VERSION_13			13ULL
620#define	SPA_VERSION_14			14ULL
621#define	SPA_VERSION_15			15ULL
622#define	SPA_VERSION_16			16ULL
623#define	SPA_VERSION_17			17ULL
624#define	SPA_VERSION_18			18ULL
625#define	SPA_VERSION_19			19ULL
626#define	SPA_VERSION_20			20ULL
627#define	SPA_VERSION_21			21ULL
628#define	SPA_VERSION_22			22ULL
629#define	SPA_VERSION_23			23ULL
630#define	SPA_VERSION_24			24ULL
631#define	SPA_VERSION_25			25ULL
632#define	SPA_VERSION_26			26ULL
633#define	SPA_VERSION_27			27ULL
634#define	SPA_VERSION_28			28ULL
635#define	SPA_VERSION_5000		5000ULL
636
637/*
638 * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
639 * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
640 * and do the appropriate changes.  Also bump the version number in
641 * usr/src/grub/capability.
642 */
643#define	SPA_VERSION			SPA_VERSION_5000
644#define	SPA_VERSION_STRING		"5000"
645
646/*
647 * Symbolic names for the changes that caused a SPA_VERSION switch.
648 * Used in the code when checking for presence or absence of a feature.
649 * Feel free to define multiple symbolic names for each version if there
650 * were multiple changes to on-disk structures during that version.
651 *
652 * NOTE: When checking the current SPA_VERSION in your code, be sure
653 *       to use spa_version() since it reports the version of the
654 *       last synced uberblock.  Checking the in-flight version can
655 *       be dangerous in some cases.
656 */
657#define	SPA_VERSION_INITIAL		SPA_VERSION_1
658#define	SPA_VERSION_DITTO_BLOCKS	SPA_VERSION_2
659#define	SPA_VERSION_SPARES		SPA_VERSION_3
660#define	SPA_VERSION_RAID6		SPA_VERSION_3
661#define	SPA_VERSION_BPLIST_ACCOUNT	SPA_VERSION_3
662#define	SPA_VERSION_RAIDZ_DEFLATE	SPA_VERSION_3
663#define	SPA_VERSION_DNODE_BYTES		SPA_VERSION_3
664#define	SPA_VERSION_ZPOOL_HISTORY	SPA_VERSION_4
665#define	SPA_VERSION_GZIP_COMPRESSION	SPA_VERSION_5
666#define	SPA_VERSION_BOOTFS		SPA_VERSION_6
667#define	SPA_VERSION_SLOGS		SPA_VERSION_7
668#define	SPA_VERSION_DELEGATED_PERMS	SPA_VERSION_8
669#define	SPA_VERSION_FUID		SPA_VERSION_9
670#define	SPA_VERSION_REFRESERVATION	SPA_VERSION_9
671#define	SPA_VERSION_REFQUOTA		SPA_VERSION_9
672#define	SPA_VERSION_UNIQUE_ACCURATE	SPA_VERSION_9
673#define	SPA_VERSION_L2CACHE		SPA_VERSION_10
674#define	SPA_VERSION_NEXT_CLONES		SPA_VERSION_11
675#define	SPA_VERSION_ORIGIN		SPA_VERSION_11
676#define	SPA_VERSION_DSL_SCRUB		SPA_VERSION_11
677#define	SPA_VERSION_SNAP_PROPS		SPA_VERSION_12
678#define	SPA_VERSION_USED_BREAKDOWN	SPA_VERSION_13
679#define	SPA_VERSION_PASSTHROUGH_X	SPA_VERSION_14
680#define SPA_VERSION_USERSPACE		SPA_VERSION_15
681#define	SPA_VERSION_STMF_PROP		SPA_VERSION_16
682#define	SPA_VERSION_RAIDZ3		SPA_VERSION_17
683#define	SPA_VERSION_USERREFS		SPA_VERSION_18
684#define	SPA_VERSION_HOLES		SPA_VERSION_19
685#define	SPA_VERSION_ZLE_COMPRESSION	SPA_VERSION_20
686#define	SPA_VERSION_DEDUP		SPA_VERSION_21
687#define	SPA_VERSION_RECVD_PROPS		SPA_VERSION_22
688#define	SPA_VERSION_SLIM_ZIL		SPA_VERSION_23
689#define	SPA_VERSION_SA			SPA_VERSION_24
690#define	SPA_VERSION_SCAN		SPA_VERSION_25
691#define	SPA_VERSION_DIR_CLONES		SPA_VERSION_26
692#define	SPA_VERSION_DEADLISTS		SPA_VERSION_26
693#define	SPA_VERSION_FAST_SNAP		SPA_VERSION_27
694#define	SPA_VERSION_MULTI_REPLACE	SPA_VERSION_28
695#define	SPA_VERSION_BEFORE_FEATURES	SPA_VERSION_28
696#define	SPA_VERSION_FEATURES		SPA_VERSION_5000
697
698#define	SPA_VERSION_IS_SUPPORTED(v) \
699	(((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
700	((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
701
702/*
703 * The following are configuration names used in the nvlist describing a pool's
704 * configuration.
705 */
706#define	ZPOOL_CONFIG_VERSION		"version"
707#define	ZPOOL_CONFIG_POOL_NAME		"name"
708#define	ZPOOL_CONFIG_POOL_STATE		"state"
709#define	ZPOOL_CONFIG_POOL_TXG		"txg"
710#define	ZPOOL_CONFIG_POOL_GUID		"pool_guid"
711#define	ZPOOL_CONFIG_CREATE_TXG		"create_txg"
712#define	ZPOOL_CONFIG_TOP_GUID		"top_guid"
713#define	ZPOOL_CONFIG_VDEV_TREE		"vdev_tree"
714#define	ZPOOL_CONFIG_TYPE		"type"
715#define	ZPOOL_CONFIG_CHILDREN		"children"
716#define	ZPOOL_CONFIG_ID			"id"
717#define	ZPOOL_CONFIG_GUID		"guid"
718#define	ZPOOL_CONFIG_PATH		"path"
719#define	ZPOOL_CONFIG_DEVID		"devid"
720#define	ZPOOL_CONFIG_METASLAB_ARRAY	"metaslab_array"
721#define	ZPOOL_CONFIG_METASLAB_SHIFT	"metaslab_shift"
722#define	ZPOOL_CONFIG_ASHIFT		"ashift"
723#define	ZPOOL_CONFIG_ASIZE		"asize"
724#define	ZPOOL_CONFIG_DTL		"DTL"
725#define	ZPOOL_CONFIG_STATS		"stats"
726#define	ZPOOL_CONFIG_WHOLE_DISK		"whole_disk"
727#define	ZPOOL_CONFIG_ERRCOUNT		"error_count"
728#define	ZPOOL_CONFIG_NOT_PRESENT	"not_present"
729#define	ZPOOL_CONFIG_SPARES		"spares"
730#define	ZPOOL_CONFIG_IS_SPARE		"is_spare"
731#define	ZPOOL_CONFIG_NPARITY		"nparity"
732#define	ZPOOL_CONFIG_HOSTID		"hostid"
733#define	ZPOOL_CONFIG_HOSTNAME		"hostname"
734#define	ZPOOL_CONFIG_IS_LOG		"is_log"
735#define	ZPOOL_CONFIG_TIMESTAMP		"timestamp" /* not stored on disk */
736#define	ZPOOL_CONFIG_FEATURES_FOR_READ	"features_for_read"
737
738/*
739 * The persistent vdev state is stored as separate values rather than a single
740 * 'vdev_state' entry.  This is because a device can be in multiple states, such
741 * as offline and degraded.
742 */
743#define	ZPOOL_CONFIG_OFFLINE            "offline"
744#define	ZPOOL_CONFIG_FAULTED            "faulted"
745#define	ZPOOL_CONFIG_DEGRADED           "degraded"
746#define	ZPOOL_CONFIG_REMOVED            "removed"
747#define	ZPOOL_CONFIG_FRU		"fru"
748#define	ZPOOL_CONFIG_AUX_STATE		"aux_state"
749
750#define	VDEV_TYPE_ROOT			"root"
751#define	VDEV_TYPE_MIRROR		"mirror"
752#define	VDEV_TYPE_REPLACING		"replacing"
753#define	VDEV_TYPE_RAIDZ			"raidz"
754#define	VDEV_TYPE_DISK			"disk"
755#define	VDEV_TYPE_FILE			"file"
756#define	VDEV_TYPE_MISSING		"missing"
757#define	VDEV_TYPE_HOLE			"hole"
758#define	VDEV_TYPE_SPARE			"spare"
759#define	VDEV_TYPE_LOG			"log"
760#define	VDEV_TYPE_L2CACHE		"l2cache"
761
762/*
763 * This is needed in userland to report the minimum necessary device size.
764 */
765#define	SPA_MINDEVSIZE		(64ULL << 20)
766
767/*
768 * The location of the pool configuration repository, shared between kernel and
769 * userland.
770 */
771#define	ZPOOL_CACHE		"/boot/zfs/zpool.cache"
772
773/*
774 * vdev states are ordered from least to most healthy.
775 * A vdev that's CANT_OPEN or below is considered unusable.
776 */
777typedef enum vdev_state {
778	VDEV_STATE_UNKNOWN = 0,	/* Uninitialized vdev			*/
779	VDEV_STATE_CLOSED,	/* Not currently open			*/
780	VDEV_STATE_OFFLINE,	/* Not allowed to open			*/
781	VDEV_STATE_REMOVED,	/* Explicitly removed from system	*/
782	VDEV_STATE_CANT_OPEN,	/* Tried to open, but failed		*/
783	VDEV_STATE_FAULTED,	/* External request to fault device	*/
784	VDEV_STATE_DEGRADED,	/* Replicated vdev with unhealthy kids	*/
785	VDEV_STATE_HEALTHY	/* Presumed good			*/
786} vdev_state_t;
787
788/*
789 * vdev aux states.  When a vdev is in the CANT_OPEN state, the aux field
790 * of the vdev stats structure uses these constants to distinguish why.
791 */
792typedef enum vdev_aux {
793	VDEV_AUX_NONE,		/* no error				*/
794	VDEV_AUX_OPEN_FAILED,	/* ldi_open_*() or vn_open() failed	*/
795	VDEV_AUX_CORRUPT_DATA,	/* bad label or disk contents		*/
796	VDEV_AUX_NO_REPLICAS,	/* insufficient number of replicas	*/
797	VDEV_AUX_BAD_GUID_SUM,	/* vdev guid sum doesn't match		*/
798	VDEV_AUX_TOO_SMALL,	/* vdev size is too small		*/
799	VDEV_AUX_BAD_LABEL,	/* the label is OK but invalid		*/
800	VDEV_AUX_VERSION_NEWER,	/* on-disk version is too new		*/
801	VDEV_AUX_VERSION_OLDER,	/* on-disk version is too old		*/
802	VDEV_AUX_SPARED		/* hot spare used in another pool	*/
803} vdev_aux_t;
804
805/*
806 * pool state.  The following states are written to disk as part of the normal
807 * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE.  The remaining states are
808 * software abstractions used at various levels to communicate pool state.
809 */
810typedef enum pool_state {
811	POOL_STATE_ACTIVE = 0,		/* In active use		*/
812	POOL_STATE_EXPORTED,		/* Explicitly exported		*/
813	POOL_STATE_DESTROYED,		/* Explicitly destroyed		*/
814	POOL_STATE_SPARE,		/* Reserved for hot spare use	*/
815	POOL_STATE_UNINITIALIZED,	/* Internal spa_t state		*/
816	POOL_STATE_UNAVAIL,		/* Internal libzfs state	*/
817	POOL_STATE_POTENTIALLY_ACTIVE	/* Internal libzfs state	*/
818} pool_state_t;
819
820/*
821 * The uberblock version is incremented whenever an incompatible on-disk
822 * format change is made to the SPA, DMU, or ZAP.
823 *
824 * Note: the first two fields should never be moved.  When a storage pool
825 * is opened, the uberblock must be read off the disk before the version
826 * can be checked.  If the ub_version field is moved, we may not detect
827 * version mismatch.  If the ub_magic field is moved, applications that
828 * expect the magic number in the first word won't work.
829 */
830#define	UBERBLOCK_MAGIC		0x00bab10c		/* oo-ba-bloc!	*/
831#define	UBERBLOCK_SHIFT		10			/* up to 1K	*/
832
833struct uberblock {
834	uint64_t	ub_magic;	/* UBERBLOCK_MAGIC		*/
835	uint64_t	ub_version;	/* SPA_VERSION			*/
836	uint64_t	ub_txg;		/* txg of last sync		*/
837	uint64_t	ub_guid_sum;	/* sum of all vdev guids	*/
838	uint64_t	ub_timestamp;	/* UTC time of last sync	*/
839	blkptr_t	ub_rootbp;	/* MOS objset_phys_t		*/
840};
841
842/*
843 * Flags.
844 */
845#define	DNODE_MUST_BE_ALLOCATED	1
846#define	DNODE_MUST_BE_FREE	2
847
848/*
849 * Fixed constants.
850 */
851#define	DNODE_SHIFT		9	/* 512 bytes */
852#define	DN_MIN_INDBLKSHIFT	12	/* 4k */
853#define	DN_MAX_INDBLKSHIFT	14	/* 16k */
854#define	DNODE_BLOCK_SHIFT	14	/* 16k */
855#define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
856#define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
857#define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
858
859/*
860 * Derived constants.
861 */
862#define	DNODE_SIZE	(1 << DNODE_SHIFT)
863#define	DN_MAX_NBLKPTR	((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT)
864#define	DN_MAX_BONUSLEN	(DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT))
865#define	DN_MAX_OBJECT	(1ULL << DN_MAX_OBJECT_SHIFT)
866
867#define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
868#define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
869#define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
870
871/* The +2 here is a cheesy way to round up */
872#define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
873	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
874
875#define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
876	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
877
878#define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
879	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
880
881#define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
882
883/* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
884#define	DNODE_FLAG_USED_BYTES		(1<<0)
885#define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
886
887/* Does dnode have a SA spill blkptr in bonus? */
888#define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
889
890typedef struct dnode_phys {
891	uint8_t dn_type;		/* dmu_object_type_t */
892	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
893	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
894	uint8_t dn_nblkptr;		/* length of dn_blkptr */
895	uint8_t dn_bonustype;		/* type of data in bonus buffer */
896	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
897	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
898	uint8_t dn_flags;		/* DNODE_FLAG_* */
899	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
900	uint16_t dn_bonuslen;		/* length of dn_bonus */
901	uint8_t dn_pad2[4];
902
903	/* accounting is protected by dn_dirty_mtx */
904	uint64_t dn_maxblkid;		/* largest allocated block ID */
905	uint64_t dn_used;		/* bytes (or sectors) of disk space */
906
907	uint64_t dn_pad3[4];
908
909	blkptr_t dn_blkptr[1];
910	uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)];
911	blkptr_t dn_spill;
912} dnode_phys_t;
913
914typedef enum dmu_object_byteswap {
915	DMU_BSWAP_UINT8,
916	DMU_BSWAP_UINT16,
917	DMU_BSWAP_UINT32,
918	DMU_BSWAP_UINT64,
919	DMU_BSWAP_ZAP,
920	DMU_BSWAP_DNODE,
921	DMU_BSWAP_OBJSET,
922	DMU_BSWAP_ZNODE,
923	DMU_BSWAP_OLDACL,
924	DMU_BSWAP_ACL,
925	/*
926	 * Allocating a new byteswap type number makes the on-disk format
927	 * incompatible with any other format that uses the same number.
928	 *
929	 * Data can usually be structured to work with one of the
930	 * DMU_BSWAP_UINT* or DMU_BSWAP_ZAP types.
931	 */
932	DMU_BSWAP_NUMFUNCS
933} dmu_object_byteswap_t;
934
935#define	DMU_OT_NEWTYPE 0x80
936#define	DMU_OT_METADATA 0x40
937#define	DMU_OT_BYTESWAP_MASK 0x3f
938
939/*
940 * Defines a uint8_t object type. Object types specify if the data
941 * in the object is metadata (boolean) and how to byteswap the data
942 * (dmu_object_byteswap_t).
943 */
944#define	DMU_OT(byteswap, metadata) \
945	(DMU_OT_NEWTYPE | \
946	((metadata) ? DMU_OT_METADATA : 0) | \
947	((byteswap) & DMU_OT_BYTESWAP_MASK))
948
949typedef enum dmu_object_type {
950	DMU_OT_NONE,
951	/* general: */
952	DMU_OT_OBJECT_DIRECTORY,	/* ZAP */
953	DMU_OT_OBJECT_ARRAY,		/* UINT64 */
954	DMU_OT_PACKED_NVLIST,		/* UINT8 (XDR by nvlist_pack/unpack) */
955	DMU_OT_PACKED_NVLIST_SIZE,	/* UINT64 */
956	DMU_OT_BPLIST,			/* UINT64 */
957	DMU_OT_BPLIST_HDR,		/* UINT64 */
958	/* spa: */
959	DMU_OT_SPACE_MAP_HEADER,	/* UINT64 */
960	DMU_OT_SPACE_MAP,		/* UINT64 */
961	/* zil: */
962	DMU_OT_INTENT_LOG,		/* UINT64 */
963	/* dmu: */
964	DMU_OT_DNODE,			/* DNODE */
965	DMU_OT_OBJSET,			/* OBJSET */
966	/* dsl: */
967	DMU_OT_DSL_DIR,			/* UINT64 */
968	DMU_OT_DSL_DIR_CHILD_MAP,	/* ZAP */
969	DMU_OT_DSL_DS_SNAP_MAP,		/* ZAP */
970	DMU_OT_DSL_PROPS,		/* ZAP */
971	DMU_OT_DSL_DATASET,		/* UINT64 */
972	/* zpl: */
973	DMU_OT_ZNODE,			/* ZNODE */
974	DMU_OT_OLDACL,			/* Old ACL */
975	DMU_OT_PLAIN_FILE_CONTENTS,	/* UINT8 */
976	DMU_OT_DIRECTORY_CONTENTS,	/* ZAP */
977	DMU_OT_MASTER_NODE,		/* ZAP */
978	DMU_OT_UNLINKED_SET,		/* ZAP */
979	/* zvol: */
980	DMU_OT_ZVOL,			/* UINT8 */
981	DMU_OT_ZVOL_PROP,		/* ZAP */
982	/* other; for testing only! */
983	DMU_OT_PLAIN_OTHER,		/* UINT8 */
984	DMU_OT_UINT64_OTHER,		/* UINT64 */
985	DMU_OT_ZAP_OTHER,		/* ZAP */
986	/* new object types: */
987	DMU_OT_ERROR_LOG,		/* ZAP */
988	DMU_OT_SPA_HISTORY,		/* UINT8 */
989	DMU_OT_SPA_HISTORY_OFFSETS,	/* spa_his_phys_t */
990	DMU_OT_POOL_PROPS,		/* ZAP */
991	DMU_OT_DSL_PERMS,		/* ZAP */
992	DMU_OT_ACL,			/* ACL */
993	DMU_OT_SYSACL,			/* SYSACL */
994	DMU_OT_FUID,			/* FUID table (Packed NVLIST UINT8) */
995	DMU_OT_FUID_SIZE,		/* FUID table size UINT64 */
996	DMU_OT_NEXT_CLONES,		/* ZAP */
997	DMU_OT_SCAN_QUEUE,		/* ZAP */
998	DMU_OT_USERGROUP_USED,		/* ZAP */
999	DMU_OT_USERGROUP_QUOTA,		/* ZAP */
1000	DMU_OT_USERREFS,		/* ZAP */
1001	DMU_OT_DDT_ZAP,			/* ZAP */
1002	DMU_OT_DDT_STATS,		/* ZAP */
1003	DMU_OT_SA,			/* System attr */
1004	DMU_OT_SA_MASTER_NODE,		/* ZAP */
1005	DMU_OT_SA_ATTR_REGISTRATION,	/* ZAP */
1006	DMU_OT_SA_ATTR_LAYOUTS,		/* ZAP */
1007	DMU_OT_SCAN_XLATE,		/* ZAP */
1008	DMU_OT_DEDUP,			/* fake dedup BP from ddt_bp_create() */
1009	DMU_OT_NUMTYPES,
1010
1011	/*
1012	 * Names for valid types declared with DMU_OT().
1013	 */
1014	DMU_OTN_UINT8_DATA = DMU_OT(DMU_BSWAP_UINT8, B_FALSE),
1015	DMU_OTN_UINT8_METADATA = DMU_OT(DMU_BSWAP_UINT8, B_TRUE),
1016	DMU_OTN_UINT16_DATA = DMU_OT(DMU_BSWAP_UINT16, B_FALSE),
1017	DMU_OTN_UINT16_METADATA = DMU_OT(DMU_BSWAP_UINT16, B_TRUE),
1018	DMU_OTN_UINT32_DATA = DMU_OT(DMU_BSWAP_UINT32, B_FALSE),
1019	DMU_OTN_UINT32_METADATA = DMU_OT(DMU_BSWAP_UINT32, B_TRUE),
1020	DMU_OTN_UINT64_DATA = DMU_OT(DMU_BSWAP_UINT64, B_FALSE),
1021	DMU_OTN_UINT64_METADATA = DMU_OT(DMU_BSWAP_UINT64, B_TRUE),
1022	DMU_OTN_ZAP_DATA = DMU_OT(DMU_BSWAP_ZAP, B_FALSE),
1023	DMU_OTN_ZAP_METADATA = DMU_OT(DMU_BSWAP_ZAP, B_TRUE)
1024} dmu_object_type_t;
1025
1026typedef enum dmu_objset_type {
1027	DMU_OST_NONE,
1028	DMU_OST_META,
1029	DMU_OST_ZFS,
1030	DMU_OST_ZVOL,
1031	DMU_OST_OTHER,			/* For testing only! */
1032	DMU_OST_ANY,			/* Be careful! */
1033	DMU_OST_NUMTYPES
1034} dmu_objset_type_t;
1035
1036/*
1037 * header for all bonus and spill buffers.
1038 * The header has a fixed portion with a variable number
1039 * of "lengths" depending on the number of variable sized
1040 * attribues which are determined by the "layout number"
1041 */
1042
1043#define	SA_MAGIC	0x2F505A  /* ZFS SA */
1044typedef struct sa_hdr_phys {
1045	uint32_t sa_magic;
1046	uint16_t sa_layout_info;  /* Encoded with hdrsize and layout number */
1047	uint16_t sa_lengths[1];	/* optional sizes for variable length attrs */
1048	/* ... Data follows the lengths.  */
1049} sa_hdr_phys_t;
1050
1051/*
1052 * sa_hdr_phys -> sa_layout_info
1053 *
1054 * 16      10       0
1055 * +--------+-------+
1056 * | hdrsz  |layout |
1057 * +--------+-------+
1058 *
1059 * Bits 0-10 are the layout number
1060 * Bits 11-16 are the size of the header.
1061 * The hdrsize is the number * 8
1062 *
1063 * For example.
1064 * hdrsz of 1 ==> 8 byte header
1065 *          2 ==> 16 byte header
1066 *
1067 */
1068
1069#define	SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10)
1070#define	SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 16, 3, 0)
1071#define	SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \
1072{ \
1073	BF32_SET_SB(x, 10, 6, 3, 0, size); \
1074	BF32_SET(x, 0, 10, num); \
1075}
1076
1077#define	SA_MODE_OFFSET		0
1078#define	SA_SIZE_OFFSET		8
1079#define	SA_GEN_OFFSET		16
1080#define	SA_UID_OFFSET		24
1081#define	SA_GID_OFFSET		32
1082#define	SA_PARENT_OFFSET	40
1083#define	SA_SYMLINK_OFFSET	160
1084
1085/*
1086 * Intent log header - this on disk structure holds fields to manage
1087 * the log.  All fields are 64 bit to easily handle cross architectures.
1088 */
1089typedef struct zil_header {
1090	uint64_t zh_claim_txg;	/* txg in which log blocks were claimed */
1091	uint64_t zh_replay_seq;	/* highest replayed sequence number */
1092	blkptr_t zh_log;	/* log chain */
1093	uint64_t zh_claim_seq;	/* highest claimed sequence number */
1094	uint64_t zh_pad[5];
1095} zil_header_t;
1096
1097#define	OBJSET_PHYS_SIZE 2048
1098
1099typedef struct objset_phys {
1100	dnode_phys_t os_meta_dnode;
1101	zil_header_t os_zil_header;
1102	uint64_t os_type;
1103	uint64_t os_flags;
1104	char os_pad[OBJSET_PHYS_SIZE - sizeof (dnode_phys_t)*3 -
1105	    sizeof (zil_header_t) - sizeof (uint64_t)*2];
1106	dnode_phys_t os_userused_dnode;
1107	dnode_phys_t os_groupused_dnode;
1108} objset_phys_t;
1109
1110typedef struct dsl_dir_phys {
1111	uint64_t dd_creation_time; /* not actually used */
1112	uint64_t dd_head_dataset_obj;
1113	uint64_t dd_parent_obj;
1114	uint64_t dd_clone_parent_obj;
1115	uint64_t dd_child_dir_zapobj;
1116	/*
1117	 * how much space our children are accounting for; for leaf
1118	 * datasets, == physical space used by fs + snaps
1119	 */
1120	uint64_t dd_used_bytes;
1121	uint64_t dd_compressed_bytes;
1122	uint64_t dd_uncompressed_bytes;
1123	/* Administrative quota setting */
1124	uint64_t dd_quota;
1125	/* Administrative reservation setting */
1126	uint64_t dd_reserved;
1127	uint64_t dd_props_zapobj;
1128	uint64_t dd_pad[21]; /* pad out to 256 bytes for good measure */
1129} dsl_dir_phys_t;
1130
1131typedef struct dsl_dataset_phys {
1132	uint64_t ds_dir_obj;
1133	uint64_t ds_prev_snap_obj;
1134	uint64_t ds_prev_snap_txg;
1135	uint64_t ds_next_snap_obj;
1136	uint64_t ds_snapnames_zapobj;	/* zap obj of snaps; ==0 for snaps */
1137	uint64_t ds_num_children;	/* clone/snap children; ==0 for head */
1138	uint64_t ds_creation_time;	/* seconds since 1970 */
1139	uint64_t ds_creation_txg;
1140	uint64_t ds_deadlist_obj;
1141	uint64_t ds_used_bytes;
1142	uint64_t ds_compressed_bytes;
1143	uint64_t ds_uncompressed_bytes;
1144	uint64_t ds_unique_bytes;	/* only relevant to snapshots */
1145	/*
1146	 * The ds_fsid_guid is a 56-bit ID that can change to avoid
1147	 * collisions.  The ds_guid is a 64-bit ID that will never
1148	 * change, so there is a small probability that it will collide.
1149	 */
1150	uint64_t ds_fsid_guid;
1151	uint64_t ds_guid;
1152	uint64_t ds_flags;
1153	blkptr_t ds_bp;
1154	uint64_t ds_pad[8]; /* pad out to 320 bytes for good measure */
1155} dsl_dataset_phys_t;
1156
1157/*
1158 * The names of zap entries in the DIRECTORY_OBJECT of the MOS.
1159 */
1160#define	DMU_POOL_DIRECTORY_OBJECT	1
1161#define	DMU_POOL_CONFIG			"config"
1162#define	DMU_POOL_FEATURES_FOR_READ	"features_for_read"
1163#define	DMU_POOL_ROOT_DATASET		"root_dataset"
1164#define	DMU_POOL_SYNC_BPLIST		"sync_bplist"
1165#define	DMU_POOL_ERRLOG_SCRUB		"errlog_scrub"
1166#define	DMU_POOL_ERRLOG_LAST		"errlog_last"
1167#define	DMU_POOL_SPARES			"spares"
1168#define	DMU_POOL_DEFLATE		"deflate"
1169#define	DMU_POOL_HISTORY		"history"
1170#define	DMU_POOL_PROPS			"pool_props"
1171#define	DMU_POOL_CHECKSUM_SALT		"org.illumos:checksum_salt"
1172
1173#define	ZAP_MAGIC 0x2F52AB2ABULL
1174
1175#define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_block_shift)
1176
1177#define	ZAP_MAXCD		(uint32_t)(-1)
1178#define	ZAP_HASHBITS		28
1179#define	MZAP_ENT_LEN		64
1180#define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
1181#define	MZAP_MAX_BLKSHIFT	SPA_MAXBLOCKSHIFT
1182#define	MZAP_MAX_BLKSZ		(1 << MZAP_MAX_BLKSHIFT)
1183
1184typedef struct mzap_ent_phys {
1185	uint64_t mze_value;
1186	uint32_t mze_cd;
1187	uint16_t mze_pad;	/* in case we want to chain them someday */
1188	char mze_name[MZAP_NAME_LEN];
1189} mzap_ent_phys_t;
1190
1191typedef struct mzap_phys {
1192	uint64_t mz_block_type;	/* ZBT_MICRO */
1193	uint64_t mz_salt;
1194	uint64_t mz_pad[6];
1195	mzap_ent_phys_t mz_chunk[1];
1196	/* actually variable size depending on block size */
1197} mzap_phys_t;
1198
1199/*
1200 * The (fat) zap is stored in one object. It is an array of
1201 * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
1202 *
1203 * ptrtbl fits in first block:
1204 * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
1205 *
1206 * ptrtbl too big for first block:
1207 * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
1208 *
1209 */
1210
1211#define	ZBT_LEAF		((1ULL << 63) + 0)
1212#define	ZBT_HEADER		((1ULL << 63) + 1)
1213#define	ZBT_MICRO		((1ULL << 63) + 3)
1214/* any other values are ptrtbl blocks */
1215
1216/*
1217 * the embedded pointer table takes up half a block:
1218 * block size / entry size (2^3) / 2
1219 */
1220#define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
1221
1222/*
1223 * The embedded pointer table starts half-way through the block.  Since
1224 * the pointer table itself is half the block, it starts at (64-bit)
1225 * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
1226 */
1227#define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
1228	((uint64_t *)(zap)->zap_phys) \
1229	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
1230
1231/*
1232 * TAKE NOTE:
1233 * If zap_phys_t is modified, zap_byteswap() must be modified.
1234 */
1235typedef struct zap_phys {
1236	uint64_t zap_block_type;	/* ZBT_HEADER */
1237	uint64_t zap_magic;		/* ZAP_MAGIC */
1238
1239	struct zap_table_phys {
1240		uint64_t zt_blk;	/* starting block number */
1241		uint64_t zt_numblks;	/* number of blocks */
1242		uint64_t zt_shift;	/* bits to index it */
1243		uint64_t zt_nextblk;	/* next (larger) copy start block */
1244		uint64_t zt_blks_copied; /* number source blocks copied */
1245	} zap_ptrtbl;
1246
1247	uint64_t zap_freeblk;		/* the next free block */
1248	uint64_t zap_num_leafs;		/* number of leafs */
1249	uint64_t zap_num_entries;	/* number of entries */
1250	uint64_t zap_salt;		/* salt to stir into hash function */
1251	/*
1252	 * This structure is followed by padding, and then the embedded
1253	 * pointer table.  The embedded pointer table takes up second
1254	 * half of the block.  It is accessed using the
1255	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
1256	 */
1257} zap_phys_t;
1258
1259typedef struct zap_table_phys zap_table_phys_t;
1260
1261typedef struct fat_zap {
1262	int zap_block_shift;			/* block size shift */
1263	zap_phys_t *zap_phys;
1264} fat_zap_t;
1265
1266#define	ZAP_LEAF_MAGIC 0x2AB1EAF
1267
1268/* chunk size = 24 bytes */
1269#define	ZAP_LEAF_CHUNKSIZE 24
1270
1271/*
1272 * The amount of space available for chunks is:
1273 * block size (1<<l->l_bs) - hash entry size (2) * number of hash
1274 * entries - header space (2*chunksize)
1275 */
1276#define	ZAP_LEAF_NUMCHUNKS(l) \
1277	(((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
1278	ZAP_LEAF_CHUNKSIZE - 2)
1279
1280/*
1281 * The amount of space within the chunk available for the array is:
1282 * chunk size - space for type (1) - space for next pointer (2)
1283 */
1284#define	ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
1285
1286#define	ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
1287	(((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
1288
1289/*
1290 * Low water mark:  when there are only this many chunks free, start
1291 * growing the ptrtbl.  Ideally, this should be larger than a
1292 * "reasonably-sized" entry.  20 chunks is more than enough for the
1293 * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
1294 * while still being only around 3% for 16k blocks.
1295 */
1296#define	ZAP_LEAF_LOW_WATER (20)
1297
1298/*
1299 * The leaf hash table has block size / 2^5 (32) number of entries,
1300 * which should be more than enough for the maximum number of entries,
1301 * which is less than block size / CHUNKSIZE (24) / minimum number of
1302 * chunks per entry (3).
1303 */
1304#define	ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
1305#define	ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
1306
1307/*
1308 * The chunks start immediately after the hash table.  The end of the
1309 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
1310 * chunk_t.
1311 */
1312#define	ZAP_LEAF_CHUNK(l, idx) \
1313	((zap_leaf_chunk_t *) \
1314	((l)->l_phys->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
1315#define	ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
1316
1317typedef enum zap_chunk_type {
1318	ZAP_CHUNK_FREE = 253,
1319	ZAP_CHUNK_ENTRY = 252,
1320	ZAP_CHUNK_ARRAY = 251,
1321	ZAP_CHUNK_TYPE_MAX = 250
1322} zap_chunk_type_t;
1323
1324/*
1325 * TAKE NOTE:
1326 * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
1327 */
1328typedef struct zap_leaf_phys {
1329	struct zap_leaf_header {
1330		uint64_t lh_block_type;		/* ZBT_LEAF */
1331		uint64_t lh_pad1;
1332		uint64_t lh_prefix;		/* hash prefix of this leaf */
1333		uint32_t lh_magic;		/* ZAP_LEAF_MAGIC */
1334		uint16_t lh_nfree;		/* number free chunks */
1335		uint16_t lh_nentries;		/* number of entries */
1336		uint16_t lh_prefix_len;		/* num bits used to id this */
1337
1338/* above is accessable to zap, below is zap_leaf private */
1339
1340		uint16_t lh_freelist;		/* chunk head of free list */
1341		uint8_t lh_pad2[12];
1342	} l_hdr; /* 2 24-byte chunks */
1343
1344	/*
1345	 * The header is followed by a hash table with
1346	 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries.  The hash table is
1347	 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
1348	 * zap_leaf_chunk structures.  These structures are accessed
1349	 * with the ZAP_LEAF_CHUNK() macro.
1350	 */
1351
1352	uint16_t l_hash[1];
1353} zap_leaf_phys_t;
1354
1355typedef union zap_leaf_chunk {
1356	struct zap_leaf_entry {
1357		uint8_t le_type; 		/* always ZAP_CHUNK_ENTRY */
1358		uint8_t le_value_intlen;	/* size of ints */
1359		uint16_t le_next;		/* next entry in hash chain */
1360		uint16_t le_name_chunk;		/* first chunk of the name */
1361		uint16_t le_name_numints;	/* bytes in name, incl null */
1362		uint16_t le_value_chunk;	/* first chunk of the value */
1363		uint16_t le_value_numints;	/* value length in ints */
1364		uint32_t le_cd;			/* collision differentiator */
1365		uint64_t le_hash;		/* hash value of the name */
1366	} l_entry;
1367	struct zap_leaf_array {
1368		uint8_t la_type;		/* always ZAP_CHUNK_ARRAY */
1369		uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
1370		uint16_t la_next;		/* next blk or CHAIN_END */
1371	} l_array;
1372	struct zap_leaf_free {
1373		uint8_t lf_type;		/* always ZAP_CHUNK_FREE */
1374		uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
1375		uint16_t lf_next;	/* next in free list, or CHAIN_END */
1376	} l_free;
1377} zap_leaf_chunk_t;
1378
1379typedef struct zap_leaf {
1380	int l_bs;			/* block size shift */
1381	zap_leaf_phys_t *l_phys;
1382} zap_leaf_t;
1383
1384/*
1385 * Define special zfs pflags
1386 */
1387#define	ZFS_XATTR	0x1		/* is an extended attribute */
1388#define	ZFS_INHERIT_ACE	0x2		/* ace has inheritable ACEs */
1389#define	ZFS_ACL_TRIVIAL 0x4		/* files ACL is trivial */
1390
1391#define	MASTER_NODE_OBJ	1
1392
1393/*
1394 * special attributes for master node.
1395 */
1396
1397#define	ZFS_FSID		"FSID"
1398#define	ZFS_UNLINKED_SET	"DELETE_QUEUE"
1399#define	ZFS_ROOT_OBJ		"ROOT"
1400#define	ZPL_VERSION_OBJ		"VERSION"
1401#define	ZFS_PROP_BLOCKPERPAGE	"BLOCKPERPAGE"
1402#define	ZFS_PROP_NOGROWBLOCKS	"NOGROWBLOCKS"
1403
1404#define	ZFS_FLAG_BLOCKPERPAGE	0x1
1405#define	ZFS_FLAG_NOGROWBLOCKS	0x2
1406
1407/*
1408 * ZPL version - rev'd whenever an incompatible on-disk format change
1409 * occurs.  Independent of SPA/DMU/ZAP versioning.
1410 */
1411
1412#define	ZPL_VERSION		1ULL
1413
1414/*
1415 * The directory entry has the type (currently unused on Solaris) in the
1416 * top 4 bits, and the object number in the low 48 bits.  The "middle"
1417 * 12 bits are unused.
1418 */
1419#define	ZFS_DIRENT_TYPE(de) BF64_GET(de, 60, 4)
1420#define	ZFS_DIRENT_OBJ(de) BF64_GET(de, 0, 48)
1421#define	ZFS_DIRENT_MAKE(type, obj) (((uint64_t)type << 60) | obj)
1422
1423typedef struct ace {
1424	uid_t		a_who;		/* uid or gid */
1425	uint32_t	a_access_mask;	/* read,write,... */
1426	uint16_t	a_flags;	/* see below */
1427	uint16_t	a_type;		/* allow or deny */
1428} ace_t;
1429
1430#define ACE_SLOT_CNT	6
1431
1432typedef struct zfs_znode_acl {
1433	uint64_t	z_acl_extern_obj;	  /* ext acl pieces */
1434	uint32_t	z_acl_count;		  /* Number of ACEs */
1435	uint16_t	z_acl_version;		  /* acl version */
1436	uint16_t	z_acl_pad;		  /* pad */
1437	ace_t		z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */
1438} zfs_znode_acl_t;
1439
1440/*
1441 * This is the persistent portion of the znode.  It is stored
1442 * in the "bonus buffer" of the file.  Short symbolic links
1443 * are also stored in the bonus buffer.
1444 */
1445typedef struct znode_phys {
1446	uint64_t zp_atime[2];		/*  0 - last file access time */
1447	uint64_t zp_mtime[2];		/* 16 - last file modification time */
1448	uint64_t zp_ctime[2];		/* 32 - last file change time */
1449	uint64_t zp_crtime[2];		/* 48 - creation time */
1450	uint64_t zp_gen;		/* 64 - generation (txg of creation) */
1451	uint64_t zp_mode;		/* 72 - file mode bits */
1452	uint64_t zp_size;		/* 80 - size of file */
1453	uint64_t zp_parent;		/* 88 - directory parent (`..') */
1454	uint64_t zp_links;		/* 96 - number of links to file */
1455	uint64_t zp_xattr;		/* 104 - DMU object for xattrs */
1456	uint64_t zp_rdev;		/* 112 - dev_t for VBLK & VCHR files */
1457	uint64_t zp_flags;		/* 120 - persistent flags */
1458	uint64_t zp_uid;		/* 128 - file owner */
1459	uint64_t zp_gid;		/* 136 - owning group */
1460	uint64_t zp_pad[4];		/* 144 - future */
1461	zfs_znode_acl_t zp_acl;		/* 176 - 263 ACL */
1462	/*
1463	 * Data may pad out any remaining bytes in the znode buffer, eg:
1464	 *
1465	 * |<---------------------- dnode_phys (512) ------------------------>|
1466	 * |<-- dnode (192) --->|<----------- "bonus" buffer (320) ---------->|
1467	 *			|<---- znode (264) ---->|<---- data (56) ---->|
1468	 *
1469	 * At present, we only use this space to store symbolic links.
1470	 */
1471} znode_phys_t;
1472
1473/*
1474 * In-core vdev representation.
1475 */
1476struct vdev;
1477struct spa;
1478typedef int vdev_phys_read_t(struct vdev *vdev, void *priv,
1479    off_t offset, void *buf, size_t bytes);
1480typedef int vdev_read_t(struct vdev *vdev, const blkptr_t *bp,
1481    void *buf, off_t offset, size_t bytes);
1482
1483typedef STAILQ_HEAD(vdev_list, vdev) vdev_list_t;
1484
1485typedef struct vdev {
1486	STAILQ_ENTRY(vdev) v_childlink;	/* link in parent's child list */
1487	STAILQ_ENTRY(vdev) v_alllink;	/* link in global vdev list */
1488	vdev_list_t	v_children;	/* children of this vdev */
1489	const char	*v_name;	/* vdev name */
1490	uint64_t	v_guid;		/* vdev guid */
1491	int		v_id;		/* index in parent */
1492	int		v_ashift;	/* offset to block shift */
1493	int		v_nparity;	/* # parity for raidz */
1494	struct vdev	*v_top;		/* parent vdev */
1495	int		v_nchildren;	/* # children */
1496	vdev_state_t	v_state;	/* current state */
1497	vdev_phys_read_t *v_phys_read;	/* read from raw leaf vdev */
1498	vdev_read_t	*v_read;	/* read from vdev */
1499	void		*v_read_priv;	/* private data for read function */
1500	struct spa	*spa;		/* link to spa */
1501} vdev_t;
1502
1503/*
1504 * In-core pool representation.
1505 */
1506typedef STAILQ_HEAD(spa_list, spa) spa_list_t;
1507
1508typedef struct spa {
1509	STAILQ_ENTRY(spa) spa_link;	/* link in global pool list */
1510	char		*spa_name;	/* pool name */
1511	uint64_t	spa_guid;	/* pool guid */
1512	uint64_t	spa_txg;	/* most recent transaction */
1513	struct uberblock spa_uberblock;	/* best uberblock so far */
1514	vdev_list_t	spa_vdevs;	/* list of all toplevel vdevs */
1515	objset_phys_t	spa_mos;	/* MOS for this pool */
1516	zio_cksum_salt_t spa_cksum_salt;	/* secret salt for cksum */
1517	void		*spa_cksum_tmpls[ZIO_CHECKSUM_FUNCTIONS];
1518	int		spa_inited;	/* initialized */
1519} spa_t;
1520
1521static void decode_embedded_bp_compressed(const blkptr_t *, void *);
1522