]> git.proxmox.com Git - mirror_zfs.git/blame - include/sys/spa.h
OpenZFS 9256 - zfs send space estimation off by > 10% on some datasets
[mirror_zfs.git] / include / sys / spa.h
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
5dbd68a3 23 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
3541dc6d 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
0c66c32d 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
3c67d83a 26 * Copyright 2013 Saso Kiselkov. All rights reserved.
12fa0466
DE
27 * Copyright (c) 2014 Integros [integros.com]
28 * Copyright 2017 Joyent, Inc.
0ea05c64 29 * Copyright (c) 2017 Datto Inc.
34dc7c2f
BB
30 */
31
32#ifndef _SYS_SPA_H
33#define _SYS_SPA_H
34
34dc7c2f
BB
35#include <sys/avl.h>
36#include <sys/zfs_context.h>
efcd79a8 37#include <sys/kstat.h>
34dc7c2f
BB
38#include <sys/nvpair.h>
39#include <sys/sysmacros.h>
40#include <sys/types.h>
41#include <sys/fs/zfs.h>
1eeb4562 42#include <sys/spa_checksum.h>
c4434877 43#include <sys/dmu.h>
34dc7c2f
BB
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49/*
50 * Forward references that lots of things need.
51 */
52typedef struct spa spa_t;
53typedef struct vdev vdev_t;
54typedef struct metaslab metaslab_t;
428870ff
BB
55typedef struct metaslab_group metaslab_group_t;
56typedef struct metaslab_class metaslab_class_t;
57typedef struct zio zio_t;
34dc7c2f 58typedef struct zilog zilog_t;
34dc7c2f 59typedef struct spa_aux_vdev spa_aux_vdev_t;
428870ff
BB
60typedef struct ddt ddt_t;
61typedef struct ddt_entry ddt_entry_t;
5dbd68a3 62typedef struct zbookmark_phys zbookmark_phys_t;
1421c891 63
34dc7c2f 64struct dsl_pool;
6f1ffb06 65struct dsl_dataset;
b5256303 66struct dsl_crypto_params;
34dc7c2f
BB
67
68/*
69 * General-purpose 32-bit and 64-bit bitfield encodings.
70 */
71#define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len))
72#define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len))
73#define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low))
74#define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low))
75
76#define BF32_GET(x, low, len) BF32_DECODE(x, low, len)
77#define BF64_GET(x, low, len) BF64_DECODE(x, low, len)
78
b0bc7a84
MG
79#define BF32_SET(x, low, len, val) do { \
80 ASSERT3U(val, <, 1U << (len)); \
81 ASSERT3U(low + len, <=, 32); \
82 (x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \
83_NOTE(CONSTCOND) } while (0)
84
85#define BF64_SET(x, low, len, val) do { \
86 ASSERT3U(val, <, 1ULL << (len)); \
87 ASSERT3U(low + len, <=, 64); \
88 ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \
89_NOTE(CONSTCOND) } while (0)
34dc7c2f
BB
90
91#define BF32_GET_SB(x, low, len, shift, bias) \
92 ((BF32_GET(x, low, len) + (bias)) << (shift))
93#define BF64_GET_SB(x, low, len, shift, bias) \
94 ((BF64_GET(x, low, len) + (bias)) << (shift))
95
b0bc7a84
MG
96#define BF32_SET_SB(x, low, len, shift, bias, val) do { \
97 ASSERT(IS_P2ALIGNED(val, 1U << shift)); \
98 ASSERT3S((val) >> (shift), >=, bias); \
99 BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \
100_NOTE(CONSTCOND) } while (0)
101#define BF64_SET_SB(x, low, len, shift, bias, val) do { \
102 ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \
103 ASSERT3S((val) >> (shift), >=, bias); \
104 BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \
105_NOTE(CONSTCOND) } while (0)
34dc7c2f
BB
106
107/*
f1512ee6
MA
108 * We currently support block sizes from 512 bytes to 16MB.
109 * The benefits of larger blocks, and thus larger IO, need to be weighed
110 * against the cost of COWing a giant block to modify one byte, and the
111 * large latency of reading or writing a large block.
112 *
113 * Note that although blocks up to 16MB are supported, the recordsize
114 * property can not be set larger than zfs_max_recordsize (default 1MB).
115 * See the comment near zfs_max_recordsize in dsl_dataset.c for details.
116 *
117 * Note that although the LSIZE field of the blkptr_t can store sizes up
118 * to 32MB, the dnode's dn_datablkszsec can only store sizes up to
119 * 32MB - 512 bytes. Therefore, we limit SPA_MAXBLOCKSIZE to 16MB.
34dc7c2f
BB
120 */
121#define SPA_MINBLOCKSHIFT 9
f1512ee6
MA
122#define SPA_OLD_MAXBLOCKSHIFT 17
123#define SPA_MAXBLOCKSHIFT 24
34dc7c2f 124#define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT)
f1512ee6 125#define SPA_OLD_MAXBLOCKSIZE (1ULL << SPA_OLD_MAXBLOCKSHIFT)
34dc7c2f
BB
126#define SPA_MAXBLOCKSIZE (1ULL << SPA_MAXBLOCKSHIFT)
127
ff61d1a4 128/*
129 * Alignment Shift (ashift) is an immutable, internal top-level vdev property
130 * which can only be set at vdev creation time. Physical writes are always done
131 * according to it, which makes 2^ashift the smallest possible IO on a vdev.
132 *
dddef7d6 133 * We currently allow values ranging from 512 bytes (2^9 = 512) to 64 KiB
134 * (2^16 = 65,536).
ff61d1a4 135 */
136#define ASHIFT_MIN 9
dddef7d6 137#define ASHIFT_MAX 16
ff61d1a4 138
b128c09f
BB
139/*
140 * Size of block to hold the configuration data (a packed nvlist)
141 */
9ae529ec 142#define SPA_CONFIG_BLOCKSIZE (1ULL << 14)
b128c09f 143
34dc7c2f
BB
144/*
145 * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
146 * The ASIZE encoding should be at least 64 times larger (6 more bits)
147 * to support up to 4-way RAID-Z mirror mode with worst-case gang block
148 * overhead, three DVAs per bp, plus one more bit in case we do anything
149 * else that expands the ASIZE.
150 */
151#define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */
152#define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */
153#define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */
154
d3c2ae1c
GW
155#define SPA_COMPRESSBITS 7
156
34dc7c2f
BB
157/*
158 * All SPA data is represented by 128-bit data virtual addresses (DVAs).
159 * The members of the dva_t should be considered opaque outside the SPA.
160 */
161typedef struct dva {
162 uint64_t dva_word[2];
163} dva_t;
164
34dc7c2f 165
3c67d83a
TH
166/*
167 * Some checksums/hashes need a 256-bit initialization salt. This salt is kept
168 * secret and is suitable for use in MAC algorithms as the key.
169 */
170typedef struct zio_cksum_salt {
171 uint8_t zcs_bytes[32];
172} zio_cksum_salt_t;
173
34dc7c2f
BB
174/*
175 * Each block is described by its DVAs, time of birth, checksum, etc.
176 * The word-by-word, bit-by-bit layout of the blkptr is as follows:
177 *
178 * 64 56 48 40 32 24 16 8 0
179 * +-------+-------+-------+-------+-------+-------+-------+-------+
180 * 0 | vdev1 | GRID | ASIZE |
181 * +-------+-------+-------+-------+-------+-------+-------+-------+
182 * 1 |G| offset1 |
183 * +-------+-------+-------+-------+-------+-------+-------+-------+
184 * 2 | vdev2 | GRID | ASIZE |
185 * +-------+-------+-------+-------+-------+-------+-------+-------+
186 * 3 |G| offset2 |
187 * +-------+-------+-------+-------+-------+-------+-------+-------+
188 * 4 | vdev3 | GRID | ASIZE |
189 * +-------+-------+-------+-------+-------+-------+-------+-------+
190 * 5 |G| offset3 |
191 * +-------+-------+-------+-------+-------+-------+-------+-------+
9b67f605 192 * 6 |BDX|lvl| type | cksum |E| comp| PSIZE | LSIZE |
34dc7c2f
BB
193 * +-------+-------+-------+-------+-------+-------+-------+-------+
194 * 7 | padding |
195 * +-------+-------+-------+-------+-------+-------+-------+-------+
196 * 8 | padding |
197 * +-------+-------+-------+-------+-------+-------+-------+-------+
428870ff 198 * 9 | physical birth txg |
34dc7c2f 199 * +-------+-------+-------+-------+-------+-------+-------+-------+
428870ff 200 * a | logical birth txg |
34dc7c2f
BB
201 * +-------+-------+-------+-------+-------+-------+-------+-------+
202 * b | fill count |
203 * +-------+-------+-------+-------+-------+-------+-------+-------+
204 * c | checksum[0] |
205 * +-------+-------+-------+-------+-------+-------+-------+-------+
206 * d | checksum[1] |
207 * +-------+-------+-------+-------+-------+-------+-------+-------+
208 * e | checksum[2] |
209 * +-------+-------+-------+-------+-------+-------+-------+-------+
210 * f | checksum[3] |
211 * +-------+-------+-------+-------+-------+-------+-------+-------+
212 *
213 * Legend:
214 *
215 * vdev virtual device ID
216 * offset offset into virtual device
217 * LSIZE logical size
218 * PSIZE physical size (after compression)
219 * ASIZE allocated size (including RAID-Z parity and gang block headers)
220 * GRID RAID-Z layout information (reserved for future use)
221 * cksum checksum function
222 * comp compression function
223 * G gang block indicator
428870ff
BB
224 * B byteorder (endianness)
225 * D dedup
b5256303 226 * X encryption
9b67f605 227 * E blkptr_t contains embedded data (see below)
34dc7c2f 228 * lvl level of indirection
428870ff 229 * type DMU object type
a1d477c2
MA
230 * phys birth txg when dva[0] was written; zero if same as logical birth txg
231 * note that typically all the dva's would be written in this
232 * txg, but they could be different if they were moved by
233 * device removal.
428870ff 234 * log. birth transaction group in which the block was logically born
34dc7c2f
BB
235 * fill count number of non-zero blocks under this bp
236 * checksum[4] 256-bit checksum of the data this bp describes
237 */
9b67f605 238
b5256303
TC
239/*
240 * The blkptr_t's of encrypted blocks also need to store the encryption
241 * parameters so that the block can be decrypted. This layout is as follows:
242 *
243 * 64 56 48 40 32 24 16 8 0
244 * +-------+-------+-------+-------+-------+-------+-------+-------+
245 * 0 | vdev1 | GRID | ASIZE |
246 * +-------+-------+-------+-------+-------+-------+-------+-------+
247 * 1 |G| offset1 |
248 * +-------+-------+-------+-------+-------+-------+-------+-------+
249 * 2 | vdev2 | GRID | ASIZE |
250 * +-------+-------+-------+-------+-------+-------+-------+-------+
251 * 3 |G| offset2 |
252 * +-------+-------+-------+-------+-------+-------+-------+-------+
253 * 4 | salt |
254 * +-------+-------+-------+-------+-------+-------+-------+-------+
255 * 5 | IV1 |
256 * +-------+-------+-------+-------+-------+-------+-------+-------+
257 * 6 |BDX|lvl| type | cksum |E| comp| PSIZE | LSIZE |
258 * +-------+-------+-------+-------+-------+-------+-------+-------+
259 * 7 | padding |
260 * +-------+-------+-------+-------+-------+-------+-------+-------+
261 * 8 | padding |
262 * +-------+-------+-------+-------+-------+-------+-------+-------+
263 * 9 | physical birth txg |
264 * +-------+-------+-------+-------+-------+-------+-------+-------+
265 * a | logical birth txg |
266 * +-------+-------+-------+-------+-------+-------+-------+-------+
267 * b | IV2 | fill count |
268 * +-------+-------+-------+-------+-------+-------+-------+-------+
269 * c | checksum[0] |
270 * +-------+-------+-------+-------+-------+-------+-------+-------+
271 * d | checksum[1] |
272 * +-------+-------+-------+-------+-------+-------+-------+-------+
273 * e | MAC[0] |
274 * +-------+-------+-------+-------+-------+-------+-------+-------+
275 * f | MAC[1] |
276 * +-------+-------+-------+-------+-------+-------+-------+-------+
277 *
278 * Legend:
279 *
280 * salt Salt for generating encryption keys
281 * IV1 First 64 bits of encryption IV
282 * X Block requires encryption handling (set to 1)
283 * E blkptr_t contains embedded data (set to 0, see below)
284 * fill count number of non-zero blocks under this bp (truncated to 32 bits)
285 * IV2 Last 32 bits of encryption IV
286 * checksum[2] 128-bit checksum of the data this bp describes
287 * MAC[2] 128-bit message authentication code for this data
288 *
289 * The X bit being set indicates that this block is one of 3 types. If this is
290 * a level 0 block with an encrypted object type, the block is encrypted
291 * (see BP_IS_ENCRYPTED()). If this is a level 0 block with an unencrypted
292 * object type, this block is authenticated with an HMAC (see
293 * BP_IS_AUTHENTICATED()). Otherwise (if level > 0), this bp will use the MAC
294 * words to store a checksum-of-MACs from the level below (see
295 * BP_HAS_INDIRECT_MAC_CKSUM()). For convenience in the code, BP_IS_PROTECTED()
296 * refers to both encrypted and authenticated blocks and BP_USES_CRYPT()
297 * refers to any of these 3 kinds of blocks.
298 *
299 * The additional encryption parameters are the salt, IV, and MAC which are
300 * explained in greater detail in the block comment at the top of zio_crypt.c.
301 * The MAC occupies half of the checksum space since it serves a very similar
302 * purpose: to prevent data corruption on disk. The only functional difference
303 * is that the checksum is used to detect on-disk corruption whether or not the
304 * encryption key is loaded and the MAC provides additional protection against
305 * malicious disk tampering. We use the 3rd DVA to store the salt and first
306 * 64 bits of the IV. As a result encrypted blocks can only have 2 copies
307 * maximum instead of the normal 3. The last 32 bits of the IV are stored in
308 * the upper bits of what is usually the fill count. Note that only blocks at
309 * level 0 or -2 are ever encrypted, which allows us to guarantee that these
310 * 32 bits are not trampled over by other code (see zio_crypt.c for details).
311 * The salt and IV are not used for authenticated bps or bps with an indirect
312 * MAC checksum, so these blocks can utilize all 3 DVAs and the full 64 bits
313 * for the fill count.
314 */
315
9b67f605
MA
316/*
317 * "Embedded" blkptr_t's don't actually point to a block, instead they
318 * have a data payload embedded in the blkptr_t itself. See the comment
319 * in blkptr.c for more details.
320 *
321 * The blkptr_t is laid out as follows:
322 *
323 * 64 56 48 40 32 24 16 8 0
324 * +-------+-------+-------+-------+-------+-------+-------+-------+
325 * 0 | payload |
326 * 1 | payload |
327 * 2 | payload |
328 * 3 | payload |
329 * 4 | payload |
330 * 5 | payload |
331 * +-------+-------+-------+-------+-------+-------+-------+-------+
332 * 6 |BDX|lvl| type | etype |E| comp| PSIZE| LSIZE |
333 * +-------+-------+-------+-------+-------+-------+-------+-------+
334 * 7 | payload |
335 * 8 | payload |
336 * 9 | payload |
337 * +-------+-------+-------+-------+-------+-------+-------+-------+
338 * a | logical birth txg |
339 * +-------+-------+-------+-------+-------+-------+-------+-------+
340 * b | payload |
341 * c | payload |
342 * d | payload |
343 * e | payload |
344 * f | payload |
345 * +-------+-------+-------+-------+-------+-------+-------+-------+
346 *
347 * Legend:
348 *
349 * payload contains the embedded data
350 * B (byteorder) byteorder (endianness)
351 * D (dedup) padding (set to zero)
b5256303 352 * X encryption (set to zero)
9b67f605
MA
353 * E (embedded) set to one
354 * lvl indirection level
355 * type DMU object type
356 * etype how to interpret embedded data (BP_EMBEDDED_TYPE_*)
357 * comp compression function of payload
358 * PSIZE size of payload after compression, in bytes
359 * LSIZE logical size of payload, in bytes
360 * note that 25 bits is enough to store the largest
361 * "normal" BP's LSIZE (2^16 * 2^9) in bytes
362 * log. birth transaction group in which the block was logically born
363 *
364 * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded
365 * bp's they are stored in units of SPA_MINBLOCKSHIFT.
366 * Generally, the generic BP_GET_*() macros can be used on embedded BP's.
367 * The B, D, X, lvl, type, and comp fields are stored the same as with normal
368 * BP's so the BP_SET_* macros can be used with them. etype, PSIZE, LSIZE must
369 * be set with the BPE_SET_* macros. BP_SET_EMBEDDED() should be called before
370 * other macros, as they assert that they are only used on BP's of the correct
b5256303
TC
371 * "embedded-ness". Encrypted blkptr_t's cannot be embedded because they use
372 * the payload space for encryption parameters (see the comment above on
373 * how encryption parameters are stored).
9b67f605
MA
374 */
375
376#define BPE_GET_ETYPE(bp) \
377 (ASSERT(BP_IS_EMBEDDED(bp)), \
378 BF64_GET((bp)->blk_prop, 40, 8))
379#define BPE_SET_ETYPE(bp, t) do { \
380 ASSERT(BP_IS_EMBEDDED(bp)); \
381 BF64_SET((bp)->blk_prop, 40, 8, t); \
382_NOTE(CONSTCOND) } while (0)
383
384#define BPE_GET_LSIZE(bp) \
385 (ASSERT(BP_IS_EMBEDDED(bp)), \
386 BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1))
387#define BPE_SET_LSIZE(bp, x) do { \
388 ASSERT(BP_IS_EMBEDDED(bp)); \
389 BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \
390_NOTE(CONSTCOND) } while (0)
391
392#define BPE_GET_PSIZE(bp) \
393 (ASSERT(BP_IS_EMBEDDED(bp)), \
394 BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1))
395#define BPE_SET_PSIZE(bp, x) do { \
396 ASSERT(BP_IS_EMBEDDED(bp)); \
397 BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \
398_NOTE(CONSTCOND) } while (0)
399
400typedef enum bp_embedded_type {
401 BP_EMBEDDED_TYPE_DATA,
402 BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */
403 NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED
404} bp_embedded_type_t;
405
406#define BPE_NUM_WORDS 14
407#define BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t))
408#define BPE_IS_PAYLOADWORD(bp, wp) \
409 ((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth)
410
34dc7c2f
BB
411#define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */
412#define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */
413
b0bc7a84
MG
414/*
415 * A block is a hole when it has either 1) never been written to, or
416 * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
417 * without physically allocating disk space. Holes are represented in the
418 * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
419 * done through the BP_IS_HOLE macro. For holes, the logical size, level,
420 * DMU object type, and birth times are all also stored for holes that
421 * were written to at some point (i.e. were punched after having been filled).
422 */
428870ff
BB
423typedef struct blkptr {
424 dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
425 uint64_t blk_prop; /* size, compression, type, etc */
426 uint64_t blk_pad[2]; /* Extra space for the future */
427 uint64_t blk_phys_birth; /* txg when block was allocated */
428 uint64_t blk_birth; /* transaction group at birth */
429 uint64_t blk_fill; /* fill count */
430 zio_cksum_t blk_cksum; /* 256-bit checksum */
431} blkptr_t;
432
34dc7c2f
BB
433/*
434 * Macros to get and set fields in a bp or DVA.
435 */
436#define DVA_GET_ASIZE(dva) \
b0bc7a84 437 BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
34dc7c2f 438#define DVA_SET_ASIZE(dva, x) \
b0bc7a84
MG
439 BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
440 SPA_MINBLOCKSHIFT, 0, x)
34dc7c2f
BB
441
442#define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8)
443#define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x)
444
445#define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32)
446#define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x)
447
448#define DVA_GET_OFFSET(dva) \
449 BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
450#define DVA_SET_OFFSET(dva, x) \
451 BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
452
453#define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1)
454#define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x)
455
456#define BP_GET_LSIZE(bp) \
9b67f605
MA
457 (BP_IS_EMBEDDED(bp) ? \
458 (BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
459 BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
460#define BP_SET_LSIZE(bp, x) do { \
461 ASSERT(!BP_IS_EMBEDDED(bp)); \
462 BF64_SET_SB((bp)->blk_prop, \
463 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
464_NOTE(CONSTCOND) } while (0)
34dc7c2f
BB
465
466#define BP_GET_PSIZE(bp) \
9b67f605
MA
467 (BP_IS_EMBEDDED(bp) ? 0 : \
468 BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
469#define BP_SET_PSIZE(bp, x) do { \
470 ASSERT(!BP_IS_EMBEDDED(bp)); \
471 BF64_SET_SB((bp)->blk_prop, \
472 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
473_NOTE(CONSTCOND) } while (0)
474
d3c2ae1c
GW
475#define BP_GET_COMPRESS(bp) \
476 BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
477#define BP_SET_COMPRESS(bp, x) \
478 BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
34dc7c2f 479
9b67f605
MA
480#define BP_IS_EMBEDDED(bp) BF64_GET((bp)->blk_prop, 39, 1)
481#define BP_SET_EMBEDDED(bp, x) BF64_SET((bp)->blk_prop, 39, 1, x)
428870ff 482
9b67f605
MA
483#define BP_GET_CHECKSUM(bp) \
484 (BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
485 BF64_GET((bp)->blk_prop, 40, 8))
486#define BP_SET_CHECKSUM(bp, x) do { \
487 ASSERT(!BP_IS_EMBEDDED(bp)); \
488 BF64_SET((bp)->blk_prop, 40, 8, x); \
489_NOTE(CONSTCOND) } while (0)
34dc7c2f 490
428870ff
BB
491#define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8)
492#define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x)
34dc7c2f 493
428870ff
BB
494#define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5)
495#define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x)
34dc7c2f 496
b5256303
TC
497/* encrypted, authenticated, and MAC cksum bps use the same bit */
498#define BP_USES_CRYPT(bp) BF64_GET((bp)->blk_prop, 61, 1)
499#define BP_SET_CRYPT(bp, x) BF64_SET((bp)->blk_prop, 61, 1, x)
500
501#define BP_IS_ENCRYPTED(bp) \
502 (BP_USES_CRYPT(bp) && \
503 BP_GET_LEVEL(bp) <= 0 && \
504 DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
505
506#define BP_IS_AUTHENTICATED(bp) \
507 (BP_USES_CRYPT(bp) && \
508 BP_GET_LEVEL(bp) <= 0 && \
509 !DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
510
511#define BP_HAS_INDIRECT_MAC_CKSUM(bp) \
512 (BP_USES_CRYPT(bp) && BP_GET_LEVEL(bp) > 0)
513
514#define BP_IS_PROTECTED(bp) \
515 (BP_IS_ENCRYPTED(bp) || BP_IS_AUTHENTICATED(bp))
516
428870ff
BB
517#define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1)
518#define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x)
519
b0bc7a84 520#define BP_GET_BYTEORDER(bp) BF64_GET((bp)->blk_prop, 63, 1)
428870ff
BB
521#define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x)
522
523#define BP_PHYSICAL_BIRTH(bp) \
9b67f605
MA
524 (BP_IS_EMBEDDED(bp) ? 0 : \
525 (bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
428870ff
BB
526
527#define BP_SET_BIRTH(bp, logical, physical) \
528{ \
9b67f605 529 ASSERT(!BP_IS_EMBEDDED(bp)); \
428870ff
BB
530 (bp)->blk_birth = (logical); \
531 (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
532}
34dc7c2f 533
b5256303
TC
534#define BP_GET_FILL(bp) \
535 ((BP_IS_ENCRYPTED(bp)) ? BF64_GET((bp)->blk_fill, 0, 32) : \
536 ((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill))
537
538#define BP_SET_FILL(bp, fill) \
539{ \
540 if (BP_IS_ENCRYPTED(bp)) \
541 BF64_SET((bp)->blk_fill, 0, 32, fill); \
542 else \
543 (bp)->blk_fill = fill; \
544}
545
546#define BP_GET_IV2(bp) \
547 (ASSERT(BP_IS_ENCRYPTED(bp)), \
548 BF64_GET((bp)->blk_fill, 32, 32))
549#define BP_SET_IV2(bp, iv2) \
550{ \
551 ASSERT(BP_IS_ENCRYPTED(bp)); \
552 BF64_SET((bp)->blk_fill, 32, 32, iv2); \
553}
9b67f605 554
a6255b7f
DQ
555#define BP_IS_METADATA(bp) \
556 (BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
557
34dc7c2f 558#define BP_GET_ASIZE(bp) \
9b67f605
MA
559 (BP_IS_EMBEDDED(bp) ? 0 : \
560 DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
561 DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
b5256303 562 (DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
34dc7c2f 563
a6255b7f
DQ
564#define BP_GET_UCSIZE(bp) \
565 (BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
34dc7c2f
BB
566
567#define BP_GET_NDVAS(bp) \
9b67f605
MA
568 (BP_IS_EMBEDDED(bp) ? 0 : \
569 !!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
34dc7c2f 570 !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
b5256303 571 (!!DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
34dc7c2f
BB
572
573#define BP_COUNT_GANG(bp) \
9b67f605 574 (BP_IS_EMBEDDED(bp) ? 0 : \
34dc7c2f
BB
575 (DVA_GET_GANG(&(bp)->blk_dva[0]) + \
576 DVA_GET_GANG(&(bp)->blk_dva[1]) + \
b5256303 577 (DVA_GET_GANG(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp))))
34dc7c2f
BB
578
579#define DVA_EQUAL(dva1, dva2) \
580 ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
581 (dva1)->dva_word[0] == (dva2)->dva_word[0])
582
428870ff
BB
583#define BP_EQUAL(bp1, bp2) \
584 (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \
9b67f605 585 (bp1)->blk_birth == (bp2)->blk_birth && \
428870ff
BB
586 DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \
587 DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \
588 DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
589
37f8a883 590
34dc7c2f
BB
591#define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0)
592
9b67f605
MA
593#define BP_IDENTITY(bp) (ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
594#define BP_IS_GANG(bp) \
595 (BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
b0bc7a84
MG
596#define DVA_IS_EMPTY(dva) ((dva)->dva_word[0] == 0ULL && \
597 (dva)->dva_word[1] == 0ULL)
9b67f605
MA
598#define BP_IS_HOLE(bp) \
599 (!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
428870ff
BB
600
601/* BP_IS_RAIDZ(bp) assumes no block compression */
602#define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
603 BP_GET_PSIZE(bp))
34dc7c2f 604
b128c09f 605#define BP_ZERO(bp) \
34dc7c2f
BB
606{ \
607 (bp)->blk_dva[0].dva_word[0] = 0; \
608 (bp)->blk_dva[0].dva_word[1] = 0; \
609 (bp)->blk_dva[1].dva_word[0] = 0; \
610 (bp)->blk_dva[1].dva_word[1] = 0; \
611 (bp)->blk_dva[2].dva_word[0] = 0; \
612 (bp)->blk_dva[2].dva_word[1] = 0; \
34dc7c2f
BB
613 (bp)->blk_prop = 0; \
614 (bp)->blk_pad[0] = 0; \
615 (bp)->blk_pad[1] = 0; \
428870ff 616 (bp)->blk_phys_birth = 0; \
b128c09f 617 (bp)->blk_birth = 0; \
34dc7c2f
BB
618 (bp)->blk_fill = 0; \
619 ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \
620}
621
34dc7c2f
BB
622#ifdef _BIG_ENDIAN
623#define ZFS_HOST_BYTEORDER (0ULL)
624#else
b0bc7a84 625#define ZFS_HOST_BYTEORDER (1ULL)
34dc7c2f
BB
626#endif
627
628#define BP_SHOULD_BYTESWAP(bp) (BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
629
b5256303 630#define BP_SPRINTF_LEN 400
34dc7c2f 631
428870ff
BB
632/*
633 * This macro allows code sharing between zfs, libzpool, and mdb.
634 * 'func' is either snprintf() or mdb_snprintf().
635 * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
636 */
5c27ec10 637#define SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
428870ff
BB
638{ \
639 static const char *copyname[] = \
640 { "zero", "single", "double", "triple" }; \
428870ff
BB
641 int len = 0; \
642 int copies = 0; \
5c27ec10
MA
643 const char *crypt_type; \
644 if (bp != NULL) { \
645 if (BP_IS_ENCRYPTED(bp)) { \
646 crypt_type = "encrypted"; \
647 /* LINTED E_SUSPICIOUS_COMPARISON */ \
648 } else if (BP_IS_AUTHENTICATED(bp)) { \
649 crypt_type = "authenticated"; \
650 } else if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) { \
651 crypt_type = "indirect-MAC"; \
652 } else { \
653 crypt_type = "unencrypted"; \
654 } \
655 } \
428870ff 656 if (bp == NULL) { \
b0bc7a84 657 len += func(buf + len, size - len, "<NULL>"); \
428870ff 658 } else if (BP_IS_HOLE(bp)) { \
a4069eef
PS
659 len += func(buf + len, size - len, \
660 "HOLE [L%llu %s] " \
661 "size=%llxL birth=%lluL", \
662 (u_longlong_t)BP_GET_LEVEL(bp), \
663 type, \
664 (u_longlong_t)BP_GET_LSIZE(bp), \
665 (u_longlong_t)bp->blk_birth); \
9b67f605
MA
666 } else if (BP_IS_EMBEDDED(bp)) { \
667 len = func(buf + len, size - len, \
668 "EMBEDDED [L%llu %s] et=%u %s " \
669 "size=%llxL/%llxP birth=%lluL", \
670 (u_longlong_t)BP_GET_LEVEL(bp), \
671 type, \
672 (int)BPE_GET_ETYPE(bp), \
673 compress, \
674 (u_longlong_t)BPE_GET_LSIZE(bp), \
675 (u_longlong_t)BPE_GET_PSIZE(bp), \
676 (u_longlong_t)bp->blk_birth); \
428870ff 677 } else { \
1c27024e 678 for (int d = 0; d < BP_GET_NDVAS(bp); d++) { \
428870ff
BB
679 const dva_t *dva = &bp->blk_dva[d]; \
680 if (DVA_IS_VALID(dva)) \
681 copies++; \
682 len += func(buf + len, size - len, \
683 "DVA[%d]=<%llu:%llx:%llx>%c", d, \
684 (u_longlong_t)DVA_GET_VDEV(dva), \
685 (u_longlong_t)DVA_GET_OFFSET(dva), \
686 (u_longlong_t)DVA_GET_ASIZE(dva), \
687 ws); \
688 } \
b5256303
TC
689 if (BP_IS_ENCRYPTED(bp)) { \
690 len += func(buf + len, size - len, \
691 "salt=%llx iv=%llx:%llx%c", \
692 (u_longlong_t)bp->blk_dva[2].dva_word[0], \
693 (u_longlong_t)bp->blk_dva[2].dva_word[1], \
694 (u_longlong_t)BP_GET_IV2(bp), \
695 ws); \
696 } \
428870ff
BB
697 if (BP_IS_GANG(bp) && \
698 DVA_GET_ASIZE(&bp->blk_dva[2]) <= \
699 DVA_GET_ASIZE(&bp->blk_dva[1]) / 2) \
700 copies--; \
701 len += func(buf + len, size - len, \
b5256303 702 "[L%llu %s] %s %s %s %s %s %s %s%c" \
428870ff
BB
703 "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c" \
704 "cksum=%llx:%llx:%llx:%llx", \
705 (u_longlong_t)BP_GET_LEVEL(bp), \
706 type, \
707 checksum, \
708 compress, \
b5256303 709 crypt_type, \
428870ff
BB
710 BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE", \
711 BP_IS_GANG(bp) ? "gang" : "contiguous", \
712 BP_GET_DEDUP(bp) ? "dedup" : "unique", \
713 copyname[copies], \
714 ws, \
715 (u_longlong_t)BP_GET_LSIZE(bp), \
716 (u_longlong_t)BP_GET_PSIZE(bp), \
717 (u_longlong_t)bp->blk_birth, \
718 (u_longlong_t)BP_PHYSICAL_BIRTH(bp), \
9b67f605 719 (u_longlong_t)BP_GET_FILL(bp), \
428870ff
BB
720 ws, \
721 (u_longlong_t)bp->blk_cksum.zc_word[0], \
722 (u_longlong_t)bp->blk_cksum.zc_word[1], \
723 (u_longlong_t)bp->blk_cksum.zc_word[2], \
724 (u_longlong_t)bp->blk_cksum.zc_word[3]); \
725 } \
726 ASSERT(len < size); \
727}
728
34dc7c2f 729#define BP_GET_BUFC_TYPE(bp) \
a6255b7f 730 (BP_IS_METADATA(bp) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
428870ff
BB
731
732typedef enum spa_import_type {
733 SPA_IMPORT_EXISTING,
734 SPA_IMPORT_ASSEMBLE
735} spa_import_type_t;
34dc7c2f
BB
736
737/* state manipulation functions */
738extern int spa_open(const char *pool, spa_t **, void *tag);
428870ff
BB
739extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
740 nvlist_t *policy, nvlist_t **config);
9ae529ec
CS
741extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
742 size_t buflen);
b5256303
TC
743extern int spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
744 nvlist_t *zplprops, struct dsl_crypto_params *dcp);
13fe0198 745extern int spa_import(char *pool, nvlist_t *config, nvlist_t *props,
572e2857 746 uint64_t flags);
34dc7c2f
BB
747extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
748extern int spa_destroy(char *pool);
fb5f0bc8
BB
749extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
750 boolean_t hardforce);
34dc7c2f
BB
751extern int spa_reset(char *pool);
752extern void spa_async_request(spa_t *spa, int flag);
b128c09f 753extern void spa_async_unrequest(spa_t *spa, int flag);
34dc7c2f
BB
754extern void spa_async_suspend(spa_t *spa);
755extern void spa_async_resume(spa_t *spa);
756extern spa_t *spa_inject_addref(char *pool);
757extern void spa_inject_delref(spa_t *spa);
428870ff
BB
758extern void spa_scan_stat_init(spa_t *spa);
759extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
34dc7c2f 760
b128c09f
BB
761#define SPA_ASYNC_CONFIG_UPDATE 0x01
762#define SPA_ASYNC_REMOVE 0x02
763#define SPA_ASYNC_PROBE 0x04
764#define SPA_ASYNC_RESILVER_DONE 0x08
765#define SPA_ASYNC_RESILVER 0x10
9babb374 766#define SPA_ASYNC_AUTOEXPAND 0x20
428870ff
BB
767#define SPA_ASYNC_REMOVE_DONE 0x40
768#define SPA_ASYNC_REMOVE_STOP 0x80
769
770/*
771 * Controls the behavior of spa_vdev_remove().
772 */
773#define SPA_REMOVE_UNSPARE 0x01
774#define SPA_REMOVE_DONE 0x02
34dc7c2f
BB
775
776/* device manipulation */
777extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
778extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
779 int replacing);
fb5f0bc8
BB
780extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
781 int replace_done);
34dc7c2f 782extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
428870ff 783extern boolean_t spa_vdev_remove_active(spa_t *spa);
34dc7c2f 784extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
9babb374 785extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
428870ff
BB
786extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
787 nvlist_t *props, boolean_t exp);
34dc7c2f
BB
788
789/* spare state (which is global across all pools) */
790extern void spa_spare_add(vdev_t *vd);
791extern void spa_spare_remove(vdev_t *vd);
b128c09f 792extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
34dc7c2f
BB
793extern void spa_spare_activate(vdev_t *vd);
794
795/* L2ARC state (which is global across all pools) */
796extern void spa_l2cache_add(vdev_t *vd);
797extern void spa_l2cache_remove(vdev_t *vd);
798extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
799extern void spa_l2cache_activate(vdev_t *vd);
800extern void spa_l2cache_drop(spa_t *spa);
34dc7c2f 801
428870ff
BB
802/* scanning */
803extern int spa_scan(spa_t *spa, pool_scan_func_t func);
804extern int spa_scan_stop(spa_t *spa);
0ea05c64 805extern int spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t flag);
34dc7c2f
BB
806
807/* spa syncing */
808extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
809extern void spa_sync_allpools(void);
810
55d85d5a 811extern int zfs_sync_pass_deferred_free;
428870ff 812
b128c09f
BB
813/* spa namespace global mutex */
814extern kmutex_t spa_namespace_lock;
815
34dc7c2f
BB
816/*
817 * SPA configuration functions in spa_config.c
818 */
819
820#define SPA_CONFIG_UPDATE_POOL 0
821#define SPA_CONFIG_UPDATE_VDEVS 1
822
a1d477c2 823extern void spa_write_cachefile(spa_t *, boolean_t, boolean_t);
34dc7c2f
BB
824extern void spa_config_load(void);
825extern nvlist_t *spa_all_configs(uint64_t *);
826extern void spa_config_set(spa_t *spa, nvlist_t *config);
827extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
828 int getstats);
829extern void spa_config_update(spa_t *spa, int what);
34dc7c2f
BB
830
831/*
832 * Miscellaneous SPA routines in spa_misc.c
833 */
834
835/* Namespace manipulation */
836extern spa_t *spa_lookup(const char *name);
428870ff 837extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
34dc7c2f
BB
838extern void spa_remove(spa_t *spa);
839extern spa_t *spa_next(spa_t *prev);
840
841/* Refcount functions */
842extern void spa_open_ref(spa_t *spa, void *tag);
843extern void spa_close(spa_t *spa, void *tag);
0c66c32d 844extern void spa_async_close(spa_t *spa, void *tag);
34dc7c2f
BB
845extern boolean_t spa_refcount_zero(spa_t *spa);
846
428870ff 847#define SCL_NONE 0x00
b128c09f
BB
848#define SCL_CONFIG 0x01
849#define SCL_STATE 0x02
850#define SCL_L2ARC 0x04 /* hack until L2ARC 2.0 */
851#define SCL_ALLOC 0x08
852#define SCL_ZIO 0x10
853#define SCL_FREE 0x20
854#define SCL_VDEV 0x40
855#define SCL_LOCKS 7
856#define SCL_ALL ((1 << SCL_LOCKS) - 1)
857#define SCL_STATE_ALL (SCL_STATE | SCL_L2ARC | SCL_ZIO)
858
1421c891
PS
859/* Historical pool statistics */
860typedef struct spa_stats_history {
861 kmutex_t lock;
862 uint64_t count;
863 uint64_t size;
864 kstat_t *kstat;
865 void *private;
866 list_t list;
867} spa_stats_history_t;
868
869typedef struct spa_stats {
870 spa_stats_history_t read_history;
0b1401ee 871 spa_stats_history_t txg_history;
2d37239a 872 spa_stats_history_t tx_assign_histogram;
330847ff 873 spa_stats_history_t io_history;
379ca9cf 874 spa_stats_history_t mmp_history;
1421c891
PS
875} spa_stats_t;
876
0b1401ee
BB
877typedef enum txg_state {
878 TXG_STATE_BIRTH = 0,
879 TXG_STATE_OPEN = 1,
880 TXG_STATE_QUIESCED = 2,
478d64fd
IL
881 TXG_STATE_WAIT_FOR_SYNC = 3,
882 TXG_STATE_SYNCED = 4,
883 TXG_STATE_COMMITTED = 5,
0b1401ee
BB
884} txg_state_t;
885
baf67d15
BB
886typedef struct txg_stat {
887 vdev_stat_t vs1;
888 vdev_stat_t vs2;
889 uint64_t txg;
890 uint64_t ndirty;
891} txg_stat_t;
892
1421c891
PS
893extern void spa_stats_init(spa_t *spa);
894extern void spa_stats_destroy(spa_t *spa);
5dbd68a3 895extern void spa_read_history_add(spa_t *spa, const zbookmark_phys_t *zb,
1421c891 896 uint32_t aflags);
01b738f4 897extern void spa_txg_history_add(spa_t *spa, uint64_t txg, hrtime_t birth_time);
0b1401ee
BB
898extern int spa_txg_history_set(spa_t *spa, uint64_t txg,
899 txg_state_t completed_state, hrtime_t completed_time);
baf67d15
BB
900extern txg_stat_t *spa_txg_history_init_io(spa_t *, uint64_t,
901 struct dsl_pool *);
902extern void spa_txg_history_fini_io(spa_t *, txg_stat_t *);
2d37239a 903extern void spa_tx_assign_add_nsecs(spa_t *spa, uint64_t nsecs);
d2160d05 904extern int spa_mmp_history_set_skip(spa_t *spa, uint64_t mmp_kstat_id);
7088545d
OF
905extern int spa_mmp_history_set(spa_t *spa, uint64_t mmp_kstat_id, int io_error,
906 hrtime_t duration);
d2160d05
OF
907extern void *spa_mmp_history_add(spa_t *spa, uint64_t txg, uint64_t timestamp,
908 uint64_t mmp_delay, vdev_t *vd, int label, uint64_t mmp_kstat_id,
909 int error);
1421c891 910
b128c09f
BB
911/* Pool configuration locks */
912extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
913extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
914extern void spa_config_exit(spa_t *spa, int locks, void *tag);
915extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
34dc7c2f
BB
916
917/* Pool vdev add/remove lock */
918extern uint64_t spa_vdev_enter(spa_t *spa);
428870ff
BB
919extern uint64_t spa_vdev_config_enter(spa_t *spa);
920extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
921 int error, char *tag);
34dc7c2f
BB
922extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
923
b128c09f 924/* Pool vdev state change lock */
428870ff 925extern void spa_vdev_state_enter(spa_t *spa, int oplock);
b128c09f
BB
926extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
927
428870ff
BB
928/* Log state */
929typedef enum spa_log_state {
930 SPA_LOG_UNKNOWN = 0, /* unknown log state */
931 SPA_LOG_MISSING, /* missing log(s) */
932 SPA_LOG_CLEAR, /* clear the log(s) */
933 SPA_LOG_GOOD, /* log(s) are good */
934} spa_log_state_t;
935
936extern spa_log_state_t spa_get_log_state(spa_t *spa);
937extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
a1d477c2 938extern int spa_reset_logs(spa_t *spa);
428870ff
BB
939
940/* Log claim callback */
941extern void spa_claim_notify(zio_t *zio);
cc92e9d0 942extern void spa_deadman(void *);
428870ff 943
34dc7c2f 944/* Accessor functions */
b128c09f 945extern boolean_t spa_shutting_down(spa_t *spa);
34dc7c2f 946extern struct dsl_pool *spa_get_dsl(spa_t *spa);
9ae529ec 947extern boolean_t spa_is_initializing(spa_t *spa);
a1d477c2 948extern boolean_t spa_indirect_vdevs_loaded(spa_t *spa);
34dc7c2f
BB
949extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
950extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
951extern void spa_altroot(spa_t *, char *, size_t);
952extern int spa_sync_pass(spa_t *spa);
953extern char *spa_name(spa_t *spa);
954extern uint64_t spa_guid(spa_t *spa);
3541dc6d 955extern uint64_t spa_load_guid(spa_t *spa);
34dc7c2f
BB
956extern uint64_t spa_last_synced_txg(spa_t *spa);
957extern uint64_t spa_first_txg(spa_t *spa);
428870ff 958extern uint64_t spa_syncing_txg(spa_t *spa);
3b7f360c 959extern uint64_t spa_final_dirty_txg(spa_t *spa);
34dc7c2f 960extern uint64_t spa_version(spa_t *spa);
b128c09f 961extern pool_state_t spa_state(spa_t *spa);
428870ff 962extern spa_load_state_t spa_load_state(spa_t *spa);
34dc7c2f 963extern uint64_t spa_freeze_txg(spa_t *spa);
3ec3bc21 964extern uint64_t spa_get_worst_case_asize(spa_t *spa, uint64_t lsize);
428870ff 965extern uint64_t spa_get_dspace(spa_t *spa);
0c60cc32 966extern uint64_t spa_get_slop_space(spa_t *spa);
428870ff 967extern void spa_update_dspace(spa_t *spa);
34dc7c2f 968extern uint64_t spa_version(spa_t *spa);
428870ff
BB
969extern boolean_t spa_deflate(spa_t *spa);
970extern metaslab_class_t *spa_normal_class(spa_t *spa);
971extern metaslab_class_t *spa_log_class(spa_t *spa);
0c66c32d
JG
972extern void spa_evicting_os_register(spa_t *, objset_t *os);
973extern void spa_evicting_os_deregister(spa_t *, objset_t *os);
974extern void spa_evicting_os_wait(spa_t *spa);
34dc7c2f 975extern int spa_max_replication(spa_t *spa);
428870ff 976extern int spa_prev_software_version(spa_t *spa);
8fb1ede1
BB
977extern uint64_t spa_get_failmode(spa_t *spa);
978extern uint64_t spa_get_deadman_failmode(spa_t *spa);
979extern void spa_set_deadman_failmode(spa_t *spa, const char *failmode);
b128c09f 980extern boolean_t spa_suspended(spa_t *spa);
428870ff
BB
981extern uint64_t spa_bootfs(spa_t *spa);
982extern uint64_t spa_delegation(spa_t *spa);
983extern objset_t *spa_meta_objset(spa_t *spa);
cc92e9d0 984extern uint64_t spa_deadman_synctime(spa_t *spa);
8fb1ede1 985extern uint64_t spa_deadman_ziotime(spa_t *spa);
34dc7c2f
BB
986
987/* Miscellaneous support routines */
b0bc7a84
MG
988extern void spa_activate_mos_feature(spa_t *spa, const char *feature,
989 dmu_tx_t *tx);
9ae529ec 990extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
34dc7c2f 991extern int spa_rename(const char *oldname, const char *newname);
572e2857 992extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
34dc7c2f
BB
993extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
994extern char *spa_strdup(const char *);
995extern void spa_strfree(char *);
996extern uint64_t spa_get_random(uint64_t range);
428870ff 997extern uint64_t spa_generate_guid(spa_t *spa);
b0bc7a84 998extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
34dc7c2f 999extern void spa_freeze(spa_t *spa);
3541dc6d 1000extern int spa_change_guid(spa_t *spa);
34dc7c2f
BB
1001extern void spa_upgrade(spa_t *spa, uint64_t version);
1002extern void spa_evict_all(void);
b128c09f
BB
1003extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
1004 boolean_t l2cache);
34dc7c2f 1005extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
428870ff
BB
1006extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
1007extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
1008extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
34dc7c2f 1009extern boolean_t spa_has_slogs(spa_t *spa);
b128c09f 1010extern boolean_t spa_is_root(spa_t *spa);
fb5f0bc8 1011extern boolean_t spa_writeable(spa_t *spa);
acbad6ff 1012extern boolean_t spa_has_pending_synctask(spa_t *spa);
f1512ee6 1013extern int spa_maxblocksize(spa_t *spa);
50c957f7 1014extern int spa_maxdnodesize(spa_t *spa);
63e3a861 1015extern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp);
a1d477c2
MA
1016typedef void (*spa_remap_cb_t)(uint64_t vdev, uint64_t offset, uint64_t size,
1017 void *arg);
1018extern boolean_t spa_remap_blkptr(spa_t *spa, blkptr_t *bp,
1019 spa_remap_cb_t callback, void *arg);
1020extern uint64_t spa_get_last_removal_txg(spa_t *spa);
379ca9cf
OF
1021extern boolean_t spa_multihost(spa_t *spa);
1022extern unsigned long spa_get_hostid(void);
428870ff 1023
fb5f0bc8 1024extern int spa_mode(spa_t *spa);
e19572e4 1025extern uint64_t zfs_strtonum(const char *str, char **nptr);
34dc7c2f 1026
34dc7c2f
BB
1027extern char *spa_his_ievent_table[];
1028
1029extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
1030extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
1031 char *his_buf);
6f1ffb06
MA
1032extern int spa_history_log(spa_t *spa, const char *his_buf);
1033extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
d5e024cb
BB
1034extern void spa_history_log_version(spa_t *spa, const char *operation,
1035 dmu_tx_t *tx);
6f1ffb06
MA
1036extern void spa_history_log_internal(spa_t *spa, const char *operation,
1037 dmu_tx_t *tx, const char *fmt, ...);
1038extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
1039 dmu_tx_t *tx, const char *fmt, ...);
1040extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
1041 dmu_tx_t *tx, const char *fmt, ...);
34dc7c2f
BB
1042
1043/* error handling */
5dbd68a3 1044struct zbookmark_phys;
b5256303 1045extern void spa_log_error(spa_t *spa, const zbookmark_phys_t *zb);
34dc7c2f 1046extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
a2c2ed1b
TC
1047 const zbookmark_phys_t *zb, zio_t *zio, uint64_t stateoroffset,
1048 uint64_t length);
12fa0466
DE
1049extern nvlist_t *zfs_event_create(spa_t *spa, vdev_t *vd, const char *type,
1050 const char *name, nvlist_t *aux);
34dc7c2f 1051extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
d02ca379 1052extern void zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate);
34dc7c2f
BB
1053extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
1054extern uint64_t spa_get_errlog_size(spa_t *spa);
1055extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
1056extern void spa_errlog_rotate(spa_t *spa);
1057extern void spa_errlog_drain(spa_t *spa);
1058extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
1059extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
1060
1061/* vdev cache */
1062extern void vdev_cache_stat_init(void);
1063extern void vdev_cache_stat_fini(void);
1064
551905dd
GN
1065/* vdev mirror */
1066extern void vdev_mirror_stat_init(void);
1067extern void vdev_mirror_stat_fini(void);
1068
34dc7c2f
BB
1069/* Initialization and termination */
1070extern void spa_init(int flags);
1071extern void spa_fini(void);
0bc8fd78 1072extern void spa_boot_init(void);
34dc7c2f
BB
1073
1074/* properties */
1075extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
1076extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
1077extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
d164b209 1078extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
34dc7c2f
BB
1079
1080/* asynchronous event notification */
12fa0466
DE
1081extern void spa_event_notify(spa_t *spa, vdev_t *vdev, nvlist_t *hist_nvl,
1082 const char *name);
34dc7c2f
BB
1083
1084#ifdef ZFS_DEBUG
79c76d5b
BB
1085#define dprintf_bp(bp, fmt, ...) do { \
1086 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
1087 char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP); \
1088 snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp)); \
1089 dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf); \
1090 kmem_free(__blkbuf, BP_SPRINTF_LEN); \
34dc7c2f
BB
1091 } \
1092_NOTE(CONSTCOND) } while (0)
1093#else
1094#define dprintf_bp(bp, fmt, ...)
1095#endif
1096
fb5f0bc8 1097extern int spa_mode_global; /* mode, e.g. FREAD | FWRITE */
8fb1ede1
BB
1098extern int zfs_deadman_enabled;
1099extern unsigned long zfs_deadman_synctime_ms;
1100extern unsigned long zfs_deadman_ziotime_ms;
1101extern unsigned long zfs_deadman_checktime_ms;
34dc7c2f
BB
1102
1103#ifdef __cplusplus
1104}
1105#endif
1106
1107#endif /* _SYS_SPA_H */