spa.h revision 8ad4d6dd86f5bc65fb3afa566c8133f3bac21648
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/*
2287db74cek * Copyright 2008 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#include <sys/avl.h>
30fa9e406ahrens#include <sys/zfs_context.h>
31fa9e406ahrens#include <sys/nvpair.h>
32fa9e406ahrens#include <sys/sysmacros.h>
33fa9e406ahrens#include <sys/types.h>
34fa9e406ahrens#include <sys/fs/zfs.h>
35fa9e406ahrens
36fa9e406ahrens#ifdef	__cplusplus
37fa9e406ahrensextern "C" {
38fa9e406ahrens#endif
39fa9e406ahrens
40fa9e406ahrens/*
41fa9e406ahrens * Forward references that lots of things need.
42fa9e406ahrens */
43fa9e406ahrenstypedef struct spa spa_t;
44fa9e406ahrenstypedef struct vdev vdev_t;
45fa9e406ahrenstypedef struct metaslab metaslab_t;
46fa9e406ahrenstypedef struct zilog zilog_t;
47fa94a07brendantypedef struct spa_aux_vdev spa_aux_vdev_t;
48fa9e406ahrensstruct dsl_pool;
49fa9e406ahrens
50fa9e406ahrens/*
51fa9e406ahrens * General-purpose 32-bit and 64-bit bitfield encodings.
52fa9e406ahrens */
53fa9e406ahrens#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
54fa9e406ahrens#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
55fa9e406ahrens#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
56fa9e406ahrens#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
57fa9e406ahrens
58fa9e406ahrens#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
59fa9e406ahrens#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
60fa9e406ahrens
61fa9e406ahrens#define	BF32_SET(x, low, len, val)	\
625ad8204nd	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
63fa9e406ahrens#define	BF64_SET(x, low, len, val)	\
645ad8204nd	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
65fa9e406ahrens
66fa9e406ahrens#define	BF32_GET_SB(x, low, len, shift, bias)	\
67fa9e406ahrens	((BF32_GET(x, low, len) + (bias)) << (shift))
68fa9e406ahrens#define	BF64_GET_SB(x, low, len, shift, bias)	\
69fa9e406ahrens	((BF64_GET(x, low, len) + (bias)) << (shift))
70fa9e406ahrens
71fa9e406ahrens#define	BF32_SET_SB(x, low, len, shift, bias, val)	\
72fa9e406ahrens	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
73fa9e406ahrens#define	BF64_SET_SB(x, low, len, shift, bias, val)	\
74fa9e406ahrens	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
75fa9e406ahrens
76fa9e406ahrens/*
77fa9e406ahrens * We currently support nine block sizes, from 512 bytes to 128K.
78fa9e406ahrens * We could go higher, but the benefits are near-zero and the cost
79fa9e406ahrens * of COWing a giant block to modify one byte would become excessive.
80fa9e406ahrens */
81fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
82fa9e406ahrens#define	SPA_MAXBLOCKSHIFT	17
83fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
84fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
85fa9e406ahrens
86fa9e406ahrens#define	SPA_BLOCKSIZES		(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
87fa9e406ahrens
88fa9e406ahrens/*
89f7991baTim Haley * Size of block to hold the configuration data (a packed nvlist)
90f7991baTim Haley */
91f7991baTim Haley#define	SPA_CONFIG_BLOCKSIZE	(1 << 14)
92f7991baTim Haley
93f7991baTim Haley/*
94fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
95fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
96fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
97fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
98fa9e406ahrens * else that expands the ASIZE.
99fa9e406ahrens */
100fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
101fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
102fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
103fa9e406ahrens
104fa9e406ahrens/*
105fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
106fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
107fa9e406ahrens */
108fa9e406ahrenstypedef struct dva {
109fa9e406ahrens	uint64_t	dva_word[2];
110fa9e406ahrens} dva_t;
111fa9e406ahrens
112fa9e406ahrens/*
113fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
114fa9e406ahrens */
115fa9e406ahrenstypedef struct zio_cksum {
116fa9e406ahrens	uint64_t	zc_word[4];
117fa9e406ahrens} zio_cksum_t;
118fa9e406ahrens
119fa9e406ahrens/*
120fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
121fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
122fa9e406ahrens *
123fa9e406ahrens *	64	56	48	40	32	24	16	8	0
124fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
125fa9e406ahrens * 0	|		vdev1		| GRID  |	  ASIZE		|
126fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
127fa9e406ahrens * 1	|G|			 offset1				|
128fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
129fa9e406ahrens * 2	|		vdev2		| GRID  |	  ASIZE		|
130fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
131fa9e406ahrens * 3	|G|			 offset2				|
132fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
133fa9e406ahrens * 4	|		vdev3		| GRID  |	  ASIZE		|
134fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
135fa9e406ahrens * 5	|G|			 offset3				|
136fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
137fa9e406ahrens * 6	|E| lvl | type	| cksum | comp	|     PSIZE	|     LSIZE	|
138fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
139fa9e406ahrens * 7	|			padding					|
140fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
141fa9e406ahrens * 8	|			padding					|
142fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
143fa9e406ahrens * 9	|			padding					|
144fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
145fa9e406ahrens * a	|			birth txg				|
146fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
147fa9e406ahrens * b	|			fill count				|
148fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
149fa9e406ahrens * c	|			checksum[0]				|
150fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
151fa9e406ahrens * d	|			checksum[1]				|
152fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
153fa9e406ahrens * e	|			checksum[2]				|
154fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
155fa9e406ahrens * f	|			checksum[3]				|
156fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
157fa9e406ahrens *
158fa9e406ahrens * Legend:
159fa9e406ahrens *
160fa9e406ahrens * vdev		virtual device ID
161fa9e406ahrens * offset	offset into virtual device
162fa9e406ahrens * LSIZE	logical size
163fa9e406ahrens * PSIZE	physical size (after compression)
164fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
165fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
166fa9e406ahrens * cksum	checksum function
167fa9e406ahrens * comp		compression function
168fa9e406ahrens * G		gang block indicator
169fa9e406ahrens * E		endianness
170fa9e406ahrens * type		DMU object type
171fa9e406ahrens * lvl		level of indirection
172fa9e406ahrens * birth txg	transaction group in which the block was born
173fa9e406ahrens * fill count	number of non-zero blocks under this bp
174fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
175fa9e406ahrens */
176fa9e406ahrenstypedef struct blkptr {
177fa9e406ahrens	dva_t		blk_dva[3];	/* 128-bit Data Virtual Address	*/
178fa9e406ahrens	uint64_t	blk_prop;	/* size, compression, type, etc	*/
179fa9e406ahrens	uint64_t	blk_pad[3];	/* Extra space for the future	*/
180fa9e406ahrens	uint64_t	blk_birth;	/* transaction group at birth	*/
181fa9e406ahrens	uint64_t	blk_fill;	/* fill count			*/
182fa9e406ahrens	zio_cksum_t	blk_cksum;	/* 256-bit checksum		*/
183fa9e406ahrens} blkptr_t;
184fa9e406ahrens
185fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
186fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
187fa9e406ahrens
188fa9e406ahrens/*
189fa9e406ahrens * Macros to get and set fields in a bp or DVA.
190fa9e406ahrens */
191fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
192fa9e406ahrens	BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
193fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
194fa9e406ahrens	BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
195fa9e406ahrens
196fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
197fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
198fa9e406ahrens
199fa9e406ahrens#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
200fa9e406ahrens#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
201fa9e406ahrens
202fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
203fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
204fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
205fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
206fa9e406ahrens
207fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
208fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
209fa9e406ahrens
210fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
211fa9e406ahrens	(BP_IS_HOLE(bp) ? 0 : \
212fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1))
213fa9e406ahrens#define	BP_SET_LSIZE(bp, x)	\
214fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
215fa9e406ahrens
216fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
217fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
218fa9e406ahrens#define	BP_SET_PSIZE(bp, x)	\
219fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
220fa9e406ahrens
221fa9e406ahrens#define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 8)
222fa9e406ahrens#define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 8, x)
223fa9e406ahrens
224fa9e406ahrens#define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
225fa9e406ahrens#define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
226fa9e406ahrens
227fa9e406ahrens#define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
228fa9e406ahrens#define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
229fa9e406ahrens
230fa9e406ahrens#define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
231fa9e406ahrens#define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
232fa9e406ahrens
233fa9e406ahrens#define	BP_GET_BYTEORDER(bp)	(0 - BF64_GET((bp)->blk_prop, 63, 1))
234fa9e406ahrens#define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
235fa9e406ahrens
236fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
237fa9e406ahrens	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
23899653d4eschrock		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
23999653d4eschrock
24099653d4eschrock#define	BP_GET_UCSIZE(bp) \
24199653d4eschrock	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
24299653d4eschrock	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
243fa9e406ahrens
24444cd46cbillm#define	BP_GET_NDVAS(bp)	\
24544cd46cbillm	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
24644cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
24744cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
24844cd46cbillm
24944cd46cbillm#define	BP_COUNT_GANG(bp)	\
25044cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
25144cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
25244cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[2]))
25344cd46cbillm
254fa9e406ahrens#define	DVA_EQUAL(dva1, dva2)	\
255fa9e406ahrens	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
256fa9e406ahrens	(dva1)->dva_word[0] == (dva2)->dva_word[0])
257fa9e406ahrens
2586b4acc8ahrens#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
2596b4acc8ahrens	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
2606b4acc8ahrens	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
2616b4acc8ahrens	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
2626b4acc8ahrens	((zc1).zc_word[3] - (zc2).zc_word[3])))
2636b4acc8ahrens
264fa9e406ahrens#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
265fa9e406ahrens
266fa9e406ahrens#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
267fa9e406ahrens{						\
268fa9e406ahrens	(zcp)->zc_word[0] = w0;			\
269fa9e406ahrens	(zcp)->zc_word[1] = w1;			\
270fa9e406ahrens	(zcp)->zc_word[2] = w2;			\
271fa9e406ahrens	(zcp)->zc_word[3] = w3;			\
272fa9e406ahrens}
273fa9e406ahrens
274fa9e406ahrens#define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
27544cd46cbillm#define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
27644cd46cbillm#define	BP_IS_HOLE(bp)		((bp)->blk_birth == 0)
277c717a56maybee#define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
278fa9e406ahrens
279e14bb32Jeff Bonwick#define	BP_ZERO(bp)				\
280fa9e406ahrens{						\
281fa9e406ahrens	(bp)->blk_dva[0].dva_word[0] = 0;	\
282fa9e406ahrens	(bp)->blk_dva[0].dva_word[1] = 0;	\
283fa9e406ahrens	(bp)->blk_dva[1].dva_word[0] = 0;	\
284fa9e406ahrens	(bp)->blk_dva[1].dva_word[1] = 0;	\
285fa9e406ahrens	(bp)->blk_dva[2].dva_word[0] = 0;	\
286fa9e406ahrens	(bp)->blk_dva[2].dva_word[1] = 0;	\
287fa9e406ahrens	(bp)->blk_prop = 0;			\
288fa9e406ahrens	(bp)->blk_pad[0] = 0;			\
289fa9e406ahrens	(bp)->blk_pad[1] = 0;			\
290fa9e406ahrens	(bp)->blk_pad[2] = 0;			\
291e14bb32Jeff Bonwick	(bp)->blk_birth = 0;			\
292fa9e406ahrens	(bp)->blk_fill = 0;			\
293fa9e406ahrens	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
294fa9e406ahrens}
295fa9e406ahrens
296e14bb32Jeff Bonwick#define	BLK_FILL_ALREADY_FREED	(-1ULL)
297e14bb32Jeff Bonwick
298fa9e406ahrens/*
299fa9e406ahrens * Note: the byteorder is either 0 or -1, both of which are palindromes.
300fa9e406ahrens * This simplifies the endianness handling a bit.
301fa9e406ahrens */
302fa9e406ahrens#ifdef _BIG_ENDIAN
303fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(0ULL)
304fa9e406ahrens#else
305fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(-1ULL)
306fa9e406ahrens#endif
307fa9e406ahrens
308fa9e406ahrens#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
309fa9e406ahrens
31044cd46cbillm#define	BP_SPRINTF_LEN	320
311fbabab8maybee
312fa9e406ahrens#include <sys/dmu.h>
313fa9e406ahrens
314ad23a2djohansen#define	BP_GET_BUFC_TYPE(bp)						\
315ad23a2djohansen	(((BP_GET_LEVEL(bp) > 0) || (dmu_ot[BP_GET_TYPE(bp)].ot_metadata)) ? \
316ad23a2djohansen	ARC_BUFC_METADATA : ARC_BUFC_DATA);
317fa9e406ahrens/*
318fa9e406ahrens * Routines found in spa.c
319fa9e406ahrens */
320fa9e406ahrens
321fa9e406ahrens/* state manipulation functions */
322fa9e406ahrensextern int spa_open(const char *pool, spa_t **, void *tag);
323ea8dc4beschrockextern int spa_get_stats(const char *pool, nvlist_t **config,
324ea8dc4beschrock    char *altroot, size_t buflen);
325990b485llingextern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
3260a48a24timh    const char *history_str, nvlist_t *zplprops);
327051aabetaylorextern int spa_check_rootconf(char *devpath, char *devid,
328051aabetaylor    nvlist_t **bestconf, uint64_t *besttxg);
329e7cbe64gwextern boolean_t spa_rootdev_validate(nvlist_t *nv);
330051aabetaylorextern int spa_import_rootpool(char *devpath, char *devid);
331990b485llingextern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props);
332c5904d1eschrockextern int spa_import_faulted(const char *, nvlist_t *, nvlist_t *);
333fa9e406ahrensextern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
334fa9e406ahrensextern int spa_destroy(char *pool);
335394ab0cGeorge Wilsonextern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
336394ab0cGeorge Wilson    boolean_t hardforce);
337ea8dc4beschrockextern int spa_reset(char *pool);
338ea8dc4beschrockextern void spa_async_request(spa_t *spa, int flag);
339088f389ahrensextern void spa_async_unrequest(spa_t *spa, int flag);
340ea8dc4beschrockextern void spa_async_suspend(spa_t *spa);
341ea8dc4beschrockextern void spa_async_resume(spa_t *spa);
342ea8dc4beschrockextern spa_t *spa_inject_addref(char *pool);
343ea8dc4beschrockextern void spa_inject_delref(spa_t *spa);
344ea8dc4beschrock
345e14bb32Jeff Bonwick#define	SPA_ASYNC_CONFIG_UPDATE	0x01
346e14bb32Jeff Bonwick#define	SPA_ASYNC_REMOVE	0x02
347e14bb32Jeff Bonwick#define	SPA_ASYNC_PROBE		0x04
348e14bb32Jeff Bonwick#define	SPA_ASYNC_RESILVER_DONE	0x08
349e14bb32Jeff Bonwick#define	SPA_ASYNC_RESILVER	0x10
350fa9e406ahrens
351fa9e406ahrens/* device manipulation */
352fa9e406ahrensextern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
353ea8dc4beschrockextern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
354fa9e406ahrens    int replacing);
3558ad4d6dJeff Bonwickextern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
3568ad4d6dJeff Bonwick    int replace_done);
35799653d4eschrockextern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
358c67d967eschrockextern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
359fa9e406ahrens
36099653d4eschrock/* spare state (which is global across all pools) */
36139c2341eschrockextern void spa_spare_add(vdev_t *vd);
36239c2341eschrockextern void spa_spare_remove(vdev_t *vd);
36389a89ebllingextern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
36439c2341eschrockextern void spa_spare_activate(vdev_t *vd);
36599653d4eschrock
366fa94a07brendan/* L2ARC state (which is global across all pools) */
367fa94a07brendanextern void spa_l2cache_add(vdev_t *vd);
368fa94a07brendanextern void spa_l2cache_remove(vdev_t *vd);
369fa94a07brendanextern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
370fa94a07brendanextern void spa_l2cache_activate(vdev_t *vd);
371fa94a07brendanextern void spa_l2cache_drop(spa_t *spa);
372fa94a07brendanextern void spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc);
373fa94a07brendan
374fa9e406ahrens/* scrubbing */
375088f389ahrensextern int spa_scrub(spa_t *spa, pool_scrub_type_t type);
376fa9e406ahrens
377fa9e406ahrens/* spa syncing */
378fa9e406ahrensextern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
379fa9e406ahrensextern void spa_sync_allpools(void);
380fa9e406ahrens
3813a737e0brendan/* spa namespace global mutex */
3823a737e0brendanextern kmutex_t spa_namespace_lock;
3833a737e0brendan
384fa9e406ahrens/*
385fa9e406ahrens * SPA configuration functions in spa_config.c
386fa9e406ahrens */
3870373e76bonwick
3880373e76bonwick#define	SPA_CONFIG_UPDATE_POOL	0
3890373e76bonwick#define	SPA_CONFIG_UPDATE_VDEVS	1
3900373e76bonwick
391c5904d1eschrockextern void spa_config_sync(spa_t *, boolean_t, boolean_t);
392fa9e406ahrensextern void spa_config_load(void);
393fa9e406ahrensextern nvlist_t *spa_all_configs(uint64_t *);
394fa9e406ahrensextern void spa_config_set(spa_t *spa, nvlist_t *config);
395fa9e406ahrensextern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
396fa9e406ahrens    int getstats);
3970373e76bonwickextern void spa_config_update(spa_t *spa, int what);
398e7cbe64gwextern void spa_config_update_common(spa_t *spa, int what, boolean_t isroot);
399fa9e406ahrens
400fa9e406ahrens/*
401fa9e406ahrens * Miscellaneous SPA routines in spa_misc.c
402fa9e406ahrens */
403fa9e406ahrens
404fa9e406ahrens/* Namespace manipulation */
405fa9e406ahrensextern spa_t *spa_lookup(const char *name);
4060373e76bonwickextern spa_t *spa_add(const char *name, const char *altroot);
407fa9e406ahrensextern void spa_remove(spa_t *spa);
408fa9e406ahrensextern spa_t *spa_next(spa_t *prev);
409fa9e406ahrens
410fa9e406ahrens/* Refcount functions */
411fa9e406ahrensextern void spa_open_ref(spa_t *spa, void *tag);
412fa9e406ahrensextern void spa_close(spa_t *spa, void *tag);
413fa9e406ahrensextern boolean_t spa_refcount_zero(spa_t *spa);
414fa9e406ahrens
415e14bb32Jeff Bonwick#define	SCL_CONFIG	0x01
416e14bb32Jeff Bonwick#define	SCL_STATE	0x02
417e14bb32Jeff Bonwick#define	SCL_L2ARC	0x04		/* hack until L2ARC 2.0 */
418e14bb32Jeff Bonwick#define	SCL_ALLOC	0x08
419e14bb32Jeff Bonwick#define	SCL_ZIO		0x10
420e14bb32Jeff Bonwick#define	SCL_FREE	0x20
421e14bb32Jeff Bonwick#define	SCL_VDEV	0x40
422e14bb32Jeff Bonwick#define	SCL_LOCKS	7
423e14bb32Jeff Bonwick#define	SCL_ALL		((1 << SCL_LOCKS) - 1)
424e14bb32Jeff Bonwick#define	SCL_STATE_ALL	(SCL_STATE | SCL_L2ARC | SCL_ZIO)
425e14bb32Jeff Bonwick
426e14bb32Jeff Bonwick/* Pool configuration locks */
427e14bb32Jeff Bonwickextern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
428e14bb32Jeff Bonwickextern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
429e14bb32Jeff Bonwickextern void spa_config_exit(spa_t *spa, int locks, void *tag);
430e14bb32Jeff Bonwickextern int spa_config_held(spa_t *spa, int locks, krw_t rw);
431fa9e406ahrens
432fa9e406ahrens/* Pool vdev add/remove lock */
433fa9e406ahrensextern uint64_t spa_vdev_enter(spa_t *spa);
434fa9e406ahrensextern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
435fa9e406ahrens
436e14bb32Jeff Bonwick/* Pool vdev state change lock */
437e14bb32Jeff Bonwickextern void spa_vdev_state_enter(spa_t *spa);
438e14bb32Jeff Bonwickextern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
439e14bb32Jeff Bonwick
440fa9e406ahrens/* Accessor functions */
44188b7b0fMatthew Ahrensextern boolean_t spa_shutting_down(spa_t *spa);
442fa9e406ahrensextern struct dsl_pool *spa_get_dsl(spa_t *spa);
443fa9e406ahrensextern blkptr_t *spa_get_rootblkptr(spa_t *spa);
444fa9e406ahrensextern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
445fa9e406ahrensextern void spa_altroot(spa_t *, char *, size_t);
446fa9e406ahrensextern int spa_sync_pass(spa_t *spa);
447fa9e406ahrensextern char *spa_name(spa_t *spa);
448fa9e406ahrensextern uint64_t spa_guid(spa_t *spa);
449fa9e406ahrensextern uint64_t spa_last_synced_txg(spa_t *spa);
450fa9e406ahrensextern uint64_t spa_first_txg(spa_t *spa);
45199653d4eschrockextern uint64_t spa_version(spa_t *spa);
45288b7b0fMatthew Ahrensextern pool_state_t spa_state(spa_t *spa);
453fa9e406ahrensextern uint64_t spa_freeze_txg(spa_t *spa);
454fa9e406ahrensextern uint64_t spa_get_alloc(spa_t *spa);
455fa9e406ahrensextern uint64_t spa_get_space(spa_t *spa);
45699653d4eschrockextern uint64_t spa_get_dspace(spa_t *spa);
457fa9e406ahrensextern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
45844cd46cbillmextern uint64_t spa_version(spa_t *spa);
45944cd46cbillmextern int spa_max_replication(spa_t *spa);
460fa9e406ahrensextern int spa_busy(void);
4610a4e951gwextern uint8_t spa_get_failmode(spa_t *spa);
462e14bb32Jeff Bonwickextern boolean_t spa_suspended(spa_t *spa);
463fa9e406ahrens
464fa9e406ahrens/* Miscellaneous support routines */
465fa9e406ahrensextern int spa_rename(const char *oldname, const char *newname);
466fa9e406ahrensextern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
467fa9e406ahrensextern char *spa_strdup(const char *);
468fa9e406ahrensextern void spa_strfree(char *);
469fa9e406ahrensextern uint64_t spa_get_random(uint64_t range);
470d80c45ebonwickextern void sprintf_blkptr(char *buf, int len, const blkptr_t *bp);
471fa9e406ahrensextern void spa_freeze(spa_t *spa);
472990b485llingextern void spa_upgrade(spa_t *spa, uint64_t version);
473fa9e406ahrensextern void spa_evict_all(void);
474c5904d1eschrockextern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
475c5904d1eschrock    boolean_t l2cache);
47699653d4eschrockextern boolean_t spa_has_spare(spa_t *, uint64_t guid);
47799653d4eschrockextern uint64_t bp_get_dasize(spa_t *spa, const blkptr_t *bp);
4786ce0521perrinextern boolean_t spa_has_slogs(spa_t *spa);
479bf82a41eschrockextern boolean_t spa_is_root(spa_t *spa);
4808ad4d6dJeff Bonwickextern boolean_t spa_writeable(spa_t *spa);
4818ad4d6dJeff Bonwickextern int spa_mode(spa_t *spa);
482ea8dc4beschrock
48306eeb2aek/* history logging */
484ecd6cf8markstypedef enum history_log_type {
485ecd6cf8marks	LOG_CMD_POOL_CREATE,
486ecd6cf8marks	LOG_CMD_NORMAL,
487ecd6cf8marks	LOG_INTERNAL
488ecd6cf8marks} history_log_type_t;
489ecd6cf8marks
490ecd6cf8markstypedef struct history_arg {
491ecd6cf8marks	const char *ha_history_str;
492ecd6cf8marks	history_log_type_t ha_log_type;
493ecd6cf8marks	history_internal_events_t ha_event;
494ecd6cf8marks	char ha_zone[MAXPATHLEN];
495ecd6cf8marks} history_arg_t;
496ecd6cf8marks
497ecd6cf8marksextern char *spa_his_ievent_table[];
498ecd6cf8marks
49906eeb2aekextern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
50006eeb2aekextern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
50106eeb2aek    char *his_buf);
50206eeb2aekextern int spa_history_log(spa_t *spa, const char *his_buf,
503ecd6cf8marks    history_log_type_t what);
504ecd6cf8marksvoid spa_history_internal_log(history_internal_events_t event, spa_t *spa,
505ecd6cf8marks    dmu_tx_t *tx, cred_t *cr, const char *fmt, ...);
50606eeb2aek
507ea8dc4beschrock/* error handling */
508ea8dc4beschrockstruct zbookmark;
509ea8dc4beschrockstruct zio;
510ea8dc4beschrockextern void spa_log_error(spa_t *spa, struct zio *zio);
511ea8dc4beschrockextern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
512ea8dc4beschrock    struct zio *zio, uint64_t stateoroffset, uint64_t length);
5133d7072feschrockextern void zfs_post_remove(spa_t *spa, vdev_t *vd);
5143d7072feschrockextern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
515ea8dc4beschrockextern uint64_t spa_get_errlog_size(spa_t *spa);
516ea8dc4beschrockextern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
517ea8dc4beschrockextern void spa_errlog_rotate(spa_t *spa);
518ea8dc4beschrockextern void spa_errlog_drain(spa_t *spa);
519ea8dc4beschrockextern void spa_errlog_sync(spa_t *spa, uint64_t txg);
520ea8dc4beschrockextern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
521fa9e406ahrens
52287db74cek/* vdev cache */
52387db74cekextern void vdev_cache_stat_init(void);
52487db74cekextern void vdev_cache_stat_fini(void);
52587db74cek
526fa9e406ahrens/* Initialization and termination */
527fa9e406ahrensextern void spa_init(int flags);
528fa9e406ahrensextern void spa_fini(void);
529e7cbe64gwextern void spa_boot_init();
530fa9e406ahrens
531b1b8ab3lling/* properties */
532990b485llingextern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
533990b485llingextern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
534990b485llingextern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
535b1b8ab3lling
5363d7072feschrock/* asynchronous event notification */
5373d7072feschrockextern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name);
5383d7072feschrock
539fa9e406ahrens#ifdef ZFS_DEBUG
540c0a8126ek#define	dprintf_bp(bp, fmt, ...) do {				\
541c0a8126ek	if (zfs_flags & ZFS_DEBUG_DPRINTF) { 			\
542c0a8126ek	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
543c0a8126ek	sprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));		\
544c0a8126ek	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
545c0a8126ek	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
546fa9e406ahrens	} \
547fa9e406ahrens_NOTE(CONSTCOND) } while (0)
548fa9e406ahrens#else
549fa9e406ahrens#define	dprintf_bp(bp, fmt, ...)
550fa9e406ahrens#endif
551fa9e406ahrens
5528ad4d6dJeff Bonwickextern int spa_mode_global;			/* mode, e.g. FREAD | FWRITE */
553fa9e406ahrens
554fa9e406ahrens#ifdef	__cplusplus
555fa9e406ahrens}
556fa9e406ahrens#endif
557fa9e406ahrens
558fa9e406ahrens#endif	/* _SYS_SPA_H */
559