xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 4d7988d6050abba5c1ff60e7fd196e95c22e20f4)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
229842588bSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23814dcd43SSerapheim Dimitropoulos  * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24e495b6e6SSaso Kiselkov  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2645818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
281702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
29663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa_impl.h>
34283b8460SGeorge.Wilson #include <sys/spa_boot.h>
35fa9e4066Sahrens #include <sys/zio.h>
36fa9e4066Sahrens #include <sys/zio_checksum.h>
37fa9e4066Sahrens #include <sys/zio_compress.h>
38fa9e4066Sahrens #include <sys/dmu.h>
39fa9e4066Sahrens #include <sys/dmu_tx.h>
40fa9e4066Sahrens #include <sys/zap.h>
41fa9e4066Sahrens #include <sys/zil.h>
42fa9e4066Sahrens #include <sys/vdev_impl.h>
43094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
44084fd14fSBrian Behlendorf #include <sys/vdev_trim.h>
45fa9e4066Sahrens #include <sys/metaslab.h>
46fa9e4066Sahrens #include <sys/uberblock_impl.h>
47fa9e4066Sahrens #include <sys/txg.h>
48fa9e4066Sahrens #include <sys/avl.h>
49fa9e4066Sahrens #include <sys/unique.h>
50fa9e4066Sahrens #include <sys/dsl_pool.h>
51fa9e4066Sahrens #include <sys/dsl_dir.h>
52fa9e4066Sahrens #include <sys/dsl_prop.h>
533f9d6ad7SLin Ling #include <sys/dsl_scan.h>
54fa9e4066Sahrens #include <sys/fs/zfs.h>
556ce0521aSperrin #include <sys/metaslab_impl.h>
56e14bb325SJeff Bonwick #include <sys/arc.h>
57485bbbf5SGeorge Wilson #include <sys/ddt.h>
5891ebeef5Sahrens #include "zfs_prop.h"
59*4d7988d6SPaul Dagnelie #include <sys/btree.h>
6045818ee1SMatthew Ahrens #include <sys/zfeature.h>
61fa9e4066Sahrens 
62fa9e4066Sahrens /*
63fa9e4066Sahrens  * SPA locking
64fa9e4066Sahrens  *
65814dcd43SSerapheim Dimitropoulos  * There are three basic locks for managing spa_t structures:
66fa9e4066Sahrens  *
67fa9e4066Sahrens  * spa_namespace_lock (global mutex)
68fa9e4066Sahrens  *
6944cd46caSbillm  *	This lock must be acquired to do any of the following:
70fa9e4066Sahrens  *
7144cd46caSbillm  *		- Lookup a spa_t by name
7244cd46caSbillm  *		- Add or remove a spa_t from the namespace
7344cd46caSbillm  *		- Increase spa_refcount from non-zero
7444cd46caSbillm  *		- Check if spa_refcount is zero
7544cd46caSbillm  *		- Rename a spa_t
76ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
7744cd46caSbillm  *		- Held for the duration of create/destroy/import/export
78fa9e4066Sahrens  *
7944cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
8044cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
8144cd46caSbillm  *	definition they must have an existing reference, and will never need
8244cd46caSbillm  *	to lookup a spa_t by name.
83fa9e4066Sahrens  *
84e914ace2STim Schumacher  * spa_refcount (per-spa zfs_refcount_t protected by mutex)
85fa9e4066Sahrens  *
8644cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
8744cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8844cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
89088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
9044cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
91fa9e4066Sahrens  *
92e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
93fa9e4066Sahrens  *
9491ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
9591ebeef5Sahrens  *	the following circumstances:
96fa9e4066Sahrens  *
9744cd46caSbillm  *		- RW_READER to perform I/O to the spa
9844cd46caSbillm  *		- RW_WRITER to change the vdev config
99fa9e4066Sahrens  *
100fa9e4066Sahrens  * The locking order is fairly straightforward:
101fa9e4066Sahrens  *
10244cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
103fa9e4066Sahrens  *
10444cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
10544cd46caSbillm  *	or to check if it is zero.
106fa9e4066Sahrens  *
107e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
108fa9e4066Sahrens  *
10944cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
11044cd46caSbillm  *	the config lock.
111fa9e4066Sahrens  *
112e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
113fa9e4066Sahrens  *
11444cd46caSbillm  *	The namespace lock must always be taken before the config lock.
115fa9e4066Sahrens  *
116fa9e4066Sahrens  *
117e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
118fa9e4066Sahrens  *
119e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
120e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
121fa9e4066Sahrens  *
12244cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
123fa9e4066Sahrens  *
12444cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
125fa9e4066Sahrens  *
12644cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
12744cd46caSbillm  *				frees up any memory associated with the spa_t.
128fa9e4066Sahrens  *
12944cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
13044cd46caSbillm  *				first if NULL is passed.
131fa9e4066Sahrens  *
13244cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
13344cd46caSbillm  *				the system.
134fa9e4066Sahrens  *
135ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
136fa9e4066Sahrens  *
137fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
138fa9e4066Sahrens  *
13944cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
14044cd46caSbillm  *				called with spa_namespace_lock held if the
14144cd46caSbillm  *				refcount is currently zero.
142fa9e4066Sahrens  *
14344cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
14444cd46caSbillm  *				not free the spa_t or remove it from the
14544cd46caSbillm  *				namespace.  No locking is required.
146fa9e4066Sahrens  *
14744cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14844cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14944cd46caSbillm  *				held.
150fa9e4066Sahrens  *
151e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
152e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
153e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
154e14bb325SJeff Bonwick  *
155e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
156e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
157e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
158e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
159e14bb325SJeff Bonwick  *
160e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
161e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
162e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
163e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
164e14bb325SJeff Bonwick  *
165e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
166e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
167e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
168e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
169e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
170e14bb325SJeff Bonwick  *
171e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
172e14bb325SJeff Bonwick  *
173e14bb325SJeff Bonwick  * SCL_CONFIG
174e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
175e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
176e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
177e14bb325SJeff Bonwick  *
178e14bb325SJeff Bonwick  * SCL_STATE
179e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
180e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
181e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
182e14bb325SJeff Bonwick  *
183e14bb325SJeff Bonwick  * SCL_ALLOC
184e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
185e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
186e14bb325SJeff Bonwick  *
187e14bb325SJeff Bonwick  * SCL_ZIO
188e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
189e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
190e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
191e14bb325SJeff Bonwick  *
192e14bb325SJeff Bonwick  * SCL_FREE
193e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
194e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
195e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
196e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
197e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
198e14bb325SJeff Bonwick  *
199e14bb325SJeff Bonwick  * SCL_VDEV
200e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
201b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
202e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
203e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
204e14bb325SJeff Bonwick  *
205e14bb325SJeff Bonwick  * In addition, the following rules apply:
206e14bb325SJeff Bonwick  *
207e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
208e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
209e14bb325SJeff Bonwick  *
210e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
211e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
212e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
213e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
214e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
215fa9e4066Sahrens  *
216ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
217fa9e4066Sahrens  *
21844cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
219ea8dc4b6Seschrock  *				for writing.
220fa9e4066Sahrens  *
22144cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
22244cd46caSbillm  *				to complete, sync the updated configs to the
223ea8dc4b6Seschrock  *				cache, and release the namespace lock.
224fa9e4066Sahrens  *
225e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
226e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
227e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
228fa9e4066Sahrens  */
229fa9e4066Sahrens 
230fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
231fa9e4066Sahrens kmutex_t spa_namespace_lock;
232fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2330373e76bSbonwick static int spa_active_count;
234416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
235fa9e4066Sahrens 
23699653d4eSeschrock static kmutex_t spa_spare_lock;
23739c23413Seschrock static avl_tree_t spa_spare_avl;
238fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
239fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
24099653d4eSeschrock 
241fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2428ad4d6ddSJeff Bonwick int spa_mode_global;
243fa9e4066Sahrens 
244fa9e4066Sahrens #ifdef ZFS_DEBUG
2455cabbc6bSPrashanth Sreenivasa /*
2465cabbc6bSPrashanth Sreenivasa  * Everything except dprintf, spa, and indirect_remap is on by default
2475cabbc6bSPrashanth Sreenivasa  * in debug builds.
2485cabbc6bSPrashanth Sreenivasa  */
24921f7c81cSMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP);
250fa9e4066Sahrens #else
251fa9e4066Sahrens int zfs_flags = 0;
252fa9e4066Sahrens #endif
253fa9e4066Sahrens 
2540125049cSahrens /*
2550125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2560125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2570125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2588b36997aSMatthew Ahrens  * This should only be used as a last resort, as it typically results
2598b36997aSMatthew Ahrens  * in leaked space, or worse.
2600125049cSahrens  */
2617fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE;
2627fd05ac4SMatthew Ahrens 
2637fd05ac4SMatthew Ahrens /*
2647fd05ac4SMatthew Ahrens  * If destroy encounters an EIO while reading metadata (e.g. indirect
2657fd05ac4SMatthew Ahrens  * blocks), space referenced by the missing metadata can not be freed.
2667fd05ac4SMatthew Ahrens  * Normally this causes the background destroy to become "stalled", as
2677fd05ac4SMatthew Ahrens  * it is unable to make forward progress.  While in this stalled state,
2687fd05ac4SMatthew Ahrens  * all remaining space to free from the error-encountering filesystem is
2697fd05ac4SMatthew Ahrens  * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
2707fd05ac4SMatthew Ahrens  * permanently leak the space from indirect blocks that can not be read,
2717fd05ac4SMatthew Ahrens  * and continue to free everything else that it can.
2727fd05ac4SMatthew Ahrens  *
2737fd05ac4SMatthew Ahrens  * The default, "stalling" behavior is useful if the storage partially
2747fd05ac4SMatthew Ahrens  * fails (i.e. some but not all i/os fail), and then later recovers.  In
2757fd05ac4SMatthew Ahrens  * this case, we will be able to continue pool operations while it is
2767fd05ac4SMatthew Ahrens  * partially failed, and when it recovers, we can continue to free the
2777fd05ac4SMatthew Ahrens  * space, with no leaks.  However, note that this case is actually
2787fd05ac4SMatthew Ahrens  * fairly rare.
2797fd05ac4SMatthew Ahrens  *
2807fd05ac4SMatthew Ahrens  * Typically pools either (a) fail completely (but perhaps temporarily,
2817fd05ac4SMatthew Ahrens  * e.g. a top-level vdev going offline), or (b) have localized,
2827fd05ac4SMatthew Ahrens  * permanent errors (e.g. disk returns the wrong data due to bit flip or
2837fd05ac4SMatthew Ahrens  * firmware bug).  In case (a), this setting does not matter because the
2847fd05ac4SMatthew Ahrens  * pool will be suspended and the sync thread will not be able to make
2857fd05ac4SMatthew Ahrens  * forward progress regardless.  In case (b), because the error is
2867fd05ac4SMatthew Ahrens  * permanent, the best we can do is leak the minimum amount of space,
2877fd05ac4SMatthew Ahrens  * which is what setting this flag will do.  Therefore, it is reasonable
2887fd05ac4SMatthew Ahrens  * for this flag to normally be set, but we chose the more conservative
2897fd05ac4SMatthew Ahrens  * approach of not setting it, so that there is no possibility of
2907fd05ac4SMatthew Ahrens  * leaking space in the "partial temporary" failure case.
2917fd05ac4SMatthew Ahrens  */
2927fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE;
2930125049cSahrens 
29469962b56SMatthew Ahrens /*
29569962b56SMatthew Ahrens  * Expiration time in milliseconds. This value has two meanings. First it is
29669962b56SMatthew Ahrens  * used to determine when the spa_deadman() logic should fire. By default the
29769962b56SMatthew Ahrens  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
29869962b56SMatthew Ahrens  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
29969962b56SMatthew Ahrens  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
30069962b56SMatthew Ahrens  * in a system panic.
30169962b56SMatthew Ahrens  */
30269962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL;
303283b8460SGeorge.Wilson 
304283b8460SGeorge.Wilson /*
30569962b56SMatthew Ahrens  * Check time in milliseconds. This defines the frequency at which we check
30669962b56SMatthew Ahrens  * for hung I/O.
307283b8460SGeorge.Wilson  */
30869962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL;
309283b8460SGeorge.Wilson 
310283b8460SGeorge.Wilson /*
311283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
312283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
313283b8460SGeorge.Wilson  */
314283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
315283b8460SGeorge.Wilson 
31669962b56SMatthew Ahrens /*
31769962b56SMatthew Ahrens  * The worst case is single-sector max-parity RAID-Z blocks, in which
31869962b56SMatthew Ahrens  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
31969962b56SMatthew Ahrens  * times the size; so just assume that.  Add to this the fact that
32069962b56SMatthew Ahrens  * we can have up to 3 DVAs per bp, and one more factor of 2 because
32169962b56SMatthew Ahrens  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
32269962b56SMatthew Ahrens  * the worst case is:
32369962b56SMatthew Ahrens  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
32469962b56SMatthew Ahrens  */
32569962b56SMatthew Ahrens int spa_asize_inflation = 24;
326fa9e4066Sahrens 
3277d46dc6cSMatthew Ahrens /*
3287d46dc6cSMatthew Ahrens  * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3297d46dc6cSMatthew Ahrens  * the pool to be consumed.  This ensures that we don't run the pool
3307d46dc6cSMatthew Ahrens  * completely out of space, due to unaccounted changes (e.g. to the MOS).
3317d46dc6cSMatthew Ahrens  * It also limits the worst-case time to allocate space.  If we have
3327d46dc6cSMatthew Ahrens  * less than this amount of free space, most ZPL operations (e.g. write,
3337d46dc6cSMatthew Ahrens  * create) will return ENOSPC.
3347d46dc6cSMatthew Ahrens  *
3357d46dc6cSMatthew Ahrens  * Certain operations (e.g. file removal, most administrative actions) can
3367d46dc6cSMatthew Ahrens  * use half the slop space.  They will only return ENOSPC if less than half
3377d46dc6cSMatthew Ahrens  * the slop space is free.  Typically, once the pool has less than the slop
3387d46dc6cSMatthew Ahrens  * space free, the user will use these operations to free up space in the pool.
3397d46dc6cSMatthew Ahrens  * These are the operations that call dsl_pool_adjustedsize() with the netfree
3407d46dc6cSMatthew Ahrens  * argument set to TRUE.
3417d46dc6cSMatthew Ahrens  *
34286714001SSerapheim Dimitropoulos  * Operations that are almost guaranteed to free up space in the absence of
34386714001SSerapheim Dimitropoulos  * a pool checkpoint can use up to three quarters of the slop space
34486714001SSerapheim Dimitropoulos  * (e.g zfs destroy).
34586714001SSerapheim Dimitropoulos  *
3467d46dc6cSMatthew Ahrens  * A very restricted set of operations are always permitted, regardless of
3477d46dc6cSMatthew Ahrens  * the amount of free space.  These are the operations that call
34886714001SSerapheim Dimitropoulos  * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
34986714001SSerapheim Dimitropoulos  * increase in the amount of space used, it is possible to run the pool
35086714001SSerapheim Dimitropoulos  * completely out of space, causing it to be permanently read-only.
3517d46dc6cSMatthew Ahrens  *
3524b5c8e93SMatthew Ahrens  * Note that on very small pools, the slop space will be larger than
3534b5c8e93SMatthew Ahrens  * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
3544b5c8e93SMatthew Ahrens  * but we never allow it to be more than half the pool size.
3554b5c8e93SMatthew Ahrens  *
3567d46dc6cSMatthew Ahrens  * See also the comments in zfs_space_check_t.
3577d46dc6cSMatthew Ahrens  */
3587d46dc6cSMatthew Ahrens int spa_slop_shift = 5;
3594b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024;
3607d46dc6cSMatthew Ahrens 
361f78cdc34SPaul Dagnelie int spa_allocators = 4;
362f78cdc34SPaul Dagnelie 
3633ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
3643ee8c80cSPavel Zakharov void
3653ee8c80cSPavel Zakharov spa_load_failed(spa_t *spa, const char *fmt, ...)
3663ee8c80cSPavel Zakharov {
3673ee8c80cSPavel Zakharov 	va_list adx;
3683ee8c80cSPavel Zakharov 	char buf[256];
3693ee8c80cSPavel Zakharov 
3703ee8c80cSPavel Zakharov 	va_start(adx, fmt);
3713ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3723ee8c80cSPavel Zakharov 	va_end(adx);
3733ee8c80cSPavel Zakharov 
3746f793812SPavel Zakharov 	zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
3756f793812SPavel Zakharov 	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3763ee8c80cSPavel Zakharov }
3773ee8c80cSPavel Zakharov 
3783ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
3793ee8c80cSPavel Zakharov void
3803ee8c80cSPavel Zakharov spa_load_note(spa_t *spa, const char *fmt, ...)
3813ee8c80cSPavel Zakharov {
3823ee8c80cSPavel Zakharov 	va_list adx;
3833ee8c80cSPavel Zakharov 	char buf[256];
3843ee8c80cSPavel Zakharov 
3853ee8c80cSPavel Zakharov 	va_start(adx, fmt);
3863ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3873ee8c80cSPavel Zakharov 	va_end(adx);
3883ee8c80cSPavel Zakharov 
3896f793812SPavel Zakharov 	zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
3906f793812SPavel Zakharov 	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3913ee8c80cSPavel Zakharov }
3923ee8c80cSPavel Zakharov 
393663207adSDon Brady /*
394663207adSDon Brady  * By default dedup and user data indirects land in the special class
395663207adSDon Brady  */
396663207adSDon Brady int zfs_ddt_data_is_special = B_TRUE;
397663207adSDon Brady int zfs_user_indirect_is_special = B_TRUE;
398663207adSDon Brady 
399663207adSDon Brady /*
400663207adSDon Brady  * The percentage of special class final space reserved for metadata only.
401663207adSDon Brady  * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
402663207adSDon Brady  * let metadata into the class.
403663207adSDon Brady  */
404663207adSDon Brady int zfs_special_class_metadata_reserve_pct = 25;
405663207adSDon Brady 
406e05725b1Sbonwick /*
407e05725b1Sbonwick  * ==========================================================================
408e05725b1Sbonwick  * SPA config locking
409e05725b1Sbonwick  * ==========================================================================
410e05725b1Sbonwick  */
411e05725b1Sbonwick static void
412e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
413e14bb325SJeff Bonwick {
414e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
415e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
416e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
417e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
418e914ace2STim Schumacher 		zfs_refcount_create_untracked(&scl->scl_count);
419e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
420e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
421e14bb325SJeff Bonwick 	}
422e05725b1Sbonwick }
423e05725b1Sbonwick 
424e05725b1Sbonwick static void
425e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
426e14bb325SJeff Bonwick {
427e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
428e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
429e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
430e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
431e914ace2STim Schumacher 		zfs_refcount_destroy(&scl->scl_count);
432e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
433e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
434e14bb325SJeff Bonwick 	}
435e14bb325SJeff Bonwick }
436e14bb325SJeff Bonwick 
437e14bb325SJeff Bonwick int
438e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
439e05725b1Sbonwick {
440e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
441e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
442e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
443e14bb325SJeff Bonwick 			continue;
444e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
445e14bb325SJeff Bonwick 		if (rw == RW_READER) {
446e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
447e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
448e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
449e495b6e6SSaso Kiselkov 				    tag);
450e14bb325SJeff Bonwick 				return (0);
451e14bb325SJeff Bonwick 			}
452e14bb325SJeff Bonwick 		} else {
453e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
454e914ace2STim Schumacher 			if (!zfs_refcount_is_zero(&scl->scl_count)) {
455e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
456e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
457e495b6e6SSaso Kiselkov 				    tag);
458e14bb325SJeff Bonwick 				return (0);
459e14bb325SJeff Bonwick 			}
460e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
461e14bb325SJeff Bonwick 		}
462e914ace2STim Schumacher 		(void) zfs_refcount_add(&scl->scl_count, tag);
463e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
464e14bb325SJeff Bonwick 	}
465e14bb325SJeff Bonwick 	return (1);
466e05725b1Sbonwick }
467e05725b1Sbonwick 
468e05725b1Sbonwick void
469e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
470e05725b1Sbonwick {
471f64c0e34SEric Taylor 	int wlocks_held = 0;
472f64c0e34SEric Taylor 
4733b2aab18SMatthew Ahrens 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4743b2aab18SMatthew Ahrens 
475e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
476e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
477f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
478f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
479e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
480e14bb325SJeff Bonwick 			continue;
481e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
482e14bb325SJeff Bonwick 		if (rw == RW_READER) {
483e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
484e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
485e14bb325SJeff Bonwick 			}
486e14bb325SJeff Bonwick 		} else {
487e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
488e914ace2STim Schumacher 			while (!zfs_refcount_is_zero(&scl->scl_count)) {
489e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
490e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
491e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
492e14bb325SJeff Bonwick 			}
493e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
494e14bb325SJeff Bonwick 		}
495e914ace2STim Schumacher 		(void) zfs_refcount_add(&scl->scl_count, tag);
496e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
497e05725b1Sbonwick 	}
4985cabbc6bSPrashanth Sreenivasa 	ASSERT3U(wlocks_held, <=, locks);
499e05725b1Sbonwick }
500e05725b1Sbonwick 
501e05725b1Sbonwick void
502e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
503e05725b1Sbonwick {
504e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
505e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
506e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
507e14bb325SJeff Bonwick 			continue;
508e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
509e914ace2STim Schumacher 		ASSERT(!zfs_refcount_is_zero(&scl->scl_count));
510e914ace2STim Schumacher 		if (zfs_refcount_remove(&scl->scl_count, tag) == 0) {
511e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
512e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
513e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
514e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
515e14bb325SJeff Bonwick 		}
516e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
517e05725b1Sbonwick 	}
518e05725b1Sbonwick }
519e05725b1Sbonwick 
520e14bb325SJeff Bonwick int
521e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
522e05725b1Sbonwick {
523e14bb325SJeff Bonwick 	int locks_held = 0;
524e05725b1Sbonwick 
525e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
526e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
527e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
528e14bb325SJeff Bonwick 			continue;
529e914ace2STim Schumacher 		if ((rw == RW_READER &&
530e914ace2STim Schumacher 		    !zfs_refcount_is_zero(&scl->scl_count)) ||
531e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
532e14bb325SJeff Bonwick 			locks_held |= 1 << i;
533e14bb325SJeff Bonwick 	}
534e14bb325SJeff Bonwick 
535e14bb325SJeff Bonwick 	return (locks_held);
536e05725b1Sbonwick }
537e05725b1Sbonwick 
538fa9e4066Sahrens /*
539fa9e4066Sahrens  * ==========================================================================
540fa9e4066Sahrens  * SPA namespace functions
541fa9e4066Sahrens  * ==========================================================================
542fa9e4066Sahrens  */
543fa9e4066Sahrens 
544fa9e4066Sahrens /*
545fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
546fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
547fa9e4066Sahrens  */
548fa9e4066Sahrens spa_t *
549fa9e4066Sahrens spa_lookup(const char *name)
550fa9e4066Sahrens {
551e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
552e14bb325SJeff Bonwick 	spa_t *spa;
553fa9e4066Sahrens 	avl_index_t where;
55440feaa91Sahrens 	char *cp;
555fa9e4066Sahrens 
556fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
557fa9e4066Sahrens 
5583b2aab18SMatthew Ahrens 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
5593b2aab18SMatthew Ahrens 
56040feaa91Sahrens 	/*
56140feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
56240feaa91Sahrens 	 * just use that.
56340feaa91Sahrens 	 */
56478f17100SMatthew Ahrens 	cp = strpbrk(search.spa_name, "/@#");
5653b2aab18SMatthew Ahrens 	if (cp != NULL)
56640feaa91Sahrens 		*cp = '\0';
56740feaa91Sahrens 
568fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
569fa9e4066Sahrens 
570fa9e4066Sahrens 	return (spa);
571fa9e4066Sahrens }
572fa9e4066Sahrens 
573283b8460SGeorge.Wilson /*
574283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
575283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
576283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
577283b8460SGeorge.Wilson  */
578283b8460SGeorge.Wilson void
579283b8460SGeorge.Wilson spa_deadman(void *arg)
580283b8460SGeorge.Wilson {
581283b8460SGeorge.Wilson 	spa_t *spa = arg;
582283b8460SGeorge.Wilson 
5830713e232SGeorge Wilson 	/*
5840713e232SGeorge Wilson 	 * Disable the deadman timer if the pool is suspended.
5850713e232SGeorge Wilson 	 */
5860713e232SGeorge Wilson 	if (spa_suspended(spa)) {
5870713e232SGeorge Wilson 		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5880713e232SGeorge Wilson 		return;
5890713e232SGeorge Wilson 	}
5900713e232SGeorge Wilson 
591283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
592283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
593283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
594283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
595283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
596283b8460SGeorge.Wilson }
597283b8460SGeorge.Wilson 
598814dcd43SSerapheim Dimitropoulos int
599814dcd43SSerapheim Dimitropoulos spa_log_sm_sort_by_txg(const void *va, const void *vb)
600814dcd43SSerapheim Dimitropoulos {
601814dcd43SSerapheim Dimitropoulos 	const spa_log_sm_t *a = va;
602814dcd43SSerapheim Dimitropoulos 	const spa_log_sm_t *b = vb;
603814dcd43SSerapheim Dimitropoulos 
604*4d7988d6SPaul Dagnelie 	return (TREE_CMP(a->sls_txg, b->sls_txg));
605814dcd43SSerapheim Dimitropoulos }
606814dcd43SSerapheim Dimitropoulos 
607fa9e4066Sahrens /*
608fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
609fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
610fa9e4066Sahrens  * exist by calling spa_lookup() first.
611fa9e4066Sahrens  */
612fa9e4066Sahrens spa_t *
613468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
614fa9e4066Sahrens {
615fa9e4066Sahrens 	spa_t *spa;
616c5904d13Seschrock 	spa_config_dirent_t *dp;
617283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
618283b8460SGeorge.Wilson 	cyc_time_t when;
619fa9e4066Sahrens 
620fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
621fa9e4066Sahrens 
622fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
623fa9e4066Sahrens 
624c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
625c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
62635a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
627bc9014e6SJustin Gibbs 	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
628c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
62935a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
630c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
63145818ee1SMatthew Ahrens 	mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
63235a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
633a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
634a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
635c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
636814dcd43SSerapheim Dimitropoulos 	mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
637c25056deSgw 
638c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
639bc9014e6SJustin Gibbs 	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
64035a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
641c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
642e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
643c25056deSgw 
644b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
645cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
646b24ab676SJeff Bonwick 
647e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
648fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
649fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
6500373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
651468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
65235a5a358SJonathan Adams 	spa->spa_proc = &p0;
65335a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
6546f793812SPavel Zakharov 	spa->spa_trust_config = B_TRUE;
655fa9e4066Sahrens 
656283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
657283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
658283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
659283b8460SGeorge.Wilson 
66069962b56SMatthew Ahrens 	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
661283b8460SGeorge.Wilson 
662283b8460SGeorge.Wilson 	/*
663283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
664283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
665283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
66669962b56SMatthew Ahrens 	 * Instead wait for 5 seconds before checking again.
667283b8460SGeorge.Wilson 	 */
66869962b56SMatthew Ahrens 	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
669283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
670283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
671283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
672283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
673283b8460SGeorge.Wilson 
674e914ace2STim Schumacher 	zfs_refcount_create(&spa->spa_refcount);
675e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
676fa9e4066Sahrens 
677fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
678fa9e4066Sahrens 
6790373e76bSbonwick 	/*
6800373e76bSbonwick 	 * Set the alternate root, if there is one.
6810373e76bSbonwick 	 */
6820373e76bSbonwick 	if (altroot) {
6830373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
6840373e76bSbonwick 		spa_active_count++;
6850373e76bSbonwick 	}
6860373e76bSbonwick 
687f78cdc34SPaul Dagnelie 	spa->spa_alloc_count = spa_allocators;
688f78cdc34SPaul Dagnelie 	spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
689f78cdc34SPaul Dagnelie 	    sizeof (kmutex_t), KM_SLEEP);
690f78cdc34SPaul Dagnelie 	spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
691f78cdc34SPaul Dagnelie 	    sizeof (avl_tree_t), KM_SLEEP);
692f78cdc34SPaul Dagnelie 	for (int i = 0; i < spa->spa_alloc_count; i++) {
693f78cdc34SPaul Dagnelie 		mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
694f78cdc34SPaul Dagnelie 		avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
695f78cdc34SPaul Dagnelie 		    sizeof (zio_t), offsetof(zio_t, io_alloc_node));
696f78cdc34SPaul Dagnelie 	}
697814dcd43SSerapheim Dimitropoulos 	avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
698814dcd43SSerapheim Dimitropoulos 	    sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
699814dcd43SSerapheim Dimitropoulos 	avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
700814dcd43SSerapheim Dimitropoulos 	    sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
701814dcd43SSerapheim Dimitropoulos 	list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
702814dcd43SSerapheim Dimitropoulos 	    offsetof(log_summary_entry_t, lse_node));
7030f7643c7SGeorge Wilson 
704c5904d13Seschrock 	/*
705c5904d13Seschrock 	 * Every pool starts with the default cachefile
706c5904d13Seschrock 	 */
707c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
708c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
709c5904d13Seschrock 
710c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
711ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
712c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
713c5904d13Seschrock 
7144b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
7154b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
7164b964adaSGeorge Wilson 
717ad135b5dSChristopher Siden 	if (config != NULL) {
718ad135b5dSChristopher Siden 		nvlist_t *features;
719ad135b5dSChristopher Siden 
720ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
721ad135b5dSChristopher Siden 		    &features) == 0) {
722ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
723ad135b5dSChristopher Siden 			    0) == 0);
724ad135b5dSChristopher Siden 		}
725ad135b5dSChristopher Siden 
726468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
727ad135b5dSChristopher Siden 	}
728ad135b5dSChristopher Siden 
729ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
730ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
731ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
732ad135b5dSChristopher Siden 	}
733468c413aSTim Haley 
734c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
735c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
736c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
737c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
738c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
739c3a66015SMatthew Ahrens 	}
740c3a66015SMatthew Ahrens 
74181cd5c55SMatthew Ahrens 	spa->spa_min_ashift = INT_MAX;
74281cd5c55SMatthew Ahrens 	spa->spa_max_ashift = 0;
74381cd5c55SMatthew Ahrens 
74443466aaeSMax Grossman 	/*
74543466aaeSMax Grossman 	 * As a pool is being created, treat all features as disabled by
74643466aaeSMax Grossman 	 * setting SPA_FEATURE_DISABLED for all entries in the feature
74743466aaeSMax Grossman 	 * refcount cache.
74843466aaeSMax Grossman 	 */
74943466aaeSMax Grossman 	for (int i = 0; i < SPA_FEATURES; i++) {
75043466aaeSMax Grossman 		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
75143466aaeSMax Grossman 	}
75243466aaeSMax Grossman 
753e0f1c0afSOlaf Faaland 	list_create(&spa->spa_leaf_list, sizeof (vdev_t),
754e0f1c0afSOlaf Faaland 	    offsetof(vdev_t, vdev_leaf_node));
755e0f1c0afSOlaf Faaland 
756fa9e4066Sahrens 	return (spa);
757fa9e4066Sahrens }
758fa9e4066Sahrens 
759fa9e4066Sahrens /*
760fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
761fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
762fa9e4066Sahrens  * deactivated.
763fa9e4066Sahrens  */
764fa9e4066Sahrens void
765fa9e4066Sahrens spa_remove(spa_t *spa)
766fa9e4066Sahrens {
767c5904d13Seschrock 	spa_config_dirent_t *dp;
768c5904d13Seschrock 
769fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
770814dcd43SSerapheim Dimitropoulos 	ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
771e914ace2STim Schumacher 	ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
772fa9e4066Sahrens 
7731195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
7741195e687SMark J Musante 
775fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
776fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
777fa9e4066Sahrens 
7780373e76bSbonwick 	if (spa->spa_root) {
779fa9e4066Sahrens 		spa_strfree(spa->spa_root);
7800373e76bSbonwick 		spa_active_count--;
7810373e76bSbonwick 	}
782fa9e4066Sahrens 
783c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
784c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
785c5904d13Seschrock 		if (dp->scd_path != NULL)
786c5904d13Seschrock 			spa_strfree(dp->scd_path);
787c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
788c5904d13Seschrock 	}
789c5904d13Seschrock 
790f78cdc34SPaul Dagnelie 	for (int i = 0; i < spa->spa_alloc_count; i++) {
791f78cdc34SPaul Dagnelie 		avl_destroy(&spa->spa_alloc_trees[i]);
792f78cdc34SPaul Dagnelie 		mutex_destroy(&spa->spa_alloc_locks[i]);
793f78cdc34SPaul Dagnelie 	}
794f78cdc34SPaul Dagnelie 	kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count *
795f78cdc34SPaul Dagnelie 	    sizeof (kmutex_t));
796f78cdc34SPaul Dagnelie 	kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count *
797f78cdc34SPaul Dagnelie 	    sizeof (avl_tree_t));
798f78cdc34SPaul Dagnelie 
799814dcd43SSerapheim Dimitropoulos 	avl_destroy(&spa->spa_metaslabs_by_flushed);
800814dcd43SSerapheim Dimitropoulos 	avl_destroy(&spa->spa_sm_logs_by_txg);
801814dcd43SSerapheim Dimitropoulos 	list_destroy(&spa->spa_log_summary);
802c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
803e0f1c0afSOlaf Faaland 	list_destroy(&spa->spa_leaf_list);
8042f8aaab3Seschrock 
805ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
8064b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
807fa9e4066Sahrens 	spa_config_set(spa, NULL);
808fa9e4066Sahrens 
809283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
810283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
811283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
812283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
813283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
814283b8460SGeorge.Wilson 
815e914ace2STim Schumacher 	zfs_refcount_destroy(&spa->spa_refcount);
81691ebeef5Sahrens 
817e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
818fa9e4066Sahrens 
819c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
820c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
821c3a66015SMatthew Ahrens 
822b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
823cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
824b24ab676SJeff Bonwick 
82545818ee1SMatthew Ahrens 	zio_checksum_templates_free(spa);
82645818ee1SMatthew Ahrens 
827c25056deSgw 	cv_destroy(&spa->spa_async_cv);
828bc9014e6SJustin Gibbs 	cv_destroy(&spa->spa_evicting_os_cv);
82935a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
830c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
831e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
832c25056deSgw 
833814dcd43SSerapheim Dimitropoulos 	mutex_destroy(&spa->spa_flushed_ms_lock);
8345ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
835c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
83635a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
837bc9014e6SJustin Gibbs 	mutex_destroy(&spa->spa_evicting_os_lock);
83806eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
83935a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
840b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
84145818ee1SMatthew Ahrens 	mutex_destroy(&spa->spa_cksum_tmpls_lock);
84235a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
843e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
844a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
845c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
8465ad82045Snd 
847fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
848fa9e4066Sahrens }
849fa9e4066Sahrens 
850fa9e4066Sahrens /*
851fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
852fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
853fa9e4066Sahrens  */
854fa9e4066Sahrens spa_t *
855fa9e4066Sahrens spa_next(spa_t *prev)
856fa9e4066Sahrens {
857fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
858fa9e4066Sahrens 
859fa9e4066Sahrens 	if (prev)
860fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
861fa9e4066Sahrens 	else
862fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
863fa9e4066Sahrens }
864fa9e4066Sahrens 
865fa9e4066Sahrens /*
866fa9e4066Sahrens  * ==========================================================================
867fa9e4066Sahrens  * SPA refcount functions
868fa9e4066Sahrens  * ==========================================================================
869fa9e4066Sahrens  */
870fa9e4066Sahrens 
871fa9e4066Sahrens /*
872fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
873fa9e4066Sahrens  * have the namespace lock held.
874fa9e4066Sahrens  */
875fa9e4066Sahrens void
876fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
877fa9e4066Sahrens {
878e914ace2STim Schumacher 	ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
879fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
880e914ace2STim Schumacher 	(void) zfs_refcount_add(&spa->spa_refcount, tag);
881fa9e4066Sahrens }
882fa9e4066Sahrens 
883fa9e4066Sahrens /*
884fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
885fa9e4066Sahrens  * have the namespace lock held.
886fa9e4066Sahrens  */
887fa9e4066Sahrens void
888fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
889fa9e4066Sahrens {
890e914ace2STim Schumacher 	ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
891fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
892e914ace2STim Schumacher 	(void) zfs_refcount_remove(&spa->spa_refcount, tag);
893fa9e4066Sahrens }
894fa9e4066Sahrens 
895bc9014e6SJustin Gibbs /*
896bc9014e6SJustin Gibbs  * Remove a reference to the given spa_t held by a dsl dir that is
897bc9014e6SJustin Gibbs  * being asynchronously released.  Async releases occur from a taskq
898bc9014e6SJustin Gibbs  * performing eviction of dsl datasets and dirs.  The namespace lock
899bc9014e6SJustin Gibbs  * isn't held and the hold by the object being evicted may contribute to
900bc9014e6SJustin Gibbs  * spa_minref (e.g. dataset or directory released during pool export),
901bc9014e6SJustin Gibbs  * so the asserts in spa_close() do not apply.
902bc9014e6SJustin Gibbs  */
903bc9014e6SJustin Gibbs void
904bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag)
905bc9014e6SJustin Gibbs {
906e914ace2STim Schumacher 	(void) zfs_refcount_remove(&spa->spa_refcount, tag);
907bc9014e6SJustin Gibbs }
908bc9014e6SJustin Gibbs 
909fa9e4066Sahrens /*
910fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
911088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
912fa9e4066Sahrens  * number of references acquired when opening a pool
913fa9e4066Sahrens  */
914fa9e4066Sahrens boolean_t
915fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
916fa9e4066Sahrens {
917fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
918fa9e4066Sahrens 
919e914ace2STim Schumacher 	return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
920fa9e4066Sahrens }
921fa9e4066Sahrens 
92299653d4eSeschrock /*
92399653d4eSeschrock  * ==========================================================================
924fa94a07fSbrendan  * SPA spare and l2cache tracking
92599653d4eSeschrock  * ==========================================================================
92699653d4eSeschrock  */
92799653d4eSeschrock 
928fa94a07fSbrendan /*
929fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
930fa94a07fSbrendan  * for 'auxiliary' devices.
931fa94a07fSbrendan  */
932fa94a07fSbrendan 
933fa94a07fSbrendan typedef struct spa_aux {
934fa94a07fSbrendan 	uint64_t	aux_guid;
935fa94a07fSbrendan 	uint64_t	aux_pool;
936fa94a07fSbrendan 	avl_node_t	aux_avl;
937fa94a07fSbrendan 	int		aux_count;
938fa94a07fSbrendan } spa_aux_t;
939fa94a07fSbrendan 
940c4ab0d3fSGvozden Neskovic static inline int
941fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
942fa94a07fSbrendan {
943c4ab0d3fSGvozden Neskovic 	const spa_aux_t *sa = (const spa_aux_t *)a;
944c4ab0d3fSGvozden Neskovic 	const spa_aux_t *sb = (const spa_aux_t *)b;
945fa94a07fSbrendan 
946*4d7988d6SPaul Dagnelie 	return (TREE_CMP(sa->aux_guid, sb->aux_guid));
947fa94a07fSbrendan }
948fa94a07fSbrendan 
949fa94a07fSbrendan void
950fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
951fa94a07fSbrendan {
952fa94a07fSbrendan 	avl_index_t where;
953fa94a07fSbrendan 	spa_aux_t search;
954fa94a07fSbrendan 	spa_aux_t *aux;
955fa94a07fSbrendan 
956fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
957fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
958fa94a07fSbrendan 		aux->aux_count++;
959fa94a07fSbrendan 	} else {
960fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
961fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
962fa94a07fSbrendan 		aux->aux_count = 1;
963fa94a07fSbrendan 		avl_insert(avl, aux, where);
964fa94a07fSbrendan 	}
965fa94a07fSbrendan }
966fa94a07fSbrendan 
967fa94a07fSbrendan void
968fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
969fa94a07fSbrendan {
970fa94a07fSbrendan 	spa_aux_t search;
971fa94a07fSbrendan 	spa_aux_t *aux;
972fa94a07fSbrendan 	avl_index_t where;
973fa94a07fSbrendan 
974fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
975fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
976fa94a07fSbrendan 
977fa94a07fSbrendan 	ASSERT(aux != NULL);
978fa94a07fSbrendan 
979fa94a07fSbrendan 	if (--aux->aux_count == 0) {
980fa94a07fSbrendan 		avl_remove(avl, aux);
981fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
982fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
983fa94a07fSbrendan 		aux->aux_pool = 0ULL;
984fa94a07fSbrendan 	}
985fa94a07fSbrendan }
986fa94a07fSbrendan 
987fa94a07fSbrendan boolean_t
98889a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
989fa94a07fSbrendan {
990fa94a07fSbrendan 	spa_aux_t search, *found;
991fa94a07fSbrendan 
992fa94a07fSbrendan 	search.aux_guid = guid;
99389a89ebfSlling 	found = avl_find(avl, &search, NULL);
994fa94a07fSbrendan 
995fa94a07fSbrendan 	if (pool) {
996fa94a07fSbrendan 		if (found)
997fa94a07fSbrendan 			*pool = found->aux_pool;
998fa94a07fSbrendan 		else
999fa94a07fSbrendan 			*pool = 0ULL;
1000fa94a07fSbrendan 	}
1001fa94a07fSbrendan 
100289a89ebfSlling 	if (refcnt) {
100389a89ebfSlling 		if (found)
100489a89ebfSlling 			*refcnt = found->aux_count;
100589a89ebfSlling 		else
100689a89ebfSlling 			*refcnt = 0;
100789a89ebfSlling 	}
100889a89ebfSlling 
1009fa94a07fSbrendan 	return (found != NULL);
1010fa94a07fSbrendan }
1011fa94a07fSbrendan 
1012fa94a07fSbrendan void
1013fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1014fa94a07fSbrendan {
1015fa94a07fSbrendan 	spa_aux_t search, *found;
1016fa94a07fSbrendan 	avl_index_t where;
1017fa94a07fSbrendan 
1018fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
1019fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
1020fa94a07fSbrendan 	ASSERT(found != NULL);
1021fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
1022fa94a07fSbrendan 
1023fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
1024fa94a07fSbrendan }
1025fa94a07fSbrendan 
102699653d4eSeschrock /*
102739c23413Seschrock  * Spares are tracked globally due to the following constraints:
102839c23413Seschrock  *
102954811da5SToomas Soome  *	- A spare may be part of multiple pools.
103054811da5SToomas Soome  *	- A spare may be added to a pool even if it's actively in use within
103139c23413Seschrock  *	  another pool.
103254811da5SToomas Soome  *	- A spare in use in any pool can only be the source of a replacement if
103339c23413Seschrock  *	  the target is a spare in the same pool.
103439c23413Seschrock  *
103539c23413Seschrock  * We keep track of all spares on the system through the use of a reference
103639c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
103739c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
103839c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
103939c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
104039c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
104139c23413Seschrock  *
104239c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
104339c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
104439c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
104539c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
104699653d4eSeschrock  */
104739c23413Seschrock 
104899653d4eSeschrock static int
104999653d4eSeschrock spa_spare_compare(const void *a, const void *b)
105099653d4eSeschrock {
1051fa94a07fSbrendan 	return (spa_aux_compare(a, b));
105299653d4eSeschrock }
105399653d4eSeschrock 
105499653d4eSeschrock void
105539c23413Seschrock spa_spare_add(vdev_t *vd)
105699653d4eSeschrock {
105799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
105839c23413Seschrock 	ASSERT(!vd->vdev_isspare);
1059fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
106039c23413Seschrock 	vd->vdev_isspare = B_TRUE;
106199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
106299653d4eSeschrock }
106399653d4eSeschrock 
106499653d4eSeschrock void
106539c23413Seschrock spa_spare_remove(vdev_t *vd)
106699653d4eSeschrock {
106799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
106839c23413Seschrock 	ASSERT(vd->vdev_isspare);
1069fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
107039c23413Seschrock 	vd->vdev_isspare = B_FALSE;
107199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
107299653d4eSeschrock }
107399653d4eSeschrock 
107499653d4eSeschrock boolean_t
107589a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
107699653d4eSeschrock {
1077fa94a07fSbrendan 	boolean_t found;
107899653d4eSeschrock 
107999653d4eSeschrock 	mutex_enter(&spa_spare_lock);
108089a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
108199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
108299653d4eSeschrock 
1083fa94a07fSbrendan 	return (found);
108439c23413Seschrock }
108539c23413Seschrock 
108639c23413Seschrock void
108739c23413Seschrock spa_spare_activate(vdev_t *vd)
108839c23413Seschrock {
108939c23413Seschrock 	mutex_enter(&spa_spare_lock);
109039c23413Seschrock 	ASSERT(vd->vdev_isspare);
1091fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
1092fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
1093fa94a07fSbrendan }
109439c23413Seschrock 
1095fa94a07fSbrendan /*
1096fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
1097fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
1098fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
1099fa94a07fSbrendan  */
110039c23413Seschrock 
1101fa94a07fSbrendan static int
1102fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
1103fa94a07fSbrendan {
1104fa94a07fSbrendan 	return (spa_aux_compare(a, b));
1105fa94a07fSbrendan }
1106fa94a07fSbrendan 
1107fa94a07fSbrendan void
1108fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
1109fa94a07fSbrendan {
1110fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1111fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
1112fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
1113fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
1114fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1115fa94a07fSbrendan }
1116fa94a07fSbrendan 
1117fa94a07fSbrendan void
1118fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
1119fa94a07fSbrendan {
1120fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1121fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1122fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
1123fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
1124fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1125fa94a07fSbrendan }
1126fa94a07fSbrendan 
1127fa94a07fSbrendan boolean_t
1128fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1129fa94a07fSbrendan {
1130fa94a07fSbrendan 	boolean_t found;
1131fa94a07fSbrendan 
1132fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
113389a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1134fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1135fa94a07fSbrendan 
1136fa94a07fSbrendan 	return (found);
1137fa94a07fSbrendan }
1138fa94a07fSbrendan 
1139fa94a07fSbrendan void
1140fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
1141fa94a07fSbrendan {
1142fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1143fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1144fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
1145fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1146fa94a07fSbrendan }
1147fa94a07fSbrendan 
1148fa9e4066Sahrens /*
1149fa9e4066Sahrens  * ==========================================================================
1150fa9e4066Sahrens  * SPA vdev locking
1151fa9e4066Sahrens  * ==========================================================================
1152fa9e4066Sahrens  */
1153fa9e4066Sahrens 
1154fa9e4066Sahrens /*
1155ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
1156ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1157fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
1158fa9e4066Sahrens  */
1159fa9e4066Sahrens uint64_t
1160fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
1161fa9e4066Sahrens {
1162a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
1163bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
1164084fd14fSBrian Behlendorf 
1165084fd14fSBrian Behlendorf 	vdev_autotrim_stop_all(spa);
1166084fd14fSBrian Behlendorf 
116788ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
116888ecc943SGeorge Wilson }
116988ecc943SGeorge Wilson 
117088ecc943SGeorge Wilson /*
117188ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
117288ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
117388ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
117488ecc943SGeorge Wilson  */
117588ecc943SGeorge Wilson uint64_t
117688ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
117788ecc943SGeorge Wilson {
117888ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
11793d7072f8Seschrock 
1180e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1181fa9e4066Sahrens 
1182fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
1183fa9e4066Sahrens }
1184fa9e4066Sahrens 
1185fa9e4066Sahrens /*
118688ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
118788ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
1188fa9e4066Sahrens  */
118988ecc943SGeorge Wilson void
119088ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1191fa9e4066Sahrens {
119288ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
119388ecc943SGeorge Wilson 
11940e34b6a7Sbonwick 	int config_changed = B_FALSE;
1195ea8dc4b6Seschrock 
11960373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
11970e34b6a7Sbonwick 
1198e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
1199e14bb325SJeff Bonwick 
12000e34b6a7Sbonwick 	/*
12010e34b6a7Sbonwick 	 * Reassess the DTLs.
12020e34b6a7Sbonwick 	 */
12030373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
12040e34b6a7Sbonwick 
1205e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
12060e34b6a7Sbonwick 		config_changed = B_TRUE;
12078f18d1faSGeorge Wilson 		spa->spa_config_generation++;
12080e34b6a7Sbonwick 	}
1209ea8dc4b6Seschrock 
121088ecc943SGeorge Wilson 	/*
121188ecc943SGeorge Wilson 	 * Verify the metaslab classes.
121288ecc943SGeorge Wilson 	 */
1213b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1214b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1215663207adSDon Brady 	ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1216663207adSDon Brady 	ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
121788ecc943SGeorge Wilson 
1218e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
1219fa9e4066Sahrens 
122088ecc943SGeorge Wilson 	/*
122188ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
122288ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
122388ecc943SGeorge Wilson 	 * transactionally.
122488ecc943SGeorge Wilson 	 */
122588ecc943SGeorge Wilson 	if (zio_injection_enabled)
12261195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
122788ecc943SGeorge Wilson 
1228fa9e4066Sahrens 	/*
1229fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1230fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1231fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1232fa9e4066Sahrens 	 */
1233fa9e4066Sahrens 	if (error == 0)
1234fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1235fa9e4066Sahrens 
1236fa9e4066Sahrens 	if (vd != NULL) {
12370713e232SGeorge Wilson 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1238094e47e9SGeorge Wilson 		if (vd->vdev_ops->vdev_op_leaf) {
1239094e47e9SGeorge Wilson 			mutex_enter(&vd->vdev_initialize_lock);
1240084fd14fSBrian Behlendorf 			vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1241084fd14fSBrian Behlendorf 			    NULL);
1242094e47e9SGeorge Wilson 			mutex_exit(&vd->vdev_initialize_lock);
1243084fd14fSBrian Behlendorf 
1244084fd14fSBrian Behlendorf 			mutex_enter(&vd->vdev_trim_lock);
1245084fd14fSBrian Behlendorf 			vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1246084fd14fSBrian Behlendorf 			mutex_exit(&vd->vdev_trim_lock);
1247094e47e9SGeorge Wilson 		}
1248094e47e9SGeorge Wilson 
1249084fd14fSBrian Behlendorf 		/*
1250084fd14fSBrian Behlendorf 		 * The vdev may be both a leaf and top-level device.
1251084fd14fSBrian Behlendorf 		 */
1252084fd14fSBrian Behlendorf 		vdev_autotrim_stop_wait(vd);
1253084fd14fSBrian Behlendorf 
12548ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1255fa9e4066Sahrens 		vdev_free(vd);
12568ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1257fa9e4066Sahrens 	}
1258fa9e4066Sahrens 
1259fa9e4066Sahrens 	/*
12600e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1261fa9e4066Sahrens 	 */
12620e34b6a7Sbonwick 	if (config_changed)
12635cabbc6bSPrashanth Sreenivasa 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
126488ecc943SGeorge Wilson }
1265ea8dc4b6Seschrock 
126688ecc943SGeorge Wilson /*
126788ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
126888ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
126988ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
127088ecc943SGeorge Wilson  * information.
127188ecc943SGeorge Wilson  */
127288ecc943SGeorge Wilson int
127388ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
127488ecc943SGeorge Wilson {
1275084fd14fSBrian Behlendorf 	vdev_autotrim_restart(spa);
1276084fd14fSBrian Behlendorf 
127788ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1278ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1279bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1280fa9e4066Sahrens 
1281fa9e4066Sahrens 	return (error);
1282fa9e4066Sahrens }
1283fa9e4066Sahrens 
1284e14bb325SJeff Bonwick /*
1285e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1286e14bb325SJeff Bonwick  */
1287e14bb325SJeff Bonwick void
12888f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1289e14bb325SJeff Bonwick {
12908f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
12918f18d1faSGeorge Wilson 
1292dcba9f3fSGeorge Wilson 	/*
1293dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1294dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1295dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1296dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1297dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1298dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1299dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1300dcba9f3fSGeorge Wilson 	 */
1301dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
13029842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
13039842588bSGeorge Wilson 		int high = locks & ~low;
13049842588bSGeorge Wilson 
13059842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1306dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
13079842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1308dcba9f3fSGeorge Wilson 	} else {
1309dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1310dcba9f3fSGeorge Wilson 	}
13118f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1312e14bb325SJeff Bonwick }
1313e14bb325SJeff Bonwick 
1314e14bb325SJeff Bonwick int
1315e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1316e14bb325SJeff Bonwick {
1317c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1318c6065d0fSGeorge Wilson 
1319b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1320b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1321b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1322b24ab676SJeff Bonwick 
13238f18d1faSGeorge Wilson 	if (vd != NULL) {
1324e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1325c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
13268f18d1faSGeorge Wilson 		spa->spa_config_generation++;
13278f18d1faSGeorge Wilson 	}
1328e14bb325SJeff Bonwick 
1329dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1330dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1331dcba9f3fSGeorge Wilson 
13328f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
13338f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1334e14bb325SJeff Bonwick 
13358ad4d6ddSJeff Bonwick 	/*
13368ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
13378ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
13388ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
13398ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
13408ad4d6ddSJeff Bonwick 	 */
13418ad4d6ddSJeff Bonwick 	if (vd != NULL)
13428ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
13438ad4d6ddSJeff Bonwick 
1344c6065d0fSGeorge Wilson 	/*
1345c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1346c6065d0fSGeorge Wilson 	 */
1347c6065d0fSGeorge Wilson 	if (config_changed) {
1348c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
13495cabbc6bSPrashanth Sreenivasa 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
1350c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1351c6065d0fSGeorge Wilson 	}
1352c6065d0fSGeorge Wilson 
1353e14bb325SJeff Bonwick 	return (error);
1354e14bb325SJeff Bonwick }
1355e14bb325SJeff Bonwick 
1356fa9e4066Sahrens /*
1357fa9e4066Sahrens  * ==========================================================================
1358fa9e4066Sahrens  * Miscellaneous functions
1359fa9e4066Sahrens  * ==========================================================================
1360fa9e4066Sahrens  */
1361fa9e4066Sahrens 
1362ad135b5dSChristopher Siden void
136343466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1364ad135b5dSChristopher Siden {
13652acef22dSMatthew Ahrens 	if (!nvlist_exists(spa->spa_label_features, feature)) {
13662acef22dSMatthew Ahrens 		fnvlist_add_boolean(spa->spa_label_features, feature);
136743466aaeSMax Grossman 		/*
136843466aaeSMax Grossman 		 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
136943466aaeSMax Grossman 		 * dirty the vdev config because lock SCL_CONFIG is not held.
137043466aaeSMax Grossman 		 * Thankfully, in this case we don't need to dirty the config
137143466aaeSMax Grossman 		 * because it will be written out anyway when we finish
137243466aaeSMax Grossman 		 * creating the pool.
137343466aaeSMax Grossman 		 */
137443466aaeSMax Grossman 		if (tx->tx_txg != TXG_INITIAL)
137543466aaeSMax Grossman 			vdev_config_dirty(spa->spa_root_vdev);
13762acef22dSMatthew Ahrens 	}
1377ad135b5dSChristopher Siden }
1378ad135b5dSChristopher Siden 
1379ad135b5dSChristopher Siden void
1380ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1381ad135b5dSChristopher Siden {
13822acef22dSMatthew Ahrens 	if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
13832acef22dSMatthew Ahrens 		vdev_config_dirty(spa->spa_root_vdev);
1384ad135b5dSChristopher Siden }
1385ad135b5dSChristopher Siden 
1386fa9e4066Sahrens /*
1387f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1388f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1389f9af39baSGeorge Wilson  * a device with the specified device_guid.
1390fa9e4066Sahrens  */
1391f9af39baSGeorge Wilson spa_t *
1392f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1393fa9e4066Sahrens {
1394fa9e4066Sahrens 	spa_t *spa;
1395fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1396fa9e4066Sahrens 
1397ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1398fa9e4066Sahrens 
1399fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1400fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1401fa9e4066Sahrens 			continue;
1402fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1403fa9e4066Sahrens 			continue;
140439c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
140539c23413Seschrock 			if (device_guid == 0)
140639c23413Seschrock 				break;
140739c23413Seschrock 
140839c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
140939c23413Seschrock 			    device_guid) != NULL)
141039c23413Seschrock 				break;
141139c23413Seschrock 
141239c23413Seschrock 			/*
14138654d025Sperrin 			 * Check any devices we may be in the process of adding.
141439c23413Seschrock 			 */
141539c23413Seschrock 			if (spa->spa_pending_vdev) {
141639c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
141739c23413Seschrock 				    device_guid) != NULL)
141839c23413Seschrock 					break;
141939c23413Seschrock 			}
142039c23413Seschrock 		}
1421fa9e4066Sahrens 	}
1422fa9e4066Sahrens 
1423f9af39baSGeorge Wilson 	return (spa);
1424f9af39baSGeorge Wilson }
1425f9af39baSGeorge Wilson 
1426f9af39baSGeorge Wilson /*
1427f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1428f9af39baSGeorge Wilson  */
1429f9af39baSGeorge Wilson boolean_t
1430f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1431f9af39baSGeorge Wilson {
1432f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1433fa9e4066Sahrens }
1434fa9e4066Sahrens 
1435fa9e4066Sahrens char *
1436fa9e4066Sahrens spa_strdup(const char *s)
1437fa9e4066Sahrens {
1438fa9e4066Sahrens 	size_t len;
1439fa9e4066Sahrens 	char *new;
1440fa9e4066Sahrens 
1441fa9e4066Sahrens 	len = strlen(s);
1442fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1443fa9e4066Sahrens 	bcopy(s, new, len);
1444fa9e4066Sahrens 	new[len] = '\0';
1445fa9e4066Sahrens 
1446fa9e4066Sahrens 	return (new);
1447fa9e4066Sahrens }
1448fa9e4066Sahrens 
1449fa9e4066Sahrens void
1450fa9e4066Sahrens spa_strfree(char *s)
1451fa9e4066Sahrens {
1452fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1453fa9e4066Sahrens }
1454fa9e4066Sahrens 
1455fa9e4066Sahrens uint64_t
1456fa9e4066Sahrens spa_get_random(uint64_t range)
1457fa9e4066Sahrens {
1458fa9e4066Sahrens 	uint64_t r;
1459fa9e4066Sahrens 
1460fa9e4066Sahrens 	ASSERT(range != 0);
1461fa9e4066Sahrens 
1462e0f1c0afSOlaf Faaland 	if (range == 1)
1463e0f1c0afSOlaf Faaland 		return (0);
1464e0f1c0afSOlaf Faaland 
1465fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1466fa9e4066Sahrens 
1467fa9e4066Sahrens 	return (r % range);
1468fa9e4066Sahrens }
1469fa9e4066Sahrens 
14701195e687SMark J Musante uint64_t
14711195e687SMark J Musante spa_generate_guid(spa_t *spa)
14721195e687SMark J Musante {
14731195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
14741195e687SMark J Musante 
14751195e687SMark J Musante 	if (spa != NULL) {
14761195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
14771195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14781195e687SMark J Musante 	} else {
14791195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
14801195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14811195e687SMark J Musante 	}
14821195e687SMark J Musante 
14831195e687SMark J Musante 	return (guid);
14841195e687SMark J Musante }
14851195e687SMark J Musante 
1486fa9e4066Sahrens void
148743466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1488fa9e4066Sahrens {
1489ad135b5dSChristopher Siden 	char type[256];
1490f0ba89beSJeff Bonwick 	char *checksum = NULL;
1491f0ba89beSJeff Bonwick 	char *compress = NULL;
1492f0ba89beSJeff Bonwick 
1493f0ba89beSJeff Bonwick 	if (bp != NULL) {
1494ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1495ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1496ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1497ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1498ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1499ad135b5dSChristopher Siden 			    "metadata" : "data",
1500ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1501ad135b5dSChristopher Siden 		} else {
1502ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1503ad135b5dSChristopher Siden 			    sizeof (type));
1504ad135b5dSChristopher Siden 		}
15055d7b4d43SMatthew Ahrens 		if (!BP_IS_EMBEDDED(bp)) {
15065d7b4d43SMatthew Ahrens 			checksum =
15075d7b4d43SMatthew Ahrens 			    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
15085d7b4d43SMatthew Ahrens 		}
1509f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1510f0ba89beSJeff Bonwick 	}
1511fa9e4066Sahrens 
151243466aaeSMax Grossman 	SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
151343466aaeSMax Grossman 	    compress);
1514fa9e4066Sahrens }
1515fa9e4066Sahrens 
1516fa9e4066Sahrens void
1517fa9e4066Sahrens spa_freeze(spa_t *spa)
1518fa9e4066Sahrens {
1519fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1520fa9e4066Sahrens 
1521e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1522fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1523fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1524fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1525fa9e4066Sahrens 	}
1526e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1527fa9e4066Sahrens 	if (freeze_txg != 0)
1528fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1529fa9e4066Sahrens }
1530fa9e4066Sahrens 
15310125049cSahrens void
15320125049cSahrens zfs_panic_recover(const char *fmt, ...)
15330125049cSahrens {
15340125049cSahrens 	va_list adx;
15350125049cSahrens 
15360125049cSahrens 	va_start(adx, fmt);
15370125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
15380125049cSahrens 	va_end(adx);
15390125049cSahrens }
15400125049cSahrens 
15413f9d6ad7SLin Ling /*
15423f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
1543f7170741SWill Andrews  * lowercase hexadecimal numbers that don't overflow.
15443f9d6ad7SLin Ling  */
15453f9d6ad7SLin Ling uint64_t
15464585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr)
15473f9d6ad7SLin Ling {
15483f9d6ad7SLin Ling 	uint64_t val = 0;
15493f9d6ad7SLin Ling 	char c;
15503f9d6ad7SLin Ling 	int digit;
15513f9d6ad7SLin Ling 
15523f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
15533f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
15543f9d6ad7SLin Ling 			digit = c - '0';
15553f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
15563f9d6ad7SLin Ling 			digit = 10 + c - 'a';
15573f9d6ad7SLin Ling 		else
15583f9d6ad7SLin Ling 			break;
15593f9d6ad7SLin Ling 
15603f9d6ad7SLin Ling 		val *= 16;
15613f9d6ad7SLin Ling 		val += digit;
15623f9d6ad7SLin Ling 
15633f9d6ad7SLin Ling 		str++;
15643f9d6ad7SLin Ling 	}
15653f9d6ad7SLin Ling 
15663f9d6ad7SLin Ling 	if (nptr)
15673f9d6ad7SLin Ling 		*nptr = (char *)str;
15683f9d6ad7SLin Ling 
15693f9d6ad7SLin Ling 	return (val);
15703f9d6ad7SLin Ling }
15713f9d6ad7SLin Ling 
1572663207adSDon Brady void
1573663207adSDon Brady spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1574663207adSDon Brady {
1575663207adSDon Brady 	/*
1576663207adSDon Brady 	 * We bump the feature refcount for each special vdev added to the pool
1577663207adSDon Brady 	 */
1578663207adSDon Brady 	ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1579663207adSDon Brady 	spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1580663207adSDon Brady }
1581663207adSDon Brady 
1582fa9e4066Sahrens /*
1583fa9e4066Sahrens  * ==========================================================================
1584fa9e4066Sahrens  * Accessor functions
1585fa9e4066Sahrens  * ==========================================================================
1586fa9e4066Sahrens  */
1587fa9e4066Sahrens 
1588088f3894Sahrens boolean_t
158988b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1590fa9e4066Sahrens {
159188b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1592fa9e4066Sahrens }
1593fa9e4066Sahrens 
1594fa9e4066Sahrens dsl_pool_t *
1595fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1596fa9e4066Sahrens {
1597fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1598fa9e4066Sahrens }
1599fa9e4066Sahrens 
1600ad135b5dSChristopher Siden boolean_t
1601ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1602ad135b5dSChristopher Siden {
1603ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1604ad135b5dSChristopher Siden }
1605ad135b5dSChristopher Siden 
16065cabbc6bSPrashanth Sreenivasa boolean_t
16075cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa)
16085cabbc6bSPrashanth Sreenivasa {
16095cabbc6bSPrashanth Sreenivasa 	return (spa->spa_indirect_vdevs_loaded);
16105cabbc6bSPrashanth Sreenivasa }
16115cabbc6bSPrashanth Sreenivasa 
1612fa9e4066Sahrens blkptr_t *
1613fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1614fa9e4066Sahrens {
1615fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1616fa9e4066Sahrens }
1617fa9e4066Sahrens 
1618fa9e4066Sahrens void
1619fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1620fa9e4066Sahrens {
1621fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1622fa9e4066Sahrens }
1623fa9e4066Sahrens 
1624fa9e4066Sahrens void
1625fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1626fa9e4066Sahrens {
1627fa9e4066Sahrens 	if (spa->spa_root == NULL)
1628fa9e4066Sahrens 		buf[0] = '\0';
1629fa9e4066Sahrens 	else
1630fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1631fa9e4066Sahrens }
1632fa9e4066Sahrens 
1633fa9e4066Sahrens int
1634fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1635fa9e4066Sahrens {
1636fa9e4066Sahrens 	return (spa->spa_sync_pass);
1637fa9e4066Sahrens }
1638fa9e4066Sahrens 
1639fa9e4066Sahrens char *
1640fa9e4066Sahrens spa_name(spa_t *spa)
1641fa9e4066Sahrens {
1642fa9e4066Sahrens 	return (spa->spa_name);
1643fa9e4066Sahrens }
1644fa9e4066Sahrens 
1645fa9e4066Sahrens uint64_t
1646fa9e4066Sahrens spa_guid(spa_t *spa)
1647fa9e4066Sahrens {
1648dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1649dfbb9432SGeorge Wilson 	uint64_t guid;
1650dfbb9432SGeorge Wilson 
1651b5989ec7Seschrock 	/*
1652b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1653b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1654e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1655b5989ec7Seschrock 	 * this case.
1656b5989ec7Seschrock 	 */
1657dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1658dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1659dfbb9432SGeorge Wilson 
1660dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1661dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1662dfbb9432SGeorge Wilson 
1663dfbb9432SGeorge Wilson 	/*
1664dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1665dfbb9432SGeorge Wilson 	 * in syncing context.
1666dfbb9432SGeorge Wilson 	 */
1667dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1668b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1669b5989ec7Seschrock 	else
1670dfbb9432SGeorge Wilson 		return (guid);
1671e9103aaeSGarrett D'Amore }
1672e9103aaeSGarrett D'Amore 
1673e9103aaeSGarrett D'Amore uint64_t
1674e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1675e9103aaeSGarrett D'Amore {
1676e9103aaeSGarrett D'Amore 	/*
1677e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1678e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1679e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1680e9103aaeSGarrett D'Amore 	 */
1681e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1682fa9e4066Sahrens }
1683fa9e4066Sahrens 
1684fa9e4066Sahrens uint64_t
1685fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1686fa9e4066Sahrens {
1687fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1688fa9e4066Sahrens }
1689fa9e4066Sahrens 
1690fa9e4066Sahrens uint64_t
1691fa9e4066Sahrens spa_first_txg(spa_t *spa)
1692fa9e4066Sahrens {
1693fa9e4066Sahrens 	return (spa->spa_first_txg);
1694fa9e4066Sahrens }
1695fa9e4066Sahrens 
1696b24ab676SJeff Bonwick uint64_t
1697b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1698b24ab676SJeff Bonwick {
1699b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1700b24ab676SJeff Bonwick }
1701b24ab676SJeff Bonwick 
17023991b535SGeorge Wilson /*
17033991b535SGeorge Wilson  * Return the last txg where data can be dirtied. The final txgs
17043991b535SGeorge Wilson  * will be used to just clear out any deferred frees that remain.
17053991b535SGeorge Wilson  */
17063991b535SGeorge Wilson uint64_t
17073991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa)
17083991b535SGeorge Wilson {
17093991b535SGeorge Wilson 	return (spa->spa_final_txg - TXG_DEFER_SIZE);
17103991b535SGeorge Wilson }
17113991b535SGeorge Wilson 
171288b7b0f2SMatthew Ahrens pool_state_t
1713fa9e4066Sahrens spa_state(spa_t *spa)
1714fa9e4066Sahrens {
1715fa9e4066Sahrens 	return (spa->spa_state);
1716fa9e4066Sahrens }
1717fa9e4066Sahrens 
1718b16da2e2SGeorge Wilson spa_load_state_t
1719b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1720b16da2e2SGeorge Wilson {
1721b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1722b16da2e2SGeorge Wilson }
1723b16da2e2SGeorge Wilson 
1724fa9e4066Sahrens uint64_t
1725fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1726fa9e4066Sahrens {
1727fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1728fa9e4066Sahrens }
1729fa9e4066Sahrens 
1730fa9e4066Sahrens /* ARGSUSED */
1731fa9e4066Sahrens uint64_t
173261e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1733fa9e4066Sahrens {
173469962b56SMatthew Ahrens 	return (lsize * spa_asize_inflation);
173544cd46caSbillm }
173644cd46caSbillm 
17377d46dc6cSMatthew Ahrens /*
17387d46dc6cSMatthew Ahrens  * Return the amount of slop space in bytes.  It is 1/32 of the pool (3.2%),
17394b5c8e93SMatthew Ahrens  * or at least 128MB, unless that would cause it to be more than half the
17404b5c8e93SMatthew Ahrens  * pool size.
17417d46dc6cSMatthew Ahrens  *
17427d46dc6cSMatthew Ahrens  * See the comment above spa_slop_shift for details.
17437d46dc6cSMatthew Ahrens  */
17447d46dc6cSMatthew Ahrens uint64_t
17454b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa)
17464b5c8e93SMatthew Ahrens {
17477d46dc6cSMatthew Ahrens 	uint64_t space = spa_get_dspace(spa);
17484b5c8e93SMatthew Ahrens 	return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
17497d46dc6cSMatthew Ahrens }
17507d46dc6cSMatthew Ahrens 
1751485bbbf5SGeorge Wilson uint64_t
1752485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1753485bbbf5SGeorge Wilson {
1754485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1755485bbbf5SGeorge Wilson }
1756485bbbf5SGeorge Wilson 
175786714001SSerapheim Dimitropoulos uint64_t
175886714001SSerapheim Dimitropoulos spa_get_checkpoint_space(spa_t *spa)
175986714001SSerapheim Dimitropoulos {
176086714001SSerapheim Dimitropoulos 	return (spa->spa_checkpoint_info.sci_dspace);
176186714001SSerapheim Dimitropoulos }
176286714001SSerapheim Dimitropoulos 
1763485bbbf5SGeorge Wilson void
1764485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1765485bbbf5SGeorge Wilson {
1766485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1767485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
17685cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal != NULL) {
17695cabbc6bSPrashanth Sreenivasa 		/*
17705cabbc6bSPrashanth Sreenivasa 		 * We can't allocate from the removing device, so
17715cabbc6bSPrashanth Sreenivasa 		 * subtract its size.  This prevents the DMU/DSL from
17725cabbc6bSPrashanth Sreenivasa 		 * filling up the (now smaller) pool while we are in the
17735cabbc6bSPrashanth Sreenivasa 		 * middle of removing the device.
17745cabbc6bSPrashanth Sreenivasa 		 *
17755cabbc6bSPrashanth Sreenivasa 		 * Note that the DMU/DSL doesn't actually know or care
17765cabbc6bSPrashanth Sreenivasa 		 * how much space is allocated (it does its own tracking
17775cabbc6bSPrashanth Sreenivasa 		 * of how much space has been logically used).  So it
17785cabbc6bSPrashanth Sreenivasa 		 * doesn't matter that the data we are moving may be
17795cabbc6bSPrashanth Sreenivasa 		 * allocated twice (on the old device and the new
17805cabbc6bSPrashanth Sreenivasa 		 * device).
17815cabbc6bSPrashanth Sreenivasa 		 */
17823a4b1be9SMatthew Ahrens 		spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
17833a4b1be9SMatthew Ahrens 		vdev_t *vd =
17843a4b1be9SMatthew Ahrens 		    vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
17855cabbc6bSPrashanth Sreenivasa 		spa->spa_dspace -= spa_deflate(spa) ?
17865cabbc6bSPrashanth Sreenivasa 		    vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
17873a4b1be9SMatthew Ahrens 		spa_config_exit(spa, SCL_VDEV, FTAG);
17885cabbc6bSPrashanth Sreenivasa 	}
1789485bbbf5SGeorge Wilson }
1790485bbbf5SGeorge Wilson 
17910a4e9518Sgw /*
17920a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
17930a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
17940a4e9518Sgw  */
17950a4e9518Sgw uint8_t
17960a4e9518Sgw spa_get_failmode(spa_t *spa)
17970a4e9518Sgw {
17980a4e9518Sgw 	return (spa->spa_failmode);
17990a4e9518Sgw }
18000a4e9518Sgw 
1801e14bb325SJeff Bonwick boolean_t
1802e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1803e14bb325SJeff Bonwick {
1804e0f1c0afSOlaf Faaland 	return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1805e14bb325SJeff Bonwick }
1806e14bb325SJeff Bonwick 
180744cd46caSbillm uint64_t
180844cd46caSbillm spa_version(spa_t *spa)
180944cd46caSbillm {
181044cd46caSbillm 	return (spa->spa_ubsync.ub_version);
181144cd46caSbillm }
181244cd46caSbillm 
1813b24ab676SJeff Bonwick boolean_t
1814b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1815b24ab676SJeff Bonwick {
1816b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1817b24ab676SJeff Bonwick }
1818b24ab676SJeff Bonwick 
1819b24ab676SJeff Bonwick metaslab_class_t *
1820b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1821b24ab676SJeff Bonwick {
1822b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1823b24ab676SJeff Bonwick }
1824b24ab676SJeff Bonwick 
1825b24ab676SJeff Bonwick metaslab_class_t *
1826b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1827b24ab676SJeff Bonwick {
1828b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1829b24ab676SJeff Bonwick }
1830b24ab676SJeff Bonwick 
1831663207adSDon Brady metaslab_class_t *
1832663207adSDon Brady spa_special_class(spa_t *spa)
1833663207adSDon Brady {
1834663207adSDon Brady 	return (spa->spa_special_class);
1835663207adSDon Brady }
1836663207adSDon Brady 
1837663207adSDon Brady metaslab_class_t *
1838663207adSDon Brady spa_dedup_class(spa_t *spa)
1839663207adSDon Brady {
1840663207adSDon Brady 	return (spa->spa_dedup_class);
1841663207adSDon Brady }
1842663207adSDon Brady 
1843663207adSDon Brady /*
1844663207adSDon Brady  * Locate an appropriate allocation class
1845663207adSDon Brady  */
1846663207adSDon Brady metaslab_class_t *
1847663207adSDon Brady spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1848663207adSDon Brady     uint_t level, uint_t special_smallblk)
1849663207adSDon Brady {
1850663207adSDon Brady 	if (DMU_OT_IS_ZIL(objtype)) {
1851663207adSDon Brady 		if (spa->spa_log_class->mc_groups != 0)
1852663207adSDon Brady 			return (spa_log_class(spa));
1853663207adSDon Brady 		else
1854663207adSDon Brady 			return (spa_normal_class(spa));
1855663207adSDon Brady 	}
1856663207adSDon Brady 
1857663207adSDon Brady 	boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1858663207adSDon Brady 
1859663207adSDon Brady 	if (DMU_OT_IS_DDT(objtype)) {
1860663207adSDon Brady 		if (spa->spa_dedup_class->mc_groups != 0)
1861663207adSDon Brady 			return (spa_dedup_class(spa));
1862663207adSDon Brady 		else if (has_special_class && zfs_ddt_data_is_special)
1863663207adSDon Brady 			return (spa_special_class(spa));
1864663207adSDon Brady 		else
1865663207adSDon Brady 			return (spa_normal_class(spa));
1866663207adSDon Brady 	}
1867663207adSDon Brady 
1868663207adSDon Brady 	/* Indirect blocks for user data can land in special if allowed */
1869663207adSDon Brady 	if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1870663207adSDon Brady 		if (has_special_class && zfs_user_indirect_is_special)
1871663207adSDon Brady 			return (spa_special_class(spa));
1872663207adSDon Brady 		else
1873663207adSDon Brady 			return (spa_normal_class(spa));
1874663207adSDon Brady 	}
1875663207adSDon Brady 
1876663207adSDon Brady 	if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1877663207adSDon Brady 		if (has_special_class)
1878663207adSDon Brady 			return (spa_special_class(spa));
1879663207adSDon Brady 		else
1880663207adSDon Brady 			return (spa_normal_class(spa));
1881663207adSDon Brady 	}
1882663207adSDon Brady 
1883663207adSDon Brady 	/*
1884663207adSDon Brady 	 * Allow small file blocks in special class in some cases (like
1885663207adSDon Brady 	 * for the dRAID vdev feature). But always leave a reserve of
1886663207adSDon Brady 	 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1887663207adSDon Brady 	 */
1888663207adSDon Brady 	if (DMU_OT_IS_FILE(objtype) &&
1889663207adSDon Brady 	    has_special_class && size <= special_smallblk) {
1890663207adSDon Brady 		metaslab_class_t *special = spa_special_class(spa);
1891663207adSDon Brady 		uint64_t alloc = metaslab_class_get_alloc(special);
1892663207adSDon Brady 		uint64_t space = metaslab_class_get_space(special);
1893663207adSDon Brady 		uint64_t limit =
1894663207adSDon Brady 		    (space * (100 - zfs_special_class_metadata_reserve_pct))
1895663207adSDon Brady 		    / 100;
1896663207adSDon Brady 
1897663207adSDon Brady 		if (alloc < limit)
1898663207adSDon Brady 			return (special);
1899663207adSDon Brady 	}
1900663207adSDon Brady 
1901663207adSDon Brady 	return (spa_normal_class(spa));
1902663207adSDon Brady }
1903663207adSDon Brady 
1904bc9014e6SJustin Gibbs void
1905bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os)
1906bc9014e6SJustin Gibbs {
1907bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1908bc9014e6SJustin Gibbs 	list_insert_head(&spa->spa_evicting_os_list, os);
1909bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1910bc9014e6SJustin Gibbs }
1911bc9014e6SJustin Gibbs 
1912bc9014e6SJustin Gibbs void
1913bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1914bc9014e6SJustin Gibbs {
1915bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1916bc9014e6SJustin Gibbs 	list_remove(&spa->spa_evicting_os_list, os);
1917bc9014e6SJustin Gibbs 	cv_broadcast(&spa->spa_evicting_os_cv);
1918bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1919bc9014e6SJustin Gibbs }
1920bc9014e6SJustin Gibbs 
1921bc9014e6SJustin Gibbs void
1922bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa)
1923bc9014e6SJustin Gibbs {
1924bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1925bc9014e6SJustin Gibbs 	while (!list_is_empty(&spa->spa_evicting_os_list))
1926bc9014e6SJustin Gibbs 		cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1927bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1928bc9014e6SJustin Gibbs 
1929bc9014e6SJustin Gibbs 	dmu_buf_user_evict_wait();
1930bc9014e6SJustin Gibbs }
1931bc9014e6SJustin Gibbs 
193244cd46caSbillm int
193344cd46caSbillm spa_max_replication(spa_t *spa)
193444cd46caSbillm {
193544cd46caSbillm 	/*
1936e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
193744cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
193844cd46caSbillm 	 * replication level accordingly.
1939fa9e4066Sahrens 	 */
1940e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
194144cd46caSbillm 		return (1);
194244cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1943fa9e4066Sahrens }
1944fa9e4066Sahrens 
19453f9d6ad7SLin Ling int
19463f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
19473f9d6ad7SLin Ling {
19483f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
19493f9d6ad7SLin Ling }
19503f9d6ad7SLin Ling 
1951283b8460SGeorge.Wilson uint64_t
1952283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1953283b8460SGeorge.Wilson {
1954283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1955283b8460SGeorge.Wilson }
1956283b8460SGeorge.Wilson 
1957084fd14fSBrian Behlendorf spa_autotrim_t
1958084fd14fSBrian Behlendorf spa_get_autotrim(spa_t *spa)
1959084fd14fSBrian Behlendorf {
1960084fd14fSBrian Behlendorf 	return (spa->spa_autotrim);
1961084fd14fSBrian Behlendorf }
1962084fd14fSBrian Behlendorf 
196399653d4eSeschrock uint64_t
1964b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
196599653d4eSeschrock {
1966b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1967b24ab676SJeff Bonwick 	uint64_t dsize = asize;
196899653d4eSeschrock 
1969b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
197099653d4eSeschrock 
1971b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1972b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1973b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
197499653d4eSeschrock 	}
1975b24ab676SJeff Bonwick 
1976b24ab676SJeff Bonwick 	return (dsize);
1977b24ab676SJeff Bonwick }
1978b24ab676SJeff Bonwick 
1979b24ab676SJeff Bonwick uint64_t
1980b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1981b24ab676SJeff Bonwick {
1982b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1983b24ab676SJeff Bonwick 
19845d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1985b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1986b24ab676SJeff Bonwick 
1987b24ab676SJeff Bonwick 	return (dsize);
1988b24ab676SJeff Bonwick }
1989b24ab676SJeff Bonwick 
1990b24ab676SJeff Bonwick uint64_t
1991b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1992b24ab676SJeff Bonwick {
1993b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1994b24ab676SJeff Bonwick 
1995b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1996b24ab676SJeff Bonwick 
19975d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1998b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1999b24ab676SJeff Bonwick 
2000e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
2001b24ab676SJeff Bonwick 
2002b24ab676SJeff Bonwick 	return (dsize);
200399653d4eSeschrock }
200499653d4eSeschrock 
2005abe1fd01SDon Brady uint64_t
2006abe1fd01SDon Brady spa_dirty_data(spa_t *spa)
2007abe1fd01SDon Brady {
2008abe1fd01SDon Brady 	return (spa->spa_dsl_pool->dp_dirty_total);
2009abe1fd01SDon Brady }
2010abe1fd01SDon Brady 
2011fa9e4066Sahrens /*
2012fa9e4066Sahrens  * ==========================================================================
2013fa9e4066Sahrens  * Initialization and Termination
2014fa9e4066Sahrens  * ==========================================================================
2015fa9e4066Sahrens  */
2016fa9e4066Sahrens 
2017fa9e4066Sahrens static int
2018fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
2019fa9e4066Sahrens {
2020fa9e4066Sahrens 	const spa_t *s1 = a1;
2021fa9e4066Sahrens 	const spa_t *s2 = a2;
2022fa9e4066Sahrens 	int s;
2023fa9e4066Sahrens 
2024fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
2025c4ab0d3fSGvozden Neskovic 
2026*4d7988d6SPaul Dagnelie 	return (TREE_ISIGN(s));
2027fa9e4066Sahrens }
2028fa9e4066Sahrens 
20290373e76bSbonwick int
20300373e76bSbonwick spa_busy(void)
20310373e76bSbonwick {
20320373e76bSbonwick 	return (spa_active_count);
20330373e76bSbonwick }
20340373e76bSbonwick 
2035e7cbe64fSgw void
2036e7cbe64fSgw spa_boot_init()
2037e7cbe64fSgw {
2038e7cbe64fSgw 	spa_config_load();
2039e7cbe64fSgw }
2040e7cbe64fSgw 
2041fa9e4066Sahrens void
2042fa9e4066Sahrens spa_init(int mode)
2043fa9e4066Sahrens {
2044fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2045c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2046fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2047fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2048fa9e4066Sahrens 
2049fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2050fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
2051fa9e4066Sahrens 
2052fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2053fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
2054fa94a07fSbrendan 
2055fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2056fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
205799653d4eSeschrock 
20588ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
2059fa9e4066Sahrens 
2060283b8460SGeorge.Wilson #ifdef _KERNEL
2061283b8460SGeorge.Wilson 	spa_arch_init();
2062283b8460SGeorge.Wilson #else
2063cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
2064cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
2065cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
2066cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
2067cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
2068cd1c8b85SMatthew Ahrens 		} else {
2069cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
2070cd1c8b85SMatthew Ahrens 		}
2071cd1c8b85SMatthew Ahrens 	}
2072cd1c8b85SMatthew Ahrens #endif
2073cd1c8b85SMatthew Ahrens 
2074e914ace2STim Schumacher 	zfs_refcount_init();
2075fa9e4066Sahrens 	unique_init();
2076*4d7988d6SPaul Dagnelie 	zfs_btree_init();
2077*4d7988d6SPaul Dagnelie 	metaslab_stat_init();
2078fa9e4066Sahrens 	zio_init();
2079fa9e4066Sahrens 	dmu_init();
2080fa9e4066Sahrens 	zil_init();
208187db74c1Sek 	vdev_cache_stat_init();
208212a8814cSTom Caputi 	vdev_mirror_stat_init();
208391ebeef5Sahrens 	zfs_prop_init();
2084990b4856Slling 	zpool_prop_init();
2085ad135b5dSChristopher Siden 	zpool_feature_init();
2086fa9e4066Sahrens 	spa_config_load();
2087e14bb325SJeff Bonwick 	l2arc_start();
2088a3874b8bSToomas Soome 	scan_init();
2089fa9e4066Sahrens }
2090fa9e4066Sahrens 
2091fa9e4066Sahrens void
2092fa9e4066Sahrens spa_fini(void)
2093fa9e4066Sahrens {
2094e14bb325SJeff Bonwick 	l2arc_stop();
2095e14bb325SJeff Bonwick 
2096fa9e4066Sahrens 	spa_evict_all();
2097fa9e4066Sahrens 
209887db74c1Sek 	vdev_cache_stat_fini();
209912a8814cSTom Caputi 	vdev_mirror_stat_fini();
2100fa9e4066Sahrens 	zil_fini();
2101fa9e4066Sahrens 	dmu_fini();
2102fa9e4066Sahrens 	zio_fini();
2103*4d7988d6SPaul Dagnelie 	metaslab_stat_fini();
2104*4d7988d6SPaul Dagnelie 	zfs_btree_fini();
210591ebeef5Sahrens 	unique_fini();
2106e914ace2STim Schumacher 	zfs_refcount_fini();
2107a3874b8bSToomas Soome 	scan_fini();
2108fa9e4066Sahrens 
2109fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
211099653d4eSeschrock 	avl_destroy(&spa_spare_avl);
2111fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
2112fa9e4066Sahrens 
2113fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
2114fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
2115c25056deSgw 	mutex_destroy(&spa_spare_lock);
2116fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
2117fa9e4066Sahrens }
21186ce0521aSperrin 
21196ce0521aSperrin /*
21206ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
21216ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
21226ce0521aSperrin  * performance and not correctness
21236ce0521aSperrin  */
21246ce0521aSperrin boolean_t
21256ce0521aSperrin spa_has_slogs(spa_t *spa)
21266ce0521aSperrin {
21276ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
21286ce0521aSperrin }
2129bf82a41bSeschrock 
2130b24ab676SJeff Bonwick spa_log_state_t
2131b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
2132b24ab676SJeff Bonwick {
2133b24ab676SJeff Bonwick 	return (spa->spa_log_state);
2134b24ab676SJeff Bonwick }
2135b24ab676SJeff Bonwick 
2136b24ab676SJeff Bonwick void
2137b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
2138b24ab676SJeff Bonwick {
2139b24ab676SJeff Bonwick 	spa->spa_log_state = state;
2140b24ab676SJeff Bonwick }
2141b24ab676SJeff Bonwick 
2142bf82a41bSeschrock boolean_t
2143bf82a41bSeschrock spa_is_root(spa_t *spa)
2144bf82a41bSeschrock {
2145bf82a41bSeschrock 	return (spa->spa_is_root);
2146bf82a41bSeschrock }
21478ad4d6ddSJeff Bonwick 
21488ad4d6ddSJeff Bonwick boolean_t
21498ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
21508ad4d6ddSJeff Bonwick {
21516f793812SPavel Zakharov 	return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config);
21528ad4d6ddSJeff Bonwick }
21538ad4d6ddSJeff Bonwick 
215473527f44SAlex Reece /*
215573527f44SAlex Reece  * Returns true if there is a pending sync task in any of the current
215673527f44SAlex Reece  * syncing txg, the current quiescing txg, or the current open txg.
215773527f44SAlex Reece  */
215873527f44SAlex Reece boolean_t
215973527f44SAlex Reece spa_has_pending_synctask(spa_t *spa)
216073527f44SAlex Reece {
216186714001SSerapheim Dimitropoulos 	return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
216286714001SSerapheim Dimitropoulos 	    !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
216373527f44SAlex Reece }
216473527f44SAlex Reece 
21658ad4d6ddSJeff Bonwick int
21668ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
21678ad4d6ddSJeff Bonwick {
21688ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
21698ad4d6ddSJeff Bonwick }
2170b24ab676SJeff Bonwick 
2171b24ab676SJeff Bonwick uint64_t
2172b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
2173b24ab676SJeff Bonwick {
2174b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
2175b24ab676SJeff Bonwick }
2176b24ab676SJeff Bonwick 
2177b24ab676SJeff Bonwick uint64_t
2178b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
2179b24ab676SJeff Bonwick {
2180b24ab676SJeff Bonwick 	return (spa->spa_delegation);
2181b24ab676SJeff Bonwick }
2182b24ab676SJeff Bonwick 
2183b24ab676SJeff Bonwick objset_t *
2184b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
2185b24ab676SJeff Bonwick {
2186b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
2187b24ab676SJeff Bonwick }
2188b24ab676SJeff Bonwick 
2189b24ab676SJeff Bonwick enum zio_checksum
2190b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
2191b24ab676SJeff Bonwick {
2192b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
2193b24ab676SJeff Bonwick }
21943f9d6ad7SLin Ling 
21953f9d6ad7SLin Ling /*
21963f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
21973f9d6ad7SLin Ling  */
21983f9d6ad7SLin Ling void
21993f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
22003f9d6ad7SLin Ling {
22013f9d6ad7SLin Ling 	/* data not stored on disk */
22023f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
22031702cce7SAlek Pinchuk 	if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
22041702cce7SAlek Pinchuk 		spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
22051702cce7SAlek Pinchuk 	else
22061702cce7SAlek Pinchuk 		spa->spa_scan_pass_scrub_pause = 0;
22071702cce7SAlek Pinchuk 	spa->spa_scan_pass_scrub_spent_paused = 0;
22083f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
2209a3874b8bSToomas Soome 	spa->spa_scan_pass_issued = 0;
22103f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
22113f9d6ad7SLin Ling }
22123f9d6ad7SLin Ling 
22133f9d6ad7SLin Ling /*
22143f9d6ad7SLin Ling  * Get scan stats for zpool status reports
22153f9d6ad7SLin Ling  */
22163f9d6ad7SLin Ling int
22173f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
22183f9d6ad7SLin Ling {
22193f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
22203f9d6ad7SLin Ling 
22213f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2222be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
22233f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
22243f9d6ad7SLin Ling 
22253f9d6ad7SLin Ling 	/* data stored on disk */
22263f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
2227a3874b8bSToomas Soome 	ps->pss_state = scn->scn_phys.scn_state;
22283f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
22293f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
22303f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
22313f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
22323f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
22333f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
2234a3874b8bSToomas Soome 	ps->pss_examined = scn->scn_phys.scn_examined;
2235a3874b8bSToomas Soome 	ps->pss_issued =
2236a3874b8bSToomas Soome 	    scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
22373f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
22383f9d6ad7SLin Ling 
22393f9d6ad7SLin Ling 	/* data not stored on disk */
22403f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
22413f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
2242a3874b8bSToomas Soome 	ps->pss_pass_issued = spa->spa_scan_pass_issued;
22431702cce7SAlek Pinchuk 	ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
22441702cce7SAlek Pinchuk 	ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
22453f9d6ad7SLin Ling 
22463f9d6ad7SLin Ling 	return (0);
22473f9d6ad7SLin Ling }
224809c9d376SGeorge Wilson 
2249b5152584SMatthew Ahrens int
2250b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa)
2251b5152584SMatthew Ahrens {
2252b5152584SMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2253b5152584SMatthew Ahrens 		return (SPA_MAXBLOCKSIZE);
2254b5152584SMatthew Ahrens 	else
2255b5152584SMatthew Ahrens 		return (SPA_OLD_MAXBLOCKSIZE);
2256b5152584SMatthew Ahrens }
22575cabbc6bSPrashanth Sreenivasa 
225854811da5SToomas Soome int
225954811da5SToomas Soome spa_maxdnodesize(spa_t *spa)
226054811da5SToomas Soome {
226154811da5SToomas Soome 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
226254811da5SToomas Soome 		return (DNODE_MAX_SIZE);
226354811da5SToomas Soome 	else
226454811da5SToomas Soome 		return (DNODE_MIN_SIZE);
226554811da5SToomas Soome }
226654811da5SToomas Soome 
2267e0f1c0afSOlaf Faaland boolean_t
2268e0f1c0afSOlaf Faaland spa_multihost(spa_t *spa)
2269e0f1c0afSOlaf Faaland {
2270e0f1c0afSOlaf Faaland 	return (spa->spa_multihost ? B_TRUE : B_FALSE);
2271e0f1c0afSOlaf Faaland }
2272e0f1c0afSOlaf Faaland 
2273e0f1c0afSOlaf Faaland unsigned long
2274e0f1c0afSOlaf Faaland spa_get_hostid(void)
2275e0f1c0afSOlaf Faaland {
2276e0f1c0afSOlaf Faaland 	unsigned long myhostid;
2277e0f1c0afSOlaf Faaland 
2278e0f1c0afSOlaf Faaland #ifdef	_KERNEL
2279e0f1c0afSOlaf Faaland 	myhostid = zone_get_hostid(NULL);
2280e0f1c0afSOlaf Faaland #else	/* _KERNEL */
2281e0f1c0afSOlaf Faaland 	/*
2282e0f1c0afSOlaf Faaland 	 * We're emulating the system's hostid in userland, so
2283e0f1c0afSOlaf Faaland 	 * we can't use zone_get_hostid().
2284e0f1c0afSOlaf Faaland 	 */
2285e0f1c0afSOlaf Faaland 	(void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2286e0f1c0afSOlaf Faaland #endif	/* _KERNEL */
2287e0f1c0afSOlaf Faaland 
2288e0f1c0afSOlaf Faaland 	return (myhostid);
2289e0f1c0afSOlaf Faaland }
2290e0f1c0afSOlaf Faaland 
22915cabbc6bSPrashanth Sreenivasa /*
22925cabbc6bSPrashanth Sreenivasa  * Returns the txg that the last device removal completed. No indirect mappings
22935cabbc6bSPrashanth Sreenivasa  * have been added since this txg.
22945cabbc6bSPrashanth Sreenivasa  */
22955cabbc6bSPrashanth Sreenivasa uint64_t
22965cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa)
22975cabbc6bSPrashanth Sreenivasa {
22985cabbc6bSPrashanth Sreenivasa 	uint64_t vdevid;
22995cabbc6bSPrashanth Sreenivasa 	uint64_t ret = -1ULL;
23005cabbc6bSPrashanth Sreenivasa 
23015cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
23025cabbc6bSPrashanth Sreenivasa 	/*
23035cabbc6bSPrashanth Sreenivasa 	 * sr_prev_indirect_vdev is only modified while holding all the
23045cabbc6bSPrashanth Sreenivasa 	 * config locks, so it is sufficient to hold SCL_VDEV as reader when
23055cabbc6bSPrashanth Sreenivasa 	 * examining it.
23065cabbc6bSPrashanth Sreenivasa 	 */
23075cabbc6bSPrashanth Sreenivasa 	vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
23085cabbc6bSPrashanth Sreenivasa 
23095cabbc6bSPrashanth Sreenivasa 	while (vdevid != -1ULL) {
23105cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa, vdevid);
23115cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_t *vib = vd->vdev_indirect_births;
23125cabbc6bSPrashanth Sreenivasa 
23135cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
23145cabbc6bSPrashanth Sreenivasa 
23155cabbc6bSPrashanth Sreenivasa 		/*
23165cabbc6bSPrashanth Sreenivasa 		 * If the removal did not remap any data, we don't care.
23175cabbc6bSPrashanth Sreenivasa 		 */
23185cabbc6bSPrashanth Sreenivasa 		if (vdev_indirect_births_count(vib) != 0) {
23195cabbc6bSPrashanth Sreenivasa 			ret = vdev_indirect_births_last_entry_txg(vib);
23205cabbc6bSPrashanth Sreenivasa 			break;
23215cabbc6bSPrashanth Sreenivasa 		}
23225cabbc6bSPrashanth Sreenivasa 
23235cabbc6bSPrashanth Sreenivasa 		vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
23245cabbc6bSPrashanth Sreenivasa 	}
23255cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
23265cabbc6bSPrashanth Sreenivasa 
23275cabbc6bSPrashanth Sreenivasa 	IMPLY(ret != -1ULL,
23285cabbc6bSPrashanth Sreenivasa 	    spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
23295cabbc6bSPrashanth Sreenivasa 
23305cabbc6bSPrashanth Sreenivasa 	return (ret);
23315cabbc6bSPrashanth Sreenivasa }
23326f793812SPavel Zakharov 
23336f793812SPavel Zakharov boolean_t
23346f793812SPavel Zakharov spa_trust_config(spa_t *spa)
23356f793812SPavel Zakharov {
23366f793812SPavel Zakharov 	return (spa->spa_trust_config);
23376f793812SPavel Zakharov }
23386f793812SPavel Zakharov 
23396f793812SPavel Zakharov uint64_t
23406f793812SPavel Zakharov spa_missing_tvds_allowed(spa_t *spa)
23416f793812SPavel Zakharov {
23426f793812SPavel Zakharov 	return (spa->spa_missing_tvds_allowed);
23436f793812SPavel Zakharov }
23446f793812SPavel Zakharov 
2345814dcd43SSerapheim Dimitropoulos space_map_t *
2346814dcd43SSerapheim Dimitropoulos spa_syncing_log_sm(spa_t *spa)
2347814dcd43SSerapheim Dimitropoulos {
2348814dcd43SSerapheim Dimitropoulos 	return (spa->spa_syncing_log_sm);
2349814dcd43SSerapheim Dimitropoulos }
2350814dcd43SSerapheim Dimitropoulos 
23516f793812SPavel Zakharov void
23526f793812SPavel Zakharov spa_set_missing_tvds(spa_t *spa, uint64_t missing)
23536f793812SPavel Zakharov {
23546f793812SPavel Zakharov 	spa->spa_missing_tvds = missing;
23556f793812SPavel Zakharov }
235686714001SSerapheim Dimitropoulos 
235786714001SSerapheim Dimitropoulos boolean_t
235886714001SSerapheim Dimitropoulos spa_top_vdevs_spacemap_addressable(spa_t *spa)
235986714001SSerapheim Dimitropoulos {
236086714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
236186714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
236286714001SSerapheim Dimitropoulos 		if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
236386714001SSerapheim Dimitropoulos 			return (B_FALSE);
236486714001SSerapheim Dimitropoulos 	}
236586714001SSerapheim Dimitropoulos 	return (B_TRUE);
236686714001SSerapheim Dimitropoulos }
236786714001SSerapheim Dimitropoulos 
236886714001SSerapheim Dimitropoulos boolean_t
236986714001SSerapheim Dimitropoulos spa_has_checkpoint(spa_t *spa)
237086714001SSerapheim Dimitropoulos {
237186714001SSerapheim Dimitropoulos 	return (spa->spa_checkpoint_txg != 0);
237286714001SSerapheim Dimitropoulos }
237386714001SSerapheim Dimitropoulos 
237486714001SSerapheim Dimitropoulos boolean_t
237586714001SSerapheim Dimitropoulos spa_importing_readonly_checkpoint(spa_t *spa)
237686714001SSerapheim Dimitropoulos {
237786714001SSerapheim Dimitropoulos 	return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
237886714001SSerapheim Dimitropoulos 	    spa->spa_mode == FREAD);
237986714001SSerapheim Dimitropoulos }
238086714001SSerapheim Dimitropoulos 
238186714001SSerapheim Dimitropoulos uint64_t
238286714001SSerapheim Dimitropoulos spa_min_claim_txg(spa_t *spa)
238386714001SSerapheim Dimitropoulos {
238486714001SSerapheim Dimitropoulos 	uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
238586714001SSerapheim Dimitropoulos 
238686714001SSerapheim Dimitropoulos 	if (checkpoint_txg != 0)
238786714001SSerapheim Dimitropoulos 		return (checkpoint_txg + 1);
238886714001SSerapheim Dimitropoulos 
238986714001SSerapheim Dimitropoulos 	return (spa->spa_first_txg);
239086714001SSerapheim Dimitropoulos }
239186714001SSerapheim Dimitropoulos 
239286714001SSerapheim Dimitropoulos /*
239386714001SSerapheim Dimitropoulos  * If there is a checkpoint, async destroys may consume more space from
239486714001SSerapheim Dimitropoulos  * the pool instead of freeing it. In an attempt to save the pool from
239586714001SSerapheim Dimitropoulos  * getting suspended when it is about to run out of space, we stop
239686714001SSerapheim Dimitropoulos  * processing async destroys.
239786714001SSerapheim Dimitropoulos  */
239886714001SSerapheim Dimitropoulos boolean_t
239986714001SSerapheim Dimitropoulos spa_suspend_async_destroy(spa_t *spa)
240086714001SSerapheim Dimitropoulos {
240186714001SSerapheim Dimitropoulos 	dsl_pool_t *dp = spa_get_dsl(spa);
240286714001SSerapheim Dimitropoulos 
240386714001SSerapheim Dimitropoulos 	uint64_t unreserved = dsl_pool_unreserved_space(dp,
240486714001SSerapheim Dimitropoulos 	    ZFS_SPACE_CHECK_EXTRA_RESERVED);
240586714001SSerapheim Dimitropoulos 	uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
240686714001SSerapheim Dimitropoulos 	uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
240786714001SSerapheim Dimitropoulos 
240886714001SSerapheim Dimitropoulos 	if (spa_has_checkpoint(spa) && avail == 0)
240986714001SSerapheim Dimitropoulos 		return (B_TRUE);
241086714001SSerapheim Dimitropoulos 
241186714001SSerapheim Dimitropoulos 	return (B_FALSE);
241286714001SSerapheim Dimitropoulos }
2413