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