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. 23f78cdc34SPaul Dagnelie * Copyright (c) 2011, 2018 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. 29663207adSDon Brady * Copyright (c) 2017, Intel Corporation. 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens #include <sys/zfs_context.h> 33fa9e4066Sahrens #include <sys/spa_impl.h> 34283b8460SGeorge.Wilson #include <sys/spa_boot.h> 35fa9e4066Sahrens #include <sys/zio.h> 36fa9e4066Sahrens #include <sys/zio_checksum.h> 37fa9e4066Sahrens #include <sys/zio_compress.h> 38fa9e4066Sahrens #include <sys/dmu.h> 39fa9e4066Sahrens #include <sys/dmu_tx.h> 40fa9e4066Sahrens #include <sys/zap.h> 41fa9e4066Sahrens #include <sys/zil.h> 42fa9e4066Sahrens #include <sys/vdev_impl.h> 43094e47e9SGeorge Wilson #include <sys/vdev_initialize.h> 44*084fd14fSBrian Behlendorf #include <sys/vdev_trim.h> 45fa9e4066Sahrens #include <sys/metaslab.h> 46fa9e4066Sahrens #include <sys/uberblock_impl.h> 47fa9e4066Sahrens #include <sys/txg.h> 48fa9e4066Sahrens #include <sys/avl.h> 49fa9e4066Sahrens #include <sys/unique.h> 50fa9e4066Sahrens #include <sys/dsl_pool.h> 51fa9e4066Sahrens #include <sys/dsl_dir.h> 52fa9e4066Sahrens #include <sys/dsl_prop.h> 533f9d6ad7SLin Ling #include <sys/dsl_scan.h> 54fa9e4066Sahrens #include <sys/fs/zfs.h> 556ce0521aSperrin #include <sys/metaslab_impl.h> 56e14bb325SJeff Bonwick #include <sys/arc.h> 57485bbbf5SGeorge Wilson #include <sys/ddt.h> 5891ebeef5Sahrens #include "zfs_prop.h" 5945818ee1SMatthew Ahrens #include <sys/zfeature.h> 60fa9e4066Sahrens 61fa9e4066Sahrens /* 62fa9e4066Sahrens * SPA locking 63fa9e4066Sahrens * 64fa9e4066Sahrens * There are four basic locks for managing spa_t structures: 65fa9e4066Sahrens * 66fa9e4066Sahrens * spa_namespace_lock (global mutex) 67fa9e4066Sahrens * 6844cd46caSbillm * This lock must be acquired to do any of the following: 69fa9e4066Sahrens * 7044cd46caSbillm * - Lookup a spa_t by name 7144cd46caSbillm * - Add or remove a spa_t from the namespace 7244cd46caSbillm * - Increase spa_refcount from non-zero 7344cd46caSbillm * - Check if spa_refcount is zero 7444cd46caSbillm * - Rename a spa_t 75ea8dc4b6Seschrock * - add/remove/attach/detach devices 7644cd46caSbillm * - Held for the duration of create/destroy/import/export 77fa9e4066Sahrens * 7844cd46caSbillm * It does not need to handle recursion. A create or destroy may 7944cd46caSbillm * reference objects (files or zvols) in other pools, but by 8044cd46caSbillm * definition they must have an existing reference, and will never need 8144cd46caSbillm * to lookup a spa_t by name. 82fa9e4066Sahrens * 83e914ace2STim Schumacher * spa_refcount (per-spa zfs_refcount_t protected by mutex) 84fa9e4066Sahrens * 8544cd46caSbillm * This reference count keep track of any active users of the spa_t. The 8644cd46caSbillm * spa_t cannot be destroyed or freed while this is non-zero. Internally, 8744cd46caSbillm * the refcount is never really 'zero' - opening a pool implicitly keeps 88088f3894Sahrens * some references in the DMU. Internally we check against spa_minref, but 8944cd46caSbillm * present the image of a zero/non-zero value to consumers. 90fa9e4066Sahrens * 91e14bb325SJeff Bonwick * spa_config_lock[] (per-spa array of rwlocks) 92fa9e4066Sahrens * 9391ebeef5Sahrens * This protects the spa_t from config changes, and must be held in 9491ebeef5Sahrens * the following circumstances: 95fa9e4066Sahrens * 9644cd46caSbillm * - RW_READER to perform I/O to the spa 9744cd46caSbillm * - RW_WRITER to change the vdev config 98fa9e4066Sahrens * 99fa9e4066Sahrens * The locking order is fairly straightforward: 100fa9e4066Sahrens * 10144cd46caSbillm * spa_namespace_lock -> spa_refcount 102fa9e4066Sahrens * 10344cd46caSbillm * The namespace lock must be acquired to increase the refcount from 0 10444cd46caSbillm * or to check if it is zero. 105fa9e4066Sahrens * 106e14bb325SJeff Bonwick * spa_refcount -> spa_config_lock[] 107fa9e4066Sahrens * 10844cd46caSbillm * There must be at least one valid reference on the spa_t to acquire 10944cd46caSbillm * the config lock. 110fa9e4066Sahrens * 111e14bb325SJeff Bonwick * spa_namespace_lock -> spa_config_lock[] 112fa9e4066Sahrens * 11344cd46caSbillm * The namespace lock must always be taken before the config lock. 114fa9e4066Sahrens * 115fa9e4066Sahrens * 116e14bb325SJeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible. 117fa9e4066Sahrens * 118e14bb325SJeff Bonwick * The namespace is manipulated using the following functions, all of which 119e14bb325SJeff Bonwick * require the spa_namespace_lock to be held. 120fa9e4066Sahrens * 12144cd46caSbillm * spa_lookup() Lookup a spa_t by name. 122fa9e4066Sahrens * 12344cd46caSbillm * spa_add() Create a new spa_t in the namespace. 124fa9e4066Sahrens * 12544cd46caSbillm * spa_remove() Remove a spa_t from the namespace. This also 12644cd46caSbillm * frees up any memory associated with the spa_t. 127fa9e4066Sahrens * 12844cd46caSbillm * spa_next() Returns the next spa_t in the system, or the 12944cd46caSbillm * first if NULL is passed. 130fa9e4066Sahrens * 13144cd46caSbillm * spa_evict_all() Shutdown and remove all spa_t structures in 13244cd46caSbillm * the system. 133fa9e4066Sahrens * 134ea8dc4b6Seschrock * spa_guid_exists() Determine whether a pool/device guid exists. 135fa9e4066Sahrens * 136fa9e4066Sahrens * The spa_refcount is manipulated using the following functions: 137fa9e4066Sahrens * 13844cd46caSbillm * spa_open_ref() Adds a reference to the given spa_t. Must be 13944cd46caSbillm * called with spa_namespace_lock held if the 14044cd46caSbillm * refcount is currently zero. 141fa9e4066Sahrens * 14244cd46caSbillm * spa_close() Remove a reference from the spa_t. This will 14344cd46caSbillm * not free the spa_t or remove it from the 14444cd46caSbillm * namespace. No locking is required. 145fa9e4066Sahrens * 14644cd46caSbillm * spa_refcount_zero() Returns true if the refcount is currently 14744cd46caSbillm * zero. Must be called with spa_namespace_lock 14844cd46caSbillm * held. 149fa9e4066Sahrens * 150e14bb325SJeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows: 151e14bb325SJeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 152e14bb325SJeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 153e14bb325SJeff Bonwick * 154e14bb325SJeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader. 155e14bb325SJeff Bonwick * To modify the configuration, you must hold all locks as writer. To modify 156e14bb325SJeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline), 157e14bb325SJeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer. 158e14bb325SJeff Bonwick * 159e14bb325SJeff Bonwick * We use these distinct config locks to avoid recursive lock entry. 160e14bb325SJeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 161e14bb325SJeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps 162e14bb325SJeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 163e14bb325SJeff Bonwick * 164e14bb325SJeff Bonwick * The spa config locks cannot be normal rwlocks because we need the 165e14bb325SJeff Bonwick * ability to hand off ownership. For example, SCL_ZIO is acquired 166e14bb325SJeff Bonwick * by the issuing thread and later released by an interrupt thread. 167e14bb325SJeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent 168e14bb325SJeff Bonwick * writer (i.e. system administrator) starvation. 169e14bb325SJeff Bonwick * 170e14bb325SJeff Bonwick * The lock acquisition rules are as follows: 171e14bb325SJeff Bonwick * 172e14bb325SJeff Bonwick * SCL_CONFIG 173e14bb325SJeff Bonwick * Protects changes to the vdev tree topology, such as vdev 174e14bb325SJeff Bonwick * add/remove/attach/detach. Protects the dirty config list 175e14bb325SJeff Bonwick * (spa_config_dirty_list) and the set of spares and l2arc devices. 176e14bb325SJeff Bonwick * 177e14bb325SJeff Bonwick * SCL_STATE 178e14bb325SJeff Bonwick * Protects changes to pool state and vdev state, such as vdev 179e14bb325SJeff Bonwick * online/offline/fault/degrade/clear. Protects the dirty state list 180e14bb325SJeff Bonwick * (spa_state_dirty_list) and global pool state (spa_state). 181e14bb325SJeff Bonwick * 182e14bb325SJeff Bonwick * SCL_ALLOC 183e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 184e14bb325SJeff Bonwick * Held as reader by metaslab_alloc() and metaslab_claim(). 185e14bb325SJeff Bonwick * 186e14bb325SJeff Bonwick * SCL_ZIO 187e14bb325SJeff Bonwick * Held by bp-level zios (those which have no io_vd upon entry) 188e14bb325SJeff Bonwick * to prevent changes to the vdev tree. The bp-level zio implicitly 189e14bb325SJeff Bonwick * protects all of its vdev child zios, which do not hold SCL_ZIO. 190e14bb325SJeff Bonwick * 191e14bb325SJeff Bonwick * SCL_FREE 192e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 193e14bb325SJeff Bonwick * Held as reader by metaslab_free(). SCL_FREE is distinct from 194e14bb325SJeff Bonwick * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 195e14bb325SJeff Bonwick * blocks in zio_done() while another i/o that holds either 196e14bb325SJeff Bonwick * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 197e14bb325SJeff Bonwick * 198e14bb325SJeff Bonwick * SCL_VDEV 199e14bb325SJeff Bonwick * Held as reader to prevent changes to the vdev tree during trivial 200b24ab676SJeff Bonwick * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 201e14bb325SJeff Bonwick * other locks, and lower than all of them, to ensure that it's safe 202e14bb325SJeff Bonwick * to acquire regardless of caller context. 203e14bb325SJeff Bonwick * 204e14bb325SJeff Bonwick * In addition, the following rules apply: 205e14bb325SJeff Bonwick * 206e14bb325SJeff Bonwick * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 207e14bb325SJeff Bonwick * The lock ordering is SCL_CONFIG > spa_props_lock. 208e14bb325SJeff Bonwick * 209e14bb325SJeff Bonwick * (b) I/O operations on leaf vdevs. For any zio operation that takes 210e14bb325SJeff Bonwick * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 211e14bb325SJeff Bonwick * or zio_write_phys() -- the caller must ensure that the config cannot 212e14bb325SJeff Bonwick * cannot change in the interim, and that the vdev cannot be reopened. 213e14bb325SJeff Bonwick * SCL_STATE as reader suffices for both. 214fa9e4066Sahrens * 215ea8dc4b6Seschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 216fa9e4066Sahrens * 21744cd46caSbillm * spa_vdev_enter() Acquire the namespace lock and the config lock 218ea8dc4b6Seschrock * for writing. 219fa9e4066Sahrens * 22044cd46caSbillm * spa_vdev_exit() Release the config lock, wait for all I/O 22144cd46caSbillm * to complete, sync the updated configs to the 222ea8dc4b6Seschrock * cache, and release the namespace lock. 223fa9e4066Sahrens * 224e14bb325SJeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 225e14bb325SJeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 226e14bb325SJeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 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 2445cabbc6bSPrashanth Sreenivasa /* 2455cabbc6bSPrashanth Sreenivasa * Everything except dprintf, spa, and indirect_remap is on by default 2465cabbc6bSPrashanth Sreenivasa * in debug builds. 2475cabbc6bSPrashanth Sreenivasa */ 24821f7c81cSMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | 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 * 34186714001SSerapheim Dimitropoulos * Operations that are almost guaranteed to free up space in the absence of 34286714001SSerapheim Dimitropoulos * a pool checkpoint can use up to three quarters of the slop space 34386714001SSerapheim Dimitropoulos * (e.g zfs destroy). 34486714001SSerapheim Dimitropoulos * 3457d46dc6cSMatthew Ahrens * A very restricted set of operations are always permitted, regardless of 3467d46dc6cSMatthew Ahrens * the amount of free space. These are the operations that call 34786714001SSerapheim Dimitropoulos * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net 34886714001SSerapheim Dimitropoulos * increase in the amount of space used, it is possible to run the pool 34986714001SSerapheim Dimitropoulos * completely out of space, causing it to be permanently read-only. 3507d46dc6cSMatthew Ahrens * 3514b5c8e93SMatthew Ahrens * Note that on very small pools, the slop space will be larger than 3524b5c8e93SMatthew Ahrens * 3.2%, in an effort to have it be at least spa_min_slop (128MB), 3534b5c8e93SMatthew Ahrens * but we never allow it to be more than half the pool size. 3544b5c8e93SMatthew Ahrens * 3557d46dc6cSMatthew Ahrens * See also the comments in zfs_space_check_t. 3567d46dc6cSMatthew Ahrens */ 3577d46dc6cSMatthew Ahrens int spa_slop_shift = 5; 3584b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024; 3597d46dc6cSMatthew Ahrens 360f78cdc34SPaul Dagnelie int spa_allocators = 4; 361f78cdc34SPaul Dagnelie 3623ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 3633ee8c80cSPavel Zakharov void 3643ee8c80cSPavel Zakharov spa_load_failed(spa_t *spa, const char *fmt, ...) 3653ee8c80cSPavel Zakharov { 3663ee8c80cSPavel Zakharov va_list adx; 3673ee8c80cSPavel Zakharov char buf[256]; 3683ee8c80cSPavel Zakharov 3693ee8c80cSPavel Zakharov va_start(adx, fmt); 3703ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 3713ee8c80cSPavel Zakharov va_end(adx); 3723ee8c80cSPavel Zakharov 3736f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name, 3746f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf); 3753ee8c80cSPavel Zakharov } 3763ee8c80cSPavel Zakharov 3773ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 3783ee8c80cSPavel Zakharov void 3793ee8c80cSPavel Zakharov spa_load_note(spa_t *spa, const char *fmt, ...) 3803ee8c80cSPavel Zakharov { 3813ee8c80cSPavel Zakharov va_list adx; 3823ee8c80cSPavel Zakharov char buf[256]; 3833ee8c80cSPavel Zakharov 3843ee8c80cSPavel Zakharov va_start(adx, fmt); 3853ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 3863ee8c80cSPavel Zakharov va_end(adx); 3873ee8c80cSPavel Zakharov 3886f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name, 3896f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf); 3903ee8c80cSPavel Zakharov } 3913ee8c80cSPavel Zakharov 392663207adSDon Brady /* 393663207adSDon Brady * By default dedup and user data indirects land in the special class 394663207adSDon Brady */ 395663207adSDon Brady int zfs_ddt_data_is_special = B_TRUE; 396663207adSDon Brady int zfs_user_indirect_is_special = B_TRUE; 397663207adSDon Brady 398663207adSDon Brady /* 399663207adSDon Brady * The percentage of special class final space reserved for metadata only. 400663207adSDon Brady * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only 401663207adSDon Brady * let metadata into the class. 402663207adSDon Brady */ 403663207adSDon Brady int zfs_special_class_metadata_reserve_pct = 25; 404663207adSDon Brady 405e05725b1Sbonwick /* 406e05725b1Sbonwick * ========================================================================== 407e05725b1Sbonwick * SPA config locking 408e05725b1Sbonwick * ========================================================================== 409e05725b1Sbonwick */ 410e05725b1Sbonwick static void 411e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa) 412e14bb325SJeff Bonwick { 413e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 414e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 415e14bb325SJeff Bonwick mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 416e14bb325SJeff Bonwick cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 417e914ace2STim Schumacher zfs_refcount_create_untracked(&scl->scl_count); 418e14bb325SJeff Bonwick scl->scl_writer = NULL; 419e14bb325SJeff Bonwick scl->scl_write_wanted = 0; 420e14bb325SJeff Bonwick } 421e05725b1Sbonwick } 422e05725b1Sbonwick 423e05725b1Sbonwick static void 424e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa) 425e14bb325SJeff Bonwick { 426e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 427e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 428e14bb325SJeff Bonwick mutex_destroy(&scl->scl_lock); 429e14bb325SJeff Bonwick cv_destroy(&scl->scl_cv); 430e914ace2STim Schumacher zfs_refcount_destroy(&scl->scl_count); 431e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL); 432e14bb325SJeff Bonwick ASSERT(scl->scl_write_wanted == 0); 433e14bb325SJeff Bonwick } 434e14bb325SJeff Bonwick } 435e14bb325SJeff Bonwick 436e14bb325SJeff Bonwick int 437e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 438e05725b1Sbonwick { 439e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 440e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 441e14bb325SJeff Bonwick if (!(locks & (1 << i))) 442e14bb325SJeff Bonwick continue; 443e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 444e14bb325SJeff Bonwick if (rw == RW_READER) { 445e14bb325SJeff Bonwick if (scl->scl_writer || scl->scl_write_wanted) { 446e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 447e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 448e495b6e6SSaso Kiselkov tag); 449e14bb325SJeff Bonwick return (0); 450e14bb325SJeff Bonwick } 451e14bb325SJeff Bonwick } else { 452e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 453e914ace2STim Schumacher if (!zfs_refcount_is_zero(&scl->scl_count)) { 454e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 455e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 456e495b6e6SSaso Kiselkov tag); 457e14bb325SJeff Bonwick return (0); 458e14bb325SJeff Bonwick } 459e14bb325SJeff Bonwick scl->scl_writer = curthread; 460e14bb325SJeff Bonwick } 461e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag); 462e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 463e14bb325SJeff Bonwick } 464e14bb325SJeff Bonwick return (1); 465e05725b1Sbonwick } 466e05725b1Sbonwick 467e05725b1Sbonwick void 468e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 469e05725b1Sbonwick { 470f64c0e34SEric Taylor int wlocks_held = 0; 471f64c0e34SEric Taylor 4723b2aab18SMatthew Ahrens ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 4733b2aab18SMatthew Ahrens 474e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 475e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 476f64c0e34SEric Taylor if (scl->scl_writer == curthread) 477f64c0e34SEric Taylor wlocks_held |= (1 << i); 478e14bb325SJeff Bonwick if (!(locks & (1 << i))) 479e14bb325SJeff Bonwick continue; 480e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 481e14bb325SJeff Bonwick if (rw == RW_READER) { 482e14bb325SJeff Bonwick while (scl->scl_writer || scl->scl_write_wanted) { 483e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 484e14bb325SJeff Bonwick } 485e14bb325SJeff Bonwick } else { 486e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 487e914ace2STim Schumacher while (!zfs_refcount_is_zero(&scl->scl_count)) { 488e14bb325SJeff Bonwick scl->scl_write_wanted++; 489e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 490e14bb325SJeff Bonwick scl->scl_write_wanted--; 491e14bb325SJeff Bonwick } 492e14bb325SJeff Bonwick scl->scl_writer = curthread; 493e14bb325SJeff Bonwick } 494e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag); 495e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 496e05725b1Sbonwick } 4975cabbc6bSPrashanth Sreenivasa ASSERT3U(wlocks_held, <=, locks); 498e05725b1Sbonwick } 499e05725b1Sbonwick 500e05725b1Sbonwick void 501e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag) 502e05725b1Sbonwick { 503e14bb325SJeff Bonwick for (int i = SCL_LOCKS - 1; i >= 0; i--) { 504e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 505e14bb325SJeff Bonwick if (!(locks & (1 << i))) 506e14bb325SJeff Bonwick continue; 507e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 508e914ace2STim Schumacher ASSERT(!zfs_refcount_is_zero(&scl->scl_count)); 509e914ace2STim Schumacher if (zfs_refcount_remove(&scl->scl_count, tag) == 0) { 510e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL || 511e14bb325SJeff Bonwick scl->scl_writer == curthread); 512e14bb325SJeff Bonwick scl->scl_writer = NULL; /* OK in either case */ 513e14bb325SJeff Bonwick cv_broadcast(&scl->scl_cv); 514e14bb325SJeff Bonwick } 515e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 516e05725b1Sbonwick } 517e05725b1Sbonwick } 518e05725b1Sbonwick 519e14bb325SJeff Bonwick int 520e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw) 521e05725b1Sbonwick { 522e14bb325SJeff Bonwick int locks_held = 0; 523e05725b1Sbonwick 524e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 525e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 526e14bb325SJeff Bonwick if (!(locks & (1 << i))) 527e14bb325SJeff Bonwick continue; 528e914ace2STim Schumacher if ((rw == RW_READER && 529e914ace2STim Schumacher !zfs_refcount_is_zero(&scl->scl_count)) || 530e14bb325SJeff Bonwick (rw == RW_WRITER && scl->scl_writer == curthread)) 531e14bb325SJeff Bonwick locks_held |= 1 << i; 532e14bb325SJeff Bonwick } 533e14bb325SJeff Bonwick 534e14bb325SJeff Bonwick return (locks_held); 535e05725b1Sbonwick } 536e05725b1Sbonwick 537fa9e4066Sahrens /* 538fa9e4066Sahrens * ========================================================================== 539fa9e4066Sahrens * SPA namespace functions 540fa9e4066Sahrens * ========================================================================== 541fa9e4066Sahrens */ 542fa9e4066Sahrens 543fa9e4066Sahrens /* 544fa9e4066Sahrens * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 545fa9e4066Sahrens * Returns NULL if no matching spa_t is found. 546fa9e4066Sahrens */ 547fa9e4066Sahrens spa_t * 548fa9e4066Sahrens spa_lookup(const char *name) 549fa9e4066Sahrens { 550e14bb325SJeff Bonwick static spa_t search; /* spa_t is large; don't allocate on stack */ 551e14bb325SJeff Bonwick spa_t *spa; 552fa9e4066Sahrens avl_index_t where; 55340feaa91Sahrens char *cp; 554fa9e4066Sahrens 555fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 556fa9e4066Sahrens 5573b2aab18SMatthew Ahrens (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 5583b2aab18SMatthew Ahrens 55940feaa91Sahrens /* 56040feaa91Sahrens * If it's a full dataset name, figure out the pool name and 56140feaa91Sahrens * just use that. 56240feaa91Sahrens */ 56378f17100SMatthew Ahrens cp = strpbrk(search.spa_name, "/@#"); 5643b2aab18SMatthew Ahrens if (cp != NULL) 56540feaa91Sahrens *cp = '\0'; 56640feaa91Sahrens 567fa9e4066Sahrens spa = avl_find(&spa_namespace_avl, &search, &where); 568fa9e4066Sahrens 569fa9e4066Sahrens return (spa); 570fa9e4066Sahrens } 571fa9e4066Sahrens 572283b8460SGeorge.Wilson /* 573283b8460SGeorge.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 574283b8460SGeorge.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 575283b8460SGeorge.Wilson * looking for potentially hung I/Os. 576283b8460SGeorge.Wilson */ 577283b8460SGeorge.Wilson void 578283b8460SGeorge.Wilson spa_deadman(void *arg) 579283b8460SGeorge.Wilson { 580283b8460SGeorge.Wilson spa_t *spa = arg; 581283b8460SGeorge.Wilson 5820713e232SGeorge Wilson /* 5830713e232SGeorge Wilson * Disable the deadman timer if the pool is suspended. 5840713e232SGeorge Wilson */ 5850713e232SGeorge Wilson if (spa_suspended(spa)) { 5860713e232SGeorge Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 5870713e232SGeorge Wilson return; 5880713e232SGeorge Wilson } 5890713e232SGeorge Wilson 590283b8460SGeorge.Wilson zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 591283b8460SGeorge.Wilson (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 592283b8460SGeorge.Wilson ++spa->spa_deadman_calls); 593283b8460SGeorge.Wilson if (zfs_deadman_enabled) 594283b8460SGeorge.Wilson vdev_deadman(spa->spa_root_vdev); 595283b8460SGeorge.Wilson } 596283b8460SGeorge.Wilson 597fa9e4066Sahrens /* 598fa9e4066Sahrens * Create an uninitialized spa_t with the given name. Requires 599fa9e4066Sahrens * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 600fa9e4066Sahrens * exist by calling spa_lookup() first. 601fa9e4066Sahrens */ 602fa9e4066Sahrens spa_t * 603468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot) 604fa9e4066Sahrens { 605fa9e4066Sahrens spa_t *spa; 606c5904d13Seschrock spa_config_dirent_t *dp; 607283b8460SGeorge.Wilson cyc_handler_t hdlr; 608283b8460SGeorge.Wilson cyc_time_t when; 609fa9e4066Sahrens 610fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 611fa9e4066Sahrens 612fa9e4066Sahrens spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 613fa9e4066Sahrens 614c25056deSgw mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 615c25056deSgw mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 61635a5a358SJonathan Adams mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 617bc9014e6SJustin Gibbs mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL); 618c25056deSgw mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 61935a5a358SJonathan Adams mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 620c25056deSgw mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 62145818ee1SMatthew Ahrens mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL); 62235a5a358SJonathan Adams mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 623a1521560SJeff Bonwick mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 624a1521560SJeff Bonwick mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 625c3a66015SMatthew Ahrens mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL); 626c25056deSgw 627c25056deSgw cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 628bc9014e6SJustin Gibbs cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL); 62935a5a358SJonathan Adams cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 630c25056deSgw cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 631e14bb325SJeff Bonwick cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 632c25056deSgw 633b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 634cde58dbcSMatthew Ahrens bplist_create(&spa->spa_free_bplist[t]); 635b24ab676SJeff Bonwick 636e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 637fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 638fa9e4066Sahrens spa->spa_freeze_txg = UINT64_MAX; 6390373e76bSbonwick spa->spa_final_txg = UINT64_MAX; 640468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 64135a5a358SJonathan Adams spa->spa_proc = &p0; 64235a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 6436f793812SPavel Zakharov spa->spa_trust_config = B_TRUE; 644fa9e4066Sahrens 645283b8460SGeorge.Wilson hdlr.cyh_func = spa_deadman; 646283b8460SGeorge.Wilson hdlr.cyh_arg = spa; 647283b8460SGeorge.Wilson hdlr.cyh_level = CY_LOW_LEVEL; 648283b8460SGeorge.Wilson 64969962b56SMatthew Ahrens spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 650283b8460SGeorge.Wilson 651283b8460SGeorge.Wilson /* 652283b8460SGeorge.Wilson * This determines how often we need to check for hung I/Os after 653283b8460SGeorge.Wilson * the cyclic has already fired. Since checking for hung I/Os is 654283b8460SGeorge.Wilson * an expensive operation we don't want to check too frequently. 65569962b56SMatthew Ahrens * Instead wait for 5 seconds before checking again. 656283b8460SGeorge.Wilson */ 65769962b56SMatthew Ahrens when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms); 658283b8460SGeorge.Wilson when.cyt_when = CY_INFINITY; 659283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 660283b8460SGeorge.Wilson spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 661283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 662283b8460SGeorge.Wilson 663e914ace2STim Schumacher zfs_refcount_create(&spa->spa_refcount); 664e14bb325SJeff Bonwick spa_config_lock_init(spa); 665fa9e4066Sahrens 666fa9e4066Sahrens avl_add(&spa_namespace_avl, spa); 667fa9e4066Sahrens 6680373e76bSbonwick /* 6690373e76bSbonwick * Set the alternate root, if there is one. 6700373e76bSbonwick */ 6710373e76bSbonwick if (altroot) { 6720373e76bSbonwick spa->spa_root = spa_strdup(altroot); 6730373e76bSbonwick spa_active_count++; 6740373e76bSbonwick } 6750373e76bSbonwick 676f78cdc34SPaul Dagnelie spa->spa_alloc_count = spa_allocators; 677f78cdc34SPaul Dagnelie spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count * 678f78cdc34SPaul Dagnelie sizeof (kmutex_t), KM_SLEEP); 679f78cdc34SPaul Dagnelie spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count * 680f78cdc34SPaul Dagnelie sizeof (avl_tree_t), KM_SLEEP); 681f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) { 682f78cdc34SPaul Dagnelie mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL); 683f78cdc34SPaul Dagnelie avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare, 684f78cdc34SPaul Dagnelie sizeof (zio_t), offsetof(zio_t, io_alloc_node)); 685f78cdc34SPaul Dagnelie } 6860f7643c7SGeorge Wilson 687c5904d13Seschrock /* 688c5904d13Seschrock * Every pool starts with the default cachefile 689c5904d13Seschrock */ 690c5904d13Seschrock list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 691c5904d13Seschrock offsetof(spa_config_dirent_t, scd_link)); 692c5904d13Seschrock 693c5904d13Seschrock dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 694ef912c80STim Haley dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 695c5904d13Seschrock list_insert_head(&spa->spa_config_list, dp); 696c5904d13Seschrock 6974b964adaSGeorge Wilson VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 6984b964adaSGeorge Wilson KM_SLEEP) == 0); 6994b964adaSGeorge Wilson 700ad135b5dSChristopher Siden if (config != NULL) { 701ad135b5dSChristopher Siden nvlist_t *features; 702ad135b5dSChristopher Siden 703ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 704ad135b5dSChristopher Siden &features) == 0) { 705ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 706ad135b5dSChristopher Siden 0) == 0); 707ad135b5dSChristopher Siden } 708ad135b5dSChristopher Siden 709468c413aSTim Haley VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 710ad135b5dSChristopher Siden } 711ad135b5dSChristopher Siden 712ad135b5dSChristopher Siden if (spa->spa_label_features == NULL) { 713ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 714ad135b5dSChristopher Siden KM_SLEEP) == 0); 715ad135b5dSChristopher Siden } 716468c413aSTim Haley 717c3a66015SMatthew Ahrens spa->spa_iokstat = kstat_create("zfs", 0, name, 718c3a66015SMatthew Ahrens "disk", KSTAT_TYPE_IO, 1, 0); 719c3a66015SMatthew Ahrens if (spa->spa_iokstat) { 720c3a66015SMatthew Ahrens spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock; 721c3a66015SMatthew Ahrens kstat_install(spa->spa_iokstat); 722c3a66015SMatthew Ahrens } 723c3a66015SMatthew Ahrens 72481cd5c55SMatthew Ahrens spa->spa_min_ashift = INT_MAX; 72581cd5c55SMatthew Ahrens spa->spa_max_ashift = 0; 72681cd5c55SMatthew Ahrens 72743466aaeSMax Grossman /* 72843466aaeSMax Grossman * As a pool is being created, treat all features as disabled by 72943466aaeSMax Grossman * setting SPA_FEATURE_DISABLED for all entries in the feature 73043466aaeSMax Grossman * refcount cache. 73143466aaeSMax Grossman */ 73243466aaeSMax Grossman for (int i = 0; i < SPA_FEATURES; i++) { 73343466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED; 73443466aaeSMax Grossman } 73543466aaeSMax Grossman 736e0f1c0afSOlaf Faaland list_create(&spa->spa_leaf_list, sizeof (vdev_t), 737e0f1c0afSOlaf Faaland offsetof(vdev_t, vdev_leaf_node)); 738e0f1c0afSOlaf Faaland 739fa9e4066Sahrens return (spa); 740fa9e4066Sahrens } 741fa9e4066Sahrens 742fa9e4066Sahrens /* 743fa9e4066Sahrens * Removes a spa_t from the namespace, freeing up any memory used. Requires 744fa9e4066Sahrens * spa_namespace_lock. This is called only after the spa_t has been closed and 745fa9e4066Sahrens * deactivated. 746fa9e4066Sahrens */ 747fa9e4066Sahrens void 748fa9e4066Sahrens spa_remove(spa_t *spa) 749fa9e4066Sahrens { 750c5904d13Seschrock spa_config_dirent_t *dp; 751c5904d13Seschrock 752fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 753fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 754e914ace2STim Schumacher ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0); 755fa9e4066Sahrens 7561195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 7571195e687SMark J Musante 758fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa); 759fa9e4066Sahrens cv_broadcast(&spa_namespace_cv); 760fa9e4066Sahrens 7610373e76bSbonwick if (spa->spa_root) { 762fa9e4066Sahrens spa_strfree(spa->spa_root); 7630373e76bSbonwick spa_active_count--; 7640373e76bSbonwick } 765fa9e4066Sahrens 766c5904d13Seschrock while ((dp = list_head(&spa->spa_config_list)) != NULL) { 767c5904d13Seschrock list_remove(&spa->spa_config_list, dp); 768c5904d13Seschrock if (dp->scd_path != NULL) 769c5904d13Seschrock spa_strfree(dp->scd_path); 770c5904d13Seschrock kmem_free(dp, sizeof (spa_config_dirent_t)); 771c5904d13Seschrock } 772c5904d13Seschrock 773f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) { 774f78cdc34SPaul Dagnelie avl_destroy(&spa->spa_alloc_trees[i]); 775f78cdc34SPaul Dagnelie mutex_destroy(&spa->spa_alloc_locks[i]); 776f78cdc34SPaul Dagnelie } 777f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count * 778f78cdc34SPaul Dagnelie sizeof (kmutex_t)); 779f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count * 780f78cdc34SPaul Dagnelie sizeof (avl_tree_t)); 781f78cdc34SPaul Dagnelie 782c5904d13Seschrock list_destroy(&spa->spa_config_list); 783e0f1c0afSOlaf Faaland list_destroy(&spa->spa_leaf_list); 7842f8aaab3Seschrock 785ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 7864b964adaSGeorge Wilson nvlist_free(spa->spa_load_info); 787fa9e4066Sahrens spa_config_set(spa, NULL); 788fa9e4066Sahrens 789283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 790283b8460SGeorge.Wilson if (spa->spa_deadman_cycid != CYCLIC_NONE) 791283b8460SGeorge.Wilson cyclic_remove(spa->spa_deadman_cycid); 792283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 793283b8460SGeorge.Wilson spa->spa_deadman_cycid = CYCLIC_NONE; 794283b8460SGeorge.Wilson 795e914ace2STim Schumacher zfs_refcount_destroy(&spa->spa_refcount); 79691ebeef5Sahrens 797e14bb325SJeff Bonwick spa_config_lock_destroy(spa); 798fa9e4066Sahrens 799c3a66015SMatthew Ahrens kstat_delete(spa->spa_iokstat); 800c3a66015SMatthew Ahrens spa->spa_iokstat = NULL; 801c3a66015SMatthew Ahrens 802b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 803cde58dbcSMatthew Ahrens bplist_destroy(&spa->spa_free_bplist[t]); 804b24ab676SJeff Bonwick 80545818ee1SMatthew Ahrens zio_checksum_templates_free(spa); 80645818ee1SMatthew Ahrens 807c25056deSgw cv_destroy(&spa->spa_async_cv); 808bc9014e6SJustin Gibbs cv_destroy(&spa->spa_evicting_os_cv); 80935a5a358SJonathan Adams cv_destroy(&spa->spa_proc_cv); 810c25056deSgw cv_destroy(&spa->spa_scrub_io_cv); 811e14bb325SJeff Bonwick cv_destroy(&spa->spa_suspend_cv); 812c25056deSgw 8135ad82045Snd mutex_destroy(&spa->spa_async_lock); 814c25056deSgw mutex_destroy(&spa->spa_errlist_lock); 81535a5a358SJonathan Adams mutex_destroy(&spa->spa_errlog_lock); 816bc9014e6SJustin Gibbs mutex_destroy(&spa->spa_evicting_os_lock); 81706eeb2adSek mutex_destroy(&spa->spa_history_lock); 81835a5a358SJonathan Adams mutex_destroy(&spa->spa_proc_lock); 819b1b8ab34Slling mutex_destroy(&spa->spa_props_lock); 82045818ee1SMatthew Ahrens mutex_destroy(&spa->spa_cksum_tmpls_lock); 82135a5a358SJonathan Adams mutex_destroy(&spa->spa_scrub_lock); 822e14bb325SJeff Bonwick mutex_destroy(&spa->spa_suspend_lock); 823a1521560SJeff Bonwick mutex_destroy(&spa->spa_vdev_top_lock); 824c3a66015SMatthew Ahrens mutex_destroy(&spa->spa_iokstat_lock); 8255ad82045Snd 826fa9e4066Sahrens kmem_free(spa, sizeof (spa_t)); 827fa9e4066Sahrens } 828fa9e4066Sahrens 829fa9e4066Sahrens /* 830fa9e4066Sahrens * Given a pool, return the next pool in the namespace, or NULL if there is 831fa9e4066Sahrens * none. If 'prev' is NULL, return the first pool. 832fa9e4066Sahrens */ 833fa9e4066Sahrens spa_t * 834fa9e4066Sahrens spa_next(spa_t *prev) 835fa9e4066Sahrens { 836fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 837fa9e4066Sahrens 838fa9e4066Sahrens if (prev) 839fa9e4066Sahrens return (AVL_NEXT(&spa_namespace_avl, prev)); 840fa9e4066Sahrens else 841fa9e4066Sahrens return (avl_first(&spa_namespace_avl)); 842fa9e4066Sahrens } 843fa9e4066Sahrens 844fa9e4066Sahrens /* 845fa9e4066Sahrens * ========================================================================== 846fa9e4066Sahrens * SPA refcount functions 847fa9e4066Sahrens * ========================================================================== 848fa9e4066Sahrens */ 849fa9e4066Sahrens 850fa9e4066Sahrens /* 851fa9e4066Sahrens * Add a reference to the given spa_t. Must have at least one reference, or 852fa9e4066Sahrens * have the namespace lock held. 853fa9e4066Sahrens */ 854fa9e4066Sahrens void 855fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag) 856fa9e4066Sahrens { 857e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref || 858fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 859e914ace2STim Schumacher (void) zfs_refcount_add(&spa->spa_refcount, tag); 860fa9e4066Sahrens } 861fa9e4066Sahrens 862fa9e4066Sahrens /* 863fa9e4066Sahrens * Remove a reference to the given spa_t. Must have at least one reference, or 864fa9e4066Sahrens * have the namespace lock held. 865fa9e4066Sahrens */ 866fa9e4066Sahrens void 867fa9e4066Sahrens spa_close(spa_t *spa, void *tag) 868fa9e4066Sahrens { 869e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref || 870fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 871e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag); 872fa9e4066Sahrens } 873fa9e4066Sahrens 874bc9014e6SJustin Gibbs /* 875bc9014e6SJustin Gibbs * Remove a reference to the given spa_t held by a dsl dir that is 876bc9014e6SJustin Gibbs * being asynchronously released. Async releases occur from a taskq 877bc9014e6SJustin Gibbs * performing eviction of dsl datasets and dirs. The namespace lock 878bc9014e6SJustin Gibbs * isn't held and the hold by the object being evicted may contribute to 879bc9014e6SJustin Gibbs * spa_minref (e.g. dataset or directory released during pool export), 880bc9014e6SJustin Gibbs * so the asserts in spa_close() do not apply. 881bc9014e6SJustin Gibbs */ 882bc9014e6SJustin Gibbs void 883bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag) 884bc9014e6SJustin Gibbs { 885e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag); 886bc9014e6SJustin Gibbs } 887bc9014e6SJustin Gibbs 888fa9e4066Sahrens /* 889fa9e4066Sahrens * Check to see if the spa refcount is zero. Must be called with 890088f3894Sahrens * spa_namespace_lock held. We really compare against spa_minref, which is the 891fa9e4066Sahrens * number of references acquired when opening a pool 892fa9e4066Sahrens */ 893fa9e4066Sahrens boolean_t 894fa9e4066Sahrens spa_refcount_zero(spa_t *spa) 895fa9e4066Sahrens { 896fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 897fa9e4066Sahrens 898e914ace2STim Schumacher return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref); 899fa9e4066Sahrens } 900fa9e4066Sahrens 90199653d4eSeschrock /* 90299653d4eSeschrock * ========================================================================== 903fa94a07fSbrendan * SPA spare and l2cache tracking 90499653d4eSeschrock * ========================================================================== 90599653d4eSeschrock */ 90699653d4eSeschrock 907fa94a07fSbrendan /* 908fa94a07fSbrendan * Hot spares and cache devices are tracked using the same code below, 909fa94a07fSbrendan * for 'auxiliary' devices. 910fa94a07fSbrendan */ 911fa94a07fSbrendan 912fa94a07fSbrendan typedef struct spa_aux { 913fa94a07fSbrendan uint64_t aux_guid; 914fa94a07fSbrendan uint64_t aux_pool; 915fa94a07fSbrendan avl_node_t aux_avl; 916fa94a07fSbrendan int aux_count; 917fa94a07fSbrendan } spa_aux_t; 918fa94a07fSbrendan 919c4ab0d3fSGvozden Neskovic static inline int 920fa94a07fSbrendan spa_aux_compare(const void *a, const void *b) 921fa94a07fSbrendan { 922c4ab0d3fSGvozden Neskovic const spa_aux_t *sa = (const spa_aux_t *)a; 923c4ab0d3fSGvozden Neskovic const spa_aux_t *sb = (const spa_aux_t *)b; 924fa94a07fSbrendan 925c4ab0d3fSGvozden Neskovic return (AVL_CMP(sa->aux_guid, sb->aux_guid)); 926fa94a07fSbrendan } 927fa94a07fSbrendan 928fa94a07fSbrendan void 929fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl) 930fa94a07fSbrendan { 931fa94a07fSbrendan avl_index_t where; 932fa94a07fSbrendan spa_aux_t search; 933fa94a07fSbrendan spa_aux_t *aux; 934fa94a07fSbrendan 935fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 936fa94a07fSbrendan if ((aux = avl_find(avl, &search, &where)) != NULL) { 937fa94a07fSbrendan aux->aux_count++; 938fa94a07fSbrendan } else { 939fa94a07fSbrendan aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 940fa94a07fSbrendan aux->aux_guid = vd->vdev_guid; 941fa94a07fSbrendan aux->aux_count = 1; 942fa94a07fSbrendan avl_insert(avl, aux, where); 943fa94a07fSbrendan } 944fa94a07fSbrendan } 945fa94a07fSbrendan 946fa94a07fSbrendan void 947fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 948fa94a07fSbrendan { 949fa94a07fSbrendan spa_aux_t search; 950fa94a07fSbrendan spa_aux_t *aux; 951fa94a07fSbrendan avl_index_t where; 952fa94a07fSbrendan 953fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 954fa94a07fSbrendan aux = avl_find(avl, &search, &where); 955fa94a07fSbrendan 956fa94a07fSbrendan ASSERT(aux != NULL); 957fa94a07fSbrendan 958fa94a07fSbrendan if (--aux->aux_count == 0) { 959fa94a07fSbrendan avl_remove(avl, aux); 960fa94a07fSbrendan kmem_free(aux, sizeof (spa_aux_t)); 961fa94a07fSbrendan } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 962fa94a07fSbrendan aux->aux_pool = 0ULL; 963fa94a07fSbrendan } 964fa94a07fSbrendan } 965fa94a07fSbrendan 966fa94a07fSbrendan boolean_t 96789a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 968fa94a07fSbrendan { 969fa94a07fSbrendan spa_aux_t search, *found; 970fa94a07fSbrendan 971fa94a07fSbrendan search.aux_guid = guid; 97289a89ebfSlling found = avl_find(avl, &search, NULL); 973fa94a07fSbrendan 974fa94a07fSbrendan if (pool) { 975fa94a07fSbrendan if (found) 976fa94a07fSbrendan *pool = found->aux_pool; 977fa94a07fSbrendan else 978fa94a07fSbrendan *pool = 0ULL; 979fa94a07fSbrendan } 980fa94a07fSbrendan 98189a89ebfSlling if (refcnt) { 98289a89ebfSlling if (found) 98389a89ebfSlling *refcnt = found->aux_count; 98489a89ebfSlling else 98589a89ebfSlling *refcnt = 0; 98689a89ebfSlling } 98789a89ebfSlling 988fa94a07fSbrendan return (found != NULL); 989fa94a07fSbrendan } 990fa94a07fSbrendan 991fa94a07fSbrendan void 992fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 993fa94a07fSbrendan { 994fa94a07fSbrendan spa_aux_t search, *found; 995fa94a07fSbrendan avl_index_t where; 996fa94a07fSbrendan 997fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 998fa94a07fSbrendan found = avl_find(avl, &search, &where); 999fa94a07fSbrendan ASSERT(found != NULL); 1000fa94a07fSbrendan ASSERT(found->aux_pool == 0ULL); 1001fa94a07fSbrendan 1002fa94a07fSbrendan found->aux_pool = spa_guid(vd->vdev_spa); 1003fa94a07fSbrendan } 1004fa94a07fSbrendan 100599653d4eSeschrock /* 100639c23413Seschrock * Spares are tracked globally due to the following constraints: 100739c23413Seschrock * 100854811da5SToomas Soome * - A spare may be part of multiple pools. 100954811da5SToomas Soome * - A spare may be added to a pool even if it's actively in use within 101039c23413Seschrock * another pool. 101154811da5SToomas Soome * - A spare in use in any pool can only be the source of a replacement if 101239c23413Seschrock * the target is a spare in the same pool. 101339c23413Seschrock * 101439c23413Seschrock * We keep track of all spares on the system through the use of a reference 101539c23413Seschrock * counted AVL tree. When a vdev is added as a spare, or used as a replacement 101639c23413Seschrock * spare, then we bump the reference count in the AVL tree. In addition, we set 101739c23413Seschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or 101839c23413Seschrock * inactive). When a spare is made active (used to replace a device in the 101939c23413Seschrock * pool), we also keep track of which pool its been made a part of. 102039c23413Seschrock * 102139c23413Seschrock * The 'spa_spare_lock' protects the AVL tree. These functions are normally 102239c23413Seschrock * called under the spa_namespace lock as part of vdev reconfiguration. The 102339c23413Seschrock * separate spare lock exists for the status query path, which does not need to 102439c23413Seschrock * be completely consistent with respect to other vdev configuration changes. 102599653d4eSeschrock */ 102639c23413Seschrock 102799653d4eSeschrock static int 102899653d4eSeschrock spa_spare_compare(const void *a, const void *b) 102999653d4eSeschrock { 1030fa94a07fSbrendan return (spa_aux_compare(a, b)); 103199653d4eSeschrock } 103299653d4eSeschrock 103399653d4eSeschrock void 103439c23413Seschrock spa_spare_add(vdev_t *vd) 103599653d4eSeschrock { 103699653d4eSeschrock mutex_enter(&spa_spare_lock); 103739c23413Seschrock ASSERT(!vd->vdev_isspare); 1038fa94a07fSbrendan spa_aux_add(vd, &spa_spare_avl); 103939c23413Seschrock vd->vdev_isspare = B_TRUE; 104099653d4eSeschrock mutex_exit(&spa_spare_lock); 104199653d4eSeschrock } 104299653d4eSeschrock 104399653d4eSeschrock void 104439c23413Seschrock spa_spare_remove(vdev_t *vd) 104599653d4eSeschrock { 104699653d4eSeschrock mutex_enter(&spa_spare_lock); 104739c23413Seschrock ASSERT(vd->vdev_isspare); 1048fa94a07fSbrendan spa_aux_remove(vd, &spa_spare_avl); 104939c23413Seschrock vd->vdev_isspare = B_FALSE; 105099653d4eSeschrock mutex_exit(&spa_spare_lock); 105199653d4eSeschrock } 105299653d4eSeschrock 105399653d4eSeschrock boolean_t 105489a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 105599653d4eSeschrock { 1056fa94a07fSbrendan boolean_t found; 105799653d4eSeschrock 105899653d4eSeschrock mutex_enter(&spa_spare_lock); 105989a89ebfSlling found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 106099653d4eSeschrock mutex_exit(&spa_spare_lock); 106199653d4eSeschrock 1062fa94a07fSbrendan return (found); 106339c23413Seschrock } 106439c23413Seschrock 106539c23413Seschrock void 106639c23413Seschrock spa_spare_activate(vdev_t *vd) 106739c23413Seschrock { 106839c23413Seschrock mutex_enter(&spa_spare_lock); 106939c23413Seschrock ASSERT(vd->vdev_isspare); 1070fa94a07fSbrendan spa_aux_activate(vd, &spa_spare_avl); 1071fa94a07fSbrendan mutex_exit(&spa_spare_lock); 1072fa94a07fSbrendan } 107339c23413Seschrock 1074fa94a07fSbrendan /* 1075fa94a07fSbrendan * Level 2 ARC devices are tracked globally for the same reasons as spares. 1076fa94a07fSbrendan * Cache devices currently only support one pool per cache device, and so 1077fa94a07fSbrendan * for these devices the aux reference count is currently unused beyond 1. 1078fa94a07fSbrendan */ 107939c23413Seschrock 1080fa94a07fSbrendan static int 1081fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b) 1082fa94a07fSbrendan { 1083fa94a07fSbrendan return (spa_aux_compare(a, b)); 1084fa94a07fSbrendan } 1085fa94a07fSbrendan 1086fa94a07fSbrendan void 1087fa94a07fSbrendan spa_l2cache_add(vdev_t *vd) 1088fa94a07fSbrendan { 1089fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1090fa94a07fSbrendan ASSERT(!vd->vdev_isl2cache); 1091fa94a07fSbrendan spa_aux_add(vd, &spa_l2cache_avl); 1092fa94a07fSbrendan vd->vdev_isl2cache = B_TRUE; 1093fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1094fa94a07fSbrendan } 1095fa94a07fSbrendan 1096fa94a07fSbrendan void 1097fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd) 1098fa94a07fSbrendan { 1099fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1100fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1101fa94a07fSbrendan spa_aux_remove(vd, &spa_l2cache_avl); 1102fa94a07fSbrendan vd->vdev_isl2cache = B_FALSE; 1103fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1104fa94a07fSbrendan } 1105fa94a07fSbrendan 1106fa94a07fSbrendan boolean_t 1107fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool) 1108fa94a07fSbrendan { 1109fa94a07fSbrendan boolean_t found; 1110fa94a07fSbrendan 1111fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 111289a89ebfSlling found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 1113fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1114fa94a07fSbrendan 1115fa94a07fSbrendan return (found); 1116fa94a07fSbrendan } 1117fa94a07fSbrendan 1118fa94a07fSbrendan void 1119fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd) 1120fa94a07fSbrendan { 1121fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1122fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1123fa94a07fSbrendan spa_aux_activate(vd, &spa_l2cache_avl); 1124fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1125fa94a07fSbrendan } 1126fa94a07fSbrendan 1127fa9e4066Sahrens /* 1128fa9e4066Sahrens * ========================================================================== 1129fa9e4066Sahrens * SPA vdev locking 1130fa9e4066Sahrens * ========================================================================== 1131fa9e4066Sahrens */ 1132fa9e4066Sahrens 1133fa9e4066Sahrens /* 1134ea8dc4b6Seschrock * Lock the given spa_t for the purpose of adding or removing a vdev. 1135ea8dc4b6Seschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing. 1136fa9e4066Sahrens * It returns the next transaction group for the spa_t. 1137fa9e4066Sahrens */ 1138fa9e4066Sahrens uint64_t 1139fa9e4066Sahrens spa_vdev_enter(spa_t *spa) 1140fa9e4066Sahrens { 1141a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 1142bbfd46c4SJeff Bonwick mutex_enter(&spa_namespace_lock); 1143*084fd14fSBrian Behlendorf 1144*084fd14fSBrian Behlendorf vdev_autotrim_stop_all(spa); 1145*084fd14fSBrian Behlendorf 114688ecc943SGeorge Wilson return (spa_vdev_config_enter(spa)); 114788ecc943SGeorge Wilson } 114888ecc943SGeorge Wilson 114988ecc943SGeorge Wilson /* 115088ecc943SGeorge Wilson * Internal implementation for spa_vdev_enter(). Used when a vdev 115188ecc943SGeorge Wilson * operation requires multiple syncs (i.e. removing a device) while 115288ecc943SGeorge Wilson * keeping the spa_namespace_lock held. 115388ecc943SGeorge Wilson */ 115488ecc943SGeorge Wilson uint64_t 115588ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa) 115688ecc943SGeorge Wilson { 115788ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 11583d7072f8Seschrock 1159e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1160fa9e4066Sahrens 1161fa9e4066Sahrens return (spa_last_synced_txg(spa) + 1); 1162fa9e4066Sahrens } 1163fa9e4066Sahrens 1164fa9e4066Sahrens /* 116588ecc943SGeorge Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing 116688ecc943SGeorge Wilson * of multiple transactions without releasing the spa_namespace_lock. 1167fa9e4066Sahrens */ 116888ecc943SGeorge Wilson void 116988ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 1170fa9e4066Sahrens { 117188ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 117288ecc943SGeorge Wilson 11730e34b6a7Sbonwick int config_changed = B_FALSE; 1174ea8dc4b6Seschrock 11750373e76bSbonwick ASSERT(txg > spa_last_synced_txg(spa)); 11760e34b6a7Sbonwick 1177e14bb325SJeff Bonwick spa->spa_pending_vdev = NULL; 1178e14bb325SJeff Bonwick 11790e34b6a7Sbonwick /* 11800e34b6a7Sbonwick * Reassess the DTLs. 11810e34b6a7Sbonwick */ 11820373e76bSbonwick vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 11830e34b6a7Sbonwick 1184e14bb325SJeff Bonwick if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 11850e34b6a7Sbonwick config_changed = B_TRUE; 11868f18d1faSGeorge Wilson spa->spa_config_generation++; 11870e34b6a7Sbonwick } 1188ea8dc4b6Seschrock 118988ecc943SGeorge Wilson /* 119088ecc943SGeorge Wilson * Verify the metaslab classes. 119188ecc943SGeorge Wilson */ 1192b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1193b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 1194663207adSDon Brady ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0); 1195663207adSDon Brady ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0); 119688ecc943SGeorge Wilson 1197e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1198fa9e4066Sahrens 119988ecc943SGeorge Wilson /* 120088ecc943SGeorge Wilson * Panic the system if the specified tag requires it. This 120188ecc943SGeorge Wilson * is useful for ensuring that configurations are updated 120288ecc943SGeorge Wilson * transactionally. 120388ecc943SGeorge Wilson */ 120488ecc943SGeorge Wilson if (zio_injection_enabled) 12051195e687SMark J Musante zio_handle_panic_injection(spa, tag, 0); 120688ecc943SGeorge Wilson 1207fa9e4066Sahrens /* 1208fa9e4066Sahrens * Note: this txg_wait_synced() is important because it ensures 1209fa9e4066Sahrens * that there won't be more than one config change per txg. 1210fa9e4066Sahrens * This allows us to use the txg as the generation number. 1211fa9e4066Sahrens */ 1212fa9e4066Sahrens if (error == 0) 1213fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1214fa9e4066Sahrens 1215fa9e4066Sahrens if (vd != NULL) { 12160713e232SGeorge Wilson ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL); 1217094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 1218094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 1219*084fd14fSBrian Behlendorf vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, 1220*084fd14fSBrian Behlendorf NULL); 1221094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 1222*084fd14fSBrian Behlendorf 1223*084fd14fSBrian Behlendorf mutex_enter(&vd->vdev_trim_lock); 1224*084fd14fSBrian Behlendorf vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL); 1225*084fd14fSBrian Behlendorf mutex_exit(&vd->vdev_trim_lock); 1226094e47e9SGeorge Wilson } 1227094e47e9SGeorge Wilson 1228*084fd14fSBrian Behlendorf /* 1229*084fd14fSBrian Behlendorf * The vdev may be both a leaf and top-level device. 1230*084fd14fSBrian Behlendorf */ 1231*084fd14fSBrian Behlendorf vdev_autotrim_stop_wait(vd); 1232*084fd14fSBrian Behlendorf 12338ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1234fa9e4066Sahrens vdev_free(vd); 12358ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1236fa9e4066Sahrens } 1237fa9e4066Sahrens 1238fa9e4066Sahrens /* 12390e34b6a7Sbonwick * If the config changed, update the config cache. 1240fa9e4066Sahrens */ 12410e34b6a7Sbonwick if (config_changed) 12425cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 124388ecc943SGeorge Wilson } 1244ea8dc4b6Seschrock 124588ecc943SGeorge Wilson /* 124688ecc943SGeorge Wilson * Unlock the spa_t after adding or removing a vdev. Besides undoing the 124788ecc943SGeorge Wilson * locking of spa_vdev_enter(), we also want make sure the transactions have 124888ecc943SGeorge Wilson * synced to disk, and then update the global configuration cache with the new 124988ecc943SGeorge Wilson * information. 125088ecc943SGeorge Wilson */ 125188ecc943SGeorge Wilson int 125288ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 125388ecc943SGeorge Wilson { 1254*084fd14fSBrian Behlendorf vdev_autotrim_restart(spa); 1255*084fd14fSBrian Behlendorf 125688ecc943SGeorge Wilson spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1257ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1258bbfd46c4SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 1259fa9e4066Sahrens 1260fa9e4066Sahrens return (error); 1261fa9e4066Sahrens } 1262fa9e4066Sahrens 1263e14bb325SJeff Bonwick /* 1264e14bb325SJeff Bonwick * Lock the given spa_t for the purpose of changing vdev state. 1265e14bb325SJeff Bonwick */ 1266e14bb325SJeff Bonwick void 12678f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks) 1268e14bb325SJeff Bonwick { 12698f18d1faSGeorge Wilson int locks = SCL_STATE_ALL | oplocks; 12708f18d1faSGeorge Wilson 1271dcba9f3fSGeorge Wilson /* 1272dcba9f3fSGeorge Wilson * Root pools may need to read of the underlying devfs filesystem 1273dcba9f3fSGeorge Wilson * when opening up a vdev. Unfortunately if we're holding the 1274dcba9f3fSGeorge Wilson * SCL_ZIO lock it will result in a deadlock when we try to issue 1275dcba9f3fSGeorge Wilson * the read from the root filesystem. Instead we "prefetch" 1276dcba9f3fSGeorge Wilson * the associated vnodes that we need prior to opening the 1277dcba9f3fSGeorge Wilson * underlying devices and cache them so that we can prevent 1278dcba9f3fSGeorge Wilson * any I/O when we are doing the actual open. 1279dcba9f3fSGeorge Wilson */ 1280dcba9f3fSGeorge Wilson if (spa_is_root(spa)) { 12819842588bSGeorge Wilson int low = locks & ~(SCL_ZIO - 1); 12829842588bSGeorge Wilson int high = locks & ~low; 12839842588bSGeorge Wilson 12849842588bSGeorge Wilson spa_config_enter(spa, high, spa, RW_WRITER); 1285dcba9f3fSGeorge Wilson vdev_hold(spa->spa_root_vdev); 12869842588bSGeorge Wilson spa_config_enter(spa, low, spa, RW_WRITER); 1287dcba9f3fSGeorge Wilson } else { 1288dcba9f3fSGeorge Wilson spa_config_enter(spa, locks, spa, RW_WRITER); 1289dcba9f3fSGeorge Wilson } 12908f18d1faSGeorge Wilson spa->spa_vdev_locks = locks; 1291e14bb325SJeff Bonwick } 1292e14bb325SJeff Bonwick 1293e14bb325SJeff Bonwick int 1294e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1295e14bb325SJeff Bonwick { 1296c6065d0fSGeorge Wilson boolean_t config_changed = B_FALSE; 1297c6065d0fSGeorge Wilson 1298b24ab676SJeff Bonwick if (vd != NULL || error == 0) 1299b24ab676SJeff Bonwick vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1300b24ab676SJeff Bonwick 0, 0, B_FALSE); 1301b24ab676SJeff Bonwick 13028f18d1faSGeorge Wilson if (vd != NULL) { 1303e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 1304c6065d0fSGeorge Wilson config_changed = B_TRUE; 13058f18d1faSGeorge Wilson spa->spa_config_generation++; 13068f18d1faSGeorge Wilson } 1307e14bb325SJeff Bonwick 1308dcba9f3fSGeorge Wilson if (spa_is_root(spa)) 1309dcba9f3fSGeorge Wilson vdev_rele(spa->spa_root_vdev); 1310dcba9f3fSGeorge Wilson 13118f18d1faSGeorge Wilson ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 13128f18d1faSGeorge Wilson spa_config_exit(spa, spa->spa_vdev_locks, spa); 1313e14bb325SJeff Bonwick 13148ad4d6ddSJeff Bonwick /* 13158ad4d6ddSJeff Bonwick * If anything changed, wait for it to sync. This ensures that, 13168ad4d6ddSJeff Bonwick * from the system administrator's perspective, zpool(1M) commands 13178ad4d6ddSJeff Bonwick * are synchronous. This is important for things like zpool offline: 13188ad4d6ddSJeff Bonwick * when the command completes, you expect no further I/O from ZFS. 13198ad4d6ddSJeff Bonwick */ 13208ad4d6ddSJeff Bonwick if (vd != NULL) 13218ad4d6ddSJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, 0); 13228ad4d6ddSJeff Bonwick 1323c6065d0fSGeorge Wilson /* 1324c6065d0fSGeorge Wilson * If the config changed, update the config cache. 1325c6065d0fSGeorge Wilson */ 1326c6065d0fSGeorge Wilson if (config_changed) { 1327c6065d0fSGeorge Wilson mutex_enter(&spa_namespace_lock); 13285cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 1329c6065d0fSGeorge Wilson mutex_exit(&spa_namespace_lock); 1330c6065d0fSGeorge Wilson } 1331c6065d0fSGeorge Wilson 1332e14bb325SJeff Bonwick return (error); 1333e14bb325SJeff Bonwick } 1334e14bb325SJeff Bonwick 1335fa9e4066Sahrens /* 1336fa9e4066Sahrens * ========================================================================== 1337fa9e4066Sahrens * Miscellaneous functions 1338fa9e4066Sahrens * ========================================================================== 1339fa9e4066Sahrens */ 1340fa9e4066Sahrens 1341ad135b5dSChristopher Siden void 134243466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx) 1343ad135b5dSChristopher Siden { 13442acef22dSMatthew Ahrens if (!nvlist_exists(spa->spa_label_features, feature)) { 13452acef22dSMatthew Ahrens fnvlist_add_boolean(spa->spa_label_features, feature); 134643466aaeSMax Grossman /* 134743466aaeSMax Grossman * When we are creating the pool (tx_txg==TXG_INITIAL), we can't 134843466aaeSMax Grossman * dirty the vdev config because lock SCL_CONFIG is not held. 134943466aaeSMax Grossman * Thankfully, in this case we don't need to dirty the config 135043466aaeSMax Grossman * because it will be written out anyway when we finish 135143466aaeSMax Grossman * creating the pool. 135243466aaeSMax Grossman */ 135343466aaeSMax Grossman if (tx->tx_txg != TXG_INITIAL) 135443466aaeSMax Grossman vdev_config_dirty(spa->spa_root_vdev); 13552acef22dSMatthew Ahrens } 1356ad135b5dSChristopher Siden } 1357ad135b5dSChristopher Siden 1358ad135b5dSChristopher Siden void 1359ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1360ad135b5dSChristopher Siden { 13612acef22dSMatthew Ahrens if (nvlist_remove_all(spa->spa_label_features, feature) == 0) 13622acef22dSMatthew Ahrens vdev_config_dirty(spa->spa_root_vdev); 1363ad135b5dSChristopher Siden } 1364ad135b5dSChristopher Siden 1365fa9e4066Sahrens /* 1366f9af39baSGeorge Wilson * Return the spa_t associated with given pool_guid, if it exists. If 1367f9af39baSGeorge Wilson * device_guid is non-zero, determine whether the pool exists *and* contains 1368f9af39baSGeorge Wilson * a device with the specified device_guid. 1369fa9e4066Sahrens */ 1370f9af39baSGeorge Wilson spa_t * 1371f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1372fa9e4066Sahrens { 1373fa9e4066Sahrens spa_t *spa; 1374fa9e4066Sahrens avl_tree_t *t = &spa_namespace_avl; 1375fa9e4066Sahrens 1376ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1377fa9e4066Sahrens 1378fa9e4066Sahrens for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1379fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1380fa9e4066Sahrens continue; 1381fa9e4066Sahrens if (spa->spa_root_vdev == NULL) 1382fa9e4066Sahrens continue; 138339c23413Seschrock if (spa_guid(spa) == pool_guid) { 138439c23413Seschrock if (device_guid == 0) 138539c23413Seschrock break; 138639c23413Seschrock 138739c23413Seschrock if (vdev_lookup_by_guid(spa->spa_root_vdev, 138839c23413Seschrock device_guid) != NULL) 138939c23413Seschrock break; 139039c23413Seschrock 139139c23413Seschrock /* 13928654d025Sperrin * Check any devices we may be in the process of adding. 139339c23413Seschrock */ 139439c23413Seschrock if (spa->spa_pending_vdev) { 139539c23413Seschrock if (vdev_lookup_by_guid(spa->spa_pending_vdev, 139639c23413Seschrock device_guid) != NULL) 139739c23413Seschrock break; 139839c23413Seschrock } 139939c23413Seschrock } 1400fa9e4066Sahrens } 1401fa9e4066Sahrens 1402f9af39baSGeorge Wilson return (spa); 1403f9af39baSGeorge Wilson } 1404f9af39baSGeorge Wilson 1405f9af39baSGeorge Wilson /* 1406f9af39baSGeorge Wilson * Determine whether a pool with the given pool_guid exists. 1407f9af39baSGeorge Wilson */ 1408f9af39baSGeorge Wilson boolean_t 1409f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1410f9af39baSGeorge Wilson { 1411f9af39baSGeorge Wilson return (spa_by_guid(pool_guid, device_guid) != NULL); 1412fa9e4066Sahrens } 1413fa9e4066Sahrens 1414fa9e4066Sahrens char * 1415fa9e4066Sahrens spa_strdup(const char *s) 1416fa9e4066Sahrens { 1417fa9e4066Sahrens size_t len; 1418fa9e4066Sahrens char *new; 1419fa9e4066Sahrens 1420fa9e4066Sahrens len = strlen(s); 1421fa9e4066Sahrens new = kmem_alloc(len + 1, KM_SLEEP); 1422fa9e4066Sahrens bcopy(s, new, len); 1423fa9e4066Sahrens new[len] = '\0'; 1424fa9e4066Sahrens 1425fa9e4066Sahrens return (new); 1426fa9e4066Sahrens } 1427fa9e4066Sahrens 1428fa9e4066Sahrens void 1429fa9e4066Sahrens spa_strfree(char *s) 1430fa9e4066Sahrens { 1431fa9e4066Sahrens kmem_free(s, strlen(s) + 1); 1432fa9e4066Sahrens } 1433fa9e4066Sahrens 1434fa9e4066Sahrens uint64_t 1435fa9e4066Sahrens spa_get_random(uint64_t range) 1436fa9e4066Sahrens { 1437fa9e4066Sahrens uint64_t r; 1438fa9e4066Sahrens 1439fa9e4066Sahrens ASSERT(range != 0); 1440fa9e4066Sahrens 1441e0f1c0afSOlaf Faaland if (range == 1) 1442e0f1c0afSOlaf Faaland return (0); 1443e0f1c0afSOlaf Faaland 1444fa9e4066Sahrens (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1445fa9e4066Sahrens 1446fa9e4066Sahrens return (r % range); 1447fa9e4066Sahrens } 1448fa9e4066Sahrens 14491195e687SMark J Musante uint64_t 14501195e687SMark J Musante spa_generate_guid(spa_t *spa) 14511195e687SMark J Musante { 14521195e687SMark J Musante uint64_t guid = spa_get_random(-1ULL); 14531195e687SMark J Musante 14541195e687SMark J Musante if (spa != NULL) { 14551195e687SMark J Musante while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 14561195e687SMark J Musante guid = spa_get_random(-1ULL); 14571195e687SMark J Musante } else { 14581195e687SMark J Musante while (guid == 0 || spa_guid_exists(guid, 0)) 14591195e687SMark J Musante guid = spa_get_random(-1ULL); 14601195e687SMark J Musante } 14611195e687SMark J Musante 14621195e687SMark J Musante return (guid); 14631195e687SMark J Musante } 14641195e687SMark J Musante 1465fa9e4066Sahrens void 146643466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp) 1467fa9e4066Sahrens { 1468ad135b5dSChristopher Siden char type[256]; 1469f0ba89beSJeff Bonwick char *checksum = NULL; 1470f0ba89beSJeff Bonwick char *compress = NULL; 1471f0ba89beSJeff Bonwick 1472f0ba89beSJeff Bonwick if (bp != NULL) { 1473ad135b5dSChristopher Siden if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1474ad135b5dSChristopher Siden dmu_object_byteswap_t bswap = 1475ad135b5dSChristopher Siden DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1476ad135b5dSChristopher Siden (void) snprintf(type, sizeof (type), "bswap %s %s", 1477ad135b5dSChristopher Siden DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1478ad135b5dSChristopher Siden "metadata" : "data", 1479ad135b5dSChristopher Siden dmu_ot_byteswap[bswap].ob_name); 1480ad135b5dSChristopher Siden } else { 1481ad135b5dSChristopher Siden (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1482ad135b5dSChristopher Siden sizeof (type)); 1483ad135b5dSChristopher Siden } 14845d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 14855d7b4d43SMatthew Ahrens checksum = 14865d7b4d43SMatthew Ahrens zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 14875d7b4d43SMatthew Ahrens } 1488f0ba89beSJeff Bonwick compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1489f0ba89beSJeff Bonwick } 1490fa9e4066Sahrens 149143466aaeSMax Grossman SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum, 149243466aaeSMax Grossman compress); 1493fa9e4066Sahrens } 1494fa9e4066Sahrens 1495fa9e4066Sahrens void 1496fa9e4066Sahrens spa_freeze(spa_t *spa) 1497fa9e4066Sahrens { 1498fa9e4066Sahrens uint64_t freeze_txg = 0; 1499fa9e4066Sahrens 1500e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1501fa9e4066Sahrens if (spa->spa_freeze_txg == UINT64_MAX) { 1502fa9e4066Sahrens freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1503fa9e4066Sahrens spa->spa_freeze_txg = freeze_txg; 1504fa9e4066Sahrens } 1505e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1506fa9e4066Sahrens if (freeze_txg != 0) 1507fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1508fa9e4066Sahrens } 1509fa9e4066Sahrens 15100125049cSahrens void 15110125049cSahrens zfs_panic_recover(const char *fmt, ...) 15120125049cSahrens { 15130125049cSahrens va_list adx; 15140125049cSahrens 15150125049cSahrens va_start(adx, fmt); 15160125049cSahrens vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 15170125049cSahrens va_end(adx); 15180125049cSahrens } 15190125049cSahrens 15203f9d6ad7SLin Ling /* 15213f9d6ad7SLin Ling * This is a stripped-down version of strtoull, suitable only for converting 1522f7170741SWill Andrews * lowercase hexadecimal numbers that don't overflow. 15233f9d6ad7SLin Ling */ 15243f9d6ad7SLin Ling uint64_t 15254585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr) 15263f9d6ad7SLin Ling { 15273f9d6ad7SLin Ling uint64_t val = 0; 15283f9d6ad7SLin Ling char c; 15293f9d6ad7SLin Ling int digit; 15303f9d6ad7SLin Ling 15313f9d6ad7SLin Ling while ((c = *str) != '\0') { 15323f9d6ad7SLin Ling if (c >= '0' && c <= '9') 15333f9d6ad7SLin Ling digit = c - '0'; 15343f9d6ad7SLin Ling else if (c >= 'a' && c <= 'f') 15353f9d6ad7SLin Ling digit = 10 + c - 'a'; 15363f9d6ad7SLin Ling else 15373f9d6ad7SLin Ling break; 15383f9d6ad7SLin Ling 15393f9d6ad7SLin Ling val *= 16; 15403f9d6ad7SLin Ling val += digit; 15413f9d6ad7SLin Ling 15423f9d6ad7SLin Ling str++; 15433f9d6ad7SLin Ling } 15443f9d6ad7SLin Ling 15453f9d6ad7SLin Ling if (nptr) 15463f9d6ad7SLin Ling *nptr = (char *)str; 15473f9d6ad7SLin Ling 15483f9d6ad7SLin Ling return (val); 15493f9d6ad7SLin Ling } 15503f9d6ad7SLin Ling 1551663207adSDon Brady void 1552663207adSDon Brady spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx) 1553663207adSDon Brady { 1554663207adSDon Brady /* 1555663207adSDon Brady * We bump the feature refcount for each special vdev added to the pool 1556663207adSDon Brady */ 1557663207adSDon Brady ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)); 1558663207adSDon Brady spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx); 1559663207adSDon Brady } 1560663207adSDon Brady 1561fa9e4066Sahrens /* 1562fa9e4066Sahrens * ========================================================================== 1563fa9e4066Sahrens * Accessor functions 1564fa9e4066Sahrens * ========================================================================== 1565fa9e4066Sahrens */ 1566fa9e4066Sahrens 1567088f3894Sahrens boolean_t 156888b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa) 1569fa9e4066Sahrens { 157088b7b0f2SMatthew Ahrens return (spa->spa_async_suspended); 1571fa9e4066Sahrens } 1572fa9e4066Sahrens 1573fa9e4066Sahrens dsl_pool_t * 1574fa9e4066Sahrens spa_get_dsl(spa_t *spa) 1575fa9e4066Sahrens { 1576fa9e4066Sahrens return (spa->spa_dsl_pool); 1577fa9e4066Sahrens } 1578fa9e4066Sahrens 1579ad135b5dSChristopher Siden boolean_t 1580ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa) 1581ad135b5dSChristopher Siden { 1582ad135b5dSChristopher Siden return (spa->spa_is_initializing); 1583ad135b5dSChristopher Siden } 1584ad135b5dSChristopher Siden 15855cabbc6bSPrashanth Sreenivasa boolean_t 15865cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa) 15875cabbc6bSPrashanth Sreenivasa { 15885cabbc6bSPrashanth Sreenivasa return (spa->spa_indirect_vdevs_loaded); 15895cabbc6bSPrashanth Sreenivasa } 15905cabbc6bSPrashanth Sreenivasa 1591fa9e4066Sahrens blkptr_t * 1592fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa) 1593fa9e4066Sahrens { 1594fa9e4066Sahrens return (&spa->spa_ubsync.ub_rootbp); 1595fa9e4066Sahrens } 1596fa9e4066Sahrens 1597fa9e4066Sahrens void 1598fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1599fa9e4066Sahrens { 1600fa9e4066Sahrens spa->spa_uberblock.ub_rootbp = *bp; 1601fa9e4066Sahrens } 1602fa9e4066Sahrens 1603fa9e4066Sahrens void 1604fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen) 1605fa9e4066Sahrens { 1606fa9e4066Sahrens if (spa->spa_root == NULL) 1607fa9e4066Sahrens buf[0] = '\0'; 1608fa9e4066Sahrens else 1609fa9e4066Sahrens (void) strncpy(buf, spa->spa_root, buflen); 1610fa9e4066Sahrens } 1611fa9e4066Sahrens 1612fa9e4066Sahrens int 1613fa9e4066Sahrens spa_sync_pass(spa_t *spa) 1614fa9e4066Sahrens { 1615fa9e4066Sahrens return (spa->spa_sync_pass); 1616fa9e4066Sahrens } 1617fa9e4066Sahrens 1618fa9e4066Sahrens char * 1619fa9e4066Sahrens spa_name(spa_t *spa) 1620fa9e4066Sahrens { 1621fa9e4066Sahrens return (spa->spa_name); 1622fa9e4066Sahrens } 1623fa9e4066Sahrens 1624fa9e4066Sahrens uint64_t 1625fa9e4066Sahrens spa_guid(spa_t *spa) 1626fa9e4066Sahrens { 1627dfbb9432SGeorge Wilson dsl_pool_t *dp = spa_get_dsl(spa); 1628dfbb9432SGeorge Wilson uint64_t guid; 1629dfbb9432SGeorge Wilson 1630b5989ec7Seschrock /* 1631b5989ec7Seschrock * If we fail to parse the config during spa_load(), we can go through 1632b5989ec7Seschrock * the error path (which posts an ereport) and end up here with no root 1633e9103aaeSGarrett D'Amore * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1634b5989ec7Seschrock * this case. 1635b5989ec7Seschrock */ 1636dfbb9432SGeorge Wilson if (spa->spa_root_vdev == NULL) 1637dfbb9432SGeorge Wilson return (spa->spa_config_guid); 1638dfbb9432SGeorge Wilson 1639dfbb9432SGeorge Wilson guid = spa->spa_last_synced_guid != 0 ? 1640dfbb9432SGeorge Wilson spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1641dfbb9432SGeorge Wilson 1642dfbb9432SGeorge Wilson /* 1643dfbb9432SGeorge Wilson * Return the most recently synced out guid unless we're 1644dfbb9432SGeorge Wilson * in syncing context. 1645dfbb9432SGeorge Wilson */ 1646dfbb9432SGeorge Wilson if (dp && dsl_pool_sync_context(dp)) 1647b5989ec7Seschrock return (spa->spa_root_vdev->vdev_guid); 1648b5989ec7Seschrock else 1649dfbb9432SGeorge Wilson return (guid); 1650e9103aaeSGarrett D'Amore } 1651e9103aaeSGarrett D'Amore 1652e9103aaeSGarrett D'Amore uint64_t 1653e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa) 1654e9103aaeSGarrett D'Amore { 1655e9103aaeSGarrett D'Amore /* 1656e9103aaeSGarrett D'Amore * This is a GUID that exists solely as a reference for the 1657e9103aaeSGarrett D'Amore * purposes of the arc. It is generated at load time, and 1658e9103aaeSGarrett D'Amore * is never written to persistent storage. 1659e9103aaeSGarrett D'Amore */ 1660e9103aaeSGarrett D'Amore return (spa->spa_load_guid); 1661fa9e4066Sahrens } 1662fa9e4066Sahrens 1663fa9e4066Sahrens uint64_t 1664fa9e4066Sahrens spa_last_synced_txg(spa_t *spa) 1665fa9e4066Sahrens { 1666fa9e4066Sahrens return (spa->spa_ubsync.ub_txg); 1667fa9e4066Sahrens } 1668fa9e4066Sahrens 1669fa9e4066Sahrens uint64_t 1670fa9e4066Sahrens spa_first_txg(spa_t *spa) 1671fa9e4066Sahrens { 1672fa9e4066Sahrens return (spa->spa_first_txg); 1673fa9e4066Sahrens } 1674fa9e4066Sahrens 1675b24ab676SJeff Bonwick uint64_t 1676b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa) 1677b24ab676SJeff Bonwick { 1678b24ab676SJeff Bonwick return (spa->spa_syncing_txg); 1679b24ab676SJeff Bonwick } 1680b24ab676SJeff Bonwick 16813991b535SGeorge Wilson /* 16823991b535SGeorge Wilson * Return the last txg where data can be dirtied. The final txgs 16833991b535SGeorge Wilson * will be used to just clear out any deferred frees that remain. 16843991b535SGeorge Wilson */ 16853991b535SGeorge Wilson uint64_t 16863991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa) 16873991b535SGeorge Wilson { 16883991b535SGeorge Wilson return (spa->spa_final_txg - TXG_DEFER_SIZE); 16893991b535SGeorge Wilson } 16903991b535SGeorge Wilson 169188b7b0f2SMatthew Ahrens pool_state_t 1692fa9e4066Sahrens spa_state(spa_t *spa) 1693fa9e4066Sahrens { 1694fa9e4066Sahrens return (spa->spa_state); 1695fa9e4066Sahrens } 1696fa9e4066Sahrens 1697b16da2e2SGeorge Wilson spa_load_state_t 1698b16da2e2SGeorge Wilson spa_load_state(spa_t *spa) 1699b16da2e2SGeorge Wilson { 1700b16da2e2SGeorge Wilson return (spa->spa_load_state); 1701b16da2e2SGeorge Wilson } 1702b16da2e2SGeorge Wilson 1703fa9e4066Sahrens uint64_t 1704fa9e4066Sahrens spa_freeze_txg(spa_t *spa) 1705fa9e4066Sahrens { 1706fa9e4066Sahrens return (spa->spa_freeze_txg); 1707fa9e4066Sahrens } 1708fa9e4066Sahrens 1709fa9e4066Sahrens /* ARGSUSED */ 1710fa9e4066Sahrens uint64_t 171161e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize) 1712fa9e4066Sahrens { 171369962b56SMatthew Ahrens return (lsize * spa_asize_inflation); 171444cd46caSbillm } 171544cd46caSbillm 17167d46dc6cSMatthew Ahrens /* 17177d46dc6cSMatthew Ahrens * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%), 17184b5c8e93SMatthew Ahrens * or at least 128MB, unless that would cause it to be more than half the 17194b5c8e93SMatthew Ahrens * pool size. 17207d46dc6cSMatthew Ahrens * 17217d46dc6cSMatthew Ahrens * See the comment above spa_slop_shift for details. 17227d46dc6cSMatthew Ahrens */ 17237d46dc6cSMatthew Ahrens uint64_t 17244b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa) 17254b5c8e93SMatthew Ahrens { 17267d46dc6cSMatthew Ahrens uint64_t space = spa_get_dspace(spa); 17274b5c8e93SMatthew Ahrens return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop))); 17287d46dc6cSMatthew Ahrens } 17297d46dc6cSMatthew Ahrens 1730485bbbf5SGeorge Wilson uint64_t 1731485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa) 1732485bbbf5SGeorge Wilson { 1733485bbbf5SGeorge Wilson return (spa->spa_dspace); 1734485bbbf5SGeorge Wilson } 1735485bbbf5SGeorge Wilson 173686714001SSerapheim Dimitropoulos uint64_t 173786714001SSerapheim Dimitropoulos spa_get_checkpoint_space(spa_t *spa) 173886714001SSerapheim Dimitropoulos { 173986714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_info.sci_dspace); 174086714001SSerapheim Dimitropoulos } 174186714001SSerapheim Dimitropoulos 1742485bbbf5SGeorge Wilson void 1743485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa) 1744485bbbf5SGeorge Wilson { 1745485bbbf5SGeorge Wilson spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1746485bbbf5SGeorge Wilson ddt_get_dedup_dspace(spa); 17475cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 17485cabbc6bSPrashanth Sreenivasa /* 17495cabbc6bSPrashanth Sreenivasa * We can't allocate from the removing device, so 17505cabbc6bSPrashanth Sreenivasa * subtract its size. This prevents the DMU/DSL from 17515cabbc6bSPrashanth Sreenivasa * filling up the (now smaller) pool while we are in the 17525cabbc6bSPrashanth Sreenivasa * middle of removing the device. 17535cabbc6bSPrashanth Sreenivasa * 17545cabbc6bSPrashanth Sreenivasa * Note that the DMU/DSL doesn't actually know or care 17555cabbc6bSPrashanth Sreenivasa * how much space is allocated (it does its own tracking 17565cabbc6bSPrashanth Sreenivasa * of how much space has been logically used). So it 17575cabbc6bSPrashanth Sreenivasa * doesn't matter that the data we are moving may be 17585cabbc6bSPrashanth Sreenivasa * allocated twice (on the old device and the new 17595cabbc6bSPrashanth Sreenivasa * device). 17605cabbc6bSPrashanth Sreenivasa */ 17613a4b1be9SMatthew Ahrens spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 17623a4b1be9SMatthew Ahrens vdev_t *vd = 17633a4b1be9SMatthew Ahrens vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id); 17645cabbc6bSPrashanth Sreenivasa spa->spa_dspace -= spa_deflate(spa) ? 17655cabbc6bSPrashanth Sreenivasa vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 17663a4b1be9SMatthew Ahrens spa_config_exit(spa, SCL_VDEV, FTAG); 17675cabbc6bSPrashanth Sreenivasa } 1768485bbbf5SGeorge Wilson } 1769485bbbf5SGeorge Wilson 17700a4e9518Sgw /* 17710a4e9518Sgw * Return the failure mode that has been set to this pool. The default 17720a4e9518Sgw * behavior will be to block all I/Os when a complete failure occurs. 17730a4e9518Sgw */ 17740a4e9518Sgw uint8_t 17750a4e9518Sgw spa_get_failmode(spa_t *spa) 17760a4e9518Sgw { 17770a4e9518Sgw return (spa->spa_failmode); 17780a4e9518Sgw } 17790a4e9518Sgw 1780e14bb325SJeff Bonwick boolean_t 1781e14bb325SJeff Bonwick spa_suspended(spa_t *spa) 1782e14bb325SJeff Bonwick { 1783e0f1c0afSOlaf Faaland return (spa->spa_suspended != ZIO_SUSPEND_NONE); 1784e14bb325SJeff Bonwick } 1785e14bb325SJeff Bonwick 178644cd46caSbillm uint64_t 178744cd46caSbillm spa_version(spa_t *spa) 178844cd46caSbillm { 178944cd46caSbillm return (spa->spa_ubsync.ub_version); 179044cd46caSbillm } 179144cd46caSbillm 1792b24ab676SJeff Bonwick boolean_t 1793b24ab676SJeff Bonwick spa_deflate(spa_t *spa) 1794b24ab676SJeff Bonwick { 1795b24ab676SJeff Bonwick return (spa->spa_deflate); 1796b24ab676SJeff Bonwick } 1797b24ab676SJeff Bonwick 1798b24ab676SJeff Bonwick metaslab_class_t * 1799b24ab676SJeff Bonwick spa_normal_class(spa_t *spa) 1800b24ab676SJeff Bonwick { 1801b24ab676SJeff Bonwick return (spa->spa_normal_class); 1802b24ab676SJeff Bonwick } 1803b24ab676SJeff Bonwick 1804b24ab676SJeff Bonwick metaslab_class_t * 1805b24ab676SJeff Bonwick spa_log_class(spa_t *spa) 1806b24ab676SJeff Bonwick { 1807b24ab676SJeff Bonwick return (spa->spa_log_class); 1808b24ab676SJeff Bonwick } 1809b24ab676SJeff Bonwick 1810663207adSDon Brady metaslab_class_t * 1811663207adSDon Brady spa_special_class(spa_t *spa) 1812663207adSDon Brady { 1813663207adSDon Brady return (spa->spa_special_class); 1814663207adSDon Brady } 1815663207adSDon Brady 1816663207adSDon Brady metaslab_class_t * 1817663207adSDon Brady spa_dedup_class(spa_t *spa) 1818663207adSDon Brady { 1819663207adSDon Brady return (spa->spa_dedup_class); 1820663207adSDon Brady } 1821663207adSDon Brady 1822663207adSDon Brady /* 1823663207adSDon Brady * Locate an appropriate allocation class 1824663207adSDon Brady */ 1825663207adSDon Brady metaslab_class_t * 1826663207adSDon Brady spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype, 1827663207adSDon Brady uint_t level, uint_t special_smallblk) 1828663207adSDon Brady { 1829663207adSDon Brady if (DMU_OT_IS_ZIL(objtype)) { 1830663207adSDon Brady if (spa->spa_log_class->mc_groups != 0) 1831663207adSDon Brady return (spa_log_class(spa)); 1832663207adSDon Brady else 1833663207adSDon Brady return (spa_normal_class(spa)); 1834663207adSDon Brady } 1835663207adSDon Brady 1836663207adSDon Brady boolean_t has_special_class = spa->spa_special_class->mc_groups != 0; 1837663207adSDon Brady 1838663207adSDon Brady if (DMU_OT_IS_DDT(objtype)) { 1839663207adSDon Brady if (spa->spa_dedup_class->mc_groups != 0) 1840663207adSDon Brady return (spa_dedup_class(spa)); 1841663207adSDon Brady else if (has_special_class && zfs_ddt_data_is_special) 1842663207adSDon Brady return (spa_special_class(spa)); 1843663207adSDon Brady else 1844663207adSDon Brady return (spa_normal_class(spa)); 1845663207adSDon Brady } 1846663207adSDon Brady 1847663207adSDon Brady /* Indirect blocks for user data can land in special if allowed */ 1848663207adSDon Brady if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) { 1849663207adSDon Brady if (has_special_class && zfs_user_indirect_is_special) 1850663207adSDon Brady return (spa_special_class(spa)); 1851663207adSDon Brady else 1852663207adSDon Brady return (spa_normal_class(spa)); 1853663207adSDon Brady } 1854663207adSDon Brady 1855663207adSDon Brady if (DMU_OT_IS_METADATA(objtype) || level > 0) { 1856663207adSDon Brady if (has_special_class) 1857663207adSDon Brady return (spa_special_class(spa)); 1858663207adSDon Brady else 1859663207adSDon Brady return (spa_normal_class(spa)); 1860663207adSDon Brady } 1861663207adSDon Brady 1862663207adSDon Brady /* 1863663207adSDon Brady * Allow small file blocks in special class in some cases (like 1864663207adSDon Brady * for the dRAID vdev feature). But always leave a reserve of 1865663207adSDon Brady * zfs_special_class_metadata_reserve_pct exclusively for metadata. 1866663207adSDon Brady */ 1867663207adSDon Brady if (DMU_OT_IS_FILE(objtype) && 1868663207adSDon Brady has_special_class && size <= special_smallblk) { 1869663207adSDon Brady metaslab_class_t *special = spa_special_class(spa); 1870663207adSDon Brady uint64_t alloc = metaslab_class_get_alloc(special); 1871663207adSDon Brady uint64_t space = metaslab_class_get_space(special); 1872663207adSDon Brady uint64_t limit = 1873663207adSDon Brady (space * (100 - zfs_special_class_metadata_reserve_pct)) 1874663207adSDon Brady / 100; 1875663207adSDon Brady 1876663207adSDon Brady if (alloc < limit) 1877663207adSDon Brady return (special); 1878663207adSDon Brady } 1879663207adSDon Brady 1880663207adSDon Brady return (spa_normal_class(spa)); 1881663207adSDon Brady } 1882663207adSDon Brady 1883bc9014e6SJustin Gibbs void 1884bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os) 1885bc9014e6SJustin Gibbs { 1886bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1887bc9014e6SJustin Gibbs list_insert_head(&spa->spa_evicting_os_list, os); 1888bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1889bc9014e6SJustin Gibbs } 1890bc9014e6SJustin Gibbs 1891bc9014e6SJustin Gibbs void 1892bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os) 1893bc9014e6SJustin Gibbs { 1894bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1895bc9014e6SJustin Gibbs list_remove(&spa->spa_evicting_os_list, os); 1896bc9014e6SJustin Gibbs cv_broadcast(&spa->spa_evicting_os_cv); 1897bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1898bc9014e6SJustin Gibbs } 1899bc9014e6SJustin Gibbs 1900bc9014e6SJustin Gibbs void 1901bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa) 1902bc9014e6SJustin Gibbs { 1903bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1904bc9014e6SJustin Gibbs while (!list_is_empty(&spa->spa_evicting_os_list)) 1905bc9014e6SJustin Gibbs cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock); 1906bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1907bc9014e6SJustin Gibbs 1908bc9014e6SJustin Gibbs dmu_buf_user_evict_wait(); 1909bc9014e6SJustin Gibbs } 1910bc9014e6SJustin Gibbs 191144cd46caSbillm int 191244cd46caSbillm spa_max_replication(spa_t *spa) 191344cd46caSbillm { 191444cd46caSbillm /* 1915e7437265Sahrens * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 191644cd46caSbillm * handle BPs with more than one DVA allocated. Set our max 191744cd46caSbillm * replication level accordingly. 1918fa9e4066Sahrens */ 1919e7437265Sahrens if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 192044cd46caSbillm return (1); 192144cd46caSbillm return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1922fa9e4066Sahrens } 1923fa9e4066Sahrens 19243f9d6ad7SLin Ling int 19253f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa) 19263f9d6ad7SLin Ling { 19273f9d6ad7SLin Ling return (spa->spa_prev_software_version); 19283f9d6ad7SLin Ling } 19293f9d6ad7SLin Ling 1930283b8460SGeorge.Wilson uint64_t 1931283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa) 1932283b8460SGeorge.Wilson { 1933283b8460SGeorge.Wilson return (spa->spa_deadman_synctime); 1934283b8460SGeorge.Wilson } 1935283b8460SGeorge.Wilson 1936*084fd14fSBrian Behlendorf spa_autotrim_t 1937*084fd14fSBrian Behlendorf spa_get_autotrim(spa_t *spa) 1938*084fd14fSBrian Behlendorf { 1939*084fd14fSBrian Behlendorf return (spa->spa_autotrim); 1940*084fd14fSBrian Behlendorf } 1941*084fd14fSBrian Behlendorf 194299653d4eSeschrock uint64_t 1943b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 194499653d4eSeschrock { 1945b24ab676SJeff Bonwick uint64_t asize = DVA_GET_ASIZE(dva); 1946b24ab676SJeff Bonwick uint64_t dsize = asize; 194799653d4eSeschrock 1948b24ab676SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 194999653d4eSeschrock 1950b24ab676SJeff Bonwick if (asize != 0 && spa->spa_deflate) { 1951b24ab676SJeff Bonwick vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 1952b24ab676SJeff Bonwick dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 195399653d4eSeschrock } 1954b24ab676SJeff Bonwick 1955b24ab676SJeff Bonwick return (dsize); 1956b24ab676SJeff Bonwick } 1957b24ab676SJeff Bonwick 1958b24ab676SJeff Bonwick uint64_t 1959b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 1960b24ab676SJeff Bonwick { 1961b24ab676SJeff Bonwick uint64_t dsize = 0; 1962b24ab676SJeff Bonwick 19635d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1964b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1965b24ab676SJeff Bonwick 1966b24ab676SJeff Bonwick return (dsize); 1967b24ab676SJeff Bonwick } 1968b24ab676SJeff Bonwick 1969b24ab676SJeff Bonwick uint64_t 1970b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp) 1971b24ab676SJeff Bonwick { 1972b24ab676SJeff Bonwick uint64_t dsize = 0; 1973b24ab676SJeff Bonwick 1974b24ab676SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1975b24ab676SJeff Bonwick 19765d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1977b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1978b24ab676SJeff Bonwick 1979e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 1980b24ab676SJeff Bonwick 1981b24ab676SJeff Bonwick return (dsize); 198299653d4eSeschrock } 198399653d4eSeschrock 1984abe1fd01SDon Brady uint64_t 1985abe1fd01SDon Brady spa_dirty_data(spa_t *spa) 1986abe1fd01SDon Brady { 1987abe1fd01SDon Brady return (spa->spa_dsl_pool->dp_dirty_total); 1988abe1fd01SDon Brady } 1989abe1fd01SDon Brady 1990fa9e4066Sahrens /* 1991fa9e4066Sahrens * ========================================================================== 1992fa9e4066Sahrens * Initialization and Termination 1993fa9e4066Sahrens * ========================================================================== 1994fa9e4066Sahrens */ 1995fa9e4066Sahrens 1996fa9e4066Sahrens static int 1997fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2) 1998fa9e4066Sahrens { 1999fa9e4066Sahrens const spa_t *s1 = a1; 2000fa9e4066Sahrens const spa_t *s2 = a2; 2001fa9e4066Sahrens int s; 2002fa9e4066Sahrens 2003fa9e4066Sahrens s = strcmp(s1->spa_name, s2->spa_name); 2004c4ab0d3fSGvozden Neskovic 2005c4ab0d3fSGvozden Neskovic return (AVL_ISIGN(s)); 2006fa9e4066Sahrens } 2007fa9e4066Sahrens 20080373e76bSbonwick int 20090373e76bSbonwick spa_busy(void) 20100373e76bSbonwick { 20110373e76bSbonwick return (spa_active_count); 20120373e76bSbonwick } 20130373e76bSbonwick 2014e7cbe64fSgw void 2015e7cbe64fSgw spa_boot_init() 2016e7cbe64fSgw { 2017e7cbe64fSgw spa_config_load(); 2018e7cbe64fSgw } 2019e7cbe64fSgw 2020fa9e4066Sahrens void 2021fa9e4066Sahrens spa_init(int mode) 2022fa9e4066Sahrens { 2023fa9e4066Sahrens mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 2024c25056deSgw mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 2025fa94a07fSbrendan mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 2026fa9e4066Sahrens cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 2027fa9e4066Sahrens 2028fa9e4066Sahrens avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 2029fa9e4066Sahrens offsetof(spa_t, spa_avl)); 2030fa9e4066Sahrens 2031fa94a07fSbrendan avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 2032fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 2033fa94a07fSbrendan 2034fa94a07fSbrendan avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 2035fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 203699653d4eSeschrock 20378ad4d6ddSJeff Bonwick spa_mode_global = mode; 2038fa9e4066Sahrens 2039283b8460SGeorge.Wilson #ifdef _KERNEL 2040283b8460SGeorge.Wilson spa_arch_init(); 2041283b8460SGeorge.Wilson #else 2042cd1c8b85SMatthew Ahrens if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 2043cd1c8b85SMatthew Ahrens arc_procfd = open("/proc/self/ctl", O_WRONLY); 2044cd1c8b85SMatthew Ahrens if (arc_procfd == -1) { 2045cd1c8b85SMatthew Ahrens perror("could not enable watchpoints: " 2046cd1c8b85SMatthew Ahrens "opening /proc/self/ctl failed: "); 2047cd1c8b85SMatthew Ahrens } else { 2048cd1c8b85SMatthew Ahrens arc_watch = B_TRUE; 2049cd1c8b85SMatthew Ahrens } 2050cd1c8b85SMatthew Ahrens } 2051cd1c8b85SMatthew Ahrens #endif 2052cd1c8b85SMatthew Ahrens 2053e914ace2STim Schumacher zfs_refcount_init(); 2054fa9e4066Sahrens unique_init(); 20550713e232SGeorge Wilson range_tree_init(); 20568363e80aSGeorge Wilson metaslab_alloc_trace_init(); 2057fa9e4066Sahrens zio_init(); 2058fa9e4066Sahrens dmu_init(); 2059fa9e4066Sahrens zil_init(); 206087db74c1Sek vdev_cache_stat_init(); 206112a8814cSTom Caputi vdev_mirror_stat_init(); 206291ebeef5Sahrens zfs_prop_init(); 2063990b4856Slling zpool_prop_init(); 2064ad135b5dSChristopher Siden zpool_feature_init(); 2065fa9e4066Sahrens spa_config_load(); 2066e14bb325SJeff Bonwick l2arc_start(); 2067a3874b8bSToomas Soome scan_init(); 2068fa9e4066Sahrens } 2069fa9e4066Sahrens 2070fa9e4066Sahrens void 2071fa9e4066Sahrens spa_fini(void) 2072fa9e4066Sahrens { 2073e14bb325SJeff Bonwick l2arc_stop(); 2074e14bb325SJeff Bonwick 2075fa9e4066Sahrens spa_evict_all(); 2076fa9e4066Sahrens 207787db74c1Sek vdev_cache_stat_fini(); 207812a8814cSTom Caputi vdev_mirror_stat_fini(); 2079fa9e4066Sahrens zil_fini(); 2080fa9e4066Sahrens dmu_fini(); 2081fa9e4066Sahrens zio_fini(); 20828363e80aSGeorge Wilson metaslab_alloc_trace_fini(); 20830713e232SGeorge Wilson range_tree_fini(); 208491ebeef5Sahrens unique_fini(); 2085e914ace2STim Schumacher zfs_refcount_fini(); 2086a3874b8bSToomas Soome scan_fini(); 2087fa9e4066Sahrens 2088fa9e4066Sahrens avl_destroy(&spa_namespace_avl); 208999653d4eSeschrock avl_destroy(&spa_spare_avl); 2090fa94a07fSbrendan avl_destroy(&spa_l2cache_avl); 2091fa9e4066Sahrens 2092fa9e4066Sahrens cv_destroy(&spa_namespace_cv); 2093fa9e4066Sahrens mutex_destroy(&spa_namespace_lock); 2094c25056deSgw mutex_destroy(&spa_spare_lock); 2095fa94a07fSbrendan mutex_destroy(&spa_l2cache_lock); 2096fa9e4066Sahrens } 20976ce0521aSperrin 20986ce0521aSperrin /* 20996ce0521aSperrin * Return whether this pool has slogs. No locking needed. 21006ce0521aSperrin * It's not a problem if the wrong answer is returned as it's only for 21016ce0521aSperrin * performance and not correctness 21026ce0521aSperrin */ 21036ce0521aSperrin boolean_t 21046ce0521aSperrin spa_has_slogs(spa_t *spa) 21056ce0521aSperrin { 21066ce0521aSperrin return (spa->spa_log_class->mc_rotor != NULL); 21076ce0521aSperrin } 2108bf82a41bSeschrock 2109b24ab676SJeff Bonwick spa_log_state_t 2110b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa) 2111b24ab676SJeff Bonwick { 2112b24ab676SJeff Bonwick return (spa->spa_log_state); 2113b24ab676SJeff Bonwick } 2114b24ab676SJeff Bonwick 2115b24ab676SJeff Bonwick void 2116b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state) 2117b24ab676SJeff Bonwick { 2118b24ab676SJeff Bonwick spa->spa_log_state = state; 2119b24ab676SJeff Bonwick } 2120b24ab676SJeff Bonwick 2121bf82a41bSeschrock boolean_t 2122bf82a41bSeschrock spa_is_root(spa_t *spa) 2123bf82a41bSeschrock { 2124bf82a41bSeschrock return (spa->spa_is_root); 2125bf82a41bSeschrock } 21268ad4d6ddSJeff Bonwick 21278ad4d6ddSJeff Bonwick boolean_t 21288ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa) 21298ad4d6ddSJeff Bonwick { 21306f793812SPavel Zakharov return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config); 21318ad4d6ddSJeff Bonwick } 21328ad4d6ddSJeff Bonwick 213373527f44SAlex Reece /* 213473527f44SAlex Reece * Returns true if there is a pending sync task in any of the current 213573527f44SAlex Reece * syncing txg, the current quiescing txg, or the current open txg. 213673527f44SAlex Reece */ 213773527f44SAlex Reece boolean_t 213873527f44SAlex Reece spa_has_pending_synctask(spa_t *spa) 213973527f44SAlex Reece { 214086714001SSerapheim Dimitropoulos return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) || 214186714001SSerapheim Dimitropoulos !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks)); 214273527f44SAlex Reece } 214373527f44SAlex Reece 21448ad4d6ddSJeff Bonwick int 21458ad4d6ddSJeff Bonwick spa_mode(spa_t *spa) 21468ad4d6ddSJeff Bonwick { 21478ad4d6ddSJeff Bonwick return (spa->spa_mode); 21488ad4d6ddSJeff Bonwick } 2149b24ab676SJeff Bonwick 2150b24ab676SJeff Bonwick uint64_t 2151b24ab676SJeff Bonwick spa_bootfs(spa_t *spa) 2152b24ab676SJeff Bonwick { 2153b24ab676SJeff Bonwick return (spa->spa_bootfs); 2154b24ab676SJeff Bonwick } 2155b24ab676SJeff Bonwick 2156b24ab676SJeff Bonwick uint64_t 2157b24ab676SJeff Bonwick spa_delegation(spa_t *spa) 2158b24ab676SJeff Bonwick { 2159b24ab676SJeff Bonwick return (spa->spa_delegation); 2160b24ab676SJeff Bonwick } 2161b24ab676SJeff Bonwick 2162b24ab676SJeff Bonwick objset_t * 2163b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa) 2164b24ab676SJeff Bonwick { 2165b24ab676SJeff Bonwick return (spa->spa_meta_objset); 2166b24ab676SJeff Bonwick } 2167b24ab676SJeff Bonwick 2168b24ab676SJeff Bonwick enum zio_checksum 2169b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa) 2170b24ab676SJeff Bonwick { 2171b24ab676SJeff Bonwick return (spa->spa_dedup_checksum); 2172b24ab676SJeff Bonwick } 21733f9d6ad7SLin Ling 21743f9d6ad7SLin Ling /* 21753f9d6ad7SLin Ling * Reset pool scan stat per scan pass (or reboot). 21763f9d6ad7SLin Ling */ 21773f9d6ad7SLin Ling void 21783f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa) 21793f9d6ad7SLin Ling { 21803f9d6ad7SLin Ling /* data not stored on disk */ 21813f9d6ad7SLin Ling spa->spa_scan_pass_start = gethrestime_sec(); 21821702cce7SAlek Pinchuk if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan)) 21831702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start; 21841702cce7SAlek Pinchuk else 21851702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = 0; 21861702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_spent_paused = 0; 21873f9d6ad7SLin Ling spa->spa_scan_pass_exam = 0; 2188a3874b8bSToomas Soome spa->spa_scan_pass_issued = 0; 21893f9d6ad7SLin Ling vdev_scan_stat_init(spa->spa_root_vdev); 21903f9d6ad7SLin Ling } 21913f9d6ad7SLin Ling 21923f9d6ad7SLin Ling /* 21933f9d6ad7SLin Ling * Get scan stats for zpool status reports 21943f9d6ad7SLin Ling */ 21953f9d6ad7SLin Ling int 21963f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 21973f9d6ad7SLin Ling { 21983f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 21993f9d6ad7SLin Ling 22003f9d6ad7SLin Ling if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 2201be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 22023f9d6ad7SLin Ling bzero(ps, sizeof (pool_scan_stat_t)); 22033f9d6ad7SLin Ling 22043f9d6ad7SLin Ling /* data stored on disk */ 22053f9d6ad7SLin Ling ps->pss_func = scn->scn_phys.scn_func; 2206a3874b8bSToomas Soome ps->pss_state = scn->scn_phys.scn_state; 22073f9d6ad7SLin Ling ps->pss_start_time = scn->scn_phys.scn_start_time; 22083f9d6ad7SLin Ling ps->pss_end_time = scn->scn_phys.scn_end_time; 22093f9d6ad7SLin Ling ps->pss_to_examine = scn->scn_phys.scn_to_examine; 22103f9d6ad7SLin Ling ps->pss_to_process = scn->scn_phys.scn_to_process; 22113f9d6ad7SLin Ling ps->pss_processed = scn->scn_phys.scn_processed; 22123f9d6ad7SLin Ling ps->pss_errors = scn->scn_phys.scn_errors; 2213a3874b8bSToomas Soome ps->pss_examined = scn->scn_phys.scn_examined; 2214a3874b8bSToomas Soome ps->pss_issued = 2215a3874b8bSToomas Soome scn->scn_issued_before_pass + spa->spa_scan_pass_issued; 22163f9d6ad7SLin Ling ps->pss_state = scn->scn_phys.scn_state; 22173f9d6ad7SLin Ling 22183f9d6ad7SLin Ling /* data not stored on disk */ 22193f9d6ad7SLin Ling ps->pss_pass_start = spa->spa_scan_pass_start; 22203f9d6ad7SLin Ling ps->pss_pass_exam = spa->spa_scan_pass_exam; 2221a3874b8bSToomas Soome ps->pss_pass_issued = spa->spa_scan_pass_issued; 22221702cce7SAlek Pinchuk ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause; 22231702cce7SAlek Pinchuk ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused; 22243f9d6ad7SLin Ling 22253f9d6ad7SLin Ling return (0); 22263f9d6ad7SLin Ling } 222709c9d376SGeorge Wilson 2228b5152584SMatthew Ahrens int 2229b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa) 2230b5152584SMatthew Ahrens { 2231b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) 2232b5152584SMatthew Ahrens return (SPA_MAXBLOCKSIZE); 2233b5152584SMatthew Ahrens else 2234b5152584SMatthew Ahrens return (SPA_OLD_MAXBLOCKSIZE); 2235b5152584SMatthew Ahrens } 22365cabbc6bSPrashanth Sreenivasa 223754811da5SToomas Soome int 223854811da5SToomas Soome spa_maxdnodesize(spa_t *spa) 223954811da5SToomas Soome { 224054811da5SToomas Soome if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) 224154811da5SToomas Soome return (DNODE_MAX_SIZE); 224254811da5SToomas Soome else 224354811da5SToomas Soome return (DNODE_MIN_SIZE); 224454811da5SToomas Soome } 224554811da5SToomas Soome 2246e0f1c0afSOlaf Faaland boolean_t 2247e0f1c0afSOlaf Faaland spa_multihost(spa_t *spa) 2248e0f1c0afSOlaf Faaland { 2249e0f1c0afSOlaf Faaland return (spa->spa_multihost ? B_TRUE : B_FALSE); 2250e0f1c0afSOlaf Faaland } 2251e0f1c0afSOlaf Faaland 2252e0f1c0afSOlaf Faaland unsigned long 2253e0f1c0afSOlaf Faaland spa_get_hostid(void) 2254e0f1c0afSOlaf Faaland { 2255e0f1c0afSOlaf Faaland unsigned long myhostid; 2256e0f1c0afSOlaf Faaland 2257e0f1c0afSOlaf Faaland #ifdef _KERNEL 2258e0f1c0afSOlaf Faaland myhostid = zone_get_hostid(NULL); 2259e0f1c0afSOlaf Faaland #else /* _KERNEL */ 2260e0f1c0afSOlaf Faaland /* 2261e0f1c0afSOlaf Faaland * We're emulating the system's hostid in userland, so 2262e0f1c0afSOlaf Faaland * we can't use zone_get_hostid(). 2263e0f1c0afSOlaf Faaland */ 2264e0f1c0afSOlaf Faaland (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 2265e0f1c0afSOlaf Faaland #endif /* _KERNEL */ 2266e0f1c0afSOlaf Faaland 2267e0f1c0afSOlaf Faaland return (myhostid); 2268e0f1c0afSOlaf Faaland } 2269e0f1c0afSOlaf Faaland 22705cabbc6bSPrashanth Sreenivasa /* 22715cabbc6bSPrashanth Sreenivasa * Returns the txg that the last device removal completed. No indirect mappings 22725cabbc6bSPrashanth Sreenivasa * have been added since this txg. 22735cabbc6bSPrashanth Sreenivasa */ 22745cabbc6bSPrashanth Sreenivasa uint64_t 22755cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa) 22765cabbc6bSPrashanth Sreenivasa { 22775cabbc6bSPrashanth Sreenivasa uint64_t vdevid; 22785cabbc6bSPrashanth Sreenivasa uint64_t ret = -1ULL; 22795cabbc6bSPrashanth Sreenivasa 22805cabbc6bSPrashanth Sreenivasa spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 22815cabbc6bSPrashanth Sreenivasa /* 22825cabbc6bSPrashanth Sreenivasa * sr_prev_indirect_vdev is only modified while holding all the 22835cabbc6bSPrashanth Sreenivasa * config locks, so it is sufficient to hold SCL_VDEV as reader when 22845cabbc6bSPrashanth Sreenivasa * examining it. 22855cabbc6bSPrashanth Sreenivasa */ 22865cabbc6bSPrashanth Sreenivasa vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev; 22875cabbc6bSPrashanth Sreenivasa 22885cabbc6bSPrashanth Sreenivasa while (vdevid != -1ULL) { 22895cabbc6bSPrashanth Sreenivasa vdev_t *vd = vdev_lookup_top(spa, vdevid); 22905cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births; 22915cabbc6bSPrashanth Sreenivasa 22925cabbc6bSPrashanth Sreenivasa ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 22935cabbc6bSPrashanth Sreenivasa 22945cabbc6bSPrashanth Sreenivasa /* 22955cabbc6bSPrashanth Sreenivasa * If the removal did not remap any data, we don't care. 22965cabbc6bSPrashanth Sreenivasa */ 22975cabbc6bSPrashanth Sreenivasa if (vdev_indirect_births_count(vib) != 0) { 22985cabbc6bSPrashanth Sreenivasa ret = vdev_indirect_births_last_entry_txg(vib); 22995cabbc6bSPrashanth Sreenivasa break; 23005cabbc6bSPrashanth Sreenivasa } 23015cabbc6bSPrashanth Sreenivasa 23025cabbc6bSPrashanth Sreenivasa vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev; 23035cabbc6bSPrashanth Sreenivasa } 23045cabbc6bSPrashanth Sreenivasa spa_config_exit(spa, SCL_VDEV, FTAG); 23055cabbc6bSPrashanth Sreenivasa 23065cabbc6bSPrashanth Sreenivasa IMPLY(ret != -1ULL, 23075cabbc6bSPrashanth Sreenivasa spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 23085cabbc6bSPrashanth Sreenivasa 23095cabbc6bSPrashanth Sreenivasa return (ret); 23105cabbc6bSPrashanth Sreenivasa } 23116f793812SPavel Zakharov 23126f793812SPavel Zakharov boolean_t 23136f793812SPavel Zakharov spa_trust_config(spa_t *spa) 23146f793812SPavel Zakharov { 23156f793812SPavel Zakharov return (spa->spa_trust_config); 23166f793812SPavel Zakharov } 23176f793812SPavel Zakharov 23186f793812SPavel Zakharov uint64_t 23196f793812SPavel Zakharov spa_missing_tvds_allowed(spa_t *spa) 23206f793812SPavel Zakharov { 23216f793812SPavel Zakharov return (spa->spa_missing_tvds_allowed); 23226f793812SPavel Zakharov } 23236f793812SPavel Zakharov 23246f793812SPavel Zakharov void 23256f793812SPavel Zakharov spa_set_missing_tvds(spa_t *spa, uint64_t missing) 23266f793812SPavel Zakharov { 23276f793812SPavel Zakharov spa->spa_missing_tvds = missing; 23286f793812SPavel Zakharov } 232986714001SSerapheim Dimitropoulos 233086714001SSerapheim Dimitropoulos boolean_t 233186714001SSerapheim Dimitropoulos spa_top_vdevs_spacemap_addressable(spa_t *spa) 233286714001SSerapheim Dimitropoulos { 233386714001SSerapheim Dimitropoulos vdev_t *rvd = spa->spa_root_vdev; 233486714001SSerapheim Dimitropoulos for (uint64_t c = 0; c < rvd->vdev_children; c++) { 233586714001SSerapheim Dimitropoulos if (!vdev_is_spacemap_addressable(rvd->vdev_child[c])) 233686714001SSerapheim Dimitropoulos return (B_FALSE); 233786714001SSerapheim Dimitropoulos } 233886714001SSerapheim Dimitropoulos return (B_TRUE); 233986714001SSerapheim Dimitropoulos } 234086714001SSerapheim Dimitropoulos 234186714001SSerapheim Dimitropoulos boolean_t 234286714001SSerapheim Dimitropoulos spa_has_checkpoint(spa_t *spa) 234386714001SSerapheim Dimitropoulos { 234486714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_txg != 0); 234586714001SSerapheim Dimitropoulos } 234686714001SSerapheim Dimitropoulos 234786714001SSerapheim Dimitropoulos boolean_t 234886714001SSerapheim Dimitropoulos spa_importing_readonly_checkpoint(spa_t *spa) 234986714001SSerapheim Dimitropoulos { 235086714001SSerapheim Dimitropoulos return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) && 235186714001SSerapheim Dimitropoulos spa->spa_mode == FREAD); 235286714001SSerapheim Dimitropoulos } 235386714001SSerapheim Dimitropoulos 235486714001SSerapheim Dimitropoulos uint64_t 235586714001SSerapheim Dimitropoulos spa_min_claim_txg(spa_t *spa) 235686714001SSerapheim Dimitropoulos { 235786714001SSerapheim Dimitropoulos uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg; 235886714001SSerapheim Dimitropoulos 235986714001SSerapheim Dimitropoulos if (checkpoint_txg != 0) 236086714001SSerapheim Dimitropoulos return (checkpoint_txg + 1); 236186714001SSerapheim Dimitropoulos 236286714001SSerapheim Dimitropoulos return (spa->spa_first_txg); 236386714001SSerapheim Dimitropoulos } 236486714001SSerapheim Dimitropoulos 236586714001SSerapheim Dimitropoulos /* 236686714001SSerapheim Dimitropoulos * If there is a checkpoint, async destroys may consume more space from 236786714001SSerapheim Dimitropoulos * the pool instead of freeing it. In an attempt to save the pool from 236886714001SSerapheim Dimitropoulos * getting suspended when it is about to run out of space, we stop 236986714001SSerapheim Dimitropoulos * processing async destroys. 237086714001SSerapheim Dimitropoulos */ 237186714001SSerapheim Dimitropoulos boolean_t 237286714001SSerapheim Dimitropoulos spa_suspend_async_destroy(spa_t *spa) 237386714001SSerapheim Dimitropoulos { 237486714001SSerapheim Dimitropoulos dsl_pool_t *dp = spa_get_dsl(spa); 237586714001SSerapheim Dimitropoulos 237686714001SSerapheim Dimitropoulos uint64_t unreserved = dsl_pool_unreserved_space(dp, 237786714001SSerapheim Dimitropoulos ZFS_SPACE_CHECK_EXTRA_RESERVED); 237886714001SSerapheim Dimitropoulos uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes; 237986714001SSerapheim Dimitropoulos uint64_t avail = (unreserved > used) ? (unreserved - used) : 0; 238086714001SSerapheim Dimitropoulos 238186714001SSerapheim Dimitropoulos if (spa_has_checkpoint(spa) && avail == 0) 238286714001SSerapheim Dimitropoulos return (B_TRUE); 238386714001SSerapheim Dimitropoulos 238486714001SSerapheim Dimitropoulos return (B_FALSE); 238586714001SSerapheim Dimitropoulos } 2386