1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 229842588bSGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23be6fd75aSMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved. 24e9103aaeSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #include <sys/zfs_context.h> 28fa9e4066Sahrens #include <sys/spa_impl.h> 29283b8460SGeorge.Wilson #include <sys/spa_boot.h> 30fa9e4066Sahrens #include <sys/zio.h> 31fa9e4066Sahrens #include <sys/zio_checksum.h> 32fa9e4066Sahrens #include <sys/zio_compress.h> 33fa9e4066Sahrens #include <sys/dmu.h> 34fa9e4066Sahrens #include <sys/dmu_tx.h> 35fa9e4066Sahrens #include <sys/zap.h> 36fa9e4066Sahrens #include <sys/zil.h> 37fa9e4066Sahrens #include <sys/vdev_impl.h> 38fa9e4066Sahrens #include <sys/metaslab.h> 39fa9e4066Sahrens #include <sys/uberblock_impl.h> 40fa9e4066Sahrens #include <sys/txg.h> 41fa9e4066Sahrens #include <sys/avl.h> 42fa9e4066Sahrens #include <sys/unique.h> 43fa9e4066Sahrens #include <sys/dsl_pool.h> 44fa9e4066Sahrens #include <sys/dsl_dir.h> 45fa9e4066Sahrens #include <sys/dsl_prop.h> 463f9d6ad7SLin Ling #include <sys/dsl_scan.h> 47fa9e4066Sahrens #include <sys/fs/zfs.h> 486ce0521aSperrin #include <sys/metaslab_impl.h> 49e14bb325SJeff Bonwick #include <sys/arc.h> 50485bbbf5SGeorge Wilson #include <sys/ddt.h> 5191ebeef5Sahrens #include "zfs_prop.h" 52ad135b5dSChristopher Siden #include "zfeature_common.h" 53fa9e4066Sahrens 54fa9e4066Sahrens /* 55fa9e4066Sahrens * SPA locking 56fa9e4066Sahrens * 57fa9e4066Sahrens * There are four basic locks for managing spa_t structures: 58fa9e4066Sahrens * 59fa9e4066Sahrens * spa_namespace_lock (global mutex) 60fa9e4066Sahrens * 6144cd46caSbillm * This lock must be acquired to do any of the following: 62fa9e4066Sahrens * 6344cd46caSbillm * - Lookup a spa_t by name 6444cd46caSbillm * - Add or remove a spa_t from the namespace 6544cd46caSbillm * - Increase spa_refcount from non-zero 6644cd46caSbillm * - Check if spa_refcount is zero 6744cd46caSbillm * - Rename a spa_t 68ea8dc4b6Seschrock * - add/remove/attach/detach devices 6944cd46caSbillm * - Held for the duration of create/destroy/import/export 70fa9e4066Sahrens * 7144cd46caSbillm * It does not need to handle recursion. A create or destroy may 7244cd46caSbillm * reference objects (files or zvols) in other pools, but by 7344cd46caSbillm * definition they must have an existing reference, and will never need 7444cd46caSbillm * to lookup a spa_t by name. 75fa9e4066Sahrens * 76fa9e4066Sahrens * spa_refcount (per-spa refcount_t protected by mutex) 77fa9e4066Sahrens * 7844cd46caSbillm * This reference count keep track of any active users of the spa_t. The 7944cd46caSbillm * spa_t cannot be destroyed or freed while this is non-zero. Internally, 8044cd46caSbillm * the refcount is never really 'zero' - opening a pool implicitly keeps 81088f3894Sahrens * some references in the DMU. Internally we check against spa_minref, but 8244cd46caSbillm * present the image of a zero/non-zero value to consumers. 83fa9e4066Sahrens * 84e14bb325SJeff Bonwick * spa_config_lock[] (per-spa array of rwlocks) 85fa9e4066Sahrens * 8691ebeef5Sahrens * This protects the spa_t from config changes, and must be held in 8791ebeef5Sahrens * the following circumstances: 88fa9e4066Sahrens * 8944cd46caSbillm * - RW_READER to perform I/O to the spa 9044cd46caSbillm * - RW_WRITER to change the vdev config 91fa9e4066Sahrens * 92fa9e4066Sahrens * The locking order is fairly straightforward: 93fa9e4066Sahrens * 9444cd46caSbillm * spa_namespace_lock -> spa_refcount 95fa9e4066Sahrens * 9644cd46caSbillm * The namespace lock must be acquired to increase the refcount from 0 9744cd46caSbillm * or to check if it is zero. 98fa9e4066Sahrens * 99e14bb325SJeff Bonwick * spa_refcount -> spa_config_lock[] 100fa9e4066Sahrens * 10144cd46caSbillm * There must be at least one valid reference on the spa_t to acquire 10244cd46caSbillm * the config lock. 103fa9e4066Sahrens * 104e14bb325SJeff Bonwick * spa_namespace_lock -> spa_config_lock[] 105fa9e4066Sahrens * 10644cd46caSbillm * The namespace lock must always be taken before the config lock. 107fa9e4066Sahrens * 108fa9e4066Sahrens * 109e14bb325SJeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible. 110fa9e4066Sahrens * 111e14bb325SJeff Bonwick * The namespace is manipulated using the following functions, all of which 112e14bb325SJeff Bonwick * require the spa_namespace_lock to be held. 113fa9e4066Sahrens * 11444cd46caSbillm * spa_lookup() Lookup a spa_t by name. 115fa9e4066Sahrens * 11644cd46caSbillm * spa_add() Create a new spa_t in the namespace. 117fa9e4066Sahrens * 11844cd46caSbillm * spa_remove() Remove a spa_t from the namespace. This also 11944cd46caSbillm * frees up any memory associated with the spa_t. 120fa9e4066Sahrens * 12144cd46caSbillm * spa_next() Returns the next spa_t in the system, or the 12244cd46caSbillm * first if NULL is passed. 123fa9e4066Sahrens * 12444cd46caSbillm * spa_evict_all() Shutdown and remove all spa_t structures in 12544cd46caSbillm * the system. 126fa9e4066Sahrens * 127ea8dc4b6Seschrock * spa_guid_exists() Determine whether a pool/device guid exists. 128fa9e4066Sahrens * 129fa9e4066Sahrens * The spa_refcount is manipulated using the following functions: 130fa9e4066Sahrens * 13144cd46caSbillm * spa_open_ref() Adds a reference to the given spa_t. Must be 13244cd46caSbillm * called with spa_namespace_lock held if the 13344cd46caSbillm * refcount is currently zero. 134fa9e4066Sahrens * 13544cd46caSbillm * spa_close() Remove a reference from the spa_t. This will 13644cd46caSbillm * not free the spa_t or remove it from the 13744cd46caSbillm * namespace. No locking is required. 138fa9e4066Sahrens * 13944cd46caSbillm * spa_refcount_zero() Returns true if the refcount is currently 14044cd46caSbillm * zero. Must be called with spa_namespace_lock 14144cd46caSbillm * held. 142fa9e4066Sahrens * 143e14bb325SJeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows: 144e14bb325SJeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 145e14bb325SJeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 146e14bb325SJeff Bonwick * 147e14bb325SJeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader. 148e14bb325SJeff Bonwick * To modify the configuration, you must hold all locks as writer. To modify 149e14bb325SJeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline), 150e14bb325SJeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer. 151e14bb325SJeff Bonwick * 152e14bb325SJeff Bonwick * We use these distinct config locks to avoid recursive lock entry. 153e14bb325SJeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 154e14bb325SJeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps 155e14bb325SJeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 156e14bb325SJeff Bonwick * 157e14bb325SJeff Bonwick * The spa config locks cannot be normal rwlocks because we need the 158e14bb325SJeff Bonwick * ability to hand off ownership. For example, SCL_ZIO is acquired 159e14bb325SJeff Bonwick * by the issuing thread and later released by an interrupt thread. 160e14bb325SJeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent 161e14bb325SJeff Bonwick * writer (i.e. system administrator) starvation. 162e14bb325SJeff Bonwick * 163e14bb325SJeff Bonwick * The lock acquisition rules are as follows: 164e14bb325SJeff Bonwick * 165e14bb325SJeff Bonwick * SCL_CONFIG 166e14bb325SJeff Bonwick * Protects changes to the vdev tree topology, such as vdev 167e14bb325SJeff Bonwick * add/remove/attach/detach. Protects the dirty config list 168e14bb325SJeff Bonwick * (spa_config_dirty_list) and the set of spares and l2arc devices. 169e14bb325SJeff Bonwick * 170e14bb325SJeff Bonwick * SCL_STATE 171e14bb325SJeff Bonwick * Protects changes to pool state and vdev state, such as vdev 172e14bb325SJeff Bonwick * online/offline/fault/degrade/clear. Protects the dirty state list 173e14bb325SJeff Bonwick * (spa_state_dirty_list) and global pool state (spa_state). 174e14bb325SJeff Bonwick * 175e14bb325SJeff Bonwick * SCL_ALLOC 176e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 177e14bb325SJeff Bonwick * Held as reader by metaslab_alloc() and metaslab_claim(). 178e14bb325SJeff Bonwick * 179e14bb325SJeff Bonwick * SCL_ZIO 180e14bb325SJeff Bonwick * Held by bp-level zios (those which have no io_vd upon entry) 181e14bb325SJeff Bonwick * to prevent changes to the vdev tree. The bp-level zio implicitly 182e14bb325SJeff Bonwick * protects all of its vdev child zios, which do not hold SCL_ZIO. 183e14bb325SJeff Bonwick * 184e14bb325SJeff Bonwick * SCL_FREE 185e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 186e14bb325SJeff Bonwick * Held as reader by metaslab_free(). SCL_FREE is distinct from 187e14bb325SJeff Bonwick * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 188e14bb325SJeff Bonwick * blocks in zio_done() while another i/o that holds either 189e14bb325SJeff Bonwick * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 190e14bb325SJeff Bonwick * 191e14bb325SJeff Bonwick * SCL_VDEV 192e14bb325SJeff Bonwick * Held as reader to prevent changes to the vdev tree during trivial 193b24ab676SJeff Bonwick * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 194e14bb325SJeff Bonwick * other locks, and lower than all of them, to ensure that it's safe 195e14bb325SJeff Bonwick * to acquire regardless of caller context. 196e14bb325SJeff Bonwick * 197e14bb325SJeff Bonwick * In addition, the following rules apply: 198e14bb325SJeff Bonwick * 199e14bb325SJeff Bonwick * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 200e14bb325SJeff Bonwick * The lock ordering is SCL_CONFIG > spa_props_lock. 201e14bb325SJeff Bonwick * 202e14bb325SJeff Bonwick * (b) I/O operations on leaf vdevs. For any zio operation that takes 203e14bb325SJeff Bonwick * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 204e14bb325SJeff Bonwick * or zio_write_phys() -- the caller must ensure that the config cannot 205e14bb325SJeff Bonwick * cannot change in the interim, and that the vdev cannot be reopened. 206e14bb325SJeff Bonwick * SCL_STATE as reader suffices for both. 207fa9e4066Sahrens * 208ea8dc4b6Seschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 209fa9e4066Sahrens * 21044cd46caSbillm * spa_vdev_enter() Acquire the namespace lock and the config lock 211ea8dc4b6Seschrock * for writing. 212fa9e4066Sahrens * 21344cd46caSbillm * spa_vdev_exit() Release the config lock, wait for all I/O 21444cd46caSbillm * to complete, sync the updated configs to the 215ea8dc4b6Seschrock * cache, and release the namespace lock. 216fa9e4066Sahrens * 217e14bb325SJeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 218e14bb325SJeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 219e14bb325SJeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 220e14bb325SJeff Bonwick * 221ad135b5dSChristopher Siden * spa_rename() is also implemented within this file since it requires 222e14bb325SJeff Bonwick * manipulation of the namespace. 223fa9e4066Sahrens */ 224fa9e4066Sahrens 225fa9e4066Sahrens static avl_tree_t spa_namespace_avl; 226fa9e4066Sahrens kmutex_t spa_namespace_lock; 227fa9e4066Sahrens static kcondvar_t spa_namespace_cv; 2280373e76bSbonwick static int spa_active_count; 229416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP; 230fa9e4066Sahrens 23199653d4eSeschrock static kmutex_t spa_spare_lock; 23239c23413Seschrock static avl_tree_t spa_spare_avl; 233fa94a07fSbrendan static kmutex_t spa_l2cache_lock; 234fa94a07fSbrendan static avl_tree_t spa_l2cache_avl; 23599653d4eSeschrock 236fa9e4066Sahrens kmem_cache_t *spa_buffer_pool; 2378ad4d6ddSJeff Bonwick int spa_mode_global; 238fa9e4066Sahrens 239fa9e4066Sahrens #ifdef ZFS_DEBUG 2403b2aab18SMatthew Ahrens /* Everything except dprintf and spa is on by default in debug builds */ 2413b2aab18SMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA); 242fa9e4066Sahrens #else 243fa9e4066Sahrens int zfs_flags = 0; 244fa9e4066Sahrens #endif 245fa9e4066Sahrens 2460125049cSahrens /* 2470125049cSahrens * zfs_recover can be set to nonzero to attempt to recover from 2480125049cSahrens * otherwise-fatal errors, typically caused by on-disk corruption. When 2490125049cSahrens * set, calls to zfs_panic_recover() will turn into warning messages. 2500125049cSahrens */ 2510125049cSahrens int zfs_recover = 0; 2520125049cSahrens 253*69962b56SMatthew Ahrens /* 254*69962b56SMatthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is 255*69962b56SMatthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the 256*69962b56SMatthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds. 257*69962b56SMatthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that 258*69962b56SMatthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting 259*69962b56SMatthew Ahrens * in a system panic. 260*69962b56SMatthew Ahrens */ 261*69962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL; 262283b8460SGeorge.Wilson 263283b8460SGeorge.Wilson /* 264*69962b56SMatthew Ahrens * Check time in milliseconds. This defines the frequency at which we check 265*69962b56SMatthew Ahrens * for hung I/O. 266283b8460SGeorge.Wilson */ 267*69962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL; 268283b8460SGeorge.Wilson 269283b8460SGeorge.Wilson /* 270283b8460SGeorge.Wilson * Override the zfs deadman behavior via /etc/system. By default the 271283b8460SGeorge.Wilson * deadman is enabled except on VMware and sparc deployments. 272283b8460SGeorge.Wilson */ 273283b8460SGeorge.Wilson int zfs_deadman_enabled = -1; 274283b8460SGeorge.Wilson 275*69962b56SMatthew Ahrens /* 276*69962b56SMatthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which 277*69962b56SMatthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 278*69962b56SMatthew Ahrens * times the size; so just assume that. Add to this the fact that 279*69962b56SMatthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because 280*69962b56SMatthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together, 281*69962b56SMatthew Ahrens * the worst case is: 282*69962b56SMatthew Ahrens * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24 283*69962b56SMatthew Ahrens */ 284*69962b56SMatthew Ahrens int spa_asize_inflation = 24; 285fa9e4066Sahrens 286e05725b1Sbonwick /* 287e05725b1Sbonwick * ========================================================================== 288e05725b1Sbonwick * SPA config locking 289e05725b1Sbonwick * ========================================================================== 290e05725b1Sbonwick */ 291e05725b1Sbonwick static void 292e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa) 293e14bb325SJeff Bonwick { 294e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 295e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 296e14bb325SJeff Bonwick mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 297e14bb325SJeff Bonwick cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 2983b2aab18SMatthew Ahrens refcount_create_untracked(&scl->scl_count); 299e14bb325SJeff Bonwick scl->scl_writer = NULL; 300e14bb325SJeff Bonwick scl->scl_write_wanted = 0; 301e14bb325SJeff Bonwick } 302e05725b1Sbonwick } 303e05725b1Sbonwick 304e05725b1Sbonwick static void 305e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa) 306e14bb325SJeff Bonwick { 307e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 308e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 309e14bb325SJeff Bonwick mutex_destroy(&scl->scl_lock); 310e14bb325SJeff Bonwick cv_destroy(&scl->scl_cv); 311e14bb325SJeff Bonwick refcount_destroy(&scl->scl_count); 312e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL); 313e14bb325SJeff Bonwick ASSERT(scl->scl_write_wanted == 0); 314e14bb325SJeff Bonwick } 315e14bb325SJeff Bonwick } 316e14bb325SJeff Bonwick 317e14bb325SJeff Bonwick int 318e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 319e05725b1Sbonwick { 320e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 321e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 322e14bb325SJeff Bonwick if (!(locks & (1 << i))) 323e14bb325SJeff Bonwick continue; 324e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 325e14bb325SJeff Bonwick if (rw == RW_READER) { 326e14bb325SJeff Bonwick if (scl->scl_writer || scl->scl_write_wanted) { 327e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 328e14bb325SJeff Bonwick spa_config_exit(spa, locks ^ (1 << i), tag); 329e14bb325SJeff Bonwick return (0); 330e14bb325SJeff Bonwick } 331e14bb325SJeff Bonwick } else { 332e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 333e14bb325SJeff Bonwick if (!refcount_is_zero(&scl->scl_count)) { 334e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 335e14bb325SJeff Bonwick spa_config_exit(spa, locks ^ (1 << i), tag); 336e14bb325SJeff Bonwick return (0); 337e14bb325SJeff Bonwick } 338e14bb325SJeff Bonwick scl->scl_writer = curthread; 339e14bb325SJeff Bonwick } 340e14bb325SJeff Bonwick (void) refcount_add(&scl->scl_count, tag); 341e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 342e14bb325SJeff Bonwick } 343e14bb325SJeff Bonwick return (1); 344e05725b1Sbonwick } 345e05725b1Sbonwick 346e05725b1Sbonwick void 347e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 348e05725b1Sbonwick { 349f64c0e34SEric Taylor int wlocks_held = 0; 350f64c0e34SEric Taylor 3513b2aab18SMatthew Ahrens ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 3523b2aab18SMatthew Ahrens 353e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 354e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 355f64c0e34SEric Taylor if (scl->scl_writer == curthread) 356f64c0e34SEric Taylor wlocks_held |= (1 << i); 357e14bb325SJeff Bonwick if (!(locks & (1 << i))) 358e14bb325SJeff Bonwick continue; 359e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 360e14bb325SJeff Bonwick if (rw == RW_READER) { 361e14bb325SJeff Bonwick while (scl->scl_writer || scl->scl_write_wanted) { 362e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 363e14bb325SJeff Bonwick } 364e14bb325SJeff Bonwick } else { 365e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 366e14bb325SJeff Bonwick while (!refcount_is_zero(&scl->scl_count)) { 367e14bb325SJeff Bonwick scl->scl_write_wanted++; 368e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 369e14bb325SJeff Bonwick scl->scl_write_wanted--; 370e14bb325SJeff Bonwick } 371e14bb325SJeff Bonwick scl->scl_writer = curthread; 372e14bb325SJeff Bonwick } 373e14bb325SJeff Bonwick (void) refcount_add(&scl->scl_count, tag); 374e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 375e05725b1Sbonwick } 376f64c0e34SEric Taylor ASSERT(wlocks_held <= locks); 377e05725b1Sbonwick } 378e05725b1Sbonwick 379e05725b1Sbonwick void 380e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag) 381e05725b1Sbonwick { 382e14bb325SJeff Bonwick for (int i = SCL_LOCKS - 1; i >= 0; i--) { 383e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 384e14bb325SJeff Bonwick if (!(locks & (1 << i))) 385e14bb325SJeff Bonwick continue; 386e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 387e14bb325SJeff Bonwick ASSERT(!refcount_is_zero(&scl->scl_count)); 388e14bb325SJeff Bonwick if (refcount_remove(&scl->scl_count, tag) == 0) { 389e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL || 390e14bb325SJeff Bonwick scl->scl_writer == curthread); 391e14bb325SJeff Bonwick scl->scl_writer = NULL; /* OK in either case */ 392e14bb325SJeff Bonwick cv_broadcast(&scl->scl_cv); 393e14bb325SJeff Bonwick } 394e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 395e05725b1Sbonwick } 396e05725b1Sbonwick } 397e05725b1Sbonwick 398e14bb325SJeff Bonwick int 399e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw) 400e05725b1Sbonwick { 401e14bb325SJeff Bonwick int locks_held = 0; 402e05725b1Sbonwick 403e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 404e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 405e14bb325SJeff Bonwick if (!(locks & (1 << i))) 406e14bb325SJeff Bonwick continue; 407e14bb325SJeff Bonwick if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) || 408e14bb325SJeff Bonwick (rw == RW_WRITER && scl->scl_writer == curthread)) 409e14bb325SJeff Bonwick locks_held |= 1 << i; 410e14bb325SJeff Bonwick } 411e14bb325SJeff Bonwick 412e14bb325SJeff Bonwick return (locks_held); 413e05725b1Sbonwick } 414e05725b1Sbonwick 415fa9e4066Sahrens /* 416fa9e4066Sahrens * ========================================================================== 417fa9e4066Sahrens * SPA namespace functions 418fa9e4066Sahrens * ========================================================================== 419fa9e4066Sahrens */ 420fa9e4066Sahrens 421fa9e4066Sahrens /* 422fa9e4066Sahrens * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 423fa9e4066Sahrens * Returns NULL if no matching spa_t is found. 424fa9e4066Sahrens */ 425fa9e4066Sahrens spa_t * 426fa9e4066Sahrens spa_lookup(const char *name) 427fa9e4066Sahrens { 428e14bb325SJeff Bonwick static spa_t search; /* spa_t is large; don't allocate on stack */ 429e14bb325SJeff Bonwick spa_t *spa; 430fa9e4066Sahrens avl_index_t where; 43140feaa91Sahrens char *cp; 432fa9e4066Sahrens 433fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 434fa9e4066Sahrens 4353b2aab18SMatthew Ahrens (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 4363b2aab18SMatthew Ahrens 43740feaa91Sahrens /* 43840feaa91Sahrens * If it's a full dataset name, figure out the pool name and 43940feaa91Sahrens * just use that. 44040feaa91Sahrens */ 4413b2aab18SMatthew Ahrens cp = strpbrk(search.spa_name, "/@"); 4423b2aab18SMatthew Ahrens if (cp != NULL) 44340feaa91Sahrens *cp = '\0'; 44440feaa91Sahrens 445fa9e4066Sahrens spa = avl_find(&spa_namespace_avl, &search, &where); 446fa9e4066Sahrens 447fa9e4066Sahrens return (spa); 448fa9e4066Sahrens } 449fa9e4066Sahrens 450283b8460SGeorge.Wilson /* 451283b8460SGeorge.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 452283b8460SGeorge.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 453283b8460SGeorge.Wilson * looking for potentially hung I/Os. 454283b8460SGeorge.Wilson */ 455283b8460SGeorge.Wilson void 456283b8460SGeorge.Wilson spa_deadman(void *arg) 457283b8460SGeorge.Wilson { 458283b8460SGeorge.Wilson spa_t *spa = arg; 459283b8460SGeorge.Wilson 460283b8460SGeorge.Wilson zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 461283b8460SGeorge.Wilson (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 462283b8460SGeorge.Wilson ++spa->spa_deadman_calls); 463283b8460SGeorge.Wilson if (zfs_deadman_enabled) 464283b8460SGeorge.Wilson vdev_deadman(spa->spa_root_vdev); 465283b8460SGeorge.Wilson } 466283b8460SGeorge.Wilson 467fa9e4066Sahrens /* 468fa9e4066Sahrens * Create an uninitialized spa_t with the given name. Requires 469fa9e4066Sahrens * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 470fa9e4066Sahrens * exist by calling spa_lookup() first. 471fa9e4066Sahrens */ 472fa9e4066Sahrens spa_t * 473468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot) 474fa9e4066Sahrens { 475fa9e4066Sahrens spa_t *spa; 476c5904d13Seschrock spa_config_dirent_t *dp; 477283b8460SGeorge.Wilson cyc_handler_t hdlr; 478283b8460SGeorge.Wilson cyc_time_t when; 479fa9e4066Sahrens 480fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 481fa9e4066Sahrens 482fa9e4066Sahrens spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 483fa9e4066Sahrens 484c25056deSgw mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 485c25056deSgw mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 48635a5a358SJonathan Adams mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 487c25056deSgw mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 48835a5a358SJonathan Adams mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 489c25056deSgw mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 49035a5a358SJonathan Adams mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 491a1521560SJeff Bonwick mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 492a1521560SJeff Bonwick mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 493c3a66015SMatthew Ahrens mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL); 494c25056deSgw 495c25056deSgw cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 49635a5a358SJonathan Adams cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 497c25056deSgw cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 498e14bb325SJeff Bonwick cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 499c25056deSgw 500b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 501cde58dbcSMatthew Ahrens bplist_create(&spa->spa_free_bplist[t]); 502b24ab676SJeff Bonwick 503e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 504fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 505fa9e4066Sahrens spa->spa_freeze_txg = UINT64_MAX; 5060373e76bSbonwick spa->spa_final_txg = UINT64_MAX; 507468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 50835a5a358SJonathan Adams spa->spa_proc = &p0; 50935a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 510fa9e4066Sahrens 511283b8460SGeorge.Wilson hdlr.cyh_func = spa_deadman; 512283b8460SGeorge.Wilson hdlr.cyh_arg = spa; 513283b8460SGeorge.Wilson hdlr.cyh_level = CY_LOW_LEVEL; 514283b8460SGeorge.Wilson 515*69962b56SMatthew Ahrens spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 516283b8460SGeorge.Wilson 517283b8460SGeorge.Wilson /* 518283b8460SGeorge.Wilson * This determines how often we need to check for hung I/Os after 519283b8460SGeorge.Wilson * the cyclic has already fired. Since checking for hung I/Os is 520283b8460SGeorge.Wilson * an expensive operation we don't want to check too frequently. 521*69962b56SMatthew Ahrens * Instead wait for 5 seconds before checking again. 522283b8460SGeorge.Wilson */ 523*69962b56SMatthew Ahrens when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms); 524283b8460SGeorge.Wilson when.cyt_when = CY_INFINITY; 525283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 526283b8460SGeorge.Wilson spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 527283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 528283b8460SGeorge.Wilson 529fa9e4066Sahrens refcount_create(&spa->spa_refcount); 530e14bb325SJeff Bonwick spa_config_lock_init(spa); 531fa9e4066Sahrens 532fa9e4066Sahrens avl_add(&spa_namespace_avl, spa); 533fa9e4066Sahrens 5340373e76bSbonwick /* 5350373e76bSbonwick * Set the alternate root, if there is one. 5360373e76bSbonwick */ 5370373e76bSbonwick if (altroot) { 5380373e76bSbonwick spa->spa_root = spa_strdup(altroot); 5390373e76bSbonwick spa_active_count++; 5400373e76bSbonwick } 5410373e76bSbonwick 542c5904d13Seschrock /* 543c5904d13Seschrock * Every pool starts with the default cachefile 544c5904d13Seschrock */ 545c5904d13Seschrock list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 546c5904d13Seschrock offsetof(spa_config_dirent_t, scd_link)); 547c5904d13Seschrock 548c5904d13Seschrock dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 549ef912c80STim Haley dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 550c5904d13Seschrock list_insert_head(&spa->spa_config_list, dp); 551c5904d13Seschrock 5524b964adaSGeorge Wilson VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 5534b964adaSGeorge Wilson KM_SLEEP) == 0); 5544b964adaSGeorge Wilson 555ad135b5dSChristopher Siden if (config != NULL) { 556ad135b5dSChristopher Siden nvlist_t *features; 557ad135b5dSChristopher Siden 558ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 559ad135b5dSChristopher Siden &features) == 0) { 560ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 561ad135b5dSChristopher Siden 0) == 0); 562ad135b5dSChristopher Siden } 563ad135b5dSChristopher Siden 564468c413aSTim Haley VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 565ad135b5dSChristopher Siden } 566ad135b5dSChristopher Siden 567ad135b5dSChristopher Siden if (spa->spa_label_features == NULL) { 568ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 569ad135b5dSChristopher Siden KM_SLEEP) == 0); 570ad135b5dSChristopher Siden } 571468c413aSTim Haley 572c3a66015SMatthew Ahrens spa->spa_iokstat = kstat_create("zfs", 0, name, 573c3a66015SMatthew Ahrens "disk", KSTAT_TYPE_IO, 1, 0); 574c3a66015SMatthew Ahrens if (spa->spa_iokstat) { 575c3a66015SMatthew Ahrens spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock; 576c3a66015SMatthew Ahrens kstat_install(spa->spa_iokstat); 577c3a66015SMatthew Ahrens } 578c3a66015SMatthew Ahrens 5793b2aab18SMatthew Ahrens spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0); 5803b2aab18SMatthew Ahrens 581fa9e4066Sahrens return (spa); 582fa9e4066Sahrens } 583fa9e4066Sahrens 584fa9e4066Sahrens /* 585fa9e4066Sahrens * Removes a spa_t from the namespace, freeing up any memory used. Requires 586fa9e4066Sahrens * spa_namespace_lock. This is called only after the spa_t has been closed and 587fa9e4066Sahrens * deactivated. 588fa9e4066Sahrens */ 589fa9e4066Sahrens void 590fa9e4066Sahrens spa_remove(spa_t *spa) 591fa9e4066Sahrens { 592c5904d13Seschrock spa_config_dirent_t *dp; 593c5904d13Seschrock 594fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 595fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 596fa9e4066Sahrens 5971195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 5981195e687SMark J Musante 599fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa); 600fa9e4066Sahrens cv_broadcast(&spa_namespace_cv); 601fa9e4066Sahrens 6020373e76bSbonwick if (spa->spa_root) { 603fa9e4066Sahrens spa_strfree(spa->spa_root); 6040373e76bSbonwick spa_active_count--; 6050373e76bSbonwick } 606fa9e4066Sahrens 607c5904d13Seschrock while ((dp = list_head(&spa->spa_config_list)) != NULL) { 608c5904d13Seschrock list_remove(&spa->spa_config_list, dp); 609c5904d13Seschrock if (dp->scd_path != NULL) 610c5904d13Seschrock spa_strfree(dp->scd_path); 611c5904d13Seschrock kmem_free(dp, sizeof (spa_config_dirent_t)); 612c5904d13Seschrock } 613c5904d13Seschrock 614c5904d13Seschrock list_destroy(&spa->spa_config_list); 6152f8aaab3Seschrock 616ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 6174b964adaSGeorge Wilson nvlist_free(spa->spa_load_info); 618fa9e4066Sahrens spa_config_set(spa, NULL); 619fa9e4066Sahrens 620283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 621283b8460SGeorge.Wilson if (spa->spa_deadman_cycid != CYCLIC_NONE) 622283b8460SGeorge.Wilson cyclic_remove(spa->spa_deadman_cycid); 623283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 624283b8460SGeorge.Wilson spa->spa_deadman_cycid = CYCLIC_NONE; 625283b8460SGeorge.Wilson 626fa9e4066Sahrens refcount_destroy(&spa->spa_refcount); 62791ebeef5Sahrens 628e14bb325SJeff Bonwick spa_config_lock_destroy(spa); 629fa9e4066Sahrens 630c3a66015SMatthew Ahrens kstat_delete(spa->spa_iokstat); 631c3a66015SMatthew Ahrens spa->spa_iokstat = NULL; 632c3a66015SMatthew Ahrens 633b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 634cde58dbcSMatthew Ahrens bplist_destroy(&spa->spa_free_bplist[t]); 635b24ab676SJeff Bonwick 636c25056deSgw cv_destroy(&spa->spa_async_cv); 63735a5a358SJonathan Adams cv_destroy(&spa->spa_proc_cv); 638c25056deSgw cv_destroy(&spa->spa_scrub_io_cv); 639e14bb325SJeff Bonwick cv_destroy(&spa->spa_suspend_cv); 640c25056deSgw 6415ad82045Snd mutex_destroy(&spa->spa_async_lock); 642c25056deSgw mutex_destroy(&spa->spa_errlist_lock); 64335a5a358SJonathan Adams mutex_destroy(&spa->spa_errlog_lock); 64406eeb2adSek mutex_destroy(&spa->spa_history_lock); 64535a5a358SJonathan Adams mutex_destroy(&spa->spa_proc_lock); 646b1b8ab34Slling mutex_destroy(&spa->spa_props_lock); 64735a5a358SJonathan Adams mutex_destroy(&spa->spa_scrub_lock); 648e14bb325SJeff Bonwick mutex_destroy(&spa->spa_suspend_lock); 649a1521560SJeff Bonwick mutex_destroy(&spa->spa_vdev_top_lock); 650c3a66015SMatthew Ahrens mutex_destroy(&spa->spa_iokstat_lock); 6515ad82045Snd 652fa9e4066Sahrens kmem_free(spa, sizeof (spa_t)); 653fa9e4066Sahrens } 654fa9e4066Sahrens 655fa9e4066Sahrens /* 656fa9e4066Sahrens * Given a pool, return the next pool in the namespace, or NULL if there is 657fa9e4066Sahrens * none. If 'prev' is NULL, return the first pool. 658fa9e4066Sahrens */ 659fa9e4066Sahrens spa_t * 660fa9e4066Sahrens spa_next(spa_t *prev) 661fa9e4066Sahrens { 662fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 663fa9e4066Sahrens 664fa9e4066Sahrens if (prev) 665fa9e4066Sahrens return (AVL_NEXT(&spa_namespace_avl, prev)); 666fa9e4066Sahrens else 667fa9e4066Sahrens return (avl_first(&spa_namespace_avl)); 668fa9e4066Sahrens } 669fa9e4066Sahrens 670fa9e4066Sahrens /* 671fa9e4066Sahrens * ========================================================================== 672fa9e4066Sahrens * SPA refcount functions 673fa9e4066Sahrens * ========================================================================== 674fa9e4066Sahrens */ 675fa9e4066Sahrens 676fa9e4066Sahrens /* 677fa9e4066Sahrens * Add a reference to the given spa_t. Must have at least one reference, or 678fa9e4066Sahrens * have the namespace lock held. 679fa9e4066Sahrens */ 680fa9e4066Sahrens void 681fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag) 682fa9e4066Sahrens { 683088f3894Sahrens ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref || 684fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 685fa9e4066Sahrens (void) refcount_add(&spa->spa_refcount, tag); 686fa9e4066Sahrens } 687fa9e4066Sahrens 688fa9e4066Sahrens /* 689fa9e4066Sahrens * Remove a reference to the given spa_t. Must have at least one reference, or 690fa9e4066Sahrens * have the namespace lock held. 691fa9e4066Sahrens */ 692fa9e4066Sahrens void 693fa9e4066Sahrens spa_close(spa_t *spa, void *tag) 694fa9e4066Sahrens { 695088f3894Sahrens ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref || 696fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 697fa9e4066Sahrens (void) refcount_remove(&spa->spa_refcount, tag); 698fa9e4066Sahrens } 699fa9e4066Sahrens 700fa9e4066Sahrens /* 701fa9e4066Sahrens * Check to see if the spa refcount is zero. Must be called with 702088f3894Sahrens * spa_namespace_lock held. We really compare against spa_minref, which is the 703fa9e4066Sahrens * number of references acquired when opening a pool 704fa9e4066Sahrens */ 705fa9e4066Sahrens boolean_t 706fa9e4066Sahrens spa_refcount_zero(spa_t *spa) 707fa9e4066Sahrens { 708fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 709fa9e4066Sahrens 710088f3894Sahrens return (refcount_count(&spa->spa_refcount) == spa->spa_minref); 711fa9e4066Sahrens } 712fa9e4066Sahrens 71399653d4eSeschrock /* 71499653d4eSeschrock * ========================================================================== 715fa94a07fSbrendan * SPA spare and l2cache tracking 71699653d4eSeschrock * ========================================================================== 71799653d4eSeschrock */ 71899653d4eSeschrock 719fa94a07fSbrendan /* 720fa94a07fSbrendan * Hot spares and cache devices are tracked using the same code below, 721fa94a07fSbrendan * for 'auxiliary' devices. 722fa94a07fSbrendan */ 723fa94a07fSbrendan 724fa94a07fSbrendan typedef struct spa_aux { 725fa94a07fSbrendan uint64_t aux_guid; 726fa94a07fSbrendan uint64_t aux_pool; 727fa94a07fSbrendan avl_node_t aux_avl; 728fa94a07fSbrendan int aux_count; 729fa94a07fSbrendan } spa_aux_t; 730fa94a07fSbrendan 731fa94a07fSbrendan static int 732fa94a07fSbrendan spa_aux_compare(const void *a, const void *b) 733fa94a07fSbrendan { 734fa94a07fSbrendan const spa_aux_t *sa = a; 735fa94a07fSbrendan const spa_aux_t *sb = b; 736fa94a07fSbrendan 737fa94a07fSbrendan if (sa->aux_guid < sb->aux_guid) 738fa94a07fSbrendan return (-1); 739fa94a07fSbrendan else if (sa->aux_guid > sb->aux_guid) 740fa94a07fSbrendan return (1); 741fa94a07fSbrendan else 742fa94a07fSbrendan return (0); 743fa94a07fSbrendan } 744fa94a07fSbrendan 745fa94a07fSbrendan void 746fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl) 747fa94a07fSbrendan { 748fa94a07fSbrendan avl_index_t where; 749fa94a07fSbrendan spa_aux_t search; 750fa94a07fSbrendan spa_aux_t *aux; 751fa94a07fSbrendan 752fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 753fa94a07fSbrendan if ((aux = avl_find(avl, &search, &where)) != NULL) { 754fa94a07fSbrendan aux->aux_count++; 755fa94a07fSbrendan } else { 756fa94a07fSbrendan aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 757fa94a07fSbrendan aux->aux_guid = vd->vdev_guid; 758fa94a07fSbrendan aux->aux_count = 1; 759fa94a07fSbrendan avl_insert(avl, aux, where); 760fa94a07fSbrendan } 761fa94a07fSbrendan } 762fa94a07fSbrendan 763fa94a07fSbrendan void 764fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 765fa94a07fSbrendan { 766fa94a07fSbrendan spa_aux_t search; 767fa94a07fSbrendan spa_aux_t *aux; 768fa94a07fSbrendan avl_index_t where; 769fa94a07fSbrendan 770fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 771fa94a07fSbrendan aux = avl_find(avl, &search, &where); 772fa94a07fSbrendan 773fa94a07fSbrendan ASSERT(aux != NULL); 774fa94a07fSbrendan 775fa94a07fSbrendan if (--aux->aux_count == 0) { 776fa94a07fSbrendan avl_remove(avl, aux); 777fa94a07fSbrendan kmem_free(aux, sizeof (spa_aux_t)); 778fa94a07fSbrendan } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 779fa94a07fSbrendan aux->aux_pool = 0ULL; 780fa94a07fSbrendan } 781fa94a07fSbrendan } 782fa94a07fSbrendan 783fa94a07fSbrendan boolean_t 78489a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 785fa94a07fSbrendan { 786fa94a07fSbrendan spa_aux_t search, *found; 787fa94a07fSbrendan 788fa94a07fSbrendan search.aux_guid = guid; 78989a89ebfSlling found = avl_find(avl, &search, NULL); 790fa94a07fSbrendan 791fa94a07fSbrendan if (pool) { 792fa94a07fSbrendan if (found) 793fa94a07fSbrendan *pool = found->aux_pool; 794fa94a07fSbrendan else 795fa94a07fSbrendan *pool = 0ULL; 796fa94a07fSbrendan } 797fa94a07fSbrendan 79889a89ebfSlling if (refcnt) { 79989a89ebfSlling if (found) 80089a89ebfSlling *refcnt = found->aux_count; 80189a89ebfSlling else 80289a89ebfSlling *refcnt = 0; 80389a89ebfSlling } 80489a89ebfSlling 805fa94a07fSbrendan return (found != NULL); 806fa94a07fSbrendan } 807fa94a07fSbrendan 808fa94a07fSbrendan void 809fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 810fa94a07fSbrendan { 811fa94a07fSbrendan spa_aux_t search, *found; 812fa94a07fSbrendan avl_index_t where; 813fa94a07fSbrendan 814fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 815fa94a07fSbrendan found = avl_find(avl, &search, &where); 816fa94a07fSbrendan ASSERT(found != NULL); 817fa94a07fSbrendan ASSERT(found->aux_pool == 0ULL); 818fa94a07fSbrendan 819fa94a07fSbrendan found->aux_pool = spa_guid(vd->vdev_spa); 820fa94a07fSbrendan } 821fa94a07fSbrendan 82299653d4eSeschrock /* 82339c23413Seschrock * Spares are tracked globally due to the following constraints: 82439c23413Seschrock * 82539c23413Seschrock * - A spare may be part of multiple pools. 82639c23413Seschrock * - A spare may be added to a pool even if it's actively in use within 82739c23413Seschrock * another pool. 82839c23413Seschrock * - A spare in use in any pool can only be the source of a replacement if 82939c23413Seschrock * the target is a spare in the same pool. 83039c23413Seschrock * 83139c23413Seschrock * We keep track of all spares on the system through the use of a reference 83239c23413Seschrock * counted AVL tree. When a vdev is added as a spare, or used as a replacement 83339c23413Seschrock * spare, then we bump the reference count in the AVL tree. In addition, we set 83439c23413Seschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or 83539c23413Seschrock * inactive). When a spare is made active (used to replace a device in the 83639c23413Seschrock * pool), we also keep track of which pool its been made a part of. 83739c23413Seschrock * 83839c23413Seschrock * The 'spa_spare_lock' protects the AVL tree. These functions are normally 83939c23413Seschrock * called under the spa_namespace lock as part of vdev reconfiguration. The 84039c23413Seschrock * separate spare lock exists for the status query path, which does not need to 84139c23413Seschrock * be completely consistent with respect to other vdev configuration changes. 84299653d4eSeschrock */ 84339c23413Seschrock 84499653d4eSeschrock static int 84599653d4eSeschrock spa_spare_compare(const void *a, const void *b) 84699653d4eSeschrock { 847fa94a07fSbrendan return (spa_aux_compare(a, b)); 84899653d4eSeschrock } 84999653d4eSeschrock 85099653d4eSeschrock void 85139c23413Seschrock spa_spare_add(vdev_t *vd) 85299653d4eSeschrock { 85399653d4eSeschrock mutex_enter(&spa_spare_lock); 85439c23413Seschrock ASSERT(!vd->vdev_isspare); 855fa94a07fSbrendan spa_aux_add(vd, &spa_spare_avl); 85639c23413Seschrock vd->vdev_isspare = B_TRUE; 85799653d4eSeschrock mutex_exit(&spa_spare_lock); 85899653d4eSeschrock } 85999653d4eSeschrock 86099653d4eSeschrock void 86139c23413Seschrock spa_spare_remove(vdev_t *vd) 86299653d4eSeschrock { 86399653d4eSeschrock mutex_enter(&spa_spare_lock); 86439c23413Seschrock ASSERT(vd->vdev_isspare); 865fa94a07fSbrendan spa_aux_remove(vd, &spa_spare_avl); 86639c23413Seschrock vd->vdev_isspare = B_FALSE; 86799653d4eSeschrock mutex_exit(&spa_spare_lock); 86899653d4eSeschrock } 86999653d4eSeschrock 87099653d4eSeschrock boolean_t 87189a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 87299653d4eSeschrock { 873fa94a07fSbrendan boolean_t found; 87499653d4eSeschrock 87599653d4eSeschrock mutex_enter(&spa_spare_lock); 87689a89ebfSlling found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 87799653d4eSeschrock mutex_exit(&spa_spare_lock); 87899653d4eSeschrock 879fa94a07fSbrendan return (found); 88039c23413Seschrock } 88139c23413Seschrock 88239c23413Seschrock void 88339c23413Seschrock spa_spare_activate(vdev_t *vd) 88439c23413Seschrock { 88539c23413Seschrock mutex_enter(&spa_spare_lock); 88639c23413Seschrock ASSERT(vd->vdev_isspare); 887fa94a07fSbrendan spa_aux_activate(vd, &spa_spare_avl); 888fa94a07fSbrendan mutex_exit(&spa_spare_lock); 889fa94a07fSbrendan } 89039c23413Seschrock 891fa94a07fSbrendan /* 892fa94a07fSbrendan * Level 2 ARC devices are tracked globally for the same reasons as spares. 893fa94a07fSbrendan * Cache devices currently only support one pool per cache device, and so 894fa94a07fSbrendan * for these devices the aux reference count is currently unused beyond 1. 895fa94a07fSbrendan */ 89639c23413Seschrock 897fa94a07fSbrendan static int 898fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b) 899fa94a07fSbrendan { 900fa94a07fSbrendan return (spa_aux_compare(a, b)); 901fa94a07fSbrendan } 902fa94a07fSbrendan 903fa94a07fSbrendan void 904fa94a07fSbrendan spa_l2cache_add(vdev_t *vd) 905fa94a07fSbrendan { 906fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 907fa94a07fSbrendan ASSERT(!vd->vdev_isl2cache); 908fa94a07fSbrendan spa_aux_add(vd, &spa_l2cache_avl); 909fa94a07fSbrendan vd->vdev_isl2cache = B_TRUE; 910fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 911fa94a07fSbrendan } 912fa94a07fSbrendan 913fa94a07fSbrendan void 914fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd) 915fa94a07fSbrendan { 916fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 917fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 918fa94a07fSbrendan spa_aux_remove(vd, &spa_l2cache_avl); 919fa94a07fSbrendan vd->vdev_isl2cache = B_FALSE; 920fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 921fa94a07fSbrendan } 922fa94a07fSbrendan 923fa94a07fSbrendan boolean_t 924fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool) 925fa94a07fSbrendan { 926fa94a07fSbrendan boolean_t found; 927fa94a07fSbrendan 928fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 92989a89ebfSlling found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 930fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 931fa94a07fSbrendan 932fa94a07fSbrendan return (found); 933fa94a07fSbrendan } 934fa94a07fSbrendan 935fa94a07fSbrendan void 936fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd) 937fa94a07fSbrendan { 938fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 939fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 940fa94a07fSbrendan spa_aux_activate(vd, &spa_l2cache_avl); 941fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 942fa94a07fSbrendan } 943fa94a07fSbrendan 944fa9e4066Sahrens /* 945fa9e4066Sahrens * ========================================================================== 946fa9e4066Sahrens * SPA vdev locking 947fa9e4066Sahrens * ========================================================================== 948fa9e4066Sahrens */ 949fa9e4066Sahrens 950fa9e4066Sahrens /* 951ea8dc4b6Seschrock * Lock the given spa_t for the purpose of adding or removing a vdev. 952ea8dc4b6Seschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing. 953fa9e4066Sahrens * It returns the next transaction group for the spa_t. 954fa9e4066Sahrens */ 955fa9e4066Sahrens uint64_t 956fa9e4066Sahrens spa_vdev_enter(spa_t *spa) 957fa9e4066Sahrens { 958a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 959bbfd46c4SJeff Bonwick mutex_enter(&spa_namespace_lock); 96088ecc943SGeorge Wilson return (spa_vdev_config_enter(spa)); 96188ecc943SGeorge Wilson } 96288ecc943SGeorge Wilson 96388ecc943SGeorge Wilson /* 96488ecc943SGeorge Wilson * Internal implementation for spa_vdev_enter(). Used when a vdev 96588ecc943SGeorge Wilson * operation requires multiple syncs (i.e. removing a device) while 96688ecc943SGeorge Wilson * keeping the spa_namespace_lock held. 96788ecc943SGeorge Wilson */ 96888ecc943SGeorge Wilson uint64_t 96988ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa) 97088ecc943SGeorge Wilson { 97188ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 9723d7072f8Seschrock 973e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 974fa9e4066Sahrens 975fa9e4066Sahrens return (spa_last_synced_txg(spa) + 1); 976fa9e4066Sahrens } 977fa9e4066Sahrens 978fa9e4066Sahrens /* 97988ecc943SGeorge Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing 98088ecc943SGeorge Wilson * of multiple transactions without releasing the spa_namespace_lock. 981fa9e4066Sahrens */ 98288ecc943SGeorge Wilson void 98388ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 984fa9e4066Sahrens { 98588ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 98688ecc943SGeorge Wilson 9870e34b6a7Sbonwick int config_changed = B_FALSE; 988ea8dc4b6Seschrock 9890373e76bSbonwick ASSERT(txg > spa_last_synced_txg(spa)); 9900e34b6a7Sbonwick 991e14bb325SJeff Bonwick spa->spa_pending_vdev = NULL; 992e14bb325SJeff Bonwick 9930e34b6a7Sbonwick /* 9940e34b6a7Sbonwick * Reassess the DTLs. 9950e34b6a7Sbonwick */ 9960373e76bSbonwick vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 9970e34b6a7Sbonwick 998e14bb325SJeff Bonwick if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 9990e34b6a7Sbonwick config_changed = B_TRUE; 10008f18d1faSGeorge Wilson spa->spa_config_generation++; 10010e34b6a7Sbonwick } 1002ea8dc4b6Seschrock 100388ecc943SGeorge Wilson /* 100488ecc943SGeorge Wilson * Verify the metaslab classes. 100588ecc943SGeorge Wilson */ 1006b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1007b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 100888ecc943SGeorge Wilson 1009e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1010fa9e4066Sahrens 101188ecc943SGeorge Wilson /* 101288ecc943SGeorge Wilson * Panic the system if the specified tag requires it. This 101388ecc943SGeorge Wilson * is useful for ensuring that configurations are updated 101488ecc943SGeorge Wilson * transactionally. 101588ecc943SGeorge Wilson */ 101688ecc943SGeorge Wilson if (zio_injection_enabled) 10171195e687SMark J Musante zio_handle_panic_injection(spa, tag, 0); 101888ecc943SGeorge Wilson 1019fa9e4066Sahrens /* 1020fa9e4066Sahrens * Note: this txg_wait_synced() is important because it ensures 1021fa9e4066Sahrens * that there won't be more than one config change per txg. 1022fa9e4066Sahrens * This allows us to use the txg as the generation number. 1023fa9e4066Sahrens */ 1024fa9e4066Sahrens if (error == 0) 1025fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1026fa9e4066Sahrens 1027fa9e4066Sahrens if (vd != NULL) { 10288ad4d6ddSJeff Bonwick ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0); 10298ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1030fa9e4066Sahrens vdev_free(vd); 10318ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1032fa9e4066Sahrens } 1033fa9e4066Sahrens 1034fa9e4066Sahrens /* 10350e34b6a7Sbonwick * If the config changed, update the config cache. 1036fa9e4066Sahrens */ 10370e34b6a7Sbonwick if (config_changed) 1038c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 103988ecc943SGeorge Wilson } 1040ea8dc4b6Seschrock 104188ecc943SGeorge Wilson /* 104288ecc943SGeorge Wilson * Unlock the spa_t after adding or removing a vdev. Besides undoing the 104388ecc943SGeorge Wilson * locking of spa_vdev_enter(), we also want make sure the transactions have 104488ecc943SGeorge Wilson * synced to disk, and then update the global configuration cache with the new 104588ecc943SGeorge Wilson * information. 104688ecc943SGeorge Wilson */ 104788ecc943SGeorge Wilson int 104888ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 104988ecc943SGeorge Wilson { 105088ecc943SGeorge Wilson spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1051ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1052bbfd46c4SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 1053fa9e4066Sahrens 1054fa9e4066Sahrens return (error); 1055fa9e4066Sahrens } 1056fa9e4066Sahrens 1057e14bb325SJeff Bonwick /* 1058e14bb325SJeff Bonwick * Lock the given spa_t for the purpose of changing vdev state. 1059e14bb325SJeff Bonwick */ 1060e14bb325SJeff Bonwick void 10618f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks) 1062e14bb325SJeff Bonwick { 10638f18d1faSGeorge Wilson int locks = SCL_STATE_ALL | oplocks; 10648f18d1faSGeorge Wilson 1065dcba9f3fSGeorge Wilson /* 1066dcba9f3fSGeorge Wilson * Root pools may need to read of the underlying devfs filesystem 1067dcba9f3fSGeorge Wilson * when opening up a vdev. Unfortunately if we're holding the 1068dcba9f3fSGeorge Wilson * SCL_ZIO lock it will result in a deadlock when we try to issue 1069dcba9f3fSGeorge Wilson * the read from the root filesystem. Instead we "prefetch" 1070dcba9f3fSGeorge Wilson * the associated vnodes that we need prior to opening the 1071dcba9f3fSGeorge Wilson * underlying devices and cache them so that we can prevent 1072dcba9f3fSGeorge Wilson * any I/O when we are doing the actual open. 1073dcba9f3fSGeorge Wilson */ 1074dcba9f3fSGeorge Wilson if (spa_is_root(spa)) { 10759842588bSGeorge Wilson int low = locks & ~(SCL_ZIO - 1); 10769842588bSGeorge Wilson int high = locks & ~low; 10779842588bSGeorge Wilson 10789842588bSGeorge Wilson spa_config_enter(spa, high, spa, RW_WRITER); 1079dcba9f3fSGeorge Wilson vdev_hold(spa->spa_root_vdev); 10809842588bSGeorge Wilson spa_config_enter(spa, low, spa, RW_WRITER); 1081dcba9f3fSGeorge Wilson } else { 1082dcba9f3fSGeorge Wilson spa_config_enter(spa, locks, spa, RW_WRITER); 1083dcba9f3fSGeorge Wilson } 10848f18d1faSGeorge Wilson spa->spa_vdev_locks = locks; 1085e14bb325SJeff Bonwick } 1086e14bb325SJeff Bonwick 1087e14bb325SJeff Bonwick int 1088e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1089e14bb325SJeff Bonwick { 1090c6065d0fSGeorge Wilson boolean_t config_changed = B_FALSE; 1091c6065d0fSGeorge Wilson 1092b24ab676SJeff Bonwick if (vd != NULL || error == 0) 1093b24ab676SJeff Bonwick vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1094b24ab676SJeff Bonwick 0, 0, B_FALSE); 1095b24ab676SJeff Bonwick 10968f18d1faSGeorge Wilson if (vd != NULL) { 1097e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 1098c6065d0fSGeorge Wilson config_changed = B_TRUE; 10998f18d1faSGeorge Wilson spa->spa_config_generation++; 11008f18d1faSGeorge Wilson } 1101e14bb325SJeff Bonwick 1102dcba9f3fSGeorge Wilson if (spa_is_root(spa)) 1103dcba9f3fSGeorge Wilson vdev_rele(spa->spa_root_vdev); 1104dcba9f3fSGeorge Wilson 11058f18d1faSGeorge Wilson ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 11068f18d1faSGeorge Wilson spa_config_exit(spa, spa->spa_vdev_locks, spa); 1107e14bb325SJeff Bonwick 11088ad4d6ddSJeff Bonwick /* 11098ad4d6ddSJeff Bonwick * If anything changed, wait for it to sync. This ensures that, 11108ad4d6ddSJeff Bonwick * from the system administrator's perspective, zpool(1M) commands 11118ad4d6ddSJeff Bonwick * are synchronous. This is important for things like zpool offline: 11128ad4d6ddSJeff Bonwick * when the command completes, you expect no further I/O from ZFS. 11138ad4d6ddSJeff Bonwick */ 11148ad4d6ddSJeff Bonwick if (vd != NULL) 11158ad4d6ddSJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, 0); 11168ad4d6ddSJeff Bonwick 1117c6065d0fSGeorge Wilson /* 1118c6065d0fSGeorge Wilson * If the config changed, update the config cache. 1119c6065d0fSGeorge Wilson */ 1120c6065d0fSGeorge Wilson if (config_changed) { 1121c6065d0fSGeorge Wilson mutex_enter(&spa_namespace_lock); 1122c6065d0fSGeorge Wilson spa_config_sync(spa, B_FALSE, B_TRUE); 1123c6065d0fSGeorge Wilson mutex_exit(&spa_namespace_lock); 1124c6065d0fSGeorge Wilson } 1125c6065d0fSGeorge Wilson 1126e14bb325SJeff Bonwick return (error); 1127e14bb325SJeff Bonwick } 1128e14bb325SJeff Bonwick 1129fa9e4066Sahrens /* 1130fa9e4066Sahrens * ========================================================================== 1131fa9e4066Sahrens * Miscellaneous functions 1132fa9e4066Sahrens * ========================================================================== 1133fa9e4066Sahrens */ 1134fa9e4066Sahrens 1135ad135b5dSChristopher Siden void 1136ad135b5dSChristopher Siden spa_activate_mos_feature(spa_t *spa, const char *feature) 1137ad135b5dSChristopher Siden { 1138ad135b5dSChristopher Siden (void) nvlist_add_boolean(spa->spa_label_features, feature); 1139ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 1140ad135b5dSChristopher Siden } 1141ad135b5dSChristopher Siden 1142ad135b5dSChristopher Siden void 1143ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1144ad135b5dSChristopher Siden { 1145ad135b5dSChristopher Siden (void) nvlist_remove_all(spa->spa_label_features, feature); 1146ad135b5dSChristopher Siden vdev_config_dirty(spa->spa_root_vdev); 1147ad135b5dSChristopher Siden } 1148ad135b5dSChristopher Siden 1149fa9e4066Sahrens /* 1150fa9e4066Sahrens * Rename a spa_t. 1151fa9e4066Sahrens */ 1152fa9e4066Sahrens int 1153fa9e4066Sahrens spa_rename(const char *name, const char *newname) 1154fa9e4066Sahrens { 1155fa9e4066Sahrens spa_t *spa; 1156fa9e4066Sahrens int err; 1157fa9e4066Sahrens 1158fa9e4066Sahrens /* 1159fa9e4066Sahrens * Lookup the spa_t and grab the config lock for writing. We need to 1160fa9e4066Sahrens * actually open the pool so that we can sync out the necessary labels. 1161fa9e4066Sahrens * It's OK to call spa_open() with the namespace lock held because we 1162ea8dc4b6Seschrock * allow recursive calls for other reasons. 1163fa9e4066Sahrens */ 1164fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1165fa9e4066Sahrens if ((err = spa_open(name, &spa, FTAG)) != 0) { 1166fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1167fa9e4066Sahrens return (err); 1168fa9e4066Sahrens } 1169fa9e4066Sahrens 1170e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1171fa9e4066Sahrens 1172fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa); 1173e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name)); 1174fa9e4066Sahrens avl_add(&spa_namespace_avl, spa); 1175fa9e4066Sahrens 1176fa9e4066Sahrens /* 1177fa9e4066Sahrens * Sync all labels to disk with the new names by marking the root vdev 1178fa9e4066Sahrens * dirty and waiting for it to sync. It will pick up the new pool name 1179fa9e4066Sahrens * during the sync. 1180fa9e4066Sahrens */ 1181fa9e4066Sahrens vdev_config_dirty(spa->spa_root_vdev); 1182fa9e4066Sahrens 1183e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1184fa9e4066Sahrens 11850373e76bSbonwick txg_wait_synced(spa->spa_dsl_pool, 0); 1186fa9e4066Sahrens 1187fa9e4066Sahrens /* 1188fa9e4066Sahrens * Sync the updated config cache. 1189fa9e4066Sahrens */ 1190c5904d13Seschrock spa_config_sync(spa, B_FALSE, B_TRUE); 1191fa9e4066Sahrens 1192fa9e4066Sahrens spa_close(spa, FTAG); 1193fa9e4066Sahrens 1194fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1195fa9e4066Sahrens 1196fa9e4066Sahrens return (0); 1197fa9e4066Sahrens } 1198fa9e4066Sahrens 1199fa9e4066Sahrens /* 1200f9af39baSGeorge Wilson * Return the spa_t associated with given pool_guid, if it exists. If 1201f9af39baSGeorge Wilson * device_guid is non-zero, determine whether the pool exists *and* contains 1202f9af39baSGeorge Wilson * a device with the specified device_guid. 1203fa9e4066Sahrens */ 1204f9af39baSGeorge Wilson spa_t * 1205f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1206fa9e4066Sahrens { 1207fa9e4066Sahrens spa_t *spa; 1208fa9e4066Sahrens avl_tree_t *t = &spa_namespace_avl; 1209fa9e4066Sahrens 1210ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1211fa9e4066Sahrens 1212fa9e4066Sahrens for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1213fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1214fa9e4066Sahrens continue; 1215fa9e4066Sahrens if (spa->spa_root_vdev == NULL) 1216fa9e4066Sahrens continue; 121739c23413Seschrock if (spa_guid(spa) == pool_guid) { 121839c23413Seschrock if (device_guid == 0) 121939c23413Seschrock break; 122039c23413Seschrock 122139c23413Seschrock if (vdev_lookup_by_guid(spa->spa_root_vdev, 122239c23413Seschrock device_guid) != NULL) 122339c23413Seschrock break; 122439c23413Seschrock 122539c23413Seschrock /* 12268654d025Sperrin * Check any devices we may be in the process of adding. 122739c23413Seschrock */ 122839c23413Seschrock if (spa->spa_pending_vdev) { 122939c23413Seschrock if (vdev_lookup_by_guid(spa->spa_pending_vdev, 123039c23413Seschrock device_guid) != NULL) 123139c23413Seschrock break; 123239c23413Seschrock } 123339c23413Seschrock } 1234fa9e4066Sahrens } 1235fa9e4066Sahrens 1236f9af39baSGeorge Wilson return (spa); 1237f9af39baSGeorge Wilson } 1238f9af39baSGeorge Wilson 1239f9af39baSGeorge Wilson /* 1240f9af39baSGeorge Wilson * Determine whether a pool with the given pool_guid exists. 1241f9af39baSGeorge Wilson */ 1242f9af39baSGeorge Wilson boolean_t 1243f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1244f9af39baSGeorge Wilson { 1245f9af39baSGeorge Wilson return (spa_by_guid(pool_guid, device_guid) != NULL); 1246fa9e4066Sahrens } 1247fa9e4066Sahrens 1248fa9e4066Sahrens char * 1249fa9e4066Sahrens spa_strdup(const char *s) 1250fa9e4066Sahrens { 1251fa9e4066Sahrens size_t len; 1252fa9e4066Sahrens char *new; 1253fa9e4066Sahrens 1254fa9e4066Sahrens len = strlen(s); 1255fa9e4066Sahrens new = kmem_alloc(len + 1, KM_SLEEP); 1256fa9e4066Sahrens bcopy(s, new, len); 1257fa9e4066Sahrens new[len] = '\0'; 1258fa9e4066Sahrens 1259fa9e4066Sahrens return (new); 1260fa9e4066Sahrens } 1261fa9e4066Sahrens 1262fa9e4066Sahrens void 1263fa9e4066Sahrens spa_strfree(char *s) 1264fa9e4066Sahrens { 1265fa9e4066Sahrens kmem_free(s, strlen(s) + 1); 1266fa9e4066Sahrens } 1267fa9e4066Sahrens 1268fa9e4066Sahrens uint64_t 1269fa9e4066Sahrens spa_get_random(uint64_t range) 1270fa9e4066Sahrens { 1271fa9e4066Sahrens uint64_t r; 1272fa9e4066Sahrens 1273fa9e4066Sahrens ASSERT(range != 0); 1274fa9e4066Sahrens 1275fa9e4066Sahrens (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1276fa9e4066Sahrens 1277fa9e4066Sahrens return (r % range); 1278fa9e4066Sahrens } 1279fa9e4066Sahrens 12801195e687SMark J Musante uint64_t 12811195e687SMark J Musante spa_generate_guid(spa_t *spa) 12821195e687SMark J Musante { 12831195e687SMark J Musante uint64_t guid = spa_get_random(-1ULL); 12841195e687SMark J Musante 12851195e687SMark J Musante if (spa != NULL) { 12861195e687SMark J Musante while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 12871195e687SMark J Musante guid = spa_get_random(-1ULL); 12881195e687SMark J Musante } else { 12891195e687SMark J Musante while (guid == 0 || spa_guid_exists(guid, 0)) 12901195e687SMark J Musante guid = spa_get_random(-1ULL); 12911195e687SMark J Musante } 12921195e687SMark J Musante 12931195e687SMark J Musante return (guid); 12941195e687SMark J Musante } 12951195e687SMark J Musante 1296fa9e4066Sahrens void 1297b24ab676SJeff Bonwick sprintf_blkptr(char *buf, const blkptr_t *bp) 1298fa9e4066Sahrens { 1299ad135b5dSChristopher Siden char type[256]; 1300f0ba89beSJeff Bonwick char *checksum = NULL; 1301f0ba89beSJeff Bonwick char *compress = NULL; 1302f0ba89beSJeff Bonwick 1303f0ba89beSJeff Bonwick if (bp != NULL) { 1304ad135b5dSChristopher Siden if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1305ad135b5dSChristopher Siden dmu_object_byteswap_t bswap = 1306ad135b5dSChristopher Siden DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1307ad135b5dSChristopher Siden (void) snprintf(type, sizeof (type), "bswap %s %s", 1308ad135b5dSChristopher Siden DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1309ad135b5dSChristopher Siden "metadata" : "data", 1310ad135b5dSChristopher Siden dmu_ot_byteswap[bswap].ob_name); 1311ad135b5dSChristopher Siden } else { 1312ad135b5dSChristopher Siden (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1313ad135b5dSChristopher Siden sizeof (type)); 1314ad135b5dSChristopher Siden } 1315f0ba89beSJeff Bonwick checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 1316f0ba89beSJeff Bonwick compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1317f0ba89beSJeff Bonwick } 1318fa9e4066Sahrens 1319b24ab676SJeff Bonwick SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress); 1320fa9e4066Sahrens } 1321fa9e4066Sahrens 1322fa9e4066Sahrens void 1323fa9e4066Sahrens spa_freeze(spa_t *spa) 1324fa9e4066Sahrens { 1325fa9e4066Sahrens uint64_t freeze_txg = 0; 1326fa9e4066Sahrens 1327e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1328fa9e4066Sahrens if (spa->spa_freeze_txg == UINT64_MAX) { 1329fa9e4066Sahrens freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1330fa9e4066Sahrens spa->spa_freeze_txg = freeze_txg; 1331fa9e4066Sahrens } 1332e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1333fa9e4066Sahrens if (freeze_txg != 0) 1334fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1335fa9e4066Sahrens } 1336fa9e4066Sahrens 13370125049cSahrens void 13380125049cSahrens zfs_panic_recover(const char *fmt, ...) 13390125049cSahrens { 13400125049cSahrens va_list adx; 13410125049cSahrens 13420125049cSahrens va_start(adx, fmt); 13430125049cSahrens vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 13440125049cSahrens va_end(adx); 13450125049cSahrens } 13460125049cSahrens 13473f9d6ad7SLin Ling /* 13483f9d6ad7SLin Ling * This is a stripped-down version of strtoull, suitable only for converting 1349f7170741SWill Andrews * lowercase hexadecimal numbers that don't overflow. 13503f9d6ad7SLin Ling */ 13513f9d6ad7SLin Ling uint64_t 13523f9d6ad7SLin Ling strtonum(const char *str, char **nptr) 13533f9d6ad7SLin Ling { 13543f9d6ad7SLin Ling uint64_t val = 0; 13553f9d6ad7SLin Ling char c; 13563f9d6ad7SLin Ling int digit; 13573f9d6ad7SLin Ling 13583f9d6ad7SLin Ling while ((c = *str) != '\0') { 13593f9d6ad7SLin Ling if (c >= '0' && c <= '9') 13603f9d6ad7SLin Ling digit = c - '0'; 13613f9d6ad7SLin Ling else if (c >= 'a' && c <= 'f') 13623f9d6ad7SLin Ling digit = 10 + c - 'a'; 13633f9d6ad7SLin Ling else 13643f9d6ad7SLin Ling break; 13653f9d6ad7SLin Ling 13663f9d6ad7SLin Ling val *= 16; 13673f9d6ad7SLin Ling val += digit; 13683f9d6ad7SLin Ling 13693f9d6ad7SLin Ling str++; 13703f9d6ad7SLin Ling } 13713f9d6ad7SLin Ling 13723f9d6ad7SLin Ling if (nptr) 13733f9d6ad7SLin Ling *nptr = (char *)str; 13743f9d6ad7SLin Ling 13753f9d6ad7SLin Ling return (val); 13763f9d6ad7SLin Ling } 13773f9d6ad7SLin Ling 1378fa9e4066Sahrens /* 1379fa9e4066Sahrens * ========================================================================== 1380fa9e4066Sahrens * Accessor functions 1381fa9e4066Sahrens * ========================================================================== 1382fa9e4066Sahrens */ 1383fa9e4066Sahrens 1384088f3894Sahrens boolean_t 138588b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa) 1386fa9e4066Sahrens { 138788b7b0f2SMatthew Ahrens return (spa->spa_async_suspended); 1388fa9e4066Sahrens } 1389fa9e4066Sahrens 1390fa9e4066Sahrens dsl_pool_t * 1391fa9e4066Sahrens spa_get_dsl(spa_t *spa) 1392fa9e4066Sahrens { 1393fa9e4066Sahrens return (spa->spa_dsl_pool); 1394fa9e4066Sahrens } 1395fa9e4066Sahrens 1396ad135b5dSChristopher Siden boolean_t 1397ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa) 1398ad135b5dSChristopher Siden { 1399ad135b5dSChristopher Siden return (spa->spa_is_initializing); 1400ad135b5dSChristopher Siden } 1401ad135b5dSChristopher Siden 1402fa9e4066Sahrens blkptr_t * 1403fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa) 1404fa9e4066Sahrens { 1405fa9e4066Sahrens return (&spa->spa_ubsync.ub_rootbp); 1406fa9e4066Sahrens } 1407fa9e4066Sahrens 1408fa9e4066Sahrens void 1409fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1410fa9e4066Sahrens { 1411fa9e4066Sahrens spa->spa_uberblock.ub_rootbp = *bp; 1412fa9e4066Sahrens } 1413fa9e4066Sahrens 1414fa9e4066Sahrens void 1415fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen) 1416fa9e4066Sahrens { 1417fa9e4066Sahrens if (spa->spa_root == NULL) 1418fa9e4066Sahrens buf[0] = '\0'; 1419fa9e4066Sahrens else 1420fa9e4066Sahrens (void) strncpy(buf, spa->spa_root, buflen); 1421fa9e4066Sahrens } 1422fa9e4066Sahrens 1423fa9e4066Sahrens int 1424fa9e4066Sahrens spa_sync_pass(spa_t *spa) 1425fa9e4066Sahrens { 1426fa9e4066Sahrens return (spa->spa_sync_pass); 1427fa9e4066Sahrens } 1428fa9e4066Sahrens 1429fa9e4066Sahrens char * 1430fa9e4066Sahrens spa_name(spa_t *spa) 1431fa9e4066Sahrens { 1432fa9e4066Sahrens return (spa->spa_name); 1433fa9e4066Sahrens } 1434fa9e4066Sahrens 1435fa9e4066Sahrens uint64_t 1436fa9e4066Sahrens spa_guid(spa_t *spa) 1437fa9e4066Sahrens { 1438dfbb9432SGeorge Wilson dsl_pool_t *dp = spa_get_dsl(spa); 1439dfbb9432SGeorge Wilson uint64_t guid; 1440dfbb9432SGeorge Wilson 1441b5989ec7Seschrock /* 1442b5989ec7Seschrock * If we fail to parse the config during spa_load(), we can go through 1443b5989ec7Seschrock * the error path (which posts an ereport) and end up here with no root 1444e9103aaeSGarrett D'Amore * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1445b5989ec7Seschrock * this case. 1446b5989ec7Seschrock */ 1447dfbb9432SGeorge Wilson if (spa->spa_root_vdev == NULL) 1448dfbb9432SGeorge Wilson return (spa->spa_config_guid); 1449dfbb9432SGeorge Wilson 1450dfbb9432SGeorge Wilson guid = spa->spa_last_synced_guid != 0 ? 1451dfbb9432SGeorge Wilson spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1452dfbb9432SGeorge Wilson 1453dfbb9432SGeorge Wilson /* 1454dfbb9432SGeorge Wilson * Return the most recently synced out guid unless we're 1455dfbb9432SGeorge Wilson * in syncing context. 1456dfbb9432SGeorge Wilson */ 1457dfbb9432SGeorge Wilson if (dp && dsl_pool_sync_context(dp)) 1458b5989ec7Seschrock return (spa->spa_root_vdev->vdev_guid); 1459b5989ec7Seschrock else 1460dfbb9432SGeorge Wilson return (guid); 1461e9103aaeSGarrett D'Amore } 1462e9103aaeSGarrett D'Amore 1463e9103aaeSGarrett D'Amore uint64_t 1464e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa) 1465e9103aaeSGarrett D'Amore { 1466e9103aaeSGarrett D'Amore /* 1467e9103aaeSGarrett D'Amore * This is a GUID that exists solely as a reference for the 1468e9103aaeSGarrett D'Amore * purposes of the arc. It is generated at load time, and 1469e9103aaeSGarrett D'Amore * is never written to persistent storage. 1470e9103aaeSGarrett D'Amore */ 1471e9103aaeSGarrett D'Amore return (spa->spa_load_guid); 1472fa9e4066Sahrens } 1473fa9e4066Sahrens 1474fa9e4066Sahrens uint64_t 1475fa9e4066Sahrens spa_last_synced_txg(spa_t *spa) 1476fa9e4066Sahrens { 1477fa9e4066Sahrens return (spa->spa_ubsync.ub_txg); 1478fa9e4066Sahrens } 1479fa9e4066Sahrens 1480fa9e4066Sahrens uint64_t 1481fa9e4066Sahrens spa_first_txg(spa_t *spa) 1482fa9e4066Sahrens { 1483fa9e4066Sahrens return (spa->spa_first_txg); 1484fa9e4066Sahrens } 1485fa9e4066Sahrens 1486b24ab676SJeff Bonwick uint64_t 1487b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa) 1488b24ab676SJeff Bonwick { 1489b24ab676SJeff Bonwick return (spa->spa_syncing_txg); 1490b24ab676SJeff Bonwick } 1491b24ab676SJeff Bonwick 149288b7b0f2SMatthew Ahrens pool_state_t 1493fa9e4066Sahrens spa_state(spa_t *spa) 1494fa9e4066Sahrens { 1495fa9e4066Sahrens return (spa->spa_state); 1496fa9e4066Sahrens } 1497fa9e4066Sahrens 1498b16da2e2SGeorge Wilson spa_load_state_t 1499b16da2e2SGeorge Wilson spa_load_state(spa_t *spa) 1500b16da2e2SGeorge Wilson { 1501b16da2e2SGeorge Wilson return (spa->spa_load_state); 1502b16da2e2SGeorge Wilson } 1503b16da2e2SGeorge Wilson 1504fa9e4066Sahrens uint64_t 1505fa9e4066Sahrens spa_freeze_txg(spa_t *spa) 1506fa9e4066Sahrens { 1507fa9e4066Sahrens return (spa->spa_freeze_txg); 1508fa9e4066Sahrens } 1509fa9e4066Sahrens 1510fa9e4066Sahrens /* ARGSUSED */ 1511fa9e4066Sahrens uint64_t 1512fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize) 1513fa9e4066Sahrens { 1514*69962b56SMatthew Ahrens return (lsize * spa_asize_inflation); 151544cd46caSbillm } 151644cd46caSbillm 1517485bbbf5SGeorge Wilson uint64_t 1518485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa) 1519485bbbf5SGeorge Wilson { 1520485bbbf5SGeorge Wilson return (spa->spa_dspace); 1521485bbbf5SGeorge Wilson } 1522485bbbf5SGeorge Wilson 1523485bbbf5SGeorge Wilson void 1524485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa) 1525485bbbf5SGeorge Wilson { 1526485bbbf5SGeorge Wilson spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1527485bbbf5SGeorge Wilson ddt_get_dedup_dspace(spa); 1528485bbbf5SGeorge Wilson } 1529485bbbf5SGeorge Wilson 15300a4e9518Sgw /* 15310a4e9518Sgw * Return the failure mode that has been set to this pool. The default 15320a4e9518Sgw * behavior will be to block all I/Os when a complete failure occurs. 15330a4e9518Sgw */ 15340a4e9518Sgw uint8_t 15350a4e9518Sgw spa_get_failmode(spa_t *spa) 15360a4e9518Sgw { 15370a4e9518Sgw return (spa->spa_failmode); 15380a4e9518Sgw } 15390a4e9518Sgw 1540e14bb325SJeff Bonwick boolean_t 1541e14bb325SJeff Bonwick spa_suspended(spa_t *spa) 1542e14bb325SJeff Bonwick { 1543e14bb325SJeff Bonwick return (spa->spa_suspended); 1544e14bb325SJeff Bonwick } 1545e14bb325SJeff Bonwick 154644cd46caSbillm uint64_t 154744cd46caSbillm spa_version(spa_t *spa) 154844cd46caSbillm { 154944cd46caSbillm return (spa->spa_ubsync.ub_version); 155044cd46caSbillm } 155144cd46caSbillm 1552b24ab676SJeff Bonwick boolean_t 1553b24ab676SJeff Bonwick spa_deflate(spa_t *spa) 1554b24ab676SJeff Bonwick { 1555b24ab676SJeff Bonwick return (spa->spa_deflate); 1556b24ab676SJeff Bonwick } 1557b24ab676SJeff Bonwick 1558b24ab676SJeff Bonwick metaslab_class_t * 1559b24ab676SJeff Bonwick spa_normal_class(spa_t *spa) 1560b24ab676SJeff Bonwick { 1561b24ab676SJeff Bonwick return (spa->spa_normal_class); 1562b24ab676SJeff Bonwick } 1563b24ab676SJeff Bonwick 1564b24ab676SJeff Bonwick metaslab_class_t * 1565b24ab676SJeff Bonwick spa_log_class(spa_t *spa) 1566b24ab676SJeff Bonwick { 1567b24ab676SJeff Bonwick return (spa->spa_log_class); 1568b24ab676SJeff Bonwick } 1569b24ab676SJeff Bonwick 157044cd46caSbillm int 157144cd46caSbillm spa_max_replication(spa_t *spa) 157244cd46caSbillm { 157344cd46caSbillm /* 1574e7437265Sahrens * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 157544cd46caSbillm * handle BPs with more than one DVA allocated. Set our max 157644cd46caSbillm * replication level accordingly. 1577fa9e4066Sahrens */ 1578e7437265Sahrens if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 157944cd46caSbillm return (1); 158044cd46caSbillm return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1581fa9e4066Sahrens } 1582fa9e4066Sahrens 15833f9d6ad7SLin Ling int 15843f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa) 15853f9d6ad7SLin Ling { 15863f9d6ad7SLin Ling return (spa->spa_prev_software_version); 15873f9d6ad7SLin Ling } 15883f9d6ad7SLin Ling 1589283b8460SGeorge.Wilson uint64_t 1590283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa) 1591283b8460SGeorge.Wilson { 1592283b8460SGeorge.Wilson return (spa->spa_deadman_synctime); 1593283b8460SGeorge.Wilson } 1594283b8460SGeorge.Wilson 159599653d4eSeschrock uint64_t 1596b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 159799653d4eSeschrock { 1598b24ab676SJeff Bonwick uint64_t asize = DVA_GET_ASIZE(dva); 1599b24ab676SJeff Bonwick uint64_t dsize = asize; 160099653d4eSeschrock 1601b24ab676SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 160299653d4eSeschrock 1603b24ab676SJeff Bonwick if (asize != 0 && spa->spa_deflate) { 1604b24ab676SJeff Bonwick vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 1605b24ab676SJeff Bonwick dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 160699653d4eSeschrock } 1607b24ab676SJeff Bonwick 1608b24ab676SJeff Bonwick return (dsize); 1609b24ab676SJeff Bonwick } 1610b24ab676SJeff Bonwick 1611b24ab676SJeff Bonwick uint64_t 1612b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 1613b24ab676SJeff Bonwick { 1614b24ab676SJeff Bonwick uint64_t dsize = 0; 1615b24ab676SJeff Bonwick 1616b24ab676SJeff Bonwick for (int d = 0; d < SPA_DVAS_PER_BP; d++) 1617b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1618b24ab676SJeff Bonwick 1619b24ab676SJeff Bonwick return (dsize); 1620b24ab676SJeff Bonwick } 1621b24ab676SJeff Bonwick 1622b24ab676SJeff Bonwick uint64_t 1623b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp) 1624b24ab676SJeff Bonwick { 1625b24ab676SJeff Bonwick uint64_t dsize = 0; 1626b24ab676SJeff Bonwick 1627b24ab676SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1628b24ab676SJeff Bonwick 1629b24ab676SJeff Bonwick for (int d = 0; d < SPA_DVAS_PER_BP; d++) 1630b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1631b24ab676SJeff Bonwick 1632e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 1633b24ab676SJeff Bonwick 1634b24ab676SJeff Bonwick return (dsize); 163599653d4eSeschrock } 163699653d4eSeschrock 1637fa9e4066Sahrens /* 1638fa9e4066Sahrens * ========================================================================== 1639fa9e4066Sahrens * Initialization and Termination 1640fa9e4066Sahrens * ========================================================================== 1641fa9e4066Sahrens */ 1642fa9e4066Sahrens 1643fa9e4066Sahrens static int 1644fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2) 1645fa9e4066Sahrens { 1646fa9e4066Sahrens const spa_t *s1 = a1; 1647fa9e4066Sahrens const spa_t *s2 = a2; 1648fa9e4066Sahrens int s; 1649fa9e4066Sahrens 1650fa9e4066Sahrens s = strcmp(s1->spa_name, s2->spa_name); 1651fa9e4066Sahrens if (s > 0) 1652fa9e4066Sahrens return (1); 1653fa9e4066Sahrens if (s < 0) 1654fa9e4066Sahrens return (-1); 1655fa9e4066Sahrens return (0); 1656fa9e4066Sahrens } 1657fa9e4066Sahrens 16580373e76bSbonwick int 16590373e76bSbonwick spa_busy(void) 16600373e76bSbonwick { 16610373e76bSbonwick return (spa_active_count); 16620373e76bSbonwick } 16630373e76bSbonwick 1664e7cbe64fSgw void 1665e7cbe64fSgw spa_boot_init() 1666e7cbe64fSgw { 1667e7cbe64fSgw spa_config_load(); 1668e7cbe64fSgw } 1669e7cbe64fSgw 1670fa9e4066Sahrens void 1671fa9e4066Sahrens spa_init(int mode) 1672fa9e4066Sahrens { 1673fa9e4066Sahrens mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 1674c25056deSgw mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 1675fa94a07fSbrendan mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 1676fa9e4066Sahrens cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 1677fa9e4066Sahrens 1678fa9e4066Sahrens avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 1679fa9e4066Sahrens offsetof(spa_t, spa_avl)); 1680fa9e4066Sahrens 1681fa94a07fSbrendan avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 1682fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 1683fa94a07fSbrendan 1684fa94a07fSbrendan avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 1685fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 168699653d4eSeschrock 16878ad4d6ddSJeff Bonwick spa_mode_global = mode; 1688fa9e4066Sahrens 1689283b8460SGeorge.Wilson #ifdef _KERNEL 1690283b8460SGeorge.Wilson spa_arch_init(); 1691283b8460SGeorge.Wilson #else 1692cd1c8b85SMatthew Ahrens if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 1693cd1c8b85SMatthew Ahrens arc_procfd = open("/proc/self/ctl", O_WRONLY); 1694cd1c8b85SMatthew Ahrens if (arc_procfd == -1) { 1695cd1c8b85SMatthew Ahrens perror("could not enable watchpoints: " 1696cd1c8b85SMatthew Ahrens "opening /proc/self/ctl failed: "); 1697cd1c8b85SMatthew Ahrens } else { 1698cd1c8b85SMatthew Ahrens arc_watch = B_TRUE; 1699cd1c8b85SMatthew Ahrens } 1700cd1c8b85SMatthew Ahrens } 1701cd1c8b85SMatthew Ahrens #endif 1702cd1c8b85SMatthew Ahrens 1703fa9e4066Sahrens refcount_init(); 1704fa9e4066Sahrens unique_init(); 170501f55e48SGeorge Wilson space_map_init(); 1706fa9e4066Sahrens zio_init(); 1707fa9e4066Sahrens dmu_init(); 1708fa9e4066Sahrens zil_init(); 170987db74c1Sek vdev_cache_stat_init(); 171091ebeef5Sahrens zfs_prop_init(); 1711990b4856Slling zpool_prop_init(); 1712ad135b5dSChristopher Siden zpool_feature_init(); 1713fa9e4066Sahrens spa_config_load(); 1714e14bb325SJeff Bonwick l2arc_start(); 1715fa9e4066Sahrens } 1716fa9e4066Sahrens 1717fa9e4066Sahrens void 1718fa9e4066Sahrens spa_fini(void) 1719fa9e4066Sahrens { 1720e14bb325SJeff Bonwick l2arc_stop(); 1721e14bb325SJeff Bonwick 1722fa9e4066Sahrens spa_evict_all(); 1723fa9e4066Sahrens 172487db74c1Sek vdev_cache_stat_fini(); 1725fa9e4066Sahrens zil_fini(); 1726fa9e4066Sahrens dmu_fini(); 1727fa9e4066Sahrens zio_fini(); 172801f55e48SGeorge Wilson space_map_fini(); 172991ebeef5Sahrens unique_fini(); 1730fa9e4066Sahrens refcount_fini(); 1731fa9e4066Sahrens 1732fa9e4066Sahrens avl_destroy(&spa_namespace_avl); 173399653d4eSeschrock avl_destroy(&spa_spare_avl); 1734fa94a07fSbrendan avl_destroy(&spa_l2cache_avl); 1735fa9e4066Sahrens 1736fa9e4066Sahrens cv_destroy(&spa_namespace_cv); 1737fa9e4066Sahrens mutex_destroy(&spa_namespace_lock); 1738c25056deSgw mutex_destroy(&spa_spare_lock); 1739fa94a07fSbrendan mutex_destroy(&spa_l2cache_lock); 1740fa9e4066Sahrens } 17416ce0521aSperrin 17426ce0521aSperrin /* 17436ce0521aSperrin * Return whether this pool has slogs. No locking needed. 17446ce0521aSperrin * It's not a problem if the wrong answer is returned as it's only for 17456ce0521aSperrin * performance and not correctness 17466ce0521aSperrin */ 17476ce0521aSperrin boolean_t 17486ce0521aSperrin spa_has_slogs(spa_t *spa) 17496ce0521aSperrin { 17506ce0521aSperrin return (spa->spa_log_class->mc_rotor != NULL); 17516ce0521aSperrin } 1752bf82a41bSeschrock 1753b24ab676SJeff Bonwick spa_log_state_t 1754b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa) 1755b24ab676SJeff Bonwick { 1756b24ab676SJeff Bonwick return (spa->spa_log_state); 1757b24ab676SJeff Bonwick } 1758b24ab676SJeff Bonwick 1759b24ab676SJeff Bonwick void 1760b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state) 1761b24ab676SJeff Bonwick { 1762b24ab676SJeff Bonwick spa->spa_log_state = state; 1763b24ab676SJeff Bonwick } 1764b24ab676SJeff Bonwick 1765bf82a41bSeschrock boolean_t 1766bf82a41bSeschrock spa_is_root(spa_t *spa) 1767bf82a41bSeschrock { 1768bf82a41bSeschrock return (spa->spa_is_root); 1769bf82a41bSeschrock } 17708ad4d6ddSJeff Bonwick 17718ad4d6ddSJeff Bonwick boolean_t 17728ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa) 17738ad4d6ddSJeff Bonwick { 17748ad4d6ddSJeff Bonwick return (!!(spa->spa_mode & FWRITE)); 17758ad4d6ddSJeff Bonwick } 17768ad4d6ddSJeff Bonwick 17778ad4d6ddSJeff Bonwick int 17788ad4d6ddSJeff Bonwick spa_mode(spa_t *spa) 17798ad4d6ddSJeff Bonwick { 17808ad4d6ddSJeff Bonwick return (spa->spa_mode); 17818ad4d6ddSJeff Bonwick } 1782b24ab676SJeff Bonwick 1783b24ab676SJeff Bonwick uint64_t 1784b24ab676SJeff Bonwick spa_bootfs(spa_t *spa) 1785b24ab676SJeff Bonwick { 1786b24ab676SJeff Bonwick return (spa->spa_bootfs); 1787b24ab676SJeff Bonwick } 1788b24ab676SJeff Bonwick 1789b24ab676SJeff Bonwick uint64_t 1790b24ab676SJeff Bonwick spa_delegation(spa_t *spa) 1791b24ab676SJeff Bonwick { 1792b24ab676SJeff Bonwick return (spa->spa_delegation); 1793b24ab676SJeff Bonwick } 1794b24ab676SJeff Bonwick 1795b24ab676SJeff Bonwick objset_t * 1796b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa) 1797b24ab676SJeff Bonwick { 1798b24ab676SJeff Bonwick return (spa->spa_meta_objset); 1799b24ab676SJeff Bonwick } 1800b24ab676SJeff Bonwick 1801b24ab676SJeff Bonwick enum zio_checksum 1802b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa) 1803b24ab676SJeff Bonwick { 1804b24ab676SJeff Bonwick return (spa->spa_dedup_checksum); 1805b24ab676SJeff Bonwick } 18063f9d6ad7SLin Ling 18073f9d6ad7SLin Ling /* 18083f9d6ad7SLin Ling * Reset pool scan stat per scan pass (or reboot). 18093f9d6ad7SLin Ling */ 18103f9d6ad7SLin Ling void 18113f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa) 18123f9d6ad7SLin Ling { 18133f9d6ad7SLin Ling /* data not stored on disk */ 18143f9d6ad7SLin Ling spa->spa_scan_pass_start = gethrestime_sec(); 18153f9d6ad7SLin Ling spa->spa_scan_pass_exam = 0; 18163f9d6ad7SLin Ling vdev_scan_stat_init(spa->spa_root_vdev); 18173f9d6ad7SLin Ling } 18183f9d6ad7SLin Ling 18193f9d6ad7SLin Ling /* 18203f9d6ad7SLin Ling * Get scan stats for zpool status reports 18213f9d6ad7SLin Ling */ 18223f9d6ad7SLin Ling int 18233f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 18243f9d6ad7SLin Ling { 18253f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 18263f9d6ad7SLin Ling 18273f9d6ad7SLin Ling if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 1828be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 18293f9d6ad7SLin Ling bzero(ps, sizeof (pool_scan_stat_t)); 18303f9d6ad7SLin Ling 18313f9d6ad7SLin Ling /* data stored on disk */ 18323f9d6ad7SLin Ling ps->pss_func = scn->scn_phys.scn_func; 18333f9d6ad7SLin Ling ps->pss_start_time = scn->scn_phys.scn_start_time; 18343f9d6ad7SLin Ling ps->pss_end_time = scn->scn_phys.scn_end_time; 18353f9d6ad7SLin Ling ps->pss_to_examine = scn->scn_phys.scn_to_examine; 18363f9d6ad7SLin Ling ps->pss_examined = scn->scn_phys.scn_examined; 18373f9d6ad7SLin Ling ps->pss_to_process = scn->scn_phys.scn_to_process; 18383f9d6ad7SLin Ling ps->pss_processed = scn->scn_phys.scn_processed; 18393f9d6ad7SLin Ling ps->pss_errors = scn->scn_phys.scn_errors; 18403f9d6ad7SLin Ling ps->pss_state = scn->scn_phys.scn_state; 18413f9d6ad7SLin Ling 18423f9d6ad7SLin Ling /* data not stored on disk */ 18433f9d6ad7SLin Ling ps->pss_pass_start = spa->spa_scan_pass_start; 18443f9d6ad7SLin Ling ps->pss_pass_exam = spa->spa_scan_pass_exam; 18453f9d6ad7SLin Ling 18463f9d6ad7SLin Ling return (0); 18473f9d6ad7SLin Ling } 184809c9d376SGeorge Wilson 184909c9d376SGeorge Wilson boolean_t 185009c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa) 185109c9d376SGeorge Wilson { 185209c9d376SGeorge Wilson return (spa->spa_debug); 185309c9d376SGeorge Wilson } 1854