1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
  24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  25  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
  26  */
  27 
  28 #include <sys/zfs_context.h>
  29 #include <sys/spa_impl.h>
  30 #include <sys/spa_boot.h>
  31 #include <sys/zio.h>
  32 #include <sys/zio_checksum.h>
  33 #include <sys/zio_compress.h>
  34 #include <sys/dmu.h>
  35 #include <sys/dmu_tx.h>
  36 #include <sys/zap.h>
  37 #include <sys/zil.h>
  38 #include <sys/vdev_impl.h>
  39 #include <sys/metaslab.h>
  40 #include <sys/uberblock_impl.h>
  41 #include <sys/txg.h>
  42 #include <sys/avl.h>
  43 #include <sys/unique.h>
  44 #include <sys/dsl_pool.h>
  45 #include <sys/dsl_dir.h>
  46 #include <sys/dsl_prop.h>
  47 #include <sys/dsl_scan.h>
  48 #include <sys/fs/zfs.h>
  49 #include <sys/metaslab_impl.h>
  50 #include <sys/arc.h>
  51 #include <sys/ddt.h>
  52 #include "zfs_prop.h"
  53 #include "zfeature_common.h"
  54 
  55 /*
  56  * SPA locking
  57  *
  58  * There are four basic locks for managing spa_t structures:
  59  *
  60  * spa_namespace_lock (global mutex)
  61  *
  62  *      This lock must be acquired to do any of the following:
  63  *
  64  *              - Lookup a spa_t by name
  65  *              - Add or remove a spa_t from the namespace
  66  *              - Increase spa_refcount from non-zero
  67  *              - Check if spa_refcount is zero
  68  *              - Rename a spa_t
  69  *              - add/remove/attach/detach devices
  70  *              - Held for the duration of create/destroy/import/export
  71  *
  72  *      It does not need to handle recursion.  A create or destroy may
  73  *      reference objects (files or zvols) in other pools, but by
  74  *      definition they must have an existing reference, and will never need
  75  *      to lookup a spa_t by name.
  76  *
  77  * spa_refcount (per-spa refcount_t protected by mutex)
  78  *
  79  *      This reference count keep track of any active users of the spa_t.  The
  80  *      spa_t cannot be destroyed or freed while this is non-zero.  Internally,
  81  *      the refcount is never really 'zero' - opening a pool implicitly keeps
  82  *      some references in the DMU.  Internally we check against spa_minref, but
  83  *      present the image of a zero/non-zero value to consumers.
  84  *
  85  * spa_config_lock[] (per-spa array of rwlocks)
  86  *
  87  *      This protects the spa_t from config changes, and must be held in
  88  *      the following circumstances:
  89  *
  90  *              - RW_READER to perform I/O to the spa
  91  *              - RW_WRITER to change the vdev config
  92  *
  93  * The locking order is fairly straightforward:
  94  *
  95  *              spa_namespace_lock      ->   spa_refcount
  96  *
  97  *      The namespace lock must be acquired to increase the refcount from 0
  98  *      or to check if it is zero.
  99  *
 100  *              spa_refcount            ->   spa_config_lock[]
 101  *
 102  *      There must be at least one valid reference on the spa_t to acquire
 103  *      the config lock.
 104  *
 105  *              spa_namespace_lock      ->   spa_config_lock[]
 106  *
 107  *      The namespace lock must always be taken before the config lock.
 108  *
 109  *
 110  * The spa_namespace_lock can be acquired directly and is globally visible.
 111  *
 112  * The namespace is manipulated using the following functions, all of which
 113  * require the spa_namespace_lock to be held.
 114  *
 115  *      spa_lookup()            Lookup a spa_t by name.
 116  *
 117  *      spa_add()               Create a new spa_t in the namespace.
 118  *
 119  *      spa_remove()            Remove a spa_t from the namespace.  This also
 120  *                              frees up any memory associated with the spa_t.
 121  *
 122  *      spa_next()              Returns the next spa_t in the system, or the
 123  *                              first if NULL is passed.
 124  *
 125  *      spa_evict_all()         Shutdown and remove all spa_t structures in
 126  *                              the system.
 127  *
 128  *      spa_guid_exists()       Determine whether a pool/device guid exists.
 129  *
 130  * The spa_refcount is manipulated using the following functions:
 131  *
 132  *      spa_open_ref()          Adds a reference to the given spa_t.  Must be
 133  *                              called with spa_namespace_lock held if the
 134  *                              refcount is currently zero.
 135  *
 136  *      spa_close()             Remove a reference from the spa_t.  This will
 137  *                              not free the spa_t or remove it from the
 138  *                              namespace.  No locking is required.
 139  *
 140  *      spa_refcount_zero()     Returns true if the refcount is currently
 141  *                              zero.  Must be called with spa_namespace_lock
 142  *                              held.
 143  *
 144  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
 145  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
 146  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
 147  *
 148  * To read the configuration, it suffices to hold one of these locks as reader.
 149  * To modify the configuration, you must hold all locks as writer.  To modify
 150  * vdev state without altering the vdev tree's topology (e.g. online/offline),
 151  * you must hold SCL_STATE and SCL_ZIO as writer.
 152  *
 153  * We use these distinct config locks to avoid recursive lock entry.
 154  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
 155  * block allocations (SCL_ALLOC), which may require reading space maps
 156  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
 157  *
 158  * The spa config locks cannot be normal rwlocks because we need the
 159  * ability to hand off ownership.  For example, SCL_ZIO is acquired
 160  * by the issuing thread and later released by an interrupt thread.
 161  * They do, however, obey the usual write-wanted semantics to prevent
 162  * writer (i.e. system administrator) starvation.
 163  *
 164  * The lock acquisition rules are as follows:
 165  *
 166  * SCL_CONFIG
 167  *      Protects changes to the vdev tree topology, such as vdev
 168  *      add/remove/attach/detach.  Protects the dirty config list
 169  *      (spa_config_dirty_list) and the set of spares and l2arc devices.
 170  *
 171  * SCL_STATE
 172  *      Protects changes to pool state and vdev state, such as vdev
 173  *      online/offline/fault/degrade/clear.  Protects the dirty state list
 174  *      (spa_state_dirty_list) and global pool state (spa_state).
 175  *
 176  * SCL_ALLOC
 177  *      Protects changes to metaslab groups and classes.
 178  *      Held as reader by metaslab_alloc() and metaslab_claim().
 179  *
 180  * SCL_ZIO
 181  *      Held by bp-level zios (those which have no io_vd upon entry)
 182  *      to prevent changes to the vdev tree.  The bp-level zio implicitly
 183  *      protects all of its vdev child zios, which do not hold SCL_ZIO.
 184  *
 185  * SCL_FREE
 186  *      Protects changes to metaslab groups and classes.
 187  *      Held as reader by metaslab_free().  SCL_FREE is distinct from
 188  *      SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
 189  *      blocks in zio_done() while another i/o that holds either
 190  *      SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
 191  *
 192  * SCL_VDEV
 193  *      Held as reader to prevent changes to the vdev tree during trivial
 194  *      inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
 195  *      other locks, and lower than all of them, to ensure that it's safe
 196  *      to acquire regardless of caller context.
 197  *
 198  * In addition, the following rules apply:
 199  *
 200  * (a)  spa_props_lock protects pool properties, spa_config and spa_config_list.
 201  *      The lock ordering is SCL_CONFIG > spa_props_lock.
 202  *
 203  * (b)  I/O operations on leaf vdevs.  For any zio operation that takes
 204  *      an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
 205  *      or zio_write_phys() -- the caller must ensure that the config cannot
 206  *      cannot change in the interim, and that the vdev cannot be reopened.
 207  *      SCL_STATE as reader suffices for both.
 208  *
 209  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
 210  *
 211  *      spa_vdev_enter()        Acquire the namespace lock and the config lock
 212  *                              for writing.
 213  *
 214  *      spa_vdev_exit()         Release the config lock, wait for all I/O
 215  *                              to complete, sync the updated configs to the
 216  *                              cache, and release the namespace lock.
 217  *
 218  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
 219  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
 220  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
 221  *
 222  * spa_rename() is also implemented within this file since it requires
 223  * manipulation of the namespace.
 224  */
 225 
 226 static avl_tree_t spa_namespace_avl;
 227 kmutex_t spa_namespace_lock;
 228 static kcondvar_t spa_namespace_cv;
 229 static int spa_active_count;
 230 int spa_max_replication_override = SPA_DVAS_PER_BP;
 231 
 232 static kmutex_t spa_spare_lock;
 233 static avl_tree_t spa_spare_avl;
 234 static kmutex_t spa_l2cache_lock;
 235 static avl_tree_t spa_l2cache_avl;
 236 
 237 kmem_cache_t *spa_buffer_pool;
 238 int spa_mode_global;
 239 
 240 #ifdef ZFS_DEBUG
 241 /* Everything except dprintf and spa is on by default in debug builds */
 242 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
 243 #else
 244 int zfs_flags = 0;
 245 #endif
 246 
 247 /*
 248  * zfs_recover can be set to nonzero to attempt to recover from
 249  * otherwise-fatal errors, typically caused by on-disk corruption.  When
 250  * set, calls to zfs_panic_recover() will turn into warning messages.
 251  * This should only be used as a last resort, as it typically results
 252  * in leaked space, or worse.
 253  */
 254 boolean_t zfs_recover = B_FALSE;
 255 
 256 /*
 257  * If destroy encounters an EIO while reading metadata (e.g. indirect
 258  * blocks), space referenced by the missing metadata can not be freed.
 259  * Normally this causes the background destroy to become "stalled", as
 260  * it is unable to make forward progress.  While in this stalled state,
 261  * all remaining space to free from the error-encountering filesystem is
 262  * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
 263  * permanently leak the space from indirect blocks that can not be read,
 264  * and continue to free everything else that it can.
 265  *
 266  * The default, "stalling" behavior is useful if the storage partially
 267  * fails (i.e. some but not all i/os fail), and then later recovers.  In
 268  * this case, we will be able to continue pool operations while it is
 269  * partially failed, and when it recovers, we can continue to free the
 270  * space, with no leaks.  However, note that this case is actually
 271  * fairly rare.
 272  *
 273  * Typically pools either (a) fail completely (but perhaps temporarily,
 274  * e.g. a top-level vdev going offline), or (b) have localized,
 275  * permanent errors (e.g. disk returns the wrong data due to bit flip or
 276  * firmware bug).  In case (a), this setting does not matter because the
 277  * pool will be suspended and the sync thread will not be able to make
 278  * forward progress regardless.  In case (b), because the error is
 279  * permanent, the best we can do is leak the minimum amount of space,
 280  * which is what setting this flag will do.  Therefore, it is reasonable
 281  * for this flag to normally be set, but we chose the more conservative
 282  * approach of not setting it, so that there is no possibility of
 283  * leaking space in the "partial temporary" failure case.
 284  */
 285 boolean_t zfs_free_leak_on_eio = B_FALSE;
 286 
 287 /*
 288  * Expiration time in milliseconds. This value has two meanings. First it is
 289  * used to determine when the spa_deadman() logic should fire. By default the
 290  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
 291  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
 292  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
 293  * in a system panic.
 294  */
 295 uint64_t zfs_deadman_synctime_ms = 1000000ULL;
 296 
 297 /*
 298  * Check time in milliseconds. This defines the frequency at which we check
 299  * for hung I/O.
 300  */
 301 uint64_t zfs_deadman_checktime_ms = 5000ULL;
 302 
 303 /*
 304  * Override the zfs deadman behavior via /etc/system. By default the
 305  * deadman is enabled except on VMware and sparc deployments.
 306  */
 307 int zfs_deadman_enabled = -1;
 308 
 309 /*
 310  * The worst case is single-sector max-parity RAID-Z blocks, in which
 311  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
 312  * times the size; so just assume that.  Add to this the fact that
 313  * we can have up to 3 DVAs per bp, and one more factor of 2 because
 314  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
 315  * the worst case is:
 316  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
 317  */
 318 int spa_asize_inflation = 24;
 319 
 320 /*
 321  * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
 322  * the pool to be consumed.  This ensures that we don't run the pool
 323  * completely out of space, due to unaccounted changes (e.g. to the MOS).
 324  * It also limits the worst-case time to allocate space.  If we have
 325  * less than this amount of free space, most ZPL operations (e.g. write,
 326  * create) will return ENOSPC.
 327  *
 328  * Certain operations (e.g. file removal, most administrative actions) can
 329  * use half the slop space.  They will only return ENOSPC if less than half
 330  * the slop space is free.  Typically, once the pool has less than the slop
 331  * space free, the user will use these operations to free up space in the pool.
 332  * These are the operations that call dsl_pool_adjustedsize() with the netfree
 333  * argument set to TRUE.
 334  *
 335  * A very restricted set of operations are always permitted, regardless of
 336  * the amount of free space.  These are the operations that call
 337  * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy".  If these
 338  * operations result in a net increase in the amount of space used,
 339  * it is possible to run the pool completely out of space, causing it to
 340  * be permanently read-only.
 341  *
 342  * See also the comments in zfs_space_check_t.
 343  */
 344 int spa_slop_shift = 5;
 345 
 346 /*
 347  * ==========================================================================
 348  * SPA config locking
 349  * ==========================================================================
 350  */
 351 static void
 352 spa_config_lock_init(spa_t *spa)
 353 {
 354         for (int i = 0; i < SCL_LOCKS; i++) {
 355                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
 356                 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
 357                 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
 358                 refcount_create_untracked(&scl->scl_count);
 359                 scl->scl_writer = NULL;
 360                 scl->scl_write_wanted = 0;
 361         }
 362 }
 363 
 364 static void
 365 spa_config_lock_destroy(spa_t *spa)
 366 {
 367         for (int i = 0; i < SCL_LOCKS; i++) {
 368                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
 369                 mutex_destroy(&scl->scl_lock);
 370                 cv_destroy(&scl->scl_cv);
 371                 refcount_destroy(&scl->scl_count);
 372                 ASSERT(scl->scl_writer == NULL);
 373                 ASSERT(scl->scl_write_wanted == 0);
 374         }
 375 }
 376 
 377 int
 378 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
 379 {
 380         for (int i = 0; i < SCL_LOCKS; i++) {
 381                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
 382                 if (!(locks & (1 << i)))
 383                         continue;
 384                 mutex_enter(&scl->scl_lock);
 385                 if (rw == RW_READER) {
 386                         if (scl->scl_writer || scl->scl_write_wanted) {
 387                                 mutex_exit(&scl->scl_lock);
 388                                 spa_config_exit(spa, locks ^ (1 << i), tag);
 389                                 return (0);
 390                         }
 391                 } else {
 392                         ASSERT(scl->scl_writer != curthread);
 393                         if (!refcount_is_zero(&scl->scl_count)) {
 394                                 mutex_exit(&scl->scl_lock);
 395                                 spa_config_exit(spa, locks ^ (1 << i), tag);
 396                                 return (0);
 397                         }
 398                         scl->scl_writer = curthread;
 399                 }
 400                 (void) refcount_add(&scl->scl_count, tag);
 401                 mutex_exit(&scl->scl_lock);
 402         }
 403         return (1);
 404 }
 405 
 406 void
 407 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
 408 {
 409         int wlocks_held = 0;
 410 
 411         ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
 412 
 413         for (int i = 0; i < SCL_LOCKS; i++) {
 414                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
 415                 if (scl->scl_writer == curthread)
 416                         wlocks_held |= (1 << i);
 417                 if (!(locks & (1 << i)))
 418                         continue;
 419                 mutex_enter(&scl->scl_lock);
 420                 if (rw == RW_READER) {
 421                         while (scl->scl_writer || scl->scl_write_wanted) {
 422                                 cv_wait(&scl->scl_cv, &scl->scl_lock);
 423                         }
 424                 } else {
 425                         ASSERT(scl->scl_writer != curthread);
 426                         while (!refcount_is_zero(&scl->scl_count)) {
 427                                 scl->scl_write_wanted++;
 428                                 cv_wait(&scl->scl_cv, &scl->scl_lock);
 429                                 scl->scl_write_wanted--;
 430                         }
 431                         scl->scl_writer = curthread;
 432                 }
 433                 (void) refcount_add(&scl->scl_count, tag);
 434                 mutex_exit(&scl->scl_lock);
 435         }
 436         ASSERT(wlocks_held <= locks);
 437 }
 438 
 439 void
 440 spa_config_exit(spa_t *spa, int locks, void *tag)
 441 {
 442         for (int i = SCL_LOCKS - 1; i >= 0; i--) {
 443                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
 444                 if (!(locks & (1 << i)))
 445                         continue;
 446                 mutex_enter(&scl->scl_lock);
 447                 ASSERT(!refcount_is_zero(&scl->scl_count));
 448                 if (refcount_remove(&scl->scl_count, tag) == 0) {
 449                         ASSERT(scl->scl_writer == NULL ||
 450                             scl->scl_writer == curthread);
 451                         scl->scl_writer = NULL;      /* OK in either case */
 452                         cv_broadcast(&scl->scl_cv);
 453                 }
 454                 mutex_exit(&scl->scl_lock);
 455         }
 456 }
 457 
 458 int
 459 spa_config_held(spa_t *spa, int locks, krw_t rw)
 460 {
 461         int locks_held = 0;
 462 
 463         for (int i = 0; i < SCL_LOCKS; i++) {
 464                 spa_config_lock_t *scl = &spa->spa_config_lock[i];
 465                 if (!(locks & (1 << i)))
 466                         continue;
 467                 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
 468                     (rw == RW_WRITER && scl->scl_writer == curthread))
 469                         locks_held |= 1 << i;
 470         }
 471 
 472         return (locks_held);
 473 }
 474 
 475 /*
 476  * ==========================================================================
 477  * SPA namespace functions
 478  * ==========================================================================
 479  */
 480 
 481 /*
 482  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
 483  * Returns NULL if no matching spa_t is found.
 484  */
 485 spa_t *
 486 spa_lookup(const char *name)
 487 {
 488         static spa_t search;    /* spa_t is large; don't allocate on stack */
 489         spa_t *spa;
 490         avl_index_t where;
 491         char *cp;
 492 
 493         ASSERT(MUTEX_HELD(&spa_namespace_lock));
 494 
 495         (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
 496 
 497         /*
 498          * If it's a full dataset name, figure out the pool name and
 499          * just use that.
 500          */
 501         cp = strpbrk(search.spa_name, "/@#");
 502         if (cp != NULL)
 503                 *cp = '\0';
 504 
 505         spa = avl_find(&spa_namespace_avl, &search, &where);
 506 
 507         return (spa);
 508 }
 509 
 510 /*
 511  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
 512  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
 513  * looking for potentially hung I/Os.
 514  */
 515 void
 516 spa_deadman(void *arg)
 517 {
 518         spa_t *spa = arg;
 519 
 520         /*
 521          * Disable the deadman timer if the pool is suspended.
 522          */
 523         if (spa_suspended(spa)) {
 524                 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
 525                 return;
 526         }
 527 
 528         zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
 529             (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
 530             ++spa->spa_deadman_calls);
 531         if (zfs_deadman_enabled)
 532                 vdev_deadman(spa->spa_root_vdev);
 533 }
 534 
 535 /*
 536  * Create an uninitialized spa_t with the given name.  Requires
 537  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
 538  * exist by calling spa_lookup() first.
 539  */
 540 spa_t *
 541 spa_add(const char *name, nvlist_t *config, const char *altroot)
 542 {
 543         spa_t *spa;
 544         spa_config_dirent_t *dp;
 545         cyc_handler_t hdlr;
 546         cyc_time_t when;
 547 
 548         ASSERT(MUTEX_HELD(&spa_namespace_lock));
 549 
 550         spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
 551 
 552         mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
 553         mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
 554         mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
 555         mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
 556         mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
 557         mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
 558         mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
 559         mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
 560         mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
 561         mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
 562         mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
 563 
 564         cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
 565         cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
 566         cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
 567         cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
 568         cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
 569 
 570         for (int t = 0; t < TXG_SIZE; t++)
 571                 bplist_create(&spa->spa_free_bplist[t]);
 572 
 573         (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
 574         spa->spa_state = POOL_STATE_UNINITIALIZED;
 575         spa->spa_freeze_txg = UINT64_MAX;
 576         spa->spa_final_txg = UINT64_MAX;
 577         spa->spa_load_max_txg = UINT64_MAX;
 578         spa->spa_proc = &p0;
 579         spa->spa_proc_state = SPA_PROC_NONE;
 580 
 581         hdlr.cyh_func = spa_deadman;
 582         hdlr.cyh_arg = spa;
 583         hdlr.cyh_level = CY_LOW_LEVEL;
 584 
 585         spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
 586 
 587         /*
 588          * This determines how often we need to check for hung I/Os after
 589          * the cyclic has already fired. Since checking for hung I/Os is
 590          * an expensive operation we don't want to check too frequently.
 591          * Instead wait for 5 seconds before checking again.
 592          */
 593         when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
 594         when.cyt_when = CY_INFINITY;
 595         mutex_enter(&cpu_lock);
 596         spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
 597         mutex_exit(&cpu_lock);
 598 
 599         refcount_create(&spa->spa_refcount);
 600         spa_config_lock_init(spa);
 601 
 602         avl_add(&spa_namespace_avl, spa);
 603 
 604         /*
 605          * Set the alternate root, if there is one.
 606          */
 607         if (altroot) {
 608                 spa->spa_root = spa_strdup(altroot);
 609                 spa_active_count++;
 610         }
 611 
 612         /*
 613          * Every pool starts with the default cachefile
 614          */
 615         list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
 616             offsetof(spa_config_dirent_t, scd_link));
 617 
 618         dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
 619         dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
 620         list_insert_head(&spa->spa_config_list, dp);
 621 
 622         VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
 623             KM_SLEEP) == 0);
 624 
 625         if (config != NULL) {
 626                 nvlist_t *features;
 627 
 628                 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
 629                     &features) == 0) {
 630                         VERIFY(nvlist_dup(features, &spa->spa_label_features,
 631                             0) == 0);
 632                 }
 633 
 634                 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
 635         }
 636 
 637         if (spa->spa_label_features == NULL) {
 638                 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
 639                     KM_SLEEP) == 0);
 640         }
 641 
 642         spa->spa_iokstat = kstat_create("zfs", 0, name,
 643             "disk", KSTAT_TYPE_IO, 1, 0);
 644         if (spa->spa_iokstat) {
 645                 spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
 646                 kstat_install(spa->spa_iokstat);
 647         }
 648 
 649         spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
 650 
 651         /*
 652          * As a pool is being created, treat all features as disabled by
 653          * setting SPA_FEATURE_DISABLED for all entries in the feature
 654          * refcount cache.
 655          */
 656         for (int i = 0; i < SPA_FEATURES; i++) {
 657                 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
 658         }
 659 
 660         return (spa);
 661 }
 662 
 663 /*
 664  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
 665  * spa_namespace_lock.  This is called only after the spa_t has been closed and
 666  * deactivated.
 667  */
 668 void
 669 spa_remove(spa_t *spa)
 670 {
 671         spa_config_dirent_t *dp;
 672 
 673         ASSERT(MUTEX_HELD(&spa_namespace_lock));
 674         ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
 675         ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
 676 
 677         nvlist_free(spa->spa_config_splitting);
 678 
 679         avl_remove(&spa_namespace_avl, spa);
 680         cv_broadcast(&spa_namespace_cv);
 681 
 682         if (spa->spa_root) {
 683                 spa_strfree(spa->spa_root);
 684                 spa_active_count--;
 685         }
 686 
 687         while ((dp = list_head(&spa->spa_config_list)) != NULL) {
 688                 list_remove(&spa->spa_config_list, dp);
 689                 if (dp->scd_path != NULL)
 690                         spa_strfree(dp->scd_path);
 691                 kmem_free(dp, sizeof (spa_config_dirent_t));
 692         }
 693 
 694         list_destroy(&spa->spa_config_list);
 695 
 696         nvlist_free(spa->spa_label_features);
 697         nvlist_free(spa->spa_load_info);
 698         spa_config_set(spa, NULL);
 699 
 700         mutex_enter(&cpu_lock);
 701         if (spa->spa_deadman_cycid != CYCLIC_NONE)
 702                 cyclic_remove(spa->spa_deadman_cycid);
 703         mutex_exit(&cpu_lock);
 704         spa->spa_deadman_cycid = CYCLIC_NONE;
 705 
 706         refcount_destroy(&spa->spa_refcount);
 707 
 708         spa_config_lock_destroy(spa);
 709 
 710         kstat_delete(spa->spa_iokstat);
 711         spa->spa_iokstat = NULL;
 712 
 713         for (int t = 0; t < TXG_SIZE; t++)
 714                 bplist_destroy(&spa->spa_free_bplist[t]);
 715 
 716         cv_destroy(&spa->spa_async_cv);
 717         cv_destroy(&spa->spa_evicting_os_cv);
 718         cv_destroy(&spa->spa_proc_cv);
 719         cv_destroy(&spa->spa_scrub_io_cv);
 720         cv_destroy(&spa->spa_suspend_cv);
 721 
 722         mutex_destroy(&spa->spa_async_lock);
 723         mutex_destroy(&spa->spa_errlist_lock);
 724         mutex_destroy(&spa->spa_errlog_lock);
 725         mutex_destroy(&spa->spa_evicting_os_lock);
 726         mutex_destroy(&spa->spa_history_lock);
 727         mutex_destroy(&spa->spa_proc_lock);
 728         mutex_destroy(&spa->spa_props_lock);
 729         mutex_destroy(&spa->spa_scrub_lock);
 730         mutex_destroy(&spa->spa_suspend_lock);
 731         mutex_destroy(&spa->spa_vdev_top_lock);
 732         mutex_destroy(&spa->spa_iokstat_lock);
 733 
 734         kmem_free(spa, sizeof (spa_t));
 735 }
 736 
 737 /*
 738  * Given a pool, return the next pool in the namespace, or NULL if there is
 739  * none.  If 'prev' is NULL, return the first pool.
 740  */
 741 spa_t *
 742 spa_next(spa_t *prev)
 743 {
 744         ASSERT(MUTEX_HELD(&spa_namespace_lock));
 745 
 746         if (prev)
 747                 return (AVL_NEXT(&spa_namespace_avl, prev));
 748         else
 749                 return (avl_first(&spa_namespace_avl));
 750 }
 751 
 752 /*
 753  * ==========================================================================
 754  * SPA refcount functions
 755  * ==========================================================================
 756  */
 757 
 758 /*
 759  * Add a reference to the given spa_t.  Must have at least one reference, or
 760  * have the namespace lock held.
 761  */
 762 void
 763 spa_open_ref(spa_t *spa, void *tag)
 764 {
 765         ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
 766             MUTEX_HELD(&spa_namespace_lock));
 767         (void) refcount_add(&spa->spa_refcount, tag);
 768 }
 769 
 770 /*
 771  * Remove a reference to the given spa_t.  Must have at least one reference, or
 772  * have the namespace lock held.
 773  */
 774 void
 775 spa_close(spa_t *spa, void *tag)
 776 {
 777         ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
 778             MUTEX_HELD(&spa_namespace_lock));
 779         (void) refcount_remove(&spa->spa_refcount, tag);
 780 }
 781 
 782 /*
 783  * Remove a reference to the given spa_t held by a dsl dir that is
 784  * being asynchronously released.  Async releases occur from a taskq
 785  * performing eviction of dsl datasets and dirs.  The namespace lock
 786  * isn't held and the hold by the object being evicted may contribute to
 787  * spa_minref (e.g. dataset or directory released during pool export),
 788  * so the asserts in spa_close() do not apply.
 789  */
 790 void
 791 spa_async_close(spa_t *spa, void *tag)
 792 {
 793         (void) refcount_remove(&spa->spa_refcount, tag);
 794 }
 795 
 796 /*
 797  * Check to see if the spa refcount is zero.  Must be called with
 798  * spa_namespace_lock held.  We really compare against spa_minref, which is the
 799  * number of references acquired when opening a pool
 800  */
 801 boolean_t
 802 spa_refcount_zero(spa_t *spa)
 803 {
 804         ASSERT(MUTEX_HELD(&spa_namespace_lock));
 805 
 806         return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
 807 }
 808 
 809 /*
 810  * ==========================================================================
 811  * SPA spare and l2cache tracking
 812  * ==========================================================================
 813  */
 814 
 815 /*
 816  * Hot spares and cache devices are tracked using the same code below,
 817  * for 'auxiliary' devices.
 818  */
 819 
 820 typedef struct spa_aux {
 821         uint64_t        aux_guid;
 822         uint64_t        aux_pool;
 823         avl_node_t      aux_avl;
 824         int             aux_count;
 825 } spa_aux_t;
 826 
 827 static int
 828 spa_aux_compare(const void *a, const void *b)
 829 {
 830         const spa_aux_t *sa = a;
 831         const spa_aux_t *sb = b;
 832 
 833         if (sa->aux_guid < sb->aux_guid)
 834                 return (-1);
 835         else if (sa->aux_guid > sb->aux_guid)
 836                 return (1);
 837         else
 838                 return (0);
 839 }
 840 
 841 void
 842 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
 843 {
 844         avl_index_t where;
 845         spa_aux_t search;
 846         spa_aux_t *aux;
 847 
 848         search.aux_guid = vd->vdev_guid;
 849         if ((aux = avl_find(avl, &search, &where)) != NULL) {
 850                 aux->aux_count++;
 851         } else {
 852                 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
 853                 aux->aux_guid = vd->vdev_guid;
 854                 aux->aux_count = 1;
 855                 avl_insert(avl, aux, where);
 856         }
 857 }
 858 
 859 void
 860 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
 861 {
 862         spa_aux_t search;
 863         spa_aux_t *aux;
 864         avl_index_t where;
 865 
 866         search.aux_guid = vd->vdev_guid;
 867         aux = avl_find(avl, &search, &where);
 868 
 869         ASSERT(aux != NULL);
 870 
 871         if (--aux->aux_count == 0) {
 872                 avl_remove(avl, aux);
 873                 kmem_free(aux, sizeof (spa_aux_t));
 874         } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
 875                 aux->aux_pool = 0ULL;
 876         }
 877 }
 878 
 879 boolean_t
 880 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
 881 {
 882         spa_aux_t search, *found;
 883 
 884         search.aux_guid = guid;
 885         found = avl_find(avl, &search, NULL);
 886 
 887         if (pool) {
 888                 if (found)
 889                         *pool = found->aux_pool;
 890                 else
 891                         *pool = 0ULL;
 892         }
 893 
 894         if (refcnt) {
 895                 if (found)
 896                         *refcnt = found->aux_count;
 897                 else
 898                         *refcnt = 0;
 899         }
 900 
 901         return (found != NULL);
 902 }
 903 
 904 void
 905 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
 906 {
 907         spa_aux_t search, *found;
 908         avl_index_t where;
 909 
 910         search.aux_guid = vd->vdev_guid;
 911         found = avl_find(avl, &search, &where);
 912         ASSERT(found != NULL);
 913         ASSERT(found->aux_pool == 0ULL);
 914 
 915         found->aux_pool = spa_guid(vd->vdev_spa);
 916 }
 917 
 918 /*
 919  * Spares are tracked globally due to the following constraints:
 920  *
 921  *      - A spare may be part of multiple pools.
 922  *      - A spare may be added to a pool even if it's actively in use within
 923  *        another pool.
 924  *      - A spare in use in any pool can only be the source of a replacement if
 925  *        the target is a spare in the same pool.
 926  *
 927  * We keep track of all spares on the system through the use of a reference
 928  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
 929  * spare, then we bump the reference count in the AVL tree.  In addition, we set
 930  * the 'vdev_isspare' member to indicate that the device is a spare (active or
 931  * inactive).  When a spare is made active (used to replace a device in the
 932  * pool), we also keep track of which pool its been made a part of.
 933  *
 934  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
 935  * called under the spa_namespace lock as part of vdev reconfiguration.  The
 936  * separate spare lock exists for the status query path, which does not need to
 937  * be completely consistent with respect to other vdev configuration changes.
 938  */
 939 
 940 static int
 941 spa_spare_compare(const void *a, const void *b)
 942 {
 943         return (spa_aux_compare(a, b));
 944 }
 945 
 946 void
 947 spa_spare_add(vdev_t *vd)
 948 {
 949         mutex_enter(&spa_spare_lock);
 950         ASSERT(!vd->vdev_isspare);
 951         spa_aux_add(vd, &spa_spare_avl);
 952         vd->vdev_isspare = B_TRUE;
 953         mutex_exit(&spa_spare_lock);
 954 }
 955 
 956 void
 957 spa_spare_remove(vdev_t *vd)
 958 {
 959         mutex_enter(&spa_spare_lock);
 960         ASSERT(vd->vdev_isspare);
 961         spa_aux_remove(vd, &spa_spare_avl);
 962         vd->vdev_isspare = B_FALSE;
 963         mutex_exit(&spa_spare_lock);
 964 }
 965 
 966 boolean_t
 967 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
 968 {
 969         boolean_t found;
 970 
 971         mutex_enter(&spa_spare_lock);
 972         found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
 973         mutex_exit(&spa_spare_lock);
 974 
 975         return (found);
 976 }
 977 
 978 void
 979 spa_spare_activate(vdev_t *vd)
 980 {
 981         mutex_enter(&spa_spare_lock);
 982         ASSERT(vd->vdev_isspare);
 983         spa_aux_activate(vd, &spa_spare_avl);
 984         mutex_exit(&spa_spare_lock);
 985 }
 986 
 987 /*
 988  * Level 2 ARC devices are tracked globally for the same reasons as spares.
 989  * Cache devices currently only support one pool per cache device, and so
 990  * for these devices the aux reference count is currently unused beyond 1.
 991  */
 992 
 993 static int
 994 spa_l2cache_compare(const void *a, const void *b)
 995 {
 996         return (spa_aux_compare(a, b));
 997 }
 998 
 999 void
1000 spa_l2cache_add(vdev_t *vd)
1001 {
1002         mutex_enter(&spa_l2cache_lock);
1003         ASSERT(!vd->vdev_isl2cache);
1004         spa_aux_add(vd, &spa_l2cache_avl);
1005         vd->vdev_isl2cache = B_TRUE;
1006         mutex_exit(&spa_l2cache_lock);
1007 }
1008 
1009 void
1010 spa_l2cache_remove(vdev_t *vd)
1011 {
1012         mutex_enter(&spa_l2cache_lock);
1013         ASSERT(vd->vdev_isl2cache);
1014         spa_aux_remove(vd, &spa_l2cache_avl);
1015         vd->vdev_isl2cache = B_FALSE;
1016         mutex_exit(&spa_l2cache_lock);
1017 }
1018 
1019 boolean_t
1020 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1021 {
1022         boolean_t found;
1023 
1024         mutex_enter(&spa_l2cache_lock);
1025         found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1026         mutex_exit(&spa_l2cache_lock);
1027 
1028         return (found);
1029 }
1030 
1031 void
1032 spa_l2cache_activate(vdev_t *vd)
1033 {
1034         mutex_enter(&spa_l2cache_lock);
1035         ASSERT(vd->vdev_isl2cache);
1036         spa_aux_activate(vd, &spa_l2cache_avl);
1037         mutex_exit(&spa_l2cache_lock);
1038 }
1039 
1040 /*
1041  * ==========================================================================
1042  * SPA vdev locking
1043  * ==========================================================================
1044  */
1045 
1046 /*
1047  * Lock the given spa_t for the purpose of adding or removing a vdev.
1048  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1049  * It returns the next transaction group for the spa_t.
1050  */
1051 uint64_t
1052 spa_vdev_enter(spa_t *spa)
1053 {
1054         mutex_enter(&spa->spa_vdev_top_lock);
1055         mutex_enter(&spa_namespace_lock);
1056         return (spa_vdev_config_enter(spa));
1057 }
1058 
1059 /*
1060  * Internal implementation for spa_vdev_enter().  Used when a vdev
1061  * operation requires multiple syncs (i.e. removing a device) while
1062  * keeping the spa_namespace_lock held.
1063  */
1064 uint64_t
1065 spa_vdev_config_enter(spa_t *spa)
1066 {
1067         ASSERT(MUTEX_HELD(&spa_namespace_lock));
1068 
1069         spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1070 
1071         return (spa_last_synced_txg(spa) + 1);
1072 }
1073 
1074 /*
1075  * Used in combination with spa_vdev_config_enter() to allow the syncing
1076  * of multiple transactions without releasing the spa_namespace_lock.
1077  */
1078 void
1079 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1080 {
1081         ASSERT(MUTEX_HELD(&spa_namespace_lock));
1082 
1083         int config_changed = B_FALSE;
1084 
1085         ASSERT(txg > spa_last_synced_txg(spa));
1086 
1087         spa->spa_pending_vdev = NULL;
1088 
1089         /*
1090          * Reassess the DTLs.
1091          */
1092         vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1093 
1094         if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1095                 config_changed = B_TRUE;
1096                 spa->spa_config_generation++;
1097         }
1098 
1099         /*
1100          * Verify the metaslab classes.
1101          */
1102         ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1103         ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1104 
1105         spa_config_exit(spa, SCL_ALL, spa);
1106 
1107         /*
1108          * Panic the system if the specified tag requires it.  This
1109          * is useful for ensuring that configurations are updated
1110          * transactionally.
1111          */
1112         if (zio_injection_enabled)
1113                 zio_handle_panic_injection(spa, tag, 0);
1114 
1115         /*
1116          * Note: this txg_wait_synced() is important because it ensures
1117          * that there won't be more than one config change per txg.
1118          * This allows us to use the txg as the generation number.
1119          */
1120         if (error == 0)
1121                 txg_wait_synced(spa->spa_dsl_pool, txg);
1122 
1123         if (vd != NULL) {
1124                 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1125                 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1126                 vdev_free(vd);
1127                 spa_config_exit(spa, SCL_ALL, spa);
1128         }
1129 
1130         /*
1131          * If the config changed, update the config cache.
1132          */
1133         if (config_changed)
1134                 spa_config_sync(spa, B_FALSE, B_TRUE);
1135 }
1136 
1137 /*
1138  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
1139  * locking of spa_vdev_enter(), we also want make sure the transactions have
1140  * synced to disk, and then update the global configuration cache with the new
1141  * information.
1142  */
1143 int
1144 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1145 {
1146         spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1147         mutex_exit(&spa_namespace_lock);
1148         mutex_exit(&spa->spa_vdev_top_lock);
1149 
1150         return (error);
1151 }
1152 
1153 /*
1154  * Lock the given spa_t for the purpose of changing vdev state.
1155  */
1156 void
1157 spa_vdev_state_enter(spa_t *spa, int oplocks)
1158 {
1159         int locks = SCL_STATE_ALL | oplocks;
1160 
1161         /*
1162          * Root pools may need to read of the underlying devfs filesystem
1163          * when opening up a vdev.  Unfortunately if we're holding the
1164          * SCL_ZIO lock it will result in a deadlock when we try to issue
1165          * the read from the root filesystem.  Instead we "prefetch"
1166          * the associated vnodes that we need prior to opening the
1167          * underlying devices and cache them so that we can prevent
1168          * any I/O when we are doing the actual open.
1169          */
1170         if (spa_is_root(spa)) {
1171                 int low = locks & ~(SCL_ZIO - 1);
1172                 int high = locks & ~low;
1173 
1174                 spa_config_enter(spa, high, spa, RW_WRITER);
1175                 vdev_hold(spa->spa_root_vdev);
1176                 spa_config_enter(spa, low, spa, RW_WRITER);
1177         } else {
1178                 spa_config_enter(spa, locks, spa, RW_WRITER);
1179         }
1180         spa->spa_vdev_locks = locks;
1181 }
1182 
1183 int
1184 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1185 {
1186         boolean_t config_changed = B_FALSE;
1187 
1188         if (vd != NULL || error == 0)
1189                 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1190                     0, 0, B_FALSE);
1191 
1192         if (vd != NULL) {
1193                 vdev_state_dirty(vd->vdev_top);
1194                 config_changed = B_TRUE;
1195                 spa->spa_config_generation++;
1196         }
1197 
1198         if (spa_is_root(spa))
1199                 vdev_rele(spa->spa_root_vdev);
1200 
1201         ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1202         spa_config_exit(spa, spa->spa_vdev_locks, spa);
1203 
1204         /*
1205          * If anything changed, wait for it to sync.  This ensures that,
1206          * from the system administrator's perspective, zpool(1M) commands
1207          * are synchronous.  This is important for things like zpool offline:
1208          * when the command completes, you expect no further I/O from ZFS.
1209          */
1210         if (vd != NULL)
1211                 txg_wait_synced(spa->spa_dsl_pool, 0);
1212 
1213         /*
1214          * If the config changed, update the config cache.
1215          */
1216         if (config_changed) {
1217                 mutex_enter(&spa_namespace_lock);
1218                 spa_config_sync(spa, B_FALSE, B_TRUE);
1219                 mutex_exit(&spa_namespace_lock);
1220         }
1221 
1222         return (error);
1223 }
1224 
1225 /*
1226  * ==========================================================================
1227  * Miscellaneous functions
1228  * ==========================================================================
1229  */
1230 
1231 void
1232 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1233 {
1234         if (!nvlist_exists(spa->spa_label_features, feature)) {
1235                 fnvlist_add_boolean(spa->spa_label_features, feature);
1236                 /*
1237                  * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1238                  * dirty the vdev config because lock SCL_CONFIG is not held.
1239                  * Thankfully, in this case we don't need to dirty the config
1240                  * because it will be written out anyway when we finish
1241                  * creating the pool.
1242                  */
1243                 if (tx->tx_txg != TXG_INITIAL)
1244                         vdev_config_dirty(spa->spa_root_vdev);
1245         }
1246 }
1247 
1248 void
1249 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1250 {
1251         if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1252                 vdev_config_dirty(spa->spa_root_vdev);
1253 }
1254 
1255 /*
1256  * Rename a spa_t.
1257  */
1258 int
1259 spa_rename(const char *name, const char *newname)
1260 {
1261         spa_t *spa;
1262         int err;
1263 
1264         /*
1265          * Lookup the spa_t and grab the config lock for writing.  We need to
1266          * actually open the pool so that we can sync out the necessary labels.
1267          * It's OK to call spa_open() with the namespace lock held because we
1268          * allow recursive calls for other reasons.
1269          */
1270         mutex_enter(&spa_namespace_lock);
1271         if ((err = spa_open(name, &spa, FTAG)) != 0) {
1272                 mutex_exit(&spa_namespace_lock);
1273                 return (err);
1274         }
1275 
1276         spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1277 
1278         avl_remove(&spa_namespace_avl, spa);
1279         (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1280         avl_add(&spa_namespace_avl, spa);
1281 
1282         /*
1283          * Sync all labels to disk with the new names by marking the root vdev
1284          * dirty and waiting for it to sync.  It will pick up the new pool name
1285          * during the sync.
1286          */
1287         vdev_config_dirty(spa->spa_root_vdev);
1288 
1289         spa_config_exit(spa, SCL_ALL, FTAG);
1290 
1291         txg_wait_synced(spa->spa_dsl_pool, 0);
1292 
1293         /*
1294          * Sync the updated config cache.
1295          */
1296         spa_config_sync(spa, B_FALSE, B_TRUE);
1297 
1298         spa_close(spa, FTAG);
1299 
1300         mutex_exit(&spa_namespace_lock);
1301 
1302         return (0);
1303 }
1304 
1305 /*
1306  * Return the spa_t associated with given pool_guid, if it exists.  If
1307  * device_guid is non-zero, determine whether the pool exists *and* contains
1308  * a device with the specified device_guid.
1309  */
1310 spa_t *
1311 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1312 {
1313         spa_t *spa;
1314         avl_tree_t *t = &spa_namespace_avl;
1315 
1316         ASSERT(MUTEX_HELD(&spa_namespace_lock));
1317 
1318         for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1319                 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1320                         continue;
1321                 if (spa->spa_root_vdev == NULL)
1322                         continue;
1323                 if (spa_guid(spa) == pool_guid) {
1324                         if (device_guid == 0)
1325                                 break;
1326 
1327                         if (vdev_lookup_by_guid(spa->spa_root_vdev,
1328                             device_guid) != NULL)
1329                                 break;
1330 
1331                         /*
1332                          * Check any devices we may be in the process of adding.
1333                          */
1334                         if (spa->spa_pending_vdev) {
1335                                 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1336                                     device_guid) != NULL)
1337                                         break;
1338                         }
1339                 }
1340         }
1341 
1342         return (spa);
1343 }
1344 
1345 /*
1346  * Determine whether a pool with the given pool_guid exists.
1347  */
1348 boolean_t
1349 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1350 {
1351         return (spa_by_guid(pool_guid, device_guid) != NULL);
1352 }
1353 
1354 char *
1355 spa_strdup(const char *s)
1356 {
1357         size_t len;
1358         char *new;
1359 
1360         len = strlen(s);
1361         new = kmem_alloc(len + 1, KM_SLEEP);
1362         bcopy(s, new, len);
1363         new[len] = '\0';
1364 
1365         return (new);
1366 }
1367 
1368 void
1369 spa_strfree(char *s)
1370 {
1371         kmem_free(s, strlen(s) + 1);
1372 }
1373 
1374 uint64_t
1375 spa_get_random(uint64_t range)
1376 {
1377         uint64_t r;
1378 
1379         ASSERT(range != 0);
1380 
1381         (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1382 
1383         return (r % range);
1384 }
1385 
1386 uint64_t
1387 spa_generate_guid(spa_t *spa)
1388 {
1389         uint64_t guid = spa_get_random(-1ULL);
1390 
1391         if (spa != NULL) {
1392                 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1393                         guid = spa_get_random(-1ULL);
1394         } else {
1395                 while (guid == 0 || spa_guid_exists(guid, 0))
1396                         guid = spa_get_random(-1ULL);
1397         }
1398 
1399         return (guid);
1400 }
1401 
1402 void
1403 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1404 {
1405         char type[256];
1406         char *checksum = NULL;
1407         char *compress = NULL;
1408 
1409         if (bp != NULL) {
1410                 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1411                         dmu_object_byteswap_t bswap =
1412                             DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1413                         (void) snprintf(type, sizeof (type), "bswap %s %s",
1414                             DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1415                             "metadata" : "data",
1416                             dmu_ot_byteswap[bswap].ob_name);
1417                 } else {
1418                         (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1419                             sizeof (type));
1420                 }
1421                 if (!BP_IS_EMBEDDED(bp)) {
1422                         checksum =
1423                             zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1424                 }
1425                 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1426         }
1427 
1428         SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1429             compress);
1430 }
1431 
1432 void
1433 spa_freeze(spa_t *spa)
1434 {
1435         uint64_t freeze_txg = 0;
1436 
1437         spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1438         if (spa->spa_freeze_txg == UINT64_MAX) {
1439                 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1440                 spa->spa_freeze_txg = freeze_txg;
1441         }
1442         spa_config_exit(spa, SCL_ALL, FTAG);
1443         if (freeze_txg != 0)
1444                 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1445 }
1446 
1447 void
1448 zfs_panic_recover(const char *fmt, ...)
1449 {
1450         va_list adx;
1451 
1452         va_start(adx, fmt);
1453         vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1454         va_end(adx);
1455 }
1456 
1457 /*
1458  * This is a stripped-down version of strtoull, suitable only for converting
1459  * lowercase hexadecimal numbers that don't overflow.
1460  */
1461 uint64_t
1462 strtonum(const char *str, char **nptr)
1463 {
1464         uint64_t val = 0;
1465         char c;
1466         int digit;
1467 
1468         while ((c = *str) != '\0') {
1469                 if (c >= '0' && c <= '9')
1470                         digit = c - '0';
1471                 else if (c >= 'a' && c <= 'f')
1472                         digit = 10 + c - 'a';
1473                 else
1474                         break;
1475 
1476                 val *= 16;
1477                 val += digit;
1478 
1479                 str++;
1480         }
1481 
1482         if (nptr)
1483                 *nptr = (char *)str;
1484 
1485         return (val);
1486 }
1487 
1488 /*
1489  * ==========================================================================
1490  * Accessor functions
1491  * ==========================================================================
1492  */
1493 
1494 boolean_t
1495 spa_shutting_down(spa_t *spa)
1496 {
1497         return (spa->spa_async_suspended);
1498 }
1499 
1500 dsl_pool_t *
1501 spa_get_dsl(spa_t *spa)
1502 {
1503         return (spa->spa_dsl_pool);
1504 }
1505 
1506 boolean_t
1507 spa_is_initializing(spa_t *spa)
1508 {
1509         return (spa->spa_is_initializing);
1510 }
1511 
1512 blkptr_t *
1513 spa_get_rootblkptr(spa_t *spa)
1514 {
1515         return (&spa->spa_ubsync.ub_rootbp);
1516 }
1517 
1518 void
1519 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1520 {
1521         spa->spa_uberblock.ub_rootbp = *bp;
1522 }
1523 
1524 void
1525 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1526 {
1527         if (spa->spa_root == NULL)
1528                 buf[0] = '\0';
1529         else
1530                 (void) strncpy(buf, spa->spa_root, buflen);
1531 }
1532 
1533 int
1534 spa_sync_pass(spa_t *spa)
1535 {
1536         return (spa->spa_sync_pass);
1537 }
1538 
1539 char *
1540 spa_name(spa_t *spa)
1541 {
1542         return (spa->spa_name);
1543 }
1544 
1545 uint64_t
1546 spa_guid(spa_t *spa)
1547 {
1548         dsl_pool_t *dp = spa_get_dsl(spa);
1549         uint64_t guid;
1550 
1551         /*
1552          * If we fail to parse the config during spa_load(), we can go through
1553          * the error path (which posts an ereport) and end up here with no root
1554          * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1555          * this case.
1556          */
1557         if (spa->spa_root_vdev == NULL)
1558                 return (spa->spa_config_guid);
1559 
1560         guid = spa->spa_last_synced_guid != 0 ?
1561             spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1562 
1563         /*
1564          * Return the most recently synced out guid unless we're
1565          * in syncing context.
1566          */
1567         if (dp && dsl_pool_sync_context(dp))
1568                 return (spa->spa_root_vdev->vdev_guid);
1569         else
1570                 return (guid);
1571 }
1572 
1573 uint64_t
1574 spa_load_guid(spa_t *spa)
1575 {
1576         /*
1577          * This is a GUID that exists solely as a reference for the
1578          * purposes of the arc.  It is generated at load time, and
1579          * is never written to persistent storage.
1580          */
1581         return (spa->spa_load_guid);
1582 }
1583 
1584 uint64_t
1585 spa_last_synced_txg(spa_t *spa)
1586 {
1587         return (spa->spa_ubsync.ub_txg);
1588 }
1589 
1590 uint64_t
1591 spa_first_txg(spa_t *spa)
1592 {
1593         return (spa->spa_first_txg);
1594 }
1595 
1596 uint64_t
1597 spa_syncing_txg(spa_t *spa)
1598 {
1599         return (spa->spa_syncing_txg);
1600 }
1601 
1602 pool_state_t
1603 spa_state(spa_t *spa)
1604 {
1605         return (spa->spa_state);
1606 }
1607 
1608 spa_load_state_t
1609 spa_load_state(spa_t *spa)
1610 {
1611         return (spa->spa_load_state);
1612 }
1613 
1614 uint64_t
1615 spa_freeze_txg(spa_t *spa)
1616 {
1617         return (spa->spa_freeze_txg);
1618 }
1619 
1620 /* ARGSUSED */
1621 uint64_t
1622 spa_get_asize(spa_t *spa, uint64_t lsize)
1623 {
1624         return (lsize * spa_asize_inflation);
1625 }
1626 
1627 /*
1628  * Return the amount of slop space in bytes.  It is 1/32 of the pool (3.2%),
1629  * or at least 32MB.
1630  *
1631  * See the comment above spa_slop_shift for details.
1632  */
1633 uint64_t
1634 spa_get_slop_space(spa_t *spa) {
1635         uint64_t space = spa_get_dspace(spa);
1636         return (MAX(space >> spa_slop_shift, SPA_MINDEVSIZE >> 1));
1637 }
1638 
1639 uint64_t
1640 spa_get_dspace(spa_t *spa)
1641 {
1642         return (spa->spa_dspace);
1643 }
1644 
1645 void
1646 spa_update_dspace(spa_t *spa)
1647 {
1648         spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1649             ddt_get_dedup_dspace(spa);
1650 }
1651 
1652 /*
1653  * Return the failure mode that has been set to this pool. The default
1654  * behavior will be to block all I/Os when a complete failure occurs.
1655  */
1656 uint8_t
1657 spa_get_failmode(spa_t *spa)
1658 {
1659         return (spa->spa_failmode);
1660 }
1661 
1662 boolean_t
1663 spa_suspended(spa_t *spa)
1664 {
1665         return (spa->spa_suspended);
1666 }
1667 
1668 uint64_t
1669 spa_version(spa_t *spa)
1670 {
1671         return (spa->spa_ubsync.ub_version);
1672 }
1673 
1674 boolean_t
1675 spa_deflate(spa_t *spa)
1676 {
1677         return (spa->spa_deflate);
1678 }
1679 
1680 metaslab_class_t *
1681 spa_normal_class(spa_t *spa)
1682 {
1683         return (spa->spa_normal_class);
1684 }
1685 
1686 metaslab_class_t *
1687 spa_log_class(spa_t *spa)
1688 {
1689         return (spa->spa_log_class);
1690 }
1691 
1692 void
1693 spa_evicting_os_register(spa_t *spa, objset_t *os)
1694 {
1695         mutex_enter(&spa->spa_evicting_os_lock);
1696         list_insert_head(&spa->spa_evicting_os_list, os);
1697         mutex_exit(&spa->spa_evicting_os_lock);
1698 }
1699 
1700 void
1701 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1702 {
1703         mutex_enter(&spa->spa_evicting_os_lock);
1704         list_remove(&spa->spa_evicting_os_list, os);
1705         cv_broadcast(&spa->spa_evicting_os_cv);
1706         mutex_exit(&spa->spa_evicting_os_lock);
1707 }
1708 
1709 void
1710 spa_evicting_os_wait(spa_t *spa)
1711 {
1712         mutex_enter(&spa->spa_evicting_os_lock);
1713         while (!list_is_empty(&spa->spa_evicting_os_list))
1714                 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1715         mutex_exit(&spa->spa_evicting_os_lock);
1716 
1717         dmu_buf_user_evict_wait();
1718 }
1719 
1720 int
1721 spa_max_replication(spa_t *spa)
1722 {
1723         /*
1724          * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1725          * handle BPs with more than one DVA allocated.  Set our max
1726          * replication level accordingly.
1727          */
1728         if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1729                 return (1);
1730         return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1731 }
1732 
1733 int
1734 spa_prev_software_version(spa_t *spa)
1735 {
1736         return (spa->spa_prev_software_version);
1737 }
1738 
1739 uint64_t
1740 spa_deadman_synctime(spa_t *spa)
1741 {
1742         return (spa->spa_deadman_synctime);
1743 }
1744 
1745 uint64_t
1746 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1747 {
1748         uint64_t asize = DVA_GET_ASIZE(dva);
1749         uint64_t dsize = asize;
1750 
1751         ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1752 
1753         if (asize != 0 && spa->spa_deflate) {
1754                 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1755                 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1756         }
1757 
1758         return (dsize);
1759 }
1760 
1761 uint64_t
1762 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1763 {
1764         uint64_t dsize = 0;
1765 
1766         for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1767                 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1768 
1769         return (dsize);
1770 }
1771 
1772 uint64_t
1773 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1774 {
1775         uint64_t dsize = 0;
1776 
1777         spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1778 
1779         for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1780                 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1781 
1782         spa_config_exit(spa, SCL_VDEV, FTAG);
1783 
1784         return (dsize);
1785 }
1786 
1787 /*
1788  * ==========================================================================
1789  * Initialization and Termination
1790  * ==========================================================================
1791  */
1792 
1793 static int
1794 spa_name_compare(const void *a1, const void *a2)
1795 {
1796         const spa_t *s1 = a1;
1797         const spa_t *s2 = a2;
1798         int s;
1799 
1800         s = strcmp(s1->spa_name, s2->spa_name);
1801         if (s > 0)
1802                 return (1);
1803         if (s < 0)
1804                 return (-1);
1805         return (0);
1806 }
1807 
1808 int
1809 spa_busy(void)
1810 {
1811         return (spa_active_count);
1812 }
1813 
1814 void
1815 spa_boot_init()
1816 {
1817         spa_config_load();
1818 }
1819 
1820 void
1821 spa_init(int mode)
1822 {
1823         mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1824         mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1825         mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1826         cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1827 
1828         avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1829             offsetof(spa_t, spa_avl));
1830 
1831         avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1832             offsetof(spa_aux_t, aux_avl));
1833 
1834         avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1835             offsetof(spa_aux_t, aux_avl));
1836 
1837         spa_mode_global = mode;
1838 
1839 #ifdef _KERNEL
1840         spa_arch_init();
1841 #else
1842         if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1843                 arc_procfd = open("/proc/self/ctl", O_WRONLY);
1844                 if (arc_procfd == -1) {
1845                         perror("could not enable watchpoints: "
1846                             "opening /proc/self/ctl failed: ");
1847                 } else {
1848                         arc_watch = B_TRUE;
1849                 }
1850         }
1851 #endif
1852 
1853         refcount_init();
1854         unique_init();
1855         range_tree_init();
1856         zio_init();
1857         dmu_init();
1858         zil_init();
1859         vdev_cache_stat_init();
1860         zfs_prop_init();
1861         zpool_prop_init();
1862         zpool_feature_init();
1863         spa_config_load();
1864         l2arc_start();
1865 }
1866 
1867 void
1868 spa_fini(void)
1869 {
1870         l2arc_stop();
1871 
1872         spa_evict_all();
1873 
1874         vdev_cache_stat_fini();
1875         zil_fini();
1876         dmu_fini();
1877         zio_fini();
1878         range_tree_fini();
1879         unique_fini();
1880         refcount_fini();
1881 
1882         avl_destroy(&spa_namespace_avl);
1883         avl_destroy(&spa_spare_avl);
1884         avl_destroy(&spa_l2cache_avl);
1885 
1886         cv_destroy(&spa_namespace_cv);
1887         mutex_destroy(&spa_namespace_lock);
1888         mutex_destroy(&spa_spare_lock);
1889         mutex_destroy(&spa_l2cache_lock);
1890 }
1891 
1892 /*
1893  * Return whether this pool has slogs. No locking needed.
1894  * It's not a problem if the wrong answer is returned as it's only for
1895  * performance and not correctness
1896  */
1897 boolean_t
1898 spa_has_slogs(spa_t *spa)
1899 {
1900         return (spa->spa_log_class->mc_rotor != NULL);
1901 }
1902 
1903 spa_log_state_t
1904 spa_get_log_state(spa_t *spa)
1905 {
1906         return (spa->spa_log_state);
1907 }
1908 
1909 void
1910 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1911 {
1912         spa->spa_log_state = state;
1913 }
1914 
1915 boolean_t
1916 spa_is_root(spa_t *spa)
1917 {
1918         return (spa->spa_is_root);
1919 }
1920 
1921 boolean_t
1922 spa_writeable(spa_t *spa)
1923 {
1924         return (!!(spa->spa_mode & FWRITE));
1925 }
1926 
1927 /*
1928  * Returns true if there is a pending sync task in any of the current
1929  * syncing txg, the current quiescing txg, or the current open txg.
1930  */
1931 boolean_t
1932 spa_has_pending_synctask(spa_t *spa)
1933 {
1934         return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
1935 }
1936 
1937 int
1938 spa_mode(spa_t *spa)
1939 {
1940         return (spa->spa_mode);
1941 }
1942 
1943 uint64_t
1944 spa_bootfs(spa_t *spa)
1945 {
1946         return (spa->spa_bootfs);
1947 }
1948 
1949 uint64_t
1950 spa_delegation(spa_t *spa)
1951 {
1952         return (spa->spa_delegation);
1953 }
1954 
1955 objset_t *
1956 spa_meta_objset(spa_t *spa)
1957 {
1958         return (spa->spa_meta_objset);
1959 }
1960 
1961 enum zio_checksum
1962 spa_dedup_checksum(spa_t *spa)
1963 {
1964         return (spa->spa_dedup_checksum);
1965 }
1966 
1967 /*
1968  * Reset pool scan stat per scan pass (or reboot).
1969  */
1970 void
1971 spa_scan_stat_init(spa_t *spa)
1972 {
1973         /* data not stored on disk */
1974         spa->spa_scan_pass_start = gethrestime_sec();
1975         spa->spa_scan_pass_exam = 0;
1976         vdev_scan_stat_init(spa->spa_root_vdev);
1977 }
1978 
1979 /*
1980  * Get scan stats for zpool status reports
1981  */
1982 int
1983 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1984 {
1985         dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1986 
1987         if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1988                 return (SET_ERROR(ENOENT));
1989         bzero(ps, sizeof (pool_scan_stat_t));
1990 
1991         /* data stored on disk */
1992         ps->pss_func = scn->scn_phys.scn_func;
1993         ps->pss_start_time = scn->scn_phys.scn_start_time;
1994         ps->pss_end_time = scn->scn_phys.scn_end_time;
1995         ps->pss_to_examine = scn->scn_phys.scn_to_examine;
1996         ps->pss_examined = scn->scn_phys.scn_examined;
1997         ps->pss_to_process = scn->scn_phys.scn_to_process;
1998         ps->pss_processed = scn->scn_phys.scn_processed;
1999         ps->pss_errors = scn->scn_phys.scn_errors;
2000         ps->pss_state = scn->scn_phys.scn_state;
2001 
2002         /* data not stored on disk */
2003         ps->pss_pass_start = spa->spa_scan_pass_start;
2004         ps->pss_pass_exam = spa->spa_scan_pass_exam;
2005 
2006         return (0);
2007 }
2008 
2009 boolean_t
2010 spa_debug_enabled(spa_t *spa)
2011 {
2012         return (spa->spa_debug);
2013 }
2014 
2015 int
2016 spa_maxblocksize(spa_t *spa)
2017 {
2018         if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2019                 return (SPA_MAXBLOCKSIZE);
2020         else
2021                 return (SPA_OLD_MAXBLOCKSIZE);
2022 }