1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23*d5de1674SSaso Kiselkov * Copyright 2013 Saso Kiselkov. All rights reserved. 2405d95d03SMatthew Ahrens * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 25e9103aaeSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 260521c5ebSJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens #ifndef _SYS_SPA_H 30fa9e4066Sahrens #define _SYS_SPA_H 31fa9e4066Sahrens 32fa9e4066Sahrens #include <sys/avl.h> 33fa9e4066Sahrens #include <sys/zfs_context.h> 34fa9e4066Sahrens #include <sys/nvpair.h> 35fa9e4066Sahrens #include <sys/sysmacros.h> 36fa9e4066Sahrens #include <sys/types.h> 37fa9e4066Sahrens #include <sys/fs/zfs.h> 38fa9e4066Sahrens 39fa9e4066Sahrens #ifdef __cplusplus 40fa9e4066Sahrens extern "C" { 41fa9e4066Sahrens #endif 42fa9e4066Sahrens 43fa9e4066Sahrens /* 44fa9e4066Sahrens * Forward references that lots of things need. 45fa9e4066Sahrens */ 46fa9e4066Sahrens typedef struct spa spa_t; 47fa9e4066Sahrens typedef struct vdev vdev_t; 48fa9e4066Sahrens typedef struct metaslab metaslab_t; 49b24ab676SJeff Bonwick typedef struct metaslab_group metaslab_group_t; 50b24ab676SJeff Bonwick typedef struct metaslab_class metaslab_class_t; 51b24ab676SJeff Bonwick typedef struct zio zio_t; 52fa9e4066Sahrens typedef struct zilog zilog_t; 53fa94a07fSbrendan typedef struct spa_aux_vdev spa_aux_vdev_t; 54b24ab676SJeff Bonwick typedef struct ddt ddt_t; 55b24ab676SJeff Bonwick typedef struct ddt_entry ddt_entry_t; 56fa9e4066Sahrens struct dsl_pool; 574445fffbSMatthew Ahrens struct dsl_dataset; 58fa9e4066Sahrens 59fa9e4066Sahrens /* 60fa9e4066Sahrens * General-purpose 32-bit and 64-bit bitfield encodings. 61fa9e4066Sahrens */ 62fa9e4066Sahrens #define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len)) 63fa9e4066Sahrens #define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len)) 64fa9e4066Sahrens #define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low)) 65fa9e4066Sahrens #define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low)) 66fa9e4066Sahrens 67fa9e4066Sahrens #define BF32_GET(x, low, len) BF32_DECODE(x, low, len) 68fa9e4066Sahrens #define BF64_GET(x, low, len) BF64_DECODE(x, low, len) 69fa9e4066Sahrens 7049064978SMax Grossman #define BF32_SET(x, low, len, val) do { \ 7149064978SMax Grossman ASSERT3U(val, <, 1U << (len)); \ 7249064978SMax Grossman ASSERT3U(low + len, <=, 32); \ 7349064978SMax Grossman (x) ^= BF32_ENCODE((x >> low) ^ (val), low, len); \ 7449064978SMax Grossman _NOTE(CONSTCOND) } while (0) 7549064978SMax Grossman 7649064978SMax Grossman #define BF64_SET(x, low, len, val) do { \ 7749064978SMax Grossman ASSERT3U(val, <, 1ULL << (len)); \ 7849064978SMax Grossman ASSERT3U(low + len, <=, 64); \ 7949064978SMax Grossman ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)); \ 8049064978SMax Grossman _NOTE(CONSTCOND) } while (0) 81fa9e4066Sahrens 82fa9e4066Sahrens #define BF32_GET_SB(x, low, len, shift, bias) \ 83fa9e4066Sahrens ((BF32_GET(x, low, len) + (bias)) << (shift)) 84fa9e4066Sahrens #define BF64_GET_SB(x, low, len, shift, bias) \ 85fa9e4066Sahrens ((BF64_GET(x, low, len) + (bias)) << (shift)) 86fa9e4066Sahrens 8749064978SMax Grossman #define BF32_SET_SB(x, low, len, shift, bias, val) do { \ 8849064978SMax Grossman ASSERT(IS_P2ALIGNED(val, 1U << shift)); \ 8949064978SMax Grossman ASSERT3S((val) >> (shift), >=, bias); \ 9049064978SMax Grossman BF32_SET(x, low, len, ((val) >> (shift)) - (bias)); \ 9149064978SMax Grossman _NOTE(CONSTCOND) } while (0) 9249064978SMax Grossman #define BF64_SET_SB(x, low, len, shift, bias, val) do { \ 9349064978SMax Grossman ASSERT(IS_P2ALIGNED(val, 1ULL << shift)); \ 9449064978SMax Grossman ASSERT3S((val) >> (shift), >=, bias); \ 9549064978SMax Grossman BF64_SET(x, low, len, ((val) >> (shift)) - (bias)); \ 9649064978SMax Grossman _NOTE(CONSTCOND) } while (0) 97fa9e4066Sahrens 98fa9e4066Sahrens /* 99d1a98260SMatthew Ahrens * We currently support block sizes from 512 bytes to 16MB. 100d1a98260SMatthew Ahrens * The benefits of larger blocks, and thus larger IO, need to be weighed 101d1a98260SMatthew Ahrens * against the cost of COWing a giant block to modify one byte, and the 102d1a98260SMatthew Ahrens * large latency of reading or writing a large block. 103d1a98260SMatthew Ahrens * 104d1a98260SMatthew Ahrens * Note that although blocks up to 16MB are supported, the recordsize 105d1a98260SMatthew Ahrens * property can not be set larger than zfs_max_recordsize (default 1MB). 106d1a98260SMatthew Ahrens * See the comment near zfs_max_recordsize in dsl_dataset.c for details. 107d1a98260SMatthew Ahrens * 108d1a98260SMatthew Ahrens * Note that although the LSIZE field of the blkptr_t can store sizes up 109d1a98260SMatthew Ahrens * to 32MB, the dnode's dn_datablkszsec can only store sizes up to 110d1a98260SMatthew Ahrens * 32MB - 512 bytes. Therefore, we limit SPA_MAXBLOCKSIZE to 16MB. 111fa9e4066Sahrens */ 112fa9e4066Sahrens #define SPA_MINBLOCKSHIFT 9 113d1a98260SMatthew Ahrens #define SPA_OLD_MAXBLOCKSHIFT 17 114d1a98260SMatthew Ahrens #define SPA_MAXBLOCKSHIFT 24 115fa9e4066Sahrens #define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT) 116d1a98260SMatthew Ahrens #define SPA_OLD_MAXBLOCKSIZE (1ULL << SPA_OLD_MAXBLOCKSHIFT) 117fa9e4066Sahrens #define SPA_MAXBLOCKSIZE (1ULL << SPA_MAXBLOCKSHIFT) 118fa9e4066Sahrens 119fa9e4066Sahrens /* 120f7991ba4STim Haley * Size of block to hold the configuration data (a packed nvlist) 121f7991ba4STim Haley */ 122ad135b5dSChristopher Siden #define SPA_CONFIG_BLOCKSIZE (1ULL << 14) 123f7991ba4STim Haley 124f7991ba4STim Haley /* 125fa9e4066Sahrens * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB. 126fa9e4066Sahrens * The ASIZE encoding should be at least 64 times larger (6 more bits) 127fa9e4066Sahrens * to support up to 4-way RAID-Z mirror mode with worst-case gang block 128fa9e4066Sahrens * overhead, three DVAs per bp, plus one more bit in case we do anything 129fa9e4066Sahrens * else that expands the ASIZE. 130fa9e4066Sahrens */ 131fa9e4066Sahrens #define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */ 132fa9e4066Sahrens #define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */ 133fa9e4066Sahrens #define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */ 134fa9e4066Sahrens 135fa9e4066Sahrens /* 136fa9e4066Sahrens * All SPA data is represented by 128-bit data virtual addresses (DVAs). 137fa9e4066Sahrens * The members of the dva_t should be considered opaque outside the SPA. 138fa9e4066Sahrens */ 139fa9e4066Sahrens typedef struct dva { 140fa9e4066Sahrens uint64_t dva_word[2]; 141fa9e4066Sahrens } dva_t; 142fa9e4066Sahrens 143fa9e4066Sahrens /* 144fa9e4066Sahrens * Each block has a 256-bit checksum -- strong enough for cryptographic hashes. 145fa9e4066Sahrens */ 146fa9e4066Sahrens typedef struct zio_cksum { 147fa9e4066Sahrens uint64_t zc_word[4]; 148fa9e4066Sahrens } zio_cksum_t; 149fa9e4066Sahrens 150fa9e4066Sahrens /* 151fa9e4066Sahrens * Each block is described by its DVAs, time of birth, checksum, etc. 152fa9e4066Sahrens * The word-by-word, bit-by-bit layout of the blkptr is as follows: 153fa9e4066Sahrens * 154fa9e4066Sahrens * 64 56 48 40 32 24 16 8 0 155fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 156fa9e4066Sahrens * 0 | vdev1 | GRID | ASIZE | 157fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 158fa9e4066Sahrens * 1 |G| offset1 | 159fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 160fa9e4066Sahrens * 2 | vdev2 | GRID | ASIZE | 161fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 162fa9e4066Sahrens * 3 |G| offset2 | 163fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 164fa9e4066Sahrens * 4 | vdev3 | GRID | ASIZE | 165fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 166fa9e4066Sahrens * 5 |G| offset3 | 167fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 168e54e0be9SMatthew Ahrens * 6 |BDX|lvl| type | cksum |E| comp| PSIZE | LSIZE | 169fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 170fa9e4066Sahrens * 7 | padding | 171fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 172fa9e4066Sahrens * 8 | padding | 173fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 174b24ab676SJeff Bonwick * 9 | physical birth txg | 175fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 176b24ab676SJeff Bonwick * a | logical birth txg | 177fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 178fa9e4066Sahrens * b | fill count | 179fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 180fa9e4066Sahrens * c | checksum[0] | 181fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 182fa9e4066Sahrens * d | checksum[1] | 183fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 184fa9e4066Sahrens * e | checksum[2] | 185fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 186fa9e4066Sahrens * f | checksum[3] | 187fa9e4066Sahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 188fa9e4066Sahrens * 189fa9e4066Sahrens * Legend: 190fa9e4066Sahrens * 191fa9e4066Sahrens * vdev virtual device ID 192fa9e4066Sahrens * offset offset into virtual device 193fa9e4066Sahrens * LSIZE logical size 194fa9e4066Sahrens * PSIZE physical size (after compression) 195fa9e4066Sahrens * ASIZE allocated size (including RAID-Z parity and gang block headers) 196fa9e4066Sahrens * GRID RAID-Z layout information (reserved for future use) 197fa9e4066Sahrens * cksum checksum function 198fa9e4066Sahrens * comp compression function 199fa9e4066Sahrens * G gang block indicator 200b24ab676SJeff Bonwick * B byteorder (endianness) 201b24ab676SJeff Bonwick * D dedup 202e54e0be9SMatthew Ahrens * X encryption (on version 30, which is not supported) 203e54e0be9SMatthew Ahrens * E blkptr_t contains embedded data (see below) 204fa9e4066Sahrens * lvl level of indirection 205b24ab676SJeff Bonwick * type DMU object type 206b24ab676SJeff Bonwick * phys birth txg of block allocation; zero if same as logical birth txg 207b24ab676SJeff Bonwick * log. birth transaction group in which the block was logically born 208fa9e4066Sahrens * fill count number of non-zero blocks under this bp 209fa9e4066Sahrens * checksum[4] 256-bit checksum of the data this bp describes 210fa9e4066Sahrens */ 211e54e0be9SMatthew Ahrens 212e54e0be9SMatthew Ahrens /* 213e54e0be9SMatthew Ahrens * "Embedded" blkptr_t's don't actually point to a block, instead they 214e54e0be9SMatthew Ahrens * have a data payload embedded in the blkptr_t itself. See the comment 215e54e0be9SMatthew Ahrens * in blkptr.c for more details. 216e54e0be9SMatthew Ahrens * 217e54e0be9SMatthew Ahrens * The blkptr_t is laid out as follows: 218e54e0be9SMatthew Ahrens * 219e54e0be9SMatthew Ahrens * 64 56 48 40 32 24 16 8 0 220e54e0be9SMatthew Ahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 221e54e0be9SMatthew Ahrens * 0 | payload | 222e54e0be9SMatthew Ahrens * 1 | payload | 223e54e0be9SMatthew Ahrens * 2 | payload | 224e54e0be9SMatthew Ahrens * 3 | payload | 225e54e0be9SMatthew Ahrens * 4 | payload | 226e54e0be9SMatthew Ahrens * 5 | payload | 227e54e0be9SMatthew Ahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 228e54e0be9SMatthew Ahrens * 6 |BDX|lvl| type | etype |E| comp| PSIZE| LSIZE | 229e54e0be9SMatthew Ahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 230e54e0be9SMatthew Ahrens * 7 | payload | 231e54e0be9SMatthew Ahrens * 8 | payload | 232e54e0be9SMatthew Ahrens * 9 | payload | 233e54e0be9SMatthew Ahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 234e54e0be9SMatthew Ahrens * a | logical birth txg | 235e54e0be9SMatthew Ahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 236e54e0be9SMatthew Ahrens * b | payload | 237e54e0be9SMatthew Ahrens * c | payload | 238e54e0be9SMatthew Ahrens * d | payload | 239e54e0be9SMatthew Ahrens * e | payload | 240e54e0be9SMatthew Ahrens * f | payload | 241e54e0be9SMatthew Ahrens * +-------+-------+-------+-------+-------+-------+-------+-------+ 242e54e0be9SMatthew Ahrens * 243e54e0be9SMatthew Ahrens * Legend: 244e54e0be9SMatthew Ahrens * 245e54e0be9SMatthew Ahrens * payload contains the embedded data 246e54e0be9SMatthew Ahrens * B (byteorder) byteorder (endianness) 247e54e0be9SMatthew Ahrens * D (dedup) padding (set to zero) 248e54e0be9SMatthew Ahrens * X encryption (set to zero; see above) 249e54e0be9SMatthew Ahrens * E (embedded) set to one 250e54e0be9SMatthew Ahrens * lvl indirection level 251e54e0be9SMatthew Ahrens * type DMU object type 252e54e0be9SMatthew Ahrens * etype how to interpret embedded data (BP_EMBEDDED_TYPE_*) 253e54e0be9SMatthew Ahrens * comp compression function of payload 254e54e0be9SMatthew Ahrens * PSIZE size of payload after compression, in bytes 255e54e0be9SMatthew Ahrens * LSIZE logical size of payload, in bytes 256e54e0be9SMatthew Ahrens * note that 25 bits is enough to store the largest 257e54e0be9SMatthew Ahrens * "normal" BP's LSIZE (2^16 * 2^9) in bytes 258e54e0be9SMatthew Ahrens * log. birth transaction group in which the block was logically born 259e54e0be9SMatthew Ahrens * 260e54e0be9SMatthew Ahrens * Note that LSIZE and PSIZE are stored in bytes, whereas for non-embedded 261e54e0be9SMatthew Ahrens * bp's they are stored in units of SPA_MINBLOCKSHIFT. 262e54e0be9SMatthew Ahrens * Generally, the generic BP_GET_*() macros can be used on embedded BP's. 263e54e0be9SMatthew Ahrens * The B, D, X, lvl, type, and comp fields are stored the same as with normal 264e54e0be9SMatthew Ahrens * BP's so the BP_SET_* macros can be used with them. etype, PSIZE, LSIZE must 265e54e0be9SMatthew Ahrens * be set with the BPE_SET_* macros. BP_SET_EMBEDDED() should be called before 266e54e0be9SMatthew Ahrens * other macros, as they assert that they are only used on BP's of the correct 267e54e0be9SMatthew Ahrens * "embedded-ness". 268e54e0be9SMatthew Ahrens */ 269e54e0be9SMatthew Ahrens 270e54e0be9SMatthew Ahrens #define BPE_GET_ETYPE(bp) \ 271e54e0be9SMatthew Ahrens (ASSERT(BP_IS_EMBEDDED(bp)), \ 272e54e0be9SMatthew Ahrens BF64_GET((bp)->blk_prop, 40, 8)) 273e54e0be9SMatthew Ahrens #define BPE_SET_ETYPE(bp, t) do { \ 274e54e0be9SMatthew Ahrens ASSERT(BP_IS_EMBEDDED(bp)); \ 275e54e0be9SMatthew Ahrens BF64_SET((bp)->blk_prop, 40, 8, t); \ 276e54e0be9SMatthew Ahrens _NOTE(CONSTCOND) } while (0) 277e54e0be9SMatthew Ahrens 278e54e0be9SMatthew Ahrens #define BPE_GET_LSIZE(bp) \ 279e54e0be9SMatthew Ahrens (ASSERT(BP_IS_EMBEDDED(bp)), \ 280e54e0be9SMatthew Ahrens BF64_GET_SB((bp)->blk_prop, 0, 25, 0, 1)) 281e54e0be9SMatthew Ahrens #define BPE_SET_LSIZE(bp, x) do { \ 282e54e0be9SMatthew Ahrens ASSERT(BP_IS_EMBEDDED(bp)); \ 283e54e0be9SMatthew Ahrens BF64_SET_SB((bp)->blk_prop, 0, 25, 0, 1, x); \ 284e54e0be9SMatthew Ahrens _NOTE(CONSTCOND) } while (0) 285e54e0be9SMatthew Ahrens 286e54e0be9SMatthew Ahrens #define BPE_GET_PSIZE(bp) \ 287e54e0be9SMatthew Ahrens (ASSERT(BP_IS_EMBEDDED(bp)), \ 288e54e0be9SMatthew Ahrens BF64_GET_SB((bp)->blk_prop, 25, 7, 0, 1)) 289e54e0be9SMatthew Ahrens #define BPE_SET_PSIZE(bp, x) do { \ 290e54e0be9SMatthew Ahrens ASSERT(BP_IS_EMBEDDED(bp)); \ 291e54e0be9SMatthew Ahrens BF64_SET_SB((bp)->blk_prop, 25, 7, 0, 1, x); \ 292e54e0be9SMatthew Ahrens _NOTE(CONSTCOND) } while (0) 293e54e0be9SMatthew Ahrens 294e54e0be9SMatthew Ahrens typedef enum bp_embedded_type { 295e54e0be9SMatthew Ahrens BP_EMBEDDED_TYPE_DATA, 296e54e0be9SMatthew Ahrens BP_EMBEDDED_TYPE_RESERVED, /* Reserved for an unintegrated feature. */ 297e54e0be9SMatthew Ahrens NUM_BP_EMBEDDED_TYPES = BP_EMBEDDED_TYPE_RESERVED 298e54e0be9SMatthew Ahrens } bp_embedded_type_t; 299e54e0be9SMatthew Ahrens 300e54e0be9SMatthew Ahrens #define BPE_NUM_WORDS 14 301e54e0be9SMatthew Ahrens #define BPE_PAYLOAD_SIZE (BPE_NUM_WORDS * sizeof (uint64_t)) 302e54e0be9SMatthew Ahrens #define BPE_IS_PAYLOADWORD(bp, wp) \ 303e54e0be9SMatthew Ahrens ((wp) != &(bp)->blk_prop && (wp) != &(bp)->blk_birth) 304e54e0be9SMatthew Ahrens 305b24ab676SJeff Bonwick #define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */ 306b24ab676SJeff Bonwick #define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */ 307b24ab676SJeff Bonwick 30849064978SMax Grossman /* 30949064978SMax Grossman * A block is a hole when it has either 1) never been written to, or 31049064978SMax Grossman * 2) is zero-filled. In both cases, ZFS can return all zeroes for all reads 31149064978SMax Grossman * without physically allocating disk space. Holes are represented in the 31249064978SMax Grossman * blkptr_t structure by zeroed blk_dva. Correct checking for holes is 31349064978SMax Grossman * done through the BP_IS_HOLE macro. For holes, the logical size, level, 31449064978SMax Grossman * DMU object type, and birth times are all also stored for holes that 31549064978SMax Grossman * were written to at some point (i.e. were punched after having been filled). 31649064978SMax Grossman */ 317fa9e4066Sahrens typedef struct blkptr { 318b24ab676SJeff Bonwick dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */ 319fa9e4066Sahrens uint64_t blk_prop; /* size, compression, type, etc */ 320b24ab676SJeff Bonwick uint64_t blk_pad[2]; /* Extra space for the future */ 321b24ab676SJeff Bonwick uint64_t blk_phys_birth; /* txg when block was allocated */ 322fa9e4066Sahrens uint64_t blk_birth; /* transaction group at birth */ 323fa9e4066Sahrens uint64_t blk_fill; /* fill count */ 324fa9e4066Sahrens zio_cksum_t blk_cksum; /* 256-bit checksum */ 325fa9e4066Sahrens } blkptr_t; 326fa9e4066Sahrens 327fa9e4066Sahrens /* 328fa9e4066Sahrens * Macros to get and set fields in a bp or DVA. 329fa9e4066Sahrens */ 330fa9e4066Sahrens #define DVA_GET_ASIZE(dva) \ 33149064978SMax Grossman BF64_GET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, SPA_MINBLOCKSHIFT, 0) 332fa9e4066Sahrens #define DVA_SET_ASIZE(dva, x) \ 33349064978SMax Grossman BF64_SET_SB((dva)->dva_word[0], 0, SPA_ASIZEBITS, \ 33449064978SMax Grossman SPA_MINBLOCKSHIFT, 0, x) 335fa9e4066Sahrens 336fa9e4066Sahrens #define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8) 337fa9e4066Sahrens #define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x) 338fa9e4066Sahrens 339fa9e4066Sahrens #define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32) 340fa9e4066Sahrens #define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x) 341fa9e4066Sahrens 342fa9e4066Sahrens #define DVA_GET_OFFSET(dva) \ 343fa9e4066Sahrens BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0) 344fa9e4066Sahrens #define DVA_SET_OFFSET(dva, x) \ 345fa9e4066Sahrens BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x) 346fa9e4066Sahrens 347fa9e4066Sahrens #define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1) 348fa9e4066Sahrens #define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x) 349fa9e4066Sahrens 350fa9e4066Sahrens #define BP_GET_LSIZE(bp) \ 351e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? \ 352e54e0be9SMatthew Ahrens (BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA ? BPE_GET_LSIZE(bp) : 0): \ 353e54e0be9SMatthew Ahrens BF64_GET_SB((bp)->blk_prop, 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1)) 354e54e0be9SMatthew Ahrens #define BP_SET_LSIZE(bp, x) do { \ 355e54e0be9SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); \ 356e54e0be9SMatthew Ahrens BF64_SET_SB((bp)->blk_prop, \ 357e54e0be9SMatthew Ahrens 0, SPA_LSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \ 358e54e0be9SMatthew Ahrens _NOTE(CONSTCOND) } while (0) 359fa9e4066Sahrens 360fa9e4066Sahrens #define BP_GET_PSIZE(bp) \ 361e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? 0 : \ 362e54e0be9SMatthew Ahrens BF64_GET_SB((bp)->blk_prop, 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1)) 363e54e0be9SMatthew Ahrens #define BP_SET_PSIZE(bp, x) do { \ 364e54e0be9SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); \ 365e54e0be9SMatthew Ahrens BF64_SET_SB((bp)->blk_prop, \ 366e54e0be9SMatthew Ahrens 16, SPA_PSIZEBITS, SPA_MINBLOCKSHIFT, 1, x); \ 367e54e0be9SMatthew Ahrens _NOTE(CONSTCOND) } while (0) 368fa9e4066Sahrens 369e54e0be9SMatthew Ahrens #define BP_GET_COMPRESS(bp) BF64_GET((bp)->blk_prop, 32, 7) 370e54e0be9SMatthew Ahrens #define BP_SET_COMPRESS(bp, x) BF64_SET((bp)->blk_prop, 32, 7, x) 371fa9e4066Sahrens 372e54e0be9SMatthew Ahrens #define BP_IS_EMBEDDED(bp) BF64_GET((bp)->blk_prop, 39, 1) 373e54e0be9SMatthew Ahrens #define BP_SET_EMBEDDED(bp, x) BF64_SET((bp)->blk_prop, 39, 1, x) 374e54e0be9SMatthew Ahrens 375e54e0be9SMatthew Ahrens #define BP_GET_CHECKSUM(bp) \ 376e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? ZIO_CHECKSUM_OFF : \ 377e54e0be9SMatthew Ahrens BF64_GET((bp)->blk_prop, 40, 8)) 378e54e0be9SMatthew Ahrens #define BP_SET_CHECKSUM(bp, x) do { \ 379e54e0be9SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); \ 380e54e0be9SMatthew Ahrens BF64_SET((bp)->blk_prop, 40, 8, x); \ 381e54e0be9SMatthew Ahrens _NOTE(CONSTCOND) } while (0) 382fa9e4066Sahrens 383fa9e4066Sahrens #define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8) 384fa9e4066Sahrens #define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x) 385fa9e4066Sahrens 386fa9e4066Sahrens #define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5) 387fa9e4066Sahrens #define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x) 388fa9e4066Sahrens 389b24ab676SJeff Bonwick #define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1) 390b24ab676SJeff Bonwick #define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x) 391b24ab676SJeff Bonwick 39249064978SMax Grossman #define BP_GET_BYTEORDER(bp) BF64_GET((bp)->blk_prop, 63, 1) 393fa9e4066Sahrens #define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x) 394fa9e4066Sahrens 395b24ab676SJeff Bonwick #define BP_PHYSICAL_BIRTH(bp) \ 396e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? 0 : \ 397e54e0be9SMatthew Ahrens (bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth) 398b24ab676SJeff Bonwick 399b24ab676SJeff Bonwick #define BP_SET_BIRTH(bp, logical, physical) \ 400b24ab676SJeff Bonwick { \ 401e54e0be9SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); \ 402b24ab676SJeff Bonwick (bp)->blk_birth = (logical); \ 403b24ab676SJeff Bonwick (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \ 404b24ab676SJeff Bonwick } 405b24ab676SJeff Bonwick 406e54e0be9SMatthew Ahrens #define BP_GET_FILL(bp) (BP_IS_EMBEDDED(bp) ? 1 : (bp)->blk_fill) 407e54e0be9SMatthew Ahrens 408fa9e4066Sahrens #define BP_GET_ASIZE(bp) \ 409e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? 0 : \ 410e54e0be9SMatthew Ahrens DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \ 411e54e0be9SMatthew Ahrens DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ 412fa9e4066Sahrens DVA_GET_ASIZE(&(bp)->blk_dva[2])) 413fa9e4066Sahrens 41499653d4eSeschrock #define BP_GET_UCSIZE(bp) \ 415ad135b5dSChristopher Siden ((BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) ? \ 4163f9d6ad7SLin Ling BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp)) 41799653d4eSeschrock 41844cd46caSbillm #define BP_GET_NDVAS(bp) \ 419e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? 0 : \ 420e54e0be9SMatthew Ahrens !!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \ 42144cd46caSbillm !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ 42244cd46caSbillm !!DVA_GET_ASIZE(&(bp)->blk_dva[2])) 42344cd46caSbillm 42444cd46caSbillm #define BP_COUNT_GANG(bp) \ 425e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? 0 : \ 42644cd46caSbillm (DVA_GET_GANG(&(bp)->blk_dva[0]) + \ 42744cd46caSbillm DVA_GET_GANG(&(bp)->blk_dva[1]) + \ 428e54e0be9SMatthew Ahrens DVA_GET_GANG(&(bp)->blk_dva[2]))) 42944cd46caSbillm 430fa9e4066Sahrens #define DVA_EQUAL(dva1, dva2) \ 431fa9e4066Sahrens ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \ 432fa9e4066Sahrens (dva1)->dva_word[0] == (dva2)->dva_word[0]) 433fa9e4066Sahrens 434b24ab676SJeff Bonwick #define BP_EQUAL(bp1, bp2) \ 435b24ab676SJeff Bonwick (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \ 436e54e0be9SMatthew Ahrens (bp1)->blk_birth == (bp2)->blk_birth && \ 437b24ab676SJeff Bonwick DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \ 438b24ab676SJeff Bonwick DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \ 439b24ab676SJeff Bonwick DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2])) 440b24ab676SJeff Bonwick 4416b4acc8bSahrens #define ZIO_CHECKSUM_EQUAL(zc1, zc2) \ 4426b4acc8bSahrens (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \ 4436b4acc8bSahrens ((zc1).zc_word[1] - (zc2).zc_word[1]) | \ 4446b4acc8bSahrens ((zc1).zc_word[2] - (zc2).zc_word[2]) | \ 4456b4acc8bSahrens ((zc1).zc_word[3] - (zc2).zc_word[3]))) 4466b4acc8bSahrens 44762e66153SMatthew Ahrens #define ZIO_CHECKSUM_IS_ZERO(zc) \ 44862e66153SMatthew Ahrens (0 == ((zc)->zc_word[0] | (zc)->zc_word[1] | \ 44962e66153SMatthew Ahrens (zc)->zc_word[2] | (zc)->zc_word[3])) 45062e66153SMatthew Ahrens 451*d5de1674SSaso Kiselkov #define ZIO_CHECKSUM_BSWAP(_zc) \ 452*d5de1674SSaso Kiselkov do { \ 453*d5de1674SSaso Kiselkov zio_cksum_t *zc = (_zc); \ 454*d5de1674SSaso Kiselkov zc->zc_word[0] = BSWAP_64(zc->zc_word[0]); \ 455*d5de1674SSaso Kiselkov zc->zc_word[1] = BSWAP_64(zc->zc_word[1]); \ 456*d5de1674SSaso Kiselkov zc->zc_word[2] = BSWAP_64(zc->zc_word[2]); \ 457*d5de1674SSaso Kiselkov zc->zc_word[3] = BSWAP_64(zc->zc_word[3]); \ 458*d5de1674SSaso Kiselkov _NOTE(NOTREACHED) \ 459*d5de1674SSaso Kiselkov _NOTE(CONSTCOND) \ 460*d5de1674SSaso Kiselkov } while (0) 46162e66153SMatthew Ahrens 462fa9e4066Sahrens #define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0) 463fa9e4066Sahrens 464fa9e4066Sahrens #define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3) \ 465fa9e4066Sahrens { \ 466fa9e4066Sahrens (zcp)->zc_word[0] = w0; \ 467fa9e4066Sahrens (zcp)->zc_word[1] = w1; \ 468fa9e4066Sahrens (zcp)->zc_word[2] = w2; \ 469fa9e4066Sahrens (zcp)->zc_word[3] = w3; \ 470fa9e4066Sahrens } 471fa9e4066Sahrens 472e54e0be9SMatthew Ahrens #define BP_IDENTITY(bp) (ASSERT(!BP_IS_EMBEDDED(bp)), &(bp)->blk_dva[0]) 473e54e0be9SMatthew Ahrens #define BP_IS_GANG(bp) \ 474e54e0be9SMatthew Ahrens (BP_IS_EMBEDDED(bp) ? B_FALSE : DVA_GET_GANG(BP_IDENTITY(bp))) 47549064978SMax Grossman #define DVA_IS_EMPTY(dva) ((dva)->dva_word[0] == 0ULL && \ 47649064978SMax Grossman (dva)->dva_word[1] == 0ULL) 477e54e0be9SMatthew Ahrens #define BP_IS_HOLE(bp) \ 478e54e0be9SMatthew Ahrens (!BP_IS_EMBEDDED(bp) && DVA_IS_EMPTY(BP_IDENTITY(bp))) 479fa9e4066Sahrens 4806e1f5caaSNeil Perrin /* BP_IS_RAIDZ(bp) assumes no block compression */ 4816e1f5caaSNeil Perrin #define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \ 4826e1f5caaSNeil Perrin BP_GET_PSIZE(bp)) 4836e1f5caaSNeil Perrin 484e14bb325SJeff Bonwick #define BP_ZERO(bp) \ 485fa9e4066Sahrens { \ 486fa9e4066Sahrens (bp)->blk_dva[0].dva_word[0] = 0; \ 487fa9e4066Sahrens (bp)->blk_dva[0].dva_word[1] = 0; \ 488fa9e4066Sahrens (bp)->blk_dva[1].dva_word[0] = 0; \ 489fa9e4066Sahrens (bp)->blk_dva[1].dva_word[1] = 0; \ 490fa9e4066Sahrens (bp)->blk_dva[2].dva_word[0] = 0; \ 491fa9e4066Sahrens (bp)->blk_dva[2].dva_word[1] = 0; \ 492fa9e4066Sahrens (bp)->blk_prop = 0; \ 493fa9e4066Sahrens (bp)->blk_pad[0] = 0; \ 494fa9e4066Sahrens (bp)->blk_pad[1] = 0; \ 495b24ab676SJeff Bonwick (bp)->blk_phys_birth = 0; \ 496e14bb325SJeff Bonwick (bp)->blk_birth = 0; \ 497fa9e4066Sahrens (bp)->blk_fill = 0; \ 498fa9e4066Sahrens ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \ 499fa9e4066Sahrens } 500fa9e4066Sahrens 501fa9e4066Sahrens #ifdef _BIG_ENDIAN 502fa9e4066Sahrens #define ZFS_HOST_BYTEORDER (0ULL) 503fa9e4066Sahrens #else 50449064978SMax Grossman #define ZFS_HOST_BYTEORDER (1ULL) 505fa9e4066Sahrens #endif 506fa9e4066Sahrens 507fa9e4066Sahrens #define BP_SHOULD_BYTESWAP(bp) (BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER) 508fa9e4066Sahrens 50944cd46caSbillm #define BP_SPRINTF_LEN 320 510fbabab8fSmaybee 511b24ab676SJeff Bonwick /* 512b24ab676SJeff Bonwick * This macro allows code sharing between zfs, libzpool, and mdb. 513b24ab676SJeff Bonwick * 'func' is either snprintf() or mdb_snprintf(). 514b24ab676SJeff Bonwick * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line. 515b24ab676SJeff Bonwick */ 51649064978SMax Grossman #define SNPRINTF_BLKPTR(func, ws, buf, size, bp, type, checksum, compress) \ 517b24ab676SJeff Bonwick { \ 518b24ab676SJeff Bonwick static const char *copyname[] = \ 519b24ab676SJeff Bonwick { "zero", "single", "double", "triple" }; \ 520b24ab676SJeff Bonwick int len = 0; \ 521b24ab676SJeff Bonwick int copies = 0; \ 522b24ab676SJeff Bonwick \ 523b24ab676SJeff Bonwick if (bp == NULL) { \ 52449064978SMax Grossman len += func(buf + len, size - len, "<NULL>"); \ 525b24ab676SJeff Bonwick } else if (BP_IS_HOLE(bp)) { \ 52649064978SMax Grossman len += func(buf + len, size - len, \ 52701eddf60SPrakash Surya "HOLE [L%llu %s] " \ 52801eddf60SPrakash Surya "size=%llxL birth=%lluL", \ 52901eddf60SPrakash Surya (u_longlong_t)BP_GET_LEVEL(bp), \ 53001eddf60SPrakash Surya type, \ 53101eddf60SPrakash Surya (u_longlong_t)BP_GET_LSIZE(bp), \ 53249064978SMax Grossman (u_longlong_t)bp->blk_birth); \ 533e54e0be9SMatthew Ahrens } else if (BP_IS_EMBEDDED(bp)) { \ 534e54e0be9SMatthew Ahrens len = func(buf + len, size - len, \ 535e54e0be9SMatthew Ahrens "EMBEDDED [L%llu %s] et=%u %s " \ 536e54e0be9SMatthew Ahrens "size=%llxL/%llxP birth=%lluL", \ 537e54e0be9SMatthew Ahrens (u_longlong_t)BP_GET_LEVEL(bp), \ 538e54e0be9SMatthew Ahrens type, \ 539e54e0be9SMatthew Ahrens (int)BPE_GET_ETYPE(bp), \ 540e54e0be9SMatthew Ahrens compress, \ 541e54e0be9SMatthew Ahrens (u_longlong_t)BPE_GET_LSIZE(bp), \ 542e54e0be9SMatthew Ahrens (u_longlong_t)BPE_GET_PSIZE(bp), \ 543e54e0be9SMatthew Ahrens (u_longlong_t)bp->blk_birth); \ 544b24ab676SJeff Bonwick } else { \ 545b24ab676SJeff Bonwick for (int d = 0; d < BP_GET_NDVAS(bp); d++) { \ 546b24ab676SJeff Bonwick const dva_t *dva = &bp->blk_dva[d]; \ 547b24ab676SJeff Bonwick if (DVA_IS_VALID(dva)) \ 548b24ab676SJeff Bonwick copies++; \ 549b24ab676SJeff Bonwick len += func(buf + len, size - len, \ 550b24ab676SJeff Bonwick "DVA[%d]=<%llu:%llx:%llx>%c", d, \ 551b24ab676SJeff Bonwick (u_longlong_t)DVA_GET_VDEV(dva), \ 552b24ab676SJeff Bonwick (u_longlong_t)DVA_GET_OFFSET(dva), \ 553b24ab676SJeff Bonwick (u_longlong_t)DVA_GET_ASIZE(dva), \ 554b24ab676SJeff Bonwick ws); \ 555b24ab676SJeff Bonwick } \ 556b24ab676SJeff Bonwick if (BP_IS_GANG(bp) && \ 557b24ab676SJeff Bonwick DVA_GET_ASIZE(&bp->blk_dva[2]) <= \ 558b24ab676SJeff Bonwick DVA_GET_ASIZE(&bp->blk_dva[1]) / 2) \ 559b24ab676SJeff Bonwick copies--; \ 560b24ab676SJeff Bonwick len += func(buf + len, size - len, \ 561b24ab676SJeff Bonwick "[L%llu %s] %s %s %s %s %s %s%c" \ 562b24ab676SJeff Bonwick "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c" \ 563b24ab676SJeff Bonwick "cksum=%llx:%llx:%llx:%llx", \ 564b24ab676SJeff Bonwick (u_longlong_t)BP_GET_LEVEL(bp), \ 565b24ab676SJeff Bonwick type, \ 566b24ab676SJeff Bonwick checksum, \ 567b24ab676SJeff Bonwick compress, \ 568b24ab676SJeff Bonwick BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE", \ 569b24ab676SJeff Bonwick BP_IS_GANG(bp) ? "gang" : "contiguous", \ 570b24ab676SJeff Bonwick BP_GET_DEDUP(bp) ? "dedup" : "unique", \ 571b24ab676SJeff Bonwick copyname[copies], \ 572b24ab676SJeff Bonwick ws, \ 573b24ab676SJeff Bonwick (u_longlong_t)BP_GET_LSIZE(bp), \ 574b24ab676SJeff Bonwick (u_longlong_t)BP_GET_PSIZE(bp), \ 575b24ab676SJeff Bonwick (u_longlong_t)bp->blk_birth, \ 576b24ab676SJeff Bonwick (u_longlong_t)BP_PHYSICAL_BIRTH(bp), \ 577e54e0be9SMatthew Ahrens (u_longlong_t)BP_GET_FILL(bp), \ 578b24ab676SJeff Bonwick ws, \ 579b24ab676SJeff Bonwick (u_longlong_t)bp->blk_cksum.zc_word[0], \ 580b24ab676SJeff Bonwick (u_longlong_t)bp->blk_cksum.zc_word[1], \ 581b24ab676SJeff Bonwick (u_longlong_t)bp->blk_cksum.zc_word[2], \ 582b24ab676SJeff Bonwick (u_longlong_t)bp->blk_cksum.zc_word[3]); \ 583b24ab676SJeff Bonwick } \ 584b24ab676SJeff Bonwick ASSERT(len < size); \ 585b24ab676SJeff Bonwick } 586b24ab676SJeff Bonwick 587fa9e4066Sahrens #include <sys/dmu.h> 588fa9e4066Sahrens 589ad23a2dbSjohansen #define BP_GET_BUFC_TYPE(bp) \ 590ad135b5dSChristopher Siden (((BP_GET_LEVEL(bp) > 0) || (DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))) ? \ 591ad135b5dSChristopher Siden ARC_BUFC_METADATA : ARC_BUFC_DATA) 592fa9e4066Sahrens 5931195e687SMark J Musante typedef enum spa_import_type { 5941195e687SMark J Musante SPA_IMPORT_EXISTING, 5951195e687SMark J Musante SPA_IMPORT_ASSEMBLE 5961195e687SMark J Musante } spa_import_type_t; 5971195e687SMark J Musante 598fa9e4066Sahrens /* state manipulation functions */ 599fa9e4066Sahrens extern int spa_open(const char *pool, spa_t **, void *tag); 600468c413aSTim Haley extern int spa_open_rewind(const char *pool, spa_t **, void *tag, 601468c413aSTim Haley nvlist_t *policy, nvlist_t **config); 602ad135b5dSChristopher Siden extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot, 603ad135b5dSChristopher Siden size_t buflen); 604990b4856Slling extern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props, 6054445fffbSMatthew Ahrens nvlist_t *zplprops); 606051aabe6Staylor extern int spa_import_rootpool(char *devpath, char *devid); 6074b964adaSGeorge Wilson extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props, 6084b964adaSGeorge Wilson uint64_t flags); 609fa9e4066Sahrens extern nvlist_t *spa_tryimport(nvlist_t *tryconfig); 610fa9e4066Sahrens extern int spa_destroy(char *pool); 611394ab0cbSGeorge Wilson extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, 612394ab0cbSGeorge Wilson boolean_t hardforce); 613ea8dc4b6Seschrock extern int spa_reset(char *pool); 614ea8dc4b6Seschrock extern void spa_async_request(spa_t *spa, int flag); 615088f3894Sahrens extern void spa_async_unrequest(spa_t *spa, int flag); 616ea8dc4b6Seschrock extern void spa_async_suspend(spa_t *spa); 617ea8dc4b6Seschrock extern void spa_async_resume(spa_t *spa); 618ea8dc4b6Seschrock extern spa_t *spa_inject_addref(char *pool); 619ea8dc4b6Seschrock extern void spa_inject_delref(spa_t *spa); 6203f9d6ad7SLin Ling extern void spa_scan_stat_init(spa_t *spa); 6213f9d6ad7SLin Ling extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps); 622ea8dc4b6Seschrock 623e14bb325SJeff Bonwick #define SPA_ASYNC_CONFIG_UPDATE 0x01 624e14bb325SJeff Bonwick #define SPA_ASYNC_REMOVE 0x02 625e14bb325SJeff Bonwick #define SPA_ASYNC_PROBE 0x04 626e14bb325SJeff Bonwick #define SPA_ASYNC_RESILVER_DONE 0x08 627e14bb325SJeff Bonwick #define SPA_ASYNC_RESILVER 0x10 628573ca77eSGeorge Wilson #define SPA_ASYNC_AUTOEXPAND 0x20 6293f9d6ad7SLin Ling #define SPA_ASYNC_REMOVE_DONE 0x40 6303f9d6ad7SLin Ling #define SPA_ASYNC_REMOVE_STOP 0x80 631*d5de1674SSaso Kiselkov #define SPA_ASYNC_L2CACHE_REBUILD 0x100 6323f9d6ad7SLin Ling 6333f9d6ad7SLin Ling /* 6343f9d6ad7SLin Ling * Controls the behavior of spa_vdev_remove(). 6353f9d6ad7SLin Ling */ 6363f9d6ad7SLin Ling #define SPA_REMOVE_UNSPARE 0x01 6373f9d6ad7SLin Ling #define SPA_REMOVE_DONE 0x02 638fa9e4066Sahrens 639fa9e4066Sahrens /* device manipulation */ 640fa9e4066Sahrens extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot); 641ea8dc4b6Seschrock extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, 642fa9e4066Sahrens int replacing); 6438ad4d6ddSJeff Bonwick extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, 6448ad4d6ddSJeff Bonwick int replace_done); 64599653d4eSeschrock extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare); 6463f9d6ad7SLin Ling extern boolean_t spa_vdev_remove_active(spa_t *spa); 647c67d9675Seschrock extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath); 6486809eb4eSEric Schrock extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru); 6491195e687SMark J Musante extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, 6501195e687SMark J Musante nvlist_t *props, boolean_t exp); 651fa9e4066Sahrens 65299653d4eSeschrock /* spare state (which is global across all pools) */ 65339c23413Seschrock extern void spa_spare_add(vdev_t *vd); 65439c23413Seschrock extern void spa_spare_remove(vdev_t *vd); 65589a89ebfSlling extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt); 65639c23413Seschrock extern void spa_spare_activate(vdev_t *vd); 65799653d4eSeschrock 658fa94a07fSbrendan /* L2ARC state (which is global across all pools) */ 659fa94a07fSbrendan extern void spa_l2cache_add(vdev_t *vd); 660fa94a07fSbrendan extern void spa_l2cache_remove(vdev_t *vd); 661fa94a07fSbrendan extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool); 662fa94a07fSbrendan extern void spa_l2cache_activate(vdev_t *vd); 663fa94a07fSbrendan extern void spa_l2cache_drop(spa_t *spa); 664fa94a07fSbrendan 6653f9d6ad7SLin Ling /* scanning */ 6663f9d6ad7SLin Ling extern int spa_scan(spa_t *spa, pool_scan_func_t func); 6673f9d6ad7SLin Ling extern int spa_scan_stop(spa_t *spa); 668fa9e4066Sahrens 669fa9e4066Sahrens /* spa syncing */ 670fa9e4066Sahrens extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */ 671fa9e4066Sahrens extern void spa_sync_allpools(void); 672fa9e4066Sahrens 6733a737e0dSbrendan /* spa namespace global mutex */ 6743a737e0dSbrendan extern kmutex_t spa_namespace_lock; 6753a737e0dSbrendan 676fa9e4066Sahrens /* 677fa9e4066Sahrens * SPA configuration functions in spa_config.c 678fa9e4066Sahrens */ 6790373e76bSbonwick 6800373e76bSbonwick #define SPA_CONFIG_UPDATE_POOL 0 6810373e76bSbonwick #define SPA_CONFIG_UPDATE_VDEVS 1 6820373e76bSbonwick 683c5904d13Seschrock extern void spa_config_sync(spa_t *, boolean_t, boolean_t); 684fa9e4066Sahrens extern void spa_config_load(void); 685fa9e4066Sahrens extern nvlist_t *spa_all_configs(uint64_t *); 686fa9e4066Sahrens extern void spa_config_set(spa_t *spa, nvlist_t *config); 687fa9e4066Sahrens extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, 688fa9e4066Sahrens int getstats); 6890373e76bSbonwick extern void spa_config_update(spa_t *spa, int what); 690fa9e4066Sahrens 691fa9e4066Sahrens /* 692fa9e4066Sahrens * Miscellaneous SPA routines in spa_misc.c 693fa9e4066Sahrens */ 694fa9e4066Sahrens 695fa9e4066Sahrens /* Namespace manipulation */ 696fa9e4066Sahrens extern spa_t *spa_lookup(const char *name); 697468c413aSTim Haley extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot); 698fa9e4066Sahrens extern void spa_remove(spa_t *spa); 699fa9e4066Sahrens extern spa_t *spa_next(spa_t *prev); 700fa9e4066Sahrens 701fa9e4066Sahrens /* Refcount functions */ 702fa9e4066Sahrens extern void spa_open_ref(spa_t *spa, void *tag); 703fa9e4066Sahrens extern void spa_close(spa_t *spa, void *tag); 7040521c5ebSJustin Gibbs extern void spa_async_close(spa_t *spa, void *tag); 705fa9e4066Sahrens extern boolean_t spa_refcount_zero(spa_t *spa); 706fa9e4066Sahrens 7078f18d1faSGeorge Wilson #define SCL_NONE 0x00 708e14bb325SJeff Bonwick #define SCL_CONFIG 0x01 709e14bb325SJeff Bonwick #define SCL_STATE 0x02 710e14bb325SJeff Bonwick #define SCL_L2ARC 0x04 /* hack until L2ARC 2.0 */ 711e14bb325SJeff Bonwick #define SCL_ALLOC 0x08 712e14bb325SJeff Bonwick #define SCL_ZIO 0x10 713e14bb325SJeff Bonwick #define SCL_FREE 0x20 714e14bb325SJeff Bonwick #define SCL_VDEV 0x40 715e14bb325SJeff Bonwick #define SCL_LOCKS 7 716e14bb325SJeff Bonwick #define SCL_ALL ((1 << SCL_LOCKS) - 1) 717e14bb325SJeff Bonwick #define SCL_STATE_ALL (SCL_STATE | SCL_L2ARC | SCL_ZIO) 718e14bb325SJeff Bonwick 719e14bb325SJeff Bonwick /* Pool configuration locks */ 720e14bb325SJeff Bonwick extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw); 721e14bb325SJeff Bonwick extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw); 722e14bb325SJeff Bonwick extern void spa_config_exit(spa_t *spa, int locks, void *tag); 723e14bb325SJeff Bonwick extern int spa_config_held(spa_t *spa, int locks, krw_t rw); 724fa9e4066Sahrens 725fa9e4066Sahrens /* Pool vdev add/remove lock */ 726fa9e4066Sahrens extern uint64_t spa_vdev_enter(spa_t *spa); 72788ecc943SGeorge Wilson extern uint64_t spa_vdev_config_enter(spa_t *spa); 72888ecc943SGeorge Wilson extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, 72988ecc943SGeorge Wilson int error, char *tag); 730fa9e4066Sahrens extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error); 731fa9e4066Sahrens 732e14bb325SJeff Bonwick /* Pool vdev state change lock */ 7338f18d1faSGeorge Wilson extern void spa_vdev_state_enter(spa_t *spa, int oplock); 734e14bb325SJeff Bonwick extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error); 735e14bb325SJeff Bonwick 736b24ab676SJeff Bonwick /* Log state */ 737b24ab676SJeff Bonwick typedef enum spa_log_state { 738b24ab676SJeff Bonwick SPA_LOG_UNKNOWN = 0, /* unknown log state */ 739b24ab676SJeff Bonwick SPA_LOG_MISSING, /* missing log(s) */ 740b24ab676SJeff Bonwick SPA_LOG_CLEAR, /* clear the log(s) */ 741b24ab676SJeff Bonwick SPA_LOG_GOOD, /* log(s) are good */ 742b24ab676SJeff Bonwick } spa_log_state_t; 743b24ab676SJeff Bonwick 744b24ab676SJeff Bonwick extern spa_log_state_t spa_get_log_state(spa_t *spa); 745b24ab676SJeff Bonwick extern void spa_set_log_state(spa_t *spa, spa_log_state_t state); 7461195e687SMark J Musante extern int spa_offline_log(spa_t *spa); 747b24ab676SJeff Bonwick 748b24ab676SJeff Bonwick /* Log claim callback */ 749b24ab676SJeff Bonwick extern void spa_claim_notify(zio_t *zio); 750b24ab676SJeff Bonwick 751fa9e4066Sahrens /* Accessor functions */ 75288b7b0f2SMatthew Ahrens extern boolean_t spa_shutting_down(spa_t *spa); 753fa9e4066Sahrens extern struct dsl_pool *spa_get_dsl(spa_t *spa); 754ad135b5dSChristopher Siden extern boolean_t spa_is_initializing(spa_t *spa); 755fa9e4066Sahrens extern blkptr_t *spa_get_rootblkptr(spa_t *spa); 756fa9e4066Sahrens extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp); 757fa9e4066Sahrens extern void spa_altroot(spa_t *, char *, size_t); 758fa9e4066Sahrens extern int spa_sync_pass(spa_t *spa); 759fa9e4066Sahrens extern char *spa_name(spa_t *spa); 760fa9e4066Sahrens extern uint64_t spa_guid(spa_t *spa); 761e9103aaeSGarrett D'Amore extern uint64_t spa_load_guid(spa_t *spa); 762fa9e4066Sahrens extern uint64_t spa_last_synced_txg(spa_t *spa); 763fa9e4066Sahrens extern uint64_t spa_first_txg(spa_t *spa); 764b24ab676SJeff Bonwick extern uint64_t spa_syncing_txg(spa_t *spa); 76599653d4eSeschrock extern uint64_t spa_version(spa_t *spa); 76688b7b0f2SMatthew Ahrens extern pool_state_t spa_state(spa_t *spa); 767b16da2e2SGeorge Wilson extern spa_load_state_t spa_load_state(spa_t *spa); 768fa9e4066Sahrens extern uint64_t spa_freeze_txg(spa_t *spa); 769fa9e4066Sahrens extern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize); 770485bbbf5SGeorge Wilson extern uint64_t spa_get_dspace(spa_t *spa); 77119fc3e96SChristopher Siden extern uint64_t spa_get_slop_space(spa_t *spa); 772485bbbf5SGeorge Wilson extern void spa_update_dspace(spa_t *spa); 77344cd46caSbillm extern uint64_t spa_version(spa_t *spa); 774b24ab676SJeff Bonwick extern boolean_t spa_deflate(spa_t *spa); 775b24ab676SJeff Bonwick extern metaslab_class_t *spa_normal_class(spa_t *spa); 776b24ab676SJeff Bonwick extern metaslab_class_t *spa_log_class(spa_t *spa); 7770521c5ebSJustin Gibbs extern void spa_evicting_os_register(spa_t *, objset_t *os); 7780521c5ebSJustin Gibbs extern void spa_evicting_os_deregister(spa_t *, objset_t *os); 7790521c5ebSJustin Gibbs extern void spa_evicting_os_wait(spa_t *spa); 78044cd46caSbillm extern int spa_max_replication(spa_t *spa); 7813f9d6ad7SLin Ling extern int spa_prev_software_version(spa_t *spa); 782fa9e4066Sahrens extern int spa_busy(void); 7830a4e9518Sgw25295 extern uint8_t spa_get_failmode(spa_t *spa); 784e14bb325SJeff Bonwick extern boolean_t spa_suspended(spa_t *spa); 785b24ab676SJeff Bonwick extern uint64_t spa_bootfs(spa_t *spa); 786b24ab676SJeff Bonwick extern uint64_t spa_delegation(spa_t *spa); 787b24ab676SJeff Bonwick extern objset_t *spa_meta_objset(spa_t *spa); 788283b8460SGeorge.Wilson extern uint64_t spa_deadman_synctime(spa_t *spa); 789fa9e4066Sahrens 790fa9e4066Sahrens /* Miscellaneous support routines */ 79149064978SMax Grossman extern void spa_activate_mos_feature(spa_t *spa, const char *feature, 79249064978SMax Grossman dmu_tx_t *tx); 793ad135b5dSChristopher Siden extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature); 794fa9e4066Sahrens extern int spa_rename(const char *oldname, const char *newname); 795f9af39baSGeorge Wilson extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid); 796fa9e4066Sahrens extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid); 797fa9e4066Sahrens extern char *spa_strdup(const char *); 798fa9e4066Sahrens extern void spa_strfree(char *); 799fa9e4066Sahrens extern uint64_t spa_get_random(uint64_t range); 8001195e687SMark J Musante extern uint64_t spa_generate_guid(spa_t *spa); 80149064978SMax Grossman extern void snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp); 802fa9e4066Sahrens extern void spa_freeze(spa_t *spa); 803e9103aaeSGarrett D'Amore extern int spa_change_guid(spa_t *spa); 804990b4856Slling extern void spa_upgrade(spa_t *spa, uint64_t version); 805fa9e4066Sahrens extern void spa_evict_all(void); 806c5904d13Seschrock extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid, 807c5904d13Seschrock boolean_t l2cache); 80899653d4eSeschrock extern boolean_t spa_has_spare(spa_t *, uint64_t guid); 809b24ab676SJeff Bonwick extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva); 810b24ab676SJeff Bonwick extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp); 811b24ab676SJeff Bonwick extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp); 8126ce0521aSperrin extern boolean_t spa_has_slogs(spa_t *spa); 813bf82a41bSeschrock extern boolean_t spa_is_root(spa_t *spa); 8148ad4d6ddSJeff Bonwick extern boolean_t spa_writeable(spa_t *spa); 815fa4888e9SAlex Reece extern boolean_t spa_has_pending_synctask(spa_t *spa); 816d1a98260SMatthew Ahrens extern int spa_maxblocksize(spa_t *spa); 817124e60fbSMatthew Ahrens extern void zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp); 818468c413aSTim Haley 8198ad4d6ddSJeff Bonwick extern int spa_mode(spa_t *spa); 820ca45db41SChris Kirby extern uint64_t strtonum(const char *str, char **nptr); 821ea8dc4b6Seschrock 822ecd6cf80Smarks extern char *spa_his_ievent_table[]; 823ecd6cf80Smarks 82406eeb2adSek110237 extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx); 82506eeb2adSek110237 extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read, 82606eeb2adSek110237 char *his_buf); 8274445fffbSMatthew Ahrens extern int spa_history_log(spa_t *spa, const char *his_buf); 8284445fffbSMatthew Ahrens extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl); 8294445fffbSMatthew Ahrens extern void spa_history_log_version(spa_t *spa, const char *operation); 8304445fffbSMatthew Ahrens extern void spa_history_log_internal(spa_t *spa, const char *operation, 8314445fffbSMatthew Ahrens dmu_tx_t *tx, const char *fmt, ...); 8324445fffbSMatthew Ahrens extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op, 8334445fffbSMatthew Ahrens dmu_tx_t *tx, const char *fmt, ...); 8344445fffbSMatthew Ahrens extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation, 8354445fffbSMatthew Ahrens dmu_tx_t *tx, const char *fmt, ...); 83606eeb2adSek110237 837ea8dc4b6Seschrock /* error handling */ 83805d95d03SMatthew Ahrens struct zbookmark_phys; 839b24ab676SJeff Bonwick extern void spa_log_error(spa_t *spa, zio_t *zio); 840ea8dc4b6Seschrock extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd, 841b24ab676SJeff Bonwick zio_t *zio, uint64_t stateoroffset, uint64_t length); 8423d7072f8Seschrock extern void zfs_post_remove(spa_t *spa, vdev_t *vd); 843069f55e2SEric Schrock extern void zfs_post_state_change(spa_t *spa, vdev_t *vd); 8443d7072f8Seschrock extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd); 845ea8dc4b6Seschrock extern uint64_t spa_get_errlog_size(spa_t *spa); 846ea8dc4b6Seschrock extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count); 847ea8dc4b6Seschrock extern void spa_errlog_rotate(spa_t *spa); 848ea8dc4b6Seschrock extern void spa_errlog_drain(spa_t *spa); 849ea8dc4b6Seschrock extern void spa_errlog_sync(spa_t *spa, uint64_t txg); 850ea8dc4b6Seschrock extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub); 851fa9e4066Sahrens 85287db74c1Sek110237 /* vdev cache */ 85387db74c1Sek110237 extern void vdev_cache_stat_init(void); 85487db74c1Sek110237 extern void vdev_cache_stat_fini(void); 85587db74c1Sek110237 856fa9e4066Sahrens /* Initialization and termination */ 857fa9e4066Sahrens extern void spa_init(int flags); 858fa9e4066Sahrens extern void spa_fini(void); 859e7cbe64fSgw25295 extern void spa_boot_init(); 860fa9e4066Sahrens 861b1b8ab34Slling /* properties */ 862990b4856Slling extern int spa_prop_set(spa_t *spa, nvlist_t *nvp); 863990b4856Slling extern int spa_prop_get(spa_t *spa, nvlist_t **nvp); 864990b4856Slling extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx); 865379c004dSEric Schrock extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t); 866b1b8ab34Slling 8673d7072f8Seschrock /* asynchronous event notification */ 8683d7072f8Seschrock extern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name); 8693d7072f8Seschrock 870fa9e4066Sahrens #ifdef ZFS_DEBUG 871fa9e4066Sahrens #define dprintf_bp(bp, fmt, ...) do { \ 872fa9e4066Sahrens if (zfs_flags & ZFS_DEBUG_DPRINTF) { \ 873c0a81264Sek110237 char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP); \ 87449064978SMax Grossman snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, (bp)); \ 875fa9e4066Sahrens dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf); \ 876c0a81264Sek110237 kmem_free(__blkbuf, BP_SPRINTF_LEN); \ 877fa9e4066Sahrens } \ 878fa9e4066Sahrens _NOTE(CONSTCOND) } while (0) 879fa9e4066Sahrens #else 880fa9e4066Sahrens #define dprintf_bp(bp, fmt, ...) 881fa9e4066Sahrens #endif 882fa9e4066Sahrens 88309c9d376SGeorge Wilson extern boolean_t spa_debug_enabled(spa_t *spa); 88409c9d376SGeorge Wilson #define spa_dbgmsg(spa, ...) \ 88509c9d376SGeorge Wilson { \ 88609c9d376SGeorge Wilson if (spa_debug_enabled(spa)) \ 88709c9d376SGeorge Wilson zfs_dbgmsg(__VA_ARGS__); \ 88809c9d376SGeorge Wilson } 88909c9d376SGeorge Wilson 8908ad4d6ddSJeff Bonwick extern int spa_mode_global; /* mode, e.g. FREAD | FWRITE */ 891fa9e4066Sahrens 892fa9e4066Sahrens #ifdef __cplusplus 893fa9e4066Sahrens } 894fa9e4066Sahrens #endif 895fa9e4066Sahrens 896fa9e4066Sahrens #endif /* _SYS_SPA_H */ 897