]> git.proxmox.com Git - mirror_zfs.git/blame - include/sys/spa.h
Use "eval" in history_002_pos for log_must
[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 */
6cb8e530 413#define SPA_SYNC_MIN_VDEVS 3 /* min vdevs to update during sync */
34dc7c2f 414
b0bc7a84
MG
415/*
416 * A block is a hole when it has either 1) never been written to, or
417 * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads
418 * without physically allocating disk space. Holes are represented in the
419 * blkptr_t structure by zeroed blk_dva. Correct checking for holes is
420 * done through the BP_IS_HOLE macro. For holes, the logical size, level,
421 * DMU object type, and birth times are all also stored for holes that
422 * were written to at some point (i.e. were punched after having been filled).
423 */
428870ff
BB
424typedef struct blkptr {
425 dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
426 uint64_t blk_prop; /* size, compression, type, etc */
427 uint64_t blk_pad[2]; /* Extra space for the future */
428 uint64_t blk_phys_birth; /* txg when block was allocated */
429 uint64_t blk_birth; /* transaction group at birth */
430 uint64_t blk_fill; /* fill count */
431 zio_cksum_t blk_cksum; /* 256-bit checksum */
432} blkptr_t;
433
34dc7c2f
BB
434/*
435 * Macros to get and set fields in a bp or DVA.
436 */
437#define DVA_GET_ASIZE(dva) \
b0bc7a84 438 BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0)
34dc7c2f 439#define DVA_SET_ASIZE(dva, x) \
b0bc7a84
MG
440 BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \
441 SPA_MINBLOCKSHIFT, 0, x)
34dc7c2f
BB
442
443#define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8)
444#define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x)
445
446#define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32)
447#define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x)
448
449#define DVA_GET_OFFSET(dva) \
450 BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
451#define DVA_SET_OFFSET(dva, x) \
452 BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
453
454#define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1)
455#define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x)
456
457#define BP_GET_LSIZE(bp) \
9b67f605
MA
458 (BP_IS_EMBEDDED(bp) ? \
459 (BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \
460 BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1))
461#define BP_SET_LSIZE(bp, x) do { \
462 ASSERT(!BP_IS_EMBEDDED(bp)); \
463 BF64_SET_SB((bp)->blk_prop, \
464 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
465_NOTE(CONSTCOND) } while (0)
34dc7c2f
BB
466
467#define BP_GET_PSIZE(bp) \
9b67f605
MA
468 (BP_IS_EMBEDDED(bp) ? 0 : \
469 BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1))
470#define BP_SET_PSIZE(bp, x) do { \
471 ASSERT(!BP_IS_EMBEDDED(bp)); \
472 BF64_SET_SB((bp)->blk_prop, \
473 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \
474_NOTE(CONSTCOND) } while (0)
475
d3c2ae1c
GW
476#define BP_GET_COMPRESS(bp) \
477 BF64_GET((bp)->blk_prop, 32, SPA_COMPRESSBITS)
478#define BP_SET_COMPRESS(bp, x) \
479 BF64_SET((bp)->blk_prop, 32, SPA_COMPRESSBITS, x)
34dc7c2f 480
9b67f605
MA
481#define BP_IS_EMBEDDED(bp) BF64_GET((bp)->blk_prop, 39, 1)
482#define BP_SET_EMBEDDED(bp, x) BF64_SET((bp)->blk_prop, 39, 1, x)
428870ff 483
9b67f605
MA
484#define BP_GET_CHECKSUM(bp) \
485 (BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \
486 BF64_GET((bp)->blk_prop, 40, 8))
487#define BP_SET_CHECKSUM(bp, x) do { \
488 ASSERT(!BP_IS_EMBEDDED(bp)); \
489 BF64_SET((bp)->blk_prop, 40, 8, x); \
490_NOTE(CONSTCOND) } while (0)
34dc7c2f 491
428870ff
BB
492#define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8)
493#define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x)
34dc7c2f 494
428870ff
BB
495#define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5)
496#define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x)
34dc7c2f 497
b5256303
TC
498/* encrypted, authenticated, and MAC cksum bps use the same bit */
499#define BP_USES_CRYPT(bp) BF64_GET((bp)->blk_prop, 61, 1)
500#define BP_SET_CRYPT(bp, x) BF64_SET((bp)->blk_prop, 61, 1, x)
501
502#define BP_IS_ENCRYPTED(bp) \
503 (BP_USES_CRYPT(bp) && \
504 BP_GET_LEVEL(bp) <= 0 && \
505 DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
506
507#define BP_IS_AUTHENTICATED(bp) \
508 (BP_USES_CRYPT(bp) && \
509 BP_GET_LEVEL(bp) <= 0 && \
510 !DMU_OT_IS_ENCRYPTED(BP_GET_TYPE(bp)))
511
512#define BP_HAS_INDIRECT_MAC_CKSUM(bp) \
513 (BP_USES_CRYPT(bp) && BP_GET_LEVEL(bp) > 0)
514
515#define BP_IS_PROTECTED(bp) \
516 (BP_IS_ENCRYPTED(bp) || BP_IS_AUTHENTICATED(bp))
517
428870ff
BB
518#define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1)
519#define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x)
520
b0bc7a84 521#define BP_GET_BYTEORDER(bp) BF64_GET((bp)->blk_prop, 63, 1)
428870ff
BB
522#define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x)
523
524#define BP_PHYSICAL_BIRTH(bp) \
9b67f605
MA
525 (BP_IS_EMBEDDED(bp) ? 0 : \
526 (bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
428870ff
BB
527
528#define BP_SET_BIRTH(bp, logical, physical) \
529{ \
9b67f605 530 ASSERT(!BP_IS_EMBEDDED(bp)); \
428870ff
BB
531 (bp)->blk_birth = (logical); \
532 (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
533}
34dc7c2f 534
b5256303
TC
535#define BP_GET_FILL(bp) \
536 ((BP_IS_ENCRYPTED(bp)) ? BF64_GET((bp)->blk_fill, 0, 32) : \
537 ((BP_IS_EMBEDDED(bp)) ? 1 : (bp)->blk_fill))
538
539#define BP_SET_FILL(bp, fill) \
540{ \
541 if (BP_IS_ENCRYPTED(bp)) \
542 BF64_SET((bp)->blk_fill, 0, 32, fill); \
543 else \
544 (bp)->blk_fill = fill; \
545}
546
547#define BP_GET_IV2(bp) \
548 (ASSERT(BP_IS_ENCRYPTED(bp)), \
549 BF64_GET((bp)->blk_fill, 32, 32))
550#define BP_SET_IV2(bp, iv2) \
551{ \
552 ASSERT(BP_IS_ENCRYPTED(bp)); \
553 BF64_SET((bp)->blk_fill, 32, 32, iv2); \
554}
9b67f605 555
a6255b7f
DQ
556#define BP_IS_METADATA(bp) \
557 (BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
558
34dc7c2f 559#define BP_GET_ASIZE(bp) \
9b67f605
MA
560 (BP_IS_EMBEDDED(bp) ? 0 : \
561 DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
562 DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
b5256303 563 (DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
34dc7c2f 564
a6255b7f
DQ
565#define BP_GET_UCSIZE(bp) \
566 (BP_IS_METADATA(bp) ? BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
34dc7c2f
BB
567
568#define BP_GET_NDVAS(bp) \
9b67f605
MA
569 (BP_IS_EMBEDDED(bp) ? 0 : \
570 !!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
34dc7c2f 571 !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
b5256303 572 (!!DVA_GET_ASIZE(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp)))
34dc7c2f
BB
573
574#define BP_COUNT_GANG(bp) \
9b67f605 575 (BP_IS_EMBEDDED(bp) ? 0 : \
34dc7c2f
BB
576 (DVA_GET_GANG(&(bp)->blk_dva[0]) + \
577 DVA_GET_GANG(&(bp)->blk_dva[1]) + \
b5256303 578 (DVA_GET_GANG(&(bp)->blk_dva[2]) * !BP_IS_ENCRYPTED(bp))))
34dc7c2f
BB
579
580#define DVA_EQUAL(dva1, dva2) \
581 ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
582 (dva1)->dva_word[0] == (dva2)->dva_word[0])
583
428870ff
BB
584#define BP_EQUAL(bp1, bp2) \
585 (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \
9b67f605 586 (bp1)->blk_birth == (bp2)->blk_birth && \
428870ff
BB
587 DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \
588 DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \
589 DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
590
37f8a883 591
34dc7c2f
BB
592#define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0)
593
9b67f605
MA
594#define BP_IDENTITY(bp) (ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0])
595#define BP_IS_GANG(bp) \
596 (BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp)))
b0bc7a84
MG
597#define DVA_IS_EMPTY(dva) ((dva)->dva_word[0] == 0ULL && \
598 (dva)->dva_word[1] == 0ULL)
9b67f605
MA
599#define BP_IS_HOLE(bp) \
600 (!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp)))
428870ff
BB
601
602/* BP_IS_RAIDZ(bp) assumes no block compression */
603#define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
604 BP_GET_PSIZE(bp))
34dc7c2f 605
b128c09f 606#define BP_ZERO(bp) \
34dc7c2f
BB
607{ \
608 (bp)->blk_dva[0].dva_word[0] = 0; \
609 (bp)->blk_dva[0].dva_word[1] = 0; \
610 (bp)->blk_dva[1].dva_word[0] = 0; \
611 (bp)->blk_dva[1].dva_word[1] = 0; \
612 (bp)->blk_dva[2].dva_word[0] = 0; \
613 (bp)->blk_dva[2].dva_word[1] = 0; \
34dc7c2f
BB
614 (bp)->blk_prop = 0; \
615 (bp)->blk_pad[0] = 0; \
616 (bp)->blk_pad[1] = 0; \
428870ff 617 (bp)->blk_phys_birth = 0; \
b128c09f 618 (bp)->blk_birth = 0; \
34dc7c2f
BB
619 (bp)->blk_fill = 0; \
620 ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \
621}
622
34dc7c2f
BB
623#ifdef _BIG_ENDIAN
624#define ZFS_HOST_BYTEORDER (0ULL)
625#else
b0bc7a84 626#define ZFS_HOST_BYTEORDER (1ULL)
34dc7c2f
BB
627#endif
628
629#define BP_SHOULD_BYTESWAP(bp) (BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
630
b5256303 631#define BP_SPRINTF_LEN 400
34dc7c2f 632
428870ff
BB
633/*
634 * This macro allows code sharing between zfs, libzpool, and mdb.
635 * 'func' is either snprintf() or mdb_snprintf().
636 * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
637 */
5c27ec10 638#define SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \
428870ff
BB
639{ \
640 static const char *copyname[] = \
641 { "zero", "single", "double", "triple" }; \
428870ff
BB
642 int len = 0; \
643 int copies = 0; \
5c27ec10
MA
644 const char *crypt_type; \
645 if (bp != NULL) { \
646 if (BP_IS_ENCRYPTED(bp)) { \
647 crypt_type = "encrypted"; \
648 /* LINTED E_SUSPICIOUS_COMPARISON */ \
649 } else if (BP_IS_AUTHENTICATED(bp)) { \
650 crypt_type = "authenticated"; \
651 } else if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) { \
652 crypt_type = "indirect-MAC"; \
653 } else { \
654 crypt_type = "unencrypted"; \
655 } \
656 } \
428870ff 657 if (bp == NULL) { \
b0bc7a84 658 len += func(buf + len, size - len, "<NULL>"); \
428870ff 659 } else if (BP_IS_HOLE(bp)) { \
a4069eef
PS
660 len += func(buf + len, size - len, \
661 "HOLE [L%llu %s] " \
662 "size=%llxL birth=%lluL", \
663 (u_longlong_t)BP_GET_LEVEL(bp), \
664 type, \
665 (u_longlong_t)BP_GET_LSIZE(bp), \
666 (u_longlong_t)bp->blk_birth); \
9b67f605
MA
667 } else if (BP_IS_EMBEDDED(bp)) { \
668 len = func(buf + len, size - len, \
669 "EMBEDDED [L%llu %s] et=%u %s " \
670 "size=%llxL/%llxP birth=%lluL", \
671 (u_longlong_t)BP_GET_LEVEL(bp), \
672 type, \
673 (int)BPE_GET_ETYPE(bp), \
674 compress, \
675 (u_longlong_t)BPE_GET_LSIZE(bp), \
676 (u_longlong_t)BPE_GET_PSIZE(bp), \
677 (u_longlong_t)bp->blk_birth); \
428870ff 678 } else { \
1c27024e 679 for (int d = 0; d < BP_GET_NDVAS(bp); d++) { \
428870ff
BB
680 const dva_t *dva = &bp->blk_dva[d]; \
681 if (DVA_IS_VALID(dva)) \
682 copies++; \
683 len += func(buf + len, size - len, \
684 "DVA[%d]=<%llu:%llx:%llx>%c", d, \
685 (u_longlong_t)DVA_GET_VDEV(dva), \
686 (u_longlong_t)DVA_GET_OFFSET(dva), \
687 (u_longlong_t)DVA_GET_ASIZE(dva), \
688 ws); \
689 } \
b5256303
TC
690 if (BP_IS_ENCRYPTED(bp)) { \
691 len += func(buf + len, size - len, \
692 "salt=%llx iv=%llx:%llx%c", \
693 (u_longlong_t)bp->blk_dva[2].dva_word[0], \
694 (u_longlong_t)bp->blk_dva[2].dva_word[1], \
695 (u_longlong_t)BP_GET_IV2(bp), \
696 ws); \
697 } \
428870ff
BB
698 if (BP_IS_GANG(bp) && \
699 DVA_GET_ASIZE(&bp->blk_dva[2]) <= \
700 DVA_GET_ASIZE(&bp->blk_dva[1]) / 2) \
701 copies--; \
702 len += func(buf + len, size - len, \
b5256303 703 "[L%llu %s] %s %s %s %s %s %s %s%c" \
428870ff
BB
704 "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c" \
705 "cksum=%llx:%llx:%llx:%llx", \
706 (u_longlong_t)BP_GET_LEVEL(bp), \
707 type, \
708 checksum, \
709 compress, \
b5256303 710 crypt_type, \
428870ff
BB
711 BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE", \
712 BP_IS_GANG(bp) ? "gang" : "contiguous", \
713 BP_GET_DEDUP(bp) ? "dedup" : "unique", \
714 copyname[copies], \
715 ws, \
716 (u_longlong_t)BP_GET_LSIZE(bp), \
717 (u_longlong_t)BP_GET_PSIZE(bp), \
718 (u_longlong_t)bp->blk_birth, \
719 (u_longlong_t)BP_PHYSICAL_BIRTH(bp), \
9b67f605 720 (u_longlong_t)BP_GET_FILL(bp), \
428870ff
BB
721 ws, \
722 (u_longlong_t)bp->blk_cksum.zc_word[0], \
723 (u_longlong_t)bp->blk_cksum.zc_word[1], \
724 (u_longlong_t)bp->blk_cksum.zc_word[2], \
725 (u_longlong_t)bp->blk_cksum.zc_word[3]); \
726 } \
727 ASSERT(len < size); \
728}
729
34dc7c2f 730#define BP_GET_BUFC_TYPE(bp) \
a6255b7f 731 (BP_IS_METADATA(bp) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
428870ff
BB
732
733typedef enum spa_import_type {
734 SPA_IMPORT_EXISTING,
735 SPA_IMPORT_ASSEMBLE
736} spa_import_type_t;
34dc7c2f
BB
737
738/* state manipulation functions */
739extern int spa_open(const char *pool, spa_t **, void *tag);
428870ff
BB
740extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
741 nvlist_t *policy, nvlist_t **config);
9ae529ec
CS
742extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
743 size_t buflen);
b5256303
TC
744extern int spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
745 nvlist_t *zplprops, struct dsl_crypto_params *dcp);
13fe0198 746extern int spa_import(char *pool, nvlist_t *config, nvlist_t *props,
572e2857 747 uint64_t flags);
34dc7c2f
BB
748extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
749extern int spa_destroy(char *pool);
fb5f0bc8
BB
750extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
751 boolean_t hardforce);
34dc7c2f
BB
752extern int spa_reset(char *pool);
753extern void spa_async_request(spa_t *spa, int flag);
b128c09f 754extern void spa_async_unrequest(spa_t *spa, int flag);
34dc7c2f
BB
755extern void spa_async_suspend(spa_t *spa);
756extern void spa_async_resume(spa_t *spa);
757extern spa_t *spa_inject_addref(char *pool);
758extern void spa_inject_delref(spa_t *spa);
428870ff
BB
759extern void spa_scan_stat_init(spa_t *spa);
760extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
34dc7c2f 761
b128c09f
BB
762#define SPA_ASYNC_CONFIG_UPDATE 0x01
763#define SPA_ASYNC_REMOVE 0x02
764#define SPA_ASYNC_PROBE 0x04
765#define SPA_ASYNC_RESILVER_DONE 0x08
766#define SPA_ASYNC_RESILVER 0x10
9babb374 767#define SPA_ASYNC_AUTOEXPAND 0x20
428870ff
BB
768#define SPA_ASYNC_REMOVE_DONE 0x40
769#define SPA_ASYNC_REMOVE_STOP 0x80
770
771/*
772 * Controls the behavior of spa_vdev_remove().
773 */
774#define SPA_REMOVE_UNSPARE 0x01
775#define SPA_REMOVE_DONE 0x02
34dc7c2f
BB
776
777/* device manipulation */
778extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
779extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
780 int replacing);
fb5f0bc8
BB
781extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
782 int replace_done);
34dc7c2f 783extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
428870ff 784extern boolean_t spa_vdev_remove_active(spa_t *spa);
34dc7c2f 785extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
9babb374 786extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
428870ff
BB
787extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
788 nvlist_t *props, boolean_t exp);
34dc7c2f
BB
789
790/* spare state (which is global across all pools) */
791extern void spa_spare_add(vdev_t *vd);
792extern void spa_spare_remove(vdev_t *vd);
b128c09f 793extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
34dc7c2f
BB
794extern void spa_spare_activate(vdev_t *vd);
795
796/* L2ARC state (which is global across all pools) */
797extern void spa_l2cache_add(vdev_t *vd);
798extern void spa_l2cache_remove(vdev_t *vd);
799extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
800extern void spa_l2cache_activate(vdev_t *vd);
801extern void spa_l2cache_drop(spa_t *spa);
34dc7c2f 802
428870ff
BB
803/* scanning */
804extern int spa_scan(spa_t *spa, pool_scan_func_t func);
805extern int spa_scan_stop(spa_t *spa);
0ea05c64 806extern int spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t flag);
34dc7c2f
BB
807
808/* spa syncing */
809extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
810extern void spa_sync_allpools(void);
811
55d85d5a 812extern int zfs_sync_pass_deferred_free;
428870ff 813
b128c09f
BB
814/* spa namespace global mutex */
815extern kmutex_t spa_namespace_lock;
816
34dc7c2f
BB
817/*
818 * SPA configuration functions in spa_config.c
819 */
820
821#define SPA_CONFIG_UPDATE_POOL 0
822#define SPA_CONFIG_UPDATE_VDEVS 1
823
a1d477c2 824extern void spa_write_cachefile(spa_t *, boolean_t, boolean_t);
34dc7c2f
BB
825extern void spa_config_load(void);
826extern nvlist_t *spa_all_configs(uint64_t *);
827extern void spa_config_set(spa_t *spa, nvlist_t *config);
828extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
829 int getstats);
830extern void spa_config_update(spa_t *spa, int what);
34dc7c2f
BB
831
832/*
833 * Miscellaneous SPA routines in spa_misc.c
834 */
835
836/* Namespace manipulation */
837extern spa_t *spa_lookup(const char *name);
428870ff 838extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
34dc7c2f
BB
839extern void spa_remove(spa_t *spa);
840extern spa_t *spa_next(spa_t *prev);
841
842/* Refcount functions */
843extern void spa_open_ref(spa_t *spa, void *tag);
844extern void spa_close(spa_t *spa, void *tag);
0c66c32d 845extern void spa_async_close(spa_t *spa, void *tag);
34dc7c2f
BB
846extern boolean_t spa_refcount_zero(spa_t *spa);
847
428870ff 848#define SCL_NONE 0x00
b128c09f
BB
849#define SCL_CONFIG 0x01
850#define SCL_STATE 0x02
851#define SCL_L2ARC 0x04 /* hack until L2ARC 2.0 */
852#define SCL_ALLOC 0x08
853#define SCL_ZIO 0x10
854#define SCL_FREE 0x20
855#define SCL_VDEV 0x40
856#define SCL_LOCKS 7
857#define SCL_ALL ((1 << SCL_LOCKS) - 1)
858#define SCL_STATE_ALL (SCL_STATE | SCL_L2ARC | SCL_ZIO)
859
1421c891
PS
860/* Historical pool statistics */
861typedef struct spa_stats_history {
862 kmutex_t lock;
863 uint64_t count;
864 uint64_t size;
865 kstat_t *kstat;
866 void *private;
867 list_t list;
868} spa_stats_history_t;
869
870typedef struct spa_stats {
871 spa_stats_history_t read_history;
0b1401ee 872 spa_stats_history_t txg_history;
2d37239a 873 spa_stats_history_t tx_assign_histogram;
330847ff 874 spa_stats_history_t io_history;
379ca9cf 875 spa_stats_history_t mmp_history;
f0ed6c74 876 spa_stats_history_t state; /* pool state */
1421c891
PS
877} spa_stats_t;
878
0b1401ee
BB
879typedef enum txg_state {
880 TXG_STATE_BIRTH = 0,
881 TXG_STATE_OPEN = 1,
882 TXG_STATE_QUIESCED = 2,
478d64fd
IL
883 TXG_STATE_WAIT_FOR_SYNC = 3,
884 TXG_STATE_SYNCED = 4,
885 TXG_STATE_COMMITTED = 5,
0b1401ee
BB
886} txg_state_t;
887
baf67d15
BB
888typedef struct txg_stat {
889 vdev_stat_t vs1;
890 vdev_stat_t vs2;
891 uint64_t txg;
892 uint64_t ndirty;
893} txg_stat_t;
894
1421c891
PS
895extern void spa_stats_init(spa_t *spa);
896extern void spa_stats_destroy(spa_t *spa);
5dbd68a3 897extern void spa_read_history_add(spa_t *spa, const zbookmark_phys_t *zb,
1421c891 898 uint32_t aflags);
01b738f4 899extern void spa_txg_history_add(spa_t *spa, uint64_t txg, hrtime_t birth_time);
0b1401ee
BB
900extern int spa_txg_history_set(spa_t *spa, uint64_t txg,
901 txg_state_t completed_state, hrtime_t completed_time);
baf67d15
BB
902extern txg_stat_t *spa_txg_history_init_io(spa_t *, uint64_t,
903 struct dsl_pool *);
904extern void spa_txg_history_fini_io(spa_t *, txg_stat_t *);
2d37239a 905extern void spa_tx_assign_add_nsecs(spa_t *spa, uint64_t nsecs);
d2160d05 906extern int spa_mmp_history_set_skip(spa_t *spa, uint64_t mmp_kstat_id);
7088545d
OF
907extern int spa_mmp_history_set(spa_t *spa, uint64_t mmp_kstat_id, int io_error,
908 hrtime_t duration);
d2160d05
OF
909extern void *spa_mmp_history_add(spa_t *spa, uint64_t txg, uint64_t timestamp,
910 uint64_t mmp_delay, vdev_t *vd, int label, uint64_t mmp_kstat_id,
911 int error);
1421c891 912
b128c09f
BB
913/* Pool configuration locks */
914extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
915extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
916extern void spa_config_exit(spa_t *spa, int locks, void *tag);
917extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
34dc7c2f
BB
918
919/* Pool vdev add/remove lock */
920extern uint64_t spa_vdev_enter(spa_t *spa);
428870ff
BB
921extern uint64_t spa_vdev_config_enter(spa_t *spa);
922extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
923 int error, char *tag);
34dc7c2f
BB
924extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
925
b128c09f 926/* Pool vdev state change lock */
428870ff 927extern void spa_vdev_state_enter(spa_t *spa, int oplock);
b128c09f
BB
928extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
929
428870ff
BB
930/* Log state */
931typedef enum spa_log_state {
932 SPA_LOG_UNKNOWN = 0, /* unknown log state */
933 SPA_LOG_MISSING, /* missing log(s) */
934 SPA_LOG_CLEAR, /* clear the log(s) */
935 SPA_LOG_GOOD, /* log(s) are good */
936} spa_log_state_t;
937
938extern spa_log_state_t spa_get_log_state(spa_t *spa);
939extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
a1d477c2 940extern int spa_reset_logs(spa_t *spa);
428870ff
BB
941
942/* Log claim callback */
943extern void spa_claim_notify(zio_t *zio);
cc92e9d0 944extern void spa_deadman(void *);
428870ff 945
34dc7c2f 946/* Accessor functions */
b128c09f 947extern boolean_t spa_shutting_down(spa_t *spa);
34dc7c2f 948extern struct dsl_pool *spa_get_dsl(spa_t *spa);
9ae529ec 949extern boolean_t spa_is_initializing(spa_t *spa);
a1d477c2 950extern boolean_t spa_indirect_vdevs_loaded(spa_t *spa);
34dc7c2f
BB
951extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
952extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
953extern void spa_altroot(spa_t *, char *, size_t);
954extern int spa_sync_pass(spa_t *spa);
955extern char *spa_name(spa_t *spa);
956extern uint64_t spa_guid(spa_t *spa);
3541dc6d 957extern uint64_t spa_load_guid(spa_t *spa);
34dc7c2f
BB
958extern uint64_t spa_last_synced_txg(spa_t *spa);
959extern uint64_t spa_first_txg(spa_t *spa);
428870ff 960extern uint64_t spa_syncing_txg(spa_t *spa);
3b7f360c 961extern uint64_t spa_final_dirty_txg(spa_t *spa);
34dc7c2f 962extern uint64_t spa_version(spa_t *spa);
b128c09f 963extern pool_state_t spa_state(spa_t *spa);
428870ff 964extern spa_load_state_t spa_load_state(spa_t *spa);
34dc7c2f 965extern uint64_t spa_freeze_txg(spa_t *spa);
3ec3bc21 966extern uint64_t spa_get_worst_case_asize(spa_t *spa, uint64_t lsize);
428870ff 967extern uint64_t spa_get_dspace(spa_t *spa);
0c60cc32 968extern uint64_t spa_get_slop_space(spa_t *spa);
428870ff 969extern void spa_update_dspace(spa_t *spa);
34dc7c2f 970extern uint64_t spa_version(spa_t *spa);
428870ff
BB
971extern boolean_t spa_deflate(spa_t *spa);
972extern metaslab_class_t *spa_normal_class(spa_t *spa);
973extern metaslab_class_t *spa_log_class(spa_t *spa);
0c66c32d
JG
974extern void spa_evicting_os_register(spa_t *, objset_t *os);
975extern void spa_evicting_os_deregister(spa_t *, objset_t *os);
976extern void spa_evicting_os_wait(spa_t *spa);
34dc7c2f 977extern int spa_max_replication(spa_t *spa);
428870ff 978extern int spa_prev_software_version(spa_t *spa);
8fb1ede1
BB
979extern uint64_t spa_get_failmode(spa_t *spa);
980extern uint64_t spa_get_deadman_failmode(spa_t *spa);
981extern void spa_set_deadman_failmode(spa_t *spa, const char *failmode);
b128c09f 982extern boolean_t spa_suspended(spa_t *spa);
428870ff
BB
983extern uint64_t spa_bootfs(spa_t *spa);
984extern uint64_t spa_delegation(spa_t *spa);
985extern objset_t *spa_meta_objset(spa_t *spa);
cc92e9d0 986extern uint64_t spa_deadman_synctime(spa_t *spa);
8fb1ede1 987extern uint64_t spa_deadman_ziotime(spa_t *spa);
34dc7c2f
BB
988
989/* Miscellaneous support routines */
4a0ee12a
PZ
990extern void spa_load_failed(spa_t *spa, const char *fmt, ...);
991extern void spa_load_note(spa_t *spa, const char *fmt, ...);
b0bc7a84
MG
992extern void spa_activate_mos_feature(spa_t *spa, const char *feature,
993 dmu_tx_t *tx);
9ae529ec 994extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
34dc7c2f 995extern int spa_rename(const char *oldname, const char *newname);
572e2857 996extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
34dc7c2f
BB
997extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
998extern char *spa_strdup(const char *);
999extern void spa_strfree(char *);
1000extern uint64_t spa_get_random(uint64_t range);
428870ff 1001extern uint64_t spa_generate_guid(spa_t *spa);
b0bc7a84 1002extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp);
34dc7c2f 1003extern void spa_freeze(spa_t *spa);
3541dc6d 1004extern int spa_change_guid(spa_t *spa);
34dc7c2f
BB
1005extern void spa_upgrade(spa_t *spa, uint64_t version);
1006extern void spa_evict_all(void);
b128c09f
BB
1007extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
1008 boolean_t l2cache);
34dc7c2f 1009extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
428870ff
BB
1010extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
1011extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
1012extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
34dc7c2f 1013extern boolean_t spa_has_slogs(spa_t *spa);
b128c09f 1014extern boolean_t spa_is_root(spa_t *spa);
fb5f0bc8 1015extern boolean_t spa_writeable(spa_t *spa);
acbad6ff 1016extern boolean_t spa_has_pending_synctask(spa_t *spa);
f1512ee6 1017extern int spa_maxblocksize(spa_t *spa);
50c957f7 1018extern int spa_maxdnodesize(spa_t *spa);
63e3a861 1019extern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp);
6cb8e530
PZ
1020extern boolean_t zfs_dva_valid(spa_t *spa, const dva_t *dva,
1021 const blkptr_t *bp);
a1d477c2
MA
1022typedef void (*spa_remap_cb_t)(uint64_t vdev, uint64_t offset, uint64_t size,
1023 void *arg);
1024extern boolean_t spa_remap_blkptr(spa_t *spa, blkptr_t *bp,
1025 spa_remap_cb_t callback, void *arg);
1026extern uint64_t spa_get_last_removal_txg(spa_t *spa);
6cb8e530
PZ
1027extern boolean_t spa_trust_config(spa_t *spa);
1028extern uint64_t spa_missing_tvds_allowed(spa_t *spa);
1029extern void spa_set_missing_tvds(spa_t *spa, uint64_t missing);
379ca9cf
OF
1030extern boolean_t spa_multihost(spa_t *spa);
1031extern unsigned long spa_get_hostid(void);
428870ff 1032
fb5f0bc8 1033extern int spa_mode(spa_t *spa);
e19572e4 1034extern uint64_t zfs_strtonum(const char *str, char **nptr);
34dc7c2f 1035
34dc7c2f
BB
1036extern char *spa_his_ievent_table[];
1037
1038extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
1039extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
1040 char *his_buf);
6f1ffb06
MA
1041extern int spa_history_log(spa_t *spa, const char *his_buf);
1042extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
d5e024cb
BB
1043extern void spa_history_log_version(spa_t *spa, const char *operation,
1044 dmu_tx_t *tx);
6f1ffb06
MA
1045extern void spa_history_log_internal(spa_t *spa, const char *operation,
1046 dmu_tx_t *tx, const char *fmt, ...);
1047extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
1048 dmu_tx_t *tx, const char *fmt, ...);
1049extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
1050 dmu_tx_t *tx, const char *fmt, ...);
34dc7c2f 1051
f0ed6c74
TH
1052extern const char *spa_state_to_name(spa_t *spa);
1053
34dc7c2f 1054/* error handling */
5dbd68a3 1055struct zbookmark_phys;
b5256303 1056extern void spa_log_error(spa_t *spa, const zbookmark_phys_t *zb);
34dc7c2f 1057extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
a2c2ed1b
TC
1058 const zbookmark_phys_t *zb, zio_t *zio, uint64_t stateoroffset,
1059 uint64_t length);
12fa0466
DE
1060extern nvlist_t *zfs_event_create(spa_t *spa, vdev_t *vd, const char *type,
1061 const char *name, nvlist_t *aux);
34dc7c2f 1062extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
d02ca379 1063extern void zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate);
34dc7c2f
BB
1064extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
1065extern uint64_t spa_get_errlog_size(spa_t *spa);
1066extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
1067extern void spa_errlog_rotate(spa_t *spa);
1068extern void spa_errlog_drain(spa_t *spa);
1069extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
1070extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
1071
1072/* vdev cache */
1073extern void vdev_cache_stat_init(void);
1074extern void vdev_cache_stat_fini(void);
1075
551905dd
GN
1076/* vdev mirror */
1077extern void vdev_mirror_stat_init(void);
1078extern void vdev_mirror_stat_fini(void);
1079
34dc7c2f
BB
1080/* Initialization and termination */
1081extern void spa_init(int flags);
1082extern void spa_fini(void);
0bc8fd78 1083extern void spa_boot_init(void);
34dc7c2f
BB
1084
1085/* properties */
1086extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
1087extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
1088extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
d164b209 1089extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
34dc7c2f
BB
1090
1091/* asynchronous event notification */
12fa0466
DE
1092extern void spa_event_notify(spa_t *spa, vdev_t *vdev, nvlist_t *hist_nvl,
1093 const char *name);
34dc7c2f
BB
1094
1095#ifdef ZFS_DEBUG
79c76d5b
BB
1096#define dprintf_bp(bp, fmt, ...) do { \
1097 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
1098 char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP); \
1099 snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp)); \
1100 dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf); \
1101 kmem_free(__blkbuf, BP_SPRINTF_LEN); \
34dc7c2f
BB
1102 } \
1103_NOTE(CONSTCOND) } while (0)
1104#else
1105#define dprintf_bp(bp, fmt, ...)
1106#endif
1107
fb5f0bc8 1108extern int spa_mode_global; /* mode, e.g. FREAD | FWRITE */
8fb1ede1
BB
1109extern int zfs_deadman_enabled;
1110extern unsigned long zfs_deadman_synctime_ms;
1111extern unsigned long zfs_deadman_ziotime_ms;
1112extern unsigned long zfs_deadman_checktime_ms;
34dc7c2f
BB
1113
1114#ifdef __cplusplus
1115}
1116#endif
1117
1118#endif /* _SYS_SPA_H */