xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 3b2aab18808792cbd248a12f1edf139b89833c13)
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.
23ad135b5dSChristopher Siden  * Copyright (c) 2012 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/spa_impl.h>
29283b8460SGeorge.Wilson #include <sys/spa_boot.h>
30fa9e4066Sahrens #include <sys/zio.h>
31fa9e4066Sahrens #include <sys/zio_checksum.h>
32fa9e4066Sahrens #include <sys/zio_compress.h>
33fa9e4066Sahrens #include <sys/dmu.h>
34fa9e4066Sahrens #include <sys/dmu_tx.h>
35fa9e4066Sahrens #include <sys/zap.h>
36fa9e4066Sahrens #include <sys/zil.h>
37fa9e4066Sahrens #include <sys/vdev_impl.h>
38fa9e4066Sahrens #include <sys/metaslab.h>
39fa9e4066Sahrens #include <sys/uberblock_impl.h>
40fa9e4066Sahrens #include <sys/txg.h>
41fa9e4066Sahrens #include <sys/avl.h>
42fa9e4066Sahrens #include <sys/unique.h>
43fa9e4066Sahrens #include <sys/dsl_pool.h>
44fa9e4066Sahrens #include <sys/dsl_dir.h>
45fa9e4066Sahrens #include <sys/dsl_prop.h>
463f9d6ad7SLin Ling #include <sys/dsl_scan.h>
47fa9e4066Sahrens #include <sys/fs/zfs.h>
486ce0521aSperrin #include <sys/metaslab_impl.h>
49e14bb325SJeff Bonwick #include <sys/arc.h>
50485bbbf5SGeorge Wilson #include <sys/ddt.h>
5191ebeef5Sahrens #include "zfs_prop.h"
52ad135b5dSChristopher Siden #include "zfeature_common.h"
53fa9e4066Sahrens 
54fa9e4066Sahrens /*
55fa9e4066Sahrens  * SPA locking
56fa9e4066Sahrens  *
57fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
58fa9e4066Sahrens  *
59fa9e4066Sahrens  * spa_namespace_lock (global mutex)
60fa9e4066Sahrens  *
6144cd46caSbillm  *	This lock must be acquired to do any of the following:
62fa9e4066Sahrens  *
6344cd46caSbillm  *		- Lookup a spa_t by name
6444cd46caSbillm  *		- Add or remove a spa_t from the namespace
6544cd46caSbillm  *		- Increase spa_refcount from non-zero
6644cd46caSbillm  *		- Check if spa_refcount is zero
6744cd46caSbillm  *		- Rename a spa_t
68ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
6944cd46caSbillm  *		- Held for the duration of create/destroy/import/export
70fa9e4066Sahrens  *
7144cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
7244cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
7344cd46caSbillm  *	definition they must have an existing reference, and will never need
7444cd46caSbillm  *	to lookup a spa_t by name.
75fa9e4066Sahrens  *
76fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
77fa9e4066Sahrens  *
7844cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
7944cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8044cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
81088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
8244cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
83fa9e4066Sahrens  *
84e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
85fa9e4066Sahrens  *
8691ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
8791ebeef5Sahrens  *	the following circumstances:
88fa9e4066Sahrens  *
8944cd46caSbillm  *		- RW_READER to perform I/O to the spa
9044cd46caSbillm  *		- RW_WRITER to change the vdev config
91fa9e4066Sahrens  *
92fa9e4066Sahrens  * The locking order is fairly straightforward:
93fa9e4066Sahrens  *
9444cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
95fa9e4066Sahrens  *
9644cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
9744cd46caSbillm  *	or to check if it is zero.
98fa9e4066Sahrens  *
99e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
100fa9e4066Sahrens  *
10144cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10244cd46caSbillm  *	the config lock.
103fa9e4066Sahrens  *
104e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
105fa9e4066Sahrens  *
10644cd46caSbillm  *	The namespace lock must always be taken before the config lock.
107fa9e4066Sahrens  *
108fa9e4066Sahrens  *
109e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
110fa9e4066Sahrens  *
111e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
112e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
113fa9e4066Sahrens  *
11444cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
115fa9e4066Sahrens  *
11644cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
117fa9e4066Sahrens  *
11844cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
11944cd46caSbillm  *				frees up any memory associated with the spa_t.
120fa9e4066Sahrens  *
12144cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12244cd46caSbillm  *				first if NULL is passed.
123fa9e4066Sahrens  *
12444cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12544cd46caSbillm  *				the system.
126fa9e4066Sahrens  *
127ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
128fa9e4066Sahrens  *
129fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
130fa9e4066Sahrens  *
13144cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13244cd46caSbillm  *				called with spa_namespace_lock held if the
13344cd46caSbillm  *				refcount is currently zero.
134fa9e4066Sahrens  *
13544cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13644cd46caSbillm  *				not free the spa_t or remove it from the
13744cd46caSbillm  *				namespace.  No locking is required.
138fa9e4066Sahrens  *
13944cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14044cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14144cd46caSbillm  *				held.
142fa9e4066Sahrens  *
143e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
144e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
145e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
146e14bb325SJeff Bonwick  *
147e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
148e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
149e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
150e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
151e14bb325SJeff Bonwick  *
152e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
153e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
154e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
155e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
156e14bb325SJeff Bonwick  *
157e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
158e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
159e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
160e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
161e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
162e14bb325SJeff Bonwick  *
163e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
164e14bb325SJeff Bonwick  *
165e14bb325SJeff Bonwick  * SCL_CONFIG
166e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
167e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
168e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
169e14bb325SJeff Bonwick  *
170e14bb325SJeff Bonwick  * SCL_STATE
171e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
172e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
173e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
174e14bb325SJeff Bonwick  *
175e14bb325SJeff Bonwick  * SCL_ALLOC
176e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
177e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
178e14bb325SJeff Bonwick  *
179e14bb325SJeff Bonwick  * SCL_ZIO
180e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
181e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
182e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
183e14bb325SJeff Bonwick  *
184e14bb325SJeff Bonwick  * SCL_FREE
185e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
186e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
187e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
188e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
189e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
190e14bb325SJeff Bonwick  *
191e14bb325SJeff Bonwick  * SCL_VDEV
192e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
193b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
194e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
195e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
196e14bb325SJeff Bonwick  *
197e14bb325SJeff Bonwick  * In addition, the following rules apply:
198e14bb325SJeff Bonwick  *
199e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
200e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
201e14bb325SJeff Bonwick  *
202e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
203e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
204e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
205e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
206e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
207fa9e4066Sahrens  *
208ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
209fa9e4066Sahrens  *
21044cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
211ea8dc4b6Seschrock  *				for writing.
212fa9e4066Sahrens  *
21344cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21444cd46caSbillm  *				to complete, sync the updated configs to the
215ea8dc4b6Seschrock  *				cache, and release the namespace lock.
216fa9e4066Sahrens  *
217e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
218e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
219e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
220e14bb325SJeff Bonwick  *
221ad135b5dSChristopher Siden  * spa_rename() is also implemented within this file since it requires
222e14bb325SJeff Bonwick  * manipulation of the namespace.
223fa9e4066Sahrens  */
224fa9e4066Sahrens 
225fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
226fa9e4066Sahrens kmutex_t spa_namespace_lock;
227fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2280373e76bSbonwick static int spa_active_count;
229416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
230fa9e4066Sahrens 
23199653d4eSeschrock static kmutex_t spa_spare_lock;
23239c23413Seschrock static avl_tree_t spa_spare_avl;
233fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
234fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23599653d4eSeschrock 
236fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2378ad4d6ddSJeff Bonwick int spa_mode_global;
238fa9e4066Sahrens 
239fa9e4066Sahrens #ifdef ZFS_DEBUG
240*3b2aab18SMatthew Ahrens /* Everything except dprintf and spa is on by default in debug builds */
241*3b2aab18SMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
242fa9e4066Sahrens #else
243fa9e4066Sahrens int zfs_flags = 0;
244fa9e4066Sahrens #endif
245fa9e4066Sahrens 
2460125049cSahrens /*
2470125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2480125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2490125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2500125049cSahrens  */
2510125049cSahrens int zfs_recover = 0;
2520125049cSahrens 
253283b8460SGeorge.Wilson extern int zfs_txg_synctime_ms;
254283b8460SGeorge.Wilson 
255283b8460SGeorge.Wilson /*
256283b8460SGeorge.Wilson  * Expiration time in units of zfs_txg_synctime_ms. This value has two
257283b8460SGeorge.Wilson  * meanings. First it is used to determine when the spa_deadman logic
258283b8460SGeorge.Wilson  * should fire. By default the spa_deadman will fire if spa_sync has
259283b8460SGeorge.Wilson  * not completed in 1000 * zfs_txg_synctime_ms (i.e. 1000 seconds).
260283b8460SGeorge.Wilson  * Secondly, the value determines if an I/O is considered "hung".
261283b8460SGeorge.Wilson  * Any I/O that has not completed in zfs_deadman_synctime is considered
262283b8460SGeorge.Wilson  * "hung" resulting in a system panic.
263283b8460SGeorge.Wilson  */
264283b8460SGeorge.Wilson uint64_t zfs_deadman_synctime = 1000ULL;
265283b8460SGeorge.Wilson 
266283b8460SGeorge.Wilson /*
267283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
268283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
269283b8460SGeorge.Wilson  */
270283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
271283b8460SGeorge.Wilson 
272fa9e4066Sahrens 
273e05725b1Sbonwick /*
274e05725b1Sbonwick  * ==========================================================================
275e05725b1Sbonwick  * SPA config locking
276e05725b1Sbonwick  * ==========================================================================
277e05725b1Sbonwick  */
278e05725b1Sbonwick static void
279e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
280e14bb325SJeff Bonwick {
281e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
282e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
283e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
284e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
285*3b2aab18SMatthew Ahrens 		refcount_create_untracked(&scl->scl_count);
286e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
287e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
288e14bb325SJeff Bonwick 	}
289e05725b1Sbonwick }
290e05725b1Sbonwick 
291e05725b1Sbonwick static void
292e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
293e14bb325SJeff Bonwick {
294e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
295e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
296e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
297e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
298e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
299e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
300e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
301e14bb325SJeff Bonwick 	}
302e14bb325SJeff Bonwick }
303e14bb325SJeff Bonwick 
304e14bb325SJeff Bonwick int
305e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
306e05725b1Sbonwick {
307e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
308e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
309e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
310e14bb325SJeff Bonwick 			continue;
311e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
312e14bb325SJeff Bonwick 		if (rw == RW_READER) {
313e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
314e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
315e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
316e14bb325SJeff Bonwick 				return (0);
317e14bb325SJeff Bonwick 			}
318e14bb325SJeff Bonwick 		} else {
319e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
320e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
321e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
322e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
323e14bb325SJeff Bonwick 				return (0);
324e14bb325SJeff Bonwick 			}
325e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
326e14bb325SJeff Bonwick 		}
327e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
328e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
329e14bb325SJeff Bonwick 	}
330e14bb325SJeff Bonwick 	return (1);
331e05725b1Sbonwick }
332e05725b1Sbonwick 
333e05725b1Sbonwick void
334e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
335e05725b1Sbonwick {
336f64c0e34SEric Taylor 	int wlocks_held = 0;
337f64c0e34SEric Taylor 
338*3b2aab18SMatthew Ahrens 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
339*3b2aab18SMatthew Ahrens 
340e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
341e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
342f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
343f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
344e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
345e14bb325SJeff Bonwick 			continue;
346e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
347e14bb325SJeff Bonwick 		if (rw == RW_READER) {
348e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
349e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
350e14bb325SJeff Bonwick 			}
351e14bb325SJeff Bonwick 		} else {
352e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
353e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
354e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
355e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
356e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
357e14bb325SJeff Bonwick 			}
358e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
359e14bb325SJeff Bonwick 		}
360e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
361e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
362e05725b1Sbonwick 	}
363f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
364e05725b1Sbonwick }
365e05725b1Sbonwick 
366e05725b1Sbonwick void
367e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
368e05725b1Sbonwick {
369e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
370e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
371e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
372e14bb325SJeff Bonwick 			continue;
373e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
374e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
375e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
376e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
377e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
378e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
379e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
380e14bb325SJeff Bonwick 		}
381e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
382e05725b1Sbonwick 	}
383e05725b1Sbonwick }
384e05725b1Sbonwick 
385e14bb325SJeff Bonwick int
386e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
387e05725b1Sbonwick {
388e14bb325SJeff Bonwick 	int locks_held = 0;
389e05725b1Sbonwick 
390e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
391e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
392e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
393e14bb325SJeff Bonwick 			continue;
394e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
395e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
396e14bb325SJeff Bonwick 			locks_held |= 1 << i;
397e14bb325SJeff Bonwick 	}
398e14bb325SJeff Bonwick 
399e14bb325SJeff Bonwick 	return (locks_held);
400e05725b1Sbonwick }
401e05725b1Sbonwick 
402fa9e4066Sahrens /*
403fa9e4066Sahrens  * ==========================================================================
404fa9e4066Sahrens  * SPA namespace functions
405fa9e4066Sahrens  * ==========================================================================
406fa9e4066Sahrens  */
407fa9e4066Sahrens 
408fa9e4066Sahrens /*
409fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
410fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
411fa9e4066Sahrens  */
412fa9e4066Sahrens spa_t *
413fa9e4066Sahrens spa_lookup(const char *name)
414fa9e4066Sahrens {
415e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
416e14bb325SJeff Bonwick 	spa_t *spa;
417fa9e4066Sahrens 	avl_index_t where;
41840feaa91Sahrens 	char *cp;
419fa9e4066Sahrens 
420fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
421fa9e4066Sahrens 
422*3b2aab18SMatthew Ahrens 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
423*3b2aab18SMatthew Ahrens 
42440feaa91Sahrens 	/*
42540feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
42640feaa91Sahrens 	 * just use that.
42740feaa91Sahrens 	 */
428*3b2aab18SMatthew Ahrens 	cp = strpbrk(search.spa_name, "/@");
429*3b2aab18SMatthew Ahrens 	if (cp != NULL)
43040feaa91Sahrens 		*cp = '\0';
43140feaa91Sahrens 
432fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
433fa9e4066Sahrens 
434fa9e4066Sahrens 	return (spa);
435fa9e4066Sahrens }
436fa9e4066Sahrens 
437283b8460SGeorge.Wilson /*
438283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
439283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
440283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
441283b8460SGeorge.Wilson  */
442283b8460SGeorge.Wilson void
443283b8460SGeorge.Wilson spa_deadman(void *arg)
444283b8460SGeorge.Wilson {
445283b8460SGeorge.Wilson 	spa_t *spa = arg;
446283b8460SGeorge.Wilson 
447283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
448283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
449283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
450283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
451283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
452283b8460SGeorge.Wilson }
453283b8460SGeorge.Wilson 
454fa9e4066Sahrens /*
455fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
456fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
457fa9e4066Sahrens  * exist by calling spa_lookup() first.
458fa9e4066Sahrens  */
459fa9e4066Sahrens spa_t *
460468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
461fa9e4066Sahrens {
462fa9e4066Sahrens 	spa_t *spa;
463c5904d13Seschrock 	spa_config_dirent_t *dp;
464283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
465283b8460SGeorge.Wilson 	cyc_time_t when;
466fa9e4066Sahrens 
467fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
468fa9e4066Sahrens 
469fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
470fa9e4066Sahrens 
471c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
472c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
47335a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
474c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
47535a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
476c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
47735a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
478a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
479a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
480c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
481c25056deSgw 
482c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
48335a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
484c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
485e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
486c25056deSgw 
487b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
488cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
489b24ab676SJeff Bonwick 
490e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
491fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
492fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
4930373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
494468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
49535a5a358SJonathan Adams 	spa->spa_proc = &p0;
49635a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
497fa9e4066Sahrens 
498283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
499283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
500283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
501283b8460SGeorge.Wilson 
502283b8460SGeorge.Wilson 	spa->spa_deadman_synctime = zfs_deadman_synctime *
503283b8460SGeorge.Wilson 	    zfs_txg_synctime_ms * MICROSEC;
504283b8460SGeorge.Wilson 
505283b8460SGeorge.Wilson 	/*
506283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
507283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
508283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
509283b8460SGeorge.Wilson 	 * Instead wait for 5 synctimes before checking again.
510283b8460SGeorge.Wilson 	 */
511283b8460SGeorge.Wilson 	when.cyt_interval = 5ULL * zfs_txg_synctime_ms * MICROSEC;
512283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
513283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
514283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
515283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
516283b8460SGeorge.Wilson 
517fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
518e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
519fa9e4066Sahrens 
520fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
521fa9e4066Sahrens 
5220373e76bSbonwick 	/*
5230373e76bSbonwick 	 * Set the alternate root, if there is one.
5240373e76bSbonwick 	 */
5250373e76bSbonwick 	if (altroot) {
5260373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
5270373e76bSbonwick 		spa_active_count++;
5280373e76bSbonwick 	}
5290373e76bSbonwick 
530c5904d13Seschrock 	/*
531c5904d13Seschrock 	 * Every pool starts with the default cachefile
532c5904d13Seschrock 	 */
533c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
534c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
535c5904d13Seschrock 
536c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
537ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
538c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
539c5904d13Seschrock 
5404b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
5414b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
5424b964adaSGeorge Wilson 
543ad135b5dSChristopher Siden 	if (config != NULL) {
544ad135b5dSChristopher Siden 		nvlist_t *features;
545ad135b5dSChristopher Siden 
546ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
547ad135b5dSChristopher Siden 		    &features) == 0) {
548ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
549ad135b5dSChristopher Siden 			    0) == 0);
550ad135b5dSChristopher Siden 		}
551ad135b5dSChristopher Siden 
552468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
553ad135b5dSChristopher Siden 	}
554ad135b5dSChristopher Siden 
555ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
556ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
557ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
558ad135b5dSChristopher Siden 	}
559468c413aSTim Haley 
560c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
561c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
562c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
563c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
564c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
565c3a66015SMatthew Ahrens 	}
566c3a66015SMatthew Ahrens 
567*3b2aab18SMatthew Ahrens 	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
568*3b2aab18SMatthew Ahrens 
569fa9e4066Sahrens 	return (spa);
570fa9e4066Sahrens }
571fa9e4066Sahrens 
572fa9e4066Sahrens /*
573fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
574fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
575fa9e4066Sahrens  * deactivated.
576fa9e4066Sahrens  */
577fa9e4066Sahrens void
578fa9e4066Sahrens spa_remove(spa_t *spa)
579fa9e4066Sahrens {
580c5904d13Seschrock 	spa_config_dirent_t *dp;
581c5904d13Seschrock 
582fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
583fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
584fa9e4066Sahrens 
5851195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
5861195e687SMark J Musante 
587fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
588fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
589fa9e4066Sahrens 
5900373e76bSbonwick 	if (spa->spa_root) {
591fa9e4066Sahrens 		spa_strfree(spa->spa_root);
5920373e76bSbonwick 		spa_active_count--;
5930373e76bSbonwick 	}
594fa9e4066Sahrens 
595c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
596c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
597c5904d13Seschrock 		if (dp->scd_path != NULL)
598c5904d13Seschrock 			spa_strfree(dp->scd_path);
599c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
600c5904d13Seschrock 	}
601c5904d13Seschrock 
602c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
6032f8aaab3Seschrock 
604ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
6054b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
606fa9e4066Sahrens 	spa_config_set(spa, NULL);
607fa9e4066Sahrens 
608283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
609283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
610283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
611283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
612283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
613283b8460SGeorge.Wilson 
614fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
61591ebeef5Sahrens 
616e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
617fa9e4066Sahrens 
618c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
619c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
620c3a66015SMatthew Ahrens 
621b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
622cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
623b24ab676SJeff Bonwick 
624c25056deSgw 	cv_destroy(&spa->spa_async_cv);
62535a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
626c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
627e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
628c25056deSgw 
6295ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
630c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
63135a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
63206eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
63335a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
634b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
63535a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
636e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
637a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
638c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
6395ad82045Snd 
640fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
641fa9e4066Sahrens }
642fa9e4066Sahrens 
643fa9e4066Sahrens /*
644fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
645fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
646fa9e4066Sahrens  */
647fa9e4066Sahrens spa_t *
648fa9e4066Sahrens spa_next(spa_t *prev)
649fa9e4066Sahrens {
650fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
651fa9e4066Sahrens 
652fa9e4066Sahrens 	if (prev)
653fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
654fa9e4066Sahrens 	else
655fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
656fa9e4066Sahrens }
657fa9e4066Sahrens 
658fa9e4066Sahrens /*
659fa9e4066Sahrens  * ==========================================================================
660fa9e4066Sahrens  * SPA refcount functions
661fa9e4066Sahrens  * ==========================================================================
662fa9e4066Sahrens  */
663fa9e4066Sahrens 
664fa9e4066Sahrens /*
665fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
666fa9e4066Sahrens  * have the namespace lock held.
667fa9e4066Sahrens  */
668fa9e4066Sahrens void
669fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
670fa9e4066Sahrens {
671088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
672fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
673fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
674fa9e4066Sahrens }
675fa9e4066Sahrens 
676fa9e4066Sahrens /*
677fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
678fa9e4066Sahrens  * have the namespace lock held.
679fa9e4066Sahrens  */
680fa9e4066Sahrens void
681fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
682fa9e4066Sahrens {
683088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
684fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
685fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
686fa9e4066Sahrens }
687fa9e4066Sahrens 
688fa9e4066Sahrens /*
689fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
690088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
691fa9e4066Sahrens  * number of references acquired when opening a pool
692fa9e4066Sahrens  */
693fa9e4066Sahrens boolean_t
694fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
695fa9e4066Sahrens {
696fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
697fa9e4066Sahrens 
698088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
699fa9e4066Sahrens }
700fa9e4066Sahrens 
70199653d4eSeschrock /*
70299653d4eSeschrock  * ==========================================================================
703fa94a07fSbrendan  * SPA spare and l2cache tracking
70499653d4eSeschrock  * ==========================================================================
70599653d4eSeschrock  */
70699653d4eSeschrock 
707fa94a07fSbrendan /*
708fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
709fa94a07fSbrendan  * for 'auxiliary' devices.
710fa94a07fSbrendan  */
711fa94a07fSbrendan 
712fa94a07fSbrendan typedef struct spa_aux {
713fa94a07fSbrendan 	uint64_t	aux_guid;
714fa94a07fSbrendan 	uint64_t	aux_pool;
715fa94a07fSbrendan 	avl_node_t	aux_avl;
716fa94a07fSbrendan 	int		aux_count;
717fa94a07fSbrendan } spa_aux_t;
718fa94a07fSbrendan 
719fa94a07fSbrendan static int
720fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
721fa94a07fSbrendan {
722fa94a07fSbrendan 	const spa_aux_t *sa = a;
723fa94a07fSbrendan 	const spa_aux_t *sb = b;
724fa94a07fSbrendan 
725fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
726fa94a07fSbrendan 		return (-1);
727fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
728fa94a07fSbrendan 		return (1);
729fa94a07fSbrendan 	else
730fa94a07fSbrendan 		return (0);
731fa94a07fSbrendan }
732fa94a07fSbrendan 
733fa94a07fSbrendan void
734fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
735fa94a07fSbrendan {
736fa94a07fSbrendan 	avl_index_t where;
737fa94a07fSbrendan 	spa_aux_t search;
738fa94a07fSbrendan 	spa_aux_t *aux;
739fa94a07fSbrendan 
740fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
741fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
742fa94a07fSbrendan 		aux->aux_count++;
743fa94a07fSbrendan 	} else {
744fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
745fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
746fa94a07fSbrendan 		aux->aux_count = 1;
747fa94a07fSbrendan 		avl_insert(avl, aux, where);
748fa94a07fSbrendan 	}
749fa94a07fSbrendan }
750fa94a07fSbrendan 
751fa94a07fSbrendan void
752fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
753fa94a07fSbrendan {
754fa94a07fSbrendan 	spa_aux_t search;
755fa94a07fSbrendan 	spa_aux_t *aux;
756fa94a07fSbrendan 	avl_index_t where;
757fa94a07fSbrendan 
758fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
759fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
760fa94a07fSbrendan 
761fa94a07fSbrendan 	ASSERT(aux != NULL);
762fa94a07fSbrendan 
763fa94a07fSbrendan 	if (--aux->aux_count == 0) {
764fa94a07fSbrendan 		avl_remove(avl, aux);
765fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
766fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
767fa94a07fSbrendan 		aux->aux_pool = 0ULL;
768fa94a07fSbrendan 	}
769fa94a07fSbrendan }
770fa94a07fSbrendan 
771fa94a07fSbrendan boolean_t
77289a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
773fa94a07fSbrendan {
774fa94a07fSbrendan 	spa_aux_t search, *found;
775fa94a07fSbrendan 
776fa94a07fSbrendan 	search.aux_guid = guid;
77789a89ebfSlling 	found = avl_find(avl, &search, NULL);
778fa94a07fSbrendan 
779fa94a07fSbrendan 	if (pool) {
780fa94a07fSbrendan 		if (found)
781fa94a07fSbrendan 			*pool = found->aux_pool;
782fa94a07fSbrendan 		else
783fa94a07fSbrendan 			*pool = 0ULL;
784fa94a07fSbrendan 	}
785fa94a07fSbrendan 
78689a89ebfSlling 	if (refcnt) {
78789a89ebfSlling 		if (found)
78889a89ebfSlling 			*refcnt = found->aux_count;
78989a89ebfSlling 		else
79089a89ebfSlling 			*refcnt = 0;
79189a89ebfSlling 	}
79289a89ebfSlling 
793fa94a07fSbrendan 	return (found != NULL);
794fa94a07fSbrendan }
795fa94a07fSbrendan 
796fa94a07fSbrendan void
797fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
798fa94a07fSbrendan {
799fa94a07fSbrendan 	spa_aux_t search, *found;
800fa94a07fSbrendan 	avl_index_t where;
801fa94a07fSbrendan 
802fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
803fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
804fa94a07fSbrendan 	ASSERT(found != NULL);
805fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
806fa94a07fSbrendan 
807fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
808fa94a07fSbrendan }
809fa94a07fSbrendan 
81099653d4eSeschrock /*
81139c23413Seschrock  * Spares are tracked globally due to the following constraints:
81239c23413Seschrock  *
81339c23413Seschrock  * 	- A spare may be part of multiple pools.
81439c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
81539c23413Seschrock  *	  another pool.
81639c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
81739c23413Seschrock  *	  the target is a spare in the same pool.
81839c23413Seschrock  *
81939c23413Seschrock  * We keep track of all spares on the system through the use of a reference
82039c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
82139c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
82239c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
82339c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
82439c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
82539c23413Seschrock  *
82639c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
82739c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
82839c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
82939c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
83099653d4eSeschrock  */
83139c23413Seschrock 
83299653d4eSeschrock static int
83399653d4eSeschrock spa_spare_compare(const void *a, const void *b)
83499653d4eSeschrock {
835fa94a07fSbrendan 	return (spa_aux_compare(a, b));
83699653d4eSeschrock }
83799653d4eSeschrock 
83899653d4eSeschrock void
83939c23413Seschrock spa_spare_add(vdev_t *vd)
84099653d4eSeschrock {
84199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
84239c23413Seschrock 	ASSERT(!vd->vdev_isspare);
843fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
84439c23413Seschrock 	vd->vdev_isspare = B_TRUE;
84599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
84699653d4eSeschrock }
84799653d4eSeschrock 
84899653d4eSeschrock void
84939c23413Seschrock spa_spare_remove(vdev_t *vd)
85099653d4eSeschrock {
85199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
85239c23413Seschrock 	ASSERT(vd->vdev_isspare);
853fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
85439c23413Seschrock 	vd->vdev_isspare = B_FALSE;
85599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
85699653d4eSeschrock }
85799653d4eSeschrock 
85899653d4eSeschrock boolean_t
85989a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
86099653d4eSeschrock {
861fa94a07fSbrendan 	boolean_t found;
86299653d4eSeschrock 
86399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
86489a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
86599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
86699653d4eSeschrock 
867fa94a07fSbrendan 	return (found);
86839c23413Seschrock }
86939c23413Seschrock 
87039c23413Seschrock void
87139c23413Seschrock spa_spare_activate(vdev_t *vd)
87239c23413Seschrock {
87339c23413Seschrock 	mutex_enter(&spa_spare_lock);
87439c23413Seschrock 	ASSERT(vd->vdev_isspare);
875fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
876fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
877fa94a07fSbrendan }
87839c23413Seschrock 
879fa94a07fSbrendan /*
880fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
881fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
882fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
883fa94a07fSbrendan  */
88439c23413Seschrock 
885fa94a07fSbrendan static int
886fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
887fa94a07fSbrendan {
888fa94a07fSbrendan 	return (spa_aux_compare(a, b));
889fa94a07fSbrendan }
890fa94a07fSbrendan 
891fa94a07fSbrendan void
892fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
893fa94a07fSbrendan {
894fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
895fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
896fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
897fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
898fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
899fa94a07fSbrendan }
900fa94a07fSbrendan 
901fa94a07fSbrendan void
902fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
903fa94a07fSbrendan {
904fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
905fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
906fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
907fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
908fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
909fa94a07fSbrendan }
910fa94a07fSbrendan 
911fa94a07fSbrendan boolean_t
912fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
913fa94a07fSbrendan {
914fa94a07fSbrendan 	boolean_t found;
915fa94a07fSbrendan 
916fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
91789a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
918fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
919fa94a07fSbrendan 
920fa94a07fSbrendan 	return (found);
921fa94a07fSbrendan }
922fa94a07fSbrendan 
923fa94a07fSbrendan void
924fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
925fa94a07fSbrendan {
926fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
927fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
928fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
929fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
930fa94a07fSbrendan }
931fa94a07fSbrendan 
932fa9e4066Sahrens /*
933fa9e4066Sahrens  * ==========================================================================
934fa9e4066Sahrens  * SPA vdev locking
935fa9e4066Sahrens  * ==========================================================================
936fa9e4066Sahrens  */
937fa9e4066Sahrens 
938fa9e4066Sahrens /*
939ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
940ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
941fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
942fa9e4066Sahrens  */
943fa9e4066Sahrens uint64_t
944fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
945fa9e4066Sahrens {
946a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
947bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
94888ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
94988ecc943SGeorge Wilson }
95088ecc943SGeorge Wilson 
95188ecc943SGeorge Wilson /*
95288ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
95388ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
95488ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
95588ecc943SGeorge Wilson  */
95688ecc943SGeorge Wilson uint64_t
95788ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
95888ecc943SGeorge Wilson {
95988ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
9603d7072f8Seschrock 
961e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
962fa9e4066Sahrens 
963fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
964fa9e4066Sahrens }
965fa9e4066Sahrens 
966fa9e4066Sahrens /*
96788ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
96888ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
969fa9e4066Sahrens  */
97088ecc943SGeorge Wilson void
97188ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
972fa9e4066Sahrens {
97388ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
97488ecc943SGeorge Wilson 
9750e34b6a7Sbonwick 	int config_changed = B_FALSE;
976ea8dc4b6Seschrock 
9770373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
9780e34b6a7Sbonwick 
979e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
980e14bb325SJeff Bonwick 
9810e34b6a7Sbonwick 	/*
9820e34b6a7Sbonwick 	 * Reassess the DTLs.
9830e34b6a7Sbonwick 	 */
9840373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
9850e34b6a7Sbonwick 
986e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
9870e34b6a7Sbonwick 		config_changed = B_TRUE;
9888f18d1faSGeorge Wilson 		spa->spa_config_generation++;
9890e34b6a7Sbonwick 	}
990ea8dc4b6Seschrock 
99188ecc943SGeorge Wilson 	/*
99288ecc943SGeorge Wilson 	 * Verify the metaslab classes.
99388ecc943SGeorge Wilson 	 */
994b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
995b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
99688ecc943SGeorge Wilson 
997e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
998fa9e4066Sahrens 
99988ecc943SGeorge Wilson 	/*
100088ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
100188ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
100288ecc943SGeorge Wilson 	 * transactionally.
100388ecc943SGeorge Wilson 	 */
100488ecc943SGeorge Wilson 	if (zio_injection_enabled)
10051195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
100688ecc943SGeorge Wilson 
1007fa9e4066Sahrens 	/*
1008fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1009fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1010fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1011fa9e4066Sahrens 	 */
1012fa9e4066Sahrens 	if (error == 0)
1013fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1014fa9e4066Sahrens 
1015fa9e4066Sahrens 	if (vd != NULL) {
10168ad4d6ddSJeff Bonwick 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
10178ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1018fa9e4066Sahrens 		vdev_free(vd);
10198ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1020fa9e4066Sahrens 	}
1021fa9e4066Sahrens 
1022fa9e4066Sahrens 	/*
10230e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1024fa9e4066Sahrens 	 */
10250e34b6a7Sbonwick 	if (config_changed)
1026c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
102788ecc943SGeorge Wilson }
1028ea8dc4b6Seschrock 
102988ecc943SGeorge Wilson /*
103088ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
103188ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
103288ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
103388ecc943SGeorge Wilson  * information.
103488ecc943SGeorge Wilson  */
103588ecc943SGeorge Wilson int
103688ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
103788ecc943SGeorge Wilson {
103888ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1039ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1040bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1041fa9e4066Sahrens 
1042fa9e4066Sahrens 	return (error);
1043fa9e4066Sahrens }
1044fa9e4066Sahrens 
1045e14bb325SJeff Bonwick /*
1046e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1047e14bb325SJeff Bonwick  */
1048e14bb325SJeff Bonwick void
10498f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1050e14bb325SJeff Bonwick {
10518f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
10528f18d1faSGeorge Wilson 
1053dcba9f3fSGeorge Wilson 	/*
1054dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1055dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1056dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1057dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1058dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1059dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1060dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1061dcba9f3fSGeorge Wilson 	 */
1062dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
10639842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
10649842588bSGeorge Wilson 		int high = locks & ~low;
10659842588bSGeorge Wilson 
10669842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1067dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
10689842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1069dcba9f3fSGeorge Wilson 	} else {
1070dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1071dcba9f3fSGeorge Wilson 	}
10728f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1073e14bb325SJeff Bonwick }
1074e14bb325SJeff Bonwick 
1075e14bb325SJeff Bonwick int
1076e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1077e14bb325SJeff Bonwick {
1078c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1079c6065d0fSGeorge Wilson 
1080b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1081b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1082b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1083b24ab676SJeff Bonwick 
10848f18d1faSGeorge Wilson 	if (vd != NULL) {
1085e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1086c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
10878f18d1faSGeorge Wilson 		spa->spa_config_generation++;
10888f18d1faSGeorge Wilson 	}
1089e14bb325SJeff Bonwick 
1090dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1091dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1092dcba9f3fSGeorge Wilson 
10938f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
10948f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1095e14bb325SJeff Bonwick 
10968ad4d6ddSJeff Bonwick 	/*
10978ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
10988ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
10998ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
11008ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
11018ad4d6ddSJeff Bonwick 	 */
11028ad4d6ddSJeff Bonwick 	if (vd != NULL)
11038ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
11048ad4d6ddSJeff Bonwick 
1105c6065d0fSGeorge Wilson 	/*
1106c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1107c6065d0fSGeorge Wilson 	 */
1108c6065d0fSGeorge Wilson 	if (config_changed) {
1109c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1110c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1111c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1112c6065d0fSGeorge Wilson 	}
1113c6065d0fSGeorge Wilson 
1114e14bb325SJeff Bonwick 	return (error);
1115e14bb325SJeff Bonwick }
1116e14bb325SJeff Bonwick 
1117fa9e4066Sahrens /*
1118fa9e4066Sahrens  * ==========================================================================
1119fa9e4066Sahrens  * Miscellaneous functions
1120fa9e4066Sahrens  * ==========================================================================
1121fa9e4066Sahrens  */
1122fa9e4066Sahrens 
1123ad135b5dSChristopher Siden void
1124ad135b5dSChristopher Siden spa_activate_mos_feature(spa_t *spa, const char *feature)
1125ad135b5dSChristopher Siden {
1126ad135b5dSChristopher Siden 	(void) nvlist_add_boolean(spa->spa_label_features, feature);
1127ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1128ad135b5dSChristopher Siden }
1129ad135b5dSChristopher Siden 
1130ad135b5dSChristopher Siden void
1131ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1132ad135b5dSChristopher Siden {
1133ad135b5dSChristopher Siden 	(void) nvlist_remove_all(spa->spa_label_features, feature);
1134ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1135ad135b5dSChristopher Siden }
1136ad135b5dSChristopher Siden 
1137fa9e4066Sahrens /*
1138fa9e4066Sahrens  * Rename a spa_t.
1139fa9e4066Sahrens  */
1140fa9e4066Sahrens int
1141fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1142fa9e4066Sahrens {
1143fa9e4066Sahrens 	spa_t *spa;
1144fa9e4066Sahrens 	int err;
1145fa9e4066Sahrens 
1146fa9e4066Sahrens 	/*
1147fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1148fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1149fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1150ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1151fa9e4066Sahrens 	 */
1152fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1153fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1154fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1155fa9e4066Sahrens 		return (err);
1156fa9e4066Sahrens 	}
1157fa9e4066Sahrens 
1158e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1159fa9e4066Sahrens 
1160fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1161e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1162fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1163fa9e4066Sahrens 
1164fa9e4066Sahrens 	/*
1165fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1166fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1167fa9e4066Sahrens 	 * during the sync.
1168fa9e4066Sahrens 	 */
1169fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1170fa9e4066Sahrens 
1171e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1172fa9e4066Sahrens 
11730373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1174fa9e4066Sahrens 
1175fa9e4066Sahrens 	/*
1176fa9e4066Sahrens 	 * Sync the updated config cache.
1177fa9e4066Sahrens 	 */
1178c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1179fa9e4066Sahrens 
1180fa9e4066Sahrens 	spa_close(spa, FTAG);
1181fa9e4066Sahrens 
1182fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1183fa9e4066Sahrens 
1184fa9e4066Sahrens 	return (0);
1185fa9e4066Sahrens }
1186fa9e4066Sahrens 
1187fa9e4066Sahrens /*
1188f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1189f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1190f9af39baSGeorge Wilson  * a device with the specified device_guid.
1191fa9e4066Sahrens  */
1192f9af39baSGeorge Wilson spa_t *
1193f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1194fa9e4066Sahrens {
1195fa9e4066Sahrens 	spa_t *spa;
1196fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1197fa9e4066Sahrens 
1198ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1199fa9e4066Sahrens 
1200fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1201fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1202fa9e4066Sahrens 			continue;
1203fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1204fa9e4066Sahrens 			continue;
120539c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
120639c23413Seschrock 			if (device_guid == 0)
120739c23413Seschrock 				break;
120839c23413Seschrock 
120939c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
121039c23413Seschrock 			    device_guid) != NULL)
121139c23413Seschrock 				break;
121239c23413Seschrock 
121339c23413Seschrock 			/*
12148654d025Sperrin 			 * Check any devices we may be in the process of adding.
121539c23413Seschrock 			 */
121639c23413Seschrock 			if (spa->spa_pending_vdev) {
121739c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
121839c23413Seschrock 				    device_guid) != NULL)
121939c23413Seschrock 					break;
122039c23413Seschrock 			}
122139c23413Seschrock 		}
1222fa9e4066Sahrens 	}
1223fa9e4066Sahrens 
1224f9af39baSGeorge Wilson 	return (spa);
1225f9af39baSGeorge Wilson }
1226f9af39baSGeorge Wilson 
1227f9af39baSGeorge Wilson /*
1228f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1229f9af39baSGeorge Wilson  */
1230f9af39baSGeorge Wilson boolean_t
1231f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1232f9af39baSGeorge Wilson {
1233f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1234fa9e4066Sahrens }
1235fa9e4066Sahrens 
1236fa9e4066Sahrens char *
1237fa9e4066Sahrens spa_strdup(const char *s)
1238fa9e4066Sahrens {
1239fa9e4066Sahrens 	size_t len;
1240fa9e4066Sahrens 	char *new;
1241fa9e4066Sahrens 
1242fa9e4066Sahrens 	len = strlen(s);
1243fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1244fa9e4066Sahrens 	bcopy(s, new, len);
1245fa9e4066Sahrens 	new[len] = '\0';
1246fa9e4066Sahrens 
1247fa9e4066Sahrens 	return (new);
1248fa9e4066Sahrens }
1249fa9e4066Sahrens 
1250fa9e4066Sahrens void
1251fa9e4066Sahrens spa_strfree(char *s)
1252fa9e4066Sahrens {
1253fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1254fa9e4066Sahrens }
1255fa9e4066Sahrens 
1256fa9e4066Sahrens uint64_t
1257fa9e4066Sahrens spa_get_random(uint64_t range)
1258fa9e4066Sahrens {
1259fa9e4066Sahrens 	uint64_t r;
1260fa9e4066Sahrens 
1261fa9e4066Sahrens 	ASSERT(range != 0);
1262fa9e4066Sahrens 
1263fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1264fa9e4066Sahrens 
1265fa9e4066Sahrens 	return (r % range);
1266fa9e4066Sahrens }
1267fa9e4066Sahrens 
12681195e687SMark J Musante uint64_t
12691195e687SMark J Musante spa_generate_guid(spa_t *spa)
12701195e687SMark J Musante {
12711195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
12721195e687SMark J Musante 
12731195e687SMark J Musante 	if (spa != NULL) {
12741195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
12751195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12761195e687SMark J Musante 	} else {
12771195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
12781195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12791195e687SMark J Musante 	}
12801195e687SMark J Musante 
12811195e687SMark J Musante 	return (guid);
12821195e687SMark J Musante }
12831195e687SMark J Musante 
1284fa9e4066Sahrens void
1285b24ab676SJeff Bonwick sprintf_blkptr(char *buf, const blkptr_t *bp)
1286fa9e4066Sahrens {
1287ad135b5dSChristopher Siden 	char type[256];
1288f0ba89beSJeff Bonwick 	char *checksum = NULL;
1289f0ba89beSJeff Bonwick 	char *compress = NULL;
1290f0ba89beSJeff Bonwick 
1291f0ba89beSJeff Bonwick 	if (bp != NULL) {
1292ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1293ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1294ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1295ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1296ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1297ad135b5dSChristopher Siden 			    "metadata" : "data",
1298ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1299ad135b5dSChristopher Siden 		} else {
1300ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1301ad135b5dSChristopher Siden 			    sizeof (type));
1302ad135b5dSChristopher Siden 		}
1303f0ba89beSJeff Bonwick 		checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1304f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1305f0ba89beSJeff Bonwick 	}
1306fa9e4066Sahrens 
1307b24ab676SJeff Bonwick 	SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1308fa9e4066Sahrens }
1309fa9e4066Sahrens 
1310fa9e4066Sahrens void
1311fa9e4066Sahrens spa_freeze(spa_t *spa)
1312fa9e4066Sahrens {
1313fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1314fa9e4066Sahrens 
1315e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1316fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1317fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1318fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1319fa9e4066Sahrens 	}
1320e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1321fa9e4066Sahrens 	if (freeze_txg != 0)
1322fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1323fa9e4066Sahrens }
1324fa9e4066Sahrens 
13250125049cSahrens void
13260125049cSahrens zfs_panic_recover(const char *fmt, ...)
13270125049cSahrens {
13280125049cSahrens 	va_list adx;
13290125049cSahrens 
13300125049cSahrens 	va_start(adx, fmt);
13310125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
13320125049cSahrens 	va_end(adx);
13330125049cSahrens }
13340125049cSahrens 
13353f9d6ad7SLin Ling /*
13363f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
13373f9d6ad7SLin Ling  * lowercase hexidecimal numbers that don't overflow.
13383f9d6ad7SLin Ling  */
13393f9d6ad7SLin Ling uint64_t
13403f9d6ad7SLin Ling strtonum(const char *str, char **nptr)
13413f9d6ad7SLin Ling {
13423f9d6ad7SLin Ling 	uint64_t val = 0;
13433f9d6ad7SLin Ling 	char c;
13443f9d6ad7SLin Ling 	int digit;
13453f9d6ad7SLin Ling 
13463f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
13473f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
13483f9d6ad7SLin Ling 			digit = c - '0';
13493f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
13503f9d6ad7SLin Ling 			digit = 10 + c - 'a';
13513f9d6ad7SLin Ling 		else
13523f9d6ad7SLin Ling 			break;
13533f9d6ad7SLin Ling 
13543f9d6ad7SLin Ling 		val *= 16;
13553f9d6ad7SLin Ling 		val += digit;
13563f9d6ad7SLin Ling 
13573f9d6ad7SLin Ling 		str++;
13583f9d6ad7SLin Ling 	}
13593f9d6ad7SLin Ling 
13603f9d6ad7SLin Ling 	if (nptr)
13613f9d6ad7SLin Ling 		*nptr = (char *)str;
13623f9d6ad7SLin Ling 
13633f9d6ad7SLin Ling 	return (val);
13643f9d6ad7SLin Ling }
13653f9d6ad7SLin Ling 
1366fa9e4066Sahrens /*
1367fa9e4066Sahrens  * ==========================================================================
1368fa9e4066Sahrens  * Accessor functions
1369fa9e4066Sahrens  * ==========================================================================
1370fa9e4066Sahrens  */
1371fa9e4066Sahrens 
1372088f3894Sahrens boolean_t
137388b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1374fa9e4066Sahrens {
137588b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1376fa9e4066Sahrens }
1377fa9e4066Sahrens 
1378fa9e4066Sahrens dsl_pool_t *
1379fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1380fa9e4066Sahrens {
1381fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1382fa9e4066Sahrens }
1383fa9e4066Sahrens 
1384ad135b5dSChristopher Siden boolean_t
1385ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1386ad135b5dSChristopher Siden {
1387ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1388ad135b5dSChristopher Siden }
1389ad135b5dSChristopher Siden 
1390fa9e4066Sahrens blkptr_t *
1391fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1392fa9e4066Sahrens {
1393fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1394fa9e4066Sahrens }
1395fa9e4066Sahrens 
1396fa9e4066Sahrens void
1397fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1398fa9e4066Sahrens {
1399fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1400fa9e4066Sahrens }
1401fa9e4066Sahrens 
1402fa9e4066Sahrens void
1403fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1404fa9e4066Sahrens {
1405fa9e4066Sahrens 	if (spa->spa_root == NULL)
1406fa9e4066Sahrens 		buf[0] = '\0';
1407fa9e4066Sahrens 	else
1408fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1409fa9e4066Sahrens }
1410fa9e4066Sahrens 
1411fa9e4066Sahrens int
1412fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1413fa9e4066Sahrens {
1414fa9e4066Sahrens 	return (spa->spa_sync_pass);
1415fa9e4066Sahrens }
1416fa9e4066Sahrens 
1417fa9e4066Sahrens char *
1418fa9e4066Sahrens spa_name(spa_t *spa)
1419fa9e4066Sahrens {
1420fa9e4066Sahrens 	return (spa->spa_name);
1421fa9e4066Sahrens }
1422fa9e4066Sahrens 
1423fa9e4066Sahrens uint64_t
1424fa9e4066Sahrens spa_guid(spa_t *spa)
1425fa9e4066Sahrens {
1426dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1427dfbb9432SGeorge Wilson 	uint64_t guid;
1428dfbb9432SGeorge Wilson 
1429b5989ec7Seschrock 	/*
1430b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1431b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1432e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1433b5989ec7Seschrock 	 * this case.
1434b5989ec7Seschrock 	 */
1435dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1436dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1437dfbb9432SGeorge Wilson 
1438dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1439dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1440dfbb9432SGeorge Wilson 
1441dfbb9432SGeorge Wilson 	/*
1442dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1443dfbb9432SGeorge Wilson 	 * in syncing context.
1444dfbb9432SGeorge Wilson 	 */
1445dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1446b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1447b5989ec7Seschrock 	else
1448dfbb9432SGeorge Wilson 		return (guid);
1449e9103aaeSGarrett D'Amore }
1450e9103aaeSGarrett D'Amore 
1451e9103aaeSGarrett D'Amore uint64_t
1452e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1453e9103aaeSGarrett D'Amore {
1454e9103aaeSGarrett D'Amore 	/*
1455e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1456e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1457e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1458e9103aaeSGarrett D'Amore 	 */
1459e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1460fa9e4066Sahrens }
1461fa9e4066Sahrens 
1462fa9e4066Sahrens uint64_t
1463fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1464fa9e4066Sahrens {
1465fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1466fa9e4066Sahrens }
1467fa9e4066Sahrens 
1468fa9e4066Sahrens uint64_t
1469fa9e4066Sahrens spa_first_txg(spa_t *spa)
1470fa9e4066Sahrens {
1471fa9e4066Sahrens 	return (spa->spa_first_txg);
1472fa9e4066Sahrens }
1473fa9e4066Sahrens 
1474b24ab676SJeff Bonwick uint64_t
1475b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1476b24ab676SJeff Bonwick {
1477b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1478b24ab676SJeff Bonwick }
1479b24ab676SJeff Bonwick 
148088b7b0f2SMatthew Ahrens pool_state_t
1481fa9e4066Sahrens spa_state(spa_t *spa)
1482fa9e4066Sahrens {
1483fa9e4066Sahrens 	return (spa->spa_state);
1484fa9e4066Sahrens }
1485fa9e4066Sahrens 
1486b16da2e2SGeorge Wilson spa_load_state_t
1487b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1488b16da2e2SGeorge Wilson {
1489b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1490b16da2e2SGeorge Wilson }
1491b16da2e2SGeorge Wilson 
1492fa9e4066Sahrens uint64_t
1493fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1494fa9e4066Sahrens {
1495fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1496fa9e4066Sahrens }
1497fa9e4066Sahrens 
1498fa9e4066Sahrens /* ARGSUSED */
1499fa9e4066Sahrens uint64_t
1500fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1501fa9e4066Sahrens {
1502fa9e4066Sahrens 	/*
1503b24ab676SJeff Bonwick 	 * The worst case is single-sector max-parity RAID-Z blocks, in which
1504b24ab676SJeff Bonwick 	 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1505b24ab676SJeff Bonwick 	 * times the size; so just assume that.  Add to this the fact that
1506b24ab676SJeff Bonwick 	 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1507b24ab676SJeff Bonwick 	 * the block may be dittoed with up to 3 DVAs by ddt_sync().
150844cd46caSbillm 	 */
1509b24ab676SJeff Bonwick 	return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
151044cd46caSbillm }
151144cd46caSbillm 
1512485bbbf5SGeorge Wilson uint64_t
1513485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1514485bbbf5SGeorge Wilson {
1515485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1516485bbbf5SGeorge Wilson }
1517485bbbf5SGeorge Wilson 
1518485bbbf5SGeorge Wilson void
1519485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1520485bbbf5SGeorge Wilson {
1521485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1522485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1523485bbbf5SGeorge Wilson }
1524485bbbf5SGeorge Wilson 
15250a4e9518Sgw /*
15260a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
15270a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
15280a4e9518Sgw  */
15290a4e9518Sgw uint8_t
15300a4e9518Sgw spa_get_failmode(spa_t *spa)
15310a4e9518Sgw {
15320a4e9518Sgw 	return (spa->spa_failmode);
15330a4e9518Sgw }
15340a4e9518Sgw 
1535e14bb325SJeff Bonwick boolean_t
1536e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1537e14bb325SJeff Bonwick {
1538e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1539e14bb325SJeff Bonwick }
1540e14bb325SJeff Bonwick 
154144cd46caSbillm uint64_t
154244cd46caSbillm spa_version(spa_t *spa)
154344cd46caSbillm {
154444cd46caSbillm 	return (spa->spa_ubsync.ub_version);
154544cd46caSbillm }
154644cd46caSbillm 
1547b24ab676SJeff Bonwick boolean_t
1548b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1549b24ab676SJeff Bonwick {
1550b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1551b24ab676SJeff Bonwick }
1552b24ab676SJeff Bonwick 
1553b24ab676SJeff Bonwick metaslab_class_t *
1554b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1555b24ab676SJeff Bonwick {
1556b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1557b24ab676SJeff Bonwick }
1558b24ab676SJeff Bonwick 
1559b24ab676SJeff Bonwick metaslab_class_t *
1560b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1561b24ab676SJeff Bonwick {
1562b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1563b24ab676SJeff Bonwick }
1564b24ab676SJeff Bonwick 
156544cd46caSbillm int
156644cd46caSbillm spa_max_replication(spa_t *spa)
156744cd46caSbillm {
156844cd46caSbillm 	/*
1569e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
157044cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
157144cd46caSbillm 	 * replication level accordingly.
1572fa9e4066Sahrens 	 */
1573e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
157444cd46caSbillm 		return (1);
157544cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1576fa9e4066Sahrens }
1577fa9e4066Sahrens 
15783f9d6ad7SLin Ling int
15793f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
15803f9d6ad7SLin Ling {
15813f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
15823f9d6ad7SLin Ling }
15833f9d6ad7SLin Ling 
1584283b8460SGeorge.Wilson uint64_t
1585283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1586283b8460SGeorge.Wilson {
1587283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1588283b8460SGeorge.Wilson }
1589283b8460SGeorge.Wilson 
159099653d4eSeschrock uint64_t
1591b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
159299653d4eSeschrock {
1593b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1594b24ab676SJeff Bonwick 	uint64_t dsize = asize;
159599653d4eSeschrock 
1596b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
159799653d4eSeschrock 
1598b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1599b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1600b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
160199653d4eSeschrock 	}
1602b24ab676SJeff Bonwick 
1603b24ab676SJeff Bonwick 	return (dsize);
1604b24ab676SJeff Bonwick }
1605b24ab676SJeff Bonwick 
1606b24ab676SJeff Bonwick uint64_t
1607b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1608b24ab676SJeff Bonwick {
1609b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1610b24ab676SJeff Bonwick 
1611b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1612b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1613b24ab676SJeff Bonwick 
1614b24ab676SJeff Bonwick 	return (dsize);
1615b24ab676SJeff Bonwick }
1616b24ab676SJeff Bonwick 
1617b24ab676SJeff Bonwick uint64_t
1618b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1619b24ab676SJeff Bonwick {
1620b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1621b24ab676SJeff Bonwick 
1622b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1623b24ab676SJeff Bonwick 
1624b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1625b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1626b24ab676SJeff Bonwick 
1627e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1628b24ab676SJeff Bonwick 
1629b24ab676SJeff Bonwick 	return (dsize);
163099653d4eSeschrock }
163199653d4eSeschrock 
1632fa9e4066Sahrens /*
1633fa9e4066Sahrens  * ==========================================================================
1634fa9e4066Sahrens  * Initialization and Termination
1635fa9e4066Sahrens  * ==========================================================================
1636fa9e4066Sahrens  */
1637fa9e4066Sahrens 
1638fa9e4066Sahrens static int
1639fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1640fa9e4066Sahrens {
1641fa9e4066Sahrens 	const spa_t *s1 = a1;
1642fa9e4066Sahrens 	const spa_t *s2 = a2;
1643fa9e4066Sahrens 	int s;
1644fa9e4066Sahrens 
1645fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1646fa9e4066Sahrens 	if (s > 0)
1647fa9e4066Sahrens 		return (1);
1648fa9e4066Sahrens 	if (s < 0)
1649fa9e4066Sahrens 		return (-1);
1650fa9e4066Sahrens 	return (0);
1651fa9e4066Sahrens }
1652fa9e4066Sahrens 
16530373e76bSbonwick int
16540373e76bSbonwick spa_busy(void)
16550373e76bSbonwick {
16560373e76bSbonwick 	return (spa_active_count);
16570373e76bSbonwick }
16580373e76bSbonwick 
1659e7cbe64fSgw void
1660e7cbe64fSgw spa_boot_init()
1661e7cbe64fSgw {
1662e7cbe64fSgw 	spa_config_load();
1663e7cbe64fSgw }
1664e7cbe64fSgw 
1665fa9e4066Sahrens void
1666fa9e4066Sahrens spa_init(int mode)
1667fa9e4066Sahrens {
1668fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1669c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1670fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1671fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1672fa9e4066Sahrens 
1673fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1674fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1675fa9e4066Sahrens 
1676fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1677fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1678fa94a07fSbrendan 
1679fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1680fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
168199653d4eSeschrock 
16828ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1683fa9e4066Sahrens 
1684283b8460SGeorge.Wilson #ifdef _KERNEL
1685283b8460SGeorge.Wilson 	spa_arch_init();
1686283b8460SGeorge.Wilson #else
1687cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1688cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1689cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1690cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1691cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1692cd1c8b85SMatthew Ahrens 		} else {
1693cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1694cd1c8b85SMatthew Ahrens 		}
1695cd1c8b85SMatthew Ahrens 	}
1696cd1c8b85SMatthew Ahrens #endif
1697cd1c8b85SMatthew Ahrens 
1698fa9e4066Sahrens 	refcount_init();
1699fa9e4066Sahrens 	unique_init();
170001f55e48SGeorge Wilson 	space_map_init();
1701fa9e4066Sahrens 	zio_init();
1702fa9e4066Sahrens 	dmu_init();
1703fa9e4066Sahrens 	zil_init();
170487db74c1Sek 	vdev_cache_stat_init();
170591ebeef5Sahrens 	zfs_prop_init();
1706990b4856Slling 	zpool_prop_init();
1707ad135b5dSChristopher Siden 	zpool_feature_init();
1708fa9e4066Sahrens 	spa_config_load();
1709e14bb325SJeff Bonwick 	l2arc_start();
1710fa9e4066Sahrens }
1711fa9e4066Sahrens 
1712fa9e4066Sahrens void
1713fa9e4066Sahrens spa_fini(void)
1714fa9e4066Sahrens {
1715e14bb325SJeff Bonwick 	l2arc_stop();
1716e14bb325SJeff Bonwick 
1717fa9e4066Sahrens 	spa_evict_all();
1718fa9e4066Sahrens 
171987db74c1Sek 	vdev_cache_stat_fini();
1720fa9e4066Sahrens 	zil_fini();
1721fa9e4066Sahrens 	dmu_fini();
1722fa9e4066Sahrens 	zio_fini();
172301f55e48SGeorge Wilson 	space_map_fini();
172491ebeef5Sahrens 	unique_fini();
1725fa9e4066Sahrens 	refcount_fini();
1726fa9e4066Sahrens 
1727fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
172899653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1729fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1730fa9e4066Sahrens 
1731fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1732fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1733c25056deSgw 	mutex_destroy(&spa_spare_lock);
1734fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1735fa9e4066Sahrens }
17366ce0521aSperrin 
17376ce0521aSperrin /*
17386ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
17396ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
17406ce0521aSperrin  * performance and not correctness
17416ce0521aSperrin  */
17426ce0521aSperrin boolean_t
17436ce0521aSperrin spa_has_slogs(spa_t *spa)
17446ce0521aSperrin {
17456ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
17466ce0521aSperrin }
1747bf82a41bSeschrock 
1748b24ab676SJeff Bonwick spa_log_state_t
1749b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1750b24ab676SJeff Bonwick {
1751b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1752b24ab676SJeff Bonwick }
1753b24ab676SJeff Bonwick 
1754b24ab676SJeff Bonwick void
1755b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1756b24ab676SJeff Bonwick {
1757b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1758b24ab676SJeff Bonwick }
1759b24ab676SJeff Bonwick 
1760bf82a41bSeschrock boolean_t
1761bf82a41bSeschrock spa_is_root(spa_t *spa)
1762bf82a41bSeschrock {
1763bf82a41bSeschrock 	return (spa->spa_is_root);
1764bf82a41bSeschrock }
17658ad4d6ddSJeff Bonwick 
17668ad4d6ddSJeff Bonwick boolean_t
17678ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
17688ad4d6ddSJeff Bonwick {
17698ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
17708ad4d6ddSJeff Bonwick }
17718ad4d6ddSJeff Bonwick 
17728ad4d6ddSJeff Bonwick int
17738ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
17748ad4d6ddSJeff Bonwick {
17758ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
17768ad4d6ddSJeff Bonwick }
1777b24ab676SJeff Bonwick 
1778b24ab676SJeff Bonwick uint64_t
1779b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1780b24ab676SJeff Bonwick {
1781b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1782b24ab676SJeff Bonwick }
1783b24ab676SJeff Bonwick 
1784b24ab676SJeff Bonwick uint64_t
1785b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1786b24ab676SJeff Bonwick {
1787b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1788b24ab676SJeff Bonwick }
1789b24ab676SJeff Bonwick 
1790b24ab676SJeff Bonwick objset_t *
1791b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1792b24ab676SJeff Bonwick {
1793b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1794b24ab676SJeff Bonwick }
1795b24ab676SJeff Bonwick 
1796b24ab676SJeff Bonwick enum zio_checksum
1797b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1798b24ab676SJeff Bonwick {
1799b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1800b24ab676SJeff Bonwick }
18013f9d6ad7SLin Ling 
18023f9d6ad7SLin Ling /*
18033f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
18043f9d6ad7SLin Ling  */
18053f9d6ad7SLin Ling void
18063f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
18073f9d6ad7SLin Ling {
18083f9d6ad7SLin Ling 	/* data not stored on disk */
18093f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
18103f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
18113f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
18123f9d6ad7SLin Ling }
18133f9d6ad7SLin Ling 
18143f9d6ad7SLin Ling /*
18153f9d6ad7SLin Ling  * Get scan stats for zpool status reports
18163f9d6ad7SLin Ling  */
18173f9d6ad7SLin Ling int
18183f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
18193f9d6ad7SLin Ling {
18203f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
18213f9d6ad7SLin Ling 
18223f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
18233f9d6ad7SLin Ling 		return (ENOENT);
18243f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
18253f9d6ad7SLin Ling 
18263f9d6ad7SLin Ling 	/* data stored on disk */
18273f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
18283f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
18293f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
18303f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
18313f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
18323f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
18333f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
18343f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
18353f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
18363f9d6ad7SLin Ling 
18373f9d6ad7SLin Ling 	/* data not stored on disk */
18383f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
18393f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
18403f9d6ad7SLin Ling 
18413f9d6ad7SLin Ling 	return (0);
18423f9d6ad7SLin Ling }
184309c9d376SGeorge Wilson 
184409c9d376SGeorge Wilson boolean_t
184509c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
184609c9d376SGeorge Wilson {
184709c9d376SGeorge Wilson 	return (spa->spa_debug);
184809c9d376SGeorge Wilson }
1849