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