Home | History | Annotate | Download | only in sys
      1    789    ahrens /*
      2    789    ahrens  * CDDL HEADER START
      3    789    ahrens  *
      4    789    ahrens  * The contents of this file are subject to the terms of the
      5   1544  eschrock  * Common Development and Distribution License (the "License").
      6   1544  eschrock  * You may not use this file except in compliance with the License.
      7    789    ahrens  *
      8    789    ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9    789    ahrens  * or http://www.opensolaris.org/os/licensing.
     10    789    ahrens  * See the License for the specific language governing permissions
     11    789    ahrens  * and limitations under the License.
     12    789    ahrens  *
     13    789    ahrens  * When distributing Covered Code, include this CDDL HEADER in each
     14    789    ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15    789    ahrens  * If applicable, add the following below this CDDL HEADER, with the
     16    789    ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
     17    789    ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
     18    789    ahrens  *
     19    789    ahrens  * CDDL HEADER END
     20    789    ahrens  */
     21    789    ahrens /*
     22   8525      Eric  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23    789    ahrens  * Use is subject to license terms.
     24    789    ahrens  */
     25    789    ahrens 
     26    789    ahrens #ifndef _SYS_SPA_H
     27    789    ahrens #define	_SYS_SPA_H
     28    789    ahrens 
     29    789    ahrens #include <sys/avl.h>
     30    789    ahrens #include <sys/zfs_context.h>
     31    789    ahrens #include <sys/nvpair.h>
     32    789    ahrens #include <sys/sysmacros.h>
     33    789    ahrens #include <sys/types.h>
     34    789    ahrens #include <sys/fs/zfs.h>
     35    789    ahrens 
     36    789    ahrens #ifdef	__cplusplus
     37    789    ahrens extern "C" {
     38    789    ahrens #endif
     39    789    ahrens 
     40    789    ahrens /*
     41    789    ahrens  * Forward references that lots of things need.
     42    789    ahrens  */
     43    789    ahrens typedef struct spa spa_t;
     44    789    ahrens typedef struct vdev vdev_t;
     45    789    ahrens typedef struct metaslab metaslab_t;
     46  10922      Jeff typedef struct metaslab_group metaslab_group_t;
     47  10922      Jeff typedef struct metaslab_class metaslab_class_t;
     48  10922      Jeff typedef struct zio zio_t;
     49    789    ahrens typedef struct zilog zilog_t;
     50   5450   brendan typedef struct spa_aux_vdev spa_aux_vdev_t;
     51  10922      Jeff typedef struct ddt ddt_t;
     52  10922      Jeff typedef struct ddt_entry ddt_entry_t;
     53    789    ahrens struct dsl_pool;
     54    789    ahrens 
     55    789    ahrens /*
     56    789    ahrens  * General-purpose 32-bit and 64-bit bitfield encodings.
     57    789    ahrens  */
     58    789    ahrens #define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
     59    789    ahrens #define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
     60    789    ahrens #define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
     61    789    ahrens #define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
     62    789    ahrens 
     63    789    ahrens #define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
     64    789    ahrens #define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
     65    789    ahrens 
     66    789    ahrens #define	BF32_SET(x, low, len, val)	\
     67   2856  nd150628 	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
     68    789    ahrens #define	BF64_SET(x, low, len, val)	\
     69   2856  nd150628 	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
     70    789    ahrens 
     71    789    ahrens #define	BF32_GET_SB(x, low, len, shift, bias)	\
     72    789    ahrens 	((BF32_GET(x, low, len) + (bias)) << (shift))
     73    789    ahrens #define	BF64_GET_SB(x, low, len, shift, bias)	\
     74    789    ahrens 	((BF64_GET(x, low, len) + (bias)) << (shift))
     75    789    ahrens 
     76    789    ahrens #define	BF32_SET_SB(x, low, len, shift, bias, val)	\
     77    789    ahrens 	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
     78    789    ahrens #define	BF64_SET_SB(x, low, len, shift, bias, val)	\
     79    789    ahrens 	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
     80    789    ahrens 
     81    789    ahrens /*
     82    789    ahrens  * We currently support nine block sizes, from 512 bytes to 128K.
     83    789    ahrens  * We could go higher, but the benefits are near-zero and the cost
     84    789    ahrens  * of COWing a giant block to modify one byte would become excessive.
     85    789    ahrens  */
     86    789    ahrens #define	SPA_MINBLOCKSHIFT	9
     87    789    ahrens #define	SPA_MAXBLOCKSHIFT	17
     88    789    ahrens #define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
     89    789    ahrens #define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
     90    789    ahrens 
     91    789    ahrens #define	SPA_BLOCKSIZES		(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
     92   7497       Tim 
     93   7497       Tim /*
     94   7497       Tim  * Size of block to hold the configuration data (a packed nvlist)
     95   7497       Tim  */
     96   7497       Tim #define	SPA_CONFIG_BLOCKSIZE	(1 << 14)
     97    789    ahrens 
     98    789    ahrens /*
     99    789    ahrens  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
    100    789    ahrens  * The ASIZE encoding should be at least 64 times larger (6 more bits)
    101    789    ahrens  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
    102    789    ahrens  * overhead, three DVAs per bp, plus one more bit in case we do anything
    103    789    ahrens  * else that expands the ASIZE.
    104    789    ahrens  */
    105    789    ahrens #define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
    106    789    ahrens #define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
    107    789    ahrens #define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
    108    789    ahrens 
    109    789    ahrens /*
    110    789    ahrens  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
    111    789    ahrens  * The members of the dva_t should be considered opaque outside the SPA.
    112    789    ahrens  */
    113    789    ahrens typedef struct dva {
    114    789    ahrens 	uint64_t	dva_word[2];
    115    789    ahrens } dva_t;
    116    789    ahrens 
    117    789    ahrens /*
    118    789    ahrens  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
    119    789    ahrens  */
    120    789    ahrens typedef struct zio_cksum {
    121    789    ahrens 	uint64_t	zc_word[4];
    122    789    ahrens } zio_cksum_t;
    123    789    ahrens 
    124    789    ahrens /*
    125    789    ahrens  * Each block is described by its DVAs, time of birth, checksum, etc.
    126    789    ahrens  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
    127    789    ahrens  *
    128    789    ahrens  *	64	56	48	40	32	24	16	8	0
    129    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    130    789    ahrens  * 0	|		vdev1		| GRID  |	  ASIZE		|
    131    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    132    789    ahrens  * 1	|G|			 offset1				|
    133    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    134    789    ahrens  * 2	|		vdev2		| GRID  |	  ASIZE		|
    135    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    136    789    ahrens  * 3	|G|			 offset2				|
    137    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    138    789    ahrens  * 4	|		vdev3		| GRID  |	  ASIZE		|
    139    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    140    789    ahrens  * 5	|G|			 offset3				|
    141    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    142  10922      Jeff  * 6	|BDX|lvl| type	| cksum | comp	|     PSIZE	|     LSIZE	|
    143    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    144    789    ahrens  * 7	|			padding					|
    145    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    146    789    ahrens  * 8	|			padding					|
    147    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    148  10922      Jeff  * 9	|			physical birth txg			|
    149    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    150  10922      Jeff  * a	|			logical birth txg			|
    151    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    152    789    ahrens  * b	|			fill count				|
    153    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    154    789    ahrens  * c	|			checksum[0]				|
    155    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    156    789    ahrens  * d	|			checksum[1]				|
    157    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    158    789    ahrens  * e	|			checksum[2]				|
    159    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    160    789    ahrens  * f	|			checksum[3]				|
    161    789    ahrens  *	+-------+-------+-------+-------+-------+-------+-------+-------+
    162    789    ahrens  *
    163    789    ahrens  * Legend:
    164    789    ahrens  *
    165    789    ahrens  * vdev		virtual device ID
    166    789    ahrens  * offset	offset into virtual device
    167    789    ahrens  * LSIZE	logical size
    168    789    ahrens  * PSIZE	physical size (after compression)
    169    789    ahrens  * ASIZE	allocated size (including RAID-Z parity and gang block headers)
    170    789    ahrens  * GRID		RAID-Z layout information (reserved for future use)
    171    789    ahrens  * cksum	checksum function
    172    789    ahrens  * comp		compression function
    173    789    ahrens  * G		gang block indicator
    174  10922      Jeff  * B		byteorder (endianness)
    175  10922      Jeff  * D		dedup
    176  10922      Jeff  * X		unused
    177  10922      Jeff  * lvl		level of indirection
    178    789    ahrens  * type		DMU object type
    179  10922      Jeff  * phys birth	txg of block allocation; zero if same as logical birth txg
    180  10922      Jeff  * log. birth	transaction group in which the block was logically born
    181    789    ahrens  * fill count	number of non-zero blocks under this bp
    182    789    ahrens  * checksum[4]	256-bit checksum of the data this bp describes
    183    789    ahrens  */
    184    789    ahrens #define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
    185    789    ahrens #define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
    186  10922      Jeff 
    187  10922      Jeff typedef struct blkptr {
    188  10922      Jeff 	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
    189  10922      Jeff 	uint64_t	blk_prop;	/* size, compression, type, etc	    */
    190  10922      Jeff 	uint64_t	blk_pad[2];	/* Extra space for the future	    */
    191  10922      Jeff 	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
    192  10922      Jeff 	uint64_t	blk_birth;	/* transaction group at birth	    */
    193  10922      Jeff 	uint64_t	blk_fill;	/* fill count			    */
    194  10922      Jeff 	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
    195  10922      Jeff } blkptr_t;
    196    789    ahrens 
    197    789    ahrens /*
    198    789    ahrens  * Macros to get and set fields in a bp or DVA.
    199    789    ahrens  */
    200    789    ahrens #define	DVA_GET_ASIZE(dva)	\
    201    789    ahrens 	BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
    202    789    ahrens #define	DVA_SET_ASIZE(dva, x)	\
    203    789    ahrens 	BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
    204    789    ahrens 
    205    789    ahrens #define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
    206    789    ahrens #define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
    207    789    ahrens 
    208    789    ahrens #define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
    209    789    ahrens #define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
    210    789    ahrens 
    211    789    ahrens #define	DVA_GET_OFFSET(dva)	\
    212    789    ahrens 	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
    213    789    ahrens #define	DVA_SET_OFFSET(dva, x)	\
    214    789    ahrens 	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
    215    789    ahrens 
    216    789    ahrens #define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
    217    789    ahrens #define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
    218    789    ahrens 
    219    789    ahrens #define	BP_GET_LSIZE(bp)	\
    220  10800      Neil 	BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
    221    789    ahrens #define	BP_SET_LSIZE(bp, x)	\
    222    789    ahrens 	BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
    223    789    ahrens 
    224    789    ahrens #define	BP_GET_PSIZE(bp)	\
    225    789    ahrens 	BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
    226    789    ahrens #define	BP_SET_PSIZE(bp, x)	\
    227    789    ahrens 	BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
    228    789    ahrens 
    229  10922      Jeff #define	BP_GET_COMPRESS(bp)		BF64_GET((bp)->blk_prop, 32, 8)
    230  10922      Jeff #define	BP_SET_COMPRESS(bp, x)		BF64_SET((bp)->blk_prop, 32, 8, x)
    231    789    ahrens 
    232  10922      Jeff #define	BP_GET_CHECKSUM(bp)		BF64_GET((bp)->blk_prop, 40, 8)
    233  10922      Jeff #define	BP_SET_CHECKSUM(bp, x)		BF64_SET((bp)->blk_prop, 40, 8, x)
    234    789    ahrens 
    235  10922      Jeff #define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
    236  10922      Jeff #define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
    237    789    ahrens 
    238  10922      Jeff #define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
    239  10922      Jeff #define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
    240    789    ahrens 
    241  10922      Jeff #define	BP_GET_PROP_BIT_61(bp)		BF64_GET((bp)->blk_prop, 61, 1)
    242  10922      Jeff #define	BP_SET_PROP_BIT_61(bp, x)	BF64_SET((bp)->blk_prop, 61, 1, x)
    243  10922      Jeff 
    244  10922      Jeff #define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
    245  10922      Jeff #define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
    246  10922      Jeff 
    247  10922      Jeff #define	BP_GET_BYTEORDER(bp)		(0 - BF64_GET((bp)->blk_prop, 63, 1))
    248  10922      Jeff #define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
    249  10922      Jeff 
    250  10922      Jeff #define	BP_PHYSICAL_BIRTH(bp)		\
    251  10922      Jeff 	((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
    252  10922      Jeff 
    253  10922      Jeff #define	BP_SET_BIRTH(bp, logical, physical)	\
    254  10922      Jeff {						\
    255  10922      Jeff 	(bp)->blk_birth = (logical);		\
    256  10922      Jeff 	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
    257  10922      Jeff }
    258    789    ahrens 
    259    789    ahrens #define	BP_GET_ASIZE(bp)	\
    260    789    ahrens 	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
    261   2082  eschrock 		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
    262   2082  eschrock 
    263   2082  eschrock #define	BP_GET_UCSIZE(bp) \
    264   2082  eschrock 	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
    265   2082  eschrock 	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
    266    789    ahrens 
    267   1775     billm #define	BP_GET_NDVAS(bp)	\
    268   1775     billm 	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
    269   1775     billm 	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
    270   1775     billm 	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
    271   1775     billm 
    272   1775     billm #define	BP_COUNT_GANG(bp)	\
    273   1775     billm 	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
    274   1775     billm 	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
    275   1775     billm 	DVA_GET_GANG(&(bp)->blk_dva[2]))
    276   1775     billm 
    277    789    ahrens #define	DVA_EQUAL(dva1, dva2)	\
    278    789    ahrens 	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
    279    789    ahrens 	(dva1)->dva_word[0] == (dva2)->dva_word[0])
    280    789    ahrens 
    281  10922      Jeff #define	BP_EQUAL(bp1, bp2)	\
    282  10922      Jeff 	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
    283  10922      Jeff 	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
    284  10922      Jeff 	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
    285  10922      Jeff 	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
    286  10922      Jeff 
    287   3093    ahrens #define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
    288   3093    ahrens 	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
    289   3093    ahrens 	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
    290   3093    ahrens 	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
    291   3093    ahrens 	((zc1).zc_word[3] - (zc2).zc_word[3])))
    292   3093    ahrens 
    293    789    ahrens #define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
    294    789    ahrens 
    295    789    ahrens #define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
    296    789    ahrens {						\
    297    789    ahrens 	(zcp)->zc_word[0] = w0;			\
    298    789    ahrens 	(zcp)->zc_word[1] = w1;			\
    299    789    ahrens 	(zcp)->zc_word[2] = w2;			\
    300    789    ahrens 	(zcp)->zc_word[3] = w3;			\
    301    789    ahrens }
    302    789    ahrens 
    303   1775     billm #define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
    304   1775     billm #define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
    305    789    ahrens #define	BP_IS_HOLE(bp)		((bp)->blk_birth == 0)
    306    789    ahrens 
    307   7754      Jeff #define	BP_ZERO(bp)				\
    308    789    ahrens {						\
    309    789    ahrens 	(bp)->blk_dva[0].dva_word[0] = 0;	\
    310    789    ahrens 	(bp)->blk_dva[0].dva_word[1] = 0;	\
    311    789    ahrens 	(bp)->blk_dva[1].dva_word[0] = 0;	\
    312    789    ahrens 	(bp)->blk_dva[1].dva_word[1] = 0;	\
    313    789    ahrens 	(bp)->blk_dva[2].dva_word[0] = 0;	\
    314    789    ahrens 	(bp)->blk_dva[2].dva_word[1] = 0;	\
    315    789    ahrens 	(bp)->blk_prop = 0;			\
    316    789    ahrens 	(bp)->blk_pad[0] = 0;			\
    317    789    ahrens 	(bp)->blk_pad[1] = 0;			\
    318  10922      Jeff 	(bp)->blk_phys_birth = 0;		\
    319   7754      Jeff 	(bp)->blk_birth = 0;			\
    320    789    ahrens 	(bp)->blk_fill = 0;			\
    321    789    ahrens 	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
    322    789    ahrens }
    323    789    ahrens 
    324    789    ahrens /*
    325    789    ahrens  * Note: the byteorder is either 0 or -1, both of which are palindromes.
    326    789    ahrens  * This simplifies the endianness handling a bit.
    327    789    ahrens  */
    328    789    ahrens #ifdef _BIG_ENDIAN
    329    789    ahrens #define	ZFS_HOST_BYTEORDER	(0ULL)
    330    789    ahrens #else
    331    789    ahrens #define	ZFS_HOST_BYTEORDER	(-1ULL)
    332    789    ahrens #endif
    333    789    ahrens 
    334    789    ahrens #define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
    335    789    ahrens 
    336   1775     billm #define	BP_SPRINTF_LEN	320
    337    896    maybee 
    338  10922      Jeff /*
    339  10922      Jeff  * This macro allows code sharing between zfs, libzpool, and mdb.
    340  10922      Jeff  * 'func' is either snprintf() or mdb_snprintf().
    341  10922      Jeff  * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
    342  10922      Jeff  */
    343  10922      Jeff #define	SPRINTF_BLKPTR(func, ws, buf, bp, type, checksum, compress)	\
    344  10922      Jeff {									\
    345  10922      Jeff 	static const char *copyname[] =					\
    346  10922      Jeff 	    { "zero", "single", "double", "triple" };			\
    347  10922      Jeff 	int size = BP_SPRINTF_LEN;					\
    348  10922      Jeff 	int len = 0;							\
    349  10922      Jeff 	int copies = 0;							\
    350  10922      Jeff 									\
    351  10922      Jeff 	if (bp == NULL) {						\
    352  10922      Jeff 		len = func(buf + len, size - len, "<NULL>");		\
    353  10922      Jeff 	} else if (BP_IS_HOLE(bp)) {					\
    354  10922      Jeff 		len = func(buf + len, size - len, "<hole>");		\
    355  10922      Jeff 	} else {							\
    356  10922      Jeff 		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
    357  10922      Jeff 			const dva_t *dva = &bp->blk_dva[d];		\
    358  10922      Jeff 			if (DVA_IS_VALID(dva))				\
    359  10922      Jeff 				copies++;				\
    360  10922      Jeff 			len += func(buf + len, size - len,		\
    361  10922      Jeff 			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
    362  10922      Jeff 			    (u_longlong_t)DVA_GET_VDEV(dva),		\
    363  10922      Jeff 			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
    364  10922      Jeff 			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
    365  10922      Jeff 			    ws);					\
    366  10922      Jeff 		}							\
    367  10922      Jeff 		if (BP_IS_GANG(bp) &&					\
    368  10922      Jeff 		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
    369  10922      Jeff 		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
    370  10922      Jeff 			copies--;					\
    371  10922      Jeff 		len += func(buf + len, size - len,			\
    372  10922      Jeff 		    "[L%llu %s] %s %s %s %s %s %s%c"			\
    373  10922      Jeff 		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
    374  10922      Jeff 		    "cksum=%llx:%llx:%llx:%llx",			\
    375  10922      Jeff 		    (u_longlong_t)BP_GET_LEVEL(bp),			\
    376  10922      Jeff 		    type,						\
    377  10922      Jeff 		    checksum,						\
    378  10922      Jeff 		    compress,						\
    379  10922      Jeff 		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
    380  10922      Jeff 		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
    381  10922      Jeff 		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
    382  10922      Jeff 		    copyname[copies],					\
    383  10922      Jeff 		    ws,							\
    384  10922      Jeff 		    (u_longlong_t)BP_GET_LSIZE(bp),			\
    385  10922      Jeff 		    (u_longlong_t)BP_GET_PSIZE(bp),			\
    386  10922      Jeff 		    (u_longlong_t)bp->blk_birth,			\
    387  10922      Jeff 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp),		\
    388  10922      Jeff 		    (u_longlong_t)bp->blk_fill,				\
    389  10922      Jeff 		    ws,							\
    390  10922      Jeff 		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
    391  10922      Jeff 		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
    392  10922      Jeff 		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
    393  10922      Jeff 		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
    394  10922      Jeff 	}								\
    395  10922      Jeff 	ASSERT(len < size);						\
    396  10922      Jeff }
    397  10922      Jeff 
    398    789    ahrens #include <sys/dmu.h>
    399    789    ahrens 
    400   3290  johansen #define	BP_GET_BUFC_TYPE(bp)						\
    401   3290  johansen 	(((BP_GET_LEVEL(bp) > 0) || (dmu_ot[BP_GET_TYPE(bp)].ot_metadata)) ? \
    402   3290  johansen 	ARC_BUFC_METADATA : ARC_BUFC_DATA);
    403    789    ahrens 
    404    789    ahrens /* state manipulation functions */
    405    789    ahrens extern int spa_open(const char *pool, spa_t **, void *tag);
    406  10921       Tim extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
    407  10921       Tim     nvlist_t *policy, nvlist_t **config);
    408   1544  eschrock extern int spa_get_stats(const char *pool, nvlist_t **config,
    409   1544  eschrock     char *altroot, size_t buflen);
    410   5094     lling extern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
    411   7184      timh     const char *history_str, nvlist_t *zplprops);
    412   7147    taylor extern int spa_import_rootpool(char *devpath, char *devid);
    413   5094     lling extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props);
    414   9425      Eric extern int spa_import_verbatim(const char *, nvlist_t *, nvlist_t *);
    415    789    ahrens extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
    416    789    ahrens extern int spa_destroy(char *pool);
    417   8211    George extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
    418   8211    George     boolean_t hardforce);
    419   1544  eschrock extern int spa_reset(char *pool);
    420   1544  eschrock extern void spa_async_request(spa_t *spa, int flag);
    421   7046    ahrens extern void spa_async_unrequest(spa_t *spa, int flag);
    422   1544  eschrock extern void spa_async_suspend(spa_t *spa);
    423   1544  eschrock extern void spa_async_resume(spa_t *spa);
    424   1544  eschrock extern spa_t *spa_inject_addref(char *pool);
    425   1544  eschrock extern void spa_inject_delref(spa_t *spa);
    426   1544  eschrock 
    427   7754      Jeff #define	SPA_ASYNC_CONFIG_UPDATE	0x01
    428   7754      Jeff #define	SPA_ASYNC_REMOVE	0x02
    429   7754      Jeff #define	SPA_ASYNC_PROBE		0x04
    430   7754      Jeff #define	SPA_ASYNC_RESILVER_DONE	0x08
    431   7754      Jeff #define	SPA_ASYNC_RESILVER	0x10
    432   9816    George #define	SPA_ASYNC_AUTOEXPAND	0x20
    433    789    ahrens 
    434    789    ahrens /* device manipulation */
    435    789    ahrens extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
    436   1544  eschrock extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
    437    789    ahrens     int replacing);
    438   8241      Jeff extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
    439   8241      Jeff     int replace_done);
    440   2082  eschrock extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
    441   1354  eschrock extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
    442   9425      Eric extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
    443   2082  eschrock 
    444   2082  eschrock /* spare state (which is global across all pools) */
    445   3377  eschrock extern void spa_spare_add(vdev_t *vd);
    446   3377  eschrock extern void spa_spare_remove(vdev_t *vd);
    447   7214     lling extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
    448   3377  eschrock extern void spa_spare_activate(vdev_t *vd);
    449    789    ahrens 
    450   5450   brendan /* L2ARC state (which is global across all pools) */
    451   5450   brendan extern void spa_l2cache_add(vdev_t *vd);
    452   5450   brendan extern void spa_l2cache_remove(vdev_t *vd);
    453   5450   brendan extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
    454   5450   brendan extern void spa_l2cache_activate(vdev_t *vd);
    455   5450   brendan extern void spa_l2cache_drop(spa_t *spa);
    456   5450   brendan 
    457    789    ahrens /* scrubbing */
    458   7046    ahrens extern int spa_scrub(spa_t *spa, pool_scrub_type_t type);
    459    789    ahrens 
    460    789    ahrens /* spa syncing */
    461    789    ahrens extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
    462    789    ahrens extern void spa_sync_allpools(void);
    463  10922      Jeff 
    464  10922      Jeff #define	SYNC_PASS_DEFERRED_FREE	1	/* defer frees after this pass */
    465  10922      Jeff #define	SYNC_PASS_DONT_COMPRESS	4	/* don't compress after this pass */
    466  10922      Jeff #define	SYNC_PASS_REWRITE	1	/* rewrite new bps after this pass */
    467    789    ahrens 
    468   6987   brendan /* spa namespace global mutex */
    469   6987   brendan extern kmutex_t spa_namespace_lock;
    470   6987   brendan 
    471    789    ahrens /*
    472    789    ahrens  * SPA configuration functions in spa_config.c
    473    789    ahrens  */
    474   1635   bonwick 
    475   1635   bonwick #define	SPA_CONFIG_UPDATE_POOL	0
    476   1635   bonwick #define	SPA_CONFIG_UPDATE_VDEVS	1
    477   1635   bonwick 
    478   6643  eschrock extern void spa_config_sync(spa_t *, boolean_t, boolean_t);
    479    789    ahrens extern void spa_config_load(void);
    480    789    ahrens extern nvlist_t *spa_all_configs(uint64_t *);
    481    789    ahrens extern void spa_config_set(spa_t *spa, nvlist_t *config);
    482    789    ahrens extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
    483    789    ahrens     int getstats);
    484   1635   bonwick extern void spa_config_update(spa_t *spa, int what);
    485    789    ahrens 
    486    789    ahrens /*
    487    789    ahrens  * Miscellaneous SPA routines in spa_misc.c
    488    789    ahrens  */
    489    789    ahrens 
    490    789    ahrens /* Namespace manipulation */
    491    789    ahrens extern spa_t *spa_lookup(const char *name);
    492  10921       Tim extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
    493    789    ahrens extern void spa_remove(spa_t *spa);
    494    789    ahrens extern spa_t *spa_next(spa_t *prev);
    495    789    ahrens 
    496    789    ahrens /* Refcount functions */
    497    789    ahrens extern void spa_open_ref(spa_t *spa, void *tag);
    498    789    ahrens extern void spa_close(spa_t *spa, void *tag);
    499    789    ahrens extern boolean_t spa_refcount_zero(spa_t *spa);
    500    789    ahrens 
    501  10685    George #define	SCL_NONE	0x00
    502   7754      Jeff #define	SCL_CONFIG	0x01
    503   7754      Jeff #define	SCL_STATE	0x02
    504   7754      Jeff #define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
    505   7754      Jeff #define	SCL_ALLOC	0x08
    506   7754      Jeff #define	SCL_ZIO		0x10
    507   7754      Jeff #define	SCL_FREE	0x20
    508   7754      Jeff #define	SCL_VDEV	0x40
    509   7754      Jeff #define	SCL_LOCKS	7
    510   7754      Jeff #define	SCL_ALL		((1 << SCL_LOCKS) - 1)
    511   7754      Jeff #define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
    512   7754      Jeff 
    513   7754      Jeff /* Pool configuration locks */
    514   7754      Jeff extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
    515   7754      Jeff extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
    516   7754      Jeff extern void spa_config_exit(spa_t *spa, int locks, void *tag);
    517   7754      Jeff extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
    518    789    ahrens 
    519    789    ahrens /* Pool vdev add/remove lock */
    520    789    ahrens extern uint64_t spa_vdev_enter(spa_t *spa);
    521  10594    George extern uint64_t spa_vdev_config_enter(spa_t *spa);
    522  10594    George extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
    523  10594    George     int error, char *tag);
    524    789    ahrens extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
    525   7754      Jeff 
    526   7754      Jeff /* Pool vdev state change lock */
    527  10685    George extern void spa_vdev_state_enter(spa_t *spa, int oplock);
    528   7754      Jeff extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
    529    789    ahrens 
    530  10922      Jeff /* Log state */
    531  10922      Jeff typedef enum spa_log_state {
    532  10922      Jeff 	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
    533  10922      Jeff 	SPA_LOG_MISSING,	/* missing log(s) */
    534  10922      Jeff 	SPA_LOG_CLEAR,		/* clear the log(s) */
    535  10922      Jeff 	SPA_LOG_GOOD,		/* log(s) are good */
    536  10922      Jeff } spa_log_state_t;
    537  10922      Jeff 
    538  10922      Jeff extern spa_log_state_t spa_get_log_state(spa_t *spa);
    539  10922      Jeff extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
    540  10922      Jeff 
    541  10922      Jeff /* Log claim callback */
    542  10922      Jeff extern void spa_claim_notify(zio_t *zio);
    543  10922      Jeff 
    544    789    ahrens /* Accessor functions */
    545   7837   Matthew extern boolean_t spa_shutting_down(spa_t *spa);
    546    789    ahrens extern struct dsl_pool *spa_get_dsl(spa_t *spa);
    547    789    ahrens extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
    548    789    ahrens extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
    549    789    ahrens extern void spa_altroot(spa_t *, char *, size_t);
    550    789    ahrens extern int spa_sync_pass(spa_t *spa);
    551    789    ahrens extern char *spa_name(spa_t *spa);
    552    789    ahrens extern uint64_t spa_guid(spa_t *spa);
    553    789    ahrens extern uint64_t spa_last_synced_txg(spa_t *spa);
    554    789    ahrens extern uint64_t spa_first_txg(spa_t *spa);
    555  10922      Jeff extern uint64_t spa_syncing_txg(spa_t *spa);
    556   2082  eschrock extern uint64_t spa_version(spa_t *spa);
    557   7837   Matthew extern pool_state_t spa_state(spa_t *spa);
    558  11147    George extern spa_load_state_t spa_load_state(spa_t *spa);
    559    789    ahrens extern uint64_t spa_freeze_txg(spa_t *spa);
    560    789    ahrens extern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
    561  10956    George extern uint64_t spa_get_dspace(spa_t *spa);
    562  10956    George extern void spa_update_dspace(spa_t *spa);
    563   1775     billm extern uint64_t spa_version(spa_t *spa);
    564  10922      Jeff extern boolean_t spa_deflate(spa_t *spa);
    565  10922      Jeff extern metaslab_class_t *spa_normal_class(spa_t *spa);
    566  10922      Jeff extern metaslab_class_t *spa_log_class(spa_t *spa);
    567   1775     billm extern int spa_max_replication(spa_t *spa);
    568    789    ahrens extern int spa_busy(void);
    569   5329   gw25295 extern uint8_t spa_get_failmode(spa_t *spa);
    570   7754      Jeff extern boolean_t spa_suspended(spa_t *spa);
    571  10922      Jeff extern uint64_t spa_bootfs(spa_t *spa);
    572  10922      Jeff extern uint64_t spa_delegation(spa_t *spa);
    573  10922      Jeff extern objset_t *spa_meta_objset(spa_t *spa);
    574  10922      Jeff extern enum zio_checksum spa_dedup_checksum(spa_t *spa);
    575    789    ahrens 
    576    789    ahrens /* Miscellaneous support routines */
    577    789    ahrens extern int spa_rename(const char *oldname, const char *newname);
    578    789    ahrens extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
    579    789    ahrens extern char *spa_strdup(const char *);
    580    789    ahrens extern void spa_strfree(char *);
    581    789    ahrens extern uint64_t spa_get_random(uint64_t range);
    582  10922      Jeff extern void sprintf_blkptr(char *buf, const blkptr_t *bp);
    583    789    ahrens extern void spa_freeze(spa_t *spa);
    584   5094     lling extern void spa_upgrade(spa_t *spa, uint64_t version);
    585    789    ahrens extern void spa_evict_all(void);
    586   6643  eschrock extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
    587   6643  eschrock     boolean_t l2cache);
    588   2082  eschrock extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
    589  10922      Jeff extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
    590  10922      Jeff extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
    591  10922      Jeff extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
    592   4620    perrin extern boolean_t spa_has_slogs(spa_t *spa);
    593   6673  eschrock extern boolean_t spa_is_root(spa_t *spa);
    594   8241      Jeff extern boolean_t spa_writeable(spa_t *spa);
    595  10921       Tim extern void spa_rewind_data_to_nvlist(spa_t *spa, nvlist_t *to);
    596  10921       Tim 
    597   8241      Jeff extern int spa_mode(spa_t *spa);
    598  10342     chris extern uint64_t strtonum(const char *str, char **nptr);
    599   1544  eschrock 
    600   2926  ek110237 /* history logging */
    601   4543     marks typedef enum history_log_type {
    602   4543     marks 	LOG_CMD_POOL_CREATE,
    603   4543     marks 	LOG_CMD_NORMAL,
    604   4543     marks 	LOG_INTERNAL
    605   4543     marks } history_log_type_t;
    606   4543     marks 
    607   4543     marks typedef struct history_arg {
    608   4543     marks 	const char *ha_history_str;
    609   4543     marks 	history_log_type_t ha_log_type;
    610   4543     marks 	history_internal_events_t ha_event;
    611   4543     marks 	char ha_zone[MAXPATHLEN];
    612   4543     marks } history_arg_t;
    613   4543     marks 
    614   4543     marks extern char *spa_his_ievent_table[];
    615   4543     marks 
    616   2926  ek110237 extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
    617   2926  ek110237 extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
    618   2926  ek110237     char *his_buf);
    619   2926  ek110237 extern int spa_history_log(spa_t *spa, const char *his_buf,
    620   4543     marks     history_log_type_t what);
    621   9946      Mark extern void spa_history_internal_log(history_internal_events_t event,
    622   9946      Mark     spa_t *spa, dmu_tx_t *tx, cred_t *cr, const char *fmt, ...);
    623   9946      Mark extern void spa_history_log_version(spa_t *spa, history_internal_events_t evt);
    624   2926  ek110237 
    625   1544  eschrock /* error handling */
    626   1544  eschrock struct zbookmark;
    627  10922      Jeff extern void spa_log_error(spa_t *spa, zio_t *zio);
    628   1544  eschrock extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
    629  10922      Jeff     zio_t *zio, uint64_t stateoroffset, uint64_t length);
    630   4451  eschrock extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
    631  10817      Eric extern void zfs_post_state_change(spa_t *spa, vdev_t *vd);
    632   4451  eschrock extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
    633   1544  eschrock extern uint64_t spa_get_errlog_size(spa_t *spa);
    634   1544  eschrock extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
    635   1544  eschrock extern void spa_errlog_rotate(spa_t *spa);
    636   1544  eschrock extern void spa_errlog_drain(spa_t *spa);
    637   1544  eschrock extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
    638   1544  eschrock extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
    639    789    ahrens 
    640   5810  ek110237 /* vdev cache */
    641   5810  ek110237 extern void vdev_cache_stat_init(void);
    642   5810  ek110237 extern void vdev_cache_stat_fini(void);
    643   5810  ek110237 
    644    789    ahrens /* Initialization and termination */
    645    789    ahrens extern void spa_init(int flags);
    646    789    ahrens extern void spa_fini(void);
    647   6423   gw25295 extern void spa_boot_init();
    648    789    ahrens 
    649   3912     lling /* properties */
    650   5094     lling extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
    651   5094     lling extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
    652   5094     lling extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
    653   8525      Eric extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
    654   4451  eschrock 
    655   4451  eschrock /* asynchronous event notification */
    656   4451  eschrock extern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name);
    657   3912     lling 
    658    789    ahrens #ifdef ZFS_DEBUG
    659   3700  ek110237 #define	dprintf_bp(bp, fmt, ...) do {				\
    660   3700  ek110237 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { 			\
    661   3700  ek110237 	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
    662  10922      Jeff 	sprintf_blkptr(__blkbuf, (bp));				\
    663   3700  ek110237 	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
    664   3700  ek110237 	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
    665    789    ahrens 	} \
    666    789    ahrens _NOTE(CONSTCOND) } while (0)
    667    789    ahrens #else
    668    789    ahrens #define	dprintf_bp(bp, fmt, ...)
    669    789    ahrens #endif
    670    789    ahrens 
    671   8241      Jeff extern int spa_mode_global;			/* mode, e.g. FREAD | FWRITE */
    672    789    ahrens 
    673    789    ahrens #ifdef	__cplusplus
    674    789    ahrens }
    675    789    ahrens #endif
    676    789    ahrens 
    677    789    ahrens #endif	/* _SYS_SPA_H */
    678