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 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 26 * Copyright 2013 Saso Kiselkov. All rights reserved. 27 */ 28 29 #ifndef _SYS_SPA_H 30 #define _SYS_SPA_H 31 32 #include <sys/avl.h> 33 #include <sys/zfs_context.h> 34 #include <sys/nvpair.h> 35 #include <sys/sysmacros.h> 36 #include <sys/types.h> 37 #include <sys/fs/zfs.h> 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif 42 43 /* 44 * Forward references that lots of things need. 45 */ 46 typedef struct spa spa_t; 47 typedef struct vdev vdev_t; 48 typedef struct metaslab metaslab_t; 49 typedef struct metaslab_group metaslab_group_t; 50 typedef struct metaslab_class metaslab_class_t; 51 typedef struct zio zio_t; 52 typedef struct zilog zilog_t; 53 typedef struct spa_aux_vdev spa_aux_vdev_t; 54 typedef struct ddt ddt_t; 55 typedef struct ddt_entry ddt_entry_t; 56 struct dsl_pool; 57 struct dsl_dataset; 58 59 /* 60 * General-purpose 32-bit and 64-bit bitfield encodings. 61 */ 62 #define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len)) 63 #define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len)) 64 #define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low)) 65 #define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low)) 66 67 #define BF32_GET(x, low, len) BF32_DECODE(x, low, len) 68 #define BF64_GET(x, low, len) BF64_DECODE(x, low, len) 69 70 #define BF32_SET(x, low, len, val) do { \ 71 ASSERT3U(val, <, 1U << (len)); \ 72 ASSERT3U(low + len, <=, 32); \ 73 (x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \ 74 _NOTE(CONSTCOND) } while (0) 75 76 #define BF64_SET(x, low, len, val) do { \ 77 ASSERT3U(val, <, 1ULL << (len)); \ 78 ASSERT3U(low + len, <=, 64); \ 79 ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \ 80 _NOTE(CONSTCOND) } while (0) 81 82 #define BF32_GET_SB(x, low, len, shift, bias) \ 83 ((BF32_GET(x, low, len) + (bias)) << (shift)) 84 #define BF64_GET_SB(x, low, len, shift, bias) \ 85 ((BF64_GET(x, low, len) + (bias)) << (shift)) 86 87 #define BF32_SET_SB(x, low, len, shift, bias, val) do { \ 88 ASSERT(IS_P2ALIGNED(val, 1U << shift)); \ 89 ASSERT3S((val) >> (shift), >=, bias); \ 90 BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \ 91 _NOTE(CONSTCOND) } while (0) 92 #define BF64_SET_SB(x, low, len, shift, bias, val) do { \ 93 ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \ 94 ASSERT3S((val) >> (shift), >=, bias); \ 95 BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \ 96 _NOTE(CONSTCOND) } while (0) 97 98 /* 99 * We currently support block sizes from 512 bytes to 16MB. 100 * The benefits of larger blocks, and thus larger IO, need to be weighed 101 * against the cost of COWing a giant block to modify one byte, and the 102 * large latency of reading or writing a large block. 103 * 104 * Note that although blocks up to 16MB are supported, the recordsize 105 * property can not be set larger than zfs_max_recordsize (default 1MB). 106 * See the comment near zfs_max_recordsize in dsl_dataset.c for details. 107 * 108 * Note that although the LSIZE field of the blkptr_t can store sizes up 109 * to 32MB, the dnode's dn_datablkszsec can only store sizes up to 110 * 32MB - 512 bytes. Therefore, we limit SPA_MAXBLOCKSIZE to 16MB. 111 */ 112 #define SPA_MINBLOCKSHIFT 9 113 #define SPA_OLD_MAXBLOCKSHIFT 17 114 #define SPA_MAXBLOCKSHIFT 24 115 #define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT) 116 #define SPA_OLD_MAXBLOCKSIZE (1ULL << SPA_OLD_MAXBLOCKSHIFT) 117 #define SPA_MAXBLOCKSIZE (1ULL << SPA_MAXBLOCKSHIFT) 118 119 /* 120 * Size of block to hold the configuration data (a packed nvlist) 121 */ 122 #define SPA_CONFIG_BLOCKSIZE (1ULL << 14) 123 124 /* 125 * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB. 126 * The ASIZE encoding should be at least 64 times larger (6 more bits) 127 * to support up to 4-way RAID-Z mirror mode with worst-case gang block 128 * overhead, three DVAs per bp, plus one more bit in case we do anything 129 * else that expands the ASIZE. 130 */ 131 #define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */ 132 #define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */ 133 #define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */ 134 135 /* 136 * All SPA data is represented by 128-bit data virtual addresses (DVAs). 137 * The members of the dva_t should be considered opaque outside the SPA. 138 */ 139 typedef struct dva { 140 uint64_t dva_word[2]; 141 } dva_t; 142 143 /* 144 * Each block has a 256-bit checksum -- strong enough for cryptographic hashes. 145 */ 146 typedef struct zio_cksum { 147 uint64_t zc_word[4]; 148 } zio_cksum_t; 149 150 /* 151 * Some checksums/hashes need a 256-bit initialization salt. This salt is kept 152 * secret and is suitable for use in MAC algorithms as the key. 153 */ 154 typedef struct zio_cksum_salt { 155 uint8_t zcs_bytes[32]; 156 } zio_cksum_salt_t; 157 158 /* 159 * Each block is described by its DVAs, time of birth, checksum, etc. 160 * The word-by-word, bit-by-bit layout of the blkptr is as follows: 161 * 162 * 64 56 48 40 32 24 16 8 0 163 * +-------+-------+-------+-------+-------+-------+-------+-------+ 164 * 0 | vdev1 | GRID | ASIZE | 165 * +-------+-------+-------+-------+-------+-------+-------+-------+ 166 * 1 |G| offset1 | 167 * +-------+-------+-------+-------+-------+-------+-------+-------+ 168 * 2 | vdev2 | GRID | ASIZE | 169 * +-------+-------+-------+-------+-------+-------+-------+-------+ 170 * 3 |G| offset2 | 171 * +-------+-------+-------+-------+-------+-------+-------+-------+ 172 * 4 | vdev3 | GRID | ASIZE | 173 * +-------+-------+-------+-------+-------+-------+-------+-------+ 174 * 5 |G| offset3 | 175 * +-------+-------+-------+-------+-------+-------+-------+-------+ 176 * 6 |BDX|lvl| type | cksum |E| comp| PSIZE | LSIZE | 177 * +-------+-------+-------+-------+-------+-------+-------+-------+ 178 * 7 | padding | 179 * +-------+-------+-------+-------+-------+-------+-------+-------+ 180 * 8 | padding | 181 * +-------+-------+-------+-------+-------+-------+-------+-------+ 182 * 9 | physical birth txg | 183 * +-------+-------+-------+-------+-------+-------+-------+-------+ 184 * a | logical birth txg | 185 * +-------+-------+-------+-------+-------+-------+-------+-------+ 186 * b | fill count | 187 * +-------+-------+-------+-------+-------+-------+-------+-------+ 188 * c | checksum[0] | 189 * +-------+-------+-------+-------+-------+-------+-------+-------+ 190 * d | checksum[1] | 191 * +-------+-------+-------+-------+-------+-------+-------+-------+ 192 * e | checksum[2] | 193 * +-------+-------+-------+-------+-------+-------+-------+-------+ 194 * f | checksum[3] | 195 * +-------+-------+-------+-------+-------+-------+-------+-------+ 196 * 197 * Legend: 198 * 199 * vdev virtual device ID 200 * offset offset into virtual device 201 * LSIZE logical size 202 * PSIZE physical size (after compression) 203 * ASIZE allocated size (including RAID-Z parity and gang block headers) 204 * GRID RAID-Z layout information (reserved for future use) 205 * cksum checksum function 206 * comp compression function 207 * G gang block indicator 208 * B byteorder (endianness) 209 * D dedup 210 * X encryption (on version 30, which is not supported) 211 * E blkptr_t contains embedded data (see below) 212 * lvl level of indirection 213 * type DMU object type 214 * phys birth txg of block allocation; zero if same as logical birth txg 215 * log. birth transaction group in which the block was logically born 216 * fill count number of non-zero blocks under this bp 217 * checksum[4] 256-bit checksum of the data this bp describes 218 */ 219 220 /* 221 * "Embedded" blkptr_t's don't actually point to a block, instead they 222 * have a data payload embedded in the blkptr_t itself. See the comment 223 * in blkptr.c for more details. 224 * 225 * The blkptr_t is laid out as follows: 226 * 227 * 64 56 48 40 32 24 16 8 0 228 * +-------+-------+-------+-------+-------+-------+-------+-------+ 229 * 0 | payload | 230 * 1 | payload | 231 * 2 | payload | 232 * 3 | payload | 233 * 4 | payload | 234 * 5 | payload | 235 * +-------+-------+-------+-------+-------+-------+-------+-------+ 236 * 6 |BDX|lvl| type | etype |E| comp| PSIZE| LSIZE | 237 * +-------+-------+-------+-------+-------+-------+-------+-------+ 238 * 7 | payload | 239 * 8 | payload | 240 * 9 | payload | 241 * +-------+-------+-------+-------+-------+-------+-------+-------+ 242 * a | logical birth txg | 243 * +-------+-------+-------+-------+-------+-------+-------+-------+ 244 * b | payload | 245 * c | payload | 246 * d | payload | 247 * e | payload | 248 * f | payload | 249 * +-------+-------+-------+-------+-------+-------+-------+-------+ 250 * 251 * Legend: 252 * 253 * payload contains the embedded data 254 * B (byteorder) byteorder (endianness) 255 * D (dedup) padding (set to zero) 256 * X encryption (set to zero; see above) 257 * E (embedded) set to one 258 * lvl indirection level 259 * type DMU object type 260 * etype how to interpret embedded data (BP_EMBEDDED_TYPE_*) 261 * comp compression function of payload 262 * PSIZE size of payload after compression, in bytes 263 * LSIZE logical size of payload, in bytes 264 * note that 25 bits is enough to store the largest 265 * "normal" BP's LSIZE (2^16 * 2^9) in bytes 266 * log. birth transaction group in which the block was logically born 267 * 268 * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded 269 * bp's they are stored in units of SPA_MINBLOCKSHIFT. 270 * Generally, the generic BP_GET_*() macros can be used on embedded BP's. 271 * The B, D, X, lvl, type, and comp fields are stored the same as with normal 272 * BP's so the BP_SET_* macros can be used with them. etype, PSIZE, LSIZE must 273 * be set with the BPE_SET_* macros. BP_SET_EMBEDDED() should be called before 274 * other macros, as they assert that they are only used on BP's of the correct 275 * "embedded-ness". 276 */ 277 278 #define BPE_GET_ETYPE(bp) \ 279 (ASSERT(BP_IS_EMBEDDED(bp)), \ 280 BF64_GET((bp)->blk_prop, 40, 8)) 281 #define BPE_SET_ETYPE(bp, t) do { \ 282 ASSERT(BP_IS_EMBEDDED(bp)); \ 283 BF64_SET((bp)->blk_prop, 40, 8, t); \ 284 _NOTE(CONSTCOND) } while (0) 285 286 #define BPE_GET_LSIZE(bp) \ 287 (ASSERT(BP_IS_EMBEDDED(bp)), \ 288 BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1)) 289 #define BPE_SET_LSIZE(bp, x) do { \ 290 ASSERT(BP_IS_EMBEDDED(bp)); \ 291 BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \ 292 _NOTE(CONSTCOND) } while (0) 293 294 #define BPE_GET_PSIZE(bp) \ 295 (ASSERT(BP_IS_EMBEDDED(bp)), \ 296 BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1)) 297 #define BPE_SET_PSIZE(bp, x) do { \ 298 ASSERT(BP_IS_EMBEDDED(bp)); \ 299 BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \ 300 _NOTE(CONSTCOND) } while (0) 301 302 typedef enum bp_embedded_type { 303 BP_EMBEDDED_TYPE_DATA, 304 BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */ 305 NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED 306 } bp_embedded_type_t; 307 308 #define BPE_NUM_WORDS 14 309 #define BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t)) 310 #define BPE_IS_PAYLOADWORD(bp, wp) \ 311 ((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth) 312 313 #define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */ 314 #define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */ 315 316 /* 317 * A block is a hole when it has either 1) never been written to, or 318 * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads 319 * without physically allocating disk space. Holes are represented in the 320 * blkptr_t structure by zeroed blk_dva. Correct checking for holes is 321 * done through the BP_IS_HOLE macro. For holes, the logical size, level, 322 * DMU object type, and birth times are all also stored for holes that 323 * were written to at some point (i.e. were punched after having been filled). 324 */ 325 typedef struct blkptr { 326 dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */ 327 uint64_t blk_prop; /* size, compression, type, etc */ 328 uint64_t blk_pad[2]; /* Extra space for the future */ 329 uint64_t blk_phys_birth; /* txg when block was allocated */ 330 uint64_t blk_birth; /* transaction group at birth */ 331 uint64_t blk_fill; /* fill count */ 332 zio_cksum_t blk_cksum; /* 256-bit checksum */ 333 } blkptr_t; 334 335 /* 336 * Macros to get and set fields in a bp or DVA. 337 */ 338 #define DVA_GET_ASIZE(dva) \ 339 BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0) 340 #define DVA_SET_ASIZE(dva, x) \ 341 BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \ 342 SPA_MINBLOCKSHIFT, 0, x) 343 344 #define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8) 345 #define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x) 346 347 #define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32) 348 #define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x) 349 350 #define DVA_GET_OFFSET(dva) \ 351 BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0) 352 #define DVA_SET_OFFSET(dva, x) \ 353 BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x) 354 355 #define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1) 356 #define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x) 357 358 #define BP_GET_LSIZE(bp) \ 359 (BP_IS_EMBEDDED(bp) ? \ 360 (BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \ 361 BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)) 362 #define BP_SET_LSIZE(bp, x) do { \ 363 ASSERT(!BP_IS_EMBEDDED(bp)); \ 364 BF64_SET_SB((bp)->blk_prop, \ 365 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \ 366 _NOTE(CONSTCOND) } while (0) 367 368 #define BP_GET_PSIZE(bp) \ 369 (BP_IS_EMBEDDED(bp) ? 0 : \ 370 BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1)) 371 #define BP_SET_PSIZE(bp, x) do { \ 372 ASSERT(!BP_IS_EMBEDDED(bp)); \ 373 BF64_SET_SB((bp)->blk_prop, \ 374 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \ 375 _NOTE(CONSTCOND) } while (0) 376 377 #define BP_GET_COMPRESS(bp) BF64_GET((bp)->blk_prop, 32, 7) 378 #define BP_SET_COMPRESS(bp, x) BF64_SET((bp)->blk_prop, 32, 7, x) 379 380 #define BP_IS_EMBEDDED(bp) BF64_GET((bp)->blk_prop, 39, 1) 381 #define BP_SET_EMBEDDED(bp, x) BF64_SET((bp)->blk_prop, 39, 1, x) 382 383 #define BP_GET_CHECKSUM(bp) \ 384 (BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \ 385 BF64_GET((bp)->blk_prop, 40, 8)) 386 #define BP_SET_CHECKSUM(bp, x) do { \ 387 ASSERT(!BP_IS_EMBEDDED(bp)); \ 388 BF64_SET((bp)->blk_prop, 40, 8, x); \ 389 _NOTE(CONSTCOND) } while (0) 390 391 #define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8) 392 #define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x) 393 394 #define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5) 395 #define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x) 396 397 #define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1) 398 #define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x) 399 400 #define BP_GET_BYTEORDER(bp) BF64_GET((bp)->blk_prop, 63, 1) 401 #define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x) 402 403 #define BP_PHYSICAL_BIRTH(bp) \ 404 (BP_IS_EMBEDDED(bp) ? 0 : \ 405 (bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth) 406 407 #define BP_SET_BIRTH(bp, logical, physical) \ 408 { \ 409 ASSERT(!BP_IS_EMBEDDED(bp)); \ 410 (bp)->blk_birth = (logical); \ 411 (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \ 412 } 413 414 #define BP_GET_FILL(bp) (BP_IS_EMBEDDED(bp) ? 1 : (bp)->blk_fill) 415 416 #define BP_GET_ASIZE(bp) \ 417 (BP_IS_EMBEDDED(bp) ? 0 : \ 418 DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \ 419 DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ 420 DVA_GET_ASIZE(&(bp)->blk_dva[2])) 421 422 #define BP_GET_UCSIZE(bp) \ 423 ((BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) ? \ 424 BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp)) 425 426 #define BP_GET_NDVAS(bp) \ 427 (BP_IS_EMBEDDED(bp) ? 0 : \ 428 !!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \ 429 !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ 430 !!DVA_GET_ASIZE(&(bp)->blk_dva[2])) 431 432 #define BP_COUNT_GANG(bp) \ 433 (BP_IS_EMBEDDED(bp) ? 0 : \ 434 (DVA_GET_GANG(&(bp)->blk_dva[0]) + \ 435 DVA_GET_GANG(&(bp)->blk_dva[1]) + \ 436 DVA_GET_GANG(&(bp)->blk_dva[2]))) 437 438 #define DVA_EQUAL(dva1, dva2) \ 439 ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \ 440 (dva1)->dva_word[0] == (dva2)->dva_word[0]) 441 442 #define BP_EQUAL(bp1, bp2) \ 443 (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \ 444 (bp1)->blk_birth == (bp2)->blk_birth && \ 445 DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \ 446 DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \ 447 DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2])) 448 449 #define ZIO_CHECKSUM_EQUAL(zc1, zc2) \ 450 (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \ 451 ((zc1).zc_word[1] - (zc2).zc_word[1]) | \ 452 ((zc1).zc_word[2] - (zc2).zc_word[2]) | \ 453 ((zc1).zc_word[3] - (zc2).zc_word[3]))) 454 455 #define ZIO_CHECKSUM_IS_ZERO(zc) \ 456 (0 == ((zc)->zc_word[0] | (zc)->zc_word[1] | \ 457 (zc)->zc_word[2] | (zc)->zc_word[3])) 458 459 #define ZIO_CHECKSUM_BSWAP(zcp) \ 460 { \ 461 (zcp)->zc_word[0] = BSWAP_64((zcp)->zc_word[0]); \ 462 (zcp)->zc_word[1] = BSWAP_64((zcp)->zc_word[1]); \ 463 (zcp)->zc_word[2] = BSWAP_64((zcp)->zc_word[2]); \ 464 (zcp)->zc_word[3] = BSWAP_64((zcp)->zc_word[3]); \ 465 } 466 467 468 #define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0) 469 470 #define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3) \ 471 { \ 472 (zcp)->zc_word[0] = w0; \ 473 (zcp)->zc_word[1] = w1; \ 474 (zcp)->zc_word[2] = w2; \ 475 (zcp)->zc_word[3] = w3; \ 476 } 477 478 #define BP_IDENTITY(bp) (ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0]) 479 #define BP_IS_GANG(bp) \ 480 (BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp))) 481 #define DVA_IS_EMPTY(dva) ((dva)->dva_word[0] == 0ULL && \ 482 (dva)->dva_word[1] == 0ULL) 483 #define BP_IS_HOLE(bp) \ 484 (!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp))) 485 486 /* BP_IS_RAIDZ(bp) assumes no block compression */ 487 #define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \ 488 BP_GET_PSIZE(bp)) 489 490 #define BP_ZERO(bp) \ 491 { \ 492 (bp)->blk_dva[0].dva_word[0] = 0; \ 493 (bp)->blk_dva[0].dva_word[1] = 0; \ 494 (bp)->blk_dva[1].dva_word[0] = 0; \ 495 (bp)->blk_dva[1].dva_word[1] = 0; \ 496 (bp)->blk_dva[2].dva_word[0] = 0; \ 497 (bp)->blk_dva[2].dva_word[1] = 0; \ 498 (bp)->blk_prop = 0; \ 499 (bp)->blk_pad[0] = 0; \ 500 (bp)->blk_pad[1] = 0; \ 501 (bp)->blk_phys_birth = 0; \ 502 (bp)->blk_birth = 0; \ 503 (bp)->blk_fill = 0; \ 504 ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \ 505 } 506 507 #ifdef _BIG_ENDIAN 508 #define ZFS_HOST_BYTEORDER (0ULL) 509 #else 510 #define ZFS_HOST_BYTEORDER (1ULL) 511 #endif 512 513 #define BP_SHOULD_BYTESWAP(bp) (BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER) 514 515 #define BP_SPRINTF_LEN 320 516 517 /* 518 * This macro allows code sharing between zfs, libzpool, and mdb. 519 * 'func' is either snprintf() or mdb_snprintf(). 520 * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line. 521 */ 522 #define SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \ 523 { \ 524 static const char *copyname[] = \ 525 { "zero", "single", "double", "triple" }; \ 526 int len = 0; \ 527 int copies = 0; \ 528 \ 529 if (bp == NULL) { \ 530 len += func(buf + len, size - len, "<NULL>"); \ 531 } else if (BP_IS_HOLE(bp)) { \ 532 len += func(buf + len, size - len, \ 533 "HOLE [L%llu %s] " \ 534 "size=%llxL birth=%lluL", \ 535 (u_longlong_t)BP_GET_LEVEL(bp), \ 536 type, \ 537 (u_longlong_t)BP_GET_LSIZE(bp), \ 538 (u_longlong_t)bp->blk_birth); \ 539 } else if (BP_IS_EMBEDDED(bp)) { \ 540 len = func(buf + len, size - len, \ 541 "EMBEDDED [L%llu %s] et=%u %s " \ 542 "size=%llxL/%llxP birth=%lluL", \ 543 (u_longlong_t)BP_GET_LEVEL(bp), \ 544 type, \ 545 (int)BPE_GET_ETYPE(bp), \ 546 compress, \ 547 (u_longlong_t)BPE_GET_LSIZE(bp), \ 548 (u_longlong_t)BPE_GET_PSIZE(bp), \ 549 (u_longlong_t)bp->blk_birth); \ 550 } else { \ 551 for (int d = 0; d < BP_GET_NDVAS(bp); d++) { \ 552 const dva_t *dva = &bp->blk_dva[d]; \ 553 if (DVA_IS_VALID(dva)) \ 554 copies++; \ 555 len += func(buf + len, size - len, \ 556 "DVA[%d]=<%llu:%llx:%llx>%c", d, \ 557 (u_longlong_t)DVA_GET_VDEV(dva), \ 558 (u_longlong_t)DVA_GET_OFFSET(dva), \ 559 (u_longlong_t)DVA_GET_ASIZE(dva), \ 560 ws); \ 561 } \ 562 if (BP_IS_GANG(bp) && \ 563 DVA_GET_ASIZE(&bp->blk_dva[2]) <= \ 564 DVA_GET_ASIZE(&bp->blk_dva[1]) / 2) \ 565 copies--; \ 566 len += func(buf + len, size - len, \ 567 "[L%llu %s] %s %s %s %s %s %s%c" \ 568 "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c" \ 569 "cksum=%llx:%llx:%llx:%llx", \ 570 (u_longlong_t)BP_GET_LEVEL(bp), \ 571 type, \ 572 checksum, \ 573 compress, \ 574 BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE", \ 575 BP_IS_GANG(bp) ? "gang" : "contiguous", \ 576 BP_GET_DEDUP(bp) ? "dedup" : "unique", \ 577 copyname[copies], \ 578 ws, \ 579 (u_longlong_t)BP_GET_LSIZE(bp), \ 580 (u_longlong_t)BP_GET_PSIZE(bp), \ 581 (u_longlong_t)bp->blk_birth, \ 582 (u_longlong_t)BP_PHYSICAL_BIRTH(bp), \ 583 (u_longlong_t)BP_GET_FILL(bp), \ 584 ws, \ 585 (u_longlong_t)bp->blk_cksum.zc_word[0], \ 586 (u_longlong_t)bp->blk_cksum.zc_word[1], \ 587 (u_longlong_t)bp->blk_cksum.zc_word[2], \ 588 (u_longlong_t)bp->blk_cksum.zc_word[3]); \ 589 } \ 590 ASSERT(len < size); \ 591 } 592 593 #include <sys/dmu.h> 594 595 #define BP_GET_BUFC_TYPE(bp) \ 596 (((BP_GET_LEVEL(bp) > 0) || (DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))) ? \ 597 ARC_BUFC_METADATA : ARC_BUFC_DATA) 598 599 typedef enum spa_import_type { 600 SPA_IMPORT_EXISTING, 601 SPA_IMPORT_ASSEMBLE 602 } spa_import_type_t; 603 604 /* state manipulation functions */ 605 extern int spa_open(const char *pool, spa_t **, void *tag); 606 extern int spa_open_rewind(const char *pool, spa_t **, void *tag, 607 nvlist_t *policy, nvlist_t **config); 608 extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot, 609 size_t buflen); 610 extern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props, 611 nvlist_t *zplprops); 612 extern int spa_import_rootpool(char *devpath, char *devid); 613 extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props, 614 uint64_t flags); 615 extern nvlist_t *spa_tryimport(nvlist_t *tryconfig); 616 extern int spa_destroy(char *pool); 617 extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 618 boolean_t hardforce); 619 extern int spa_reset(char *pool); 620 extern void spa_async_request(spa_t *spa, int flag); 621 extern void spa_async_unrequest(spa_t *spa, int flag); 622 extern void spa_async_suspend(spa_t *spa); 623 extern void spa_async_resume(spa_t *spa); 624 extern spa_t *spa_inject_addref(char *pool); 625 extern void spa_inject_delref(spa_t *spa); 626 extern void spa_scan_stat_init(spa_t *spa); 627 extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps); 628 629 #define SPA_ASYNC_CONFIG_UPDATE 0x01 630 #define SPA_ASYNC_REMOVE 0x02 631 #define SPA_ASYNC_PROBE 0x04 632 #define SPA_ASYNC_RESILVER_DONE 0x08 633 #define SPA_ASYNC_RESILVER 0x10 634 #define SPA_ASYNC_AUTOEXPAND 0x20 635 #define SPA_ASYNC_REMOVE_DONE 0x40 636 #define SPA_ASYNC_REMOVE_STOP 0x80 637 638 /* 639 * Controls the behavior of spa_vdev_remove(). 640 */ 641 #define SPA_REMOVE_UNSPARE 0x01 642 #define SPA_REMOVE_DONE 0x02 643 644 /* device manipulation */ 645 extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot); 646 extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, 647 int replacing); 648 extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, 649 int replace_done); 650 extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare); 651 extern boolean_t spa_vdev_remove_active(spa_t *spa); 652 extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath); 653 extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru); 654 extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 655 nvlist_t *props, boolean_t exp); 656 657 /* spare state (which is global across all pools) */ 658 extern void spa_spare_add(vdev_t *vd); 659 extern void spa_spare_remove(vdev_t *vd); 660 extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt); 661 extern void spa_spare_activate(vdev_t *vd); 662 663 /* L2ARC state (which is global across all pools) */ 664 extern void spa_l2cache_add(vdev_t *vd); 665 extern void spa_l2cache_remove(vdev_t *vd); 666 extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool); 667 extern void spa_l2cache_activate(vdev_t *vd); 668 extern void spa_l2cache_drop(spa_t *spa); 669 670 /* scanning */ 671 extern int spa_scan(spa_t *spa, pool_scan_func_t func); 672 extern int spa_scan_stop(spa_t *spa); 673 674 /* spa syncing */ 675 extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */ 676 extern void spa_sync_allpools(void); 677 678 /* spa namespace global mutex */ 679 extern kmutex_t spa_namespace_lock; 680 681 /* 682 * SPA configuration functions in spa_config.c 683 */ 684 685 #define SPA_CONFIG_UPDATE_POOL 0 686 #define SPA_CONFIG_UPDATE_VDEVS 1 687 688 extern void spa_config_sync(spa_t *, boolean_t, boolean_t); 689 extern void spa_config_load(void); 690 extern nvlist_t *spa_all_configs(uint64_t *); 691 extern void spa_config_set(spa_t *spa, nvlist_t *config); 692 extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, 693 int getstats); 694 extern void spa_config_update(spa_t *spa, int what); 695 696 /* 697 * Miscellaneous SPA routines in spa_misc.c 698 */ 699 700 /* Namespace manipulation */ 701 extern spa_t *spa_lookup(const char *name); 702 extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot); 703 extern void spa_remove(spa_t *spa); 704 extern spa_t *spa_next(spa_t *prev); 705 706 /* Refcount functions */ 707 extern void spa_open_ref(spa_t *spa, void *tag); 708 extern void spa_close(spa_t *spa, void *tag); 709 extern void spa_async_close(spa_t *spa, void *tag); 710 extern boolean_t spa_refcount_zero(spa_t *spa); 711 712 #define SCL_NONE 0x00 713 #define SCL_CONFIG 0x01 714 #define SCL_STATE 0x02 715 #define SCL_L2ARC 0x04 /* hack until L2ARC 2.0 */ 716 #define SCL_ALLOC 0x08 717 #define SCL_ZIO 0x10 718 #define SCL_FREE 0x20 719 #define SCL_VDEV 0x40 720 #define SCL_LOCKS 7 721 #define SCL_ALL ((1 << SCL_LOCKS) - 1) 722 #define SCL_STATE_ALL (SCL_STATE | SCL_L2ARC | SCL_ZIO) 723 724 /* Pool configuration locks */ 725 extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw); 726 extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw); 727 extern void spa_config_exit(spa_t *spa, int locks, void *tag); 728 extern int spa_config_held(spa_t *spa, int locks, krw_t rw); 729 730 /* Pool vdev add/remove lock */ 731 extern uint64_t spa_vdev_enter(spa_t *spa); 732 extern uint64_t spa_vdev_config_enter(spa_t *spa); 733 extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, 734 int error, char *tag); 735 extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error); 736 737 /* Pool vdev state change lock */ 738 extern void spa_vdev_state_enter(spa_t *spa, int oplock); 739 extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error); 740 741 /* Log state */ 742 typedef enum spa_log_state { 743 SPA_LOG_UNKNOWN = 0, /* unknown log state */ 744 SPA_LOG_MISSING, /* missing log(s) */ 745 SPA_LOG_CLEAR, /* clear the log(s) */ 746 SPA_LOG_GOOD, /* log(s) are good */ 747 } spa_log_state_t; 748 749 extern spa_log_state_t spa_get_log_state(spa_t *spa); 750 extern void spa_set_log_state(spa_t *spa, spa_log_state_t state); 751 extern int spa_offline_log(spa_t *spa); 752 753 /* Log claim callback */ 754 extern void spa_claim_notify(zio_t *zio); 755 756 /* Accessor functions */ 757 extern boolean_t spa_shutting_down(spa_t *spa); 758 extern struct dsl_pool *spa_get_dsl(spa_t *spa); 759 extern boolean_t spa_is_initializing(spa_t *spa); 760 extern blkptr_t *spa_get_rootblkptr(spa_t *spa); 761 extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp); 762 extern void spa_altroot(spa_t *, char *, size_t); 763 extern int spa_sync_pass(spa_t *spa); 764 extern char *spa_name(spa_t *spa); 765 extern uint64_t spa_guid(spa_t *spa); 766 extern uint64_t spa_load_guid(spa_t *spa); 767 extern uint64_t spa_last_synced_txg(spa_t *spa); 768 extern uint64_t spa_first_txg(spa_t *spa); 769 extern uint64_t spa_syncing_txg(spa_t *spa); 770 extern uint64_t spa_version(spa_t *spa); 771 extern pool_state_t spa_state(spa_t *spa); 772 extern spa_load_state_t spa_load_state(spa_t *spa); 773 extern uint64_t spa_freeze_txg(spa_t *spa); 774 extern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize); 775 extern uint64_t spa_get_dspace(spa_t *spa); 776 extern uint64_t spa_get_slop_space(spa_t *spa); 777 extern void spa_update_dspace(spa_t *spa); 778 extern uint64_t spa_version(spa_t *spa); 779 extern boolean_t spa_deflate(spa_t *spa); 780 extern metaslab_class_t *spa_normal_class(spa_t *spa); 781 extern metaslab_class_t *spa_log_class(spa_t *spa); 782 extern void spa_evicting_os_register(spa_t *, objset_t *os); 783 extern void spa_evicting_os_deregister(spa_t *, objset_t *os); 784 extern void spa_evicting_os_wait(spa_t *spa); 785 extern int spa_max_replication(spa_t *spa); 786 extern int spa_prev_software_version(spa_t *spa); 787 extern int spa_busy(void); 788 extern uint8_t spa_get_failmode(spa_t *spa); 789 extern boolean_t spa_suspended(spa_t *spa); 790 extern uint64_t spa_bootfs(spa_t *spa); 791 extern uint64_t spa_delegation(spa_t *spa); 792 extern objset_t *spa_meta_objset(spa_t *spa); 793 extern uint64_t spa_deadman_synctime(spa_t *spa); 794 795 /* Miscellaneous support routines */ 796 extern void spa_activate_mos_feature(spa_t *spa, const char *feature, 797 dmu_tx_t *tx); 798 extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature); 799 extern int spa_rename(const char *oldname, const char *newname); 800 extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid); 801 extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid); 802 extern char *spa_strdup(const char *); 803 extern void spa_strfree(char *); 804 extern uint64_t spa_get_random(uint64_t range); 805 extern uint64_t spa_generate_guid(spa_t *spa); 806 extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp); 807 extern void spa_freeze(spa_t *spa); 808 extern int spa_change_guid(spa_t *spa); 809 extern void spa_upgrade(spa_t *spa, uint64_t version); 810 extern void spa_evict_all(void); 811 extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid, 812 boolean_t l2cache); 813 extern boolean_t spa_has_spare(spa_t *, uint64_t guid); 814 extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva); 815 extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp); 816 extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp); 817 extern boolean_t spa_has_slogs(spa_t *spa); 818 extern boolean_t spa_is_root(spa_t *spa); 819 extern boolean_t spa_writeable(spa_t *spa); 820 extern boolean_t spa_has_pending_synctask(spa_t *spa); 821 extern int spa_maxblocksize(spa_t *spa); 822 extern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp); 823 824 extern int spa_mode(spa_t *spa); 825 extern uint64_t strtonum(const char *str, char **nptr); 826 827 extern char *spa_his_ievent_table[]; 828 829 extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx); 830 extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read, 831 char *his_buf); 832 extern int spa_history_log(spa_t *spa, const char *his_buf); 833 extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl); 834 extern void spa_history_log_version(spa_t *spa, const char *operation); 835 extern void spa_history_log_internal(spa_t *spa, const char *operation, 836 dmu_tx_t *tx, const char *fmt, ...); 837 extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op, 838 dmu_tx_t *tx, const char *fmt, ...); 839 extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation, 840 dmu_tx_t *tx, const char *fmt, ...); 841 842 /* error handling */ 843 struct zbookmark_phys; 844 extern void spa_log_error(spa_t *spa, zio_t *zio); 845 extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd, 846 zio_t *zio, uint64_t stateoroffset, uint64_t length); 847 extern void zfs_post_remove(spa_t *spa, vdev_t *vd); 848 extern void zfs_post_state_change(spa_t *spa, vdev_t *vd); 849 extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd); 850 extern uint64_t spa_get_errlog_size(spa_t *spa); 851 extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count); 852 extern void spa_errlog_rotate(spa_t *spa); 853 extern void spa_errlog_drain(spa_t *spa); 854 extern void spa_errlog_sync(spa_t *spa, uint64_t txg); 855 extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub); 856 857 /* vdev cache */ 858 extern void vdev_cache_stat_init(void); 859 extern void vdev_cache_stat_fini(void); 860 861 /* Initialization and termination */ 862 extern void spa_init(int flags); 863 extern void spa_fini(void); 864 extern void spa_boot_init(); 865 866 /* properties */ 867 extern int spa_prop_set(spa_t *spa, nvlist_t *nvp); 868 extern int spa_prop_get(spa_t *spa, nvlist_t **nvp); 869 extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx); 870 extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t); 871 872 /* asynchronous event notification */ 873 extern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name); 874 875 #ifdef ZFS_DEBUG 876 #define dprintf_bp(bp, fmt, ...) do { \ 877 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 878 char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP); \ 879 snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp)); \ 880 dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf); \ 881 kmem_free(__blkbuf, BP_SPRINTF_LEN); \ 882 } \ 883 _NOTE(CONSTCOND) } while (0) 884 #else 885 #define dprintf_bp(bp, fmt, ...) 886 #endif 887 888 extern boolean_t spa_debug_enabled(spa_t *spa); 889 #define spa_dbgmsg(spa, ...) \ 890 { \ 891 if (spa_debug_enabled(spa)) \ 892 zfs_dbgmsg(__VA_ARGS__); \ 893 } 894 895 extern int spa_mode_global; /* mode, e.g. FREAD | FWRITE */ 896 897 #ifdef __cplusplus 898 } 899 #endif 900 901 #endif /* _SYS_SPA_H */ 902