spa.h revision 770499e185d15678ccb0be57ebc626ad18d93383
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 * Copyright 2013 Saso Kiselkov. All rights reserved.
27 * Copyright (c) 2014 Integros [integros.com]
28 */
29
30#ifndef _SYS_SPA_H
31#define	_SYS_SPA_H
32
33#include <sys/avl.h>
34#include <sys/zfs_context.h>
35#include <sys/nvpair.h>
36#include <sys/sysmacros.h>
37#include <sys/types.h>
38#include <sys/fs/zfs.h>
39#include <sys/dmu.h>
40
41#ifdef	__cplusplus
42extern "C" {
43#endif
44
45/*
46 * Forward references that lots of things need.
47 */
48typedef struct spa spa_t;
49typedef struct vdev vdev_t;
50typedef struct metaslab metaslab_t;
51typedef struct metaslab_group metaslab_group_t;
52typedef struct metaslab_class metaslab_class_t;
53typedef struct zio zio_t;
54typedef struct zilog zilog_t;
55typedef struct spa_aux_vdev spa_aux_vdev_t;
56typedef struct ddt ddt_t;
57typedef struct ddt_entry ddt_entry_t;
58struct dsl_pool;
59struct dsl_dataset;
60
61/*
62 * General-purpose 32-bit and 64-bit bitfield encodings.
63 */
64#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
65#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
66#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
67#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
68
69#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
70#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
71
72#define	BF32_SET(x, low, len, val) do { \
73	ASSERT3U(val, <, 1U << (len)); \
74	ASSERT3U(low + len, <=, 32); \
75	(x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
76_NOTE(CONSTCOND) } while (0)
77
78#define	BF64_SET(x, low, len, val) do { \
79	ASSERT3U(val, <, 1ULL << (len)); \
80	ASSERT3U(low + len, <=, 64); \
81	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
82_NOTE(CONSTCOND) } while (0)
83
84#define	BF32_GET_SB(x, low, len, shift, bias)	\
85	((BF32_GET(x, low, len) + (bias)) << (shift))
86#define	BF64_GET_SB(x, low, len, shift, bias)	\
87	((BF64_GET(x, low, len) + (bias)) << (shift))
88
89#define	BF32_SET_SB(x, low, len, shift, bias, val) do { \
90	ASSERT(IS_P2ALIGNED(val, 1U << shift)); \
91	ASSERT3S((val) >> (shift), >=, bias); \
92	BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
93_NOTE(CONSTCOND) } while (0)
94#define	BF64_SET_SB(x, low, len, shift, bias, val) do { \
95	ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \
96	ASSERT3S((val) >> (shift), >=, bias); \
97	BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
98_NOTE(CONSTCOND) } while (0)
99
100/*
101 * We currently support block sizes from 512 bytes to 16MB.
102 * The benefits of larger blocks, and thus larger IO, need to be weighed
103 * against the cost of COWing a giant block to modify one byte, and the
104 * large latency of reading or writing a large block.
105 *
106 * Note that although blocks up to 16MB are supported, the recordsize
107 * property can not be set larger than zfs_max_recordsize (default 1MB).
108 * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
109 *
110 * Note that although the LSIZE field of the blkptr_t can store sizes up
111 * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
112 * 32MB - 512 bytes.  Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
113 */
114#define	SPA_MINBLOCKSHIFT	9
115#define	SPA_OLD_MAXBLOCKSHIFT	17
116#define	SPA_MAXBLOCKSHIFT	24
117#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
118#define	SPA_OLD_MAXBLOCKSIZE	(1ULL << SPA_OLD_MAXBLOCKSHIFT)
119#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
120
121/*
122 * Size of block to hold the configuration data (a packed nvlist)
123 */
124#define	SPA_CONFIG_BLOCKSIZE	(1ULL << 14)
125
126/*
127 * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
128 * The ASIZE encoding should be at least 64 times larger (6 more bits)
129 * to support up to 4-way RAID-Z mirror mode with worst-case gang block
130 * overhead, three DVAs per bp, plus one more bit in case we do anything
131 * else that expands the ASIZE.
132 */
133#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
134#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
135#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
136
137#define	SPA_COMPRESSBITS	7
138
139/*
140 * All SPA data is represented by 128-bit data virtual addresses (DVAs).
141 * The members of the dva_t should be considered opaque outside the SPA.
142 */
143typedef struct dva {
144	uint64_t	dva_word[2];
145} dva_t;
146
147/*
148 * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
149 */
150typedef struct zio_cksum {
151	uint64_t	zc_word[4];
152} zio_cksum_t;
153
154/*
155 * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
156 * secret and is suitable for use in MAC algorithms as the key.
157 */
158typedef struct zio_cksum_salt {
159	uint8_t		zcs_bytes[32];
160} zio_cksum_salt_t;
161
162/*
163 * Each block is described by its DVAs, time of birth, checksum, etc.
164 * The word-by-word, bit-by-bit layout of the blkptr is as follows:
165 *
166 *	64	56	48	40	32	24	16	8	0
167 *	+-------+-------+-------+-------+-------+-------+-------+-------+
168 * 0	|		vdev1		| GRID  |	  ASIZE		|
169 *	+-------+-------+-------+-------+-------+-------+-------+-------+
170 * 1	|G|			 offset1				|
171 *	+-------+-------+-------+-------+-------+-------+-------+-------+
172 * 2	|		vdev2		| GRID  |	  ASIZE		|
173 *	+-------+-------+-------+-------+-------+-------+-------+-------+
174 * 3	|G|			 offset2				|
175 *	+-------+-------+-------+-------+-------+-------+-------+-------+
176 * 4	|		vdev3		| GRID  |	  ASIZE		|
177 *	+-------+-------+-------+-------+-------+-------+-------+-------+
178 * 5	|G|			 offset3				|
179 *	+-------+-------+-------+-------+-------+-------+-------+-------+
180 * 6	|BDX|lvl| type	| cksum |E| comp|    PSIZE	|     LSIZE	|
181 *	+-------+-------+-------+-------+-------+-------+-------+-------+
182 * 7	|			padding					|
183 *	+-------+-------+-------+-------+-------+-------+-------+-------+
184 * 8	|			padding					|
185 *	+-------+-------+-------+-------+-------+-------+-------+-------+
186 * 9	|			physical birth txg			|
187 *	+-------+-------+-------+-------+-------+-------+-------+-------+
188 * a	|			logical birth txg			|
189 *	+-------+-------+-------+-------+-------+-------+-------+-------+
190 * b	|			fill count				|
191 *	+-------+-------+-------+-------+-------+-------+-------+-------+
192 * c	|			checksum[0]				|
193 *	+-------+-------+-------+-------+-------+-------+-------+-------+
194 * d	|			checksum[1]				|
195 *	+-------+-------+-------+-------+-------+-------+-------+-------+
196 * e	|			checksum[2]				|
197 *	+-------+-------+-------+-------+-------+-------+-------+-------+
198 * f	|			checksum[3]				|
199 *	+-------+-------+-------+-------+-------+-------+-------+-------+
200 *
201 * Legend:
202 *
203 * vdev		virtual device ID
204 * offset	offset into virtual device
205 * LSIZE	logical size
206 * PSIZE	physical size (after compression)
207 * ASIZE	allocated size (including RAID-Z parity and gang block headers)
208 * GRID		RAID-Z layout information (reserved for future use)
209 * cksum	checksum function
210 * comp		compression function
211 * G		gang block indicator
212 * B		byteorder (endianness)
213 * D		dedup
214 * X		encryption (on version 30, which is not supported)
215 * E		blkptr_t contains embedded data (see below)
216 * lvl		level of indirection
217 * type		DMU object type
218 * phys birth	txg of block allocation; zero if same as logical birth txg
219 * log. birth	transaction group in which the block was logically born
220 * fill count	number of non-zero blocks under this bp
221 * checksum[4]	256-bit checksum of the data this bp describes
222 */
223
224/*
225 * "Embedded" blkptr_t's don't actually point to a block, instead they
226 * have a data payload embedded in the blkptr_t itself.  See the comment
227 * in blkptr.c for more details.
228 *
229 * The blkptr_t is laid out as follows:
230 *
231 *	64	56	48	40	32	24	16	8	0
232 *	+-------+-------+-------+-------+-------+-------+-------+-------+
233 * 0	|      payload                                                  |
234 * 1	|      payload                                                  |
235 * 2	|      payload                                                  |
236 * 3	|      payload                                                  |
237 * 4	|      payload                                                  |
238 * 5	|      payload                                                  |
239 *	+-------+-------+-------+-------+-------+-------+-------+-------+
240 * 6	|BDX|lvl| type	| etype |E| comp| PSIZE|              LSIZE	|
241 *	+-------+-------+-------+-------+-------+-------+-------+-------+
242 * 7	|      payload                                                  |
243 * 8	|      payload                                                  |
244 * 9	|      payload                                                  |
245 *	+-------+-------+-------+-------+-------+-------+-------+-------+
246 * a	|			logical birth txg			|
247 *	+-------+-------+-------+-------+-------+-------+-------+-------+
248 * b	|      payload                                                  |
249 * c	|      payload                                                  |
250 * d	|      payload                                                  |
251 * e	|      payload                                                  |
252 * f	|      payload                                                  |
253 *	+-------+-------+-------+-------+-------+-------+-------+-------+
254 *
255 * Legend:
256 *
257 * payload		contains the embedded data
258 * B (byteorder)	byteorder (endianness)
259 * D (dedup)		padding (set to zero)
260 * X			encryption (set to zero; see above)
261 * E (embedded)		set to one
262 * lvl			indirection level
263 * type			DMU object type
264 * etype		how to interpret embedded data (BP_EMBEDDED_TYPE_*)
265 * comp			compression function of payload
266 * PSIZE		size of payload after compression, in bytes
267 * LSIZE		logical size of payload, in bytes
268 *			note that 25 bits is enough to store the largest
269 *			"normal" BP's LSIZE (2^16 * 2^9) in bytes
270 * log. birth		transaction group in which the block was logically born
271 *
272 * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
273 * bp's they are stored in units of SPA_MINBLOCKSHIFT.
274 * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
275 * The B, D, X, lvl, type, and comp fields are stored the same as with normal
276 * BP's so the BP_SET_* macros can be used with them.  etype, PSIZE, LSIZE must
277 * be set with the BPE_SET_* macros.  BP_SET_EMBEDDED() should be called before
278 * other macros, as they assert that they are only used on BP's of the correct
279 * "embedded-ness".
280 */
281
282#define	BPE_GET_ETYPE(bp)	\
283	(ASSERT(BP_IS_EMBEDDED(bp)), \
284	BF64_GET((bp)->blk_prop, 40, 8))
285#define	BPE_SET_ETYPE(bp, t)	do { \
286	ASSERT(BP_IS_EMBEDDED(bp)); \
287	BF64_SET((bp)->blk_prop, 40, 8, t); \
288_NOTE(CONSTCOND) } while (0)
289
290#define	BPE_GET_LSIZE(bp)	\
291	(ASSERT(BP_IS_EMBEDDED(bp)), \
292	BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
293#define	BPE_SET_LSIZE(bp, x)	do { \
294	ASSERT(BP_IS_EMBEDDED(bp)); \
295	BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
296_NOTE(CONSTCOND) } while (0)
297
298#define	BPE_GET_PSIZE(bp)	\
299	(ASSERT(BP_IS_EMBEDDED(bp)), \
300	BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
301#define	BPE_SET_PSIZE(bp, x)	do { \
302	ASSERT(BP_IS_EMBEDDED(bp)); \
303	BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
304_NOTE(CONSTCOND) } while (0)
305
306typedef enum bp_embedded_type {
307	BP_EMBEDDED_TYPE_DATA,
308	BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
309	NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
310} bp_embedded_type_t;
311
312#define	BPE_NUM_WORDS 14
313#define	BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
314#define	BPE_IS_PAYLOADWORD(bp, wp) \
315	((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
316
317#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
318#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
319
320/*
321 * A block is a hole when it has either 1) never been written to, or
322 * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
323 * without physically allocating disk space. Holes are represented in the
324 * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
325 * done through the BP_IS_HOLE macro. For holes, the logical size, level,
326 * DMU object type, and birth times are all also stored for holes that
327 * were written to at some point (i.e. were punched after having been filled).
328 */
329typedef struct blkptr {
330	dva_t		blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
331	uint64_t	blk_prop;	/* size, compression, type, etc	    */
332	uint64_t	blk_pad[2];	/* Extra space for the future	    */
333	uint64_t	blk_phys_birth;	/* txg when block was allocated	    */
334	uint64_t	blk_birth;	/* transaction group at birth	    */
335	uint64_t	blk_fill;	/* fill count			    */
336	zio_cksum_t	blk_cksum;	/* 256-bit checksum		    */
337} blkptr_t;
338
339/*
340 * Macros to get and set fields in a bp or DVA.
341 */
342#define	DVA_GET_ASIZE(dva)	\
343	BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
344#define	DVA_SET_ASIZE(dva, x)	\
345	BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
346	SPA_MINBLOCKSHIFT, 0, x)
347
348#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
349#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
350
351#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
352#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
353
354#define	DVA_GET_OFFSET(dva)	\
355	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
356#define	DVA_SET_OFFSET(dva, x)	\
357	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
358
359#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
360#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
361
362#define	BP_GET_LSIZE(bp)	\
363	(BP_IS_EMBEDDED(bp) ?	\
364	(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
365	BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
366#define	BP_SET_LSIZE(bp, x)	do { \
367	ASSERT(!BP_IS_EMBEDDED(bp)); \
368	BF64_SET_SB((bp)->blk_prop, \
369	    0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
370_NOTE(CONSTCOND) } while (0)
371
372#define	BP_GET_PSIZE(bp)	\
373	(BP_IS_EMBEDDED(bp) ? 0 : \
374	BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
375#define	BP_SET_PSIZE(bp, x)	do { \
376	ASSERT(!BP_IS_EMBEDDED(bp)); \
377	BF64_SET_SB((bp)->blk_prop, \
378	    16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
379_NOTE(CONSTCOND) } while (0)
380
381#define	BP_GET_COMPRESS(bp)		\
382	BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
383#define	BP_SET_COMPRESS(bp, x)		\
384	BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
385
386#define	BP_IS_EMBEDDED(bp)		BF64_GET((bp)->blk_prop, 39, 1)
387#define	BP_SET_EMBEDDED(bp, x)		BF64_SET((bp)->blk_prop, 39, 1, x)
388
389#define	BP_GET_CHECKSUM(bp)		\
390	(BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
391	BF64_GET((bp)->blk_prop, 40, 8))
392#define	BP_SET_CHECKSUM(bp, x)		do { \
393	ASSERT(!BP_IS_EMBEDDED(bp)); \
394	BF64_SET((bp)->blk_prop, 40, 8, x); \
395_NOTE(CONSTCOND) } while (0)
396
397#define	BP_GET_TYPE(bp)			BF64_GET((bp)->blk_prop, 48, 8)
398#define	BP_SET_TYPE(bp, x)		BF64_SET((bp)->blk_prop, 48, 8, x)
399
400#define	BP_GET_LEVEL(bp)		BF64_GET((bp)->blk_prop, 56, 5)
401#define	BP_SET_LEVEL(bp, x)		BF64_SET((bp)->blk_prop, 56, 5, x)
402
403#define	BP_GET_DEDUP(bp)		BF64_GET((bp)->blk_prop, 62, 1)
404#define	BP_SET_DEDUP(bp, x)		BF64_SET((bp)->blk_prop, 62, 1, x)
405
406#define	BP_GET_BYTEORDER(bp)		BF64_GET((bp)->blk_prop, 63, 1)
407#define	BP_SET_BYTEORDER(bp, x)		BF64_SET((bp)->blk_prop, 63, 1, x)
408
409#define	BP_PHYSICAL_BIRTH(bp)		\
410	(BP_IS_EMBEDDED(bp) ? 0 : \
411	(bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
412
413#define	BP_SET_BIRTH(bp, logical, physical)	\
414{						\
415	ASSERT(!BP_IS_EMBEDDED(bp));		\
416	(bp)->blk_birth = (logical);		\
417	(bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
418}
419
420#define	BP_GET_FILL(bp) (BP_IS_EMBEDDED(bp) ? 1 : (bp)->blk_fill)
421
422#define	BP_IS_METADATA(bp)	\
423	(BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
424
425#define	BP_GET_ASIZE(bp)	\
426	(BP_IS_EMBEDDED(bp) ? 0 : \
427	DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
428	DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
429	DVA_GET_ASIZE(&(bp)->blk_dva[2]))
430
431#define	BP_GET_UCSIZE(bp)	\
432	(BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
433
434#define	BP_GET_NDVAS(bp)	\
435	(BP_IS_EMBEDDED(bp) ? 0 : \
436	!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
437	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
438	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
439
440#define	BP_COUNT_GANG(bp)	\
441	(BP_IS_EMBEDDED(bp) ? 0 : \
442	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
443	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
444	DVA_GET_GANG(&(bp)->blk_dva[2])))
445
446#define	DVA_EQUAL(dva1, dva2)	\
447	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
448	(dva1)->dva_word[0] == (dva2)->dva_word[0])
449
450#define	BP_EQUAL(bp1, bp2)	\
451	(BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&	\
452	(bp1)->blk_birth == (bp2)->blk_birth &&			\
453	DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&	\
454	DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&	\
455	DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
456
457#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
458	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
459	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
460	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
461	((zc1).zc_word[3] - (zc2).zc_word[3])))
462
463#define	ZIO_CHECKSUM_IS_ZERO(zc) \
464	(0 == ((zc)->zc_word[0] | (zc)->zc_word[1] | \
465	(zc)->zc_word[2] | (zc)->zc_word[3]))
466
467#define	ZIO_CHECKSUM_BSWAP(zcp)					\
468{								\
469	(zcp)->zc_word[0] = BSWAP_64((zcp)->zc_word[0]);	\
470	(zcp)->zc_word[1] = BSWAP_64((zcp)->zc_word[1]);	\
471	(zcp)->zc_word[2] = BSWAP_64((zcp)->zc_word[2]);	\
472	(zcp)->zc_word[3] = BSWAP_64((zcp)->zc_word[3]);	\
473}
474
475
476#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
477
478#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
479{						\
480	(zcp)->zc_word[0] = w0;			\
481	(zcp)->zc_word[1] = w1;			\
482	(zcp)->zc_word[2] = w2;			\
483	(zcp)->zc_word[3] = w3;			\
484}
485
486#define	BP_IDENTITY(bp)		(ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
487#define	BP_IS_GANG(bp)		\
488	(BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
489#define	DVA_IS_EMPTY(dva)	((dva)->dva_word[0] == 0ULL &&	\
490				(dva)->dva_word[1] == 0ULL)
491#define	BP_IS_HOLE(bp) \
492	(!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
493
494/* BP_IS_RAIDZ(bp) assumes no block compression */
495#define	BP_IS_RAIDZ(bp)		(DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
496				BP_GET_PSIZE(bp))
497
498#define	BP_ZERO(bp)				\
499{						\
500	(bp)->blk_dva[0].dva_word[0] = 0;	\
501	(bp)->blk_dva[0].dva_word[1] = 0;	\
502	(bp)->blk_dva[1].dva_word[0] = 0;	\
503	(bp)->blk_dva[1].dva_word[1] = 0;	\
504	(bp)->blk_dva[2].dva_word[0] = 0;	\
505	(bp)->blk_dva[2].dva_word[1] = 0;	\
506	(bp)->blk_prop = 0;			\
507	(bp)->blk_pad[0] = 0;			\
508	(bp)->blk_pad[1] = 0;			\
509	(bp)->blk_phys_birth = 0;		\
510	(bp)->blk_birth = 0;			\
511	(bp)->blk_fill = 0;			\
512	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
513}
514
515#ifdef _BIG_ENDIAN
516#define	ZFS_HOST_BYTEORDER	(0ULL)
517#else
518#define	ZFS_HOST_BYTEORDER	(1ULL)
519#endif
520
521#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
522
523#define	BP_SPRINTF_LEN	320
524
525/*
526 * This macro allows code sharing between zfs, libzpool, and mdb.
527 * 'func' is either snprintf() or mdb_snprintf().
528 * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
529 */
530#define	SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
531{									\
532	static const char *copyname[] =					\
533	    { "zero", "single", "double", "triple" };			\
534	int len = 0;							\
535	int copies = 0;							\
536									\
537	if (bp == NULL) {						\
538		len += func(buf + len, size - len, "<NULL>");		\
539	} else if (BP_IS_HOLE(bp)) {					\
540		len += func(buf + len, size - len,			\
541		    "HOLE [L%llu %s] "					\
542		    "size=%llxL birth=%lluL",				\
543		    (u_longlong_t)BP_GET_LEVEL(bp),			\
544		    type,						\
545		    (u_longlong_t)BP_GET_LSIZE(bp),			\
546		    (u_longlong_t)bp->blk_birth);			\
547	} else if (BP_IS_EMBEDDED(bp)) {				\
548		len = func(buf + len, size - len,			\
549		    "EMBEDDED [L%llu %s] et=%u %s "			\
550		    "size=%llxL/%llxP birth=%lluL",			\
551		    (u_longlong_t)BP_GET_LEVEL(bp),			\
552		    type,						\
553		    (int)BPE_GET_ETYPE(bp),				\
554		    compress,						\
555		    (u_longlong_t)BPE_GET_LSIZE(bp),			\
556		    (u_longlong_t)BPE_GET_PSIZE(bp),			\
557		    (u_longlong_t)bp->blk_birth);			\
558	} else {							\
559		for (int d = 0; d < BP_GET_NDVAS(bp); d++) {		\
560			const dva_t *dva = &bp->blk_dva[d];		\
561			if (DVA_IS_VALID(dva))				\
562				copies++;				\
563			len += func(buf + len, size - len,		\
564			    "DVA[%d]=<%llu:%llx:%llx>%c", d,		\
565			    (u_longlong_t)DVA_GET_VDEV(dva),		\
566			    (u_longlong_t)DVA_GET_OFFSET(dva),		\
567			    (u_longlong_t)DVA_GET_ASIZE(dva),		\
568			    ws);					\
569		}							\
570		if (BP_IS_GANG(bp) &&					\
571		    DVA_GET_ASIZE(&bp->blk_dva[2]) <=			\
572		    DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)			\
573			copies--;					\
574		len += func(buf + len, size - len,			\
575		    "[L%llu %s] %s %s %s %s %s %s%c"			\
576		    "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"	\
577		    "cksum=%llx:%llx:%llx:%llx",			\
578		    (u_longlong_t)BP_GET_LEVEL(bp),			\
579		    type,						\
580		    checksum,						\
581		    compress,						\
582		    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",		\
583		    BP_IS_GANG(bp) ? "gang" : "contiguous",		\
584		    BP_GET_DEDUP(bp) ? "dedup" : "unique",		\
585		    copyname[copies],					\
586		    ws,							\
587		    (u_longlong_t)BP_GET_LSIZE(bp),			\
588		    (u_longlong_t)BP_GET_PSIZE(bp),			\
589		    (u_longlong_t)bp->blk_birth,			\
590		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp),		\
591		    (u_longlong_t)BP_GET_FILL(bp),			\
592		    ws,							\
593		    (u_longlong_t)bp->blk_cksum.zc_word[0],		\
594		    (u_longlong_t)bp->blk_cksum.zc_word[1],		\
595		    (u_longlong_t)bp->blk_cksum.zc_word[2],		\
596		    (u_longlong_t)bp->blk_cksum.zc_word[3]);		\
597	}								\
598	ASSERT(len < size);						\
599}
600
601#define	BP_GET_BUFC_TYPE(bp)						\
602	(BP_IS_METADATA(bp) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
603
604typedef enum spa_import_type {
605	SPA_IMPORT_EXISTING,
606	SPA_IMPORT_ASSEMBLE
607} spa_import_type_t;
608
609/* state manipulation functions */
610extern int spa_open(const char *pool, spa_t **, void *tag);
611extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
612    nvlist_t *policy, nvlist_t **config);
613extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
614    size_t buflen);
615extern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
616    nvlist_t *zplprops);
617extern int spa_import_rootpool(char *devpath, char *devid);
618extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props,
619    uint64_t flags);
620extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
621extern int spa_destroy(char *pool);
622extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
623    boolean_t hardforce);
624extern int spa_reset(char *pool);
625extern void spa_async_request(spa_t *spa, int flag);
626extern void spa_async_unrequest(spa_t *spa, int flag);
627extern void spa_async_suspend(spa_t *spa);
628extern void spa_async_resume(spa_t *spa);
629extern spa_t *spa_inject_addref(char *pool);
630extern void spa_inject_delref(spa_t *spa);
631extern void spa_scan_stat_init(spa_t *spa);
632extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
633
634#define	SPA_ASYNC_CONFIG_UPDATE	0x01
635#define	SPA_ASYNC_REMOVE	0x02
636#define	SPA_ASYNC_PROBE		0x04
637#define	SPA_ASYNC_RESILVER_DONE	0x08
638#define	SPA_ASYNC_RESILVER	0x10
639#define	SPA_ASYNC_AUTOEXPAND	0x20
640#define	SPA_ASYNC_REMOVE_DONE	0x40
641#define	SPA_ASYNC_REMOVE_STOP	0x80
642
643/*
644 * Controls the behavior of spa_vdev_remove().
645 */
646#define	SPA_REMOVE_UNSPARE	0x01
647#define	SPA_REMOVE_DONE		0x02
648
649/* device manipulation */
650extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
651extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
652    int replacing);
653extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
654    int replace_done);
655extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
656extern boolean_t spa_vdev_remove_active(spa_t *spa);
657extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
658extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
659extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
660    nvlist_t *props, boolean_t exp);
661
662/* spare state (which is global across all pools) */
663extern void spa_spare_add(vdev_t *vd);
664extern void spa_spare_remove(vdev_t *vd);
665extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
666extern void spa_spare_activate(vdev_t *vd);
667
668/* L2ARC state (which is global across all pools) */
669extern void spa_l2cache_add(vdev_t *vd);
670extern void spa_l2cache_remove(vdev_t *vd);
671extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
672extern void spa_l2cache_activate(vdev_t *vd);
673extern void spa_l2cache_drop(spa_t *spa);
674
675/* scanning */
676extern int spa_scan(spa_t *spa, pool_scan_func_t func);
677extern int spa_scan_stop(spa_t *spa);
678
679/* spa syncing */
680extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
681extern void spa_sync_allpools(void);
682
683/* spa namespace global mutex */
684extern kmutex_t spa_namespace_lock;
685
686/*
687 * SPA configuration functions in spa_config.c
688 */
689
690#define	SPA_CONFIG_UPDATE_POOL	0
691#define	SPA_CONFIG_UPDATE_VDEVS	1
692
693extern void spa_config_sync(spa_t *, boolean_t, boolean_t);
694extern void spa_config_load(void);
695extern nvlist_t *spa_all_configs(uint64_t *);
696extern void spa_config_set(spa_t *spa, nvlist_t *config);
697extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
698    int getstats);
699extern void spa_config_update(spa_t *spa, int what);
700
701/*
702 * Miscellaneous SPA routines in spa_misc.c
703 */
704
705/* Namespace manipulation */
706extern spa_t *spa_lookup(const char *name);
707extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
708extern void spa_remove(spa_t *spa);
709extern spa_t *spa_next(spa_t *prev);
710
711/* Refcount functions */
712extern void spa_open_ref(spa_t *spa, void *tag);
713extern void spa_close(spa_t *spa, void *tag);
714extern void spa_async_close(spa_t *spa, void *tag);
715extern boolean_t spa_refcount_zero(spa_t *spa);
716
717#define	SCL_NONE	0x00
718#define	SCL_CONFIG	0x01
719#define	SCL_STATE	0x02
720#define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
721#define	SCL_ALLOC	0x08
722#define	SCL_ZIO		0x10
723#define	SCL_FREE	0x20
724#define	SCL_VDEV	0x40
725#define	SCL_LOCKS	7
726#define	SCL_ALL		((1 << SCL_LOCKS) - 1)
727#define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
728
729/* Pool configuration locks */
730extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
731extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
732extern void spa_config_exit(spa_t *spa, int locks, void *tag);
733extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
734
735/* Pool vdev add/remove lock */
736extern uint64_t spa_vdev_enter(spa_t *spa);
737extern uint64_t spa_vdev_config_enter(spa_t *spa);
738extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
739    int error, char *tag);
740extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
741
742/* Pool vdev state change lock */
743extern void spa_vdev_state_enter(spa_t *spa, int oplock);
744extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
745
746/* Log state */
747typedef enum spa_log_state {
748	SPA_LOG_UNKNOWN = 0,	/* unknown log state */
749	SPA_LOG_MISSING,	/* missing log(s) */
750	SPA_LOG_CLEAR,		/* clear the log(s) */
751	SPA_LOG_GOOD,		/* log(s) are good */
752} spa_log_state_t;
753
754extern spa_log_state_t spa_get_log_state(spa_t *spa);
755extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
756extern int spa_offline_log(spa_t *spa);
757
758/* Log claim callback */
759extern void spa_claim_notify(zio_t *zio);
760
761/* Accessor functions */
762extern boolean_t spa_shutting_down(spa_t *spa);
763extern struct dsl_pool *spa_get_dsl(spa_t *spa);
764extern boolean_t spa_is_initializing(spa_t *spa);
765extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
766extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
767extern void spa_altroot(spa_t *, char *, size_t);
768extern int spa_sync_pass(spa_t *spa);
769extern char *spa_name(spa_t *spa);
770extern uint64_t spa_guid(spa_t *spa);
771extern uint64_t spa_load_guid(spa_t *spa);
772extern uint64_t spa_last_synced_txg(spa_t *spa);
773extern uint64_t spa_first_txg(spa_t *spa);
774extern uint64_t spa_syncing_txg(spa_t *spa);
775extern uint64_t spa_final_dirty_txg(spa_t *spa);
776extern uint64_t spa_version(spa_t *spa);
777extern pool_state_t spa_state(spa_t *spa);
778extern spa_load_state_t spa_load_state(spa_t *spa);
779extern uint64_t spa_freeze_txg(spa_t *spa);
780extern uint64_t spa_get_worst_case_asize(spa_t *spa, uint64_t lsize);
781extern uint64_t spa_get_dspace(spa_t *spa);
782extern uint64_t spa_get_slop_space(spa_t *spa);
783extern void spa_update_dspace(spa_t *spa);
784extern uint64_t spa_version(spa_t *spa);
785extern boolean_t spa_deflate(spa_t *spa);
786extern metaslab_class_t *spa_normal_class(spa_t *spa);
787extern metaslab_class_t *spa_log_class(spa_t *spa);
788extern void spa_evicting_os_register(spa_t *, objset_t *os);
789extern void spa_evicting_os_deregister(spa_t *, objset_t *os);
790extern void spa_evicting_os_wait(spa_t *spa);
791extern int spa_max_replication(spa_t *spa);
792extern int spa_prev_software_version(spa_t *spa);
793extern int spa_busy(void);
794extern uint8_t spa_get_failmode(spa_t *spa);
795extern boolean_t spa_suspended(spa_t *spa);
796extern uint64_t spa_bootfs(spa_t *spa);
797extern uint64_t spa_delegation(spa_t *spa);
798extern objset_t *spa_meta_objset(spa_t *spa);
799extern uint64_t spa_deadman_synctime(spa_t *spa);
800
801/* Miscellaneous support routines */
802extern void spa_activate_mos_feature(spa_t *spa, const char *feature,
803    dmu_tx_t *tx);
804extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
805extern int spa_rename(const char *oldname, const char *newname);
806extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
807extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
808extern char *spa_strdup(const char *);
809extern void spa_strfree(char *);
810extern uint64_t spa_get_random(uint64_t range);
811extern uint64_t spa_generate_guid(spa_t *spa);
812extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
813extern void spa_freeze(spa_t *spa);
814extern int spa_change_guid(spa_t *spa);
815extern void spa_upgrade(spa_t *spa, uint64_t version);
816extern void spa_evict_all(void);
817extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
818    boolean_t l2cache);
819extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
820extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
821extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
822extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
823extern boolean_t spa_has_slogs(spa_t *spa);
824extern boolean_t spa_is_root(spa_t *spa);
825extern boolean_t spa_writeable(spa_t *spa);
826extern boolean_t spa_has_pending_synctask(spa_t *spa);
827extern int spa_maxblocksize(spa_t *spa);
828extern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp);
829
830extern int spa_mode(spa_t *spa);
831extern uint64_t strtonum(const char *str, char **nptr);
832
833extern char *spa_his_ievent_table[];
834
835extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
836extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
837    char *his_buf);
838extern int spa_history_log(spa_t *spa, const char *his_buf);
839extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
840extern void spa_history_log_version(spa_t *spa, const char *operation);
841extern void spa_history_log_internal(spa_t *spa, const char *operation,
842    dmu_tx_t *tx, const char *fmt, ...);
843extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
844    dmu_tx_t *tx, const char *fmt, ...);
845extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
846    dmu_tx_t *tx, const char *fmt, ...);
847
848/* error handling */
849struct zbookmark_phys;
850extern void spa_log_error(spa_t *spa, zio_t *zio);
851extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
852    zio_t *zio, uint64_t stateoroffset, uint64_t length);
853extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
854extern void zfs_post_state_change(spa_t *spa, vdev_t *vd);
855extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
856extern uint64_t spa_get_errlog_size(spa_t *spa);
857extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
858extern void spa_errlog_rotate(spa_t *spa);
859extern void spa_errlog_drain(spa_t *spa);
860extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
861extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
862
863/* vdev cache */
864extern void vdev_cache_stat_init(void);
865extern void vdev_cache_stat_fini(void);
866
867/* Initialization and termination */
868extern void spa_init(int flags);
869extern void spa_fini(void);
870extern void spa_boot_init();
871
872/* properties */
873extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
874extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
875extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
876extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
877
878/* asynchronous event notification */
879extern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name);
880
881#ifdef ZFS_DEBUG
882#define	dprintf_bp(bp, fmt, ...) do {				\
883	if (zfs_flags & ZFS_DEBUG_DPRINTF) {			\
884	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
885	snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));	\
886	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
887	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
888	} \
889_NOTE(CONSTCOND) } while (0)
890#else
891#define	dprintf_bp(bp, fmt, ...)
892#endif
893
894extern boolean_t spa_debug_enabled(spa_t *spa);
895#define	spa_dbgmsg(spa, ...)			\
896{						\
897	if (spa_debug_enabled(spa))		\
898		zfs_dbgmsg(__VA_ARGS__);	\
899}
900
901extern int spa_mode_global;			/* mode, e.g. FREAD | FWRITE */
902
903#ifdef	__cplusplus
904}
905#endif
906
907#endif	/* _SYS_SPA_H */
908