spa.h revision f7991ba49ef5aba7b9cb3c356205d10b753dbbc8
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;
47fa9e406ahrenstypedef struct traverse_handle traverse_handle_t;
48fa94a07brendantypedef struct spa_aux_vdev spa_aux_vdev_t;
49fa9e406ahrensstruct dsl_pool;
50fa9e406ahrens
51fa9e406ahrens/*
52fa9e406ahrens * General-purpose 32-bit and 64-bit bitfield encodings.
53fa9e406ahrens */
54fa9e406ahrens#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
55fa9e406ahrens#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
56fa9e406ahrens#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
57fa9e406ahrens#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
58fa9e406ahrens
59fa9e406ahrens#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
60fa9e406ahrens#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
61fa9e406ahrens
62fa9e406ahrens#define	BF32_SET(x, low, len, val)	\
635ad8204nd	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
64fa9e406ahrens#define	BF64_SET(x, low, len, val)	\
655ad8204nd	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
66fa9e406ahrens
67fa9e406ahrens#define	BF32_GET_SB(x, low, len, shift, bias)	\
68fa9e406ahrens	((BF32_GET(x, low, len) + (bias)) << (shift))
69fa9e406ahrens#define	BF64_GET_SB(x, low, len, shift, bias)	\
70fa9e406ahrens	((BF64_GET(x, low, len) + (bias)) << (shift))
71fa9e406ahrens
72fa9e406ahrens#define	BF32_SET_SB(x, low, len, shift, bias, val)	\
73fa9e406ahrens	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
74fa9e406ahrens#define	BF64_SET_SB(x, low, len, shift, bias, val)	\
75fa9e406ahrens	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
76fa9e406ahrens
77fa9e406ahrens/*
78fa9e406ahrens * We currently support nine block sizes, from 512 bytes to 128K.
79fa9e406ahrens * We could go higher, but the benefits are near-zero and the cost
80fa9e406ahrens * of COWing a giant block to modify one byte would become excessive.
81fa9e406ahrens */
82fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
83fa9e406ahrens#define	SPA_MAXBLOCKSHIFT	17
84fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
85fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
86fa9e406ahrens
87fa9e406ahrens#define	SPA_BLOCKSIZES		(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
88fa9e406ahrens
89fa9e406ahrens/*
90f7991baTim Haley * Size of block to hold the configuration data (a packed nvlist)
91f7991baTim Haley */
92f7991baTim Haley#define	SPA_CONFIG_BLOCKSIZE	(1 << 14)
93f7991baTim Haley
94f7991baTim Haley/*
95fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
96fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
97fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
98fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
99fa9e406ahrens * else that expands the ASIZE.
100fa9e406ahrens */
101fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
102fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
103fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
104fa9e406ahrens
105fa9e406ahrens/*
106fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
107fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
108fa9e406ahrens */
109fa9e406ahrenstypedef struct dva {
110fa9e406ahrens	uint64_t	dva_word[2];
111fa9e406ahrens} dva_t;
112fa9e406ahrens
113fa9e406ahrens/*
114fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
115fa9e406ahrens */
116fa9e406ahrenstypedef struct zio_cksum {
117fa9e406ahrens	uint64_t	zc_word[4];
118fa9e406ahrens} zio_cksum_t;
119fa9e406ahrens
120fa9e406ahrens/*
121fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
122fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
123fa9e406ahrens *
124fa9e406ahrens *	64	56	48	40	32	24	16	8	0
125fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
126fa9e406ahrens * 0	|		vdev1		| GRID  |	  ASIZE		|
127fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
128fa9e406ahrens * 1	|G|			 offset1				|
129fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
130fa9e406ahrens * 2	|		vdev2		| GRID  |	  ASIZE		|
131fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
132fa9e406ahrens * 3	|G|			 offset2				|
133fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
134fa9e406ahrens * 4	|		vdev3		| GRID  |	  ASIZE		|
135fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
136fa9e406ahrens * 5	|G|			 offset3				|
137fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
138fa9e406ahrens * 6	|E| lvl | type	| cksum | comp	|     PSIZE	|     LSIZE	|
139fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
140fa9e406ahrens * 7	|			padding					|
141fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
142fa9e406ahrens * 8	|			padding					|
143fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
144fa9e406ahrens * 9	|			padding					|
145fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
146fa9e406ahrens * a	|			birth txg				|
147fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
148fa9e406ahrens * b	|			fill count				|
149fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
150fa9e406ahrens * c	|			checksum[0]				|
151fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
152fa9e406ahrens * d	|			checksum[1]				|
153fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
154fa9e406ahrens * e	|			checksum[2]				|
155fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
156fa9e406ahrens * f	|			checksum[3]				|
157fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
158fa9e406ahrens *
159fa9e406ahrens * Legend:
160fa9e406ahrens *
161fa9e406ahrens * vdev		virtual device ID
162fa9e406ahrens * offset	offset into virtual device
163fa9e406ahrens * LSIZE	logical size
164fa9e406ahrens * PSIZE	physical size (after compression)
165fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
166fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
167fa9e406ahrens * cksum	checksum function
168fa9e406ahrens * comp		compression function
169fa9e406ahrens * G		gang block indicator
170fa9e406ahrens * E		endianness
171fa9e406ahrens * type		DMU object type
172fa9e406ahrens * lvl		level of indirection
173fa9e406ahrens * birth txg	transaction group in which the block was born
174fa9e406ahrens * fill count	number of non-zero blocks under this bp
175fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
176fa9e406ahrens */
177fa9e406ahrenstypedef struct blkptr {
178fa9e406ahrens	dva_t		blk_dva[3];	/* 128-bit Data Virtual Address	*/
179fa9e406ahrens	uint64_t	blk_prop;	/* size, compression, type, etc	*/
180fa9e406ahrens	uint64_t	blk_pad[3];	/* Extra space for the future	*/
181fa9e406ahrens	uint64_t	blk_birth;	/* transaction group at birth	*/
182fa9e406ahrens	uint64_t	blk_fill;	/* fill count			*/
183fa9e406ahrens	zio_cksum_t	blk_cksum;	/* 256-bit checksum		*/
184fa9e406ahrens} blkptr_t;
185fa9e406ahrens
186fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
187fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
188fa9e406ahrens
189fa9e406ahrens/*
190fa9e406ahrens * Macros to get and set fields in a bp or DVA.
191fa9e406ahrens */
192fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
193fa9e406ahrens	BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
194fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
195fa9e406ahrens	BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
196fa9e406ahrens
197fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
198fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
199fa9e406ahrens
200fa9e406ahrens#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
201fa9e406ahrens#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
202fa9e406ahrens
203fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
204fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
205fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
206fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
207fa9e406ahrens
208fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
209fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
210fa9e406ahrens
211fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
212fa9e406ahrens	(BP_IS_HOLE(bp) ? 0 : \
213fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1))
214fa9e406ahrens#define	BP_SET_LSIZE(bp, x)	\
215fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
216fa9e406ahrens
217fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
218fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
219fa9e406ahrens#define	BP_SET_PSIZE(bp, x)	\
220fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
221fa9e406ahrens
222fa9e406ahrens#define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 8)
223fa9e406ahrens#define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 8, x)
224fa9e406ahrens
225fa9e406ahrens#define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
226fa9e406ahrens#define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
227fa9e406ahrens
228fa9e406ahrens#define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
229fa9e406ahrens#define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
230fa9e406ahrens
231fa9e406ahrens#define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
232fa9e406ahrens#define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
233fa9e406ahrens
234fa9e406ahrens#define	BP_GET_BYTEORDER(bp)	(0 - BF64_GET((bp)->blk_prop, 63, 1))
235fa9e406ahrens#define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
236fa9e406ahrens
237fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
238fa9e406ahrens	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
23999653d4eschrock		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
24099653d4eschrock
24199653d4eschrock#define	BP_GET_UCSIZE(bp) \
24299653d4eschrock	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
24399653d4eschrock	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
244fa9e406ahrens
24544cd46cbillm#define	BP_GET_NDVAS(bp)	\
24644cd46cbillm	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
24744cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
24844cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
24944cd46cbillm
25044cd46cbillm#define	BP_COUNT_GANG(bp)	\
25144cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
25244cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
25344cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[2]))
25444cd46cbillm
255fa9e406ahrens#define	DVA_EQUAL(dva1, dva2)	\
256fa9e406ahrens	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
257fa9e406ahrens	(dva1)->dva_word[0] == (dva2)->dva_word[0])
258fa9e406ahrens
2596b4acc8ahrens#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
2606b4acc8ahrens	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
2616b4acc8ahrens	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
2626b4acc8ahrens	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
2636b4acc8ahrens	((zc1).zc_word[3] - (zc2).zc_word[3])))
2646b4acc8ahrens
2656b4acc8ahrens
266fa9e406ahrens#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
267fa9e406ahrens
268fa9e406ahrens#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
269fa9e406ahrens{						\
270fa9e406ahrens	(zcp)->zc_word[0] = w0;			\
271fa9e406ahrens	(zcp)->zc_word[1] = w1;			\
272fa9e406ahrens	(zcp)->zc_word[2] = w2;			\
273fa9e406ahrens	(zcp)->zc_word[3] = w3;			\
274fa9e406ahrens}
275fa9e406ahrens
276fa9e406ahrens#define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
27744cd46cbillm#define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
27844cd46cbillm#define	BP_IS_HOLE(bp)		((bp)->blk_birth == 0)
279c717a56maybee#define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
280fa9e406ahrens
2810a4e951gw#define	BP_ZERO_DVAS(bp)			\
282fa9e406ahrens{						\
283fa9e406ahrens	(bp)->blk_dva[0].dva_word[0] = 0;	\
284fa9e406ahrens	(bp)->blk_dva[0].dva_word[1] = 0;	\
285fa9e406ahrens	(bp)->blk_dva[1].dva_word[0] = 0;	\
286fa9e406ahrens	(bp)->blk_dva[1].dva_word[1] = 0;	\
287fa9e406ahrens	(bp)->blk_dva[2].dva_word[0] = 0;	\
288fa9e406ahrens	(bp)->blk_dva[2].dva_word[1] = 0;	\
2890a4e951gw	(bp)->blk_birth = 0;			\
2900a4e951gw}
2910a4e951gw
2920a4e951gw#define	BP_ZERO(bp)				\
2930a4e951gw{						\
2940a4e951gw	BP_ZERO_DVAS(bp)			\
295fa9e406ahrens	(bp)->blk_prop = 0;			\
296fa9e406ahrens	(bp)->blk_pad[0] = 0;			\
297fa9e406ahrens	(bp)->blk_pad[1] = 0;			\
298fa9e406ahrens	(bp)->blk_pad[2] = 0;			\
299fa9e406ahrens	(bp)->blk_fill = 0;			\
300fa9e406ahrens	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
301fa9e406ahrens}
302fa9e406ahrens
303fa9e406ahrens/*
304fa9e406ahrens * Note: the byteorder is either 0 or -1, both of which are palindromes.
305fa9e406ahrens * This simplifies the endianness handling a bit.
306fa9e406ahrens */
307fa9e406ahrens#ifdef _BIG_ENDIAN
308fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(0ULL)
309fa9e406ahrens#else
310fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(-1ULL)
311fa9e406ahrens#endif
312fa9e406ahrens
313fa9e406ahrens#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
314fa9e406ahrens
31544cd46cbillm#define	BP_SPRINTF_LEN	320
316fbabab8maybee
317fa9e406ahrens#include <sys/dmu.h>
318fa9e406ahrens
319ad23a2djohansen#define	BP_GET_BUFC_TYPE(bp)						\
320ad23a2djohansen	(((BP_GET_LEVEL(bp) > 0) || (dmu_ot[BP_GET_TYPE(bp)].ot_metadata)) ? \
321ad23a2djohansen	ARC_BUFC_METADATA : ARC_BUFC_DATA);
322fa9e406ahrens/*
323fa9e406ahrens * Routines found in spa.c
324fa9e406ahrens */
325fa9e406ahrens
326fa9e406ahrens/* state manipulation functions */
327fa9e406ahrensextern int spa_open(const char *pool, spa_t **, void *tag);
328ea8dc4beschrockextern int spa_get_stats(const char *pool, nvlist_t **config,
329ea8dc4beschrock    char *altroot, size_t buflen);
330990b485llingextern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
3310a48a24timh    const char *history_str, nvlist_t *zplprops);
332051aabetaylorextern int spa_check_rootconf(char *devpath, char *devid,
333051aabetaylor    nvlist_t **bestconf, uint64_t *besttxg);
334e7cbe64gwextern boolean_t spa_rootdev_validate(nvlist_t *nv);
335051aabetaylorextern int spa_import_rootpool(char *devpath, char *devid);
336990b485llingextern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props);
337c5904d1eschrockextern int spa_import_faulted(const char *, nvlist_t *, nvlist_t *);
338fa9e406ahrensextern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
339fa9e406ahrensextern int spa_destroy(char *pool);
34089a89ebllingextern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force);
341ea8dc4beschrockextern int spa_reset(char *pool);
342ea8dc4beschrockextern void spa_async_request(spa_t *spa, int flag);
343088f389ahrensextern void spa_async_unrequest(spa_t *spa, int flag);
344ea8dc4beschrockextern void spa_async_suspend(spa_t *spa);
345ea8dc4beschrockextern void spa_async_resume(spa_t *spa);
346ea8dc4beschrockextern spa_t *spa_inject_addref(char *pool);
347ea8dc4beschrockextern void spa_inject_delref(spa_t *spa);
348ea8dc4beschrock
3493d7072feschrock#define	SPA_ASYNC_REMOVE	0x01
3503d7072feschrock#define	SPA_ASYNC_RESILVER_DONE	0x02
351ea8dc4beschrock#define	SPA_ASYNC_RESILVER	0x08
3520373e76bonwick#define	SPA_ASYNC_CONFIG_UPDATE	0x10
353fa9e406ahrens
354fa9e406ahrens/* device manipulation */
355fa9e406ahrensextern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
356ea8dc4beschrockextern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
357fa9e406ahrens    int replacing);
358ea8dc4beschrockextern int spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done);
35999653d4eschrockextern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
360c67d967eschrockextern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
361fa9e406ahrens
36299653d4eschrock/* spare state (which is global across all pools) */
36339c2341eschrockextern void spa_spare_add(vdev_t *vd);
36439c2341eschrockextern void spa_spare_remove(vdev_t *vd);
36589a89ebllingextern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
36639c2341eschrockextern void spa_spare_activate(vdev_t *vd);
36799653d4eschrock
368fa94a07brendan/* L2ARC state (which is global across all pools) */
369fa94a07brendanextern void spa_l2cache_add(vdev_t *vd);
370fa94a07brendanextern void spa_l2cache_remove(vdev_t *vd);
371fa94a07brendanextern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
372fa94a07brendanextern void spa_l2cache_activate(vdev_t *vd);
373fa94a07brendanextern void spa_l2cache_drop(spa_t *spa);
374fa94a07brendanextern void spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc);
375fa94a07brendan
376fa9e406ahrens/* scrubbing */
377088f389ahrensextern int spa_scrub(spa_t *spa, pool_scrub_type_t type);
378fa9e406ahrens
379fa9e406ahrens/* spa syncing */
380fa9e406ahrensextern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
381fa9e406ahrensextern void spa_sync_allpools(void);
382fa9e406ahrens
3833a737e0brendan/* spa namespace global mutex */
3843a737e0brendanextern kmutex_t spa_namespace_lock;
3853a737e0brendan
386fa9e406ahrens/*
387fa9e406ahrens * SPA configuration functions in spa_config.c
388fa9e406ahrens */
3890373e76bonwick
3900373e76bonwick#define	SPA_CONFIG_UPDATE_POOL	0
3910373e76bonwick#define	SPA_CONFIG_UPDATE_VDEVS	1
3920373e76bonwick
393c5904d1eschrockextern void spa_config_sync(spa_t *, boolean_t, boolean_t);
394fa9e406ahrensextern void spa_config_load(void);
395fa9e406ahrensextern nvlist_t *spa_all_configs(uint64_t *);
396fa9e406ahrensextern void spa_config_set(spa_t *spa, nvlist_t *config);
397fa9e406ahrensextern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
398fa9e406ahrens    int getstats);
3990373e76bonwickextern void spa_config_update(spa_t *spa, int what);
400e7cbe64gwextern void spa_config_update_common(spa_t *spa, int what, boolean_t isroot);
401fa9e406ahrens
402fa9e406ahrens/*
403fa9e406ahrens * Miscellaneous SPA routines in spa_misc.c
404fa9e406ahrens */
405fa9e406ahrens
406fa9e406ahrens/* Namespace manipulation */
407fa9e406ahrensextern spa_t *spa_lookup(const char *name);
4080373e76bonwickextern spa_t *spa_add(const char *name, const char *altroot);
409fa9e406ahrensextern void spa_remove(spa_t *spa);
410fa9e406ahrensextern spa_t *spa_next(spa_t *prev);
411fa9e406ahrens
412fa9e406ahrens/* Refcount functions */
413fa9e406ahrensextern void spa_open_ref(spa_t *spa, void *tag);
414fa9e406ahrensextern void spa_close(spa_t *spa, void *tag);
415fa9e406ahrensextern boolean_t spa_refcount_zero(spa_t *spa);
416fa9e406ahrens
417fa9e406ahrens/* Pool configuration lock */
418ea8dc4beschrockextern void spa_config_enter(spa_t *spa, krw_t rw, void *tag);
419ea8dc4beschrockextern void spa_config_exit(spa_t *spa, void *tag);
420fa9e406ahrensextern boolean_t spa_config_held(spa_t *spa, krw_t rw);
421fa9e406ahrens
422fa9e406ahrens/* Pool vdev add/remove lock */
423fa9e406ahrensextern uint64_t spa_vdev_enter(spa_t *spa);
424fa9e406ahrensextern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
425fa9e406ahrens
426fa9e406ahrens/* Accessor functions */
427fa9e406ahrensextern krwlock_t *spa_traverse_rwlock(spa_t *spa);
428088f389ahrensextern boolean_t spa_traverse_wanted(spa_t *spa);
429fa9e406ahrensextern struct dsl_pool *spa_get_dsl(spa_t *spa);
430fa9e406ahrensextern blkptr_t *spa_get_rootblkptr(spa_t *spa);
431fa9e406ahrensextern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
432fa9e406ahrensextern void spa_altroot(spa_t *, char *, size_t);
433fa9e406ahrensextern int spa_sync_pass(spa_t *spa);
434fa9e406ahrensextern char *spa_name(spa_t *spa);
435fa9e406ahrensextern uint64_t spa_guid(spa_t *spa);
436fa9e406ahrensextern uint64_t spa_last_synced_txg(spa_t *spa);
437fa9e406ahrensextern uint64_t spa_first_txg(spa_t *spa);
43899653d4eschrockextern uint64_t spa_version(spa_t *spa);
439fa9e406ahrensextern int spa_state(spa_t *spa);
440fa9e406ahrensextern uint64_t spa_freeze_txg(spa_t *spa);
441fa9e406ahrensextern uint64_t spa_get_alloc(spa_t *spa);
442fa9e406ahrensextern uint64_t spa_get_space(spa_t *spa);
44399653d4eschrockextern uint64_t spa_get_dspace(spa_t *spa);
444fa9e406ahrensextern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
44544cd46cbillmextern uint64_t spa_version(spa_t *spa);
44644cd46cbillmextern int spa_max_replication(spa_t *spa);
447fa9e406ahrensextern int spa_busy(void);
4480a4e951gwextern uint8_t spa_get_failmode(spa_t *spa);
449fa9e406ahrens
450fa9e406ahrens/* Miscellaneous support routines */
451fa9e406ahrensextern int spa_rename(const char *oldname, const char *newname);
452fa9e406ahrensextern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
453fa9e406ahrensextern char *spa_strdup(const char *);
454fa9e406ahrensextern void spa_strfree(char *);
455fa9e406ahrensextern uint64_t spa_get_random(uint64_t range);
456d80c45ebonwickextern void sprintf_blkptr(char *buf, int len, const blkptr_t *bp);
457fa9e406ahrensextern void spa_freeze(spa_t *spa);
458990b485llingextern void spa_upgrade(spa_t *spa, uint64_t version);
459fa9e406ahrensextern void spa_evict_all(void);
460c5904d1eschrockextern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
461c5904d1eschrock    boolean_t l2cache);
46299653d4eschrockextern boolean_t spa_has_spare(spa_t *, uint64_t guid);
46399653d4eschrockextern uint64_t bp_get_dasize(spa_t *spa, const blkptr_t *bp);
4646ce0521perrinextern boolean_t spa_has_slogs(spa_t *spa);
465bf82a41eschrockextern boolean_t spa_is_root(spa_t *spa);
466ea8dc4beschrock
46706eeb2aek/* history logging */
468ecd6cf8markstypedef enum history_log_type {
469ecd6cf8marks	LOG_CMD_POOL_CREATE,
470ecd6cf8marks	LOG_CMD_NORMAL,
471ecd6cf8marks	LOG_INTERNAL
472ecd6cf8marks} history_log_type_t;
473ecd6cf8marks
474ecd6cf8markstypedef struct history_arg {
475ecd6cf8marks	const char *ha_history_str;
476ecd6cf8marks	history_log_type_t ha_log_type;
477ecd6cf8marks	history_internal_events_t ha_event;
478ecd6cf8marks	char ha_zone[MAXPATHLEN];
479ecd6cf8marks} history_arg_t;
480ecd6cf8marks
481ecd6cf8marksextern char *spa_his_ievent_table[];
482ecd6cf8marks
48306eeb2aekextern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
48406eeb2aekextern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
48506eeb2aek    char *his_buf);
48606eeb2aekextern int spa_history_log(spa_t *spa, const char *his_buf,
487ecd6cf8marks    history_log_type_t what);
488ecd6cf8marksvoid spa_history_internal_log(history_internal_events_t event, spa_t *spa,
489ecd6cf8marks    dmu_tx_t *tx, cred_t *cr, const char *fmt, ...);
49006eeb2aek
491ea8dc4beschrock/* error handling */
492ea8dc4beschrockstruct zbookmark;
493ea8dc4beschrockstruct zio;
494ea8dc4beschrockextern void spa_log_error(spa_t *spa, struct zio *zio);
495ea8dc4beschrockextern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
496ea8dc4beschrock    struct zio *zio, uint64_t stateoroffset, uint64_t length);
4973d7072feschrockextern void zfs_post_remove(spa_t *spa, vdev_t *vd);
4983d7072feschrockextern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
499ea8dc4beschrockextern uint64_t spa_get_errlog_size(spa_t *spa);
500ea8dc4beschrockextern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
501ea8dc4beschrockextern void spa_errlog_rotate(spa_t *spa);
502ea8dc4beschrockextern void spa_errlog_drain(spa_t *spa);
503ea8dc4beschrockextern void spa_errlog_sync(spa_t *spa, uint64_t txg);
504ea8dc4beschrockextern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
505fa9e406ahrens
50687db74cek/* vdev cache */
50787db74cekextern void vdev_cache_stat_init(void);
50887db74cekextern void vdev_cache_stat_fini(void);
50987db74cek
510fa9e406ahrens/* Initialization and termination */
511fa9e406ahrensextern void spa_init(int flags);
512fa9e406ahrensextern void spa_fini(void);
513e7cbe64gwextern void spa_boot_init();
514fa9e406ahrens
515b1b8ab3lling/* properties */
516990b485llingextern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
517990b485llingextern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
518990b485llingextern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
519b1b8ab3lling
5203d7072feschrock/* asynchronous event notification */
5213d7072feschrockextern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name);
5223d7072feschrock
523fa9e406ahrens#ifdef ZFS_DEBUG
524c0a8126ek#define	dprintf_bp(bp, fmt, ...) do {				\
525c0a8126ek	if (zfs_flags & ZFS_DEBUG_DPRINTF) { 			\
526c0a8126ek	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
527c0a8126ek	sprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));		\
528c0a8126ek	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
529c0a8126ek	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
530fa9e406ahrens	} \
531fa9e406ahrens_NOTE(CONSTCOND) } while (0)
532fa9e406ahrens#else
533fa9e406ahrens#define	dprintf_bp(bp, fmt, ...)
534fa9e406ahrens#endif
535fa9e406ahrens
536fa9e406ahrensextern int spa_mode;			/* mode, e.g. FREAD | FWRITE */
537fa9e406ahrens
538fa9e406ahrens#ifdef	__cplusplus
539fa9e406ahrens}
540fa9e406ahrens#endif
541fa9e406ahrens
542fa9e406ahrens#endif	/* _SYS_SPA_H */
543