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