1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2012 by Delphix. All rights reserved.
  24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  25  * Copyright 2013 Saso Kiselkov. All rights reserved.
  26  */
  27 
  28 #ifndef _SYS_SPA_H
  29 #define _SYS_SPA_H
  30 
  31 #include <sys/avl.h>
  32 #include <sys/zfs_context.h>
  33 #include <sys/nvpair.h>
  34 #include <sys/sysmacros.h>
  35 #include <sys/types.h>
  36 #include <sys/fs/zfs.h>
  37 
  38 #ifdef  __cplusplus
  39 extern "C" {
  40 #endif
  41 
  42 /*
  43  * Forward references that lots of things need.
  44  */
  45 typedef struct spa spa_t;
  46 typedef struct vdev vdev_t;
  47 typedef struct metaslab metaslab_t;
  48 typedef struct metaslab_group metaslab_group_t;
  49 typedef struct metaslab_class metaslab_class_t;
  50 typedef struct zio zio_t;
  51 typedef struct zilog zilog_t;
  52 typedef struct spa_aux_vdev spa_aux_vdev_t;
  53 typedef struct ddt ddt_t;
  54 typedef struct ddt_entry ddt_entry_t;
  55 struct dsl_pool;
  56 struct dsl_dataset;
  57 
  58 /*
  59  * General-purpose 32-bit and 64-bit bitfield encodings.
  60  */
  61 #define BF32_DECODE(x, low, len)        P2PHASE((x) >> (low), 1U << (len))
  62 #define BF64_DECODE(x, low, len)        P2PHASE((x) >> (low), 1ULL << (len))
  63 #define BF32_ENCODE(x, low, len)        (P2PHASE((x), 1U << (len)) << (low))
  64 #define BF64_ENCODE(x, low, len)        (P2PHASE((x), 1ULL << (len)) << (low))
  65 
  66 #define BF32_GET(x, low, len)           BF32_DECODE(x, low, len)
  67 #define BF64_GET(x, low, len)           BF64_DECODE(x, low, len)
  68 
  69 #define BF32_SET(x, low, len, val)      \
  70         ((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len))
  71 #define BF64_SET(x, low, len, val)      \
  72         ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len))
  73 
  74 #define BF32_GET_SB(x, low, len, shift, bias)   \
  75         ((BF32_GET(x, low, len) + (bias)) << (shift))
  76 #define BF64_GET_SB(x, low, len, shift, bias)   \
  77         ((BF64_GET(x, low, len) + (bias)) << (shift))
  78 
  79 #define BF32_SET_SB(x, low, len, shift, bias, val)      \
  80         BF32_SET(x, low, len, ((val) >> (shift)) - (bias))
  81 #define BF64_SET_SB(x, low, len, shift, bias, val)      \
  82         BF64_SET(x, low, len, ((val) >> (shift)) - (bias))
  83 
  84 /*
  85  * We currently support nine block sizes, from 512 bytes to 128K.
  86  * We could go higher, but the benefits are near-zero and the cost
  87  * of COWing a giant block to modify one byte would become excessive.
  88  */
  89 #define SPA_MINBLOCKSHIFT       9
  90 #define SPA_MAXBLOCKSHIFT       17
  91 #define SPA_MINBLOCKSIZE        (1ULL << SPA_MINBLOCKSHIFT)
  92 #define SPA_MAXBLOCKSIZE        (1ULL << SPA_MAXBLOCKSHIFT)
  93 
  94 #define SPA_BLOCKSIZES          (SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)
  95 
  96 /*
  97  * Size of block to hold the configuration data (a packed nvlist)
  98  */
  99 #define SPA_CONFIG_BLOCKSIZE    (1ULL << 14)
 100 
 101 /*
 102  * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB.
 103  * The ASIZE encoding should be at least 64 times larger (6 more bits)
 104  * to support up to 4-way RAID-Z mirror mode with worst-case gang block
 105  * overhead, three DVAs per bp, plus one more bit in case we do anything
 106  * else that expands the ASIZE.
 107  */
 108 #define SPA_LSIZEBITS           16      /* LSIZE up to 32M (2^16 * 512) */
 109 #define SPA_PSIZEBITS           16      /* PSIZE up to 32M (2^16 * 512) */
 110 #define SPA_ASIZEBITS           24      /* ASIZE up to 64 times larger  */
 111 
 112 /*
 113  * All SPA data is represented by 128-bit data virtual addresses (DVAs).
 114  * The members of the dva_t should be considered opaque outside the SPA.
 115  */
 116 typedef struct dva {
 117         uint64_t        dva_word[2];
 118 } dva_t;
 119 
 120 /*
 121  * Each block has a 256-bit checksum -- strong enough for cryptographic hashes.
 122  */
 123 typedef struct zio_cksum {
 124         uint64_t        zc_word[4];
 125 } zio_cksum_t;
 126 
 127 /*
 128  * Each block is described by its DVAs, time of birth, checksum, etc.
 129  * The word-by-word, bit-by-bit layout of the blkptr is as follows:
 130  *
 131  *      64      56      48      40      32      24      16      8       0
 132  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 133  * 0    |               vdev1           | GRID  |         ASIZE         |
 134  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 135  * 1    |G|                      offset1                                |
 136  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 137  * 2    |               vdev2           | GRID  |         ASIZE         |
 138  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 139  * 3    |G|                      offset2                                |
 140  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 141  * 4    |               vdev3           | GRID  |         ASIZE         |
 142  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 143  * 5    |G|                      offset3                                |
 144  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 145  * 6    |BDX|lvl| type  | cksum | comp  |     PSIZE     |     LSIZE     |
 146  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 147  * 7    |                       padding                                 |
 148  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 149  * 8    |                       padding                                 |
 150  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 151  * 9    |                       physical birth txg                      |
 152  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 153  * a    |                       logical birth txg                       |
 154  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 155  * b    |                       fill count                              |
 156  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 157  * c    |                       checksum[0]                             |
 158  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 159  * d    |                       checksum[1]                             |
 160  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 161  * e    |                       checksum[2]                             |
 162  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 163  * f    |                       checksum[3]                             |
 164  *      +-------+-------+-------+-------+-------+-------+-------+-------+
 165  *
 166  * Legend:
 167  *
 168  * vdev         virtual device ID
 169  * offset       offset into virtual device
 170  * LSIZE        logical size
 171  * PSIZE        physical size (after compression)
 172  * ASIZE        allocated size (including RAID-Z parity and gang block headers)
 173  * GRID         RAID-Z layout information (reserved for future use)
 174  * cksum        checksum function
 175  * comp         compression function
 176  * G            gang block indicator
 177  * B            byteorder (endianness)
 178  * D            dedup
 179  * X            unused
 180  * lvl          level of indirection
 181  * type         DMU object type
 182  * phys birth   txg of block allocation; zero if same as logical birth txg
 183  * log. birth   transaction group in which the block was logically born
 184  * fill count   number of non-zero blocks under this bp
 185  * checksum[4]  256-bit checksum of the data this bp describes
 186  */
 187 #define SPA_BLKPTRSHIFT 7               /* blkptr_t is 128 bytes        */
 188 #define SPA_DVAS_PER_BP 3               /* Number of DVAs in a bp       */
 189 
 190 typedef struct blkptr {
 191         dva_t           blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */
 192         uint64_t        blk_prop;       /* size, compression, type, etc     */
 193         uint64_t        blk_pad[2];     /* Extra space for the future       */
 194         uint64_t        blk_phys_birth; /* txg when block was allocated     */
 195         uint64_t        blk_birth;      /* transaction group at birth       */
 196         uint64_t        blk_fill;       /* fill count                       */
 197         zio_cksum_t     blk_cksum;      /* 256-bit checksum                 */
 198 } blkptr_t;
 199 
 200 /*
 201  * Macros to get and set fields in a bp or DVA.
 202  */
 203 #define DVA_GET_ASIZE(dva)      \
 204         BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0)
 205 #define DVA_SET_ASIZE(dva, x)   \
 206         BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x)
 207 
 208 #define DVA_GET_GRID(dva)       BF64_GET((dva)->dva_word[0], 24, 8)
 209 #define DVA_SET_GRID(dva, x)    BF64_SET((dva)->dva_word[0], 24, 8, x)
 210 
 211 #define DVA_GET_VDEV(dva)       BF64_GET((dva)->dva_word[0], 32, 32)
 212 #define DVA_SET_VDEV(dva, x)    BF64_SET((dva)->dva_word[0], 32, 32, x)
 213 
 214 #define DVA_GET_OFFSET(dva)     \
 215         BF64_GET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0)
 216 #define DVA_SET_OFFSET(dva, x)  \
 217         BF64_SET_SB((dva)->dva_word[1], 0, 63, SPA_MINBLOCKSHIFT, 0, x)
 218 
 219 #define DVA_GET_GANG(dva)       BF64_GET((dva)->dva_word[1], 63, 1)
 220 #define DVA_SET_GANG(dva, x)    BF64_SET((dva)->dva_word[1], 63, 1, x)
 221 
 222 #define BP_GET_LSIZE(bp)        \
 223         BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
 224 #define BP_SET_LSIZE(bp, x)     \
 225         BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
 226 
 227 #define BP_GET_PSIZE(bp)        \
 228         BF64_GET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
 229 #define BP_SET_PSIZE(bp, x)     \
 230         BF64_SET_SB((bp)->blk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
 231 
 232 #define BP_GET_COMPRESS(bp)             BF64_GET((bp)->blk_prop, 32, 8)
 233 #define BP_SET_COMPRESS(bp, x)          BF64_SET((bp)->blk_prop, 32, 8, x)
 234 
 235 #define BP_GET_CHECKSUM(bp)             BF64_GET((bp)->blk_prop, 40, 8)
 236 #define BP_SET_CHECKSUM(bp, x)          BF64_SET((bp)->blk_prop, 40, 8, x)
 237 
 238 #define BP_GET_TYPE(bp)                 BF64_GET((bp)->blk_prop, 48, 8)
 239 #define BP_SET_TYPE(bp, x)              BF64_SET((bp)->blk_prop, 48, 8, x)
 240 
 241 #define BP_GET_LEVEL(bp)                BF64_GET((bp)->blk_prop, 56, 5)
 242 #define BP_SET_LEVEL(bp, x)             BF64_SET((bp)->blk_prop, 56, 5, x)
 243 
 244 #define BP_GET_PROP_BIT_61(bp)          BF64_GET((bp)->blk_prop, 61, 1)
 245 #define BP_SET_PROP_BIT_61(bp, x)       BF64_SET((bp)->blk_prop, 61, 1, x)
 246 
 247 #define BP_GET_DEDUP(bp)                BF64_GET((bp)->blk_prop, 62, 1)
 248 #define BP_SET_DEDUP(bp, x)             BF64_SET((bp)->blk_prop, 62, 1, x)
 249 
 250 #define BP_GET_BYTEORDER(bp)            (0 - BF64_GET((bp)->blk_prop, 63, 1))
 251 #define BP_SET_BYTEORDER(bp, x)         BF64_SET((bp)->blk_prop, 63, 1, x)
 252 
 253 #define BP_PHYSICAL_BIRTH(bp)           \
 254         ((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth)
 255 
 256 #define BP_SET_BIRTH(bp, logical, physical)     \
 257 {                                               \
 258         (bp)->blk_birth = (logical);         \
 259         (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \
 260 }
 261 
 262 #define BP_GET_ASIZE(bp)        \
 263         (DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
 264                 DVA_GET_ASIZE(&(bp)->blk_dva[2]))
 265 
 266 #define BP_GET_UCSIZE(bp) \
 267         ((BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) ? \
 268         BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp))
 269 
 270 #define BP_GET_NDVAS(bp)        \
 271         (!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \
 272         !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \
 273         !!DVA_GET_ASIZE(&(bp)->blk_dva[2]))
 274 
 275 #define BP_COUNT_GANG(bp)       \
 276         (DVA_GET_GANG(&(bp)->blk_dva[0]) + \
 277         DVA_GET_GANG(&(bp)->blk_dva[1]) + \
 278         DVA_GET_GANG(&(bp)->blk_dva[2]))
 279 
 280 #define DVA_EQUAL(dva1, dva2)   \
 281         ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \
 282         (dva1)->dva_word[0] == (dva2)->dva_word[0])
 283 
 284 #define BP_EQUAL(bp1, bp2)      \
 285         (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) &&    \
 286         DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) &&      \
 287         DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) &&      \
 288         DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2]))
 289 
 290 #define ZIO_CHECKSUM_EQUAL(zc1, zc2) \
 291         (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \
 292         ((zc1).zc_word[1] - (zc2).zc_word[1]) | \
 293         ((zc1).zc_word[2] - (zc2).zc_word[2]) | \
 294         ((zc1).zc_word[3] - (zc2).zc_word[3])))
 295 
 296 #define ZIO_CHECKSUM_BSWAP(_zc) \
 297         do { \
 298                 zio_cksum_t *zc = (_zc); \
 299                 zc->zc_word[0] = BSWAP_64(zc->zc_word[0]); \
 300                 zc->zc_word[1] = BSWAP_64(zc->zc_word[1]); \
 301                 zc->zc_word[2] = BSWAP_64(zc->zc_word[2]); \
 302                 zc->zc_word[3] = BSWAP_64(zc->zc_word[3]); \
 303                 _NOTE(NOTREACHED) \
 304                 _NOTE(CONSTCOND) \
 305         } while (0)
 306 
 307 #define DVA_IS_VALID(dva)       (DVA_GET_ASIZE(dva) != 0)
 308 
 309 #define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3)   \
 310 {                                               \
 311         (zcp)->zc_word[0] = w0;                      \
 312         (zcp)->zc_word[1] = w1;                      \
 313         (zcp)->zc_word[2] = w2;                      \
 314         (zcp)->zc_word[3] = w3;                      \
 315 }
 316 
 317 #define BP_IDENTITY(bp)         (&(bp)->blk_dva[0])
 318 #define BP_IS_GANG(bp)          DVA_GET_GANG(BP_IDENTITY(bp))
 319 #define BP_IS_HOLE(bp)          ((bp)->blk_birth == 0)
 320 
 321 /* BP_IS_RAIDZ(bp) assumes no block compression */
 322 #define BP_IS_RAIDZ(bp)         (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \
 323                                 BP_GET_PSIZE(bp))
 324 
 325 #define BP_ZERO(bp)                             \
 326 {                                               \
 327         (bp)->blk_dva[0].dva_word[0] = 0;    \
 328         (bp)->blk_dva[0].dva_word[1] = 0;    \
 329         (bp)->blk_dva[1].dva_word[0] = 0;    \
 330         (bp)->blk_dva[1].dva_word[1] = 0;    \
 331         (bp)->blk_dva[2].dva_word[0] = 0;    \
 332         (bp)->blk_dva[2].dva_word[1] = 0;    \
 333         (bp)->blk_prop = 0;                  \
 334         (bp)->blk_pad[0] = 0;                        \
 335         (bp)->blk_pad[1] = 0;                        \
 336         (bp)->blk_phys_birth = 0;            \
 337         (bp)->blk_birth = 0;                 \
 338         (bp)->blk_fill = 0;                  \
 339         ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0);  \
 340 }
 341 
 342 /*
 343  * Note: the byteorder is either 0 or -1, both of which are palindromes.
 344  * This simplifies the endianness handling a bit.
 345  */
 346 #ifdef _BIG_ENDIAN
 347 #define ZFS_HOST_BYTEORDER      (0ULL)
 348 #else
 349 #define ZFS_HOST_BYTEORDER      (-1ULL)
 350 #endif
 351 
 352 #define BP_SHOULD_BYTESWAP(bp)  (BP_GET_BYTEORDER(bp) != ZFS_HOST_BYTEORDER)
 353 
 354 #define BP_SPRINTF_LEN  320
 355 
 356 /*
 357  * This macro allows code sharing between zfs, libzpool, and mdb.
 358  * 'func' is either snprintf() or mdb_snprintf().
 359  * 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
 360  */
 361 #define SPRINTF_BLKPTR(func, ws, buf, bp, type, checksum, compress)     \
 362 {                                                                       \
 363         static const char *copyname[] =                                 \
 364             { "zero", "single", "double", "triple" };                   \
 365         int size = BP_SPRINTF_LEN;                                      \
 366         int len = 0;                                                    \
 367         int copies = 0;                                                 \
 368                                                                         \
 369         if (bp == NULL) {                                               \
 370                 len = func(buf + len, size - len, "<NULL>");              \
 371         } else if (BP_IS_HOLE(bp)) {                                    \
 372                 len = func(buf + len, size - len, "<hole>");              \
 373         } else {                                                        \
 374                 for (int d = 0; d < BP_GET_NDVAS(bp); d++) {         \
 375                         const dva_t *dva = &bp->blk_dva[d];              \
 376                         if (DVA_IS_VALID(dva))                          \
 377                                 copies++;                               \
 378                         len += func(buf + len, size - len,              \
 379                             "DVA[%d]=<%llu:%llx:%llx>%c", d,              \
 380                             (u_longlong_t)DVA_GET_VDEV(dva),            \
 381                             (u_longlong_t)DVA_GET_OFFSET(dva),          \
 382                             (u_longlong_t)DVA_GET_ASIZE(dva),           \
 383                             ws);                                        \
 384                 }                                                       \
 385                 if (BP_IS_GANG(bp) &&                                   \
 386                     DVA_GET_ASIZE(&bp->blk_dva[2]) <=                 \
 387                     DVA_GET_ASIZE(&bp->blk_dva[1]) / 2)                  \
 388                         copies--;                                       \
 389                 len += func(buf + len, size - len,                      \
 390                     "[L%llu %s] %s %s %s %s %s %s%c"                    \
 391                     "size=%llxL/%llxP birth=%lluL/%lluP fill=%llu%c"    \
 392                     "cksum=%llx:%llx:%llx:%llx",                        \
 393                     (u_longlong_t)BP_GET_LEVEL(bp),                     \
 394                     type,                                               \
 395                     checksum,                                           \
 396                     compress,                                           \
 397                     BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",            \
 398                     BP_IS_GANG(bp) ? "gang" : "contiguous",             \
 399                     BP_GET_DEDUP(bp) ? "dedup" : "unique",              \
 400                     copyname[copies],                                   \
 401                     ws,                                                 \
 402                     (u_longlong_t)BP_GET_LSIZE(bp),                     \
 403                     (u_longlong_t)BP_GET_PSIZE(bp),                     \
 404                     (u_longlong_t)bp->blk_birth,                     \
 405                     (u_longlong_t)BP_PHYSICAL_BIRTH(bp),                \
 406                     (u_longlong_t)bp->blk_fill,                              \
 407                     ws,                                                 \
 408                     (u_longlong_t)bp->blk_cksum.zc_word[0],          \
 409                     (u_longlong_t)bp->blk_cksum.zc_word[1],          \
 410                     (u_longlong_t)bp->blk_cksum.zc_word[2],          \
 411                     (u_longlong_t)bp->blk_cksum.zc_word[3]);         \
 412         }                                                               \
 413         ASSERT(len < size);                                          \
 414 }
 415 
 416 #include <sys/dmu.h>
 417 
 418 #define BP_GET_BUFC_TYPE(bp)                                            \
 419         (((BP_GET_LEVEL(bp) > 0) || (DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))) ? \
 420         ARC_BUFC_METADATA : ARC_BUFC_DATA)
 421 
 422 typedef enum spa_import_type {
 423         SPA_IMPORT_EXISTING,
 424         SPA_IMPORT_ASSEMBLE
 425 } spa_import_type_t;
 426 
 427 /* state manipulation functions */
 428 extern int spa_open(const char *pool, spa_t **, void *tag);
 429 extern int spa_open_rewind(const char *pool, spa_t **, void *tag,
 430     nvlist_t *policy, nvlist_t **config);
 431 extern int spa_get_stats(const char *pool, nvlist_t **config, char *altroot,
 432     size_t buflen);
 433 extern int spa_create(const char *pool, nvlist_t *config, nvlist_t *props,
 434     nvlist_t *zplprops);
 435 extern int spa_import_rootpool(char *devpath, char *devid);
 436 extern int spa_import(const char *pool, nvlist_t *config, nvlist_t *props,
 437     uint64_t flags);
 438 extern nvlist_t *spa_tryimport(nvlist_t *tryconfig);
 439 extern int spa_destroy(char *pool);
 440 extern int spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
 441     boolean_t hardforce);
 442 extern int spa_reset(char *pool);
 443 extern void spa_async_request(spa_t *spa, int flag);
 444 extern void spa_async_unrequest(spa_t *spa, int flag);
 445 extern void spa_async_suspend(spa_t *spa);
 446 extern void spa_async_resume(spa_t *spa);
 447 extern spa_t *spa_inject_addref(char *pool);
 448 extern void spa_inject_delref(spa_t *spa);
 449 extern void spa_scan_stat_init(spa_t *spa);
 450 extern int spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps);
 451 
 452 #define SPA_ASYNC_CONFIG_UPDATE 0x01
 453 #define SPA_ASYNC_REMOVE        0x02
 454 #define SPA_ASYNC_PROBE         0x04
 455 #define SPA_ASYNC_RESILVER_DONE 0x08
 456 #define SPA_ASYNC_RESILVER      0x10
 457 #define SPA_ASYNC_AUTOEXPAND    0x20
 458 #define SPA_ASYNC_REMOVE_DONE   0x40
 459 #define SPA_ASYNC_REMOVE_STOP   0x80
 460 
 461 /*
 462  * Controls the behavior of spa_vdev_remove().
 463  */
 464 #define SPA_REMOVE_UNSPARE      0x01
 465 #define SPA_REMOVE_DONE         0x02
 466 
 467 /* device manipulation */
 468 extern int spa_vdev_add(spa_t *spa, nvlist_t *nvroot);
 469 extern int spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot,
 470     int replacing);
 471 extern int spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid,
 472     int replace_done);
 473 extern int spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare);
 474 extern boolean_t spa_vdev_remove_active(spa_t *spa);
 475 extern int spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath);
 476 extern int spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru);
 477 extern int spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
 478     nvlist_t *props, boolean_t exp);
 479 
 480 /* spare state (which is global across all pools) */
 481 extern void spa_spare_add(vdev_t *vd);
 482 extern void spa_spare_remove(vdev_t *vd);
 483 extern boolean_t spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt);
 484 extern void spa_spare_activate(vdev_t *vd);
 485 
 486 /* L2ARC state (which is global across all pools) */
 487 extern void spa_l2cache_add(vdev_t *vd);
 488 extern void spa_l2cache_remove(vdev_t *vd);
 489 extern boolean_t spa_l2cache_exists(uint64_t guid, uint64_t *pool);
 490 extern void spa_l2cache_activate(vdev_t *vd);
 491 extern void spa_l2cache_drop(spa_t *spa);
 492 
 493 /* scanning */
 494 extern int spa_scan(spa_t *spa, pool_scan_func_t func);
 495 extern int spa_scan_stop(spa_t *spa);
 496 
 497 /* spa syncing */
 498 extern void spa_sync(spa_t *spa, uint64_t txg); /* only for DMU use */
 499 extern void spa_sync_allpools(void);
 500 
 501 /* spa namespace global mutex */
 502 extern kmutex_t spa_namespace_lock;
 503 
 504 /*
 505  * SPA configuration functions in spa_config.c
 506  */
 507 
 508 #define SPA_CONFIG_UPDATE_POOL  0
 509 #define SPA_CONFIG_UPDATE_VDEVS 1
 510 
 511 extern void spa_config_sync(spa_t *, boolean_t, boolean_t);
 512 extern void spa_config_load(void);
 513 extern nvlist_t *spa_all_configs(uint64_t *);
 514 extern void spa_config_set(spa_t *spa, nvlist_t *config);
 515 extern nvlist_t *spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg,
 516     int getstats);
 517 extern void spa_config_update(spa_t *spa, int what);
 518 
 519 /*
 520  * Miscellaneous SPA routines in spa_misc.c
 521  */
 522 
 523 /* Namespace manipulation */
 524 extern spa_t *spa_lookup(const char *name);
 525 extern spa_t *spa_add(const char *name, nvlist_t *config, const char *altroot);
 526 extern void spa_remove(spa_t *spa);
 527 extern spa_t *spa_next(spa_t *prev);
 528 
 529 /* Refcount functions */
 530 extern void spa_open_ref(spa_t *spa, void *tag);
 531 extern void spa_close(spa_t *spa, void *tag);
 532 extern boolean_t spa_refcount_zero(spa_t *spa);
 533 
 534 #define SCL_NONE        0x00
 535 #define SCL_CONFIG      0x01
 536 #define SCL_STATE       0x02
 537 #define SCL_L2ARC       0x04            /* hack until L2ARC 2.0 */
 538 #define SCL_ALLOC       0x08
 539 #define SCL_ZIO         0x10
 540 #define SCL_FREE        0x20
 541 #define SCL_VDEV        0x40
 542 #define SCL_LOCKS       7
 543 #define SCL_ALL         ((1 << SCL_LOCKS) - 1)
 544 #define SCL_STATE_ALL   (SCL_STATE | SCL_L2ARC | SCL_ZIO)
 545 
 546 /* Pool configuration locks */
 547 extern int spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw);
 548 extern void spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw);
 549 extern void spa_config_exit(spa_t *spa, int locks, void *tag);
 550 extern int spa_config_held(spa_t *spa, int locks, krw_t rw);
 551 
 552 /* Pool vdev add/remove lock */
 553 extern uint64_t spa_vdev_enter(spa_t *spa);
 554 extern uint64_t spa_vdev_config_enter(spa_t *spa);
 555 extern void spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg,
 556     int error, char *tag);
 557 extern int spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error);
 558 
 559 /* Pool vdev state change lock */
 560 extern void spa_vdev_state_enter(spa_t *spa, int oplock);
 561 extern int spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error);
 562 
 563 /* Log state */
 564 typedef enum spa_log_state {
 565         SPA_LOG_UNKNOWN = 0,    /* unknown log state */
 566         SPA_LOG_MISSING,        /* missing log(s) */
 567         SPA_LOG_CLEAR,          /* clear the log(s) */
 568         SPA_LOG_GOOD,           /* log(s) are good */
 569 } spa_log_state_t;
 570 
 571 extern spa_log_state_t spa_get_log_state(spa_t *spa);
 572 extern void spa_set_log_state(spa_t *spa, spa_log_state_t state);
 573 extern int spa_offline_log(spa_t *spa);
 574 
 575 /* Log claim callback */
 576 extern void spa_claim_notify(zio_t *zio);
 577 
 578 /* Accessor functions */
 579 extern boolean_t spa_shutting_down(spa_t *spa);
 580 extern struct dsl_pool *spa_get_dsl(spa_t *spa);
 581 extern boolean_t spa_is_initializing(spa_t *spa);
 582 extern blkptr_t *spa_get_rootblkptr(spa_t *spa);
 583 extern void spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp);
 584 extern void spa_altroot(spa_t *, char *, size_t);
 585 extern int spa_sync_pass(spa_t *spa);
 586 extern char *spa_name(spa_t *spa);
 587 extern uint64_t spa_guid(spa_t *spa);
 588 extern uint64_t spa_load_guid(spa_t *spa);
 589 extern uint64_t spa_last_synced_txg(spa_t *spa);
 590 extern uint64_t spa_first_txg(spa_t *spa);
 591 extern uint64_t spa_syncing_txg(spa_t *spa);
 592 extern uint64_t spa_version(spa_t *spa);
 593 extern pool_state_t spa_state(spa_t *spa);
 594 extern spa_load_state_t spa_load_state(spa_t *spa);
 595 extern uint64_t spa_freeze_txg(spa_t *spa);
 596 extern uint64_t spa_get_asize(spa_t *spa, uint64_t lsize);
 597 extern uint64_t spa_get_dspace(spa_t *spa);
 598 extern void spa_update_dspace(spa_t *spa);
 599 extern uint64_t spa_version(spa_t *spa);
 600 extern boolean_t spa_deflate(spa_t *spa);
 601 extern metaslab_class_t *spa_normal_class(spa_t *spa);
 602 extern metaslab_class_t *spa_log_class(spa_t *spa);
 603 extern int spa_max_replication(spa_t *spa);
 604 extern int spa_prev_software_version(spa_t *spa);
 605 extern int spa_busy(void);
 606 extern uint8_t spa_get_failmode(spa_t *spa);
 607 extern boolean_t spa_suspended(spa_t *spa);
 608 extern uint64_t spa_bootfs(spa_t *spa);
 609 extern uint64_t spa_delegation(spa_t *spa);
 610 extern objset_t *spa_meta_objset(spa_t *spa);
 611 extern uint64_t spa_deadman_synctime(spa_t *spa);
 612 
 613 /* Miscellaneous support routines */
 614 extern void spa_activate_mos_feature(spa_t *spa, const char *feature);
 615 extern void spa_deactivate_mos_feature(spa_t *spa, const char *feature);
 616 extern int spa_rename(const char *oldname, const char *newname);
 617 extern spa_t *spa_by_guid(uint64_t pool_guid, uint64_t device_guid);
 618 extern boolean_t spa_guid_exists(uint64_t pool_guid, uint64_t device_guid);
 619 extern char *spa_strdup(const char *);
 620 extern void spa_strfree(char *);
 621 extern uint64_t spa_get_random(uint64_t range);
 622 extern uint64_t spa_generate_guid(spa_t *spa);
 623 extern void sprintf_blkptr(char *buf, const blkptr_t *bp);
 624 extern void spa_freeze(spa_t *spa);
 625 extern int spa_change_guid(spa_t *spa);
 626 extern void spa_upgrade(spa_t *spa, uint64_t version);
 627 extern void spa_evict_all(void);
 628 extern vdev_t *spa_lookup_by_guid(spa_t *spa, uint64_t guid,
 629     boolean_t l2cache);
 630 extern boolean_t spa_has_spare(spa_t *, uint64_t guid);
 631 extern uint64_t dva_get_dsize_sync(spa_t *spa, const dva_t *dva);
 632 extern uint64_t bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp);
 633 extern uint64_t bp_get_dsize(spa_t *spa, const blkptr_t *bp);
 634 extern boolean_t spa_has_slogs(spa_t *spa);
 635 extern boolean_t spa_is_root(spa_t *spa);
 636 extern boolean_t spa_writeable(spa_t *spa);
 637 
 638 extern int spa_mode(spa_t *spa);
 639 extern uint64_t strtonum(const char *str, char **nptr);
 640 
 641 extern char *spa_his_ievent_table[];
 642 
 643 extern void spa_history_create_obj(spa_t *spa, dmu_tx_t *tx);
 644 extern int spa_history_get(spa_t *spa, uint64_t *offset, uint64_t *len_read,
 645     char *his_buf);
 646 extern int spa_history_log(spa_t *spa, const char *his_buf);
 647 extern int spa_history_log_nvl(spa_t *spa, nvlist_t *nvl);
 648 extern void spa_history_log_version(spa_t *spa, const char *operation);
 649 extern void spa_history_log_internal(spa_t *spa, const char *operation,
 650     dmu_tx_t *tx, const char *fmt, ...);
 651 extern void spa_history_log_internal_ds(struct dsl_dataset *ds, const char *op,
 652     dmu_tx_t *tx, const char *fmt, ...);
 653 extern void spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
 654     dmu_tx_t *tx, const char *fmt, ...);
 655 
 656 /* error handling */
 657 struct zbookmark;
 658 extern void spa_log_error(spa_t *spa, zio_t *zio);
 659 extern void zfs_ereport_post(const char *class, spa_t *spa, vdev_t *vd,
 660     zio_t *zio, uint64_t stateoroffset, uint64_t length);
 661 extern void zfs_post_remove(spa_t *spa, vdev_t *vd);
 662 extern void zfs_post_state_change(spa_t *spa, vdev_t *vd);
 663 extern void zfs_post_autoreplace(spa_t *spa, vdev_t *vd);
 664 extern uint64_t spa_get_errlog_size(spa_t *spa);
 665 extern int spa_get_errlog(spa_t *spa, void *uaddr, size_t *count);
 666 extern void spa_errlog_rotate(spa_t *spa);
 667 extern void spa_errlog_drain(spa_t *spa);
 668 extern void spa_errlog_sync(spa_t *spa, uint64_t txg);
 669 extern void spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub);
 670 
 671 /* vdev cache */
 672 extern void vdev_cache_stat_init(void);
 673 extern void vdev_cache_stat_fini(void);
 674 
 675 /* Initialization and termination */
 676 extern void spa_init(int flags);
 677 extern void spa_fini(void);
 678 extern void spa_boot_init();
 679 
 680 /* properties */
 681 extern int spa_prop_set(spa_t *spa, nvlist_t *nvp);
 682 extern int spa_prop_get(spa_t *spa, nvlist_t **nvp);
 683 extern void spa_prop_clear_bootfs(spa_t *spa, uint64_t obj, dmu_tx_t *tx);
 684 extern void spa_configfile_set(spa_t *, nvlist_t *, boolean_t);
 685 
 686 /* asynchronous event notification */
 687 extern void spa_event_notify(spa_t *spa, vdev_t *vdev, const char *name);
 688 
 689 #ifdef ZFS_DEBUG
 690 #define dprintf_bp(bp, fmt, ...) do {                           \
 691         if (zfs_flags & ZFS_DEBUG_DPRINTF) {                        \
 692         char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP);  \
 693         sprintf_blkptr(__blkbuf, (bp));                         \
 694         dprintf(fmt " %s\n", __VA_ARGS__, __blkbuf);            \
 695         kmem_free(__blkbuf, BP_SPRINTF_LEN);                    \
 696         } \
 697 _NOTE(CONSTCOND) } while (0)
 698 #else
 699 #define dprintf_bp(bp, fmt, ...)
 700 #endif
 701 
 702 extern boolean_t spa_debug_enabled(spa_t *spa);
 703 #define spa_dbgmsg(spa, ...)                    \
 704 {                                               \
 705         if (spa_debug_enabled(spa))             \
 706                 zfs_dbgmsg(__VA_ARGS__);        \
 707 }
 708 
 709 extern int spa_mode_global;                     /* mode, e.g. FREAD | FWRITE */
 710 
 711 #ifdef  __cplusplus
 712 }
 713 #endif
 714 
 715 #endif  /* _SYS_SPA_H */