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. 29*663207adSDon 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> 44fa9e4066Sahrens #include <sys/metaslab.h> 45fa9e4066Sahrens #include <sys/uberblock_impl.h> 46fa9e4066Sahrens #include <sys/txg.h> 47fa9e4066Sahrens #include <sys/avl.h> 48fa9e4066Sahrens #include <sys/unique.h> 49fa9e4066Sahrens #include <sys/dsl_pool.h> 50fa9e4066Sahrens #include <sys/dsl_dir.h> 51fa9e4066Sahrens #include <sys/dsl_prop.h> 523f9d6ad7SLin Ling #include <sys/dsl_scan.h> 53fa9e4066Sahrens #include <sys/fs/zfs.h> 546ce0521aSperrin #include <sys/metaslab_impl.h> 55e14bb325SJeff Bonwick #include <sys/arc.h> 56485bbbf5SGeorge Wilson #include <sys/ddt.h> 5791ebeef5Sahrens #include "zfs_prop.h" 5845818ee1SMatthew Ahrens #include <sys/zfeature.h> 59fa9e4066Sahrens 60fa9e4066Sahrens /* 61fa9e4066Sahrens * SPA locking 62fa9e4066Sahrens * 63fa9e4066Sahrens * There are four basic locks for managing spa_t structures: 64fa9e4066Sahrens * 65fa9e4066Sahrens * spa_namespace_lock (global mutex) 66fa9e4066Sahrens * 6744cd46caSbillm * This lock must be acquired to do any of the following: 68fa9e4066Sahrens * 6944cd46caSbillm * - Lookup a spa_t by name 7044cd46caSbillm * - Add or remove a spa_t from the namespace 7144cd46caSbillm * - Increase spa_refcount from non-zero 7244cd46caSbillm * - Check if spa_refcount is zero 7344cd46caSbillm * - Rename a spa_t 74ea8dc4b6Seschrock * - add/remove/attach/detach devices 7544cd46caSbillm * - Held for the duration of create/destroy/import/export 76fa9e4066Sahrens * 7744cd46caSbillm * It does not need to handle recursion. A create or destroy may 7844cd46caSbillm * reference objects (files or zvols) in other pools, but by 7944cd46caSbillm * definition they must have an existing reference, and will never need 8044cd46caSbillm * to lookup a spa_t by name. 81fa9e4066Sahrens * 82e914ace2STim Schumacher * spa_refcount (per-spa zfs_refcount_t protected by mutex) 83fa9e4066Sahrens * 8444cd46caSbillm * This reference count keep track of any active users of the spa_t. The 8544cd46caSbillm * spa_t cannot be destroyed or freed while this is non-zero. Internally, 8644cd46caSbillm * the refcount is never really 'zero' - opening a pool implicitly keeps 87088f3894Sahrens * some references in the DMU. Internally we check against spa_minref, but 8844cd46caSbillm * present the image of a zero/non-zero value to consumers. 89fa9e4066Sahrens * 90e14bb325SJeff Bonwick * spa_config_lock[] (per-spa array of rwlocks) 91fa9e4066Sahrens * 9291ebeef5Sahrens * This protects the spa_t from config changes, and must be held in 9391ebeef5Sahrens * the following circumstances: 94fa9e4066Sahrens * 9544cd46caSbillm * - RW_READER to perform I/O to the spa 9644cd46caSbillm * - RW_WRITER to change the vdev config 97fa9e4066Sahrens * 98fa9e4066Sahrens * The locking order is fairly straightforward: 99fa9e4066Sahrens * 10044cd46caSbillm * spa_namespace_lock -> spa_refcount 101fa9e4066Sahrens * 10244cd46caSbillm * The namespace lock must be acquired to increase the refcount from 0 10344cd46caSbillm * or to check if it is zero. 104fa9e4066Sahrens * 105e14bb325SJeff Bonwick * spa_refcount -> spa_config_lock[] 106fa9e4066Sahrens * 10744cd46caSbillm * There must be at least one valid reference on the spa_t to acquire 10844cd46caSbillm * the config lock. 109fa9e4066Sahrens * 110e14bb325SJeff Bonwick * spa_namespace_lock -> spa_config_lock[] 111fa9e4066Sahrens * 11244cd46caSbillm * The namespace lock must always be taken before the config lock. 113fa9e4066Sahrens * 114fa9e4066Sahrens * 115e14bb325SJeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible. 116fa9e4066Sahrens * 117e14bb325SJeff Bonwick * The namespace is manipulated using the following functions, all of which 118e14bb325SJeff Bonwick * require the spa_namespace_lock to be held. 119fa9e4066Sahrens * 12044cd46caSbillm * spa_lookup() Lookup a spa_t by name. 121fa9e4066Sahrens * 12244cd46caSbillm * spa_add() Create a new spa_t in the namespace. 123fa9e4066Sahrens * 12444cd46caSbillm * spa_remove() Remove a spa_t from the namespace. This also 12544cd46caSbillm * frees up any memory associated with the spa_t. 126fa9e4066Sahrens * 12744cd46caSbillm * spa_next() Returns the next spa_t in the system, or the 12844cd46caSbillm * first if NULL is passed. 129fa9e4066Sahrens * 13044cd46caSbillm * spa_evict_all() Shutdown and remove all spa_t structures in 13144cd46caSbillm * the system. 132fa9e4066Sahrens * 133ea8dc4b6Seschrock * spa_guid_exists() Determine whether a pool/device guid exists. 134fa9e4066Sahrens * 135fa9e4066Sahrens * The spa_refcount is manipulated using the following functions: 136fa9e4066Sahrens * 13744cd46caSbillm * spa_open_ref() Adds a reference to the given spa_t. Must be 13844cd46caSbillm * called with spa_namespace_lock held if the 13944cd46caSbillm * refcount is currently zero. 140fa9e4066Sahrens * 14144cd46caSbillm * spa_close() Remove a reference from the spa_t. This will 14244cd46caSbillm * not free the spa_t or remove it from the 14344cd46caSbillm * namespace. No locking is required. 144fa9e4066Sahrens * 14544cd46caSbillm * spa_refcount_zero() Returns true if the refcount is currently 14644cd46caSbillm * zero. Must be called with spa_namespace_lock 14744cd46caSbillm * held. 148fa9e4066Sahrens * 149e14bb325SJeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows: 150e14bb325SJeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 151e14bb325SJeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 152e14bb325SJeff Bonwick * 153e14bb325SJeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader. 154e14bb325SJeff Bonwick * To modify the configuration, you must hold all locks as writer. To modify 155e14bb325SJeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline), 156e14bb325SJeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer. 157e14bb325SJeff Bonwick * 158e14bb325SJeff Bonwick * We use these distinct config locks to avoid recursive lock entry. 159e14bb325SJeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 160e14bb325SJeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps 161e14bb325SJeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 162e14bb325SJeff Bonwick * 163e14bb325SJeff Bonwick * The spa config locks cannot be normal rwlocks because we need the 164e14bb325SJeff Bonwick * ability to hand off ownership. For example, SCL_ZIO is acquired 165e14bb325SJeff Bonwick * by the issuing thread and later released by an interrupt thread. 166e14bb325SJeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent 167e14bb325SJeff Bonwick * writer (i.e. system administrator) starvation. 168e14bb325SJeff Bonwick * 169e14bb325SJeff Bonwick * The lock acquisition rules are as follows: 170e14bb325SJeff Bonwick * 171e14bb325SJeff Bonwick * SCL_CONFIG 172e14bb325SJeff Bonwick * Protects changes to the vdev tree topology, such as vdev 173e14bb325SJeff Bonwick * add/remove/attach/detach. Protects the dirty config list 174e14bb325SJeff Bonwick * (spa_config_dirty_list) and the set of spares and l2arc devices. 175e14bb325SJeff Bonwick * 176e14bb325SJeff Bonwick * SCL_STATE 177e14bb325SJeff Bonwick * Protects changes to pool state and vdev state, such as vdev 178e14bb325SJeff Bonwick * online/offline/fault/degrade/clear. Protects the dirty state list 179e14bb325SJeff Bonwick * (spa_state_dirty_list) and global pool state (spa_state). 180e14bb325SJeff Bonwick * 181e14bb325SJeff Bonwick * SCL_ALLOC 182e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 183e14bb325SJeff Bonwick * Held as reader by metaslab_alloc() and metaslab_claim(). 184e14bb325SJeff Bonwick * 185e14bb325SJeff Bonwick * SCL_ZIO 186e14bb325SJeff Bonwick * Held by bp-level zios (those which have no io_vd upon entry) 187e14bb325SJeff Bonwick * to prevent changes to the vdev tree. The bp-level zio implicitly 188e14bb325SJeff Bonwick * protects all of its vdev child zios, which do not hold SCL_ZIO. 189e14bb325SJeff Bonwick * 190e14bb325SJeff Bonwick * SCL_FREE 191e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 192e14bb325SJeff Bonwick * Held as reader by metaslab_free(). SCL_FREE is distinct from 193e14bb325SJeff Bonwick * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 194e14bb325SJeff Bonwick * blocks in zio_done() while another i/o that holds either 195e14bb325SJeff Bonwick * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 196e14bb325SJeff Bonwick * 197e14bb325SJeff Bonwick * SCL_VDEV 198e14bb325SJeff Bonwick * Held as reader to prevent changes to the vdev tree during trivial 199b24ab676SJeff Bonwick * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 200e14bb325SJeff Bonwick * other locks, and lower than all of them, to ensure that it's safe 201e14bb325SJeff Bonwick * to acquire regardless of caller context. 202e14bb325SJeff Bonwick * 203e14bb325SJeff Bonwick * In addition, the following rules apply: 204e14bb325SJeff Bonwick * 205e14bb325SJeff Bonwick * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 206e14bb325SJeff Bonwick * The lock ordering is SCL_CONFIG > spa_props_lock. 207e14bb325SJeff Bonwick * 208e14bb325SJeff Bonwick * (b) I/O operations on leaf vdevs. For any zio operation that takes 209e14bb325SJeff Bonwick * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 210e14bb325SJeff Bonwick * or zio_write_phys() -- the caller must ensure that the config cannot 211e14bb325SJeff Bonwick * cannot change in the interim, and that the vdev cannot be reopened. 212e14bb325SJeff Bonwick * SCL_STATE as reader suffices for both. 213fa9e4066Sahrens * 214ea8dc4b6Seschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 215fa9e4066Sahrens * 21644cd46caSbillm * spa_vdev_enter() Acquire the namespace lock and the config lock 217ea8dc4b6Seschrock * for writing. 218fa9e4066Sahrens * 21944cd46caSbillm * spa_vdev_exit() Release the config lock, wait for all I/O 22044cd46caSbillm * to complete, sync the updated configs to the 221ea8dc4b6Seschrock * cache, and release the namespace lock. 222fa9e4066Sahrens * 223e14bb325SJeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 224e14bb325SJeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 225e14bb325SJeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 226fa9e4066Sahrens */ 227fa9e4066Sahrens 228fa9e4066Sahrens static avl_tree_t spa_namespace_avl; 229fa9e4066Sahrens kmutex_t spa_namespace_lock; 230fa9e4066Sahrens static kcondvar_t spa_namespace_cv; 2310373e76bSbonwick static int spa_active_count; 232416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP; 233fa9e4066Sahrens 23499653d4eSeschrock static kmutex_t spa_spare_lock; 23539c23413Seschrock static avl_tree_t spa_spare_avl; 236fa94a07fSbrendan static kmutex_t spa_l2cache_lock; 237fa94a07fSbrendan static avl_tree_t spa_l2cache_avl; 23899653d4eSeschrock 239fa9e4066Sahrens kmem_cache_t *spa_buffer_pool; 2408ad4d6ddSJeff Bonwick int spa_mode_global; 241fa9e4066Sahrens 242fa9e4066Sahrens #ifdef ZFS_DEBUG 2435cabbc6bSPrashanth Sreenivasa /* 2445cabbc6bSPrashanth Sreenivasa * Everything except dprintf, spa, and indirect_remap is on by default 2455cabbc6bSPrashanth Sreenivasa * in debug builds. 2465cabbc6bSPrashanth Sreenivasa */ 24721f7c81cSMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP); 248fa9e4066Sahrens #else 249fa9e4066Sahrens int zfs_flags = 0; 250fa9e4066Sahrens #endif 251fa9e4066Sahrens 2520125049cSahrens /* 2530125049cSahrens * zfs_recover can be set to nonzero to attempt to recover from 2540125049cSahrens * otherwise-fatal errors, typically caused by on-disk corruption. When 2550125049cSahrens * set, calls to zfs_panic_recover() will turn into warning messages. 2568b36997aSMatthew Ahrens * This should only be used as a last resort, as it typically results 2578b36997aSMatthew Ahrens * in leaked space, or worse. 2580125049cSahrens */ 2597fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE; 2607fd05ac4SMatthew Ahrens 2617fd05ac4SMatthew Ahrens /* 2627fd05ac4SMatthew Ahrens * If destroy encounters an EIO while reading metadata (e.g. indirect 2637fd05ac4SMatthew Ahrens * blocks), space referenced by the missing metadata can not be freed. 2647fd05ac4SMatthew Ahrens * Normally this causes the background destroy to become "stalled", as 2657fd05ac4SMatthew Ahrens * it is unable to make forward progress. While in this stalled state, 2667fd05ac4SMatthew Ahrens * all remaining space to free from the error-encountering filesystem is 2677fd05ac4SMatthew Ahrens * "temporarily leaked". Set this flag to cause it to ignore the EIO, 2687fd05ac4SMatthew Ahrens * permanently leak the space from indirect blocks that can not be read, 2697fd05ac4SMatthew Ahrens * and continue to free everything else that it can. 2707fd05ac4SMatthew Ahrens * 2717fd05ac4SMatthew Ahrens * The default, "stalling" behavior is useful if the storage partially 2727fd05ac4SMatthew Ahrens * fails (i.e. some but not all i/os fail), and then later recovers. In 2737fd05ac4SMatthew Ahrens * this case, we will be able to continue pool operations while it is 2747fd05ac4SMatthew Ahrens * partially failed, and when it recovers, we can continue to free the 2757fd05ac4SMatthew Ahrens * space, with no leaks. However, note that this case is actually 2767fd05ac4SMatthew Ahrens * fairly rare. 2777fd05ac4SMatthew Ahrens * 2787fd05ac4SMatthew Ahrens * Typically pools either (a) fail completely (but perhaps temporarily, 2797fd05ac4SMatthew Ahrens * e.g. a top-level vdev going offline), or (b) have localized, 2807fd05ac4SMatthew Ahrens * permanent errors (e.g. disk returns the wrong data due to bit flip or 2817fd05ac4SMatthew Ahrens * firmware bug). In case (a), this setting does not matter because the 2827fd05ac4SMatthew Ahrens * pool will be suspended and the sync thread will not be able to make 2837fd05ac4SMatthew Ahrens * forward progress regardless. In case (b), because the error is 2847fd05ac4SMatthew Ahrens * permanent, the best we can do is leak the minimum amount of space, 2857fd05ac4SMatthew Ahrens * which is what setting this flag will do. Therefore, it is reasonable 2867fd05ac4SMatthew Ahrens * for this flag to normally be set, but we chose the more conservative 2877fd05ac4SMatthew Ahrens * approach of not setting it, so that there is no possibility of 2887fd05ac4SMatthew Ahrens * leaking space in the "partial temporary" failure case. 2897fd05ac4SMatthew Ahrens */ 2907fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE; 2910125049cSahrens 29269962b56SMatthew Ahrens /* 29369962b56SMatthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is 29469962b56SMatthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the 29569962b56SMatthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds. 29669962b56SMatthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that 29769962b56SMatthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting 29869962b56SMatthew Ahrens * in a system panic. 29969962b56SMatthew Ahrens */ 30069962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL; 301283b8460SGeorge.Wilson 302283b8460SGeorge.Wilson /* 30369962b56SMatthew Ahrens * Check time in milliseconds. This defines the frequency at which we check 30469962b56SMatthew Ahrens * for hung I/O. 305283b8460SGeorge.Wilson */ 30669962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL; 307283b8460SGeorge.Wilson 308283b8460SGeorge.Wilson /* 309283b8460SGeorge.Wilson * Override the zfs deadman behavior via /etc/system. By default the 310283b8460SGeorge.Wilson * deadman is enabled except on VMware and sparc deployments. 311283b8460SGeorge.Wilson */ 312283b8460SGeorge.Wilson int zfs_deadman_enabled = -1; 313283b8460SGeorge.Wilson 31469962b56SMatthew Ahrens /* 31569962b56SMatthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which 31669962b56SMatthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 31769962b56SMatthew Ahrens * times the size; so just assume that. Add to this the fact that 31869962b56SMatthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because 31969962b56SMatthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together, 32069962b56SMatthew Ahrens * the worst case is: 32169962b56SMatthew Ahrens * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24 32269962b56SMatthew Ahrens */ 32369962b56SMatthew Ahrens int spa_asize_inflation = 24; 324fa9e4066Sahrens 3257d46dc6cSMatthew Ahrens /* 3267d46dc6cSMatthew Ahrens * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in 3277d46dc6cSMatthew Ahrens * the pool to be consumed. This ensures that we don't run the pool 3287d46dc6cSMatthew Ahrens * completely out of space, due to unaccounted changes (e.g. to the MOS). 3297d46dc6cSMatthew Ahrens * It also limits the worst-case time to allocate space. If we have 3307d46dc6cSMatthew Ahrens * less than this amount of free space, most ZPL operations (e.g. write, 3317d46dc6cSMatthew Ahrens * create) will return ENOSPC. 3327d46dc6cSMatthew Ahrens * 3337d46dc6cSMatthew Ahrens * Certain operations (e.g. file removal, most administrative actions) can 3347d46dc6cSMatthew Ahrens * use half the slop space. They will only return ENOSPC if less than half 3357d46dc6cSMatthew Ahrens * the slop space is free. Typically, once the pool has less than the slop 3367d46dc6cSMatthew Ahrens * space free, the user will use these operations to free up space in the pool. 3377d46dc6cSMatthew Ahrens * These are the operations that call dsl_pool_adjustedsize() with the netfree 3387d46dc6cSMatthew Ahrens * argument set to TRUE. 3397d46dc6cSMatthew Ahrens * 34086714001SSerapheim Dimitropoulos * Operations that are almost guaranteed to free up space in the absence of 34186714001SSerapheim Dimitropoulos * a pool checkpoint can use up to three quarters of the slop space 34286714001SSerapheim Dimitropoulos * (e.g zfs destroy). 34386714001SSerapheim Dimitropoulos * 3447d46dc6cSMatthew Ahrens * A very restricted set of operations are always permitted, regardless of 3457d46dc6cSMatthew Ahrens * the amount of free space. These are the operations that call 34686714001SSerapheim Dimitropoulos * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net 34786714001SSerapheim Dimitropoulos * increase in the amount of space used, it is possible to run the pool 34886714001SSerapheim Dimitropoulos * completely out of space, causing it to be permanently read-only. 3497d46dc6cSMatthew Ahrens * 3504b5c8e93SMatthew Ahrens * Note that on very small pools, the slop space will be larger than 3514b5c8e93SMatthew Ahrens * 3.2%, in an effort to have it be at least spa_min_slop (128MB), 3524b5c8e93SMatthew Ahrens * but we never allow it to be more than half the pool size. 3534b5c8e93SMatthew Ahrens * 3547d46dc6cSMatthew Ahrens * See also the comments in zfs_space_check_t. 3557d46dc6cSMatthew Ahrens */ 3567d46dc6cSMatthew Ahrens int spa_slop_shift = 5; 3574b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024; 3587d46dc6cSMatthew Ahrens 359f78cdc34SPaul Dagnelie int spa_allocators = 4; 360f78cdc34SPaul Dagnelie 3613ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 3623ee8c80cSPavel Zakharov void 3633ee8c80cSPavel Zakharov spa_load_failed(spa_t *spa, const char *fmt, ...) 3643ee8c80cSPavel Zakharov { 3653ee8c80cSPavel Zakharov va_list adx; 3663ee8c80cSPavel Zakharov char buf[256]; 3673ee8c80cSPavel Zakharov 3683ee8c80cSPavel Zakharov va_start(adx, fmt); 3693ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 3703ee8c80cSPavel Zakharov va_end(adx); 3713ee8c80cSPavel Zakharov 3726f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name, 3736f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf); 3743ee8c80cSPavel Zakharov } 3753ee8c80cSPavel Zakharov 3763ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 3773ee8c80cSPavel Zakharov void 3783ee8c80cSPavel Zakharov spa_load_note(spa_t *spa, const char *fmt, ...) 3793ee8c80cSPavel Zakharov { 3803ee8c80cSPavel Zakharov va_list adx; 3813ee8c80cSPavel Zakharov char buf[256]; 3823ee8c80cSPavel Zakharov 3833ee8c80cSPavel Zakharov va_start(adx, fmt); 3843ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 3853ee8c80cSPavel Zakharov va_end(adx); 3863ee8c80cSPavel Zakharov 3876f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name, 3886f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf); 3893ee8c80cSPavel Zakharov } 3903ee8c80cSPavel Zakharov 391*663207adSDon Brady /* 392*663207adSDon Brady * By default dedup and user data indirects land in the special class 393*663207adSDon Brady */ 394*663207adSDon Brady int zfs_ddt_data_is_special = B_TRUE; 395*663207adSDon Brady int zfs_user_indirect_is_special = B_TRUE; 396*663207adSDon Brady 397*663207adSDon Brady /* 398*663207adSDon Brady * The percentage of special class final space reserved for metadata only. 399*663207adSDon Brady * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only 400*663207adSDon Brady * let metadata into the class. 401*663207adSDon Brady */ 402*663207adSDon Brady int zfs_special_class_metadata_reserve_pct = 25; 403*663207adSDon Brady 404e05725b1Sbonwick /* 405e05725b1Sbonwick * ========================================================================== 406e05725b1Sbonwick * SPA config locking 407e05725b1Sbonwick * ========================================================================== 408e05725b1Sbonwick */ 409e05725b1Sbonwick static void 410e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa) 411e14bb325SJeff Bonwick { 412e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 413e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 414e14bb325SJeff Bonwick mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 415e14bb325SJeff Bonwick cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 416e914ace2STim Schumacher zfs_refcount_create_untracked(&scl->scl_count); 417e14bb325SJeff Bonwick scl->scl_writer = NULL; 418e14bb325SJeff Bonwick scl->scl_write_wanted = 0; 419e14bb325SJeff Bonwick } 420e05725b1Sbonwick } 421e05725b1Sbonwick 422e05725b1Sbonwick static void 423e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa) 424e14bb325SJeff Bonwick { 425e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 426e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 427e14bb325SJeff Bonwick mutex_destroy(&scl->scl_lock); 428e14bb325SJeff Bonwick cv_destroy(&scl->scl_cv); 429e914ace2STim Schumacher zfs_refcount_destroy(&scl->scl_count); 430e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL); 431e14bb325SJeff Bonwick ASSERT(scl->scl_write_wanted == 0); 432e14bb325SJeff Bonwick } 433e14bb325SJeff Bonwick } 434e14bb325SJeff Bonwick 435e14bb325SJeff Bonwick int 436e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 437e05725b1Sbonwick { 438e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 439e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 440e14bb325SJeff Bonwick if (!(locks & (1 << i))) 441e14bb325SJeff Bonwick continue; 442e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 443e14bb325SJeff Bonwick if (rw == RW_READER) { 444e14bb325SJeff Bonwick if (scl->scl_writer || scl->scl_write_wanted) { 445e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 446e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 447e495b6e6SSaso Kiselkov tag); 448e14bb325SJeff Bonwick return (0); 449e14bb325SJeff Bonwick } 450e14bb325SJeff Bonwick } else { 451e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 452e914ace2STim Schumacher if (!zfs_refcount_is_zero(&scl->scl_count)) { 453e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 454e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 455e495b6e6SSaso Kiselkov tag); 456e14bb325SJeff Bonwick return (0); 457e14bb325SJeff Bonwick } 458e14bb325SJeff Bonwick scl->scl_writer = curthread; 459e14bb325SJeff Bonwick } 460e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag); 461e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 462e14bb325SJeff Bonwick } 463e14bb325SJeff Bonwick return (1); 464e05725b1Sbonwick } 465e05725b1Sbonwick 466e05725b1Sbonwick void 467e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 468e05725b1Sbonwick { 469f64c0e34SEric Taylor int wlocks_held = 0; 470f64c0e34SEric Taylor 4713b2aab18SMatthew Ahrens ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 4723b2aab18SMatthew Ahrens 473e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 474e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 475f64c0e34SEric Taylor if (scl->scl_writer == curthread) 476f64c0e34SEric Taylor wlocks_held |= (1 << i); 477e14bb325SJeff Bonwick if (!(locks & (1 << i))) 478e14bb325SJeff Bonwick continue; 479e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 480e14bb325SJeff Bonwick if (rw == RW_READER) { 481e14bb325SJeff Bonwick while (scl->scl_writer || scl->scl_write_wanted) { 482e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 483e14bb325SJeff Bonwick } 484e14bb325SJeff Bonwick } else { 485e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 486e914ace2STim Schumacher while (!zfs_refcount_is_zero(&scl->scl_count)) { 487e14bb325SJeff Bonwick scl->scl_write_wanted++; 488e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 489e14bb325SJeff Bonwick scl->scl_write_wanted--; 490e14bb325SJeff Bonwick } 491e14bb325SJeff Bonwick scl->scl_writer = curthread; 492e14bb325SJeff Bonwick } 493e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag); 494e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 495e05725b1Sbonwick } 4965cabbc6bSPrashanth Sreenivasa ASSERT3U(wlocks_held, <=, locks); 497e05725b1Sbonwick } 498e05725b1Sbonwick 499e05725b1Sbonwick void 500e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag) 501e05725b1Sbonwick { 502e14bb325SJeff Bonwick for (int i = SCL_LOCKS - 1; i >= 0; i--) { 503e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 504e14bb325SJeff Bonwick if (!(locks & (1 << i))) 505e14bb325SJeff Bonwick continue; 506e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 507e914ace2STim Schumacher ASSERT(!zfs_refcount_is_zero(&scl->scl_count)); 508e914ace2STim Schumacher if (zfs_refcount_remove(&scl->scl_count, tag) == 0) { 509e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL || 510e14bb325SJeff Bonwick scl->scl_writer == curthread); 511e14bb325SJeff Bonwick scl->scl_writer = NULL; /* OK in either case */ 512e14bb325SJeff Bonwick cv_broadcast(&scl->scl_cv); 513e14bb325SJeff Bonwick } 514e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 515e05725b1Sbonwick } 516e05725b1Sbonwick } 517e05725b1Sbonwick 518e14bb325SJeff Bonwick int 519e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw) 520e05725b1Sbonwick { 521e14bb325SJeff Bonwick int locks_held = 0; 522e05725b1Sbonwick 523e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 524e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 525e14bb325SJeff Bonwick if (!(locks & (1 << i))) 526e14bb325SJeff Bonwick continue; 527e914ace2STim Schumacher if ((rw == RW_READER && 528e914ace2STim Schumacher !zfs_refcount_is_zero(&scl->scl_count)) || 529e14bb325SJeff Bonwick (rw == RW_WRITER && scl->scl_writer == curthread)) 530e14bb325SJeff Bonwick locks_held |= 1 << i; 531e14bb325SJeff Bonwick } 532e14bb325SJeff Bonwick 533e14bb325SJeff Bonwick return (locks_held); 534e05725b1Sbonwick } 535e05725b1Sbonwick 536fa9e4066Sahrens /* 537fa9e4066Sahrens * ========================================================================== 538fa9e4066Sahrens * SPA namespace functions 539fa9e4066Sahrens * ========================================================================== 540fa9e4066Sahrens */ 541fa9e4066Sahrens 542fa9e4066Sahrens /* 543fa9e4066Sahrens * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 544fa9e4066Sahrens * Returns NULL if no matching spa_t is found. 545fa9e4066Sahrens */ 546fa9e4066Sahrens spa_t * 547fa9e4066Sahrens spa_lookup(const char *name) 548fa9e4066Sahrens { 549e14bb325SJeff Bonwick static spa_t search; /* spa_t is large; don't allocate on stack */ 550e14bb325SJeff Bonwick spa_t *spa; 551fa9e4066Sahrens avl_index_t where; 55240feaa91Sahrens char *cp; 553fa9e4066Sahrens 554fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 555fa9e4066Sahrens 5563b2aab18SMatthew Ahrens (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 5573b2aab18SMatthew Ahrens 55840feaa91Sahrens /* 55940feaa91Sahrens * If it's a full dataset name, figure out the pool name and 56040feaa91Sahrens * just use that. 56140feaa91Sahrens */ 56278f17100SMatthew Ahrens cp = strpbrk(search.spa_name, "/@#"); 5633b2aab18SMatthew Ahrens if (cp != NULL) 56440feaa91Sahrens *cp = '\0'; 56540feaa91Sahrens 566fa9e4066Sahrens spa = avl_find(&spa_namespace_avl, &search, &where); 567fa9e4066Sahrens 568fa9e4066Sahrens return (spa); 569fa9e4066Sahrens } 570fa9e4066Sahrens 571283b8460SGeorge.Wilson /* 572283b8460SGeorge.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 573283b8460SGeorge.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 574283b8460SGeorge.Wilson * looking for potentially hung I/Os. 575283b8460SGeorge.Wilson */ 576283b8460SGeorge.Wilson void 577283b8460SGeorge.Wilson spa_deadman(void *arg) 578283b8460SGeorge.Wilson { 579283b8460SGeorge.Wilson spa_t *spa = arg; 580283b8460SGeorge.Wilson 5810713e232SGeorge Wilson /* 5820713e232SGeorge Wilson * Disable the deadman timer if the pool is suspended. 5830713e232SGeorge Wilson */ 5840713e232SGeorge Wilson if (spa_suspended(spa)) { 5850713e232SGeorge Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 5860713e232SGeorge Wilson return; 5870713e232SGeorge Wilson } 5880713e232SGeorge Wilson 589283b8460SGeorge.Wilson zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 590283b8460SGeorge.Wilson (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 591283b8460SGeorge.Wilson ++spa->spa_deadman_calls); 592283b8460SGeorge.Wilson if (zfs_deadman_enabled) 593283b8460SGeorge.Wilson vdev_deadman(spa->spa_root_vdev); 594283b8460SGeorge.Wilson } 595283b8460SGeorge.Wilson 596fa9e4066Sahrens /* 597fa9e4066Sahrens * Create an uninitialized spa_t with the given name. Requires 598fa9e4066Sahrens * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 599fa9e4066Sahrens * exist by calling spa_lookup() first. 600fa9e4066Sahrens */ 601fa9e4066Sahrens spa_t * 602468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot) 603fa9e4066Sahrens { 604fa9e4066Sahrens spa_t *spa; 605c5904d13Seschrock spa_config_dirent_t *dp; 606283b8460SGeorge.Wilson cyc_handler_t hdlr; 607283b8460SGeorge.Wilson cyc_time_t when; 608fa9e4066Sahrens 609fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 610fa9e4066Sahrens 611fa9e4066Sahrens spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 612fa9e4066Sahrens 613c25056deSgw mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 614c25056deSgw mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 61535a5a358SJonathan Adams mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 616bc9014e6SJustin Gibbs mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL); 617c25056deSgw mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 61835a5a358SJonathan Adams mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 619c25056deSgw mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 62045818ee1SMatthew Ahrens mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL); 62135a5a358SJonathan Adams mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 622a1521560SJeff Bonwick mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 623a1521560SJeff Bonwick mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 624c3a66015SMatthew Ahrens mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL); 625c25056deSgw 626c25056deSgw cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 627bc9014e6SJustin Gibbs cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL); 62835a5a358SJonathan Adams cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 629c25056deSgw cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 630e14bb325SJeff Bonwick cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 631c25056deSgw 632b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 633cde58dbcSMatthew Ahrens bplist_create(&spa->spa_free_bplist[t]); 634b24ab676SJeff Bonwick 635e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 636fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 637fa9e4066Sahrens spa->spa_freeze_txg = UINT64_MAX; 6380373e76bSbonwick spa->spa_final_txg = UINT64_MAX; 639468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 64035a5a358SJonathan Adams spa->spa_proc = &p0; 64135a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 6426f793812SPavel Zakharov spa->spa_trust_config = B_TRUE; 643fa9e4066Sahrens 644283b8460SGeorge.Wilson hdlr.cyh_func = spa_deadman; 645283b8460SGeorge.Wilson hdlr.cyh_arg = spa; 646283b8460SGeorge.Wilson hdlr.cyh_level = CY_LOW_LEVEL; 647283b8460SGeorge.Wilson 64869962b56SMatthew Ahrens spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 649283b8460SGeorge.Wilson 650283b8460SGeorge.Wilson /* 651283b8460SGeorge.Wilson * This determines how often we need to check for hung I/Os after 652283b8460SGeorge.Wilson * the cyclic has already fired. Since checking for hung I/Os is 653283b8460SGeorge.Wilson * an expensive operation we don't want to check too frequently. 65469962b56SMatthew Ahrens * Instead wait for 5 seconds before checking again. 655283b8460SGeorge.Wilson */ 65669962b56SMatthew Ahrens when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms); 657283b8460SGeorge.Wilson when.cyt_when = CY_INFINITY; 658283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 659283b8460SGeorge.Wilson spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 660283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 661283b8460SGeorge.Wilson 662e914ace2STim Schumacher zfs_refcount_create(&spa->spa_refcount); 663e14bb325SJeff Bonwick spa_config_lock_init(spa); 664fa9e4066Sahrens 665fa9e4066Sahrens avl_add(&spa_namespace_avl, spa); 666fa9e4066Sahrens 6670373e76bSbonwick /* 6680373e76bSbonwick * Set the alternate root, if there is one. 6690373e76bSbonwick */ 6700373e76bSbonwick if (altroot) { 6710373e76bSbonwick spa->spa_root = spa_strdup(altroot); 6720373e76bSbonwick spa_active_count++; 6730373e76bSbonwick } 6740373e76bSbonwick 675f78cdc34SPaul Dagnelie spa->spa_alloc_count = spa_allocators; 676f78cdc34SPaul Dagnelie spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count * 677f78cdc34SPaul Dagnelie sizeof (kmutex_t), KM_SLEEP); 678f78cdc34SPaul Dagnelie spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count * 679f78cdc34SPaul Dagnelie sizeof (avl_tree_t), KM_SLEEP); 680f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) { 681f78cdc34SPaul Dagnelie mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL); 682f78cdc34SPaul Dagnelie avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare, 683f78cdc34SPaul Dagnelie sizeof (zio_t), offsetof(zio_t, io_alloc_node)); 684f78cdc34SPaul Dagnelie } 6850f7643c7SGeorge Wilson 686c5904d13Seschrock /* 687c5904d13Seschrock * Every pool starts with the default cachefile 688c5904d13Seschrock */ 689c5904d13Seschrock list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 690c5904d13Seschrock offsetof(spa_config_dirent_t, scd_link)); 691c5904d13Seschrock 692c5904d13Seschrock dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 693ef912c80STim Haley dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 694c5904d13Seschrock list_insert_head(&spa->spa_config_list, dp); 695c5904d13Seschrock 6964b964adaSGeorge Wilson VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 6974b964adaSGeorge Wilson KM_SLEEP) == 0); 6984b964adaSGeorge Wilson 699ad135b5dSChristopher Siden if (config != NULL) { 700ad135b5dSChristopher Siden nvlist_t *features; 701ad135b5dSChristopher Siden 702ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 703ad135b5dSChristopher Siden &features) == 0) { 704ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 705ad135b5dSChristopher Siden 0) == 0); 706ad135b5dSChristopher Siden } 707ad135b5dSChristopher Siden 708468c413aSTim Haley VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 709ad135b5dSChristopher Siden } 710ad135b5dSChristopher Siden 711ad135b5dSChristopher Siden if (spa->spa_label_features == NULL) { 712ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 713ad135b5dSChristopher Siden KM_SLEEP) == 0); 714ad135b5dSChristopher Siden } 715468c413aSTim Haley 716c3a66015SMatthew Ahrens spa->spa_iokstat = kstat_create("zfs", 0, name, 717c3a66015SMatthew Ahrens "disk", KSTAT_TYPE_IO, 1, 0); 718c3a66015SMatthew Ahrens if (spa->spa_iokstat) { 719c3a66015SMatthew Ahrens spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock; 720c3a66015SMatthew Ahrens kstat_install(spa->spa_iokstat); 721c3a66015SMatthew Ahrens } 722c3a66015SMatthew Ahrens 72381cd5c55SMatthew Ahrens spa->spa_min_ashift = INT_MAX; 72481cd5c55SMatthew Ahrens spa->spa_max_ashift = 0; 72581cd5c55SMatthew Ahrens 72643466aaeSMax Grossman /* 72743466aaeSMax Grossman * As a pool is being created, treat all features as disabled by 72843466aaeSMax Grossman * setting SPA_FEATURE_DISABLED for all entries in the feature 72943466aaeSMax Grossman * refcount cache. 73043466aaeSMax Grossman */ 73143466aaeSMax Grossman for (int i = 0; i < SPA_FEATURES; i++) { 73243466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED; 73343466aaeSMax Grossman } 73443466aaeSMax Grossman 735e0f1c0afSOlaf Faaland list_create(&spa->spa_leaf_list, sizeof (vdev_t), 736e0f1c0afSOlaf Faaland offsetof(vdev_t, vdev_leaf_node)); 737e0f1c0afSOlaf Faaland 738fa9e4066Sahrens return (spa); 739fa9e4066Sahrens } 740fa9e4066Sahrens 741fa9e4066Sahrens /* 742fa9e4066Sahrens * Removes a spa_t from the namespace, freeing up any memory used. Requires 743fa9e4066Sahrens * spa_namespace_lock. This is called only after the spa_t has been closed and 744fa9e4066Sahrens * deactivated. 745fa9e4066Sahrens */ 746fa9e4066Sahrens void 747fa9e4066Sahrens spa_remove(spa_t *spa) 748fa9e4066Sahrens { 749c5904d13Seschrock spa_config_dirent_t *dp; 750c5904d13Seschrock 751fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 752fa9e4066Sahrens ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 753e914ace2STim Schumacher ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0); 754fa9e4066Sahrens 7551195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 7561195e687SMark J Musante 757fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa); 758fa9e4066Sahrens cv_broadcast(&spa_namespace_cv); 759fa9e4066Sahrens 7600373e76bSbonwick if (spa->spa_root) { 761fa9e4066Sahrens spa_strfree(spa->spa_root); 7620373e76bSbonwick spa_active_count--; 7630373e76bSbonwick } 764fa9e4066Sahrens 765c5904d13Seschrock while ((dp = list_head(&spa->spa_config_list)) != NULL) { 766c5904d13Seschrock list_remove(&spa->spa_config_list, dp); 767c5904d13Seschrock if (dp->scd_path != NULL) 768c5904d13Seschrock spa_strfree(dp->scd_path); 769c5904d13Seschrock kmem_free(dp, sizeof (spa_config_dirent_t)); 770c5904d13Seschrock } 771c5904d13Seschrock 772f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) { 773f78cdc34SPaul Dagnelie avl_destroy(&spa->spa_alloc_trees[i]); 774f78cdc34SPaul Dagnelie mutex_destroy(&spa->spa_alloc_locks[i]); 775f78cdc34SPaul Dagnelie } 776f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count * 777f78cdc34SPaul Dagnelie sizeof (kmutex_t)); 778f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count * 779f78cdc34SPaul Dagnelie sizeof (avl_tree_t)); 780f78cdc34SPaul Dagnelie 781c5904d13Seschrock list_destroy(&spa->spa_config_list); 782e0f1c0afSOlaf Faaland list_destroy(&spa->spa_leaf_list); 7832f8aaab3Seschrock 784ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 7854b964adaSGeorge Wilson nvlist_free(spa->spa_load_info); 786fa9e4066Sahrens spa_config_set(spa, NULL); 787fa9e4066Sahrens 788283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 789283b8460SGeorge.Wilson if (spa->spa_deadman_cycid != CYCLIC_NONE) 790283b8460SGeorge.Wilson cyclic_remove(spa->spa_deadman_cycid); 791283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 792283b8460SGeorge.Wilson spa->spa_deadman_cycid = CYCLIC_NONE; 793283b8460SGeorge.Wilson 794e914ace2STim Schumacher zfs_refcount_destroy(&spa->spa_refcount); 79591ebeef5Sahrens 796e14bb325SJeff Bonwick spa_config_lock_destroy(spa); 797fa9e4066Sahrens 798c3a66015SMatthew Ahrens kstat_delete(spa->spa_iokstat); 799c3a66015SMatthew Ahrens spa->spa_iokstat = NULL; 800c3a66015SMatthew Ahrens 801b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 802cde58dbcSMatthew Ahrens bplist_destroy(&spa->spa_free_bplist[t]); 803b24ab676SJeff Bonwick 80445818ee1SMatthew Ahrens zio_checksum_templates_free(spa); 80545818ee1SMatthew Ahrens 806c25056deSgw cv_destroy(&spa->spa_async_cv); 807bc9014e6SJustin Gibbs cv_destroy(&spa->spa_evicting_os_cv); 80835a5a358SJonathan Adams cv_destroy(&spa->spa_proc_cv); 809c25056deSgw cv_destroy(&spa->spa_scrub_io_cv); 810e14bb325SJeff Bonwick cv_destroy(&spa->spa_suspend_cv); 811c25056deSgw 8125ad82045Snd mutex_destroy(&spa->spa_async_lock); 813c25056deSgw mutex_destroy(&spa->spa_errlist_lock); 81435a5a358SJonathan Adams mutex_destroy(&spa->spa_errlog_lock); 815bc9014e6SJustin Gibbs mutex_destroy(&spa->spa_evicting_os_lock); 81606eeb2adSek mutex_destroy(&spa->spa_history_lock); 81735a5a358SJonathan Adams mutex_destroy(&spa->spa_proc_lock); 818b1b8ab34Slling mutex_destroy(&spa->spa_props_lock); 81945818ee1SMatthew Ahrens mutex_destroy(&spa->spa_cksum_tmpls_lock); 82035a5a358SJonathan Adams mutex_destroy(&spa->spa_scrub_lock); 821e14bb325SJeff Bonwick mutex_destroy(&spa->spa_suspend_lock); 822a1521560SJeff Bonwick mutex_destroy(&spa->spa_vdev_top_lock); 823c3a66015SMatthew Ahrens mutex_destroy(&spa->spa_iokstat_lock); 8245ad82045Snd 825fa9e4066Sahrens kmem_free(spa, sizeof (spa_t)); 826fa9e4066Sahrens } 827fa9e4066Sahrens 828fa9e4066Sahrens /* 829fa9e4066Sahrens * Given a pool, return the next pool in the namespace, or NULL if there is 830fa9e4066Sahrens * none. If 'prev' is NULL, return the first pool. 831fa9e4066Sahrens */ 832fa9e4066Sahrens spa_t * 833fa9e4066Sahrens spa_next(spa_t *prev) 834fa9e4066Sahrens { 835fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 836fa9e4066Sahrens 837fa9e4066Sahrens if (prev) 838fa9e4066Sahrens return (AVL_NEXT(&spa_namespace_avl, prev)); 839fa9e4066Sahrens else 840fa9e4066Sahrens return (avl_first(&spa_namespace_avl)); 841fa9e4066Sahrens } 842fa9e4066Sahrens 843fa9e4066Sahrens /* 844fa9e4066Sahrens * ========================================================================== 845fa9e4066Sahrens * SPA refcount functions 846fa9e4066Sahrens * ========================================================================== 847fa9e4066Sahrens */ 848fa9e4066Sahrens 849fa9e4066Sahrens /* 850fa9e4066Sahrens * Add a reference to the given spa_t. Must have at least one reference, or 851fa9e4066Sahrens * have the namespace lock held. 852fa9e4066Sahrens */ 853fa9e4066Sahrens void 854fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag) 855fa9e4066Sahrens { 856e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref || 857fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 858e914ace2STim Schumacher (void) zfs_refcount_add(&spa->spa_refcount, tag); 859fa9e4066Sahrens } 860fa9e4066Sahrens 861fa9e4066Sahrens /* 862fa9e4066Sahrens * Remove a reference to the given spa_t. Must have at least one reference, or 863fa9e4066Sahrens * have the namespace lock held. 864fa9e4066Sahrens */ 865fa9e4066Sahrens void 866fa9e4066Sahrens spa_close(spa_t *spa, void *tag) 867fa9e4066Sahrens { 868e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref || 869fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 870e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag); 871fa9e4066Sahrens } 872fa9e4066Sahrens 873bc9014e6SJustin Gibbs /* 874bc9014e6SJustin Gibbs * Remove a reference to the given spa_t held by a dsl dir that is 875bc9014e6SJustin Gibbs * being asynchronously released. Async releases occur from a taskq 876bc9014e6SJustin Gibbs * performing eviction of dsl datasets and dirs. The namespace lock 877bc9014e6SJustin Gibbs * isn't held and the hold by the object being evicted may contribute to 878bc9014e6SJustin Gibbs * spa_minref (e.g. dataset or directory released during pool export), 879bc9014e6SJustin Gibbs * so the asserts in spa_close() do not apply. 880bc9014e6SJustin Gibbs */ 881bc9014e6SJustin Gibbs void 882bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag) 883bc9014e6SJustin Gibbs { 884e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag); 885bc9014e6SJustin Gibbs } 886bc9014e6SJustin Gibbs 887fa9e4066Sahrens /* 888fa9e4066Sahrens * Check to see if the spa refcount is zero. Must be called with 889088f3894Sahrens * spa_namespace_lock held. We really compare against spa_minref, which is the 890fa9e4066Sahrens * number of references acquired when opening a pool 891fa9e4066Sahrens */ 892fa9e4066Sahrens boolean_t 893fa9e4066Sahrens spa_refcount_zero(spa_t *spa) 894fa9e4066Sahrens { 895fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 896fa9e4066Sahrens 897e914ace2STim Schumacher return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref); 898fa9e4066Sahrens } 899fa9e4066Sahrens 90099653d4eSeschrock /* 90199653d4eSeschrock * ========================================================================== 902fa94a07fSbrendan * SPA spare and l2cache tracking 90399653d4eSeschrock * ========================================================================== 90499653d4eSeschrock */ 90599653d4eSeschrock 906fa94a07fSbrendan /* 907fa94a07fSbrendan * Hot spares and cache devices are tracked using the same code below, 908fa94a07fSbrendan * for 'auxiliary' devices. 909fa94a07fSbrendan */ 910fa94a07fSbrendan 911fa94a07fSbrendan typedef struct spa_aux { 912fa94a07fSbrendan uint64_t aux_guid; 913fa94a07fSbrendan uint64_t aux_pool; 914fa94a07fSbrendan avl_node_t aux_avl; 915fa94a07fSbrendan int aux_count; 916fa94a07fSbrendan } spa_aux_t; 917fa94a07fSbrendan 918fa94a07fSbrendan static int 919fa94a07fSbrendan spa_aux_compare(const void *a, const void *b) 920fa94a07fSbrendan { 921fa94a07fSbrendan const spa_aux_t *sa = a; 922fa94a07fSbrendan const spa_aux_t *sb = b; 923fa94a07fSbrendan 924fa94a07fSbrendan if (sa->aux_guid < sb->aux_guid) 925fa94a07fSbrendan return (-1); 926fa94a07fSbrendan else if (sa->aux_guid > sb->aux_guid) 927fa94a07fSbrendan return (1); 928fa94a07fSbrendan else 929fa94a07fSbrendan return (0); 930fa94a07fSbrendan } 931fa94a07fSbrendan 932fa94a07fSbrendan void 933fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl) 934fa94a07fSbrendan { 935fa94a07fSbrendan avl_index_t where; 936fa94a07fSbrendan spa_aux_t search; 937fa94a07fSbrendan spa_aux_t *aux; 938fa94a07fSbrendan 939fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 940fa94a07fSbrendan if ((aux = avl_find(avl, &search, &where)) != NULL) { 941fa94a07fSbrendan aux->aux_count++; 942fa94a07fSbrendan } else { 943fa94a07fSbrendan aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 944fa94a07fSbrendan aux->aux_guid = vd->vdev_guid; 945fa94a07fSbrendan aux->aux_count = 1; 946fa94a07fSbrendan avl_insert(avl, aux, where); 947fa94a07fSbrendan } 948fa94a07fSbrendan } 949fa94a07fSbrendan 950fa94a07fSbrendan void 951fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 952fa94a07fSbrendan { 953fa94a07fSbrendan spa_aux_t search; 954fa94a07fSbrendan spa_aux_t *aux; 955fa94a07fSbrendan avl_index_t where; 956fa94a07fSbrendan 957fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 958fa94a07fSbrendan aux = avl_find(avl, &search, &where); 959fa94a07fSbrendan 960fa94a07fSbrendan ASSERT(aux != NULL); 961fa94a07fSbrendan 962fa94a07fSbrendan if (--aux->aux_count == 0) { 963fa94a07fSbrendan avl_remove(avl, aux); 964fa94a07fSbrendan kmem_free(aux, sizeof (spa_aux_t)); 965fa94a07fSbrendan } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 966fa94a07fSbrendan aux->aux_pool = 0ULL; 967fa94a07fSbrendan } 968fa94a07fSbrendan } 969fa94a07fSbrendan 970fa94a07fSbrendan boolean_t 97189a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 972fa94a07fSbrendan { 973fa94a07fSbrendan spa_aux_t search, *found; 974fa94a07fSbrendan 975fa94a07fSbrendan search.aux_guid = guid; 97689a89ebfSlling found = avl_find(avl, &search, NULL); 977fa94a07fSbrendan 978fa94a07fSbrendan if (pool) { 979fa94a07fSbrendan if (found) 980fa94a07fSbrendan *pool = found->aux_pool; 981fa94a07fSbrendan else 982fa94a07fSbrendan *pool = 0ULL; 983fa94a07fSbrendan } 984fa94a07fSbrendan 98589a89ebfSlling if (refcnt) { 98689a89ebfSlling if (found) 98789a89ebfSlling *refcnt = found->aux_count; 98889a89ebfSlling else 98989a89ebfSlling *refcnt = 0; 99089a89ebfSlling } 99189a89ebfSlling 992fa94a07fSbrendan return (found != NULL); 993fa94a07fSbrendan } 994fa94a07fSbrendan 995fa94a07fSbrendan void 996fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 997fa94a07fSbrendan { 998fa94a07fSbrendan spa_aux_t search, *found; 999fa94a07fSbrendan avl_index_t where; 1000fa94a07fSbrendan 1001fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 1002fa94a07fSbrendan found = avl_find(avl, &search, &where); 1003fa94a07fSbrendan ASSERT(found != NULL); 1004fa94a07fSbrendan ASSERT(found->aux_pool == 0ULL); 1005fa94a07fSbrendan 1006fa94a07fSbrendan found->aux_pool = spa_guid(vd->vdev_spa); 1007fa94a07fSbrendan } 1008fa94a07fSbrendan 100999653d4eSeschrock /* 101039c23413Seschrock * Spares are tracked globally due to the following constraints: 101139c23413Seschrock * 101254811da5SToomas Soome * - A spare may be part of multiple pools. 101354811da5SToomas Soome * - A spare may be added to a pool even if it's actively in use within 101439c23413Seschrock * another pool. 101554811da5SToomas Soome * - A spare in use in any pool can only be the source of a replacement if 101639c23413Seschrock * the target is a spare in the same pool. 101739c23413Seschrock * 101839c23413Seschrock * We keep track of all spares on the system through the use of a reference 101939c23413Seschrock * counted AVL tree. When a vdev is added as a spare, or used as a replacement 102039c23413Seschrock * spare, then we bump the reference count in the AVL tree. In addition, we set 102139c23413Seschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or 102239c23413Seschrock * inactive). When a spare is made active (used to replace a device in the 102339c23413Seschrock * pool), we also keep track of which pool its been made a part of. 102439c23413Seschrock * 102539c23413Seschrock * The 'spa_spare_lock' protects the AVL tree. These functions are normally 102639c23413Seschrock * called under the spa_namespace lock as part of vdev reconfiguration. The 102739c23413Seschrock * separate spare lock exists for the status query path, which does not need to 102839c23413Seschrock * be completely consistent with respect to other vdev configuration changes. 102999653d4eSeschrock */ 103039c23413Seschrock 103199653d4eSeschrock static int 103299653d4eSeschrock spa_spare_compare(const void *a, const void *b) 103399653d4eSeschrock { 1034fa94a07fSbrendan return (spa_aux_compare(a, b)); 103599653d4eSeschrock } 103699653d4eSeschrock 103799653d4eSeschrock void 103839c23413Seschrock spa_spare_add(vdev_t *vd) 103999653d4eSeschrock { 104099653d4eSeschrock mutex_enter(&spa_spare_lock); 104139c23413Seschrock ASSERT(!vd->vdev_isspare); 1042fa94a07fSbrendan spa_aux_add(vd, &spa_spare_avl); 104339c23413Seschrock vd->vdev_isspare = B_TRUE; 104499653d4eSeschrock mutex_exit(&spa_spare_lock); 104599653d4eSeschrock } 104699653d4eSeschrock 104799653d4eSeschrock void 104839c23413Seschrock spa_spare_remove(vdev_t *vd) 104999653d4eSeschrock { 105099653d4eSeschrock mutex_enter(&spa_spare_lock); 105139c23413Seschrock ASSERT(vd->vdev_isspare); 1052fa94a07fSbrendan spa_aux_remove(vd, &spa_spare_avl); 105339c23413Seschrock vd->vdev_isspare = B_FALSE; 105499653d4eSeschrock mutex_exit(&spa_spare_lock); 105599653d4eSeschrock } 105699653d4eSeschrock 105799653d4eSeschrock boolean_t 105889a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 105999653d4eSeschrock { 1060fa94a07fSbrendan boolean_t found; 106199653d4eSeschrock 106299653d4eSeschrock mutex_enter(&spa_spare_lock); 106389a89ebfSlling found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 106499653d4eSeschrock mutex_exit(&spa_spare_lock); 106599653d4eSeschrock 1066fa94a07fSbrendan return (found); 106739c23413Seschrock } 106839c23413Seschrock 106939c23413Seschrock void 107039c23413Seschrock spa_spare_activate(vdev_t *vd) 107139c23413Seschrock { 107239c23413Seschrock mutex_enter(&spa_spare_lock); 107339c23413Seschrock ASSERT(vd->vdev_isspare); 1074fa94a07fSbrendan spa_aux_activate(vd, &spa_spare_avl); 1075fa94a07fSbrendan mutex_exit(&spa_spare_lock); 1076fa94a07fSbrendan } 107739c23413Seschrock 1078fa94a07fSbrendan /* 1079fa94a07fSbrendan * Level 2 ARC devices are tracked globally for the same reasons as spares. 1080fa94a07fSbrendan * Cache devices currently only support one pool per cache device, and so 1081fa94a07fSbrendan * for these devices the aux reference count is currently unused beyond 1. 1082fa94a07fSbrendan */ 108339c23413Seschrock 1084fa94a07fSbrendan static int 1085fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b) 1086fa94a07fSbrendan { 1087fa94a07fSbrendan return (spa_aux_compare(a, b)); 1088fa94a07fSbrendan } 1089fa94a07fSbrendan 1090fa94a07fSbrendan void 1091fa94a07fSbrendan spa_l2cache_add(vdev_t *vd) 1092fa94a07fSbrendan { 1093fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1094fa94a07fSbrendan ASSERT(!vd->vdev_isl2cache); 1095fa94a07fSbrendan spa_aux_add(vd, &spa_l2cache_avl); 1096fa94a07fSbrendan vd->vdev_isl2cache = B_TRUE; 1097fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1098fa94a07fSbrendan } 1099fa94a07fSbrendan 1100fa94a07fSbrendan void 1101fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd) 1102fa94a07fSbrendan { 1103fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1104fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1105fa94a07fSbrendan spa_aux_remove(vd, &spa_l2cache_avl); 1106fa94a07fSbrendan vd->vdev_isl2cache = B_FALSE; 1107fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1108fa94a07fSbrendan } 1109fa94a07fSbrendan 1110fa94a07fSbrendan boolean_t 1111fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool) 1112fa94a07fSbrendan { 1113fa94a07fSbrendan boolean_t found; 1114fa94a07fSbrendan 1115fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 111689a89ebfSlling found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 1117fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1118fa94a07fSbrendan 1119fa94a07fSbrendan return (found); 1120fa94a07fSbrendan } 1121fa94a07fSbrendan 1122fa94a07fSbrendan void 1123fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd) 1124fa94a07fSbrendan { 1125fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1126fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1127fa94a07fSbrendan spa_aux_activate(vd, &spa_l2cache_avl); 1128fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1129fa94a07fSbrendan } 1130fa94a07fSbrendan 1131fa9e4066Sahrens /* 1132fa9e4066Sahrens * ========================================================================== 1133fa9e4066Sahrens * SPA vdev locking 1134fa9e4066Sahrens * ========================================================================== 1135fa9e4066Sahrens */ 1136fa9e4066Sahrens 1137fa9e4066Sahrens /* 1138ea8dc4b6Seschrock * Lock the given spa_t for the purpose of adding or removing a vdev. 1139ea8dc4b6Seschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing. 1140fa9e4066Sahrens * It returns the next transaction group for the spa_t. 1141fa9e4066Sahrens */ 1142fa9e4066Sahrens uint64_t 1143fa9e4066Sahrens spa_vdev_enter(spa_t *spa) 1144fa9e4066Sahrens { 1145a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 1146bbfd46c4SJeff Bonwick mutex_enter(&spa_namespace_lock); 114788ecc943SGeorge Wilson return (spa_vdev_config_enter(spa)); 114888ecc943SGeorge Wilson } 114988ecc943SGeorge Wilson 115088ecc943SGeorge Wilson /* 115188ecc943SGeorge Wilson * Internal implementation for spa_vdev_enter(). Used when a vdev 115288ecc943SGeorge Wilson * operation requires multiple syncs (i.e. removing a device) while 115388ecc943SGeorge Wilson * keeping the spa_namespace_lock held. 115488ecc943SGeorge Wilson */ 115588ecc943SGeorge Wilson uint64_t 115688ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa) 115788ecc943SGeorge Wilson { 115888ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 11593d7072f8Seschrock 1160e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1161fa9e4066Sahrens 1162fa9e4066Sahrens return (spa_last_synced_txg(spa) + 1); 1163fa9e4066Sahrens } 1164fa9e4066Sahrens 1165fa9e4066Sahrens /* 116688ecc943SGeorge Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing 116788ecc943SGeorge Wilson * of multiple transactions without releasing the spa_namespace_lock. 1168fa9e4066Sahrens */ 116988ecc943SGeorge Wilson void 117088ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 1171fa9e4066Sahrens { 117288ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 117388ecc943SGeorge Wilson 11740e34b6a7Sbonwick int config_changed = B_FALSE; 1175ea8dc4b6Seschrock 11760373e76bSbonwick ASSERT(txg > spa_last_synced_txg(spa)); 11770e34b6a7Sbonwick 1178e14bb325SJeff Bonwick spa->spa_pending_vdev = NULL; 1179e14bb325SJeff Bonwick 11800e34b6a7Sbonwick /* 11810e34b6a7Sbonwick * Reassess the DTLs. 11820e34b6a7Sbonwick */ 11830373e76bSbonwick vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 11840e34b6a7Sbonwick 1185e14bb325SJeff Bonwick if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 11860e34b6a7Sbonwick config_changed = B_TRUE; 11878f18d1faSGeorge Wilson spa->spa_config_generation++; 11880e34b6a7Sbonwick } 1189ea8dc4b6Seschrock 119088ecc943SGeorge Wilson /* 119188ecc943SGeorge Wilson * Verify the metaslab classes. 119288ecc943SGeorge Wilson */ 1193b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1194b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 1195*663207adSDon Brady ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0); 1196*663207adSDon Brady ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0); 119788ecc943SGeorge Wilson 1198e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1199fa9e4066Sahrens 120088ecc943SGeorge Wilson /* 120188ecc943SGeorge Wilson * Panic the system if the specified tag requires it. This 120288ecc943SGeorge Wilson * is useful for ensuring that configurations are updated 120388ecc943SGeorge Wilson * transactionally. 120488ecc943SGeorge Wilson */ 120588ecc943SGeorge Wilson if (zio_injection_enabled) 12061195e687SMark J Musante zio_handle_panic_injection(spa, tag, 0); 120788ecc943SGeorge Wilson 1208fa9e4066Sahrens /* 1209fa9e4066Sahrens * Note: this txg_wait_synced() is important because it ensures 1210fa9e4066Sahrens * that there won't be more than one config change per txg. 1211fa9e4066Sahrens * This allows us to use the txg as the generation number. 1212fa9e4066Sahrens */ 1213fa9e4066Sahrens if (error == 0) 1214fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1215fa9e4066Sahrens 1216fa9e4066Sahrens if (vd != NULL) { 12170713e232SGeorge Wilson ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL); 1218094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 1219094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 1220094e47e9SGeorge Wilson vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED); 1221094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 1222094e47e9SGeorge Wilson } 1223094e47e9SGeorge Wilson 12248ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1225fa9e4066Sahrens vdev_free(vd); 12268ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1227fa9e4066Sahrens } 1228fa9e4066Sahrens 1229fa9e4066Sahrens /* 12300e34b6a7Sbonwick * If the config changed, update the config cache. 1231fa9e4066Sahrens */ 12320e34b6a7Sbonwick if (config_changed) 12335cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 123488ecc943SGeorge Wilson } 1235ea8dc4b6Seschrock 123688ecc943SGeorge Wilson /* 123788ecc943SGeorge Wilson * Unlock the spa_t after adding or removing a vdev. Besides undoing the 123888ecc943SGeorge Wilson * locking of spa_vdev_enter(), we also want make sure the transactions have 123988ecc943SGeorge Wilson * synced to disk, and then update the global configuration cache with the new 124088ecc943SGeorge Wilson * information. 124188ecc943SGeorge Wilson */ 124288ecc943SGeorge Wilson int 124388ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 124488ecc943SGeorge Wilson { 124588ecc943SGeorge Wilson spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1246ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1247bbfd46c4SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 1248fa9e4066Sahrens 1249fa9e4066Sahrens return (error); 1250fa9e4066Sahrens } 1251fa9e4066Sahrens 1252e14bb325SJeff Bonwick /* 1253e14bb325SJeff Bonwick * Lock the given spa_t for the purpose of changing vdev state. 1254e14bb325SJeff Bonwick */ 1255e14bb325SJeff Bonwick void 12568f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks) 1257e14bb325SJeff Bonwick { 12588f18d1faSGeorge Wilson int locks = SCL_STATE_ALL | oplocks; 12598f18d1faSGeorge Wilson 1260dcba9f3fSGeorge Wilson /* 1261dcba9f3fSGeorge Wilson * Root pools may need to read of the underlying devfs filesystem 1262dcba9f3fSGeorge Wilson * when opening up a vdev. Unfortunately if we're holding the 1263dcba9f3fSGeorge Wilson * SCL_ZIO lock it will result in a deadlock when we try to issue 1264dcba9f3fSGeorge Wilson * the read from the root filesystem. Instead we "prefetch" 1265dcba9f3fSGeorge Wilson * the associated vnodes that we need prior to opening the 1266dcba9f3fSGeorge Wilson * underlying devices and cache them so that we can prevent 1267dcba9f3fSGeorge Wilson * any I/O when we are doing the actual open. 1268dcba9f3fSGeorge Wilson */ 1269dcba9f3fSGeorge Wilson if (spa_is_root(spa)) { 12709842588bSGeorge Wilson int low = locks & ~(SCL_ZIO - 1); 12719842588bSGeorge Wilson int high = locks & ~low; 12729842588bSGeorge Wilson 12739842588bSGeorge Wilson spa_config_enter(spa, high, spa, RW_WRITER); 1274dcba9f3fSGeorge Wilson vdev_hold(spa->spa_root_vdev); 12759842588bSGeorge Wilson spa_config_enter(spa, low, spa, RW_WRITER); 1276dcba9f3fSGeorge Wilson } else { 1277dcba9f3fSGeorge Wilson spa_config_enter(spa, locks, spa, RW_WRITER); 1278dcba9f3fSGeorge Wilson } 12798f18d1faSGeorge Wilson spa->spa_vdev_locks = locks; 1280e14bb325SJeff Bonwick } 1281e14bb325SJeff Bonwick 1282e14bb325SJeff Bonwick int 1283e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1284e14bb325SJeff Bonwick { 1285c6065d0fSGeorge Wilson boolean_t config_changed = B_FALSE; 1286c6065d0fSGeorge Wilson 1287b24ab676SJeff Bonwick if (vd != NULL || error == 0) 1288b24ab676SJeff Bonwick vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1289b24ab676SJeff Bonwick 0, 0, B_FALSE); 1290b24ab676SJeff Bonwick 12918f18d1faSGeorge Wilson if (vd != NULL) { 1292e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 1293c6065d0fSGeorge Wilson config_changed = B_TRUE; 12948f18d1faSGeorge Wilson spa->spa_config_generation++; 12958f18d1faSGeorge Wilson } 1296e14bb325SJeff Bonwick 1297dcba9f3fSGeorge Wilson if (spa_is_root(spa)) 1298dcba9f3fSGeorge Wilson vdev_rele(spa->spa_root_vdev); 1299dcba9f3fSGeorge Wilson 13008f18d1faSGeorge Wilson ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 13018f18d1faSGeorge Wilson spa_config_exit(spa, spa->spa_vdev_locks, spa); 1302e14bb325SJeff Bonwick 13038ad4d6ddSJeff Bonwick /* 13048ad4d6ddSJeff Bonwick * If anything changed, wait for it to sync. This ensures that, 13058ad4d6ddSJeff Bonwick * from the system administrator's perspective, zpool(1M) commands 13068ad4d6ddSJeff Bonwick * are synchronous. This is important for things like zpool offline: 13078ad4d6ddSJeff Bonwick * when the command completes, you expect no further I/O from ZFS. 13088ad4d6ddSJeff Bonwick */ 13098ad4d6ddSJeff Bonwick if (vd != NULL) 13108ad4d6ddSJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, 0); 13118ad4d6ddSJeff Bonwick 1312c6065d0fSGeorge Wilson /* 1313c6065d0fSGeorge Wilson * If the config changed, update the config cache. 1314c6065d0fSGeorge Wilson */ 1315c6065d0fSGeorge Wilson if (config_changed) { 1316c6065d0fSGeorge Wilson mutex_enter(&spa_namespace_lock); 13175cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 1318c6065d0fSGeorge Wilson mutex_exit(&spa_namespace_lock); 1319c6065d0fSGeorge Wilson } 1320c6065d0fSGeorge Wilson 1321e14bb325SJeff Bonwick return (error); 1322e14bb325SJeff Bonwick } 1323e14bb325SJeff Bonwick 1324fa9e4066Sahrens /* 1325fa9e4066Sahrens * ========================================================================== 1326fa9e4066Sahrens * Miscellaneous functions 1327fa9e4066Sahrens * ========================================================================== 1328fa9e4066Sahrens */ 1329fa9e4066Sahrens 1330ad135b5dSChristopher Siden void 133143466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx) 1332ad135b5dSChristopher Siden { 13332acef22dSMatthew Ahrens if (!nvlist_exists(spa->spa_label_features, feature)) { 13342acef22dSMatthew Ahrens fnvlist_add_boolean(spa->spa_label_features, feature); 133543466aaeSMax Grossman /* 133643466aaeSMax Grossman * When we are creating the pool (tx_txg==TXG_INITIAL), we can't 133743466aaeSMax Grossman * dirty the vdev config because lock SCL_CONFIG is not held. 133843466aaeSMax Grossman * Thankfully, in this case we don't need to dirty the config 133943466aaeSMax Grossman * because it will be written out anyway when we finish 134043466aaeSMax Grossman * creating the pool. 134143466aaeSMax Grossman */ 134243466aaeSMax Grossman if (tx->tx_txg != TXG_INITIAL) 134343466aaeSMax Grossman vdev_config_dirty(spa->spa_root_vdev); 13442acef22dSMatthew Ahrens } 1345ad135b5dSChristopher Siden } 1346ad135b5dSChristopher Siden 1347ad135b5dSChristopher Siden void 1348ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1349ad135b5dSChristopher Siden { 13502acef22dSMatthew Ahrens if (nvlist_remove_all(spa->spa_label_features, feature) == 0) 13512acef22dSMatthew Ahrens vdev_config_dirty(spa->spa_root_vdev); 1352ad135b5dSChristopher Siden } 1353ad135b5dSChristopher Siden 1354fa9e4066Sahrens /* 1355f9af39baSGeorge Wilson * Return the spa_t associated with given pool_guid, if it exists. If 1356f9af39baSGeorge Wilson * device_guid is non-zero, determine whether the pool exists *and* contains 1357f9af39baSGeorge Wilson * a device with the specified device_guid. 1358fa9e4066Sahrens */ 1359f9af39baSGeorge Wilson spa_t * 1360f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1361fa9e4066Sahrens { 1362fa9e4066Sahrens spa_t *spa; 1363fa9e4066Sahrens avl_tree_t *t = &spa_namespace_avl; 1364fa9e4066Sahrens 1365ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1366fa9e4066Sahrens 1367fa9e4066Sahrens for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1368fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1369fa9e4066Sahrens continue; 1370fa9e4066Sahrens if (spa->spa_root_vdev == NULL) 1371fa9e4066Sahrens continue; 137239c23413Seschrock if (spa_guid(spa) == pool_guid) { 137339c23413Seschrock if (device_guid == 0) 137439c23413Seschrock break; 137539c23413Seschrock 137639c23413Seschrock if (vdev_lookup_by_guid(spa->spa_root_vdev, 137739c23413Seschrock device_guid) != NULL) 137839c23413Seschrock break; 137939c23413Seschrock 138039c23413Seschrock /* 13818654d025Sperrin * Check any devices we may be in the process of adding. 138239c23413Seschrock */ 138339c23413Seschrock if (spa->spa_pending_vdev) { 138439c23413Seschrock if (vdev_lookup_by_guid(spa->spa_pending_vdev, 138539c23413Seschrock device_guid) != NULL) 138639c23413Seschrock break; 138739c23413Seschrock } 138839c23413Seschrock } 1389fa9e4066Sahrens } 1390fa9e4066Sahrens 1391f9af39baSGeorge Wilson return (spa); 1392f9af39baSGeorge Wilson } 1393f9af39baSGeorge Wilson 1394f9af39baSGeorge Wilson /* 1395f9af39baSGeorge Wilson * Determine whether a pool with the given pool_guid exists. 1396f9af39baSGeorge Wilson */ 1397f9af39baSGeorge Wilson boolean_t 1398f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1399f9af39baSGeorge Wilson { 1400f9af39baSGeorge Wilson return (spa_by_guid(pool_guid, device_guid) != NULL); 1401fa9e4066Sahrens } 1402fa9e4066Sahrens 1403fa9e4066Sahrens char * 1404fa9e4066Sahrens spa_strdup(const char *s) 1405fa9e4066Sahrens { 1406fa9e4066Sahrens size_t len; 1407fa9e4066Sahrens char *new; 1408fa9e4066Sahrens 1409fa9e4066Sahrens len = strlen(s); 1410fa9e4066Sahrens new = kmem_alloc(len + 1, KM_SLEEP); 1411fa9e4066Sahrens bcopy(s, new, len); 1412fa9e4066Sahrens new[len] = '\0'; 1413fa9e4066Sahrens 1414fa9e4066Sahrens return (new); 1415fa9e4066Sahrens } 1416fa9e4066Sahrens 1417fa9e4066Sahrens void 1418fa9e4066Sahrens spa_strfree(char *s) 1419fa9e4066Sahrens { 1420fa9e4066Sahrens kmem_free(s, strlen(s) + 1); 1421fa9e4066Sahrens } 1422fa9e4066Sahrens 1423fa9e4066Sahrens uint64_t 1424fa9e4066Sahrens spa_get_random(uint64_t range) 1425fa9e4066Sahrens { 1426fa9e4066Sahrens uint64_t r; 1427fa9e4066Sahrens 1428fa9e4066Sahrens ASSERT(range != 0); 1429fa9e4066Sahrens 1430e0f1c0afSOlaf Faaland if (range == 1) 1431e0f1c0afSOlaf Faaland return (0); 1432e0f1c0afSOlaf Faaland 1433fa9e4066Sahrens (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1434fa9e4066Sahrens 1435fa9e4066Sahrens return (r % range); 1436fa9e4066Sahrens } 1437fa9e4066Sahrens 14381195e687SMark J Musante uint64_t 14391195e687SMark J Musante spa_generate_guid(spa_t *spa) 14401195e687SMark J Musante { 14411195e687SMark J Musante uint64_t guid = spa_get_random(-1ULL); 14421195e687SMark J Musante 14431195e687SMark J Musante if (spa != NULL) { 14441195e687SMark J Musante while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 14451195e687SMark J Musante guid = spa_get_random(-1ULL); 14461195e687SMark J Musante } else { 14471195e687SMark J Musante while (guid == 0 || spa_guid_exists(guid, 0)) 14481195e687SMark J Musante guid = spa_get_random(-1ULL); 14491195e687SMark J Musante } 14501195e687SMark J Musante 14511195e687SMark J Musante return (guid); 14521195e687SMark J Musante } 14531195e687SMark J Musante 1454fa9e4066Sahrens void 145543466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp) 1456fa9e4066Sahrens { 1457ad135b5dSChristopher Siden char type[256]; 1458f0ba89beSJeff Bonwick char *checksum = NULL; 1459f0ba89beSJeff Bonwick char *compress = NULL; 1460f0ba89beSJeff Bonwick 1461f0ba89beSJeff Bonwick if (bp != NULL) { 1462ad135b5dSChristopher Siden if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1463ad135b5dSChristopher Siden dmu_object_byteswap_t bswap = 1464ad135b5dSChristopher Siden DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1465ad135b5dSChristopher Siden (void) snprintf(type, sizeof (type), "bswap %s %s", 1466ad135b5dSChristopher Siden DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1467ad135b5dSChristopher Siden "metadata" : "data", 1468ad135b5dSChristopher Siden dmu_ot_byteswap[bswap].ob_name); 1469ad135b5dSChristopher Siden } else { 1470ad135b5dSChristopher Siden (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1471ad135b5dSChristopher Siden sizeof (type)); 1472ad135b5dSChristopher Siden } 14735d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 14745d7b4d43SMatthew Ahrens checksum = 14755d7b4d43SMatthew Ahrens zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 14765d7b4d43SMatthew Ahrens } 1477f0ba89beSJeff Bonwick compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1478f0ba89beSJeff Bonwick } 1479fa9e4066Sahrens 148043466aaeSMax Grossman SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum, 148143466aaeSMax Grossman compress); 1482fa9e4066Sahrens } 1483fa9e4066Sahrens 1484fa9e4066Sahrens void 1485fa9e4066Sahrens spa_freeze(spa_t *spa) 1486fa9e4066Sahrens { 1487fa9e4066Sahrens uint64_t freeze_txg = 0; 1488fa9e4066Sahrens 1489e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1490fa9e4066Sahrens if (spa->spa_freeze_txg == UINT64_MAX) { 1491fa9e4066Sahrens freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1492fa9e4066Sahrens spa->spa_freeze_txg = freeze_txg; 1493fa9e4066Sahrens } 1494e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1495fa9e4066Sahrens if (freeze_txg != 0) 1496fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1497fa9e4066Sahrens } 1498fa9e4066Sahrens 14990125049cSahrens void 15000125049cSahrens zfs_panic_recover(const char *fmt, ...) 15010125049cSahrens { 15020125049cSahrens va_list adx; 15030125049cSahrens 15040125049cSahrens va_start(adx, fmt); 15050125049cSahrens vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 15060125049cSahrens va_end(adx); 15070125049cSahrens } 15080125049cSahrens 15093f9d6ad7SLin Ling /* 15103f9d6ad7SLin Ling * This is a stripped-down version of strtoull, suitable only for converting 1511f7170741SWill Andrews * lowercase hexadecimal numbers that don't overflow. 15123f9d6ad7SLin Ling */ 15133f9d6ad7SLin Ling uint64_t 15144585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr) 15153f9d6ad7SLin Ling { 15163f9d6ad7SLin Ling uint64_t val = 0; 15173f9d6ad7SLin Ling char c; 15183f9d6ad7SLin Ling int digit; 15193f9d6ad7SLin Ling 15203f9d6ad7SLin Ling while ((c = *str) != '\0') { 15213f9d6ad7SLin Ling if (c >= '0' && c <= '9') 15223f9d6ad7SLin Ling digit = c - '0'; 15233f9d6ad7SLin Ling else if (c >= 'a' && c <= 'f') 15243f9d6ad7SLin Ling digit = 10 + c - 'a'; 15253f9d6ad7SLin Ling else 15263f9d6ad7SLin Ling break; 15273f9d6ad7SLin Ling 15283f9d6ad7SLin Ling val *= 16; 15293f9d6ad7SLin Ling val += digit; 15303f9d6ad7SLin Ling 15313f9d6ad7SLin Ling str++; 15323f9d6ad7SLin Ling } 15333f9d6ad7SLin Ling 15343f9d6ad7SLin Ling if (nptr) 15353f9d6ad7SLin Ling *nptr = (char *)str; 15363f9d6ad7SLin Ling 15373f9d6ad7SLin Ling return (val); 15383f9d6ad7SLin Ling } 15393f9d6ad7SLin Ling 1540*663207adSDon Brady void 1541*663207adSDon Brady spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx) 1542*663207adSDon Brady { 1543*663207adSDon Brady /* 1544*663207adSDon Brady * We bump the feature refcount for each special vdev added to the pool 1545*663207adSDon Brady */ 1546*663207adSDon Brady ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)); 1547*663207adSDon Brady spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx); 1548*663207adSDon Brady } 1549*663207adSDon Brady 1550fa9e4066Sahrens /* 1551fa9e4066Sahrens * ========================================================================== 1552fa9e4066Sahrens * Accessor functions 1553fa9e4066Sahrens * ========================================================================== 1554fa9e4066Sahrens */ 1555fa9e4066Sahrens 1556088f3894Sahrens boolean_t 155788b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa) 1558fa9e4066Sahrens { 155988b7b0f2SMatthew Ahrens return (spa->spa_async_suspended); 1560fa9e4066Sahrens } 1561fa9e4066Sahrens 1562fa9e4066Sahrens dsl_pool_t * 1563fa9e4066Sahrens spa_get_dsl(spa_t *spa) 1564fa9e4066Sahrens { 1565fa9e4066Sahrens return (spa->spa_dsl_pool); 1566fa9e4066Sahrens } 1567fa9e4066Sahrens 1568ad135b5dSChristopher Siden boolean_t 1569ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa) 1570ad135b5dSChristopher Siden { 1571ad135b5dSChristopher Siden return (spa->spa_is_initializing); 1572ad135b5dSChristopher Siden } 1573ad135b5dSChristopher Siden 15745cabbc6bSPrashanth Sreenivasa boolean_t 15755cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa) 15765cabbc6bSPrashanth Sreenivasa { 15775cabbc6bSPrashanth Sreenivasa return (spa->spa_indirect_vdevs_loaded); 15785cabbc6bSPrashanth Sreenivasa } 15795cabbc6bSPrashanth Sreenivasa 1580fa9e4066Sahrens blkptr_t * 1581fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa) 1582fa9e4066Sahrens { 1583fa9e4066Sahrens return (&spa->spa_ubsync.ub_rootbp); 1584fa9e4066Sahrens } 1585fa9e4066Sahrens 1586fa9e4066Sahrens void 1587fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1588fa9e4066Sahrens { 1589fa9e4066Sahrens spa->spa_uberblock.ub_rootbp = *bp; 1590fa9e4066Sahrens } 1591fa9e4066Sahrens 1592fa9e4066Sahrens void 1593fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen) 1594fa9e4066Sahrens { 1595fa9e4066Sahrens if (spa->spa_root == NULL) 1596fa9e4066Sahrens buf[0] = '\0'; 1597fa9e4066Sahrens else 1598fa9e4066Sahrens (void) strncpy(buf, spa->spa_root, buflen); 1599fa9e4066Sahrens } 1600fa9e4066Sahrens 1601fa9e4066Sahrens int 1602fa9e4066Sahrens spa_sync_pass(spa_t *spa) 1603fa9e4066Sahrens { 1604fa9e4066Sahrens return (spa->spa_sync_pass); 1605fa9e4066Sahrens } 1606fa9e4066Sahrens 1607fa9e4066Sahrens char * 1608fa9e4066Sahrens spa_name(spa_t *spa) 1609fa9e4066Sahrens { 1610fa9e4066Sahrens return (spa->spa_name); 1611fa9e4066Sahrens } 1612fa9e4066Sahrens 1613fa9e4066Sahrens uint64_t 1614fa9e4066Sahrens spa_guid(spa_t *spa) 1615fa9e4066Sahrens { 1616dfbb9432SGeorge Wilson dsl_pool_t *dp = spa_get_dsl(spa); 1617dfbb9432SGeorge Wilson uint64_t guid; 1618dfbb9432SGeorge Wilson 1619b5989ec7Seschrock /* 1620b5989ec7Seschrock * If we fail to parse the config during spa_load(), we can go through 1621b5989ec7Seschrock * the error path (which posts an ereport) and end up here with no root 1622e9103aaeSGarrett D'Amore * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1623b5989ec7Seschrock * this case. 1624b5989ec7Seschrock */ 1625dfbb9432SGeorge Wilson if (spa->spa_root_vdev == NULL) 1626dfbb9432SGeorge Wilson return (spa->spa_config_guid); 1627dfbb9432SGeorge Wilson 1628dfbb9432SGeorge Wilson guid = spa->spa_last_synced_guid != 0 ? 1629dfbb9432SGeorge Wilson spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1630dfbb9432SGeorge Wilson 1631dfbb9432SGeorge Wilson /* 1632dfbb9432SGeorge Wilson * Return the most recently synced out guid unless we're 1633dfbb9432SGeorge Wilson * in syncing context. 1634dfbb9432SGeorge Wilson */ 1635dfbb9432SGeorge Wilson if (dp && dsl_pool_sync_context(dp)) 1636b5989ec7Seschrock return (spa->spa_root_vdev->vdev_guid); 1637b5989ec7Seschrock else 1638dfbb9432SGeorge Wilson return (guid); 1639e9103aaeSGarrett D'Amore } 1640e9103aaeSGarrett D'Amore 1641e9103aaeSGarrett D'Amore uint64_t 1642e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa) 1643e9103aaeSGarrett D'Amore { 1644e9103aaeSGarrett D'Amore /* 1645e9103aaeSGarrett D'Amore * This is a GUID that exists solely as a reference for the 1646e9103aaeSGarrett D'Amore * purposes of the arc. It is generated at load time, and 1647e9103aaeSGarrett D'Amore * is never written to persistent storage. 1648e9103aaeSGarrett D'Amore */ 1649e9103aaeSGarrett D'Amore return (spa->spa_load_guid); 1650fa9e4066Sahrens } 1651fa9e4066Sahrens 1652fa9e4066Sahrens uint64_t 1653fa9e4066Sahrens spa_last_synced_txg(spa_t *spa) 1654fa9e4066Sahrens { 1655fa9e4066Sahrens return (spa->spa_ubsync.ub_txg); 1656fa9e4066Sahrens } 1657fa9e4066Sahrens 1658fa9e4066Sahrens uint64_t 1659fa9e4066Sahrens spa_first_txg(spa_t *spa) 1660fa9e4066Sahrens { 1661fa9e4066Sahrens return (spa->spa_first_txg); 1662fa9e4066Sahrens } 1663fa9e4066Sahrens 1664b24ab676SJeff Bonwick uint64_t 1665b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa) 1666b24ab676SJeff Bonwick { 1667b24ab676SJeff Bonwick return (spa->spa_syncing_txg); 1668b24ab676SJeff Bonwick } 1669b24ab676SJeff Bonwick 16703991b535SGeorge Wilson /* 16713991b535SGeorge Wilson * Return the last txg where data can be dirtied. The final txgs 16723991b535SGeorge Wilson * will be used to just clear out any deferred frees that remain. 16733991b535SGeorge Wilson */ 16743991b535SGeorge Wilson uint64_t 16753991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa) 16763991b535SGeorge Wilson { 16773991b535SGeorge Wilson return (spa->spa_final_txg - TXG_DEFER_SIZE); 16783991b535SGeorge Wilson } 16793991b535SGeorge Wilson 168088b7b0f2SMatthew Ahrens pool_state_t 1681fa9e4066Sahrens spa_state(spa_t *spa) 1682fa9e4066Sahrens { 1683fa9e4066Sahrens return (spa->spa_state); 1684fa9e4066Sahrens } 1685fa9e4066Sahrens 1686b16da2e2SGeorge Wilson spa_load_state_t 1687b16da2e2SGeorge Wilson spa_load_state(spa_t *spa) 1688b16da2e2SGeorge Wilson { 1689b16da2e2SGeorge Wilson return (spa->spa_load_state); 1690b16da2e2SGeorge Wilson } 1691b16da2e2SGeorge Wilson 1692fa9e4066Sahrens uint64_t 1693fa9e4066Sahrens spa_freeze_txg(spa_t *spa) 1694fa9e4066Sahrens { 1695fa9e4066Sahrens return (spa->spa_freeze_txg); 1696fa9e4066Sahrens } 1697fa9e4066Sahrens 1698fa9e4066Sahrens /* ARGSUSED */ 1699fa9e4066Sahrens uint64_t 170061e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize) 1701fa9e4066Sahrens { 170269962b56SMatthew Ahrens return (lsize * spa_asize_inflation); 170344cd46caSbillm } 170444cd46caSbillm 17057d46dc6cSMatthew Ahrens /* 17067d46dc6cSMatthew Ahrens * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%), 17074b5c8e93SMatthew Ahrens * or at least 128MB, unless that would cause it to be more than half the 17084b5c8e93SMatthew Ahrens * pool size. 17097d46dc6cSMatthew Ahrens * 17107d46dc6cSMatthew Ahrens * See the comment above spa_slop_shift for details. 17117d46dc6cSMatthew Ahrens */ 17127d46dc6cSMatthew Ahrens uint64_t 17134b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa) 17144b5c8e93SMatthew Ahrens { 17157d46dc6cSMatthew Ahrens uint64_t space = spa_get_dspace(spa); 17164b5c8e93SMatthew Ahrens return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop))); 17177d46dc6cSMatthew Ahrens } 17187d46dc6cSMatthew Ahrens 1719485bbbf5SGeorge Wilson uint64_t 1720485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa) 1721485bbbf5SGeorge Wilson { 1722485bbbf5SGeorge Wilson return (spa->spa_dspace); 1723485bbbf5SGeorge Wilson } 1724485bbbf5SGeorge Wilson 172586714001SSerapheim Dimitropoulos uint64_t 172686714001SSerapheim Dimitropoulos spa_get_checkpoint_space(spa_t *spa) 172786714001SSerapheim Dimitropoulos { 172886714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_info.sci_dspace); 172986714001SSerapheim Dimitropoulos } 173086714001SSerapheim Dimitropoulos 1731485bbbf5SGeorge Wilson void 1732485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa) 1733485bbbf5SGeorge Wilson { 1734485bbbf5SGeorge Wilson spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1735485bbbf5SGeorge Wilson ddt_get_dedup_dspace(spa); 17365cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 17375cabbc6bSPrashanth Sreenivasa /* 17385cabbc6bSPrashanth Sreenivasa * We can't allocate from the removing device, so 17395cabbc6bSPrashanth Sreenivasa * subtract its size. This prevents the DMU/DSL from 17405cabbc6bSPrashanth Sreenivasa * filling up the (now smaller) pool while we are in the 17415cabbc6bSPrashanth Sreenivasa * middle of removing the device. 17425cabbc6bSPrashanth Sreenivasa * 17435cabbc6bSPrashanth Sreenivasa * Note that the DMU/DSL doesn't actually know or care 17445cabbc6bSPrashanth Sreenivasa * how much space is allocated (it does its own tracking 17455cabbc6bSPrashanth Sreenivasa * of how much space has been logically used). So it 17465cabbc6bSPrashanth Sreenivasa * doesn't matter that the data we are moving may be 17475cabbc6bSPrashanth Sreenivasa * allocated twice (on the old device and the new 17485cabbc6bSPrashanth Sreenivasa * device). 17495cabbc6bSPrashanth Sreenivasa */ 17503a4b1be9SMatthew Ahrens spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 17513a4b1be9SMatthew Ahrens vdev_t *vd = 17523a4b1be9SMatthew Ahrens vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id); 17535cabbc6bSPrashanth Sreenivasa spa->spa_dspace -= spa_deflate(spa) ? 17545cabbc6bSPrashanth Sreenivasa vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 17553a4b1be9SMatthew Ahrens spa_config_exit(spa, SCL_VDEV, FTAG); 17565cabbc6bSPrashanth Sreenivasa } 1757485bbbf5SGeorge Wilson } 1758485bbbf5SGeorge Wilson 17590a4e9518Sgw /* 17600a4e9518Sgw * Return the failure mode that has been set to this pool. The default 17610a4e9518Sgw * behavior will be to block all I/Os when a complete failure occurs. 17620a4e9518Sgw */ 17630a4e9518Sgw uint8_t 17640a4e9518Sgw spa_get_failmode(spa_t *spa) 17650a4e9518Sgw { 17660a4e9518Sgw return (spa->spa_failmode); 17670a4e9518Sgw } 17680a4e9518Sgw 1769e14bb325SJeff Bonwick boolean_t 1770e14bb325SJeff Bonwick spa_suspended(spa_t *spa) 1771e14bb325SJeff Bonwick { 1772e0f1c0afSOlaf Faaland return (spa->spa_suspended != ZIO_SUSPEND_NONE); 1773e14bb325SJeff Bonwick } 1774e14bb325SJeff Bonwick 177544cd46caSbillm uint64_t 177644cd46caSbillm spa_version(spa_t *spa) 177744cd46caSbillm { 177844cd46caSbillm return (spa->spa_ubsync.ub_version); 177944cd46caSbillm } 178044cd46caSbillm 1781b24ab676SJeff Bonwick boolean_t 1782b24ab676SJeff Bonwick spa_deflate(spa_t *spa) 1783b24ab676SJeff Bonwick { 1784b24ab676SJeff Bonwick return (spa->spa_deflate); 1785b24ab676SJeff Bonwick } 1786b24ab676SJeff Bonwick 1787b24ab676SJeff Bonwick metaslab_class_t * 1788b24ab676SJeff Bonwick spa_normal_class(spa_t *spa) 1789b24ab676SJeff Bonwick { 1790b24ab676SJeff Bonwick return (spa->spa_normal_class); 1791b24ab676SJeff Bonwick } 1792b24ab676SJeff Bonwick 1793b24ab676SJeff Bonwick metaslab_class_t * 1794b24ab676SJeff Bonwick spa_log_class(spa_t *spa) 1795b24ab676SJeff Bonwick { 1796b24ab676SJeff Bonwick return (spa->spa_log_class); 1797b24ab676SJeff Bonwick } 1798b24ab676SJeff Bonwick 1799*663207adSDon Brady metaslab_class_t * 1800*663207adSDon Brady spa_special_class(spa_t *spa) 1801*663207adSDon Brady { 1802*663207adSDon Brady return (spa->spa_special_class); 1803*663207adSDon Brady } 1804*663207adSDon Brady 1805*663207adSDon Brady metaslab_class_t * 1806*663207adSDon Brady spa_dedup_class(spa_t *spa) 1807*663207adSDon Brady { 1808*663207adSDon Brady return (spa->spa_dedup_class); 1809*663207adSDon Brady } 1810*663207adSDon Brady 1811*663207adSDon Brady /* 1812*663207adSDon Brady * Locate an appropriate allocation class 1813*663207adSDon Brady */ 1814*663207adSDon Brady metaslab_class_t * 1815*663207adSDon Brady spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype, 1816*663207adSDon Brady uint_t level, uint_t special_smallblk) 1817*663207adSDon Brady { 1818*663207adSDon Brady if (DMU_OT_IS_ZIL(objtype)) { 1819*663207adSDon Brady if (spa->spa_log_class->mc_groups != 0) 1820*663207adSDon Brady return (spa_log_class(spa)); 1821*663207adSDon Brady else 1822*663207adSDon Brady return (spa_normal_class(spa)); 1823*663207adSDon Brady } 1824*663207adSDon Brady 1825*663207adSDon Brady boolean_t has_special_class = spa->spa_special_class->mc_groups != 0; 1826*663207adSDon Brady 1827*663207adSDon Brady if (DMU_OT_IS_DDT(objtype)) { 1828*663207adSDon Brady if (spa->spa_dedup_class->mc_groups != 0) 1829*663207adSDon Brady return (spa_dedup_class(spa)); 1830*663207adSDon Brady else if (has_special_class && zfs_ddt_data_is_special) 1831*663207adSDon Brady return (spa_special_class(spa)); 1832*663207adSDon Brady else 1833*663207adSDon Brady return (spa_normal_class(spa)); 1834*663207adSDon Brady } 1835*663207adSDon Brady 1836*663207adSDon Brady /* Indirect blocks for user data can land in special if allowed */ 1837*663207adSDon Brady if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) { 1838*663207adSDon Brady if (has_special_class && zfs_user_indirect_is_special) 1839*663207adSDon Brady return (spa_special_class(spa)); 1840*663207adSDon Brady else 1841*663207adSDon Brady return (spa_normal_class(spa)); 1842*663207adSDon Brady } 1843*663207adSDon Brady 1844*663207adSDon Brady if (DMU_OT_IS_METADATA(objtype) || level > 0) { 1845*663207adSDon Brady if (has_special_class) 1846*663207adSDon Brady return (spa_special_class(spa)); 1847*663207adSDon Brady else 1848*663207adSDon Brady return (spa_normal_class(spa)); 1849*663207adSDon Brady } 1850*663207adSDon Brady 1851*663207adSDon Brady /* 1852*663207adSDon Brady * Allow small file blocks in special class in some cases (like 1853*663207adSDon Brady * for the dRAID vdev feature). But always leave a reserve of 1854*663207adSDon Brady * zfs_special_class_metadata_reserve_pct exclusively for metadata. 1855*663207adSDon Brady */ 1856*663207adSDon Brady if (DMU_OT_IS_FILE(objtype) && 1857*663207adSDon Brady has_special_class && size <= special_smallblk) { 1858*663207adSDon Brady metaslab_class_t *special = spa_special_class(spa); 1859*663207adSDon Brady uint64_t alloc = metaslab_class_get_alloc(special); 1860*663207adSDon Brady uint64_t space = metaslab_class_get_space(special); 1861*663207adSDon Brady uint64_t limit = 1862*663207adSDon Brady (space * (100 - zfs_special_class_metadata_reserve_pct)) 1863*663207adSDon Brady / 100; 1864*663207adSDon Brady 1865*663207adSDon Brady if (alloc < limit) 1866*663207adSDon Brady return (special); 1867*663207adSDon Brady } 1868*663207adSDon Brady 1869*663207adSDon Brady return (spa_normal_class(spa)); 1870*663207adSDon Brady } 1871*663207adSDon Brady 1872bc9014e6SJustin Gibbs void 1873bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os) 1874bc9014e6SJustin Gibbs { 1875bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1876bc9014e6SJustin Gibbs list_insert_head(&spa->spa_evicting_os_list, os); 1877bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1878bc9014e6SJustin Gibbs } 1879bc9014e6SJustin Gibbs 1880bc9014e6SJustin Gibbs void 1881bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os) 1882bc9014e6SJustin Gibbs { 1883bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1884bc9014e6SJustin Gibbs list_remove(&spa->spa_evicting_os_list, os); 1885bc9014e6SJustin Gibbs cv_broadcast(&spa->spa_evicting_os_cv); 1886bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1887bc9014e6SJustin Gibbs } 1888bc9014e6SJustin Gibbs 1889bc9014e6SJustin Gibbs void 1890bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa) 1891bc9014e6SJustin Gibbs { 1892bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1893bc9014e6SJustin Gibbs while (!list_is_empty(&spa->spa_evicting_os_list)) 1894bc9014e6SJustin Gibbs cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock); 1895bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1896bc9014e6SJustin Gibbs 1897bc9014e6SJustin Gibbs dmu_buf_user_evict_wait(); 1898bc9014e6SJustin Gibbs } 1899bc9014e6SJustin Gibbs 190044cd46caSbillm int 190144cd46caSbillm spa_max_replication(spa_t *spa) 190244cd46caSbillm { 190344cd46caSbillm /* 1904e7437265Sahrens * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 190544cd46caSbillm * handle BPs with more than one DVA allocated. Set our max 190644cd46caSbillm * replication level accordingly. 1907fa9e4066Sahrens */ 1908e7437265Sahrens if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 190944cd46caSbillm return (1); 191044cd46caSbillm return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1911fa9e4066Sahrens } 1912fa9e4066Sahrens 19133f9d6ad7SLin Ling int 19143f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa) 19153f9d6ad7SLin Ling { 19163f9d6ad7SLin Ling return (spa->spa_prev_software_version); 19173f9d6ad7SLin Ling } 19183f9d6ad7SLin Ling 1919283b8460SGeorge.Wilson uint64_t 1920283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa) 1921283b8460SGeorge.Wilson { 1922283b8460SGeorge.Wilson return (spa->spa_deadman_synctime); 1923283b8460SGeorge.Wilson } 1924283b8460SGeorge.Wilson 192599653d4eSeschrock uint64_t 1926b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 192799653d4eSeschrock { 1928b24ab676SJeff Bonwick uint64_t asize = DVA_GET_ASIZE(dva); 1929b24ab676SJeff Bonwick uint64_t dsize = asize; 193099653d4eSeschrock 1931b24ab676SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 193299653d4eSeschrock 1933b24ab676SJeff Bonwick if (asize != 0 && spa->spa_deflate) { 1934b24ab676SJeff Bonwick vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 1935b24ab676SJeff Bonwick dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 193699653d4eSeschrock } 1937b24ab676SJeff Bonwick 1938b24ab676SJeff Bonwick return (dsize); 1939b24ab676SJeff Bonwick } 1940b24ab676SJeff Bonwick 1941b24ab676SJeff Bonwick uint64_t 1942b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 1943b24ab676SJeff Bonwick { 1944b24ab676SJeff Bonwick uint64_t dsize = 0; 1945b24ab676SJeff Bonwick 19465d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1947b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1948b24ab676SJeff Bonwick 1949b24ab676SJeff Bonwick return (dsize); 1950b24ab676SJeff Bonwick } 1951b24ab676SJeff Bonwick 1952b24ab676SJeff Bonwick uint64_t 1953b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp) 1954b24ab676SJeff Bonwick { 1955b24ab676SJeff Bonwick uint64_t dsize = 0; 1956b24ab676SJeff Bonwick 1957b24ab676SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1958b24ab676SJeff Bonwick 19595d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1960b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1961b24ab676SJeff Bonwick 1962e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 1963b24ab676SJeff Bonwick 1964b24ab676SJeff Bonwick return (dsize); 196599653d4eSeschrock } 196699653d4eSeschrock 1967abe1fd01SDon Brady uint64_t 1968abe1fd01SDon Brady spa_dirty_data(spa_t *spa) 1969abe1fd01SDon Brady { 1970abe1fd01SDon Brady return (spa->spa_dsl_pool->dp_dirty_total); 1971abe1fd01SDon Brady } 1972abe1fd01SDon Brady 1973fa9e4066Sahrens /* 1974fa9e4066Sahrens * ========================================================================== 1975fa9e4066Sahrens * Initialization and Termination 1976fa9e4066Sahrens * ========================================================================== 1977fa9e4066Sahrens */ 1978fa9e4066Sahrens 1979fa9e4066Sahrens static int 1980fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2) 1981fa9e4066Sahrens { 1982fa9e4066Sahrens const spa_t *s1 = a1; 1983fa9e4066Sahrens const spa_t *s2 = a2; 1984fa9e4066Sahrens int s; 1985fa9e4066Sahrens 1986fa9e4066Sahrens s = strcmp(s1->spa_name, s2->spa_name); 1987fa9e4066Sahrens if (s > 0) 1988fa9e4066Sahrens return (1); 1989fa9e4066Sahrens if (s < 0) 1990fa9e4066Sahrens return (-1); 1991fa9e4066Sahrens return (0); 1992fa9e4066Sahrens } 1993fa9e4066Sahrens 19940373e76bSbonwick int 19950373e76bSbonwick spa_busy(void) 19960373e76bSbonwick { 19970373e76bSbonwick return (spa_active_count); 19980373e76bSbonwick } 19990373e76bSbonwick 2000e7cbe64fSgw void 2001e7cbe64fSgw spa_boot_init() 2002e7cbe64fSgw { 2003e7cbe64fSgw spa_config_load(); 2004e7cbe64fSgw } 2005e7cbe64fSgw 2006fa9e4066Sahrens void 2007fa9e4066Sahrens spa_init(int mode) 2008fa9e4066Sahrens { 2009fa9e4066Sahrens mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 2010c25056deSgw mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 2011fa94a07fSbrendan mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 2012fa9e4066Sahrens cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 2013fa9e4066Sahrens 2014fa9e4066Sahrens avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 2015fa9e4066Sahrens offsetof(spa_t, spa_avl)); 2016fa9e4066Sahrens 2017fa94a07fSbrendan avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 2018fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 2019fa94a07fSbrendan 2020fa94a07fSbrendan avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 2021fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 202299653d4eSeschrock 20238ad4d6ddSJeff Bonwick spa_mode_global = mode; 2024fa9e4066Sahrens 2025283b8460SGeorge.Wilson #ifdef _KERNEL 2026283b8460SGeorge.Wilson spa_arch_init(); 2027283b8460SGeorge.Wilson #else 2028cd1c8b85SMatthew Ahrens if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 2029cd1c8b85SMatthew Ahrens arc_procfd = open("/proc/self/ctl", O_WRONLY); 2030cd1c8b85SMatthew Ahrens if (arc_procfd == -1) { 2031cd1c8b85SMatthew Ahrens perror("could not enable watchpoints: " 2032cd1c8b85SMatthew Ahrens "opening /proc/self/ctl failed: "); 2033cd1c8b85SMatthew Ahrens } else { 2034cd1c8b85SMatthew Ahrens arc_watch = B_TRUE; 2035cd1c8b85SMatthew Ahrens } 2036cd1c8b85SMatthew Ahrens } 2037cd1c8b85SMatthew Ahrens #endif 2038cd1c8b85SMatthew Ahrens 2039e914ace2STim Schumacher zfs_refcount_init(); 2040fa9e4066Sahrens unique_init(); 20410713e232SGeorge Wilson range_tree_init(); 20428363e80aSGeorge Wilson metaslab_alloc_trace_init(); 2043fa9e4066Sahrens zio_init(); 2044fa9e4066Sahrens dmu_init(); 2045fa9e4066Sahrens zil_init(); 204687db74c1Sek vdev_cache_stat_init(); 204791ebeef5Sahrens zfs_prop_init(); 2048990b4856Slling zpool_prop_init(); 2049ad135b5dSChristopher Siden zpool_feature_init(); 2050fa9e4066Sahrens spa_config_load(); 2051e14bb325SJeff Bonwick l2arc_start(); 2052fa9e4066Sahrens } 2053fa9e4066Sahrens 2054fa9e4066Sahrens void 2055fa9e4066Sahrens spa_fini(void) 2056fa9e4066Sahrens { 2057e14bb325SJeff Bonwick l2arc_stop(); 2058e14bb325SJeff Bonwick 2059fa9e4066Sahrens spa_evict_all(); 2060fa9e4066Sahrens 206187db74c1Sek vdev_cache_stat_fini(); 2062fa9e4066Sahrens zil_fini(); 2063fa9e4066Sahrens dmu_fini(); 2064fa9e4066Sahrens zio_fini(); 20658363e80aSGeorge Wilson metaslab_alloc_trace_fini(); 20660713e232SGeorge Wilson range_tree_fini(); 206791ebeef5Sahrens unique_fini(); 2068e914ace2STim Schumacher zfs_refcount_fini(); 2069fa9e4066Sahrens 2070fa9e4066Sahrens avl_destroy(&spa_namespace_avl); 207199653d4eSeschrock avl_destroy(&spa_spare_avl); 2072fa94a07fSbrendan avl_destroy(&spa_l2cache_avl); 2073fa9e4066Sahrens 2074fa9e4066Sahrens cv_destroy(&spa_namespace_cv); 2075fa9e4066Sahrens mutex_destroy(&spa_namespace_lock); 2076c25056deSgw mutex_destroy(&spa_spare_lock); 2077fa94a07fSbrendan mutex_destroy(&spa_l2cache_lock); 2078fa9e4066Sahrens } 20796ce0521aSperrin 20806ce0521aSperrin /* 20816ce0521aSperrin * Return whether this pool has slogs. No locking needed. 20826ce0521aSperrin * It's not a problem if the wrong answer is returned as it's only for 20836ce0521aSperrin * performance and not correctness 20846ce0521aSperrin */ 20856ce0521aSperrin boolean_t 20866ce0521aSperrin spa_has_slogs(spa_t *spa) 20876ce0521aSperrin { 20886ce0521aSperrin return (spa->spa_log_class->mc_rotor != NULL); 20896ce0521aSperrin } 2090bf82a41bSeschrock 2091b24ab676SJeff Bonwick spa_log_state_t 2092b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa) 2093b24ab676SJeff Bonwick { 2094b24ab676SJeff Bonwick return (spa->spa_log_state); 2095b24ab676SJeff Bonwick } 2096b24ab676SJeff Bonwick 2097b24ab676SJeff Bonwick void 2098b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state) 2099b24ab676SJeff Bonwick { 2100b24ab676SJeff Bonwick spa->spa_log_state = state; 2101b24ab676SJeff Bonwick } 2102b24ab676SJeff Bonwick 2103bf82a41bSeschrock boolean_t 2104bf82a41bSeschrock spa_is_root(spa_t *spa) 2105bf82a41bSeschrock { 2106bf82a41bSeschrock return (spa->spa_is_root); 2107bf82a41bSeschrock } 21088ad4d6ddSJeff Bonwick 21098ad4d6ddSJeff Bonwick boolean_t 21108ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa) 21118ad4d6ddSJeff Bonwick { 21126f793812SPavel Zakharov return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config); 21138ad4d6ddSJeff Bonwick } 21148ad4d6ddSJeff Bonwick 211573527f44SAlex Reece /* 211673527f44SAlex Reece * Returns true if there is a pending sync task in any of the current 211773527f44SAlex Reece * syncing txg, the current quiescing txg, or the current open txg. 211873527f44SAlex Reece */ 211973527f44SAlex Reece boolean_t 212073527f44SAlex Reece spa_has_pending_synctask(spa_t *spa) 212173527f44SAlex Reece { 212286714001SSerapheim Dimitropoulos return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) || 212386714001SSerapheim Dimitropoulos !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks)); 212473527f44SAlex Reece } 212573527f44SAlex Reece 21268ad4d6ddSJeff Bonwick int 21278ad4d6ddSJeff Bonwick spa_mode(spa_t *spa) 21288ad4d6ddSJeff Bonwick { 21298ad4d6ddSJeff Bonwick return (spa->spa_mode); 21308ad4d6ddSJeff Bonwick } 2131b24ab676SJeff Bonwick 2132b24ab676SJeff Bonwick uint64_t 2133b24ab676SJeff Bonwick spa_bootfs(spa_t *spa) 2134b24ab676SJeff Bonwick { 2135b24ab676SJeff Bonwick return (spa->spa_bootfs); 2136b24ab676SJeff Bonwick } 2137b24ab676SJeff Bonwick 2138b24ab676SJeff Bonwick uint64_t 2139b24ab676SJeff Bonwick spa_delegation(spa_t *spa) 2140b24ab676SJeff Bonwick { 2141b24ab676SJeff Bonwick return (spa->spa_delegation); 2142b24ab676SJeff Bonwick } 2143b24ab676SJeff Bonwick 2144b24ab676SJeff Bonwick objset_t * 2145b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa) 2146b24ab676SJeff Bonwick { 2147b24ab676SJeff Bonwick return (spa->spa_meta_objset); 2148b24ab676SJeff Bonwick } 2149b24ab676SJeff Bonwick 2150b24ab676SJeff Bonwick enum zio_checksum 2151b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa) 2152b24ab676SJeff Bonwick { 2153b24ab676SJeff Bonwick return (spa->spa_dedup_checksum); 2154b24ab676SJeff Bonwick } 21553f9d6ad7SLin Ling 21563f9d6ad7SLin Ling /* 21573f9d6ad7SLin Ling * Reset pool scan stat per scan pass (or reboot). 21583f9d6ad7SLin Ling */ 21593f9d6ad7SLin Ling void 21603f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa) 21613f9d6ad7SLin Ling { 21623f9d6ad7SLin Ling /* data not stored on disk */ 21633f9d6ad7SLin Ling spa->spa_scan_pass_start = gethrestime_sec(); 21641702cce7SAlek Pinchuk if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan)) 21651702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start; 21661702cce7SAlek Pinchuk else 21671702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = 0; 21681702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_spent_paused = 0; 21693f9d6ad7SLin Ling spa->spa_scan_pass_exam = 0; 21703f9d6ad7SLin Ling vdev_scan_stat_init(spa->spa_root_vdev); 21713f9d6ad7SLin Ling } 21723f9d6ad7SLin Ling 21733f9d6ad7SLin Ling /* 21743f9d6ad7SLin Ling * Get scan stats for zpool status reports 21753f9d6ad7SLin Ling */ 21763f9d6ad7SLin Ling int 21773f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 21783f9d6ad7SLin Ling { 21793f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 21803f9d6ad7SLin Ling 21813f9d6ad7SLin Ling if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 2182be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 21833f9d6ad7SLin Ling bzero(ps, sizeof (pool_scan_stat_t)); 21843f9d6ad7SLin Ling 21853f9d6ad7SLin Ling /* data stored on disk */ 21863f9d6ad7SLin Ling ps->pss_func = scn->scn_phys.scn_func; 21873f9d6ad7SLin Ling ps->pss_start_time = scn->scn_phys.scn_start_time; 21883f9d6ad7SLin Ling ps->pss_end_time = scn->scn_phys.scn_end_time; 21893f9d6ad7SLin Ling ps->pss_to_examine = scn->scn_phys.scn_to_examine; 21903f9d6ad7SLin Ling ps->pss_examined = scn->scn_phys.scn_examined; 21913f9d6ad7SLin Ling ps->pss_to_process = scn->scn_phys.scn_to_process; 21923f9d6ad7SLin Ling ps->pss_processed = scn->scn_phys.scn_processed; 21933f9d6ad7SLin Ling ps->pss_errors = scn->scn_phys.scn_errors; 21943f9d6ad7SLin Ling ps->pss_state = scn->scn_phys.scn_state; 21953f9d6ad7SLin Ling 21963f9d6ad7SLin Ling /* data not stored on disk */ 21973f9d6ad7SLin Ling ps->pss_pass_start = spa->spa_scan_pass_start; 21983f9d6ad7SLin Ling ps->pss_pass_exam = spa->spa_scan_pass_exam; 21991702cce7SAlek Pinchuk ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause; 22001702cce7SAlek Pinchuk ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused; 22013f9d6ad7SLin Ling 22023f9d6ad7SLin Ling return (0); 22033f9d6ad7SLin Ling } 220409c9d376SGeorge Wilson 2205b5152584SMatthew Ahrens int 2206b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa) 2207b5152584SMatthew Ahrens { 2208b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) 2209b5152584SMatthew Ahrens return (SPA_MAXBLOCKSIZE); 2210b5152584SMatthew Ahrens else 2211b5152584SMatthew Ahrens return (SPA_OLD_MAXBLOCKSIZE); 2212b5152584SMatthew Ahrens } 22135cabbc6bSPrashanth Sreenivasa 221454811da5SToomas Soome int 221554811da5SToomas Soome spa_maxdnodesize(spa_t *spa) 221654811da5SToomas Soome { 221754811da5SToomas Soome if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) 221854811da5SToomas Soome return (DNODE_MAX_SIZE); 221954811da5SToomas Soome else 222054811da5SToomas Soome return (DNODE_MIN_SIZE); 222154811da5SToomas Soome } 222254811da5SToomas Soome 2223e0f1c0afSOlaf Faaland boolean_t 2224e0f1c0afSOlaf Faaland spa_multihost(spa_t *spa) 2225e0f1c0afSOlaf Faaland { 2226e0f1c0afSOlaf Faaland return (spa->spa_multihost ? B_TRUE : B_FALSE); 2227e0f1c0afSOlaf Faaland } 2228e0f1c0afSOlaf Faaland 2229e0f1c0afSOlaf Faaland unsigned long 2230e0f1c0afSOlaf Faaland spa_get_hostid(void) 2231e0f1c0afSOlaf Faaland { 2232e0f1c0afSOlaf Faaland unsigned long myhostid; 2233e0f1c0afSOlaf Faaland 2234e0f1c0afSOlaf Faaland #ifdef _KERNEL 2235e0f1c0afSOlaf Faaland myhostid = zone_get_hostid(NULL); 2236e0f1c0afSOlaf Faaland #else /* _KERNEL */ 2237e0f1c0afSOlaf Faaland /* 2238e0f1c0afSOlaf Faaland * We're emulating the system's hostid in userland, so 2239e0f1c0afSOlaf Faaland * we can't use zone_get_hostid(). 2240e0f1c0afSOlaf Faaland */ 2241e0f1c0afSOlaf Faaland (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 2242e0f1c0afSOlaf Faaland #endif /* _KERNEL */ 2243e0f1c0afSOlaf Faaland 2244e0f1c0afSOlaf Faaland return (myhostid); 2245e0f1c0afSOlaf Faaland } 2246e0f1c0afSOlaf Faaland 22475cabbc6bSPrashanth Sreenivasa /* 22485cabbc6bSPrashanth Sreenivasa * Returns the txg that the last device removal completed. No indirect mappings 22495cabbc6bSPrashanth Sreenivasa * have been added since this txg. 22505cabbc6bSPrashanth Sreenivasa */ 22515cabbc6bSPrashanth Sreenivasa uint64_t 22525cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa) 22535cabbc6bSPrashanth Sreenivasa { 22545cabbc6bSPrashanth Sreenivasa uint64_t vdevid; 22555cabbc6bSPrashanth Sreenivasa uint64_t ret = -1ULL; 22565cabbc6bSPrashanth Sreenivasa 22575cabbc6bSPrashanth Sreenivasa spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 22585cabbc6bSPrashanth Sreenivasa /* 22595cabbc6bSPrashanth Sreenivasa * sr_prev_indirect_vdev is only modified while holding all the 22605cabbc6bSPrashanth Sreenivasa * config locks, so it is sufficient to hold SCL_VDEV as reader when 22615cabbc6bSPrashanth Sreenivasa * examining it. 22625cabbc6bSPrashanth Sreenivasa */ 22635cabbc6bSPrashanth Sreenivasa vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev; 22645cabbc6bSPrashanth Sreenivasa 22655cabbc6bSPrashanth Sreenivasa while (vdevid != -1ULL) { 22665cabbc6bSPrashanth Sreenivasa vdev_t *vd = vdev_lookup_top(spa, vdevid); 22675cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births; 22685cabbc6bSPrashanth Sreenivasa 22695cabbc6bSPrashanth Sreenivasa ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 22705cabbc6bSPrashanth Sreenivasa 22715cabbc6bSPrashanth Sreenivasa /* 22725cabbc6bSPrashanth Sreenivasa * If the removal did not remap any data, we don't care. 22735cabbc6bSPrashanth Sreenivasa */ 22745cabbc6bSPrashanth Sreenivasa if (vdev_indirect_births_count(vib) != 0) { 22755cabbc6bSPrashanth Sreenivasa ret = vdev_indirect_births_last_entry_txg(vib); 22765cabbc6bSPrashanth Sreenivasa break; 22775cabbc6bSPrashanth Sreenivasa } 22785cabbc6bSPrashanth Sreenivasa 22795cabbc6bSPrashanth Sreenivasa vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev; 22805cabbc6bSPrashanth Sreenivasa } 22815cabbc6bSPrashanth Sreenivasa spa_config_exit(spa, SCL_VDEV, FTAG); 22825cabbc6bSPrashanth Sreenivasa 22835cabbc6bSPrashanth Sreenivasa IMPLY(ret != -1ULL, 22845cabbc6bSPrashanth Sreenivasa spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 22855cabbc6bSPrashanth Sreenivasa 22865cabbc6bSPrashanth Sreenivasa return (ret); 22875cabbc6bSPrashanth Sreenivasa } 22886f793812SPavel Zakharov 22896f793812SPavel Zakharov boolean_t 22906f793812SPavel Zakharov spa_trust_config(spa_t *spa) 22916f793812SPavel Zakharov { 22926f793812SPavel Zakharov return (spa->spa_trust_config); 22936f793812SPavel Zakharov } 22946f793812SPavel Zakharov 22956f793812SPavel Zakharov uint64_t 22966f793812SPavel Zakharov spa_missing_tvds_allowed(spa_t *spa) 22976f793812SPavel Zakharov { 22986f793812SPavel Zakharov return (spa->spa_missing_tvds_allowed); 22996f793812SPavel Zakharov } 23006f793812SPavel Zakharov 23016f793812SPavel Zakharov void 23026f793812SPavel Zakharov spa_set_missing_tvds(spa_t *spa, uint64_t missing) 23036f793812SPavel Zakharov { 23046f793812SPavel Zakharov spa->spa_missing_tvds = missing; 23056f793812SPavel Zakharov } 230686714001SSerapheim Dimitropoulos 230786714001SSerapheim Dimitropoulos boolean_t 230886714001SSerapheim Dimitropoulos spa_top_vdevs_spacemap_addressable(spa_t *spa) 230986714001SSerapheim Dimitropoulos { 231086714001SSerapheim Dimitropoulos vdev_t *rvd = spa->spa_root_vdev; 231186714001SSerapheim Dimitropoulos for (uint64_t c = 0; c < rvd->vdev_children; c++) { 231286714001SSerapheim Dimitropoulos if (!vdev_is_spacemap_addressable(rvd->vdev_child[c])) 231386714001SSerapheim Dimitropoulos return (B_FALSE); 231486714001SSerapheim Dimitropoulos } 231586714001SSerapheim Dimitropoulos return (B_TRUE); 231686714001SSerapheim Dimitropoulos } 231786714001SSerapheim Dimitropoulos 231886714001SSerapheim Dimitropoulos boolean_t 231986714001SSerapheim Dimitropoulos spa_has_checkpoint(spa_t *spa) 232086714001SSerapheim Dimitropoulos { 232186714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_txg != 0); 232286714001SSerapheim Dimitropoulos } 232386714001SSerapheim Dimitropoulos 232486714001SSerapheim Dimitropoulos boolean_t 232586714001SSerapheim Dimitropoulos spa_importing_readonly_checkpoint(spa_t *spa) 232686714001SSerapheim Dimitropoulos { 232786714001SSerapheim Dimitropoulos return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) && 232886714001SSerapheim Dimitropoulos spa->spa_mode == FREAD); 232986714001SSerapheim Dimitropoulos } 233086714001SSerapheim Dimitropoulos 233186714001SSerapheim Dimitropoulos uint64_t 233286714001SSerapheim Dimitropoulos spa_min_claim_txg(spa_t *spa) 233386714001SSerapheim Dimitropoulos { 233486714001SSerapheim Dimitropoulos uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg; 233586714001SSerapheim Dimitropoulos 233686714001SSerapheim Dimitropoulos if (checkpoint_txg != 0) 233786714001SSerapheim Dimitropoulos return (checkpoint_txg + 1); 233886714001SSerapheim Dimitropoulos 233986714001SSerapheim Dimitropoulos return (spa->spa_first_txg); 234086714001SSerapheim Dimitropoulos } 234186714001SSerapheim Dimitropoulos 234286714001SSerapheim Dimitropoulos /* 234386714001SSerapheim Dimitropoulos * If there is a checkpoint, async destroys may consume more space from 234486714001SSerapheim Dimitropoulos * the pool instead of freeing it. In an attempt to save the pool from 234586714001SSerapheim Dimitropoulos * getting suspended when it is about to run out of space, we stop 234686714001SSerapheim Dimitropoulos * processing async destroys. 234786714001SSerapheim Dimitropoulos */ 234886714001SSerapheim Dimitropoulos boolean_t 234986714001SSerapheim Dimitropoulos spa_suspend_async_destroy(spa_t *spa) 235086714001SSerapheim Dimitropoulos { 235186714001SSerapheim Dimitropoulos dsl_pool_t *dp = spa_get_dsl(spa); 235286714001SSerapheim Dimitropoulos 235386714001SSerapheim Dimitropoulos uint64_t unreserved = dsl_pool_unreserved_space(dp, 235486714001SSerapheim Dimitropoulos ZFS_SPACE_CHECK_EXTRA_RESERVED); 235586714001SSerapheim Dimitropoulos uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes; 235686714001SSerapheim Dimitropoulos uint64_t avail = (unreserved > used) ? (unreserved - used) : 0; 235786714001SSerapheim Dimitropoulos 235886714001SSerapheim Dimitropoulos if (spa_has_checkpoint(spa) && avail == 0) 235986714001SSerapheim Dimitropoulos return (B_TRUE); 236086714001SSerapheim Dimitropoulos 236186714001SSerapheim Dimitropoulos return (B_FALSE); 236286714001SSerapheim Dimitropoulos } 2363