spa.h revision ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
22c67d967eschrock * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens#ifndef _SYS_SPA_H
27fa9e406ahrens#define	_SYS_SPA_H
28fa9e406ahrens
29fa9e406ahrens#pragma ident	"%Z%%M%	%I%	%E% SMI"
30fa9e406ahrens
31fa9e406ahrens#include <sys/avl.h>
32fa9e406ahrens#include <sys/zfs_context.h>
33fa9e406ahrens#include <sys/nvpair.h>
34fa9e406ahrens#include <sys/sysmacros.h>
35fa9e406ahrens#include <sys/types.h>
36fa9e406ahrens#include <sys/fs/zfs.h>
37fa9e406ahrens
38fa9e406ahrens#ifdef	__cplusplus
39fa9e406ahrensextern "C" {
40fa9e406ahrens#endif
41fa9e406ahrens
42fa9e406ahrens/*
43fa9e406ahrens * Forward references that lots of things need.
44fa9e406ahrens */
45fa9e406ahrenstypedef struct spa spa_t;
46fa9e406ahrenstypedef struct vdev vdev_t;
47fa9e406ahrenstypedef struct metaslab metaslab_t;
48fa9e406ahrenstypedef struct zilog zilog_t;
49fa9e406ahrenstypedef struct traverse_handle traverse_handle_t;
50fa9e406ahrensstruct dsl_pool;
51fa9e406ahrens
52fa9e406ahrens/*
53fa9e406ahrens * General-purpose 32-bit and 64-bit bitfield encodings.
54fa9e406ahrens */
55fa9e406ahrens#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
56fa9e406ahrens#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
57fa9e406ahrens#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
58fa9e406ahrens#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
59fa9e406ahrens
60fa9e406ahrens#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
61fa9e406ahrens#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
62fa9e406ahrens
63fa9e406ahrens#define	BF32_SET(x, low, len, val)	\
645ad8204nd	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
65fa9e406ahrens#define	BF64_SET(x, low, len, val)	\
665ad8204nd	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
67fa9e406ahrens
68fa9e406ahrens#define	BF32_GET_SB(x, low, len, shift, bias)	\
69fa9e406ahrens	((BF32_GET(x, low, len) + (bias)) << (shift))
70fa9e406ahrens#define	BF64_GET_SB(x, low, len, shift, bias)	\
71fa9e406ahrens	((BF64_GET(x, low, len) + (bias)) << (shift))
72fa9e406ahrens
73fa9e406ahrens#define	BF32_SET_SB(x, low, len, shift, bias, val)	\
74fa9e406ahrens	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
75fa9e406ahrens#define	BF64_SET_SB(x, low, len, shift, bias, val)	\
76fa9e406ahrens	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
77fa9e406ahrens
78fa9e406ahrens/*
79fa9e406ahrens * We currently support nine block sizes, from 512 bytes to 128K.
80fa9e406ahrens * We could go higher, but the benefits are near-zero and the cost
81fa9e406ahrens * of COWing a giant block to modify one byte would become excessive.
82fa9e406ahrens */
83fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
84fa9e406ahrens#define	SPA_MAXBLOCKSHIFT	17
85fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
86fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
87fa9e406ahrens
88fa9e406ahrens#define	SPA_BLOCKSIZES		(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
89fa9e406ahrens
90fa9e406ahrens/*
91fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
92fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
93fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
94fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
95fa9e406ahrens * else that expands the ASIZE.
96fa9e406ahrens */
97fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
98fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
99fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
100fa9e406ahrens
101fa9e406ahrens/*
102fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
103fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
104fa9e406ahrens */
105fa9e406ahrenstypedef struct dva {
106fa9e406ahrens	uint64_t	dva_word[2];
107fa9e406ahrens} dva_t;
108fa9e406ahrens
109fa9e406ahrens/*
110fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
111fa9e406ahrens */
112fa9e406ahrenstypedef struct zio_cksum {
113fa9e406ahrens	uint64_t	zc_word[4];
114fa9e406ahrens} zio_cksum_t;
115fa9e406ahrens
116fa9e406ahrens/*
117fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
118fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
119fa9e406ahrens *
120fa9e406ahrens *	64	56	48	40	32	24	16	8	0
121fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
122fa9e406ahrens * 0	|		vdev1		| GRID  |	  ASIZE		|
123fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
124fa9e406ahrens * 1	|G|			 offset1				|
125fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
126fa9e406ahrens * 2	|		vdev2		| GRID  |	  ASIZE		|
127fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
128fa9e406ahrens * 3	|G|			 offset2				|
129fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
130fa9e406ahrens * 4	|		vdev3		| GRID  |	  ASIZE		|
131fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
132fa9e406ahrens * 5	|G|			 offset3				|
133fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
134fa9e406ahrens * 6	|E| lvl | type	| cksum | comp	|     PSIZE	|     LSIZE	|
135fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
136fa9e406ahrens * 7	|			padding					|
137fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
138fa9e406ahrens * 8	|			padding					|
139fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
140fa9e406ahrens * 9	|			padding					|
141fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
142fa9e406ahrens * a	|			birth txg				|
143fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
144fa9e406ahrens * b	|			fill count				|
145fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
146fa9e406ahrens * c	|			checksum[0]				|
147fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
148fa9e406ahrens * d	|			checksum[1]				|
149fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
150fa9e406ahrens * e	|			checksum[2]				|
151fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
152fa9e406ahrens * f	|			checksum[3]				|
153fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
154fa9e406ahrens *
155fa9e406ahrens * Legend:
156fa9e406ahrens *
157fa9e406ahrens * vdev		virtual device ID
158fa9e406ahrens * offset	offset into virtual device
159fa9e406ahrens * LSIZE	logical size
160fa9e406ahrens * PSIZE	physical size (after compression)
161fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
162fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
163fa9e406ahrens * cksum	checksum function
164fa9e406ahrens * comp		compression function
165fa9e406ahrens * G		gang block indicator
166fa9e406ahrens * E		endianness
167fa9e406ahrens * type		DMU object type
168fa9e406ahrens * lvl		level of indirection
169fa9e406ahrens * birth txg	transaction group in which the block was born
170fa9e406ahrens * fill count	number of non-zero blocks under this bp
171fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
172fa9e406ahrens */
173fa9e406ahrenstypedef struct blkptr {
174fa9e406ahrens	dva_t		blk_dva[3];	/* 128-bit Data Virtual Address	*/
175fa9e406ahrens	uint64_t	blk_prop;	/* size, compression, type, etc	*/
176fa9e406ahrens	uint64_t	blk_pad[3];	/* Extra space for the future	*/
177fa9e406ahrens	uint64_t	blk_birth;	/* transaction group at birth	*/
178fa9e406ahrens	uint64_t	blk_fill;	/* fill count			*/
179fa9e406ahrens	zio_cksum_t	blk_cksum;	/* 256-bit checksum		*/
180fa9e406ahrens} blkptr_t;
181fa9e406ahrens
182fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
183fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
184fa9e406ahrens
185fa9e406ahrens/*
186fa9e406ahrens * Macros to get and set fields in a bp or DVA.
187fa9e406ahrens */
188fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
189fa9e406ahrens	BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
190fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
191fa9e406ahrens	BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
192fa9e406ahrens
193fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
194fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
195fa9e406ahrens
196fa9e406ahrens#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
197fa9e406ahrens#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
198fa9e406ahrens
199fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
200fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
201fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
202fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
203fa9e406ahrens
204fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
205fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
206fa9e406ahrens
207fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
208fa9e406ahrens	(BP_IS_HOLE(bp) ? 0 : \
209fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1))
210fa9e406ahrens#define	BP_SET_LSIZE(bp, x)	\
211fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
212fa9e406ahrens
213fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
214fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
215fa9e406ahrens#define	BP_SET_PSIZE(bp, x)	\
216fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
217fa9e406ahrens
218fa9e406ahrens#define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 8)
219fa9e406ahrens#define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 8, x)
220fa9e406ahrens
221fa9e406ahrens#define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
222fa9e406ahrens#define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
223fa9e406ahrens
224fa9e406ahrens#define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
225fa9e406ahrens#define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
226fa9e406ahrens
227fa9e406ahrens#define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
228fa9e406ahrens#define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
229fa9e406ahrens
230fa9e406ahrens#define	BP_GET_BYTEORDER(bp)	(0 - BF64_GET((bp)->blk_prop, 63, 1))
231fa9e406ahrens#define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
232fa9e406ahrens
233fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
234fa9e406ahrens	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
23599653d4eschrock		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
23699653d4eschrock
23799653d4eschrock#define	BP_GET_UCSIZE(bp) \
23899653d4eschrock	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
23999653d4eschrock	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
240fa9e406ahrens
24144cd46cbillm#define	BP_GET_NDVAS(bp)	\
24244cd46cbillm	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
24344cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
24444cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
24544cd46cbillm
24644cd46cbillm#define	BP_COUNT_GANG(bp)	\
24744cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
24844cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
24944cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[2]))
25044cd46cbillm
251fa9e406ahrens#define	DVA_EQUAL(dva1, dva2)	\
252fa9e406ahrens	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
253fa9e406ahrens	(dva1)->dva_word[0] == (dva2)->dva_word[0])
254fa9e406ahrens
2556b4acc8ahrens#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
2566b4acc8ahrens	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
2576b4acc8ahrens	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
2586b4acc8ahrens	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
2596b4acc8ahrens	((zc1).zc_word[3] - (zc2).zc_word[3])))
2606b4acc8ahrens
2616b4acc8ahrens
262fa9e406ahrens#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
263fa9e406ahrens
264fa9e406ahrens#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
265fa9e406ahrens{						\
266fa9e406ahrens	(zcp)->zc_word[0] = w0;			\
267fa9e406ahrens	(zcp)->zc_word[1] = w1;			\
268fa9e406ahrens	(zcp)->zc_word[2] = w2;			\
269fa9e406ahrens	(zcp)->zc_word[3] = w3;			\
270fa9e406ahrens}
271fa9e406ahrens
272fa9e406ahrens#define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
27344cd46cbillm#define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
27444cd46cbillm#define	BP_IS_HOLE(bp)		((bp)->blk_birth == 0)
275fa9e406ahrens
276fa9e406ahrens#define	BP_ZERO(bp)				\
277fa9e406ahrens{						\
278fa9e406ahrens	(bp)->blk_dva[0].dva_word[0] = 0;	\
279fa9e406ahrens	(bp)->blk_dva[0].dva_word[1] = 0;	\
280fa9e406ahrens	(bp)->blk_dva[1].dva_word[0] = 0;	\
281fa9e406ahrens	(bp)->blk_dva[1].dva_word[1] = 0;	\
282fa9e406ahrens	(bp)->blk_dva[2].dva_word[0] = 0;	\
283fa9e406ahrens	(bp)->blk_dva[2].dva_word[1] = 0;	\
284fa9e406ahrens	(bp)->blk_prop = 0;			\
285fa9e406ahrens	(bp)->blk_pad[0] = 0;			\
286fa9e406ahrens	(bp)->blk_pad[1] = 0;			\
287fa9e406ahrens	(bp)->blk_pad[2] = 0;			\
288fa9e406ahrens	(bp)->blk_birth = 0;			\
289fa9e406ahrens	(bp)->blk_fill = 0;			\
290fa9e406ahrens	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
291fa9e406ahrens}
292fa9e406ahrens
293fa9e406ahrens/*
294fa9e406ahrens * Note: the byteorder is either 0 or -1, both of which are palindromes.
295fa9e406ahrens * This simplifies the endianness handling a bit.
296fa9e406ahrens */
297fa9e406ahrens#ifdef _BIG_ENDIAN
298fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(0ULL)
299fa9e406ahrens#else
300fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(-1ULL)
301fa9e406ahrens#endif
302fa9e406ahrens
303fa9e406ahrens#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
304fa9e406ahrens
30544cd46cbillm#define	BP_SPRINTF_LEN	320
306fbabab8maybee
307fa9e406ahrens#include <sys/dmu.h>
308fa9e406ahrens
309ad23a2djohansen#define	BP_GET_BUFC_TYPE(bp)						\
310ad23a2djohansen	(((BP_GET_LEVEL(bp) > 0) || (dmu_ot[BP_GET_TYPE(bp)].ot_metadata)) ? \
311ad23a2djohansen	ARC_BUFC_METADATA : ARC_BUFC_DATA);
312fa9e406ahrens/*
313fa9e406ahrens * Routines found in spa.c
314fa9e406ahrens */
315fa9e406ahrens
316fa9e406ahrens/* state manipulation functions */
317fa9e406ahrensextern int spa_open(const char *pool, spa_t **, void *tag);
318ea8dc4beschrockextern int spa_get_stats(const char *pool, nvlist_t **config,
319ea8dc4beschrock    char *altroot, size_t buflen);
3200373e76bonwickextern int spa_create(const char *pool, nvlist_t *config, const char *altroot);
3210373e76bonwickextern int spa_import(const char *pool, nvlist_t *config, const char *altroot);
322fa9e406ahrensextern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
323fa9e406ahrensextern int spa_destroy(char *pool);
32444cd46cbillmextern int spa_export(char *pool, nvlist_t **oldconfig);
325ea8dc4beschrockextern int spa_reset(char *pool);
326ea8dc4beschrockextern void spa_async_request(spa_t *spa, int flag);
327ea8dc4beschrockextern void spa_async_suspend(spa_t *spa);
328ea8dc4beschrockextern void spa_async_resume(spa_t *spa);
329ea8dc4beschrockextern spa_t *spa_inject_addref(char *pool);
330ea8dc4beschrockextern void spa_inject_delref(spa_t *spa);
331ea8dc4beschrock
332ea8dc4beschrock#define	SPA_ASYNC_REOPEN	0x01
333ea8dc4beschrock#define	SPA_ASYNC_REPLACE_DONE	0x02
334ea8dc4beschrock#define	SPA_ASYNC_SCRUB		0x04
335ea8dc4beschrock#define	SPA_ASYNC_RESILVER	0x08
3360373e76bonwick#define	SPA_ASYNC_CONFIG_UPDATE	0x10
337fa9e406ahrens
338fa9e406ahrens/* device manipulation */
339fa9e406ahrensextern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
340ea8dc4beschrockextern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
341fa9e406ahrens    int replacing);
342ea8dc4beschrockextern int spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done);
34399653d4eschrockextern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
344c67d967eschrockextern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
345fa9e406ahrens
34699653d4eschrock/* spare state (which is global across all pools) */
34799653d4eschrockextern void spa_spare_add(uint64_t guid);
34899653d4eschrockextern void spa_spare_remove(uint64_t guid);
34999653d4eschrockextern boolean_t spa_spare_inuse(uint64_t guid);
35099653d4eschrock
351fa9e406ahrens/* scrubbing */
352fa9e406ahrensextern int spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force);
353fa9e406ahrensextern void spa_scrub_suspend(spa_t *spa);
354fa9e406ahrensextern void spa_scrub_resume(spa_t *spa);
355fa9e406ahrensextern void spa_scrub_restart(spa_t *spa, uint64_t txg);
356ea8dc4beschrockextern void spa_scrub_throttle(spa_t *spa, int direction);
357fa9e406ahrens
358fa9e406ahrens/* spa syncing */
359fa9e406ahrensextern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
360fa9e406ahrensextern void spa_sync_allpools(void);
361fa9e406ahrens
362fa9e406ahrens/*
363fa9e406ahrens * SPA configuration functions in spa_config.c
364fa9e406ahrens */
3650373e76bonwick
3660373e76bonwick#define	SPA_CONFIG_UPDATE_POOL	0
3670373e76bonwick#define	SPA_CONFIG_UPDATE_VDEVS	1
3680373e76bonwick
369fa9e406ahrensextern void spa_config_sync(void);
370fa9e406ahrensextern void spa_config_load(void);
371fa9e406ahrensextern nvlist_t *spa_all_configs(uint64_t *);
372fa9e406ahrensextern void spa_config_set(spa_t *spa, nvlist_t *config);
373fa9e406ahrensextern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
374fa9e406ahrens    int getstats);
3750373e76bonwickextern void spa_config_update(spa_t *spa, int what);
376fa9e406ahrens
377fa9e406ahrens/*
378fa9e406ahrens * Miscellaneous SPA routines in spa_misc.c
379fa9e406ahrens */
380fa9e406ahrens
381fa9e406ahrens/* Namespace manipulation */
382fa9e406ahrensextern spa_t *spa_lookup(const char *name);
3830373e76bonwickextern spa_t *spa_add(const char *name, const char *altroot);
384fa9e406ahrensextern void spa_remove(spa_t *spa);
385fa9e406ahrensextern spa_t *spa_next(spa_t *prev);
386fa9e406ahrens
387fa9e406ahrens/* Refcount functions */
388fa9e406ahrensextern void spa_open_ref(spa_t *spa, void *tag);
389fa9e406ahrensextern void spa_close(spa_t *spa, void *tag);
390fa9e406ahrensextern boolean_t spa_refcount_zero(spa_t *spa);
391fa9e406ahrens
392fa9e406ahrens/* Pool configuration lock */
393ea8dc4beschrockextern void spa_config_enter(spa_t *spa, krw_t rw, void *tag);
394ea8dc4beschrockextern void spa_config_exit(spa_t *spa, void *tag);
395fa9e406ahrensextern boolean_t spa_config_held(spa_t *spa, krw_t rw);
396fa9e406ahrens
397fa9e406ahrens/* Pool vdev add/remove lock */
398fa9e406ahrensextern uint64_t spa_vdev_enter(spa_t *spa);
399fa9e406ahrensextern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
400fa9e406ahrens
401fa9e406ahrens/* Accessor functions */
402fa9e406ahrensextern krwlock_t *spa_traverse_rwlock(spa_t *spa);
403fa9e406ahrensextern int spa_traverse_wanted(spa_t *spa);
404fa9e406ahrensextern struct dsl_pool *spa_get_dsl(spa_t *spa);
405fa9e406ahrensextern blkptr_t *spa_get_rootblkptr(spa_t *spa);
406fa9e406ahrensextern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
407fa9e406ahrensextern void spa_altroot(spa_t *, char *, size_t);
408fa9e406ahrensextern int spa_sync_pass(spa_t *spa);
409fa9e406ahrensextern char *spa_name(spa_t *spa);
410fa9e406ahrensextern uint64_t spa_guid(spa_t *spa);
411fa9e406ahrensextern uint64_t spa_last_synced_txg(spa_t *spa);
412fa9e406ahrensextern uint64_t spa_first_txg(spa_t *spa);
41399653d4eschrockextern uint64_t spa_version(spa_t *spa);
414fa9e406ahrensextern int spa_state(spa_t *spa);
415fa9e406ahrensextern uint64_t spa_freeze_txg(spa_t *spa);
416fa9e406ahrensstruct metaslab_class;
417fa9e406ahrensextern struct metaslab_class *spa_metaslab_class_select(spa_t *spa);
418fa9e406ahrensextern uint64_t spa_get_alloc(spa_t *spa);
419fa9e406ahrensextern uint64_t spa_get_space(spa_t *spa);
42099653d4eschrockextern uint64_t spa_get_dspace(spa_t *spa);
421fa9e406ahrensextern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
42244cd46cbillmextern uint64_t spa_version(spa_t *spa);
42344cd46cbillmextern int spa_max_replication(spa_t *spa);
424fa9e406ahrensextern int spa_busy(void);
425fa9e406ahrens
426fa9e406ahrens/* Miscellaneous support routines */
427fa9e406ahrensextern int spa_rename(const char *oldname, const char *newname);
428fa9e406ahrensextern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
429fa9e406ahrensextern char *spa_strdup(const char *);
430fa9e406ahrensextern void spa_strfree(char *);
431fa9e406ahrensextern uint64_t spa_get_random(uint64_t range);
432d80c45ebonwickextern void sprintf_blkptr(char *buf, int len, const blkptr_t *bp);
433fa9e406ahrensextern void spa_freeze(spa_t *spa);
434eaca9bbeschrockextern void spa_upgrade(spa_t *spa);
435fa9e406ahrensextern void spa_evict_all(void);
436ea8dc4beschrockextern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid);
43799653d4eschrockextern boolean_t spa_has_spare(spa_t *, uint64_t guid);
43899653d4eschrockextern uint64_t bp_get_dasize(spa_t *spa, const blkptr_t *bp);
439ea8dc4beschrock
44006eeb2aek/* history logging */
44106eeb2aekextern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
44206eeb2aekextern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
44306eeb2aek    char *his_buf);
44406eeb2aekextern int spa_history_log(spa_t *spa, const char *his_buf,
44506eeb2aek    uint64_t pool_create);
44606eeb2aek
447ea8dc4beschrock/* error handling */
448ea8dc4beschrockstruct zbookmark;
449ea8dc4beschrockstruct zio;
450ea8dc4beschrockextern void spa_log_error(spa_t *spa, struct zio *zio);
451ea8dc4beschrockextern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
452ea8dc4beschrock    struct zio *zio, uint64_t stateoroffset, uint64_t length);
453ea8dc4beschrockextern void zfs_post_ok(spa_t *spa, vdev_t *vd);
454ea8dc4beschrockextern uint64_t spa_get_errlog_size(spa_t *spa);
455ea8dc4beschrockextern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
456ea8dc4beschrockextern void spa_errlog_rotate(spa_t *spa);
457ea8dc4beschrockextern void spa_errlog_drain(spa_t *spa);
458ea8dc4beschrockextern void spa_errlog_sync(spa_t *spa, uint64_t txg);
459e9dbad6eschrockextern int spa_bookmark_name(spa_t *spa, struct zbookmark *zb,
460e9dbad6eschrock    nvlist_t *nvl);
461ea8dc4beschrockextern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
462fa9e406ahrens
463fa9e406ahrens/* Initialization and termination */
464fa9e406ahrensextern void spa_init(int flags);
465fa9e406ahrensextern void spa_fini(void);
466fa9e406ahrens
467fa9e406ahrens#ifdef ZFS_DEBUG
468fa9e406ahrens#define	dprintf_bp(bp, fmt, ...) do {			\
469fbabab8maybee	if (zfs_flags & ZFS_DEBUG_DPRINTF) { 		\
470fbabab8maybee	char __blkbuf[BP_SPRINTF_LEN];			\
471fbabab8maybee	sprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));	\
472fa9e406ahrens	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);	\
473fa9e406ahrens	} \
474fa9e406ahrens_NOTE(CONSTCOND) } while (0)
475fa9e406ahrens#else
476fa9e406ahrens#define	dprintf_bp(bp, fmt, ...)
477fa9e406ahrens#endif
478fa9e406ahrens
479fa9e406ahrensextern int spa_mode;			/* mode, e.g. FREAD | FWRITE */
480fa9e406ahrens
481fa9e406ahrens#ifdef	__cplusplus
482fa9e406ahrens}
483fa9e406ahrens#endif
484fa9e406ahrens
485fa9e406ahrens#endif	/* _SYS_SPA_H */
486