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