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