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. 2394c2d0ebSMatthew Ahrens * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 24e495b6e6SSaso Kiselkov * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 25bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 2645818ee1SMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved. 27c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 281702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 29fa9e4066Sahrens */ 30fa9e4066Sahrens 31fa9e4066Sahrens #include <sys/zfs_context.h> 32fa9e4066Sahrens #include <sys/spa_impl.h> 33283b8460SGeorge.Wilson #include <sys/spa_boot.h> 34fa9e4066Sahrens #include <sys/zio.h> 35fa9e4066Sahrens #include <sys/zio_checksum.h> 36fa9e4066Sahrens #include <sys/zio_compress.h> 37fa9e4066Sahrens #include <sys/dmu.h> 38fa9e4066Sahrens #include <sys/dmu_tx.h> 39fa9e4066Sahrens #include <sys/zap.h> 40fa9e4066Sahrens #include <sys/zil.h> 41fa9e4066Sahrens #include <sys/vdev_impl.h> 42fa9e4066Sahrens #include <sys/metaslab.h> 43fa9e4066Sahrens #include <sys/uberblock_impl.h> 44fa9e4066Sahrens #include <sys/txg.h> 45fa9e4066Sahrens #include <sys/avl.h> 46fa9e4066Sahrens #include <sys/unique.h> 47fa9e4066Sahrens #include <sys/dsl_pool.h> 48fa9e4066Sahrens #include <sys/dsl_dir.h> 49fa9e4066Sahrens #include <sys/dsl_prop.h> 503f9d6ad7SLin Ling #include <sys/dsl_scan.h> 51fa9e4066Sahrens #include <sys/fs/zfs.h> 526ce0521aSperrin #include <sys/metaslab_impl.h> 53e14bb325SJeff Bonwick #include <sys/arc.h> 54485bbbf5SGeorge Wilson #include <sys/ddt.h> 5591ebeef5Sahrens #include "zfs_prop.h" 5645818ee1SMatthew Ahrens #include <sys/zfeature.h> 57fa9e4066Sahrens 58fa9e4066Sahrens /* 59fa9e4066Sahrens * SPA locking 60fa9e4066Sahrens * 61fa9e4066Sahrens * There are four basic locks for managing spa_t structures: 62fa9e4066Sahrens * 63fa9e4066Sahrens * spa_namespace_lock (global mutex) 64fa9e4066Sahrens * 6544cd46caSbillm * This lock must be acquired to do any of the following: 66fa9e4066Sahrens * 6744cd46caSbillm * - Lookup a spa_t by name 6844cd46caSbillm * - Add or remove a spa_t from the namespace 6944cd46caSbillm * - Increase spa_refcount from non-zero 7044cd46caSbillm * - Check if spa_refcount is zero 7144cd46caSbillm * - Rename a spa_t 72ea8dc4b6Seschrock * - add/remove/attach/detach devices 7344cd46caSbillm * - Held for the duration of create/destroy/import/export 74fa9e4066Sahrens * 7544cd46caSbillm * It does not need to handle recursion. A create or destroy may 7644cd46caSbillm * reference objects (files or zvols) in other pools, but by 7744cd46caSbillm * definition they must have an existing reference, and will never need 7844cd46caSbillm * to lookup a spa_t by name. 79fa9e4066Sahrens * 80fa9e4066Sahrens * spa_refcount (per-spa refcount_t protected by mutex) 81fa9e4066Sahrens * 8244cd46caSbillm * This reference count keep track of any active users of the spa_t. The 8344cd46caSbillm * spa_t cannot be destroyed or freed while this is non-zero. Internally, 8444cd46caSbillm * the refcount is never really 'zero' - opening a pool implicitly keeps 85088f3894Sahrens * some references in the DMU. Internally we check against spa_minref, but 8644cd46caSbillm * present the image of a zero/non-zero value to consumers. 87fa9e4066Sahrens * 88e14bb325SJeff Bonwick * spa_config_lock[] (per-spa array of rwlocks) 89fa9e4066Sahrens * 9091ebeef5Sahrens * This protects the spa_t from config changes, and must be held in 9191ebeef5Sahrens * the following circumstances: 92fa9e4066Sahrens * 9344cd46caSbillm * - RW_READER to perform I/O to the spa 9444cd46caSbillm * - RW_WRITER to change the vdev config 95fa9e4066Sahrens * 96fa9e4066Sahrens * The locking order is fairly straightforward: 97fa9e4066Sahrens * 9844cd46caSbillm * spa_namespace_lock -> spa_refcount 99fa9e4066Sahrens * 10044cd46caSbillm * The namespace lock must be acquired to increase the refcount from 0 10144cd46caSbillm * or to check if it is zero. 102fa9e4066Sahrens * 103e14bb325SJeff Bonwick * spa_refcount -> spa_config_lock[] 104fa9e4066Sahrens * 10544cd46caSbillm * There must be at least one valid reference on the spa_t to acquire 10644cd46caSbillm * the config lock. 107fa9e4066Sahrens * 108e14bb325SJeff Bonwick * spa_namespace_lock -> spa_config_lock[] 109fa9e4066Sahrens * 11044cd46caSbillm * The namespace lock must always be taken before the config lock. 111fa9e4066Sahrens * 112fa9e4066Sahrens * 113e14bb325SJeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible. 114fa9e4066Sahrens * 115e14bb325SJeff Bonwick * The namespace is manipulated using the following functions, all of which 116e14bb325SJeff Bonwick * require the spa_namespace_lock to be held. 117fa9e4066Sahrens * 11844cd46caSbillm * spa_lookup() Lookup a spa_t by name. 119fa9e4066Sahrens * 12044cd46caSbillm * spa_add() Create a new spa_t in the namespace. 121fa9e4066Sahrens * 12244cd46caSbillm * spa_remove() Remove a spa_t from the namespace. This also 12344cd46caSbillm * frees up any memory associated with the spa_t. 124fa9e4066Sahrens * 12544cd46caSbillm * spa_next() Returns the next spa_t in the system, or the 12644cd46caSbillm * first if NULL is passed. 127fa9e4066Sahrens * 12844cd46caSbillm * spa_evict_all() Shutdown and remove all spa_t structures in 12944cd46caSbillm * the system. 130fa9e4066Sahrens * 131ea8dc4b6Seschrock * spa_guid_exists() Determine whether a pool/device guid exists. 132fa9e4066Sahrens * 133fa9e4066Sahrens * The spa_refcount is manipulated using the following functions: 134fa9e4066Sahrens * 13544cd46caSbillm * spa_open_ref() Adds a reference to the given spa_t. Must be 13644cd46caSbillm * called with spa_namespace_lock held if the 13744cd46caSbillm * refcount is currently zero. 138fa9e4066Sahrens * 13944cd46caSbillm * spa_close() Remove a reference from the spa_t. This will 14044cd46caSbillm * not free the spa_t or remove it from the 14144cd46caSbillm * namespace. No locking is required. 142fa9e4066Sahrens * 14344cd46caSbillm * spa_refcount_zero() Returns true if the refcount is currently 14444cd46caSbillm * zero. Must be called with spa_namespace_lock 14544cd46caSbillm * held. 146fa9e4066Sahrens * 147e14bb325SJeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows: 148e14bb325SJeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 149e14bb325SJeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 150e14bb325SJeff Bonwick * 151e14bb325SJeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader. 152e14bb325SJeff Bonwick * To modify the configuration, you must hold all locks as writer. To modify 153e14bb325SJeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline), 154e14bb325SJeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer. 155e14bb325SJeff Bonwick * 156e14bb325SJeff Bonwick * We use these distinct config locks to avoid recursive lock entry. 157e14bb325SJeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 158e14bb325SJeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps 159e14bb325SJeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 160e14bb325SJeff Bonwick * 161e14bb325SJeff Bonwick * The spa config locks cannot be normal rwlocks because we need the 162e14bb325SJeff Bonwick * ability to hand off ownership. For example, SCL_ZIO is acquired 163e14bb325SJeff Bonwick * by the issuing thread and later released by an interrupt thread. 164e14bb325SJeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent 165e14bb325SJeff Bonwick * writer (i.e. system administrator) starvation. 166e14bb325SJeff Bonwick * 167e14bb325SJeff Bonwick * The lock acquisition rules are as follows: 168e14bb325SJeff Bonwick * 169e14bb325SJeff Bonwick * SCL_CONFIG 170e14bb325SJeff Bonwick * Protects changes to the vdev tree topology, such as vdev 171e14bb325SJeff Bonwick * add/remove/attach/detach. Protects the dirty config list 172e14bb325SJeff Bonwick * (spa_config_dirty_list) and the set of spares and l2arc devices. 173e14bb325SJeff Bonwick * 174e14bb325SJeff Bonwick * SCL_STATE 175e14bb325SJeff Bonwick * Protects changes to pool state and vdev state, such as vdev 176e14bb325SJeff Bonwick * online/offline/fault/degrade/clear. Protects the dirty state list 177e14bb325SJeff Bonwick * (spa_state_dirty_list) and global pool state (spa_state). 178e14bb325SJeff Bonwick * 179e14bb325SJeff Bonwick * SCL_ALLOC 180e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 181e14bb325SJeff Bonwick * Held as reader by metaslab_alloc() and metaslab_claim(). 182e14bb325SJeff Bonwick * 183e14bb325SJeff Bonwick * SCL_ZIO 184e14bb325SJeff Bonwick * Held by bp-level zios (those which have no io_vd upon entry) 185e14bb325SJeff Bonwick * to prevent changes to the vdev tree. The bp-level zio implicitly 186e14bb325SJeff Bonwick * protects all of its vdev child zios, which do not hold SCL_ZIO. 187e14bb325SJeff Bonwick * 188e14bb325SJeff Bonwick * SCL_FREE 189e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 190e14bb325SJeff Bonwick * Held as reader by metaslab_free(). SCL_FREE is distinct from 191e14bb325SJeff Bonwick * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 192e14bb325SJeff Bonwick * blocks in zio_done() while another i/o that holds either 193e14bb325SJeff Bonwick * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 194e14bb325SJeff Bonwick * 195e14bb325SJeff Bonwick * SCL_VDEV 196e14bb325SJeff Bonwick * Held as reader to prevent changes to the vdev tree during trivial 197b24ab676SJeff Bonwick * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 198e14bb325SJeff Bonwick * other locks, and lower than all of them, to ensure that it's safe 199e14bb325SJeff Bonwick * to acquire regardless of caller context. 200e14bb325SJeff Bonwick * 201e14bb325SJeff Bonwick * In addition, the following rules apply: 202e14bb325SJeff Bonwick * 203e14bb325SJeff Bonwick * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 204e14bb325SJeff Bonwick * The lock ordering is SCL_CONFIG > spa_props_lock. 205e14bb325SJeff Bonwick * 206e14bb325SJeff Bonwick * (b) I/O operations on leaf vdevs. For any zio operation that takes 207e14bb325SJeff Bonwick * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 208e14bb325SJeff Bonwick * or zio_write_phys() -- the caller must ensure that the config cannot 209e14bb325SJeff Bonwick * cannot change in the interim, and that the vdev cannot be reopened. 210e14bb325SJeff Bonwick * SCL_STATE as reader suffices for both. 211fa9e4066Sahrens * 212ea8dc4b6Seschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 213fa9e4066Sahrens * 21444cd46caSbillm * spa_vdev_enter() Acquire the namespace lock and the config lock 215ea8dc4b6Seschrock * for writing. 216fa9e4066Sahrens * 21744cd46caSbillm * spa_vdev_exit() Release the config lock, wait for all I/O 21844cd46caSbillm * to complete, sync the updated configs to the 219ea8dc4b6Seschrock * cache, and release the namespace lock. 220fa9e4066Sahrens * 221e14bb325SJeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 222e14bb325SJeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 223e14bb325SJeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 224e14bb325SJeff Bonwick * 225ad135b5dSChristopher Siden * spa_rename() is also implemented within this file since it requires 226e14bb325SJeff Bonwick * manipulation of the namespace. 227fa9e4066Sahrens */ 228fa9e4066Sahrens 229fa9e4066Sahrens static avl_tree_t spa_namespace_avl; 230fa9e4066Sahrens kmutex_t spa_namespace_lock; 231fa9e4066Sahrens static kcondvar_t spa_namespace_cv; 2320373e76bSbonwick static int spa_active_count; 233416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP; 234fa9e4066Sahrens 23599653d4eSeschrock static kmutex_t spa_spare_lock; 23639c23413Seschrock static avl_tree_t spa_spare_avl; 237fa94a07fSbrendan static kmutex_t spa_l2cache_lock; 238fa94a07fSbrendan static avl_tree_t spa_l2cache_avl; 23999653d4eSeschrock 240fa9e4066Sahrens kmem_cache_t *spa_buffer_pool; 2418ad4d6ddSJeff Bonwick int spa_mode_global; 242fa9e4066Sahrens 243fa9e4066Sahrens #ifdef ZFS_DEBUG 244*5cabbc6bSPrashanth Sreenivasa /* 245*5cabbc6bSPrashanth Sreenivasa * Everything except dprintf, spa, and indirect_remap is on by default 246*5cabbc6bSPrashanth Sreenivasa * in debug builds. 247*5cabbc6bSPrashanth Sreenivasa */ 248*5cabbc6bSPrashanth Sreenivasa int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA | ZFS_DEBUG_INDIRECT_REMAP); 249fa9e4066Sahrens #else 250fa9e4066Sahrens int zfs_flags = 0; 251fa9e4066Sahrens #endif 252fa9e4066Sahrens 2530125049cSahrens /* 2540125049cSahrens * zfs_recover can be set to nonzero to attempt to recover from 2550125049cSahrens * otherwise-fatal errors, typically caused by on-disk corruption. When 2560125049cSahrens * set, calls to zfs_panic_recover() will turn into warning messages. 2578b36997aSMatthew Ahrens * This should only be used as a last resort, as it typically results 2588b36997aSMatthew Ahrens * in leaked space, or worse. 2590125049cSahrens */ 2607fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE; 2617fd05ac4SMatthew Ahrens 2627fd05ac4SMatthew Ahrens /* 2637fd05ac4SMatthew Ahrens * If destroy encounters an EIO while reading metadata (e.g. indirect 2647fd05ac4SMatthew Ahrens * blocks), space referenced by the missing metadata can not be freed. 2657fd05ac4SMatthew Ahrens * Normally this causes the background destroy to become "stalled", as 2667fd05ac4SMatthew Ahrens * it is unable to make forward progress. While in this stalled state, 2677fd05ac4SMatthew Ahrens * all remaining space to free from the error-encountering filesystem is 2687fd05ac4SMatthew Ahrens * "temporarily leaked". Set this flag to cause it to ignore the EIO, 2697fd05ac4SMatthew Ahrens * permanently leak the space from indirect blocks that can not be read, 2707fd05ac4SMatthew Ahrens * and continue to free everything else that it can. 2717fd05ac4SMatthew Ahrens * 2727fd05ac4SMatthew Ahrens * The default, "stalling" behavior is useful if the storage partially 2737fd05ac4SMatthew Ahrens * fails (i.e. some but not all i/os fail), and then later recovers. In 2747fd05ac4SMatthew Ahrens * this case, we will be able to continue pool operations while it is 2757fd05ac4SMatthew Ahrens * partially failed, and when it recovers, we can continue to free the 2767fd05ac4SMatthew Ahrens * space, with no leaks. However, note that this case is actually 2777fd05ac4SMatthew Ahrens * fairly rare. 2787fd05ac4SMatthew Ahrens * 2797fd05ac4SMatthew Ahrens * Typically pools either (a) fail completely (but perhaps temporarily, 2807fd05ac4SMatthew Ahrens * e.g. a top-level vdev going offline), or (b) have localized, 2817fd05ac4SMatthew Ahrens * permanent errors (e.g. disk returns the wrong data due to bit flip or 2827fd05ac4SMatthew Ahrens * firmware bug). In case (a), this setting does not matter because the 2837fd05ac4SMatthew Ahrens * pool will be suspended and the sync thread will not be able to make 2847fd05ac4SMatthew Ahrens * forward progress regardless. In case (b), because the error is 2857fd05ac4SMatthew Ahrens * permanent, the best we can do is leak the minimum amount of space, 2867fd05ac4SMatthew Ahrens * which is what setting this flag will do. Therefore, it is reasonable 2877fd05ac4SMatthew Ahrens * for this flag to normally be set, but we chose the more conservative 2887fd05ac4SMatthew Ahrens * approach of not setting it, so that there is no possibility of 2897fd05ac4SMatthew Ahrens * leaking space in the "partial temporary" failure case. 2907fd05ac4SMatthew Ahrens */ 2917fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE; 2920125049cSahrens 29369962b56SMatthew Ahrens /* 29469962b56SMatthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is 29569962b56SMatthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the 29669962b56SMatthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds. 29769962b56SMatthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that 29869962b56SMatthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting 29969962b56SMatthew Ahrens * in a system panic. 30069962b56SMatthew Ahrens */ 30169962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL; 302283b8460SGeorge.Wilson 303283b8460SGeorge.Wilson /* 30469962b56SMatthew Ahrens * Check time in milliseconds. This defines the frequency at which we check 30569962b56SMatthew Ahrens * for hung I/O. 306283b8460SGeorge.Wilson */ 30769962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL; 308283b8460SGeorge.Wilson 309283b8460SGeorge.Wilson /* 310283b8460SGeorge.Wilson * Override the zfs deadman behavior via /etc/system. By default the 311283b8460SGeorge.Wilson * deadman is enabled except on VMware and sparc deployments. 312283b8460SGeorge.Wilson */ 313283b8460SGeorge.Wilson int zfs_deadman_enabled = -1; 314283b8460SGeorge.Wilson 31569962b56SMatthew Ahrens /* 31669962b56SMatthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which 31769962b56SMatthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 31869962b56SMatthew Ahrens * times the size; so just assume that. Add to this the fact that 31969962b56SMatthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because 32069962b56SMatthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together, 32169962b56SMatthew Ahrens * the worst case is: 32269962b56SMatthew Ahrens * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24 32369962b56SMatthew Ahrens */ 32469962b56SMatthew Ahrens int spa_asize_inflation = 24; 325fa9e4066Sahrens 3267d46dc6cSMatthew Ahrens /* 3277d46dc6cSMatthew Ahrens * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in 3287d46dc6cSMatthew Ahrens * the pool to be consumed. This ensures that we don't run the pool 3297d46dc6cSMatthew Ahrens * completely out of space, due to unaccounted changes (e.g. to the MOS). 3307d46dc6cSMatthew Ahrens * It also limits the worst-case time to allocate space. If we have 3317d46dc6cSMatthew Ahrens * less than this amount of free space, most ZPL operations (e.g. write, 3327d46dc6cSMatthew Ahrens * create) will return ENOSPC. 3337d46dc6cSMatthew Ahrens * 3347d46dc6cSMatthew Ahrens * Certain operations (e.g. file removal, most administrative actions) can 3357d46dc6cSMatthew Ahrens * use half the slop space. They will only return ENOSPC if less than half 3367d46dc6cSMatthew Ahrens * the slop space is free. Typically, once the pool has less than the slop 3377d46dc6cSMatthew Ahrens * space free, the user will use these operations to free up space in the pool. 3387d46dc6cSMatthew Ahrens * These are the operations that call dsl_pool_adjustedsize() with the netfree 3397d46dc6cSMatthew Ahrens * argument set to TRUE. 3407d46dc6cSMatthew Ahrens * 3417d46dc6cSMatthew Ahrens * A very restricted set of operations are always permitted, regardless of 3427d46dc6cSMatthew Ahrens * the amount of free space. These are the operations that call 3437d46dc6cSMatthew Ahrens * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these 3447d46dc6cSMatthew Ahrens * operations result in a net increase in the amount of space used, 3457d46dc6cSMatthew Ahrens * it is possible to run the pool completely out of space, causing it to 3467d46dc6cSMatthew Ahrens * be permanently read-only. 3477d46dc6cSMatthew Ahrens * 3484b5c8e93SMatthew Ahrens * Note that on very small pools, the slop space will be larger than 3494b5c8e93SMatthew Ahrens * 3.2%, in an effort to have it be at least spa_min_slop (128MB), 3504b5c8e93SMatthew Ahrens * but we never allow it to be more than half the pool size. 3514b5c8e93SMatthew Ahrens * 3527d46dc6cSMatthew Ahrens * See also the comments in zfs_space_check_t. 3537d46dc6cSMatthew Ahrens */ 3547d46dc6cSMatthew Ahrens int spa_slop_shift = 5; 3554b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024; 3567d46dc6cSMatthew Ahrens 357e05725b1Sbonwick /* 358e05725b1Sbonwick * ========================================================================== 359e05725b1Sbonwick * SPA config locking 360e05725b1Sbonwick * ========================================================================== 361e05725b1Sbonwick */ 362e05725b1Sbonwick static void 363e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa) 364e14bb325SJeff Bonwick { 365e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 366e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 367e14bb325SJeff Bonwick mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 368e14bb325SJeff Bonwick cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 3693b2aab18SMatthew Ahrens refcount_create_untracked(&scl->scl_count); 370e14bb325SJeff Bonwick scl->scl_writer = NULL; 371e14bb325SJeff Bonwick scl->scl_write_wanted = 0; 372e14bb325SJeff Bonwick } 373e05725b1Sbonwick } 374e05725b1Sbonwick 375e05725b1Sbonwick static void 376e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa) 377e14bb325SJeff Bonwick { 378e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 379e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 380e14bb325SJeff Bonwick mutex_destroy(&scl->scl_lock); 381e14bb325SJeff Bonwick cv_destroy(&scl->scl_cv); 382e14bb325SJeff Bonwick refcount_destroy(&scl->scl_count); 383e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL); 384e14bb325SJeff Bonwick ASSERT(scl->scl_write_wanted == 0); 385e14bb325SJeff Bonwick } 386e14bb325SJeff Bonwick } 387e14bb325SJeff Bonwick 388e14bb325SJeff Bonwick int 389e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 390e05725b1Sbonwick { 391e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 392e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 393e14bb325SJeff Bonwick if (!(locks & (1 << i))) 394e14bb325SJeff Bonwick continue; 395e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 396e14bb325SJeff Bonwick if (rw == RW_READER) { 397e14bb325SJeff Bonwick if (scl->scl_writer || scl->scl_write_wanted) { 398e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 399e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 400e495b6e6SSaso Kiselkov tag); 401e14bb325SJeff Bonwick return (0); 402e14bb325SJeff Bonwick } 403e14bb325SJeff Bonwick } else { 404e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 405e14bb325SJeff Bonwick if (!refcount_is_zero(&scl->scl_count)) { 406e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 407e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 408e495b6e6SSaso Kiselkov tag); 409e14bb325SJeff Bonwick return (0); 410e14bb325SJeff Bonwick } 411e14bb325SJeff Bonwick scl->scl_writer = curthread; 412e14bb325SJeff Bonwick } 413e14bb325SJeff Bonwick (void) refcount_add(&scl->scl_count, tag); 414e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 415e14bb325SJeff Bonwick } 416e14bb325SJeff Bonwick return (1); 417e05725b1Sbonwick } 418e05725b1Sbonwick 419e05725b1Sbonwick void 420e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 421e05725b1Sbonwick { 422f64c0e34SEric Taylor int wlocks_held = 0; 423f64c0e34SEric Taylor 4243b2aab18SMatthew Ahrens ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 4253b2aab18SMatthew Ahrens 426e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 427e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 428f64c0e34SEric Taylor if (scl->scl_writer == curthread) 429f64c0e34SEric Taylor wlocks_held |= (1 << i); 430e14bb325SJeff Bonwick if (!(locks & (1 << i))) 431e14bb325SJeff Bonwick continue; 432e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 433e14bb325SJeff Bonwick if (rw == RW_READER) { 434e14bb325SJeff Bonwick while (scl->scl_writer || scl->scl_write_wanted) { 435e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 436e14bb325SJeff Bonwick } 437e14bb325SJeff Bonwick } else { 438e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 439e14bb325SJeff Bonwick while (!refcount_is_zero(&scl->scl_count)) { 440e14bb325SJeff Bonwick scl->scl_write_wanted++; 441e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 442e14bb325SJeff Bonwick scl->scl_write_wanted--; 443e14bb325SJeff Bonwick } 444e14bb325SJeff Bonwick scl->scl_writer = curthread; 445e14bb325SJeff Bonwick } 446e14bb325SJeff Bonwick (void) refcount_add(&scl->scl_count, tag); 447e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 448e05725b1Sbonwick } 449*5cabbc6bSPrashanth Sreenivasa ASSERT3U(wlocks_held, <=, locks); 450e05725b1Sbonwick } 451e05725b1Sbonwick 452e05725b1Sbonwick void 453e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag) 454e05725b1Sbonwick { 455e14bb325SJeff Bonwick for (int i = SCL_LOCKS - 1; i >= 0; i--) { 456e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 457e14bb325SJeff Bonwick if (!(locks & (1 << i))) 458e14bb325SJeff Bonwick continue; 459e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 460e14bb325SJeff Bonwick ASSERT(!refcount_is_zero(&scl->scl_count)); 461e14bb325SJeff Bonwick if (refcount_remove(&scl->scl_count, tag) == 0) { 462e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL || 463e14bb325SJeff Bonwick scl->scl_writer == curthread); 464e14bb325SJeff Bonwick scl->scl_writer = NULL; /* OK in either case */ 465e14bb325SJeff Bonwick cv_broadcast(&scl->scl_cv); 466e14bb325SJeff Bonwick } 467e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 468e05725b1Sbonwick } 469e05725b1Sbonwick } 470e05725b1Sbonwick 471e14bb325SJeff Bonwick int 472e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw) 473e05725b1Sbonwick { 474e14bb325SJeff Bonwick int locks_held = 0; 475e05725b1Sbonwick 476e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 477e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 478e14bb325SJeff Bonwick if (!(locks & (1 << i))) 479e14bb325SJeff Bonwick continue; 480e14bb325SJeff Bonwick if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) || 481e14bb325SJeff Bonwick (rw == RW_WRITER && scl->scl_writer == curthread)) 482e14bb325SJeff Bonwick locks_held |= 1 << i; 483e14bb325SJeff Bonwick } 484e14bb325SJeff Bonwick 485e14bb325SJeff Bonwick return (locks_held); 486e05725b1Sbonwick } 487e05725b1Sbonwick 488fa9e4066Sahrens /* 489fa9e4066Sahrens * ========================================================================== 490fa9e4066Sahrens * SPA namespace functions 491fa9e4066Sahrens * ========================================================================== 492fa9e4066Sahrens */ 493fa9e4066Sahrens 494fa9e4066Sahrens /* 495fa9e4066Sahrens * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 496fa9e4066Sahrens * Returns NULL if no matching spa_t is found. 497fa9e4066Sahrens */ 498fa9e4066Sahrens spa_t * 499fa9e4066Sahrens spa_lookup(const char *name) 500fa9e4066Sahrens { 501e14bb325SJeff Bonwick static spa_t search; /* spa_t is large; don't allocate on stack */ 502e14bb325SJeff Bonwick spa_t *spa; 503fa9e4066Sahrens avl_index_t where; 50440feaa91Sahrens char *cp; 505fa9e4066Sahrens 506fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 507fa9e4066Sahrens 5083b2aab18SMatthew Ahrens (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 5093b2aab18SMatthew Ahrens 51040feaa91Sahrens /* 51140feaa91Sahrens * If it's a full dataset name, figure out the pool name and 51240feaa91Sahrens * just use that. 51340feaa91Sahrens */ 51478f17100SMatthew Ahrens cp = strpbrk(search.spa_name, "/@#"); 5153b2aab18SMatthew Ahrens if (cp != NULL) 51640feaa91Sahrens *cp = '\0'; 51740feaa91Sahrens 518fa9e4066Sahrens spa = avl_find(&spa_namespace_avl, &search, &where); 519fa9e4066Sahrens 520fa9e4066Sahrens return (spa); 521fa9e4066Sahrens } 522fa9e4066Sahrens 523283b8460SGeorge.Wilson /* 524283b8460SGeorge.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 525283b8460SGeorge.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 526283b8460SGeorge.Wilson * looking for potentially hung I/Os. 527283b8460SGeorge.Wilson */ 528283b8460SGeorge.Wilson void 529283b8460SGeorge.Wilson spa_deadman(void *arg) 530283b8460SGeorge.Wilson { 531283b8460SGeorge.Wilson spa_t *spa = arg; 532283b8460SGeorge.Wilson 5330713e232SGeorge Wilson /* 5340713e232SGeorge Wilson * Disable the deadman timer if the pool is suspended. 5350713e232SGeorge Wilson */ 5360713e232SGeorge Wilson if (spa_suspended(spa)) { 5370713e232SGeorge Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 5380713e232SGeorge Wilson return; 5390713e232SGeorge Wilson } 5400713e232SGeorge Wilson 541283b8460SGeorge.Wilson zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 542283b8460SGeorge.Wilson (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 543283b8460SGeorge.Wilson ++spa->spa_deadman_calls); 544283b8460SGeorge.Wilson if (zfs_deadman_enabled) 545283b8460SGeorge.Wilson vdev_deadman(spa->spa_root_vdev); 546283b8460SGeorge.Wilson } 547283b8460SGeorge.Wilson 548fa9e4066Sahrens /* 549fa9e4066Sahrens * Create an uninitialized spa_t with the given name. Requires 550fa9e4066Sahrens * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 551fa9e4066Sahrens * exist by calling spa_lookup() first. 552fa9e4066Sahrens */ 553fa9e4066Sahrens spa_t * 554468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot) 555fa9e4066Sahrens { 556fa9e4066Sahrens spa_t *spa; 557c5904d13Seschrock spa_config_dirent_t *dp; 558283b8460SGeorge.Wilson cyc_handler_t hdlr; 559283b8460SGeorge.Wilson cyc_time_t when; 560fa9e4066Sahrens 561fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 562fa9e4066Sahrens 563fa9e4066Sahrens spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 564fa9e4066Sahrens 565c25056deSgw mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 566c25056deSgw mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 56735a5a358SJonathan Adams mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 568bc9014e6SJustin Gibbs mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL); 569c25056deSgw mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 57035a5a358SJonathan Adams mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 571c25056deSgw mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 57245818ee1SMatthew Ahrens mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL); 57335a5a358SJonathan Adams mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 574a1521560SJeff Bonwick mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 575a1521560SJeff Bonwick mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 576c3a66015SMatthew Ahrens mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL); 5770f7643c7SGeorge Wilson mutex_init(&spa->spa_alloc_lock, NULL, MUTEX_DEFAULT, NULL); 578c25056deSgw 579c25056deSgw cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 580bc9014e6SJustin Gibbs cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL); 58135a5a358SJonathan Adams cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 582c25056deSgw cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 583e14bb325SJeff Bonwick cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 584c25056deSgw 585b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 586cde58dbcSMatthew Ahrens bplist_create(&spa->spa_free_bplist[t]); 587b24ab676SJeff Bonwick 588e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 589fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 590fa9e4066Sahrens spa->spa_freeze_txg = UINT64_MAX; 5910373e76bSbonwick spa->spa_final_txg = UINT64_MAX; 592468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 59335a5a358SJonathan Adams spa->spa_proc = &p0; 59435a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 595fa9e4066Sahrens 596283b8460SGeorge.Wilson hdlr.cyh_func = spa_deadman; 597283b8460SGeorge.Wilson hdlr.cyh_arg = spa; 598283b8460SGeorge.Wilson hdlr.cyh_level = CY_LOW_LEVEL; 599283b8460SGeorge.Wilson 60069962b56SMatthew Ahrens spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 601283b8460SGeorge.Wilson 602283b8460SGeorge.Wilson /* 603283b8460SGeorge.Wilson * This determines how often we need to check for hung I/Os after 604283b8460SGeorge.Wilson * the cyclic has already fired. Since checking for hung I/Os is 605283b8460SGeorge.Wilson * an expensive operation we don't want to check too frequently. 60669962b56SMatthew Ahrens * Instead wait for 5 seconds before checking again. 607283b8460SGeorge.Wilson */ 60869962b56SMatthew Ahrens when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms); 609283b8460SGeorge.Wilson when.cyt_when = CY_INFINITY; 610283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 611283b8460SGeorge.Wilson spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 612283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 613283b8460SGeorge.Wilson 614fa9e4066Sahrens refcount_create(&spa->spa_refcount); 615e14bb325SJeff Bonwick spa_config_lock_init(spa); 616fa9e4066Sahrens 617fa9e4066Sahrens avl_add(&spa_namespace_avl, spa); 618fa9e4066Sahrens 6190373e76bSbonwick /* 6200373e76bSbonwick * Set the alternate root, if there is one. 6210373e76bSbonwick */ 6220373e76bSbonwick if (altroot) { 6230373e76bSbonwick spa->spa_root = spa_strdup(altroot); 6240373e76bSbonwick spa_active_count++; 6250373e76bSbonwick } 6260373e76bSbonwick 62794c2d0ebSMatthew Ahrens avl_create(&spa->spa_alloc_tree, zio_bookmark_compare, 6280f7643c7SGeorge Wilson sizeof (zio_t), offsetof(zio_t, io_alloc_node)); 6290f7643c7SGeorge Wilson 630c5904d13Seschrock /* 631c5904d13Seschrock * Every pool starts with the default cachefile 632c5904d13Seschrock */ 633c5904d13Seschrock list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 634c5904d13Seschrock offsetof(spa_config_dirent_t, scd_link)); 635c5904d13Seschrock 636c5904d13Seschrock dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 637ef912c80STim Haley dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 638c5904d13Seschrock list_insert_head(&spa->spa_config_list, dp); 639c5904d13Seschrock 6404b964adaSGeorge Wilson VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 6414b964adaSGeorge Wilson KM_SLEEP) == 0); 6424b964adaSGeorge Wilson 643ad135b5dSChristopher Siden if (config != NULL) { 644ad135b5dSChristopher Siden nvlist_t *features; 645ad135b5dSChristopher Siden 646ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 647ad135b5dSChristopher Siden &features) == 0) { 648ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 649ad135b5dSChristopher Siden 0) == 0); 650ad135b5dSChristopher Siden } 651ad135b5dSChristopher Siden 652468c413aSTim Haley VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 653ad135b5dSChristopher Siden } 654ad135b5dSChristopher Siden 655ad135b5dSChristopher Siden if (spa->spa_label_features == NULL) { 656ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 657ad135b5dSChristopher Siden KM_SLEEP) == 0); 658ad135b5dSChristopher Siden } 659468c413aSTim Haley 660c3a66015SMatthew Ahrens spa->spa_iokstat = kstat_create("zfs", 0, name, 661c3a66015SMatthew Ahrens "disk", KSTAT_TYPE_IO, 1, 0); 662c3a66015SMatthew Ahrens if (spa->spa_iokstat) { 663c3a66015SMatthew Ahrens spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock; 664c3a66015SMatthew Ahrens kstat_install(spa->spa_iokstat); 665c3a66015SMatthew Ahrens } 666c3a66015SMatthew Ahrens 6673b2aab18SMatthew Ahrens spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0); 6683b2aab18SMatthew Ahrens 66981cd5c55SMatthew Ahrens spa->spa_min_ashift = INT_MAX; 67081cd5c55SMatthew Ahrens spa->spa_max_ashift = 0; 67181cd5c55SMatthew Ahrens 67243466aaeSMax Grossman /* 67343466aaeSMax Grossman * As a pool is being created, treat all features as disabled by 67443466aaeSMax Grossman * setting SPA_FEATURE_DISABLED for all entries in the feature 67543466aaeSMax Grossman * refcount cache. 67643466aaeSMax Grossman */ 67743466aaeSMax Grossman for (int i = 0; i < SPA_FEATURES; i++) { 67843466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED; 67943466aaeSMax Grossman } 68043466aaeSMax Grossman 681fa9e4066Sahrens return (spa); 682fa9e4066Sahrens } 683fa9e4066Sahrens 684fa9e4066Sahrens /* 685fa9e4066Sahrens * Removes a spa_t from the namespace, freeing up any memory used. Requires 686fa9e4066Sahrens * spa_namespace_lock. This is called only after the spa_t has been closed and 687fa9e4066Sahrens * deactivated. 688fa9e4066Sahrens */ 689fa9e4066Sahrens void 690fa9e4066Sahrens spa_remove(spa_t *spa) 691fa9e4066Sahrens { 692c5904d13Seschrock spa_config_dirent_t *dp; 693c5904d13Seschrock 694fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 695fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 696bc9014e6SJustin Gibbs ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0); 697fa9e4066Sahrens 6981195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 6991195e687SMark J Musante 700fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa); 701fa9e4066Sahrens cv_broadcast(&spa_namespace_cv); 702fa9e4066Sahrens 7030373e76bSbonwick if (spa->spa_root) { 704fa9e4066Sahrens spa_strfree(spa->spa_root); 7050373e76bSbonwick spa_active_count--; 7060373e76bSbonwick } 707fa9e4066Sahrens 708c5904d13Seschrock while ((dp = list_head(&spa->spa_config_list)) != NULL) { 709c5904d13Seschrock list_remove(&spa->spa_config_list, dp); 710c5904d13Seschrock if (dp->scd_path != NULL) 711c5904d13Seschrock spa_strfree(dp->scd_path); 712c5904d13Seschrock kmem_free(dp, sizeof (spa_config_dirent_t)); 713c5904d13Seschrock } 714c5904d13Seschrock 7150f7643c7SGeorge Wilson avl_destroy(&spa->spa_alloc_tree); 716c5904d13Seschrock list_destroy(&spa->spa_config_list); 7172f8aaab3Seschrock 718ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 7194b964adaSGeorge Wilson nvlist_free(spa->spa_load_info); 720fa9e4066Sahrens spa_config_set(spa, NULL); 721fa9e4066Sahrens 722283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 723283b8460SGeorge.Wilson if (spa->spa_deadman_cycid != CYCLIC_NONE) 724283b8460SGeorge.Wilson cyclic_remove(spa->spa_deadman_cycid); 725283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 726283b8460SGeorge.Wilson spa->spa_deadman_cycid = CYCLIC_NONE; 727283b8460SGeorge.Wilson 728fa9e4066Sahrens refcount_destroy(&spa->spa_refcount); 72991ebeef5Sahrens 730e14bb325SJeff Bonwick spa_config_lock_destroy(spa); 731fa9e4066Sahrens 732c3a66015SMatthew Ahrens kstat_delete(spa->spa_iokstat); 733c3a66015SMatthew Ahrens spa->spa_iokstat = NULL; 734c3a66015SMatthew Ahrens 735b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 736cde58dbcSMatthew Ahrens bplist_destroy(&spa->spa_free_bplist[t]); 737b24ab676SJeff Bonwick 73845818ee1SMatthew Ahrens zio_checksum_templates_free(spa); 73945818ee1SMatthew Ahrens 740c25056deSgw cv_destroy(&spa->spa_async_cv); 741bc9014e6SJustin Gibbs cv_destroy(&spa->spa_evicting_os_cv); 74235a5a358SJonathan Adams cv_destroy(&spa->spa_proc_cv); 743c25056deSgw cv_destroy(&spa->spa_scrub_io_cv); 744e14bb325SJeff Bonwick cv_destroy(&spa->spa_suspend_cv); 745c25056deSgw 7460f7643c7SGeorge Wilson mutex_destroy(&spa->spa_alloc_lock); 7475ad82045Snd mutex_destroy(&spa->spa_async_lock); 748c25056deSgw mutex_destroy(&spa->spa_errlist_lock); 74935a5a358SJonathan Adams mutex_destroy(&spa->spa_errlog_lock); 750bc9014e6SJustin Gibbs mutex_destroy(&spa->spa_evicting_os_lock); 75106eeb2adSek mutex_destroy(&spa->spa_history_lock); 75235a5a358SJonathan Adams mutex_destroy(&spa->spa_proc_lock); 753b1b8ab34Slling mutex_destroy(&spa->spa_props_lock); 75445818ee1SMatthew Ahrens mutex_destroy(&spa->spa_cksum_tmpls_lock); 75535a5a358SJonathan Adams mutex_destroy(&spa->spa_scrub_lock); 756e14bb325SJeff Bonwick mutex_destroy(&spa->spa_suspend_lock); 757a1521560SJeff Bonwick mutex_destroy(&spa->spa_vdev_top_lock); 758c3a66015SMatthew Ahrens mutex_destroy(&spa->spa_iokstat_lock); 7595ad82045Snd 760fa9e4066Sahrens kmem_free(spa, sizeof (spa_t)); 761fa9e4066Sahrens } 762fa9e4066Sahrens 763fa9e4066Sahrens /* 764fa9e4066Sahrens * Given a pool, return the next pool in the namespace, or NULL if there is 765fa9e4066Sahrens * none. If 'prev' is NULL, return the first pool. 766fa9e4066Sahrens */ 767fa9e4066Sahrens spa_t * 768fa9e4066Sahrens spa_next(spa_t *prev) 769fa9e4066Sahrens { 770fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 771fa9e4066Sahrens 772fa9e4066Sahrens if (prev) 773fa9e4066Sahrens return (AVL_NEXT(&spa_namespace_avl, prev)); 774fa9e4066Sahrens else 775fa9e4066Sahrens return (avl_first(&spa_namespace_avl)); 776fa9e4066Sahrens } 777fa9e4066Sahrens 778fa9e4066Sahrens /* 779fa9e4066Sahrens * ========================================================================== 780fa9e4066Sahrens * SPA refcount functions 781fa9e4066Sahrens * ========================================================================== 782fa9e4066Sahrens */ 783fa9e4066Sahrens 784fa9e4066Sahrens /* 785fa9e4066Sahrens * Add a reference to the given spa_t. Must have at least one reference, or 786fa9e4066Sahrens * have the namespace lock held. 787fa9e4066Sahrens */ 788fa9e4066Sahrens void 789fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag) 790fa9e4066Sahrens { 791088f3894Sahrens ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref || 792fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 793fa9e4066Sahrens (void) refcount_add(&spa->spa_refcount, tag); 794fa9e4066Sahrens } 795fa9e4066Sahrens 796fa9e4066Sahrens /* 797fa9e4066Sahrens * Remove a reference to the given spa_t. Must have at least one reference, or 798fa9e4066Sahrens * have the namespace lock held. 799fa9e4066Sahrens */ 800fa9e4066Sahrens void 801fa9e4066Sahrens spa_close(spa_t *spa, void *tag) 802fa9e4066Sahrens { 803088f3894Sahrens ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref || 804fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 805fa9e4066Sahrens (void) refcount_remove(&spa->spa_refcount, tag); 806fa9e4066Sahrens } 807fa9e4066Sahrens 808bc9014e6SJustin Gibbs /* 809bc9014e6SJustin Gibbs * Remove a reference to the given spa_t held by a dsl dir that is 810bc9014e6SJustin Gibbs * being asynchronously released. Async releases occur from a taskq 811bc9014e6SJustin Gibbs * performing eviction of dsl datasets and dirs. The namespace lock 812bc9014e6SJustin Gibbs * isn't held and the hold by the object being evicted may contribute to 813bc9014e6SJustin Gibbs * spa_minref (e.g. dataset or directory released during pool export), 814bc9014e6SJustin Gibbs * so the asserts in spa_close() do not apply. 815bc9014e6SJustin Gibbs */ 816bc9014e6SJustin Gibbs void 817bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag) 818bc9014e6SJustin Gibbs { 819bc9014e6SJustin Gibbs (void) refcount_remove(&spa->spa_refcount, tag); 820bc9014e6SJustin Gibbs } 821bc9014e6SJustin Gibbs 822fa9e4066Sahrens /* 823fa9e4066Sahrens * Check to see if the spa refcount is zero. Must be called with 824088f3894Sahrens * spa_namespace_lock held. We really compare against spa_minref, which is the 825fa9e4066Sahrens * number of references acquired when opening a pool 826fa9e4066Sahrens */ 827fa9e4066Sahrens boolean_t 828fa9e4066Sahrens spa_refcount_zero(spa_t *spa) 829fa9e4066Sahrens { 830fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 831fa9e4066Sahrens 832088f3894Sahrens return (refcount_count(&spa->spa_refcount) == spa->spa_minref); 833fa9e4066Sahrens } 834fa9e4066Sahrens 83599653d4eSeschrock /* 83699653d4eSeschrock * ========================================================================== 837fa94a07fSbrendan * SPA spare and l2cache tracking 83899653d4eSeschrock * ========================================================================== 83999653d4eSeschrock */ 84099653d4eSeschrock 841fa94a07fSbrendan /* 842fa94a07fSbrendan * Hot spares and cache devices are tracked using the same code below, 843fa94a07fSbrendan * for 'auxiliary' devices. 844fa94a07fSbrendan */ 845fa94a07fSbrendan 846fa94a07fSbrendan typedef struct spa_aux { 847fa94a07fSbrendan uint64_t aux_guid; 848fa94a07fSbrendan uint64_t aux_pool; 849fa94a07fSbrendan avl_node_t aux_avl; 850fa94a07fSbrendan int aux_count; 851fa94a07fSbrendan } spa_aux_t; 852fa94a07fSbrendan 853fa94a07fSbrendan static int 854fa94a07fSbrendan spa_aux_compare(const void *a, const void *b) 855fa94a07fSbrendan { 856fa94a07fSbrendan const spa_aux_t *sa = a; 857fa94a07fSbrendan const spa_aux_t *sb = b; 858fa94a07fSbrendan 859fa94a07fSbrendan if (sa->aux_guid < sb->aux_guid) 860fa94a07fSbrendan return (-1); 861fa94a07fSbrendan else if (sa->aux_guid > sb->aux_guid) 862fa94a07fSbrendan return (1); 863fa94a07fSbrendan else 864fa94a07fSbrendan return (0); 865fa94a07fSbrendan } 866fa94a07fSbrendan 867fa94a07fSbrendan void 868fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl) 869fa94a07fSbrendan { 870fa94a07fSbrendan avl_index_t where; 871fa94a07fSbrendan spa_aux_t search; 872fa94a07fSbrendan spa_aux_t *aux; 873fa94a07fSbrendan 874fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 875fa94a07fSbrendan if ((aux = avl_find(avl, &search, &where)) != NULL) { 876fa94a07fSbrendan aux->aux_count++; 877fa94a07fSbrendan } else { 878fa94a07fSbrendan aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 879fa94a07fSbrendan aux->aux_guid = vd->vdev_guid; 880fa94a07fSbrendan aux->aux_count = 1; 881fa94a07fSbrendan avl_insert(avl, aux, where); 882fa94a07fSbrendan } 883fa94a07fSbrendan } 884fa94a07fSbrendan 885fa94a07fSbrendan void 886fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 887fa94a07fSbrendan { 888fa94a07fSbrendan spa_aux_t search; 889fa94a07fSbrendan spa_aux_t *aux; 890fa94a07fSbrendan avl_index_t where; 891fa94a07fSbrendan 892fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 893fa94a07fSbrendan aux = avl_find(avl, &search, &where); 894fa94a07fSbrendan 895fa94a07fSbrendan ASSERT(aux != NULL); 896fa94a07fSbrendan 897fa94a07fSbrendan if (--aux->aux_count == 0) { 898fa94a07fSbrendan avl_remove(avl, aux); 899fa94a07fSbrendan kmem_free(aux, sizeof (spa_aux_t)); 900fa94a07fSbrendan } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 901fa94a07fSbrendan aux->aux_pool = 0ULL; 902fa94a07fSbrendan } 903fa94a07fSbrendan } 904fa94a07fSbrendan 905fa94a07fSbrendan boolean_t 90689a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 907fa94a07fSbrendan { 908fa94a07fSbrendan spa_aux_t search, *found; 909fa94a07fSbrendan 910fa94a07fSbrendan search.aux_guid = guid; 91189a89ebfSlling found = avl_find(avl, &search, NULL); 912fa94a07fSbrendan 913fa94a07fSbrendan if (pool) { 914fa94a07fSbrendan if (found) 915fa94a07fSbrendan *pool = found->aux_pool; 916fa94a07fSbrendan else 917fa94a07fSbrendan *pool = 0ULL; 918fa94a07fSbrendan } 919fa94a07fSbrendan 92089a89ebfSlling if (refcnt) { 92189a89ebfSlling if (found) 92289a89ebfSlling *refcnt = found->aux_count; 92389a89ebfSlling else 92489a89ebfSlling *refcnt = 0; 92589a89ebfSlling } 92689a89ebfSlling 927fa94a07fSbrendan return (found != NULL); 928fa94a07fSbrendan } 929fa94a07fSbrendan 930fa94a07fSbrendan void 931fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 932fa94a07fSbrendan { 933fa94a07fSbrendan spa_aux_t search, *found; 934fa94a07fSbrendan avl_index_t where; 935fa94a07fSbrendan 936fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 937fa94a07fSbrendan found = avl_find(avl, &search, &where); 938fa94a07fSbrendan ASSERT(found != NULL); 939fa94a07fSbrendan ASSERT(found->aux_pool == 0ULL); 940fa94a07fSbrendan 941fa94a07fSbrendan found->aux_pool = spa_guid(vd->vdev_spa); 942fa94a07fSbrendan } 943fa94a07fSbrendan 94499653d4eSeschrock /* 94539c23413Seschrock * Spares are tracked globally due to the following constraints: 94639c23413Seschrock * 94739c23413Seschrock * - A spare may be part of multiple pools. 94839c23413Seschrock * - A spare may be added to a pool even if it's actively in use within 94939c23413Seschrock * another pool. 95039c23413Seschrock * - A spare in use in any pool can only be the source of a replacement if 95139c23413Seschrock * the target is a spare in the same pool. 95239c23413Seschrock * 95339c23413Seschrock * We keep track of all spares on the system through the use of a reference 95439c23413Seschrock * counted AVL tree. When a vdev is added as a spare, or used as a replacement 95539c23413Seschrock * spare, then we bump the reference count in the AVL tree. In addition, we set 95639c23413Seschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or 95739c23413Seschrock * inactive). When a spare is made active (used to replace a device in the 95839c23413Seschrock * pool), we also keep track of which pool its been made a part of. 95939c23413Seschrock * 96039c23413Seschrock * The 'spa_spare_lock' protects the AVL tree. These functions are normally 96139c23413Seschrock * called under the spa_namespace lock as part of vdev reconfiguration. The 96239c23413Seschrock * separate spare lock exists for the status query path, which does not need to 96339c23413Seschrock * be completely consistent with respect to other vdev configuration changes. 96499653d4eSeschrock */ 96539c23413Seschrock 96699653d4eSeschrock static int 96799653d4eSeschrock spa_spare_compare(const void *a, const void *b) 96899653d4eSeschrock { 969fa94a07fSbrendan return (spa_aux_compare(a, b)); 97099653d4eSeschrock } 97199653d4eSeschrock 97299653d4eSeschrock void 97339c23413Seschrock spa_spare_add(vdev_t *vd) 97499653d4eSeschrock { 97599653d4eSeschrock mutex_enter(&spa_spare_lock); 97639c23413Seschrock ASSERT(!vd->vdev_isspare); 977fa94a07fSbrendan spa_aux_add(vd, &spa_spare_avl); 97839c23413Seschrock vd->vdev_isspare = B_TRUE; 97999653d4eSeschrock mutex_exit(&spa_spare_lock); 98099653d4eSeschrock } 98199653d4eSeschrock 98299653d4eSeschrock void 98339c23413Seschrock spa_spare_remove(vdev_t *vd) 98499653d4eSeschrock { 98599653d4eSeschrock mutex_enter(&spa_spare_lock); 98639c23413Seschrock ASSERT(vd->vdev_isspare); 987fa94a07fSbrendan spa_aux_remove(vd, &spa_spare_avl); 98839c23413Seschrock vd->vdev_isspare = B_FALSE; 98999653d4eSeschrock mutex_exit(&spa_spare_lock); 99099653d4eSeschrock } 99199653d4eSeschrock 99299653d4eSeschrock boolean_t 99389a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 99499653d4eSeschrock { 995fa94a07fSbrendan boolean_t found; 99699653d4eSeschrock 99799653d4eSeschrock mutex_enter(&spa_spare_lock); 99889a89ebfSlling found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 99999653d4eSeschrock mutex_exit(&spa_spare_lock); 100099653d4eSeschrock 1001fa94a07fSbrendan return (found); 100239c23413Seschrock } 100339c23413Seschrock 100439c23413Seschrock void 100539c23413Seschrock spa_spare_activate(vdev_t *vd) 100639c23413Seschrock { 100739c23413Seschrock mutex_enter(&spa_spare_lock); 100839c23413Seschrock ASSERT(vd->vdev_isspare); 1009fa94a07fSbrendan spa_aux_activate(vd, &spa_spare_avl); 1010fa94a07fSbrendan mutex_exit(&spa_spare_lock); 1011fa94a07fSbrendan } 101239c23413Seschrock 1013fa94a07fSbrendan /* 1014fa94a07fSbrendan * Level 2 ARC devices are tracked globally for the same reasons as spares. 1015fa94a07fSbrendan * Cache devices currently only support one pool per cache device, and so 1016fa94a07fSbrendan * for these devices the aux reference count is currently unused beyond 1. 1017fa94a07fSbrendan */ 101839c23413Seschrock 1019fa94a07fSbrendan static int 1020fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b) 1021fa94a07fSbrendan { 1022fa94a07fSbrendan return (spa_aux_compare(a, b)); 1023fa94a07fSbrendan } 1024fa94a07fSbrendan 1025fa94a07fSbrendan void 1026fa94a07fSbrendan spa_l2cache_add(vdev_t *vd) 1027fa94a07fSbrendan { 1028fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1029fa94a07fSbrendan ASSERT(!vd->vdev_isl2cache); 1030fa94a07fSbrendan spa_aux_add(vd, &spa_l2cache_avl); 1031fa94a07fSbrendan vd->vdev_isl2cache = B_TRUE; 1032fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1033fa94a07fSbrendan } 1034fa94a07fSbrendan 1035fa94a07fSbrendan void 1036fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd) 1037fa94a07fSbrendan { 1038fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1039fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1040fa94a07fSbrendan spa_aux_remove(vd, &spa_l2cache_avl); 1041fa94a07fSbrendan vd->vdev_isl2cache = B_FALSE; 1042fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1043fa94a07fSbrendan } 1044fa94a07fSbrendan 1045fa94a07fSbrendan boolean_t 1046fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool) 1047fa94a07fSbrendan { 1048fa94a07fSbrendan boolean_t found; 1049fa94a07fSbrendan 1050fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 105189a89ebfSlling found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 1052fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1053fa94a07fSbrendan 1054fa94a07fSbrendan return (found); 1055fa94a07fSbrendan } 1056fa94a07fSbrendan 1057fa94a07fSbrendan void 1058fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd) 1059fa94a07fSbrendan { 1060fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1061fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1062fa94a07fSbrendan spa_aux_activate(vd, &spa_l2cache_avl); 1063fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1064fa94a07fSbrendan } 1065fa94a07fSbrendan 1066fa9e4066Sahrens /* 1067fa9e4066Sahrens * ========================================================================== 1068fa9e4066Sahrens * SPA vdev locking 1069fa9e4066Sahrens * ========================================================================== 1070fa9e4066Sahrens */ 1071fa9e4066Sahrens 1072fa9e4066Sahrens /* 1073ea8dc4b6Seschrock * Lock the given spa_t for the purpose of adding or removing a vdev. 1074ea8dc4b6Seschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing. 1075fa9e4066Sahrens * It returns the next transaction group for the spa_t. 1076fa9e4066Sahrens */ 1077fa9e4066Sahrens uint64_t 1078fa9e4066Sahrens spa_vdev_enter(spa_t *spa) 1079fa9e4066Sahrens { 1080a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 1081bbfd46c4SJeff Bonwick mutex_enter(&spa_namespace_lock); 108288ecc943SGeorge Wilson return (spa_vdev_config_enter(spa)); 108388ecc943SGeorge Wilson } 108488ecc943SGeorge Wilson 108588ecc943SGeorge Wilson /* 108688ecc943SGeorge Wilson * Internal implementation for spa_vdev_enter(). Used when a vdev 108788ecc943SGeorge Wilson * operation requires multiple syncs (i.e. removing a device) while 108888ecc943SGeorge Wilson * keeping the spa_namespace_lock held. 108988ecc943SGeorge Wilson */ 109088ecc943SGeorge Wilson uint64_t 109188ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa) 109288ecc943SGeorge Wilson { 109388ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 10943d7072f8Seschrock 1095e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1096fa9e4066Sahrens 1097fa9e4066Sahrens return (spa_last_synced_txg(spa) + 1); 1098fa9e4066Sahrens } 1099fa9e4066Sahrens 1100fa9e4066Sahrens /* 110188ecc943SGeorge Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing 110288ecc943SGeorge Wilson * of multiple transactions without releasing the spa_namespace_lock. 1103fa9e4066Sahrens */ 110488ecc943SGeorge Wilson void 110588ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 1106fa9e4066Sahrens { 110788ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 110888ecc943SGeorge Wilson 11090e34b6a7Sbonwick int config_changed = B_FALSE; 1110ea8dc4b6Seschrock 11110373e76bSbonwick ASSERT(txg > spa_last_synced_txg(spa)); 11120e34b6a7Sbonwick 1113e14bb325SJeff Bonwick spa->spa_pending_vdev = NULL; 1114e14bb325SJeff Bonwick 11150e34b6a7Sbonwick /* 11160e34b6a7Sbonwick * Reassess the DTLs. 11170e34b6a7Sbonwick */ 11180373e76bSbonwick vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 11190e34b6a7Sbonwick 1120e14bb325SJeff Bonwick if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 11210e34b6a7Sbonwick config_changed = B_TRUE; 11228f18d1faSGeorge Wilson spa->spa_config_generation++; 11230e34b6a7Sbonwick } 1124ea8dc4b6Seschrock 112588ecc943SGeorge Wilson /* 112688ecc943SGeorge Wilson * Verify the metaslab classes. 112788ecc943SGeorge Wilson */ 1128b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1129b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 113088ecc943SGeorge Wilson 1131e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1132fa9e4066Sahrens 113388ecc943SGeorge Wilson /* 113488ecc943SGeorge Wilson * Panic the system if the specified tag requires it. This 113588ecc943SGeorge Wilson * is useful for ensuring that configurations are updated 113688ecc943SGeorge Wilson * transactionally. 113788ecc943SGeorge Wilson */ 113888ecc943SGeorge Wilson if (zio_injection_enabled) 11391195e687SMark J Musante zio_handle_panic_injection(spa, tag, 0); 114088ecc943SGeorge Wilson 1141fa9e4066Sahrens /* 1142fa9e4066Sahrens * Note: this txg_wait_synced() is important because it ensures 1143fa9e4066Sahrens * that there won't be more than one config change per txg. 1144fa9e4066Sahrens * This allows us to use the txg as the generation number. 1145fa9e4066Sahrens */ 1146fa9e4066Sahrens if (error == 0) 1147fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1148fa9e4066Sahrens 1149fa9e4066Sahrens if (vd != NULL) { 11500713e232SGeorge Wilson ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL); 11518ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1152fa9e4066Sahrens vdev_free(vd); 11538ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1154fa9e4066Sahrens } 1155fa9e4066Sahrens 1156fa9e4066Sahrens /* 11570e34b6a7Sbonwick * If the config changed, update the config cache. 1158fa9e4066Sahrens */ 11590e34b6a7Sbonwick if (config_changed) 1160*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 116188ecc943SGeorge Wilson } 1162ea8dc4b6Seschrock 116388ecc943SGeorge Wilson /* 116488ecc943SGeorge Wilson * Unlock the spa_t after adding or removing a vdev. Besides undoing the 116588ecc943SGeorge Wilson * locking of spa_vdev_enter(), we also want make sure the transactions have 116688ecc943SGeorge Wilson * synced to disk, and then update the global configuration cache with the new 116788ecc943SGeorge Wilson * information. 116888ecc943SGeorge Wilson */ 116988ecc943SGeorge Wilson int 117088ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 117188ecc943SGeorge Wilson { 117288ecc943SGeorge Wilson spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1173ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1174bbfd46c4SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 1175fa9e4066Sahrens 1176fa9e4066Sahrens return (error); 1177fa9e4066Sahrens } 1178fa9e4066Sahrens 1179e14bb325SJeff Bonwick /* 1180e14bb325SJeff Bonwick * Lock the given spa_t for the purpose of changing vdev state. 1181e14bb325SJeff Bonwick */ 1182e14bb325SJeff Bonwick void 11838f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks) 1184e14bb325SJeff Bonwick { 11858f18d1faSGeorge Wilson int locks = SCL_STATE_ALL | oplocks; 11868f18d1faSGeorge Wilson 1187dcba9f3fSGeorge Wilson /* 1188dcba9f3fSGeorge Wilson * Root pools may need to read of the underlying devfs filesystem 1189dcba9f3fSGeorge Wilson * when opening up a vdev. Unfortunately if we're holding the 1190dcba9f3fSGeorge Wilson * SCL_ZIO lock it will result in a deadlock when we try to issue 1191dcba9f3fSGeorge Wilson * the read from the root filesystem. Instead we "prefetch" 1192dcba9f3fSGeorge Wilson * the associated vnodes that we need prior to opening the 1193dcba9f3fSGeorge Wilson * underlying devices and cache them so that we can prevent 1194dcba9f3fSGeorge Wilson * any I/O when we are doing the actual open. 1195dcba9f3fSGeorge Wilson */ 1196dcba9f3fSGeorge Wilson if (spa_is_root(spa)) { 11979842588bSGeorge Wilson int low = locks & ~(SCL_ZIO - 1); 11989842588bSGeorge Wilson int high = locks & ~low; 11999842588bSGeorge Wilson 12009842588bSGeorge Wilson spa_config_enter(spa, high, spa, RW_WRITER); 1201dcba9f3fSGeorge Wilson vdev_hold(spa->spa_root_vdev); 12029842588bSGeorge Wilson spa_config_enter(spa, low, spa, RW_WRITER); 1203dcba9f3fSGeorge Wilson } else { 1204dcba9f3fSGeorge Wilson spa_config_enter(spa, locks, spa, RW_WRITER); 1205dcba9f3fSGeorge Wilson } 12068f18d1faSGeorge Wilson spa->spa_vdev_locks = locks; 1207e14bb325SJeff Bonwick } 1208e14bb325SJeff Bonwick 1209e14bb325SJeff Bonwick int 1210e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1211e14bb325SJeff Bonwick { 1212c6065d0fSGeorge Wilson boolean_t config_changed = B_FALSE; 1213c6065d0fSGeorge Wilson 1214b24ab676SJeff Bonwick if (vd != NULL || error == 0) 1215b24ab676SJeff Bonwick vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1216b24ab676SJeff Bonwick 0, 0, B_FALSE); 1217b24ab676SJeff Bonwick 12188f18d1faSGeorge Wilson if (vd != NULL) { 1219e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 1220c6065d0fSGeorge Wilson config_changed = B_TRUE; 12218f18d1faSGeorge Wilson spa->spa_config_generation++; 12228f18d1faSGeorge Wilson } 1223e14bb325SJeff Bonwick 1224dcba9f3fSGeorge Wilson if (spa_is_root(spa)) 1225dcba9f3fSGeorge Wilson vdev_rele(spa->spa_root_vdev); 1226dcba9f3fSGeorge Wilson 12278f18d1faSGeorge Wilson ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 12288f18d1faSGeorge Wilson spa_config_exit(spa, spa->spa_vdev_locks, spa); 1229e14bb325SJeff Bonwick 12308ad4d6ddSJeff Bonwick /* 12318ad4d6ddSJeff Bonwick * If anything changed, wait for it to sync. This ensures that, 12328ad4d6ddSJeff Bonwick * from the system administrator's perspective, zpool(1M) commands 12338ad4d6ddSJeff Bonwick * are synchronous. This is important for things like zpool offline: 12348ad4d6ddSJeff Bonwick * when the command completes, you expect no further I/O from ZFS. 12358ad4d6ddSJeff Bonwick */ 12368ad4d6ddSJeff Bonwick if (vd != NULL) 12378ad4d6ddSJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, 0); 12388ad4d6ddSJeff Bonwick 1239c6065d0fSGeorge Wilson /* 1240c6065d0fSGeorge Wilson * If the config changed, update the config cache. 1241c6065d0fSGeorge Wilson */ 1242c6065d0fSGeorge Wilson if (config_changed) { 1243c6065d0fSGeorge Wilson mutex_enter(&spa_namespace_lock); 1244*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 1245c6065d0fSGeorge Wilson mutex_exit(&spa_namespace_lock); 1246c6065d0fSGeorge Wilson } 1247c6065d0fSGeorge Wilson 1248e14bb325SJeff Bonwick return (error); 1249e14bb325SJeff Bonwick } 1250e14bb325SJeff Bonwick 1251fa9e4066Sahrens /* 1252fa9e4066Sahrens * ========================================================================== 1253fa9e4066Sahrens * Miscellaneous functions 1254fa9e4066Sahrens * ========================================================================== 1255fa9e4066Sahrens */ 1256fa9e4066Sahrens 1257ad135b5dSChristopher Siden void 125843466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx) 1259ad135b5dSChristopher Siden { 12602acef22dSMatthew Ahrens if (!nvlist_exists(spa->spa_label_features, feature)) { 12612acef22dSMatthew Ahrens fnvlist_add_boolean(spa->spa_label_features, feature); 126243466aaeSMax Grossman /* 126343466aaeSMax Grossman * When we are creating the pool (tx_txg==TXG_INITIAL), we can't 126443466aaeSMax Grossman * dirty the vdev config because lock SCL_CONFIG is not held. 126543466aaeSMax Grossman * Thankfully, in this case we don't need to dirty the config 126643466aaeSMax Grossman * because it will be written out anyway when we finish 126743466aaeSMax Grossman * creating the pool. 126843466aaeSMax Grossman */ 126943466aaeSMax Grossman if (tx->tx_txg != TXG_INITIAL) 127043466aaeSMax Grossman vdev_config_dirty(spa->spa_root_vdev); 12712acef22dSMatthew Ahrens } 1272ad135b5dSChristopher Siden } 1273ad135b5dSChristopher Siden 1274ad135b5dSChristopher Siden void 1275ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1276ad135b5dSChristopher Siden { 12772acef22dSMatthew Ahrens if (nvlist_remove_all(spa->spa_label_features, feature) == 0) 12782acef22dSMatthew Ahrens vdev_config_dirty(spa->spa_root_vdev); 1279ad135b5dSChristopher Siden } 1280ad135b5dSChristopher Siden 1281fa9e4066Sahrens /* 1282fa9e4066Sahrens * Rename a spa_t. 1283fa9e4066Sahrens */ 1284fa9e4066Sahrens int 1285fa9e4066Sahrens spa_rename(const char *name, const char *newname) 1286fa9e4066Sahrens { 1287fa9e4066Sahrens spa_t *spa; 1288fa9e4066Sahrens int err; 1289fa9e4066Sahrens 1290fa9e4066Sahrens /* 1291fa9e4066Sahrens * Lookup the spa_t and grab the config lock for writing. We need to 1292fa9e4066Sahrens * actually open the pool so that we can sync out the necessary labels. 1293fa9e4066Sahrens * It's OK to call spa_open() with the namespace lock held because we 1294ea8dc4b6Seschrock * allow recursive calls for other reasons. 1295fa9e4066Sahrens */ 1296fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 1297fa9e4066Sahrens if ((err = spa_open(name, &spa, FTAG)) != 0) { 1298fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1299fa9e4066Sahrens return (err); 1300fa9e4066Sahrens } 1301fa9e4066Sahrens 1302e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1303fa9e4066Sahrens 1304fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa); 1305e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name)); 1306fa9e4066Sahrens avl_add(&spa_namespace_avl, spa); 1307fa9e4066Sahrens 1308fa9e4066Sahrens /* 1309fa9e4066Sahrens * Sync all labels to disk with the new names by marking the root vdev 1310fa9e4066Sahrens * dirty and waiting for it to sync. It will pick up the new pool name 1311fa9e4066Sahrens * during the sync. 1312fa9e4066Sahrens */ 1313fa9e4066Sahrens vdev_config_dirty(spa->spa_root_vdev); 1314fa9e4066Sahrens 1315e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1316fa9e4066Sahrens 13170373e76bSbonwick txg_wait_synced(spa->spa_dsl_pool, 0); 1318fa9e4066Sahrens 1319fa9e4066Sahrens /* 1320fa9e4066Sahrens * Sync the updated config cache. 1321fa9e4066Sahrens */ 1322*5cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 1323fa9e4066Sahrens 1324fa9e4066Sahrens spa_close(spa, FTAG); 1325fa9e4066Sahrens 1326fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 1327fa9e4066Sahrens 1328fa9e4066Sahrens return (0); 1329fa9e4066Sahrens } 1330fa9e4066Sahrens 1331fa9e4066Sahrens /* 1332f9af39baSGeorge Wilson * Return the spa_t associated with given pool_guid, if it exists. If 1333f9af39baSGeorge Wilson * device_guid is non-zero, determine whether the pool exists *and* contains 1334f9af39baSGeorge Wilson * a device with the specified device_guid. 1335fa9e4066Sahrens */ 1336f9af39baSGeorge Wilson spa_t * 1337f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1338fa9e4066Sahrens { 1339fa9e4066Sahrens spa_t *spa; 1340fa9e4066Sahrens avl_tree_t *t = &spa_namespace_avl; 1341fa9e4066Sahrens 1342ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1343fa9e4066Sahrens 1344fa9e4066Sahrens for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1345fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1346fa9e4066Sahrens continue; 1347fa9e4066Sahrens if (spa->spa_root_vdev == NULL) 1348fa9e4066Sahrens continue; 134939c23413Seschrock if (spa_guid(spa) == pool_guid) { 135039c23413Seschrock if (device_guid == 0) 135139c23413Seschrock break; 135239c23413Seschrock 135339c23413Seschrock if (vdev_lookup_by_guid(spa->spa_root_vdev, 135439c23413Seschrock device_guid) != NULL) 135539c23413Seschrock break; 135639c23413Seschrock 135739c23413Seschrock /* 13588654d025Sperrin * Check any devices we may be in the process of adding. 135939c23413Seschrock */ 136039c23413Seschrock if (spa->spa_pending_vdev) { 136139c23413Seschrock if (vdev_lookup_by_guid(spa->spa_pending_vdev, 136239c23413Seschrock device_guid) != NULL) 136339c23413Seschrock break; 136439c23413Seschrock } 136539c23413Seschrock } 1366fa9e4066Sahrens } 1367fa9e4066Sahrens 1368f9af39baSGeorge Wilson return (spa); 1369f9af39baSGeorge Wilson } 1370f9af39baSGeorge Wilson 1371f9af39baSGeorge Wilson /* 1372f9af39baSGeorge Wilson * Determine whether a pool with the given pool_guid exists. 1373f9af39baSGeorge Wilson */ 1374f9af39baSGeorge Wilson boolean_t 1375f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1376f9af39baSGeorge Wilson { 1377f9af39baSGeorge Wilson return (spa_by_guid(pool_guid, device_guid) != NULL); 1378fa9e4066Sahrens } 1379fa9e4066Sahrens 1380fa9e4066Sahrens char * 1381fa9e4066Sahrens spa_strdup(const char *s) 1382fa9e4066Sahrens { 1383fa9e4066Sahrens size_t len; 1384fa9e4066Sahrens char *new; 1385fa9e4066Sahrens 1386fa9e4066Sahrens len = strlen(s); 1387fa9e4066Sahrens new = kmem_alloc(len + 1, KM_SLEEP); 1388fa9e4066Sahrens bcopy(s, new, len); 1389fa9e4066Sahrens new[len] = '\0'; 1390fa9e4066Sahrens 1391fa9e4066Sahrens return (new); 1392fa9e4066Sahrens } 1393fa9e4066Sahrens 1394fa9e4066Sahrens void 1395fa9e4066Sahrens spa_strfree(char *s) 1396fa9e4066Sahrens { 1397fa9e4066Sahrens kmem_free(s, strlen(s) + 1); 1398fa9e4066Sahrens } 1399fa9e4066Sahrens 1400fa9e4066Sahrens uint64_t 1401fa9e4066Sahrens spa_get_random(uint64_t range) 1402fa9e4066Sahrens { 1403fa9e4066Sahrens uint64_t r; 1404fa9e4066Sahrens 1405fa9e4066Sahrens ASSERT(range != 0); 1406fa9e4066Sahrens 1407fa9e4066Sahrens (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1408fa9e4066Sahrens 1409fa9e4066Sahrens return (r % range); 1410fa9e4066Sahrens } 1411fa9e4066Sahrens 14121195e687SMark J Musante uint64_t 14131195e687SMark J Musante spa_generate_guid(spa_t *spa) 14141195e687SMark J Musante { 14151195e687SMark J Musante uint64_t guid = spa_get_random(-1ULL); 14161195e687SMark J Musante 14171195e687SMark J Musante if (spa != NULL) { 14181195e687SMark J Musante while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 14191195e687SMark J Musante guid = spa_get_random(-1ULL); 14201195e687SMark J Musante } else { 14211195e687SMark J Musante while (guid == 0 || spa_guid_exists(guid, 0)) 14221195e687SMark J Musante guid = spa_get_random(-1ULL); 14231195e687SMark J Musante } 14241195e687SMark J Musante 14251195e687SMark J Musante return (guid); 14261195e687SMark J Musante } 14271195e687SMark J Musante 1428fa9e4066Sahrens void 142943466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp) 1430fa9e4066Sahrens { 1431ad135b5dSChristopher Siden char type[256]; 1432f0ba89beSJeff Bonwick char *checksum = NULL; 1433f0ba89beSJeff Bonwick char *compress = NULL; 1434f0ba89beSJeff Bonwick 1435f0ba89beSJeff Bonwick if (bp != NULL) { 1436ad135b5dSChristopher Siden if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1437ad135b5dSChristopher Siden dmu_object_byteswap_t bswap = 1438ad135b5dSChristopher Siden DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1439ad135b5dSChristopher Siden (void) snprintf(type, sizeof (type), "bswap %s %s", 1440ad135b5dSChristopher Siden DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1441ad135b5dSChristopher Siden "metadata" : "data", 1442ad135b5dSChristopher Siden dmu_ot_byteswap[bswap].ob_name); 1443ad135b5dSChristopher Siden } else { 1444ad135b5dSChristopher Siden (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1445ad135b5dSChristopher Siden sizeof (type)); 1446ad135b5dSChristopher Siden } 14475d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 14485d7b4d43SMatthew Ahrens checksum = 14495d7b4d43SMatthew Ahrens zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 14505d7b4d43SMatthew Ahrens } 1451f0ba89beSJeff Bonwick compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1452f0ba89beSJeff Bonwick } 1453fa9e4066Sahrens 145443466aaeSMax Grossman SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum, 145543466aaeSMax Grossman compress); 1456fa9e4066Sahrens } 1457fa9e4066Sahrens 1458fa9e4066Sahrens void 1459fa9e4066Sahrens spa_freeze(spa_t *spa) 1460fa9e4066Sahrens { 1461fa9e4066Sahrens uint64_t freeze_txg = 0; 1462fa9e4066Sahrens 1463e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1464fa9e4066Sahrens if (spa->spa_freeze_txg == UINT64_MAX) { 1465fa9e4066Sahrens freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1466fa9e4066Sahrens spa->spa_freeze_txg = freeze_txg; 1467fa9e4066Sahrens } 1468e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1469fa9e4066Sahrens if (freeze_txg != 0) 1470fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1471fa9e4066Sahrens } 1472fa9e4066Sahrens 14730125049cSahrens void 14740125049cSahrens zfs_panic_recover(const char *fmt, ...) 14750125049cSahrens { 14760125049cSahrens va_list adx; 14770125049cSahrens 14780125049cSahrens va_start(adx, fmt); 14790125049cSahrens vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 14800125049cSahrens va_end(adx); 14810125049cSahrens } 14820125049cSahrens 14833f9d6ad7SLin Ling /* 14843f9d6ad7SLin Ling * This is a stripped-down version of strtoull, suitable only for converting 1485f7170741SWill Andrews * lowercase hexadecimal numbers that don't overflow. 14863f9d6ad7SLin Ling */ 14873f9d6ad7SLin Ling uint64_t 14884585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr) 14893f9d6ad7SLin Ling { 14903f9d6ad7SLin Ling uint64_t val = 0; 14913f9d6ad7SLin Ling char c; 14923f9d6ad7SLin Ling int digit; 14933f9d6ad7SLin Ling 14943f9d6ad7SLin Ling while ((c = *str) != '\0') { 14953f9d6ad7SLin Ling if (c >= '0' && c <= '9') 14963f9d6ad7SLin Ling digit = c - '0'; 14973f9d6ad7SLin Ling else if (c >= 'a' && c <= 'f') 14983f9d6ad7SLin Ling digit = 10 + c - 'a'; 14993f9d6ad7SLin Ling else 15003f9d6ad7SLin Ling break; 15013f9d6ad7SLin Ling 15023f9d6ad7SLin Ling val *= 16; 15033f9d6ad7SLin Ling val += digit; 15043f9d6ad7SLin Ling 15053f9d6ad7SLin Ling str++; 15063f9d6ad7SLin Ling } 15073f9d6ad7SLin Ling 15083f9d6ad7SLin Ling if (nptr) 15093f9d6ad7SLin Ling *nptr = (char *)str; 15103f9d6ad7SLin Ling 15113f9d6ad7SLin Ling return (val); 15123f9d6ad7SLin Ling } 15133f9d6ad7SLin Ling 1514fa9e4066Sahrens /* 1515fa9e4066Sahrens * ========================================================================== 1516fa9e4066Sahrens * Accessor functions 1517fa9e4066Sahrens * ========================================================================== 1518fa9e4066Sahrens */ 1519fa9e4066Sahrens 1520088f3894Sahrens boolean_t 152188b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa) 1522fa9e4066Sahrens { 152388b7b0f2SMatthew Ahrens return (spa->spa_async_suspended); 1524fa9e4066Sahrens } 1525fa9e4066Sahrens 1526fa9e4066Sahrens dsl_pool_t * 1527fa9e4066Sahrens spa_get_dsl(spa_t *spa) 1528fa9e4066Sahrens { 1529fa9e4066Sahrens return (spa->spa_dsl_pool); 1530fa9e4066Sahrens } 1531fa9e4066Sahrens 1532ad135b5dSChristopher Siden boolean_t 1533ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa) 1534ad135b5dSChristopher Siden { 1535ad135b5dSChristopher Siden return (spa->spa_is_initializing); 1536ad135b5dSChristopher Siden } 1537ad135b5dSChristopher Siden 1538*5cabbc6bSPrashanth Sreenivasa boolean_t 1539*5cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa) 1540*5cabbc6bSPrashanth Sreenivasa { 1541*5cabbc6bSPrashanth Sreenivasa return (spa->spa_indirect_vdevs_loaded); 1542*5cabbc6bSPrashanth Sreenivasa } 1543*5cabbc6bSPrashanth Sreenivasa 1544fa9e4066Sahrens blkptr_t * 1545fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa) 1546fa9e4066Sahrens { 1547fa9e4066Sahrens return (&spa->spa_ubsync.ub_rootbp); 1548fa9e4066Sahrens } 1549fa9e4066Sahrens 1550fa9e4066Sahrens void 1551fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1552fa9e4066Sahrens { 1553fa9e4066Sahrens spa->spa_uberblock.ub_rootbp = *bp; 1554fa9e4066Sahrens } 1555fa9e4066Sahrens 1556fa9e4066Sahrens void 1557fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen) 1558fa9e4066Sahrens { 1559fa9e4066Sahrens if (spa->spa_root == NULL) 1560fa9e4066Sahrens buf[0] = '\0'; 1561fa9e4066Sahrens else 1562fa9e4066Sahrens (void) strncpy(buf, spa->spa_root, buflen); 1563fa9e4066Sahrens } 1564fa9e4066Sahrens 1565fa9e4066Sahrens int 1566fa9e4066Sahrens spa_sync_pass(spa_t *spa) 1567fa9e4066Sahrens { 1568fa9e4066Sahrens return (spa->spa_sync_pass); 1569fa9e4066Sahrens } 1570fa9e4066Sahrens 1571fa9e4066Sahrens char * 1572fa9e4066Sahrens spa_name(spa_t *spa) 1573fa9e4066Sahrens { 1574fa9e4066Sahrens return (spa->spa_name); 1575fa9e4066Sahrens } 1576fa9e4066Sahrens 1577fa9e4066Sahrens uint64_t 1578fa9e4066Sahrens spa_guid(spa_t *spa) 1579fa9e4066Sahrens { 1580dfbb9432SGeorge Wilson dsl_pool_t *dp = spa_get_dsl(spa); 1581dfbb9432SGeorge Wilson uint64_t guid; 1582dfbb9432SGeorge Wilson 1583b5989ec7Seschrock /* 1584b5989ec7Seschrock * If we fail to parse the config during spa_load(), we can go through 1585b5989ec7Seschrock * the error path (which posts an ereport) and end up here with no root 1586e9103aaeSGarrett D'Amore * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1587b5989ec7Seschrock * this case. 1588b5989ec7Seschrock */ 1589dfbb9432SGeorge Wilson if (spa->spa_root_vdev == NULL) 1590dfbb9432SGeorge Wilson return (spa->spa_config_guid); 1591dfbb9432SGeorge Wilson 1592dfbb9432SGeorge Wilson guid = spa->spa_last_synced_guid != 0 ? 1593dfbb9432SGeorge Wilson spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1594dfbb9432SGeorge Wilson 1595dfbb9432SGeorge Wilson /* 1596dfbb9432SGeorge Wilson * Return the most recently synced out guid unless we're 1597dfbb9432SGeorge Wilson * in syncing context. 1598dfbb9432SGeorge Wilson */ 1599dfbb9432SGeorge Wilson if (dp && dsl_pool_sync_context(dp)) 1600b5989ec7Seschrock return (spa->spa_root_vdev->vdev_guid); 1601b5989ec7Seschrock else 1602dfbb9432SGeorge Wilson return (guid); 1603e9103aaeSGarrett D'Amore } 1604e9103aaeSGarrett D'Amore 1605e9103aaeSGarrett D'Amore uint64_t 1606e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa) 1607e9103aaeSGarrett D'Amore { 1608e9103aaeSGarrett D'Amore /* 1609e9103aaeSGarrett D'Amore * This is a GUID that exists solely as a reference for the 1610e9103aaeSGarrett D'Amore * purposes of the arc. It is generated at load time, and 1611e9103aaeSGarrett D'Amore * is never written to persistent storage. 1612e9103aaeSGarrett D'Amore */ 1613e9103aaeSGarrett D'Amore return (spa->spa_load_guid); 1614fa9e4066Sahrens } 1615fa9e4066Sahrens 1616fa9e4066Sahrens uint64_t 1617fa9e4066Sahrens spa_last_synced_txg(spa_t *spa) 1618fa9e4066Sahrens { 1619fa9e4066Sahrens return (spa->spa_ubsync.ub_txg); 1620fa9e4066Sahrens } 1621fa9e4066Sahrens 1622fa9e4066Sahrens uint64_t 1623fa9e4066Sahrens spa_first_txg(spa_t *spa) 1624fa9e4066Sahrens { 1625fa9e4066Sahrens return (spa->spa_first_txg); 1626fa9e4066Sahrens } 1627fa9e4066Sahrens 1628b24ab676SJeff Bonwick uint64_t 1629b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa) 1630b24ab676SJeff Bonwick { 1631b24ab676SJeff Bonwick return (spa->spa_syncing_txg); 1632b24ab676SJeff Bonwick } 1633b24ab676SJeff Bonwick 16343991b535SGeorge Wilson /* 16353991b535SGeorge Wilson * Return the last txg where data can be dirtied. The final txgs 16363991b535SGeorge Wilson * will be used to just clear out any deferred frees that remain. 16373991b535SGeorge Wilson */ 16383991b535SGeorge Wilson uint64_t 16393991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa) 16403991b535SGeorge Wilson { 16413991b535SGeorge Wilson return (spa->spa_final_txg - TXG_DEFER_SIZE); 16423991b535SGeorge Wilson } 16433991b535SGeorge Wilson 164488b7b0f2SMatthew Ahrens pool_state_t 1645fa9e4066Sahrens spa_state(spa_t *spa) 1646fa9e4066Sahrens { 1647fa9e4066Sahrens return (spa->spa_state); 1648fa9e4066Sahrens } 1649fa9e4066Sahrens 1650b16da2e2SGeorge Wilson spa_load_state_t 1651b16da2e2SGeorge Wilson spa_load_state(spa_t *spa) 1652b16da2e2SGeorge Wilson { 1653b16da2e2SGeorge Wilson return (spa->spa_load_state); 1654b16da2e2SGeorge Wilson } 1655b16da2e2SGeorge Wilson 1656fa9e4066Sahrens uint64_t 1657fa9e4066Sahrens spa_freeze_txg(spa_t *spa) 1658fa9e4066Sahrens { 1659fa9e4066Sahrens return (spa->spa_freeze_txg); 1660fa9e4066Sahrens } 1661fa9e4066Sahrens 1662fa9e4066Sahrens /* ARGSUSED */ 1663fa9e4066Sahrens uint64_t 166461e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize) 1665fa9e4066Sahrens { 166669962b56SMatthew Ahrens return (lsize * spa_asize_inflation); 166744cd46caSbillm } 166844cd46caSbillm 16697d46dc6cSMatthew Ahrens /* 16707d46dc6cSMatthew Ahrens * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%), 16714b5c8e93SMatthew Ahrens * or at least 128MB, unless that would cause it to be more than half the 16724b5c8e93SMatthew Ahrens * pool size. 16737d46dc6cSMatthew Ahrens * 16747d46dc6cSMatthew Ahrens * See the comment above spa_slop_shift for details. 16757d46dc6cSMatthew Ahrens */ 16767d46dc6cSMatthew Ahrens uint64_t 16774b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa) 16784b5c8e93SMatthew Ahrens { 16797d46dc6cSMatthew Ahrens uint64_t space = spa_get_dspace(spa); 16804b5c8e93SMatthew Ahrens return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop))); 16817d46dc6cSMatthew Ahrens } 16827d46dc6cSMatthew Ahrens 1683485bbbf5SGeorge Wilson uint64_t 1684485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa) 1685485bbbf5SGeorge Wilson { 1686485bbbf5SGeorge Wilson return (spa->spa_dspace); 1687485bbbf5SGeorge Wilson } 1688485bbbf5SGeorge Wilson 1689485bbbf5SGeorge Wilson void 1690485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa) 1691485bbbf5SGeorge Wilson { 1692485bbbf5SGeorge Wilson spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1693485bbbf5SGeorge Wilson ddt_get_dedup_dspace(spa); 1694*5cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 1695*5cabbc6bSPrashanth Sreenivasa /* 1696*5cabbc6bSPrashanth Sreenivasa * We can't allocate from the removing device, so 1697*5cabbc6bSPrashanth Sreenivasa * subtract its size. This prevents the DMU/DSL from 1698*5cabbc6bSPrashanth Sreenivasa * filling up the (now smaller) pool while we are in the 1699*5cabbc6bSPrashanth Sreenivasa * middle of removing the device. 1700*5cabbc6bSPrashanth Sreenivasa * 1701*5cabbc6bSPrashanth Sreenivasa * Note that the DMU/DSL doesn't actually know or care 1702*5cabbc6bSPrashanth Sreenivasa * how much space is allocated (it does its own tracking 1703*5cabbc6bSPrashanth Sreenivasa * of how much space has been logically used). So it 1704*5cabbc6bSPrashanth Sreenivasa * doesn't matter that the data we are moving may be 1705*5cabbc6bSPrashanth Sreenivasa * allocated twice (on the old device and the new 1706*5cabbc6bSPrashanth Sreenivasa * device). 1707*5cabbc6bSPrashanth Sreenivasa */ 1708*5cabbc6bSPrashanth Sreenivasa vdev_t *vd = spa->spa_vdev_removal->svr_vdev; 1709*5cabbc6bSPrashanth Sreenivasa spa->spa_dspace -= spa_deflate(spa) ? 1710*5cabbc6bSPrashanth Sreenivasa vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 1711*5cabbc6bSPrashanth Sreenivasa } 1712485bbbf5SGeorge Wilson } 1713485bbbf5SGeorge Wilson 17140a4e9518Sgw /* 17150a4e9518Sgw * Return the failure mode that has been set to this pool. The default 17160a4e9518Sgw * behavior will be to block all I/Os when a complete failure occurs. 17170a4e9518Sgw */ 17180a4e9518Sgw uint8_t 17190a4e9518Sgw spa_get_failmode(spa_t *spa) 17200a4e9518Sgw { 17210a4e9518Sgw return (spa->spa_failmode); 17220a4e9518Sgw } 17230a4e9518Sgw 1724e14bb325SJeff Bonwick boolean_t 1725e14bb325SJeff Bonwick spa_suspended(spa_t *spa) 1726e14bb325SJeff Bonwick { 1727e14bb325SJeff Bonwick return (spa->spa_suspended); 1728e14bb325SJeff Bonwick } 1729e14bb325SJeff Bonwick 173044cd46caSbillm uint64_t 173144cd46caSbillm spa_version(spa_t *spa) 173244cd46caSbillm { 173344cd46caSbillm return (spa->spa_ubsync.ub_version); 173444cd46caSbillm } 173544cd46caSbillm 1736b24ab676SJeff Bonwick boolean_t 1737b24ab676SJeff Bonwick spa_deflate(spa_t *spa) 1738b24ab676SJeff Bonwick { 1739b24ab676SJeff Bonwick return (spa->spa_deflate); 1740b24ab676SJeff Bonwick } 1741b24ab676SJeff Bonwick 1742b24ab676SJeff Bonwick metaslab_class_t * 1743b24ab676SJeff Bonwick spa_normal_class(spa_t *spa) 1744b24ab676SJeff Bonwick { 1745b24ab676SJeff Bonwick return (spa->spa_normal_class); 1746b24ab676SJeff Bonwick } 1747b24ab676SJeff Bonwick 1748b24ab676SJeff Bonwick metaslab_class_t * 1749b24ab676SJeff Bonwick spa_log_class(spa_t *spa) 1750b24ab676SJeff Bonwick { 1751b24ab676SJeff Bonwick return (spa->spa_log_class); 1752b24ab676SJeff Bonwick } 1753b24ab676SJeff Bonwick 1754bc9014e6SJustin Gibbs void 1755bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os) 1756bc9014e6SJustin Gibbs { 1757bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1758bc9014e6SJustin Gibbs list_insert_head(&spa->spa_evicting_os_list, os); 1759bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1760bc9014e6SJustin Gibbs } 1761bc9014e6SJustin Gibbs 1762bc9014e6SJustin Gibbs void 1763bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os) 1764bc9014e6SJustin Gibbs { 1765bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1766bc9014e6SJustin Gibbs list_remove(&spa->spa_evicting_os_list, os); 1767bc9014e6SJustin Gibbs cv_broadcast(&spa->spa_evicting_os_cv); 1768bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1769bc9014e6SJustin Gibbs } 1770bc9014e6SJustin Gibbs 1771bc9014e6SJustin Gibbs void 1772bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa) 1773bc9014e6SJustin Gibbs { 1774bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1775bc9014e6SJustin Gibbs while (!list_is_empty(&spa->spa_evicting_os_list)) 1776bc9014e6SJustin Gibbs cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock); 1777bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1778bc9014e6SJustin Gibbs 1779bc9014e6SJustin Gibbs dmu_buf_user_evict_wait(); 1780bc9014e6SJustin Gibbs } 1781bc9014e6SJustin Gibbs 178244cd46caSbillm int 178344cd46caSbillm spa_max_replication(spa_t *spa) 178444cd46caSbillm { 178544cd46caSbillm /* 1786e7437265Sahrens * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 178744cd46caSbillm * handle BPs with more than one DVA allocated. Set our max 178844cd46caSbillm * replication level accordingly. 1789fa9e4066Sahrens */ 1790e7437265Sahrens if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 179144cd46caSbillm return (1); 179244cd46caSbillm return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1793fa9e4066Sahrens } 1794fa9e4066Sahrens 17953f9d6ad7SLin Ling int 17963f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa) 17973f9d6ad7SLin Ling { 17983f9d6ad7SLin Ling return (spa->spa_prev_software_version); 17993f9d6ad7SLin Ling } 18003f9d6ad7SLin Ling 1801283b8460SGeorge.Wilson uint64_t 1802283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa) 1803283b8460SGeorge.Wilson { 1804283b8460SGeorge.Wilson return (spa->spa_deadman_synctime); 1805283b8460SGeorge.Wilson } 1806283b8460SGeorge.Wilson 180799653d4eSeschrock uint64_t 1808b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 180999653d4eSeschrock { 1810b24ab676SJeff Bonwick uint64_t asize = DVA_GET_ASIZE(dva); 1811b24ab676SJeff Bonwick uint64_t dsize = asize; 181299653d4eSeschrock 1813b24ab676SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 181499653d4eSeschrock 1815b24ab676SJeff Bonwick if (asize != 0 && spa->spa_deflate) { 1816b24ab676SJeff Bonwick vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 1817b24ab676SJeff Bonwick dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 181899653d4eSeschrock } 1819b24ab676SJeff Bonwick 1820b24ab676SJeff Bonwick return (dsize); 1821b24ab676SJeff Bonwick } 1822b24ab676SJeff Bonwick 1823b24ab676SJeff Bonwick uint64_t 1824b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 1825b24ab676SJeff Bonwick { 1826b24ab676SJeff Bonwick uint64_t dsize = 0; 1827b24ab676SJeff Bonwick 18285d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1829b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1830b24ab676SJeff Bonwick 1831b24ab676SJeff Bonwick return (dsize); 1832b24ab676SJeff Bonwick } 1833b24ab676SJeff Bonwick 1834b24ab676SJeff Bonwick uint64_t 1835b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp) 1836b24ab676SJeff Bonwick { 1837b24ab676SJeff Bonwick uint64_t dsize = 0; 1838b24ab676SJeff Bonwick 1839b24ab676SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1840b24ab676SJeff Bonwick 18415d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1842b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1843b24ab676SJeff Bonwick 1844e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 1845b24ab676SJeff Bonwick 1846b24ab676SJeff Bonwick return (dsize); 184799653d4eSeschrock } 184899653d4eSeschrock 1849fa9e4066Sahrens /* 1850fa9e4066Sahrens * ========================================================================== 1851fa9e4066Sahrens * Initialization and Termination 1852fa9e4066Sahrens * ========================================================================== 1853fa9e4066Sahrens */ 1854fa9e4066Sahrens 1855fa9e4066Sahrens static int 1856fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2) 1857fa9e4066Sahrens { 1858fa9e4066Sahrens const spa_t *s1 = a1; 1859fa9e4066Sahrens const spa_t *s2 = a2; 1860fa9e4066Sahrens int s; 1861fa9e4066Sahrens 1862fa9e4066Sahrens s = strcmp(s1->spa_name, s2->spa_name); 1863fa9e4066Sahrens if (s > 0) 1864fa9e4066Sahrens return (1); 1865fa9e4066Sahrens if (s < 0) 1866fa9e4066Sahrens return (-1); 1867fa9e4066Sahrens return (0); 1868fa9e4066Sahrens } 1869fa9e4066Sahrens 18700373e76bSbonwick int 18710373e76bSbonwick spa_busy(void) 18720373e76bSbonwick { 18730373e76bSbonwick return (spa_active_count); 18740373e76bSbonwick } 18750373e76bSbonwick 1876e7cbe64fSgw void 1877e7cbe64fSgw spa_boot_init() 1878e7cbe64fSgw { 1879e7cbe64fSgw spa_config_load(); 1880e7cbe64fSgw } 1881e7cbe64fSgw 1882fa9e4066Sahrens void 1883fa9e4066Sahrens spa_init(int mode) 1884fa9e4066Sahrens { 1885fa9e4066Sahrens mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 1886c25056deSgw mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 1887fa94a07fSbrendan mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 1888fa9e4066Sahrens cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 1889fa9e4066Sahrens 1890fa9e4066Sahrens avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 1891fa9e4066Sahrens offsetof(spa_t, spa_avl)); 1892fa9e4066Sahrens 1893fa94a07fSbrendan avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 1894fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 1895fa94a07fSbrendan 1896fa94a07fSbrendan avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 1897fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 189899653d4eSeschrock 18998ad4d6ddSJeff Bonwick spa_mode_global = mode; 1900fa9e4066Sahrens 1901283b8460SGeorge.Wilson #ifdef _KERNEL 1902283b8460SGeorge.Wilson spa_arch_init(); 1903283b8460SGeorge.Wilson #else 1904cd1c8b85SMatthew Ahrens if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 1905cd1c8b85SMatthew Ahrens arc_procfd = open("/proc/self/ctl", O_WRONLY); 1906cd1c8b85SMatthew Ahrens if (arc_procfd == -1) { 1907cd1c8b85SMatthew Ahrens perror("could not enable watchpoints: " 1908cd1c8b85SMatthew Ahrens "opening /proc/self/ctl failed: "); 1909cd1c8b85SMatthew Ahrens } else { 1910cd1c8b85SMatthew Ahrens arc_watch = B_TRUE; 1911cd1c8b85SMatthew Ahrens } 1912cd1c8b85SMatthew Ahrens } 1913cd1c8b85SMatthew Ahrens #endif 1914cd1c8b85SMatthew Ahrens 1915fa9e4066Sahrens refcount_init(); 1916fa9e4066Sahrens unique_init(); 19170713e232SGeorge Wilson range_tree_init(); 19188363e80aSGeorge Wilson metaslab_alloc_trace_init(); 1919fa9e4066Sahrens zio_init(); 1920fa9e4066Sahrens dmu_init(); 1921fa9e4066Sahrens zil_init(); 192287db74c1Sek vdev_cache_stat_init(); 192391ebeef5Sahrens zfs_prop_init(); 1924990b4856Slling zpool_prop_init(); 1925ad135b5dSChristopher Siden zpool_feature_init(); 1926fa9e4066Sahrens spa_config_load(); 1927e14bb325SJeff Bonwick l2arc_start(); 1928fa9e4066Sahrens } 1929fa9e4066Sahrens 1930fa9e4066Sahrens void 1931fa9e4066Sahrens spa_fini(void) 1932fa9e4066Sahrens { 1933e14bb325SJeff Bonwick l2arc_stop(); 1934e14bb325SJeff Bonwick 1935fa9e4066Sahrens spa_evict_all(); 1936fa9e4066Sahrens 193787db74c1Sek vdev_cache_stat_fini(); 1938fa9e4066Sahrens zil_fini(); 1939fa9e4066Sahrens dmu_fini(); 1940fa9e4066Sahrens zio_fini(); 19418363e80aSGeorge Wilson metaslab_alloc_trace_fini(); 19420713e232SGeorge Wilson range_tree_fini(); 194391ebeef5Sahrens unique_fini(); 1944fa9e4066Sahrens refcount_fini(); 1945fa9e4066Sahrens 1946fa9e4066Sahrens avl_destroy(&spa_namespace_avl); 194799653d4eSeschrock avl_destroy(&spa_spare_avl); 1948fa94a07fSbrendan avl_destroy(&spa_l2cache_avl); 1949fa9e4066Sahrens 1950fa9e4066Sahrens cv_destroy(&spa_namespace_cv); 1951fa9e4066Sahrens mutex_destroy(&spa_namespace_lock); 1952c25056deSgw mutex_destroy(&spa_spare_lock); 1953fa94a07fSbrendan mutex_destroy(&spa_l2cache_lock); 1954fa9e4066Sahrens } 19556ce0521aSperrin 19566ce0521aSperrin /* 19576ce0521aSperrin * Return whether this pool has slogs. No locking needed. 19586ce0521aSperrin * It's not a problem if the wrong answer is returned as it's only for 19596ce0521aSperrin * performance and not correctness 19606ce0521aSperrin */ 19616ce0521aSperrin boolean_t 19626ce0521aSperrin spa_has_slogs(spa_t *spa) 19636ce0521aSperrin { 19646ce0521aSperrin return (spa->spa_log_class->mc_rotor != NULL); 19656ce0521aSperrin } 1966bf82a41bSeschrock 1967b24ab676SJeff Bonwick spa_log_state_t 1968b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa) 1969b24ab676SJeff Bonwick { 1970b24ab676SJeff Bonwick return (spa->spa_log_state); 1971b24ab676SJeff Bonwick } 1972b24ab676SJeff Bonwick 1973b24ab676SJeff Bonwick void 1974b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state) 1975b24ab676SJeff Bonwick { 1976b24ab676SJeff Bonwick spa->spa_log_state = state; 1977b24ab676SJeff Bonwick } 1978b24ab676SJeff Bonwick 1979bf82a41bSeschrock boolean_t 1980bf82a41bSeschrock spa_is_root(spa_t *spa) 1981bf82a41bSeschrock { 1982bf82a41bSeschrock return (spa->spa_is_root); 1983bf82a41bSeschrock } 19848ad4d6ddSJeff Bonwick 19858ad4d6ddSJeff Bonwick boolean_t 19868ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa) 19878ad4d6ddSJeff Bonwick { 19888ad4d6ddSJeff Bonwick return (!!(spa->spa_mode & FWRITE)); 19898ad4d6ddSJeff Bonwick } 19908ad4d6ddSJeff Bonwick 199173527f44SAlex Reece /* 199273527f44SAlex Reece * Returns true if there is a pending sync task in any of the current 199373527f44SAlex Reece * syncing txg, the current quiescing txg, or the current open txg. 199473527f44SAlex Reece */ 199573527f44SAlex Reece boolean_t 199673527f44SAlex Reece spa_has_pending_synctask(spa_t *spa) 199773527f44SAlex Reece { 199873527f44SAlex Reece return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks)); 199973527f44SAlex Reece } 200073527f44SAlex Reece 20018ad4d6ddSJeff Bonwick int 20028ad4d6ddSJeff Bonwick spa_mode(spa_t *spa) 20038ad4d6ddSJeff Bonwick { 20048ad4d6ddSJeff Bonwick return (spa->spa_mode); 20058ad4d6ddSJeff Bonwick } 2006b24ab676SJeff Bonwick 2007b24ab676SJeff Bonwick uint64_t 2008b24ab676SJeff Bonwick spa_bootfs(spa_t *spa) 2009b24ab676SJeff Bonwick { 2010b24ab676SJeff Bonwick return (spa->spa_bootfs); 2011b24ab676SJeff Bonwick } 2012b24ab676SJeff Bonwick 2013b24ab676SJeff Bonwick uint64_t 2014b24ab676SJeff Bonwick spa_delegation(spa_t *spa) 2015b24ab676SJeff Bonwick { 2016b24ab676SJeff Bonwick return (spa->spa_delegation); 2017b24ab676SJeff Bonwick } 2018b24ab676SJeff Bonwick 2019b24ab676SJeff Bonwick objset_t * 2020b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa) 2021b24ab676SJeff Bonwick { 2022b24ab676SJeff Bonwick return (spa->spa_meta_objset); 2023b24ab676SJeff Bonwick } 2024b24ab676SJeff Bonwick 2025b24ab676SJeff Bonwick enum zio_checksum 2026b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa) 2027b24ab676SJeff Bonwick { 2028b24ab676SJeff Bonwick return (spa->spa_dedup_checksum); 2029b24ab676SJeff Bonwick } 20303f9d6ad7SLin Ling 20313f9d6ad7SLin Ling /* 20323f9d6ad7SLin Ling * Reset pool scan stat per scan pass (or reboot). 20333f9d6ad7SLin Ling */ 20343f9d6ad7SLin Ling void 20353f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa) 20363f9d6ad7SLin Ling { 20373f9d6ad7SLin Ling /* data not stored on disk */ 20383f9d6ad7SLin Ling spa->spa_scan_pass_start = gethrestime_sec(); 20391702cce7SAlek Pinchuk if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan)) 20401702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start; 20411702cce7SAlek Pinchuk else 20421702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = 0; 20431702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_spent_paused = 0; 20443f9d6ad7SLin Ling spa->spa_scan_pass_exam = 0; 20453f9d6ad7SLin Ling vdev_scan_stat_init(spa->spa_root_vdev); 20463f9d6ad7SLin Ling } 20473f9d6ad7SLin Ling 20483f9d6ad7SLin Ling /* 20493f9d6ad7SLin Ling * Get scan stats for zpool status reports 20503f9d6ad7SLin Ling */ 20513f9d6ad7SLin Ling int 20523f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 20533f9d6ad7SLin Ling { 20543f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 20553f9d6ad7SLin Ling 20563f9d6ad7SLin Ling if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 2057be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 20583f9d6ad7SLin Ling bzero(ps, sizeof (pool_scan_stat_t)); 20593f9d6ad7SLin Ling 20603f9d6ad7SLin Ling /* data stored on disk */ 20613f9d6ad7SLin Ling ps->pss_func = scn->scn_phys.scn_func; 20623f9d6ad7SLin Ling ps->pss_start_time = scn->scn_phys.scn_start_time; 20633f9d6ad7SLin Ling ps->pss_end_time = scn->scn_phys.scn_end_time; 20643f9d6ad7SLin Ling ps->pss_to_examine = scn->scn_phys.scn_to_examine; 20653f9d6ad7SLin Ling ps->pss_examined = scn->scn_phys.scn_examined; 20663f9d6ad7SLin Ling ps->pss_to_process = scn->scn_phys.scn_to_process; 20673f9d6ad7SLin Ling ps->pss_processed = scn->scn_phys.scn_processed; 20683f9d6ad7SLin Ling ps->pss_errors = scn->scn_phys.scn_errors; 20693f9d6ad7SLin Ling ps->pss_state = scn->scn_phys.scn_state; 20703f9d6ad7SLin Ling 20713f9d6ad7SLin Ling /* data not stored on disk */ 20723f9d6ad7SLin Ling ps->pss_pass_start = spa->spa_scan_pass_start; 20733f9d6ad7SLin Ling ps->pss_pass_exam = spa->spa_scan_pass_exam; 20741702cce7SAlek Pinchuk ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause; 20751702cce7SAlek Pinchuk ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused; 20763f9d6ad7SLin Ling 20773f9d6ad7SLin Ling return (0); 20783f9d6ad7SLin Ling } 207909c9d376SGeorge Wilson 208009c9d376SGeorge Wilson boolean_t 208109c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa) 208209c9d376SGeorge Wilson { 208309c9d376SGeorge Wilson return (spa->spa_debug); 208409c9d376SGeorge Wilson } 2085b5152584SMatthew Ahrens 2086b5152584SMatthew Ahrens int 2087b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa) 2088b5152584SMatthew Ahrens { 2089b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) 2090b5152584SMatthew Ahrens return (SPA_MAXBLOCKSIZE); 2091b5152584SMatthew Ahrens else 2092b5152584SMatthew Ahrens return (SPA_OLD_MAXBLOCKSIZE); 2093b5152584SMatthew Ahrens } 2094*5cabbc6bSPrashanth Sreenivasa 2095*5cabbc6bSPrashanth Sreenivasa /* 2096*5cabbc6bSPrashanth Sreenivasa * Returns the txg that the last device removal completed. No indirect mappings 2097*5cabbc6bSPrashanth Sreenivasa * have been added since this txg. 2098*5cabbc6bSPrashanth Sreenivasa */ 2099*5cabbc6bSPrashanth Sreenivasa uint64_t 2100*5cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa) 2101*5cabbc6bSPrashanth Sreenivasa { 2102*5cabbc6bSPrashanth Sreenivasa uint64_t vdevid; 2103*5cabbc6bSPrashanth Sreenivasa uint64_t ret = -1ULL; 2104*5cabbc6bSPrashanth Sreenivasa 2105*5cabbc6bSPrashanth Sreenivasa spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2106*5cabbc6bSPrashanth Sreenivasa /* 2107*5cabbc6bSPrashanth Sreenivasa * sr_prev_indirect_vdev is only modified while holding all the 2108*5cabbc6bSPrashanth Sreenivasa * config locks, so it is sufficient to hold SCL_VDEV as reader when 2109*5cabbc6bSPrashanth Sreenivasa * examining it. 2110*5cabbc6bSPrashanth Sreenivasa */ 2111*5cabbc6bSPrashanth Sreenivasa vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev; 2112*5cabbc6bSPrashanth Sreenivasa 2113*5cabbc6bSPrashanth Sreenivasa while (vdevid != -1ULL) { 2114*5cabbc6bSPrashanth Sreenivasa vdev_t *vd = vdev_lookup_top(spa, vdevid); 2115*5cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births; 2116*5cabbc6bSPrashanth Sreenivasa 2117*5cabbc6bSPrashanth Sreenivasa ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 2118*5cabbc6bSPrashanth Sreenivasa 2119*5cabbc6bSPrashanth Sreenivasa /* 2120*5cabbc6bSPrashanth Sreenivasa * If the removal did not remap any data, we don't care. 2121*5cabbc6bSPrashanth Sreenivasa */ 2122*5cabbc6bSPrashanth Sreenivasa if (vdev_indirect_births_count(vib) != 0) { 2123*5cabbc6bSPrashanth Sreenivasa ret = vdev_indirect_births_last_entry_txg(vib); 2124*5cabbc6bSPrashanth Sreenivasa break; 2125*5cabbc6bSPrashanth Sreenivasa } 2126*5cabbc6bSPrashanth Sreenivasa 2127*5cabbc6bSPrashanth Sreenivasa vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev; 2128*5cabbc6bSPrashanth Sreenivasa } 2129*5cabbc6bSPrashanth Sreenivasa spa_config_exit(spa, SCL_VDEV, FTAG); 2130*5cabbc6bSPrashanth Sreenivasa 2131*5cabbc6bSPrashanth Sreenivasa IMPLY(ret != -1ULL, 2132*5cabbc6bSPrashanth Sreenivasa spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 2133*5cabbc6bSPrashanth Sreenivasa 2134*5cabbc6bSPrashanth Sreenivasa return (ret); 2135*5cabbc6bSPrashanth Sreenivasa } 2136