xref: /freebsd/sys/cddl/boot/zfs/zfsimpl.h (revision 2468e20df43fdc8e5042e45a42235cf00e65e4b1)
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 /*
593830659eSToomas Soome  * Copyright (c) 2020 by Delphix. All rights reserved.
6029441ba3SXin LI  */
611ba4a712SPawel Jakub Dawidek 
623830659eSToomas Soome #include <sys/queue.h>
633830659eSToomas Soome 
643830659eSToomas Soome #ifndef _ZFSIMPL_H_
653830659eSToomas Soome #define	_ZFSIMPL_H_
663830659eSToomas Soome 
672d9cf57eSMartin Matuska #define	MAXNAMELEN	256
682d9cf57eSMartin Matuska 
6929441ba3SXin LI #define _NOTE(s)
7029441ba3SXin LI 
7179a4bf89SToomas Soome /*
7279a4bf89SToomas Soome  * AVL comparator helpers
7379a4bf89SToomas Soome  */
7479a4bf89SToomas Soome #define	AVL_ISIGN(a)	(((a) > 0) - ((a) < 0))
7579a4bf89SToomas Soome #define	AVL_CMP(a, b)	(((a) > (b)) - ((a) < (b)))
7679a4bf89SToomas Soome #define	AVL_PCMP(a, b)	\
7779a4bf89SToomas Soome 	(((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b)))
7879a4bf89SToomas Soome 
791306a5dcSWarner Losh #if !defined(NEED_SOLARIS_BOOLEAN)	/* Only defined when we'll define this elsewhere */
804deb8929SAllan Jude typedef enum { B_FALSE, B_TRUE } boolean_t;
811306a5dcSWarner Losh #endif
824deb8929SAllan Jude 
831ba4a712SPawel Jakub Dawidek /* CRC64 table */
841ba4a712SPawel Jakub Dawidek #define	ZFS_CRC64_POLY	0xC96C5795D7870F42ULL	/* ECMA-182, reflected form */
851ba4a712SPawel Jakub Dawidek 
861ba4a712SPawel Jakub Dawidek /*
871ba4a712SPawel Jakub Dawidek  * Macros for various sorts of alignment and rounding when the alignment
881ba4a712SPawel Jakub Dawidek  * is known to be a power of 2.
891ba4a712SPawel Jakub Dawidek  */
901ba4a712SPawel Jakub Dawidek #define	P2ALIGN(x, align)		((x) & -(align))
911ba4a712SPawel Jakub Dawidek #define	P2PHASE(x, align)		((x) & ((align) - 1))
921ba4a712SPawel Jakub Dawidek #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
931ba4a712SPawel Jakub Dawidek #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
941ba4a712SPawel Jakub Dawidek #define	P2END(x, align)			(-(~(x) & -(align)))
951ba4a712SPawel Jakub Dawidek #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
96c43d127aSMartin Matuska #define	P2BOUNDARY(off, len, align)	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
971ba4a712SPawel Jakub Dawidek 
981ba4a712SPawel Jakub Dawidek /*
991ba4a712SPawel Jakub Dawidek  * General-purpose 32-bit and 64-bit bitfield encodings.
1001ba4a712SPawel Jakub Dawidek  */
1011ba4a712SPawel Jakub Dawidek #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
1021ba4a712SPawel Jakub Dawidek #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
1031ba4a712SPawel Jakub Dawidek #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
1041ba4a712SPawel Jakub Dawidek #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
1051ba4a712SPawel Jakub Dawidek 
1061ba4a712SPawel Jakub Dawidek #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
1071ba4a712SPawel Jakub Dawidek #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
1081ba4a712SPawel Jakub Dawidek 
1091ba4a712SPawel Jakub Dawidek #define	BF32_SET(x, low, len, val)	\
1101ba4a712SPawel Jakub Dawidek 	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
1111ba4a712SPawel Jakub Dawidek #define	BF64_SET(x, low, len, val)	\
1121ba4a712SPawel Jakub Dawidek 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
1131ba4a712SPawel Jakub Dawidek 
1141ba4a712SPawel Jakub Dawidek #define	BF32_GET_SB(x, low, len, shift, bias)	\
1151ba4a712SPawel Jakub Dawidek 	((BF32_GET(x, low, len) + (bias)) << (shift))
1161ba4a712SPawel Jakub Dawidek #define	BF64_GET_SB(x, low, len, shift, bias)	\
1171ba4a712SPawel Jakub Dawidek 	((BF64_GET(x, low, len) + (bias)) << (shift))
1181ba4a712SPawel Jakub Dawidek 
1191ba4a712SPawel Jakub Dawidek #define	BF32_SET_SB(x, low, len, shift, bias, val)	\
1201ba4a712SPawel Jakub Dawidek 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
1211ba4a712SPawel Jakub Dawidek #define	BF64_SET_SB(x, low, len, shift, bias, val)	\
1221ba4a712SPawel Jakub Dawidek 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
1231ba4a712SPawel Jakub Dawidek 
1241ba4a712SPawel Jakub Dawidek /*
12510b9d77bSPawel Jakub Dawidek  * Macros to reverse byte order
12610b9d77bSPawel Jakub Dawidek  */
12710b9d77bSPawel Jakub Dawidek #define	BSWAP_8(x)	((x) & 0xff)
12810b9d77bSPawel Jakub Dawidek #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
12910b9d77bSPawel Jakub Dawidek #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
13010b9d77bSPawel Jakub Dawidek #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
13110b9d77bSPawel Jakub Dawidek 
1321ba4a712SPawel Jakub Dawidek #define	SPA_MINBLOCKSHIFT	9
1332c55d090SToomas Soome #define	SPA_OLDMAXBLOCKSHIFT	17
1342c55d090SToomas Soome #define	SPA_MAXBLOCKSHIFT	24
1351ba4a712SPawel Jakub Dawidek #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
1362c55d090SToomas Soome #define	SPA_OLDMAXBLOCKSIZE	(1ULL << SPA_OLDMAXBLOCKSHIFT)
1371ba4a712SPawel Jakub Dawidek #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
1381ba4a712SPawel Jakub Dawidek 
1391ba4a712SPawel Jakub Dawidek /*
1401ba4a712SPawel Jakub Dawidek  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
1411ba4a712SPawel Jakub Dawidek  * The ASIZE encoding should be at least 64 times larger (6 more bits)
1421ba4a712SPawel Jakub Dawidek  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
1431ba4a712SPawel Jakub Dawidek  * overhead, three DVAs per bp, plus one more bit in case we do anything
1441ba4a712SPawel Jakub Dawidek  * else that expands the ASIZE.
1451ba4a712SPawel Jakub Dawidek  */
1461ba4a712SPawel Jakub Dawidek #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
1471ba4a712SPawel Jakub Dawidek #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
1481ba4a712SPawel Jakub Dawidek #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
1491ba4a712SPawel Jakub Dawidek 
1501ba4a712SPawel Jakub Dawidek /*
1511ba4a712SPawel Jakub Dawidek  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
1521ba4a712SPawel Jakub Dawidek  * The members of the dva_t should be considered opaque outside the SPA.
1531ba4a712SPawel Jakub Dawidek  */
1541ba4a712SPawel Jakub Dawidek typedef struct dva {
1551ba4a712SPawel Jakub Dawidek 	uint64_t	dva_word[2];
1561ba4a712SPawel Jakub Dawidek } dva_t;
1571ba4a712SPawel Jakub Dawidek 
1581ba4a712SPawel Jakub Dawidek /*
1591ba4a712SPawel Jakub Dawidek  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
1601ba4a712SPawel Jakub Dawidek  */
1611ba4a712SPawel Jakub Dawidek typedef struct zio_cksum {
1621ba4a712SPawel Jakub Dawidek 	uint64_t	zc_word[4];
1631ba4a712SPawel Jakub Dawidek } zio_cksum_t;
1641ba4a712SPawel Jakub Dawidek 
1651ba4a712SPawel Jakub Dawidek /*
1662c55d090SToomas Soome  * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
1672c55d090SToomas Soome  * secret and is suitable for use in MAC algorithms as the key.
1682c55d090SToomas Soome  */
1692c55d090SToomas Soome typedef struct zio_cksum_salt {
1702c55d090SToomas Soome 	uint8_t		zcs_bytes[32];
1712c55d090SToomas Soome } zio_cksum_salt_t;
1722c55d090SToomas Soome 
1732c55d090SToomas Soome /*
1741ba4a712SPawel Jakub Dawidek  * Each block is described by its DVAs, time of birth, checksum, etc.
1751ba4a712SPawel Jakub Dawidek  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
1761ba4a712SPawel Jakub Dawidek  *
1771ba4a712SPawel Jakub Dawidek  *	64	56	48	40	32	24	16	8	0
1781ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1791ba4a712SPawel Jakub Dawidek  * 0	|		vdev1		| GRID  |	  ASIZE		|
1801ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1811ba4a712SPawel Jakub Dawidek  * 1	|G|			 offset1				|
1821ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1831ba4a712SPawel Jakub Dawidek  * 2	|		vdev2		| GRID  |	  ASIZE		|
1841ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1851ba4a712SPawel Jakub Dawidek  * 3	|G|			 offset2				|
1861ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1871ba4a712SPawel Jakub Dawidek  * 4	|		vdev3		| GRID  |	  ASIZE		|
1881ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1891ba4a712SPawel Jakub Dawidek  * 5	|G|			 offset3				|
1901ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
19129441ba3SXin LI  * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
1921ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1931ba4a712SPawel Jakub Dawidek  * 7	|			padding					|
1941ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
1951ba4a712SPawel Jakub Dawidek  * 8	|			padding					|
1961ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
19710b9d77bSPawel Jakub Dawidek  * 9	|			physical birth txg			|
1981ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
19910b9d77bSPawel Jakub Dawidek  * a	|			logical birth txg			|
2001ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2011ba4a712SPawel Jakub Dawidek  * b	|			fill count				|
2021ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2031ba4a712SPawel Jakub Dawidek  * c	|			checksum[0]				|
2041ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2051ba4a712SPawel Jakub Dawidek  * d	|			checksum[1]				|
2061ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2071ba4a712SPawel Jakub Dawidek  * e	|			checksum[2]				|
2081ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2091ba4a712SPawel Jakub Dawidek  * f	|			checksum[3]				|
2101ba4a712SPawel Jakub Dawidek  *	+-------+-------+-------+-------+-------+-------+-------+-------+
2111ba4a712SPawel Jakub Dawidek  *
2121ba4a712SPawel Jakub Dawidek  * Legend:
2131ba4a712SPawel Jakub Dawidek  *
2141ba4a712SPawel Jakub Dawidek  * vdev		virtual device ID
2151ba4a712SPawel Jakub Dawidek  * offset	offset into virtual device
2161ba4a712SPawel Jakub Dawidek  * LSIZE	logical size
2171ba4a712SPawel Jakub Dawidek  * PSIZE	physical size (after compression)
2181ba4a712SPawel Jakub Dawidek  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
2191ba4a712SPawel Jakub Dawidek  * GRID		RAID-Z layout information (reserved for future use)
2201ba4a712SPawel Jakub Dawidek  * cksum	checksum function
2211ba4a712SPawel Jakub Dawidek  * comp		compression function
2221ba4a712SPawel Jakub Dawidek  * G		gang block indicator
22310b9d77bSPawel Jakub Dawidek  * B		byteorder (endianness)
22410b9d77bSPawel Jakub Dawidek  * D		dedup
22529441ba3SXin LI  * X		encryption (on version 30, which is not supported)
22629441ba3SXin LI  * E		blkptr_t contains embedded data (see below)
2271ba4a712SPawel Jakub Dawidek  * lvl		level of indirection
22810b9d77bSPawel Jakub Dawidek  * type		DMU object type
22910b9d77bSPawel Jakub Dawidek  * phys birth	txg of block allocation; zero if same as logical birth txg
23010b9d77bSPawel Jakub Dawidek  * log. birth	transaction group in which the block was logically born
2311ba4a712SPawel Jakub Dawidek  * fill count	number of non-zero blocks under this bp
2321ba4a712SPawel Jakub Dawidek  * checksum[4]	256-bit checksum of the data this bp describes
2331ba4a712SPawel Jakub Dawidek  */
23429441ba3SXin LI 
23529441ba3SXin LI /*
23629441ba3SXin LI  * "Embedded" blkptr_t's don't actually point to a block, instead they
23729441ba3SXin LI  * have a data payload embedded in the blkptr_t itself.  See the comment
23829441ba3SXin LI  * in blkptr.c for more details.
23929441ba3SXin LI  *
24029441ba3SXin LI  * The blkptr_t is laid out as follows:
24129441ba3SXin LI  *
24229441ba3SXin LI  *	64	56	48	40	32	24	16	8	0
24329441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
24429441ba3SXin LI  * 0	|      payload                                                  |
24529441ba3SXin LI  * 1	|      payload                                                  |
24629441ba3SXin LI  * 2	|      payload                                                  |
24729441ba3SXin LI  * 3	|      payload                                                  |
24829441ba3SXin LI  * 4	|      payload                                                  |
24929441ba3SXin LI  * 5	|      payload                                                  |
25029441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
25129441ba3SXin LI  * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
25229441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
25329441ba3SXin LI  * 7	|      payload                                                  |
25429441ba3SXin LI  * 8	|      payload                                                  |
25529441ba3SXin LI  * 9	|      payload                                                  |
25629441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
25729441ba3SXin LI  * a	|			logical birth txg			|
25829441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
25929441ba3SXin LI  * b	|      payload                                                  |
26029441ba3SXin LI  * c	|      payload                                                  |
26129441ba3SXin LI  * d	|      payload                                                  |
26229441ba3SXin LI  * e	|      payload                                                  |
26329441ba3SXin LI  * f	|      payload                                                  |
26429441ba3SXin LI  *	+-------+-------+-------+-------+-------+-------+-------+-------+
26529441ba3SXin LI  *
26629441ba3SXin LI  * Legend:
26729441ba3SXin LI  *
26829441ba3SXin LI  * payload		contains the embedded data
26929441ba3SXin LI  * B (byteorder)	byteorder (endianness)
27029441ba3SXin LI  * D (dedup)		padding (set to zero)
27129441ba3SXin LI  * X			encryption (set to zero; see above)
27229441ba3SXin LI  * E (embedded)		set to one
27329441ba3SXin LI  * lvl			indirection level
27429441ba3SXin LI  * type			DMU object type
27529441ba3SXin LI  * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
27629441ba3SXin LI  * comp			compression function of payload
27729441ba3SXin LI  * PSIZE		size of payload after compression, in bytes
27829441ba3SXin LI  * LSIZE		logical size of payload, in bytes
27929441ba3SXin LI  *			note that 25 bits is enough to store the largest
28029441ba3SXin LI  *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
28129441ba3SXin LI  * log. birth		transaction group in which the block was logically born
28229441ba3SXin LI  *
28329441ba3SXin LI  * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
28429441ba3SXin LI  * bp's they are stored in units of SPA_MINBLOCKSHIFT.
28529441ba3SXin LI  * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
28629441ba3SXin LI  * The B, D, X, lvl, type, and comp fields are stored the same as with normal
28729441ba3SXin LI  * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
28829441ba3SXin LI  * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
28929441ba3SXin LI  * other macros, as they assert that they are only used on BP's of the correct
29029441ba3SXin LI  * "embedded-ness".
29129441ba3SXin LI  */
29229441ba3SXin LI 
29329441ba3SXin LI #define	BPE_GET_ETYPE(bp)	\
29429441ba3SXin LI 	(ASSERT(BP_IS_EMBEDDED(bp)), \
29529441ba3SXin LI 	BF64_GET((bp)->blk_prop, 40, 8))
29629441ba3SXin LI #define	BPE_SET_ETYPE(bp, t)	do { \
29729441ba3SXin LI 	ASSERT(BP_IS_EMBEDDED(bp)); \
29829441ba3SXin LI 	BF64_SET((bp)->blk_prop, 40, 8, t); \
29929441ba3SXin LI _NOTE(CONSTCOND) } while (0)
30029441ba3SXin LI 
30129441ba3SXin LI #define	BPE_GET_LSIZE(bp)	\
30229441ba3SXin LI 	(ASSERT(BP_IS_EMBEDDED(bp)), \
30329441ba3SXin LI 	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
30429441ba3SXin LI #define	BPE_SET_LSIZE(bp, x)	do { \
30529441ba3SXin LI 	ASSERT(BP_IS_EMBEDDED(bp)); \
30629441ba3SXin LI 	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
30729441ba3SXin LI _NOTE(CONSTCOND) } while (0)
30829441ba3SXin LI 
30929441ba3SXin LI #define	BPE_GET_PSIZE(bp)	\
31029441ba3SXin LI 	(ASSERT(BP_IS_EMBEDDED(bp)), \
31129441ba3SXin LI 	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
31229441ba3SXin LI #define	BPE_SET_PSIZE(bp, x)	do { \
31329441ba3SXin LI 	ASSERT(BP_IS_EMBEDDED(bp)); \
31429441ba3SXin LI 	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
31529441ba3SXin LI _NOTE(CONSTCOND) } while (0)
31629441ba3SXin LI 
31729441ba3SXin LI typedef enum bp_embedded_type {
31829441ba3SXin LI 	BP_EMBEDDED_TYPE_DATA,
31929441ba3SXin LI 	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
32029441ba3SXin LI 	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
32129441ba3SXin LI } bp_embedded_type_t;
32229441ba3SXin LI 
32329441ba3SXin LI #define	BPE_NUM_WORDS 14
32429441ba3SXin LI #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
32529441ba3SXin LI #define	BPE_IS_PAYLOADWORD(bp, wp) \
32629441ba3SXin LI 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
32729441ba3SXin LI 
32810b9d77bSPawel Jakub Dawidek #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
32910b9d77bSPawel Jakub Dawidek #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
33010b9d77bSPawel Jakub Dawidek 
3311ba4a712SPawel Jakub Dawidek typedef struct blkptr {
33210b9d77bSPawel Jakub Dawidek 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
3331ba4a712SPawel Jakub Dawidek 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
33410b9d77bSPawel Jakub Dawidek 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
33510b9d77bSPawel Jakub Dawidek 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
3361ba4a712SPawel Jakub Dawidek 	uint64_t	blk_birth;	/* transaction group at birth	    */
3371ba4a712SPawel Jakub Dawidek 	uint64_t	blk_fill;	/* fill count			    */
3381ba4a712SPawel Jakub Dawidek 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
3391ba4a712SPawel Jakub Dawidek } blkptr_t;
3401ba4a712SPawel Jakub Dawidek 
3411ba4a712SPawel Jakub Dawidek /*
3421ba4a712SPawel Jakub Dawidek  * Macros to get and set fields in a bp or DVA.
3431ba4a712SPawel Jakub Dawidek  */
3441ba4a712SPawel Jakub Dawidek #define	DVA_GET_ASIZE(dva)	\
345f4c8ba83SXin LI 	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
3461ba4a712SPawel Jakub Dawidek #define	DVA_SET_ASIZE(dva, x)	\
347f4c8ba83SXin LI 	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
348f4c8ba83SXin LI 	SPA_MINBLOCKSHIFT, 0, x)
3491ba4a712SPawel Jakub Dawidek 
3501ba4a712SPawel Jakub Dawidek #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
3511ba4a712SPawel Jakub Dawidek #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
3521ba4a712SPawel Jakub Dawidek 
3531ba4a712SPawel Jakub Dawidek #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
3541ba4a712SPawel Jakub Dawidek #define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
3551ba4a712SPawel Jakub Dawidek 
3561ba4a712SPawel Jakub Dawidek #define	DVA_GET_OFFSET(dva)	\
3571ba4a712SPawel Jakub Dawidek 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
3581ba4a712SPawel Jakub Dawidek #define	DVA_SET_OFFSET(dva, x)	\
3591ba4a712SPawel Jakub Dawidek 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
3601ba4a712SPawel Jakub Dawidek 
3611ba4a712SPawel Jakub Dawidek #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
3621ba4a712SPawel Jakub Dawidek #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
3631ba4a712SPawel Jakub Dawidek 
3641ba4a712SPawel Jakub Dawidek #define	BP_GET_LSIZE(bp)	\
36529441ba3SXin LI 	(BP_IS_EMBEDDED(bp) ?	\
36629441ba3SXin LI 	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
367f4c8ba83SXin LI 	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
36829441ba3SXin LI #define	BP_SET_LSIZE(bp, x)	do { \
36929441ba3SXin LI 	ASSERT(!BP_IS_EMBEDDED(bp)); \
37029441ba3SXin LI 	BF64_SET_SB((bp)->blk_prop, \
37129441ba3SXin LI 	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
37229441ba3SXin LI _NOTE(CONSTCOND) } while (0)
3731ba4a712SPawel Jakub Dawidek 
3741ba4a712SPawel Jakub Dawidek #define	BP_GET_PSIZE(bp)	\
375f4c8ba83SXin LI 	BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)
3761ba4a712SPawel Jakub Dawidek #define	BP_SET_PSIZE(bp, x)	\
377f4c8ba83SXin LI 	BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x)
3781ba4a712SPawel Jakub Dawidek 
37929441ba3SXin LI #define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 7)
38029441ba3SXin LI #define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 7, x)
3811ba4a712SPawel Jakub Dawidek 
3821ba4a712SPawel Jakub Dawidek #define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
3831ba4a712SPawel Jakub Dawidek #define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
3841ba4a712SPawel Jakub Dawidek 
3851ba4a712SPawel Jakub Dawidek #define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
3861ba4a712SPawel Jakub Dawidek #define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
3871ba4a712SPawel Jakub Dawidek 
3881ba4a712SPawel Jakub Dawidek #define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
3891ba4a712SPawel Jakub Dawidek #define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
3901ba4a712SPawel Jakub Dawidek 
39129441ba3SXin LI #define	BP_IS_EMBEDDED(bp)	BF64_GET((bp)->blk_prop, 39, 1)
39229441ba3SXin LI 
39310b9d77bSPawel Jakub Dawidek #define	BP_GET_DEDUP(bp)	BF64_GET((bp)->blk_prop, 62, 1)
39410b9d77bSPawel Jakub Dawidek #define	BP_SET_DEDUP(bp, x)	BF64_SET((bp)->blk_prop, 62, 1, x)
39510b9d77bSPawel Jakub Dawidek 
396f4c8ba83SXin LI #define	BP_GET_BYTEORDER(bp)	BF64_GET((bp)->blk_prop, 63, 1)
3971ba4a712SPawel Jakub Dawidek #define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
3981ba4a712SPawel Jakub Dawidek 
39910b9d77bSPawel Jakub Dawidek #define	BP_PHYSICAL_BIRTH(bp)		\
40010b9d77bSPawel Jakub Dawidek 	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
40110b9d77bSPawel Jakub Dawidek 
40277649f35SMark Johnston #define	BP_SET_BIRTH(bp, logical, physical)	\
40377649f35SMark Johnston {						\
40477649f35SMark Johnston 	ASSERT(!BP_IS_EMBEDDED(bp));		\
40577649f35SMark Johnston 	(bp)->blk_birth = (logical);		\
40677649f35SMark Johnston 	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
40777649f35SMark Johnston }
40877649f35SMark Johnston 
40977649f35SMark Johnston #define	BP_GET_FILL(bp)				\
41077649f35SMark Johnston 	((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill)
41177649f35SMark Johnston 
41277649f35SMark Johnston #define	BP_SET_FILL(bp, fill)			\
41377649f35SMark Johnston {						\
41477649f35SMark Johnston 	(bp)->blk_fill = fill;			\
41577649f35SMark Johnston }
41677649f35SMark Johnston 
4171ba4a712SPawel Jakub Dawidek #define	BP_GET_ASIZE(bp)	\
4181ba4a712SPawel Jakub Dawidek 	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
4191ba4a712SPawel Jakub Dawidek 		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
4201ba4a712SPawel Jakub Dawidek 
4211ba4a712SPawel Jakub Dawidek #define	BP_GET_UCSIZE(bp) \
4221ba4a712SPawel Jakub Dawidek 	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
4231ba4a712SPawel Jakub Dawidek 	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
4241ba4a712SPawel Jakub Dawidek 
4251ba4a712SPawel Jakub Dawidek #define	BP_GET_NDVAS(bp)	\
4261ba4a712SPawel Jakub Dawidek 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
4271ba4a712SPawel Jakub Dawidek 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
4281ba4a712SPawel Jakub Dawidek 	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
4291ba4a712SPawel Jakub Dawidek 
4301ba4a712SPawel Jakub Dawidek #define	DVA_EQUAL(dva1, dva2)	\
4311ba4a712SPawel Jakub Dawidek 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
4321ba4a712SPawel Jakub Dawidek 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
4331ba4a712SPawel Jakub Dawidek 
4341ba4a712SPawel Jakub Dawidek #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
4351ba4a712SPawel Jakub Dawidek 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
4361ba4a712SPawel Jakub Dawidek 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
4371ba4a712SPawel Jakub Dawidek 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
4381ba4a712SPawel Jakub Dawidek 	((zc1).zc_word[3] - (zc2).zc_word[3])))
4391ba4a712SPawel Jakub Dawidek 
4401ba4a712SPawel Jakub Dawidek 
4411ba4a712SPawel Jakub Dawidek #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
4421ba4a712SPawel Jakub Dawidek 
4431ba4a712SPawel Jakub Dawidek #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
4441ba4a712SPawel Jakub Dawidek {						\
4451ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[0] = w0;			\
4461ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[1] = w1;			\
4471ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[2] = w2;			\
4481ba4a712SPawel Jakub Dawidek 	(zcp)->zc_word[3] = w3;			\
4491ba4a712SPawel Jakub Dawidek }
4501ba4a712SPawel Jakub Dawidek 
4511ba4a712SPawel Jakub Dawidek #define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
4521ba4a712SPawel Jakub Dawidek #define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
453f4c8ba83SXin LI #define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&  \
454f4c8ba83SXin LI 	(dva)->dva_word[1] == 0ULL)
455f4c8ba83SXin LI #define	BP_IS_HOLE(bp)		DVA_IS_EMPTY(BP_IDENTITY(bp))
4561ba4a712SPawel Jakub Dawidek #define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
4571ba4a712SPawel Jakub Dawidek 
4581ba4a712SPawel Jakub Dawidek #define	BP_ZERO(bp)				\
4591ba4a712SPawel Jakub Dawidek {						\
4601ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[0].dva_word[0] = 0;	\
4611ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[0].dva_word[1] = 0;	\
4621ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[1].dva_word[0] = 0;	\
4631ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[1].dva_word[1] = 0;	\
4641ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[2].dva_word[0] = 0;	\
4651ba4a712SPawel Jakub Dawidek 	(bp)->blk_dva[2].dva_word[1] = 0;	\
4661ba4a712SPawel Jakub Dawidek 	(bp)->blk_prop = 0;			\
4671ba4a712SPawel Jakub Dawidek 	(bp)->blk_pad[0] = 0;			\
4681ba4a712SPawel Jakub Dawidek 	(bp)->blk_pad[1] = 0;			\
46910b9d77bSPawel Jakub Dawidek 	(bp)->blk_phys_birth = 0;		\
4701ba4a712SPawel Jakub Dawidek 	(bp)->blk_birth = 0;			\
4711ba4a712SPawel Jakub Dawidek 	(bp)->blk_fill = 0;			\
4721ba4a712SPawel Jakub Dawidek 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
4731ba4a712SPawel Jakub Dawidek }
4741ba4a712SPawel Jakub Dawidek 
475903fe2b7SToomas Soome #if BYTE_ORDER == _BIG_ENDIAN
476903fe2b7SToomas Soome #define	ZFS_HOST_BYTEORDER	(0ULL)
477903fe2b7SToomas Soome #else
478903fe2b7SToomas Soome #define	ZFS_HOST_BYTEORDER	(1ULL)
479903fe2b7SToomas Soome #endif
480903fe2b7SToomas Soome 
481903fe2b7SToomas Soome #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
48229441ba3SXin LI #define	BPE_NUM_WORDS 14
48329441ba3SXin LI #define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
48429441ba3SXin LI #define	BPE_IS_PAYLOADWORD(bp, wp) \
48529441ba3SXin LI 	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
48629441ba3SXin LI 
48710b9d77bSPawel Jakub Dawidek /*
48810b9d77bSPawel Jakub Dawidek  * Embedded checksum
48910b9d77bSPawel Jakub Dawidek  */
49010b9d77bSPawel Jakub Dawidek #define	ZEC_MAGIC	0x210da7ab10c7a11ULL
4911ba4a712SPawel Jakub Dawidek 
49210b9d77bSPawel Jakub Dawidek typedef struct zio_eck {
49310b9d77bSPawel Jakub Dawidek 	uint64_t	zec_magic;	/* for validation, endianness	*/
49410b9d77bSPawel Jakub Dawidek 	zio_cksum_t	zec_cksum;	/* 256-bit checksum		*/
49510b9d77bSPawel Jakub Dawidek } zio_eck_t;
49610b9d77bSPawel Jakub Dawidek 
49710b9d77bSPawel Jakub Dawidek /*
49810b9d77bSPawel Jakub Dawidek  * Gang block headers are self-checksumming and contain an array
49910b9d77bSPawel Jakub Dawidek  * of block pointers.
50010b9d77bSPawel Jakub Dawidek  */
50110b9d77bSPawel Jakub Dawidek #define	SPA_GANGBLOCKSIZE	SPA_MINBLOCKSIZE
50210b9d77bSPawel Jakub Dawidek #define	SPA_GBH_NBLKPTRS	((SPA_GANGBLOCKSIZE - \
50310b9d77bSPawel Jakub Dawidek 	sizeof (zio_eck_t)) / sizeof (blkptr_t))
50410b9d77bSPawel Jakub Dawidek #define	SPA_GBH_FILLER		((SPA_GANGBLOCKSIZE - \
50510b9d77bSPawel Jakub Dawidek 	sizeof (zio_eck_t) - \
50610b9d77bSPawel Jakub Dawidek 	(SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\
50710b9d77bSPawel Jakub Dawidek 	sizeof (uint64_t))
50810b9d77bSPawel Jakub Dawidek 
50910b9d77bSPawel Jakub Dawidek typedef struct zio_gbh {
51010b9d77bSPawel Jakub Dawidek 	blkptr_t		zg_blkptr[SPA_GBH_NBLKPTRS];
51110b9d77bSPawel Jakub Dawidek 	uint64_t		zg_filler[SPA_GBH_FILLER];
51210b9d77bSPawel Jakub Dawidek 	zio_eck_t		zg_tail;
51310b9d77bSPawel Jakub Dawidek } zio_gbh_phys_t;
51410b9d77bSPawel Jakub Dawidek 
51510b9d77bSPawel Jakub Dawidek #define	VDEV_RAIDZ_MAXPARITY	3
5161ba4a712SPawel Jakub Dawidek 
5178fc25799SMartin Matuska #define	VDEV_PAD_SIZE		(8 << 10)
5183830659eSToomas Soome /* 2 padding areas (vl_pad1 and vl_be) to skip */
5198fc25799SMartin Matuska #define	VDEV_SKIP_SIZE		VDEV_PAD_SIZE * 2
5201ba4a712SPawel Jakub Dawidek #define	VDEV_PHYS_SIZE		(112 << 10)
5211ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_RING	(128 << 10)
5221ba4a712SPawel Jakub Dawidek 
52379a4bf89SToomas Soome /*
52479a4bf89SToomas Soome  * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock
52579a4bf89SToomas Soome  * ring when MMP is enabled.
52679a4bf89SToomas Soome  */
52779a4bf89SToomas Soome #define	MMP_BLOCKS_PER_LABEL	1
52879a4bf89SToomas Soome 
52979a4bf89SToomas Soome /* The largest uberblock we support is 8k. */
53079a4bf89SToomas Soome #define	MAX_UBERBLOCK_SHIFT	(13)
5311ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_SHIFT(vd)	\
53279a4bf89SToomas Soome 	MIN(MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT), MAX_UBERBLOCK_SHIFT)
5331ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_COUNT(vd)	\
5341ba4a712SPawel Jakub Dawidek 	(VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
5351ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_OFFSET(vd, n)	\
5361ba4a712SPawel Jakub Dawidek 	offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
5371ba4a712SPawel Jakub Dawidek #define	VDEV_UBERBLOCK_SIZE(vd)		(1ULL << VDEV_UBERBLOCK_SHIFT(vd))
5381ba4a712SPawel Jakub Dawidek 
5391ba4a712SPawel Jakub Dawidek typedef struct vdev_phys {
54010b9d77bSPawel Jakub Dawidek 	char		vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
54110b9d77bSPawel Jakub Dawidek 	zio_eck_t	vp_zbt;
5421ba4a712SPawel Jakub Dawidek } vdev_phys_t;
5431ba4a712SPawel Jakub Dawidek 
5443830659eSToomas Soome typedef enum vbe_vers {
5453830659eSToomas Soome 	/* The bootenv file is stored as ascii text in the envblock */
5463830659eSToomas Soome 	VB_RAW = 0,
5473830659eSToomas Soome 
5483830659eSToomas Soome 	/*
5493830659eSToomas Soome 	 * The bootenv file is converted to an nvlist and then packed into the
5503830659eSToomas Soome 	 * envblock.
5513830659eSToomas Soome 	 */
5523830659eSToomas Soome 	VB_NVLIST = 1
5533830659eSToomas Soome } vbe_vers_t;
5543830659eSToomas Soome 
5553830659eSToomas Soome typedef struct vdev_boot_envblock {
5563830659eSToomas Soome 	uint64_t	vbe_version;
5573830659eSToomas Soome 	char		vbe_bootenv[VDEV_PAD_SIZE - sizeof (uint64_t) -
5583830659eSToomas Soome 			sizeof (zio_eck_t)];
5593830659eSToomas Soome  	zio_eck_t	vbe_zbt;
5603830659eSToomas Soome } vdev_boot_envblock_t;
5613830659eSToomas Soome 
56277649f35SMark Johnston _Static_assert(sizeof (vdev_boot_envblock_t) == VDEV_PAD_SIZE,
56377649f35SMark Johnston     "bad size for vdev_boot_envblock_t");
5643830659eSToomas Soome 
5651ba4a712SPawel Jakub Dawidek typedef struct vdev_label {
5668fc25799SMartin Matuska 	char		vl_pad1[VDEV_PAD_SIZE];			/*  8K  */
5673830659eSToomas Soome 	vdev_boot_envblock_t	vl_be;				/*  8K  */
5681ba4a712SPawel Jakub Dawidek 	vdev_phys_t	vl_vdev_phys;				/* 112K	*/
5691ba4a712SPawel Jakub Dawidek 	char		vl_uberblock[VDEV_UBERBLOCK_RING];	/* 128K	*/
5701ba4a712SPawel Jakub Dawidek } vdev_label_t;							/* 256K total */
5711ba4a712SPawel Jakub Dawidek 
5721ba4a712SPawel Jakub Dawidek /*
5731ba4a712SPawel Jakub Dawidek  * vdev_dirty() flags
5741ba4a712SPawel Jakub Dawidek  */
5751ba4a712SPawel Jakub Dawidek #define	VDD_METASLAB	0x01
5761ba4a712SPawel Jakub Dawidek #define	VDD_DTL		0x02
5771ba4a712SPawel Jakub Dawidek 
5781ba4a712SPawel Jakub Dawidek /*
5791ba4a712SPawel Jakub Dawidek  * Size and offset of embedded boot loader region on each label.
5801ba4a712SPawel Jakub Dawidek  * The total size of the first two labels plus the boot area is 4MB.
5811ba4a712SPawel Jakub Dawidek  */
5821ba4a712SPawel Jakub Dawidek #define	VDEV_BOOT_OFFSET	(2 * sizeof (vdev_label_t))
5831ba4a712SPawel Jakub Dawidek #define	VDEV_BOOT_SIZE		(7ULL << 19)			/* 3.5M	*/
5841ba4a712SPawel Jakub Dawidek 
5851ba4a712SPawel Jakub Dawidek /*
5861ba4a712SPawel Jakub Dawidek  * Size of label regions at the start and end of each leaf device.
5871ba4a712SPawel Jakub Dawidek  */
5881ba4a712SPawel Jakub Dawidek #define	VDEV_LABEL_START_SIZE	(2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
5891ba4a712SPawel Jakub Dawidek #define	VDEV_LABEL_END_SIZE	(2 * sizeof (vdev_label_t))
5901ba4a712SPawel Jakub Dawidek #define	VDEV_LABELS		4
5911ba4a712SPawel Jakub Dawidek 
5921ba4a712SPawel Jakub Dawidek enum zio_checksum {
5931ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_INHERIT = 0,
5941ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_ON,
5951ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_OFF,
5961ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_LABEL,
5971ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_GANG_HEADER,
5981ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_ZILOG,
5991ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_FLETCHER_2,
6001ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_FLETCHER_4,
6011ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_SHA256,
60210b9d77bSPawel Jakub Dawidek 	ZIO_CHECKSUM_ZILOG2,
6032c55d090SToomas Soome 	ZIO_CHECKSUM_NOPARITY,
6042c55d090SToomas Soome 	ZIO_CHECKSUM_SHA512,
6052c55d090SToomas Soome 	ZIO_CHECKSUM_SKEIN,
6062c55d090SToomas Soome 	ZIO_CHECKSUM_EDONR,
60775ad2477SWarner Losh 	ZIO_CHECKSUM_BLAKE3,
6081ba4a712SPawel Jakub Dawidek 	ZIO_CHECKSUM_FUNCTIONS
6091ba4a712SPawel Jakub Dawidek };
6101ba4a712SPawel Jakub Dawidek 
61110b9d77bSPawel Jakub Dawidek #define	ZIO_CHECKSUM_ON_VALUE	ZIO_CHECKSUM_FLETCHER_4
6121ba4a712SPawel Jakub Dawidek #define	ZIO_CHECKSUM_DEFAULT	ZIO_CHECKSUM_ON
6131ba4a712SPawel Jakub Dawidek 
6141ba4a712SPawel Jakub Dawidek enum zio_compress {
6151ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_INHERIT = 0,
6161ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_ON,
6171ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_OFF,
6181ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_LZJB,
6191ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_EMPTY,
6201ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_1,
6211ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_2,
6221ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_3,
6231ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_4,
6241ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_5,
6251ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_6,
6261ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_7,
6271ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_8,
6281ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_GZIP_9,
62910b9d77bSPawel Jakub Dawidek 	ZIO_COMPRESS_ZLE,
630ef17620fSXin LI 	ZIO_COMPRESS_LZ4,
6312fec3ae8SWarner Losh 	ZIO_COMPRESS_ZSTD,
6321ba4a712SPawel Jakub Dawidek 	ZIO_COMPRESS_FUNCTIONS
6331ba4a712SPawel Jakub Dawidek };
6341ba4a712SPawel Jakub Dawidek 
6352fec3ae8SWarner Losh enum zio_zstd_levels {
6362fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_INHERIT = 0,
6372fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_1,
6382fec3ae8SWarner Losh #define	ZIO_ZSTD_LEVEL_MIN	ZIO_ZSTD_LEVEL_1
6392fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_2,
6402fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_3,
6412fec3ae8SWarner Losh #define	ZIO_ZSTD_LEVEL_DEFAULT	ZIO_ZSTD_LEVEL_3
6422fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_4,
6432fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_5,
6442fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_6,
6452fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_7,
6462fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_8,
6472fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_9,
6482fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_10,
6492fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_11,
6502fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_12,
6512fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_13,
6522fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_14,
6532fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_15,
6542fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_16,
6552fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_17,
6562fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_18,
6572fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_19,
6582fec3ae8SWarner Losh #define	ZIO_ZSTD_LEVEL_MAX	ZIO_ZSTD_LEVEL_19
6592fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_RESERVE = 101, /* Leave room for new positive levels */
6602fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST, /* Fast levels are negative */
6612fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_1,
6622fec3ae8SWarner Losh #define	ZIO_ZSTD_LEVEL_FAST_DEFAULT	ZIO_ZSTD_LEVEL_FAST_1
6632fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_2,
6642fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_3,
6652fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_4,
6662fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_5,
6672fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_6,
6682fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_7,
6692fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_8,
6702fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_9,
6712fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_10,
6722fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_20,
6732fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_30,
6742fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_40,
6752fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_50,
6762fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_60,
6772fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_70,
6782fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_80,
6792fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_90,
6802fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_100,
6812fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_500,
6822fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_FAST_1000,
6832fec3ae8SWarner Losh #define	ZIO_ZSTD_LEVEL_FAST_MAX	ZIO_ZSTD_LEVEL_FAST_1000
6842fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_AUTO = 251, /* Reserved for future use */
6852fec3ae8SWarner Losh 	ZIO_ZSTD_LEVEL_LEVELS
6862fec3ae8SWarner Losh };
6872fec3ae8SWarner Losh 
6881ba4a712SPawel Jakub Dawidek #define	ZIO_COMPRESS_ON_VALUE	ZIO_COMPRESS_LZJB
6891ba4a712SPawel Jakub Dawidek #define	ZIO_COMPRESS_DEFAULT	ZIO_COMPRESS_OFF
6901ba4a712SPawel Jakub Dawidek 
6911ba4a712SPawel Jakub Dawidek /*
6921ba4a712SPawel Jakub Dawidek  * On-disk version number.
6931ba4a712SPawel Jakub Dawidek  */
6940d16312bSDoug Rabson #define	SPA_VERSION_1			1ULL
6950d16312bSDoug Rabson #define	SPA_VERSION_2			2ULL
6960d16312bSDoug Rabson #define	SPA_VERSION_3			3ULL
6970d16312bSDoug Rabson #define	SPA_VERSION_4			4ULL
6980d16312bSDoug Rabson #define	SPA_VERSION_5			5ULL
6990d16312bSDoug Rabson #define	SPA_VERSION_6			6ULL
7000d16312bSDoug Rabson #define	SPA_VERSION_7			7ULL
7010d16312bSDoug Rabson #define	SPA_VERSION_8			8ULL
7020d16312bSDoug Rabson #define	SPA_VERSION_9			9ULL
7030d16312bSDoug Rabson #define	SPA_VERSION_10			10ULL
7040d16312bSDoug Rabson #define	SPA_VERSION_11			11ULL
705786895f6SDoug Rabson #define	SPA_VERSION_12			12ULL
706786895f6SDoug Rabson #define	SPA_VERSION_13			13ULL
70791891290SXin LI #define	SPA_VERSION_14			14ULL
7088fc25799SMartin Matuska #define	SPA_VERSION_15			15ULL
70910b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_16			16ULL
71010b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_17			17ULL
71110b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_18			18ULL
71210b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_19			19ULL
71310b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_20			20ULL
71410b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_21			21ULL
71510b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_22			22ULL
71610b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_23			23ULL
71710b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_24			24ULL
71810b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_25			25ULL
71910b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_26			26ULL
72010b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_27			27ULL
72110b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_28			28ULL
7222d9cf57eSMartin Matuska #define	SPA_VERSION_5000		5000ULL
72310b9d77bSPawel Jakub Dawidek 
7241ba4a712SPawel Jakub Dawidek /*
72510b9d77bSPawel Jakub Dawidek  * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk
72610b9d77bSPawel Jakub Dawidek  * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*},
72710b9d77bSPawel Jakub Dawidek  * and do the appropriate changes.  Also bump the version number in
72810b9d77bSPawel Jakub Dawidek  * usr/src/grub/capability.
7291ba4a712SPawel Jakub Dawidek  */
7302d9cf57eSMartin Matuska #define	SPA_VERSION			SPA_VERSION_5000
7312d9cf57eSMartin Matuska #define	SPA_VERSION_STRING		"5000"
7321ba4a712SPawel Jakub Dawidek 
7331ba4a712SPawel Jakub Dawidek /*
7340d16312bSDoug Rabson  * Symbolic names for the changes that caused a SPA_VERSION switch.
7351ba4a712SPawel Jakub Dawidek  * Used in the code when checking for presence or absence of a feature.
7361ba4a712SPawel Jakub Dawidek  * Feel free to define multiple symbolic names for each version if there
7371ba4a712SPawel Jakub Dawidek  * were multiple changes to on-disk structures during that version.
7381ba4a712SPawel Jakub Dawidek  *
7390d16312bSDoug Rabson  * NOTE: When checking the current SPA_VERSION in your code, be sure
7401ba4a712SPawel Jakub Dawidek  *       to use spa_version() since it reports the version of the
7411ba4a712SPawel Jakub Dawidek  *       last synced uberblock.  Checking the in-flight version can
7421ba4a712SPawel Jakub Dawidek  *       be dangerous in some cases.
7431ba4a712SPawel Jakub Dawidek  */
7440d16312bSDoug Rabson #define	SPA_VERSION_INITIAL		SPA_VERSION_1
7450d16312bSDoug Rabson #define	SPA_VERSION_DITTO_BLOCKS	SPA_VERSION_2
7460d16312bSDoug Rabson #define	SPA_VERSION_SPARES		SPA_VERSION_3
7470d16312bSDoug Rabson #define	SPA_VERSION_RAID6		SPA_VERSION_3
7480d16312bSDoug Rabson #define	SPA_VERSION_BPLIST_ACCOUNT	SPA_VERSION_3
7490d16312bSDoug Rabson #define	SPA_VERSION_RAIDZ_DEFLATE	SPA_VERSION_3
7500d16312bSDoug Rabson #define	SPA_VERSION_DNODE_BYTES		SPA_VERSION_3
7510d16312bSDoug Rabson #define	SPA_VERSION_ZPOOL_HISTORY	SPA_VERSION_4
7520d16312bSDoug Rabson #define	SPA_VERSION_GZIP_COMPRESSION	SPA_VERSION_5
7530d16312bSDoug Rabson #define	SPA_VERSION_BOOTFS		SPA_VERSION_6
754786895f6SDoug Rabson #define	SPA_VERSION_SLOGS		SPA_VERSION_7
755786895f6SDoug Rabson #define	SPA_VERSION_DELEGATED_PERMS	SPA_VERSION_8
756786895f6SDoug Rabson #define	SPA_VERSION_FUID		SPA_VERSION_9
757786895f6SDoug Rabson #define	SPA_VERSION_REFRESERVATION	SPA_VERSION_9
758786895f6SDoug Rabson #define	SPA_VERSION_REFQUOTA		SPA_VERSION_9
759786895f6SDoug Rabson #define	SPA_VERSION_UNIQUE_ACCURATE	SPA_VERSION_9
760786895f6SDoug Rabson #define	SPA_VERSION_L2CACHE		SPA_VERSION_10
761786895f6SDoug Rabson #define	SPA_VERSION_NEXT_CLONES		SPA_VERSION_11
762786895f6SDoug Rabson #define	SPA_VERSION_ORIGIN		SPA_VERSION_11
763786895f6SDoug Rabson #define	SPA_VERSION_DSL_SCRUB		SPA_VERSION_11
764786895f6SDoug Rabson #define	SPA_VERSION_SNAP_PROPS		SPA_VERSION_12
765786895f6SDoug Rabson #define	SPA_VERSION_USED_BREAKDOWN	SPA_VERSION_13
76691891290SXin LI #define	SPA_VERSION_PASSTHROUGH_X	SPA_VERSION_14
7678fc25799SMartin Matuska #define SPA_VERSION_USERSPACE		SPA_VERSION_15
76810b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_STMF_PROP		SPA_VERSION_16
76910b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_RAIDZ3		SPA_VERSION_17
77010b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_USERREFS		SPA_VERSION_18
77110b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_HOLES		SPA_VERSION_19
77210b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_ZLE_COMPRESSION	SPA_VERSION_20
77310b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_DEDUP		SPA_VERSION_21
77410b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_RECVD_PROPS		SPA_VERSION_22
77510b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_SLIM_ZIL		SPA_VERSION_23
77610b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_SA			SPA_VERSION_24
77710b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_SCAN		SPA_VERSION_25
77810b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_DIR_CLONES		SPA_VERSION_26
77910b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_DEADLISTS		SPA_VERSION_26
78010b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_FAST_SNAP		SPA_VERSION_27
78110b9d77bSPawel Jakub Dawidek #define	SPA_VERSION_MULTI_REPLACE	SPA_VERSION_28
7822d9cf57eSMartin Matuska #define	SPA_VERSION_BEFORE_FEATURES	SPA_VERSION_28
7832d9cf57eSMartin Matuska #define	SPA_VERSION_FEATURES		SPA_VERSION_5000
7842d9cf57eSMartin Matuska 
7852d9cf57eSMartin Matuska #define	SPA_VERSION_IS_SUPPORTED(v) \
7862d9cf57eSMartin Matuska 	(((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \
7872d9cf57eSMartin Matuska 	((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION))
7881ba4a712SPawel Jakub Dawidek 
7891ba4a712SPawel Jakub Dawidek /*
7901ba4a712SPawel Jakub Dawidek  * The following are configuration names used in the nvlist describing a pool's
7911ba4a712SPawel Jakub Dawidek  * configuration.
7921ba4a712SPawel Jakub Dawidek  */
7931ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_VERSION		"version"
7941ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_NAME		"name"
7951ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_STATE		"state"
7961ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_TXG		"txg"
7971ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_POOL_GUID		"pool_guid"
7981ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_CREATE_TXG		"create_txg"
7991ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_TOP_GUID		"top_guid"
8001ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_VDEV_TREE		"vdev_tree"
8011ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_TYPE		"type"
8021ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_CHILDREN		"children"
8031ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ID			"id"
8041ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_GUID		"guid"
805b1b93268SToomas Soome #define	ZPOOL_CONFIG_INDIRECT_OBJECT	"com.delphix:indirect_object"
806b1b93268SToomas Soome #define	ZPOOL_CONFIG_INDIRECT_BIRTHS	"com.delphix:indirect_births"
807b1b93268SToomas Soome #define	ZPOOL_CONFIG_PREV_INDIRECT_VDEV	"com.delphix:prev_indirect_vdev"
8081ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_PATH		"path"
8091ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_DEVID		"devid"
8101ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_METASLAB_ARRAY	"metaslab_array"
8111ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_METASLAB_SHIFT	"metaslab_shift"
8121ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ASHIFT		"ashift"
8131ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ASIZE		"asize"
8141ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_DTL		"DTL"
8151ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_STATS		"stats"
8161ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_WHOLE_DISK		"whole_disk"
8171ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_ERRCOUNT		"error_count"
8181ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_NOT_PRESENT	"not_present"
8191ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_SPARES		"spares"
8201ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_IS_SPARE		"is_spare"
8211ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_NPARITY		"nparity"
8221ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_HOSTID		"hostid"
8231ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_HOSTNAME		"hostname"
8246a85b5e0SPawel Jakub Dawidek #define	ZPOOL_CONFIG_IS_LOG		"is_log"
8251ba4a712SPawel Jakub Dawidek #define	ZPOOL_CONFIG_TIMESTAMP		"timestamp" /* not stored on disk */
8262d9cf57eSMartin Matuska #define	ZPOOL_CONFIG_FEATURES_FOR_READ	"features_for_read"
8273c2db0efSToomas Soome #define	ZPOOL_CONFIG_VDEV_CHILDREN	"vdev_children"
8281ba4a712SPawel Jakub Dawidek 
82903ec6213SXin LI /*
83003ec6213SXin LI  * The persistent vdev state is stored as separate values rather than a single
83103ec6213SXin LI  * 'vdev_state' entry.  This is because a device can be in multiple states, such
83203ec6213SXin LI  * as offline and degraded.
83303ec6213SXin LI  */
83403ec6213SXin LI #define	ZPOOL_CONFIG_OFFLINE            "offline"
83503ec6213SXin LI #define	ZPOOL_CONFIG_FAULTED            "faulted"
83603ec6213SXin LI #define	ZPOOL_CONFIG_DEGRADED           "degraded"
83703ec6213SXin LI #define	ZPOOL_CONFIG_REMOVED            "removed"
83810b9d77bSPawel Jakub Dawidek #define	ZPOOL_CONFIG_FRU		"fru"
83910b9d77bSPawel Jakub Dawidek #define	ZPOOL_CONFIG_AUX_STATE		"aux_state"
84003ec6213SXin LI 
8411ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_ROOT			"root"
8421ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_MIRROR		"mirror"
8431ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_REPLACING		"replacing"
8441ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_RAIDZ			"raidz"
8451ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_DISK			"disk"
8461ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_FILE			"file"
8471ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_MISSING		"missing"
84810b9d77bSPawel Jakub Dawidek #define	VDEV_TYPE_HOLE			"hole"
8491ba4a712SPawel Jakub Dawidek #define	VDEV_TYPE_SPARE			"spare"
85010b9d77bSPawel Jakub Dawidek #define	VDEV_TYPE_LOG			"log"
85110b9d77bSPawel Jakub Dawidek #define	VDEV_TYPE_L2CACHE		"l2cache"
852b1b93268SToomas Soome #define	VDEV_TYPE_INDIRECT		"indirect"
8531ba4a712SPawel Jakub Dawidek 
8541ba4a712SPawel Jakub Dawidek /*
8551ba4a712SPawel Jakub Dawidek  * This is needed in userland to report the minimum necessary device size.
8561ba4a712SPawel Jakub Dawidek  */
8571ba4a712SPawel Jakub Dawidek #define	SPA_MINDEVSIZE		(64ULL << 20)
8581ba4a712SPawel Jakub Dawidek 
8591ba4a712SPawel Jakub Dawidek /*
8601ba4a712SPawel Jakub Dawidek  * The location of the pool configuration repository, shared between kernel and
8611ba4a712SPawel Jakub Dawidek  * userland.
8621ba4a712SPawel Jakub Dawidek  */
86310b9d77bSPawel Jakub Dawidek #define	ZPOOL_CACHE		"/boot/zfs/zpool.cache"
8641ba4a712SPawel Jakub Dawidek 
8651ba4a712SPawel Jakub Dawidek /*
8661ba4a712SPawel Jakub Dawidek  * vdev states are ordered from least to most healthy.
8671ba4a712SPawel Jakub Dawidek  * A vdev that's CANT_OPEN or below is considered unusable.
8681ba4a712SPawel Jakub Dawidek  */
8691ba4a712SPawel Jakub Dawidek typedef enum vdev_state {
8701ba4a712SPawel Jakub Dawidek 	VDEV_STATE_UNKNOWN = 0,	/* Uninitialized vdev			*/
8711ba4a712SPawel Jakub Dawidek 	VDEV_STATE_CLOSED,	/* Not currently open			*/
8721ba4a712SPawel Jakub Dawidek 	VDEV_STATE_OFFLINE,	/* Not allowed to open			*/
87303ec6213SXin LI 	VDEV_STATE_REMOVED,	/* Explicitly removed from system	*/
8741ba4a712SPawel Jakub Dawidek 	VDEV_STATE_CANT_OPEN,	/* Tried to open, but failed		*/
87503ec6213SXin LI 	VDEV_STATE_FAULTED,	/* External request to fault device	*/
8761ba4a712SPawel Jakub Dawidek 	VDEV_STATE_DEGRADED,	/* Replicated vdev with unhealthy kids	*/
8771ba4a712SPawel Jakub Dawidek 	VDEV_STATE_HEALTHY	/* Presumed good			*/
8781ba4a712SPawel Jakub Dawidek } vdev_state_t;
8791ba4a712SPawel Jakub Dawidek 
8801ba4a712SPawel Jakub Dawidek /*
8811ba4a712SPawel Jakub Dawidek  * vdev aux states.  When a vdev is in the CANT_OPEN state, the aux field
8821ba4a712SPawel Jakub Dawidek  * of the vdev stats structure uses these constants to distinguish why.
8831ba4a712SPawel Jakub Dawidek  */
8841ba4a712SPawel Jakub Dawidek typedef enum vdev_aux {
8851ba4a712SPawel Jakub Dawidek 	VDEV_AUX_NONE,		/* no error				*/
8861ba4a712SPawel Jakub Dawidek 	VDEV_AUX_OPEN_FAILED,	/* ldi_open_*() or vn_open() failed	*/
8871ba4a712SPawel Jakub Dawidek 	VDEV_AUX_CORRUPT_DATA,	/* bad label or disk contents		*/
8881ba4a712SPawel Jakub Dawidek 	VDEV_AUX_NO_REPLICAS,	/* insufficient number of replicas	*/
8891ba4a712SPawel Jakub Dawidek 	VDEV_AUX_BAD_GUID_SUM,	/* vdev guid sum doesn't match		*/
8901ba4a712SPawel Jakub Dawidek 	VDEV_AUX_TOO_SMALL,	/* vdev size is too small		*/
8911ba4a712SPawel Jakub Dawidek 	VDEV_AUX_BAD_LABEL,	/* the label is OK but invalid		*/
8921ba4a712SPawel Jakub Dawidek 	VDEV_AUX_VERSION_NEWER,	/* on-disk version is too new		*/
8931ba4a712SPawel Jakub Dawidek 	VDEV_AUX_VERSION_OLDER,	/* on-disk version is too old		*/
8941ba4a712SPawel Jakub Dawidek 	VDEV_AUX_SPARED		/* hot spare used in another pool	*/
8951ba4a712SPawel Jakub Dawidek } vdev_aux_t;
8961ba4a712SPawel Jakub Dawidek 
8971ba4a712SPawel Jakub Dawidek /*
8981ba4a712SPawel Jakub Dawidek  * pool state.  The following states are written to disk as part of the normal
8991ba4a712SPawel Jakub Dawidek  * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE.  The remaining states are
9001ba4a712SPawel Jakub Dawidek  * software abstractions used at various levels to communicate pool state.
9011ba4a712SPawel Jakub Dawidek  */
9021ba4a712SPawel Jakub Dawidek typedef enum pool_state {
9031ba4a712SPawel Jakub Dawidek 	POOL_STATE_ACTIVE = 0,		/* In active use		*/
9041ba4a712SPawel Jakub Dawidek 	POOL_STATE_EXPORTED,		/* Explicitly exported		*/
9051ba4a712SPawel Jakub Dawidek 	POOL_STATE_DESTROYED,		/* Explicitly destroyed		*/
9061ba4a712SPawel Jakub Dawidek 	POOL_STATE_SPARE,		/* Reserved for hot spare use	*/
9071ba4a712SPawel Jakub Dawidek 	POOL_STATE_UNINITIALIZED,	/* Internal spa_t state		*/
9081ba4a712SPawel Jakub Dawidek 	POOL_STATE_UNAVAIL,		/* Internal libzfs state	*/
9091ba4a712SPawel Jakub Dawidek 	POOL_STATE_POTENTIALLY_ACTIVE	/* Internal libzfs state	*/
9101ba4a712SPawel Jakub Dawidek } pool_state_t;
9111ba4a712SPawel Jakub Dawidek 
9121ba4a712SPawel Jakub Dawidek /*
9131ba4a712SPawel Jakub Dawidek  * The uberblock version is incremented whenever an incompatible on-disk
9141ba4a712SPawel Jakub Dawidek  * format change is made to the SPA, DMU, or ZAP.
9151ba4a712SPawel Jakub Dawidek  *
9161ba4a712SPawel Jakub Dawidek  * Note: the first two fields should never be moved.  When a storage pool
9171ba4a712SPawel Jakub Dawidek  * is opened, the uberblock must be read off the disk before the version
9181ba4a712SPawel Jakub Dawidek  * can be checked.  If the ub_version field is moved, we may not detect
9191ba4a712SPawel Jakub Dawidek  * version mismatch.  If the ub_magic field is moved, applications that
9201ba4a712SPawel Jakub Dawidek  * expect the magic number in the first word won't work.
9211ba4a712SPawel Jakub Dawidek  */
9221ba4a712SPawel Jakub Dawidek #define	UBERBLOCK_MAGIC		0x00bab10c		/* oo-ba-bloc!	*/
9231ba4a712SPawel Jakub Dawidek #define	UBERBLOCK_SHIFT		10			/* up to 1K	*/
9241ba4a712SPawel Jakub Dawidek 
92579a4bf89SToomas Soome #define	MMP_MAGIC		0xa11cea11		/* all-see-all  */
92679a4bf89SToomas Soome 
92779a4bf89SToomas Soome #define	MMP_INTERVAL_VALID_BIT	0x01
92879a4bf89SToomas Soome #define	MMP_SEQ_VALID_BIT	0x02
92979a4bf89SToomas Soome #define	MMP_FAIL_INT_VALID_BIT	0x04
93079a4bf89SToomas Soome 
93179a4bf89SToomas Soome #define	MMP_VALID(ubp)		(ubp->ub_magic == UBERBLOCK_MAGIC && \
93279a4bf89SToomas Soome 				    ubp->ub_mmp_magic == MMP_MAGIC)
93379a4bf89SToomas Soome #define	MMP_INTERVAL_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
93479a4bf89SToomas Soome 				    MMP_INTERVAL_VALID_BIT))
93579a4bf89SToomas Soome #define	MMP_SEQ_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
93679a4bf89SToomas Soome 				    MMP_SEQ_VALID_BIT))
93779a4bf89SToomas Soome #define	MMP_FAIL_INT_VALID(ubp)	(MMP_VALID(ubp) && (ubp->ub_mmp_config & \
93879a4bf89SToomas Soome 				    MMP_FAIL_INT_VALID_BIT))
93979a4bf89SToomas Soome 
94079a4bf89SToomas Soome #define	MMP_INTERVAL(ubp)	((ubp->ub_mmp_config & 0x00000000FFFFFF00) \
94179a4bf89SToomas Soome 				    >> 8)
94279a4bf89SToomas Soome #define	MMP_SEQ(ubp)		((ubp->ub_mmp_config & 0x0000FFFF00000000) \
94379a4bf89SToomas Soome 				    >> 32)
94479a4bf89SToomas Soome #define	MMP_FAIL_INT(ubp)	((ubp->ub_mmp_config & 0xFFFF000000000000) \
94579a4bf89SToomas Soome 				    >> 48)
94679a4bf89SToomas Soome 
94779a4bf89SToomas Soome typedef struct uberblock {
9481ba4a712SPawel Jakub Dawidek 	uint64_t	ub_magic;	/* UBERBLOCK_MAGIC		*/
9490d16312bSDoug Rabson 	uint64_t	ub_version;	/* SPA_VERSION			*/
9501ba4a712SPawel Jakub Dawidek 	uint64_t	ub_txg;		/* txg of last sync		*/
9511ba4a712SPawel Jakub Dawidek 	uint64_t	ub_guid_sum;	/* sum of all vdev guids	*/
9521ba4a712SPawel Jakub Dawidek 	uint64_t	ub_timestamp;	/* UTC time of last sync	*/
9531ba4a712SPawel Jakub Dawidek 	blkptr_t	ub_rootbp;	/* MOS objset_phys_t		*/
95479a4bf89SToomas Soome 	/* highest SPA_VERSION supported by software that wrote this txg */
95579a4bf89SToomas Soome 	uint64_t	ub_software_version;
95679a4bf89SToomas Soome 	/* Maybe missing in uberblocks we read, but always written */
95779a4bf89SToomas Soome 	uint64_t	ub_mmp_magic;
95879a4bf89SToomas Soome 	/*
95979a4bf89SToomas Soome 	 * If ub_mmp_delay == 0 and ub_mmp_magic is valid, MMP is off.
96079a4bf89SToomas Soome 	 * Otherwise, nanosec since last MMP write.
96179a4bf89SToomas Soome 	 */
96279a4bf89SToomas Soome 	uint64_t	ub_mmp_delay;
96379a4bf89SToomas Soome 
96479a4bf89SToomas Soome 	/*
96579a4bf89SToomas Soome 	 * The ub_mmp_config contains the multihost write interval, multihost
96679a4bf89SToomas Soome 	 * fail intervals, sequence number for sub-second granularity, and
96779a4bf89SToomas Soome 	 * valid bit mask.  This layout is as follows:
96879a4bf89SToomas Soome 	 *
96979a4bf89SToomas Soome 	 *   64      56      48      40      32      24      16      8       0
97079a4bf89SToomas Soome 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
97179a4bf89SToomas Soome 	 * 0 | Fail Intervals|      Seq      |   Write Interval (ms) | VALID |
97279a4bf89SToomas Soome 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
97379a4bf89SToomas Soome 	 *
97479a4bf89SToomas Soome 	 * This allows a write_interval of (2^24/1000)s, over 4.5 hours
97579a4bf89SToomas Soome 	 *
97679a4bf89SToomas Soome 	 * VALID Bits:
97779a4bf89SToomas Soome 	 * - 0x01 - Write Interval (ms)
97879a4bf89SToomas Soome 	 * - 0x02 - Sequence number exists
97979a4bf89SToomas Soome 	 * - 0x04 - Fail Intervals
98079a4bf89SToomas Soome 	 * - 0xf8 - Reserved
98179a4bf89SToomas Soome 	 */
98279a4bf89SToomas Soome 	uint64_t	ub_mmp_config;
98379a4bf89SToomas Soome 
98479a4bf89SToomas Soome 	/*
98579a4bf89SToomas Soome 	 * ub_checkpoint_txg indicates two things about the current uberblock:
98679a4bf89SToomas Soome 	 *
98779a4bf89SToomas Soome 	 * 1] If it is not zero then this uberblock is a checkpoint. If it is
98879a4bf89SToomas Soome 	 *    zero, then this uberblock is not a checkpoint.
98979a4bf89SToomas Soome 	 *
99079a4bf89SToomas Soome 	 * 2] On checkpointed uberblocks, the value of ub_checkpoint_txg is
99179a4bf89SToomas Soome 	 *    the ub_txg that the uberblock had at the time we moved it to
99279a4bf89SToomas Soome 	 *    the MOS config.
99379a4bf89SToomas Soome 	 *
99479a4bf89SToomas Soome 	 * The field is set when we checkpoint the uberblock and continues to
99579a4bf89SToomas Soome 	 * hold that value even after we've rewound (unlike the ub_txg that
99679a4bf89SToomas Soome 	 * is reset to a higher value).
99779a4bf89SToomas Soome 	 *
99879a4bf89SToomas Soome 	 * Besides checks used to determine whether we are reopening the
99979a4bf89SToomas Soome 	 * pool from a checkpointed uberblock [see spa_ld_select_uberblock()],
100079a4bf89SToomas Soome 	 * the value of the field is used to determine which ZIL blocks have
100179a4bf89SToomas Soome 	 * been allocated according to the ms_sm when we are rewinding to a
100279a4bf89SToomas Soome 	 * checkpoint. Specifically, if blk_birth > ub_checkpoint_txg, then
100379a4bf89SToomas Soome 	 * the ZIL block is not allocated [see uses of spa_min_claim_txg()].
100479a4bf89SToomas Soome 	 */
100579a4bf89SToomas Soome 	uint64_t	ub_checkpoint_txg;
100679a4bf89SToomas Soome } uberblock_t;
10071ba4a712SPawel Jakub Dawidek 
10081ba4a712SPawel Jakub Dawidek /*
10091ba4a712SPawel Jakub Dawidek  * Flags.
10101ba4a712SPawel Jakub Dawidek  */
10111ba4a712SPawel Jakub Dawidek #define	DNODE_MUST_BE_ALLOCATED	1
10121ba4a712SPawel Jakub Dawidek #define	DNODE_MUST_BE_FREE	2
10131ba4a712SPawel Jakub Dawidek 
10141ba4a712SPawel Jakub Dawidek /*
10151ba4a712SPawel Jakub Dawidek  * Fixed constants.
10161ba4a712SPawel Jakub Dawidek  */
10171ba4a712SPawel Jakub Dawidek #define	DNODE_SHIFT		9	/* 512 bytes */
1018263f396eSXin LI #define	DN_MIN_INDBLKSHIFT	12	/* 4k */
1019b1b93268SToomas Soome #define	DN_MAX_INDBLKSHIFT	17	/* 128k */
10201ba4a712SPawel Jakub Dawidek #define	DNODE_BLOCK_SHIFT	14	/* 16k */
10211ba4a712SPawel Jakub Dawidek #define	DNODE_CORE_SIZE		64	/* 64 bytes for dnode sans blkptrs */
10221ba4a712SPawel Jakub Dawidek #define	DN_MAX_OBJECT_SHIFT	48	/* 256 trillion (zfs_fid_t limit) */
10231ba4a712SPawel Jakub Dawidek #define	DN_MAX_OFFSET_SHIFT	64	/* 2^64 bytes in a dnode */
10241ba4a712SPawel Jakub Dawidek 
10251ba4a712SPawel Jakub Dawidek /*
10261ba4a712SPawel Jakub Dawidek  * Derived constants.
10271ba4a712SPawel Jakub Dawidek  */
1028b40aaca6SToomas Soome #define	DNODE_MIN_SIZE		(1 << DNODE_SHIFT)
1029b40aaca6SToomas Soome #define	DNODE_MAX_SIZE		(1 << DNODE_BLOCK_SHIFT)
1030b40aaca6SToomas Soome #define	DNODE_BLOCK_SIZE	(1 << DNODE_BLOCK_SHIFT)
1031b40aaca6SToomas Soome #define	DNODE_MIN_SLOTS		(DNODE_MIN_SIZE >> DNODE_SHIFT)
1032b40aaca6SToomas Soome #define	DNODE_MAX_SLOTS		(DNODE_MAX_SIZE >> DNODE_SHIFT)
1033b40aaca6SToomas Soome #define	DN_BONUS_SIZE(dnsize)	((dnsize) - DNODE_CORE_SIZE - \
1034b40aaca6SToomas Soome 	(1 << SPA_BLKPTRSHIFT))
1035b40aaca6SToomas Soome #define	DN_SLOTS_TO_BONUSLEN(slots)	DN_BONUS_SIZE((slots) << DNODE_SHIFT)
1036b40aaca6SToomas Soome #define	DN_OLD_MAX_BONUSLEN		(DN_BONUS_SIZE(DNODE_MIN_SIZE))
1037b40aaca6SToomas Soome #define	DN_MAX_NBLKPTR		((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> \
1038b40aaca6SToomas Soome 	SPA_BLKPTRSHIFT)
10391ba4a712SPawel Jakub Dawidek #define	DN_MAX_OBJECT		(1ULL << DN_MAX_OBJECT_SHIFT)
1040b40aaca6SToomas Soome #define	DN_ZERO_BONUSLEN	(DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1)
10411ba4a712SPawel Jakub Dawidek 
10421ba4a712SPawel Jakub Dawidek #define	DNODES_PER_BLOCK_SHIFT	(DNODE_BLOCK_SHIFT - DNODE_SHIFT)
10431ba4a712SPawel Jakub Dawidek #define	DNODES_PER_BLOCK	(1ULL << DNODES_PER_BLOCK_SHIFT)
10441ba4a712SPawel Jakub Dawidek #define	DNODES_PER_LEVEL_SHIFT	(DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT)
10451ba4a712SPawel Jakub Dawidek 
10461ba4a712SPawel Jakub Dawidek /* The +2 here is a cheesy way to round up */
10471ba4a712SPawel Jakub Dawidek #define	DN_MAX_LEVELS	(2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \
10481ba4a712SPawel Jakub Dawidek 	(DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT)))
10491ba4a712SPawel Jakub Dawidek 
10501ba4a712SPawel Jakub Dawidek #define	DN_BONUS(dnp)	((void*)((dnp)->dn_bonus + \
10511ba4a712SPawel Jakub Dawidek 	(((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t))))
10521ba4a712SPawel Jakub Dawidek 
10531ba4a712SPawel Jakub Dawidek #define	DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \
10541ba4a712SPawel Jakub Dawidek 	(dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT)
10551ba4a712SPawel Jakub Dawidek 
10561ba4a712SPawel Jakub Dawidek #define	EPB(blkshift, typeshift)	(1 << (blkshift - typeshift))
10571ba4a712SPawel Jakub Dawidek 
10581ba4a712SPawel Jakub Dawidek /* Is dn_used in bytes?  if not, it's in multiples of SPA_MINBLOCKSIZE */
10591ba4a712SPawel Jakub Dawidek #define	DNODE_FLAG_USED_BYTES		(1<<0)
106010b9d77bSPawel Jakub Dawidek #define	DNODE_FLAG_USERUSED_ACCOUNTED	(1<<1)
106110b9d77bSPawel Jakub Dawidek 
106210b9d77bSPawel Jakub Dawidek /* Does dnode have a SA spill blkptr in bonus? */
106310b9d77bSPawel Jakub Dawidek #define	DNODE_FLAG_SPILL_BLKPTR	(1<<2)
10641ba4a712SPawel Jakub Dawidek 
10651ba4a712SPawel Jakub Dawidek typedef struct dnode_phys {
10661ba4a712SPawel Jakub Dawidek 	uint8_t dn_type;		/* dmu_object_type_t */
10671ba4a712SPawel Jakub Dawidek 	uint8_t dn_indblkshift;		/* ln2(indirect block size) */
10681ba4a712SPawel Jakub Dawidek 	uint8_t dn_nlevels;		/* 1=dn_blkptr->data blocks */
10691ba4a712SPawel Jakub Dawidek 	uint8_t dn_nblkptr;		/* length of dn_blkptr */
10701ba4a712SPawel Jakub Dawidek 	uint8_t dn_bonustype;		/* type of data in bonus buffer */
10711ba4a712SPawel Jakub Dawidek 	uint8_t	dn_checksum;		/* ZIO_CHECKSUM type */
10721ba4a712SPawel Jakub Dawidek 	uint8_t	dn_compress;		/* ZIO_COMPRESS type */
10731ba4a712SPawel Jakub Dawidek 	uint8_t dn_flags;		/* DNODE_FLAG_* */
10741ba4a712SPawel Jakub Dawidek 	uint16_t dn_datablkszsec;	/* data block size in 512b sectors */
10751ba4a712SPawel Jakub Dawidek 	uint16_t dn_bonuslen;		/* length of dn_bonus */
1076b40aaca6SToomas Soome 	uint8_t dn_extra_slots;		/* # of subsequent slots consumed */
1077b40aaca6SToomas Soome 	uint8_t dn_pad2[3];
10781ba4a712SPawel Jakub Dawidek 
10791ba4a712SPawel Jakub Dawidek 	/* accounting is protected by dn_dirty_mtx */
10801ba4a712SPawel Jakub Dawidek 	uint64_t dn_maxblkid;		/* largest allocated block ID */
10811ba4a712SPawel Jakub Dawidek 	uint64_t dn_used;		/* bytes (or sectors) of disk space */
10821ba4a712SPawel Jakub Dawidek 
10831ba4a712SPawel Jakub Dawidek 	uint64_t dn_pad3[4];
10841ba4a712SPawel Jakub Dawidek 
1085b40aaca6SToomas Soome 	/*
1086b40aaca6SToomas Soome 	 * The tail region is 448 bytes for a 512 byte dnode, and
1087b40aaca6SToomas Soome 	 * correspondingly larger for larger dnode sizes. The spill
1088b40aaca6SToomas Soome 	 * block pointer, when present, is always at the end of the tail
1089b40aaca6SToomas Soome 	 * region. There are three ways this space may be used, using
1090b40aaca6SToomas Soome 	 * a 512 byte dnode for this diagram:
1091b40aaca6SToomas Soome 	 *
1092b40aaca6SToomas Soome 	 * 0       64      128     192     256     320     384     448 (offset)
1093b40aaca6SToomas Soome 	 * +---------------+---------------+---------------+-------+
1094b40aaca6SToomas Soome 	 * | dn_blkptr[0]  | dn_blkptr[1]  | dn_blkptr[2]  | /     |
1095b40aaca6SToomas Soome 	 * +---------------+---------------+---------------+-------+
1096b40aaca6SToomas Soome 	 * | dn_blkptr[0]  | dn_bonus[0..319]                      |
1097b40aaca6SToomas Soome 	 * +---------------+-----------------------+---------------+
1098b40aaca6SToomas Soome 	 * | dn_blkptr[0]  | dn_bonus[0..191]      | dn_spill      |
1099b40aaca6SToomas Soome 	 * +---------------+-----------------------+---------------+
1100b40aaca6SToomas Soome 	 */
1101b40aaca6SToomas Soome 	union {
1102b40aaca6SToomas Soome 		blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)];
1103b40aaca6SToomas Soome 		struct {
1104b40aaca6SToomas Soome 			blkptr_t __dn_ignore1;
1105b40aaca6SToomas Soome 			uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN];
1106b40aaca6SToomas Soome 		};
1107b40aaca6SToomas Soome 		struct {
1108b40aaca6SToomas Soome 			blkptr_t __dn_ignore2;
1109b40aaca6SToomas Soome 			uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN -
1110b40aaca6SToomas Soome 			    sizeof (blkptr_t)];
111110b9d77bSPawel Jakub Dawidek 			blkptr_t dn_spill;
1112b40aaca6SToomas Soome 		};
1113b40aaca6SToomas Soome 	};
11141ba4a712SPawel Jakub Dawidek } dnode_phys_t;
11151ba4a712SPawel Jakub Dawidek 
1116b40aaca6SToomas Soome #define	DN_SPILL_BLKPTR(dnp)	(blkptr_t *)((char *)(dnp) + \
1117b40aaca6SToomas Soome 	(((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT))
1118b40aaca6SToomas Soome 
11194deb8929SAllan Jude typedef enum dmu_object_byteswap {
11204deb8929SAllan Jude 	DMU_BSWAP_UINT8,
11214deb8929SAllan Jude 	DMU_BSWAP_UINT16,
11224deb8929SAllan Jude 	DMU_BSWAP_UINT32,
11234deb8929SAllan Jude 	DMU_BSWAP_UINT64,
11244deb8929SAllan Jude 	DMU_BSWAP_ZAP,
11254deb8929SAllan Jude 	DMU_BSWAP_DNODE,
11264deb8929SAllan Jude 	DMU_BSWAP_OBJSET,
11274deb8929SAllan Jude 	DMU_BSWAP_ZNODE,
11284deb8929SAllan Jude 	DMU_BSWAP_OLDACL,
11294deb8929SAllan Jude 	DMU_BSWAP_ACL,
11304deb8929SAllan Jude 	/*
11314deb8929SAllan Jude 	 * Allocating a new byteswap type number makes the on-disk format
11324deb8929SAllan Jude 	 * incompatible with any other format that uses the same number.
11334deb8929SAllan Jude 	 *
11344deb8929SAllan Jude 	 * Data can usually be structured to work with one of the
11354deb8929SAllan Jude 	 * DMU_BSWAP_UINT* or DMU_BSWAP_ZAP types.
11364deb8929SAllan Jude 	 */
11374deb8929SAllan Jude 	DMU_BSWAP_NUMFUNCS
11384deb8929SAllan Jude } dmu_object_byteswap_t;
11394deb8929SAllan Jude 
11404deb8929SAllan Jude #define	DMU_OT_NEWTYPE 0x80
11414deb8929SAllan Jude #define	DMU_OT_METADATA 0x40
11424deb8929SAllan Jude #define	DMU_OT_BYTESWAP_MASK 0x3f
11434deb8929SAllan Jude 
11444deb8929SAllan Jude /*
11454deb8929SAllan Jude  * Defines a uint8_t object type. Object types specify if the data
11464deb8929SAllan Jude  * in the object is metadata (boolean) and how to byteswap the data
11474deb8929SAllan Jude  * (dmu_object_byteswap_t).
11484deb8929SAllan Jude  */
11494deb8929SAllan Jude #define	DMU_OT(byteswap, metadata) \
11504deb8929SAllan Jude 	(DMU_OT_NEWTYPE | \
11514deb8929SAllan Jude 	((metadata) ? DMU_OT_METADATA : 0) | \
11524deb8929SAllan Jude 	((byteswap) & DMU_OT_BYTESWAP_MASK))
11534deb8929SAllan Jude 
11541ba4a712SPawel Jakub Dawidek typedef enum dmu_object_type {
11551ba4a712SPawel Jakub Dawidek 	DMU_OT_NONE,
11561ba4a712SPawel Jakub Dawidek 	/* general: */
11571ba4a712SPawel Jakub Dawidek 	DMU_OT_OBJECT_DIRECTORY,	/* ZAP */
11581ba4a712SPawel Jakub Dawidek 	DMU_OT_OBJECT_ARRAY,		/* UINT64 */
11591ba4a712SPawel Jakub Dawidek 	DMU_OT_PACKED_NVLIST,		/* UINT8 (XDR by nvlist_pack/unpack) */
11601ba4a712SPawel Jakub Dawidek 	DMU_OT_PACKED_NVLIST_SIZE,	/* UINT64 */
116177649f35SMark Johnston 	DMU_OT_BPOBJ,			/* UINT64 */
116277649f35SMark Johnston 	DMU_OT_BPOBJ_HDR,		/* UINT64 */
11631ba4a712SPawel Jakub Dawidek 	/* spa: */
11641ba4a712SPawel Jakub Dawidek 	DMU_OT_SPACE_MAP_HEADER,	/* UINT64 */
11651ba4a712SPawel Jakub Dawidek 	DMU_OT_SPACE_MAP,		/* UINT64 */
11661ba4a712SPawel Jakub Dawidek 	/* zil: */
11671ba4a712SPawel Jakub Dawidek 	DMU_OT_INTENT_LOG,		/* UINT64 */
11681ba4a712SPawel Jakub Dawidek 	/* dmu: */
11691ba4a712SPawel Jakub Dawidek 	DMU_OT_DNODE,			/* DNODE */
11701ba4a712SPawel Jakub Dawidek 	DMU_OT_OBJSET,			/* OBJSET */
11711ba4a712SPawel Jakub Dawidek 	/* dsl: */
11721ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DIR,			/* UINT64 */
11731ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DIR_CHILD_MAP,	/* ZAP */
11741ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DS_SNAP_MAP,		/* ZAP */
11751ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_PROPS,		/* ZAP */
11761ba4a712SPawel Jakub Dawidek 	DMU_OT_DSL_DATASET,		/* UINT64 */
11771ba4a712SPawel Jakub Dawidek 	/* zpl: */
11781ba4a712SPawel Jakub Dawidek 	DMU_OT_ZNODE,			/* ZNODE */
117910b9d77bSPawel Jakub Dawidek 	DMU_OT_OLDACL,			/* Old ACL */
11801ba4a712SPawel Jakub Dawidek 	DMU_OT_PLAIN_FILE_CONTENTS,	/* UINT8 */
11811ba4a712SPawel Jakub Dawidek 	DMU_OT_DIRECTORY_CONTENTS,	/* ZAP */
11821ba4a712SPawel Jakub Dawidek 	DMU_OT_MASTER_NODE,		/* ZAP */
11831ba4a712SPawel Jakub Dawidek 	DMU_OT_UNLINKED_SET,		/* ZAP */
11841ba4a712SPawel Jakub Dawidek 	/* zvol: */
11851ba4a712SPawel Jakub Dawidek 	DMU_OT_ZVOL,			/* UINT8 */
11861ba4a712SPawel Jakub Dawidek 	DMU_OT_ZVOL_PROP,		/* ZAP */
11871ba4a712SPawel Jakub Dawidek 	/* other; for testing only! */
11881ba4a712SPawel Jakub Dawidek 	DMU_OT_PLAIN_OTHER,		/* UINT8 */
11891ba4a712SPawel Jakub Dawidek 	DMU_OT_UINT64_OTHER,		/* UINT64 */
11901ba4a712SPawel Jakub Dawidek 	DMU_OT_ZAP_OTHER,		/* ZAP */
11911ba4a712SPawel Jakub Dawidek 	/* new object types: */
11921ba4a712SPawel Jakub Dawidek 	DMU_OT_ERROR_LOG,		/* ZAP */
11931ba4a712SPawel Jakub Dawidek 	DMU_OT_SPA_HISTORY,		/* UINT8 */
11941ba4a712SPawel Jakub Dawidek 	DMU_OT_SPA_HISTORY_OFFSETS,	/* spa_his_phys_t */
11951ba4a712SPawel Jakub Dawidek 	DMU_OT_POOL_PROPS,		/* ZAP */
119610b9d77bSPawel Jakub Dawidek 	DMU_OT_DSL_PERMS,		/* ZAP */
119710b9d77bSPawel Jakub Dawidek 	DMU_OT_ACL,			/* ACL */
119810b9d77bSPawel Jakub Dawidek 	DMU_OT_SYSACL,			/* SYSACL */
119910b9d77bSPawel Jakub Dawidek 	DMU_OT_FUID,			/* FUID table (Packed NVLIST UINT8) */
120010b9d77bSPawel Jakub Dawidek 	DMU_OT_FUID_SIZE,		/* FUID table size UINT64 */
120110b9d77bSPawel Jakub Dawidek 	DMU_OT_NEXT_CLONES,		/* ZAP */
120210b9d77bSPawel Jakub Dawidek 	DMU_OT_SCAN_QUEUE,		/* ZAP */
120310b9d77bSPawel Jakub Dawidek 	DMU_OT_USERGROUP_USED,		/* ZAP */
120410b9d77bSPawel Jakub Dawidek 	DMU_OT_USERGROUP_QUOTA,		/* ZAP */
120510b9d77bSPawel Jakub Dawidek 	DMU_OT_USERREFS,		/* ZAP */
120610b9d77bSPawel Jakub Dawidek 	DMU_OT_DDT_ZAP,			/* ZAP */
120710b9d77bSPawel Jakub Dawidek 	DMU_OT_DDT_STATS,		/* ZAP */
120810b9d77bSPawel Jakub Dawidek 	DMU_OT_SA,			/* System attr */
120910b9d77bSPawel Jakub Dawidek 	DMU_OT_SA_MASTER_NODE,		/* ZAP */
121010b9d77bSPawel Jakub Dawidek 	DMU_OT_SA_ATTR_REGISTRATION,	/* ZAP */
121110b9d77bSPawel Jakub Dawidek 	DMU_OT_SA_ATTR_LAYOUTS,		/* ZAP */
121210b9d77bSPawel Jakub Dawidek 	DMU_OT_SCAN_XLATE,		/* ZAP */
121310b9d77bSPawel Jakub Dawidek 	DMU_OT_DEDUP,			/* fake dedup BP from ddt_bp_create() */
121477649f35SMark Johnston 	DMU_OT_DEADLIST,		/* ZAP */
121577649f35SMark Johnston 	DMU_OT_DEADLIST_HDR,		/* UINT64 */
121677649f35SMark Johnston 	DMU_OT_DSL_CLONES,		/* ZAP */
121777649f35SMark Johnston 	DMU_OT_BPOBJ_SUBOBJ,		/* UINT64 */
12184deb8929SAllan Jude 	DMU_OT_NUMTYPES,
12194deb8929SAllan Jude 
12204deb8929SAllan Jude 	/*
12214deb8929SAllan Jude 	 * Names for valid types declared with DMU_OT().
12224deb8929SAllan Jude 	 */
12234deb8929SAllan Jude 	DMU_OTN_UINT8_DATA = DMU_OT(DMU_BSWAP_UINT8, B_FALSE),
12244deb8929SAllan Jude 	DMU_OTN_UINT8_METADATA = DMU_OT(DMU_BSWAP_UINT8, B_TRUE),
12254deb8929SAllan Jude 	DMU_OTN_UINT16_DATA = DMU_OT(DMU_BSWAP_UINT16, B_FALSE),
12264deb8929SAllan Jude 	DMU_OTN_UINT16_METADATA = DMU_OT(DMU_BSWAP_UINT16, B_TRUE),
12274deb8929SAllan Jude 	DMU_OTN_UINT32_DATA = DMU_OT(DMU_BSWAP_UINT32, B_FALSE),
12284deb8929SAllan Jude 	DMU_OTN_UINT32_METADATA = DMU_OT(DMU_BSWAP_UINT32, B_TRUE),
12294deb8929SAllan Jude 	DMU_OTN_UINT64_DATA = DMU_OT(DMU_BSWAP_UINT64, B_FALSE),
12304deb8929SAllan Jude 	DMU_OTN_UINT64_METADATA = DMU_OT(DMU_BSWAP_UINT64, B_TRUE),
12314deb8929SAllan Jude 	DMU_OTN_ZAP_DATA = DMU_OT(DMU_BSWAP_ZAP, B_FALSE),
12324deb8929SAllan Jude 	DMU_OTN_ZAP_METADATA = DMU_OT(DMU_BSWAP_ZAP, B_TRUE)
12331ba4a712SPawel Jakub Dawidek } dmu_object_type_t;
12341ba4a712SPawel Jakub Dawidek 
12351ba4a712SPawel Jakub Dawidek typedef enum dmu_objset_type {
12361ba4a712SPawel Jakub Dawidek 	DMU_OST_NONE,
12371ba4a712SPawel Jakub Dawidek 	DMU_OST_META,
12381ba4a712SPawel Jakub Dawidek 	DMU_OST_ZFS,
12391ba4a712SPawel Jakub Dawidek 	DMU_OST_ZVOL,
12401ba4a712SPawel Jakub Dawidek 	DMU_OST_OTHER,			/* For testing only! */
12411ba4a712SPawel Jakub Dawidek 	DMU_OST_ANY,			/* Be careful! */
12421ba4a712SPawel Jakub Dawidek 	DMU_OST_NUMTYPES
12431ba4a712SPawel Jakub Dawidek } dmu_objset_type_t;
12441ba4a712SPawel Jakub Dawidek 
12453c2db0efSToomas Soome #define	ZAP_MAXVALUELEN	(1024 * 8)
12463c2db0efSToomas Soome 
12471ba4a712SPawel Jakub Dawidek /*
124810b9d77bSPawel Jakub Dawidek  * header for all bonus and spill buffers.
124910b9d77bSPawel Jakub Dawidek  * The header has a fixed portion with a variable number
125010b9d77bSPawel Jakub Dawidek  * of "lengths" depending on the number of variable sized
125110b9d77bSPawel Jakub Dawidek  * attribues which are determined by the "layout number"
125210b9d77bSPawel Jakub Dawidek  */
125310b9d77bSPawel Jakub Dawidek 
125410b9d77bSPawel Jakub Dawidek #define	SA_MAGIC	0x2F505A  /* ZFS SA */
125510b9d77bSPawel Jakub Dawidek typedef struct sa_hdr_phys {
125610b9d77bSPawel Jakub Dawidek 	uint32_t sa_magic;
125710b9d77bSPawel Jakub Dawidek 	uint16_t sa_layout_info;  /* Encoded with hdrsize and layout number */
125810b9d77bSPawel Jakub Dawidek 	uint16_t sa_lengths[1];	/* optional sizes for variable length attrs */
125910b9d77bSPawel Jakub Dawidek 	/* ... Data follows the lengths.  */
126010b9d77bSPawel Jakub Dawidek } sa_hdr_phys_t;
126110b9d77bSPawel Jakub Dawidek 
126210b9d77bSPawel Jakub Dawidek /*
126310b9d77bSPawel Jakub Dawidek  * sa_hdr_phys -> sa_layout_info
126410b9d77bSPawel Jakub Dawidek  *
126510b9d77bSPawel Jakub Dawidek  * 16      10       0
126610b9d77bSPawel Jakub Dawidek  * +--------+-------+
126710b9d77bSPawel Jakub Dawidek  * | hdrsz  |layout |
126810b9d77bSPawel Jakub Dawidek  * +--------+-------+
126910b9d77bSPawel Jakub Dawidek  *
127010b9d77bSPawel Jakub Dawidek  * Bits 0-10 are the layout number
127110b9d77bSPawel Jakub Dawidek  * Bits 11-16 are the size of the header.
127210b9d77bSPawel Jakub Dawidek  * The hdrsize is the number * 8
127310b9d77bSPawel Jakub Dawidek  *
127410b9d77bSPawel Jakub Dawidek  * For example.
127510b9d77bSPawel Jakub Dawidek  * hdrsz of 1 ==> 8 byte header
127610b9d77bSPawel Jakub Dawidek  *          2 ==> 16 byte header
127710b9d77bSPawel Jakub Dawidek  *
127810b9d77bSPawel Jakub Dawidek  */
127910b9d77bSPawel Jakub Dawidek 
128010b9d77bSPawel Jakub Dawidek #define	SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10)
128110b9d77bSPawel Jakub Dawidek #define	SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 16, 3, 0)
128210b9d77bSPawel Jakub Dawidek #define	SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \
128310b9d77bSPawel Jakub Dawidek { \
128410b9d77bSPawel Jakub Dawidek 	BF32_SET_SB(x, 10, 6, 3, 0, size); \
128510b9d77bSPawel Jakub Dawidek 	BF32_SET(x, 0, 10, num); \
128610b9d77bSPawel Jakub Dawidek }
128710b9d77bSPawel Jakub Dawidek 
128877649f35SMark Johnston #define	SA_ATTR_BSWAP(x)	BF32_GET(x, 16, 8)
128977649f35SMark Johnston #define	SA_ATTR_LENGTH(x)	BF32_GET(x, 24, 16)
129077649f35SMark Johnston #define	SA_ATTR_NUM(x)		BF32_GET(x, 0, 16)
129177649f35SMark Johnston #define	SA_ATTR_ENCODE(x, attr, length, bswap) \
129277649f35SMark Johnston { \
129377649f35SMark Johnston 	BF64_SET(x, 24, 16, length); \
129477649f35SMark Johnston 	BF64_SET(x, 16, 8, bswap); \
129577649f35SMark Johnston 	BF64_SET(x, 0, 16, attr); \
129677649f35SMark Johnston }
129777649f35SMark Johnston 
129810b9d77bSPawel Jakub Dawidek #define	SA_MODE_OFFSET		0
129910b9d77bSPawel Jakub Dawidek #define	SA_SIZE_OFFSET		8
130010b9d77bSPawel Jakub Dawidek #define	SA_GEN_OFFSET		16
130110b9d77bSPawel Jakub Dawidek #define	SA_UID_OFFSET		24
130210b9d77bSPawel Jakub Dawidek #define	SA_GID_OFFSET		32
130310b9d77bSPawel Jakub Dawidek #define	SA_PARENT_OFFSET	40
130484a6eddcSToomas Soome #define	SA_SYMLINK_OFFSET	160
130510b9d77bSPawel Jakub Dawidek 
130677649f35SMark Johnston #define	SA_REGISTRY	"REGISTRY"
130777649f35SMark Johnston #define	SA_LAYOUTS	"LAYOUTS"
130877649f35SMark Johnston 
130977649f35SMark Johnston typedef enum sa_bswap_type {
131077649f35SMark Johnston 	SA_UINT64_ARRAY,
131177649f35SMark Johnston 	SA_UINT32_ARRAY,
131277649f35SMark Johnston 	SA_UINT16_ARRAY,
131377649f35SMark Johnston 	SA_UINT8_ARRAY,
131477649f35SMark Johnston 	SA_ACL,
131577649f35SMark Johnston } sa_bswap_type_t;
131677649f35SMark Johnston 
131777649f35SMark Johnston typedef uint16_t	sa_attr_type_t;
131877649f35SMark Johnston 
131927e05a19SMatt Macy #define	ZIO_OBJSET_MAC_LEN		32
132027e05a19SMatt Macy 
132110b9d77bSPawel Jakub Dawidek /*
13221ba4a712SPawel Jakub Dawidek  * Intent log header - this on disk structure holds fields to manage
13231ba4a712SPawel Jakub Dawidek  * the log.  All fields are 64 bit to easily handle cross architectures.
13241ba4a712SPawel Jakub Dawidek  */
13251ba4a712SPawel Jakub Dawidek typedef struct zil_header {
13261ba4a712SPawel Jakub Dawidek 	uint64_t zh_claim_txg;	/* txg in which log blocks were claimed */
13271ba4a712SPawel Jakub Dawidek 	uint64_t zh_replay_seq;	/* highest replayed sequence number */
13281ba4a712SPawel Jakub Dawidek 	blkptr_t zh_log;	/* log chain */
13291ba4a712SPawel Jakub Dawidek 	uint64_t zh_claim_seq;	/* highest claimed sequence number */
13301ba4a712SPawel Jakub Dawidek 	uint64_t zh_pad[5];
13311ba4a712SPawel Jakub Dawidek } zil_header_t;
13321ba4a712SPawel Jakub Dawidek 
133327e05a19SMatt Macy #define	OBJSET_PHYS_SIZE_V2 2048
133427e05a19SMatt Macy #define	OBJSET_PHYS_SIZE_V3 4096
133510b9d77bSPawel Jakub Dawidek 
13361ba4a712SPawel Jakub Dawidek typedef struct objset_phys {
13371ba4a712SPawel Jakub Dawidek 	dnode_phys_t os_meta_dnode;
13381ba4a712SPawel Jakub Dawidek 	zil_header_t os_zil_header;
13391ba4a712SPawel Jakub Dawidek 	uint64_t os_type;
13408fc25799SMartin Matuska 	uint64_t os_flags;
134127e05a19SMatt Macy 	uint8_t os_portable_mac[ZIO_OBJSET_MAC_LEN];
134227e05a19SMatt Macy 	uint8_t os_local_mac[ZIO_OBJSET_MAC_LEN];
134327e05a19SMatt Macy 	char os_pad0[OBJSET_PHYS_SIZE_V2 - sizeof (dnode_phys_t)*3 -
134427e05a19SMatt Macy 		sizeof (zil_header_t) - sizeof (uint64_t)*2 -
134527e05a19SMatt Macy 		2*ZIO_OBJSET_MAC_LEN];
13468fc25799SMartin Matuska 	dnode_phys_t os_userused_dnode;
13478fc25799SMartin Matuska 	dnode_phys_t os_groupused_dnode;
134827e05a19SMatt Macy 	dnode_phys_t os_projectused_dnode;
134927e05a19SMatt Macy 	char os_pad1[OBJSET_PHYS_SIZE_V3 - OBJSET_PHYS_SIZE_V2 -
135027e05a19SMatt Macy 	    sizeof (dnode_phys_t)];
13511ba4a712SPawel Jakub Dawidek } objset_phys_t;
13521ba4a712SPawel Jakub Dawidek 
135377649f35SMark Johnston typedef struct space_map_phys {
135477649f35SMark Johnston 	/* object number: not needed but kept for backwards compatibility */
135577649f35SMark Johnston 	uint64_t	smp_object;
135677649f35SMark Johnston 
135777649f35SMark Johnston 	/* length of the object in bytes */
135877649f35SMark Johnston 	uint64_t	smp_length;
135977649f35SMark Johnston 
136077649f35SMark Johnston 	/* space allocated from the map */
136177649f35SMark Johnston 	int64_t		smp_alloc;
136277649f35SMark Johnston } space_map_phys_t;
136377649f35SMark Johnston 
136477649f35SMark Johnston typedef enum {
136577649f35SMark Johnston 	SM_ALLOC,
136677649f35SMark Johnston 	SM_FREE
136777649f35SMark Johnston } maptype_t;
136877649f35SMark Johnston 
136977649f35SMark Johnston /* one-word entry constants */
137077649f35SMark Johnston #define	SM_DEBUG_PREFIX	2
137177649f35SMark Johnston #define	SM_OFFSET_BITS	47
137277649f35SMark Johnston #define	SM_RUN_BITS	15
137377649f35SMark Johnston 
137477649f35SMark Johnston /* two-word entry constants */
137577649f35SMark Johnston #define	SM2_PREFIX	3
137677649f35SMark Johnston #define	SM2_OFFSET_BITS	63
137777649f35SMark Johnston #define	SM2_RUN_BITS	36
137877649f35SMark Johnston 
137977649f35SMark Johnston #define	SM_PREFIX_DECODE(x)	BF64_DECODE(x, 62, 2)
138077649f35SMark Johnston #define	SM_PREFIX_ENCODE(x)	BF64_ENCODE(x, 62, 2)
138177649f35SMark Johnston 
138277649f35SMark Johnston #define	SM_DEBUG_ACTION_DECODE(x)	BF64_DECODE(x, 60, 2)
138377649f35SMark Johnston #define	SM_DEBUG_ACTION_ENCODE(x)	BF64_ENCODE(x, 60, 2)
138477649f35SMark Johnston #define	SM_DEBUG_SYNCPASS_DECODE(x)	BF64_DECODE(x, 50, 10)
138577649f35SMark Johnston #define	SM_DEBUG_SYNCPASS_ENCODE(x)	BF64_ENCODE(x, 50, 10)
138677649f35SMark Johnston #define	SM_DEBUG_TXG_DECODE(x)		BF64_DECODE(x, 0, 50)
138777649f35SMark Johnston #define	SM_DEBUG_TXG_ENCODE(x)		BF64_ENCODE(x, 0, 50)
138877649f35SMark Johnston 
138977649f35SMark Johnston #define	SM_OFFSET_DECODE(x)	BF64_DECODE(x, 16, SM_OFFSET_BITS)
139077649f35SMark Johnston #define	SM_OFFSET_ENCODE(x)	BF64_ENCODE(x, 16, SM_OFFSET_BITS)
139177649f35SMark Johnston #define	SM_TYPE_DECODE(x)	BF64_DECODE(x, 15, 1)
139277649f35SMark Johnston #define	SM_TYPE_ENCODE(x)	BF64_ENCODE(x, 15, 1)
139377649f35SMark Johnston #define	SM_RUN_DECODE(x)	(BF64_DECODE(x, 0, SM_RUN_BITS) + 1)
139477649f35SMark Johnston #define	SM_RUN_ENCODE(x)	BF64_ENCODE((x) - 1, 0, SM_RUN_BITS)
139577649f35SMark Johnston #define	SM_RUN_MAX		SM_RUN_DECODE(~0ULL)
139677649f35SMark Johnston #define	SM_OFFSET_MAX		SM_OFFSET_DECODE(~0ULL)
139777649f35SMark Johnston 
139877649f35SMark Johnston #define	SM2_RUN_DECODE(x)	(BF64_DECODE(x, 24, SM2_RUN_BITS) + 1)
139977649f35SMark Johnston #define	SM2_RUN_ENCODE(x)	BF64_ENCODE((x) - 1, 24, SM2_RUN_BITS)
140077649f35SMark Johnston #define	SM2_VDEV_DECODE(x)	BF64_DECODE(x, 0, 24)
140177649f35SMark Johnston #define	SM2_VDEV_ENCODE(x)	BF64_ENCODE(x, 0, 24)
140277649f35SMark Johnston #define	SM2_TYPE_DECODE(x)	BF64_DECODE(x, SM2_OFFSET_BITS, 1)
140377649f35SMark Johnston #define	SM2_TYPE_ENCODE(x)	BF64_ENCODE(x, SM2_OFFSET_BITS, 1)
140477649f35SMark Johnston #define	SM2_OFFSET_DECODE(x)	BF64_DECODE(x, 0, SM2_OFFSET_BITS)
140577649f35SMark Johnston #define	SM2_OFFSET_ENCODE(x)	BF64_ENCODE(x, 0, SM2_OFFSET_BITS)
140677649f35SMark Johnston #define	SM2_RUN_MAX		SM2_RUN_DECODE(~0ULL)
140777649f35SMark Johnston #define	SM2_OFFSET_MAX		SM2_OFFSET_DECODE(~0ULL)
140877649f35SMark Johnston 
140931155389SMark Johnston typedef enum dd_used {
141031155389SMark Johnston 	DD_USED_HEAD,
141131155389SMark Johnston 	DD_USED_SNAP,
141231155389SMark Johnston 	DD_USED_CHILD,
141331155389SMark Johnston 	DD_USED_CHILD_RSRV,
141431155389SMark Johnston 	DD_USED_REFRSRV,
141531155389SMark Johnston 	DD_USED_NUM
141631155389SMark Johnston } dd_used_t;
141731155389SMark Johnston 
141831155389SMark Johnston #define	DD_FLAG_USED_BREAKDOWN (1 << 0)
141931155389SMark Johnston 
14201ba4a712SPawel Jakub Dawidek typedef struct dsl_dir_phys {
14211ba4a712SPawel Jakub Dawidek 	uint64_t dd_creation_time; /* not actually used */
14221ba4a712SPawel Jakub Dawidek 	uint64_t dd_head_dataset_obj;
14231ba4a712SPawel Jakub Dawidek 	uint64_t dd_parent_obj;
14241ba4a712SPawel Jakub Dawidek 	uint64_t dd_clone_parent_obj;
14251ba4a712SPawel Jakub Dawidek 	uint64_t dd_child_dir_zapobj;
14261ba4a712SPawel Jakub Dawidek 	/*
14271ba4a712SPawel Jakub Dawidek 	 * how much space our children are accounting for; for leaf
14281ba4a712SPawel Jakub Dawidek 	 * datasets, == physical space used by fs + snaps
14291ba4a712SPawel Jakub Dawidek 	 */
14301ba4a712SPawel Jakub Dawidek 	uint64_t dd_used_bytes;
14311ba4a712SPawel Jakub Dawidek 	uint64_t dd_compressed_bytes;
14321ba4a712SPawel Jakub Dawidek 	uint64_t dd_uncompressed_bytes;
14331ba4a712SPawel Jakub Dawidek 	/* Administrative quota setting */
14341ba4a712SPawel Jakub Dawidek 	uint64_t dd_quota;
14351ba4a712SPawel Jakub Dawidek 	/* Administrative reservation setting */
14361ba4a712SPawel Jakub Dawidek 	uint64_t dd_reserved;
14371ba4a712SPawel Jakub Dawidek 	uint64_t dd_props_zapobj;
143831155389SMark Johnston 	uint64_t dd_pad[1];
143931155389SMark Johnston 	uint64_t dd_flags;
144031155389SMark Johnston 	uint64_t dd_used_breakdown[DD_USED_NUM];
144177649f35SMark Johnston 	uint64_t dd_clones;
144277649f35SMark Johnston 	uint64_t dd_pad1[13]; /* pad out to 256 bytes for good measure */
14431ba4a712SPawel Jakub Dawidek } dsl_dir_phys_t;
14441ba4a712SPawel Jakub Dawidek 
14451ba4a712SPawel Jakub Dawidek typedef struct dsl_dataset_phys {
14461ba4a712SPawel Jakub Dawidek 	uint64_t ds_dir_obj;
14471ba4a712SPawel Jakub Dawidek 	uint64_t ds_prev_snap_obj;
14481ba4a712SPawel Jakub Dawidek 	uint64_t ds_prev_snap_txg;
14491ba4a712SPawel Jakub Dawidek 	uint64_t ds_next_snap_obj;
14501ba4a712SPawel Jakub Dawidek 	uint64_t ds_snapnames_zapobj;	/* zap obj of snaps; ==0 for snaps */
14511ba4a712SPawel Jakub Dawidek 	uint64_t ds_num_children;	/* clone/snap children; ==0 for head */
14521ba4a712SPawel Jakub Dawidek 	uint64_t ds_creation_time;	/* seconds since 1970 */
14531ba4a712SPawel Jakub Dawidek 	uint64_t ds_creation_txg;
14541ba4a712SPawel Jakub Dawidek 	uint64_t ds_deadlist_obj;
14551ba4a712SPawel Jakub Dawidek 	uint64_t ds_used_bytes;
14561ba4a712SPawel Jakub Dawidek 	uint64_t ds_compressed_bytes;
14571ba4a712SPawel Jakub Dawidek 	uint64_t ds_uncompressed_bytes;
14581ba4a712SPawel Jakub Dawidek 	uint64_t ds_unique_bytes;	/* only relevant to snapshots */
14591ba4a712SPawel Jakub Dawidek 	/*
14601ba4a712SPawel Jakub Dawidek 	 * The ds_fsid_guid is a 56-bit ID that can change to avoid
14611ba4a712SPawel Jakub Dawidek 	 * collisions.  The ds_guid is a 64-bit ID that will never
14621ba4a712SPawel Jakub Dawidek 	 * change, so there is a small probability that it will collide.
14631ba4a712SPawel Jakub Dawidek 	 */
14641ba4a712SPawel Jakub Dawidek 	uint64_t ds_fsid_guid;
14651ba4a712SPawel Jakub Dawidek 	uint64_t ds_guid;
14661ba4a712SPawel Jakub Dawidek 	uint64_t ds_flags;
14671ba4a712SPawel Jakub Dawidek 	blkptr_t ds_bp;
1468*2468e20dSMark Johnston 	uint64_t ds_next_clones_obj;	/* DMU_OT_DSL_CLONES */
1469*2468e20dSMark Johnston 	uint64_t ds_props_obj;		/* DMU_OT_DSL_PROPS for snaps */
1470*2468e20dSMark Johnston 	uint64_t ds_userrefs_obj;	/* DMU_OT_USERREFS */
1471*2468e20dSMark Johnston 	uint64_t ds_pad[5]; /* pad out to 320 bytes for good measure */
14721ba4a712SPawel Jakub Dawidek } dsl_dataset_phys_t;
14731ba4a712SPawel Jakub Dawidek 
147477649f35SMark Johnston typedef struct dsl_deadlist_phys {
147577649f35SMark Johnston 	uint64_t dl_used;
147677649f35SMark Johnston 	uint64_t dl_comp;
147777649f35SMark Johnston 	uint64_t dl_uncomp;
147877649f35SMark Johnston 	uint64_t dl_pad[37]; /* pad out to 320b for future expansion */
147977649f35SMark Johnston } dsl_deadlist_phys_t;
148077649f35SMark Johnston 
148177649f35SMark Johnston #define	BPOBJ_SIZE_V2	(6 * sizeof (uint64_t))
148277649f35SMark Johnston 
148377649f35SMark Johnston typedef struct bpobj_phys {
148477649f35SMark Johnston 	uint64_t	bpo_num_blkptrs;
148577649f35SMark Johnston 	uint64_t	bpo_bytes;
148677649f35SMark Johnston 	uint64_t	bpo_comp;
148777649f35SMark Johnston 	uint64_t	bpo_uncomp;
148877649f35SMark Johnston 	uint64_t	bpo_subobjs;
148977649f35SMark Johnston 	uint64_t	bpo_num_subobjs;
149077649f35SMark Johnston 	uint64_t	bpo_num_freed;
149177649f35SMark Johnston } bpobj_phys_t;
149277649f35SMark Johnston 
14931ba4a712SPawel Jakub Dawidek /*
14941ba4a712SPawel Jakub Dawidek  * The names of zap entries in the DIRECTORY_OBJECT of the MOS.
14951ba4a712SPawel Jakub Dawidek  */
14961ba4a712SPawel Jakub Dawidek #define	DMU_POOL_DIRECTORY_OBJECT	1
14971ba4a712SPawel Jakub Dawidek #define	DMU_POOL_CONFIG			"config"
14984deb8929SAllan Jude #define	DMU_POOL_FEATURES_FOR_READ	"features_for_read"
149977649f35SMark Johnston #define	DMU_POOL_FEATURES_FOR_WRITE	"features_for_write"
150077649f35SMark Johnston #define	DMU_POOL_FEATURE_DESCRIPTIONS	"feature_descriptions"
15011ba4a712SPawel Jakub Dawidek #define	DMU_POOL_ROOT_DATASET		"root_dataset"
15021ba4a712SPawel Jakub Dawidek #define	DMU_POOL_SYNC_BPLIST		"sync_bplist"
15031ba4a712SPawel Jakub Dawidek #define	DMU_POOL_ERRLOG_SCRUB		"errlog_scrub"
15041ba4a712SPawel Jakub Dawidek #define	DMU_POOL_ERRLOG_LAST		"errlog_last"
15051ba4a712SPawel Jakub Dawidek #define	DMU_POOL_SPARES			"spares"
15061ba4a712SPawel Jakub Dawidek #define	DMU_POOL_DEFLATE		"deflate"
15071ba4a712SPawel Jakub Dawidek #define	DMU_POOL_HISTORY		"history"
15081ba4a712SPawel Jakub Dawidek #define	DMU_POOL_PROPS			"pool_props"
150977649f35SMark Johnston #define	DMU_POOL_FREE_BPOBJ		"free_bpobj"
151077649f35SMark Johnston #define	DMU_POOL_BPTREE_OBJ		"bptree_obj"
151177649f35SMark Johnston #define	DMU_POOL_EMPTY_BPOBJ		"empty_bpobj"
151277649f35SMark Johnston #define	DMU_POOL_TMP_USERREFS		"tmp_userrefs"
15132c55d090SToomas Soome #define	DMU_POOL_CHECKSUM_SALT		"org.illumos:checksum_salt"
1514b1b93268SToomas Soome #define	DMU_POOL_REMOVING		"com.delphix:removing"
1515b1b93268SToomas Soome #define	DMU_POOL_OBSOLETE_BPOBJ		"com.delphix:obsolete_bpobj"
1516b1b93268SToomas Soome #define	DMU_POOL_CONDENSING_INDIRECT	"com.delphix:condensing_indirect"
1517277f38abSMariusz Zaborski #define	DMU_POOL_ZPOOL_CHECKPOINT       "com.delphix:zpool_checkpoint"
15181ba4a712SPawel Jakub Dawidek 
15191ba4a712SPawel Jakub Dawidek #define	ZAP_MAGIC 0x2F52AB2ABULL
15201ba4a712SPawel Jakub Dawidek 
15211ba4a712SPawel Jakub Dawidek #define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_block_shift)
15221ba4a712SPawel Jakub Dawidek 
15231ba4a712SPawel Jakub Dawidek #define	ZAP_MAXCD		(uint32_t)(-1)
15241ba4a712SPawel Jakub Dawidek #define	ZAP_HASHBITS		28
15251ba4a712SPawel Jakub Dawidek #define	MZAP_ENT_LEN		64
152677649f35SMark Johnston #define	MZAP_ENT_MAX		\
152777649f35SMark Johnston 	((MZAP_MAX_BLKSZ - sizeof(mzap_phys_t)) / sizeof(mzap_ent_phys_t) + 1)
15281ba4a712SPawel Jakub Dawidek #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
152977649f35SMark Johnston #define	MZAP_MAX_BLKSZ		SPA_OLDMAXBLOCKSIZE
15301ba4a712SPawel Jakub Dawidek 
15311ba4a712SPawel Jakub Dawidek typedef struct mzap_ent_phys {
15321ba4a712SPawel Jakub Dawidek 	uint64_t mze_value;
15331ba4a712SPawel Jakub Dawidek 	uint32_t mze_cd;
15341ba4a712SPawel Jakub Dawidek 	uint16_t mze_pad;	/* in case we want to chain them someday */
15351ba4a712SPawel Jakub Dawidek 	char mze_name[MZAP_NAME_LEN];
15361ba4a712SPawel Jakub Dawidek } mzap_ent_phys_t;
15371ba4a712SPawel Jakub Dawidek 
15381ba4a712SPawel Jakub Dawidek typedef struct mzap_phys {
15391ba4a712SPawel Jakub Dawidek 	uint64_t mz_block_type;	/* ZBT_MICRO */
15401ba4a712SPawel Jakub Dawidek 	uint64_t mz_salt;
15414d297e70SToomas Soome 	uint64_t mz_normflags;
15424d297e70SToomas Soome 	uint64_t mz_pad[5];
15431ba4a712SPawel Jakub Dawidek 	mzap_ent_phys_t mz_chunk[1];
15441ba4a712SPawel Jakub Dawidek 	/* actually variable size depending on block size */
15451ba4a712SPawel Jakub Dawidek } mzap_phys_t;
15461ba4a712SPawel Jakub Dawidek 
15471ba4a712SPawel Jakub Dawidek /*
15481ba4a712SPawel Jakub Dawidek  * The (fat) zap is stored in one object. It is an array of
15491ba4a712SPawel Jakub Dawidek  * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
15501ba4a712SPawel Jakub Dawidek  *
15511ba4a712SPawel Jakub Dawidek  * ptrtbl fits in first block:
15521ba4a712SPawel Jakub Dawidek  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
15531ba4a712SPawel Jakub Dawidek  *
15541ba4a712SPawel Jakub Dawidek  * ptrtbl too big for first block:
15551ba4a712SPawel Jakub Dawidek  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
15561ba4a712SPawel Jakub Dawidek  *
15571ba4a712SPawel Jakub Dawidek  */
15581ba4a712SPawel Jakub Dawidek 
15591ba4a712SPawel Jakub Dawidek #define	ZBT_LEAF		((1ULL << 63) + 0)
15601ba4a712SPawel Jakub Dawidek #define	ZBT_HEADER		((1ULL << 63) + 1)
15611ba4a712SPawel Jakub Dawidek #define	ZBT_MICRO		((1ULL << 63) + 3)
15621ba4a712SPawel Jakub Dawidek /* any other values are ptrtbl blocks */
15631ba4a712SPawel Jakub Dawidek 
15641ba4a712SPawel Jakub Dawidek /*
15651ba4a712SPawel Jakub Dawidek  * the embedded pointer table takes up half a block:
15661ba4a712SPawel Jakub Dawidek  * block size / entry size (2^3) / 2
15671ba4a712SPawel Jakub Dawidek  */
15681ba4a712SPawel Jakub Dawidek #define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
15691ba4a712SPawel Jakub Dawidek 
15701ba4a712SPawel Jakub Dawidek /*
15711ba4a712SPawel Jakub Dawidek  * The embedded pointer table starts half-way through the block.  Since
15721ba4a712SPawel Jakub Dawidek  * the pointer table itself is half the block, it starts at (64-bit)
15731ba4a712SPawel Jakub Dawidek  * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
15741ba4a712SPawel Jakub Dawidek  */
15751ba4a712SPawel Jakub Dawidek #define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
15761ba4a712SPawel Jakub Dawidek 	((uint64_t *)(zap)->zap_phys) \
15771ba4a712SPawel Jakub Dawidek 	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
15781ba4a712SPawel Jakub Dawidek 
157977649f35SMark Johnston #define	ZAP_HASH_IDX(hash, n)	(((n) == 0) ? 0 : ((hash) >> (64 - (n))))
158077649f35SMark Johnston 
15811ba4a712SPawel Jakub Dawidek /*
15821ba4a712SPawel Jakub Dawidek  * TAKE NOTE:
15831ba4a712SPawel Jakub Dawidek  * If zap_phys_t is modified, zap_byteswap() must be modified.
15841ba4a712SPawel Jakub Dawidek  */
15851ba4a712SPawel Jakub Dawidek typedef struct zap_phys {
15861ba4a712SPawel Jakub Dawidek 	uint64_t zap_block_type;	/* ZBT_HEADER */
15871ba4a712SPawel Jakub Dawidek 	uint64_t zap_magic;		/* ZAP_MAGIC */
15881ba4a712SPawel Jakub Dawidek 
15891ba4a712SPawel Jakub Dawidek 	struct zap_table_phys {
15901ba4a712SPawel Jakub Dawidek 		uint64_t zt_blk;	/* starting block number */
15911ba4a712SPawel Jakub Dawidek 		uint64_t zt_numblks;	/* number of blocks */
15921ba4a712SPawel Jakub Dawidek 		uint64_t zt_shift;	/* bits to index it */
15931ba4a712SPawel Jakub Dawidek 		uint64_t zt_nextblk;	/* next (larger) copy start block */
15941ba4a712SPawel Jakub Dawidek 		uint64_t zt_blks_copied; /* number source blocks copied */
15951ba4a712SPawel Jakub Dawidek 	} zap_ptrtbl;
15961ba4a712SPawel Jakub Dawidek 
15971ba4a712SPawel Jakub Dawidek 	uint64_t zap_freeblk;		/* the next free block */
15981ba4a712SPawel Jakub Dawidek 	uint64_t zap_num_leafs;		/* number of leafs */
15991ba4a712SPawel Jakub Dawidek 	uint64_t zap_num_entries;	/* number of entries */
16001ba4a712SPawel Jakub Dawidek 	uint64_t zap_salt;		/* salt to stir into hash function */
16014d297e70SToomas Soome 	uint64_t zap_normflags;		/* flags for u8_textprep_str() */
16024d297e70SToomas Soome 	uint64_t zap_flags;		/* zap_flags_t */
16031ba4a712SPawel Jakub Dawidek 	/*
16041ba4a712SPawel Jakub Dawidek 	 * This structure is followed by padding, and then the embedded
16051ba4a712SPawel Jakub Dawidek 	 * pointer table.  The embedded pointer table takes up second
16061ba4a712SPawel Jakub Dawidek 	 * half of the block.  It is accessed using the
16071ba4a712SPawel Jakub Dawidek 	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
16081ba4a712SPawel Jakub Dawidek 	 */
16091ba4a712SPawel Jakub Dawidek } zap_phys_t;
16101ba4a712SPawel Jakub Dawidek 
16111ba4a712SPawel Jakub Dawidek typedef struct zap_table_phys zap_table_phys_t;
16121ba4a712SPawel Jakub Dawidek 
16134d297e70SToomas Soome struct spa;
16141ba4a712SPawel Jakub Dawidek typedef struct fat_zap {
16151ba4a712SPawel Jakub Dawidek 	int zap_block_shift;			/* block size shift */
16161ba4a712SPawel Jakub Dawidek 	zap_phys_t *zap_phys;
16174d297e70SToomas Soome 	const struct spa *zap_spa;
16184d297e70SToomas Soome 	const dnode_phys_t *zap_dnode;
16191ba4a712SPawel Jakub Dawidek } fat_zap_t;
16201ba4a712SPawel Jakub Dawidek 
16211ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_MAGIC 0x2AB1EAF
16221ba4a712SPawel Jakub Dawidek 
16231ba4a712SPawel Jakub Dawidek /* chunk size = 24 bytes */
16241ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_CHUNKSIZE 24
16251ba4a712SPawel Jakub Dawidek 
16261ba4a712SPawel Jakub Dawidek /*
16271ba4a712SPawel Jakub Dawidek  * The amount of space available for chunks is:
16281ba4a712SPawel Jakub Dawidek  * block size (1<<l->l_bs) - hash entry size (2) * number of hash
16291ba4a712SPawel Jakub Dawidek  * entries - header space (2*chunksize)
16301ba4a712SPawel Jakub Dawidek  */
16311ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_NUMCHUNKS(l) \
16321ba4a712SPawel Jakub Dawidek 	(((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
16331ba4a712SPawel Jakub Dawidek 	ZAP_LEAF_CHUNKSIZE - 2)
16341ba4a712SPawel Jakub Dawidek 
16351ba4a712SPawel Jakub Dawidek /*
16361ba4a712SPawel Jakub Dawidek  * The amount of space within the chunk available for the array is:
16371ba4a712SPawel Jakub Dawidek  * chunk size - space for type (1) - space for next pointer (2)
16381ba4a712SPawel Jakub Dawidek  */
16391ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
16401ba4a712SPawel Jakub Dawidek 
16411ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
16421ba4a712SPawel Jakub Dawidek 	(((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
16431ba4a712SPawel Jakub Dawidek 
16441ba4a712SPawel Jakub Dawidek /*
16451ba4a712SPawel Jakub Dawidek  * Low water mark:  when there are only this many chunks free, start
16461ba4a712SPawel Jakub Dawidek  * growing the ptrtbl.  Ideally, this should be larger than a
16471ba4a712SPawel Jakub Dawidek  * "reasonably-sized" entry.  20 chunks is more than enough for the
16481ba4a712SPawel Jakub Dawidek  * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
16491ba4a712SPawel Jakub Dawidek  * while still being only around 3% for 16k blocks.
16501ba4a712SPawel Jakub Dawidek  */
16511ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_LOW_WATER (20)
16521ba4a712SPawel Jakub Dawidek 
16531ba4a712SPawel Jakub Dawidek /*
16541ba4a712SPawel Jakub Dawidek  * The leaf hash table has block size / 2^5 (32) number of entries,
16551ba4a712SPawel Jakub Dawidek  * which should be more than enough for the maximum number of entries,
16561ba4a712SPawel Jakub Dawidek  * which is less than block size / CHUNKSIZE (24) / minimum number of
16571ba4a712SPawel Jakub Dawidek  * chunks per entry (3).
16581ba4a712SPawel Jakub Dawidek  */
16591ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
16601ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
16611ba4a712SPawel Jakub Dawidek 
16621ba4a712SPawel Jakub Dawidek /*
16631ba4a712SPawel Jakub Dawidek  * The chunks start immediately after the hash table.  The end of the
16641ba4a712SPawel Jakub Dawidek  * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
16651ba4a712SPawel Jakub Dawidek  * chunk_t.
16661ba4a712SPawel Jakub Dawidek  */
16671ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_CHUNK(l, idx) \
166877649f35SMark Johnston 	((zap_leaf_chunk_t *)(void *) \
16691ba4a712SPawel Jakub Dawidek 	((l)->l_phys->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
16701ba4a712SPawel Jakub Dawidek #define	ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
16711ba4a712SPawel Jakub Dawidek 
167277649f35SMark Johnston #define	ZAP_LEAF_HASH(l, h) \
167377649f35SMark Johnston 	((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
167477649f35SMark Johnston 	((h) >> \
167577649f35SMark Johnston 	(64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len)))
167677649f35SMark Johnston #define	ZAP_LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[ZAP_LEAF_HASH(l, h)])
167777649f35SMark Johnston 
16781ba4a712SPawel Jakub Dawidek typedef enum zap_chunk_type {
16791ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_FREE = 253,
16801ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_ENTRY = 252,
16811ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_ARRAY = 251,
16821ba4a712SPawel Jakub Dawidek 	ZAP_CHUNK_TYPE_MAX = 250
16831ba4a712SPawel Jakub Dawidek } zap_chunk_type_t;
16841ba4a712SPawel Jakub Dawidek 
16851ba4a712SPawel Jakub Dawidek /*
16861ba4a712SPawel Jakub Dawidek  * TAKE NOTE:
16871ba4a712SPawel Jakub Dawidek  * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
16881ba4a712SPawel Jakub Dawidek  */
16891ba4a712SPawel Jakub Dawidek typedef struct zap_leaf_phys {
16901ba4a712SPawel Jakub Dawidek 	struct zap_leaf_header {
16911ba4a712SPawel Jakub Dawidek 		uint64_t lh_block_type;		/* ZBT_LEAF */
16921ba4a712SPawel Jakub Dawidek 		uint64_t lh_pad1;
16931ba4a712SPawel Jakub Dawidek 		uint64_t lh_prefix;		/* hash prefix of this leaf */
16941ba4a712SPawel Jakub Dawidek 		uint32_t lh_magic;		/* ZAP_LEAF_MAGIC */
16951ba4a712SPawel Jakub Dawidek 		uint16_t lh_nfree;		/* number free chunks */
16961ba4a712SPawel Jakub Dawidek 		uint16_t lh_nentries;		/* number of entries */
16971ba4a712SPawel Jakub Dawidek 		uint16_t lh_prefix_len;		/* num bits used to id this */
16981ba4a712SPawel Jakub Dawidek 
16991ba4a712SPawel Jakub Dawidek /* above is accessable to zap, below is zap_leaf private */
17001ba4a712SPawel Jakub Dawidek 
17011ba4a712SPawel Jakub Dawidek 		uint16_t lh_freelist;		/* chunk head of free list */
17021ba4a712SPawel Jakub Dawidek 		uint8_t lh_pad2[12];
17031ba4a712SPawel Jakub Dawidek 	} l_hdr; /* 2 24-byte chunks */
17041ba4a712SPawel Jakub Dawidek 
17051ba4a712SPawel Jakub Dawidek 	/*
17061ba4a712SPawel Jakub Dawidek 	 * The header is followed by a hash table with
17071ba4a712SPawel Jakub Dawidek 	 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries.  The hash table is
17081ba4a712SPawel Jakub Dawidek 	 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
17091ba4a712SPawel Jakub Dawidek 	 * zap_leaf_chunk structures.  These structures are accessed
17101ba4a712SPawel Jakub Dawidek 	 * with the ZAP_LEAF_CHUNK() macro.
17111ba4a712SPawel Jakub Dawidek 	 */
17121ba4a712SPawel Jakub Dawidek 
17131ba4a712SPawel Jakub Dawidek 	uint16_t l_hash[1];
17141ba4a712SPawel Jakub Dawidek } zap_leaf_phys_t;
17151ba4a712SPawel Jakub Dawidek 
17161ba4a712SPawel Jakub Dawidek typedef union zap_leaf_chunk {
17171ba4a712SPawel Jakub Dawidek 	struct zap_leaf_entry {
17181ba4a712SPawel Jakub Dawidek 		uint8_t le_type; 		/* always ZAP_CHUNK_ENTRY */
17194ed4f4cfSAndriy Gapon 		uint8_t le_value_intlen;	/* size of ints */
17201ba4a712SPawel Jakub Dawidek 		uint16_t le_next;		/* next entry in hash chain */
17211ba4a712SPawel Jakub Dawidek 		uint16_t le_name_chunk;		/* first chunk of the name */
17224ed4f4cfSAndriy Gapon 		uint16_t le_name_numints;	/* bytes in name, incl null */
17231ba4a712SPawel Jakub Dawidek 		uint16_t le_value_chunk;	/* first chunk of the value */
17244ed4f4cfSAndriy Gapon 		uint16_t le_value_numints;	/* value length in ints */
17251ba4a712SPawel Jakub Dawidek 		uint32_t le_cd;			/* collision differentiator */
17261ba4a712SPawel Jakub Dawidek 		uint64_t le_hash;		/* hash value of the name */
17271ba4a712SPawel Jakub Dawidek 	} l_entry;
17281ba4a712SPawel Jakub Dawidek 	struct zap_leaf_array {
17291ba4a712SPawel Jakub Dawidek 		uint8_t la_type;		/* always ZAP_CHUNK_ARRAY */
17301ba4a712SPawel Jakub Dawidek 		uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
17311ba4a712SPawel Jakub Dawidek 		uint16_t la_next;		/* next blk or CHAIN_END */
17321ba4a712SPawel Jakub Dawidek 	} l_array;
17331ba4a712SPawel Jakub Dawidek 	struct zap_leaf_free {
17341ba4a712SPawel Jakub Dawidek 		uint8_t lf_type;		/* always ZAP_CHUNK_FREE */
17351ba4a712SPawel Jakub Dawidek 		uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
17361ba4a712SPawel Jakub Dawidek 		uint16_t lf_next;	/* next in free list, or CHAIN_END */
17371ba4a712SPawel Jakub Dawidek 	} l_free;
17381ba4a712SPawel Jakub Dawidek } zap_leaf_chunk_t;
17391ba4a712SPawel Jakub Dawidek 
17401ba4a712SPawel Jakub Dawidek typedef struct zap_leaf {
17411ba4a712SPawel Jakub Dawidek 	int l_bs;			/* block size shift */
17421ba4a712SPawel Jakub Dawidek 	zap_leaf_phys_t *l_phys;
17431ba4a712SPawel Jakub Dawidek } zap_leaf_t;
17441ba4a712SPawel Jakub Dawidek 
174577649f35SMark Johnston #define	ZAP_MAXNAMELEN 256
174677649f35SMark Johnston #define	ZAP_MAXVALUELEN (1024 * 8)
174777649f35SMark Johnston 
174877649f35SMark Johnston #define	ACE_READ_DATA		0x00000001	/* file: read data */
174977649f35SMark Johnston #define	ACE_LIST_DIRECTORY	0x00000001	/* dir: list files */
175077649f35SMark Johnston #define	ACE_WRITE_DATA		0x00000002	/* file: write data */
175177649f35SMark Johnston #define	ACE_ADD_FILE		0x00000002	/* dir: create file */
175277649f35SMark Johnston #define	ACE_APPEND_DATA		0x00000004	/* file: append data */
175377649f35SMark Johnston #define	ACE_ADD_SUBDIRECTORY	0x00000004	/* dir: create subdir */
175477649f35SMark Johnston #define	ACE_READ_NAMED_ATTRS	0x00000008	/* FILE_READ_EA */
175577649f35SMark Johnston #define	ACE_WRITE_NAMED_ATTRS	0x00000010	/* FILE_WRITE_EA */
175677649f35SMark Johnston #define	ACE_EXECUTE		0x00000020	/* file: execute */
175777649f35SMark Johnston #define	ACE_TRAVERSE		0x00000020	/* dir: lookup name */
175877649f35SMark Johnston #define	ACE_DELETE_CHILD	0x00000040	/* dir: unlink child */
175977649f35SMark Johnston #define	ACE_READ_ATTRIBUTES	0x00000080	/* (all) stat, etc. */
176077649f35SMark Johnston #define	ACE_WRITE_ATTRIBUTES	0x00000100	/* (all) utimes, etc. */
176177649f35SMark Johnston #define	ACE_DELETE		0x00010000	/* (all) unlink self */
176277649f35SMark Johnston #define	ACE_READ_ACL		0x00020000	/* (all) getsecattr */
176377649f35SMark Johnston #define	ACE_WRITE_ACL		0x00040000	/* (all) setsecattr */
176477649f35SMark Johnston #define	ACE_WRITE_OWNER		0x00080000	/* (all) chown */
176577649f35SMark Johnston #define	ACE_SYNCHRONIZE		0x00100000	/* (all) */
176677649f35SMark Johnston 
176777649f35SMark Johnston #define	ACE_FILE_INHERIT_ACE		0x0001
176877649f35SMark Johnston #define	ACE_DIRECTORY_INHERIT_ACE	0x0002
176977649f35SMark Johnston #define	ACE_NO_PROPAGATE_INHERIT_ACE	0x0004
177077649f35SMark Johnston #define	ACE_INHERIT_ONLY_ACE		0x0008
177177649f35SMark Johnston #define	ACE_SUCCESSFUL_ACCESS_ACE_FLAG	0x0010
177277649f35SMark Johnston #define	ACE_FAILED_ACCESS_ACE_FLAG	0x0020
177377649f35SMark Johnston #define	ACE_IDENTIFIER_GROUP		0x0040
177477649f35SMark Johnston #define	ACE_INHERITED_ACE		0x0080
177577649f35SMark Johnston #define	ACE_OWNER			0x1000
177677649f35SMark Johnston #define	ACE_GROUP			0x2000
177777649f35SMark Johnston #define	ACE_EVERYONE			0x4000
177877649f35SMark Johnston 
177977649f35SMark Johnston #define	ACE_ACCESS_ALLOWED_ACE_TYPE	0x0000
178077649f35SMark Johnston #define	ACE_ACCESS_DENIED_ACE_TYPE	0x0001
178177649f35SMark Johnston #define	ACE_SYSTEM_AUDIT_ACE_TYPE	0x0002
178277649f35SMark Johnston #define	ACE_SYSTEM_ALARM_ACE_TYPE	0x0003
178377649f35SMark Johnston 
178477649f35SMark Johnston typedef struct zfs_ace_hdr {
178577649f35SMark Johnston 	uint16_t z_type;
178677649f35SMark Johnston 	uint16_t z_flags;
178777649f35SMark Johnston 	uint32_t z_access_mask;
178877649f35SMark Johnston } zfs_ace_hdr_t;
178977649f35SMark Johnston 
17901ba4a712SPawel Jakub Dawidek /*
17911ba4a712SPawel Jakub Dawidek  * Define special zfs pflags
17921ba4a712SPawel Jakub Dawidek  */
17931ba4a712SPawel Jakub Dawidek #define	ZFS_XATTR		0x1		/* is an extended attribute */
17941ba4a712SPawel Jakub Dawidek #define	ZFS_INHERIT_ACE		0x2		/* ace has inheritable ACEs */
17951ba4a712SPawel Jakub Dawidek #define	ZFS_ACL_TRIVIAL		0x4		/* files ACL is trivial */
179677649f35SMark Johnston #define	ZFS_ACL_OBJ_ACE		0x8		/* ACL has CMPLX Object ACE */
179777649f35SMark Johnston #define	ZFS_ACL_PROTECTED	0x10		/* ACL protected */
179877649f35SMark Johnston #define	ZFS_ACL_DEFAULTED	0x20		/* ACL should be defaulted */
179977649f35SMark Johnston #define	ZFS_ACL_AUTO_INHERIT	0x40		/* ACL should be inherited */
180077649f35SMark Johnston #define	ZFS_BONUS_SCANSTAMP	0x80		/* Scanstamp in bonus area */
180177649f35SMark Johnston #define	ZFS_NO_EXECS_DENIED	0x100		/* exec was given to everyone */
180277649f35SMark Johnston 
180377649f35SMark Johnston #define	ZFS_READONLY		0x0000000100000000ull
180477649f35SMark Johnston #define	ZFS_HIDDEN		0x0000000200000000ull
180577649f35SMark Johnston #define	ZFS_SYSTEM		0x0000000400000000ull
180677649f35SMark Johnston #define	ZFS_ARCHIVE		0x0000000800000000ull
180777649f35SMark Johnston #define	ZFS_IMMUTABLE		0x0000001000000000ull
180877649f35SMark Johnston #define	ZFS_NOUNLINK		0x0000002000000000ull
180977649f35SMark Johnston #define	ZFS_APPENDONLY		0x0000004000000000ull
181077649f35SMark Johnston #define	ZFS_NODUMP		0x0000008000000000ull
181177649f35SMark Johnston #define	ZFS_OPAQUE		0x0000010000000000ull
181277649f35SMark Johnston #define	ZFS_AV_QUARANTINED	0x0000020000000000ull
181377649f35SMark Johnston #define	ZFS_AV_MODIFIED		0x0000040000000000ull
181477649f35SMark Johnston #define	ZFS_REPARSE		0x0000080000000000ull
181577649f35SMark Johnston #define	ZFS_OFFLINE		0x0000100000000000ull
181677649f35SMark Johnston #define	ZFS_SPARSE		0x0000200000000000ull
18171ba4a712SPawel Jakub Dawidek 
18181ba4a712SPawel Jakub Dawidek #define	MASTER_NODE_OBJ	1
18191ba4a712SPawel Jakub Dawidek 
18201ba4a712SPawel Jakub Dawidek /*
18211ba4a712SPawel Jakub Dawidek  * special attributes for master node.
18221ba4a712SPawel Jakub Dawidek  */
18231ba4a712SPawel Jakub Dawidek 
18241ba4a712SPawel Jakub Dawidek #define	ZFS_FSID		"FSID"
18251ba4a712SPawel Jakub Dawidek #define	ZFS_UNLINKED_SET	"DELETE_QUEUE"
18261ba4a712SPawel Jakub Dawidek #define	ZFS_ROOT_OBJ		"ROOT"
18271ba4a712SPawel Jakub Dawidek #define	ZPL_VERSION_OBJ		"VERSION"
18281ba4a712SPawel Jakub Dawidek #define	ZFS_PROP_BLOCKPERPAGE	"BLOCKPERPAGE"
18291ba4a712SPawel Jakub Dawidek #define	ZFS_PROP_NOGROWBLOCKS	"NOGROWBLOCKS"
183077649f35SMark Johnston #define	ZFS_SA_ATTRS		"SA_ATTRS"
18311ba4a712SPawel Jakub Dawidek 
18321ba4a712SPawel Jakub Dawidek #define	ZFS_FLAG_BLOCKPERPAGE	0x1
18331ba4a712SPawel Jakub Dawidek #define	ZFS_FLAG_NOGROWBLOCKS	0x2
18341ba4a712SPawel Jakub Dawidek 
18351ba4a712SPawel Jakub Dawidek /*
18361ba4a712SPawel Jakub Dawidek  * ZPL version - rev'd whenever an incompatible on-disk format change
18371ba4a712SPawel Jakub Dawidek  * occurs.  Independent of SPA/DMU/ZAP versioning.
18381ba4a712SPawel Jakub Dawidek  */
18391ba4a712SPawel Jakub Dawidek 
18401ba4a712SPawel Jakub Dawidek #define	ZPL_VERSION		1ULL
18411ba4a712SPawel Jakub Dawidek 
18421ba4a712SPawel Jakub Dawidek /*
18431ba4a712SPawel Jakub Dawidek  * The directory entry has the type (currently unused on Solaris) in the
18441ba4a712SPawel Jakub Dawidek  * top 4 bits, and the object number in the low 48 bits.  The "middle"
18451ba4a712SPawel Jakub Dawidek  * 12 bits are unused.
18461ba4a712SPawel Jakub Dawidek  */
18471ba4a712SPawel Jakub Dawidek #define	ZFS_DIRENT_TYPE(de) BF64_GET(de, 60, 4)
18481ba4a712SPawel Jakub Dawidek #define	ZFS_DIRENT_OBJ(de) BF64_GET(de, 0, 48)
18491ba4a712SPawel Jakub Dawidek #define	ZFS_DIRENT_MAKE(type, obj) (((uint64_t)type << 60) | obj)
18501ba4a712SPawel Jakub Dawidek 
18511ba4a712SPawel Jakub Dawidek typedef struct ace {
18521ba4a712SPawel Jakub Dawidek 	uid_t		a_who;		/* uid or gid */
18531ba4a712SPawel Jakub Dawidek 	uint32_t	a_access_mask;	/* read,write,... */
18541ba4a712SPawel Jakub Dawidek 	uint16_t	a_flags;	/* see below */
18551ba4a712SPawel Jakub Dawidek 	uint16_t	a_type;		/* allow or deny */
18561ba4a712SPawel Jakub Dawidek } ace_t;
18571ba4a712SPawel Jakub Dawidek 
18581ba4a712SPawel Jakub Dawidek #define ACE_SLOT_CNT	6
18591ba4a712SPawel Jakub Dawidek 
18601ba4a712SPawel Jakub Dawidek typedef struct zfs_znode_acl {
18611ba4a712SPawel Jakub Dawidek 	uint64_t	z_acl_extern_obj;	  /* ext acl pieces */
18621ba4a712SPawel Jakub Dawidek 	uint32_t	z_acl_count;		  /* Number of ACEs */
18631ba4a712SPawel Jakub Dawidek 	uint16_t	z_acl_version;		  /* acl version */
18641ba4a712SPawel Jakub Dawidek 	uint16_t	z_acl_pad;		  /* pad */
18651ba4a712SPawel Jakub Dawidek 	ace_t		z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */
18661ba4a712SPawel Jakub Dawidek } zfs_znode_acl_t;
18671ba4a712SPawel Jakub Dawidek 
18681ba4a712SPawel Jakub Dawidek /*
18691ba4a712SPawel Jakub Dawidek  * This is the persistent portion of the znode.  It is stored
18701ba4a712SPawel Jakub Dawidek  * in the "bonus buffer" of the file.  Short symbolic links
18711ba4a712SPawel Jakub Dawidek  * are also stored in the bonus buffer.
18721ba4a712SPawel Jakub Dawidek  */
18731ba4a712SPawel Jakub Dawidek typedef struct znode_phys {
18741ba4a712SPawel Jakub Dawidek 	uint64_t zp_atime[2];		/*  0 - last file access time */
18751ba4a712SPawel Jakub Dawidek 	uint64_t zp_mtime[2];		/* 16 - last file modification time */
18761ba4a712SPawel Jakub Dawidek 	uint64_t zp_ctime[2];		/* 32 - last file change time */
18771ba4a712SPawel Jakub Dawidek 	uint64_t zp_crtime[2];		/* 48 - creation time */
18781ba4a712SPawel Jakub Dawidek 	uint64_t zp_gen;		/* 64 - generation (txg of creation) */
18791ba4a712SPawel Jakub Dawidek 	uint64_t zp_mode;		/* 72 - file mode bits */
18801ba4a712SPawel Jakub Dawidek 	uint64_t zp_size;		/* 80 - size of file */
18811ba4a712SPawel Jakub Dawidek 	uint64_t zp_parent;		/* 88 - directory parent (`..') */
18821ba4a712SPawel Jakub Dawidek 	uint64_t zp_links;		/* 96 - number of links to file */
18831ba4a712SPawel Jakub Dawidek 	uint64_t zp_xattr;		/* 104 - DMU object for xattrs */
18841ba4a712SPawel Jakub Dawidek 	uint64_t zp_rdev;		/* 112 - dev_t for VBLK & VCHR files */
18851ba4a712SPawel Jakub Dawidek 	uint64_t zp_flags;		/* 120 - persistent flags */
18861ba4a712SPawel Jakub Dawidek 	uint64_t zp_uid;		/* 128 - file owner */
18871ba4a712SPawel Jakub Dawidek 	uint64_t zp_gid;		/* 136 - owning group */
18881ba4a712SPawel Jakub Dawidek 	uint64_t zp_pad[4];		/* 144 - future */
18891ba4a712SPawel Jakub Dawidek 	zfs_znode_acl_t zp_acl;		/* 176 - 263 ACL */
18901ba4a712SPawel Jakub Dawidek 	/*
18911ba4a712SPawel Jakub Dawidek 	 * Data may pad out any remaining bytes in the znode buffer, eg:
18921ba4a712SPawel Jakub Dawidek 	 *
18931ba4a712SPawel Jakub Dawidek 	 * |<---------------------- dnode_phys (512) ------------------------>|
18941ba4a712SPawel Jakub Dawidek 	 * |<-- dnode (192) --->|<----------- "bonus" buffer (320) ---------->|
18951ba4a712SPawel Jakub Dawidek 	 *			|<---- znode (264) ---->|<---- data (56) ---->|
18961ba4a712SPawel Jakub Dawidek 	 *
18971ba4a712SPawel Jakub Dawidek 	 * At present, we only use this space to store symbolic links.
18981ba4a712SPawel Jakub Dawidek 	 */
18991ba4a712SPawel Jakub Dawidek } znode_phys_t;
19001ba4a712SPawel Jakub Dawidek 
19011ba4a712SPawel Jakub Dawidek /*
19021ba4a712SPawel Jakub Dawidek  * In-core vdev representation.
19031ba4a712SPawel Jakub Dawidek  */
19041ba4a712SPawel Jakub Dawidek struct vdev;
19052c55d090SToomas Soome struct spa;
1906e307eb94SToomas Soome typedef int vdev_phys_read_t(struct vdev *, void *, off_t, void *, size_t);
1907e307eb94SToomas Soome typedef int vdev_phys_write_t(struct vdev *, off_t, void *, size_t);
1908e307eb94SToomas Soome typedef int vdev_read_t(struct vdev *, const blkptr_t *, void *, off_t, size_t);
19091ba4a712SPawel Jakub Dawidek 
19101ba4a712SPawel Jakub Dawidek typedef STAILQ_HEAD(vdev_list, vdev) vdev_list_t;
19111ba4a712SPawel Jakub Dawidek 
1912b1b93268SToomas Soome typedef struct vdev_indirect_mapping_entry_phys {
1913b1b93268SToomas Soome 	/*
1914b1b93268SToomas Soome 	 * Decode with DVA_MAPPING_* macros.
1915b1b93268SToomas Soome 	 * Contains:
1916b1b93268SToomas Soome 	 *   the source offset (low 63 bits)
1917b1b93268SToomas Soome 	 *   the one-bit "mark", used for garbage collection (by zdb)
1918b1b93268SToomas Soome 	 */
1919b1b93268SToomas Soome 	uint64_t vimep_src;
1920b1b93268SToomas Soome 
1921b1b93268SToomas Soome 	/*
1922b1b93268SToomas Soome 	 * Note: the DVA's asize is 24 bits, and can thus store ranges
1923b1b93268SToomas Soome 	 * up to 8GB.
1924b1b93268SToomas Soome 	 */
1925b1b93268SToomas Soome 	dva_t	vimep_dst;
1926b1b93268SToomas Soome } vdev_indirect_mapping_entry_phys_t;
1927b1b93268SToomas Soome 
1928b1b93268SToomas Soome #define	DVA_MAPPING_GET_SRC_OFFSET(vimep)	\
1929b1b93268SToomas Soome 	BF64_GET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0)
1930b1b93268SToomas Soome #define	DVA_MAPPING_SET_SRC_OFFSET(vimep, x)	\
1931b1b93268SToomas Soome 	BF64_SET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0, x)
1932b1b93268SToomas Soome 
1933b1b93268SToomas Soome /*
1934b1b93268SToomas Soome  * This is stored in the bonus buffer of the mapping object, see comment of
1935b1b93268SToomas Soome  * vdev_indirect_config for more details.
1936b1b93268SToomas Soome  */
1937b1b93268SToomas Soome typedef struct vdev_indirect_mapping_phys {
1938b1b93268SToomas Soome 	uint64_t	vimp_max_offset;
1939b1b93268SToomas Soome 	uint64_t	vimp_bytes_mapped;
1940b1b93268SToomas Soome 	uint64_t	vimp_num_entries; /* number of v_i_m_entry_phys_t's */
1941b1b93268SToomas Soome 
1942b1b93268SToomas Soome 	/*
1943b1b93268SToomas Soome 	 * For each entry in the mapping object, this object contains an
1944b1b93268SToomas Soome 	 * entry representing the number of bytes of that mapping entry
1945b1b93268SToomas Soome 	 * that were no longer in use by the pool at the time this indirect
1946b1b93268SToomas Soome 	 * vdev was last condensed.
1947b1b93268SToomas Soome 	 */
1948b1b93268SToomas Soome 	uint64_t	vimp_counts_object;
1949b1b93268SToomas Soome } vdev_indirect_mapping_phys_t;
1950b1b93268SToomas Soome 
1951b1b93268SToomas Soome #define	VDEV_INDIRECT_MAPPING_SIZE_V0	(3 * sizeof (uint64_t))
1952b1b93268SToomas Soome 
1953b1b93268SToomas Soome typedef struct vdev_indirect_mapping {
1954b1b93268SToomas Soome 	uint64_t	vim_object;
1955b1b93268SToomas Soome 	boolean_t	vim_havecounts;
1956b1b93268SToomas Soome 
1957b1b93268SToomas Soome 	/* vim_entries segment offset currently in memory. */
1958b1b93268SToomas Soome 	uint64_t	vim_entry_offset;
1959b1b93268SToomas Soome 	/* vim_entries segment size. */
1960b1b93268SToomas Soome 	size_t		vim_num_entries;
1961b1b93268SToomas Soome 
1962b1b93268SToomas Soome 	/* Needed by dnode_read() */
1963b1b93268SToomas Soome 	const void	*vim_spa;
1964b1b93268SToomas Soome 	dnode_phys_t	*vim_dn;
1965b1b93268SToomas Soome 
1966b1b93268SToomas Soome 	/*
1967b1b93268SToomas Soome 	 * An ordered array of mapping entries, sorted by source offset.
1968b1b93268SToomas Soome 	 * Note that vim_entries is needed during a removal (and contains
1969b1b93268SToomas Soome 	 * mappings that have been synced to disk so far) to handle frees
1970b1b93268SToomas Soome 	 * from the removing device.
1971b1b93268SToomas Soome 	 */
1972b1b93268SToomas Soome 	vdev_indirect_mapping_entry_phys_t *vim_entries;
1973b1b93268SToomas Soome 	objset_phys_t	*vim_objset;
1974b1b93268SToomas Soome 	vdev_indirect_mapping_phys_t	*vim_phys;
1975b1b93268SToomas Soome } vdev_indirect_mapping_t;
1976b1b93268SToomas Soome 
1977b1b93268SToomas Soome /*
1978b1b93268SToomas Soome  * On-disk indirect vdev state.
1979b1b93268SToomas Soome  *
1980b1b93268SToomas Soome  * An indirect vdev is described exclusively in the MOS config of a pool.
1981b1b93268SToomas Soome  * The config for an indirect vdev includes several fields, which are
1982b1b93268SToomas Soome  * accessed in memory by a vdev_indirect_config_t.
1983b1b93268SToomas Soome  */
1984b1b93268SToomas Soome typedef struct vdev_indirect_config {
1985b1b93268SToomas Soome 	/*
1986b1b93268SToomas Soome 	 * Object (in MOS) which contains the indirect mapping. This object
1987b1b93268SToomas Soome 	 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by
1988b1b93268SToomas Soome 	 * vimep_src. The bonus buffer for this object is a
1989b1b93268SToomas Soome 	 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev
1990b1b93268SToomas Soome 	 * removal is initiated.
1991b1b93268SToomas Soome 	 *
1992b1b93268SToomas Soome 	 * Note that this object can be empty if none of the data on the vdev
1993b1b93268SToomas Soome 	 * has been copied yet.
1994b1b93268SToomas Soome 	 */
1995b1b93268SToomas Soome 	uint64_t	vic_mapping_object;
1996b1b93268SToomas Soome 
1997b1b93268SToomas Soome 	/*
1998b1b93268SToomas Soome 	 * Object (in MOS) which contains the birth times for the mapping
1999b1b93268SToomas Soome 	 * entries. This object contains an array of
2000b1b93268SToomas Soome 	 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus
2001b1b93268SToomas Soome 	 * buffer for this object is a vdev_indirect_birth_phys_t. This object
2002b1b93268SToomas Soome 	 * is allocated when a vdev removal is initiated.
2003b1b93268SToomas Soome 	 *
2004b1b93268SToomas Soome 	 * Note that this object can be empty if none of the vdev has yet been
2005b1b93268SToomas Soome 	 * copied.
2006b1b93268SToomas Soome 	 */
2007b1b93268SToomas Soome 	uint64_t	vic_births_object;
2008b1b93268SToomas Soome 
2009b1b93268SToomas Soome /*
2010b1b93268SToomas Soome  * This is the vdev ID which was removed previous to this vdev, or
2011b1b93268SToomas Soome  * UINT64_MAX if there are no previously removed vdevs.
2012b1b93268SToomas Soome  */
2013b1b93268SToomas Soome 	uint64_t	vic_prev_indirect_vdev;
2014b1b93268SToomas Soome } vdev_indirect_config_t;
2015b1b93268SToomas Soome 
20161ba4a712SPawel Jakub Dawidek typedef struct vdev {
20171ba4a712SPawel Jakub Dawidek 	STAILQ_ENTRY(vdev) v_childlink;	/* link in parent's child list */
20181ba4a712SPawel Jakub Dawidek 	STAILQ_ENTRY(vdev) v_alllink;	/* link in global vdev list */
20191ba4a712SPawel Jakub Dawidek 	vdev_list_t	v_children;	/* children of this vdev */
202010b9d77bSPawel Jakub Dawidek 	const char	*v_name;	/* vdev name */
20211ba4a712SPawel Jakub Dawidek 	uint64_t	v_guid;		/* vdev guid */
2022abca0bd5SToomas Soome 	uint64_t	v_id;		/* index in parent */
2023abca0bd5SToomas Soome 	uint64_t	v_psize;	/* physical device capacity */
2024e1899ef6SDoug Rabson 	int		v_ashift;	/* offset to block shift */
2025e1899ef6SDoug Rabson 	int		v_nparity;	/* # parity for raidz */
202610b9d77bSPawel Jakub Dawidek 	struct vdev	*v_top;		/* parent vdev */
20273c2db0efSToomas Soome 	size_t		v_nchildren;	/* # children */
20281ba4a712SPawel Jakub Dawidek 	vdev_state_t	v_state;	/* current state */
2029e1899ef6SDoug Rabson 	vdev_phys_read_t *v_phys_read;	/* read from raw leaf vdev */
2030e307eb94SToomas Soome 	vdev_phys_write_t *v_phys_write; /* write to raw leaf vdev */
2031e1899ef6SDoug Rabson 	vdev_read_t	*v_read;	/* read from vdev */
2032e307eb94SToomas Soome 	void		*v_priv;	/* data for read/write function */
20330c0a882cSToomas Soome 	boolean_t	v_islog;
20343c2db0efSToomas Soome 	struct spa	*v_spa;		/* link to spa */
2035b1b93268SToomas Soome 	/*
2036b1b93268SToomas Soome 	 * Values stored in the config for an indirect or removing vdev.
2037b1b93268SToomas Soome 	 */
2038b1b93268SToomas Soome 	vdev_indirect_config_t vdev_indirect_config;
2039b1b93268SToomas Soome 	vdev_indirect_mapping_t *v_mapping;
20401ba4a712SPawel Jakub Dawidek } vdev_t;
20411ba4a712SPawel Jakub Dawidek 
20421ba4a712SPawel Jakub Dawidek /*
20431ba4a712SPawel Jakub Dawidek  * In-core pool representation.
20441ba4a712SPawel Jakub Dawidek  */
20451ba4a712SPawel Jakub Dawidek typedef STAILQ_HEAD(spa_list, spa) spa_list_t;
20461ba4a712SPawel Jakub Dawidek 
20471ba4a712SPawel Jakub Dawidek typedef struct spa {
20481ba4a712SPawel Jakub Dawidek 	STAILQ_ENTRY(spa) spa_link;	/* link in global pool list */
20491ba4a712SPawel Jakub Dawidek 	char		*spa_name;	/* pool name */
20501ba4a712SPawel Jakub Dawidek 	uint64_t	spa_guid;	/* pool guid */
20511ba4a712SPawel Jakub Dawidek 	uint64_t	spa_txg;	/* most recent transaction */
2052277f38abSMariusz Zaborski 	struct uberblock *spa_uberblock;	/* best uberblock so far */
20533c2db0efSToomas Soome 	vdev_t		*spa_root_vdev;	/* toplevel vdev container */
2054277f38abSMariusz Zaborski 	objset_phys_t	*spa_mos;	/* MOS for this pool */
20552c55d090SToomas Soome 	zio_cksum_salt_t spa_cksum_salt;	/* secret salt for cksum */
20562c55d090SToomas Soome 	void		*spa_cksum_tmpls[ZIO_CHECKSUM_FUNCTIONS];
20570c0a882cSToomas Soome 	boolean_t	spa_with_log;	/* this pool has log */
2058277f38abSMariusz Zaborski 
2059277f38abSMariusz Zaborski 	struct uberblock spa_uberblock_master;	/* best uberblock so far */
2060277f38abSMariusz Zaborski 	objset_phys_t	spa_mos_master;		/* MOS for this pool */
2061277f38abSMariusz Zaborski 	struct uberblock spa_uberblock_checkpoint; /* checkpoint uberblock */
2062277f38abSMariusz Zaborski 	objset_phys_t	spa_mos_checkpoint;	/* Checkpoint MOS */
2063e307eb94SToomas Soome 	void		*spa_bootenv;		/* bootenv from pool label */
20641ba4a712SPawel Jakub Dawidek } spa_t;
206529441ba3SXin LI 
2066b1b93268SToomas Soome /* IO related arguments. */
2067b1b93268SToomas Soome typedef struct zio {
2068b1b93268SToomas Soome 	spa_t		*io_spa;
2069b1b93268SToomas Soome 	blkptr_t	*io_bp;
2070b1b93268SToomas Soome 	void		*io_data;
2071b1b93268SToomas Soome 	uint64_t	io_size;
2072b1b93268SToomas Soome 	uint64_t	io_offset;
2073b1b93268SToomas Soome 
2074b1b93268SToomas Soome 	/* Stuff for the vdev stack */
2075b1b93268SToomas Soome 	vdev_t		*io_vd;
2076b1b93268SToomas Soome 	void		*io_vsd;
2077b1b93268SToomas Soome 
2078b1b93268SToomas Soome 	int		io_error;
2079b1b93268SToomas Soome } zio_t;
2080b1b93268SToomas Soome 
208177649f35SMark Johnston extern void decode_embedded_bp_compressed(const blkptr_t *, void *);
20823830659eSToomas Soome 
20833830659eSToomas Soome #endif /* _ZFSIMPL_H_ */
2084