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