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