xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision e914ace2e9d9bf2dbf9a1f1ce81cb776022096f5)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
229842588bSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23f78cdc34SPaul Dagnelie  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24e495b6e6SSaso Kiselkov  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2645818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
281702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/spa_impl.h>
33283b8460SGeorge.Wilson #include <sys/spa_boot.h>
34fa9e4066Sahrens #include <sys/zio.h>
35fa9e4066Sahrens #include <sys/zio_checksum.h>
36fa9e4066Sahrens #include <sys/zio_compress.h>
37fa9e4066Sahrens #include <sys/dmu.h>
38fa9e4066Sahrens #include <sys/dmu_tx.h>
39fa9e4066Sahrens #include <sys/zap.h>
40fa9e4066Sahrens #include <sys/zil.h>
41fa9e4066Sahrens #include <sys/vdev_impl.h>
42094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
43fa9e4066Sahrens #include <sys/metaslab.h>
44fa9e4066Sahrens #include <sys/uberblock_impl.h>
45fa9e4066Sahrens #include <sys/txg.h>
46fa9e4066Sahrens #include <sys/avl.h>
47fa9e4066Sahrens #include <sys/unique.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dsl_dir.h>
50fa9e4066Sahrens #include <sys/dsl_prop.h>
513f9d6ad7SLin Ling #include <sys/dsl_scan.h>
52fa9e4066Sahrens #include <sys/fs/zfs.h>
536ce0521aSperrin #include <sys/metaslab_impl.h>
54e14bb325SJeff Bonwick #include <sys/arc.h>
55485bbbf5SGeorge Wilson #include <sys/ddt.h>
5691ebeef5Sahrens #include "zfs_prop.h"
5745818ee1SMatthew Ahrens #include <sys/zfeature.h>
58fa9e4066Sahrens 
59fa9e4066Sahrens /*
60fa9e4066Sahrens  * SPA locking
61fa9e4066Sahrens  *
62fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
63fa9e4066Sahrens  *
64fa9e4066Sahrens  * spa_namespace_lock (global mutex)
65fa9e4066Sahrens  *
6644cd46caSbillm  *	This lock must be acquired to do any of the following:
67fa9e4066Sahrens  *
6844cd46caSbillm  *		- Lookup a spa_t by name
6944cd46caSbillm  *		- Add or remove a spa_t from the namespace
7044cd46caSbillm  *		- Increase spa_refcount from non-zero
7144cd46caSbillm  *		- Check if spa_refcount is zero
7244cd46caSbillm  *		- Rename a spa_t
73ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
7444cd46caSbillm  *		- Held for the duration of create/destroy/import/export
75fa9e4066Sahrens  *
7644cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
7744cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
7844cd46caSbillm  *	definition they must have an existing reference, and will never need
7944cd46caSbillm  *	to lookup a spa_t by name.
80fa9e4066Sahrens  *
81*e914ace2STim Schumacher  * spa_refcount (per-spa zfs_refcount_t protected by mutex)
82fa9e4066Sahrens  *
8344cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
8444cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8544cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
86088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
8744cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
88fa9e4066Sahrens  *
89e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
90fa9e4066Sahrens  *
9191ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
9291ebeef5Sahrens  *	the following circumstances:
93fa9e4066Sahrens  *
9444cd46caSbillm  *		- RW_READER to perform I/O to the spa
9544cd46caSbillm  *		- RW_WRITER to change the vdev config
96fa9e4066Sahrens  *
97fa9e4066Sahrens  * The locking order is fairly straightforward:
98fa9e4066Sahrens  *
9944cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
100fa9e4066Sahrens  *
10144cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
10244cd46caSbillm  *	or to check if it is zero.
103fa9e4066Sahrens  *
104e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
105fa9e4066Sahrens  *
10644cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10744cd46caSbillm  *	the config lock.
108fa9e4066Sahrens  *
109e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
110fa9e4066Sahrens  *
11144cd46caSbillm  *	The namespace lock must always be taken before the config lock.
112fa9e4066Sahrens  *
113fa9e4066Sahrens  *
114e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
115fa9e4066Sahrens  *
116e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
117e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
118fa9e4066Sahrens  *
11944cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
120fa9e4066Sahrens  *
12144cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
122fa9e4066Sahrens  *
12344cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
12444cd46caSbillm  *				frees up any memory associated with the spa_t.
125fa9e4066Sahrens  *
12644cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12744cd46caSbillm  *				first if NULL is passed.
128fa9e4066Sahrens  *
12944cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
13044cd46caSbillm  *				the system.
131fa9e4066Sahrens  *
132ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
133fa9e4066Sahrens  *
134fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
135fa9e4066Sahrens  *
13644cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13744cd46caSbillm  *				called with spa_namespace_lock held if the
13844cd46caSbillm  *				refcount is currently zero.
139fa9e4066Sahrens  *
14044cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
14144cd46caSbillm  *				not free the spa_t or remove it from the
14244cd46caSbillm  *				namespace.  No locking is required.
143fa9e4066Sahrens  *
14444cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14544cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14644cd46caSbillm  *				held.
147fa9e4066Sahrens  *
148e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
149e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
150e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
151e14bb325SJeff Bonwick  *
152e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
153e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
154e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
155e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
156e14bb325SJeff Bonwick  *
157e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
158e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
159e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
160e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
161e14bb325SJeff Bonwick  *
162e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
163e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
164e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
165e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
166e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
167e14bb325SJeff Bonwick  *
168e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
169e14bb325SJeff Bonwick  *
170e14bb325SJeff Bonwick  * SCL_CONFIG
171e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
172e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
173e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
174e14bb325SJeff Bonwick  *
175e14bb325SJeff Bonwick  * SCL_STATE
176e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
177e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
178e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
179e14bb325SJeff Bonwick  *
180e14bb325SJeff Bonwick  * SCL_ALLOC
181e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
182e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
183e14bb325SJeff Bonwick  *
184e14bb325SJeff Bonwick  * SCL_ZIO
185e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
186e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
187e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
188e14bb325SJeff Bonwick  *
189e14bb325SJeff Bonwick  * SCL_FREE
190e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
191e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
192e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
193e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
194e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
195e14bb325SJeff Bonwick  *
196e14bb325SJeff Bonwick  * SCL_VDEV
197e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
198b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
199e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
200e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
201e14bb325SJeff Bonwick  *
202e14bb325SJeff Bonwick  * In addition, the following rules apply:
203e14bb325SJeff Bonwick  *
204e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
205e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
206e14bb325SJeff Bonwick  *
207e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
208e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
209e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
210e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
211e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
212fa9e4066Sahrens  *
213ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
214fa9e4066Sahrens  *
21544cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
216ea8dc4b6Seschrock  *				for writing.
217fa9e4066Sahrens  *
21844cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21944cd46caSbillm  *				to complete, sync the updated configs to the
220ea8dc4b6Seschrock  *				cache, and release the namespace lock.
221fa9e4066Sahrens  *
222e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
223e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
224e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
225fa9e4066Sahrens  */
226fa9e4066Sahrens 
227fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
228fa9e4066Sahrens kmutex_t spa_namespace_lock;
229fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2300373e76bSbonwick static int spa_active_count;
231416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
232fa9e4066Sahrens 
23399653d4eSeschrock static kmutex_t spa_spare_lock;
23439c23413Seschrock static avl_tree_t spa_spare_avl;
235fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
236fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23799653d4eSeschrock 
238fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2398ad4d6ddSJeff Bonwick int spa_mode_global;
240fa9e4066Sahrens 
241fa9e4066Sahrens #ifdef ZFS_DEBUG
2425cabbc6bSPrashanth Sreenivasa /*
2435cabbc6bSPrashanth Sreenivasa  * Everything except dprintf, spa, and indirect_remap is on by default
2445cabbc6bSPrashanth Sreenivasa  * in debug builds.
2455cabbc6bSPrashanth Sreenivasa  */
24621f7c81cSMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP);
247fa9e4066Sahrens #else
248fa9e4066Sahrens int zfs_flags = 0;
249fa9e4066Sahrens #endif
250fa9e4066Sahrens 
2510125049cSahrens /*
2520125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2530125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2540125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2558b36997aSMatthew Ahrens  * This should only be used as a last resort, as it typically results
2568b36997aSMatthew Ahrens  * in leaked space, or worse.
2570125049cSahrens  */
2587fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE;
2597fd05ac4SMatthew Ahrens 
2607fd05ac4SMatthew Ahrens /*
2617fd05ac4SMatthew Ahrens  * If destroy encounters an EIO while reading metadata (e.g. indirect
2627fd05ac4SMatthew Ahrens  * blocks), space referenced by the missing metadata can not be freed.
2637fd05ac4SMatthew Ahrens  * Normally this causes the background destroy to become "stalled", as
2647fd05ac4SMatthew Ahrens  * it is unable to make forward progress.  While in this stalled state,
2657fd05ac4SMatthew Ahrens  * all remaining space to free from the error-encountering filesystem is
2667fd05ac4SMatthew Ahrens  * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
2677fd05ac4SMatthew Ahrens  * permanently leak the space from indirect blocks that can not be read,
2687fd05ac4SMatthew Ahrens  * and continue to free everything else that it can.
2697fd05ac4SMatthew Ahrens  *
2707fd05ac4SMatthew Ahrens  * The default, "stalling" behavior is useful if the storage partially
2717fd05ac4SMatthew Ahrens  * fails (i.e. some but not all i/os fail), and then later recovers.  In
2727fd05ac4SMatthew Ahrens  * this case, we will be able to continue pool operations while it is
2737fd05ac4SMatthew Ahrens  * partially failed, and when it recovers, we can continue to free the
2747fd05ac4SMatthew Ahrens  * space, with no leaks.  However, note that this case is actually
2757fd05ac4SMatthew Ahrens  * fairly rare.
2767fd05ac4SMatthew Ahrens  *
2777fd05ac4SMatthew Ahrens  * Typically pools either (a) fail completely (but perhaps temporarily,
2787fd05ac4SMatthew Ahrens  * e.g. a top-level vdev going offline), or (b) have localized,
2797fd05ac4SMatthew Ahrens  * permanent errors (e.g. disk returns the wrong data due to bit flip or
2807fd05ac4SMatthew Ahrens  * firmware bug).  In case (a), this setting does not matter because the
2817fd05ac4SMatthew Ahrens  * pool will be suspended and the sync thread will not be able to make
2827fd05ac4SMatthew Ahrens  * forward progress regardless.  In case (b), because the error is
2837fd05ac4SMatthew Ahrens  * permanent, the best we can do is leak the minimum amount of space,
2847fd05ac4SMatthew Ahrens  * which is what setting this flag will do.  Therefore, it is reasonable
2857fd05ac4SMatthew Ahrens  * for this flag to normally be set, but we chose the more conservative
2867fd05ac4SMatthew Ahrens  * approach of not setting it, so that there is no possibility of
2877fd05ac4SMatthew Ahrens  * leaking space in the "partial temporary" failure case.
2887fd05ac4SMatthew Ahrens  */
2897fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE;
2900125049cSahrens 
29169962b56SMatthew Ahrens /*
29269962b56SMatthew Ahrens  * Expiration time in milliseconds. This value has two meanings. First it is
29369962b56SMatthew Ahrens  * used to determine when the spa_deadman() logic should fire. By default the
29469962b56SMatthew Ahrens  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
29569962b56SMatthew Ahrens  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
29669962b56SMatthew Ahrens  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
29769962b56SMatthew Ahrens  * in a system panic.
29869962b56SMatthew Ahrens  */
29969962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL;
300283b8460SGeorge.Wilson 
301283b8460SGeorge.Wilson /*
30269962b56SMatthew Ahrens  * Check time in milliseconds. This defines the frequency at which we check
30369962b56SMatthew Ahrens  * for hung I/O.
304283b8460SGeorge.Wilson  */
30569962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL;
306283b8460SGeorge.Wilson 
307283b8460SGeorge.Wilson /*
308283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
309283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
310283b8460SGeorge.Wilson  */
311283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
312283b8460SGeorge.Wilson 
31369962b56SMatthew Ahrens /*
31469962b56SMatthew Ahrens  * The worst case is single-sector max-parity RAID-Z blocks, in which
31569962b56SMatthew Ahrens  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
31669962b56SMatthew Ahrens  * times the size; so just assume that.  Add to this the fact that
31769962b56SMatthew Ahrens  * we can have up to 3 DVAs per bp, and one more factor of 2 because
31869962b56SMatthew Ahrens  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
31969962b56SMatthew Ahrens  * the worst case is:
32069962b56SMatthew Ahrens  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
32169962b56SMatthew Ahrens  */
32269962b56SMatthew Ahrens int spa_asize_inflation = 24;
323fa9e4066Sahrens 
3247d46dc6cSMatthew Ahrens /*
3257d46dc6cSMatthew Ahrens  * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3267d46dc6cSMatthew Ahrens  * the pool to be consumed.  This ensures that we don't run the pool
3277d46dc6cSMatthew Ahrens  * completely out of space, due to unaccounted changes (e.g. to the MOS).
3287d46dc6cSMatthew Ahrens  * It also limits the worst-case time to allocate space.  If we have
3297d46dc6cSMatthew Ahrens  * less than this amount of free space, most ZPL operations (e.g. write,
3307d46dc6cSMatthew Ahrens  * create) will return ENOSPC.
3317d46dc6cSMatthew Ahrens  *
3327d46dc6cSMatthew Ahrens  * Certain operations (e.g. file removal, most administrative actions) can
3337d46dc6cSMatthew Ahrens  * use half the slop space.  They will only return ENOSPC if less than half
3347d46dc6cSMatthew Ahrens  * the slop space is free.  Typically, once the pool has less than the slop
3357d46dc6cSMatthew Ahrens  * space free, the user will use these operations to free up space in the pool.
3367d46dc6cSMatthew Ahrens  * These are the operations that call dsl_pool_adjustedsize() with the netfree
3377d46dc6cSMatthew Ahrens  * argument set to TRUE.
3387d46dc6cSMatthew Ahrens  *
33986714001SSerapheim Dimitropoulos  * Operations that are almost guaranteed to free up space in the absence of
34086714001SSerapheim Dimitropoulos  * a pool checkpoint can use up to three quarters of the slop space
34186714001SSerapheim Dimitropoulos  * (e.g zfs destroy).
34286714001SSerapheim Dimitropoulos  *
3437d46dc6cSMatthew Ahrens  * A very restricted set of operations are always permitted, regardless of
3447d46dc6cSMatthew Ahrens  * the amount of free space.  These are the operations that call
34586714001SSerapheim Dimitropoulos  * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
34686714001SSerapheim Dimitropoulos  * increase in the amount of space used, it is possible to run the pool
34786714001SSerapheim Dimitropoulos  * completely out of space, causing it to be permanently read-only.
3487d46dc6cSMatthew Ahrens  *
3494b5c8e93SMatthew Ahrens  * Note that on very small pools, the slop space will be larger than
3504b5c8e93SMatthew Ahrens  * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
3514b5c8e93SMatthew Ahrens  * but we never allow it to be more than half the pool size.
3524b5c8e93SMatthew Ahrens  *
3537d46dc6cSMatthew Ahrens  * See also the comments in zfs_space_check_t.
3547d46dc6cSMatthew Ahrens  */
3557d46dc6cSMatthew Ahrens int spa_slop_shift = 5;
3564b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024;
3577d46dc6cSMatthew Ahrens 
358f78cdc34SPaul Dagnelie int spa_allocators = 4;
359f78cdc34SPaul Dagnelie 
3603ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
3613ee8c80cSPavel Zakharov void
3623ee8c80cSPavel Zakharov spa_load_failed(spa_t *spa, const char *fmt, ...)
3633ee8c80cSPavel Zakharov {
3643ee8c80cSPavel Zakharov 	va_list adx;
3653ee8c80cSPavel Zakharov 	char buf[256];
3663ee8c80cSPavel Zakharov 
3673ee8c80cSPavel Zakharov 	va_start(adx, fmt);
3683ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3693ee8c80cSPavel Zakharov 	va_end(adx);
3703ee8c80cSPavel Zakharov 
3716f793812SPavel Zakharov 	zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
3726f793812SPavel Zakharov 	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3733ee8c80cSPavel Zakharov }
3743ee8c80cSPavel Zakharov 
3753ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
3763ee8c80cSPavel Zakharov void
3773ee8c80cSPavel Zakharov spa_load_note(spa_t *spa, const char *fmt, ...)
3783ee8c80cSPavel Zakharov {
3793ee8c80cSPavel Zakharov 	va_list adx;
3803ee8c80cSPavel Zakharov 	char buf[256];
3813ee8c80cSPavel Zakharov 
3823ee8c80cSPavel Zakharov 	va_start(adx, fmt);
3833ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3843ee8c80cSPavel Zakharov 	va_end(adx);
3853ee8c80cSPavel Zakharov 
3866f793812SPavel Zakharov 	zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
3876f793812SPavel Zakharov 	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3883ee8c80cSPavel Zakharov }
3893ee8c80cSPavel Zakharov 
390e05725b1Sbonwick /*
391e05725b1Sbonwick  * ==========================================================================
392e05725b1Sbonwick  * SPA config locking
393e05725b1Sbonwick  * ==========================================================================
394e05725b1Sbonwick  */
395e05725b1Sbonwick static void
396e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
397e14bb325SJeff Bonwick {
398e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
399e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
400e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
401e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
402*e914ace2STim Schumacher 		zfs_refcount_create_untracked(&scl->scl_count);
403e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
404e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
405e14bb325SJeff Bonwick 	}
406e05725b1Sbonwick }
407e05725b1Sbonwick 
408e05725b1Sbonwick static void
409e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
410e14bb325SJeff Bonwick {
411e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
412e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
413e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
414e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
415*e914ace2STim Schumacher 		zfs_refcount_destroy(&scl->scl_count);
416e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
417e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
418e14bb325SJeff Bonwick 	}
419e14bb325SJeff Bonwick }
420e14bb325SJeff Bonwick 
421e14bb325SJeff Bonwick int
422e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
423e05725b1Sbonwick {
424e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
425e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
426e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
427e14bb325SJeff Bonwick 			continue;
428e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
429e14bb325SJeff Bonwick 		if (rw == RW_READER) {
430e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
431e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
432e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
433e495b6e6SSaso Kiselkov 				    tag);
434e14bb325SJeff Bonwick 				return (0);
435e14bb325SJeff Bonwick 			}
436e14bb325SJeff Bonwick 		} else {
437e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
438*e914ace2STim Schumacher 			if (!zfs_refcount_is_zero(&scl->scl_count)) {
439e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
440e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
441e495b6e6SSaso Kiselkov 				    tag);
442e14bb325SJeff Bonwick 				return (0);
443e14bb325SJeff Bonwick 			}
444e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
445e14bb325SJeff Bonwick 		}
446*e914ace2STim Schumacher 		(void) zfs_refcount_add(&scl->scl_count, tag);
447e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
448e14bb325SJeff Bonwick 	}
449e14bb325SJeff Bonwick 	return (1);
450e05725b1Sbonwick }
451e05725b1Sbonwick 
452e05725b1Sbonwick void
453e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
454e05725b1Sbonwick {
455f64c0e34SEric Taylor 	int wlocks_held = 0;
456f64c0e34SEric Taylor 
4573b2aab18SMatthew Ahrens 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4583b2aab18SMatthew Ahrens 
459e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
460e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
461f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
462f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
463e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
464e14bb325SJeff Bonwick 			continue;
465e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
466e14bb325SJeff Bonwick 		if (rw == RW_READER) {
467e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
468e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
469e14bb325SJeff Bonwick 			}
470e14bb325SJeff Bonwick 		} else {
471e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
472*e914ace2STim Schumacher 			while (!zfs_refcount_is_zero(&scl->scl_count)) {
473e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
474e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
475e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
476e14bb325SJeff Bonwick 			}
477e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
478e14bb325SJeff Bonwick 		}
479*e914ace2STim Schumacher 		(void) zfs_refcount_add(&scl->scl_count, tag);
480e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
481e05725b1Sbonwick 	}
4825cabbc6bSPrashanth Sreenivasa 	ASSERT3U(wlocks_held, <=, locks);
483e05725b1Sbonwick }
484e05725b1Sbonwick 
485e05725b1Sbonwick void
486e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
487e05725b1Sbonwick {
488e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
489e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
490e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
491e14bb325SJeff Bonwick 			continue;
492e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
493*e914ace2STim Schumacher 		ASSERT(!zfs_refcount_is_zero(&scl->scl_count));
494*e914ace2STim Schumacher 		if (zfs_refcount_remove(&scl->scl_count, tag) == 0) {
495e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
496e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
497e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
498e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
499e14bb325SJeff Bonwick 		}
500e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
501e05725b1Sbonwick 	}
502e05725b1Sbonwick }
503e05725b1Sbonwick 
504e14bb325SJeff Bonwick int
505e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
506e05725b1Sbonwick {
507e14bb325SJeff Bonwick 	int locks_held = 0;
508e05725b1Sbonwick 
509e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
510e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
511e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
512e14bb325SJeff Bonwick 			continue;
513*e914ace2STim Schumacher 		if ((rw == RW_READER &&
514*e914ace2STim Schumacher 		    !zfs_refcount_is_zero(&scl->scl_count)) ||
515e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
516e14bb325SJeff Bonwick 			locks_held |= 1 << i;
517e14bb325SJeff Bonwick 	}
518e14bb325SJeff Bonwick 
519e14bb325SJeff Bonwick 	return (locks_held);
520e05725b1Sbonwick }
521e05725b1Sbonwick 
522fa9e4066Sahrens /*
523fa9e4066Sahrens  * ==========================================================================
524fa9e4066Sahrens  * SPA namespace functions
525fa9e4066Sahrens  * ==========================================================================
526fa9e4066Sahrens  */
527fa9e4066Sahrens 
528fa9e4066Sahrens /*
529fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
530fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
531fa9e4066Sahrens  */
532fa9e4066Sahrens spa_t *
533fa9e4066Sahrens spa_lookup(const char *name)
534fa9e4066Sahrens {
535e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
536e14bb325SJeff Bonwick 	spa_t *spa;
537fa9e4066Sahrens 	avl_index_t where;
53840feaa91Sahrens 	char *cp;
539fa9e4066Sahrens 
540fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
541fa9e4066Sahrens 
5423b2aab18SMatthew Ahrens 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
5433b2aab18SMatthew Ahrens 
54440feaa91Sahrens 	/*
54540feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
54640feaa91Sahrens 	 * just use that.
54740feaa91Sahrens 	 */
54878f17100SMatthew Ahrens 	cp = strpbrk(search.spa_name, "/@#");
5493b2aab18SMatthew Ahrens 	if (cp != NULL)
55040feaa91Sahrens 		*cp = '\0';
55140feaa91Sahrens 
552fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
553fa9e4066Sahrens 
554fa9e4066Sahrens 	return (spa);
555fa9e4066Sahrens }
556fa9e4066Sahrens 
557283b8460SGeorge.Wilson /*
558283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
559283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
560283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
561283b8460SGeorge.Wilson  */
562283b8460SGeorge.Wilson void
563283b8460SGeorge.Wilson spa_deadman(void *arg)
564283b8460SGeorge.Wilson {
565283b8460SGeorge.Wilson 	spa_t *spa = arg;
566283b8460SGeorge.Wilson 
5670713e232SGeorge Wilson 	/*
5680713e232SGeorge Wilson 	 * Disable the deadman timer if the pool is suspended.
5690713e232SGeorge Wilson 	 */
5700713e232SGeorge Wilson 	if (spa_suspended(spa)) {
5710713e232SGeorge Wilson 		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5720713e232SGeorge Wilson 		return;
5730713e232SGeorge Wilson 	}
5740713e232SGeorge Wilson 
575283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
576283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
577283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
578283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
579283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
580283b8460SGeorge.Wilson }
581283b8460SGeorge.Wilson 
582fa9e4066Sahrens /*
583fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
584fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
585fa9e4066Sahrens  * exist by calling spa_lookup() first.
586fa9e4066Sahrens  */
587fa9e4066Sahrens spa_t *
588468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
589fa9e4066Sahrens {
590fa9e4066Sahrens 	spa_t *spa;
591c5904d13Seschrock 	spa_config_dirent_t *dp;
592283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
593283b8460SGeorge.Wilson 	cyc_time_t when;
594fa9e4066Sahrens 
595fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
596fa9e4066Sahrens 
597fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
598fa9e4066Sahrens 
599c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
600c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
60135a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
602bc9014e6SJustin Gibbs 	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
603c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
60435a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
605c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
60645818ee1SMatthew Ahrens 	mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
60735a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
608a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
609a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
610c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
611c25056deSgw 
612c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
613bc9014e6SJustin Gibbs 	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
61435a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
615c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
616e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
617c25056deSgw 
618b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
619cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
620b24ab676SJeff Bonwick 
621e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
622fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
623fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
6240373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
625468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
62635a5a358SJonathan Adams 	spa->spa_proc = &p0;
62735a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
6286f793812SPavel Zakharov 	spa->spa_trust_config = B_TRUE;
629fa9e4066Sahrens 
630283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
631283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
632283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
633283b8460SGeorge.Wilson 
63469962b56SMatthew Ahrens 	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
635283b8460SGeorge.Wilson 
636283b8460SGeorge.Wilson 	/*
637283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
638283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
639283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
64069962b56SMatthew Ahrens 	 * Instead wait for 5 seconds before checking again.
641283b8460SGeorge.Wilson 	 */
64269962b56SMatthew Ahrens 	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
643283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
644283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
645283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
646283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
647283b8460SGeorge.Wilson 
648*e914ace2STim Schumacher 	zfs_refcount_create(&spa->spa_refcount);
649e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
650fa9e4066Sahrens 
651fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
652fa9e4066Sahrens 
6530373e76bSbonwick 	/*
6540373e76bSbonwick 	 * Set the alternate root, if there is one.
6550373e76bSbonwick 	 */
6560373e76bSbonwick 	if (altroot) {
6570373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
6580373e76bSbonwick 		spa_active_count++;
6590373e76bSbonwick 	}
6600373e76bSbonwick 
661f78cdc34SPaul Dagnelie 	spa->spa_alloc_count = spa_allocators;
662f78cdc34SPaul Dagnelie 	spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
663f78cdc34SPaul Dagnelie 	    sizeof (kmutex_t), KM_SLEEP);
664f78cdc34SPaul Dagnelie 	spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
665f78cdc34SPaul Dagnelie 	    sizeof (avl_tree_t), KM_SLEEP);
666f78cdc34SPaul Dagnelie 	for (int i = 0; i < spa->spa_alloc_count; i++) {
667f78cdc34SPaul Dagnelie 		mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
668f78cdc34SPaul Dagnelie 		avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
669f78cdc34SPaul Dagnelie 		    sizeof (zio_t), offsetof(zio_t, io_alloc_node));
670f78cdc34SPaul Dagnelie 	}
6710f7643c7SGeorge Wilson 
672c5904d13Seschrock 	/*
673c5904d13Seschrock 	 * Every pool starts with the default cachefile
674c5904d13Seschrock 	 */
675c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
676c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
677c5904d13Seschrock 
678c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
679ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
680c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
681c5904d13Seschrock 
6824b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
6834b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
6844b964adaSGeorge Wilson 
685ad135b5dSChristopher Siden 	if (config != NULL) {
686ad135b5dSChristopher Siden 		nvlist_t *features;
687ad135b5dSChristopher Siden 
688ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
689ad135b5dSChristopher Siden 		    &features) == 0) {
690ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
691ad135b5dSChristopher Siden 			    0) == 0);
692ad135b5dSChristopher Siden 		}
693ad135b5dSChristopher Siden 
694468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
695ad135b5dSChristopher Siden 	}
696ad135b5dSChristopher Siden 
697ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
698ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
699ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
700ad135b5dSChristopher Siden 	}
701468c413aSTim Haley 
702c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
703c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
704c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
705c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
706c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
707c3a66015SMatthew Ahrens 	}
708c3a66015SMatthew Ahrens 
70981cd5c55SMatthew Ahrens 	spa->spa_min_ashift = INT_MAX;
71081cd5c55SMatthew Ahrens 	spa->spa_max_ashift = 0;
71181cd5c55SMatthew Ahrens 
71243466aaeSMax Grossman 	/*
71343466aaeSMax Grossman 	 * As a pool is being created, treat all features as disabled by
71443466aaeSMax Grossman 	 * setting SPA_FEATURE_DISABLED for all entries in the feature
71543466aaeSMax Grossman 	 * refcount cache.
71643466aaeSMax Grossman 	 */
71743466aaeSMax Grossman 	for (int i = 0; i < SPA_FEATURES; i++) {
71843466aaeSMax Grossman 		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
71943466aaeSMax Grossman 	}
72043466aaeSMax Grossman 
721fa9e4066Sahrens 	return (spa);
722fa9e4066Sahrens }
723fa9e4066Sahrens 
724fa9e4066Sahrens /*
725fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
726fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
727fa9e4066Sahrens  * deactivated.
728fa9e4066Sahrens  */
729fa9e4066Sahrens void
730fa9e4066Sahrens spa_remove(spa_t *spa)
731fa9e4066Sahrens {
732c5904d13Seschrock 	spa_config_dirent_t *dp;
733c5904d13Seschrock 
734fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
735fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
736*e914ace2STim Schumacher 	ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
737fa9e4066Sahrens 
7381195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
7391195e687SMark J Musante 
740fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
741fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
742fa9e4066Sahrens 
7430373e76bSbonwick 	if (spa->spa_root) {
744fa9e4066Sahrens 		spa_strfree(spa->spa_root);
7450373e76bSbonwick 		spa_active_count--;
7460373e76bSbonwick 	}
747fa9e4066Sahrens 
748c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
749c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
750c5904d13Seschrock 		if (dp->scd_path != NULL)
751c5904d13Seschrock 			spa_strfree(dp->scd_path);
752c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
753c5904d13Seschrock 	}
754c5904d13Seschrock 
755f78cdc34SPaul Dagnelie 	for (int i = 0; i < spa->spa_alloc_count; i++) {
756f78cdc34SPaul Dagnelie 		avl_destroy(&spa->spa_alloc_trees[i]);
757f78cdc34SPaul Dagnelie 		mutex_destroy(&spa->spa_alloc_locks[i]);
758f78cdc34SPaul Dagnelie 	}
759f78cdc34SPaul Dagnelie 	kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count *
760f78cdc34SPaul Dagnelie 	    sizeof (kmutex_t));
761f78cdc34SPaul Dagnelie 	kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count *
762f78cdc34SPaul Dagnelie 	    sizeof (avl_tree_t));
763f78cdc34SPaul Dagnelie 
764c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
7652f8aaab3Seschrock 
766ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
7674b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
768fa9e4066Sahrens 	spa_config_set(spa, NULL);
769fa9e4066Sahrens 
770283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
771283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
772283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
773283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
774283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
775283b8460SGeorge.Wilson 
776*e914ace2STim Schumacher 	zfs_refcount_destroy(&spa->spa_refcount);
77791ebeef5Sahrens 
778e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
779fa9e4066Sahrens 
780c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
781c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
782c3a66015SMatthew Ahrens 
783b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
784cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
785b24ab676SJeff Bonwick 
78645818ee1SMatthew Ahrens 	zio_checksum_templates_free(spa);
78745818ee1SMatthew Ahrens 
788c25056deSgw 	cv_destroy(&spa->spa_async_cv);
789bc9014e6SJustin Gibbs 	cv_destroy(&spa->spa_evicting_os_cv);
79035a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
791c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
792e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
793c25056deSgw 
7945ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
795c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
79635a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
797bc9014e6SJustin Gibbs 	mutex_destroy(&spa->spa_evicting_os_lock);
79806eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
79935a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
800b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
80145818ee1SMatthew Ahrens 	mutex_destroy(&spa->spa_cksum_tmpls_lock);
80235a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
803e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
804a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
805c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
8065ad82045Snd 
807fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
808fa9e4066Sahrens }
809fa9e4066Sahrens 
810fa9e4066Sahrens /*
811fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
812fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
813fa9e4066Sahrens  */
814fa9e4066Sahrens spa_t *
815fa9e4066Sahrens spa_next(spa_t *prev)
816fa9e4066Sahrens {
817fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
818fa9e4066Sahrens 
819fa9e4066Sahrens 	if (prev)
820fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
821fa9e4066Sahrens 	else
822fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
823fa9e4066Sahrens }
824fa9e4066Sahrens 
825fa9e4066Sahrens /*
826fa9e4066Sahrens  * ==========================================================================
827fa9e4066Sahrens  * SPA refcount functions
828fa9e4066Sahrens  * ==========================================================================
829fa9e4066Sahrens  */
830fa9e4066Sahrens 
831fa9e4066Sahrens /*
832fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
833fa9e4066Sahrens  * have the namespace lock held.
834fa9e4066Sahrens  */
835fa9e4066Sahrens void
836fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
837fa9e4066Sahrens {
838*e914ace2STim Schumacher 	ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
839fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
840*e914ace2STim Schumacher 	(void) zfs_refcount_add(&spa->spa_refcount, tag);
841fa9e4066Sahrens }
842fa9e4066Sahrens 
843fa9e4066Sahrens /*
844fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
845fa9e4066Sahrens  * have the namespace lock held.
846fa9e4066Sahrens  */
847fa9e4066Sahrens void
848fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
849fa9e4066Sahrens {
850*e914ace2STim Schumacher 	ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
851fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
852*e914ace2STim Schumacher 	(void) zfs_refcount_remove(&spa->spa_refcount, tag);
853fa9e4066Sahrens }
854fa9e4066Sahrens 
855bc9014e6SJustin Gibbs /*
856bc9014e6SJustin Gibbs  * Remove a reference to the given spa_t held by a dsl dir that is
857bc9014e6SJustin Gibbs  * being asynchronously released.  Async releases occur from a taskq
858bc9014e6SJustin Gibbs  * performing eviction of dsl datasets and dirs.  The namespace lock
859bc9014e6SJustin Gibbs  * isn't held and the hold by the object being evicted may contribute to
860bc9014e6SJustin Gibbs  * spa_minref (e.g. dataset or directory released during pool export),
861bc9014e6SJustin Gibbs  * so the asserts in spa_close() do not apply.
862bc9014e6SJustin Gibbs  */
863bc9014e6SJustin Gibbs void
864bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag)
865bc9014e6SJustin Gibbs {
866*e914ace2STim Schumacher 	(void) zfs_refcount_remove(&spa->spa_refcount, tag);
867bc9014e6SJustin Gibbs }
868bc9014e6SJustin Gibbs 
869fa9e4066Sahrens /*
870fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
871088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
872fa9e4066Sahrens  * number of references acquired when opening a pool
873fa9e4066Sahrens  */
874fa9e4066Sahrens boolean_t
875fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
876fa9e4066Sahrens {
877fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
878fa9e4066Sahrens 
879*e914ace2STim Schumacher 	return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
880fa9e4066Sahrens }
881fa9e4066Sahrens 
88299653d4eSeschrock /*
88399653d4eSeschrock  * ==========================================================================
884fa94a07fSbrendan  * SPA spare and l2cache tracking
88599653d4eSeschrock  * ==========================================================================
88699653d4eSeschrock  */
88799653d4eSeschrock 
888fa94a07fSbrendan /*
889fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
890fa94a07fSbrendan  * for 'auxiliary' devices.
891fa94a07fSbrendan  */
892fa94a07fSbrendan 
893fa94a07fSbrendan typedef struct spa_aux {
894fa94a07fSbrendan 	uint64_t	aux_guid;
895fa94a07fSbrendan 	uint64_t	aux_pool;
896fa94a07fSbrendan 	avl_node_t	aux_avl;
897fa94a07fSbrendan 	int		aux_count;
898fa94a07fSbrendan } spa_aux_t;
899fa94a07fSbrendan 
900fa94a07fSbrendan static int
901fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
902fa94a07fSbrendan {
903fa94a07fSbrendan 	const spa_aux_t *sa = a;
904fa94a07fSbrendan 	const spa_aux_t *sb = b;
905fa94a07fSbrendan 
906fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
907fa94a07fSbrendan 		return (-1);
908fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
909fa94a07fSbrendan 		return (1);
910fa94a07fSbrendan 	else
911fa94a07fSbrendan 		return (0);
912fa94a07fSbrendan }
913fa94a07fSbrendan 
914fa94a07fSbrendan void
915fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
916fa94a07fSbrendan {
917fa94a07fSbrendan 	avl_index_t where;
918fa94a07fSbrendan 	spa_aux_t search;
919fa94a07fSbrendan 	spa_aux_t *aux;
920fa94a07fSbrendan 
921fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
922fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
923fa94a07fSbrendan 		aux->aux_count++;
924fa94a07fSbrendan 	} else {
925fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
926fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
927fa94a07fSbrendan 		aux->aux_count = 1;
928fa94a07fSbrendan 		avl_insert(avl, aux, where);
929fa94a07fSbrendan 	}
930fa94a07fSbrendan }
931fa94a07fSbrendan 
932fa94a07fSbrendan void
933fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
934fa94a07fSbrendan {
935fa94a07fSbrendan 	spa_aux_t search;
936fa94a07fSbrendan 	spa_aux_t *aux;
937fa94a07fSbrendan 	avl_index_t where;
938fa94a07fSbrendan 
939fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
940fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
941fa94a07fSbrendan 
942fa94a07fSbrendan 	ASSERT(aux != NULL);
943fa94a07fSbrendan 
944fa94a07fSbrendan 	if (--aux->aux_count == 0) {
945fa94a07fSbrendan 		avl_remove(avl, aux);
946fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
947fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
948fa94a07fSbrendan 		aux->aux_pool = 0ULL;
949fa94a07fSbrendan 	}
950fa94a07fSbrendan }
951fa94a07fSbrendan 
952fa94a07fSbrendan boolean_t
95389a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
954fa94a07fSbrendan {
955fa94a07fSbrendan 	spa_aux_t search, *found;
956fa94a07fSbrendan 
957fa94a07fSbrendan 	search.aux_guid = guid;
95889a89ebfSlling 	found = avl_find(avl, &search, NULL);
959fa94a07fSbrendan 
960fa94a07fSbrendan 	if (pool) {
961fa94a07fSbrendan 		if (found)
962fa94a07fSbrendan 			*pool = found->aux_pool;
963fa94a07fSbrendan 		else
964fa94a07fSbrendan 			*pool = 0ULL;
965fa94a07fSbrendan 	}
966fa94a07fSbrendan 
96789a89ebfSlling 	if (refcnt) {
96889a89ebfSlling 		if (found)
96989a89ebfSlling 			*refcnt = found->aux_count;
97089a89ebfSlling 		else
97189a89ebfSlling 			*refcnt = 0;
97289a89ebfSlling 	}
97389a89ebfSlling 
974fa94a07fSbrendan 	return (found != NULL);
975fa94a07fSbrendan }
976fa94a07fSbrendan 
977fa94a07fSbrendan void
978fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
979fa94a07fSbrendan {
980fa94a07fSbrendan 	spa_aux_t search, *found;
981fa94a07fSbrendan 	avl_index_t where;
982fa94a07fSbrendan 
983fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
984fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
985fa94a07fSbrendan 	ASSERT(found != NULL);
986fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
987fa94a07fSbrendan 
988fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
989fa94a07fSbrendan }
990fa94a07fSbrendan 
99199653d4eSeschrock /*
99239c23413Seschrock  * Spares are tracked globally due to the following constraints:
99339c23413Seschrock  *
99454811da5SToomas Soome  *	- A spare may be part of multiple pools.
99554811da5SToomas Soome  *	- A spare may be added to a pool even if it's actively in use within
99639c23413Seschrock  *	  another pool.
99754811da5SToomas Soome  *	- A spare in use in any pool can only be the source of a replacement if
99839c23413Seschrock  *	  the target is a spare in the same pool.
99939c23413Seschrock  *
100039c23413Seschrock  * We keep track of all spares on the system through the use of a reference
100139c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
100239c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
100339c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
100439c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
100539c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
100639c23413Seschrock  *
100739c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
100839c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
100939c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
101039c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
101199653d4eSeschrock  */
101239c23413Seschrock 
101399653d4eSeschrock static int
101499653d4eSeschrock spa_spare_compare(const void *a, const void *b)
101599653d4eSeschrock {
1016fa94a07fSbrendan 	return (spa_aux_compare(a, b));
101799653d4eSeschrock }
101899653d4eSeschrock 
101999653d4eSeschrock void
102039c23413Seschrock spa_spare_add(vdev_t *vd)
102199653d4eSeschrock {
102299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
102339c23413Seschrock 	ASSERT(!vd->vdev_isspare);
1024fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
102539c23413Seschrock 	vd->vdev_isspare = B_TRUE;
102699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
102799653d4eSeschrock }
102899653d4eSeschrock 
102999653d4eSeschrock void
103039c23413Seschrock spa_spare_remove(vdev_t *vd)
103199653d4eSeschrock {
103299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
103339c23413Seschrock 	ASSERT(vd->vdev_isspare);
1034fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
103539c23413Seschrock 	vd->vdev_isspare = B_FALSE;
103699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
103799653d4eSeschrock }
103899653d4eSeschrock 
103999653d4eSeschrock boolean_t
104089a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
104199653d4eSeschrock {
1042fa94a07fSbrendan 	boolean_t found;
104399653d4eSeschrock 
104499653d4eSeschrock 	mutex_enter(&spa_spare_lock);
104589a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
104699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
104799653d4eSeschrock 
1048fa94a07fSbrendan 	return (found);
104939c23413Seschrock }
105039c23413Seschrock 
105139c23413Seschrock void
105239c23413Seschrock spa_spare_activate(vdev_t *vd)
105339c23413Seschrock {
105439c23413Seschrock 	mutex_enter(&spa_spare_lock);
105539c23413Seschrock 	ASSERT(vd->vdev_isspare);
1056fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
1057fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
1058fa94a07fSbrendan }
105939c23413Seschrock 
1060fa94a07fSbrendan /*
1061fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
1062fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
1063fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
1064fa94a07fSbrendan  */
106539c23413Seschrock 
1066fa94a07fSbrendan static int
1067fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
1068fa94a07fSbrendan {
1069fa94a07fSbrendan 	return (spa_aux_compare(a, b));
1070fa94a07fSbrendan }
1071fa94a07fSbrendan 
1072fa94a07fSbrendan void
1073fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
1074fa94a07fSbrendan {
1075fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1076fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
1077fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
1078fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
1079fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1080fa94a07fSbrendan }
1081fa94a07fSbrendan 
1082fa94a07fSbrendan void
1083fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
1084fa94a07fSbrendan {
1085fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1086fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1087fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
1088fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
1089fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1090fa94a07fSbrendan }
1091fa94a07fSbrendan 
1092fa94a07fSbrendan boolean_t
1093fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1094fa94a07fSbrendan {
1095fa94a07fSbrendan 	boolean_t found;
1096fa94a07fSbrendan 
1097fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
109889a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1099fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1100fa94a07fSbrendan 
1101fa94a07fSbrendan 	return (found);
1102fa94a07fSbrendan }
1103fa94a07fSbrendan 
1104fa94a07fSbrendan void
1105fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
1106fa94a07fSbrendan {
1107fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1108fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1109fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
1110fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1111fa94a07fSbrendan }
1112fa94a07fSbrendan 
1113fa9e4066Sahrens /*
1114fa9e4066Sahrens  * ==========================================================================
1115fa9e4066Sahrens  * SPA vdev locking
1116fa9e4066Sahrens  * ==========================================================================
1117fa9e4066Sahrens  */
1118fa9e4066Sahrens 
1119fa9e4066Sahrens /*
1120ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
1121ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1122fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
1123fa9e4066Sahrens  */
1124fa9e4066Sahrens uint64_t
1125fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
1126fa9e4066Sahrens {
1127a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
1128bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
112988ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
113088ecc943SGeorge Wilson }
113188ecc943SGeorge Wilson 
113288ecc943SGeorge Wilson /*
113388ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
113488ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
113588ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
113688ecc943SGeorge Wilson  */
113788ecc943SGeorge Wilson uint64_t
113888ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
113988ecc943SGeorge Wilson {
114088ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
11413d7072f8Seschrock 
1142e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1143fa9e4066Sahrens 
1144fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
1145fa9e4066Sahrens }
1146fa9e4066Sahrens 
1147fa9e4066Sahrens /*
114888ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
114988ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
1150fa9e4066Sahrens  */
115188ecc943SGeorge Wilson void
115288ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1153fa9e4066Sahrens {
115488ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
115588ecc943SGeorge Wilson 
11560e34b6a7Sbonwick 	int config_changed = B_FALSE;
1157ea8dc4b6Seschrock 
11580373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
11590e34b6a7Sbonwick 
1160e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
1161e14bb325SJeff Bonwick 
11620e34b6a7Sbonwick 	/*
11630e34b6a7Sbonwick 	 * Reassess the DTLs.
11640e34b6a7Sbonwick 	 */
11650373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
11660e34b6a7Sbonwick 
1167e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
11680e34b6a7Sbonwick 		config_changed = B_TRUE;
11698f18d1faSGeorge Wilson 		spa->spa_config_generation++;
11700e34b6a7Sbonwick 	}
1171ea8dc4b6Seschrock 
117288ecc943SGeorge Wilson 	/*
117388ecc943SGeorge Wilson 	 * Verify the metaslab classes.
117488ecc943SGeorge Wilson 	 */
1175b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1176b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
117788ecc943SGeorge Wilson 
1178e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
1179fa9e4066Sahrens 
118088ecc943SGeorge Wilson 	/*
118188ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
118288ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
118388ecc943SGeorge Wilson 	 * transactionally.
118488ecc943SGeorge Wilson 	 */
118588ecc943SGeorge Wilson 	if (zio_injection_enabled)
11861195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
118788ecc943SGeorge Wilson 
1188fa9e4066Sahrens 	/*
1189fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1190fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1191fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1192fa9e4066Sahrens 	 */
1193fa9e4066Sahrens 	if (error == 0)
1194fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1195fa9e4066Sahrens 
1196fa9e4066Sahrens 	if (vd != NULL) {
11970713e232SGeorge Wilson 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1198094e47e9SGeorge Wilson 		if (vd->vdev_ops->vdev_op_leaf) {
1199094e47e9SGeorge Wilson 			mutex_enter(&vd->vdev_initialize_lock);
1200094e47e9SGeorge Wilson 			vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED);
1201094e47e9SGeorge Wilson 			mutex_exit(&vd->vdev_initialize_lock);
1202094e47e9SGeorge Wilson 		}
1203094e47e9SGeorge Wilson 
12048ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1205fa9e4066Sahrens 		vdev_free(vd);
12068ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1207fa9e4066Sahrens 	}
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 	/*
12100e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1211fa9e4066Sahrens 	 */
12120e34b6a7Sbonwick 	if (config_changed)
12135cabbc6bSPrashanth Sreenivasa 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
121488ecc943SGeorge Wilson }
1215ea8dc4b6Seschrock 
121688ecc943SGeorge Wilson /*
121788ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
121888ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
121988ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
122088ecc943SGeorge Wilson  * information.
122188ecc943SGeorge Wilson  */
122288ecc943SGeorge Wilson int
122388ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
122488ecc943SGeorge Wilson {
122588ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1226ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1227bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 	return (error);
1230fa9e4066Sahrens }
1231fa9e4066Sahrens 
1232e14bb325SJeff Bonwick /*
1233e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1234e14bb325SJeff Bonwick  */
1235e14bb325SJeff Bonwick void
12368f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1237e14bb325SJeff Bonwick {
12388f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
12398f18d1faSGeorge Wilson 
1240dcba9f3fSGeorge Wilson 	/*
1241dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1242dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1243dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1244dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1245dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1246dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1247dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1248dcba9f3fSGeorge Wilson 	 */
1249dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
12509842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
12519842588bSGeorge Wilson 		int high = locks & ~low;
12529842588bSGeorge Wilson 
12539842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1254dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
12559842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1256dcba9f3fSGeorge Wilson 	} else {
1257dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1258dcba9f3fSGeorge Wilson 	}
12598f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1260e14bb325SJeff Bonwick }
1261e14bb325SJeff Bonwick 
1262e14bb325SJeff Bonwick int
1263e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1264e14bb325SJeff Bonwick {
1265c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1266c6065d0fSGeorge Wilson 
1267b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1268b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1269b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1270b24ab676SJeff Bonwick 
12718f18d1faSGeorge Wilson 	if (vd != NULL) {
1272e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1273c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
12748f18d1faSGeorge Wilson 		spa->spa_config_generation++;
12758f18d1faSGeorge Wilson 	}
1276e14bb325SJeff Bonwick 
1277dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1278dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1279dcba9f3fSGeorge Wilson 
12808f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
12818f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1282e14bb325SJeff Bonwick 
12838ad4d6ddSJeff Bonwick 	/*
12848ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
12858ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
12868ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
12878ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
12888ad4d6ddSJeff Bonwick 	 */
12898ad4d6ddSJeff Bonwick 	if (vd != NULL)
12908ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
12918ad4d6ddSJeff Bonwick 
1292c6065d0fSGeorge Wilson 	/*
1293c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1294c6065d0fSGeorge Wilson 	 */
1295c6065d0fSGeorge Wilson 	if (config_changed) {
1296c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
12975cabbc6bSPrashanth Sreenivasa 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
1298c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1299c6065d0fSGeorge Wilson 	}
1300c6065d0fSGeorge Wilson 
1301e14bb325SJeff Bonwick 	return (error);
1302e14bb325SJeff Bonwick }
1303e14bb325SJeff Bonwick 
1304fa9e4066Sahrens /*
1305fa9e4066Sahrens  * ==========================================================================
1306fa9e4066Sahrens  * Miscellaneous functions
1307fa9e4066Sahrens  * ==========================================================================
1308fa9e4066Sahrens  */
1309fa9e4066Sahrens 
1310ad135b5dSChristopher Siden void
131143466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1312ad135b5dSChristopher Siden {
13132acef22dSMatthew Ahrens 	if (!nvlist_exists(spa->spa_label_features, feature)) {
13142acef22dSMatthew Ahrens 		fnvlist_add_boolean(spa->spa_label_features, feature);
131543466aaeSMax Grossman 		/*
131643466aaeSMax Grossman 		 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
131743466aaeSMax Grossman 		 * dirty the vdev config because lock SCL_CONFIG is not held.
131843466aaeSMax Grossman 		 * Thankfully, in this case we don't need to dirty the config
131943466aaeSMax Grossman 		 * because it will be written out anyway when we finish
132043466aaeSMax Grossman 		 * creating the pool.
132143466aaeSMax Grossman 		 */
132243466aaeSMax Grossman 		if (tx->tx_txg != TXG_INITIAL)
132343466aaeSMax Grossman 			vdev_config_dirty(spa->spa_root_vdev);
13242acef22dSMatthew Ahrens 	}
1325ad135b5dSChristopher Siden }
1326ad135b5dSChristopher Siden 
1327ad135b5dSChristopher Siden void
1328ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1329ad135b5dSChristopher Siden {
13302acef22dSMatthew Ahrens 	if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
13312acef22dSMatthew Ahrens 		vdev_config_dirty(spa->spa_root_vdev);
1332ad135b5dSChristopher Siden }
1333ad135b5dSChristopher Siden 
1334fa9e4066Sahrens /*
1335f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1336f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1337f9af39baSGeorge Wilson  * a device with the specified device_guid.
1338fa9e4066Sahrens  */
1339f9af39baSGeorge Wilson spa_t *
1340f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1341fa9e4066Sahrens {
1342fa9e4066Sahrens 	spa_t *spa;
1343fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1344fa9e4066Sahrens 
1345ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1346fa9e4066Sahrens 
1347fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1348fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1349fa9e4066Sahrens 			continue;
1350fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1351fa9e4066Sahrens 			continue;
135239c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
135339c23413Seschrock 			if (device_guid == 0)
135439c23413Seschrock 				break;
135539c23413Seschrock 
135639c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
135739c23413Seschrock 			    device_guid) != NULL)
135839c23413Seschrock 				break;
135939c23413Seschrock 
136039c23413Seschrock 			/*
13618654d025Sperrin 			 * Check any devices we may be in the process of adding.
136239c23413Seschrock 			 */
136339c23413Seschrock 			if (spa->spa_pending_vdev) {
136439c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
136539c23413Seschrock 				    device_guid) != NULL)
136639c23413Seschrock 					break;
136739c23413Seschrock 			}
136839c23413Seschrock 		}
1369fa9e4066Sahrens 	}
1370fa9e4066Sahrens 
1371f9af39baSGeorge Wilson 	return (spa);
1372f9af39baSGeorge Wilson }
1373f9af39baSGeorge Wilson 
1374f9af39baSGeorge Wilson /*
1375f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1376f9af39baSGeorge Wilson  */
1377f9af39baSGeorge Wilson boolean_t
1378f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1379f9af39baSGeorge Wilson {
1380f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1381fa9e4066Sahrens }
1382fa9e4066Sahrens 
1383fa9e4066Sahrens char *
1384fa9e4066Sahrens spa_strdup(const char *s)
1385fa9e4066Sahrens {
1386fa9e4066Sahrens 	size_t len;
1387fa9e4066Sahrens 	char *new;
1388fa9e4066Sahrens 
1389fa9e4066Sahrens 	len = strlen(s);
1390fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1391fa9e4066Sahrens 	bcopy(s, new, len);
1392fa9e4066Sahrens 	new[len] = '\0';
1393fa9e4066Sahrens 
1394fa9e4066Sahrens 	return (new);
1395fa9e4066Sahrens }
1396fa9e4066Sahrens 
1397fa9e4066Sahrens void
1398fa9e4066Sahrens spa_strfree(char *s)
1399fa9e4066Sahrens {
1400fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1401fa9e4066Sahrens }
1402fa9e4066Sahrens 
1403fa9e4066Sahrens uint64_t
1404fa9e4066Sahrens spa_get_random(uint64_t range)
1405fa9e4066Sahrens {
1406fa9e4066Sahrens 	uint64_t r;
1407fa9e4066Sahrens 
1408fa9e4066Sahrens 	ASSERT(range != 0);
1409fa9e4066Sahrens 
1410fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1411fa9e4066Sahrens 
1412fa9e4066Sahrens 	return (r % range);
1413fa9e4066Sahrens }
1414fa9e4066Sahrens 
14151195e687SMark J Musante uint64_t
14161195e687SMark J Musante spa_generate_guid(spa_t *spa)
14171195e687SMark J Musante {
14181195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
14191195e687SMark J Musante 
14201195e687SMark J Musante 	if (spa != NULL) {
14211195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
14221195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14231195e687SMark J Musante 	} else {
14241195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
14251195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14261195e687SMark J Musante 	}
14271195e687SMark J Musante 
14281195e687SMark J Musante 	return (guid);
14291195e687SMark J Musante }
14301195e687SMark J Musante 
1431fa9e4066Sahrens void
143243466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1433fa9e4066Sahrens {
1434ad135b5dSChristopher Siden 	char type[256];
1435f0ba89beSJeff Bonwick 	char *checksum = NULL;
1436f0ba89beSJeff Bonwick 	char *compress = NULL;
1437f0ba89beSJeff Bonwick 
1438f0ba89beSJeff Bonwick 	if (bp != NULL) {
1439ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1440ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1441ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1442ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1443ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1444ad135b5dSChristopher Siden 			    "metadata" : "data",
1445ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1446ad135b5dSChristopher Siden 		} else {
1447ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1448ad135b5dSChristopher Siden 			    sizeof (type));
1449ad135b5dSChristopher Siden 		}
14505d7b4d43SMatthew Ahrens 		if (!BP_IS_EMBEDDED(bp)) {
14515d7b4d43SMatthew Ahrens 			checksum =
14525d7b4d43SMatthew Ahrens 			    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
14535d7b4d43SMatthew Ahrens 		}
1454f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1455f0ba89beSJeff Bonwick 	}
1456fa9e4066Sahrens 
145743466aaeSMax Grossman 	SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
145843466aaeSMax Grossman 	    compress);
1459fa9e4066Sahrens }
1460fa9e4066Sahrens 
1461fa9e4066Sahrens void
1462fa9e4066Sahrens spa_freeze(spa_t *spa)
1463fa9e4066Sahrens {
1464fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1465fa9e4066Sahrens 
1466e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1467fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1468fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1469fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1470fa9e4066Sahrens 	}
1471e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1472fa9e4066Sahrens 	if (freeze_txg != 0)
1473fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1474fa9e4066Sahrens }
1475fa9e4066Sahrens 
14760125049cSahrens void
14770125049cSahrens zfs_panic_recover(const char *fmt, ...)
14780125049cSahrens {
14790125049cSahrens 	va_list adx;
14800125049cSahrens 
14810125049cSahrens 	va_start(adx, fmt);
14820125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
14830125049cSahrens 	va_end(adx);
14840125049cSahrens }
14850125049cSahrens 
14863f9d6ad7SLin Ling /*
14873f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
1488f7170741SWill Andrews  * lowercase hexadecimal numbers that don't overflow.
14893f9d6ad7SLin Ling  */
14903f9d6ad7SLin Ling uint64_t
14914585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr)
14923f9d6ad7SLin Ling {
14933f9d6ad7SLin Ling 	uint64_t val = 0;
14943f9d6ad7SLin Ling 	char c;
14953f9d6ad7SLin Ling 	int digit;
14963f9d6ad7SLin Ling 
14973f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
14983f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
14993f9d6ad7SLin Ling 			digit = c - '0';
15003f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
15013f9d6ad7SLin Ling 			digit = 10 + c - 'a';
15023f9d6ad7SLin Ling 		else
15033f9d6ad7SLin Ling 			break;
15043f9d6ad7SLin Ling 
15053f9d6ad7SLin Ling 		val *= 16;
15063f9d6ad7SLin Ling 		val += digit;
15073f9d6ad7SLin Ling 
15083f9d6ad7SLin Ling 		str++;
15093f9d6ad7SLin Ling 	}
15103f9d6ad7SLin Ling 
15113f9d6ad7SLin Ling 	if (nptr)
15123f9d6ad7SLin Ling 		*nptr = (char *)str;
15133f9d6ad7SLin Ling 
15143f9d6ad7SLin Ling 	return (val);
15153f9d6ad7SLin Ling }
15163f9d6ad7SLin Ling 
1517fa9e4066Sahrens /*
1518fa9e4066Sahrens  * ==========================================================================
1519fa9e4066Sahrens  * Accessor functions
1520fa9e4066Sahrens  * ==========================================================================
1521fa9e4066Sahrens  */
1522fa9e4066Sahrens 
1523088f3894Sahrens boolean_t
152488b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1525fa9e4066Sahrens {
152688b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1527fa9e4066Sahrens }
1528fa9e4066Sahrens 
1529fa9e4066Sahrens dsl_pool_t *
1530fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1531fa9e4066Sahrens {
1532fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1533fa9e4066Sahrens }
1534fa9e4066Sahrens 
1535ad135b5dSChristopher Siden boolean_t
1536ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1537ad135b5dSChristopher Siden {
1538ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1539ad135b5dSChristopher Siden }
1540ad135b5dSChristopher Siden 
15415cabbc6bSPrashanth Sreenivasa boolean_t
15425cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa)
15435cabbc6bSPrashanth Sreenivasa {
15445cabbc6bSPrashanth Sreenivasa 	return (spa->spa_indirect_vdevs_loaded);
15455cabbc6bSPrashanth Sreenivasa }
15465cabbc6bSPrashanth Sreenivasa 
1547fa9e4066Sahrens blkptr_t *
1548fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1549fa9e4066Sahrens {
1550fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1551fa9e4066Sahrens }
1552fa9e4066Sahrens 
1553fa9e4066Sahrens void
1554fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1555fa9e4066Sahrens {
1556fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1557fa9e4066Sahrens }
1558fa9e4066Sahrens 
1559fa9e4066Sahrens void
1560fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1561fa9e4066Sahrens {
1562fa9e4066Sahrens 	if (spa->spa_root == NULL)
1563fa9e4066Sahrens 		buf[0] = '\0';
1564fa9e4066Sahrens 	else
1565fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1566fa9e4066Sahrens }
1567fa9e4066Sahrens 
1568fa9e4066Sahrens int
1569fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1570fa9e4066Sahrens {
1571fa9e4066Sahrens 	return (spa->spa_sync_pass);
1572fa9e4066Sahrens }
1573fa9e4066Sahrens 
1574fa9e4066Sahrens char *
1575fa9e4066Sahrens spa_name(spa_t *spa)
1576fa9e4066Sahrens {
1577fa9e4066Sahrens 	return (spa->spa_name);
1578fa9e4066Sahrens }
1579fa9e4066Sahrens 
1580fa9e4066Sahrens uint64_t
1581fa9e4066Sahrens spa_guid(spa_t *spa)
1582fa9e4066Sahrens {
1583dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1584dfbb9432SGeorge Wilson 	uint64_t guid;
1585dfbb9432SGeorge Wilson 
1586b5989ec7Seschrock 	/*
1587b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1588b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1589e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1590b5989ec7Seschrock 	 * this case.
1591b5989ec7Seschrock 	 */
1592dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1593dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1594dfbb9432SGeorge Wilson 
1595dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1596dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1597dfbb9432SGeorge Wilson 
1598dfbb9432SGeorge Wilson 	/*
1599dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1600dfbb9432SGeorge Wilson 	 * in syncing context.
1601dfbb9432SGeorge Wilson 	 */
1602dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1603b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1604b5989ec7Seschrock 	else
1605dfbb9432SGeorge Wilson 		return (guid);
1606e9103aaeSGarrett D'Amore }
1607e9103aaeSGarrett D'Amore 
1608e9103aaeSGarrett D'Amore uint64_t
1609e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1610e9103aaeSGarrett D'Amore {
1611e9103aaeSGarrett D'Amore 	/*
1612e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1613e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1614e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1615e9103aaeSGarrett D'Amore 	 */
1616e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1617fa9e4066Sahrens }
1618fa9e4066Sahrens 
1619fa9e4066Sahrens uint64_t
1620fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1621fa9e4066Sahrens {
1622fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1623fa9e4066Sahrens }
1624fa9e4066Sahrens 
1625fa9e4066Sahrens uint64_t
1626fa9e4066Sahrens spa_first_txg(spa_t *spa)
1627fa9e4066Sahrens {
1628fa9e4066Sahrens 	return (spa->spa_first_txg);
1629fa9e4066Sahrens }
1630fa9e4066Sahrens 
1631b24ab676SJeff Bonwick uint64_t
1632b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1633b24ab676SJeff Bonwick {
1634b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1635b24ab676SJeff Bonwick }
1636b24ab676SJeff Bonwick 
16373991b535SGeorge Wilson /*
16383991b535SGeorge Wilson  * Return the last txg where data can be dirtied. The final txgs
16393991b535SGeorge Wilson  * will be used to just clear out any deferred frees that remain.
16403991b535SGeorge Wilson  */
16413991b535SGeorge Wilson uint64_t
16423991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa)
16433991b535SGeorge Wilson {
16443991b535SGeorge Wilson 	return (spa->spa_final_txg - TXG_DEFER_SIZE);
16453991b535SGeorge Wilson }
16463991b535SGeorge Wilson 
164788b7b0f2SMatthew Ahrens pool_state_t
1648fa9e4066Sahrens spa_state(spa_t *spa)
1649fa9e4066Sahrens {
1650fa9e4066Sahrens 	return (spa->spa_state);
1651fa9e4066Sahrens }
1652fa9e4066Sahrens 
1653b16da2e2SGeorge Wilson spa_load_state_t
1654b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1655b16da2e2SGeorge Wilson {
1656b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1657b16da2e2SGeorge Wilson }
1658b16da2e2SGeorge Wilson 
1659fa9e4066Sahrens uint64_t
1660fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1661fa9e4066Sahrens {
1662fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1663fa9e4066Sahrens }
1664fa9e4066Sahrens 
1665fa9e4066Sahrens /* ARGSUSED */
1666fa9e4066Sahrens uint64_t
166761e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1668fa9e4066Sahrens {
166969962b56SMatthew Ahrens 	return (lsize * spa_asize_inflation);
167044cd46caSbillm }
167144cd46caSbillm 
16727d46dc6cSMatthew Ahrens /*
16737d46dc6cSMatthew Ahrens  * Return the amount of slop space in bytes.  It is 1/32 of the pool (3.2%),
16744b5c8e93SMatthew Ahrens  * or at least 128MB, unless that would cause it to be more than half the
16754b5c8e93SMatthew Ahrens  * pool size.
16767d46dc6cSMatthew Ahrens  *
16777d46dc6cSMatthew Ahrens  * See the comment above spa_slop_shift for details.
16787d46dc6cSMatthew Ahrens  */
16797d46dc6cSMatthew Ahrens uint64_t
16804b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa)
16814b5c8e93SMatthew Ahrens {
16827d46dc6cSMatthew Ahrens 	uint64_t space = spa_get_dspace(spa);
16834b5c8e93SMatthew Ahrens 	return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
16847d46dc6cSMatthew Ahrens }
16857d46dc6cSMatthew Ahrens 
1686485bbbf5SGeorge Wilson uint64_t
1687485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1688485bbbf5SGeorge Wilson {
1689485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1690485bbbf5SGeorge Wilson }
1691485bbbf5SGeorge Wilson 
169286714001SSerapheim Dimitropoulos uint64_t
169386714001SSerapheim Dimitropoulos spa_get_checkpoint_space(spa_t *spa)
169486714001SSerapheim Dimitropoulos {
169586714001SSerapheim Dimitropoulos 	return (spa->spa_checkpoint_info.sci_dspace);
169686714001SSerapheim Dimitropoulos }
169786714001SSerapheim Dimitropoulos 
1698485bbbf5SGeorge Wilson void
1699485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1700485bbbf5SGeorge Wilson {
1701485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1702485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
17035cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal != NULL) {
17045cabbc6bSPrashanth Sreenivasa 		/*
17055cabbc6bSPrashanth Sreenivasa 		 * We can't allocate from the removing device, so
17065cabbc6bSPrashanth Sreenivasa 		 * subtract its size.  This prevents the DMU/DSL from
17075cabbc6bSPrashanth Sreenivasa 		 * filling up the (now smaller) pool while we are in the
17085cabbc6bSPrashanth Sreenivasa 		 * middle of removing the device.
17095cabbc6bSPrashanth Sreenivasa 		 *
17105cabbc6bSPrashanth Sreenivasa 		 * Note that the DMU/DSL doesn't actually know or care
17115cabbc6bSPrashanth Sreenivasa 		 * how much space is allocated (it does its own tracking
17125cabbc6bSPrashanth Sreenivasa 		 * of how much space has been logically used).  So it
17135cabbc6bSPrashanth Sreenivasa 		 * doesn't matter that the data we are moving may be
17145cabbc6bSPrashanth Sreenivasa 		 * allocated twice (on the old device and the new
17155cabbc6bSPrashanth Sreenivasa 		 * device).
17165cabbc6bSPrashanth Sreenivasa 		 */
17173a4b1be9SMatthew Ahrens 		spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
17183a4b1be9SMatthew Ahrens 		vdev_t *vd =
17193a4b1be9SMatthew Ahrens 		    vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
17205cabbc6bSPrashanth Sreenivasa 		spa->spa_dspace -= spa_deflate(spa) ?
17215cabbc6bSPrashanth Sreenivasa 		    vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
17223a4b1be9SMatthew Ahrens 		spa_config_exit(spa, SCL_VDEV, FTAG);
17235cabbc6bSPrashanth Sreenivasa 	}
1724485bbbf5SGeorge Wilson }
1725485bbbf5SGeorge Wilson 
17260a4e9518Sgw /*
17270a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
17280a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
17290a4e9518Sgw  */
17300a4e9518Sgw uint8_t
17310a4e9518Sgw spa_get_failmode(spa_t *spa)
17320a4e9518Sgw {
17330a4e9518Sgw 	return (spa->spa_failmode);
17340a4e9518Sgw }
17350a4e9518Sgw 
1736e14bb325SJeff Bonwick boolean_t
1737e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1738e14bb325SJeff Bonwick {
1739e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1740e14bb325SJeff Bonwick }
1741e14bb325SJeff Bonwick 
174244cd46caSbillm uint64_t
174344cd46caSbillm spa_version(spa_t *spa)
174444cd46caSbillm {
174544cd46caSbillm 	return (spa->spa_ubsync.ub_version);
174644cd46caSbillm }
174744cd46caSbillm 
1748b24ab676SJeff Bonwick boolean_t
1749b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1750b24ab676SJeff Bonwick {
1751b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1752b24ab676SJeff Bonwick }
1753b24ab676SJeff Bonwick 
1754b24ab676SJeff Bonwick metaslab_class_t *
1755b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1756b24ab676SJeff Bonwick {
1757b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1758b24ab676SJeff Bonwick }
1759b24ab676SJeff Bonwick 
1760b24ab676SJeff Bonwick metaslab_class_t *
1761b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1762b24ab676SJeff Bonwick {
1763b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1764b24ab676SJeff Bonwick }
1765b24ab676SJeff Bonwick 
1766bc9014e6SJustin Gibbs void
1767bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os)
1768bc9014e6SJustin Gibbs {
1769bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1770bc9014e6SJustin Gibbs 	list_insert_head(&spa->spa_evicting_os_list, os);
1771bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1772bc9014e6SJustin Gibbs }
1773bc9014e6SJustin Gibbs 
1774bc9014e6SJustin Gibbs void
1775bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1776bc9014e6SJustin Gibbs {
1777bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1778bc9014e6SJustin Gibbs 	list_remove(&spa->spa_evicting_os_list, os);
1779bc9014e6SJustin Gibbs 	cv_broadcast(&spa->spa_evicting_os_cv);
1780bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1781bc9014e6SJustin Gibbs }
1782bc9014e6SJustin Gibbs 
1783bc9014e6SJustin Gibbs void
1784bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa)
1785bc9014e6SJustin Gibbs {
1786bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1787bc9014e6SJustin Gibbs 	while (!list_is_empty(&spa->spa_evicting_os_list))
1788bc9014e6SJustin Gibbs 		cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1789bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1790bc9014e6SJustin Gibbs 
1791bc9014e6SJustin Gibbs 	dmu_buf_user_evict_wait();
1792bc9014e6SJustin Gibbs }
1793bc9014e6SJustin Gibbs 
179444cd46caSbillm int
179544cd46caSbillm spa_max_replication(spa_t *spa)
179644cd46caSbillm {
179744cd46caSbillm 	/*
1798e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
179944cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
180044cd46caSbillm 	 * replication level accordingly.
1801fa9e4066Sahrens 	 */
1802e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
180344cd46caSbillm 		return (1);
180444cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1805fa9e4066Sahrens }
1806fa9e4066Sahrens 
18073f9d6ad7SLin Ling int
18083f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
18093f9d6ad7SLin Ling {
18103f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
18113f9d6ad7SLin Ling }
18123f9d6ad7SLin Ling 
1813283b8460SGeorge.Wilson uint64_t
1814283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1815283b8460SGeorge.Wilson {
1816283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1817283b8460SGeorge.Wilson }
1818283b8460SGeorge.Wilson 
181999653d4eSeschrock uint64_t
1820b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
182199653d4eSeschrock {
1822b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1823b24ab676SJeff Bonwick 	uint64_t dsize = asize;
182499653d4eSeschrock 
1825b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
182699653d4eSeschrock 
1827b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1828b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1829b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
183099653d4eSeschrock 	}
1831b24ab676SJeff Bonwick 
1832b24ab676SJeff Bonwick 	return (dsize);
1833b24ab676SJeff Bonwick }
1834b24ab676SJeff Bonwick 
1835b24ab676SJeff Bonwick uint64_t
1836b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1837b24ab676SJeff Bonwick {
1838b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1839b24ab676SJeff Bonwick 
18405d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1841b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1842b24ab676SJeff Bonwick 
1843b24ab676SJeff Bonwick 	return (dsize);
1844b24ab676SJeff Bonwick }
1845b24ab676SJeff Bonwick 
1846b24ab676SJeff Bonwick uint64_t
1847b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1848b24ab676SJeff Bonwick {
1849b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1850b24ab676SJeff Bonwick 
1851b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1852b24ab676SJeff Bonwick 
18535d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1854b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1855b24ab676SJeff Bonwick 
1856e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1857b24ab676SJeff Bonwick 
1858b24ab676SJeff Bonwick 	return (dsize);
185999653d4eSeschrock }
186099653d4eSeschrock 
1861abe1fd01SDon Brady uint64_t
1862abe1fd01SDon Brady spa_dirty_data(spa_t *spa)
1863abe1fd01SDon Brady {
1864abe1fd01SDon Brady 	return (spa->spa_dsl_pool->dp_dirty_total);
1865abe1fd01SDon Brady }
1866abe1fd01SDon Brady 
1867fa9e4066Sahrens /*
1868fa9e4066Sahrens  * ==========================================================================
1869fa9e4066Sahrens  * Initialization and Termination
1870fa9e4066Sahrens  * ==========================================================================
1871fa9e4066Sahrens  */
1872fa9e4066Sahrens 
1873fa9e4066Sahrens static int
1874fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1875fa9e4066Sahrens {
1876fa9e4066Sahrens 	const spa_t *s1 = a1;
1877fa9e4066Sahrens 	const spa_t *s2 = a2;
1878fa9e4066Sahrens 	int s;
1879fa9e4066Sahrens 
1880fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1881fa9e4066Sahrens 	if (s > 0)
1882fa9e4066Sahrens 		return (1);
1883fa9e4066Sahrens 	if (s < 0)
1884fa9e4066Sahrens 		return (-1);
1885fa9e4066Sahrens 	return (0);
1886fa9e4066Sahrens }
1887fa9e4066Sahrens 
18880373e76bSbonwick int
18890373e76bSbonwick spa_busy(void)
18900373e76bSbonwick {
18910373e76bSbonwick 	return (spa_active_count);
18920373e76bSbonwick }
18930373e76bSbonwick 
1894e7cbe64fSgw void
1895e7cbe64fSgw spa_boot_init()
1896e7cbe64fSgw {
1897e7cbe64fSgw 	spa_config_load();
1898e7cbe64fSgw }
1899e7cbe64fSgw 
1900fa9e4066Sahrens void
1901fa9e4066Sahrens spa_init(int mode)
1902fa9e4066Sahrens {
1903fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1904c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1905fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1906fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1907fa9e4066Sahrens 
1908fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1909fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1910fa9e4066Sahrens 
1911fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1912fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1913fa94a07fSbrendan 
1914fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1915fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
191699653d4eSeschrock 
19178ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1918fa9e4066Sahrens 
1919283b8460SGeorge.Wilson #ifdef _KERNEL
1920283b8460SGeorge.Wilson 	spa_arch_init();
1921283b8460SGeorge.Wilson #else
1922cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1923cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1924cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1925cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1926cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1927cd1c8b85SMatthew Ahrens 		} else {
1928cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1929cd1c8b85SMatthew Ahrens 		}
1930cd1c8b85SMatthew Ahrens 	}
1931cd1c8b85SMatthew Ahrens #endif
1932cd1c8b85SMatthew Ahrens 
1933*e914ace2STim Schumacher 	zfs_refcount_init();
1934fa9e4066Sahrens 	unique_init();
19350713e232SGeorge Wilson 	range_tree_init();
19368363e80aSGeorge Wilson 	metaslab_alloc_trace_init();
1937fa9e4066Sahrens 	zio_init();
1938fa9e4066Sahrens 	dmu_init();
1939fa9e4066Sahrens 	zil_init();
194087db74c1Sek 	vdev_cache_stat_init();
194191ebeef5Sahrens 	zfs_prop_init();
1942990b4856Slling 	zpool_prop_init();
1943ad135b5dSChristopher Siden 	zpool_feature_init();
1944fa9e4066Sahrens 	spa_config_load();
1945e14bb325SJeff Bonwick 	l2arc_start();
1946fa9e4066Sahrens }
1947fa9e4066Sahrens 
1948fa9e4066Sahrens void
1949fa9e4066Sahrens spa_fini(void)
1950fa9e4066Sahrens {
1951e14bb325SJeff Bonwick 	l2arc_stop();
1952e14bb325SJeff Bonwick 
1953fa9e4066Sahrens 	spa_evict_all();
1954fa9e4066Sahrens 
195587db74c1Sek 	vdev_cache_stat_fini();
1956fa9e4066Sahrens 	zil_fini();
1957fa9e4066Sahrens 	dmu_fini();
1958fa9e4066Sahrens 	zio_fini();
19598363e80aSGeorge Wilson 	metaslab_alloc_trace_fini();
19600713e232SGeorge Wilson 	range_tree_fini();
196191ebeef5Sahrens 	unique_fini();
1962*e914ace2STim Schumacher 	zfs_refcount_fini();
1963fa9e4066Sahrens 
1964fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
196599653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1966fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1967fa9e4066Sahrens 
1968fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1969fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1970c25056deSgw 	mutex_destroy(&spa_spare_lock);
1971fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1972fa9e4066Sahrens }
19736ce0521aSperrin 
19746ce0521aSperrin /*
19756ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
19766ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
19776ce0521aSperrin  * performance and not correctness
19786ce0521aSperrin  */
19796ce0521aSperrin boolean_t
19806ce0521aSperrin spa_has_slogs(spa_t *spa)
19816ce0521aSperrin {
19826ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
19836ce0521aSperrin }
1984bf82a41bSeschrock 
1985b24ab676SJeff Bonwick spa_log_state_t
1986b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1987b24ab676SJeff Bonwick {
1988b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1989b24ab676SJeff Bonwick }
1990b24ab676SJeff Bonwick 
1991b24ab676SJeff Bonwick void
1992b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1993b24ab676SJeff Bonwick {
1994b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1995b24ab676SJeff Bonwick }
1996b24ab676SJeff Bonwick 
1997bf82a41bSeschrock boolean_t
1998bf82a41bSeschrock spa_is_root(spa_t *spa)
1999bf82a41bSeschrock {
2000bf82a41bSeschrock 	return (spa->spa_is_root);
2001bf82a41bSeschrock }
20028ad4d6ddSJeff Bonwick 
20038ad4d6ddSJeff Bonwick boolean_t
20048ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
20058ad4d6ddSJeff Bonwick {
20066f793812SPavel Zakharov 	return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config);
20078ad4d6ddSJeff Bonwick }
20088ad4d6ddSJeff Bonwick 
200973527f44SAlex Reece /*
201073527f44SAlex Reece  * Returns true if there is a pending sync task in any of the current
201173527f44SAlex Reece  * syncing txg, the current quiescing txg, or the current open txg.
201273527f44SAlex Reece  */
201373527f44SAlex Reece boolean_t
201473527f44SAlex Reece spa_has_pending_synctask(spa_t *spa)
201573527f44SAlex Reece {
201686714001SSerapheim Dimitropoulos 	return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
201786714001SSerapheim Dimitropoulos 	    !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
201873527f44SAlex Reece }
201973527f44SAlex Reece 
20208ad4d6ddSJeff Bonwick int
20218ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
20228ad4d6ddSJeff Bonwick {
20238ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
20248ad4d6ddSJeff Bonwick }
2025b24ab676SJeff Bonwick 
2026b24ab676SJeff Bonwick uint64_t
2027b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
2028b24ab676SJeff Bonwick {
2029b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
2030b24ab676SJeff Bonwick }
2031b24ab676SJeff Bonwick 
2032b24ab676SJeff Bonwick uint64_t
2033b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
2034b24ab676SJeff Bonwick {
2035b24ab676SJeff Bonwick 	return (spa->spa_delegation);
2036b24ab676SJeff Bonwick }
2037b24ab676SJeff Bonwick 
2038b24ab676SJeff Bonwick objset_t *
2039b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
2040b24ab676SJeff Bonwick {
2041b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
2042b24ab676SJeff Bonwick }
2043b24ab676SJeff Bonwick 
2044b24ab676SJeff Bonwick enum zio_checksum
2045b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
2046b24ab676SJeff Bonwick {
2047b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
2048b24ab676SJeff Bonwick }
20493f9d6ad7SLin Ling 
20503f9d6ad7SLin Ling /*
20513f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
20523f9d6ad7SLin Ling  */
20533f9d6ad7SLin Ling void
20543f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
20553f9d6ad7SLin Ling {
20563f9d6ad7SLin Ling 	/* data not stored on disk */
20573f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
20581702cce7SAlek Pinchuk 	if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
20591702cce7SAlek Pinchuk 		spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
20601702cce7SAlek Pinchuk 	else
20611702cce7SAlek Pinchuk 		spa->spa_scan_pass_scrub_pause = 0;
20621702cce7SAlek Pinchuk 	spa->spa_scan_pass_scrub_spent_paused = 0;
20633f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
20643f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
20653f9d6ad7SLin Ling }
20663f9d6ad7SLin Ling 
20673f9d6ad7SLin Ling /*
20683f9d6ad7SLin Ling  * Get scan stats for zpool status reports
20693f9d6ad7SLin Ling  */
20703f9d6ad7SLin Ling int
20713f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
20723f9d6ad7SLin Ling {
20733f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
20743f9d6ad7SLin Ling 
20753f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2076be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
20773f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
20783f9d6ad7SLin Ling 
20793f9d6ad7SLin Ling 	/* data stored on disk */
20803f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
20813f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
20823f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
20833f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
20843f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
20853f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
20863f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
20873f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
20883f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
20893f9d6ad7SLin Ling 
20903f9d6ad7SLin Ling 	/* data not stored on disk */
20913f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
20923f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
20931702cce7SAlek Pinchuk 	ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
20941702cce7SAlek Pinchuk 	ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
20953f9d6ad7SLin Ling 
20963f9d6ad7SLin Ling 	return (0);
20973f9d6ad7SLin Ling }
209809c9d376SGeorge Wilson 
2099b5152584SMatthew Ahrens int
2100b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa)
2101b5152584SMatthew Ahrens {
2102b5152584SMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2103b5152584SMatthew Ahrens 		return (SPA_MAXBLOCKSIZE);
2104b5152584SMatthew Ahrens 	else
2105b5152584SMatthew Ahrens 		return (SPA_OLD_MAXBLOCKSIZE);
2106b5152584SMatthew Ahrens }
21075cabbc6bSPrashanth Sreenivasa 
210854811da5SToomas Soome int
210954811da5SToomas Soome spa_maxdnodesize(spa_t *spa)
211054811da5SToomas Soome {
211154811da5SToomas Soome 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
211254811da5SToomas Soome 		return (DNODE_MAX_SIZE);
211354811da5SToomas Soome 	else
211454811da5SToomas Soome 		return (DNODE_MIN_SIZE);
211554811da5SToomas Soome }
211654811da5SToomas Soome 
21175cabbc6bSPrashanth Sreenivasa /*
21185cabbc6bSPrashanth Sreenivasa  * Returns the txg that the last device removal completed. No indirect mappings
21195cabbc6bSPrashanth Sreenivasa  * have been added since this txg.
21205cabbc6bSPrashanth Sreenivasa  */
21215cabbc6bSPrashanth Sreenivasa uint64_t
21225cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa)
21235cabbc6bSPrashanth Sreenivasa {
21245cabbc6bSPrashanth Sreenivasa 	uint64_t vdevid;
21255cabbc6bSPrashanth Sreenivasa 	uint64_t ret = -1ULL;
21265cabbc6bSPrashanth Sreenivasa 
21275cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
21285cabbc6bSPrashanth Sreenivasa 	/*
21295cabbc6bSPrashanth Sreenivasa 	 * sr_prev_indirect_vdev is only modified while holding all the
21305cabbc6bSPrashanth Sreenivasa 	 * config locks, so it is sufficient to hold SCL_VDEV as reader when
21315cabbc6bSPrashanth Sreenivasa 	 * examining it.
21325cabbc6bSPrashanth Sreenivasa 	 */
21335cabbc6bSPrashanth Sreenivasa 	vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
21345cabbc6bSPrashanth Sreenivasa 
21355cabbc6bSPrashanth Sreenivasa 	while (vdevid != -1ULL) {
21365cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa, vdevid);
21375cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_t *vib = vd->vdev_indirect_births;
21385cabbc6bSPrashanth Sreenivasa 
21395cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
21405cabbc6bSPrashanth Sreenivasa 
21415cabbc6bSPrashanth Sreenivasa 		/*
21425cabbc6bSPrashanth Sreenivasa 		 * If the removal did not remap any data, we don't care.
21435cabbc6bSPrashanth Sreenivasa 		 */
21445cabbc6bSPrashanth Sreenivasa 		if (vdev_indirect_births_count(vib) != 0) {
21455cabbc6bSPrashanth Sreenivasa 			ret = vdev_indirect_births_last_entry_txg(vib);
21465cabbc6bSPrashanth Sreenivasa 			break;
21475cabbc6bSPrashanth Sreenivasa 		}
21485cabbc6bSPrashanth Sreenivasa 
21495cabbc6bSPrashanth Sreenivasa 		vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
21505cabbc6bSPrashanth Sreenivasa 	}
21515cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
21525cabbc6bSPrashanth Sreenivasa 
21535cabbc6bSPrashanth Sreenivasa 	IMPLY(ret != -1ULL,
21545cabbc6bSPrashanth Sreenivasa 	    spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
21555cabbc6bSPrashanth Sreenivasa 
21565cabbc6bSPrashanth Sreenivasa 	return (ret);
21575cabbc6bSPrashanth Sreenivasa }
21586f793812SPavel Zakharov 
21596f793812SPavel Zakharov boolean_t
21606f793812SPavel Zakharov spa_trust_config(spa_t *spa)
21616f793812SPavel Zakharov {
21626f793812SPavel Zakharov 	return (spa->spa_trust_config);
21636f793812SPavel Zakharov }
21646f793812SPavel Zakharov 
21656f793812SPavel Zakharov uint64_t
21666f793812SPavel Zakharov spa_missing_tvds_allowed(spa_t *spa)
21676f793812SPavel Zakharov {
21686f793812SPavel Zakharov 	return (spa->spa_missing_tvds_allowed);
21696f793812SPavel Zakharov }
21706f793812SPavel Zakharov 
21716f793812SPavel Zakharov void
21726f793812SPavel Zakharov spa_set_missing_tvds(spa_t *spa, uint64_t missing)
21736f793812SPavel Zakharov {
21746f793812SPavel Zakharov 	spa->spa_missing_tvds = missing;
21756f793812SPavel Zakharov }
217686714001SSerapheim Dimitropoulos 
217786714001SSerapheim Dimitropoulos boolean_t
217886714001SSerapheim Dimitropoulos spa_top_vdevs_spacemap_addressable(spa_t *spa)
217986714001SSerapheim Dimitropoulos {
218086714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
218186714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
218286714001SSerapheim Dimitropoulos 		if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
218386714001SSerapheim Dimitropoulos 			return (B_FALSE);
218486714001SSerapheim Dimitropoulos 	}
218586714001SSerapheim Dimitropoulos 	return (B_TRUE);
218686714001SSerapheim Dimitropoulos }
218786714001SSerapheim Dimitropoulos 
218886714001SSerapheim Dimitropoulos boolean_t
218986714001SSerapheim Dimitropoulos spa_has_checkpoint(spa_t *spa)
219086714001SSerapheim Dimitropoulos {
219186714001SSerapheim Dimitropoulos 	return (spa->spa_checkpoint_txg != 0);
219286714001SSerapheim Dimitropoulos }
219386714001SSerapheim Dimitropoulos 
219486714001SSerapheim Dimitropoulos boolean_t
219586714001SSerapheim Dimitropoulos spa_importing_readonly_checkpoint(spa_t *spa)
219686714001SSerapheim Dimitropoulos {
219786714001SSerapheim Dimitropoulos 	return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
219886714001SSerapheim Dimitropoulos 	    spa->spa_mode == FREAD);
219986714001SSerapheim Dimitropoulos }
220086714001SSerapheim Dimitropoulos 
220186714001SSerapheim Dimitropoulos uint64_t
220286714001SSerapheim Dimitropoulos spa_min_claim_txg(spa_t *spa)
220386714001SSerapheim Dimitropoulos {
220486714001SSerapheim Dimitropoulos 	uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
220586714001SSerapheim Dimitropoulos 
220686714001SSerapheim Dimitropoulos 	if (checkpoint_txg != 0)
220786714001SSerapheim Dimitropoulos 		return (checkpoint_txg + 1);
220886714001SSerapheim Dimitropoulos 
220986714001SSerapheim Dimitropoulos 	return (spa->spa_first_txg);
221086714001SSerapheim Dimitropoulos }
221186714001SSerapheim Dimitropoulos 
221286714001SSerapheim Dimitropoulos /*
221386714001SSerapheim Dimitropoulos  * If there is a checkpoint, async destroys may consume more space from
221486714001SSerapheim Dimitropoulos  * the pool instead of freeing it. In an attempt to save the pool from
221586714001SSerapheim Dimitropoulos  * getting suspended when it is about to run out of space, we stop
221686714001SSerapheim Dimitropoulos  * processing async destroys.
221786714001SSerapheim Dimitropoulos  */
221886714001SSerapheim Dimitropoulos boolean_t
221986714001SSerapheim Dimitropoulos spa_suspend_async_destroy(spa_t *spa)
222086714001SSerapheim Dimitropoulos {
222186714001SSerapheim Dimitropoulos 	dsl_pool_t *dp = spa_get_dsl(spa);
222286714001SSerapheim Dimitropoulos 
222386714001SSerapheim Dimitropoulos 	uint64_t unreserved = dsl_pool_unreserved_space(dp,
222486714001SSerapheim Dimitropoulos 	    ZFS_SPACE_CHECK_EXTRA_RESERVED);
222586714001SSerapheim Dimitropoulos 	uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
222686714001SSerapheim Dimitropoulos 	uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
222786714001SSerapheim Dimitropoulos 
222886714001SSerapheim Dimitropoulos 	if (spa_has_checkpoint(spa) && avail == 0)
222986714001SSerapheim Dimitropoulos 		return (B_TRUE);
223086714001SSerapheim Dimitropoulos 
223186714001SSerapheim Dimitropoulos 	return (B_FALSE);
223286714001SSerapheim Dimitropoulos }
2233