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. 23814dcd43SSerapheim Dimitropoulos * Copyright (c) 2011, 2019 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. 29e830fb12SKody A Kantor * Copyright 2019 Joyent, Inc. 30663207adSDon Brady * Copyright (c) 2017, Intel Corporation. 31*dd50e0ccSTony Hutter * Copyright 2020 Joyent, Inc. 32fa9e4066Sahrens */ 33fa9e4066Sahrens 34fa9e4066Sahrens #include <sys/zfs_context.h> 35fa9e4066Sahrens #include <sys/spa_impl.h> 36283b8460SGeorge.Wilson #include <sys/spa_boot.h> 37fa9e4066Sahrens #include <sys/zio.h> 38fa9e4066Sahrens #include <sys/zio_checksum.h> 39fa9e4066Sahrens #include <sys/zio_compress.h> 40fa9e4066Sahrens #include <sys/dmu.h> 41fa9e4066Sahrens #include <sys/dmu_tx.h> 42fa9e4066Sahrens #include <sys/zap.h> 43fa9e4066Sahrens #include <sys/zil.h> 44fa9e4066Sahrens #include <sys/vdev_impl.h> 45094e47e9SGeorge Wilson #include <sys/vdev_initialize.h> 46084fd14fSBrian Behlendorf #include <sys/vdev_trim.h> 47fa9e4066Sahrens #include <sys/metaslab.h> 48fa9e4066Sahrens #include <sys/uberblock_impl.h> 49fa9e4066Sahrens #include <sys/txg.h> 50fa9e4066Sahrens #include <sys/avl.h> 51fa9e4066Sahrens #include <sys/unique.h> 52fa9e4066Sahrens #include <sys/dsl_pool.h> 53fa9e4066Sahrens #include <sys/dsl_dir.h> 54fa9e4066Sahrens #include <sys/dsl_prop.h> 553f9d6ad7SLin Ling #include <sys/dsl_scan.h> 56fa9e4066Sahrens #include <sys/fs/zfs.h> 576ce0521aSperrin #include <sys/metaslab_impl.h> 58e14bb325SJeff Bonwick #include <sys/arc.h> 59485bbbf5SGeorge Wilson #include <sys/ddt.h> 6091ebeef5Sahrens #include "zfs_prop.h" 614d7988d6SPaul Dagnelie #include <sys/btree.h> 6245818ee1SMatthew Ahrens #include <sys/zfeature.h> 63fa9e4066Sahrens 64fa9e4066Sahrens /* 65fa9e4066Sahrens * SPA locking 66fa9e4066Sahrens * 67814dcd43SSerapheim Dimitropoulos * There are three basic locks for managing spa_t structures: 68fa9e4066Sahrens * 69fa9e4066Sahrens * spa_namespace_lock (global mutex) 70fa9e4066Sahrens * 7144cd46caSbillm * This lock must be acquired to do any of the following: 72fa9e4066Sahrens * 7344cd46caSbillm * - Lookup a spa_t by name 7444cd46caSbillm * - Add or remove a spa_t from the namespace 7544cd46caSbillm * - Increase spa_refcount from non-zero 7644cd46caSbillm * - Check if spa_refcount is zero 7744cd46caSbillm * - Rename a spa_t 78ea8dc4b6Seschrock * - add/remove/attach/detach devices 7944cd46caSbillm * - Held for the duration of create/destroy/import/export 80fa9e4066Sahrens * 8144cd46caSbillm * It does not need to handle recursion. A create or destroy may 8244cd46caSbillm * reference objects (files or zvols) in other pools, but by 8344cd46caSbillm * definition they must have an existing reference, and will never need 8444cd46caSbillm * to lookup a spa_t by name. 85fa9e4066Sahrens * 86e914ace2STim Schumacher * spa_refcount (per-spa zfs_refcount_t protected by mutex) 87fa9e4066Sahrens * 8844cd46caSbillm * This reference count keep track of any active users of the spa_t. The 8944cd46caSbillm * spa_t cannot be destroyed or freed while this is non-zero. Internally, 9044cd46caSbillm * the refcount is never really 'zero' - opening a pool implicitly keeps 91088f3894Sahrens * some references in the DMU. Internally we check against spa_minref, but 9244cd46caSbillm * present the image of a zero/non-zero value to consumers. 93fa9e4066Sahrens * 94e14bb325SJeff Bonwick * spa_config_lock[] (per-spa array of rwlocks) 95fa9e4066Sahrens * 9691ebeef5Sahrens * This protects the spa_t from config changes, and must be held in 9791ebeef5Sahrens * the following circumstances: 98fa9e4066Sahrens * 9944cd46caSbillm * - RW_READER to perform I/O to the spa 10044cd46caSbillm * - RW_WRITER to change the vdev config 101fa9e4066Sahrens * 102fa9e4066Sahrens * The locking order is fairly straightforward: 103fa9e4066Sahrens * 10444cd46caSbillm * spa_namespace_lock -> spa_refcount 105fa9e4066Sahrens * 10644cd46caSbillm * The namespace lock must be acquired to increase the refcount from 0 10744cd46caSbillm * or to check if it is zero. 108fa9e4066Sahrens * 109e14bb325SJeff Bonwick * spa_refcount -> spa_config_lock[] 110fa9e4066Sahrens * 11144cd46caSbillm * There must be at least one valid reference on the spa_t to acquire 11244cd46caSbillm * the config lock. 113fa9e4066Sahrens * 114e14bb325SJeff Bonwick * spa_namespace_lock -> spa_config_lock[] 115fa9e4066Sahrens * 11644cd46caSbillm * The namespace lock must always be taken before the config lock. 117fa9e4066Sahrens * 118fa9e4066Sahrens * 119e14bb325SJeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible. 120fa9e4066Sahrens * 121e14bb325SJeff Bonwick * The namespace is manipulated using the following functions, all of which 122e14bb325SJeff Bonwick * require the spa_namespace_lock to be held. 123fa9e4066Sahrens * 12444cd46caSbillm * spa_lookup() Lookup a spa_t by name. 125fa9e4066Sahrens * 12644cd46caSbillm * spa_add() Create a new spa_t in the namespace. 127fa9e4066Sahrens * 12844cd46caSbillm * spa_remove() Remove a spa_t from the namespace. This also 12944cd46caSbillm * frees up any memory associated with the spa_t. 130fa9e4066Sahrens * 13144cd46caSbillm * spa_next() Returns the next spa_t in the system, or the 13244cd46caSbillm * first if NULL is passed. 133fa9e4066Sahrens * 13444cd46caSbillm * spa_evict_all() Shutdown and remove all spa_t structures in 13544cd46caSbillm * the system. 136fa9e4066Sahrens * 137ea8dc4b6Seschrock * spa_guid_exists() Determine whether a pool/device guid exists. 138fa9e4066Sahrens * 139fa9e4066Sahrens * The spa_refcount is manipulated using the following functions: 140fa9e4066Sahrens * 14144cd46caSbillm * spa_open_ref() Adds a reference to the given spa_t. Must be 14244cd46caSbillm * called with spa_namespace_lock held if the 14344cd46caSbillm * refcount is currently zero. 144fa9e4066Sahrens * 14544cd46caSbillm * spa_close() Remove a reference from the spa_t. This will 14644cd46caSbillm * not free the spa_t or remove it from the 14744cd46caSbillm * namespace. No locking is required. 148fa9e4066Sahrens * 14944cd46caSbillm * spa_refcount_zero() Returns true if the refcount is currently 15044cd46caSbillm * zero. Must be called with spa_namespace_lock 15144cd46caSbillm * held. 152fa9e4066Sahrens * 153e14bb325SJeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows: 154e14bb325SJeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 155e14bb325SJeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 156e14bb325SJeff Bonwick * 157e14bb325SJeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader. 158e14bb325SJeff Bonwick * To modify the configuration, you must hold all locks as writer. To modify 159e14bb325SJeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline), 160e14bb325SJeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer. 161e14bb325SJeff Bonwick * 162e14bb325SJeff Bonwick * We use these distinct config locks to avoid recursive lock entry. 163e14bb325SJeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 164e14bb325SJeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps 165e14bb325SJeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 166e14bb325SJeff Bonwick * 167e14bb325SJeff Bonwick * The spa config locks cannot be normal rwlocks because we need the 168e14bb325SJeff Bonwick * ability to hand off ownership. For example, SCL_ZIO is acquired 169e14bb325SJeff Bonwick * by the issuing thread and later released by an interrupt thread. 170e14bb325SJeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent 171e14bb325SJeff Bonwick * writer (i.e. system administrator) starvation. 172e14bb325SJeff Bonwick * 173e14bb325SJeff Bonwick * The lock acquisition rules are as follows: 174e14bb325SJeff Bonwick * 175e14bb325SJeff Bonwick * SCL_CONFIG 176e14bb325SJeff Bonwick * Protects changes to the vdev tree topology, such as vdev 177e14bb325SJeff Bonwick * add/remove/attach/detach. Protects the dirty config list 178e14bb325SJeff Bonwick * (spa_config_dirty_list) and the set of spares and l2arc devices. 179e14bb325SJeff Bonwick * 180e14bb325SJeff Bonwick * SCL_STATE 181e14bb325SJeff Bonwick * Protects changes to pool state and vdev state, such as vdev 182e14bb325SJeff Bonwick * online/offline/fault/degrade/clear. Protects the dirty state list 183e14bb325SJeff Bonwick * (spa_state_dirty_list) and global pool state (spa_state). 184e14bb325SJeff Bonwick * 185e14bb325SJeff Bonwick * SCL_ALLOC 186e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 187e14bb325SJeff Bonwick * Held as reader by metaslab_alloc() and metaslab_claim(). 188e14bb325SJeff Bonwick * 189e14bb325SJeff Bonwick * SCL_ZIO 190e14bb325SJeff Bonwick * Held by bp-level zios (those which have no io_vd upon entry) 191e14bb325SJeff Bonwick * to prevent changes to the vdev tree. The bp-level zio implicitly 192e14bb325SJeff Bonwick * protects all of its vdev child zios, which do not hold SCL_ZIO. 193e14bb325SJeff Bonwick * 194e14bb325SJeff Bonwick * SCL_FREE 195e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes. 196e14bb325SJeff Bonwick * Held as reader by metaslab_free(). SCL_FREE is distinct from 197e14bb325SJeff Bonwick * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 198e14bb325SJeff Bonwick * blocks in zio_done() while another i/o that holds either 199e14bb325SJeff Bonwick * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 200e14bb325SJeff Bonwick * 201e14bb325SJeff Bonwick * SCL_VDEV 202e14bb325SJeff Bonwick * Held as reader to prevent changes to the vdev tree during trivial 203b24ab676SJeff Bonwick * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 204e14bb325SJeff Bonwick * other locks, and lower than all of them, to ensure that it's safe 205e14bb325SJeff Bonwick * to acquire regardless of caller context. 206e14bb325SJeff Bonwick * 207e14bb325SJeff Bonwick * In addition, the following rules apply: 208e14bb325SJeff Bonwick * 209e14bb325SJeff Bonwick * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 210e14bb325SJeff Bonwick * The lock ordering is SCL_CONFIG > spa_props_lock. 211e14bb325SJeff Bonwick * 212e14bb325SJeff Bonwick * (b) I/O operations on leaf vdevs. For any zio operation that takes 213e14bb325SJeff Bonwick * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 214e14bb325SJeff Bonwick * or zio_write_phys() -- the caller must ensure that the config cannot 215e14bb325SJeff Bonwick * cannot change in the interim, and that the vdev cannot be reopened. 216e14bb325SJeff Bonwick * SCL_STATE as reader suffices for both. 217fa9e4066Sahrens * 218ea8dc4b6Seschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 219fa9e4066Sahrens * 22044cd46caSbillm * spa_vdev_enter() Acquire the namespace lock and the config lock 221ea8dc4b6Seschrock * for writing. 222fa9e4066Sahrens * 22344cd46caSbillm * spa_vdev_exit() Release the config lock, wait for all I/O 22444cd46caSbillm * to complete, sync the updated configs to the 225ea8dc4b6Seschrock * cache, and release the namespace lock. 226fa9e4066Sahrens * 227e14bb325SJeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 228e14bb325SJeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 229e14bb325SJeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 230fa9e4066Sahrens */ 231fa9e4066Sahrens 232fa9e4066Sahrens static avl_tree_t spa_namespace_avl; 233fa9e4066Sahrens kmutex_t spa_namespace_lock; 234fa9e4066Sahrens static kcondvar_t spa_namespace_cv; 2350373e76bSbonwick static int spa_active_count; 236416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP; 237fa9e4066Sahrens 23899653d4eSeschrock static kmutex_t spa_spare_lock; 23939c23413Seschrock static avl_tree_t spa_spare_avl; 240fa94a07fSbrendan static kmutex_t spa_l2cache_lock; 241fa94a07fSbrendan static avl_tree_t spa_l2cache_avl; 24299653d4eSeschrock 243fa9e4066Sahrens kmem_cache_t *spa_buffer_pool; 2448ad4d6ddSJeff Bonwick int spa_mode_global; 245fa9e4066Sahrens 246fa9e4066Sahrens #ifdef ZFS_DEBUG 2475cabbc6bSPrashanth Sreenivasa /* 2485cabbc6bSPrashanth Sreenivasa * Everything except dprintf, spa, and indirect_remap is on by default 2495cabbc6bSPrashanth Sreenivasa * in debug builds. 2505cabbc6bSPrashanth Sreenivasa */ 25121f7c81cSMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP); 252fa9e4066Sahrens #else 253fa9e4066Sahrens int zfs_flags = 0; 254fa9e4066Sahrens #endif 255fa9e4066Sahrens 2560125049cSahrens /* 2570125049cSahrens * zfs_recover can be set to nonzero to attempt to recover from 2580125049cSahrens * otherwise-fatal errors, typically caused by on-disk corruption. When 2590125049cSahrens * set, calls to zfs_panic_recover() will turn into warning messages. 2608b36997aSMatthew Ahrens * This should only be used as a last resort, as it typically results 2618b36997aSMatthew Ahrens * in leaked space, or worse. 2620125049cSahrens */ 2637fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE; 2647fd05ac4SMatthew Ahrens 2657fd05ac4SMatthew Ahrens /* 2667fd05ac4SMatthew Ahrens * If destroy encounters an EIO while reading metadata (e.g. indirect 2677fd05ac4SMatthew Ahrens * blocks), space referenced by the missing metadata can not be freed. 2687fd05ac4SMatthew Ahrens * Normally this causes the background destroy to become "stalled", as 2697fd05ac4SMatthew Ahrens * it is unable to make forward progress. While in this stalled state, 2707fd05ac4SMatthew Ahrens * all remaining space to free from the error-encountering filesystem is 2717fd05ac4SMatthew Ahrens * "temporarily leaked". Set this flag to cause it to ignore the EIO, 2727fd05ac4SMatthew Ahrens * permanently leak the space from indirect blocks that can not be read, 2737fd05ac4SMatthew Ahrens * and continue to free everything else that it can. 2747fd05ac4SMatthew Ahrens * 2757fd05ac4SMatthew Ahrens * The default, "stalling" behavior is useful if the storage partially 2767fd05ac4SMatthew Ahrens * fails (i.e. some but not all i/os fail), and then later recovers. In 2777fd05ac4SMatthew Ahrens * this case, we will be able to continue pool operations while it is 2787fd05ac4SMatthew Ahrens * partially failed, and when it recovers, we can continue to free the 2797fd05ac4SMatthew Ahrens * space, with no leaks. However, note that this case is actually 2807fd05ac4SMatthew Ahrens * fairly rare. 2817fd05ac4SMatthew Ahrens * 2827fd05ac4SMatthew Ahrens * Typically pools either (a) fail completely (but perhaps temporarily, 2837fd05ac4SMatthew Ahrens * e.g. a top-level vdev going offline), or (b) have localized, 2847fd05ac4SMatthew Ahrens * permanent errors (e.g. disk returns the wrong data due to bit flip or 2857fd05ac4SMatthew Ahrens * firmware bug). In case (a), this setting does not matter because the 2867fd05ac4SMatthew Ahrens * pool will be suspended and the sync thread will not be able to make 2877fd05ac4SMatthew Ahrens * forward progress regardless. In case (b), because the error is 2887fd05ac4SMatthew Ahrens * permanent, the best we can do is leak the minimum amount of space, 2897fd05ac4SMatthew Ahrens * which is what setting this flag will do. Therefore, it is reasonable 2907fd05ac4SMatthew Ahrens * for this flag to normally be set, but we chose the more conservative 2917fd05ac4SMatthew Ahrens * approach of not setting it, so that there is no possibility of 2927fd05ac4SMatthew Ahrens * leaking space in the "partial temporary" failure case. 2937fd05ac4SMatthew Ahrens */ 2947fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE; 2950125049cSahrens 29669962b56SMatthew Ahrens /* 29769962b56SMatthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is 29869962b56SMatthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the 29969962b56SMatthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds. 30069962b56SMatthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that 30169962b56SMatthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting 30269962b56SMatthew Ahrens * in a system panic. 30369962b56SMatthew Ahrens */ 30469962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL; 305283b8460SGeorge.Wilson 306283b8460SGeorge.Wilson /* 30769962b56SMatthew Ahrens * Check time in milliseconds. This defines the frequency at which we check 30869962b56SMatthew Ahrens * for hung I/O. 309283b8460SGeorge.Wilson */ 31069962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL; 311283b8460SGeorge.Wilson 312283b8460SGeorge.Wilson /* 313283b8460SGeorge.Wilson * Override the zfs deadman behavior via /etc/system. By default the 314283b8460SGeorge.Wilson * deadman is enabled except on VMware and sparc deployments. 315283b8460SGeorge.Wilson */ 316283b8460SGeorge.Wilson int zfs_deadman_enabled = -1; 317283b8460SGeorge.Wilson 31869962b56SMatthew Ahrens /* 31969962b56SMatthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which 32069962b56SMatthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 32169962b56SMatthew Ahrens * times the size; so just assume that. Add to this the fact that 32269962b56SMatthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because 32369962b56SMatthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together, 32469962b56SMatthew Ahrens * the worst case is: 32569962b56SMatthew Ahrens * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24 32669962b56SMatthew Ahrens */ 32769962b56SMatthew Ahrens int spa_asize_inflation = 24; 328fa9e4066Sahrens 3297d46dc6cSMatthew Ahrens /* 3307d46dc6cSMatthew Ahrens * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in 3317d46dc6cSMatthew Ahrens * the pool to be consumed. This ensures that we don't run the pool 3327d46dc6cSMatthew Ahrens * completely out of space, due to unaccounted changes (e.g. to the MOS). 3337d46dc6cSMatthew Ahrens * It also limits the worst-case time to allocate space. If we have 3347d46dc6cSMatthew Ahrens * less than this amount of free space, most ZPL operations (e.g. write, 3357d46dc6cSMatthew Ahrens * create) will return ENOSPC. 3367d46dc6cSMatthew Ahrens * 3377d46dc6cSMatthew Ahrens * Certain operations (e.g. file removal, most administrative actions) can 3387d46dc6cSMatthew Ahrens * use half the slop space. They will only return ENOSPC if less than half 3397d46dc6cSMatthew Ahrens * the slop space is free. Typically, once the pool has less than the slop 3407d46dc6cSMatthew Ahrens * space free, the user will use these operations to free up space in the pool. 3417d46dc6cSMatthew Ahrens * These are the operations that call dsl_pool_adjustedsize() with the netfree 3427d46dc6cSMatthew Ahrens * argument set to TRUE. 3437d46dc6cSMatthew Ahrens * 34486714001SSerapheim Dimitropoulos * Operations that are almost guaranteed to free up space in the absence of 34586714001SSerapheim Dimitropoulos * a pool checkpoint can use up to three quarters of the slop space 34686714001SSerapheim Dimitropoulos * (e.g zfs destroy). 34786714001SSerapheim Dimitropoulos * 3487d46dc6cSMatthew Ahrens * A very restricted set of operations are always permitted, regardless of 3497d46dc6cSMatthew Ahrens * the amount of free space. These are the operations that call 35086714001SSerapheim Dimitropoulos * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net 35186714001SSerapheim Dimitropoulos * increase in the amount of space used, it is possible to run the pool 35286714001SSerapheim Dimitropoulos * completely out of space, causing it to be permanently read-only. 3537d46dc6cSMatthew Ahrens * 3544b5c8e93SMatthew Ahrens * Note that on very small pools, the slop space will be larger than 3554b5c8e93SMatthew Ahrens * 3.2%, in an effort to have it be at least spa_min_slop (128MB), 3564b5c8e93SMatthew Ahrens * but we never allow it to be more than half the pool size. 3574b5c8e93SMatthew Ahrens * 3587d46dc6cSMatthew Ahrens * See also the comments in zfs_space_check_t. 3597d46dc6cSMatthew Ahrens */ 3607d46dc6cSMatthew Ahrens int spa_slop_shift = 5; 3614b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024; 3627d46dc6cSMatthew Ahrens 363f78cdc34SPaul Dagnelie int spa_allocators = 4; 364f78cdc34SPaul Dagnelie 3653ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 3663ee8c80cSPavel Zakharov void 3673ee8c80cSPavel Zakharov spa_load_failed(spa_t *spa, const char *fmt, ...) 3683ee8c80cSPavel Zakharov { 3693ee8c80cSPavel Zakharov va_list adx; 3703ee8c80cSPavel Zakharov char buf[256]; 3713ee8c80cSPavel Zakharov 3723ee8c80cSPavel Zakharov va_start(adx, fmt); 3733ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 3743ee8c80cSPavel Zakharov va_end(adx); 3753ee8c80cSPavel Zakharov 3766f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name, 3776f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf); 3783ee8c80cSPavel Zakharov } 3793ee8c80cSPavel Zakharov 3803ee8c80cSPavel Zakharov /*PRINTFLIKE2*/ 3813ee8c80cSPavel Zakharov void 3823ee8c80cSPavel Zakharov spa_load_note(spa_t *spa, const char *fmt, ...) 3833ee8c80cSPavel Zakharov { 3843ee8c80cSPavel Zakharov va_list adx; 3853ee8c80cSPavel Zakharov char buf[256]; 3863ee8c80cSPavel Zakharov 3873ee8c80cSPavel Zakharov va_start(adx, fmt); 3883ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx); 3893ee8c80cSPavel Zakharov va_end(adx); 3903ee8c80cSPavel Zakharov 3916f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name, 3926f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf); 3933ee8c80cSPavel Zakharov } 3943ee8c80cSPavel Zakharov 395663207adSDon Brady /* 396663207adSDon Brady * By default dedup and user data indirects land in the special class 397663207adSDon Brady */ 398663207adSDon Brady int zfs_ddt_data_is_special = B_TRUE; 399663207adSDon Brady int zfs_user_indirect_is_special = B_TRUE; 400663207adSDon Brady 401663207adSDon Brady /* 402663207adSDon Brady * The percentage of special class final space reserved for metadata only. 403663207adSDon Brady * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only 404663207adSDon Brady * let metadata into the class. 405663207adSDon Brady */ 406663207adSDon Brady int zfs_special_class_metadata_reserve_pct = 25; 407663207adSDon Brady 408e05725b1Sbonwick /* 409e05725b1Sbonwick * ========================================================================== 410e05725b1Sbonwick * SPA config locking 411e05725b1Sbonwick * ========================================================================== 412e05725b1Sbonwick */ 413e05725b1Sbonwick static void 414e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa) 415e14bb325SJeff Bonwick { 416e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 417e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 418e14bb325SJeff Bonwick mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 419e14bb325SJeff Bonwick cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 420e914ace2STim Schumacher zfs_refcount_create_untracked(&scl->scl_count); 421e14bb325SJeff Bonwick scl->scl_writer = NULL; 422e14bb325SJeff Bonwick scl->scl_write_wanted = 0; 423e14bb325SJeff Bonwick } 424e05725b1Sbonwick } 425e05725b1Sbonwick 426e05725b1Sbonwick static void 427e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa) 428e14bb325SJeff Bonwick { 429e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 430e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 431e14bb325SJeff Bonwick mutex_destroy(&scl->scl_lock); 432e14bb325SJeff Bonwick cv_destroy(&scl->scl_cv); 433e914ace2STim Schumacher zfs_refcount_destroy(&scl->scl_count); 434e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL); 435e14bb325SJeff Bonwick ASSERT(scl->scl_write_wanted == 0); 436e14bb325SJeff Bonwick } 437e14bb325SJeff Bonwick } 438e14bb325SJeff Bonwick 439e14bb325SJeff Bonwick int 440e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 441e05725b1Sbonwick { 442e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 443e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 444e14bb325SJeff Bonwick if (!(locks & (1 << i))) 445e14bb325SJeff Bonwick continue; 446e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 447e14bb325SJeff Bonwick if (rw == RW_READER) { 448e14bb325SJeff Bonwick if (scl->scl_writer || scl->scl_write_wanted) { 449e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 450e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 451e495b6e6SSaso Kiselkov tag); 452e14bb325SJeff Bonwick return (0); 453e14bb325SJeff Bonwick } 454e14bb325SJeff Bonwick } else { 455e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 456e914ace2STim Schumacher if (!zfs_refcount_is_zero(&scl->scl_count)) { 457e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 458e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1), 459e495b6e6SSaso Kiselkov tag); 460e14bb325SJeff Bonwick return (0); 461e14bb325SJeff Bonwick } 462e14bb325SJeff Bonwick scl->scl_writer = curthread; 463e14bb325SJeff Bonwick } 464e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag); 465e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 466e14bb325SJeff Bonwick } 467e14bb325SJeff Bonwick return (1); 468e05725b1Sbonwick } 469e05725b1Sbonwick 470e05725b1Sbonwick void 471e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 472e05725b1Sbonwick { 473f64c0e34SEric Taylor int wlocks_held = 0; 474f64c0e34SEric Taylor 4753b2aab18SMatthew Ahrens ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 4763b2aab18SMatthew Ahrens 477e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 478e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 479f64c0e34SEric Taylor if (scl->scl_writer == curthread) 480f64c0e34SEric Taylor wlocks_held |= (1 << i); 481e14bb325SJeff Bonwick if (!(locks & (1 << i))) 482e14bb325SJeff Bonwick continue; 483e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 484e14bb325SJeff Bonwick if (rw == RW_READER) { 485e14bb325SJeff Bonwick while (scl->scl_writer || scl->scl_write_wanted) { 486e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 487e14bb325SJeff Bonwick } 488e14bb325SJeff Bonwick } else { 489e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread); 490e914ace2STim Schumacher while (!zfs_refcount_is_zero(&scl->scl_count)) { 491e14bb325SJeff Bonwick scl->scl_write_wanted++; 492e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock); 493e14bb325SJeff Bonwick scl->scl_write_wanted--; 494e14bb325SJeff Bonwick } 495e14bb325SJeff Bonwick scl->scl_writer = curthread; 496e14bb325SJeff Bonwick } 497e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag); 498e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 499e05725b1Sbonwick } 5005cabbc6bSPrashanth Sreenivasa ASSERT3U(wlocks_held, <=, locks); 501e05725b1Sbonwick } 502e05725b1Sbonwick 503e05725b1Sbonwick void 504e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag) 505e05725b1Sbonwick { 506e14bb325SJeff Bonwick for (int i = SCL_LOCKS - 1; i >= 0; i--) { 507e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 508e14bb325SJeff Bonwick if (!(locks & (1 << i))) 509e14bb325SJeff Bonwick continue; 510e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock); 511e914ace2STim Schumacher ASSERT(!zfs_refcount_is_zero(&scl->scl_count)); 512e914ace2STim Schumacher if (zfs_refcount_remove(&scl->scl_count, tag) == 0) { 513e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL || 514e14bb325SJeff Bonwick scl->scl_writer == curthread); 515e14bb325SJeff Bonwick scl->scl_writer = NULL; /* OK in either case */ 516e14bb325SJeff Bonwick cv_broadcast(&scl->scl_cv); 517e14bb325SJeff Bonwick } 518e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock); 519e05725b1Sbonwick } 520e05725b1Sbonwick } 521e05725b1Sbonwick 522e14bb325SJeff Bonwick int 523e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw) 524e05725b1Sbonwick { 525e14bb325SJeff Bonwick int locks_held = 0; 526e05725b1Sbonwick 527e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) { 528e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i]; 529e14bb325SJeff Bonwick if (!(locks & (1 << i))) 530e14bb325SJeff Bonwick continue; 531e914ace2STim Schumacher if ((rw == RW_READER && 532e914ace2STim Schumacher !zfs_refcount_is_zero(&scl->scl_count)) || 533e14bb325SJeff Bonwick (rw == RW_WRITER && scl->scl_writer == curthread)) 534e14bb325SJeff Bonwick locks_held |= 1 << i; 535e14bb325SJeff Bonwick } 536e14bb325SJeff Bonwick 537e14bb325SJeff Bonwick return (locks_held); 538e05725b1Sbonwick } 539e05725b1Sbonwick 540fa9e4066Sahrens /* 541fa9e4066Sahrens * ========================================================================== 542fa9e4066Sahrens * SPA namespace functions 543fa9e4066Sahrens * ========================================================================== 544fa9e4066Sahrens */ 545fa9e4066Sahrens 546fa9e4066Sahrens /* 547fa9e4066Sahrens * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 548fa9e4066Sahrens * Returns NULL if no matching spa_t is found. 549fa9e4066Sahrens */ 550fa9e4066Sahrens spa_t * 551fa9e4066Sahrens spa_lookup(const char *name) 552fa9e4066Sahrens { 553e14bb325SJeff Bonwick static spa_t search; /* spa_t is large; don't allocate on stack */ 554e14bb325SJeff Bonwick spa_t *spa; 555fa9e4066Sahrens avl_index_t where; 55640feaa91Sahrens char *cp; 557fa9e4066Sahrens 558fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 559fa9e4066Sahrens 5603b2aab18SMatthew Ahrens (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 5613b2aab18SMatthew Ahrens 56240feaa91Sahrens /* 56340feaa91Sahrens * If it's a full dataset name, figure out the pool name and 56440feaa91Sahrens * just use that. 56540feaa91Sahrens */ 56678f17100SMatthew Ahrens cp = strpbrk(search.spa_name, "/@#"); 5673b2aab18SMatthew Ahrens if (cp != NULL) 56840feaa91Sahrens *cp = '\0'; 56940feaa91Sahrens 570fa9e4066Sahrens spa = avl_find(&spa_namespace_avl, &search, &where); 571fa9e4066Sahrens 572fa9e4066Sahrens return (spa); 573fa9e4066Sahrens } 574fa9e4066Sahrens 575283b8460SGeorge.Wilson /* 576283b8460SGeorge.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 577283b8460SGeorge.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 578283b8460SGeorge.Wilson * looking for potentially hung I/Os. 579283b8460SGeorge.Wilson */ 580283b8460SGeorge.Wilson void 581283b8460SGeorge.Wilson spa_deadman(void *arg) 582283b8460SGeorge.Wilson { 583283b8460SGeorge.Wilson spa_t *spa = arg; 584283b8460SGeorge.Wilson 5850713e232SGeorge Wilson /* 5860713e232SGeorge Wilson * Disable the deadman timer if the pool is suspended. 5870713e232SGeorge Wilson */ 5880713e232SGeorge Wilson if (spa_suspended(spa)) { 5890713e232SGeorge Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 5900713e232SGeorge Wilson return; 5910713e232SGeorge Wilson } 5920713e232SGeorge Wilson 593283b8460SGeorge.Wilson zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 594283b8460SGeorge.Wilson (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 595283b8460SGeorge.Wilson ++spa->spa_deadman_calls); 596283b8460SGeorge.Wilson if (zfs_deadman_enabled) 597283b8460SGeorge.Wilson vdev_deadman(spa->spa_root_vdev); 598283b8460SGeorge.Wilson } 599283b8460SGeorge.Wilson 600814dcd43SSerapheim Dimitropoulos int 601814dcd43SSerapheim Dimitropoulos spa_log_sm_sort_by_txg(const void *va, const void *vb) 602814dcd43SSerapheim Dimitropoulos { 603814dcd43SSerapheim Dimitropoulos const spa_log_sm_t *a = va; 604814dcd43SSerapheim Dimitropoulos const spa_log_sm_t *b = vb; 605814dcd43SSerapheim Dimitropoulos 6064d7988d6SPaul Dagnelie return (TREE_CMP(a->sls_txg, b->sls_txg)); 607814dcd43SSerapheim Dimitropoulos } 608814dcd43SSerapheim Dimitropoulos 609fa9e4066Sahrens /* 610fa9e4066Sahrens * Create an uninitialized spa_t with the given name. Requires 611fa9e4066Sahrens * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 612fa9e4066Sahrens * exist by calling spa_lookup() first. 613fa9e4066Sahrens */ 614fa9e4066Sahrens spa_t * 615468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot) 616fa9e4066Sahrens { 617fa9e4066Sahrens spa_t *spa; 618c5904d13Seschrock spa_config_dirent_t *dp; 619283b8460SGeorge.Wilson cyc_handler_t hdlr; 620283b8460SGeorge.Wilson cyc_time_t when; 621fa9e4066Sahrens 622fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 623fa9e4066Sahrens 624fa9e4066Sahrens spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 625fa9e4066Sahrens 626c25056deSgw mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 627c25056deSgw mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 62835a5a358SJonathan Adams mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 629bc9014e6SJustin Gibbs mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL); 630c25056deSgw mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 63135a5a358SJonathan Adams mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 632c25056deSgw mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 63345818ee1SMatthew Ahrens mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL); 63435a5a358SJonathan Adams mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 635a1521560SJeff Bonwick mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 636a1521560SJeff Bonwick mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 637c3a66015SMatthew Ahrens mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL); 638814dcd43SSerapheim Dimitropoulos mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL); 639*dd50e0ccSTony Hutter mutex_init(&spa->spa_imp_kstat_lock, NULL, MUTEX_DEFAULT, NULL); 640c25056deSgw 641c25056deSgw cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 642bc9014e6SJustin Gibbs cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL); 64335a5a358SJonathan Adams cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 644c25056deSgw cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 645e14bb325SJeff Bonwick cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 646c25056deSgw 647b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 648cde58dbcSMatthew Ahrens bplist_create(&spa->spa_free_bplist[t]); 649b24ab676SJeff Bonwick 650e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 651fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED; 652fa9e4066Sahrens spa->spa_freeze_txg = UINT64_MAX; 6530373e76bSbonwick spa->spa_final_txg = UINT64_MAX; 654468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX; 65535a5a358SJonathan Adams spa->spa_proc = &p0; 65635a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE; 6576f793812SPavel Zakharov spa->spa_trust_config = B_TRUE; 658fa9e4066Sahrens 659283b8460SGeorge.Wilson hdlr.cyh_func = spa_deadman; 660283b8460SGeorge.Wilson hdlr.cyh_arg = spa; 661283b8460SGeorge.Wilson hdlr.cyh_level = CY_LOW_LEVEL; 662283b8460SGeorge.Wilson 66369962b56SMatthew Ahrens spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 664283b8460SGeorge.Wilson 665283b8460SGeorge.Wilson /* 666283b8460SGeorge.Wilson * This determines how often we need to check for hung I/Os after 667283b8460SGeorge.Wilson * the cyclic has already fired. Since checking for hung I/Os is 668283b8460SGeorge.Wilson * an expensive operation we don't want to check too frequently. 66969962b56SMatthew Ahrens * Instead wait for 5 seconds before checking again. 670283b8460SGeorge.Wilson */ 67169962b56SMatthew Ahrens when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms); 672283b8460SGeorge.Wilson when.cyt_when = CY_INFINITY; 673283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 674283b8460SGeorge.Wilson spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 675283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 676283b8460SGeorge.Wilson 677e914ace2STim Schumacher zfs_refcount_create(&spa->spa_refcount); 678e14bb325SJeff Bonwick spa_config_lock_init(spa); 679fa9e4066Sahrens 680fa9e4066Sahrens avl_add(&spa_namespace_avl, spa); 681fa9e4066Sahrens 6820373e76bSbonwick /* 6830373e76bSbonwick * Set the alternate root, if there is one. 6840373e76bSbonwick */ 6850373e76bSbonwick if (altroot) { 6860373e76bSbonwick spa->spa_root = spa_strdup(altroot); 6870373e76bSbonwick spa_active_count++; 6880373e76bSbonwick } 6890373e76bSbonwick 690f78cdc34SPaul Dagnelie spa->spa_alloc_count = spa_allocators; 691f78cdc34SPaul Dagnelie spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count * 692f78cdc34SPaul Dagnelie sizeof (kmutex_t), KM_SLEEP); 693f78cdc34SPaul Dagnelie spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count * 694f78cdc34SPaul Dagnelie sizeof (avl_tree_t), KM_SLEEP); 695f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) { 696f78cdc34SPaul Dagnelie mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL); 697f78cdc34SPaul Dagnelie avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare, 698f78cdc34SPaul Dagnelie sizeof (zio_t), offsetof(zio_t, io_alloc_node)); 699f78cdc34SPaul Dagnelie } 700814dcd43SSerapheim Dimitropoulos avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed, 701814dcd43SSerapheim Dimitropoulos sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node)); 702814dcd43SSerapheim Dimitropoulos avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg, 703814dcd43SSerapheim Dimitropoulos sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node)); 704814dcd43SSerapheim Dimitropoulos list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t), 705814dcd43SSerapheim Dimitropoulos offsetof(log_summary_entry_t, lse_node)); 7060f7643c7SGeorge Wilson 707c5904d13Seschrock /* 708c5904d13Seschrock * Every pool starts with the default cachefile 709c5904d13Seschrock */ 710c5904d13Seschrock list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 711c5904d13Seschrock offsetof(spa_config_dirent_t, scd_link)); 712c5904d13Seschrock 713c5904d13Seschrock dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 714ef912c80STim Haley dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 715c5904d13Seschrock list_insert_head(&spa->spa_config_list, dp); 716c5904d13Seschrock 7174b964adaSGeorge Wilson VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 7184b964adaSGeorge Wilson KM_SLEEP) == 0); 7194b964adaSGeorge Wilson 720ad135b5dSChristopher Siden if (config != NULL) { 721ad135b5dSChristopher Siden nvlist_t *features; 722ad135b5dSChristopher Siden 723ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 724ad135b5dSChristopher Siden &features) == 0) { 725ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features, 726ad135b5dSChristopher Siden 0) == 0); 727ad135b5dSChristopher Siden } 728ad135b5dSChristopher Siden 729468c413aSTim Haley VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 730ad135b5dSChristopher Siden } 731ad135b5dSChristopher Siden 732ad135b5dSChristopher Siden if (spa->spa_label_features == NULL) { 733ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 734ad135b5dSChristopher Siden KM_SLEEP) == 0); 735ad135b5dSChristopher Siden } 736468c413aSTim Haley 737c3a66015SMatthew Ahrens spa->spa_iokstat = kstat_create("zfs", 0, name, 738c3a66015SMatthew Ahrens "disk", KSTAT_TYPE_IO, 1, 0); 739c3a66015SMatthew Ahrens if (spa->spa_iokstat) { 740c3a66015SMatthew Ahrens spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock; 741c3a66015SMatthew Ahrens kstat_install(spa->spa_iokstat); 742c3a66015SMatthew Ahrens } 743c3a66015SMatthew Ahrens 74481cd5c55SMatthew Ahrens spa->spa_min_ashift = INT_MAX; 74581cd5c55SMatthew Ahrens spa->spa_max_ashift = 0; 74681cd5c55SMatthew Ahrens 74743466aaeSMax Grossman /* 74843466aaeSMax Grossman * As a pool is being created, treat all features as disabled by 74943466aaeSMax Grossman * setting SPA_FEATURE_DISABLED for all entries in the feature 75043466aaeSMax Grossman * refcount cache. 75143466aaeSMax Grossman */ 75243466aaeSMax Grossman for (int i = 0; i < SPA_FEATURES; i++) { 75343466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED; 75443466aaeSMax Grossman } 75543466aaeSMax Grossman 756e0f1c0afSOlaf Faaland list_create(&spa->spa_leaf_list, sizeof (vdev_t), 757e0f1c0afSOlaf Faaland offsetof(vdev_t, vdev_leaf_node)); 758e0f1c0afSOlaf Faaland 759fa9e4066Sahrens return (spa); 760fa9e4066Sahrens } 761fa9e4066Sahrens 762fa9e4066Sahrens /* 763fa9e4066Sahrens * Removes a spa_t from the namespace, freeing up any memory used. Requires 764fa9e4066Sahrens * spa_namespace_lock. This is called only after the spa_t has been closed and 765fa9e4066Sahrens * deactivated. 766fa9e4066Sahrens */ 767fa9e4066Sahrens void 768fa9e4066Sahrens spa_remove(spa_t *spa) 769fa9e4066Sahrens { 770c5904d13Seschrock spa_config_dirent_t *dp; 771c5904d13Seschrock 772fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 773814dcd43SSerapheim Dimitropoulos ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED); 774e914ace2STim Schumacher ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0); 775fa9e4066Sahrens 7761195e687SMark J Musante nvlist_free(spa->spa_config_splitting); 7771195e687SMark J Musante 778fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa); 779fa9e4066Sahrens cv_broadcast(&spa_namespace_cv); 780fa9e4066Sahrens 7810373e76bSbonwick if (spa->spa_root) { 782fa9e4066Sahrens spa_strfree(spa->spa_root); 7830373e76bSbonwick spa_active_count--; 7840373e76bSbonwick } 785fa9e4066Sahrens 786c5904d13Seschrock while ((dp = list_head(&spa->spa_config_list)) != NULL) { 787c5904d13Seschrock list_remove(&spa->spa_config_list, dp); 788c5904d13Seschrock if (dp->scd_path != NULL) 789c5904d13Seschrock spa_strfree(dp->scd_path); 790c5904d13Seschrock kmem_free(dp, sizeof (spa_config_dirent_t)); 791c5904d13Seschrock } 792c5904d13Seschrock 793f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) { 794f78cdc34SPaul Dagnelie avl_destroy(&spa->spa_alloc_trees[i]); 795f78cdc34SPaul Dagnelie mutex_destroy(&spa->spa_alloc_locks[i]); 796f78cdc34SPaul Dagnelie } 797f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count * 798f78cdc34SPaul Dagnelie sizeof (kmutex_t)); 799f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count * 800f78cdc34SPaul Dagnelie sizeof (avl_tree_t)); 801f78cdc34SPaul Dagnelie 802814dcd43SSerapheim Dimitropoulos avl_destroy(&spa->spa_metaslabs_by_flushed); 803814dcd43SSerapheim Dimitropoulos avl_destroy(&spa->spa_sm_logs_by_txg); 804814dcd43SSerapheim Dimitropoulos list_destroy(&spa->spa_log_summary); 805c5904d13Seschrock list_destroy(&spa->spa_config_list); 806e0f1c0afSOlaf Faaland list_destroy(&spa->spa_leaf_list); 8072f8aaab3Seschrock 808ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features); 8094b964adaSGeorge Wilson nvlist_free(spa->spa_load_info); 810fa9e4066Sahrens spa_config_set(spa, NULL); 811fa9e4066Sahrens 812283b8460SGeorge.Wilson mutex_enter(&cpu_lock); 813283b8460SGeorge.Wilson if (spa->spa_deadman_cycid != CYCLIC_NONE) 814283b8460SGeorge.Wilson cyclic_remove(spa->spa_deadman_cycid); 815283b8460SGeorge.Wilson mutex_exit(&cpu_lock); 816283b8460SGeorge.Wilson spa->spa_deadman_cycid = CYCLIC_NONE; 817283b8460SGeorge.Wilson 818e914ace2STim Schumacher zfs_refcount_destroy(&spa->spa_refcount); 81991ebeef5Sahrens 820e14bb325SJeff Bonwick spa_config_lock_destroy(spa); 821fa9e4066Sahrens 822c3a66015SMatthew Ahrens kstat_delete(spa->spa_iokstat); 823c3a66015SMatthew Ahrens spa->spa_iokstat = NULL; 824c3a66015SMatthew Ahrens 825b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++) 826cde58dbcSMatthew Ahrens bplist_destroy(&spa->spa_free_bplist[t]); 827b24ab676SJeff Bonwick 82845818ee1SMatthew Ahrens zio_checksum_templates_free(spa); 82945818ee1SMatthew Ahrens 830c25056deSgw cv_destroy(&spa->spa_async_cv); 831bc9014e6SJustin Gibbs cv_destroy(&spa->spa_evicting_os_cv); 83235a5a358SJonathan Adams cv_destroy(&spa->spa_proc_cv); 833c25056deSgw cv_destroy(&spa->spa_scrub_io_cv); 834e14bb325SJeff Bonwick cv_destroy(&spa->spa_suspend_cv); 835c25056deSgw 836814dcd43SSerapheim Dimitropoulos mutex_destroy(&spa->spa_flushed_ms_lock); 8375ad82045Snd mutex_destroy(&spa->spa_async_lock); 838c25056deSgw mutex_destroy(&spa->spa_errlist_lock); 83935a5a358SJonathan Adams mutex_destroy(&spa->spa_errlog_lock); 840bc9014e6SJustin Gibbs mutex_destroy(&spa->spa_evicting_os_lock); 84106eeb2adSek mutex_destroy(&spa->spa_history_lock); 84235a5a358SJonathan Adams mutex_destroy(&spa->spa_proc_lock); 843b1b8ab34Slling mutex_destroy(&spa->spa_props_lock); 84445818ee1SMatthew Ahrens mutex_destroy(&spa->spa_cksum_tmpls_lock); 84535a5a358SJonathan Adams mutex_destroy(&spa->spa_scrub_lock); 846e14bb325SJeff Bonwick mutex_destroy(&spa->spa_suspend_lock); 847a1521560SJeff Bonwick mutex_destroy(&spa->spa_vdev_top_lock); 848c3a66015SMatthew Ahrens mutex_destroy(&spa->spa_iokstat_lock); 849*dd50e0ccSTony Hutter mutex_destroy(&spa->spa_imp_kstat_lock); 8505ad82045Snd 851fa9e4066Sahrens kmem_free(spa, sizeof (spa_t)); 852fa9e4066Sahrens } 853fa9e4066Sahrens 854fa9e4066Sahrens /* 855fa9e4066Sahrens * Given a pool, return the next pool in the namespace, or NULL if there is 856fa9e4066Sahrens * none. If 'prev' is NULL, return the first pool. 857fa9e4066Sahrens */ 858fa9e4066Sahrens spa_t * 859fa9e4066Sahrens spa_next(spa_t *prev) 860fa9e4066Sahrens { 861fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 862fa9e4066Sahrens 863fa9e4066Sahrens if (prev) 864fa9e4066Sahrens return (AVL_NEXT(&spa_namespace_avl, prev)); 865fa9e4066Sahrens else 866fa9e4066Sahrens return (avl_first(&spa_namespace_avl)); 867fa9e4066Sahrens } 868fa9e4066Sahrens 869fa9e4066Sahrens /* 870fa9e4066Sahrens * ========================================================================== 871fa9e4066Sahrens * SPA refcount functions 872fa9e4066Sahrens * ========================================================================== 873fa9e4066Sahrens */ 874fa9e4066Sahrens 875fa9e4066Sahrens /* 876fa9e4066Sahrens * Add a reference to the given spa_t. Must have at least one reference, or 877fa9e4066Sahrens * have the namespace lock held. 878fa9e4066Sahrens */ 879fa9e4066Sahrens void 880fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag) 881fa9e4066Sahrens { 882e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref || 883fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 884e914ace2STim Schumacher (void) zfs_refcount_add(&spa->spa_refcount, tag); 885fa9e4066Sahrens } 886fa9e4066Sahrens 887fa9e4066Sahrens /* 888fa9e4066Sahrens * Remove a reference to the given spa_t. Must have at least one reference, or 889fa9e4066Sahrens * have the namespace lock held. 890fa9e4066Sahrens */ 891fa9e4066Sahrens void 892fa9e4066Sahrens spa_close(spa_t *spa, void *tag) 893fa9e4066Sahrens { 894e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref || 895fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock)); 896e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag); 897fa9e4066Sahrens } 898fa9e4066Sahrens 899bc9014e6SJustin Gibbs /* 900bc9014e6SJustin Gibbs * Remove a reference to the given spa_t held by a dsl dir that is 901bc9014e6SJustin Gibbs * being asynchronously released. Async releases occur from a taskq 902bc9014e6SJustin Gibbs * performing eviction of dsl datasets and dirs. The namespace lock 903bc9014e6SJustin Gibbs * isn't held and the hold by the object being evicted may contribute to 904bc9014e6SJustin Gibbs * spa_minref (e.g. dataset or directory released during pool export), 905bc9014e6SJustin Gibbs * so the asserts in spa_close() do not apply. 906bc9014e6SJustin Gibbs */ 907bc9014e6SJustin Gibbs void 908bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag) 909bc9014e6SJustin Gibbs { 910e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag); 911bc9014e6SJustin Gibbs } 912bc9014e6SJustin Gibbs 913fa9e4066Sahrens /* 914fa9e4066Sahrens * Check to see if the spa refcount is zero. Must be called with 915088f3894Sahrens * spa_namespace_lock held. We really compare against spa_minref, which is the 916fa9e4066Sahrens * number of references acquired when opening a pool 917fa9e4066Sahrens */ 918fa9e4066Sahrens boolean_t 919fa9e4066Sahrens spa_refcount_zero(spa_t *spa) 920fa9e4066Sahrens { 921fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 922fa9e4066Sahrens 923e914ace2STim Schumacher return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref); 924fa9e4066Sahrens } 925fa9e4066Sahrens 92699653d4eSeschrock /* 92799653d4eSeschrock * ========================================================================== 928fa94a07fSbrendan * SPA spare and l2cache tracking 92999653d4eSeschrock * ========================================================================== 93099653d4eSeschrock */ 93199653d4eSeschrock 932fa94a07fSbrendan /* 933fa94a07fSbrendan * Hot spares and cache devices are tracked using the same code below, 934fa94a07fSbrendan * for 'auxiliary' devices. 935fa94a07fSbrendan */ 936fa94a07fSbrendan 937fa94a07fSbrendan typedef struct spa_aux { 938fa94a07fSbrendan uint64_t aux_guid; 939fa94a07fSbrendan uint64_t aux_pool; 940fa94a07fSbrendan avl_node_t aux_avl; 941fa94a07fSbrendan int aux_count; 942fa94a07fSbrendan } spa_aux_t; 943fa94a07fSbrendan 944c4ab0d3fSGvozden Neskovic static inline int 945fa94a07fSbrendan spa_aux_compare(const void *a, const void *b) 946fa94a07fSbrendan { 947c4ab0d3fSGvozden Neskovic const spa_aux_t *sa = (const spa_aux_t *)a; 948c4ab0d3fSGvozden Neskovic const spa_aux_t *sb = (const spa_aux_t *)b; 949fa94a07fSbrendan 9504d7988d6SPaul Dagnelie return (TREE_CMP(sa->aux_guid, sb->aux_guid)); 951fa94a07fSbrendan } 952fa94a07fSbrendan 953fa94a07fSbrendan void 954fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl) 955fa94a07fSbrendan { 956fa94a07fSbrendan avl_index_t where; 957fa94a07fSbrendan spa_aux_t search; 958fa94a07fSbrendan spa_aux_t *aux; 959fa94a07fSbrendan 960fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 961fa94a07fSbrendan if ((aux = avl_find(avl, &search, &where)) != NULL) { 962fa94a07fSbrendan aux->aux_count++; 963fa94a07fSbrendan } else { 964fa94a07fSbrendan aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 965fa94a07fSbrendan aux->aux_guid = vd->vdev_guid; 966fa94a07fSbrendan aux->aux_count = 1; 967fa94a07fSbrendan avl_insert(avl, aux, where); 968fa94a07fSbrendan } 969fa94a07fSbrendan } 970fa94a07fSbrendan 971fa94a07fSbrendan void 972fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 973fa94a07fSbrendan { 974fa94a07fSbrendan spa_aux_t search; 975fa94a07fSbrendan spa_aux_t *aux; 976fa94a07fSbrendan avl_index_t where; 977fa94a07fSbrendan 978fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 979fa94a07fSbrendan aux = avl_find(avl, &search, &where); 980fa94a07fSbrendan 981fa94a07fSbrendan ASSERT(aux != NULL); 982fa94a07fSbrendan 983fa94a07fSbrendan if (--aux->aux_count == 0) { 984fa94a07fSbrendan avl_remove(avl, aux); 985fa94a07fSbrendan kmem_free(aux, sizeof (spa_aux_t)); 986fa94a07fSbrendan } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 987fa94a07fSbrendan aux->aux_pool = 0ULL; 988fa94a07fSbrendan } 989fa94a07fSbrendan } 990fa94a07fSbrendan 991fa94a07fSbrendan boolean_t 99289a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 993fa94a07fSbrendan { 994fa94a07fSbrendan spa_aux_t search, *found; 995fa94a07fSbrendan 996fa94a07fSbrendan search.aux_guid = guid; 99789a89ebfSlling found = avl_find(avl, &search, NULL); 998fa94a07fSbrendan 999fa94a07fSbrendan if (pool) { 1000fa94a07fSbrendan if (found) 1001fa94a07fSbrendan *pool = found->aux_pool; 1002fa94a07fSbrendan else 1003fa94a07fSbrendan *pool = 0ULL; 1004fa94a07fSbrendan } 1005fa94a07fSbrendan 100689a89ebfSlling if (refcnt) { 100789a89ebfSlling if (found) 100889a89ebfSlling *refcnt = found->aux_count; 100989a89ebfSlling else 101089a89ebfSlling *refcnt = 0; 101189a89ebfSlling } 101289a89ebfSlling 1013fa94a07fSbrendan return (found != NULL); 1014fa94a07fSbrendan } 1015fa94a07fSbrendan 1016fa94a07fSbrendan void 1017fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 1018fa94a07fSbrendan { 1019fa94a07fSbrendan spa_aux_t search, *found; 1020fa94a07fSbrendan avl_index_t where; 1021fa94a07fSbrendan 1022fa94a07fSbrendan search.aux_guid = vd->vdev_guid; 1023fa94a07fSbrendan found = avl_find(avl, &search, &where); 1024fa94a07fSbrendan ASSERT(found != NULL); 1025fa94a07fSbrendan ASSERT(found->aux_pool == 0ULL); 1026fa94a07fSbrendan 1027fa94a07fSbrendan found->aux_pool = spa_guid(vd->vdev_spa); 1028fa94a07fSbrendan } 1029fa94a07fSbrendan 103099653d4eSeschrock /* 103139c23413Seschrock * Spares are tracked globally due to the following constraints: 103239c23413Seschrock * 103354811da5SToomas Soome * - A spare may be part of multiple pools. 103454811da5SToomas Soome * - A spare may be added to a pool even if it's actively in use within 103539c23413Seschrock * another pool. 103654811da5SToomas Soome * - A spare in use in any pool can only be the source of a replacement if 103739c23413Seschrock * the target is a spare in the same pool. 103839c23413Seschrock * 103939c23413Seschrock * We keep track of all spares on the system through the use of a reference 104039c23413Seschrock * counted AVL tree. When a vdev is added as a spare, or used as a replacement 104139c23413Seschrock * spare, then we bump the reference count in the AVL tree. In addition, we set 104239c23413Seschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or 104339c23413Seschrock * inactive). When a spare is made active (used to replace a device in the 104439c23413Seschrock * pool), we also keep track of which pool its been made a part of. 104539c23413Seschrock * 104639c23413Seschrock * The 'spa_spare_lock' protects the AVL tree. These functions are normally 104739c23413Seschrock * called under the spa_namespace lock as part of vdev reconfiguration. The 104839c23413Seschrock * separate spare lock exists for the status query path, which does not need to 104939c23413Seschrock * be completely consistent with respect to other vdev configuration changes. 105099653d4eSeschrock */ 105139c23413Seschrock 1052e830fb12SKody A Kantor /* 1053e830fb12SKody A Kantor * Poll the spare vdevs to make sure they are not faulty. 1054e830fb12SKody A Kantor * 1055e830fb12SKody A Kantor * The probe operation will raise an ENXIO error and create an FM ereport if the 1056e830fb12SKody A Kantor * probe fails. 1057e830fb12SKody A Kantor */ 1058e830fb12SKody A Kantor void 1059e830fb12SKody A Kantor spa_spare_poll(spa_t *spa) 1060e830fb12SKody A Kantor { 1061e830fb12SKody A Kantor boolean_t async_request = B_FALSE; 1062e830fb12SKody A Kantor spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1063e830fb12SKody A Kantor for (int i = 0; i < spa->spa_spares.sav_count; i++) { 1064e830fb12SKody A Kantor spa_aux_t search, *found; 1065e830fb12SKody A Kantor vdev_t *vd = spa->spa_spares.sav_vdevs[i]; 1066e830fb12SKody A Kantor 1067e830fb12SKody A Kantor search.aux_guid = vd->vdev_guid; 1068e830fb12SKody A Kantor 1069e830fb12SKody A Kantor mutex_enter(&spa_spare_lock); 1070e830fb12SKody A Kantor found = avl_find(&spa_spare_avl, &search, NULL); 1071e830fb12SKody A Kantor /* This spare is in use by a pool. */ 1072e830fb12SKody A Kantor if (found != NULL && found->aux_pool != 0) { 1073e830fb12SKody A Kantor mutex_exit(&spa_spare_lock); 1074e830fb12SKody A Kantor continue; 1075e830fb12SKody A Kantor } 1076e830fb12SKody A Kantor mutex_exit(&spa_spare_lock); 1077e830fb12SKody A Kantor 1078e830fb12SKody A Kantor vd->vdev_probe_wanted = B_TRUE; 1079e830fb12SKody A Kantor async_request = B_TRUE; 1080e830fb12SKody A Kantor } 1081e830fb12SKody A Kantor if (async_request) 1082e830fb12SKody A Kantor spa_async_request(spa, SPA_ASYNC_PROBE); 1083e830fb12SKody A Kantor 1084e830fb12SKody A Kantor spa_config_exit(spa, SCL_STATE, FTAG); 1085e830fb12SKody A Kantor } 1086e830fb12SKody A Kantor 108799653d4eSeschrock static int 108899653d4eSeschrock spa_spare_compare(const void *a, const void *b) 108999653d4eSeschrock { 1090fa94a07fSbrendan return (spa_aux_compare(a, b)); 109199653d4eSeschrock } 109299653d4eSeschrock 109399653d4eSeschrock void 109439c23413Seschrock spa_spare_add(vdev_t *vd) 109599653d4eSeschrock { 109699653d4eSeschrock mutex_enter(&spa_spare_lock); 109739c23413Seschrock ASSERT(!vd->vdev_isspare); 1098fa94a07fSbrendan spa_aux_add(vd, &spa_spare_avl); 109939c23413Seschrock vd->vdev_isspare = B_TRUE; 110099653d4eSeschrock mutex_exit(&spa_spare_lock); 110199653d4eSeschrock } 110299653d4eSeschrock 110399653d4eSeschrock void 110439c23413Seschrock spa_spare_remove(vdev_t *vd) 110599653d4eSeschrock { 110699653d4eSeschrock mutex_enter(&spa_spare_lock); 110739c23413Seschrock ASSERT(vd->vdev_isspare); 1108fa94a07fSbrendan spa_aux_remove(vd, &spa_spare_avl); 110939c23413Seschrock vd->vdev_isspare = B_FALSE; 111099653d4eSeschrock mutex_exit(&spa_spare_lock); 111199653d4eSeschrock } 111299653d4eSeschrock 111399653d4eSeschrock boolean_t 111489a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 111599653d4eSeschrock { 1116fa94a07fSbrendan boolean_t found; 111799653d4eSeschrock 111899653d4eSeschrock mutex_enter(&spa_spare_lock); 111989a89ebfSlling found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 112099653d4eSeschrock mutex_exit(&spa_spare_lock); 112199653d4eSeschrock 1122fa94a07fSbrendan return (found); 112339c23413Seschrock } 112439c23413Seschrock 112539c23413Seschrock void 112639c23413Seschrock spa_spare_activate(vdev_t *vd) 112739c23413Seschrock { 112839c23413Seschrock mutex_enter(&spa_spare_lock); 112939c23413Seschrock ASSERT(vd->vdev_isspare); 1130fa94a07fSbrendan spa_aux_activate(vd, &spa_spare_avl); 1131fa94a07fSbrendan mutex_exit(&spa_spare_lock); 1132fa94a07fSbrendan } 113339c23413Seschrock 1134fa94a07fSbrendan /* 1135fa94a07fSbrendan * Level 2 ARC devices are tracked globally for the same reasons as spares. 1136fa94a07fSbrendan * Cache devices currently only support one pool per cache device, and so 1137fa94a07fSbrendan * for these devices the aux reference count is currently unused beyond 1. 1138fa94a07fSbrendan */ 113939c23413Seschrock 1140fa94a07fSbrendan static int 1141fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b) 1142fa94a07fSbrendan { 1143fa94a07fSbrendan return (spa_aux_compare(a, b)); 1144fa94a07fSbrendan } 1145fa94a07fSbrendan 1146fa94a07fSbrendan void 1147fa94a07fSbrendan spa_l2cache_add(vdev_t *vd) 1148fa94a07fSbrendan { 1149fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1150fa94a07fSbrendan ASSERT(!vd->vdev_isl2cache); 1151fa94a07fSbrendan spa_aux_add(vd, &spa_l2cache_avl); 1152fa94a07fSbrendan vd->vdev_isl2cache = B_TRUE; 1153fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1154fa94a07fSbrendan } 1155fa94a07fSbrendan 1156fa94a07fSbrendan void 1157fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd) 1158fa94a07fSbrendan { 1159fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1160fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1161fa94a07fSbrendan spa_aux_remove(vd, &spa_l2cache_avl); 1162fa94a07fSbrendan vd->vdev_isl2cache = B_FALSE; 1163fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1164fa94a07fSbrendan } 1165fa94a07fSbrendan 1166fa94a07fSbrendan boolean_t 1167fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool) 1168fa94a07fSbrendan { 1169fa94a07fSbrendan boolean_t found; 1170fa94a07fSbrendan 1171fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 117289a89ebfSlling found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 1173fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1174fa94a07fSbrendan 1175fa94a07fSbrendan return (found); 1176fa94a07fSbrendan } 1177fa94a07fSbrendan 1178fa94a07fSbrendan void 1179fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd) 1180fa94a07fSbrendan { 1181fa94a07fSbrendan mutex_enter(&spa_l2cache_lock); 1182fa94a07fSbrendan ASSERT(vd->vdev_isl2cache); 1183fa94a07fSbrendan spa_aux_activate(vd, &spa_l2cache_avl); 1184fa94a07fSbrendan mutex_exit(&spa_l2cache_lock); 1185fa94a07fSbrendan } 1186fa94a07fSbrendan 1187fa9e4066Sahrens /* 1188fa9e4066Sahrens * ========================================================================== 1189fa9e4066Sahrens * SPA vdev locking 1190fa9e4066Sahrens * ========================================================================== 1191fa9e4066Sahrens */ 1192fa9e4066Sahrens 1193fa9e4066Sahrens /* 1194ea8dc4b6Seschrock * Lock the given spa_t for the purpose of adding or removing a vdev. 1195ea8dc4b6Seschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing. 1196fa9e4066Sahrens * It returns the next transaction group for the spa_t. 1197fa9e4066Sahrens */ 1198fa9e4066Sahrens uint64_t 1199fa9e4066Sahrens spa_vdev_enter(spa_t *spa) 1200fa9e4066Sahrens { 1201a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock); 1202bbfd46c4SJeff Bonwick mutex_enter(&spa_namespace_lock); 1203084fd14fSBrian Behlendorf 1204084fd14fSBrian Behlendorf vdev_autotrim_stop_all(spa); 1205084fd14fSBrian Behlendorf 120688ecc943SGeorge Wilson return (spa_vdev_config_enter(spa)); 120788ecc943SGeorge Wilson } 120888ecc943SGeorge Wilson 120988ecc943SGeorge Wilson /* 121088ecc943SGeorge Wilson * Internal implementation for spa_vdev_enter(). Used when a vdev 121188ecc943SGeorge Wilson * operation requires multiple syncs (i.e. removing a device) while 121288ecc943SGeorge Wilson * keeping the spa_namespace_lock held. 121388ecc943SGeorge Wilson */ 121488ecc943SGeorge Wilson uint64_t 121588ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa) 121688ecc943SGeorge Wilson { 121788ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 12183d7072f8Seschrock 1219e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1220fa9e4066Sahrens 1221fa9e4066Sahrens return (spa_last_synced_txg(spa) + 1); 1222fa9e4066Sahrens } 1223fa9e4066Sahrens 1224fa9e4066Sahrens /* 122588ecc943SGeorge Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing 122688ecc943SGeorge Wilson * of multiple transactions without releasing the spa_namespace_lock. 1227fa9e4066Sahrens */ 122888ecc943SGeorge Wilson void 122988ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 1230fa9e4066Sahrens { 123188ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock)); 123288ecc943SGeorge Wilson 12330e34b6a7Sbonwick int config_changed = B_FALSE; 1234ea8dc4b6Seschrock 12350373e76bSbonwick ASSERT(txg > spa_last_synced_txg(spa)); 12360e34b6a7Sbonwick 1237e14bb325SJeff Bonwick spa->spa_pending_vdev = NULL; 1238e14bb325SJeff Bonwick 12390e34b6a7Sbonwick /* 12400e34b6a7Sbonwick * Reassess the DTLs. 12410e34b6a7Sbonwick */ 12420373e76bSbonwick vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 12430e34b6a7Sbonwick 1244e14bb325SJeff Bonwick if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 12450e34b6a7Sbonwick config_changed = B_TRUE; 12468f18d1faSGeorge Wilson spa->spa_config_generation++; 12470e34b6a7Sbonwick } 1248ea8dc4b6Seschrock 124988ecc943SGeorge Wilson /* 125088ecc943SGeorge Wilson * Verify the metaslab classes. 125188ecc943SGeorge Wilson */ 1252b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1253b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 1254663207adSDon Brady ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0); 1255663207adSDon Brady ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0); 125688ecc943SGeorge Wilson 1257e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1258fa9e4066Sahrens 125988ecc943SGeorge Wilson /* 126088ecc943SGeorge Wilson * Panic the system if the specified tag requires it. This 126188ecc943SGeorge Wilson * is useful for ensuring that configurations are updated 126288ecc943SGeorge Wilson * transactionally. 126388ecc943SGeorge Wilson */ 126488ecc943SGeorge Wilson if (zio_injection_enabled) 12651195e687SMark J Musante zio_handle_panic_injection(spa, tag, 0); 126688ecc943SGeorge Wilson 1267fa9e4066Sahrens /* 1268fa9e4066Sahrens * Note: this txg_wait_synced() is important because it ensures 1269fa9e4066Sahrens * that there won't be more than one config change per txg. 1270fa9e4066Sahrens * This allows us to use the txg as the generation number. 1271fa9e4066Sahrens */ 1272fa9e4066Sahrens if (error == 0) 1273fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg); 1274fa9e4066Sahrens 1275fa9e4066Sahrens if (vd != NULL) { 12760713e232SGeorge Wilson ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL); 1277094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) { 1278094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock); 1279084fd14fSBrian Behlendorf vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, 1280084fd14fSBrian Behlendorf NULL); 1281094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock); 1282084fd14fSBrian Behlendorf 1283084fd14fSBrian Behlendorf mutex_enter(&vd->vdev_trim_lock); 1284084fd14fSBrian Behlendorf vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL); 1285084fd14fSBrian Behlendorf mutex_exit(&vd->vdev_trim_lock); 1286094e47e9SGeorge Wilson } 1287094e47e9SGeorge Wilson 1288084fd14fSBrian Behlendorf /* 1289084fd14fSBrian Behlendorf * The vdev may be both a leaf and top-level device. 1290084fd14fSBrian Behlendorf */ 1291084fd14fSBrian Behlendorf vdev_autotrim_stop_wait(vd); 1292084fd14fSBrian Behlendorf 12938ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1294fa9e4066Sahrens vdev_free(vd); 12958ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, spa); 1296fa9e4066Sahrens } 1297fa9e4066Sahrens 1298fa9e4066Sahrens /* 12990e34b6a7Sbonwick * If the config changed, update the config cache. 1300fa9e4066Sahrens */ 13010e34b6a7Sbonwick if (config_changed) 13025cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 130388ecc943SGeorge Wilson } 1304ea8dc4b6Seschrock 130588ecc943SGeorge Wilson /* 130688ecc943SGeorge Wilson * Unlock the spa_t after adding or removing a vdev. Besides undoing the 130788ecc943SGeorge Wilson * locking of spa_vdev_enter(), we also want make sure the transactions have 130888ecc943SGeorge Wilson * synced to disk, and then update the global configuration cache with the new 130988ecc943SGeorge Wilson * information. 131088ecc943SGeorge Wilson */ 131188ecc943SGeorge Wilson int 131288ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 131388ecc943SGeorge Wilson { 1314084fd14fSBrian Behlendorf vdev_autotrim_restart(spa); 1315084fd14fSBrian Behlendorf 131688ecc943SGeorge Wilson spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1317ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock); 1318bbfd46c4SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock); 1319fa9e4066Sahrens 1320fa9e4066Sahrens return (error); 1321fa9e4066Sahrens } 1322fa9e4066Sahrens 1323e14bb325SJeff Bonwick /* 1324e14bb325SJeff Bonwick * Lock the given spa_t for the purpose of changing vdev state. 1325e14bb325SJeff Bonwick */ 1326e14bb325SJeff Bonwick void 13278f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks) 1328e14bb325SJeff Bonwick { 13298f18d1faSGeorge Wilson int locks = SCL_STATE_ALL | oplocks; 13308f18d1faSGeorge Wilson 1331dcba9f3fSGeorge Wilson /* 1332dcba9f3fSGeorge Wilson * Root pools may need to read of the underlying devfs filesystem 1333dcba9f3fSGeorge Wilson * when opening up a vdev. Unfortunately if we're holding the 1334dcba9f3fSGeorge Wilson * SCL_ZIO lock it will result in a deadlock when we try to issue 1335dcba9f3fSGeorge Wilson * the read from the root filesystem. Instead we "prefetch" 1336dcba9f3fSGeorge Wilson * the associated vnodes that we need prior to opening the 1337dcba9f3fSGeorge Wilson * underlying devices and cache them so that we can prevent 1338dcba9f3fSGeorge Wilson * any I/O when we are doing the actual open. 1339dcba9f3fSGeorge Wilson */ 1340dcba9f3fSGeorge Wilson if (spa_is_root(spa)) { 13419842588bSGeorge Wilson int low = locks & ~(SCL_ZIO - 1); 13429842588bSGeorge Wilson int high = locks & ~low; 13439842588bSGeorge Wilson 13449842588bSGeorge Wilson spa_config_enter(spa, high, spa, RW_WRITER); 1345dcba9f3fSGeorge Wilson vdev_hold(spa->spa_root_vdev); 13469842588bSGeorge Wilson spa_config_enter(spa, low, spa, RW_WRITER); 1347dcba9f3fSGeorge Wilson } else { 1348dcba9f3fSGeorge Wilson spa_config_enter(spa, locks, spa, RW_WRITER); 1349dcba9f3fSGeorge Wilson } 13508f18d1faSGeorge Wilson spa->spa_vdev_locks = locks; 1351e14bb325SJeff Bonwick } 1352e14bb325SJeff Bonwick 1353e14bb325SJeff Bonwick int 1354e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1355e14bb325SJeff Bonwick { 1356c6065d0fSGeorge Wilson boolean_t config_changed = B_FALSE; 1357c6065d0fSGeorge Wilson 1358b24ab676SJeff Bonwick if (vd != NULL || error == 0) 1359b24ab676SJeff Bonwick vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1360b24ab676SJeff Bonwick 0, 0, B_FALSE); 1361b24ab676SJeff Bonwick 13628f18d1faSGeorge Wilson if (vd != NULL) { 1363e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top); 1364c6065d0fSGeorge Wilson config_changed = B_TRUE; 13658f18d1faSGeorge Wilson spa->spa_config_generation++; 13668f18d1faSGeorge Wilson } 1367e14bb325SJeff Bonwick 1368dcba9f3fSGeorge Wilson if (spa_is_root(spa)) 1369dcba9f3fSGeorge Wilson vdev_rele(spa->spa_root_vdev); 1370dcba9f3fSGeorge Wilson 13718f18d1faSGeorge Wilson ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 13728f18d1faSGeorge Wilson spa_config_exit(spa, spa->spa_vdev_locks, spa); 1373e14bb325SJeff Bonwick 13748ad4d6ddSJeff Bonwick /* 13758ad4d6ddSJeff Bonwick * If anything changed, wait for it to sync. This ensures that, 13768ad4d6ddSJeff Bonwick * from the system administrator's perspective, zpool(1M) commands 13778ad4d6ddSJeff Bonwick * are synchronous. This is important for things like zpool offline: 13788ad4d6ddSJeff Bonwick * when the command completes, you expect no further I/O from ZFS. 13798ad4d6ddSJeff Bonwick */ 13808ad4d6ddSJeff Bonwick if (vd != NULL) 13818ad4d6ddSJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, 0); 13828ad4d6ddSJeff Bonwick 1383c6065d0fSGeorge Wilson /* 1384c6065d0fSGeorge Wilson * If the config changed, update the config cache. 1385c6065d0fSGeorge Wilson */ 1386c6065d0fSGeorge Wilson if (config_changed) { 1387c6065d0fSGeorge Wilson mutex_enter(&spa_namespace_lock); 13885cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE); 1389c6065d0fSGeorge Wilson mutex_exit(&spa_namespace_lock); 1390c6065d0fSGeorge Wilson } 1391c6065d0fSGeorge Wilson 1392e14bb325SJeff Bonwick return (error); 1393e14bb325SJeff Bonwick } 1394e14bb325SJeff Bonwick 1395fa9e4066Sahrens /* 1396fa9e4066Sahrens * ========================================================================== 1397fa9e4066Sahrens * Miscellaneous functions 1398fa9e4066Sahrens * ========================================================================== 1399fa9e4066Sahrens */ 1400fa9e4066Sahrens 1401ad135b5dSChristopher Siden void 140243466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx) 1403ad135b5dSChristopher Siden { 14042acef22dSMatthew Ahrens if (!nvlist_exists(spa->spa_label_features, feature)) { 14052acef22dSMatthew Ahrens fnvlist_add_boolean(spa->spa_label_features, feature); 140643466aaeSMax Grossman /* 140743466aaeSMax Grossman * When we are creating the pool (tx_txg==TXG_INITIAL), we can't 140843466aaeSMax Grossman * dirty the vdev config because lock SCL_CONFIG is not held. 140943466aaeSMax Grossman * Thankfully, in this case we don't need to dirty the config 141043466aaeSMax Grossman * because it will be written out anyway when we finish 141143466aaeSMax Grossman * creating the pool. 141243466aaeSMax Grossman */ 141343466aaeSMax Grossman if (tx->tx_txg != TXG_INITIAL) 141443466aaeSMax Grossman vdev_config_dirty(spa->spa_root_vdev); 14152acef22dSMatthew Ahrens } 1416ad135b5dSChristopher Siden } 1417ad135b5dSChristopher Siden 1418ad135b5dSChristopher Siden void 1419ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1420ad135b5dSChristopher Siden { 14212acef22dSMatthew Ahrens if (nvlist_remove_all(spa->spa_label_features, feature) == 0) 14222acef22dSMatthew Ahrens vdev_config_dirty(spa->spa_root_vdev); 1423ad135b5dSChristopher Siden } 1424ad135b5dSChristopher Siden 1425fa9e4066Sahrens /* 1426f9af39baSGeorge Wilson * Return the spa_t associated with given pool_guid, if it exists. If 1427f9af39baSGeorge Wilson * device_guid is non-zero, determine whether the pool exists *and* contains 1428f9af39baSGeorge Wilson * a device with the specified device_guid. 1429fa9e4066Sahrens */ 1430f9af39baSGeorge Wilson spa_t * 1431f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1432fa9e4066Sahrens { 1433fa9e4066Sahrens spa_t *spa; 1434fa9e4066Sahrens avl_tree_t *t = &spa_namespace_avl; 1435fa9e4066Sahrens 1436ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1437fa9e4066Sahrens 1438fa9e4066Sahrens for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1439fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1440fa9e4066Sahrens continue; 1441fa9e4066Sahrens if (spa->spa_root_vdev == NULL) 1442fa9e4066Sahrens continue; 144339c23413Seschrock if (spa_guid(spa) == pool_guid) { 144439c23413Seschrock if (device_guid == 0) 144539c23413Seschrock break; 144639c23413Seschrock 144739c23413Seschrock if (vdev_lookup_by_guid(spa->spa_root_vdev, 144839c23413Seschrock device_guid) != NULL) 144939c23413Seschrock break; 145039c23413Seschrock 145139c23413Seschrock /* 14528654d025Sperrin * Check any devices we may be in the process of adding. 145339c23413Seschrock */ 145439c23413Seschrock if (spa->spa_pending_vdev) { 145539c23413Seschrock if (vdev_lookup_by_guid(spa->spa_pending_vdev, 145639c23413Seschrock device_guid) != NULL) 145739c23413Seschrock break; 145839c23413Seschrock } 145939c23413Seschrock } 1460fa9e4066Sahrens } 1461fa9e4066Sahrens 1462f9af39baSGeorge Wilson return (spa); 1463f9af39baSGeorge Wilson } 1464f9af39baSGeorge Wilson 1465f9af39baSGeorge Wilson /* 1466f9af39baSGeorge Wilson * Determine whether a pool with the given pool_guid exists. 1467f9af39baSGeorge Wilson */ 1468f9af39baSGeorge Wilson boolean_t 1469f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1470f9af39baSGeorge Wilson { 1471f9af39baSGeorge Wilson return (spa_by_guid(pool_guid, device_guid) != NULL); 1472fa9e4066Sahrens } 1473fa9e4066Sahrens 1474fa9e4066Sahrens char * 1475fa9e4066Sahrens spa_strdup(const char *s) 1476fa9e4066Sahrens { 1477fa9e4066Sahrens size_t len; 1478fa9e4066Sahrens char *new; 1479fa9e4066Sahrens 1480fa9e4066Sahrens len = strlen(s); 1481fa9e4066Sahrens new = kmem_alloc(len + 1, KM_SLEEP); 1482fa9e4066Sahrens bcopy(s, new, len); 1483fa9e4066Sahrens new[len] = '\0'; 1484fa9e4066Sahrens 1485fa9e4066Sahrens return (new); 1486fa9e4066Sahrens } 1487fa9e4066Sahrens 1488fa9e4066Sahrens void 1489fa9e4066Sahrens spa_strfree(char *s) 1490fa9e4066Sahrens { 1491fa9e4066Sahrens kmem_free(s, strlen(s) + 1); 1492fa9e4066Sahrens } 1493fa9e4066Sahrens 1494fa9e4066Sahrens uint64_t 1495fa9e4066Sahrens spa_get_random(uint64_t range) 1496fa9e4066Sahrens { 1497fa9e4066Sahrens uint64_t r; 1498fa9e4066Sahrens 1499fa9e4066Sahrens ASSERT(range != 0); 1500fa9e4066Sahrens 1501e0f1c0afSOlaf Faaland if (range == 1) 1502e0f1c0afSOlaf Faaland return (0); 1503e0f1c0afSOlaf Faaland 1504fa9e4066Sahrens (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1505fa9e4066Sahrens 1506fa9e4066Sahrens return (r % range); 1507fa9e4066Sahrens } 1508fa9e4066Sahrens 15091195e687SMark J Musante uint64_t 15101195e687SMark J Musante spa_generate_guid(spa_t *spa) 15111195e687SMark J Musante { 15121195e687SMark J Musante uint64_t guid = spa_get_random(-1ULL); 15131195e687SMark J Musante 15141195e687SMark J Musante if (spa != NULL) { 15151195e687SMark J Musante while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 15161195e687SMark J Musante guid = spa_get_random(-1ULL); 15171195e687SMark J Musante } else { 15181195e687SMark J Musante while (guid == 0 || spa_guid_exists(guid, 0)) 15191195e687SMark J Musante guid = spa_get_random(-1ULL); 15201195e687SMark J Musante } 15211195e687SMark J Musante 15221195e687SMark J Musante return (guid); 15231195e687SMark J Musante } 15241195e687SMark J Musante 1525fa9e4066Sahrens void 152643466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp) 1527fa9e4066Sahrens { 1528ad135b5dSChristopher Siden char type[256]; 1529f0ba89beSJeff Bonwick char *checksum = NULL; 1530f0ba89beSJeff Bonwick char *compress = NULL; 1531f0ba89beSJeff Bonwick 1532f0ba89beSJeff Bonwick if (bp != NULL) { 1533ad135b5dSChristopher Siden if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1534ad135b5dSChristopher Siden dmu_object_byteswap_t bswap = 1535ad135b5dSChristopher Siden DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1536ad135b5dSChristopher Siden (void) snprintf(type, sizeof (type), "bswap %s %s", 1537ad135b5dSChristopher Siden DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1538ad135b5dSChristopher Siden "metadata" : "data", 1539ad135b5dSChristopher Siden dmu_ot_byteswap[bswap].ob_name); 1540ad135b5dSChristopher Siden } else { 1541ad135b5dSChristopher Siden (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1542ad135b5dSChristopher Siden sizeof (type)); 1543ad135b5dSChristopher Siden } 15445d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) { 15455d7b4d43SMatthew Ahrens checksum = 15465d7b4d43SMatthew Ahrens zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 15475d7b4d43SMatthew Ahrens } 1548f0ba89beSJeff Bonwick compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1549f0ba89beSJeff Bonwick } 1550fa9e4066Sahrens 155143466aaeSMax Grossman SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum, 155243466aaeSMax Grossman compress); 1553fa9e4066Sahrens } 1554fa9e4066Sahrens 1555fa9e4066Sahrens void 1556fa9e4066Sahrens spa_freeze(spa_t *spa) 1557fa9e4066Sahrens { 1558fa9e4066Sahrens uint64_t freeze_txg = 0; 1559fa9e4066Sahrens 1560e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1561fa9e4066Sahrens if (spa->spa_freeze_txg == UINT64_MAX) { 1562fa9e4066Sahrens freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1563fa9e4066Sahrens spa->spa_freeze_txg = freeze_txg; 1564fa9e4066Sahrens } 1565e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 1566fa9e4066Sahrens if (freeze_txg != 0) 1567fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1568fa9e4066Sahrens } 1569fa9e4066Sahrens 15700125049cSahrens void 15710125049cSahrens zfs_panic_recover(const char *fmt, ...) 15720125049cSahrens { 15730125049cSahrens va_list adx; 15740125049cSahrens 15750125049cSahrens va_start(adx, fmt); 15760125049cSahrens vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 15770125049cSahrens va_end(adx); 15780125049cSahrens } 15790125049cSahrens 15803f9d6ad7SLin Ling /* 15813f9d6ad7SLin Ling * This is a stripped-down version of strtoull, suitable only for converting 1582f7170741SWill Andrews * lowercase hexadecimal numbers that don't overflow. 15833f9d6ad7SLin Ling */ 15843f9d6ad7SLin Ling uint64_t 15854585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr) 15863f9d6ad7SLin Ling { 15873f9d6ad7SLin Ling uint64_t val = 0; 15883f9d6ad7SLin Ling char c; 15893f9d6ad7SLin Ling int digit; 15903f9d6ad7SLin Ling 15913f9d6ad7SLin Ling while ((c = *str) != '\0') { 15923f9d6ad7SLin Ling if (c >= '0' && c <= '9') 15933f9d6ad7SLin Ling digit = c - '0'; 15943f9d6ad7SLin Ling else if (c >= 'a' && c <= 'f') 15953f9d6ad7SLin Ling digit = 10 + c - 'a'; 15963f9d6ad7SLin Ling else 15973f9d6ad7SLin Ling break; 15983f9d6ad7SLin Ling 15993f9d6ad7SLin Ling val *= 16; 16003f9d6ad7SLin Ling val += digit; 16013f9d6ad7SLin Ling 16023f9d6ad7SLin Ling str++; 16033f9d6ad7SLin Ling } 16043f9d6ad7SLin Ling 16053f9d6ad7SLin Ling if (nptr) 16063f9d6ad7SLin Ling *nptr = (char *)str; 16073f9d6ad7SLin Ling 16083f9d6ad7SLin Ling return (val); 16093f9d6ad7SLin Ling } 16103f9d6ad7SLin Ling 1611663207adSDon Brady void 1612663207adSDon Brady spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx) 1613663207adSDon Brady { 1614663207adSDon Brady /* 1615663207adSDon Brady * We bump the feature refcount for each special vdev added to the pool 1616663207adSDon Brady */ 1617663207adSDon Brady ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)); 1618663207adSDon Brady spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx); 1619663207adSDon Brady } 1620663207adSDon Brady 1621fa9e4066Sahrens /* 1622fa9e4066Sahrens * ========================================================================== 1623fa9e4066Sahrens * Accessor functions 1624fa9e4066Sahrens * ========================================================================== 1625fa9e4066Sahrens */ 1626fa9e4066Sahrens 1627088f3894Sahrens boolean_t 162888b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa) 1629fa9e4066Sahrens { 163088b7b0f2SMatthew Ahrens return (spa->spa_async_suspended); 1631fa9e4066Sahrens } 1632fa9e4066Sahrens 1633fa9e4066Sahrens dsl_pool_t * 1634fa9e4066Sahrens spa_get_dsl(spa_t *spa) 1635fa9e4066Sahrens { 1636fa9e4066Sahrens return (spa->spa_dsl_pool); 1637fa9e4066Sahrens } 1638fa9e4066Sahrens 1639ad135b5dSChristopher Siden boolean_t 1640ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa) 1641ad135b5dSChristopher Siden { 1642ad135b5dSChristopher Siden return (spa->spa_is_initializing); 1643ad135b5dSChristopher Siden } 1644ad135b5dSChristopher Siden 16455cabbc6bSPrashanth Sreenivasa boolean_t 16465cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa) 16475cabbc6bSPrashanth Sreenivasa { 16485cabbc6bSPrashanth Sreenivasa return (spa->spa_indirect_vdevs_loaded); 16495cabbc6bSPrashanth Sreenivasa } 16505cabbc6bSPrashanth Sreenivasa 1651fa9e4066Sahrens blkptr_t * 1652fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa) 1653fa9e4066Sahrens { 1654fa9e4066Sahrens return (&spa->spa_ubsync.ub_rootbp); 1655fa9e4066Sahrens } 1656fa9e4066Sahrens 1657fa9e4066Sahrens void 1658fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1659fa9e4066Sahrens { 1660fa9e4066Sahrens spa->spa_uberblock.ub_rootbp = *bp; 1661fa9e4066Sahrens } 1662fa9e4066Sahrens 1663fa9e4066Sahrens void 1664fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen) 1665fa9e4066Sahrens { 1666fa9e4066Sahrens if (spa->spa_root == NULL) 1667fa9e4066Sahrens buf[0] = '\0'; 1668fa9e4066Sahrens else 1669fa9e4066Sahrens (void) strncpy(buf, spa->spa_root, buflen); 1670fa9e4066Sahrens } 1671fa9e4066Sahrens 1672fa9e4066Sahrens int 1673fa9e4066Sahrens spa_sync_pass(spa_t *spa) 1674fa9e4066Sahrens { 1675fa9e4066Sahrens return (spa->spa_sync_pass); 1676fa9e4066Sahrens } 1677fa9e4066Sahrens 1678fa9e4066Sahrens char * 1679fa9e4066Sahrens spa_name(spa_t *spa) 1680fa9e4066Sahrens { 1681fa9e4066Sahrens return (spa->spa_name); 1682fa9e4066Sahrens } 1683fa9e4066Sahrens 1684fa9e4066Sahrens uint64_t 1685fa9e4066Sahrens spa_guid(spa_t *spa) 1686fa9e4066Sahrens { 1687dfbb9432SGeorge Wilson dsl_pool_t *dp = spa_get_dsl(spa); 1688dfbb9432SGeorge Wilson uint64_t guid; 1689dfbb9432SGeorge Wilson 1690b5989ec7Seschrock /* 1691b5989ec7Seschrock * If we fail to parse the config during spa_load(), we can go through 1692b5989ec7Seschrock * the error path (which posts an ereport) and end up here with no root 1693e9103aaeSGarrett D'Amore * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1694b5989ec7Seschrock * this case. 1695b5989ec7Seschrock */ 1696dfbb9432SGeorge Wilson if (spa->spa_root_vdev == NULL) 1697dfbb9432SGeorge Wilson return (spa->spa_config_guid); 1698dfbb9432SGeorge Wilson 1699dfbb9432SGeorge Wilson guid = spa->spa_last_synced_guid != 0 ? 1700dfbb9432SGeorge Wilson spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1701dfbb9432SGeorge Wilson 1702dfbb9432SGeorge Wilson /* 1703dfbb9432SGeorge Wilson * Return the most recently synced out guid unless we're 1704dfbb9432SGeorge Wilson * in syncing context. 1705dfbb9432SGeorge Wilson */ 1706dfbb9432SGeorge Wilson if (dp && dsl_pool_sync_context(dp)) 1707b5989ec7Seschrock return (spa->spa_root_vdev->vdev_guid); 1708b5989ec7Seschrock else 1709dfbb9432SGeorge Wilson return (guid); 1710e9103aaeSGarrett D'Amore } 1711e9103aaeSGarrett D'Amore 1712e9103aaeSGarrett D'Amore uint64_t 1713e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa) 1714e9103aaeSGarrett D'Amore { 1715e9103aaeSGarrett D'Amore /* 1716e9103aaeSGarrett D'Amore * This is a GUID that exists solely as a reference for the 1717e9103aaeSGarrett D'Amore * purposes of the arc. It is generated at load time, and 1718e9103aaeSGarrett D'Amore * is never written to persistent storage. 1719e9103aaeSGarrett D'Amore */ 1720e9103aaeSGarrett D'Amore return (spa->spa_load_guid); 1721fa9e4066Sahrens } 1722fa9e4066Sahrens 1723fa9e4066Sahrens uint64_t 1724fa9e4066Sahrens spa_last_synced_txg(spa_t *spa) 1725fa9e4066Sahrens { 1726fa9e4066Sahrens return (spa->spa_ubsync.ub_txg); 1727fa9e4066Sahrens } 1728fa9e4066Sahrens 1729fa9e4066Sahrens uint64_t 1730fa9e4066Sahrens spa_first_txg(spa_t *spa) 1731fa9e4066Sahrens { 1732fa9e4066Sahrens return (spa->spa_first_txg); 1733fa9e4066Sahrens } 1734fa9e4066Sahrens 1735b24ab676SJeff Bonwick uint64_t 1736b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa) 1737b24ab676SJeff Bonwick { 1738b24ab676SJeff Bonwick return (spa->spa_syncing_txg); 1739b24ab676SJeff Bonwick } 1740b24ab676SJeff Bonwick 17413991b535SGeorge Wilson /* 17423991b535SGeorge Wilson * Return the last txg where data can be dirtied. The final txgs 17433991b535SGeorge Wilson * will be used to just clear out any deferred frees that remain. 17443991b535SGeorge Wilson */ 17453991b535SGeorge Wilson uint64_t 17463991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa) 17473991b535SGeorge Wilson { 17483991b535SGeorge Wilson return (spa->spa_final_txg - TXG_DEFER_SIZE); 17493991b535SGeorge Wilson } 17503991b535SGeorge Wilson 175188b7b0f2SMatthew Ahrens pool_state_t 1752fa9e4066Sahrens spa_state(spa_t *spa) 1753fa9e4066Sahrens { 1754fa9e4066Sahrens return (spa->spa_state); 1755fa9e4066Sahrens } 1756fa9e4066Sahrens 1757b16da2e2SGeorge Wilson spa_load_state_t 1758b16da2e2SGeorge Wilson spa_load_state(spa_t *spa) 1759b16da2e2SGeorge Wilson { 1760b16da2e2SGeorge Wilson return (spa->spa_load_state); 1761b16da2e2SGeorge Wilson } 1762b16da2e2SGeorge Wilson 1763fa9e4066Sahrens uint64_t 1764fa9e4066Sahrens spa_freeze_txg(spa_t *spa) 1765fa9e4066Sahrens { 1766fa9e4066Sahrens return (spa->spa_freeze_txg); 1767fa9e4066Sahrens } 1768fa9e4066Sahrens 1769fa9e4066Sahrens /* ARGSUSED */ 1770fa9e4066Sahrens uint64_t 177161e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize) 1772fa9e4066Sahrens { 177369962b56SMatthew Ahrens return (lsize * spa_asize_inflation); 177444cd46caSbillm } 177544cd46caSbillm 17767d46dc6cSMatthew Ahrens /* 17777d46dc6cSMatthew Ahrens * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%), 17784b5c8e93SMatthew Ahrens * or at least 128MB, unless that would cause it to be more than half the 17794b5c8e93SMatthew Ahrens * pool size. 17807d46dc6cSMatthew Ahrens * 17817d46dc6cSMatthew Ahrens * See the comment above spa_slop_shift for details. 17827d46dc6cSMatthew Ahrens */ 17837d46dc6cSMatthew Ahrens uint64_t 17844b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa) 17854b5c8e93SMatthew Ahrens { 17867d46dc6cSMatthew Ahrens uint64_t space = spa_get_dspace(spa); 17874b5c8e93SMatthew Ahrens return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop))); 17887d46dc6cSMatthew Ahrens } 17897d46dc6cSMatthew Ahrens 1790485bbbf5SGeorge Wilson uint64_t 1791485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa) 1792485bbbf5SGeorge Wilson { 1793485bbbf5SGeorge Wilson return (spa->spa_dspace); 1794485bbbf5SGeorge Wilson } 1795485bbbf5SGeorge Wilson 179686714001SSerapheim Dimitropoulos uint64_t 179786714001SSerapheim Dimitropoulos spa_get_checkpoint_space(spa_t *spa) 179886714001SSerapheim Dimitropoulos { 179986714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_info.sci_dspace); 180086714001SSerapheim Dimitropoulos } 180186714001SSerapheim Dimitropoulos 1802485bbbf5SGeorge Wilson void 1803485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa) 1804485bbbf5SGeorge Wilson { 1805485bbbf5SGeorge Wilson spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1806485bbbf5SGeorge Wilson ddt_get_dedup_dspace(spa); 18075cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) { 18085cabbc6bSPrashanth Sreenivasa /* 18095cabbc6bSPrashanth Sreenivasa * We can't allocate from the removing device, so 18105cabbc6bSPrashanth Sreenivasa * subtract its size. This prevents the DMU/DSL from 18115cabbc6bSPrashanth Sreenivasa * filling up the (now smaller) pool while we are in the 18125cabbc6bSPrashanth Sreenivasa * middle of removing the device. 18135cabbc6bSPrashanth Sreenivasa * 18145cabbc6bSPrashanth Sreenivasa * Note that the DMU/DSL doesn't actually know or care 18155cabbc6bSPrashanth Sreenivasa * how much space is allocated (it does its own tracking 18165cabbc6bSPrashanth Sreenivasa * of how much space has been logically used). So it 18175cabbc6bSPrashanth Sreenivasa * doesn't matter that the data we are moving may be 18185cabbc6bSPrashanth Sreenivasa * allocated twice (on the old device and the new 18195cabbc6bSPrashanth Sreenivasa * device). 18205cabbc6bSPrashanth Sreenivasa */ 18213a4b1be9SMatthew Ahrens spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 18223a4b1be9SMatthew Ahrens vdev_t *vd = 18233a4b1be9SMatthew Ahrens vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id); 18245cabbc6bSPrashanth Sreenivasa spa->spa_dspace -= spa_deflate(spa) ? 18255cabbc6bSPrashanth Sreenivasa vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 18263a4b1be9SMatthew Ahrens spa_config_exit(spa, SCL_VDEV, FTAG); 18275cabbc6bSPrashanth Sreenivasa } 1828485bbbf5SGeorge Wilson } 1829485bbbf5SGeorge Wilson 18300a4e9518Sgw /* 18310a4e9518Sgw * Return the failure mode that has been set to this pool. The default 18320a4e9518Sgw * behavior will be to block all I/Os when a complete failure occurs. 18330a4e9518Sgw */ 18340a4e9518Sgw uint8_t 18350a4e9518Sgw spa_get_failmode(spa_t *spa) 18360a4e9518Sgw { 18370a4e9518Sgw return (spa->spa_failmode); 18380a4e9518Sgw } 18390a4e9518Sgw 1840e14bb325SJeff Bonwick boolean_t 1841e14bb325SJeff Bonwick spa_suspended(spa_t *spa) 1842e14bb325SJeff Bonwick { 1843e0f1c0afSOlaf Faaland return (spa->spa_suspended != ZIO_SUSPEND_NONE); 1844e14bb325SJeff Bonwick } 1845e14bb325SJeff Bonwick 184644cd46caSbillm uint64_t 184744cd46caSbillm spa_version(spa_t *spa) 184844cd46caSbillm { 184944cd46caSbillm return (spa->spa_ubsync.ub_version); 185044cd46caSbillm } 185144cd46caSbillm 1852b24ab676SJeff Bonwick boolean_t 1853b24ab676SJeff Bonwick spa_deflate(spa_t *spa) 1854b24ab676SJeff Bonwick { 1855b24ab676SJeff Bonwick return (spa->spa_deflate); 1856b24ab676SJeff Bonwick } 1857b24ab676SJeff Bonwick 1858b24ab676SJeff Bonwick metaslab_class_t * 1859b24ab676SJeff Bonwick spa_normal_class(spa_t *spa) 1860b24ab676SJeff Bonwick { 1861b24ab676SJeff Bonwick return (spa->spa_normal_class); 1862b24ab676SJeff Bonwick } 1863b24ab676SJeff Bonwick 1864b24ab676SJeff Bonwick metaslab_class_t * 1865b24ab676SJeff Bonwick spa_log_class(spa_t *spa) 1866b24ab676SJeff Bonwick { 1867b24ab676SJeff Bonwick return (spa->spa_log_class); 1868b24ab676SJeff Bonwick } 1869b24ab676SJeff Bonwick 1870663207adSDon Brady metaslab_class_t * 1871663207adSDon Brady spa_special_class(spa_t *spa) 1872663207adSDon Brady { 1873663207adSDon Brady return (spa->spa_special_class); 1874663207adSDon Brady } 1875663207adSDon Brady 1876663207adSDon Brady metaslab_class_t * 1877663207adSDon Brady spa_dedup_class(spa_t *spa) 1878663207adSDon Brady { 1879663207adSDon Brady return (spa->spa_dedup_class); 1880663207adSDon Brady } 1881663207adSDon Brady 1882663207adSDon Brady /* 1883663207adSDon Brady * Locate an appropriate allocation class 1884663207adSDon Brady */ 1885663207adSDon Brady metaslab_class_t * 1886663207adSDon Brady spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype, 1887663207adSDon Brady uint_t level, uint_t special_smallblk) 1888663207adSDon Brady { 1889663207adSDon Brady if (DMU_OT_IS_ZIL(objtype)) { 1890663207adSDon Brady if (spa->spa_log_class->mc_groups != 0) 1891663207adSDon Brady return (spa_log_class(spa)); 1892663207adSDon Brady else 1893663207adSDon Brady return (spa_normal_class(spa)); 1894663207adSDon Brady } 1895663207adSDon Brady 1896663207adSDon Brady boolean_t has_special_class = spa->spa_special_class->mc_groups != 0; 1897663207adSDon Brady 1898663207adSDon Brady if (DMU_OT_IS_DDT(objtype)) { 1899663207adSDon Brady if (spa->spa_dedup_class->mc_groups != 0) 1900663207adSDon Brady return (spa_dedup_class(spa)); 1901663207adSDon Brady else if (has_special_class && zfs_ddt_data_is_special) 1902663207adSDon Brady return (spa_special_class(spa)); 1903663207adSDon Brady else 1904663207adSDon Brady return (spa_normal_class(spa)); 1905663207adSDon Brady } 1906663207adSDon Brady 1907663207adSDon Brady /* Indirect blocks for user data can land in special if allowed */ 1908663207adSDon Brady if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) { 1909663207adSDon Brady if (has_special_class && zfs_user_indirect_is_special) 1910663207adSDon Brady return (spa_special_class(spa)); 1911663207adSDon Brady else 1912663207adSDon Brady return (spa_normal_class(spa)); 1913663207adSDon Brady } 1914663207adSDon Brady 1915663207adSDon Brady if (DMU_OT_IS_METADATA(objtype) || level > 0) { 1916663207adSDon Brady if (has_special_class) 1917663207adSDon Brady return (spa_special_class(spa)); 1918663207adSDon Brady else 1919663207adSDon Brady return (spa_normal_class(spa)); 1920663207adSDon Brady } 1921663207adSDon Brady 1922663207adSDon Brady /* 1923663207adSDon Brady * Allow small file blocks in special class in some cases (like 1924663207adSDon Brady * for the dRAID vdev feature). But always leave a reserve of 1925663207adSDon Brady * zfs_special_class_metadata_reserve_pct exclusively for metadata. 1926663207adSDon Brady */ 1927663207adSDon Brady if (DMU_OT_IS_FILE(objtype) && 1928663207adSDon Brady has_special_class && size <= special_smallblk) { 1929663207adSDon Brady metaslab_class_t *special = spa_special_class(spa); 1930663207adSDon Brady uint64_t alloc = metaslab_class_get_alloc(special); 1931663207adSDon Brady uint64_t space = metaslab_class_get_space(special); 1932663207adSDon Brady uint64_t limit = 1933663207adSDon Brady (space * (100 - zfs_special_class_metadata_reserve_pct)) 1934663207adSDon Brady / 100; 1935663207adSDon Brady 1936663207adSDon Brady if (alloc < limit) 1937663207adSDon Brady return (special); 1938663207adSDon Brady } 1939663207adSDon Brady 1940663207adSDon Brady return (spa_normal_class(spa)); 1941663207adSDon Brady } 1942663207adSDon Brady 1943bc9014e6SJustin Gibbs void 1944bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os) 1945bc9014e6SJustin Gibbs { 1946bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1947bc9014e6SJustin Gibbs list_insert_head(&spa->spa_evicting_os_list, os); 1948bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1949bc9014e6SJustin Gibbs } 1950bc9014e6SJustin Gibbs 1951bc9014e6SJustin Gibbs void 1952bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os) 1953bc9014e6SJustin Gibbs { 1954bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1955bc9014e6SJustin Gibbs list_remove(&spa->spa_evicting_os_list, os); 1956bc9014e6SJustin Gibbs cv_broadcast(&spa->spa_evicting_os_cv); 1957bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1958bc9014e6SJustin Gibbs } 1959bc9014e6SJustin Gibbs 1960bc9014e6SJustin Gibbs void 1961bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa) 1962bc9014e6SJustin Gibbs { 1963bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock); 1964bc9014e6SJustin Gibbs while (!list_is_empty(&spa->spa_evicting_os_list)) 1965bc9014e6SJustin Gibbs cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock); 1966bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock); 1967bc9014e6SJustin Gibbs 1968bc9014e6SJustin Gibbs dmu_buf_user_evict_wait(); 1969bc9014e6SJustin Gibbs } 1970bc9014e6SJustin Gibbs 197144cd46caSbillm int 197244cd46caSbillm spa_max_replication(spa_t *spa) 197344cd46caSbillm { 197444cd46caSbillm /* 1975e7437265Sahrens * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 197644cd46caSbillm * handle BPs with more than one DVA allocated. Set our max 197744cd46caSbillm * replication level accordingly. 1978fa9e4066Sahrens */ 1979e7437265Sahrens if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 198044cd46caSbillm return (1); 198144cd46caSbillm return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1982fa9e4066Sahrens } 1983fa9e4066Sahrens 19843f9d6ad7SLin Ling int 19853f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa) 19863f9d6ad7SLin Ling { 19873f9d6ad7SLin Ling return (spa->spa_prev_software_version); 19883f9d6ad7SLin Ling } 19893f9d6ad7SLin Ling 1990283b8460SGeorge.Wilson uint64_t 1991283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa) 1992283b8460SGeorge.Wilson { 1993283b8460SGeorge.Wilson return (spa->spa_deadman_synctime); 1994283b8460SGeorge.Wilson } 1995283b8460SGeorge.Wilson 1996084fd14fSBrian Behlendorf spa_autotrim_t 1997084fd14fSBrian Behlendorf spa_get_autotrim(spa_t *spa) 1998084fd14fSBrian Behlendorf { 1999084fd14fSBrian Behlendorf return (spa->spa_autotrim); 2000084fd14fSBrian Behlendorf } 2001084fd14fSBrian Behlendorf 200299653d4eSeschrock uint64_t 2003b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 200499653d4eSeschrock { 2005b24ab676SJeff Bonwick uint64_t asize = DVA_GET_ASIZE(dva); 2006b24ab676SJeff Bonwick uint64_t dsize = asize; 200799653d4eSeschrock 2008b24ab676SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 200999653d4eSeschrock 2010b24ab676SJeff Bonwick if (asize != 0 && spa->spa_deflate) { 2011b24ab676SJeff Bonwick vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 2012b24ab676SJeff Bonwick dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 201399653d4eSeschrock } 2014b24ab676SJeff Bonwick 2015b24ab676SJeff Bonwick return (dsize); 2016b24ab676SJeff Bonwick } 2017b24ab676SJeff Bonwick 2018b24ab676SJeff Bonwick uint64_t 2019b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 2020b24ab676SJeff Bonwick { 2021b24ab676SJeff Bonwick uint64_t dsize = 0; 2022b24ab676SJeff Bonwick 20235d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 2024b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 2025b24ab676SJeff Bonwick 2026b24ab676SJeff Bonwick return (dsize); 2027b24ab676SJeff Bonwick } 2028b24ab676SJeff Bonwick 2029b24ab676SJeff Bonwick uint64_t 2030b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp) 2031b24ab676SJeff Bonwick { 2032b24ab676SJeff Bonwick uint64_t dsize = 0; 2033b24ab676SJeff Bonwick 2034b24ab676SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2035b24ab676SJeff Bonwick 20365d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++) 2037b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 2038b24ab676SJeff Bonwick 2039e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG); 2040b24ab676SJeff Bonwick 2041b24ab676SJeff Bonwick return (dsize); 204299653d4eSeschrock } 204399653d4eSeschrock 2044abe1fd01SDon Brady uint64_t 2045abe1fd01SDon Brady spa_dirty_data(spa_t *spa) 2046abe1fd01SDon Brady { 2047abe1fd01SDon Brady return (spa->spa_dsl_pool->dp_dirty_total); 2048abe1fd01SDon Brady } 2049abe1fd01SDon Brady 2050*dd50e0ccSTony Hutter /* 2051*dd50e0ccSTony Hutter * ========================================================================== 2052*dd50e0ccSTony Hutter * SPA Import Progress Routines 2053*dd50e0ccSTony Hutter * The illumos implementation of these are different from OpenZFS. OpenZFS 2054*dd50e0ccSTony Hutter * uses the Linux /proc fs, whereas we use a kstat on the spa. 2055*dd50e0ccSTony Hutter * ========================================================================== 2056*dd50e0ccSTony Hutter */ 2057*dd50e0ccSTony Hutter 2058*dd50e0ccSTony Hutter typedef struct spa_import_progress { 2059*dd50e0ccSTony Hutter kstat_named_t sip_load_state; 2060*dd50e0ccSTony Hutter kstat_named_t sip_mmp_sec_remaining; /* MMP activity check */ 2061*dd50e0ccSTony Hutter kstat_named_t sip_load_max_txg; /* rewind txg */ 2062*dd50e0ccSTony Hutter } spa_import_progress_t; 2063*dd50e0ccSTony Hutter 2064*dd50e0ccSTony Hutter static void 2065*dd50e0ccSTony Hutter spa_import_progress_init(void) 2066*dd50e0ccSTony Hutter { 2067*dd50e0ccSTony Hutter } 2068*dd50e0ccSTony Hutter 2069*dd50e0ccSTony Hutter static void 2070*dd50e0ccSTony Hutter spa_import_progress_destroy(void) 2071*dd50e0ccSTony Hutter { 2072*dd50e0ccSTony Hutter } 2073*dd50e0ccSTony Hutter 2074*dd50e0ccSTony Hutter void spa_import_progress_add(spa_t *); 2075*dd50e0ccSTony Hutter 2076*dd50e0ccSTony Hutter int 2077*dd50e0ccSTony Hutter spa_import_progress_set_state(spa_t *spa, spa_load_state_t load_state) 2078*dd50e0ccSTony Hutter { 2079*dd50e0ccSTony Hutter if (spa->spa_imp_kstat == NULL) 2080*dd50e0ccSTony Hutter spa_import_progress_add(spa); 2081*dd50e0ccSTony Hutter 2082*dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock); 2083*dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) { 2084*dd50e0ccSTony Hutter spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data; 2085*dd50e0ccSTony Hutter if (sip != NULL) 2086*dd50e0ccSTony Hutter sip->sip_load_state.value.ui64 = (uint64_t)load_state; 2087*dd50e0ccSTony Hutter } 2088*dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock); 2089*dd50e0ccSTony Hutter 2090*dd50e0ccSTony Hutter return (0); 2091*dd50e0ccSTony Hutter } 2092*dd50e0ccSTony Hutter 2093*dd50e0ccSTony Hutter int 2094*dd50e0ccSTony Hutter spa_import_progress_set_max_txg(spa_t *spa, uint64_t load_max_txg) 2095*dd50e0ccSTony Hutter { 2096*dd50e0ccSTony Hutter if (spa->spa_imp_kstat == NULL) 2097*dd50e0ccSTony Hutter spa_import_progress_add(spa); 2098*dd50e0ccSTony Hutter 2099*dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock); 2100*dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) { 2101*dd50e0ccSTony Hutter spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data; 2102*dd50e0ccSTony Hutter if (sip != NULL) 2103*dd50e0ccSTony Hutter sip->sip_load_max_txg.value.ui64 = load_max_txg; 2104*dd50e0ccSTony Hutter } 2105*dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock); 2106*dd50e0ccSTony Hutter 2107*dd50e0ccSTony Hutter return (0); 2108*dd50e0ccSTony Hutter } 2109*dd50e0ccSTony Hutter 2110*dd50e0ccSTony Hutter int 2111*dd50e0ccSTony Hutter spa_import_progress_set_mmp_check(spa_t *spa, uint64_t mmp_sec_remaining) 2112*dd50e0ccSTony Hutter { 2113*dd50e0ccSTony Hutter if (spa->spa_imp_kstat == NULL) 2114*dd50e0ccSTony Hutter spa_import_progress_add(spa); 2115*dd50e0ccSTony Hutter 2116*dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock); 2117*dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) { 2118*dd50e0ccSTony Hutter spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data; 2119*dd50e0ccSTony Hutter if (sip != NULL) 2120*dd50e0ccSTony Hutter sip->sip_mmp_sec_remaining.value.ui64 = 2121*dd50e0ccSTony Hutter mmp_sec_remaining; 2122*dd50e0ccSTony Hutter } 2123*dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock); 2124*dd50e0ccSTony Hutter 2125*dd50e0ccSTony Hutter return (0); 2126*dd50e0ccSTony Hutter } 2127*dd50e0ccSTony Hutter 2128*dd50e0ccSTony Hutter /* 2129*dd50e0ccSTony Hutter * A new import is in progress. Add an entry. 2130*dd50e0ccSTony Hutter */ 2131*dd50e0ccSTony Hutter void 2132*dd50e0ccSTony Hutter spa_import_progress_add(spa_t *spa) 2133*dd50e0ccSTony Hutter { 2134*dd50e0ccSTony Hutter char *poolname = NULL; 2135*dd50e0ccSTony Hutter spa_import_progress_t *sip; 2136*dd50e0ccSTony Hutter 2137*dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock); 2138*dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) { 2139*dd50e0ccSTony Hutter sip = spa->spa_imp_kstat->ks_data; 2140*dd50e0ccSTony Hutter sip->sip_load_state.value.ui64 = (uint64_t)spa_load_state(spa); 2141*dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock); 2142*dd50e0ccSTony Hutter return; 2143*dd50e0ccSTony Hutter } 2144*dd50e0ccSTony Hutter 2145*dd50e0ccSTony Hutter (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME, 2146*dd50e0ccSTony Hutter &poolname); 2147*dd50e0ccSTony Hutter if (poolname == NULL) 2148*dd50e0ccSTony Hutter poolname = spa_name(spa); 2149*dd50e0ccSTony Hutter 2150*dd50e0ccSTony Hutter spa->spa_imp_kstat = kstat_create("zfs_import", 0, poolname, 2151*dd50e0ccSTony Hutter "zfs_misc", KSTAT_TYPE_NAMED, 2152*dd50e0ccSTony Hutter sizeof (spa_import_progress_t) / sizeof (kstat_named_t), 2153*dd50e0ccSTony Hutter KSTAT_FLAG_VIRTUAL); 2154*dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) { 2155*dd50e0ccSTony Hutter sip = kmem_alloc(sizeof (spa_import_progress_t), KM_SLEEP); 2156*dd50e0ccSTony Hutter spa->spa_imp_kstat->ks_data = sip; 2157*dd50e0ccSTony Hutter 2158*dd50e0ccSTony Hutter sip->sip_load_state.value.ui64 = (uint64_t)spa_load_state(spa); 2159*dd50e0ccSTony Hutter 2160*dd50e0ccSTony Hutter kstat_named_init(&sip->sip_load_state, 2161*dd50e0ccSTony Hutter "spa_load_state", KSTAT_DATA_UINT64); 2162*dd50e0ccSTony Hutter kstat_named_init(&sip->sip_mmp_sec_remaining, 2163*dd50e0ccSTony Hutter "mmp_sec_remaining", KSTAT_DATA_UINT64); 2164*dd50e0ccSTony Hutter kstat_named_init(&sip->sip_load_max_txg, 2165*dd50e0ccSTony Hutter "spa_load_max_txg", KSTAT_DATA_UINT64); 2166*dd50e0ccSTony Hutter spa->spa_imp_kstat->ks_lock = &spa->spa_imp_kstat_lock; 2167*dd50e0ccSTony Hutter kstat_install(spa->spa_imp_kstat); 2168*dd50e0ccSTony Hutter } 2169*dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock); 2170*dd50e0ccSTony Hutter } 2171*dd50e0ccSTony Hutter 2172*dd50e0ccSTony Hutter void 2173*dd50e0ccSTony Hutter spa_import_progress_remove(spa_t *spa) 2174*dd50e0ccSTony Hutter { 2175*dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) { 2176*dd50e0ccSTony Hutter void *data = spa->spa_imp_kstat->ks_data; 2177*dd50e0ccSTony Hutter 2178*dd50e0ccSTony Hutter kstat_delete(spa->spa_imp_kstat); 2179*dd50e0ccSTony Hutter spa->spa_imp_kstat = NULL; 2180*dd50e0ccSTony Hutter kmem_free(data, sizeof (spa_import_progress_t)); 2181*dd50e0ccSTony Hutter } 2182*dd50e0ccSTony Hutter } 2183*dd50e0ccSTony Hutter 2184fa9e4066Sahrens /* 2185fa9e4066Sahrens * ========================================================================== 2186fa9e4066Sahrens * Initialization and Termination 2187fa9e4066Sahrens * ========================================================================== 2188fa9e4066Sahrens */ 2189fa9e4066Sahrens 2190fa9e4066Sahrens static int 2191fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2) 2192fa9e4066Sahrens { 2193fa9e4066Sahrens const spa_t *s1 = a1; 2194fa9e4066Sahrens const spa_t *s2 = a2; 2195fa9e4066Sahrens int s; 2196fa9e4066Sahrens 2197fa9e4066Sahrens s = strcmp(s1->spa_name, s2->spa_name); 2198c4ab0d3fSGvozden Neskovic 21994d7988d6SPaul Dagnelie return (TREE_ISIGN(s)); 2200fa9e4066Sahrens } 2201fa9e4066Sahrens 22020373e76bSbonwick int 22030373e76bSbonwick spa_busy(void) 22040373e76bSbonwick { 22050373e76bSbonwick return (spa_active_count); 22060373e76bSbonwick } 22070373e76bSbonwick 2208e7cbe64fSgw void 2209e7cbe64fSgw spa_boot_init() 2210e7cbe64fSgw { 2211e7cbe64fSgw spa_config_load(); 2212e7cbe64fSgw } 2213e7cbe64fSgw 2214fa9e4066Sahrens void 2215fa9e4066Sahrens spa_init(int mode) 2216fa9e4066Sahrens { 2217fa9e4066Sahrens mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 2218c25056deSgw mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 2219fa94a07fSbrendan mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 2220fa9e4066Sahrens cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 2221fa9e4066Sahrens 2222fa9e4066Sahrens avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 2223fa9e4066Sahrens offsetof(spa_t, spa_avl)); 2224fa9e4066Sahrens 2225fa94a07fSbrendan avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 2226fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 2227fa94a07fSbrendan 2228fa94a07fSbrendan avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 2229fa94a07fSbrendan offsetof(spa_aux_t, aux_avl)); 223099653d4eSeschrock 22318ad4d6ddSJeff Bonwick spa_mode_global = mode; 2232fa9e4066Sahrens 2233283b8460SGeorge.Wilson #ifdef _KERNEL 2234283b8460SGeorge.Wilson spa_arch_init(); 2235283b8460SGeorge.Wilson #else 2236cd1c8b85SMatthew Ahrens if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 2237cd1c8b85SMatthew Ahrens arc_procfd = open("/proc/self/ctl", O_WRONLY); 2238cd1c8b85SMatthew Ahrens if (arc_procfd == -1) { 2239cd1c8b85SMatthew Ahrens perror("could not enable watchpoints: " 2240cd1c8b85SMatthew Ahrens "opening /proc/self/ctl failed: "); 2241cd1c8b85SMatthew Ahrens } else { 2242cd1c8b85SMatthew Ahrens arc_watch = B_TRUE; 2243cd1c8b85SMatthew Ahrens } 2244cd1c8b85SMatthew Ahrens } 2245cd1c8b85SMatthew Ahrens #endif 2246cd1c8b85SMatthew Ahrens 2247e914ace2STim Schumacher zfs_refcount_init(); 2248fa9e4066Sahrens unique_init(); 22494d7988d6SPaul Dagnelie zfs_btree_init(); 22504d7988d6SPaul Dagnelie metaslab_stat_init(); 2251fa9e4066Sahrens zio_init(); 2252fa9e4066Sahrens dmu_init(); 2253fa9e4066Sahrens zil_init(); 225487db74c1Sek vdev_cache_stat_init(); 225512a8814cSTom Caputi vdev_mirror_stat_init(); 225691ebeef5Sahrens zfs_prop_init(); 2257990b4856Slling zpool_prop_init(); 2258ad135b5dSChristopher Siden zpool_feature_init(); 2259fa9e4066Sahrens spa_config_load(); 2260e14bb325SJeff Bonwick l2arc_start(); 2261a3874b8bSToomas Soome scan_init(); 2262*dd50e0ccSTony Hutter spa_import_progress_init(); 2263fa9e4066Sahrens } 2264fa9e4066Sahrens 2265fa9e4066Sahrens void 2266fa9e4066Sahrens spa_fini(void) 2267fa9e4066Sahrens { 2268e14bb325SJeff Bonwick l2arc_stop(); 2269e14bb325SJeff Bonwick 2270fa9e4066Sahrens spa_evict_all(); 2271fa9e4066Sahrens 227287db74c1Sek vdev_cache_stat_fini(); 227312a8814cSTom Caputi vdev_mirror_stat_fini(); 2274fa9e4066Sahrens zil_fini(); 2275fa9e4066Sahrens dmu_fini(); 2276fa9e4066Sahrens zio_fini(); 22774d7988d6SPaul Dagnelie metaslab_stat_fini(); 22784d7988d6SPaul Dagnelie zfs_btree_fini(); 227991ebeef5Sahrens unique_fini(); 2280e914ace2STim Schumacher zfs_refcount_fini(); 2281a3874b8bSToomas Soome scan_fini(); 2282*dd50e0ccSTony Hutter spa_import_progress_destroy(); 2283fa9e4066Sahrens 2284fa9e4066Sahrens avl_destroy(&spa_namespace_avl); 228599653d4eSeschrock avl_destroy(&spa_spare_avl); 2286fa94a07fSbrendan avl_destroy(&spa_l2cache_avl); 2287fa9e4066Sahrens 2288fa9e4066Sahrens cv_destroy(&spa_namespace_cv); 2289fa9e4066Sahrens mutex_destroy(&spa_namespace_lock); 2290c25056deSgw mutex_destroy(&spa_spare_lock); 2291fa94a07fSbrendan mutex_destroy(&spa_l2cache_lock); 2292fa9e4066Sahrens } 22936ce0521aSperrin 22946ce0521aSperrin /* 22956ce0521aSperrin * Return whether this pool has slogs. No locking needed. 22966ce0521aSperrin * It's not a problem if the wrong answer is returned as it's only for 22976ce0521aSperrin * performance and not correctness 22986ce0521aSperrin */ 22996ce0521aSperrin boolean_t 23006ce0521aSperrin spa_has_slogs(spa_t *spa) 23016ce0521aSperrin { 23026ce0521aSperrin return (spa->spa_log_class->mc_rotor != NULL); 23036ce0521aSperrin } 2304bf82a41bSeschrock 2305b24ab676SJeff Bonwick spa_log_state_t 2306b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa) 2307b24ab676SJeff Bonwick { 2308b24ab676SJeff Bonwick return (spa->spa_log_state); 2309b24ab676SJeff Bonwick } 2310b24ab676SJeff Bonwick 2311b24ab676SJeff Bonwick void 2312b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state) 2313b24ab676SJeff Bonwick { 2314b24ab676SJeff Bonwick spa->spa_log_state = state; 2315b24ab676SJeff Bonwick } 2316b24ab676SJeff Bonwick 2317bf82a41bSeschrock boolean_t 2318bf82a41bSeschrock spa_is_root(spa_t *spa) 2319bf82a41bSeschrock { 2320bf82a41bSeschrock return (spa->spa_is_root); 2321bf82a41bSeschrock } 23228ad4d6ddSJeff Bonwick 23238ad4d6ddSJeff Bonwick boolean_t 23248ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa) 23258ad4d6ddSJeff Bonwick { 23266f793812SPavel Zakharov return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config); 23278ad4d6ddSJeff Bonwick } 23288ad4d6ddSJeff Bonwick 232973527f44SAlex Reece /* 233073527f44SAlex Reece * Returns true if there is a pending sync task in any of the current 233173527f44SAlex Reece * syncing txg, the current quiescing txg, or the current open txg. 233273527f44SAlex Reece */ 233373527f44SAlex Reece boolean_t 233473527f44SAlex Reece spa_has_pending_synctask(spa_t *spa) 233573527f44SAlex Reece { 233686714001SSerapheim Dimitropoulos return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) || 233786714001SSerapheim Dimitropoulos !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks)); 233873527f44SAlex Reece } 233973527f44SAlex Reece 23408ad4d6ddSJeff Bonwick int 23418ad4d6ddSJeff Bonwick spa_mode(spa_t *spa) 23428ad4d6ddSJeff Bonwick { 23438ad4d6ddSJeff Bonwick return (spa->spa_mode); 23448ad4d6ddSJeff Bonwick } 2345b24ab676SJeff Bonwick 2346b24ab676SJeff Bonwick uint64_t 2347b24ab676SJeff Bonwick spa_bootfs(spa_t *spa) 2348b24ab676SJeff Bonwick { 2349b24ab676SJeff Bonwick return (spa->spa_bootfs); 2350b24ab676SJeff Bonwick } 2351b24ab676SJeff Bonwick 2352b24ab676SJeff Bonwick uint64_t 2353b24ab676SJeff Bonwick spa_delegation(spa_t *spa) 2354b24ab676SJeff Bonwick { 2355b24ab676SJeff Bonwick return (spa->spa_delegation); 2356b24ab676SJeff Bonwick } 2357b24ab676SJeff Bonwick 2358b24ab676SJeff Bonwick objset_t * 2359b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa) 2360b24ab676SJeff Bonwick { 2361b24ab676SJeff Bonwick return (spa->spa_meta_objset); 2362b24ab676SJeff Bonwick } 2363b24ab676SJeff Bonwick 2364b24ab676SJeff Bonwick enum zio_checksum 2365b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa) 2366b24ab676SJeff Bonwick { 2367b24ab676SJeff Bonwick return (spa->spa_dedup_checksum); 2368b24ab676SJeff Bonwick } 23693f9d6ad7SLin Ling 23703f9d6ad7SLin Ling /* 23713f9d6ad7SLin Ling * Reset pool scan stat per scan pass (or reboot). 23723f9d6ad7SLin Ling */ 23733f9d6ad7SLin Ling void 23743f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa) 23753f9d6ad7SLin Ling { 23763f9d6ad7SLin Ling /* data not stored on disk */ 23773f9d6ad7SLin Ling spa->spa_scan_pass_start = gethrestime_sec(); 23781702cce7SAlek Pinchuk if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan)) 23791702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start; 23801702cce7SAlek Pinchuk else 23811702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = 0; 23821702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_spent_paused = 0; 23833f9d6ad7SLin Ling spa->spa_scan_pass_exam = 0; 2384a3874b8bSToomas Soome spa->spa_scan_pass_issued = 0; 23853f9d6ad7SLin Ling vdev_scan_stat_init(spa->spa_root_vdev); 23863f9d6ad7SLin Ling } 23873f9d6ad7SLin Ling 23883f9d6ad7SLin Ling /* 23893f9d6ad7SLin Ling * Get scan stats for zpool status reports 23903f9d6ad7SLin Ling */ 23913f9d6ad7SLin Ling int 23923f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 23933f9d6ad7SLin Ling { 23943f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 23953f9d6ad7SLin Ling 23963f9d6ad7SLin Ling if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 2397be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 23983f9d6ad7SLin Ling bzero(ps, sizeof (pool_scan_stat_t)); 23993f9d6ad7SLin Ling 24003f9d6ad7SLin Ling /* data stored on disk */ 24013f9d6ad7SLin Ling ps->pss_func = scn->scn_phys.scn_func; 2402a3874b8bSToomas Soome ps->pss_state = scn->scn_phys.scn_state; 24033f9d6ad7SLin Ling ps->pss_start_time = scn->scn_phys.scn_start_time; 24043f9d6ad7SLin Ling ps->pss_end_time = scn->scn_phys.scn_end_time; 24053f9d6ad7SLin Ling ps->pss_to_examine = scn->scn_phys.scn_to_examine; 24063f9d6ad7SLin Ling ps->pss_to_process = scn->scn_phys.scn_to_process; 24073f9d6ad7SLin Ling ps->pss_processed = scn->scn_phys.scn_processed; 24083f9d6ad7SLin Ling ps->pss_errors = scn->scn_phys.scn_errors; 2409a3874b8bSToomas Soome ps->pss_examined = scn->scn_phys.scn_examined; 2410a3874b8bSToomas Soome ps->pss_issued = 2411a3874b8bSToomas Soome scn->scn_issued_before_pass + spa->spa_scan_pass_issued; 24123f9d6ad7SLin Ling ps->pss_state = scn->scn_phys.scn_state; 24133f9d6ad7SLin Ling 24143f9d6ad7SLin Ling /* data not stored on disk */ 24153f9d6ad7SLin Ling ps->pss_pass_start = spa->spa_scan_pass_start; 24163f9d6ad7SLin Ling ps->pss_pass_exam = spa->spa_scan_pass_exam; 2417a3874b8bSToomas Soome ps->pss_pass_issued = spa->spa_scan_pass_issued; 24181702cce7SAlek Pinchuk ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause; 24191702cce7SAlek Pinchuk ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused; 24203f9d6ad7SLin Ling 24213f9d6ad7SLin Ling return (0); 24223f9d6ad7SLin Ling } 242309c9d376SGeorge Wilson 2424b5152584SMatthew Ahrens int 2425b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa) 2426b5152584SMatthew Ahrens { 2427b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) 2428b5152584SMatthew Ahrens return (SPA_MAXBLOCKSIZE); 2429b5152584SMatthew Ahrens else 2430b5152584SMatthew Ahrens return (SPA_OLD_MAXBLOCKSIZE); 2431b5152584SMatthew Ahrens } 24325cabbc6bSPrashanth Sreenivasa 243354811da5SToomas Soome int 243454811da5SToomas Soome spa_maxdnodesize(spa_t *spa) 243554811da5SToomas Soome { 243654811da5SToomas Soome if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) 243754811da5SToomas Soome return (DNODE_MAX_SIZE); 243854811da5SToomas Soome else 243954811da5SToomas Soome return (DNODE_MIN_SIZE); 244054811da5SToomas Soome } 244154811da5SToomas Soome 2442e0f1c0afSOlaf Faaland boolean_t 2443e0f1c0afSOlaf Faaland spa_multihost(spa_t *spa) 2444e0f1c0afSOlaf Faaland { 2445e0f1c0afSOlaf Faaland return (spa->spa_multihost ? B_TRUE : B_FALSE); 2446e0f1c0afSOlaf Faaland } 2447e0f1c0afSOlaf Faaland 2448e0f1c0afSOlaf Faaland unsigned long 2449e0f1c0afSOlaf Faaland spa_get_hostid(void) 2450e0f1c0afSOlaf Faaland { 2451e0f1c0afSOlaf Faaland unsigned long myhostid; 2452e0f1c0afSOlaf Faaland 2453e0f1c0afSOlaf Faaland #ifdef _KERNEL 2454e0f1c0afSOlaf Faaland myhostid = zone_get_hostid(NULL); 2455e0f1c0afSOlaf Faaland #else /* _KERNEL */ 2456e0f1c0afSOlaf Faaland /* 2457e0f1c0afSOlaf Faaland * We're emulating the system's hostid in userland, so 2458e0f1c0afSOlaf Faaland * we can't use zone_get_hostid(). 2459e0f1c0afSOlaf Faaland */ 2460e0f1c0afSOlaf Faaland (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); 2461e0f1c0afSOlaf Faaland #endif /* _KERNEL */ 2462e0f1c0afSOlaf Faaland 2463e0f1c0afSOlaf Faaland return (myhostid); 2464e0f1c0afSOlaf Faaland } 2465e0f1c0afSOlaf Faaland 24665cabbc6bSPrashanth Sreenivasa /* 24675cabbc6bSPrashanth Sreenivasa * Returns the txg that the last device removal completed. No indirect mappings 24685cabbc6bSPrashanth Sreenivasa * have been added since this txg. 24695cabbc6bSPrashanth Sreenivasa */ 24705cabbc6bSPrashanth Sreenivasa uint64_t 24715cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa) 24725cabbc6bSPrashanth Sreenivasa { 24735cabbc6bSPrashanth Sreenivasa uint64_t vdevid; 24745cabbc6bSPrashanth Sreenivasa uint64_t ret = -1ULL; 24755cabbc6bSPrashanth Sreenivasa 24765cabbc6bSPrashanth Sreenivasa spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 24775cabbc6bSPrashanth Sreenivasa /* 24785cabbc6bSPrashanth Sreenivasa * sr_prev_indirect_vdev is only modified while holding all the 24795cabbc6bSPrashanth Sreenivasa * config locks, so it is sufficient to hold SCL_VDEV as reader when 24805cabbc6bSPrashanth Sreenivasa * examining it. 24815cabbc6bSPrashanth Sreenivasa */ 24825cabbc6bSPrashanth Sreenivasa vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev; 24835cabbc6bSPrashanth Sreenivasa 24845cabbc6bSPrashanth Sreenivasa while (vdevid != -1ULL) { 24855cabbc6bSPrashanth Sreenivasa vdev_t *vd = vdev_lookup_top(spa, vdevid); 24865cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births; 24875cabbc6bSPrashanth Sreenivasa 24885cabbc6bSPrashanth Sreenivasa ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 24895cabbc6bSPrashanth Sreenivasa 24905cabbc6bSPrashanth Sreenivasa /* 24915cabbc6bSPrashanth Sreenivasa * If the removal did not remap any data, we don't care. 24925cabbc6bSPrashanth Sreenivasa */ 24935cabbc6bSPrashanth Sreenivasa if (vdev_indirect_births_count(vib) != 0) { 24945cabbc6bSPrashanth Sreenivasa ret = vdev_indirect_births_last_entry_txg(vib); 24955cabbc6bSPrashanth Sreenivasa break; 24965cabbc6bSPrashanth Sreenivasa } 24975cabbc6bSPrashanth Sreenivasa 24985cabbc6bSPrashanth Sreenivasa vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev; 24995cabbc6bSPrashanth Sreenivasa } 25005cabbc6bSPrashanth Sreenivasa spa_config_exit(spa, SCL_VDEV, FTAG); 25015cabbc6bSPrashanth Sreenivasa 25025cabbc6bSPrashanth Sreenivasa IMPLY(ret != -1ULL, 25035cabbc6bSPrashanth Sreenivasa spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 25045cabbc6bSPrashanth Sreenivasa 25055cabbc6bSPrashanth Sreenivasa return (ret); 25065cabbc6bSPrashanth Sreenivasa } 25076f793812SPavel Zakharov 25086f793812SPavel Zakharov boolean_t 25096f793812SPavel Zakharov spa_trust_config(spa_t *spa) 25106f793812SPavel Zakharov { 25116f793812SPavel Zakharov return (spa->spa_trust_config); 25126f793812SPavel Zakharov } 25136f793812SPavel Zakharov 25146f793812SPavel Zakharov uint64_t 25156f793812SPavel Zakharov spa_missing_tvds_allowed(spa_t *spa) 25166f793812SPavel Zakharov { 25176f793812SPavel Zakharov return (spa->spa_missing_tvds_allowed); 25186f793812SPavel Zakharov } 25196f793812SPavel Zakharov 2520814dcd43SSerapheim Dimitropoulos space_map_t * 2521814dcd43SSerapheim Dimitropoulos spa_syncing_log_sm(spa_t *spa) 2522814dcd43SSerapheim Dimitropoulos { 2523814dcd43SSerapheim Dimitropoulos return (spa->spa_syncing_log_sm); 2524814dcd43SSerapheim Dimitropoulos } 2525814dcd43SSerapheim Dimitropoulos 25266f793812SPavel Zakharov void 25276f793812SPavel Zakharov spa_set_missing_tvds(spa_t *spa, uint64_t missing) 25286f793812SPavel Zakharov { 25296f793812SPavel Zakharov spa->spa_missing_tvds = missing; 25306f793812SPavel Zakharov } 253186714001SSerapheim Dimitropoulos 253286714001SSerapheim Dimitropoulos boolean_t 253386714001SSerapheim Dimitropoulos spa_top_vdevs_spacemap_addressable(spa_t *spa) 253486714001SSerapheim Dimitropoulos { 253586714001SSerapheim Dimitropoulos vdev_t *rvd = spa->spa_root_vdev; 253686714001SSerapheim Dimitropoulos for (uint64_t c = 0; c < rvd->vdev_children; c++) { 253786714001SSerapheim Dimitropoulos if (!vdev_is_spacemap_addressable(rvd->vdev_child[c])) 253886714001SSerapheim Dimitropoulos return (B_FALSE); 253986714001SSerapheim Dimitropoulos } 254086714001SSerapheim Dimitropoulos return (B_TRUE); 254186714001SSerapheim Dimitropoulos } 254286714001SSerapheim Dimitropoulos 254386714001SSerapheim Dimitropoulos boolean_t 254486714001SSerapheim Dimitropoulos spa_has_checkpoint(spa_t *spa) 254586714001SSerapheim Dimitropoulos { 254686714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_txg != 0); 254786714001SSerapheim Dimitropoulos } 254886714001SSerapheim Dimitropoulos 254986714001SSerapheim Dimitropoulos boolean_t 255086714001SSerapheim Dimitropoulos spa_importing_readonly_checkpoint(spa_t *spa) 255186714001SSerapheim Dimitropoulos { 255286714001SSerapheim Dimitropoulos return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) && 255386714001SSerapheim Dimitropoulos spa->spa_mode == FREAD); 255486714001SSerapheim Dimitropoulos } 255586714001SSerapheim Dimitropoulos 255686714001SSerapheim Dimitropoulos uint64_t 255786714001SSerapheim Dimitropoulos spa_min_claim_txg(spa_t *spa) 255886714001SSerapheim Dimitropoulos { 255986714001SSerapheim Dimitropoulos uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg; 256086714001SSerapheim Dimitropoulos 256186714001SSerapheim Dimitropoulos if (checkpoint_txg != 0) 256286714001SSerapheim Dimitropoulos return (checkpoint_txg + 1); 256386714001SSerapheim Dimitropoulos 256486714001SSerapheim Dimitropoulos return (spa->spa_first_txg); 256586714001SSerapheim Dimitropoulos } 256686714001SSerapheim Dimitropoulos 256786714001SSerapheim Dimitropoulos /* 256886714001SSerapheim Dimitropoulos * If there is a checkpoint, async destroys may consume more space from 256986714001SSerapheim Dimitropoulos * the pool instead of freeing it. In an attempt to save the pool from 257086714001SSerapheim Dimitropoulos * getting suspended when it is about to run out of space, we stop 257186714001SSerapheim Dimitropoulos * processing async destroys. 257286714001SSerapheim Dimitropoulos */ 257386714001SSerapheim Dimitropoulos boolean_t 257486714001SSerapheim Dimitropoulos spa_suspend_async_destroy(spa_t *spa) 257586714001SSerapheim Dimitropoulos { 257686714001SSerapheim Dimitropoulos dsl_pool_t *dp = spa_get_dsl(spa); 257786714001SSerapheim Dimitropoulos 257886714001SSerapheim Dimitropoulos uint64_t unreserved = dsl_pool_unreserved_space(dp, 257986714001SSerapheim Dimitropoulos ZFS_SPACE_CHECK_EXTRA_RESERVED); 258086714001SSerapheim Dimitropoulos uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes; 258186714001SSerapheim Dimitropoulos uint64_t avail = (unreserved > used) ? (unreserved - used) : 0; 258286714001SSerapheim Dimitropoulos 258386714001SSerapheim Dimitropoulos if (spa_has_checkpoint(spa) && avail == 0) 258486714001SSerapheim Dimitropoulos return (B_TRUE); 258586714001SSerapheim Dimitropoulos 258686714001SSerapheim Dimitropoulos return (B_FALSE); 258786714001SSerapheim Dimitropoulos } 2588