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