spa.h revision fa94a07fd0519b8abfd871ad8fe60e6bebe1e2bb
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/*
2239c2341eschrock * Copyright 2007 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;
50fa94a07brendantypedef struct spa_aux_vdev spa_aux_vdev_t;
51fa9e406ahrensstruct dsl_pool;
52fa9e406ahrens
53fa9e406ahrens/*
54fa9e406ahrens * General-purpose 32-bit and 64-bit bitfield encodings.
55fa9e406ahrens */
56fa9e406ahrens#define	BF32_DECODE(x, low, len)	P2PHASE((x) >> (low), 1U << (len))
57fa9e406ahrens#define	BF64_DECODE(x, low, len)	P2PHASE((x) >> (low), 1ULL << (len))
58fa9e406ahrens#define	BF32_ENCODE(x, low, len)	(P2PHASE((x), 1U << (len)) << (low))
59fa9e406ahrens#define	BF64_ENCODE(x, low, len)	(P2PHASE((x), 1ULL << (len)) << (low))
60fa9e406ahrens
61fa9e406ahrens#define	BF32_GET(x, low, len)		BF32_DECODE(x, low, len)
62fa9e406ahrens#define	BF64_GET(x, low, len)		BF64_DECODE(x, low, len)
63fa9e406ahrens
64fa9e406ahrens#define	BF32_SET(x, low, len, val)	\
655ad8204nd	((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
66fa9e406ahrens#define	BF64_SET(x, low, len, val)	\
675ad8204nd	((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
68fa9e406ahrens
69fa9e406ahrens#define	BF32_GET_SB(x, low, len, shift, bias)	\
70fa9e406ahrens	((BF32_GET(x, low, len) + (bias)) << (shift))
71fa9e406ahrens#define	BF64_GET_SB(x, low, len, shift, bias)	\
72fa9e406ahrens	((BF64_GET(x, low, len) + (bias)) << (shift))
73fa9e406ahrens
74fa9e406ahrens#define	BF32_SET_SB(x, low, len, shift, bias, val)	\
75fa9e406ahrens	BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
76fa9e406ahrens#define	BF64_SET_SB(x, low, len, shift, bias, val)	\
77fa9e406ahrens	BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
78fa9e406ahrens
79fa9e406ahrens/*
80fa9e406ahrens * We currently support nine block sizes, from 512 bytes to 128K.
81fa9e406ahrens * We could go higher, but the benefits are near-zero and the cost
82fa9e406ahrens * of COWing a giant block to modify one byte would become excessive.
83fa9e406ahrens */
84fa9e406ahrens#define	SPA_MINBLOCKSHIFT	9
85fa9e406ahrens#define	SPA_MAXBLOCKSHIFT	17
86fa9e406ahrens#define	SPA_MINBLOCKSIZE	(1ULL << SPA_MINBLOCKSHIFT)
87fa9e406ahrens#define	SPA_MAXBLOCKSIZE	(1ULL << SPA_MAXBLOCKSHIFT)
88fa9e406ahrens
89fa9e406ahrens#define	SPA_BLOCKSIZES		(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
90fa9e406ahrens
91fa9e406ahrens/*
92fa9e406ahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
93fa9e406ahrens * The ASIZE encoding should be at least 64 times larger (6 more bits)
94fa9e406ahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block
95fa9e406ahrens * overhead, three DVAs per bp, plus one more bit in case we do anything
96fa9e406ahrens * else that expands the ASIZE.
97fa9e406ahrens */
98fa9e406ahrens#define	SPA_LSIZEBITS		16	/* LSIZE up to 32M (2^16 * 512)	*/
99fa9e406ahrens#define	SPA_PSIZEBITS		16	/* PSIZE up to 32M (2^16 * 512)	*/
100fa9e406ahrens#define	SPA_ASIZEBITS		24	/* ASIZE up to 64 times larger	*/
101fa9e406ahrens
102fa9e406ahrens/*
103fa9e406ahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs).
104fa9e406ahrens * The members of the dva_t should be considered opaque outside the SPA.
105fa9e406ahrens */
106fa9e406ahrenstypedef struct dva {
107fa9e406ahrens	uint64_t	dva_word[2];
108fa9e406ahrens} dva_t;
109fa9e406ahrens
110fa9e406ahrens/*
111fa9e406ahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
112fa9e406ahrens */
113fa9e406ahrenstypedef struct zio_cksum {
114fa9e406ahrens	uint64_t	zc_word[4];
115fa9e406ahrens} zio_cksum_t;
116fa9e406ahrens
117fa9e406ahrens/*
118fa9e406ahrens * Each block is described by its DVAs, time of birth, checksum, etc.
119fa9e406ahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows:
120fa9e406ahrens *
121fa9e406ahrens *	64	56	48	40	32	24	16	8	0
122fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
123fa9e406ahrens * 0	|		vdev1		| GRID  |	  ASIZE		|
124fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
125fa9e406ahrens * 1	|G|			 offset1				|
126fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
127fa9e406ahrens * 2	|		vdev2		| GRID  |	  ASIZE		|
128fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
129fa9e406ahrens * 3	|G|			 offset2				|
130fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
131fa9e406ahrens * 4	|		vdev3		| GRID  |	  ASIZE		|
132fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
133fa9e406ahrens * 5	|G|			 offset3				|
134fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
135fa9e406ahrens * 6	|E| lvl | type	| cksum | comp	|     PSIZE	|     LSIZE	|
136fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
137fa9e406ahrens * 7	|			padding					|
138fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
139fa9e406ahrens * 8	|			padding					|
140fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
141fa9e406ahrens * 9	|			padding					|
142fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
143fa9e406ahrens * a	|			birth txg				|
144fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
145fa9e406ahrens * b	|			fill count				|
146fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
147fa9e406ahrens * c	|			checksum[0]				|
148fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
149fa9e406ahrens * d	|			checksum[1]				|
150fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
151fa9e406ahrens * e	|			checksum[2]				|
152fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
153fa9e406ahrens * f	|			checksum[3]				|
154fa9e406ahrens *	+-------+-------+-------+-------+-------+-------+-------+-------+
155fa9e406ahrens *
156fa9e406ahrens * Legend:
157fa9e406ahrens *
158fa9e406ahrens * vdev		virtual device ID
159fa9e406ahrens * offset	offset into virtual device
160fa9e406ahrens * LSIZE	logical size
161fa9e406ahrens * PSIZE	physical size (after compression)
162fa9e406ahrens * ASIZE	allocated size (including RAID-Z parity and gang block headers)
163fa9e406ahrens * GRID		RAID-Z layout information (reserved for future use)
164fa9e406ahrens * cksum	checksum function
165fa9e406ahrens * comp		compression function
166fa9e406ahrens * G		gang block indicator
167fa9e406ahrens * E		endianness
168fa9e406ahrens * type		DMU object type
169fa9e406ahrens * lvl		level of indirection
170fa9e406ahrens * birth txg	transaction group in which the block was born
171fa9e406ahrens * fill count	number of non-zero blocks under this bp
172fa9e406ahrens * checksum[4]	256-bit checksum of the data this bp describes
173fa9e406ahrens */
174fa9e406ahrenstypedef struct blkptr {
175fa9e406ahrens	dva_t		blk_dva[3];	/* 128-bit Data Virtual Address	*/
176fa9e406ahrens	uint64_t	blk_prop;	/* size, compression, type, etc	*/
177fa9e406ahrens	uint64_t	blk_pad[3];	/* Extra space for the future	*/
178fa9e406ahrens	uint64_t	blk_birth;	/* transaction group at birth	*/
179fa9e406ahrens	uint64_t	blk_fill;	/* fill count			*/
180fa9e406ahrens	zio_cksum_t	blk_cksum;	/* 256-bit checksum		*/
181fa9e406ahrens} blkptr_t;
182fa9e406ahrens
183fa9e406ahrens#define	SPA_BLKPTRSHIFT	7		/* blkptr_t is 128 bytes	*/
184fa9e406ahrens#define	SPA_DVAS_PER_BP	3		/* Number of DVAs in a bp	*/
185fa9e406ahrens
186fa9e406ahrens/*
187fa9e406ahrens * Macros to get and set fields in a bp or DVA.
188fa9e406ahrens */
189fa9e406ahrens#define	DVA_GET_ASIZE(dva)	\
190fa9e406ahrens	BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
191fa9e406ahrens#define	DVA_SET_ASIZE(dva, x)	\
192fa9e406ahrens	BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
193fa9e406ahrens
194fa9e406ahrens#define	DVA_GET_GRID(dva)	BF64_GET((dva)->dva_word[0], 24, 8)
195fa9e406ahrens#define	DVA_SET_GRID(dva, x)	BF64_SET((dva)->dva_word[0], 24, 8, x)
196fa9e406ahrens
197fa9e406ahrens#define	DVA_GET_VDEV(dva)	BF64_GET((dva)->dva_word[0], 32, 32)
198fa9e406ahrens#define	DVA_SET_VDEV(dva, x)	BF64_SET((dva)->dva_word[0], 32, 32, x)
199fa9e406ahrens
200fa9e406ahrens#define	DVA_GET_OFFSET(dva)	\
201fa9e406ahrens	BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
202fa9e406ahrens#define	DVA_SET_OFFSET(dva, x)	\
203fa9e406ahrens	BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
204fa9e406ahrens
205fa9e406ahrens#define	DVA_GET_GANG(dva)	BF64_GET((dva)->dva_word[1], 63, 1)
206fa9e406ahrens#define	DVA_SET_GANG(dva, x)	BF64_SET((dva)->dva_word[1], 63, 1, x)
207fa9e406ahrens
208fa9e406ahrens#define	BP_GET_LSIZE(bp)	\
209fa9e406ahrens	(BP_IS_HOLE(bp) ? 0 : \
210fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1))
211fa9e406ahrens#define	BP_SET_LSIZE(bp, x)	\
212fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
213fa9e406ahrens
214fa9e406ahrens#define	BP_GET_PSIZE(bp)	\
215fa9e406ahrens	BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
216fa9e406ahrens#define	BP_SET_PSIZE(bp, x)	\
217fa9e406ahrens	BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
218fa9e406ahrens
219fa9e406ahrens#define	BP_GET_COMPRESS(bp)	BF64_GET((bp)->blk_prop, 32, 8)
220fa9e406ahrens#define	BP_SET_COMPRESS(bp, x)	BF64_SET((bp)->blk_prop, 32, 8, x)
221fa9e406ahrens
222fa9e406ahrens#define	BP_GET_CHECKSUM(bp)	BF64_GET((bp)->blk_prop, 40, 8)
223fa9e406ahrens#define	BP_SET_CHECKSUM(bp, x)	BF64_SET((bp)->blk_prop, 40, 8, x)
224fa9e406ahrens
225fa9e406ahrens#define	BP_GET_TYPE(bp)		BF64_GET((bp)->blk_prop, 48, 8)
226fa9e406ahrens#define	BP_SET_TYPE(bp, x)	BF64_SET((bp)->blk_prop, 48, 8, x)
227fa9e406ahrens
228fa9e406ahrens#define	BP_GET_LEVEL(bp)	BF64_GET((bp)->blk_prop, 56, 5)
229fa9e406ahrens#define	BP_SET_LEVEL(bp, x)	BF64_SET((bp)->blk_prop, 56, 5, x)
230fa9e406ahrens
231fa9e406ahrens#define	BP_GET_BYTEORDER(bp)	(0 - BF64_GET((bp)->blk_prop, 63, 1))
232fa9e406ahrens#define	BP_SET_BYTEORDER(bp, x)	BF64_SET((bp)->blk_prop, 63, 1, x)
233fa9e406ahrens
234fa9e406ahrens#define	BP_GET_ASIZE(bp)	\
235fa9e406ahrens	(DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
23699653d4eschrock		DVA_GET_ASIZE(&(bp)->blk_dva[2]))
23799653d4eschrock
23899653d4eschrock#define	BP_GET_UCSIZE(bp) \
23999653d4eschrock	((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \
24099653d4eschrock	BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp));
241fa9e406ahrens
24244cd46cbillm#define	BP_GET_NDVAS(bp)	\
24344cd46cbillm	(!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
24444cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
24544cd46cbillm	!!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
24644cd46cbillm
24744cd46cbillm#define	BP_COUNT_GANG(bp)	\
24844cd46cbillm	(DVA_GET_GANG(&(bp)->blk_dva[0]) + \
24944cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[1]) + \
25044cd46cbillm	DVA_GET_GANG(&(bp)->blk_dva[2]))
25144cd46cbillm
252fa9e406ahrens#define	DVA_EQUAL(dva1, dva2)	\
253fa9e406ahrens	((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
254fa9e406ahrens	(dva1)->dva_word[0] == (dva2)->dva_word[0])
255fa9e406ahrens
2566b4acc8ahrens#define	ZIO_CHECKSUM_EQUAL(zc1, zc2) \
2576b4acc8ahrens	(0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
2586b4acc8ahrens	((zc1).zc_word[1] - (zc2).zc_word[1]) | \
2596b4acc8ahrens	((zc1).zc_word[2] - (zc2).zc_word[2]) | \
2606b4acc8ahrens	((zc1).zc_word[3] - (zc2).zc_word[3])))
2616b4acc8ahrens
2626b4acc8ahrens
263fa9e406ahrens#define	DVA_IS_VALID(dva)	(DVA_GET_ASIZE(dva) != 0)
264fa9e406ahrens
265fa9e406ahrens#define	ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)	\
266fa9e406ahrens{						\
267fa9e406ahrens	(zcp)->zc_word[0] = w0;			\
268fa9e406ahrens	(zcp)->zc_word[1] = w1;			\
269fa9e406ahrens	(zcp)->zc_word[2] = w2;			\
270fa9e406ahrens	(zcp)->zc_word[3] = w3;			\
271fa9e406ahrens}
272fa9e406ahrens
273fa9e406ahrens#define	BP_IDENTITY(bp)		(&(bp)->blk_dva[0])
27444cd46cbillm#define	BP_IS_GANG(bp)		DVA_GET_GANG(BP_IDENTITY(bp))
27544cd46cbillm#define	BP_IS_HOLE(bp)		((bp)->blk_birth == 0)
276c717a56maybee#define	BP_IS_OLDER(bp, txg)	(!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg))
277fa9e406ahrens
2780a4e951gw#define	BP_ZERO_DVAS(bp)			\
279fa9e406ahrens{						\
280fa9e406ahrens	(bp)->blk_dva[0].dva_word[0] = 0;	\
281fa9e406ahrens	(bp)->blk_dva[0].dva_word[1] = 0;	\
282fa9e406ahrens	(bp)->blk_dva[1].dva_word[0] = 0;	\
283fa9e406ahrens	(bp)->blk_dva[1].dva_word[1] = 0;	\
284fa9e406ahrens	(bp)->blk_dva[2].dva_word[0] = 0;	\
285fa9e406ahrens	(bp)->blk_dva[2].dva_word[1] = 0;	\
2860a4e951gw	(bp)->blk_birth = 0;			\
2870a4e951gw}
2880a4e951gw
2890a4e951gw#define	BP_ZERO(bp)				\
2900a4e951gw{						\
2910a4e951gw	BP_ZERO_DVAS(bp)			\
292fa9e406ahrens	(bp)->blk_prop = 0;			\
293fa9e406ahrens	(bp)->blk_pad[0] = 0;			\
294fa9e406ahrens	(bp)->blk_pad[1] = 0;			\
295fa9e406ahrens	(bp)->blk_pad[2] = 0;			\
296fa9e406ahrens	(bp)->blk_fill = 0;			\
297fa9e406ahrens	ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);	\
298fa9e406ahrens}
299fa9e406ahrens
300fa9e406ahrens/*
301fa9e406ahrens * Note: the byteorder is either 0 or -1, both of which are palindromes.
302fa9e406ahrens * This simplifies the endianness handling a bit.
303fa9e406ahrens */
304fa9e406ahrens#ifdef _BIG_ENDIAN
305fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(0ULL)
306fa9e406ahrens#else
307fa9e406ahrens#define	ZFS_HOST_BYTEORDER	(-1ULL)
308fa9e406ahrens#endif
309fa9e406ahrens
310fa9e406ahrens#define	BP_SHOULD_BYTESWAP(bp)	(BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
311fa9e406ahrens
31244cd46cbillm#define	BP_SPRINTF_LEN	320
313fbabab8maybee
314fa9e406ahrens#include <sys/dmu.h>
315fa9e406ahrens
316ad23a2djohansen#define	BP_GET_BUFC_TYPE(bp)						\
317ad23a2djohansen	(((BP_GET_LEVEL(bp) > 0) || (dmu_ot[BP_GET_TYPE(bp)].ot_metadata)) ? \
318ad23a2djohansen	ARC_BUFC_METADATA : ARC_BUFC_DATA);
319fa9e406ahrens/*
320fa9e406ahrens * Routines found in spa.c
321fa9e406ahrens */
322fa9e406ahrens
323fa9e406ahrens/* state manipulation functions */
324fa9e406ahrensextern int spa_open(const char *pool, spa_t **, void *tag);
325ea8dc4beschrockextern int spa_get_stats(const char *pool, nvlist_t **config,
326ea8dc4beschrock    char *altroot, size_t buflen);
327990b485llingextern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
328228975cek    const char *history_str);
329990b485llingextern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props);
330fa9e406ahrensextern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
331fa9e406ahrensextern int spa_destroy(char *pool);
33244cd46cbillmextern int spa_export(char *pool, nvlist_t **oldconfig);
333ea8dc4beschrockextern int spa_reset(char *pool);
334ea8dc4beschrockextern void spa_async_request(spa_t *spa, int flag);
335ea8dc4beschrockextern void spa_async_suspend(spa_t *spa);
336ea8dc4beschrockextern void spa_async_resume(spa_t *spa);
337ea8dc4beschrockextern spa_t *spa_inject_addref(char *pool);
338ea8dc4beschrockextern void spa_inject_delref(spa_t *spa);
339ea8dc4beschrock
3403d7072feschrock#define	SPA_ASYNC_REMOVE	0x01
3413d7072feschrock#define	SPA_ASYNC_RESILVER_DONE	0x02
342ea8dc4beschrock#define	SPA_ASYNC_SCRUB		0x04
343ea8dc4beschrock#define	SPA_ASYNC_RESILVER	0x08
3440373e76bonwick#define	SPA_ASYNC_CONFIG_UPDATE	0x10
345fa9e406ahrens
346fa9e406ahrens/* device manipulation */
347fa9e406ahrensextern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
348ea8dc4beschrockextern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
349fa9e406ahrens    int replacing);
350ea8dc4beschrockextern int spa_vdev_detach(spa_t *spa, uint64_t guid, int replace_done);
35199653d4eschrockextern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
352c67d967eschrockextern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
353fa9e406ahrens
35499653d4eschrock/* spare state (which is global across all pools) */
35539c2341eschrockextern void spa_spare_add(vdev_t *vd);
35639c2341eschrockextern void spa_spare_remove(vdev_t *vd);
35739c2341eschrockextern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool);
35839c2341eschrockextern void spa_spare_activate(vdev_t *vd);
35999653d4eschrock
360fa94a07brendan/* L2ARC state (which is global across all pools) */
361fa94a07brendanextern void spa_l2cache_add(vdev_t *vd);
362fa94a07brendanextern void spa_l2cache_remove(vdev_t *vd);
363fa94a07brendanextern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
364fa94a07brendanextern void spa_l2cache_activate(vdev_t *vd);
365fa94a07brendanextern void spa_l2cache_drop(spa_t *spa);
366fa94a07brendanextern void spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc);
367fa94a07brendan
368fa9e406ahrens/* scrubbing */
369fa9e406ahrensextern int spa_scrub(spa_t *spa, pool_scrub_type_t type, boolean_t force);
370fa9e406ahrensextern void spa_scrub_suspend(spa_t *spa);
371fa9e406ahrensextern void spa_scrub_resume(spa_t *spa);
372fa9e406ahrensextern void spa_scrub_restart(spa_t *spa, uint64_t txg);
373fa9e406ahrens
374fa9e406ahrens/* spa syncing */
375fa9e406ahrensextern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
376fa9e406ahrensextern void spa_sync_allpools(void);
377fa9e406ahrens
378fa9e406ahrens/*
379fa9e406ahrens * SPA configuration functions in spa_config.c
380fa9e406ahrens */
3810373e76bonwick
3820373e76bonwick#define	SPA_CONFIG_UPDATE_POOL	0
3830373e76bonwick#define	SPA_CONFIG_UPDATE_VDEVS	1
3840373e76bonwick
385fa9e406ahrensextern void spa_config_sync(void);
3862f8aaabeschrockextern void spa_config_check(const char *, const char *);
387fa9e406ahrensextern void spa_config_load(void);
388fa9e406ahrensextern nvlist_t *spa_all_configs(uint64_t *);
389fa9e406ahrensextern void spa_config_set(spa_t *spa, nvlist_t *config);
390fa9e406ahrensextern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
391fa9e406ahrens    int getstats);
3920373e76bonwickextern void spa_config_update(spa_t *spa, int what);
393fa9e406ahrens
394fa9e406ahrens/*
395fa9e406ahrens * Miscellaneous SPA routines in spa_misc.c
396fa9e406ahrens */
397fa9e406ahrens
398fa9e406ahrens/* Namespace manipulation */
399fa9e406ahrensextern spa_t *spa_lookup(const char *name);
4000373e76bonwickextern spa_t *spa_add(const char *name, const char *altroot);
401fa9e406ahrensextern void spa_remove(spa_t *spa);
402fa9e406ahrensextern spa_t *spa_next(spa_t *prev);
403fa9e406ahrens
404fa9e406ahrens/* Refcount functions */
405fa9e406ahrensextern void spa_open_ref(spa_t *spa, void *tag);
406fa9e406ahrensextern void spa_close(spa_t *spa, void *tag);
407fa9e406ahrensextern boolean_t spa_refcount_zero(spa_t *spa);
408fa9e406ahrens
409fa9e406ahrens/* Pool configuration lock */
410ea8dc4beschrockextern void spa_config_enter(spa_t *spa, krw_t rw, void *tag);
411ea8dc4beschrockextern void spa_config_exit(spa_t *spa, void *tag);
412fa9e406ahrensextern boolean_t spa_config_held(spa_t *spa, krw_t rw);
413fa9e406ahrens
414fa9e406ahrens/* Pool vdev add/remove lock */
415fa9e406ahrensextern uint64_t spa_vdev_enter(spa_t *spa);
416fa9e406ahrensextern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
417fa9e406ahrens
418fa9e406ahrens/* Accessor functions */
419fa9e406ahrensextern krwlock_t *spa_traverse_rwlock(spa_t *spa);
420fa9e406ahrensextern int spa_traverse_wanted(spa_t *spa);
421fa9e406ahrensextern struct dsl_pool *spa_get_dsl(spa_t *spa);
422fa9e406ahrensextern blkptr_t *spa_get_rootblkptr(spa_t *spa);
423fa9e406ahrensextern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
424fa9e406ahrensextern void spa_altroot(spa_t *, char *, size_t);
425fa9e406ahrensextern int spa_sync_pass(spa_t *spa);
426fa9e406ahrensextern char *spa_name(spa_t *spa);
427fa9e406ahrensextern uint64_t spa_guid(spa_t *spa);
428fa9e406ahrensextern uint64_t spa_last_synced_txg(spa_t *spa);
429fa9e406ahrensextern uint64_t spa_first_txg(spa_t *spa);
43099653d4eschrockextern uint64_t spa_version(spa_t *spa);
431fa9e406ahrensextern int spa_state(spa_t *spa);
432fa9e406ahrensextern uint64_t spa_freeze_txg(spa_t *spa);
433fa9e406ahrensstruct metaslab_class;
434fa9e406ahrensextern uint64_t spa_get_alloc(spa_t *spa);
435fa9e406ahrensextern uint64_t spa_get_space(spa_t *spa);
43699653d4eschrockextern uint64_t spa_get_dspace(spa_t *spa);
437fa9e406ahrensextern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
43844cd46cbillmextern uint64_t spa_version(spa_t *spa);
43944cd46cbillmextern int spa_max_replication(spa_t *spa);
440fa9e406ahrensextern int spa_busy(void);
4410a4e951gwextern uint8_t spa_get_failmode(spa_t *spa);
442fa9e406ahrens
443fa9e406ahrens/* Miscellaneous support routines */
444fa9e406ahrensextern int spa_rename(const char *oldname, const char *newname);
445fa9e406ahrensextern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
446fa9e406ahrensextern char *spa_strdup(const char *);
447fa9e406ahrensextern void spa_strfree(char *);
448fa9e406ahrensextern uint64_t spa_get_random(uint64_t range);
449d80c45ebonwickextern void sprintf_blkptr(char *buf, int len, const blkptr_t *bp);
450fa9e406ahrensextern void spa_freeze(spa_t *spa);
451990b485llingextern void spa_upgrade(spa_t *spa, uint64_t version);
452fa9e406ahrensextern void spa_evict_all(void);
453ea8dc4beschrockextern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid);
45499653d4eschrockextern boolean_t spa_has_spare(spa_t *, uint64_t guid);
45599653d4eschrockextern uint64_t bp_get_dasize(spa_t *spa, const blkptr_t *bp);
4566ce0521perrinextern boolean_t spa_has_slogs(spa_t *spa);
457ea8dc4beschrock
45806eeb2aek/* history logging */
459ecd6cf8markstypedef enum history_log_type {
460ecd6cf8marks	LOG_CMD_POOL_CREATE,
461ecd6cf8marks	LOG_CMD_NORMAL,
462ecd6cf8marks	LOG_INTERNAL
463ecd6cf8marks} history_log_type_t;
464ecd6cf8marks
465ecd6cf8markstypedef struct history_arg {
466ecd6cf8marks	const char *ha_history_str;
467ecd6cf8marks	history_log_type_t ha_log_type;
468ecd6cf8marks	history_internal_events_t ha_event;
469ecd6cf8marks	char ha_zone[MAXPATHLEN];
470ecd6cf8marks} history_arg_t;
471ecd6cf8marks
472ecd6cf8marksextern char *spa_his_ievent_table[];
473ecd6cf8marks
47406eeb2aekextern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
47506eeb2aekextern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
47606eeb2aek    char *his_buf);
47706eeb2aekextern int spa_history_log(spa_t *spa, const char *his_buf,
478ecd6cf8marks    history_log_type_t what);
479ecd6cf8marksvoid spa_history_internal_log(history_internal_events_t event, spa_t *spa,
480ecd6cf8marks    dmu_tx_t *tx, cred_t *cr, const char *fmt, ...);
48106eeb2aek
482ea8dc4beschrock/* error handling */
483ea8dc4beschrockstruct zbookmark;
484ea8dc4beschrockstruct zio;
485ea8dc4beschrockextern void spa_log_error(spa_t *spa, struct zio *zio);
486ea8dc4beschrockextern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
487ea8dc4beschrock    struct zio *zio, uint64_t stateoroffset, uint64_t length);
488ea8dc4beschrockextern void zfs_post_ok(spa_t *spa, vdev_t *vd);
4893d7072feschrockextern void zfs_post_remove(spa_t *spa, vdev_t *vd);
4903d7072feschrockextern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
491ea8dc4beschrockextern uint64_t spa_get_errlog_size(spa_t *spa);
492ea8dc4beschrockextern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
493ea8dc4beschrockextern void spa_errlog_rotate(spa_t *spa);
494ea8dc4beschrockextern void spa_errlog_drain(spa_t *spa);
495ea8dc4beschrockextern void spa_errlog_sync(spa_t *spa, uint64_t txg);
496ea8dc4beschrockextern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
497fa9e406ahrens
498fa9e406ahrens/* Initialization and termination */
499fa9e406ahrensextern void spa_init(int flags);
500fa9e406ahrensextern void spa_fini(void);
501fa9e406ahrens
502b1b8ab3lling/* properties */
503990b485llingextern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
504990b485llingextern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
505990b485llingextern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
506b1b8ab3lling
5073d7072feschrock/* asynchronous event notification */
5083d7072feschrockextern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name);
5093d7072feschrock
510fa9e406ahrens#ifdef ZFS_DEBUG
511c0a8126ek#define	dprintf_bp(bp, fmt, ...) do {				\
512c0a8126ek	if (zfs_flags & ZFS_DEBUG_DPRINTF) { 			\
513c0a8126ek	char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);	\
514c0a8126ek	sprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp));		\
515c0a8126ek	dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);		\
516c0a8126ek	kmem_free(__blkbuf, BP_SPRINTF_LEN);			\
517fa9e406ahrens	} \
518fa9e406ahrens_NOTE(CONSTCOND) } while (0)
519fa9e406ahrens#else
520fa9e406ahrens#define	dprintf_bp(bp, fmt, ...)
521fa9e406ahrens#endif
522fa9e406ahrens
523fa9e406ahrensextern int spa_mode;			/* mode, e.g. FREAD | FWRITE */
524fa9e406ahrens
525fa9e406ahrens#ifdef	__cplusplus
526fa9e406ahrens}
527fa9e406ahrens#endif
528fa9e406ahrens
529fa9e406ahrens#endif	/* _SYS_SPA_H */
530