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