xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision ad135b5d644628e791c3188a6ecbd9c257961ef8)
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.
23*ad135b5dSChristopher 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>
29fa9e4066Sahrens #include <sys/zio.h>
30fa9e4066Sahrens #include <sys/zio_checksum.h>
31fa9e4066Sahrens #include <sys/zio_compress.h>
32fa9e4066Sahrens #include <sys/dmu.h>
33fa9e4066Sahrens #include <sys/dmu_tx.h>
34fa9e4066Sahrens #include <sys/zap.h>
35fa9e4066Sahrens #include <sys/zil.h>
36fa9e4066Sahrens #include <sys/vdev_impl.h>
37fa9e4066Sahrens #include <sys/metaslab.h>
38fa9e4066Sahrens #include <sys/uberblock_impl.h>
39fa9e4066Sahrens #include <sys/txg.h>
40fa9e4066Sahrens #include <sys/avl.h>
41fa9e4066Sahrens #include <sys/unique.h>
42fa9e4066Sahrens #include <sys/dsl_pool.h>
43fa9e4066Sahrens #include <sys/dsl_dir.h>
44fa9e4066Sahrens #include <sys/dsl_prop.h>
453f9d6ad7SLin Ling #include <sys/dsl_scan.h>
46fa9e4066Sahrens #include <sys/fs/zfs.h>
476ce0521aSperrin #include <sys/metaslab_impl.h>
48e14bb325SJeff Bonwick #include <sys/arc.h>
49485bbbf5SGeorge Wilson #include <sys/ddt.h>
5091ebeef5Sahrens #include "zfs_prop.h"
51*ad135b5dSChristopher Siden #include "zfeature_common.h"
52fa9e4066Sahrens 
53fa9e4066Sahrens /*
54fa9e4066Sahrens  * SPA locking
55fa9e4066Sahrens  *
56fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * spa_namespace_lock (global mutex)
59fa9e4066Sahrens  *
6044cd46caSbillm  *	This lock must be acquired to do any of the following:
61fa9e4066Sahrens  *
6244cd46caSbillm  *		- Lookup a spa_t by name
6344cd46caSbillm  *		- Add or remove a spa_t from the namespace
6444cd46caSbillm  *		- Increase spa_refcount from non-zero
6544cd46caSbillm  *		- Check if spa_refcount is zero
6644cd46caSbillm  *		- Rename a spa_t
67ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
6844cd46caSbillm  *		- Held for the duration of create/destroy/import/export
69fa9e4066Sahrens  *
7044cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
7144cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
7244cd46caSbillm  *	definition they must have an existing reference, and will never need
7344cd46caSbillm  *	to lookup a spa_t by name.
74fa9e4066Sahrens  *
75fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
76fa9e4066Sahrens  *
7744cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
7844cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7944cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
80088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
8144cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
82fa9e4066Sahrens  *
83e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
84fa9e4066Sahrens  *
8591ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
8691ebeef5Sahrens  *	the following circumstances:
87fa9e4066Sahrens  *
8844cd46caSbillm  *		- RW_READER to perform I/O to the spa
8944cd46caSbillm  *		- RW_WRITER to change the vdev config
90fa9e4066Sahrens  *
91fa9e4066Sahrens  * The locking order is fairly straightforward:
92fa9e4066Sahrens  *
9344cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
94fa9e4066Sahrens  *
9544cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
9644cd46caSbillm  *	or to check if it is zero.
97fa9e4066Sahrens  *
98e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
99fa9e4066Sahrens  *
10044cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10144cd46caSbillm  *	the config lock.
102fa9e4066Sahrens  *
103e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
104fa9e4066Sahrens  *
10544cd46caSbillm  *	The namespace lock must always be taken before the config lock.
106fa9e4066Sahrens  *
107fa9e4066Sahrens  *
108e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
109fa9e4066Sahrens  *
110e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
111e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
112fa9e4066Sahrens  *
11344cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
114fa9e4066Sahrens  *
11544cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
116fa9e4066Sahrens  *
11744cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
11844cd46caSbillm  *				frees up any memory associated with the spa_t.
119fa9e4066Sahrens  *
12044cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12144cd46caSbillm  *				first if NULL is passed.
122fa9e4066Sahrens  *
12344cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12444cd46caSbillm  *				the system.
125fa9e4066Sahrens  *
126ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
127fa9e4066Sahrens  *
128fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
129fa9e4066Sahrens  *
13044cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13144cd46caSbillm  *				called with spa_namespace_lock held if the
13244cd46caSbillm  *				refcount is currently zero.
133fa9e4066Sahrens  *
13444cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13544cd46caSbillm  *				not free the spa_t or remove it from the
13644cd46caSbillm  *				namespace.  No locking is required.
137fa9e4066Sahrens  *
13844cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
13944cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14044cd46caSbillm  *				held.
141fa9e4066Sahrens  *
142e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
143e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
144e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
145e14bb325SJeff Bonwick  *
146e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
147e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
148e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
149e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
150e14bb325SJeff Bonwick  *
151e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
152e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
153e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
154e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
155e14bb325SJeff Bonwick  *
156e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
157e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
158e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
159e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
160e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
161e14bb325SJeff Bonwick  *
162e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
163e14bb325SJeff Bonwick  *
164e14bb325SJeff Bonwick  * SCL_CONFIG
165e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
166e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
167e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
168e14bb325SJeff Bonwick  *
169e14bb325SJeff Bonwick  * SCL_STATE
170e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
171e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
172e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
173e14bb325SJeff Bonwick  *
174e14bb325SJeff Bonwick  * SCL_ALLOC
175e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
176e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
177e14bb325SJeff Bonwick  *
178e14bb325SJeff Bonwick  * SCL_ZIO
179e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
180e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
181e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
182e14bb325SJeff Bonwick  *
183e14bb325SJeff Bonwick  * SCL_FREE
184e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
185e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
186e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
187e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
188e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
189e14bb325SJeff Bonwick  *
190e14bb325SJeff Bonwick  * SCL_VDEV
191e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
192b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
193e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
194e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
195e14bb325SJeff Bonwick  *
196e14bb325SJeff Bonwick  * In addition, the following rules apply:
197e14bb325SJeff Bonwick  *
198e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
199e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
200e14bb325SJeff Bonwick  *
201e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
202e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
203e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
204e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
205e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
206fa9e4066Sahrens  *
207ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
208fa9e4066Sahrens  *
20944cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
210ea8dc4b6Seschrock  *				for writing.
211fa9e4066Sahrens  *
21244cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21344cd46caSbillm  *				to complete, sync the updated configs to the
214ea8dc4b6Seschrock  *				cache, and release the namespace lock.
215fa9e4066Sahrens  *
216e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
217e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
218e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
219e14bb325SJeff Bonwick  *
220*ad135b5dSChristopher Siden  * spa_rename() is also implemented within this file since it requires
221e14bb325SJeff Bonwick  * manipulation of the namespace.
222fa9e4066Sahrens  */
223fa9e4066Sahrens 
224fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
225fa9e4066Sahrens kmutex_t spa_namespace_lock;
226fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2270373e76bSbonwick static int spa_active_count;
228416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
229fa9e4066Sahrens 
23099653d4eSeschrock static kmutex_t spa_spare_lock;
23139c23413Seschrock static avl_tree_t spa_spare_avl;
232fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
233fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23499653d4eSeschrock 
235fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2368ad4d6ddSJeff Bonwick int spa_mode_global;
237fa9e4066Sahrens 
238fa9e4066Sahrens #ifdef ZFS_DEBUG
23940feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
24040feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
241fa9e4066Sahrens #else
242fa9e4066Sahrens int zfs_flags = 0;
243fa9e4066Sahrens #endif
244fa9e4066Sahrens 
2450125049cSahrens /*
2460125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2470125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2480125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2490125049cSahrens  */
2500125049cSahrens int zfs_recover = 0;
2510125049cSahrens 
252fa9e4066Sahrens 
253e05725b1Sbonwick /*
254e05725b1Sbonwick  * ==========================================================================
255e05725b1Sbonwick  * SPA config locking
256e05725b1Sbonwick  * ==========================================================================
257e05725b1Sbonwick  */
258e05725b1Sbonwick static void
259e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
260e14bb325SJeff Bonwick {
261e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
262e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
263e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
264e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
265e14bb325SJeff Bonwick 		refcount_create(&scl->scl_count);
266e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
267e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
268e14bb325SJeff Bonwick 	}
269e05725b1Sbonwick }
270e05725b1Sbonwick 
271e05725b1Sbonwick static void
272e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
273e14bb325SJeff Bonwick {
274e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
275e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
276e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
277e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
278e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
279e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
280e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
281e14bb325SJeff Bonwick 	}
282e14bb325SJeff Bonwick }
283e14bb325SJeff Bonwick 
284e14bb325SJeff Bonwick int
285e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
286e05725b1Sbonwick {
287e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
288e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
289e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
290e14bb325SJeff Bonwick 			continue;
291e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
292e14bb325SJeff Bonwick 		if (rw == RW_READER) {
293e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
294e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
295e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
296e14bb325SJeff Bonwick 				return (0);
297e14bb325SJeff Bonwick 			}
298e14bb325SJeff Bonwick 		} else {
299e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
300e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
301e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
302e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
303e14bb325SJeff Bonwick 				return (0);
304e14bb325SJeff Bonwick 			}
305e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
306e14bb325SJeff Bonwick 		}
307e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
308e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
309e14bb325SJeff Bonwick 	}
310e14bb325SJeff Bonwick 	return (1);
311e05725b1Sbonwick }
312e05725b1Sbonwick 
313e05725b1Sbonwick void
314e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
315e05725b1Sbonwick {
316f64c0e34SEric Taylor 	int wlocks_held = 0;
317f64c0e34SEric Taylor 
318e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
319e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
320f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
321f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
322e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
323e14bb325SJeff Bonwick 			continue;
324e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
325e14bb325SJeff Bonwick 		if (rw == RW_READER) {
326e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
327e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
328e14bb325SJeff Bonwick 			}
329e14bb325SJeff Bonwick 		} else {
330e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
331e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
332e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
333e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
334e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
335e14bb325SJeff Bonwick 			}
336e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
337e14bb325SJeff Bonwick 		}
338e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
339e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
340e05725b1Sbonwick 	}
341f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
342e05725b1Sbonwick }
343e05725b1Sbonwick 
344e05725b1Sbonwick void
345e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
346e05725b1Sbonwick {
347e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
348e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
349e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
350e14bb325SJeff Bonwick 			continue;
351e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
352e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
353e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
354e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
355e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
356e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
357e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
358e14bb325SJeff Bonwick 		}
359e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
360e05725b1Sbonwick 	}
361e05725b1Sbonwick }
362e05725b1Sbonwick 
363e14bb325SJeff Bonwick int
364e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
365e05725b1Sbonwick {
366e14bb325SJeff Bonwick 	int locks_held = 0;
367e05725b1Sbonwick 
368e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
369e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
370e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
371e14bb325SJeff Bonwick 			continue;
372e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
373e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
374e14bb325SJeff Bonwick 			locks_held |= 1 << i;
375e14bb325SJeff Bonwick 	}
376e14bb325SJeff Bonwick 
377e14bb325SJeff Bonwick 	return (locks_held);
378e05725b1Sbonwick }
379e05725b1Sbonwick 
380fa9e4066Sahrens /*
381fa9e4066Sahrens  * ==========================================================================
382fa9e4066Sahrens  * SPA namespace functions
383fa9e4066Sahrens  * ==========================================================================
384fa9e4066Sahrens  */
385fa9e4066Sahrens 
386fa9e4066Sahrens /*
387fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
388fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
389fa9e4066Sahrens  */
390fa9e4066Sahrens spa_t *
391fa9e4066Sahrens spa_lookup(const char *name)
392fa9e4066Sahrens {
393e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
394e14bb325SJeff Bonwick 	spa_t *spa;
395fa9e4066Sahrens 	avl_index_t where;
39640feaa91Sahrens 	char c;
39740feaa91Sahrens 	char *cp;
398fa9e4066Sahrens 
399fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
400fa9e4066Sahrens 
40140feaa91Sahrens 	/*
40240feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
40340feaa91Sahrens 	 * just use that.
40440feaa91Sahrens 	 */
40540feaa91Sahrens 	cp = strpbrk(name, "/@");
40640feaa91Sahrens 	if (cp) {
40740feaa91Sahrens 		c = *cp;
40840feaa91Sahrens 		*cp = '\0';
40940feaa91Sahrens 	}
41040feaa91Sahrens 
411e14bb325SJeff Bonwick 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
412fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
413fa9e4066Sahrens 
41440feaa91Sahrens 	if (cp)
41540feaa91Sahrens 		*cp = c;
41640feaa91Sahrens 
417fa9e4066Sahrens 	return (spa);
418fa9e4066Sahrens }
419fa9e4066Sahrens 
420fa9e4066Sahrens /*
421fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
422fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
423fa9e4066Sahrens  * exist by calling spa_lookup() first.
424fa9e4066Sahrens  */
425fa9e4066Sahrens spa_t *
426468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
427fa9e4066Sahrens {
428fa9e4066Sahrens 	spa_t *spa;
429c5904d13Seschrock 	spa_config_dirent_t *dp;
430fa9e4066Sahrens 
431fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
432fa9e4066Sahrens 
433fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
434fa9e4066Sahrens 
435c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
436c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
43735a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
438c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
43935a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
440c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
44135a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
442a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
443a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
444c25056deSgw 
445c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
44635a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
447c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
448e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
449c25056deSgw 
450b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
451cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
452b24ab676SJeff Bonwick 
453e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
454fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
455fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
4560373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
457468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
45835a5a358SJonathan Adams 	spa->spa_proc = &p0;
45935a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
460fa9e4066Sahrens 
461fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
462e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
463fa9e4066Sahrens 
464fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
465fa9e4066Sahrens 
4660373e76bSbonwick 	/*
4670373e76bSbonwick 	 * Set the alternate root, if there is one.
4680373e76bSbonwick 	 */
4690373e76bSbonwick 	if (altroot) {
4700373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
4710373e76bSbonwick 		spa_active_count++;
4720373e76bSbonwick 	}
4730373e76bSbonwick 
474c5904d13Seschrock 	/*
475c5904d13Seschrock 	 * Every pool starts with the default cachefile
476c5904d13Seschrock 	 */
477c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
478c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
479c5904d13Seschrock 
480c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
481ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
482c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
483c5904d13Seschrock 
4844b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
4854b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
4864b964adaSGeorge Wilson 
487*ad135b5dSChristopher Siden 	if (config != NULL) {
488*ad135b5dSChristopher Siden 		nvlist_t *features;
489*ad135b5dSChristopher Siden 
490*ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
491*ad135b5dSChristopher Siden 		    &features) == 0) {
492*ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
493*ad135b5dSChristopher Siden 			    0) == 0);
494*ad135b5dSChristopher Siden 		}
495*ad135b5dSChristopher Siden 
496468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
497*ad135b5dSChristopher Siden 	}
498*ad135b5dSChristopher Siden 
499*ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
500*ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
501*ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
502*ad135b5dSChristopher Siden 	}
503468c413aSTim Haley 
504fa9e4066Sahrens 	return (spa);
505fa9e4066Sahrens }
506fa9e4066Sahrens 
507fa9e4066Sahrens /*
508fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
509fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
510fa9e4066Sahrens  * deactivated.
511fa9e4066Sahrens  */
512fa9e4066Sahrens void
513fa9e4066Sahrens spa_remove(spa_t *spa)
514fa9e4066Sahrens {
515c5904d13Seschrock 	spa_config_dirent_t *dp;
516c5904d13Seschrock 
517fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
518fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
519fa9e4066Sahrens 
5201195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
5211195e687SMark J Musante 
522fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
523fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
524fa9e4066Sahrens 
5250373e76bSbonwick 	if (spa->spa_root) {
526fa9e4066Sahrens 		spa_strfree(spa->spa_root);
5270373e76bSbonwick 		spa_active_count--;
5280373e76bSbonwick 	}
529fa9e4066Sahrens 
530c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
531c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
532c5904d13Seschrock 		if (dp->scd_path != NULL)
533c5904d13Seschrock 			spa_strfree(dp->scd_path);
534c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
535c5904d13Seschrock 	}
536c5904d13Seschrock 
537c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
5382f8aaab3Seschrock 
539*ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
5404b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
541fa9e4066Sahrens 	spa_config_set(spa, NULL);
542fa9e4066Sahrens 
543fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
54491ebeef5Sahrens 
545e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
546fa9e4066Sahrens 
547b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
548cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
549b24ab676SJeff Bonwick 
550c25056deSgw 	cv_destroy(&spa->spa_async_cv);
55135a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
552c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
553e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
554c25056deSgw 
5555ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
556c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
55735a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
55806eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
55935a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
560b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
56135a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
562e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
563a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
5645ad82045Snd 
565fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
566fa9e4066Sahrens }
567fa9e4066Sahrens 
568fa9e4066Sahrens /*
569fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
570fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
571fa9e4066Sahrens  */
572fa9e4066Sahrens spa_t *
573fa9e4066Sahrens spa_next(spa_t *prev)
574fa9e4066Sahrens {
575fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
576fa9e4066Sahrens 
577fa9e4066Sahrens 	if (prev)
578fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
579fa9e4066Sahrens 	else
580fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
581fa9e4066Sahrens }
582fa9e4066Sahrens 
583fa9e4066Sahrens /*
584fa9e4066Sahrens  * ==========================================================================
585fa9e4066Sahrens  * SPA refcount functions
586fa9e4066Sahrens  * ==========================================================================
587fa9e4066Sahrens  */
588fa9e4066Sahrens 
589fa9e4066Sahrens /*
590fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
591fa9e4066Sahrens  * have the namespace lock held.
592fa9e4066Sahrens  */
593fa9e4066Sahrens void
594fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
595fa9e4066Sahrens {
596088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
597fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
598fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
599fa9e4066Sahrens }
600fa9e4066Sahrens 
601fa9e4066Sahrens /*
602fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
603fa9e4066Sahrens  * have the namespace lock held.
604fa9e4066Sahrens  */
605fa9e4066Sahrens void
606fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
607fa9e4066Sahrens {
608088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
609fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
610fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
611fa9e4066Sahrens }
612fa9e4066Sahrens 
613fa9e4066Sahrens /*
614fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
615088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
616fa9e4066Sahrens  * number of references acquired when opening a pool
617fa9e4066Sahrens  */
618fa9e4066Sahrens boolean_t
619fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
620fa9e4066Sahrens {
621fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
622fa9e4066Sahrens 
623088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
624fa9e4066Sahrens }
625fa9e4066Sahrens 
62699653d4eSeschrock /*
62799653d4eSeschrock  * ==========================================================================
628fa94a07fSbrendan  * SPA spare and l2cache tracking
62999653d4eSeschrock  * ==========================================================================
63099653d4eSeschrock  */
63199653d4eSeschrock 
632fa94a07fSbrendan /*
633fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
634fa94a07fSbrendan  * for 'auxiliary' devices.
635fa94a07fSbrendan  */
636fa94a07fSbrendan 
637fa94a07fSbrendan typedef struct spa_aux {
638fa94a07fSbrendan 	uint64_t	aux_guid;
639fa94a07fSbrendan 	uint64_t	aux_pool;
640fa94a07fSbrendan 	avl_node_t	aux_avl;
641fa94a07fSbrendan 	int		aux_count;
642fa94a07fSbrendan } spa_aux_t;
643fa94a07fSbrendan 
644fa94a07fSbrendan static int
645fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
646fa94a07fSbrendan {
647fa94a07fSbrendan 	const spa_aux_t *sa = a;
648fa94a07fSbrendan 	const spa_aux_t *sb = b;
649fa94a07fSbrendan 
650fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
651fa94a07fSbrendan 		return (-1);
652fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
653fa94a07fSbrendan 		return (1);
654fa94a07fSbrendan 	else
655fa94a07fSbrendan 		return (0);
656fa94a07fSbrendan }
657fa94a07fSbrendan 
658fa94a07fSbrendan void
659fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
660fa94a07fSbrendan {
661fa94a07fSbrendan 	avl_index_t where;
662fa94a07fSbrendan 	spa_aux_t search;
663fa94a07fSbrendan 	spa_aux_t *aux;
664fa94a07fSbrendan 
665fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
666fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
667fa94a07fSbrendan 		aux->aux_count++;
668fa94a07fSbrendan 	} else {
669fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
670fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
671fa94a07fSbrendan 		aux->aux_count = 1;
672fa94a07fSbrendan 		avl_insert(avl, aux, where);
673fa94a07fSbrendan 	}
674fa94a07fSbrendan }
675fa94a07fSbrendan 
676fa94a07fSbrendan void
677fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
678fa94a07fSbrendan {
679fa94a07fSbrendan 	spa_aux_t search;
680fa94a07fSbrendan 	spa_aux_t *aux;
681fa94a07fSbrendan 	avl_index_t where;
682fa94a07fSbrendan 
683fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
684fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
685fa94a07fSbrendan 
686fa94a07fSbrendan 	ASSERT(aux != NULL);
687fa94a07fSbrendan 
688fa94a07fSbrendan 	if (--aux->aux_count == 0) {
689fa94a07fSbrendan 		avl_remove(avl, aux);
690fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
691fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
692fa94a07fSbrendan 		aux->aux_pool = 0ULL;
693fa94a07fSbrendan 	}
694fa94a07fSbrendan }
695fa94a07fSbrendan 
696fa94a07fSbrendan boolean_t
69789a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
698fa94a07fSbrendan {
699fa94a07fSbrendan 	spa_aux_t search, *found;
700fa94a07fSbrendan 
701fa94a07fSbrendan 	search.aux_guid = guid;
70289a89ebfSlling 	found = avl_find(avl, &search, NULL);
703fa94a07fSbrendan 
704fa94a07fSbrendan 	if (pool) {
705fa94a07fSbrendan 		if (found)
706fa94a07fSbrendan 			*pool = found->aux_pool;
707fa94a07fSbrendan 		else
708fa94a07fSbrendan 			*pool = 0ULL;
709fa94a07fSbrendan 	}
710fa94a07fSbrendan 
71189a89ebfSlling 	if (refcnt) {
71289a89ebfSlling 		if (found)
71389a89ebfSlling 			*refcnt = found->aux_count;
71489a89ebfSlling 		else
71589a89ebfSlling 			*refcnt = 0;
71689a89ebfSlling 	}
71789a89ebfSlling 
718fa94a07fSbrendan 	return (found != NULL);
719fa94a07fSbrendan }
720fa94a07fSbrendan 
721fa94a07fSbrendan void
722fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
723fa94a07fSbrendan {
724fa94a07fSbrendan 	spa_aux_t search, *found;
725fa94a07fSbrendan 	avl_index_t where;
726fa94a07fSbrendan 
727fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
728fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
729fa94a07fSbrendan 	ASSERT(found != NULL);
730fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
731fa94a07fSbrendan 
732fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
733fa94a07fSbrendan }
734fa94a07fSbrendan 
73599653d4eSeschrock /*
73639c23413Seschrock  * Spares are tracked globally due to the following constraints:
73739c23413Seschrock  *
73839c23413Seschrock  * 	- A spare may be part of multiple pools.
73939c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
74039c23413Seschrock  *	  another pool.
74139c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
74239c23413Seschrock  *	  the target is a spare in the same pool.
74339c23413Seschrock  *
74439c23413Seschrock  * We keep track of all spares on the system through the use of a reference
74539c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
74639c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
74739c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
74839c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
74939c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
75039c23413Seschrock  *
75139c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
75239c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
75339c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
75439c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
75599653d4eSeschrock  */
75639c23413Seschrock 
75799653d4eSeschrock static int
75899653d4eSeschrock spa_spare_compare(const void *a, const void *b)
75999653d4eSeschrock {
760fa94a07fSbrendan 	return (spa_aux_compare(a, b));
76199653d4eSeschrock }
76299653d4eSeschrock 
76399653d4eSeschrock void
76439c23413Seschrock spa_spare_add(vdev_t *vd)
76599653d4eSeschrock {
76699653d4eSeschrock 	mutex_enter(&spa_spare_lock);
76739c23413Seschrock 	ASSERT(!vd->vdev_isspare);
768fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
76939c23413Seschrock 	vd->vdev_isspare = B_TRUE;
77099653d4eSeschrock 	mutex_exit(&spa_spare_lock);
77199653d4eSeschrock }
77299653d4eSeschrock 
77399653d4eSeschrock void
77439c23413Seschrock spa_spare_remove(vdev_t *vd)
77599653d4eSeschrock {
77699653d4eSeschrock 	mutex_enter(&spa_spare_lock);
77739c23413Seschrock 	ASSERT(vd->vdev_isspare);
778fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
77939c23413Seschrock 	vd->vdev_isspare = B_FALSE;
78099653d4eSeschrock 	mutex_exit(&spa_spare_lock);
78199653d4eSeschrock }
78299653d4eSeschrock 
78399653d4eSeschrock boolean_t
78489a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
78599653d4eSeschrock {
786fa94a07fSbrendan 	boolean_t found;
78799653d4eSeschrock 
78899653d4eSeschrock 	mutex_enter(&spa_spare_lock);
78989a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
79099653d4eSeschrock 	mutex_exit(&spa_spare_lock);
79199653d4eSeschrock 
792fa94a07fSbrendan 	return (found);
79339c23413Seschrock }
79439c23413Seschrock 
79539c23413Seschrock void
79639c23413Seschrock spa_spare_activate(vdev_t *vd)
79739c23413Seschrock {
79839c23413Seschrock 	mutex_enter(&spa_spare_lock);
79939c23413Seschrock 	ASSERT(vd->vdev_isspare);
800fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
801fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
802fa94a07fSbrendan }
80339c23413Seschrock 
804fa94a07fSbrendan /*
805fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
806fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
807fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
808fa94a07fSbrendan  */
80939c23413Seschrock 
810fa94a07fSbrendan static int
811fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
812fa94a07fSbrendan {
813fa94a07fSbrendan 	return (spa_aux_compare(a, b));
814fa94a07fSbrendan }
815fa94a07fSbrendan 
816fa94a07fSbrendan void
817fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
818fa94a07fSbrendan {
819fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
820fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
821fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
822fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
823fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
824fa94a07fSbrendan }
825fa94a07fSbrendan 
826fa94a07fSbrendan void
827fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
828fa94a07fSbrendan {
829fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
830fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
831fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
832fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
833fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
834fa94a07fSbrendan }
835fa94a07fSbrendan 
836fa94a07fSbrendan boolean_t
837fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
838fa94a07fSbrendan {
839fa94a07fSbrendan 	boolean_t found;
840fa94a07fSbrendan 
841fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
84289a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
843fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
844fa94a07fSbrendan 
845fa94a07fSbrendan 	return (found);
846fa94a07fSbrendan }
847fa94a07fSbrendan 
848fa94a07fSbrendan void
849fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
850fa94a07fSbrendan {
851fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
852fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
853fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
854fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
855fa94a07fSbrendan }
856fa94a07fSbrendan 
857fa9e4066Sahrens /*
858fa9e4066Sahrens  * ==========================================================================
859fa9e4066Sahrens  * SPA vdev locking
860fa9e4066Sahrens  * ==========================================================================
861fa9e4066Sahrens  */
862fa9e4066Sahrens 
863fa9e4066Sahrens /*
864ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
865ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
866fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
867fa9e4066Sahrens  */
868fa9e4066Sahrens uint64_t
869fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
870fa9e4066Sahrens {
871a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
872bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
87388ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
87488ecc943SGeorge Wilson }
87588ecc943SGeorge Wilson 
87688ecc943SGeorge Wilson /*
87788ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
87888ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
87988ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
88088ecc943SGeorge Wilson  */
88188ecc943SGeorge Wilson uint64_t
88288ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
88388ecc943SGeorge Wilson {
88488ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8853d7072f8Seschrock 
886e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
887fa9e4066Sahrens 
888fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
889fa9e4066Sahrens }
890fa9e4066Sahrens 
891fa9e4066Sahrens /*
89288ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
89388ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
894fa9e4066Sahrens  */
89588ecc943SGeorge Wilson void
89688ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
897fa9e4066Sahrens {
89888ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
89988ecc943SGeorge Wilson 
9000e34b6a7Sbonwick 	int config_changed = B_FALSE;
901ea8dc4b6Seschrock 
9020373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
9030e34b6a7Sbonwick 
904e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
905e14bb325SJeff Bonwick 
9060e34b6a7Sbonwick 	/*
9070e34b6a7Sbonwick 	 * Reassess the DTLs.
9080e34b6a7Sbonwick 	 */
9090373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
9100e34b6a7Sbonwick 
911e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
9120e34b6a7Sbonwick 		config_changed = B_TRUE;
9138f18d1faSGeorge Wilson 		spa->spa_config_generation++;
9140e34b6a7Sbonwick 	}
915ea8dc4b6Seschrock 
91688ecc943SGeorge Wilson 	/*
91788ecc943SGeorge Wilson 	 * Verify the metaslab classes.
91888ecc943SGeorge Wilson 	 */
919b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
920b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
92188ecc943SGeorge Wilson 
922e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
923fa9e4066Sahrens 
92488ecc943SGeorge Wilson 	/*
92588ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
92688ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
92788ecc943SGeorge Wilson 	 * transactionally.
92888ecc943SGeorge Wilson 	 */
92988ecc943SGeorge Wilson 	if (zio_injection_enabled)
9301195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
93188ecc943SGeorge Wilson 
932fa9e4066Sahrens 	/*
933fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
934fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
935fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
936fa9e4066Sahrens 	 */
937fa9e4066Sahrens 	if (error == 0)
938fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
939fa9e4066Sahrens 
940fa9e4066Sahrens 	if (vd != NULL) {
9418ad4d6ddSJeff Bonwick 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
9428ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
943fa9e4066Sahrens 		vdev_free(vd);
9448ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
945fa9e4066Sahrens 	}
946fa9e4066Sahrens 
947fa9e4066Sahrens 	/*
9480e34b6a7Sbonwick 	 * If the config changed, update the config cache.
949fa9e4066Sahrens 	 */
9500e34b6a7Sbonwick 	if (config_changed)
951c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
95288ecc943SGeorge Wilson }
953ea8dc4b6Seschrock 
95488ecc943SGeorge Wilson /*
95588ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
95688ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
95788ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
95888ecc943SGeorge Wilson  * information.
95988ecc943SGeorge Wilson  */
96088ecc943SGeorge Wilson int
96188ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
96288ecc943SGeorge Wilson {
96388ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
964ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
965bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
966fa9e4066Sahrens 
967fa9e4066Sahrens 	return (error);
968fa9e4066Sahrens }
969fa9e4066Sahrens 
970e14bb325SJeff Bonwick /*
971e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
972e14bb325SJeff Bonwick  */
973e14bb325SJeff Bonwick void
9748f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
975e14bb325SJeff Bonwick {
9768f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
9778f18d1faSGeorge Wilson 
978dcba9f3fSGeorge Wilson 	/*
979dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
980dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
981dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
982dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
983dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
984dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
985dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
986dcba9f3fSGeorge Wilson 	 */
987dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
9889842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
9899842588bSGeorge Wilson 		int high = locks & ~low;
9909842588bSGeorge Wilson 
9919842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
992dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
9939842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
994dcba9f3fSGeorge Wilson 	} else {
995dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
996dcba9f3fSGeorge Wilson 	}
9978f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
998e14bb325SJeff Bonwick }
999e14bb325SJeff Bonwick 
1000e14bb325SJeff Bonwick int
1001e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1002e14bb325SJeff Bonwick {
1003c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1004c6065d0fSGeorge Wilson 
1005b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1006b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1007b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1008b24ab676SJeff Bonwick 
10098f18d1faSGeorge Wilson 	if (vd != NULL) {
1010e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1011c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
10128f18d1faSGeorge Wilson 		spa->spa_config_generation++;
10138f18d1faSGeorge Wilson 	}
1014e14bb325SJeff Bonwick 
1015dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1016dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1017dcba9f3fSGeorge Wilson 
10188f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
10198f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1020e14bb325SJeff Bonwick 
10218ad4d6ddSJeff Bonwick 	/*
10228ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
10238ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
10248ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
10258ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
10268ad4d6ddSJeff Bonwick 	 */
10278ad4d6ddSJeff Bonwick 	if (vd != NULL)
10288ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
10298ad4d6ddSJeff Bonwick 
1030c6065d0fSGeorge Wilson 	/*
1031c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1032c6065d0fSGeorge Wilson 	 */
1033c6065d0fSGeorge Wilson 	if (config_changed) {
1034c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1035c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1036c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1037c6065d0fSGeorge Wilson 	}
1038c6065d0fSGeorge Wilson 
1039e14bb325SJeff Bonwick 	return (error);
1040e14bb325SJeff Bonwick }
1041e14bb325SJeff Bonwick 
1042fa9e4066Sahrens /*
1043fa9e4066Sahrens  * ==========================================================================
1044fa9e4066Sahrens  * Miscellaneous functions
1045fa9e4066Sahrens  * ==========================================================================
1046fa9e4066Sahrens  */
1047fa9e4066Sahrens 
1048*ad135b5dSChristopher Siden void
1049*ad135b5dSChristopher Siden spa_activate_mos_feature(spa_t *spa, const char *feature)
1050*ad135b5dSChristopher Siden {
1051*ad135b5dSChristopher Siden 	(void) nvlist_add_boolean(spa->spa_label_features, feature);
1052*ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1053*ad135b5dSChristopher Siden }
1054*ad135b5dSChristopher Siden 
1055*ad135b5dSChristopher Siden void
1056*ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1057*ad135b5dSChristopher Siden {
1058*ad135b5dSChristopher Siden 	(void) nvlist_remove_all(spa->spa_label_features, feature);
1059*ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1060*ad135b5dSChristopher Siden }
1061*ad135b5dSChristopher Siden 
1062fa9e4066Sahrens /*
1063fa9e4066Sahrens  * Rename a spa_t.
1064fa9e4066Sahrens  */
1065fa9e4066Sahrens int
1066fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1067fa9e4066Sahrens {
1068fa9e4066Sahrens 	spa_t *spa;
1069fa9e4066Sahrens 	int err;
1070fa9e4066Sahrens 
1071fa9e4066Sahrens 	/*
1072fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1073fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1074fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1075ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1076fa9e4066Sahrens 	 */
1077fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1078fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1079fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1080fa9e4066Sahrens 		return (err);
1081fa9e4066Sahrens 	}
1082fa9e4066Sahrens 
1083e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1086e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1087fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1088fa9e4066Sahrens 
1089fa9e4066Sahrens 	/*
1090fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1091fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1092fa9e4066Sahrens 	 * during the sync.
1093fa9e4066Sahrens 	 */
1094fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1095fa9e4066Sahrens 
1096e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1097fa9e4066Sahrens 
10980373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1099fa9e4066Sahrens 
1100fa9e4066Sahrens 	/*
1101fa9e4066Sahrens 	 * Sync the updated config cache.
1102fa9e4066Sahrens 	 */
1103c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1104fa9e4066Sahrens 
1105fa9e4066Sahrens 	spa_close(spa, FTAG);
1106fa9e4066Sahrens 
1107fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1108fa9e4066Sahrens 
1109fa9e4066Sahrens 	return (0);
1110fa9e4066Sahrens }
1111fa9e4066Sahrens 
1112fa9e4066Sahrens /*
1113f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1114f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1115f9af39baSGeorge Wilson  * a device with the specified device_guid.
1116fa9e4066Sahrens  */
1117f9af39baSGeorge Wilson spa_t *
1118f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1119fa9e4066Sahrens {
1120fa9e4066Sahrens 	spa_t *spa;
1121fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1122fa9e4066Sahrens 
1123ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1124fa9e4066Sahrens 
1125fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1126fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1127fa9e4066Sahrens 			continue;
1128fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1129fa9e4066Sahrens 			continue;
113039c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
113139c23413Seschrock 			if (device_guid == 0)
113239c23413Seschrock 				break;
113339c23413Seschrock 
113439c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
113539c23413Seschrock 			    device_guid) != NULL)
113639c23413Seschrock 				break;
113739c23413Seschrock 
113839c23413Seschrock 			/*
11398654d025Sperrin 			 * Check any devices we may be in the process of adding.
114039c23413Seschrock 			 */
114139c23413Seschrock 			if (spa->spa_pending_vdev) {
114239c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
114339c23413Seschrock 				    device_guid) != NULL)
114439c23413Seschrock 					break;
114539c23413Seschrock 			}
114639c23413Seschrock 		}
1147fa9e4066Sahrens 	}
1148fa9e4066Sahrens 
1149f9af39baSGeorge Wilson 	return (spa);
1150f9af39baSGeorge Wilson }
1151f9af39baSGeorge Wilson 
1152f9af39baSGeorge Wilson /*
1153f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1154f9af39baSGeorge Wilson  */
1155f9af39baSGeorge Wilson boolean_t
1156f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1157f9af39baSGeorge Wilson {
1158f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1159fa9e4066Sahrens }
1160fa9e4066Sahrens 
1161fa9e4066Sahrens char *
1162fa9e4066Sahrens spa_strdup(const char *s)
1163fa9e4066Sahrens {
1164fa9e4066Sahrens 	size_t len;
1165fa9e4066Sahrens 	char *new;
1166fa9e4066Sahrens 
1167fa9e4066Sahrens 	len = strlen(s);
1168fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1169fa9e4066Sahrens 	bcopy(s, new, len);
1170fa9e4066Sahrens 	new[len] = '\0';
1171fa9e4066Sahrens 
1172fa9e4066Sahrens 	return (new);
1173fa9e4066Sahrens }
1174fa9e4066Sahrens 
1175fa9e4066Sahrens void
1176fa9e4066Sahrens spa_strfree(char *s)
1177fa9e4066Sahrens {
1178fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1179fa9e4066Sahrens }
1180fa9e4066Sahrens 
1181fa9e4066Sahrens uint64_t
1182fa9e4066Sahrens spa_get_random(uint64_t range)
1183fa9e4066Sahrens {
1184fa9e4066Sahrens 	uint64_t r;
1185fa9e4066Sahrens 
1186fa9e4066Sahrens 	ASSERT(range != 0);
1187fa9e4066Sahrens 
1188fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1189fa9e4066Sahrens 
1190fa9e4066Sahrens 	return (r % range);
1191fa9e4066Sahrens }
1192fa9e4066Sahrens 
11931195e687SMark J Musante uint64_t
11941195e687SMark J Musante spa_generate_guid(spa_t *spa)
11951195e687SMark J Musante {
11961195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
11971195e687SMark J Musante 
11981195e687SMark J Musante 	if (spa != NULL) {
11991195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
12001195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12011195e687SMark J Musante 	} else {
12021195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
12031195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12041195e687SMark J Musante 	}
12051195e687SMark J Musante 
12061195e687SMark J Musante 	return (guid);
12071195e687SMark J Musante }
12081195e687SMark J Musante 
1209fa9e4066Sahrens void
1210b24ab676SJeff Bonwick sprintf_blkptr(char *buf, const blkptr_t *bp)
1211fa9e4066Sahrens {
1212*ad135b5dSChristopher Siden 	char type[256];
1213f0ba89beSJeff Bonwick 	char *checksum = NULL;
1214f0ba89beSJeff Bonwick 	char *compress = NULL;
1215f0ba89beSJeff Bonwick 
1216f0ba89beSJeff Bonwick 	if (bp != NULL) {
1217*ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1218*ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1219*ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1220*ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1221*ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1222*ad135b5dSChristopher Siden 			    "metadata" : "data",
1223*ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1224*ad135b5dSChristopher Siden 		} else {
1225*ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1226*ad135b5dSChristopher Siden 			    sizeof (type));
1227*ad135b5dSChristopher Siden 		}
1228f0ba89beSJeff Bonwick 		checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1229f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1230f0ba89beSJeff Bonwick 	}
1231fa9e4066Sahrens 
1232b24ab676SJeff Bonwick 	SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1233fa9e4066Sahrens }
1234fa9e4066Sahrens 
1235fa9e4066Sahrens void
1236fa9e4066Sahrens spa_freeze(spa_t *spa)
1237fa9e4066Sahrens {
1238fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1239fa9e4066Sahrens 
1240e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1241fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1242fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1243fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1244fa9e4066Sahrens 	}
1245e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1246fa9e4066Sahrens 	if (freeze_txg != 0)
1247fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1248fa9e4066Sahrens }
1249fa9e4066Sahrens 
12500125049cSahrens void
12510125049cSahrens zfs_panic_recover(const char *fmt, ...)
12520125049cSahrens {
12530125049cSahrens 	va_list adx;
12540125049cSahrens 
12550125049cSahrens 	va_start(adx, fmt);
12560125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
12570125049cSahrens 	va_end(adx);
12580125049cSahrens }
12590125049cSahrens 
12603f9d6ad7SLin Ling /*
12613f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
12623f9d6ad7SLin Ling  * lowercase hexidecimal numbers that don't overflow.
12633f9d6ad7SLin Ling  */
12643f9d6ad7SLin Ling uint64_t
12653f9d6ad7SLin Ling strtonum(const char *str, char **nptr)
12663f9d6ad7SLin Ling {
12673f9d6ad7SLin Ling 	uint64_t val = 0;
12683f9d6ad7SLin Ling 	char c;
12693f9d6ad7SLin Ling 	int digit;
12703f9d6ad7SLin Ling 
12713f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
12723f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
12733f9d6ad7SLin Ling 			digit = c - '0';
12743f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
12753f9d6ad7SLin Ling 			digit = 10 + c - 'a';
12763f9d6ad7SLin Ling 		else
12773f9d6ad7SLin Ling 			break;
12783f9d6ad7SLin Ling 
12793f9d6ad7SLin Ling 		val *= 16;
12803f9d6ad7SLin Ling 		val += digit;
12813f9d6ad7SLin Ling 
12823f9d6ad7SLin Ling 		str++;
12833f9d6ad7SLin Ling 	}
12843f9d6ad7SLin Ling 
12853f9d6ad7SLin Ling 	if (nptr)
12863f9d6ad7SLin Ling 		*nptr = (char *)str;
12873f9d6ad7SLin Ling 
12883f9d6ad7SLin Ling 	return (val);
12893f9d6ad7SLin Ling }
12903f9d6ad7SLin Ling 
1291fa9e4066Sahrens /*
1292fa9e4066Sahrens  * ==========================================================================
1293fa9e4066Sahrens  * Accessor functions
1294fa9e4066Sahrens  * ==========================================================================
1295fa9e4066Sahrens  */
1296fa9e4066Sahrens 
1297088f3894Sahrens boolean_t
129888b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1299fa9e4066Sahrens {
130088b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1301fa9e4066Sahrens }
1302fa9e4066Sahrens 
1303fa9e4066Sahrens dsl_pool_t *
1304fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1305fa9e4066Sahrens {
1306fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1307fa9e4066Sahrens }
1308fa9e4066Sahrens 
1309*ad135b5dSChristopher Siden boolean_t
1310*ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1311*ad135b5dSChristopher Siden {
1312*ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1313*ad135b5dSChristopher Siden }
1314*ad135b5dSChristopher Siden 
1315fa9e4066Sahrens blkptr_t *
1316fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1317fa9e4066Sahrens {
1318fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1319fa9e4066Sahrens }
1320fa9e4066Sahrens 
1321fa9e4066Sahrens void
1322fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1323fa9e4066Sahrens {
1324fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1325fa9e4066Sahrens }
1326fa9e4066Sahrens 
1327fa9e4066Sahrens void
1328fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1329fa9e4066Sahrens {
1330fa9e4066Sahrens 	if (spa->spa_root == NULL)
1331fa9e4066Sahrens 		buf[0] = '\0';
1332fa9e4066Sahrens 	else
1333fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1334fa9e4066Sahrens }
1335fa9e4066Sahrens 
1336fa9e4066Sahrens int
1337fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1338fa9e4066Sahrens {
1339fa9e4066Sahrens 	return (spa->spa_sync_pass);
1340fa9e4066Sahrens }
1341fa9e4066Sahrens 
1342fa9e4066Sahrens char *
1343fa9e4066Sahrens spa_name(spa_t *spa)
1344fa9e4066Sahrens {
1345fa9e4066Sahrens 	return (spa->spa_name);
1346fa9e4066Sahrens }
1347fa9e4066Sahrens 
1348fa9e4066Sahrens uint64_t
1349fa9e4066Sahrens spa_guid(spa_t *spa)
1350fa9e4066Sahrens {
1351b5989ec7Seschrock 	/*
1352b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1353b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1354e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1355b5989ec7Seschrock 	 * this case.
1356b5989ec7Seschrock 	 */
1357b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
1358b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1359b5989ec7Seschrock 	else
1360e9103aaeSGarrett D'Amore 		return (spa->spa_config_guid);
1361e9103aaeSGarrett D'Amore }
1362e9103aaeSGarrett D'Amore 
1363e9103aaeSGarrett D'Amore uint64_t
1364e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1365e9103aaeSGarrett D'Amore {
1366e9103aaeSGarrett D'Amore 	/*
1367e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1368e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1369e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1370e9103aaeSGarrett D'Amore 	 */
1371e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1372fa9e4066Sahrens }
1373fa9e4066Sahrens 
1374fa9e4066Sahrens uint64_t
1375fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1376fa9e4066Sahrens {
1377fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1378fa9e4066Sahrens }
1379fa9e4066Sahrens 
1380fa9e4066Sahrens uint64_t
1381fa9e4066Sahrens spa_first_txg(spa_t *spa)
1382fa9e4066Sahrens {
1383fa9e4066Sahrens 	return (spa->spa_first_txg);
1384fa9e4066Sahrens }
1385fa9e4066Sahrens 
1386b24ab676SJeff Bonwick uint64_t
1387b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1388b24ab676SJeff Bonwick {
1389b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1390b24ab676SJeff Bonwick }
1391b24ab676SJeff Bonwick 
139288b7b0f2SMatthew Ahrens pool_state_t
1393fa9e4066Sahrens spa_state(spa_t *spa)
1394fa9e4066Sahrens {
1395fa9e4066Sahrens 	return (spa->spa_state);
1396fa9e4066Sahrens }
1397fa9e4066Sahrens 
1398b16da2e2SGeorge Wilson spa_load_state_t
1399b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1400b16da2e2SGeorge Wilson {
1401b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1402b16da2e2SGeorge Wilson }
1403b16da2e2SGeorge Wilson 
1404fa9e4066Sahrens uint64_t
1405fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1406fa9e4066Sahrens {
1407fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1408fa9e4066Sahrens }
1409fa9e4066Sahrens 
1410fa9e4066Sahrens /* ARGSUSED */
1411fa9e4066Sahrens uint64_t
1412fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1413fa9e4066Sahrens {
1414fa9e4066Sahrens 	/*
1415b24ab676SJeff Bonwick 	 * The worst case is single-sector max-parity RAID-Z blocks, in which
1416b24ab676SJeff Bonwick 	 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1417b24ab676SJeff Bonwick 	 * times the size; so just assume that.  Add to this the fact that
1418b24ab676SJeff Bonwick 	 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1419b24ab676SJeff Bonwick 	 * the block may be dittoed with up to 3 DVAs by ddt_sync().
142044cd46caSbillm 	 */
1421b24ab676SJeff Bonwick 	return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
142244cd46caSbillm }
142344cd46caSbillm 
1424485bbbf5SGeorge Wilson uint64_t
1425485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1426485bbbf5SGeorge Wilson {
1427485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1428485bbbf5SGeorge Wilson }
1429485bbbf5SGeorge Wilson 
1430485bbbf5SGeorge Wilson void
1431485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1432485bbbf5SGeorge Wilson {
1433485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1434485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1435485bbbf5SGeorge Wilson }
1436485bbbf5SGeorge Wilson 
14370a4e9518Sgw /*
14380a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
14390a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
14400a4e9518Sgw  */
14410a4e9518Sgw uint8_t
14420a4e9518Sgw spa_get_failmode(spa_t *spa)
14430a4e9518Sgw {
14440a4e9518Sgw 	return (spa->spa_failmode);
14450a4e9518Sgw }
14460a4e9518Sgw 
1447e14bb325SJeff Bonwick boolean_t
1448e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1449e14bb325SJeff Bonwick {
1450e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1451e14bb325SJeff Bonwick }
1452e14bb325SJeff Bonwick 
145344cd46caSbillm uint64_t
145444cd46caSbillm spa_version(spa_t *spa)
145544cd46caSbillm {
145644cd46caSbillm 	return (spa->spa_ubsync.ub_version);
145744cd46caSbillm }
145844cd46caSbillm 
1459b24ab676SJeff Bonwick boolean_t
1460b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1461b24ab676SJeff Bonwick {
1462b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1463b24ab676SJeff Bonwick }
1464b24ab676SJeff Bonwick 
1465b24ab676SJeff Bonwick metaslab_class_t *
1466b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1467b24ab676SJeff Bonwick {
1468b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1469b24ab676SJeff Bonwick }
1470b24ab676SJeff Bonwick 
1471b24ab676SJeff Bonwick metaslab_class_t *
1472b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1473b24ab676SJeff Bonwick {
1474b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1475b24ab676SJeff Bonwick }
1476b24ab676SJeff Bonwick 
147744cd46caSbillm int
147844cd46caSbillm spa_max_replication(spa_t *spa)
147944cd46caSbillm {
148044cd46caSbillm 	/*
1481e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
148244cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
148344cd46caSbillm 	 * replication level accordingly.
1484fa9e4066Sahrens 	 */
1485e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
148644cd46caSbillm 		return (1);
148744cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1488fa9e4066Sahrens }
1489fa9e4066Sahrens 
14903f9d6ad7SLin Ling int
14913f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
14923f9d6ad7SLin Ling {
14933f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
14943f9d6ad7SLin Ling }
14953f9d6ad7SLin Ling 
149699653d4eSeschrock uint64_t
1497b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
149899653d4eSeschrock {
1499b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1500b24ab676SJeff Bonwick 	uint64_t dsize = asize;
150199653d4eSeschrock 
1502b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
150399653d4eSeschrock 
1504b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1505b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1506b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
150799653d4eSeschrock 	}
1508b24ab676SJeff Bonwick 
1509b24ab676SJeff Bonwick 	return (dsize);
1510b24ab676SJeff Bonwick }
1511b24ab676SJeff Bonwick 
1512b24ab676SJeff Bonwick uint64_t
1513b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1514b24ab676SJeff Bonwick {
1515b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1516b24ab676SJeff Bonwick 
1517b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1518b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1519b24ab676SJeff Bonwick 
1520b24ab676SJeff Bonwick 	return (dsize);
1521b24ab676SJeff Bonwick }
1522b24ab676SJeff Bonwick 
1523b24ab676SJeff Bonwick uint64_t
1524b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1525b24ab676SJeff Bonwick {
1526b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1527b24ab676SJeff Bonwick 
1528b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1529b24ab676SJeff Bonwick 
1530b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1531b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1532b24ab676SJeff Bonwick 
1533e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1534b24ab676SJeff Bonwick 
1535b24ab676SJeff Bonwick 	return (dsize);
153699653d4eSeschrock }
153799653d4eSeschrock 
1538fa9e4066Sahrens /*
1539fa9e4066Sahrens  * ==========================================================================
1540fa9e4066Sahrens  * Initialization and Termination
1541fa9e4066Sahrens  * ==========================================================================
1542fa9e4066Sahrens  */
1543fa9e4066Sahrens 
1544fa9e4066Sahrens static int
1545fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1546fa9e4066Sahrens {
1547fa9e4066Sahrens 	const spa_t *s1 = a1;
1548fa9e4066Sahrens 	const spa_t *s2 = a2;
1549fa9e4066Sahrens 	int s;
1550fa9e4066Sahrens 
1551fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1552fa9e4066Sahrens 	if (s > 0)
1553fa9e4066Sahrens 		return (1);
1554fa9e4066Sahrens 	if (s < 0)
1555fa9e4066Sahrens 		return (-1);
1556fa9e4066Sahrens 	return (0);
1557fa9e4066Sahrens }
1558fa9e4066Sahrens 
15590373e76bSbonwick int
15600373e76bSbonwick spa_busy(void)
15610373e76bSbonwick {
15620373e76bSbonwick 	return (spa_active_count);
15630373e76bSbonwick }
15640373e76bSbonwick 
1565e7cbe64fSgw void
1566e7cbe64fSgw spa_boot_init()
1567e7cbe64fSgw {
1568e7cbe64fSgw 	spa_config_load();
1569e7cbe64fSgw }
1570e7cbe64fSgw 
1571fa9e4066Sahrens void
1572fa9e4066Sahrens spa_init(int mode)
1573fa9e4066Sahrens {
1574fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1575c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1576fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1577fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1578fa9e4066Sahrens 
1579fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1580fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1581fa9e4066Sahrens 
1582fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1583fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1584fa94a07fSbrendan 
1585fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1586fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
158799653d4eSeschrock 
15888ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1589fa9e4066Sahrens 
1590fa9e4066Sahrens 	refcount_init();
1591fa9e4066Sahrens 	unique_init();
1592fa9e4066Sahrens 	zio_init();
1593fa9e4066Sahrens 	dmu_init();
1594fa9e4066Sahrens 	zil_init();
159587db74c1Sek 	vdev_cache_stat_init();
159691ebeef5Sahrens 	zfs_prop_init();
1597990b4856Slling 	zpool_prop_init();
1598*ad135b5dSChristopher Siden 	zpool_feature_init();
1599fa9e4066Sahrens 	spa_config_load();
1600e14bb325SJeff Bonwick 	l2arc_start();
1601fa9e4066Sahrens }
1602fa9e4066Sahrens 
1603fa9e4066Sahrens void
1604fa9e4066Sahrens spa_fini(void)
1605fa9e4066Sahrens {
1606e14bb325SJeff Bonwick 	l2arc_stop();
1607e14bb325SJeff Bonwick 
1608fa9e4066Sahrens 	spa_evict_all();
1609fa9e4066Sahrens 
161087db74c1Sek 	vdev_cache_stat_fini();
1611fa9e4066Sahrens 	zil_fini();
1612fa9e4066Sahrens 	dmu_fini();
1613fa9e4066Sahrens 	zio_fini();
161491ebeef5Sahrens 	unique_fini();
1615fa9e4066Sahrens 	refcount_fini();
1616fa9e4066Sahrens 
1617fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
161899653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1619fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1620fa9e4066Sahrens 
1621fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1622fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1623c25056deSgw 	mutex_destroy(&spa_spare_lock);
1624fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1625fa9e4066Sahrens }
16266ce0521aSperrin 
16276ce0521aSperrin /*
16286ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
16296ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
16306ce0521aSperrin  * performance and not correctness
16316ce0521aSperrin  */
16326ce0521aSperrin boolean_t
16336ce0521aSperrin spa_has_slogs(spa_t *spa)
16346ce0521aSperrin {
16356ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
16366ce0521aSperrin }
1637bf82a41bSeschrock 
1638b24ab676SJeff Bonwick spa_log_state_t
1639b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1640b24ab676SJeff Bonwick {
1641b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1642b24ab676SJeff Bonwick }
1643b24ab676SJeff Bonwick 
1644b24ab676SJeff Bonwick void
1645b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1646b24ab676SJeff Bonwick {
1647b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1648b24ab676SJeff Bonwick }
1649b24ab676SJeff Bonwick 
1650bf82a41bSeschrock boolean_t
1651bf82a41bSeschrock spa_is_root(spa_t *spa)
1652bf82a41bSeschrock {
1653bf82a41bSeschrock 	return (spa->spa_is_root);
1654bf82a41bSeschrock }
16558ad4d6ddSJeff Bonwick 
16568ad4d6ddSJeff Bonwick boolean_t
16578ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
16588ad4d6ddSJeff Bonwick {
16598ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
16608ad4d6ddSJeff Bonwick }
16618ad4d6ddSJeff Bonwick 
16628ad4d6ddSJeff Bonwick int
16638ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
16648ad4d6ddSJeff Bonwick {
16658ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
16668ad4d6ddSJeff Bonwick }
1667b24ab676SJeff Bonwick 
1668b24ab676SJeff Bonwick uint64_t
1669b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1670b24ab676SJeff Bonwick {
1671b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1672b24ab676SJeff Bonwick }
1673b24ab676SJeff Bonwick 
1674b24ab676SJeff Bonwick uint64_t
1675b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1676b24ab676SJeff Bonwick {
1677b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1678b24ab676SJeff Bonwick }
1679b24ab676SJeff Bonwick 
1680b24ab676SJeff Bonwick objset_t *
1681b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1682b24ab676SJeff Bonwick {
1683b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1684b24ab676SJeff Bonwick }
1685b24ab676SJeff Bonwick 
1686b24ab676SJeff Bonwick enum zio_checksum
1687b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1688b24ab676SJeff Bonwick {
1689b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1690b24ab676SJeff Bonwick }
16913f9d6ad7SLin Ling 
16923f9d6ad7SLin Ling /*
16933f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
16943f9d6ad7SLin Ling  */
16953f9d6ad7SLin Ling void
16963f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
16973f9d6ad7SLin Ling {
16983f9d6ad7SLin Ling 	/* data not stored on disk */
16993f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
17003f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
17013f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
17023f9d6ad7SLin Ling }
17033f9d6ad7SLin Ling 
17043f9d6ad7SLin Ling /*
17053f9d6ad7SLin Ling  * Get scan stats for zpool status reports
17063f9d6ad7SLin Ling  */
17073f9d6ad7SLin Ling int
17083f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
17093f9d6ad7SLin Ling {
17103f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
17113f9d6ad7SLin Ling 
17123f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
17133f9d6ad7SLin Ling 		return (ENOENT);
17143f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
17153f9d6ad7SLin Ling 
17163f9d6ad7SLin Ling 	/* data stored on disk */
17173f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
17183f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
17193f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
17203f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
17213f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
17223f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
17233f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
17243f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
17253f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
17263f9d6ad7SLin Ling 
17273f9d6ad7SLin Ling 	/* data not stored on disk */
17283f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
17293f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
17303f9d6ad7SLin Ling 
17313f9d6ad7SLin Ling 	return (0);
17323f9d6ad7SLin Ling }
173309c9d376SGeorge Wilson 
173409c9d376SGeorge Wilson boolean_t
173509c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
173609c9d376SGeorge Wilson {
173709c9d376SGeorge Wilson 	return (spa->spa_debug);
173809c9d376SGeorge Wilson }
1739