xref: /freebsd/sys/cddl/boot/zfs/zfsimpl.h (revision 79a4bf89753ccfa40db8e76a2efade19d6116071)
11ba4a712SPawel Jakub Dawidek /*-
21ba4a712SPawel Jakub Dawidek  * Copyright (c) 2002 McAfee, Inc.
31ba4a712SPawel Jakub Dawidek  * All rights reserved.
41ba4a712SPawel Jakub Dawidek  *
51ba4a712SPawel Jakub Dawidek  * This software was developed for the FreeBSD Project by Marshall
61ba4a712SPawel Jakub Dawidek  * Kirk McKusick and McAfee Research,, the Security Research Division of
71ba4a712SPawel Jakub Dawidek  * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
81ba4a712SPawel Jakub Dawidek  * part of the DARPA CHATS research program
91ba4a712SPawel Jakub Dawidek  *
101ba4a712SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
111ba4a712SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
121ba4a712SPawel Jakub Dawidek  * are met:
131ba4a712SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
141ba4a712SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
151ba4a712SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
161ba4a712SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
171ba4a712SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
181ba4a712SPawel Jakub Dawidek  *
191ba4a712SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
201ba4a712SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211ba4a712SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221ba4a712SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
231ba4a712SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241ba4a712SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251ba4a712SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261ba4a712SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271ba4a712SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281ba4a712SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291ba4a712SPawel Jakub Dawidek  * SUCH DAMAGE.
301ba4a712SPawel Jakub Dawidek  */
311ba4a712SPawel Jakub Dawidek /*
321ba4a712SPawel Jakub Dawidek  * CDDL HEADER START
331ba4a712SPawel Jakub Dawidek  *
341ba4a712SPawel Jakub Dawidek  * The contents of this file are subject to the terms of the
351ba4a712SPawel Jakub Dawidek  * Common Development and Distribution License (the "License").
361ba4a712SPawel Jakub Dawidek  * You may not use this file except in compliance with the License.
371ba4a712SPawel Jakub Dawidek  *
381ba4a712SPawel Jakub Dawidek  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
391ba4a712SPawel Jakub Dawidek  * or http://www.opensolaris.org/os/licensing.
401ba4a712SPawel Jakub Dawidek  * See the License for the specific language governing permissions
411ba4a712SPawel Jakub Dawidek  * and limitations under the License.
421ba4a712SPawel Jakub Dawidek  *
431ba4a712SPawel Jakub Dawidek  * When distributing Covered Code, include this CDDL HEADER in each
441ba4a712SPawel Jakub Dawidek  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
451ba4a712SPawel Jakub Dawidek  * If applicable, add the following below this CDDL HEADER, with the
461ba4a712SPawel Jakub Dawidek  * fields enclosed by brackets "[]" replaced with your own identifying
471ba4a712SPawel Jakub Dawidek  * information: Portions Copyright [yyyy] [name of copyright owner]
481ba4a712SPawel Jakub Dawidek  *
491ba4a712SPawel Jakub Dawidek  * CDDL HEADER END
501ba4a712SPawel Jakub Dawidek  */
511ba4a712SPawel Jakub Dawidek /*
528fc25799SMartin Matuska  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
531ba4a712SPawel Jakub Dawidek  * Use is subject to license terms.
541ba4a712SPawel Jakub Dawidek  */
55ef17620fSXin LI /*
56ef17620fSXin LI  * Copyright 2013 by Saso Kiselkov. All rights reserved.
57ef17620fSXin LI  */
5829441ba3SXin LI /*
5929441ba3SXin LI  * Copyright (c) 2013 by Delphix. All rights reserved.
6029441ba3SXin LI  */
611ba4a712SPawel Jakub Dawidek 
622d9cf57eSMartin Matuska #define	MAXNAMELEN	256
632d9cf57eSMartin Matuska 
6429441ba3SXin LI #define _NOTE(s)
6529441ba3SXin LI 
66*79a4bf89SToomas Soome /*
67*79a4bf89SToomas Soome  * AVL comparator helpers
68*79a4bf89SToomas Soome  */
69*79a4bf89SToomas Soome #define	AVL_ISIGN(a)	(((a) > 0) - ((a) < 0))
70*79a4bf89SToomas Soome #define	AVL_CMP(a, b)	(((a) > (b)) - ((a) < (b)))
71*79a4bf89SToomas Soome #define	AVL_PCMP(a, b)	\
72*79a4bf89SToomas Soome 	(((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b)))
73*79a4bf89SToomas Soome 
744deb8929SAllan Jude typedef enum { B_FALSE, B_TRUE } boolean_t;
754deb8929SAllan Jude 
761ba4a712SPawel Jakub Dawidek /* CRC64 table */
771ba4a712SPawel Jakub Dawidek #define	ZFS_CRC64_POLY	0xC96C5795D7870F42ULL	/* ECMA-182, reflected form */
781ba4a712SPawel Jakub Dawidek 
791ba4a712SPawel Jakub Dawidek /*
801ba4a712SPawel Jakub Dawidek  * Macros for various sorts of alignment and rounding when the alignment
811ba4a712SPawel Jakub Dawidek  * is known to be a power of 2.
821ba4a712SPawel Jakub Dawidek  */
831ba4a712SPawel Jakub Dawidek #define	P2ALIGN(x, align)		((x) & -(align))
841ba4a712SPawel Jakub Dawidek #define	P2PHASE(x, align)		((x) & ((align) - 1))
851ba4a712SPawel Jakub Dawidek #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
861ba4a712SPawel Jakub Dawidek #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
871ba4a712SPawel Jakub Dawidek #define	P2END(x, align)			(-(~(x) & -(align)))
881ba4a712SPawel Jakub Dawidek #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
89c43d127aSMartin Matuska #define	P2BOUNDARY(off, len, align)	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
901ba4a712SPawel Jakub Dawidek 
911ba4a712SPawel Jakub Dawidek /*
921ba4a712SPawel Jakub Dawidek  * General-purpose 32-bit and 64-bit bitfield encodings.
931ba4a712SPawel Jakub Dawidek  */
941ba4a712SPawel Jakub Dawidek #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
951ba4a712SPawel Jakub Dawidek #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
961ba4a712SPawel Jakub Dawidek #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
971ba4a712SPawel Jakub Dawidek #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
981ba4a712SPawel Jakub Dawidek 
991ba4a712SPawel Jakub Dawidek #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
1001ba4a712SPawel Jakub Dawidek #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
1011ba4a712SPawel Jakub Dawidek 
1021ba4a712SPawel Jakub Dawidek #define	BF32_SET(x, low, len, val)	\
1031ba4a712SPawel Jakub Dawidek 	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
1041ba4a712SPawel Jakub Dawidek #define	BF64_SET(x, low, len, val)	\
1051ba4a712SPawel Jakub Dawidek 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
1061ba4a712SPawel Jakub Dawidek 
1071ba4a712SPawel Jakub Dawidek #define	BF32_GET_SB(x, low, len, shift, bias)	\
1081ba4a712SPawel Jakub Dawidek 	((BF32_GET(x, low, len) + (bias)) << (shift))
1091ba4a712SPawel Jakub Dawidek #define	BF64_GET_SB(x, low, len, shift, bias)	\
1101ba4a712SPawel Jakub Dawidek 	((BF64_GET(x, low, len) + (bias)) << (shift))
1111ba4a712SPawel Jakub Dawidek 
1121ba4a712SPawel Jakub Dawidek #define	BF32_SET_SB(x, low, len, shift, bias, val)	\
1131ba4a712SPawel Jakub Dawidek 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
1141ba4a712SPawel Jakub Dawidek #define	BF64_SET_SB(x, low, len, shift, bias, val)	\
1151ba4a712SPawel Jakub Dawidek 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
1161ba4a712SPawel Jakub Dawidek 
1171ba4a712SPawel Jakub Dawidek /*
11810b9d77bSPawel Jakub Dawidek  * Macros to reverse byte order
11910b9d77bSPawel Jakub Dawidek  */
12010b9d77bSPawel Jakub Dawidek #define	BSWAP_8(x)	((x) & 0xff)
12110b9d77bSPawel Jakub Dawidek #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
12210b9d77bSPawel Jakub Dawidek #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
12310b9d77bSPawel Jakub Dawidek #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
12410b9d77bSPawel Jakub Dawidek 
1251ba4a712SPawel Jakub Dawidek #define	SPA_MINBLOCKSHIFT	9
1262c55d090SToomas Soome #define	SPA_OLDMAXBLOCKSHIFT	17
1272c55d090SToomas Soome #define	SPA_MAXBLOCKSHIFT	24
1281ba4a712SPawel Jakub Dawidek #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
1292c55d090SToomas Soome #define	SPA_OLDMAXBLOCKSIZE	(1ULL << SPA_OLDMAXBLOCKSHIFT)
1301ba4a712SPawel Jakub Dawidek #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
1311ba4a712SPawel Jakub Dawidek 
1321ba4a712SPawel Jakub Dawidek /*
1331ba4a712SPawel Jakub Dawidek  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
1341ba4a712SPawel Jakub Dawidek  * The ASIZE encoding should be at least 64 times larger (6 more bits)
1351ba4a712SPawel Jakub Dawidek  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
1361ba4a712SPawel Jakub Dawidek  * overhead, three DVAs per bp, plus one more bit in case we do anything
1371ba4a712SPawel Jakub Dawidek  * else that expands the ASIZE.
1381ba4a712SPawel Jakub Dawidek  */
1391ba4a712SPawel Jakub Dawidek #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
1401ba4a712SPawel Jakub Dawidek #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
1411ba4a712SPawel Jakub Dawidek #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
1421ba4a712SPawel Jakub Dawidek 
1431ba4a712SPawel Jakub Dawidek /*
1441ba4a712SPawel Jakub Dawidek  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
1451ba4a712SPawel Jakub Dawidek  * The members of the dva_t should be considered opaque outside the SPA.
1461ba4a712SPawel Jakub Dawidek  */
1471ba4a712SPawel Jakub Dawidek typedef struct dva {
1481ba4a712SPawel Jakub Dawidek 	uint64_t	dva_word[2];
1491ba4a712SPawel Jakub Dawidek } dva_t;
1501ba4a712SPawel Jakub Dawidek 
1511ba4a712SPawel Jakub Dawidek /*
1521ba4a712SPawel Jakub Dawidek  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
1531ba4a712SPawel Jakub Dawidek  */
1541ba4a712SPawel Jakub Dawidek typedef struct zio_cksum {
1551ba4a712SPawel Jakub Dawidek 	uint64_t	zc_word[4];
1561ba4a712SPawel Jakub Dawidek } zio_cksum_t;
1571ba4a712SPawel Jakub Dawidek 
1581ba4a712SPawel Jakub Dawidek /*
1592c55d090SToomas Soome  * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
1602c55d090SToomas Soome  * secret and is suitable for use in MAC algorithms as the key.
1612c55d090SToomas Soome  */
1622c55d090SToomas Soome typedef struct zio_cksum_salt {
1632c55d090SToomas Soome 	uint8_t		zcs_bytes[32];
1642c55d090SToomas Soome } zio_cksum_salt_t;
1652c55d090SToomas Soome 
1662c55d090SToomas Soome /*
1671ba4a712SPawel Jakub Dawidek  * Each block is described by its DVAs, time of birth, checksum, etc.
1681ba4a712SPawel Jakub Dawidek  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
1691ba4a712SPawel Jakub Dawidek  *
1701ba4a712SPawel Jakub Dawidek  *	64	56	48	40	32	24	16	8	0
1711ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1721ba4a712SPawel Jakub Dawidek  * 0	|		vdev1		| GRID  |	  ASIZE		|
1731ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1741ba4a712SPawel Jakub Dawidek  * 1	|G|			 offset1				|
1751ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1761ba4a712SPawel Jakub Dawidek  * 2	|		vdev2		| GRID  |	  ASIZE		|
1771ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1781ba4a712SPawel Jakub Dawidek  * 3	|G|			 offset2				|
1791ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1801ba4a712SPawel Jakub Dawidek  * 4	|		vdev3		| GRID  |	  ASIZE		|
1811ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1821ba4a712SPawel Jakub Dawidek  * 5	|G|			 offset3				|
1831ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
18429441ba3SXin LI  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
1851ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1861ba4a712SPawel Jakub Dawidek  * 7	|			padding					|
1871ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1881ba4a712SPawel Jakub Dawidek  * 8	|			padding					|
1891ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
19010b9d77bSPawel Jakub Dawidek  * 9	|			physical birth txg			|
1911ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
19210b9d77bSPawel Jakub Dawidek  * a	|			logical birth txg			|
1931ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1941ba4a712SPawel Jakub Dawidek  * b	|			fill count				|
1951ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1961ba4a712SPawel Jakub Dawidek  * c	|			checksum[0]				|
1971ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1981ba4a712SPawel Jakub Dawidek  * d	|			checksum[1]				|
1991ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2001ba4a712SPawel Jakub Dawidek  * e	|			checksum[2]				|
2011ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2021ba4a712SPawel Jakub Dawidek  * f	|			checksum[3]				|
2031ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2041ba4a712SPawel Jakub Dawidek  *
2051ba4a712SPawel Jakub Dawidek  * Legend:
2061ba4a712SPawel Jakub Dawidek  *
2071ba4a712SPawel Jakub Dawidek  * vdev		virtual device ID
2081ba4a712SPawel Jakub Dawidek  * offset	offset into virtual device
2091ba4a712SPawel Jakub Dawidek  * LSIZE	logical size
2101ba4a712SPawel Jakub Dawidek  * PSIZE	physical size (after compression)
2111ba4a712SPawel Jakub Dawidek  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
2121ba4a712SPawel Jakub Dawidek  * GRID		RAID-Z layout information (reserved for future use)
2131ba4a712SPawel Jakub Dawidek  * cksum	checksum function
2141ba4a712SPawel Jakub Dawidek  * comp		compression function
2151ba4a712SPawel Jakub Dawidek  * G		gang block indicator
21610b9d77bSPawel Jakub Dawidek  * B		byteorder (endianness)
21710b9d77bSPawel Jakub Dawidek  * D		dedup
21829441ba3SXin LI  * X		encryption (on version 30, which is not supported)
21929441ba3SXin LI  * E		blkptr_t contains embedded data (see below)
2201ba4a712SPawel Jakub Dawidek  * lvl		level of indirection
22110b9d77bSPawel Jakub Dawidek  * type		DMU object type
22210b9d77bSPawel Jakub Dawidek  * phys birth	txg of block allocation; zero if same as logical birth txg
22310b9d77bSPawel Jakub Dawidek  * log. birth	transaction group in which the block was logically born
2241ba4a712SPawel Jakub Dawidek  * fill count	number of non-zero blocks under this bp
2251ba4a712SPawel Jakub Dawidek  * checksum[4]	256-bit checksum of the data this bp describes
2261ba4a712SPawel Jakub Dawidek  */
22729441ba3SXin LI 
22829441ba3SXin LI /*
22929441ba3SXin LI  * "Embedded" blkptr_t's don't actually point to a block, instead they
23029441ba3SXin LI  * have a data payload embedded in the blkptr_t itself.  See the comment
23129441ba3SXin LI  * in blkptr.c for more details.
23229441ba3SXin LI  *
23329441ba3SXin LI  * The blkptr_t is laid out as follows:
23429441ba3SXin LI  *
23529441ba3SXin LI  *	64	56	48	40	32	24	16	8	0
23629441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
23729441ba3SXin LI  * 0	|      payload                                                  |
23829441ba3SXin LI  * 1	|      payload                                                  |
23929441ba3SXin LI  * 2	|      payload                                                  |
24029441ba3SXin LI  * 3	|      payload                                                  |
24129441ba3SXin LI  * 4	|      payload                                                  |
24229441ba3SXin LI  * 5	|      payload                                                  |
24329441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
24429441ba3SXin LI  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
24529441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
24629441ba3SXin LI  * 7	|      payload                                                  |
24729441ba3SXin LI  * 8	|      payload                                                  |
24829441ba3SXin LI  * 9	|      payload                                                  |
24929441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
25029441ba3SXin LI  * a	|			logical birth txg			|
25129441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
25229441ba3SXin LI  * b	|      payload                                                  |
25329441ba3SXin LI  * c	|      payload                                                  |
25429441ba3SXin LI  * d	|      payload                                                  |
25529441ba3SXin LI  * e	|      payload                                                  |
25629441ba3SXin LI  * f	|      payload                                                  |
25729441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
25829441ba3SXin LI  *
25929441ba3SXin LI  * Legend:
26029441ba3SXin LI  *
26129441ba3SXin LI  * payload		contains the embedded data
26229441ba3SXin LI  * B (byteorder)	byteorder (endianness)
26329441ba3SXin LI  * D (dedup)		padding (set to zero)
26429441ba3SXin LI  * X			encryption (set to zero; see above)
26529441ba3SXin LI  * E (embedded)		set to one
26629441ba3SXin LI  * lvl			indirection level
26729441ba3SXin LI  * type			DMU object type
26829441ba3SXin LI  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
26929441ba3SXin LI  * comp			compression function of payload
27029441ba3SXin LI  * PSIZE		size of payload after compression, in bytes
27129441ba3SXin LI  * LSIZE		logical size of payload, in bytes
27229441ba3SXin LI  *			note that 25 bits is enough to store the largest
27329441ba3SXin LI  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
27429441ba3SXin LI  * log. birth		transaction group in which the block was logically born
27529441ba3SXin LI  *
27629441ba3SXin LI  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
27729441ba3SXin LI  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
27829441ba3SXin LI  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
27929441ba3SXin LI  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
28029441ba3SXin LI  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
28129441ba3SXin LI  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
28229441ba3SXin LI  * other macros, as they assert that they are only used on BP's of the correct
28329441ba3SXin LI  * "embedded-ness".
28429441ba3SXin LI  */
28529441ba3SXin LI 
28629441ba3SXin LI #define	BPE_GET_ETYPE(bp)	\
28729441ba3SXin LI 	(ASSERT(BP_IS_EMBEDDED(bp)), \
28829441ba3SXin LI 	BF64_GET((bp)->blk_prop, 40, 8))
28929441ba3SXin LI #define	BPE_SET_ETYPE(bp, t)	do { \
29029441ba3SXin LI 	ASSERT(BP_IS_EMBEDDED(bp)); \
29129441ba3SXin LI 	BF64_SET((bp)->blk_prop, 40, 8, t); \
29229441ba3SXin LI _NOTE(CONSTCOND) } while (0)
29329441ba3SXin LI 
29429441ba3SXin LI #define	BPE_GET_LSIZE(bp)	\
29529441ba3SXin LI 	(ASSERT(BP_IS_EMBEDDED(bp)), \
29629441ba3SXin LI 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
29729441ba3SXin LI #define	BPE_SET_LSIZE(bp, x)	do { \
29829441ba3SXin LI 	ASSERT(BP_IS_EMBEDDED(bp)); \
29929441ba3SXin LI 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
30029441ba3SXin LI _NOTE(CONSTCOND) } while (0)
30129441ba3SXin LI 
30229441ba3SXin LI #define	BPE_GET_PSIZE(bp)	\
30329441ba3SXin LI 	(ASSERT(BP_IS_EMBEDDED(bp)), \
30429441ba3SXin LI 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
30529441ba3SXin LI #define	BPE_SET_PSIZE(bp, x)	do { \
30629441ba3SXin LI 	ASSERT(BP_IS_EMBEDDED(bp)); \
30729441ba3SXin LI 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
30829441ba3SXin LI _NOTE(CONSTCOND) } while (0)
30929441ba3SXin LI 
31029441ba3SXin LI typedef enum bp_embedded_type {
31129441ba3SXin LI 	BP_EMBEDDED_TYPE_DATA,
31229441ba3SXin LI 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
31329441ba3SXin LI 	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
31429441ba3SXin LI } bp_embedded_type_t;
31529441ba3SXin LI 
31629441ba3SXin LI #define	BPE_NUM_WORDS 14
31729441ba3SXin LI #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
31829441ba3SXin LI #define	BPE_IS_PAYLOADWORD(bp, wp) \
31929441ba3SXin LI 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
32029441ba3SXin LI 
32110b9d77bSPawel Jakub Dawidek #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
32210b9d77bSPawel Jakub Dawidek #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
32310b9d77bSPawel Jakub Dawidek 
3241ba4a712SPawel Jakub Dawidek typedef struct blkptr {
32510b9d77bSPawel Jakub Dawidek 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
3261ba4a712SPawel Jakub Dawidek 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
32710b9d77bSPawel Jakub Dawidek 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
32810b9d77bSPawel Jakub Dawidek 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
3291ba4a712SPawel Jakub Dawidek 	uint64_t	blk_birth;	/* transaction group at birth	    */
3301ba4a712SPawel Jakub Dawidek 	uint64_t	blk_fill;	/* fill count			    */
3311ba4a712SPawel Jakub Dawidek 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
3321ba4a712SPawel Jakub Dawidek } blkptr_t;
3331ba4a712SPawel Jakub Dawidek 
3341ba4a712SPawel Jakub Dawidek /*
3351ba4a712SPawel Jakub Dawidek  * Macros to get and set fields in a bp or DVA.
3361ba4a712SPawel Jakub Dawidek  */
3371ba4a712SPawel Jakub Dawidek #define	DVA_GET_ASIZE(dva)	\
338f4c8ba83SXin LI 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
3391ba4a712SPawel Jakub Dawidek #define	DVA_SET_ASIZE(dva, x)	\
340f4c8ba83SXin LI 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
341f4c8ba83SXin LI 	SPA_MINBLOCKSHIFT, 0, x)
3421ba4a712SPawel Jakub Dawidek 
3431ba4a712SPawel Jakub Dawidek #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
3441ba4a712SPawel Jakub Dawidek #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
3451ba4a712SPawel Jakub Dawidek 
3461ba4a712SPawel Jakub Dawidek #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
3471ba4a712SPawel Jakub Dawidek #define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
3481ba4a712SPawel Jakub Dawidek 
3491ba4a712SPawel Jakub Dawidek #define	DVA_GET_OFFSET(dva)	\
3501ba4a712SPawel Jakub Dawidek 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
3511ba4a712SPawel Jakub Dawidek #define	DVA_SET_OFFSET(dva, x)	\
3521ba4a712SPawel Jakub Dawidek 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
3531ba4a712SPawel Jakub Dawidek 
3541ba4a712SPawel Jakub Dawidek #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
3551ba4a712SPawel Jakub Dawidek #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
3561ba4a712SPawel Jakub Dawidek 
3571ba4a712SPawel Jakub Dawidek #define	BP_GET_LSIZE(bp)	\
35829441ba3SXin LI 	(BP_IS_EMBEDDED(bp) ?	\
35929441ba3SXin LI 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
360f4c8ba83SXin LI 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
36129441ba3SXin LI #define	BP_SET_LSIZE(bp, x)	do { \
36229441ba3SXin LI 	ASSERT(!BP_IS_EMBEDDED(bp)); \
36329441ba3SXin LI 	BF64_SET_SB((bp)->blk_prop, \
36429441ba3SXin LI 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
36529441ba3SXin LI _NOTE(CONSTCOND) } while (0)
3661ba4a712SPawel Jakub Dawidek 
3671ba4a712SPawel Jakub Dawidek #define	BP_GET_PSIZE(bp)	\
368f4c8ba83SXin LI 	BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)
3691ba4a712SPawel Jakub Dawidek #define	BP_SET_PSIZE(bp, x)	\
370f4c8ba83SXin LI 	BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x)
3711ba4a712SPawel Jakub Dawidek 
37229441ba3SXin LI #define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 7)
37329441ba3SXin LI #define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 7, x)
3741ba4a712SPawel Jakub Dawidek 
3751ba4a712SPawel Jakub Dawidek #define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
3761ba4a712SPawel Jakub Dawidek #define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
3771ba4a712SPawel Jakub Dawidek 
3781ba4a712SPawel Jakub Dawidek #define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
3791ba4a712SPawel Jakub Dawidek #define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
3801ba4a712SPawel Jakub Dawidek 
3811ba4a712SPawel Jakub Dawidek #define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
3821ba4a712SPawel Jakub Dawidek #define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
3831ba4a712SPawel Jakub Dawidek 
38429441ba3SXin LI #define	BP_IS_EMBEDDED(bp)	BF64_GET((bp)->blk_prop, 39, 1)
38529441ba3SXin LI 
38610b9d77bSPawel Jakub Dawidek #define	BP_GET_DEDUP(bp)	BF64_GET((bp)->blk_prop, 62, 1)
38710b9d77bSPawel Jakub Dawidek #define	BP_SET_DEDUP(bp, x)	BF64_SET((bp)->blk_prop, 62, 1, x)
38810b9d77bSPawel Jakub Dawidek 
389f4c8ba83SXin LI #define	BP_GET_BYTEORDER(bp)	BF64_GET((bp)->blk_prop, 63, 1)
3901ba4a712SPawel Jakub Dawidek #define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
3911ba4a712SPawel Jakub Dawidek 
39210b9d77bSPawel Jakub Dawidek #define	BP_PHYSICAL_BIRTH(bp)		\
39310b9d77bSPawel Jakub Dawidek 	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
39410b9d77bSPawel Jakub Dawidek 
3951ba4a712SPawel Jakub Dawidek #define	BP_GET_ASIZE(bp)	\
3961ba4a712SPawel Jakub Dawidek 	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
3971ba4a712SPawel Jakub Dawidek 		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
3981ba4a712SPawel Jakub Dawidek 
3991ba4a712SPawel Jakub Dawidek #define	BP_GET_UCSIZE(bp) \
4001ba4a712SPawel Jakub Dawidek 	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
4011ba4a712SPawel Jakub Dawidek 	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
4021ba4a712SPawel Jakub Dawidek 
4031ba4a712SPawel Jakub Dawidek #define	BP_GET_NDVAS(bp)	\
4041ba4a712SPawel Jakub Dawidek 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
4051ba4a712SPawel Jakub Dawidek 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
4061ba4a712SPawel Jakub Dawidek 	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
4071ba4a712SPawel Jakub Dawidek 
4081ba4a712SPawel Jakub Dawidek #define	DVA_EQUAL(dva1, dva2)	\
4091ba4a712SPawel Jakub Dawidek 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
4101ba4a712SPawel Jakub Dawidek 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
4111ba4a712SPawel Jakub Dawidek 
4121ba4a712SPawel Jakub Dawidek #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
4131ba4a712SPawel Jakub Dawidek 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
4141ba4a712SPawel Jakub Dawidek 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
4151ba4a712SPawel Jakub Dawidek 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
4161ba4a712SPawel Jakub Dawidek 	((zc1).zc_word[3] - (zc2).zc_word[3])))
4171ba4a712SPawel Jakub Dawidek 
4181ba4a712SPawel Jakub Dawidek 
4191ba4a712SPawel Jakub Dawidek #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
4201ba4a712SPawel Jakub Dawidek 
4211ba4a712SPawel Jakub Dawidek #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
4221ba4a712SPawel Jakub Dawidek {						\
4231ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[0] = w0;			\
4241ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[1] = w1;			\
4251ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[2] = w2;			\
4261ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[3] = w3;			\
4271ba4a712SPawel Jakub Dawidek }
4281ba4a712SPawel Jakub Dawidek 
4291ba4a712SPawel Jakub Dawidek #define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
4301ba4a712SPawel Jakub Dawidek #define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
431f4c8ba83SXin LI #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&  \
432f4c8ba83SXin LI 	(dva)->dva_word[1] == 0ULL)
433f4c8ba83SXin LI #define	BP_IS_HOLE(bp)		DVA_IS_EMPTY(BP_IDENTITY(bp))
4341ba4a712SPawel Jakub Dawidek #define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
4351ba4a712SPawel Jakub Dawidek 
4361ba4a712SPawel Jakub Dawidek #define	BP_ZERO(bp)				\
4371ba4a712SPawel Jakub Dawidek {						\
4381ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[0].dva_word[0] = 0;	\
4391ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[0].dva_word[1] = 0;	\
4401ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[1].dva_word[0] = 0;	\
4411ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[1].dva_word[1] = 0;	\
4421ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[2].dva_word[0] = 0;	\
4431ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[2].dva_word[1] = 0;	\
4441ba4a712SPawel Jakub Dawidek 	(bp)->blk_prop = 0;			\
4451ba4a712SPawel Jakub Dawidek 	(bp)->blk_pad[0] = 0;			\
4461ba4a712SPawel Jakub Dawidek 	(bp)->blk_pad[1] = 0;			\
44710b9d77bSPawel Jakub Dawidek 	(bp)->blk_phys_birth = 0;		\
4481ba4a712SPawel Jakub Dawidek 	(bp)->blk_birth = 0;			\
4491ba4a712SPawel Jakub Dawidek 	(bp)->blk_fill = 0;			\
4501ba4a712SPawel Jakub Dawidek 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
4511ba4a712SPawel Jakub Dawidek }
4521ba4a712SPawel Jakub Dawidek 
453903fe2b7SToomas Soome #if BYTE_ORDER == _BIG_ENDIAN
454903fe2b7SToomas Soome #define	ZFS_HOST_BYTEORDER	(0ULL)
455903fe2b7SToomas Soome #else
456903fe2b7SToomas Soome #define	ZFS_HOST_BYTEORDER	(1ULL)
457903fe2b7SToomas Soome #endif
458903fe2b7SToomas Soome 
459903fe2b7SToomas Soome #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
46029441ba3SXin LI #define	BPE_NUM_WORDS 14
46129441ba3SXin LI #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
46229441ba3SXin LI #define	BPE_IS_PAYLOADWORD(bp, wp) \
46329441ba3SXin LI 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
46429441ba3SXin LI 
46510b9d77bSPawel Jakub Dawidek /*
46610b9d77bSPawel Jakub Dawidek  * Embedded checksum
46710b9d77bSPawel Jakub Dawidek  */
46810b9d77bSPawel Jakub Dawidek #define	ZEC_MAGIC	0x210da7ab10c7a11ULL
4691ba4a712SPawel Jakub Dawidek 
47010b9d77bSPawel Jakub Dawidek typedef struct zio_eck {
47110b9d77bSPawel Jakub Dawidek 	uint64_t	zec_magic;	/* for validation, endianness	*/
47210b9d77bSPawel Jakub Dawidek 	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
47310b9d77bSPawel Jakub Dawidek } zio_eck_t;
47410b9d77bSPawel Jakub Dawidek 
47510b9d77bSPawel Jakub Dawidek /*
47610b9d77bSPawel Jakub Dawidek  * Gang block headers are self-checksumming and contain an array
47710b9d77bSPawel Jakub Dawidek  * of block pointers.
47810b9d77bSPawel Jakub Dawidek  */
47910b9d77bSPawel Jakub Dawidek #define	SPA_GANGBLOCKSIZE	SPA_MINBLOCKSIZE
48010b9d77bSPawel Jakub Dawidek #define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
48110b9d77bSPawel Jakub Dawidek 	sizeof (zio_eck_t)) / sizeof (blkptr_t))
48210b9d77bSPawel Jakub Dawidek #define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
48310b9d77bSPawel Jakub Dawidek 	sizeof (zio_eck_t) - \
48410b9d77bSPawel Jakub Dawidek 	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
48510b9d77bSPawel Jakub Dawidek 	sizeof (uint64_t))
48610b9d77bSPawel Jakub Dawidek 
48710b9d77bSPawel Jakub Dawidek typedef struct zio_gbh {
48810b9d77bSPawel Jakub Dawidek 	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
48910b9d77bSPawel Jakub Dawidek 	uint64_t		zg_filler[SPA_GBH_FILLER];
49010b9d77bSPawel Jakub Dawidek 	zio_eck_t		zg_tail;
49110b9d77bSPawel Jakub Dawidek } zio_gbh_phys_t;
49210b9d77bSPawel Jakub Dawidek 
49310b9d77bSPawel Jakub Dawidek #define	VDEV_RAIDZ_MAXPARITY	3
4941ba4a712SPawel Jakub Dawidek 
4958fc25799SMartin Matuska #define	VDEV_PAD_SIZE		(8 << 10)
4968fc25799SMartin Matuska /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
4978fc25799SMartin Matuska #define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
4981ba4a712SPawel Jakub Dawidek #define	VDEV_PHYS_SIZE		(112 << 10)
4991ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_RING	(128 << 10)
5001ba4a712SPawel Jakub Dawidek 
501*79a4bf89SToomas Soome /*
502*79a4bf89SToomas Soome  * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
503*79a4bf89SToomas Soome  * ring when MMP is enabled.
504*79a4bf89SToomas Soome  */
505*79a4bf89SToomas Soome #define	MMP_BLOCKS_PER_LABEL	1
506*79a4bf89SToomas Soome 
507*79a4bf89SToomas Soome /* The largest uberblock we support is 8k. */
508*79a4bf89SToomas Soome #define	MAX_UBERBLOCK_SHIFT	(13)
5091ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_SHIFT(vd)	\
510*79a4bf89SToomas Soome 	MIN(MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT), MAX_UBERBLOCK_SHIFT)
5111ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_COUNT(vd)	\
5121ba4a712SPawel Jakub Dawidek 	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
5131ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
5141ba4a712SPawel Jakub Dawidek 	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
5151ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
5161ba4a712SPawel Jakub Dawidek 
5171ba4a712SPawel Jakub Dawidek typedef struct vdev_phys {
51810b9d77bSPawel Jakub Dawidek 	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
51910b9d77bSPawel Jakub Dawidek 	zio_eck_t	vp_zbt;
5201ba4a712SPawel Jakub Dawidek } vdev_phys_t;
5211ba4a712SPawel Jakub Dawidek 
5221ba4a712SPawel Jakub Dawidek typedef struct vdev_label {
5238fc25799SMartin Matuska 	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K  */
5248fc25799SMartin Matuska 	char		vl_pad2[VDEV_PAD_SIZE];			/*  8K  */
5251ba4a712SPawel Jakub Dawidek 	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
5261ba4a712SPawel Jakub Dawidek 	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
5271ba4a712SPawel Jakub Dawidek } vdev_label_t;							/* 256K total */
5281ba4a712SPawel Jakub Dawidek 
5291ba4a712SPawel Jakub Dawidek /*
5301ba4a712SPawel Jakub Dawidek  * vdev_dirty() flags
5311ba4a712SPawel Jakub Dawidek  */
5321ba4a712SPawel Jakub Dawidek #define	VDD_METASLAB	0x01
5331ba4a712SPawel Jakub Dawidek #define	VDD_DTL		0x02
5341ba4a712SPawel Jakub Dawidek 
5351ba4a712SPawel Jakub Dawidek /*
5361ba4a712SPawel Jakub Dawidek  * Size and offset of embedded boot loader region on each label.
5371ba4a712SPawel Jakub Dawidek  * The total size of the first two labels plus the boot area is 4MB.
5381ba4a712SPawel Jakub Dawidek  */
5391ba4a712SPawel Jakub Dawidek #define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
5401ba4a712SPawel Jakub Dawidek #define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M	*/
5411ba4a712SPawel Jakub Dawidek 
5421ba4a712SPawel Jakub Dawidek /*
5431ba4a712SPawel Jakub Dawidek  * Size of label regions at the start and end of each leaf device.
5441ba4a712SPawel Jakub Dawidek  */
5451ba4a712SPawel Jakub Dawidek #define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
5461ba4a712SPawel Jakub Dawidek #define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
5471ba4a712SPawel Jakub Dawidek #define	VDEV_LABELS		4
5481ba4a712SPawel Jakub Dawidek 
5491ba4a712SPawel Jakub Dawidek enum zio_checksum {
5501ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_INHERIT = 0,
5511ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_ON,
5521ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_OFF,
5531ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_LABEL,
5541ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_GANG_HEADER,
5551ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_ZILOG,
5561ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_FLETCHER_2,
5571ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_FLETCHER_4,
5581ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_SHA256,
55910b9d77bSPawel Jakub Dawidek 	ZIO_CHECKSUM_ZILOG2,
5602c55d090SToomas Soome 	ZIO_CHECKSUM_NOPARITY,
5612c55d090SToomas Soome 	ZIO_CHECKSUM_SHA512,
5622c55d090SToomas Soome 	ZIO_CHECKSUM_SKEIN,
5632c55d090SToomas Soome 	ZIO_CHECKSUM_EDONR,
5641ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_FUNCTIONS
5651ba4a712SPawel Jakub Dawidek };
5661ba4a712SPawel Jakub Dawidek 
56710b9d77bSPawel Jakub Dawidek #define	ZIO_CHECKSUM_ON_VALUE	ZIO_CHECKSUM_FLETCHER_4
5681ba4a712SPawel Jakub Dawidek #define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
5691ba4a712SPawel Jakub Dawidek 
5701ba4a712SPawel Jakub Dawidek enum zio_compress {
5711ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_INHERIT = 0,
5721ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_ON,
5731ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_OFF,
5741ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_LZJB,
5751ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_EMPTY,
5761ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_1,
5771ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_2,
5781ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_3,
5791ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_4,
5801ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_5,
5811ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_6,
5821ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_7,
5831ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_8,
5841ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_9,
58510b9d77bSPawel Jakub Dawidek 	ZIO_COMPRESS_ZLE,
586ef17620fSXin LI 	ZIO_COMPRESS_LZ4,
5871ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_FUNCTIONS
5881ba4a712SPawel Jakub Dawidek };
5891ba4a712SPawel Jakub Dawidek 
5901ba4a712SPawel Jakub Dawidek #define	ZIO_COMPRESS_ON_VALUE	ZIO_COMPRESS_LZJB
5911ba4a712SPawel Jakub Dawidek #define	ZIO_COMPRESS_DEFAULT	ZIO_COMPRESS_OFF
5921ba4a712SPawel Jakub Dawidek 
5931ba4a712SPawel Jakub Dawidek /* nvlist pack encoding */
5941ba4a712SPawel Jakub Dawidek #define	NV_ENCODE_NATIVE	0
5951ba4a712SPawel Jakub Dawidek #define	NV_ENCODE_XDR		1
5961ba4a712SPawel Jakub Dawidek 
5971ba4a712SPawel Jakub Dawidek typedef enum {
5981ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UNKNOWN = 0,
5991ba4a712SPawel Jakub Dawidek 	DATA_TYPE_BOOLEAN,
6001ba4a712SPawel Jakub Dawidek 	DATA_TYPE_BYTE,
6011ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT16,
6021ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT16,
6031ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT32,
6041ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT32,
6051ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT64,
6061ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT64,
6071ba4a712SPawel Jakub Dawidek 	DATA_TYPE_STRING,
6081ba4a712SPawel Jakub Dawidek 	DATA_TYPE_BYTE_ARRAY,
6091ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT16_ARRAY,
6101ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT16_ARRAY,
6111ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT32_ARRAY,
6121ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT32_ARRAY,
6131ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT64_ARRAY,
6141ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT64_ARRAY,
6151ba4a712SPawel Jakub Dawidek 	DATA_TYPE_STRING_ARRAY,
6161ba4a712SPawel Jakub Dawidek 	DATA_TYPE_HRTIME,
6171ba4a712SPawel Jakub Dawidek 	DATA_TYPE_NVLIST,
6181ba4a712SPawel Jakub Dawidek 	DATA_TYPE_NVLIST_ARRAY,
6191ba4a712SPawel Jakub Dawidek 	DATA_TYPE_BOOLEAN_VALUE,
6201ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT8,
6211ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT8,
6221ba4a712SPawel Jakub Dawidek 	DATA_TYPE_BOOLEAN_ARRAY,
6231ba4a712SPawel Jakub Dawidek 	DATA_TYPE_INT8_ARRAY,
6241ba4a712SPawel Jakub Dawidek 	DATA_TYPE_UINT8_ARRAY
6251ba4a712SPawel Jakub Dawidek } data_type_t;
6261ba4a712SPawel Jakub Dawidek 
6271ba4a712SPawel Jakub Dawidek /*
6281ba4a712SPawel Jakub Dawidek  * On-disk version number.
6291ba4a712SPawel Jakub Dawidek  */
6300d16312bSDoug Rabson #define	SPA_VERSION_1			1ULL
6310d16312bSDoug Rabson #define	SPA_VERSION_2			2ULL
6320d16312bSDoug Rabson #define	SPA_VERSION_3			3ULL
6330d16312bSDoug Rabson #define	SPA_VERSION_4			4ULL
6340d16312bSDoug Rabson #define	SPA_VERSION_5			5ULL
6350d16312bSDoug Rabson #define	SPA_VERSION_6			6ULL
6360d16312bSDoug Rabson #define	SPA_VERSION_7			7ULL
6370d16312bSDoug Rabson #define	SPA_VERSION_8			8ULL
6380d16312bSDoug Rabson #define	SPA_VERSION_9			9ULL
6390d16312bSDoug Rabson #define	SPA_VERSION_10			10ULL
6400d16312bSDoug Rabson #define	SPA_VERSION_11			11ULL
641786895f6SDoug Rabson #define	SPA_VERSION_12			12ULL
642786895f6SDoug Rabson #define	SPA_VERSION_13			13ULL
64391891290SXin LI #define	SPA_VERSION_14			14ULL
6448fc25799SMartin Matuska #define	SPA_VERSION_15			15ULL
64510b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_16			16ULL
64610b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_17			17ULL
64710b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_18			18ULL
64810b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_19			19ULL
64910b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_20			20ULL
65010b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_21			21ULL
65110b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_22			22ULL
65210b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_23			23ULL
65310b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_24			24ULL
65410b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_25			25ULL
65510b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_26			26ULL
65610b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_27			27ULL
65710b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_28			28ULL
6582d9cf57eSMartin Matuska #define	SPA_VERSION_5000		5000ULL
65910b9d77bSPawel Jakub Dawidek 
6601ba4a712SPawel Jakub Dawidek /*
66110b9d77bSPawel Jakub Dawidek  * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
66210b9d77bSPawel Jakub Dawidek  * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
66310b9d77bSPawel Jakub Dawidek  * and do the appropriate changes.  Also bump the version number in
66410b9d77bSPawel Jakub Dawidek  * usr/src/grub/capability.
6651ba4a712SPawel Jakub Dawidek  */
6662d9cf57eSMartin Matuska #define	SPA_VERSION			SPA_VERSION_5000
6672d9cf57eSMartin Matuska #define	SPA_VERSION_STRING		"5000"
6681ba4a712SPawel Jakub Dawidek 
6691ba4a712SPawel Jakub Dawidek /*
6700d16312bSDoug Rabson  * Symbolic names for the changes that caused a SPA_VERSION switch.
6711ba4a712SPawel Jakub Dawidek  * Used in the code when checking for presence or absence of a feature.
6721ba4a712SPawel Jakub Dawidek  * Feel free to define multiple symbolic names for each version if there
6731ba4a712SPawel Jakub Dawidek  * were multiple changes to on-disk structures during that version.
6741ba4a712SPawel Jakub Dawidek  *
6750d16312bSDoug Rabson  * NOTE: When checking the current SPA_VERSION in your code, be sure
6761ba4a712SPawel Jakub Dawidek  *       to use spa_version() since it reports the version of the
6771ba4a712SPawel Jakub Dawidek  *       last synced uberblock.  Checking the in-flight version can
6781ba4a712SPawel Jakub Dawidek  *       be dangerous in some cases.
6791ba4a712SPawel Jakub Dawidek  */
6800d16312bSDoug Rabson #define	SPA_VERSION_INITIAL		SPA_VERSION_1
6810d16312bSDoug Rabson #define	SPA_VERSION_DITTO_BLOCKS	SPA_VERSION_2
6820d16312bSDoug Rabson #define	SPA_VERSION_SPARES		SPA_VERSION_3
6830d16312bSDoug Rabson #define	SPA_VERSION_RAID6		SPA_VERSION_3
6840d16312bSDoug Rabson #define	SPA_VERSION_BPLIST_ACCOUNT	SPA_VERSION_3
6850d16312bSDoug Rabson #define	SPA_VERSION_RAIDZ_DEFLATE	SPA_VERSION_3
6860d16312bSDoug Rabson #define	SPA_VERSION_DNODE_BYTES		SPA_VERSION_3
6870d16312bSDoug Rabson #define	SPA_VERSION_ZPOOL_HISTORY	SPA_VERSION_4
6880d16312bSDoug Rabson #define	SPA_VERSION_GZIP_COMPRESSION	SPA_VERSION_5
6890d16312bSDoug Rabson #define	SPA_VERSION_BOOTFS		SPA_VERSION_6
690786895f6SDoug Rabson #define	SPA_VERSION_SLOGS		SPA_VERSION_7
691786895f6SDoug Rabson #define	SPA_VERSION_DELEGATED_PERMS	SPA_VERSION_8
692786895f6SDoug Rabson #define	SPA_VERSION_FUID		SPA_VERSION_9
693786895f6SDoug Rabson #define	SPA_VERSION_REFRESERVATION	SPA_VERSION_9
694786895f6SDoug Rabson #define	SPA_VERSION_REFQUOTA		SPA_VERSION_9
695786895f6SDoug Rabson #define	SPA_VERSION_UNIQUE_ACCURATE	SPA_VERSION_9
696786895f6SDoug Rabson #define	SPA_VERSION_L2CACHE		SPA_VERSION_10
697786895f6SDoug Rabson #define	SPA_VERSION_NEXT_CLONES		SPA_VERSION_11
698786895f6SDoug Rabson #define	SPA_VERSION_ORIGIN		SPA_VERSION_11
699786895f6SDoug Rabson #define	SPA_VERSION_DSL_SCRUB		SPA_VERSION_11
700786895f6SDoug Rabson #define	SPA_VERSION_SNAP_PROPS		SPA_VERSION_12
701786895f6SDoug Rabson #define	SPA_VERSION_USED_BREAKDOWN	SPA_VERSION_13
70291891290SXin LI #define	SPA_VERSION_PASSTHROUGH_X	SPA_VERSION_14
7038fc25799SMartin Matuska #define SPA_VERSION_USERSPACE		SPA_VERSION_15
70410b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_STMF_PROP		SPA_VERSION_16
70510b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_RAIDZ3		SPA_VERSION_17
70610b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_USERREFS		SPA_VERSION_18
70710b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_HOLES		SPA_VERSION_19
70810b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_ZLE_COMPRESSION	SPA_VERSION_20
70910b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_DEDUP		SPA_VERSION_21
71010b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_RECVD_PROPS		SPA_VERSION_22
71110b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_SLIM_ZIL		SPA_VERSION_23
71210b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_SA			SPA_VERSION_24
71310b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_SCAN		SPA_VERSION_25
71410b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_DIR_CLONES		SPA_VERSION_26
71510b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_DEADLISTS		SPA_VERSION_26
71610b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_FAST_SNAP		SPA_VERSION_27
71710b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_MULTI_REPLACE	SPA_VERSION_28
7182d9cf57eSMartin Matuska #define	SPA_VERSION_BEFORE_FEATURES	SPA_VERSION_28
7192d9cf57eSMartin Matuska #define	SPA_VERSION_FEATURES		SPA_VERSION_5000
7202d9cf57eSMartin Matuska 
7212d9cf57eSMartin Matuska #define	SPA_VERSION_IS_SUPPORTED(v) \
7222d9cf57eSMartin Matuska 	(((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
7232d9cf57eSMartin Matuska 	((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
7241ba4a712SPawel Jakub Dawidek 
7251ba4a712SPawel Jakub Dawidek /*
7261ba4a712SPawel Jakub Dawidek  * The following are configuration names used in the nvlist describing a pool's
7271ba4a712SPawel Jakub Dawidek  * configuration.
7281ba4a712SPawel Jakub Dawidek  */
7291ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_VERSION		"version"
7301ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_NAME		"name"
7311ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_STATE		"state"
7321ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_TXG		"txg"
7331ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_GUID		"pool_guid"
7341ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_CREATE_TXG		"create_txg"
7351ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_TOP_GUID		"top_guid"
7361ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_VDEV_TREE		"vdev_tree"
7371ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_TYPE		"type"
7381ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_CHILDREN		"children"
7391ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ID			"id"
7401ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_GUID		"guid"
741b1b93268SToomas Soome #define	ZPOOL_CONFIG_INDIRECT_OBJECT	"com.delphix:indirect_object"
742b1b93268SToomas Soome #define	ZPOOL_CONFIG_INDIRECT_BIRTHS	"com.delphix:indirect_births"
743b1b93268SToomas Soome #define	ZPOOL_CONFIG_PREV_INDIRECT_VDEV	"com.delphix:prev_indirect_vdev"
7441ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_PATH		"path"
7451ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_DEVID		"devid"
7461ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_METASLAB_ARRAY	"metaslab_array"
7471ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_METASLAB_SHIFT	"metaslab_shift"
7481ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ASHIFT		"ashift"
7491ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ASIZE		"asize"
7501ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_DTL		"DTL"
7511ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_STATS		"stats"
7521ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_WHOLE_DISK		"whole_disk"
7531ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ERRCOUNT		"error_count"
7541ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_NOT_PRESENT	"not_present"
7551ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_SPARES		"spares"
7561ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_IS_SPARE		"is_spare"
7571ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_NPARITY		"nparity"
7581ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_HOSTID		"hostid"
7591ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_HOSTNAME		"hostname"
7606a85b5e0SPawel Jakub Dawidek #define	ZPOOL_CONFIG_IS_LOG		"is_log"
7611ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_TIMESTAMP		"timestamp" /* not stored on disk */
7622d9cf57eSMartin Matuska #define	ZPOOL_CONFIG_FEATURES_FOR_READ	"features_for_read"
7631ba4a712SPawel Jakub Dawidek 
76403ec6213SXin LI /*
76503ec6213SXin LI  * The persistent vdev state is stored as separate values rather than a single
76603ec6213SXin LI  * 'vdev_state' entry.  This is because a device can be in multiple states, such
76703ec6213SXin LI  * as offline and degraded.
76803ec6213SXin LI  */
76903ec6213SXin LI #define	ZPOOL_CONFIG_OFFLINE            "offline"
77003ec6213SXin LI #define	ZPOOL_CONFIG_FAULTED            "faulted"
77103ec6213SXin LI #define	ZPOOL_CONFIG_DEGRADED           "degraded"
77203ec6213SXin LI #define	ZPOOL_CONFIG_REMOVED            "removed"
77310b9d77bSPawel Jakub Dawidek #define	ZPOOL_CONFIG_FRU		"fru"
77410b9d77bSPawel Jakub Dawidek #define	ZPOOL_CONFIG_AUX_STATE		"aux_state"
77503ec6213SXin LI 
7761ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_ROOT			"root"
7771ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_MIRROR		"mirror"
7781ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_REPLACING		"replacing"
7791ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_RAIDZ			"raidz"
7801ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_DISK			"disk"
7811ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_FILE			"file"
7821ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_MISSING		"missing"
78310b9d77bSPawel Jakub Dawidek #define	VDEV_TYPE_HOLE			"hole"
7841ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_SPARE			"spare"
78510b9d77bSPawel Jakub Dawidek #define	VDEV_TYPE_LOG			"log"
78610b9d77bSPawel Jakub Dawidek #define	VDEV_TYPE_L2CACHE		"l2cache"
787b1b93268SToomas Soome #define	VDEV_TYPE_INDIRECT		"indirect"
7881ba4a712SPawel Jakub Dawidek 
7891ba4a712SPawel Jakub Dawidek /*
7901ba4a712SPawel Jakub Dawidek  * This is needed in userland to report the minimum necessary device size.
7911ba4a712SPawel Jakub Dawidek  */
7921ba4a712SPawel Jakub Dawidek #define	SPA_MINDEVSIZE		(64ULL << 20)
7931ba4a712SPawel Jakub Dawidek 
7941ba4a712SPawel Jakub Dawidek /*
7951ba4a712SPawel Jakub Dawidek  * The location of the pool configuration repository, shared between kernel and
7961ba4a712SPawel Jakub Dawidek  * userland.
7971ba4a712SPawel Jakub Dawidek  */
79810b9d77bSPawel Jakub Dawidek #define	ZPOOL_CACHE		"/boot/zfs/zpool.cache"
7991ba4a712SPawel Jakub Dawidek 
8001ba4a712SPawel Jakub Dawidek /*
8011ba4a712SPawel Jakub Dawidek  * vdev states are ordered from least to most healthy.
8021ba4a712SPawel Jakub Dawidek  * A vdev that's CANT_OPEN or below is considered unusable.
8031ba4a712SPawel Jakub Dawidek  */
8041ba4a712SPawel Jakub Dawidek typedef enum vdev_state {
8051ba4a712SPawel Jakub Dawidek 	VDEV_STATE_UNKNOWN = 0,	/* Uninitialized vdev			*/
8061ba4a712SPawel Jakub Dawidek 	VDEV_STATE_CLOSED,	/* Not currently open			*/
8071ba4a712SPawel Jakub Dawidek 	VDEV_STATE_OFFLINE,	/* Not allowed to open			*/
80803ec6213SXin LI 	VDEV_STATE_REMOVED,	/* Explicitly removed from system	*/
8091ba4a712SPawel Jakub Dawidek 	VDEV_STATE_CANT_OPEN,	/* Tried to open, but failed		*/
81003ec6213SXin LI 	VDEV_STATE_FAULTED,	/* External request to fault device	*/
8111ba4a712SPawel Jakub Dawidek 	VDEV_STATE_DEGRADED,	/* Replicated vdev with unhealthy kids	*/
8121ba4a712SPawel Jakub Dawidek 	VDEV_STATE_HEALTHY	/* Presumed good			*/
8131ba4a712SPawel Jakub Dawidek } vdev_state_t;
8141ba4a712SPawel Jakub Dawidek 
8151ba4a712SPawel Jakub Dawidek /*
8161ba4a712SPawel Jakub Dawidek  * vdev aux states.  When a vdev is in the CANT_OPEN state, the aux field
8171ba4a712SPawel Jakub Dawidek  * of the vdev stats structure uses these constants to distinguish why.
8181ba4a712SPawel Jakub Dawidek  */
8191ba4a712SPawel Jakub Dawidek typedef enum vdev_aux {
8201ba4a712SPawel Jakub Dawidek 	VDEV_AUX_NONE,		/* no error				*/
8211ba4a712SPawel Jakub Dawidek 	VDEV_AUX_OPEN_FAILED,	/* ldi_open_*() or vn_open() failed	*/
8221ba4a712SPawel Jakub Dawidek 	VDEV_AUX_CORRUPT_DATA,	/* bad label or disk contents		*/
8231ba4a712SPawel Jakub Dawidek 	VDEV_AUX_NO_REPLICAS,	/* insufficient number of replicas	*/
8241ba4a712SPawel Jakub Dawidek 	VDEV_AUX_BAD_GUID_SUM,	/* vdev guid sum doesn't match		*/
8251ba4a712SPawel Jakub Dawidek 	VDEV_AUX_TOO_SMALL,	/* vdev size is too small		*/
8261ba4a712SPawel Jakub Dawidek 	VDEV_AUX_BAD_LABEL,	/* the label is OK but invalid		*/
8271ba4a712SPawel Jakub Dawidek 	VDEV_AUX_VERSION_NEWER,	/* on-disk version is too new		*/
8281ba4a712SPawel Jakub Dawidek 	VDEV_AUX_VERSION_OLDER,	/* on-disk version is too old		*/
8291ba4a712SPawel Jakub Dawidek 	VDEV_AUX_SPARED		/* hot spare used in another pool	*/
8301ba4a712SPawel Jakub Dawidek } vdev_aux_t;
8311ba4a712SPawel Jakub Dawidek 
8321ba4a712SPawel Jakub Dawidek /*
8331ba4a712SPawel Jakub Dawidek  * pool state.  The following states are written to disk as part of the normal
8341ba4a712SPawel Jakub Dawidek  * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE.  The remaining states are
8351ba4a712SPawel Jakub Dawidek  * software abstractions used at various levels to communicate pool state.
8361ba4a712SPawel Jakub Dawidek  */
8371ba4a712SPawel Jakub Dawidek typedef enum pool_state {
8381ba4a712SPawel Jakub Dawidek 	POOL_STATE_ACTIVE = 0,		/* In active use		*/
8391ba4a712SPawel Jakub Dawidek 	POOL_STATE_EXPORTED,		/* Explicitly exported		*/
8401ba4a712SPawel Jakub Dawidek 	POOL_STATE_DESTROYED,		/* Explicitly destroyed		*/
8411ba4a712SPawel Jakub Dawidek 	POOL_STATE_SPARE,		/* Reserved for hot spare use	*/
8421ba4a712SPawel Jakub Dawidek 	POOL_STATE_UNINITIALIZED,	/* Internal spa_t state		*/
8431ba4a712SPawel Jakub Dawidek 	POOL_STATE_UNAVAIL,		/* Internal libzfs state	*/
8441ba4a712SPawel Jakub Dawidek 	POOL_STATE_POTENTIALLY_ACTIVE	/* Internal libzfs state	*/
8451ba4a712SPawel Jakub Dawidek } pool_state_t;
8461ba4a712SPawel Jakub Dawidek 
8471ba4a712SPawel Jakub Dawidek /*
8481ba4a712SPawel Jakub Dawidek  * The uberblock version is incremented whenever an incompatible on-disk
8491ba4a712SPawel Jakub Dawidek  * format change is made to the SPA, DMU, or ZAP.
8501ba4a712SPawel Jakub Dawidek  *
8511ba4a712SPawel Jakub Dawidek  * Note: the first two fields should never be moved.  When a storage pool
8521ba4a712SPawel Jakub Dawidek  * is opened, the uberblock must be read off the disk before the version
8531ba4a712SPawel Jakub Dawidek  * can be checked.  If the ub_version field is moved, we may not detect
8541ba4a712SPawel Jakub Dawidek  * version mismatch.  If the ub_magic field is moved, applications that
8551ba4a712SPawel Jakub Dawidek  * expect the magic number in the first word won't work.
8561ba4a712SPawel Jakub Dawidek  */
8571ba4a712SPawel Jakub Dawidek #define	UBERBLOCK_MAGIC		0x00bab10c		/* oo-ba-bloc!	*/
8581ba4a712SPawel Jakub Dawidek #define	UBERBLOCK_SHIFT		10			/* up to 1K	*/
8591ba4a712SPawel Jakub Dawidek 
860*79a4bf89SToomas Soome #define	MMP_MAGIC		0xa11cea11		/* all-see-all  */
861*79a4bf89SToomas Soome 
862*79a4bf89SToomas Soome #define	MMP_INTERVAL_VALID_BIT	0x01
863*79a4bf89SToomas Soome #define	MMP_SEQ_VALID_BIT	0x02
864*79a4bf89SToomas Soome #define	MMP_FAIL_INT_VALID_BIT	0x04
865*79a4bf89SToomas Soome 
866*79a4bf89SToomas Soome #define	MMP_VALID(ubp)		(ubp->ub_magic == UBERBLOCK_MAGIC && \
867*79a4bf89SToomas Soome 				    ubp->ub_mmp_magic == MMP_MAGIC)
868*79a4bf89SToomas Soome #define	MMP_INTERVAL_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
869*79a4bf89SToomas Soome 				    MMP_INTERVAL_VALID_BIT))
870*79a4bf89SToomas Soome #define	MMP_SEQ_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
871*79a4bf89SToomas Soome 				    MMP_SEQ_VALID_BIT))
872*79a4bf89SToomas Soome #define	MMP_FAIL_INT_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
873*79a4bf89SToomas Soome 				    MMP_FAIL_INT_VALID_BIT))
874*79a4bf89SToomas Soome 
875*79a4bf89SToomas Soome #define	MMP_INTERVAL(ubp)	((ubp->ub_mmp_config & 0x00000000FFFFFF00) \
876*79a4bf89SToomas Soome 				    >> 8)
877*79a4bf89SToomas Soome #define	MMP_SEQ(ubp)		((ubp->ub_mmp_config & 0x0000FFFF00000000) \
878*79a4bf89SToomas Soome 				    >> 32)
879*79a4bf89SToomas Soome #define	MMP_FAIL_INT(ubp)	((ubp->ub_mmp_config & 0xFFFF000000000000) \
880*79a4bf89SToomas Soome 				    >> 48)
881*79a4bf89SToomas Soome 
882*79a4bf89SToomas Soome typedef struct uberblock {
8831ba4a712SPawel Jakub Dawidek 	uint64_t	ub_magic;	/* UBERBLOCK_MAGIC		*/
8840d16312bSDoug Rabson 	uint64_t	ub_version;	/* SPA_VERSION			*/
8851ba4a712SPawel Jakub Dawidek 	uint64_t	ub_txg;		/* txg of last sync		*/
8861ba4a712SPawel Jakub Dawidek 	uint64_t	ub_guid_sum;	/* sum of all vdev guids	*/
8871ba4a712SPawel Jakub Dawidek 	uint64_t	ub_timestamp;	/* UTC time of last sync	*/
8881ba4a712SPawel Jakub Dawidek 	blkptr_t	ub_rootbp;	/* MOS objset_phys_t		*/
889*79a4bf89SToomas Soome 	/* highest SPA_VERSION supported by software that wrote this txg */
890*79a4bf89SToomas Soome 	uint64_t	ub_software_version;
891*79a4bf89SToomas Soome 	/* Maybe missing in uberblocks we read, but always written */
892*79a4bf89SToomas Soome 	uint64_t	ub_mmp_magic;
893*79a4bf89SToomas Soome 	/*
894*79a4bf89SToomas Soome 	 * If ub_mmp_delay == 0 and ub_mmp_magic is valid, MMP is off.
895*79a4bf89SToomas Soome 	 * Otherwise, nanosec since last MMP write.
896*79a4bf89SToomas Soome 	 */
897*79a4bf89SToomas Soome 	uint64_t	ub_mmp_delay;
898*79a4bf89SToomas Soome 
899*79a4bf89SToomas Soome 	/*
900*79a4bf89SToomas Soome 	 * The ub_mmp_config contains the multihost write interval, multihost
901*79a4bf89SToomas Soome 	 * fail intervals, sequence number for sub-second granularity, and
902*79a4bf89SToomas Soome 	 * valid bit mask.  This layout is as follows:
903*79a4bf89SToomas Soome 	 *
904*79a4bf89SToomas Soome 	 *   64      56      48      40      32      24      16      8       0
905*79a4bf89SToomas Soome 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
906*79a4bf89SToomas Soome 	 * 0 | Fail Intervals|      Seq      |   Write Interval (ms) | VALID |
907*79a4bf89SToomas Soome 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
908*79a4bf89SToomas Soome 	 *
909*79a4bf89SToomas Soome 	 * This allows a write_interval of (2^24/1000)s, over 4.5 hours
910*79a4bf89SToomas Soome 	 *
911*79a4bf89SToomas Soome 	 * VALID Bits:
912*79a4bf89SToomas Soome 	 * - 0x01 - Write Interval (ms)
913*79a4bf89SToomas Soome 	 * - 0x02 - Sequence number exists
914*79a4bf89SToomas Soome 	 * - 0x04 - Fail Intervals
915*79a4bf89SToomas Soome 	 * - 0xf8 - Reserved
916*79a4bf89SToomas Soome 	 */
917*79a4bf89SToomas Soome 	uint64_t	ub_mmp_config;
918*79a4bf89SToomas Soome 
919*79a4bf89SToomas Soome 	/*
920*79a4bf89SToomas Soome 	 * ub_checkpoint_txg indicates two things about the current uberblock:
921*79a4bf89SToomas Soome 	 *
922*79a4bf89SToomas Soome 	 * 1] If it is not zero then this uberblock is a checkpoint. If it is
923*79a4bf89SToomas Soome 	 *    zero, then this uberblock is not a checkpoint.
924*79a4bf89SToomas Soome 	 *
925*79a4bf89SToomas Soome 	 * 2] On checkpointed uberblocks, the value of ub_checkpoint_txg is
926*79a4bf89SToomas Soome 	 *    the ub_txg that the uberblock had at the time we moved it to
927*79a4bf89SToomas Soome 	 *    the MOS config.
928*79a4bf89SToomas Soome 	 *
929*79a4bf89SToomas Soome 	 * The field is set when we checkpoint the uberblock and continues to
930*79a4bf89SToomas Soome 	 * hold that value even after we've rewound (unlike the ub_txg that
931*79a4bf89SToomas Soome 	 * is reset to a higher value).
932*79a4bf89SToomas Soome 	 *
933*79a4bf89SToomas Soome 	 * Besides checks used to determine whether we are reopening the
934*79a4bf89SToomas Soome 	 * pool from a checkpointed uberblock [see spa_ld_select_uberblock()],
935*79a4bf89SToomas Soome 	 * the value of the field is used to determine which ZIL blocks have
936*79a4bf89SToomas Soome 	 * been allocated according to the ms_sm when we are rewinding to a
937*79a4bf89SToomas Soome 	 * checkpoint. Specifically, if blk_birth > ub_checkpoint_txg, then
938*79a4bf89SToomas Soome 	 * the ZIL block is not allocated [see uses of spa_min_claim_txg()].
939*79a4bf89SToomas Soome 	 */
940*79a4bf89SToomas Soome 	uint64_t	ub_checkpoint_txg;
941*79a4bf89SToomas Soome } uberblock_t;
9421ba4a712SPawel Jakub Dawidek 
9431ba4a712SPawel Jakub Dawidek /*
9441ba4a712SPawel Jakub Dawidek  * Flags.
9451ba4a712SPawel Jakub Dawidek  */
9461ba4a712SPawel Jakub Dawidek #define	DNODE_MUST_BE_ALLOCATED	1
9471ba4a712SPawel Jakub Dawidek #define	DNODE_MUST_BE_FREE	2
9481ba4a712SPawel Jakub Dawidek 
9491ba4a712SPawel Jakub Dawidek /*
9501ba4a712SPawel Jakub Dawidek  * Fixed constants.
9511ba4a712SPawel Jakub Dawidek  */
9521ba4a712SPawel Jakub Dawidek #define	DNODE_SHIFT		9	/* 512 bytes */
953263f396eSXin LI #define	DN_MIN_INDBLKSHIFT	12	/* 4k */
954b1b93268SToomas Soome #define	DN_MAX_INDBLKSHIFT	17	/* 128k */
9551ba4a712SPawel Jakub Dawidek #define	DNODE_BLOCK_SHIFT	14	/* 16k */
9561ba4a712SPawel Jakub Dawidek #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
9571ba4a712SPawel Jakub Dawidek #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
9581ba4a712SPawel Jakub Dawidek #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
9591ba4a712SPawel Jakub Dawidek 
9601ba4a712SPawel Jakub Dawidek /*
9611ba4a712SPawel Jakub Dawidek  * Derived constants.
9621ba4a712SPawel Jakub Dawidek  */
963b40aaca6SToomas Soome #define	DNODE_MIN_SIZE		(1 << DNODE_SHIFT)
964b40aaca6SToomas Soome #define	DNODE_MAX_SIZE		(1 << DNODE_BLOCK_SHIFT)
965b40aaca6SToomas Soome #define	DNODE_BLOCK_SIZE	(1 << DNODE_BLOCK_SHIFT)
966b40aaca6SToomas Soome #define	DNODE_MIN_SLOTS		(DNODE_MIN_SIZE >> DNODE_SHIFT)
967b40aaca6SToomas Soome #define	DNODE_MAX_SLOTS		(DNODE_MAX_SIZE >> DNODE_SHIFT)
968b40aaca6SToomas Soome #define	DN_BONUS_SIZE(dnsize)	((dnsize) - DNODE_CORE_SIZE - \
969b40aaca6SToomas Soome 	(1 << SPA_BLKPTRSHIFT))
970b40aaca6SToomas Soome #define	DN_SLOTS_TO_BONUSLEN(slots)	DN_BONUS_SIZE((slots) << DNODE_SHIFT)
971b40aaca6SToomas Soome #define	DN_OLD_MAX_BONUSLEN		(DN_BONUS_SIZE(DNODE_MIN_SIZE))
972b40aaca6SToomas Soome #define	DN_MAX_NBLKPTR		((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> \
973b40aaca6SToomas Soome 	SPA_BLKPTRSHIFT)
9741ba4a712SPawel Jakub Dawidek #define	DN_MAX_OBJECT		(1ULL << DN_MAX_OBJECT_SHIFT)
975b40aaca6SToomas Soome #define	DN_ZERO_BONUSLEN	(DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
9761ba4a712SPawel Jakub Dawidek 
9771ba4a712SPawel Jakub Dawidek #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
9781ba4a712SPawel Jakub Dawidek #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
9791ba4a712SPawel Jakub Dawidek #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
9801ba4a712SPawel Jakub Dawidek 
9811ba4a712SPawel Jakub Dawidek /* The +2 here is a cheesy way to round up */
9821ba4a712SPawel Jakub Dawidek #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
9831ba4a712SPawel Jakub Dawidek 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
9841ba4a712SPawel Jakub Dawidek 
9851ba4a712SPawel Jakub Dawidek #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
9861ba4a712SPawel Jakub Dawidek 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
9871ba4a712SPawel Jakub Dawidek 
9881ba4a712SPawel Jakub Dawidek #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
9891ba4a712SPawel Jakub Dawidek 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
9901ba4a712SPawel Jakub Dawidek 
9911ba4a712SPawel Jakub Dawidek #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
9921ba4a712SPawel Jakub Dawidek 
9931ba4a712SPawel Jakub Dawidek /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
9941ba4a712SPawel Jakub Dawidek #define	DNODE_FLAG_USED_BYTES		(1<<0)
99510b9d77bSPawel Jakub Dawidek #define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
99610b9d77bSPawel Jakub Dawidek 
99710b9d77bSPawel Jakub Dawidek /* Does dnode have a SA spill blkptr in bonus? */
99810b9d77bSPawel Jakub Dawidek #define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
9991ba4a712SPawel Jakub Dawidek 
10001ba4a712SPawel Jakub Dawidek typedef struct dnode_phys {
10011ba4a712SPawel Jakub Dawidek 	uint8_t dn_type;		/* dmu_object_type_t */
10021ba4a712SPawel Jakub Dawidek 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
10031ba4a712SPawel Jakub Dawidek 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
10041ba4a712SPawel Jakub Dawidek 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
10051ba4a712SPawel Jakub Dawidek 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
10061ba4a712SPawel Jakub Dawidek 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
10071ba4a712SPawel Jakub Dawidek 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
10081ba4a712SPawel Jakub Dawidek 	uint8_t dn_flags;		/* DNODE_FLAG_* */
10091ba4a712SPawel Jakub Dawidek 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
10101ba4a712SPawel Jakub Dawidek 	uint16_t dn_bonuslen;		/* length of dn_bonus */
1011b40aaca6SToomas Soome 	uint8_t dn_extra_slots;		/* # of subsequent slots consumed */
1012b40aaca6SToomas Soome 	uint8_t dn_pad2[3];
10131ba4a712SPawel Jakub Dawidek 
10141ba4a712SPawel Jakub Dawidek 	/* accounting is protected by dn_dirty_mtx */
10151ba4a712SPawel Jakub Dawidek 	uint64_t dn_maxblkid;		/* largest allocated block ID */
10161ba4a712SPawel Jakub Dawidek 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
10171ba4a712SPawel Jakub Dawidek 
10181ba4a712SPawel Jakub Dawidek 	uint64_t dn_pad3[4];
10191ba4a712SPawel Jakub Dawidek 
1020b40aaca6SToomas Soome 	/*
1021b40aaca6SToomas Soome 	 * The tail region is 448 bytes for a 512 byte dnode, and
1022b40aaca6SToomas Soome 	 * correspondingly larger for larger dnode sizes. The spill
1023b40aaca6SToomas Soome 	 * block pointer, when present, is always at the end of the tail
1024b40aaca6SToomas Soome 	 * region. There are three ways this space may be used, using
1025b40aaca6SToomas Soome 	 * a 512 byte dnode for this diagram:
1026b40aaca6SToomas Soome 	 *
1027b40aaca6SToomas Soome 	 * 0       64      128     192     256     320     384     448 (offset)
1028b40aaca6SToomas Soome 	 * +---------------+---------------+---------------+-------+
1029b40aaca6SToomas Soome 	 * | dn_blkptr[0]  | dn_blkptr[1]  | dn_blkptr[2]  | /     |
1030b40aaca6SToomas Soome 	 * +---------------+---------------+---------------+-------+
1031b40aaca6SToomas Soome 	 * | dn_blkptr[0]  | dn_bonus[0..319]                      |
1032b40aaca6SToomas Soome 	 * +---------------+-----------------------+---------------+
1033b40aaca6SToomas Soome 	 * | dn_blkptr[0]  | dn_bonus[0..191]      | dn_spill      |
1034b40aaca6SToomas Soome 	 * +---------------+-----------------------+---------------+
1035b40aaca6SToomas Soome 	 */
1036b40aaca6SToomas Soome 	union {
1037b40aaca6SToomas Soome 		blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
1038b40aaca6SToomas Soome 		struct {
1039b40aaca6SToomas Soome 			blkptr_t __dn_ignore1;
1040b40aaca6SToomas Soome 			uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
1041b40aaca6SToomas Soome 		};
1042b40aaca6SToomas Soome 		struct {
1043b40aaca6SToomas Soome 			blkptr_t __dn_ignore2;
1044b40aaca6SToomas Soome 			uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
1045b40aaca6SToomas Soome 			    sizeof (blkptr_t)];
104610b9d77bSPawel Jakub Dawidek 			blkptr_t dn_spill;
1047b40aaca6SToomas Soome 		};
1048b40aaca6SToomas Soome 	};
10491ba4a712SPawel Jakub Dawidek } dnode_phys_t;
10501ba4a712SPawel Jakub Dawidek 
1051b40aaca6SToomas Soome #define	DN_SPILL_BLKPTR(dnp)	(blkptr_t *)((char *)(dnp) + \
1052b40aaca6SToomas Soome 	(((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT))
1053b40aaca6SToomas Soome 
10544deb8929SAllan Jude typedef enum dmu_object_byteswap {
10554deb8929SAllan Jude 	DMU_BSWAP_UINT8,
10564deb8929SAllan Jude 	DMU_BSWAP_UINT16,
10574deb8929SAllan Jude 	DMU_BSWAP_UINT32,
10584deb8929SAllan Jude 	DMU_BSWAP_UINT64,
10594deb8929SAllan Jude 	DMU_BSWAP_ZAP,
10604deb8929SAllan Jude 	DMU_BSWAP_DNODE,
10614deb8929SAllan Jude 	DMU_BSWAP_OBJSET,
10624deb8929SAllan Jude 	DMU_BSWAP_ZNODE,
10634deb8929SAllan Jude 	DMU_BSWAP_OLDACL,
10644deb8929SAllan Jude 	DMU_BSWAP_ACL,
10654deb8929SAllan Jude 	/*
10664deb8929SAllan Jude 	 * Allocating a new byteswap type number makes the on-disk format
10674deb8929SAllan Jude 	 * incompatible with any other format that uses the same number.
10684deb8929SAllan Jude 	 *
10694deb8929SAllan Jude 	 * Data can usually be structured to work with one of the
10704deb8929SAllan Jude 	 * DMU_BSWAP_UINT* or DMU_BSWAP_ZAP types.
10714deb8929SAllan Jude 	 */
10724deb8929SAllan Jude 	DMU_BSWAP_NUMFUNCS
10734deb8929SAllan Jude } dmu_object_byteswap_t;
10744deb8929SAllan Jude 
10754deb8929SAllan Jude #define	DMU_OT_NEWTYPE 0x80
10764deb8929SAllan Jude #define	DMU_OT_METADATA 0x40
10774deb8929SAllan Jude #define	DMU_OT_BYTESWAP_MASK 0x3f
10784deb8929SAllan Jude 
10794deb8929SAllan Jude /*
10804deb8929SAllan Jude  * Defines a uint8_t object type. Object types specify if the data
10814deb8929SAllan Jude  * in the object is metadata (boolean) and how to byteswap the data
10824deb8929SAllan Jude  * (dmu_object_byteswap_t).
10834deb8929SAllan Jude  */
10844deb8929SAllan Jude #define	DMU_OT(byteswap, metadata) \
10854deb8929SAllan Jude 	(DMU_OT_NEWTYPE | \
10864deb8929SAllan Jude 	((metadata) ? DMU_OT_METADATA : 0) | \
10874deb8929SAllan Jude 	((byteswap) & DMU_OT_BYTESWAP_MASK))
10884deb8929SAllan Jude 
10891ba4a712SPawel Jakub Dawidek typedef enum dmu_object_type {
10901ba4a712SPawel Jakub Dawidek 	DMU_OT_NONE,
10911ba4a712SPawel Jakub Dawidek 	/* general: */
10921ba4a712SPawel Jakub Dawidek 	DMU_OT_OBJECT_DIRECTORY,	/* ZAP */
10931ba4a712SPawel Jakub Dawidek 	DMU_OT_OBJECT_ARRAY,		/* UINT64 */
10941ba4a712SPawel Jakub Dawidek 	DMU_OT_PACKED_NVLIST,		/* UINT8 (XDR by nvlist_pack/unpack) */
10951ba4a712SPawel Jakub Dawidek 	DMU_OT_PACKED_NVLIST_SIZE,	/* UINT64 */
10961ba4a712SPawel Jakub Dawidek 	DMU_OT_BPLIST,			/* UINT64 */
10971ba4a712SPawel Jakub Dawidek 	DMU_OT_BPLIST_HDR,		/* UINT64 */
10981ba4a712SPawel Jakub Dawidek 	/* spa: */
10991ba4a712SPawel Jakub Dawidek 	DMU_OT_SPACE_MAP_HEADER,	/* UINT64 */
11001ba4a712SPawel Jakub Dawidek 	DMU_OT_SPACE_MAP,		/* UINT64 */
11011ba4a712SPawel Jakub Dawidek 	/* zil: */
11021ba4a712SPawel Jakub Dawidek 	DMU_OT_INTENT_LOG,		/* UINT64 */
11031ba4a712SPawel Jakub Dawidek 	/* dmu: */
11041ba4a712SPawel Jakub Dawidek 	DMU_OT_DNODE,			/* DNODE */
11051ba4a712SPawel Jakub Dawidek 	DMU_OT_OBJSET,			/* OBJSET */
11061ba4a712SPawel Jakub Dawidek 	/* dsl: */
11071ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DIR,			/* UINT64 */
11081ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DIR_CHILD_MAP,	/* ZAP */
11091ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DS_SNAP_MAP,		/* ZAP */
11101ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_PROPS,		/* ZAP */
11111ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DATASET,		/* UINT64 */
11121ba4a712SPawel Jakub Dawidek 	/* zpl: */
11131ba4a712SPawel Jakub Dawidek 	DMU_OT_ZNODE,			/* ZNODE */
111410b9d77bSPawel Jakub Dawidek 	DMU_OT_OLDACL,			/* Old ACL */
11151ba4a712SPawel Jakub Dawidek 	DMU_OT_PLAIN_FILE_CONTENTS,	/* UINT8 */
11161ba4a712SPawel Jakub Dawidek 	DMU_OT_DIRECTORY_CONTENTS,	/* ZAP */
11171ba4a712SPawel Jakub Dawidek 	DMU_OT_MASTER_NODE,		/* ZAP */
11181ba4a712SPawel Jakub Dawidek 	DMU_OT_UNLINKED_SET,		/* ZAP */
11191ba4a712SPawel Jakub Dawidek 	/* zvol: */
11201ba4a712SPawel Jakub Dawidek 	DMU_OT_ZVOL,			/* UINT8 */
11211ba4a712SPawel Jakub Dawidek 	DMU_OT_ZVOL_PROP,		/* ZAP */
11221ba4a712SPawel Jakub Dawidek 	/* other; for testing only! */
11231ba4a712SPawel Jakub Dawidek 	DMU_OT_PLAIN_OTHER,		/* UINT8 */
11241ba4a712SPawel Jakub Dawidek 	DMU_OT_UINT64_OTHER,		/* UINT64 */
11251ba4a712SPawel Jakub Dawidek 	DMU_OT_ZAP_OTHER,		/* ZAP */
11261ba4a712SPawel Jakub Dawidek 	/* new object types: */
11271ba4a712SPawel Jakub Dawidek 	DMU_OT_ERROR_LOG,		/* ZAP */
11281ba4a712SPawel Jakub Dawidek 	DMU_OT_SPA_HISTORY,		/* UINT8 */
11291ba4a712SPawel Jakub Dawidek 	DMU_OT_SPA_HISTORY_OFFSETS,	/* spa_his_phys_t */
11301ba4a712SPawel Jakub Dawidek 	DMU_OT_POOL_PROPS,		/* ZAP */
113110b9d77bSPawel Jakub Dawidek 	DMU_OT_DSL_PERMS,		/* ZAP */
113210b9d77bSPawel Jakub Dawidek 	DMU_OT_ACL,			/* ACL */
113310b9d77bSPawel Jakub Dawidek 	DMU_OT_SYSACL,			/* SYSACL */
113410b9d77bSPawel Jakub Dawidek 	DMU_OT_FUID,			/* FUID table (Packed NVLIST UINT8) */
113510b9d77bSPawel Jakub Dawidek 	DMU_OT_FUID_SIZE,		/* FUID table size UINT64 */
113610b9d77bSPawel Jakub Dawidek 	DMU_OT_NEXT_CLONES,		/* ZAP */
113710b9d77bSPawel Jakub Dawidek 	DMU_OT_SCAN_QUEUE,		/* ZAP */
113810b9d77bSPawel Jakub Dawidek 	DMU_OT_USERGROUP_USED,		/* ZAP */
113910b9d77bSPawel Jakub Dawidek 	DMU_OT_USERGROUP_QUOTA,		/* ZAP */
114010b9d77bSPawel Jakub Dawidek 	DMU_OT_USERREFS,		/* ZAP */
114110b9d77bSPawel Jakub Dawidek 	DMU_OT_DDT_ZAP,			/* ZAP */
114210b9d77bSPawel Jakub Dawidek 	DMU_OT_DDT_STATS,		/* ZAP */
114310b9d77bSPawel Jakub Dawidek 	DMU_OT_SA,			/* System attr */
114410b9d77bSPawel Jakub Dawidek 	DMU_OT_SA_MASTER_NODE,		/* ZAP */
114510b9d77bSPawel Jakub Dawidek 	DMU_OT_SA_ATTR_REGISTRATION,	/* ZAP */
114610b9d77bSPawel Jakub Dawidek 	DMU_OT_SA_ATTR_LAYOUTS,		/* ZAP */
114710b9d77bSPawel Jakub Dawidek 	DMU_OT_SCAN_XLATE,		/* ZAP */
114810b9d77bSPawel Jakub Dawidek 	DMU_OT_DEDUP,			/* fake dedup BP from ddt_bp_create() */
11494deb8929SAllan Jude 	DMU_OT_NUMTYPES,
11504deb8929SAllan Jude 
11514deb8929SAllan Jude 	/*
11524deb8929SAllan Jude 	 * Names for valid types declared with DMU_OT().
11534deb8929SAllan Jude 	 */
11544deb8929SAllan Jude 	DMU_OTN_UINT8_DATA = DMU_OT(DMU_BSWAP_UINT8, B_FALSE),
11554deb8929SAllan Jude 	DMU_OTN_UINT8_METADATA = DMU_OT(DMU_BSWAP_UINT8, B_TRUE),
11564deb8929SAllan Jude 	DMU_OTN_UINT16_DATA = DMU_OT(DMU_BSWAP_UINT16, B_FALSE),
11574deb8929SAllan Jude 	DMU_OTN_UINT16_METADATA = DMU_OT(DMU_BSWAP_UINT16, B_TRUE),
11584deb8929SAllan Jude 	DMU_OTN_UINT32_DATA = DMU_OT(DMU_BSWAP_UINT32, B_FALSE),
11594deb8929SAllan Jude 	DMU_OTN_UINT32_METADATA = DMU_OT(DMU_BSWAP_UINT32, B_TRUE),
11604deb8929SAllan Jude 	DMU_OTN_UINT64_DATA = DMU_OT(DMU_BSWAP_UINT64, B_FALSE),
11614deb8929SAllan Jude 	DMU_OTN_UINT64_METADATA = DMU_OT(DMU_BSWAP_UINT64, B_TRUE),
11624deb8929SAllan Jude 	DMU_OTN_ZAP_DATA = DMU_OT(DMU_BSWAP_ZAP, B_FALSE),
11634deb8929SAllan Jude 	DMU_OTN_ZAP_METADATA = DMU_OT(DMU_BSWAP_ZAP, B_TRUE)
11641ba4a712SPawel Jakub Dawidek } dmu_object_type_t;
11651ba4a712SPawel Jakub Dawidek 
11661ba4a712SPawel Jakub Dawidek typedef enum dmu_objset_type {
11671ba4a712SPawel Jakub Dawidek 	DMU_OST_NONE,
11681ba4a712SPawel Jakub Dawidek 	DMU_OST_META,
11691ba4a712SPawel Jakub Dawidek 	DMU_OST_ZFS,
11701ba4a712SPawel Jakub Dawidek 	DMU_OST_ZVOL,
11711ba4a712SPawel Jakub Dawidek 	DMU_OST_OTHER,			/* For testing only! */
11721ba4a712SPawel Jakub Dawidek 	DMU_OST_ANY,			/* Be careful! */
11731ba4a712SPawel Jakub Dawidek 	DMU_OST_NUMTYPES
11741ba4a712SPawel Jakub Dawidek } dmu_objset_type_t;
11751ba4a712SPawel Jakub Dawidek 
11761ba4a712SPawel Jakub Dawidek /*
117710b9d77bSPawel Jakub Dawidek  * header for all bonus and spill buffers.
117810b9d77bSPawel Jakub Dawidek  * The header has a fixed portion with a variable number
117910b9d77bSPawel Jakub Dawidek  * of "lengths" depending on the number of variable sized
118010b9d77bSPawel Jakub Dawidek  * attribues which are determined by the "layout number"
118110b9d77bSPawel Jakub Dawidek  */
118210b9d77bSPawel Jakub Dawidek 
118310b9d77bSPawel Jakub Dawidek #define	SA_MAGIC	0x2F505A  /* ZFS SA */
118410b9d77bSPawel Jakub Dawidek typedef struct sa_hdr_phys {
118510b9d77bSPawel Jakub Dawidek 	uint32_t sa_magic;
118610b9d77bSPawel Jakub Dawidek 	uint16_t sa_layout_info;  /* Encoded with hdrsize and layout number */
118710b9d77bSPawel Jakub Dawidek 	uint16_t sa_lengths[1];	/* optional sizes for variable length attrs */
118810b9d77bSPawel Jakub Dawidek 	/* ... Data follows the lengths.  */
118910b9d77bSPawel Jakub Dawidek } sa_hdr_phys_t;
119010b9d77bSPawel Jakub Dawidek 
119110b9d77bSPawel Jakub Dawidek /*
119210b9d77bSPawel Jakub Dawidek  * sa_hdr_phys -> sa_layout_info
119310b9d77bSPawel Jakub Dawidek  *
119410b9d77bSPawel Jakub Dawidek  * 16      10       0
119510b9d77bSPawel Jakub Dawidek  * +--------+-------+
119610b9d77bSPawel Jakub Dawidek  * | hdrsz  |layout |
119710b9d77bSPawel Jakub Dawidek  * +--------+-------+
119810b9d77bSPawel Jakub Dawidek  *
119910b9d77bSPawel Jakub Dawidek  * Bits 0-10 are the layout number
120010b9d77bSPawel Jakub Dawidek  * Bits 11-16 are the size of the header.
120110b9d77bSPawel Jakub Dawidek  * The hdrsize is the number * 8
120210b9d77bSPawel Jakub Dawidek  *
120310b9d77bSPawel Jakub Dawidek  * For example.
120410b9d77bSPawel Jakub Dawidek  * hdrsz of 1 ==> 8 byte header
120510b9d77bSPawel Jakub Dawidek  *          2 ==> 16 byte header
120610b9d77bSPawel Jakub Dawidek  *
120710b9d77bSPawel Jakub Dawidek  */
120810b9d77bSPawel Jakub Dawidek 
120910b9d77bSPawel Jakub Dawidek #define	SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10)
121010b9d77bSPawel Jakub Dawidek #define	SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 16, 3, 0)
121110b9d77bSPawel Jakub Dawidek #define	SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \
121210b9d77bSPawel Jakub Dawidek { \
121310b9d77bSPawel Jakub Dawidek 	BF32_SET_SB(x, 10, 6, 3, 0, size); \
121410b9d77bSPawel Jakub Dawidek 	BF32_SET(x, 0, 10, num); \
121510b9d77bSPawel Jakub Dawidek }
121610b9d77bSPawel Jakub Dawidek 
121710b9d77bSPawel Jakub Dawidek #define	SA_MODE_OFFSET		0
121810b9d77bSPawel Jakub Dawidek #define	SA_SIZE_OFFSET		8
121910b9d77bSPawel Jakub Dawidek #define	SA_GEN_OFFSET		16
122010b9d77bSPawel Jakub Dawidek #define	SA_UID_OFFSET		24
122110b9d77bSPawel Jakub Dawidek #define	SA_GID_OFFSET		32
122210b9d77bSPawel Jakub Dawidek #define	SA_PARENT_OFFSET	40
122384a6eddcSToomas Soome #define	SA_SYMLINK_OFFSET	160
122410b9d77bSPawel Jakub Dawidek 
122527e05a19SMatt Macy #define	ZIO_OBJSET_MAC_LEN		32
122627e05a19SMatt Macy 
122710b9d77bSPawel Jakub Dawidek /*
12281ba4a712SPawel Jakub Dawidek  * Intent log header - this on disk structure holds fields to manage
12291ba4a712SPawel Jakub Dawidek  * the log.  All fields are 64 bit to easily handle cross architectures.
12301ba4a712SPawel Jakub Dawidek  */
12311ba4a712SPawel Jakub Dawidek typedef struct zil_header {
12321ba4a712SPawel Jakub Dawidek 	uint64_t zh_claim_txg;	/* txg in which log blocks were claimed */
12331ba4a712SPawel Jakub Dawidek 	uint64_t zh_replay_seq;	/* highest replayed sequence number */
12341ba4a712SPawel Jakub Dawidek 	blkptr_t zh_log;	/* log chain */
12351ba4a712SPawel Jakub Dawidek 	uint64_t zh_claim_seq;	/* highest claimed sequence number */
12361ba4a712SPawel Jakub Dawidek 	uint64_t zh_pad[5];
12371ba4a712SPawel Jakub Dawidek } zil_header_t;
12381ba4a712SPawel Jakub Dawidek 
123927e05a19SMatt Macy #define	OBJSET_PHYS_SIZE_V2 2048
124027e05a19SMatt Macy #define	OBJSET_PHYS_SIZE_V3 4096
124110b9d77bSPawel Jakub Dawidek 
12421ba4a712SPawel Jakub Dawidek typedef struct objset_phys {
12431ba4a712SPawel Jakub Dawidek 	dnode_phys_t os_meta_dnode;
12441ba4a712SPawel Jakub Dawidek 	zil_header_t os_zil_header;
12451ba4a712SPawel Jakub Dawidek 	uint64_t os_type;
12468fc25799SMartin Matuska 	uint64_t os_flags;
124727e05a19SMatt Macy 	uint8_t os_portable_mac[ZIO_OBJSET_MAC_LEN];
124827e05a19SMatt Macy 	uint8_t os_local_mac[ZIO_OBJSET_MAC_LEN];
124927e05a19SMatt Macy 	char os_pad0[OBJSET_PHYS_SIZE_V2 - sizeof (dnode_phys_t)*3 -
125027e05a19SMatt Macy 		sizeof (zil_header_t) - sizeof (uint64_t)*2 -
125127e05a19SMatt Macy 		2*ZIO_OBJSET_MAC_LEN];
12528fc25799SMartin Matuska 	dnode_phys_t os_userused_dnode;
12538fc25799SMartin Matuska 	dnode_phys_t os_groupused_dnode;
125427e05a19SMatt Macy 	dnode_phys_t os_projectused_dnode;
125527e05a19SMatt Macy 	char os_pad1[OBJSET_PHYS_SIZE_V3 - OBJSET_PHYS_SIZE_V2 -
125627e05a19SMatt Macy 	    sizeof (dnode_phys_t)];
12571ba4a712SPawel Jakub Dawidek } objset_phys_t;
12581ba4a712SPawel Jakub Dawidek 
12591ba4a712SPawel Jakub Dawidek typedef struct dsl_dir_phys {
12601ba4a712SPawel Jakub Dawidek 	uint64_t dd_creation_time; /* not actually used */
12611ba4a712SPawel Jakub Dawidek 	uint64_t dd_head_dataset_obj;
12621ba4a712SPawel Jakub Dawidek 	uint64_t dd_parent_obj;
12631ba4a712SPawel Jakub Dawidek 	uint64_t dd_clone_parent_obj;
12641ba4a712SPawel Jakub Dawidek 	uint64_t dd_child_dir_zapobj;
12651ba4a712SPawel Jakub Dawidek 	/*
12661ba4a712SPawel Jakub Dawidek 	 * how much space our children are accounting for; for leaf
12671ba4a712SPawel Jakub Dawidek 	 * datasets, == physical space used by fs + snaps
12681ba4a712SPawel Jakub Dawidek 	 */
12691ba4a712SPawel Jakub Dawidek 	uint64_t dd_used_bytes;
12701ba4a712SPawel Jakub Dawidek 	uint64_t dd_compressed_bytes;
12711ba4a712SPawel Jakub Dawidek 	uint64_t dd_uncompressed_bytes;
12721ba4a712SPawel Jakub Dawidek 	/* Administrative quota setting */
12731ba4a712SPawel Jakub Dawidek 	uint64_t dd_quota;
12741ba4a712SPawel Jakub Dawidek 	/* Administrative reservation setting */
12751ba4a712SPawel Jakub Dawidek 	uint64_t dd_reserved;
12761ba4a712SPawel Jakub Dawidek 	uint64_t dd_props_zapobj;
12771ba4a712SPawel Jakub Dawidek 	uint64_t dd_pad[21]; /* pad out to 256 bytes for good measure */
12781ba4a712SPawel Jakub Dawidek } dsl_dir_phys_t;
12791ba4a712SPawel Jakub Dawidek 
12801ba4a712SPawel Jakub Dawidek typedef struct dsl_dataset_phys {
12811ba4a712SPawel Jakub Dawidek 	uint64_t ds_dir_obj;
12821ba4a712SPawel Jakub Dawidek 	uint64_t ds_prev_snap_obj;
12831ba4a712SPawel Jakub Dawidek 	uint64_t ds_prev_snap_txg;
12841ba4a712SPawel Jakub Dawidek 	uint64_t ds_next_snap_obj;
12851ba4a712SPawel Jakub Dawidek 	uint64_t ds_snapnames_zapobj;	/* zap obj of snaps; ==0 for snaps */
12861ba4a712SPawel Jakub Dawidek 	uint64_t ds_num_children;	/* clone/snap children; ==0 for head */
12871ba4a712SPawel Jakub Dawidek 	uint64_t ds_creation_time;	/* seconds since 1970 */
12881ba4a712SPawel Jakub Dawidek 	uint64_t ds_creation_txg;
12891ba4a712SPawel Jakub Dawidek 	uint64_t ds_deadlist_obj;
12901ba4a712SPawel Jakub Dawidek 	uint64_t ds_used_bytes;
12911ba4a712SPawel Jakub Dawidek 	uint64_t ds_compressed_bytes;
12921ba4a712SPawel Jakub Dawidek 	uint64_t ds_uncompressed_bytes;
12931ba4a712SPawel Jakub Dawidek 	uint64_t ds_unique_bytes;	/* only relevant to snapshots */
12941ba4a712SPawel Jakub Dawidek 	/*
12951ba4a712SPawel Jakub Dawidek 	 * The ds_fsid_guid is a 56-bit ID that can change to avoid
12961ba4a712SPawel Jakub Dawidek 	 * collisions.  The ds_guid is a 64-bit ID that will never
12971ba4a712SPawel Jakub Dawidek 	 * change, so there is a small probability that it will collide.
12981ba4a712SPawel Jakub Dawidek 	 */
12991ba4a712SPawel Jakub Dawidek 	uint64_t ds_fsid_guid;
13001ba4a712SPawel Jakub Dawidek 	uint64_t ds_guid;
13011ba4a712SPawel Jakub Dawidek 	uint64_t ds_flags;
13021ba4a712SPawel Jakub Dawidek 	blkptr_t ds_bp;
13031ba4a712SPawel Jakub Dawidek 	uint64_t ds_pad[8]; /* pad out to 320 bytes for good measure */
13041ba4a712SPawel Jakub Dawidek } dsl_dataset_phys_t;
13051ba4a712SPawel Jakub Dawidek 
13061ba4a712SPawel Jakub Dawidek /*
13071ba4a712SPawel Jakub Dawidek  * The names of zap entries in the DIRECTORY_OBJECT of the MOS.
13081ba4a712SPawel Jakub Dawidek  */
13091ba4a712SPawel Jakub Dawidek #define	DMU_POOL_DIRECTORY_OBJECT	1
13101ba4a712SPawel Jakub Dawidek #define	DMU_POOL_CONFIG			"config"
13114deb8929SAllan Jude #define	DMU_POOL_FEATURES_FOR_READ	"features_for_read"
13121ba4a712SPawel Jakub Dawidek #define	DMU_POOL_ROOT_DATASET		"root_dataset"
13131ba4a712SPawel Jakub Dawidek #define	DMU_POOL_SYNC_BPLIST		"sync_bplist"
13141ba4a712SPawel Jakub Dawidek #define	DMU_POOL_ERRLOG_SCRUB		"errlog_scrub"
13151ba4a712SPawel Jakub Dawidek #define	DMU_POOL_ERRLOG_LAST		"errlog_last"
13161ba4a712SPawel Jakub Dawidek #define	DMU_POOL_SPARES			"spares"
13171ba4a712SPawel Jakub Dawidek #define	DMU_POOL_DEFLATE		"deflate"
13181ba4a712SPawel Jakub Dawidek #define	DMU_POOL_HISTORY		"history"
13191ba4a712SPawel Jakub Dawidek #define	DMU_POOL_PROPS			"pool_props"
13202c55d090SToomas Soome #define	DMU_POOL_CHECKSUM_SALT		"org.illumos:checksum_salt"
1321b1b93268SToomas Soome #define	DMU_POOL_REMOVING		"com.delphix:removing"
1322b1b93268SToomas Soome #define	DMU_POOL_OBSOLETE_BPOBJ		"com.delphix:obsolete_bpobj"
1323b1b93268SToomas Soome #define	DMU_POOL_CONDENSING_INDIRECT	"com.delphix:condensing_indirect"
13241ba4a712SPawel Jakub Dawidek 
13251ba4a712SPawel Jakub Dawidek #define	ZAP_MAGIC 0x2F52AB2ABULL
13261ba4a712SPawel Jakub Dawidek 
13271ba4a712SPawel Jakub Dawidek #define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_block_shift)
13281ba4a712SPawel Jakub Dawidek 
13291ba4a712SPawel Jakub Dawidek #define	ZAP_MAXCD		(uint32_t)(-1)
13301ba4a712SPawel Jakub Dawidek #define	ZAP_HASHBITS		28
13311ba4a712SPawel Jakub Dawidek #define	MZAP_ENT_LEN		64
13321ba4a712SPawel Jakub Dawidek #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
13331ba4a712SPawel Jakub Dawidek #define	MZAP_MAX_BLKSHIFT	SPA_MAXBLOCKSHIFT
13341ba4a712SPawel Jakub Dawidek #define	MZAP_MAX_BLKSZ		(1 << MZAP_MAX_BLKSHIFT)
13351ba4a712SPawel Jakub Dawidek 
13361ba4a712SPawel Jakub Dawidek typedef struct mzap_ent_phys {
13371ba4a712SPawel Jakub Dawidek 	uint64_t mze_value;
13381ba4a712SPawel Jakub Dawidek 	uint32_t mze_cd;
13391ba4a712SPawel Jakub Dawidek 	uint16_t mze_pad;	/* in case we want to chain them someday */
13401ba4a712SPawel Jakub Dawidek 	char mze_name[MZAP_NAME_LEN];
13411ba4a712SPawel Jakub Dawidek } mzap_ent_phys_t;
13421ba4a712SPawel Jakub Dawidek 
13431ba4a712SPawel Jakub Dawidek typedef struct mzap_phys {
13441ba4a712SPawel Jakub Dawidek 	uint64_t mz_block_type;	/* ZBT_MICRO */
13451ba4a712SPawel Jakub Dawidek 	uint64_t mz_salt;
13461ba4a712SPawel Jakub Dawidek 	uint64_t mz_pad[6];
13471ba4a712SPawel Jakub Dawidek 	mzap_ent_phys_t mz_chunk[1];
13481ba4a712SPawel Jakub Dawidek 	/* actually variable size depending on block size */
13491ba4a712SPawel Jakub Dawidek } mzap_phys_t;
13501ba4a712SPawel Jakub Dawidek 
13511ba4a712SPawel Jakub Dawidek /*
13521ba4a712SPawel Jakub Dawidek  * The (fat) zap is stored in one object. It is an array of
13531ba4a712SPawel Jakub Dawidek  * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
13541ba4a712SPawel Jakub Dawidek  *
13551ba4a712SPawel Jakub Dawidek  * ptrtbl fits in first block:
13561ba4a712SPawel Jakub Dawidek  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
13571ba4a712SPawel Jakub Dawidek  *
13581ba4a712SPawel Jakub Dawidek  * ptrtbl too big for first block:
13591ba4a712SPawel Jakub Dawidek  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
13601ba4a712SPawel Jakub Dawidek  *
13611ba4a712SPawel Jakub Dawidek  */
13621ba4a712SPawel Jakub Dawidek 
13631ba4a712SPawel Jakub Dawidek #define	ZBT_LEAF		((1ULL << 63) + 0)
13641ba4a712SPawel Jakub Dawidek #define	ZBT_HEADER		((1ULL << 63) + 1)
13651ba4a712SPawel Jakub Dawidek #define	ZBT_MICRO		((1ULL << 63) + 3)
13661ba4a712SPawel Jakub Dawidek /* any other values are ptrtbl blocks */
13671ba4a712SPawel Jakub Dawidek 
13681ba4a712SPawel Jakub Dawidek /*
13691ba4a712SPawel Jakub Dawidek  * the embedded pointer table takes up half a block:
13701ba4a712SPawel Jakub Dawidek  * block size / entry size (2^3) / 2
13711ba4a712SPawel Jakub Dawidek  */
13721ba4a712SPawel Jakub Dawidek #define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
13731ba4a712SPawel Jakub Dawidek 
13741ba4a712SPawel Jakub Dawidek /*
13751ba4a712SPawel Jakub Dawidek  * The embedded pointer table starts half-way through the block.  Since
13761ba4a712SPawel Jakub Dawidek  * the pointer table itself is half the block, it starts at (64-bit)
13771ba4a712SPawel Jakub Dawidek  * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
13781ba4a712SPawel Jakub Dawidek  */
13791ba4a712SPawel Jakub Dawidek #define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
13801ba4a712SPawel Jakub Dawidek 	((uint64_t *)(zap)->zap_phys) \
13811ba4a712SPawel Jakub Dawidek 	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
13821ba4a712SPawel Jakub Dawidek 
13831ba4a712SPawel Jakub Dawidek /*
13841ba4a712SPawel Jakub Dawidek  * TAKE NOTE:
13851ba4a712SPawel Jakub Dawidek  * If zap_phys_t is modified, zap_byteswap() must be modified.
13861ba4a712SPawel Jakub Dawidek  */
13871ba4a712SPawel Jakub Dawidek typedef struct zap_phys {
13881ba4a712SPawel Jakub Dawidek 	uint64_t zap_block_type;	/* ZBT_HEADER */
13891ba4a712SPawel Jakub Dawidek 	uint64_t zap_magic;		/* ZAP_MAGIC */
13901ba4a712SPawel Jakub Dawidek 
13911ba4a712SPawel Jakub Dawidek 	struct zap_table_phys {
13921ba4a712SPawel Jakub Dawidek 		uint64_t zt_blk;	/* starting block number */
13931ba4a712SPawel Jakub Dawidek 		uint64_t zt_numblks;	/* number of blocks */
13941ba4a712SPawel Jakub Dawidek 		uint64_t zt_shift;	/* bits to index it */
13951ba4a712SPawel Jakub Dawidek 		uint64_t zt_nextblk;	/* next (larger) copy start block */
13961ba4a712SPawel Jakub Dawidek 		uint64_t zt_blks_copied; /* number source blocks copied */
13971ba4a712SPawel Jakub Dawidek 	} zap_ptrtbl;
13981ba4a712SPawel Jakub Dawidek 
13991ba4a712SPawel Jakub Dawidek 	uint64_t zap_freeblk;		/* the next free block */
14001ba4a712SPawel Jakub Dawidek 	uint64_t zap_num_leafs;		/* number of leafs */
14011ba4a712SPawel Jakub Dawidek 	uint64_t zap_num_entries;	/* number of entries */
14021ba4a712SPawel Jakub Dawidek 	uint64_t zap_salt;		/* salt to stir into hash function */
14031ba4a712SPawel Jakub Dawidek 	/*
14041ba4a712SPawel Jakub Dawidek 	 * This structure is followed by padding, and then the embedded
14051ba4a712SPawel Jakub Dawidek 	 * pointer table.  The embedded pointer table takes up second
14061ba4a712SPawel Jakub Dawidek 	 * half of the block.  It is accessed using the
14071ba4a712SPawel Jakub Dawidek 	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
14081ba4a712SPawel Jakub Dawidek 	 */
14091ba4a712SPawel Jakub Dawidek } zap_phys_t;
14101ba4a712SPawel Jakub Dawidek 
14111ba4a712SPawel Jakub Dawidek typedef struct zap_table_phys zap_table_phys_t;
14121ba4a712SPawel Jakub Dawidek 
14131ba4a712SPawel Jakub Dawidek typedef struct fat_zap {
14141ba4a712SPawel Jakub Dawidek 	int zap_block_shift;			/* block size shift */
14151ba4a712SPawel Jakub Dawidek 	zap_phys_t *zap_phys;
14161ba4a712SPawel Jakub Dawidek } fat_zap_t;
14171ba4a712SPawel Jakub Dawidek 
14181ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_MAGIC 0x2AB1EAF
14191ba4a712SPawel Jakub Dawidek 
14201ba4a712SPawel Jakub Dawidek /* chunk size = 24 bytes */
14211ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_CHUNKSIZE 24
14221ba4a712SPawel Jakub Dawidek 
14231ba4a712SPawel Jakub Dawidek /*
14241ba4a712SPawel Jakub Dawidek  * The amount of space available for chunks is:
14251ba4a712SPawel Jakub Dawidek  * block size (1<<l->l_bs) - hash entry size (2) * number of hash
14261ba4a712SPawel Jakub Dawidek  * entries - header space (2*chunksize)
14271ba4a712SPawel Jakub Dawidek  */
14281ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_NUMCHUNKS(l) \
14291ba4a712SPawel Jakub Dawidek 	(((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
14301ba4a712SPawel Jakub Dawidek 	ZAP_LEAF_CHUNKSIZE - 2)
14311ba4a712SPawel Jakub Dawidek 
14321ba4a712SPawel Jakub Dawidek /*
14331ba4a712SPawel Jakub Dawidek  * The amount of space within the chunk available for the array is:
14341ba4a712SPawel Jakub Dawidek  * chunk size - space for type (1) - space for next pointer (2)
14351ba4a712SPawel Jakub Dawidek  */
14361ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
14371ba4a712SPawel Jakub Dawidek 
14381ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
14391ba4a712SPawel Jakub Dawidek 	(((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
14401ba4a712SPawel Jakub Dawidek 
14411ba4a712SPawel Jakub Dawidek /*
14421ba4a712SPawel Jakub Dawidek  * Low water mark:  when there are only this many chunks free, start
14431ba4a712SPawel Jakub Dawidek  * growing the ptrtbl.  Ideally, this should be larger than a
14441ba4a712SPawel Jakub Dawidek  * "reasonably-sized" entry.  20 chunks is more than enough for the
14451ba4a712SPawel Jakub Dawidek  * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
14461ba4a712SPawel Jakub Dawidek  * while still being only around 3% for 16k blocks.
14471ba4a712SPawel Jakub Dawidek  */
14481ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_LOW_WATER (20)
14491ba4a712SPawel Jakub Dawidek 
14501ba4a712SPawel Jakub Dawidek /*
14511ba4a712SPawel Jakub Dawidek  * The leaf hash table has block size / 2^5 (32) number of entries,
14521ba4a712SPawel Jakub Dawidek  * which should be more than enough for the maximum number of entries,
14531ba4a712SPawel Jakub Dawidek  * which is less than block size / CHUNKSIZE (24) / minimum number of
14541ba4a712SPawel Jakub Dawidek  * chunks per entry (3).
14551ba4a712SPawel Jakub Dawidek  */
14561ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
14571ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
14581ba4a712SPawel Jakub Dawidek 
14591ba4a712SPawel Jakub Dawidek /*
14601ba4a712SPawel Jakub Dawidek  * The chunks start immediately after the hash table.  The end of the
14611ba4a712SPawel Jakub Dawidek  * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
14621ba4a712SPawel Jakub Dawidek  * chunk_t.
14631ba4a712SPawel Jakub Dawidek  */
14641ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_CHUNK(l, idx) \
14651ba4a712SPawel Jakub Dawidek 	((zap_leaf_chunk_t *) \
14661ba4a712SPawel Jakub Dawidek 	((l)->l_phys->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
14671ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
14681ba4a712SPawel Jakub Dawidek 
14691ba4a712SPawel Jakub Dawidek typedef enum zap_chunk_type {
14701ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_FREE = 253,
14711ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_ENTRY = 252,
14721ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_ARRAY = 251,
14731ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_TYPE_MAX = 250
14741ba4a712SPawel Jakub Dawidek } zap_chunk_type_t;
14751ba4a712SPawel Jakub Dawidek 
14761ba4a712SPawel Jakub Dawidek /*
14771ba4a712SPawel Jakub Dawidek  * TAKE NOTE:
14781ba4a712SPawel Jakub Dawidek  * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
14791ba4a712SPawel Jakub Dawidek  */
14801ba4a712SPawel Jakub Dawidek typedef struct zap_leaf_phys {
14811ba4a712SPawel Jakub Dawidek 	struct zap_leaf_header {
14821ba4a712SPawel Jakub Dawidek 		uint64_t lh_block_type;		/* ZBT_LEAF */
14831ba4a712SPawel Jakub Dawidek 		uint64_t lh_pad1;
14841ba4a712SPawel Jakub Dawidek 		uint64_t lh_prefix;		/* hash prefix of this leaf */
14851ba4a712SPawel Jakub Dawidek 		uint32_t lh_magic;		/* ZAP_LEAF_MAGIC */
14861ba4a712SPawel Jakub Dawidek 		uint16_t lh_nfree;		/* number free chunks */
14871ba4a712SPawel Jakub Dawidek 		uint16_t lh_nentries;		/* number of entries */
14881ba4a712SPawel Jakub Dawidek 		uint16_t lh_prefix_len;		/* num bits used to id this */
14891ba4a712SPawel Jakub Dawidek 
14901ba4a712SPawel Jakub Dawidek /* above is accessable to zap, below is zap_leaf private */
14911ba4a712SPawel Jakub Dawidek 
14921ba4a712SPawel Jakub Dawidek 		uint16_t lh_freelist;		/* chunk head of free list */
14931ba4a712SPawel Jakub Dawidek 		uint8_t lh_pad2[12];
14941ba4a712SPawel Jakub Dawidek 	} l_hdr; /* 2 24-byte chunks */
14951ba4a712SPawel Jakub Dawidek 
14961ba4a712SPawel Jakub Dawidek 	/*
14971ba4a712SPawel Jakub Dawidek 	 * The header is followed by a hash table with
14981ba4a712SPawel Jakub Dawidek 	 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries.  The hash table is
14991ba4a712SPawel Jakub Dawidek 	 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
15001ba4a712SPawel Jakub Dawidek 	 * zap_leaf_chunk structures.  These structures are accessed
15011ba4a712SPawel Jakub Dawidek 	 * with the ZAP_LEAF_CHUNK() macro.
15021ba4a712SPawel Jakub Dawidek 	 */
15031ba4a712SPawel Jakub Dawidek 
15041ba4a712SPawel Jakub Dawidek 	uint16_t l_hash[1];
15051ba4a712SPawel Jakub Dawidek } zap_leaf_phys_t;
15061ba4a712SPawel Jakub Dawidek 
15071ba4a712SPawel Jakub Dawidek typedef union zap_leaf_chunk {
15081ba4a712SPawel Jakub Dawidek 	struct zap_leaf_entry {
15091ba4a712SPawel Jakub Dawidek 		uint8_t le_type; 		/* always ZAP_CHUNK_ENTRY */
15104ed4f4cfSAndriy Gapon 		uint8_t le_value_intlen;	/* size of ints */
15111ba4a712SPawel Jakub Dawidek 		uint16_t le_next;		/* next entry in hash chain */
15121ba4a712SPawel Jakub Dawidek 		uint16_t le_name_chunk;		/* first chunk of the name */
15134ed4f4cfSAndriy Gapon 		uint16_t le_name_numints;	/* bytes in name, incl null */
15141ba4a712SPawel Jakub Dawidek 		uint16_t le_value_chunk;	/* first chunk of the value */
15154ed4f4cfSAndriy Gapon 		uint16_t le_value_numints;	/* value length in ints */
15161ba4a712SPawel Jakub Dawidek 		uint32_t le_cd;			/* collision differentiator */
15171ba4a712SPawel Jakub Dawidek 		uint64_t le_hash;		/* hash value of the name */
15181ba4a712SPawel Jakub Dawidek 	} l_entry;
15191ba4a712SPawel Jakub Dawidek 	struct zap_leaf_array {
15201ba4a712SPawel Jakub Dawidek 		uint8_t la_type;		/* always ZAP_CHUNK_ARRAY */
15211ba4a712SPawel Jakub Dawidek 		uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
15221ba4a712SPawel Jakub Dawidek 		uint16_t la_next;		/* next blk or CHAIN_END */
15231ba4a712SPawel Jakub Dawidek 	} l_array;
15241ba4a712SPawel Jakub Dawidek 	struct zap_leaf_free {
15251ba4a712SPawel Jakub Dawidek 		uint8_t lf_type;		/* always ZAP_CHUNK_FREE */
15261ba4a712SPawel Jakub Dawidek 		uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
15271ba4a712SPawel Jakub Dawidek 		uint16_t lf_next;	/* next in free list, or CHAIN_END */
15281ba4a712SPawel Jakub Dawidek 	} l_free;
15291ba4a712SPawel Jakub Dawidek } zap_leaf_chunk_t;
15301ba4a712SPawel Jakub Dawidek 
15311ba4a712SPawel Jakub Dawidek typedef struct zap_leaf {
15321ba4a712SPawel Jakub Dawidek 	int l_bs;			/* block size shift */
15331ba4a712SPawel Jakub Dawidek 	zap_leaf_phys_t *l_phys;
15341ba4a712SPawel Jakub Dawidek } zap_leaf_t;
15351ba4a712SPawel Jakub Dawidek 
15361ba4a712SPawel Jakub Dawidek /*
15371ba4a712SPawel Jakub Dawidek  * Define special zfs pflags
15381ba4a712SPawel Jakub Dawidek  */
15391ba4a712SPawel Jakub Dawidek #define	ZFS_XATTR	0x1		/* is an extended attribute */
15401ba4a712SPawel Jakub Dawidek #define	ZFS_INHERIT_ACE	0x2		/* ace has inheritable ACEs */
15411ba4a712SPawel Jakub Dawidek #define	ZFS_ACL_TRIVIAL 0x4		/* files ACL is trivial */
15421ba4a712SPawel Jakub Dawidek 
15431ba4a712SPawel Jakub Dawidek #define	MASTER_NODE_OBJ	1
15441ba4a712SPawel Jakub Dawidek 
15451ba4a712SPawel Jakub Dawidek /*
15461ba4a712SPawel Jakub Dawidek  * special attributes for master node.
15471ba4a712SPawel Jakub Dawidek  */
15481ba4a712SPawel Jakub Dawidek 
15491ba4a712SPawel Jakub Dawidek #define	ZFS_FSID		"FSID"
15501ba4a712SPawel Jakub Dawidek #define	ZFS_UNLINKED_SET	"DELETE_QUEUE"
15511ba4a712SPawel Jakub Dawidek #define	ZFS_ROOT_OBJ		"ROOT"
15521ba4a712SPawel Jakub Dawidek #define	ZPL_VERSION_OBJ		"VERSION"
15531ba4a712SPawel Jakub Dawidek #define	ZFS_PROP_BLOCKPERPAGE	"BLOCKPERPAGE"
15541ba4a712SPawel Jakub Dawidek #define	ZFS_PROP_NOGROWBLOCKS	"NOGROWBLOCKS"
15551ba4a712SPawel Jakub Dawidek 
15561ba4a712SPawel Jakub Dawidek #define	ZFS_FLAG_BLOCKPERPAGE	0x1
15571ba4a712SPawel Jakub Dawidek #define	ZFS_FLAG_NOGROWBLOCKS	0x2
15581ba4a712SPawel Jakub Dawidek 
15591ba4a712SPawel Jakub Dawidek /*
15601ba4a712SPawel Jakub Dawidek  * ZPL version - rev'd whenever an incompatible on-disk format change
15611ba4a712SPawel Jakub Dawidek  * occurs.  Independent of SPA/DMU/ZAP versioning.
15621ba4a712SPawel Jakub Dawidek  */
15631ba4a712SPawel Jakub Dawidek 
15641ba4a712SPawel Jakub Dawidek #define	ZPL_VERSION		1ULL
15651ba4a712SPawel Jakub Dawidek 
15661ba4a712SPawel Jakub Dawidek /*
15671ba4a712SPawel Jakub Dawidek  * The directory entry has the type (currently unused on Solaris) in the
15681ba4a712SPawel Jakub Dawidek  * top 4 bits, and the object number in the low 48 bits.  The "middle"
15691ba4a712SPawel Jakub Dawidek  * 12 bits are unused.
15701ba4a712SPawel Jakub Dawidek  */
15711ba4a712SPawel Jakub Dawidek #define	ZFS_DIRENT_TYPE(de) BF64_GET(de, 60, 4)
15721ba4a712SPawel Jakub Dawidek #define	ZFS_DIRENT_OBJ(de) BF64_GET(de, 0, 48)
15731ba4a712SPawel Jakub Dawidek #define	ZFS_DIRENT_MAKE(type, obj) (((uint64_t)type << 60) | obj)
15741ba4a712SPawel Jakub Dawidek 
15751ba4a712SPawel Jakub Dawidek typedef struct ace {
15761ba4a712SPawel Jakub Dawidek 	uid_t		a_who;		/* uid or gid */
15771ba4a712SPawel Jakub Dawidek 	uint32_t	a_access_mask;	/* read,write,... */
15781ba4a712SPawel Jakub Dawidek 	uint16_t	a_flags;	/* see below */
15791ba4a712SPawel Jakub Dawidek 	uint16_t	a_type;		/* allow or deny */
15801ba4a712SPawel Jakub Dawidek } ace_t;
15811ba4a712SPawel Jakub Dawidek 
15821ba4a712SPawel Jakub Dawidek #define ACE_SLOT_CNT	6
15831ba4a712SPawel Jakub Dawidek 
15841ba4a712SPawel Jakub Dawidek typedef struct zfs_znode_acl {
15851ba4a712SPawel Jakub Dawidek 	uint64_t	z_acl_extern_obj;	  /* ext acl pieces */
15861ba4a712SPawel Jakub Dawidek 	uint32_t	z_acl_count;		  /* Number of ACEs */
15871ba4a712SPawel Jakub Dawidek 	uint16_t	z_acl_version;		  /* acl version */
15881ba4a712SPawel Jakub Dawidek 	uint16_t	z_acl_pad;		  /* pad */
15891ba4a712SPawel Jakub Dawidek 	ace_t		z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */
15901ba4a712SPawel Jakub Dawidek } zfs_znode_acl_t;
15911ba4a712SPawel Jakub Dawidek 
15921ba4a712SPawel Jakub Dawidek /*
15931ba4a712SPawel Jakub Dawidek  * This is the persistent portion of the znode.  It is stored
15941ba4a712SPawel Jakub Dawidek  * in the "bonus buffer" of the file.  Short symbolic links
15951ba4a712SPawel Jakub Dawidek  * are also stored in the bonus buffer.
15961ba4a712SPawel Jakub Dawidek  */
15971ba4a712SPawel Jakub Dawidek typedef struct znode_phys {
15981ba4a712SPawel Jakub Dawidek 	uint64_t zp_atime[2];		/*  0 - last file access time */
15991ba4a712SPawel Jakub Dawidek 	uint64_t zp_mtime[2];		/* 16 - last file modification time */
16001ba4a712SPawel Jakub Dawidek 	uint64_t zp_ctime[2];		/* 32 - last file change time */
16011ba4a712SPawel Jakub Dawidek 	uint64_t zp_crtime[2];		/* 48 - creation time */
16021ba4a712SPawel Jakub Dawidek 	uint64_t zp_gen;		/* 64 - generation (txg of creation) */
16031ba4a712SPawel Jakub Dawidek 	uint64_t zp_mode;		/* 72 - file mode bits */
16041ba4a712SPawel Jakub Dawidek 	uint64_t zp_size;		/* 80 - size of file */
16051ba4a712SPawel Jakub Dawidek 	uint64_t zp_parent;		/* 88 - directory parent (`..') */
16061ba4a712SPawel Jakub Dawidek 	uint64_t zp_links;		/* 96 - number of links to file */
16071ba4a712SPawel Jakub Dawidek 	uint64_t zp_xattr;		/* 104 - DMU object for xattrs */
16081ba4a712SPawel Jakub Dawidek 	uint64_t zp_rdev;		/* 112 - dev_t for VBLK & VCHR files */
16091ba4a712SPawel Jakub Dawidek 	uint64_t zp_flags;		/* 120 - persistent flags */
16101ba4a712SPawel Jakub Dawidek 	uint64_t zp_uid;		/* 128 - file owner */
16111ba4a712SPawel Jakub Dawidek 	uint64_t zp_gid;		/* 136 - owning group */
16121ba4a712SPawel Jakub Dawidek 	uint64_t zp_pad[4];		/* 144 - future */
16131ba4a712SPawel Jakub Dawidek 	zfs_znode_acl_t zp_acl;		/* 176 - 263 ACL */
16141ba4a712SPawel Jakub Dawidek 	/*
16151ba4a712SPawel Jakub Dawidek 	 * Data may pad out any remaining bytes in the znode buffer, eg:
16161ba4a712SPawel Jakub Dawidek 	 *
16171ba4a712SPawel Jakub Dawidek 	 * |<---------------------- dnode_phys (512) ------------------------>|
16181ba4a712SPawel Jakub Dawidek 	 * |<-- dnode (192) --->|<----------- "bonus" buffer (320) ---------->|
16191ba4a712SPawel Jakub Dawidek 	 *			|<---- znode (264) ---->|<---- data (56) ---->|
16201ba4a712SPawel Jakub Dawidek 	 *
16211ba4a712SPawel Jakub Dawidek 	 * At present, we only use this space to store symbolic links.
16221ba4a712SPawel Jakub Dawidek 	 */
16231ba4a712SPawel Jakub Dawidek } znode_phys_t;
16241ba4a712SPawel Jakub Dawidek 
16251ba4a712SPawel Jakub Dawidek /*
16261ba4a712SPawel Jakub Dawidek  * In-core vdev representation.
16271ba4a712SPawel Jakub Dawidek  */
16281ba4a712SPawel Jakub Dawidek struct vdev;
16292c55d090SToomas Soome struct spa;
1630e1899ef6SDoug Rabson typedef int vdev_phys_read_t(struct vdev *vdev, void *priv,
1631e1899ef6SDoug Rabson     off_t offset, void *buf, size_t bytes);
1632e1899ef6SDoug Rabson typedef int vdev_read_t(struct vdev *vdev, const blkptr_t *bp,
1633e1899ef6SDoug Rabson     void *buf, off_t offset, size_t bytes);
16341ba4a712SPawel Jakub Dawidek 
16351ba4a712SPawel Jakub Dawidek typedef STAILQ_HEAD(vdev_list, vdev) vdev_list_t;
16361ba4a712SPawel Jakub Dawidek 
1637b1b93268SToomas Soome typedef struct vdev_indirect_mapping_entry_phys {
1638b1b93268SToomas Soome 	/*
1639b1b93268SToomas Soome 	 * Decode with DVA_MAPPING_* macros.
1640b1b93268SToomas Soome 	 * Contains:
1641b1b93268SToomas Soome 	 *   the source offset (low 63 bits)
1642b1b93268SToomas Soome 	 *   the one-bit "mark", used for garbage collection (by zdb)
1643b1b93268SToomas Soome 	 */
1644b1b93268SToomas Soome 	uint64_t vimep_src;
1645b1b93268SToomas Soome 
1646b1b93268SToomas Soome 	/*
1647b1b93268SToomas Soome 	 * Note: the DVA's asize is 24 bits, and can thus store ranges
1648b1b93268SToomas Soome 	 * up to 8GB.
1649b1b93268SToomas Soome 	 */
1650b1b93268SToomas Soome 	dva_t	vimep_dst;
1651b1b93268SToomas Soome } vdev_indirect_mapping_entry_phys_t;
1652b1b93268SToomas Soome 
1653b1b93268SToomas Soome #define	DVA_MAPPING_GET_SRC_OFFSET(vimep)	\
1654b1b93268SToomas Soome 	BF64_GET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0)
1655b1b93268SToomas Soome #define	DVA_MAPPING_SET_SRC_OFFSET(vimep, x)	\
1656b1b93268SToomas Soome 	BF64_SET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0, x)
1657b1b93268SToomas Soome 
1658b1b93268SToomas Soome typedef struct vdev_indirect_mapping_entry {
1659b1b93268SToomas Soome 	vdev_indirect_mapping_entry_phys_t	vime_mapping;
1660b1b93268SToomas Soome 	uint32_t				vime_obsolete_count;
1661b1b93268SToomas Soome 	list_node_t				vime_node;
1662b1b93268SToomas Soome } vdev_indirect_mapping_entry_t;
1663b1b93268SToomas Soome 
1664b1b93268SToomas Soome /*
1665b1b93268SToomas Soome  * This is stored in the bonus buffer of the mapping object, see comment of
1666b1b93268SToomas Soome  * vdev_indirect_config for more details.
1667b1b93268SToomas Soome  */
1668b1b93268SToomas Soome typedef struct vdev_indirect_mapping_phys {
1669b1b93268SToomas Soome 	uint64_t	vimp_max_offset;
1670b1b93268SToomas Soome 	uint64_t	vimp_bytes_mapped;
1671b1b93268SToomas Soome 	uint64_t	vimp_num_entries; /* number of v_i_m_entry_phys_t's */
1672b1b93268SToomas Soome 
1673b1b93268SToomas Soome 	/*
1674b1b93268SToomas Soome 	 * For each entry in the mapping object, this object contains an
1675b1b93268SToomas Soome 	 * entry representing the number of bytes of that mapping entry
1676b1b93268SToomas Soome 	 * that were no longer in use by the pool at the time this indirect
1677b1b93268SToomas Soome 	 * vdev was last condensed.
1678b1b93268SToomas Soome 	 */
1679b1b93268SToomas Soome 	uint64_t	vimp_counts_object;
1680b1b93268SToomas Soome } vdev_indirect_mapping_phys_t;
1681b1b93268SToomas Soome 
1682b1b93268SToomas Soome #define	VDEV_INDIRECT_MAPPING_SIZE_V0	(3 * sizeof (uint64_t))
1683b1b93268SToomas Soome 
1684b1b93268SToomas Soome typedef struct vdev_indirect_mapping {
1685b1b93268SToomas Soome 	uint64_t	vim_object;
1686b1b93268SToomas Soome 	boolean_t	vim_havecounts;
1687b1b93268SToomas Soome 
1688b1b93268SToomas Soome 	/* vim_entries segment offset currently in memory. */
1689b1b93268SToomas Soome 	uint64_t	vim_entry_offset;
1690b1b93268SToomas Soome 	/* vim_entries segment size. */
1691b1b93268SToomas Soome 	size_t		vim_num_entries;
1692b1b93268SToomas Soome 
1693b1b93268SToomas Soome 	/* Needed by dnode_read() */
1694b1b93268SToomas Soome 	const void	*vim_spa;
1695b1b93268SToomas Soome 	dnode_phys_t	*vim_dn;
1696b1b93268SToomas Soome 
1697b1b93268SToomas Soome 	/*
1698b1b93268SToomas Soome 	 * An ordered array of mapping entries, sorted by source offset.
1699b1b93268SToomas Soome 	 * Note that vim_entries is needed during a removal (and contains
1700b1b93268SToomas Soome 	 * mappings that have been synced to disk so far) to handle frees
1701b1b93268SToomas Soome 	 * from the removing device.
1702b1b93268SToomas Soome 	 */
1703b1b93268SToomas Soome 	vdev_indirect_mapping_entry_phys_t *vim_entries;
1704b1b93268SToomas Soome 	objset_phys_t	*vim_objset;
1705b1b93268SToomas Soome 	vdev_indirect_mapping_phys_t	*vim_phys;
1706b1b93268SToomas Soome } vdev_indirect_mapping_t;
1707b1b93268SToomas Soome 
1708b1b93268SToomas Soome /*
1709b1b93268SToomas Soome  * On-disk indirect vdev state.
1710b1b93268SToomas Soome  *
1711b1b93268SToomas Soome  * An indirect vdev is described exclusively in the MOS config of a pool.
1712b1b93268SToomas Soome  * The config for an indirect vdev includes several fields, which are
1713b1b93268SToomas Soome  * accessed in memory by a vdev_indirect_config_t.
1714b1b93268SToomas Soome  */
1715b1b93268SToomas Soome typedef struct vdev_indirect_config {
1716b1b93268SToomas Soome 	/*
1717b1b93268SToomas Soome 	 * Object (in MOS) which contains the indirect mapping. This object
1718b1b93268SToomas Soome 	 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
1719b1b93268SToomas Soome 	 * vimep_src. The bonus buffer for this object is a
1720b1b93268SToomas Soome 	 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
1721b1b93268SToomas Soome 	 * removal is initiated.
1722b1b93268SToomas Soome 	 *
1723b1b93268SToomas Soome 	 * Note that this object can be empty if none of the data on the vdev
1724b1b93268SToomas Soome 	 * has been copied yet.
1725b1b93268SToomas Soome 	 */
1726b1b93268SToomas Soome 	uint64_t	vic_mapping_object;
1727b1b93268SToomas Soome 
1728b1b93268SToomas Soome 	/*
1729b1b93268SToomas Soome 	 * Object (in MOS) which contains the birth times for the mapping
1730b1b93268SToomas Soome 	 * entries. This object contains an array of
1731b1b93268SToomas Soome 	 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
1732b1b93268SToomas Soome 	 * buffer for this object is a vdev_indirect_birth_phys_t. This object
1733b1b93268SToomas Soome 	 * is allocated when a vdev removal is initiated.
1734b1b93268SToomas Soome 	 *
1735b1b93268SToomas Soome 	 * Note that this object can be empty if none of the vdev has yet been
1736b1b93268SToomas Soome 	 * copied.
1737b1b93268SToomas Soome 	 */
1738b1b93268SToomas Soome 	uint64_t	vic_births_object;
1739b1b93268SToomas Soome 
1740b1b93268SToomas Soome /*
1741b1b93268SToomas Soome  * This is the vdev ID which was removed previous to this vdev, or
1742b1b93268SToomas Soome  * UINT64_MAX if there are no previously removed vdevs.
1743b1b93268SToomas Soome  */
1744b1b93268SToomas Soome 	uint64_t	vic_prev_indirect_vdev;
1745b1b93268SToomas Soome } vdev_indirect_config_t;
1746b1b93268SToomas Soome 
17471ba4a712SPawel Jakub Dawidek typedef struct vdev {
17481ba4a712SPawel Jakub Dawidek 	STAILQ_ENTRY(vdev) v_childlink;	/* link in parent's child list */
17491ba4a712SPawel Jakub Dawidek 	STAILQ_ENTRY(vdev) v_alllink;	/* link in global vdev list */
17501ba4a712SPawel Jakub Dawidek 	vdev_list_t	v_children;	/* children of this vdev */
175110b9d77bSPawel Jakub Dawidek 	const char	*v_name;	/* vdev name */
17521ba4a712SPawel Jakub Dawidek 	uint64_t	v_guid;		/* vdev guid */
1753abca0bd5SToomas Soome 	uint64_t	v_id;		/* index in parent */
1754abca0bd5SToomas Soome 	uint64_t	v_psize;	/* physical device capacity */
1755e1899ef6SDoug Rabson 	int		v_ashift;	/* offset to block shift */
1756e1899ef6SDoug Rabson 	int		v_nparity;	/* # parity for raidz */
175710b9d77bSPawel Jakub Dawidek 	struct vdev	*v_top;		/* parent vdev */
1758e1899ef6SDoug Rabson 	int		v_nchildren;	/* # children */
17591ba4a712SPawel Jakub Dawidek 	vdev_state_t	v_state;	/* current state */
1760e1899ef6SDoug Rabson 	vdev_phys_read_t *v_phys_read;	/* read from raw leaf vdev */
1761e1899ef6SDoug Rabson 	vdev_read_t	*v_read;	/* read from vdev */
17621ba4a712SPawel Jakub Dawidek 	void		*v_read_priv;	/* private data for read function */
17630c0a882cSToomas Soome 	boolean_t	v_islog;
17642c55d090SToomas Soome 	struct spa	*spa;		/* link to spa */
1765b1b93268SToomas Soome 	/*
1766b1b93268SToomas Soome 	 * Values stored in the config for an indirect or removing vdev.
1767b1b93268SToomas Soome 	 */
1768b1b93268SToomas Soome 	vdev_indirect_config_t vdev_indirect_config;
1769b1b93268SToomas Soome 	vdev_indirect_mapping_t *v_mapping;
17701ba4a712SPawel Jakub Dawidek } vdev_t;
17711ba4a712SPawel Jakub Dawidek 
17721ba4a712SPawel Jakub Dawidek /*
17731ba4a712SPawel Jakub Dawidek  * In-core pool representation.
17741ba4a712SPawel Jakub Dawidek  */
17751ba4a712SPawel Jakub Dawidek typedef STAILQ_HEAD(spa_list, spa) spa_list_t;
17761ba4a712SPawel Jakub Dawidek 
17771ba4a712SPawel Jakub Dawidek typedef struct spa {
17781ba4a712SPawel Jakub Dawidek 	STAILQ_ENTRY(spa) spa_link;	/* link in global pool list */
17791ba4a712SPawel Jakub Dawidek 	char		*spa_name;	/* pool name */
17801ba4a712SPawel Jakub Dawidek 	uint64_t	spa_guid;	/* pool guid */
17811ba4a712SPawel Jakub Dawidek 	uint64_t	spa_txg;	/* most recent transaction */
17821ba4a712SPawel Jakub Dawidek 	struct uberblock spa_uberblock;	/* best uberblock so far */
17831ba4a712SPawel Jakub Dawidek 	vdev_list_t	spa_vdevs;	/* list of all toplevel vdevs */
17841ba4a712SPawel Jakub Dawidek 	objset_phys_t	spa_mos;	/* MOS for this pool */
17852c55d090SToomas Soome 	zio_cksum_salt_t spa_cksum_salt;	/* secret salt for cksum */
17862c55d090SToomas Soome 	void		*spa_cksum_tmpls[ZIO_CHECKSUM_FUNCTIONS];
17879da75f3aSAndriy Gapon 	int		spa_inited;	/* initialized */
17880c0a882cSToomas Soome 	boolean_t	spa_with_log;	/* this pool has log */
17891ba4a712SPawel Jakub Dawidek } spa_t;
179029441ba3SXin LI 
1791b1b93268SToomas Soome /* IO related arguments. */
1792b1b93268SToomas Soome typedef struct zio {
1793b1b93268SToomas Soome 	spa_t		*io_spa;
1794b1b93268SToomas Soome 	blkptr_t	*io_bp;
1795b1b93268SToomas Soome 	void		*io_data;
1796b1b93268SToomas Soome 	uint64_t	io_size;
1797b1b93268SToomas Soome 	uint64_t	io_offset;
1798b1b93268SToomas Soome 
1799b1b93268SToomas Soome 	/* Stuff for the vdev stack */
1800b1b93268SToomas Soome 	vdev_t		*io_vd;
1801b1b93268SToomas Soome 	void		*io_vsd;
1802b1b93268SToomas Soome 
1803b1b93268SToomas Soome 	int		io_error;
1804b1b93268SToomas Soome } zio_t;
1805b1b93268SToomas Soome 
180629441ba3SXin LI static void decode_embedded_bp_compressed(const blkptr_t *, void *);
1807