1 /*- 2 * Copyright (c) 2002 McAfee, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by Marshall 6 * Kirk McKusick and McAfee Research,, the Security Research Division of 7 * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as 8 * part of the DARPA CHATS research program 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 /* 32 * CDDL HEADER START 33 * 34 * The contents of this file are subject to the terms of the 35 * Common Development and Distribution License (the "License"). 36 * You may not use this file except in compliance with the License. 37 * 38 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 39 * or http://www.opensolaris.org/os/licensing. 40 * See the License for the specific language governing permissions 41 * and limitations under the License. 42 * 43 * When distributing Covered Code, include this CDDL HEADER in each 44 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 45 * If applicable, add the following below this CDDL HEADER, with the 46 * fields enclosed by brackets "[]" replaced with your own identifying 47 * information: Portions Copyright [yyyy] [name of copyright owner] 48 * 49 * CDDL HEADER END 50 */ 51 /* 52 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 53 * Use is subject to license terms. 54 */ 55 /* 56 * Copyright 2013 by Saso Kiselkov. All rights reserved. 57 */ 58 /* 59 * Copyright (c) 2013 by Delphix. All rights reserved. 60 */ 61 62 #define MAXNAMELEN 256 63 64 #define _NOTE(s) 65 66 /* 67 * AVL comparator helpers 68 */ 69 #define AVL_ISIGN(a) (((a) > 0) - ((a) < 0)) 70 #define AVL_CMP(a, b) (((a) > (b)) - ((a) < (b))) 71 #define AVL_PCMP(a, b) \ 72 (((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b))) 73 74 typedef enum { B_FALSE, B_TRUE } boolean_t; 75 76 /* CRC64 table */ 77 #define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */ 78 79 /* 80 * Macros for various sorts of alignment and rounding when the alignment 81 * is known to be a power of 2. 82 */ 83 #define P2ALIGN(x, align) ((x) & -(align)) 84 #define P2PHASE(x, align) ((x) & ((align) - 1)) 85 #define P2NPHASE(x, align) (-(x) & ((align) - 1)) 86 #define P2ROUNDUP(x, align) (-(-(x) & -(align))) 87 #define P2END(x, align) (-(~(x) & -(align))) 88 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align))) 89 #define P2BOUNDARY(off, len, align) (((off) ^ ((off) + (len) - 1)) > (align) - 1) 90 91 /* 92 * General-purpose 32-bit and 64-bit bitfield encodings. 93 */ 94 #define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len)) 95 #define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len)) 96 #define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low)) 97 #define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low)) 98 99 #define BF32_GET(x, low, len) BF32_DECODE(x, low, len) 100 #define BF64_GET(x, low, len) BF64_DECODE(x, low, len) 101 102 #define BF32_SET(x, low, len, val) \ 103 ((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len)) 104 #define BF64_SET(x, low, len, val) \ 105 ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)) 106 107 #define BF32_GET_SB(x, low, len, shift, bias) \ 108 ((BF32_GET(x, low, len) + (bias)) << (shift)) 109 #define BF64_GET_SB(x, low, len, shift, bias) \ 110 ((BF64_GET(x, low, len) + (bias)) << (shift)) 111 112 #define BF32_SET_SB(x, low, len, shift, bias, val) \ 113 BF32_SET(x, low, len, ((val) >> (shift)) - (bias)) 114 #define BF64_SET_SB(x, low, len, shift, bias, val) \ 115 BF64_SET(x, low, len, ((val) >> (shift)) - (bias)) 116 117 /* 118 * Macros to reverse byte order 119 */ 120 #define BSWAP_8(x) ((x) & 0xff) 121 #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 122 #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) 123 #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32)) 124 125 #define SPA_MINBLOCKSHIFT 9 126 #define SPA_OLDMAXBLOCKSHIFT 17 127 #define SPA_MAXBLOCKSHIFT 24 128 #define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT) 129 #define SPA_OLDMAXBLOCKSIZE (1ULL << SPA_OLDMAXBLOCKSHIFT) 130 #define SPA_MAXBLOCKSIZE (1ULL << SPA_MAXBLOCKSHIFT) 131 132 /* 133 * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB. 134 * The ASIZE encoding should be at least 64 times larger (6 more bits) 135 * to support up to 4-way RAID-Z mirror mode with worst-case gang block 136 * overhead, three DVAs per bp, plus one more bit in case we do anything 137 * else that expands the ASIZE. 138 */ 139 #define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */ 140 #define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */ 141 #define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */ 142 143 /* 144 * All SPA data is represented by 128-bit data virtual addresses (DVAs). 145 * The members of the dva_t should be considered opaque outside the SPA. 146 */ 147 typedef struct dva { 148 uint64_t dva_word[2]; 149 } dva_t; 150 151 /* 152 * Each block has a 256-bit checksum -- strong enough for cryptographic hashes. 153 */ 154 typedef struct zio_cksum { 155 uint64_t zc_word[4]; 156 } zio_cksum_t; 157 158 /* 159 * Some checksums/hashes need a 256-bit initialization salt. This salt is kept 160 * secret and is suitable for use in MAC algorithms as the key. 161 */ 162 typedef struct zio_cksum_salt { 163 uint8_t zcs_bytes[32]; 164 } zio_cksum_salt_t; 165 166 /* 167 * Each block is described by its DVAs, time of birth, checksum, etc. 168 * The word-by-word, bit-by-bit layout of the blkptr is as follows: 169 * 170 * 64 56 48 40 32 24 16 8 0 171 * +-------+-------+-------+-------+-------+-------+-------+-------+ 172 * 0 | vdev1 | GRID | ASIZE | 173 * +-------+-------+-------+-------+-------+-------+-------+-------+ 174 * 1 |G| offset1 | 175 * +-------+-------+-------+-------+-------+-------+-------+-------+ 176 * 2 | vdev2 | GRID | ASIZE | 177 * +-------+-------+-------+-------+-------+-------+-------+-------+ 178 * 3 |G| offset2 | 179 * +-------+-------+-------+-------+-------+-------+-------+-------+ 180 * 4 | vdev3 | GRID | ASIZE | 181 * +-------+-------+-------+-------+-------+-------+-------+-------+ 182 * 5 |G| offset3 | 183 * +-------+-------+-------+-------+-------+-------+-------+-------+ 184 * 6 |BDX|lvl| type | cksum |E| comp| PSIZE | LSIZE | 185 * +-------+-------+-------+-------+-------+-------+-------+-------+ 186 * 7 | padding | 187 * +-------+-------+-------+-------+-------+-------+-------+-------+ 188 * 8 | padding | 189 * +-------+-------+-------+-------+-------+-------+-------+-------+ 190 * 9 | physical birth txg | 191 * +-------+-------+-------+-------+-------+-------+-------+-------+ 192 * a | logical birth txg | 193 * +-------+-------+-------+-------+-------+-------+-------+-------+ 194 * b | fill count | 195 * +-------+-------+-------+-------+-------+-------+-------+-------+ 196 * c | checksum[0] | 197 * +-------+-------+-------+-------+-------+-------+-------+-------+ 198 * d | checksum[1] | 199 * +-------+-------+-------+-------+-------+-------+-------+-------+ 200 * e | checksum[2] | 201 * +-------+-------+-------+-------+-------+-------+-------+-------+ 202 * f | checksum[3] | 203 * +-------+-------+-------+-------+-------+-------+-------+-------+ 204 * 205 * Legend: 206 * 207 * vdev virtual device ID 208 * offset offset into virtual device 209 * LSIZE logical size 210 * PSIZE physical size (after compression) 211 * ASIZE allocated size (including RAID-Z parity and gang block headers) 212 * GRID RAID-Z layout information (reserved for future use) 213 * cksum checksum function 214 * comp compression function 215 * G gang block indicator 216 * B byteorder (endianness) 217 * D dedup 218 * X encryption (on version 30, which is not supported) 219 * E blkptr_t contains embedded data (see below) 220 * lvl level of indirection 221 * type DMU object type 222 * phys birth txg of block allocation; zero if same as logical birth txg 223 * log. birth transaction group in which the block was logically born 224 * fill count number of non-zero blocks under this bp 225 * checksum[4] 256-bit checksum of the data this bp describes 226 */ 227 228 /* 229 * "Embedded" blkptr_t's don't actually point to a block, instead they 230 * have a data payload embedded in the blkptr_t itself. See the comment 231 * in blkptr.c for more details. 232 * 233 * The blkptr_t is laid out as follows: 234 * 235 * 64 56 48 40 32 24 16 8 0 236 * +-------+-------+-------+-------+-------+-------+-------+-------+ 237 * 0 | payload | 238 * 1 | payload | 239 * 2 | payload | 240 * 3 | payload | 241 * 4 | payload | 242 * 5 | payload | 243 * +-------+-------+-------+-------+-------+-------+-------+-------+ 244 * 6 |BDX|lvl| type | etype |E| comp| PSIZE| LSIZE | 245 * +-------+-------+-------+-------+-------+-------+-------+-------+ 246 * 7 | payload | 247 * 8 | payload | 248 * 9 | payload | 249 * +-------+-------+-------+-------+-------+-------+-------+-------+ 250 * a | logical birth txg | 251 * +-------+-------+-------+-------+-------+-------+-------+-------+ 252 * b | payload | 253 * c | payload | 254 * d | payload | 255 * e | payload | 256 * f | payload | 257 * +-------+-------+-------+-------+-------+-------+-------+-------+ 258 * 259 * Legend: 260 * 261 * payload contains the embedded data 262 * B (byteorder) byteorder (endianness) 263 * D (dedup) padding (set to zero) 264 * X encryption (set to zero; see above) 265 * E (embedded) set to one 266 * lvl indirection level 267 * type DMU object type 268 * etype how to interpret embedded data (BP_EMBEDDED_TYPE_*) 269 * comp compression function of payload 270 * PSIZE size of payload after compression, in bytes 271 * LSIZE logical size of payload, in bytes 272 * note that 25 bits is enough to store the largest 273 * "normal" BP's LSIZE (2^16 * 2^9) in bytes 274 * log. birth transaction group in which the block was logically born 275 * 276 * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded 277 * bp's they are stored in units of SPA_MINBLOCKSHIFT. 278 * Generally, the generic BP_GET_*() macros can be used on embedded BP's. 279 * The B, D, X, lvl, type, and comp fields are stored the same as with normal 280 * BP's so the BP_SET_* macros can be used with them. etype, PSIZE, LSIZE must 281 * be set with the BPE_SET_* macros. BP_SET_EMBEDDED() should be called before 282 * other macros, as they assert that they are only used on BP's of the correct 283 * "embedded-ness". 284 */ 285 286 #define BPE_GET_ETYPE(bp) \ 287 (ASSERT(BP_IS_EMBEDDED(bp)), \ 288 BF64_GET((bp)->blk_prop, 40, 8)) 289 #define BPE_SET_ETYPE(bp, t) do { \ 290 ASSERT(BP_IS_EMBEDDED(bp)); \ 291 BF64_SET((bp)->blk_prop, 40, 8, t); \ 292 _NOTE(CONSTCOND) } while (0) 293 294 #define BPE_GET_LSIZE(bp) \ 295 (ASSERT(BP_IS_EMBEDDED(bp)), \ 296 BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1)) 297 #define BPE_SET_LSIZE(bp, x) do { \ 298 ASSERT(BP_IS_EMBEDDED(bp)); \ 299 BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \ 300 _NOTE(CONSTCOND) } while (0) 301 302 #define BPE_GET_PSIZE(bp) \ 303 (ASSERT(BP_IS_EMBEDDED(bp)), \ 304 BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1)) 305 #define BPE_SET_PSIZE(bp, x) do { \ 306 ASSERT(BP_IS_EMBEDDED(bp)); \ 307 BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \ 308 _NOTE(CONSTCOND) } while (0) 309 310 typedef enum bp_embedded_type { 311 BP_EMBEDDED_TYPE_DATA, 312 BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */ 313 NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED 314 } bp_embedded_type_t; 315 316 #define BPE_NUM_WORDS 14 317 #define BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t)) 318 #define BPE_IS_PAYLOADWORD(bp, wp) \ 319 ((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth) 320 321 #define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */ 322 #define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */ 323 324 typedef struct blkptr { 325 dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */ 326 uint64_t blk_prop; /* size, compression, type, etc */ 327 uint64_t blk_pad[2]; /* Extra space for the future */ 328 uint64_t blk_phys_birth; /* txg when block was allocated */ 329 uint64_t blk_birth; /* transaction group at birth */ 330 uint64_t blk_fill; /* fill count */ 331 zio_cksum_t blk_cksum; /* 256-bit checksum */ 332 } blkptr_t; 333 334 /* 335 * Macros to get and set fields in a bp or DVA. 336 */ 337 #define DVA_GET_ASIZE(dva) \ 338 BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0) 339 #define DVA_SET_ASIZE(dva, x) \ 340 BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \ 341 SPA_MINBLOCKSHIFT, 0, x) 342 343 #define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8) 344 #define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x) 345 346 #define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32) 347 #define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x) 348 349 #define DVA_GET_OFFSET(dva) \ 350 BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0) 351 #define DVA_SET_OFFSET(dva, x) \ 352 BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x) 353 354 #define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1) 355 #define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x) 356 357 #define BP_GET_LSIZE(bp) \ 358 (BP_IS_EMBEDDED(bp) ? \ 359 (BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \ 360 BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)) 361 #define BP_SET_LSIZE(bp, x) do { \ 362 ASSERT(!BP_IS_EMBEDDED(bp)); \ 363 BF64_SET_SB((bp)->blk_prop, \ 364 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \ 365 _NOTE(CONSTCOND) } while (0) 366 367 #define BP_GET_PSIZE(bp) \ 368 BF64_GET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1) 369 #define BP_SET_PSIZE(bp, x) \ 370 BF64_SET_SB((bp)->blk_prop, 16, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x) 371 372 #define BP_GET_COMPRESS(bp) BF64_GET((bp)->blk_prop, 32, 7) 373 #define BP_SET_COMPRESS(bp, x) BF64_SET((bp)->blk_prop, 32, 7, x) 374 375 #define BP_GET_CHECKSUM(bp) BF64_GET((bp)->blk_prop, 40, 8) 376 #define BP_SET_CHECKSUM(bp, x) BF64_SET((bp)->blk_prop, 40, 8, x) 377 378 #define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8) 379 #define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x) 380 381 #define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5) 382 #define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x) 383 384 #define BP_IS_EMBEDDED(bp) BF64_GET((bp)->blk_prop, 39, 1) 385 386 #define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1) 387 #define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x) 388 389 #define BP_GET_BYTEORDER(bp) BF64_GET((bp)->blk_prop, 63, 1) 390 #define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x) 391 392 #define BP_PHYSICAL_BIRTH(bp) \ 393 ((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth) 394 395 #define BP_GET_ASIZE(bp) \ 396 (DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ 397 DVA_GET_ASIZE(&(bp)->blk_dva[2])) 398 399 #define BP_GET_UCSIZE(bp) \ 400 ((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \ 401 BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp)); 402 403 #define BP_GET_NDVAS(bp) \ 404 (!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \ 405 !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ 406 !!DVA_GET_ASIZE(&(bp)->blk_dva[2])) 407 408 #define DVA_EQUAL(dva1, dva2) \ 409 ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \ 410 (dva1)->dva_word[0] == (dva2)->dva_word[0]) 411 412 #define ZIO_CHECKSUM_EQUAL(zc1, zc2) \ 413 (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \ 414 ((zc1).zc_word[1] - (zc2).zc_word[1]) | \ 415 ((zc1).zc_word[2] - (zc2).zc_word[2]) | \ 416 ((zc1).zc_word[3] - (zc2).zc_word[3]))) 417 418 419 #define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0) 420 421 #define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3) \ 422 { \ 423 (zcp)->zc_word[0] = w0; \ 424 (zcp)->zc_word[1] = w1; \ 425 (zcp)->zc_word[2] = w2; \ 426 (zcp)->zc_word[3] = w3; \ 427 } 428 429 #define BP_IDENTITY(bp) (&(bp)->blk_dva[0]) 430 #define BP_IS_GANG(bp) DVA_GET_GANG(BP_IDENTITY(bp)) 431 #define DVA_IS_EMPTY(dva) ((dva)->dva_word[0] == 0ULL && \ 432 (dva)->dva_word[1] == 0ULL) 433 #define BP_IS_HOLE(bp) DVA_IS_EMPTY(BP_IDENTITY(bp)) 434 #define BP_IS_OLDER(bp, txg) (!BP_IS_HOLE(bp) && (bp)->blk_birth < (txg)) 435 436 #define BP_ZERO(bp) \ 437 { \ 438 (bp)->blk_dva[0].dva_word[0] = 0; \ 439 (bp)->blk_dva[0].dva_word[1] = 0; \ 440 (bp)->blk_dva[1].dva_word[0] = 0; \ 441 (bp)->blk_dva[1].dva_word[1] = 0; \ 442 (bp)->blk_dva[2].dva_word[0] = 0; \ 443 (bp)->blk_dva[2].dva_word[1] = 0; \ 444 (bp)->blk_prop = 0; \ 445 (bp)->blk_pad[0] = 0; \ 446 (bp)->blk_pad[1] = 0; \ 447 (bp)->blk_phys_birth = 0; \ 448 (bp)->blk_birth = 0; \ 449 (bp)->blk_fill = 0; \ 450 ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \ 451 } 452 453 #if BYTE_ORDER == _BIG_ENDIAN 454 #define ZFS_HOST_BYTEORDER (0ULL) 455 #else 456 #define ZFS_HOST_BYTEORDER (1ULL) 457 #endif 458 459 #define BP_SHOULD_BYTESWAP(bp) (BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER) 460 #define BPE_NUM_WORDS 14 461 #define BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t)) 462 #define BPE_IS_PAYLOADWORD(bp, wp) \ 463 ((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth) 464 465 /* 466 * Embedded checksum 467 */ 468 #define ZEC_MAGIC 0x210da7ab10c7a11ULL 469 470 typedef struct zio_eck { 471 uint64_t zec_magic; /* for validation, endianness */ 472 zio_cksum_t zec_cksum; /* 256-bit checksum */ 473 } zio_eck_t; 474 475 /* 476 * Gang block headers are self-checksumming and contain an array 477 * of block pointers. 478 */ 479 #define SPA_GANGBLOCKSIZE SPA_MINBLOCKSIZE 480 #define SPA_GBH_NBLKPTRS ((SPA_GANGBLOCKSIZE - \ 481 sizeof (zio_eck_t)) / sizeof (blkptr_t)) 482 #define SPA_GBH_FILLER ((SPA_GANGBLOCKSIZE - \ 483 sizeof (zio_eck_t) - \ 484 (SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\ 485 sizeof (uint64_t)) 486 487 typedef struct zio_gbh { 488 blkptr_t zg_blkptr[SPA_GBH_NBLKPTRS]; 489 uint64_t zg_filler[SPA_GBH_FILLER]; 490 zio_eck_t zg_tail; 491 } zio_gbh_phys_t; 492 493 #define VDEV_RAIDZ_MAXPARITY 3 494 495 #define VDEV_PAD_SIZE (8 << 10) 496 /* 2 padding areas (vl_pad1 and vl_pad2) to skip */ 497 #define VDEV_SKIP_SIZE VDEV_PAD_SIZE * 2 498 #define VDEV_PHYS_SIZE (112 << 10) 499 #define VDEV_UBERBLOCK_RING (128 << 10) 500 501 /* 502 * MMP blocks occupy the last MMP_BLOCKS_PER_LABEL slots in the uberblock 503 * ring when MMP is enabled. 504 */ 505 #define MMP_BLOCKS_PER_LABEL 1 506 507 /* The largest uberblock we support is 8k. */ 508 #define MAX_UBERBLOCK_SHIFT (13) 509 #define VDEV_UBERBLOCK_SHIFT(vd) \ 510 MIN(MAX((vd)->v_top->v_ashift, UBERBLOCK_SHIFT), MAX_UBERBLOCK_SHIFT) 511 #define VDEV_UBERBLOCK_COUNT(vd) \ 512 (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd)) 513 #define VDEV_UBERBLOCK_OFFSET(vd, n) \ 514 offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)]) 515 #define VDEV_UBERBLOCK_SIZE(vd) (1ULL << VDEV_UBERBLOCK_SHIFT(vd)) 516 517 typedef struct vdev_phys { 518 char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)]; 519 zio_eck_t vp_zbt; 520 } vdev_phys_t; 521 522 typedef struct vdev_label { 523 char vl_pad1[VDEV_PAD_SIZE]; /* 8K */ 524 char vl_pad2[VDEV_PAD_SIZE]; /* 8K */ 525 vdev_phys_t vl_vdev_phys; /* 112K */ 526 char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */ 527 } vdev_label_t; /* 256K total */ 528 529 /* 530 * vdev_dirty() flags 531 */ 532 #define VDD_METASLAB 0x01 533 #define VDD_DTL 0x02 534 535 /* 536 * Size and offset of embedded boot loader region on each label. 537 * The total size of the first two labels plus the boot area is 4MB. 538 */ 539 #define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t)) 540 #define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */ 541 542 /* 543 * Size of label regions at the start and end of each leaf device. 544 */ 545 #define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE) 546 #define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t)) 547 #define VDEV_LABELS 4 548 549 enum zio_checksum { 550 ZIO_CHECKSUM_INHERIT = 0, 551 ZIO_CHECKSUM_ON, 552 ZIO_CHECKSUM_OFF, 553 ZIO_CHECKSUM_LABEL, 554 ZIO_CHECKSUM_GANG_HEADER, 555 ZIO_CHECKSUM_ZILOG, 556 ZIO_CHECKSUM_FLETCHER_2, 557 ZIO_CHECKSUM_FLETCHER_4, 558 ZIO_CHECKSUM_SHA256, 559 ZIO_CHECKSUM_ZILOG2, 560 ZIO_CHECKSUM_NOPARITY, 561 ZIO_CHECKSUM_SHA512, 562 ZIO_CHECKSUM_SKEIN, 563 ZIO_CHECKSUM_EDONR, 564 ZIO_CHECKSUM_FUNCTIONS 565 }; 566 567 #define ZIO_CHECKSUM_ON_VALUE ZIO_CHECKSUM_FLETCHER_4 568 #define ZIO_CHECKSUM_DEFAULT ZIO_CHECKSUM_ON 569 570 enum zio_compress { 571 ZIO_COMPRESS_INHERIT = 0, 572 ZIO_COMPRESS_ON, 573 ZIO_COMPRESS_OFF, 574 ZIO_COMPRESS_LZJB, 575 ZIO_COMPRESS_EMPTY, 576 ZIO_COMPRESS_GZIP_1, 577 ZIO_COMPRESS_GZIP_2, 578 ZIO_COMPRESS_GZIP_3, 579 ZIO_COMPRESS_GZIP_4, 580 ZIO_COMPRESS_GZIP_5, 581 ZIO_COMPRESS_GZIP_6, 582 ZIO_COMPRESS_GZIP_7, 583 ZIO_COMPRESS_GZIP_8, 584 ZIO_COMPRESS_GZIP_9, 585 ZIO_COMPRESS_ZLE, 586 ZIO_COMPRESS_LZ4, 587 ZIO_COMPRESS_FUNCTIONS 588 }; 589 590 #define ZIO_COMPRESS_ON_VALUE ZIO_COMPRESS_LZJB 591 #define ZIO_COMPRESS_DEFAULT ZIO_COMPRESS_OFF 592 593 /* nvlist pack encoding */ 594 #define NV_ENCODE_NATIVE 0 595 #define NV_ENCODE_XDR 1 596 597 typedef enum { 598 DATA_TYPE_UNKNOWN = 0, 599 DATA_TYPE_BOOLEAN, 600 DATA_TYPE_BYTE, 601 DATA_TYPE_INT16, 602 DATA_TYPE_UINT16, 603 DATA_TYPE_INT32, 604 DATA_TYPE_UINT32, 605 DATA_TYPE_INT64, 606 DATA_TYPE_UINT64, 607 DATA_TYPE_STRING, 608 DATA_TYPE_BYTE_ARRAY, 609 DATA_TYPE_INT16_ARRAY, 610 DATA_TYPE_UINT16_ARRAY, 611 DATA_TYPE_INT32_ARRAY, 612 DATA_TYPE_UINT32_ARRAY, 613 DATA_TYPE_INT64_ARRAY, 614 DATA_TYPE_UINT64_ARRAY, 615 DATA_TYPE_STRING_ARRAY, 616 DATA_TYPE_HRTIME, 617 DATA_TYPE_NVLIST, 618 DATA_TYPE_NVLIST_ARRAY, 619 DATA_TYPE_BOOLEAN_VALUE, 620 DATA_TYPE_INT8, 621 DATA_TYPE_UINT8, 622 DATA_TYPE_BOOLEAN_ARRAY, 623 DATA_TYPE_INT8_ARRAY, 624 DATA_TYPE_UINT8_ARRAY 625 } data_type_t; 626 627 /* 628 * On-disk version number. 629 */ 630 #define SPA_VERSION_1 1ULL 631 #define SPA_VERSION_2 2ULL 632 #define SPA_VERSION_3 3ULL 633 #define SPA_VERSION_4 4ULL 634 #define SPA_VERSION_5 5ULL 635 #define SPA_VERSION_6 6ULL 636 #define SPA_VERSION_7 7ULL 637 #define SPA_VERSION_8 8ULL 638 #define SPA_VERSION_9 9ULL 639 #define SPA_VERSION_10 10ULL 640 #define SPA_VERSION_11 11ULL 641 #define SPA_VERSION_12 12ULL 642 #define SPA_VERSION_13 13ULL 643 #define SPA_VERSION_14 14ULL 644 #define SPA_VERSION_15 15ULL 645 #define SPA_VERSION_16 16ULL 646 #define SPA_VERSION_17 17ULL 647 #define SPA_VERSION_18 18ULL 648 #define SPA_VERSION_19 19ULL 649 #define SPA_VERSION_20 20ULL 650 #define SPA_VERSION_21 21ULL 651 #define SPA_VERSION_22 22ULL 652 #define SPA_VERSION_23 23ULL 653 #define SPA_VERSION_24 24ULL 654 #define SPA_VERSION_25 25ULL 655 #define SPA_VERSION_26 26ULL 656 #define SPA_VERSION_27 27ULL 657 #define SPA_VERSION_28 28ULL 658 #define SPA_VERSION_5000 5000ULL 659 660 /* 661 * When bumping up SPA_VERSION, make sure GRUB ZFS understands the on-disk 662 * format change. Go to usr/src/grub/grub-0.97/stage2/{zfs-include/, fsys_zfs*}, 663 * and do the appropriate changes. Also bump the version number in 664 * usr/src/grub/capability. 665 */ 666 #define SPA_VERSION SPA_VERSION_5000 667 #define SPA_VERSION_STRING "5000" 668 669 /* 670 * Symbolic names for the changes that caused a SPA_VERSION switch. 671 * Used in the code when checking for presence or absence of a feature. 672 * Feel free to define multiple symbolic names for each version if there 673 * were multiple changes to on-disk structures during that version. 674 * 675 * NOTE: When checking the current SPA_VERSION in your code, be sure 676 * to use spa_version() since it reports the version of the 677 * last synced uberblock. Checking the in-flight version can 678 * be dangerous in some cases. 679 */ 680 #define SPA_VERSION_INITIAL SPA_VERSION_1 681 #define SPA_VERSION_DITTO_BLOCKS SPA_VERSION_2 682 #define SPA_VERSION_SPARES SPA_VERSION_3 683 #define SPA_VERSION_RAID6 SPA_VERSION_3 684 #define SPA_VERSION_BPLIST_ACCOUNT SPA_VERSION_3 685 #define SPA_VERSION_RAIDZ_DEFLATE SPA_VERSION_3 686 #define SPA_VERSION_DNODE_BYTES SPA_VERSION_3 687 #define SPA_VERSION_ZPOOL_HISTORY SPA_VERSION_4 688 #define SPA_VERSION_GZIP_COMPRESSION SPA_VERSION_5 689 #define SPA_VERSION_BOOTFS SPA_VERSION_6 690 #define SPA_VERSION_SLOGS SPA_VERSION_7 691 #define SPA_VERSION_DELEGATED_PERMS SPA_VERSION_8 692 #define SPA_VERSION_FUID SPA_VERSION_9 693 #define SPA_VERSION_REFRESERVATION SPA_VERSION_9 694 #define SPA_VERSION_REFQUOTA SPA_VERSION_9 695 #define SPA_VERSION_UNIQUE_ACCURATE SPA_VERSION_9 696 #define SPA_VERSION_L2CACHE SPA_VERSION_10 697 #define SPA_VERSION_NEXT_CLONES SPA_VERSION_11 698 #define SPA_VERSION_ORIGIN SPA_VERSION_11 699 #define SPA_VERSION_DSL_SCRUB SPA_VERSION_11 700 #define SPA_VERSION_SNAP_PROPS SPA_VERSION_12 701 #define SPA_VERSION_USED_BREAKDOWN SPA_VERSION_13 702 #define SPA_VERSION_PASSTHROUGH_X SPA_VERSION_14 703 #define SPA_VERSION_USERSPACE SPA_VERSION_15 704 #define SPA_VERSION_STMF_PROP SPA_VERSION_16 705 #define SPA_VERSION_RAIDZ3 SPA_VERSION_17 706 #define SPA_VERSION_USERREFS SPA_VERSION_18 707 #define SPA_VERSION_HOLES SPA_VERSION_19 708 #define SPA_VERSION_ZLE_COMPRESSION SPA_VERSION_20 709 #define SPA_VERSION_DEDUP SPA_VERSION_21 710 #define SPA_VERSION_RECVD_PROPS SPA_VERSION_22 711 #define SPA_VERSION_SLIM_ZIL SPA_VERSION_23 712 #define SPA_VERSION_SA SPA_VERSION_24 713 #define SPA_VERSION_SCAN SPA_VERSION_25 714 #define SPA_VERSION_DIR_CLONES SPA_VERSION_26 715 #define SPA_VERSION_DEADLISTS SPA_VERSION_26 716 #define SPA_VERSION_FAST_SNAP SPA_VERSION_27 717 #define SPA_VERSION_MULTI_REPLACE SPA_VERSION_28 718 #define SPA_VERSION_BEFORE_FEATURES SPA_VERSION_28 719 #define SPA_VERSION_FEATURES SPA_VERSION_5000 720 721 #define SPA_VERSION_IS_SUPPORTED(v) \ 722 (((v) >= SPA_VERSION_INITIAL && (v) <= SPA_VERSION_BEFORE_FEATURES) || \ 723 ((v) >= SPA_VERSION_FEATURES && (v) <= SPA_VERSION)) 724 725 /* 726 * The following are configuration names used in the nvlist describing a pool's 727 * configuration. 728 */ 729 #define ZPOOL_CONFIG_VERSION "version" 730 #define ZPOOL_CONFIG_POOL_NAME "name" 731 #define ZPOOL_CONFIG_POOL_STATE "state" 732 #define ZPOOL_CONFIG_POOL_TXG "txg" 733 #define ZPOOL_CONFIG_POOL_GUID "pool_guid" 734 #define ZPOOL_CONFIG_CREATE_TXG "create_txg" 735 #define ZPOOL_CONFIG_TOP_GUID "top_guid" 736 #define ZPOOL_CONFIG_VDEV_TREE "vdev_tree" 737 #define ZPOOL_CONFIG_TYPE "type" 738 #define ZPOOL_CONFIG_CHILDREN "children" 739 #define ZPOOL_CONFIG_ID "id" 740 #define ZPOOL_CONFIG_GUID "guid" 741 #define ZPOOL_CONFIG_INDIRECT_OBJECT "com.delphix:indirect_object" 742 #define ZPOOL_CONFIG_INDIRECT_BIRTHS "com.delphix:indirect_births" 743 #define ZPOOL_CONFIG_PREV_INDIRECT_VDEV "com.delphix:prev_indirect_vdev" 744 #define ZPOOL_CONFIG_PATH "path" 745 #define ZPOOL_CONFIG_DEVID "devid" 746 #define ZPOOL_CONFIG_METASLAB_ARRAY "metaslab_array" 747 #define ZPOOL_CONFIG_METASLAB_SHIFT "metaslab_shift" 748 #define ZPOOL_CONFIG_ASHIFT "ashift" 749 #define ZPOOL_CONFIG_ASIZE "asize" 750 #define ZPOOL_CONFIG_DTL "DTL" 751 #define ZPOOL_CONFIG_STATS "stats" 752 #define ZPOOL_CONFIG_WHOLE_DISK "whole_disk" 753 #define ZPOOL_CONFIG_ERRCOUNT "error_count" 754 #define ZPOOL_CONFIG_NOT_PRESENT "not_present" 755 #define ZPOOL_CONFIG_SPARES "spares" 756 #define ZPOOL_CONFIG_IS_SPARE "is_spare" 757 #define ZPOOL_CONFIG_NPARITY "nparity" 758 #define ZPOOL_CONFIG_HOSTID "hostid" 759 #define ZPOOL_CONFIG_HOSTNAME "hostname" 760 #define ZPOOL_CONFIG_IS_LOG "is_log" 761 #define ZPOOL_CONFIG_TIMESTAMP "timestamp" /* not stored on disk */ 762 #define ZPOOL_CONFIG_FEATURES_FOR_READ "features_for_read" 763 764 /* 765 * The persistent vdev state is stored as separate values rather than a single 766 * 'vdev_state' entry. This is because a device can be in multiple states, such 767 * as offline and degraded. 768 */ 769 #define ZPOOL_CONFIG_OFFLINE "offline" 770 #define ZPOOL_CONFIG_FAULTED "faulted" 771 #define ZPOOL_CONFIG_DEGRADED "degraded" 772 #define ZPOOL_CONFIG_REMOVED "removed" 773 #define ZPOOL_CONFIG_FRU "fru" 774 #define ZPOOL_CONFIG_AUX_STATE "aux_state" 775 776 #define VDEV_TYPE_ROOT "root" 777 #define VDEV_TYPE_MIRROR "mirror" 778 #define VDEV_TYPE_REPLACING "replacing" 779 #define VDEV_TYPE_RAIDZ "raidz" 780 #define VDEV_TYPE_DISK "disk" 781 #define VDEV_TYPE_FILE "file" 782 #define VDEV_TYPE_MISSING "missing" 783 #define VDEV_TYPE_HOLE "hole" 784 #define VDEV_TYPE_SPARE "spare" 785 #define VDEV_TYPE_LOG "log" 786 #define VDEV_TYPE_L2CACHE "l2cache" 787 #define VDEV_TYPE_INDIRECT "indirect" 788 789 /* 790 * This is needed in userland to report the minimum necessary device size. 791 */ 792 #define SPA_MINDEVSIZE (64ULL << 20) 793 794 /* 795 * The location of the pool configuration repository, shared between kernel and 796 * userland. 797 */ 798 #define ZPOOL_CACHE "/boot/zfs/zpool.cache" 799 800 /* 801 * vdev states are ordered from least to most healthy. 802 * A vdev that's CANT_OPEN or below is considered unusable. 803 */ 804 typedef enum vdev_state { 805 VDEV_STATE_UNKNOWN = 0, /* Uninitialized vdev */ 806 VDEV_STATE_CLOSED, /* Not currently open */ 807 VDEV_STATE_OFFLINE, /* Not allowed to open */ 808 VDEV_STATE_REMOVED, /* Explicitly removed from system */ 809 VDEV_STATE_CANT_OPEN, /* Tried to open, but failed */ 810 VDEV_STATE_FAULTED, /* External request to fault device */ 811 VDEV_STATE_DEGRADED, /* Replicated vdev with unhealthy kids */ 812 VDEV_STATE_HEALTHY /* Presumed good */ 813 } vdev_state_t; 814 815 /* 816 * vdev aux states. When a vdev is in the CANT_OPEN state, the aux field 817 * of the vdev stats structure uses these constants to distinguish why. 818 */ 819 typedef enum vdev_aux { 820 VDEV_AUX_NONE, /* no error */ 821 VDEV_AUX_OPEN_FAILED, /* ldi_open_*() or vn_open() failed */ 822 VDEV_AUX_CORRUPT_DATA, /* bad label or disk contents */ 823 VDEV_AUX_NO_REPLICAS, /* insufficient number of replicas */ 824 VDEV_AUX_BAD_GUID_SUM, /* vdev guid sum doesn't match */ 825 VDEV_AUX_TOO_SMALL, /* vdev size is too small */ 826 VDEV_AUX_BAD_LABEL, /* the label is OK but invalid */ 827 VDEV_AUX_VERSION_NEWER, /* on-disk version is too new */ 828 VDEV_AUX_VERSION_OLDER, /* on-disk version is too old */ 829 VDEV_AUX_SPARED /* hot spare used in another pool */ 830 } vdev_aux_t; 831 832 /* 833 * pool state. The following states are written to disk as part of the normal 834 * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE. The remaining states are 835 * software abstractions used at various levels to communicate pool state. 836 */ 837 typedef enum pool_state { 838 POOL_STATE_ACTIVE = 0, /* In active use */ 839 POOL_STATE_EXPORTED, /* Explicitly exported */ 840 POOL_STATE_DESTROYED, /* Explicitly destroyed */ 841 POOL_STATE_SPARE, /* Reserved for hot spare use */ 842 POOL_STATE_UNINITIALIZED, /* Internal spa_t state */ 843 POOL_STATE_UNAVAIL, /* Internal libzfs state */ 844 POOL_STATE_POTENTIALLY_ACTIVE /* Internal libzfs state */ 845 } pool_state_t; 846 847 /* 848 * The uberblock version is incremented whenever an incompatible on-disk 849 * format change is made to the SPA, DMU, or ZAP. 850 * 851 * Note: the first two fields should never be moved. When a storage pool 852 * is opened, the uberblock must be read off the disk before the version 853 * can be checked. If the ub_version field is moved, we may not detect 854 * version mismatch. If the ub_magic field is moved, applications that 855 * expect the magic number in the first word won't work. 856 */ 857 #define UBERBLOCK_MAGIC 0x00bab10c /* oo-ba-bloc! */ 858 #define UBERBLOCK_SHIFT 10 /* up to 1K */ 859 860 #define MMP_MAGIC 0xa11cea11 /* all-see-all */ 861 862 #define MMP_INTERVAL_VALID_BIT 0x01 863 #define MMP_SEQ_VALID_BIT 0x02 864 #define MMP_FAIL_INT_VALID_BIT 0x04 865 866 #define MMP_VALID(ubp) (ubp->ub_magic == UBERBLOCK_MAGIC && \ 867 ubp->ub_mmp_magic == MMP_MAGIC) 868 #define MMP_INTERVAL_VALID(ubp) (MMP_VALID(ubp) && (ubp->ub_mmp_config & \ 869 MMP_INTERVAL_VALID_BIT)) 870 #define MMP_SEQ_VALID(ubp) (MMP_VALID(ubp) && (ubp->ub_mmp_config & \ 871 MMP_SEQ_VALID_BIT)) 872 #define MMP_FAIL_INT_VALID(ubp) (MMP_VALID(ubp) && (ubp->ub_mmp_config & \ 873 MMP_FAIL_INT_VALID_BIT)) 874 875 #define MMP_INTERVAL(ubp) ((ubp->ub_mmp_config & 0x00000000FFFFFF00) \ 876 >> 8) 877 #define MMP_SEQ(ubp) ((ubp->ub_mmp_config & 0x0000FFFF00000000) \ 878 >> 32) 879 #define MMP_FAIL_INT(ubp) ((ubp->ub_mmp_config & 0xFFFF000000000000) \ 880 >> 48) 881 882 typedef struct uberblock { 883 uint64_t ub_magic; /* UBERBLOCK_MAGIC */ 884 uint64_t ub_version; /* SPA_VERSION */ 885 uint64_t ub_txg; /* txg of last sync */ 886 uint64_t ub_guid_sum; /* sum of all vdev guids */ 887 uint64_t ub_timestamp; /* UTC time of last sync */ 888 blkptr_t ub_rootbp; /* MOS objset_phys_t */ 889 /* highest SPA_VERSION supported by software that wrote this txg */ 890 uint64_t ub_software_version; 891 /* Maybe missing in uberblocks we read, but always written */ 892 uint64_t ub_mmp_magic; 893 /* 894 * If ub_mmp_delay == 0 and ub_mmp_magic is valid, MMP is off. 895 * Otherwise, nanosec since last MMP write. 896 */ 897 uint64_t ub_mmp_delay; 898 899 /* 900 * The ub_mmp_config contains the multihost write interval, multihost 901 * fail intervals, sequence number for sub-second granularity, and 902 * valid bit mask. This layout is as follows: 903 * 904 * 64 56 48 40 32 24 16 8 0 905 * +-------+-------+-------+-------+-------+-------+-------+-------+ 906 * 0 | Fail Intervals| Seq | Write Interval (ms) | VALID | 907 * +-------+-------+-------+-------+-------+-------+-------+-------+ 908 * 909 * This allows a write_interval of (2^24/1000)s, over 4.5 hours 910 * 911 * VALID Bits: 912 * - 0x01 - Write Interval (ms) 913 * - 0x02 - Sequence number exists 914 * - 0x04 - Fail Intervals 915 * - 0xf8 - Reserved 916 */ 917 uint64_t ub_mmp_config; 918 919 /* 920 * ub_checkpoint_txg indicates two things about the current uberblock: 921 * 922 * 1] If it is not zero then this uberblock is a checkpoint. If it is 923 * zero, then this uberblock is not a checkpoint. 924 * 925 * 2] On checkpointed uberblocks, the value of ub_checkpoint_txg is 926 * the ub_txg that the uberblock had at the time we moved it to 927 * the MOS config. 928 * 929 * The field is set when we checkpoint the uberblock and continues to 930 * hold that value even after we've rewound (unlike the ub_txg that 931 * is reset to a higher value). 932 * 933 * Besides checks used to determine whether we are reopening the 934 * pool from a checkpointed uberblock [see spa_ld_select_uberblock()], 935 * the value of the field is used to determine which ZIL blocks have 936 * been allocated according to the ms_sm when we are rewinding to a 937 * checkpoint. Specifically, if blk_birth > ub_checkpoint_txg, then 938 * the ZIL block is not allocated [see uses of spa_min_claim_txg()]. 939 */ 940 uint64_t ub_checkpoint_txg; 941 } uberblock_t; 942 943 /* 944 * Flags. 945 */ 946 #define DNODE_MUST_BE_ALLOCATED 1 947 #define DNODE_MUST_BE_FREE 2 948 949 /* 950 * Fixed constants. 951 */ 952 #define DNODE_SHIFT 9 /* 512 bytes */ 953 #define DN_MIN_INDBLKSHIFT 12 /* 4k */ 954 #define DN_MAX_INDBLKSHIFT 17 /* 128k */ 955 #define DNODE_BLOCK_SHIFT 14 /* 16k */ 956 #define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ 957 #define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ 958 #define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ 959 960 /* 961 * Derived constants. 962 */ 963 #define DNODE_MIN_SIZE (1 << DNODE_SHIFT) 964 #define DNODE_MAX_SIZE (1 << DNODE_BLOCK_SHIFT) 965 #define DNODE_BLOCK_SIZE (1 << DNODE_BLOCK_SHIFT) 966 #define DNODE_MIN_SLOTS (DNODE_MIN_SIZE >> DNODE_SHIFT) 967 #define DNODE_MAX_SLOTS (DNODE_MAX_SIZE >> DNODE_SHIFT) 968 #define DN_BONUS_SIZE(dnsize) ((dnsize) - DNODE_CORE_SIZE - \ 969 (1 << SPA_BLKPTRSHIFT)) 970 #define DN_SLOTS_TO_BONUSLEN(slots) DN_BONUS_SIZE((slots) << DNODE_SHIFT) 971 #define DN_OLD_MAX_BONUSLEN (DN_BONUS_SIZE(DNODE_MIN_SIZE)) 972 #define DN_MAX_NBLKPTR ((DNODE_MIN_SIZE - DNODE_CORE_SIZE) >> \ 973 SPA_BLKPTRSHIFT) 974 #define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT) 975 #define DN_ZERO_BONUSLEN (DN_BONUS_SIZE(DNODE_MAX_SIZE) + 1) 976 977 #define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) 978 #define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) 979 #define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) 980 981 /* The +2 here is a cheesy way to round up */ 982 #define DN_MAX_LEVELS (2 + ((DN_MAX_OFFSET_SHIFT - SPA_MINBLOCKSHIFT) / \ 983 (DN_MIN_INDBLKSHIFT - SPA_BLKPTRSHIFT))) 984 985 #define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ 986 (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) 987 988 #define DN_USED_BYTES(dnp) (((dnp)->dn_flags & DNODE_FLAG_USED_BYTES) ? \ 989 (dnp)->dn_used : (dnp)->dn_used << SPA_MINBLOCKSHIFT) 990 991 #define EPB(blkshift, typeshift) (1 << (blkshift - typeshift)) 992 993 /* Is dn_used in bytes? if not, it's in multiples of SPA_MINBLOCKSIZE */ 994 #define DNODE_FLAG_USED_BYTES (1<<0) 995 #define DNODE_FLAG_USERUSED_ACCOUNTED (1<<1) 996 997 /* Does dnode have a SA spill blkptr in bonus? */ 998 #define DNODE_FLAG_SPILL_BLKPTR (1<<2) 999 1000 typedef struct dnode_phys { 1001 uint8_t dn_type; /* dmu_object_type_t */ 1002 uint8_t dn_indblkshift; /* ln2(indirect block size) */ 1003 uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ 1004 uint8_t dn_nblkptr; /* length of dn_blkptr */ 1005 uint8_t dn_bonustype; /* type of data in bonus buffer */ 1006 uint8_t dn_checksum; /* ZIO_CHECKSUM type */ 1007 uint8_t dn_compress; /* ZIO_COMPRESS type */ 1008 uint8_t dn_flags; /* DNODE_FLAG_* */ 1009 uint16_t dn_datablkszsec; /* data block size in 512b sectors */ 1010 uint16_t dn_bonuslen; /* length of dn_bonus */ 1011 uint8_t dn_extra_slots; /* # of subsequent slots consumed */ 1012 uint8_t dn_pad2[3]; 1013 1014 /* accounting is protected by dn_dirty_mtx */ 1015 uint64_t dn_maxblkid; /* largest allocated block ID */ 1016 uint64_t dn_used; /* bytes (or sectors) of disk space */ 1017 1018 uint64_t dn_pad3[4]; 1019 1020 /* 1021 * The tail region is 448 bytes for a 512 byte dnode, and 1022 * correspondingly larger for larger dnode sizes. The spill 1023 * block pointer, when present, is always at the end of the tail 1024 * region. There are three ways this space may be used, using 1025 * a 512 byte dnode for this diagram: 1026 * 1027 * 0 64 128 192 256 320 384 448 (offset) 1028 * +---------------+---------------+---------------+-------+ 1029 * | dn_blkptr[0] | dn_blkptr[1] | dn_blkptr[2] | / | 1030 * +---------------+---------------+---------------+-------+ 1031 * | dn_blkptr[0] | dn_bonus[0..319] | 1032 * +---------------+-----------------------+---------------+ 1033 * | dn_blkptr[0] | dn_bonus[0..191] | dn_spill | 1034 * +---------------+-----------------------+---------------+ 1035 */ 1036 union { 1037 blkptr_t dn_blkptr[1+DN_OLD_MAX_BONUSLEN/sizeof (blkptr_t)]; 1038 struct { 1039 blkptr_t __dn_ignore1; 1040 uint8_t dn_bonus[DN_OLD_MAX_BONUSLEN]; 1041 }; 1042 struct { 1043 blkptr_t __dn_ignore2; 1044 uint8_t __dn_ignore3[DN_OLD_MAX_BONUSLEN - 1045 sizeof (blkptr_t)]; 1046 blkptr_t dn_spill; 1047 }; 1048 }; 1049 } dnode_phys_t; 1050 1051 #define DN_SPILL_BLKPTR(dnp) (blkptr_t *)((char *)(dnp) + \ 1052 (((dnp)->dn_extra_slots + 1) << DNODE_SHIFT) - (1 << SPA_BLKPTRSHIFT)) 1053 1054 typedef enum dmu_object_byteswap { 1055 DMU_BSWAP_UINT8, 1056 DMU_BSWAP_UINT16, 1057 DMU_BSWAP_UINT32, 1058 DMU_BSWAP_UINT64, 1059 DMU_BSWAP_ZAP, 1060 DMU_BSWAP_DNODE, 1061 DMU_BSWAP_OBJSET, 1062 DMU_BSWAP_ZNODE, 1063 DMU_BSWAP_OLDACL, 1064 DMU_BSWAP_ACL, 1065 /* 1066 * Allocating a new byteswap type number makes the on-disk format 1067 * incompatible with any other format that uses the same number. 1068 * 1069 * Data can usually be structured to work with one of the 1070 * DMU_BSWAP_UINT* or DMU_BSWAP_ZAP types. 1071 */ 1072 DMU_BSWAP_NUMFUNCS 1073 } dmu_object_byteswap_t; 1074 1075 #define DMU_OT_NEWTYPE 0x80 1076 #define DMU_OT_METADATA 0x40 1077 #define DMU_OT_BYTESWAP_MASK 0x3f 1078 1079 /* 1080 * Defines a uint8_t object type. Object types specify if the data 1081 * in the object is metadata (boolean) and how to byteswap the data 1082 * (dmu_object_byteswap_t). 1083 */ 1084 #define DMU_OT(byteswap, metadata) \ 1085 (DMU_OT_NEWTYPE | \ 1086 ((metadata) ? DMU_OT_METADATA : 0) | \ 1087 ((byteswap) & DMU_OT_BYTESWAP_MASK)) 1088 1089 typedef enum dmu_object_type { 1090 DMU_OT_NONE, 1091 /* general: */ 1092 DMU_OT_OBJECT_DIRECTORY, /* ZAP */ 1093 DMU_OT_OBJECT_ARRAY, /* UINT64 */ 1094 DMU_OT_PACKED_NVLIST, /* UINT8 (XDR by nvlist_pack/unpack) */ 1095 DMU_OT_PACKED_NVLIST_SIZE, /* UINT64 */ 1096 DMU_OT_BPLIST, /* UINT64 */ 1097 DMU_OT_BPLIST_HDR, /* UINT64 */ 1098 /* spa: */ 1099 DMU_OT_SPACE_MAP_HEADER, /* UINT64 */ 1100 DMU_OT_SPACE_MAP, /* UINT64 */ 1101 /* zil: */ 1102 DMU_OT_INTENT_LOG, /* UINT64 */ 1103 /* dmu: */ 1104 DMU_OT_DNODE, /* DNODE */ 1105 DMU_OT_OBJSET, /* OBJSET */ 1106 /* dsl: */ 1107 DMU_OT_DSL_DIR, /* UINT64 */ 1108 DMU_OT_DSL_DIR_CHILD_MAP, /* ZAP */ 1109 DMU_OT_DSL_DS_SNAP_MAP, /* ZAP */ 1110 DMU_OT_DSL_PROPS, /* ZAP */ 1111 DMU_OT_DSL_DATASET, /* UINT64 */ 1112 /* zpl: */ 1113 DMU_OT_ZNODE, /* ZNODE */ 1114 DMU_OT_OLDACL, /* Old ACL */ 1115 DMU_OT_PLAIN_FILE_CONTENTS, /* UINT8 */ 1116 DMU_OT_DIRECTORY_CONTENTS, /* ZAP */ 1117 DMU_OT_MASTER_NODE, /* ZAP */ 1118 DMU_OT_UNLINKED_SET, /* ZAP */ 1119 /* zvol: */ 1120 DMU_OT_ZVOL, /* UINT8 */ 1121 DMU_OT_ZVOL_PROP, /* ZAP */ 1122 /* other; for testing only! */ 1123 DMU_OT_PLAIN_OTHER, /* UINT8 */ 1124 DMU_OT_UINT64_OTHER, /* UINT64 */ 1125 DMU_OT_ZAP_OTHER, /* ZAP */ 1126 /* new object types: */ 1127 DMU_OT_ERROR_LOG, /* ZAP */ 1128 DMU_OT_SPA_HISTORY, /* UINT8 */ 1129 DMU_OT_SPA_HISTORY_OFFSETS, /* spa_his_phys_t */ 1130 DMU_OT_POOL_PROPS, /* ZAP */ 1131 DMU_OT_DSL_PERMS, /* ZAP */ 1132 DMU_OT_ACL, /* ACL */ 1133 DMU_OT_SYSACL, /* SYSACL */ 1134 DMU_OT_FUID, /* FUID table (Packed NVLIST UINT8) */ 1135 DMU_OT_FUID_SIZE, /* FUID table size UINT64 */ 1136 DMU_OT_NEXT_CLONES, /* ZAP */ 1137 DMU_OT_SCAN_QUEUE, /* ZAP */ 1138 DMU_OT_USERGROUP_USED, /* ZAP */ 1139 DMU_OT_USERGROUP_QUOTA, /* ZAP */ 1140 DMU_OT_USERREFS, /* ZAP */ 1141 DMU_OT_DDT_ZAP, /* ZAP */ 1142 DMU_OT_DDT_STATS, /* ZAP */ 1143 DMU_OT_SA, /* System attr */ 1144 DMU_OT_SA_MASTER_NODE, /* ZAP */ 1145 DMU_OT_SA_ATTR_REGISTRATION, /* ZAP */ 1146 DMU_OT_SA_ATTR_LAYOUTS, /* ZAP */ 1147 DMU_OT_SCAN_XLATE, /* ZAP */ 1148 DMU_OT_DEDUP, /* fake dedup BP from ddt_bp_create() */ 1149 DMU_OT_NUMTYPES, 1150 1151 /* 1152 * Names for valid types declared with DMU_OT(). 1153 */ 1154 DMU_OTN_UINT8_DATA = DMU_OT(DMU_BSWAP_UINT8, B_FALSE), 1155 DMU_OTN_UINT8_METADATA = DMU_OT(DMU_BSWAP_UINT8, B_TRUE), 1156 DMU_OTN_UINT16_DATA = DMU_OT(DMU_BSWAP_UINT16, B_FALSE), 1157 DMU_OTN_UINT16_METADATA = DMU_OT(DMU_BSWAP_UINT16, B_TRUE), 1158 DMU_OTN_UINT32_DATA = DMU_OT(DMU_BSWAP_UINT32, B_FALSE), 1159 DMU_OTN_UINT32_METADATA = DMU_OT(DMU_BSWAP_UINT32, B_TRUE), 1160 DMU_OTN_UINT64_DATA = DMU_OT(DMU_BSWAP_UINT64, B_FALSE), 1161 DMU_OTN_UINT64_METADATA = DMU_OT(DMU_BSWAP_UINT64, B_TRUE), 1162 DMU_OTN_ZAP_DATA = DMU_OT(DMU_BSWAP_ZAP, B_FALSE), 1163 DMU_OTN_ZAP_METADATA = DMU_OT(DMU_BSWAP_ZAP, B_TRUE) 1164 } dmu_object_type_t; 1165 1166 typedef enum dmu_objset_type { 1167 DMU_OST_NONE, 1168 DMU_OST_META, 1169 DMU_OST_ZFS, 1170 DMU_OST_ZVOL, 1171 DMU_OST_OTHER, /* For testing only! */ 1172 DMU_OST_ANY, /* Be careful! */ 1173 DMU_OST_NUMTYPES 1174 } dmu_objset_type_t; 1175 1176 /* 1177 * header for all bonus and spill buffers. 1178 * The header has a fixed portion with a variable number 1179 * of "lengths" depending on the number of variable sized 1180 * attribues which are determined by the "layout number" 1181 */ 1182 1183 #define SA_MAGIC 0x2F505A /* ZFS SA */ 1184 typedef struct sa_hdr_phys { 1185 uint32_t sa_magic; 1186 uint16_t sa_layout_info; /* Encoded with hdrsize and layout number */ 1187 uint16_t sa_lengths[1]; /* optional sizes for variable length attrs */ 1188 /* ... Data follows the lengths. */ 1189 } sa_hdr_phys_t; 1190 1191 /* 1192 * sa_hdr_phys -> sa_layout_info 1193 * 1194 * 16 10 0 1195 * +--------+-------+ 1196 * | hdrsz |layout | 1197 * +--------+-------+ 1198 * 1199 * Bits 0-10 are the layout number 1200 * Bits 11-16 are the size of the header. 1201 * The hdrsize is the number * 8 1202 * 1203 * For example. 1204 * hdrsz of 1 ==> 8 byte header 1205 * 2 ==> 16 byte header 1206 * 1207 */ 1208 1209 #define SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10) 1210 #define SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 16, 3, 0) 1211 #define SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \ 1212 { \ 1213 BF32_SET_SB(x, 10, 6, 3, 0, size); \ 1214 BF32_SET(x, 0, 10, num); \ 1215 } 1216 1217 #define SA_MODE_OFFSET 0 1218 #define SA_SIZE_OFFSET 8 1219 #define SA_GEN_OFFSET 16 1220 #define SA_UID_OFFSET 24 1221 #define SA_GID_OFFSET 32 1222 #define SA_PARENT_OFFSET 40 1223 #define SA_SYMLINK_OFFSET 160 1224 1225 #define ZIO_OBJSET_MAC_LEN 32 1226 1227 /* 1228 * Intent log header - this on disk structure holds fields to manage 1229 * the log. All fields are 64 bit to easily handle cross architectures. 1230 */ 1231 typedef struct zil_header { 1232 uint64_t zh_claim_txg; /* txg in which log blocks were claimed */ 1233 uint64_t zh_replay_seq; /* highest replayed sequence number */ 1234 blkptr_t zh_log; /* log chain */ 1235 uint64_t zh_claim_seq; /* highest claimed sequence number */ 1236 uint64_t zh_pad[5]; 1237 } zil_header_t; 1238 1239 #define OBJSET_PHYS_SIZE_V2 2048 1240 #define OBJSET_PHYS_SIZE_V3 4096 1241 1242 typedef struct objset_phys { 1243 dnode_phys_t os_meta_dnode; 1244 zil_header_t os_zil_header; 1245 uint64_t os_type; 1246 uint64_t os_flags; 1247 uint8_t os_portable_mac[ZIO_OBJSET_MAC_LEN]; 1248 uint8_t os_local_mac[ZIO_OBJSET_MAC_LEN]; 1249 char os_pad0[OBJSET_PHYS_SIZE_V2 - sizeof (dnode_phys_t)*3 - 1250 sizeof (zil_header_t) - sizeof (uint64_t)*2 - 1251 2*ZIO_OBJSET_MAC_LEN]; 1252 dnode_phys_t os_userused_dnode; 1253 dnode_phys_t os_groupused_dnode; 1254 dnode_phys_t os_projectused_dnode; 1255 char os_pad1[OBJSET_PHYS_SIZE_V3 - OBJSET_PHYS_SIZE_V2 - 1256 sizeof (dnode_phys_t)]; 1257 } objset_phys_t; 1258 1259 typedef struct dsl_dir_phys { 1260 uint64_t dd_creation_time; /* not actually used */ 1261 uint64_t dd_head_dataset_obj; 1262 uint64_t dd_parent_obj; 1263 uint64_t dd_clone_parent_obj; 1264 uint64_t dd_child_dir_zapobj; 1265 /* 1266 * how much space our children are accounting for; for leaf 1267 * datasets, == physical space used by fs + snaps 1268 */ 1269 uint64_t dd_used_bytes; 1270 uint64_t dd_compressed_bytes; 1271 uint64_t dd_uncompressed_bytes; 1272 /* Administrative quota setting */ 1273 uint64_t dd_quota; 1274 /* Administrative reservation setting */ 1275 uint64_t dd_reserved; 1276 uint64_t dd_props_zapobj; 1277 uint64_t dd_pad[21]; /* pad out to 256 bytes for good measure */ 1278 } dsl_dir_phys_t; 1279 1280 typedef struct dsl_dataset_phys { 1281 uint64_t ds_dir_obj; 1282 uint64_t ds_prev_snap_obj; 1283 uint64_t ds_prev_snap_txg; 1284 uint64_t ds_next_snap_obj; 1285 uint64_t ds_snapnames_zapobj; /* zap obj of snaps; ==0 for snaps */ 1286 uint64_t ds_num_children; /* clone/snap children; ==0 for head */ 1287 uint64_t ds_creation_time; /* seconds since 1970 */ 1288 uint64_t ds_creation_txg; 1289 uint64_t ds_deadlist_obj; 1290 uint64_t ds_used_bytes; 1291 uint64_t ds_compressed_bytes; 1292 uint64_t ds_uncompressed_bytes; 1293 uint64_t ds_unique_bytes; /* only relevant to snapshots */ 1294 /* 1295 * The ds_fsid_guid is a 56-bit ID that can change to avoid 1296 * collisions. The ds_guid is a 64-bit ID that will never 1297 * change, so there is a small probability that it will collide. 1298 */ 1299 uint64_t ds_fsid_guid; 1300 uint64_t ds_guid; 1301 uint64_t ds_flags; 1302 blkptr_t ds_bp; 1303 uint64_t ds_pad[8]; /* pad out to 320 bytes for good measure */ 1304 } dsl_dataset_phys_t; 1305 1306 /* 1307 * The names of zap entries in the DIRECTORY_OBJECT of the MOS. 1308 */ 1309 #define DMU_POOL_DIRECTORY_OBJECT 1 1310 #define DMU_POOL_CONFIG "config" 1311 #define DMU_POOL_FEATURES_FOR_READ "features_for_read" 1312 #define DMU_POOL_ROOT_DATASET "root_dataset" 1313 #define DMU_POOL_SYNC_BPLIST "sync_bplist" 1314 #define DMU_POOL_ERRLOG_SCRUB "errlog_scrub" 1315 #define DMU_POOL_ERRLOG_LAST "errlog_last" 1316 #define DMU_POOL_SPARES "spares" 1317 #define DMU_POOL_DEFLATE "deflate" 1318 #define DMU_POOL_HISTORY "history" 1319 #define DMU_POOL_PROPS "pool_props" 1320 #define DMU_POOL_CHECKSUM_SALT "org.illumos:checksum_salt" 1321 #define DMU_POOL_REMOVING "com.delphix:removing" 1322 #define DMU_POOL_OBSOLETE_BPOBJ "com.delphix:obsolete_bpobj" 1323 #define DMU_POOL_CONDENSING_INDIRECT "com.delphix:condensing_indirect" 1324 1325 #define ZAP_MAGIC 0x2F52AB2ABULL 1326 1327 #define FZAP_BLOCK_SHIFT(zap) ((zap)->zap_block_shift) 1328 1329 #define ZAP_MAXCD (uint32_t)(-1) 1330 #define ZAP_HASHBITS 28 1331 #define MZAP_ENT_LEN 64 1332 #define MZAP_NAME_LEN (MZAP_ENT_LEN - 8 - 4 - 2) 1333 #define MZAP_MAX_BLKSHIFT SPA_MAXBLOCKSHIFT 1334 #define MZAP_MAX_BLKSZ (1 << MZAP_MAX_BLKSHIFT) 1335 1336 typedef struct mzap_ent_phys { 1337 uint64_t mze_value; 1338 uint32_t mze_cd; 1339 uint16_t mze_pad; /* in case we want to chain them someday */ 1340 char mze_name[MZAP_NAME_LEN]; 1341 } mzap_ent_phys_t; 1342 1343 typedef struct mzap_phys { 1344 uint64_t mz_block_type; /* ZBT_MICRO */ 1345 uint64_t mz_salt; 1346 uint64_t mz_pad[6]; 1347 mzap_ent_phys_t mz_chunk[1]; 1348 /* actually variable size depending on block size */ 1349 } mzap_phys_t; 1350 1351 /* 1352 * The (fat) zap is stored in one object. It is an array of 1353 * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of: 1354 * 1355 * ptrtbl fits in first block: 1356 * [zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ... 1357 * 1358 * ptrtbl too big for first block: 1359 * [zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ... 1360 * 1361 */ 1362 1363 #define ZBT_LEAF ((1ULL << 63) + 0) 1364 #define ZBT_HEADER ((1ULL << 63) + 1) 1365 #define ZBT_MICRO ((1ULL << 63) + 3) 1366 /* any other values are ptrtbl blocks */ 1367 1368 /* 1369 * the embedded pointer table takes up half a block: 1370 * block size / entry size (2^3) / 2 1371 */ 1372 #define ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1) 1373 1374 /* 1375 * The embedded pointer table starts half-way through the block. Since 1376 * the pointer table itself is half the block, it starts at (64-bit) 1377 * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)). 1378 */ 1379 #define ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \ 1380 ((uint64_t *)(zap)->zap_phys) \ 1381 [(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))] 1382 1383 /* 1384 * TAKE NOTE: 1385 * If zap_phys_t is modified, zap_byteswap() must be modified. 1386 */ 1387 typedef struct zap_phys { 1388 uint64_t zap_block_type; /* ZBT_HEADER */ 1389 uint64_t zap_magic; /* ZAP_MAGIC */ 1390 1391 struct zap_table_phys { 1392 uint64_t zt_blk; /* starting block number */ 1393 uint64_t zt_numblks; /* number of blocks */ 1394 uint64_t zt_shift; /* bits to index it */ 1395 uint64_t zt_nextblk; /* next (larger) copy start block */ 1396 uint64_t zt_blks_copied; /* number source blocks copied */ 1397 } zap_ptrtbl; 1398 1399 uint64_t zap_freeblk; /* the next free block */ 1400 uint64_t zap_num_leafs; /* number of leafs */ 1401 uint64_t zap_num_entries; /* number of entries */ 1402 uint64_t zap_salt; /* salt to stir into hash function */ 1403 /* 1404 * This structure is followed by padding, and then the embedded 1405 * pointer table. The embedded pointer table takes up second 1406 * half of the block. It is accessed using the 1407 * ZAP_EMBEDDED_PTRTBL_ENT() macro. 1408 */ 1409 } zap_phys_t; 1410 1411 typedef struct zap_table_phys zap_table_phys_t; 1412 1413 typedef struct fat_zap { 1414 int zap_block_shift; /* block size shift */ 1415 zap_phys_t *zap_phys; 1416 } fat_zap_t; 1417 1418 #define ZAP_LEAF_MAGIC 0x2AB1EAF 1419 1420 /* chunk size = 24 bytes */ 1421 #define ZAP_LEAF_CHUNKSIZE 24 1422 1423 /* 1424 * The amount of space available for chunks is: 1425 * block size (1<<l->l_bs) - hash entry size (2) * number of hash 1426 * entries - header space (2*chunksize) 1427 */ 1428 #define ZAP_LEAF_NUMCHUNKS(l) \ 1429 (((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \ 1430 ZAP_LEAF_CHUNKSIZE - 2) 1431 1432 /* 1433 * The amount of space within the chunk available for the array is: 1434 * chunk size - space for type (1) - space for next pointer (2) 1435 */ 1436 #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3) 1437 1438 #define ZAP_LEAF_ARRAY_NCHUNKS(bytes) \ 1439 (((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES) 1440 1441 /* 1442 * Low water mark: when there are only this many chunks free, start 1443 * growing the ptrtbl. Ideally, this should be larger than a 1444 * "reasonably-sized" entry. 20 chunks is more than enough for the 1445 * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value), 1446 * while still being only around 3% for 16k blocks. 1447 */ 1448 #define ZAP_LEAF_LOW_WATER (20) 1449 1450 /* 1451 * The leaf hash table has block size / 2^5 (32) number of entries, 1452 * which should be more than enough for the maximum number of entries, 1453 * which is less than block size / CHUNKSIZE (24) / minimum number of 1454 * chunks per entry (3). 1455 */ 1456 #define ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5) 1457 #define ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l)) 1458 1459 /* 1460 * The chunks start immediately after the hash table. The end of the 1461 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a 1462 * chunk_t. 1463 */ 1464 #define ZAP_LEAF_CHUNK(l, idx) \ 1465 ((zap_leaf_chunk_t *) \ 1466 ((l)->l_phys->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx] 1467 #define ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry) 1468 1469 typedef enum zap_chunk_type { 1470 ZAP_CHUNK_FREE = 253, 1471 ZAP_CHUNK_ENTRY = 252, 1472 ZAP_CHUNK_ARRAY = 251, 1473 ZAP_CHUNK_TYPE_MAX = 250 1474 } zap_chunk_type_t; 1475 1476 /* 1477 * TAKE NOTE: 1478 * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified. 1479 */ 1480 typedef struct zap_leaf_phys { 1481 struct zap_leaf_header { 1482 uint64_t lh_block_type; /* ZBT_LEAF */ 1483 uint64_t lh_pad1; 1484 uint64_t lh_prefix; /* hash prefix of this leaf */ 1485 uint32_t lh_magic; /* ZAP_LEAF_MAGIC */ 1486 uint16_t lh_nfree; /* number free chunks */ 1487 uint16_t lh_nentries; /* number of entries */ 1488 uint16_t lh_prefix_len; /* num bits used to id this */ 1489 1490 /* above is accessable to zap, below is zap_leaf private */ 1491 1492 uint16_t lh_freelist; /* chunk head of free list */ 1493 uint8_t lh_pad2[12]; 1494 } l_hdr; /* 2 24-byte chunks */ 1495 1496 /* 1497 * The header is followed by a hash table with 1498 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries. The hash table is 1499 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap) 1500 * zap_leaf_chunk structures. These structures are accessed 1501 * with the ZAP_LEAF_CHUNK() macro. 1502 */ 1503 1504 uint16_t l_hash[1]; 1505 } zap_leaf_phys_t; 1506 1507 typedef union zap_leaf_chunk { 1508 struct zap_leaf_entry { 1509 uint8_t le_type; /* always ZAP_CHUNK_ENTRY */ 1510 uint8_t le_value_intlen; /* size of ints */ 1511 uint16_t le_next; /* next entry in hash chain */ 1512 uint16_t le_name_chunk; /* first chunk of the name */ 1513 uint16_t le_name_numints; /* bytes in name, incl null */ 1514 uint16_t le_value_chunk; /* first chunk of the value */ 1515 uint16_t le_value_numints; /* value length in ints */ 1516 uint32_t le_cd; /* collision differentiator */ 1517 uint64_t le_hash; /* hash value of the name */ 1518 } l_entry; 1519 struct zap_leaf_array { 1520 uint8_t la_type; /* always ZAP_CHUNK_ARRAY */ 1521 uint8_t la_array[ZAP_LEAF_ARRAY_BYTES]; 1522 uint16_t la_next; /* next blk or CHAIN_END */ 1523 } l_array; 1524 struct zap_leaf_free { 1525 uint8_t lf_type; /* always ZAP_CHUNK_FREE */ 1526 uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES]; 1527 uint16_t lf_next; /* next in free list, or CHAIN_END */ 1528 } l_free; 1529 } zap_leaf_chunk_t; 1530 1531 typedef struct zap_leaf { 1532 int l_bs; /* block size shift */ 1533 zap_leaf_phys_t *l_phys; 1534 } zap_leaf_t; 1535 1536 /* 1537 * Define special zfs pflags 1538 */ 1539 #define ZFS_XATTR 0x1 /* is an extended attribute */ 1540 #define ZFS_INHERIT_ACE 0x2 /* ace has inheritable ACEs */ 1541 #define ZFS_ACL_TRIVIAL 0x4 /* files ACL is trivial */ 1542 1543 #define MASTER_NODE_OBJ 1 1544 1545 /* 1546 * special attributes for master node. 1547 */ 1548 1549 #define ZFS_FSID "FSID" 1550 #define ZFS_UNLINKED_SET "DELETE_QUEUE" 1551 #define ZFS_ROOT_OBJ "ROOT" 1552 #define ZPL_VERSION_OBJ "VERSION" 1553 #define ZFS_PROP_BLOCKPERPAGE "BLOCKPERPAGE" 1554 #define ZFS_PROP_NOGROWBLOCKS "NOGROWBLOCKS" 1555 1556 #define ZFS_FLAG_BLOCKPERPAGE 0x1 1557 #define ZFS_FLAG_NOGROWBLOCKS 0x2 1558 1559 /* 1560 * ZPL version - rev'd whenever an incompatible on-disk format change 1561 * occurs. Independent of SPA/DMU/ZAP versioning. 1562 */ 1563 1564 #define ZPL_VERSION 1ULL 1565 1566 /* 1567 * The directory entry has the type (currently unused on Solaris) in the 1568 * top 4 bits, and the object number in the low 48 bits. The "middle" 1569 * 12 bits are unused. 1570 */ 1571 #define ZFS_DIRENT_TYPE(de) BF64_GET(de, 60, 4) 1572 #define ZFS_DIRENT_OBJ(de) BF64_GET(de, 0, 48) 1573 #define ZFS_DIRENT_MAKE(type, obj) (((uint64_t)type << 60) | obj) 1574 1575 typedef struct ace { 1576 uid_t a_who; /* uid or gid */ 1577 uint32_t a_access_mask; /* read,write,... */ 1578 uint16_t a_flags; /* see below */ 1579 uint16_t a_type; /* allow or deny */ 1580 } ace_t; 1581 1582 #define ACE_SLOT_CNT 6 1583 1584 typedef struct zfs_znode_acl { 1585 uint64_t z_acl_extern_obj; /* ext acl pieces */ 1586 uint32_t z_acl_count; /* Number of ACEs */ 1587 uint16_t z_acl_version; /* acl version */ 1588 uint16_t z_acl_pad; /* pad */ 1589 ace_t z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */ 1590 } zfs_znode_acl_t; 1591 1592 /* 1593 * This is the persistent portion of the znode. It is stored 1594 * in the "bonus buffer" of the file. Short symbolic links 1595 * are also stored in the bonus buffer. 1596 */ 1597 typedef struct znode_phys { 1598 uint64_t zp_atime[2]; /* 0 - last file access time */ 1599 uint64_t zp_mtime[2]; /* 16 - last file modification time */ 1600 uint64_t zp_ctime[2]; /* 32 - last file change time */ 1601 uint64_t zp_crtime[2]; /* 48 - creation time */ 1602 uint64_t zp_gen; /* 64 - generation (txg of creation) */ 1603 uint64_t zp_mode; /* 72 - file mode bits */ 1604 uint64_t zp_size; /* 80 - size of file */ 1605 uint64_t zp_parent; /* 88 - directory parent (`..') */ 1606 uint64_t zp_links; /* 96 - number of links to file */ 1607 uint64_t zp_xattr; /* 104 - DMU object for xattrs */ 1608 uint64_t zp_rdev; /* 112 - dev_t for VBLK & VCHR files */ 1609 uint64_t zp_flags; /* 120 - persistent flags */ 1610 uint64_t zp_uid; /* 128 - file owner */ 1611 uint64_t zp_gid; /* 136 - owning group */ 1612 uint64_t zp_pad[4]; /* 144 - future */ 1613 zfs_znode_acl_t zp_acl; /* 176 - 263 ACL */ 1614 /* 1615 * Data may pad out any remaining bytes in the znode buffer, eg: 1616 * 1617 * |<---------------------- dnode_phys (512) ------------------------>| 1618 * |<-- dnode (192) --->|<----------- "bonus" buffer (320) ---------->| 1619 * |<---- znode (264) ---->|<---- data (56) ---->| 1620 * 1621 * At present, we only use this space to store symbolic links. 1622 */ 1623 } znode_phys_t; 1624 1625 /* 1626 * In-core vdev representation. 1627 */ 1628 struct vdev; 1629 struct spa; 1630 typedef int vdev_phys_read_t(struct vdev *vdev, void *priv, 1631 off_t offset, void *buf, size_t bytes); 1632 typedef int vdev_read_t(struct vdev *vdev, const blkptr_t *bp, 1633 void *buf, off_t offset, size_t bytes); 1634 1635 typedef STAILQ_HEAD(vdev_list, vdev) vdev_list_t; 1636 1637 typedef struct vdev_indirect_mapping_entry_phys { 1638 /* 1639 * Decode with DVA_MAPPING_* macros. 1640 * Contains: 1641 * the source offset (low 63 bits) 1642 * the one-bit "mark", used for garbage collection (by zdb) 1643 */ 1644 uint64_t vimep_src; 1645 1646 /* 1647 * Note: the DVA's asize is 24 bits, and can thus store ranges 1648 * up to 8GB. 1649 */ 1650 dva_t vimep_dst; 1651 } vdev_indirect_mapping_entry_phys_t; 1652 1653 #define DVA_MAPPING_GET_SRC_OFFSET(vimep) \ 1654 BF64_GET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0) 1655 #define DVA_MAPPING_SET_SRC_OFFSET(vimep, x) \ 1656 BF64_SET_SB((vimep)->vimep_src, 0, 63, SPA_MINBLOCKSHIFT, 0, x) 1657 1658 typedef struct vdev_indirect_mapping_entry { 1659 vdev_indirect_mapping_entry_phys_t vime_mapping; 1660 uint32_t vime_obsolete_count; 1661 list_node_t vime_node; 1662 } vdev_indirect_mapping_entry_t; 1663 1664 /* 1665 * This is stored in the bonus buffer of the mapping object, see comment of 1666 * vdev_indirect_config for more details. 1667 */ 1668 typedef struct vdev_indirect_mapping_phys { 1669 uint64_t vimp_max_offset; 1670 uint64_t vimp_bytes_mapped; 1671 uint64_t vimp_num_entries; /* number of v_i_m_entry_phys_t's */ 1672 1673 /* 1674 * For each entry in the mapping object, this object contains an 1675 * entry representing the number of bytes of that mapping entry 1676 * that were no longer in use by the pool at the time this indirect 1677 * vdev was last condensed. 1678 */ 1679 uint64_t vimp_counts_object; 1680 } vdev_indirect_mapping_phys_t; 1681 1682 #define VDEV_INDIRECT_MAPPING_SIZE_V0 (3 * sizeof (uint64_t)) 1683 1684 typedef struct vdev_indirect_mapping { 1685 uint64_t vim_object; 1686 boolean_t vim_havecounts; 1687 1688 /* vim_entries segment offset currently in memory. */ 1689 uint64_t vim_entry_offset; 1690 /* vim_entries segment size. */ 1691 size_t vim_num_entries; 1692 1693 /* Needed by dnode_read() */ 1694 const void *vim_spa; 1695 dnode_phys_t *vim_dn; 1696 1697 /* 1698 * An ordered array of mapping entries, sorted by source offset. 1699 * Note that vim_entries is needed during a removal (and contains 1700 * mappings that have been synced to disk so far) to handle frees 1701 * from the removing device. 1702 */ 1703 vdev_indirect_mapping_entry_phys_t *vim_entries; 1704 objset_phys_t *vim_objset; 1705 vdev_indirect_mapping_phys_t *vim_phys; 1706 } vdev_indirect_mapping_t; 1707 1708 /* 1709 * On-disk indirect vdev state. 1710 * 1711 * An indirect vdev is described exclusively in the MOS config of a pool. 1712 * The config for an indirect vdev includes several fields, which are 1713 * accessed in memory by a vdev_indirect_config_t. 1714 */ 1715 typedef struct vdev_indirect_config { 1716 /* 1717 * Object (in MOS) which contains the indirect mapping. This object 1718 * contains an array of vdev_indirect_mapping_entry_phys_t ordered by 1719 * vimep_src. The bonus buffer for this object is a 1720 * vdev_indirect_mapping_phys_t. This object is allocated when a vdev 1721 * removal is initiated. 1722 * 1723 * Note that this object can be empty if none of the data on the vdev 1724 * has been copied yet. 1725 */ 1726 uint64_t vic_mapping_object; 1727 1728 /* 1729 * Object (in MOS) which contains the birth times for the mapping 1730 * entries. This object contains an array of 1731 * vdev_indirect_birth_entry_phys_t sorted by vibe_offset. The bonus 1732 * buffer for this object is a vdev_indirect_birth_phys_t. This object 1733 * is allocated when a vdev removal is initiated. 1734 * 1735 * Note that this object can be empty if none of the vdev has yet been 1736 * copied. 1737 */ 1738 uint64_t vic_births_object; 1739 1740 /* 1741 * This is the vdev ID which was removed previous to this vdev, or 1742 * UINT64_MAX if there are no previously removed vdevs. 1743 */ 1744 uint64_t vic_prev_indirect_vdev; 1745 } vdev_indirect_config_t; 1746 1747 typedef struct vdev { 1748 STAILQ_ENTRY(vdev) v_childlink; /* link in parent's child list */ 1749 STAILQ_ENTRY(vdev) v_alllink; /* link in global vdev list */ 1750 vdev_list_t v_children; /* children of this vdev */ 1751 const char *v_name; /* vdev name */ 1752 uint64_t v_guid; /* vdev guid */ 1753 uint64_t v_id; /* index in parent */ 1754 uint64_t v_psize; /* physical device capacity */ 1755 int v_ashift; /* offset to block shift */ 1756 int v_nparity; /* # parity for raidz */ 1757 struct vdev *v_top; /* parent vdev */ 1758 int v_nchildren; /* # children */ 1759 vdev_state_t v_state; /* current state */ 1760 vdev_phys_read_t *v_phys_read; /* read from raw leaf vdev */ 1761 vdev_read_t *v_read; /* read from vdev */ 1762 void *v_read_priv; /* private data for read function */ 1763 boolean_t v_islog; 1764 struct spa *spa; /* link to spa */ 1765 /* 1766 * Values stored in the config for an indirect or removing vdev. 1767 */ 1768 vdev_indirect_config_t vdev_indirect_config; 1769 vdev_indirect_mapping_t *v_mapping; 1770 } vdev_t; 1771 1772 /* 1773 * In-core pool representation. 1774 */ 1775 typedef STAILQ_HEAD(spa_list, spa) spa_list_t; 1776 1777 typedef struct spa { 1778 STAILQ_ENTRY(spa) spa_link; /* link in global pool list */ 1779 char *spa_name; /* pool name */ 1780 uint64_t spa_guid; /* pool guid */ 1781 uint64_t spa_txg; /* most recent transaction */ 1782 struct uberblock spa_uberblock; /* best uberblock so far */ 1783 vdev_list_t spa_vdevs; /* list of all toplevel vdevs */ 1784 objset_phys_t spa_mos; /* MOS for this pool */ 1785 zio_cksum_salt_t spa_cksum_salt; /* secret salt for cksum */ 1786 void *spa_cksum_tmpls[ZIO_CHECKSUM_FUNCTIONS]; 1787 int spa_inited; /* initialized */ 1788 boolean_t spa_with_log; /* this pool has log */ 1789 } spa_t; 1790 1791 /* IO related arguments. */ 1792 typedef struct zio { 1793 spa_t *io_spa; 1794 blkptr_t *io_bp; 1795 void *io_data; 1796 uint64_t io_size; 1797 uint64_t io_offset; 1798 1799 /* Stuff for the vdev stack */ 1800 vdev_t *io_vd; 1801 void *io_vsd; 1802 1803 int io_error; 1804 } zio_t; 1805 1806 static void decode_embedded_bp_compressed(const blkptr_t *, void *); 1807