xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision b24ab6762772a3f6a89393947930c7fa61306783)
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 /*
2254d692b7SGeorge Wilson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/zfs_context.h>
27fa9e4066Sahrens #include <sys/spa_impl.h>
28fa9e4066Sahrens #include <sys/zio.h>
29fa9e4066Sahrens #include <sys/zio_checksum.h>
30fa9e4066Sahrens #include <sys/zio_compress.h>
31fa9e4066Sahrens #include <sys/dmu.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/zap.h>
34fa9e4066Sahrens #include <sys/zil.h>
35fa9e4066Sahrens #include <sys/vdev_impl.h>
36fa9e4066Sahrens #include <sys/metaslab.h>
37fa9e4066Sahrens #include <sys/uberblock_impl.h>
38fa9e4066Sahrens #include <sys/txg.h>
39fa9e4066Sahrens #include <sys/avl.h>
40fa9e4066Sahrens #include <sys/unique.h>
41fa9e4066Sahrens #include <sys/dsl_pool.h>
42fa9e4066Sahrens #include <sys/dsl_dir.h>
43fa9e4066Sahrens #include <sys/dsl_prop.h>
44fa9e4066Sahrens #include <sys/fs/zfs.h>
456ce0521aSperrin #include <sys/metaslab_impl.h>
46e14bb325SJeff Bonwick #include <sys/sunddi.h>
47e14bb325SJeff Bonwick #include <sys/arc.h>
4891ebeef5Sahrens #include "zfs_prop.h"
49fa9e4066Sahrens 
50fa9e4066Sahrens /*
51fa9e4066Sahrens  * SPA locking
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
54fa9e4066Sahrens  *
55fa9e4066Sahrens  * spa_namespace_lock (global mutex)
56fa9e4066Sahrens  *
5744cd46caSbillm  *	This lock must be acquired to do any of the following:
58fa9e4066Sahrens  *
5944cd46caSbillm  *		- Lookup a spa_t by name
6044cd46caSbillm  *		- Add or remove a spa_t from the namespace
6144cd46caSbillm  *		- Increase spa_refcount from non-zero
6244cd46caSbillm  *		- Check if spa_refcount is zero
6344cd46caSbillm  *		- Rename a spa_t
64ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
6544cd46caSbillm  *		- Held for the duration of create/destroy/import/export
66fa9e4066Sahrens  *
6744cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
6844cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
6944cd46caSbillm  *	definition they must have an existing reference, and will never need
7044cd46caSbillm  *	to lookup a spa_t by name.
71fa9e4066Sahrens  *
72fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
73fa9e4066Sahrens  *
7444cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
7544cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7644cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
77088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
7844cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
79fa9e4066Sahrens  *
80e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
81fa9e4066Sahrens  *
8291ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
8391ebeef5Sahrens  *	the following circumstances:
84fa9e4066Sahrens  *
8544cd46caSbillm  *		- RW_READER to perform I/O to the spa
8644cd46caSbillm  *		- RW_WRITER to change the vdev config
87fa9e4066Sahrens  *
88fa9e4066Sahrens  * The locking order is fairly straightforward:
89fa9e4066Sahrens  *
9044cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
91fa9e4066Sahrens  *
9244cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
9344cd46caSbillm  *	or to check if it is zero.
94fa9e4066Sahrens  *
95e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
96fa9e4066Sahrens  *
9744cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
9844cd46caSbillm  *	the config lock.
99fa9e4066Sahrens  *
100e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
101fa9e4066Sahrens  *
10244cd46caSbillm  *	The namespace lock must always be taken before the config lock.
103fa9e4066Sahrens  *
104fa9e4066Sahrens  *
105e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
106fa9e4066Sahrens  *
107e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
108e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
109fa9e4066Sahrens  *
11044cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
111fa9e4066Sahrens  *
11244cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
113fa9e4066Sahrens  *
11444cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
11544cd46caSbillm  *				frees up any memory associated with the spa_t.
116fa9e4066Sahrens  *
11744cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
11844cd46caSbillm  *				first if NULL is passed.
119fa9e4066Sahrens  *
12044cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12144cd46caSbillm  *				the system.
122fa9e4066Sahrens  *
123ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
124fa9e4066Sahrens  *
125fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
126fa9e4066Sahrens  *
12744cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
12844cd46caSbillm  *				called with spa_namespace_lock held if the
12944cd46caSbillm  *				refcount is currently zero.
130fa9e4066Sahrens  *
13144cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13244cd46caSbillm  *				not free the spa_t or remove it from the
13344cd46caSbillm  *				namespace.  No locking is required.
134fa9e4066Sahrens  *
13544cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
13644cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
13744cd46caSbillm  *				held.
138fa9e4066Sahrens  *
139e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
140e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
141e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
142e14bb325SJeff Bonwick  *
143e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
144e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
145e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
146e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
147e14bb325SJeff Bonwick  *
148e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
149e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
150e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
151e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
152e14bb325SJeff Bonwick  *
153e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
154e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
155e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
156e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
157e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
158e14bb325SJeff Bonwick  *
159e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
160e14bb325SJeff Bonwick  *
161e14bb325SJeff Bonwick  * SCL_CONFIG
162e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
163e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
164e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
165e14bb325SJeff Bonwick  *
166e14bb325SJeff Bonwick  * SCL_STATE
167e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
168e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
169e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
170e14bb325SJeff Bonwick  *
171e14bb325SJeff Bonwick  * SCL_ALLOC
172e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
173e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
174e14bb325SJeff Bonwick  *
175e14bb325SJeff Bonwick  * SCL_ZIO
176e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
177e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
178e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
179e14bb325SJeff Bonwick  *
180e14bb325SJeff Bonwick  * SCL_FREE
181e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
182e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
183e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
184e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
185e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
186e14bb325SJeff Bonwick  *
187e14bb325SJeff Bonwick  * SCL_VDEV
188e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
189*b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
190e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
191e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
192e14bb325SJeff Bonwick  *
193e14bb325SJeff Bonwick  * In addition, the following rules apply:
194e14bb325SJeff Bonwick  *
195e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
196e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
197e14bb325SJeff Bonwick  *
198e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
199e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
200e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
201e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
202e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
203fa9e4066Sahrens  *
204ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
205fa9e4066Sahrens  *
20644cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
207ea8dc4b6Seschrock  *				for writing.
208fa9e4066Sahrens  *
20944cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21044cd46caSbillm  *				to complete, sync the updated configs to the
211ea8dc4b6Seschrock  *				cache, and release the namespace lock.
212fa9e4066Sahrens  *
213e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
214e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
215e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
216e14bb325SJeff Bonwick  *
217e14bb325SJeff Bonwick  * spa_rename() is also implemented within this file since is requires
218e14bb325SJeff Bonwick  * manipulation of the namespace.
219fa9e4066Sahrens  */
220fa9e4066Sahrens 
221fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
222fa9e4066Sahrens kmutex_t spa_namespace_lock;
223fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2240373e76bSbonwick static int spa_active_count;
225416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
226fa9e4066Sahrens 
22799653d4eSeschrock static kmutex_t spa_spare_lock;
22839c23413Seschrock static avl_tree_t spa_spare_avl;
229fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
230fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23199653d4eSeschrock 
232fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2338ad4d6ddSJeff Bonwick int spa_mode_global;
234fa9e4066Sahrens 
235fa9e4066Sahrens #ifdef ZFS_DEBUG
23640feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
23740feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
238fa9e4066Sahrens #else
239fa9e4066Sahrens int zfs_flags = 0;
240fa9e4066Sahrens #endif
241fa9e4066Sahrens 
2420125049cSahrens /*
2430125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2440125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2450125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2460125049cSahrens  */
2470125049cSahrens int zfs_recover = 0;
2480125049cSahrens 
249fa9e4066Sahrens 
250e05725b1Sbonwick /*
251e05725b1Sbonwick  * ==========================================================================
252e05725b1Sbonwick  * SPA config locking
253e05725b1Sbonwick  * ==========================================================================
254e05725b1Sbonwick  */
255e05725b1Sbonwick static void
256e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
257e14bb325SJeff Bonwick {
258e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
259e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
260e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
261e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
262e14bb325SJeff Bonwick 		refcount_create(&scl->scl_count);
263e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
264e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
265e14bb325SJeff Bonwick 	}
266e05725b1Sbonwick }
267e05725b1Sbonwick 
268e05725b1Sbonwick static void
269e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
270e14bb325SJeff Bonwick {
271e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
272e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
273e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
274e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
275e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
276e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
277e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
278e14bb325SJeff Bonwick 	}
279e14bb325SJeff Bonwick }
280e14bb325SJeff Bonwick 
281e14bb325SJeff Bonwick int
282e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
283e05725b1Sbonwick {
284e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
285e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
286e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
287e14bb325SJeff Bonwick 			continue;
288e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
289e14bb325SJeff Bonwick 		if (rw == RW_READER) {
290e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
291e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
292e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
293e14bb325SJeff Bonwick 				return (0);
294e14bb325SJeff Bonwick 			}
295e14bb325SJeff Bonwick 		} else {
296e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
297e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
298e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
299e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
300e14bb325SJeff Bonwick 				return (0);
301e14bb325SJeff Bonwick 			}
302e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
303e14bb325SJeff Bonwick 		}
304e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
305e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
306e14bb325SJeff Bonwick 	}
307e14bb325SJeff Bonwick 	return (1);
308e05725b1Sbonwick }
309e05725b1Sbonwick 
310e05725b1Sbonwick void
311e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
312e05725b1Sbonwick {
313f64c0e34SEric Taylor 	int wlocks_held = 0;
314f64c0e34SEric Taylor 
315e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
316e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
317f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
318f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
319e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
320e14bb325SJeff Bonwick 			continue;
321e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
322e14bb325SJeff Bonwick 		if (rw == RW_READER) {
323e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
324e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
325e14bb325SJeff Bonwick 			}
326e14bb325SJeff Bonwick 		} else {
327e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
328e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
329e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
330e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
331e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
332e14bb325SJeff Bonwick 			}
333e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
334e14bb325SJeff Bonwick 		}
335e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
336e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
337e05725b1Sbonwick 	}
338f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
339e05725b1Sbonwick }
340e05725b1Sbonwick 
341e05725b1Sbonwick void
342e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
343e05725b1Sbonwick {
344e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
345e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
346e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
347e14bb325SJeff Bonwick 			continue;
348e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
349e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
350e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
351e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
352e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
353e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
354e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
355e14bb325SJeff Bonwick 		}
356e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
357e05725b1Sbonwick 	}
358e05725b1Sbonwick }
359e05725b1Sbonwick 
360e14bb325SJeff Bonwick int
361e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
362e05725b1Sbonwick {
363e14bb325SJeff Bonwick 	int locks_held = 0;
364e05725b1Sbonwick 
365e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
366e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
367e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
368e14bb325SJeff Bonwick 			continue;
369e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
370e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
371e14bb325SJeff Bonwick 			locks_held |= 1 << i;
372e14bb325SJeff Bonwick 	}
373e14bb325SJeff Bonwick 
374e14bb325SJeff Bonwick 	return (locks_held);
375e05725b1Sbonwick }
376e05725b1Sbonwick 
377fa9e4066Sahrens /*
378fa9e4066Sahrens  * ==========================================================================
379fa9e4066Sahrens  * SPA namespace functions
380fa9e4066Sahrens  * ==========================================================================
381fa9e4066Sahrens  */
382fa9e4066Sahrens 
383fa9e4066Sahrens /*
384fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
385fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
386fa9e4066Sahrens  */
387fa9e4066Sahrens spa_t *
388fa9e4066Sahrens spa_lookup(const char *name)
389fa9e4066Sahrens {
390e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
391e14bb325SJeff Bonwick 	spa_t *spa;
392fa9e4066Sahrens 	avl_index_t where;
39340feaa91Sahrens 	char c;
39440feaa91Sahrens 	char *cp;
395fa9e4066Sahrens 
396fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
397fa9e4066Sahrens 
39840feaa91Sahrens 	/*
39940feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
40040feaa91Sahrens 	 * just use that.
40140feaa91Sahrens 	 */
40240feaa91Sahrens 	cp = strpbrk(name, "/@");
40340feaa91Sahrens 	if (cp) {
40440feaa91Sahrens 		c = *cp;
40540feaa91Sahrens 		*cp = '\0';
40640feaa91Sahrens 	}
40740feaa91Sahrens 
408e14bb325SJeff Bonwick 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
409fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
410fa9e4066Sahrens 
41140feaa91Sahrens 	if (cp)
41240feaa91Sahrens 		*cp = c;
41340feaa91Sahrens 
414fa9e4066Sahrens 	return (spa);
415fa9e4066Sahrens }
416fa9e4066Sahrens 
417fa9e4066Sahrens /*
418fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
419fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
420fa9e4066Sahrens  * exist by calling spa_lookup() first.
421fa9e4066Sahrens  */
422fa9e4066Sahrens spa_t *
423468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
424fa9e4066Sahrens {
425fa9e4066Sahrens 	spa_t *spa;
426c5904d13Seschrock 	spa_config_dirent_t *dp;
427fa9e4066Sahrens 
428fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
429fa9e4066Sahrens 
430fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
431fa9e4066Sahrens 
432c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
433c25056deSgw 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
434c25056deSgw 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
435c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
436c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
437c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
438c25056deSgw 
439c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
440c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
441e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
442c25056deSgw 
443*b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
444*b24ab676SJeff Bonwick 		bplist_init(&spa->spa_free_bplist[t]);
445*b24ab676SJeff Bonwick 	bplist_init(&spa->spa_deferred_bplist);
446*b24ab676SJeff Bonwick 
447e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
448fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
449fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
4500373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
451468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
452fa9e4066Sahrens 
453fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
454e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
455fa9e4066Sahrens 
456fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
457fa9e4066Sahrens 
458e14bb325SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
4590a4e9518Sgw 
4600373e76bSbonwick 	/*
4610373e76bSbonwick 	 * Set the alternate root, if there is one.
4620373e76bSbonwick 	 */
4630373e76bSbonwick 	if (altroot) {
4640373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
4650373e76bSbonwick 		spa_active_count++;
4660373e76bSbonwick 	}
4670373e76bSbonwick 
468c5904d13Seschrock 	/*
469c5904d13Seschrock 	 * Every pool starts with the default cachefile
470c5904d13Seschrock 	 */
471c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
472c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
473c5904d13Seschrock 
474c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
475c5904d13Seschrock 	dp->scd_path = spa_strdup(spa_config_path);
476c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
477c5904d13Seschrock 
478468c413aSTim Haley 	if (config != NULL)
479468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
480468c413aSTim Haley 
481fa9e4066Sahrens 	return (spa);
482fa9e4066Sahrens }
483fa9e4066Sahrens 
484fa9e4066Sahrens /*
485fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
486fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
487fa9e4066Sahrens  * deactivated.
488fa9e4066Sahrens  */
489fa9e4066Sahrens void
490fa9e4066Sahrens spa_remove(spa_t *spa)
491fa9e4066Sahrens {
492c5904d13Seschrock 	spa_config_dirent_t *dp;
493c5904d13Seschrock 
494fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
495fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
496fa9e4066Sahrens 
497fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
498fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
499fa9e4066Sahrens 
5000373e76bSbonwick 	if (spa->spa_root) {
501fa9e4066Sahrens 		spa_strfree(spa->spa_root);
5020373e76bSbonwick 		spa_active_count--;
5030373e76bSbonwick 	}
504fa9e4066Sahrens 
505c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
506c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
507c5904d13Seschrock 		if (dp->scd_path != NULL)
508c5904d13Seschrock 			spa_strfree(dp->scd_path);
509c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
510c5904d13Seschrock 	}
511c5904d13Seschrock 
512c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
5132f8aaab3Seschrock 
514fa9e4066Sahrens 	spa_config_set(spa, NULL);
515fa9e4066Sahrens 
516fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
51791ebeef5Sahrens 
518e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
519fa9e4066Sahrens 
520*b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
521*b24ab676SJeff Bonwick 		bplist_fini(&spa->spa_free_bplist[t]);
522*b24ab676SJeff Bonwick 	bplist_fini(&spa->spa_deferred_bplist);
523*b24ab676SJeff Bonwick 
524c25056deSgw 	cv_destroy(&spa->spa_async_cv);
525c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
526e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
527c25056deSgw 
5285ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
529c25056deSgw 	mutex_destroy(&spa->spa_scrub_lock);
530c25056deSgw 	mutex_destroy(&spa->spa_errlog_lock);
531c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
53206eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
533b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
534e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
5355ad82045Snd 
536fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
537fa9e4066Sahrens }
538fa9e4066Sahrens 
539fa9e4066Sahrens /*
540fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
541fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
542fa9e4066Sahrens  */
543fa9e4066Sahrens spa_t *
544fa9e4066Sahrens spa_next(spa_t *prev)
545fa9e4066Sahrens {
546fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
547fa9e4066Sahrens 
548fa9e4066Sahrens 	if (prev)
549fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
550fa9e4066Sahrens 	else
551fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
552fa9e4066Sahrens }
553fa9e4066Sahrens 
554fa9e4066Sahrens /*
555fa9e4066Sahrens  * ==========================================================================
556fa9e4066Sahrens  * SPA refcount functions
557fa9e4066Sahrens  * ==========================================================================
558fa9e4066Sahrens  */
559fa9e4066Sahrens 
560fa9e4066Sahrens /*
561fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
562fa9e4066Sahrens  * have the namespace lock held.
563fa9e4066Sahrens  */
564fa9e4066Sahrens void
565fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
566fa9e4066Sahrens {
567088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
568fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
569fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
570fa9e4066Sahrens }
571fa9e4066Sahrens 
572fa9e4066Sahrens /*
573fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
574fa9e4066Sahrens  * have the namespace lock held.
575fa9e4066Sahrens  */
576fa9e4066Sahrens void
577fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
578fa9e4066Sahrens {
579088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
580fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
581fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
582fa9e4066Sahrens }
583fa9e4066Sahrens 
584fa9e4066Sahrens /*
585fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
586088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
587fa9e4066Sahrens  * number of references acquired when opening a pool
588fa9e4066Sahrens  */
589fa9e4066Sahrens boolean_t
590fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
591fa9e4066Sahrens {
592fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
593fa9e4066Sahrens 
594088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
595fa9e4066Sahrens }
596fa9e4066Sahrens 
59799653d4eSeschrock /*
59899653d4eSeschrock  * ==========================================================================
599fa94a07fSbrendan  * SPA spare and l2cache tracking
60099653d4eSeschrock  * ==========================================================================
60199653d4eSeschrock  */
60299653d4eSeschrock 
603fa94a07fSbrendan /*
604fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
605fa94a07fSbrendan  * for 'auxiliary' devices.
606fa94a07fSbrendan  */
607fa94a07fSbrendan 
608fa94a07fSbrendan typedef struct spa_aux {
609fa94a07fSbrendan 	uint64_t	aux_guid;
610fa94a07fSbrendan 	uint64_t	aux_pool;
611fa94a07fSbrendan 	avl_node_t	aux_avl;
612fa94a07fSbrendan 	int		aux_count;
613fa94a07fSbrendan } spa_aux_t;
614fa94a07fSbrendan 
615fa94a07fSbrendan static int
616fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
617fa94a07fSbrendan {
618fa94a07fSbrendan 	const spa_aux_t *sa = a;
619fa94a07fSbrendan 	const spa_aux_t *sb = b;
620fa94a07fSbrendan 
621fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
622fa94a07fSbrendan 		return (-1);
623fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
624fa94a07fSbrendan 		return (1);
625fa94a07fSbrendan 	else
626fa94a07fSbrendan 		return (0);
627fa94a07fSbrendan }
628fa94a07fSbrendan 
629fa94a07fSbrendan void
630fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
631fa94a07fSbrendan {
632fa94a07fSbrendan 	avl_index_t where;
633fa94a07fSbrendan 	spa_aux_t search;
634fa94a07fSbrendan 	spa_aux_t *aux;
635fa94a07fSbrendan 
636fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
637fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
638fa94a07fSbrendan 		aux->aux_count++;
639fa94a07fSbrendan 	} else {
640fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
641fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
642fa94a07fSbrendan 		aux->aux_count = 1;
643fa94a07fSbrendan 		avl_insert(avl, aux, where);
644fa94a07fSbrendan 	}
645fa94a07fSbrendan }
646fa94a07fSbrendan 
647fa94a07fSbrendan void
648fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
649fa94a07fSbrendan {
650fa94a07fSbrendan 	spa_aux_t search;
651fa94a07fSbrendan 	spa_aux_t *aux;
652fa94a07fSbrendan 	avl_index_t where;
653fa94a07fSbrendan 
654fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
655fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
656fa94a07fSbrendan 
657fa94a07fSbrendan 	ASSERT(aux != NULL);
658fa94a07fSbrendan 
659fa94a07fSbrendan 	if (--aux->aux_count == 0) {
660fa94a07fSbrendan 		avl_remove(avl, aux);
661fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
662fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
663fa94a07fSbrendan 		aux->aux_pool = 0ULL;
664fa94a07fSbrendan 	}
665fa94a07fSbrendan }
666fa94a07fSbrendan 
667fa94a07fSbrendan boolean_t
66889a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
669fa94a07fSbrendan {
670fa94a07fSbrendan 	spa_aux_t search, *found;
671fa94a07fSbrendan 
672fa94a07fSbrendan 	search.aux_guid = guid;
67389a89ebfSlling 	found = avl_find(avl, &search, NULL);
674fa94a07fSbrendan 
675fa94a07fSbrendan 	if (pool) {
676fa94a07fSbrendan 		if (found)
677fa94a07fSbrendan 			*pool = found->aux_pool;
678fa94a07fSbrendan 		else
679fa94a07fSbrendan 			*pool = 0ULL;
680fa94a07fSbrendan 	}
681fa94a07fSbrendan 
68289a89ebfSlling 	if (refcnt) {
68389a89ebfSlling 		if (found)
68489a89ebfSlling 			*refcnt = found->aux_count;
68589a89ebfSlling 		else
68689a89ebfSlling 			*refcnt = 0;
68789a89ebfSlling 	}
68889a89ebfSlling 
689fa94a07fSbrendan 	return (found != NULL);
690fa94a07fSbrendan }
691fa94a07fSbrendan 
692fa94a07fSbrendan void
693fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
694fa94a07fSbrendan {
695fa94a07fSbrendan 	spa_aux_t search, *found;
696fa94a07fSbrendan 	avl_index_t where;
697fa94a07fSbrendan 
698fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
699fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
700fa94a07fSbrendan 	ASSERT(found != NULL);
701fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
702fa94a07fSbrendan 
703fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
704fa94a07fSbrendan }
705fa94a07fSbrendan 
70699653d4eSeschrock /*
70739c23413Seschrock  * Spares are tracked globally due to the following constraints:
70839c23413Seschrock  *
70939c23413Seschrock  * 	- A spare may be part of multiple pools.
71039c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
71139c23413Seschrock  *	  another pool.
71239c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
71339c23413Seschrock  *	  the target is a spare in the same pool.
71439c23413Seschrock  *
71539c23413Seschrock  * We keep track of all spares on the system through the use of a reference
71639c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
71739c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
71839c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
71939c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
72039c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
72139c23413Seschrock  *
72239c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
72339c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
72439c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
72539c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
72699653d4eSeschrock  */
72739c23413Seschrock 
72899653d4eSeschrock static int
72999653d4eSeschrock spa_spare_compare(const void *a, const void *b)
73099653d4eSeschrock {
731fa94a07fSbrendan 	return (spa_aux_compare(a, b));
73299653d4eSeschrock }
73399653d4eSeschrock 
73499653d4eSeschrock void
73539c23413Seschrock spa_spare_add(vdev_t *vd)
73699653d4eSeschrock {
73799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
73839c23413Seschrock 	ASSERT(!vd->vdev_isspare);
739fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
74039c23413Seschrock 	vd->vdev_isspare = B_TRUE;
74199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
74299653d4eSeschrock }
74399653d4eSeschrock 
74499653d4eSeschrock void
74539c23413Seschrock spa_spare_remove(vdev_t *vd)
74699653d4eSeschrock {
74799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
74839c23413Seschrock 	ASSERT(vd->vdev_isspare);
749fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
75039c23413Seschrock 	vd->vdev_isspare = B_FALSE;
75199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
75299653d4eSeschrock }
75399653d4eSeschrock 
75499653d4eSeschrock boolean_t
75589a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
75699653d4eSeschrock {
757fa94a07fSbrendan 	boolean_t found;
75899653d4eSeschrock 
75999653d4eSeschrock 	mutex_enter(&spa_spare_lock);
76089a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
76199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
76299653d4eSeschrock 
763fa94a07fSbrendan 	return (found);
76439c23413Seschrock }
76539c23413Seschrock 
76639c23413Seschrock void
76739c23413Seschrock spa_spare_activate(vdev_t *vd)
76839c23413Seschrock {
76939c23413Seschrock 	mutex_enter(&spa_spare_lock);
77039c23413Seschrock 	ASSERT(vd->vdev_isspare);
771fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
772fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
773fa94a07fSbrendan }
77439c23413Seschrock 
775fa94a07fSbrendan /*
776fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
777fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
778fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
779fa94a07fSbrendan  */
78039c23413Seschrock 
781fa94a07fSbrendan static int
782fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
783fa94a07fSbrendan {
784fa94a07fSbrendan 	return (spa_aux_compare(a, b));
785fa94a07fSbrendan }
786fa94a07fSbrendan 
787fa94a07fSbrendan void
788fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
789fa94a07fSbrendan {
790fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
791fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
792fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
793fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
794fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
795fa94a07fSbrendan }
796fa94a07fSbrendan 
797fa94a07fSbrendan void
798fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
799fa94a07fSbrendan {
800fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
801fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
802fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
803fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
804fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
805fa94a07fSbrendan }
806fa94a07fSbrendan 
807fa94a07fSbrendan boolean_t
808fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
809fa94a07fSbrendan {
810fa94a07fSbrendan 	boolean_t found;
811fa94a07fSbrendan 
812fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
81389a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
814fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
815fa94a07fSbrendan 
816fa94a07fSbrendan 	return (found);
817fa94a07fSbrendan }
818fa94a07fSbrendan 
819fa94a07fSbrendan void
820fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
821fa94a07fSbrendan {
822fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
823fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
824fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
825fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
826fa94a07fSbrendan }
827fa94a07fSbrendan 
828fa9e4066Sahrens /*
829fa9e4066Sahrens  * ==========================================================================
830fa9e4066Sahrens  * SPA vdev locking
831fa9e4066Sahrens  * ==========================================================================
832fa9e4066Sahrens  */
833fa9e4066Sahrens 
834fa9e4066Sahrens /*
835ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
836ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
837fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
838fa9e4066Sahrens  */
839fa9e4066Sahrens uint64_t
840fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
841fa9e4066Sahrens {
8423d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
84388ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
84488ecc943SGeorge Wilson }
84588ecc943SGeorge Wilson 
84688ecc943SGeorge Wilson /*
84788ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
84888ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
84988ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
85088ecc943SGeorge Wilson  */
85188ecc943SGeorge Wilson uint64_t
85288ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
85388ecc943SGeorge Wilson {
85488ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8553d7072f8Seschrock 
856e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
857fa9e4066Sahrens 
858fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
859fa9e4066Sahrens }
860fa9e4066Sahrens 
861fa9e4066Sahrens /*
86288ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
86388ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
864fa9e4066Sahrens  */
86588ecc943SGeorge Wilson void
86688ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
867fa9e4066Sahrens {
86888ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
86988ecc943SGeorge Wilson 
8700e34b6a7Sbonwick 	int config_changed = B_FALSE;
871ea8dc4b6Seschrock 
8720373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
8730e34b6a7Sbonwick 
874e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
875e14bb325SJeff Bonwick 
8760e34b6a7Sbonwick 	/*
8770e34b6a7Sbonwick 	 * Reassess the DTLs.
8780e34b6a7Sbonwick 	 */
8790373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
8800e34b6a7Sbonwick 
8810e34b6a7Sbonwick 	/*
8820373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
8830e34b6a7Sbonwick 	 */
884e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
885088f3894Sahrens 		dsl_pool_scrub_restart(spa->spa_dsl_pool);
8860e34b6a7Sbonwick 		config_changed = B_TRUE;
8878f18d1faSGeorge Wilson 		spa->spa_config_generation++;
8880e34b6a7Sbonwick 	}
889ea8dc4b6Seschrock 
89088ecc943SGeorge Wilson 	/*
89188ecc943SGeorge Wilson 	 * Verify the metaslab classes.
89288ecc943SGeorge Wilson 	 */
893*b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
894*b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
89588ecc943SGeorge Wilson 
896e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
897fa9e4066Sahrens 
89888ecc943SGeorge Wilson 	/*
89988ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
90088ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
90188ecc943SGeorge Wilson 	 * transactionally.
90288ecc943SGeorge Wilson 	 */
90388ecc943SGeorge Wilson 	if (zio_injection_enabled)
90488ecc943SGeorge Wilson 		zio_handle_panic_injection(spa, tag);
90588ecc943SGeorge Wilson 
906fa9e4066Sahrens 	/*
907fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
908fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
909fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
910fa9e4066Sahrens 	 */
911fa9e4066Sahrens 	if (error == 0)
912fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
913fa9e4066Sahrens 
914fa9e4066Sahrens 	if (vd != NULL) {
9158ad4d6ddSJeff Bonwick 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
9168ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
917fa9e4066Sahrens 		vdev_free(vd);
9188ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
919fa9e4066Sahrens 	}
920fa9e4066Sahrens 
921fa9e4066Sahrens 	/*
9220e34b6a7Sbonwick 	 * If the config changed, update the config cache.
923fa9e4066Sahrens 	 */
9240e34b6a7Sbonwick 	if (config_changed)
925c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
92688ecc943SGeorge Wilson }
927ea8dc4b6Seschrock 
92888ecc943SGeorge Wilson /*
92988ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
93088ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
93188ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
93288ecc943SGeorge Wilson  * information.
93388ecc943SGeorge Wilson  */
93488ecc943SGeorge Wilson int
93588ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
93688ecc943SGeorge Wilson {
93788ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
938ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
939fa9e4066Sahrens 
940fa9e4066Sahrens 	return (error);
941fa9e4066Sahrens }
942fa9e4066Sahrens 
943e14bb325SJeff Bonwick /*
944e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
945e14bb325SJeff Bonwick  */
946e14bb325SJeff Bonwick void
9478f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
948e14bb325SJeff Bonwick {
9498f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
9508f18d1faSGeorge Wilson 
9518f18d1faSGeorge Wilson 	spa_config_enter(spa, locks, spa, RW_WRITER);
9528f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
953e14bb325SJeff Bonwick }
954e14bb325SJeff Bonwick 
955e14bb325SJeff Bonwick int
956e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
957e14bb325SJeff Bonwick {
958*b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
959*b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
960*b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
961*b24ab676SJeff Bonwick 
9628f18d1faSGeorge Wilson 	if (vd != NULL) {
963e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
9648f18d1faSGeorge Wilson 		spa->spa_config_generation++;
9658f18d1faSGeorge Wilson 	}
966e14bb325SJeff Bonwick 
9678f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
9688f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
969e14bb325SJeff Bonwick 
9708ad4d6ddSJeff Bonwick 	/*
9718ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
9728ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
9738ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
9748ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
9758ad4d6ddSJeff Bonwick 	 */
9768ad4d6ddSJeff Bonwick 	if (vd != NULL)
9778ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
9788ad4d6ddSJeff Bonwick 
979e14bb325SJeff Bonwick 	return (error);
980e14bb325SJeff Bonwick }
981e14bb325SJeff Bonwick 
982fa9e4066Sahrens /*
983fa9e4066Sahrens  * ==========================================================================
984fa9e4066Sahrens  * Miscellaneous functions
985fa9e4066Sahrens  * ==========================================================================
986fa9e4066Sahrens  */
987fa9e4066Sahrens 
988fa9e4066Sahrens /*
989fa9e4066Sahrens  * Rename a spa_t.
990fa9e4066Sahrens  */
991fa9e4066Sahrens int
992fa9e4066Sahrens spa_rename(const char *name, const char *newname)
993fa9e4066Sahrens {
994fa9e4066Sahrens 	spa_t *spa;
995fa9e4066Sahrens 	int err;
996fa9e4066Sahrens 
997fa9e4066Sahrens 	/*
998fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
999fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1000fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1001ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1002fa9e4066Sahrens 	 */
1003fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1004fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1005fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1006fa9e4066Sahrens 		return (err);
1007fa9e4066Sahrens 	}
1008fa9e4066Sahrens 
1009e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1010fa9e4066Sahrens 
1011fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1012e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1013fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1014fa9e4066Sahrens 
1015fa9e4066Sahrens 	/*
1016fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1017fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1018fa9e4066Sahrens 	 * during the sync.
1019fa9e4066Sahrens 	 */
1020fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1021fa9e4066Sahrens 
1022e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1023fa9e4066Sahrens 
10240373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1025fa9e4066Sahrens 
1026fa9e4066Sahrens 	/*
1027fa9e4066Sahrens 	 * Sync the updated config cache.
1028fa9e4066Sahrens 	 */
1029c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1030fa9e4066Sahrens 
1031fa9e4066Sahrens 	spa_close(spa, FTAG);
1032fa9e4066Sahrens 
1033fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1034fa9e4066Sahrens 
1035fa9e4066Sahrens 	return (0);
1036fa9e4066Sahrens }
1037fa9e4066Sahrens 
1038fa9e4066Sahrens 
1039fa9e4066Sahrens /*
1040fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
1041fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
1042fa9e4066Sahrens  * specified device_guid.
1043fa9e4066Sahrens  */
1044fa9e4066Sahrens boolean_t
1045fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1046fa9e4066Sahrens {
1047fa9e4066Sahrens 	spa_t *spa;
1048fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1049fa9e4066Sahrens 
1050ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1051fa9e4066Sahrens 
1052fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1053fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1054fa9e4066Sahrens 			continue;
1055fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1056fa9e4066Sahrens 			continue;
105739c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
105839c23413Seschrock 			if (device_guid == 0)
105939c23413Seschrock 				break;
106039c23413Seschrock 
106139c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
106239c23413Seschrock 			    device_guid) != NULL)
106339c23413Seschrock 				break;
106439c23413Seschrock 
106539c23413Seschrock 			/*
10668654d025Sperrin 			 * Check any devices we may be in the process of adding.
106739c23413Seschrock 			 */
106839c23413Seschrock 			if (spa->spa_pending_vdev) {
106939c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
107039c23413Seschrock 				    device_guid) != NULL)
107139c23413Seschrock 					break;
107239c23413Seschrock 			}
107339c23413Seschrock 		}
1074fa9e4066Sahrens 	}
1075fa9e4066Sahrens 
1076fa9e4066Sahrens 	return (spa != NULL);
1077fa9e4066Sahrens }
1078fa9e4066Sahrens 
1079fa9e4066Sahrens char *
1080fa9e4066Sahrens spa_strdup(const char *s)
1081fa9e4066Sahrens {
1082fa9e4066Sahrens 	size_t len;
1083fa9e4066Sahrens 	char *new;
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 	len = strlen(s);
1086fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1087fa9e4066Sahrens 	bcopy(s, new, len);
1088fa9e4066Sahrens 	new[len] = '\0';
1089fa9e4066Sahrens 
1090fa9e4066Sahrens 	return (new);
1091fa9e4066Sahrens }
1092fa9e4066Sahrens 
1093fa9e4066Sahrens void
1094fa9e4066Sahrens spa_strfree(char *s)
1095fa9e4066Sahrens {
1096fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1097fa9e4066Sahrens }
1098fa9e4066Sahrens 
1099fa9e4066Sahrens uint64_t
1100fa9e4066Sahrens spa_get_random(uint64_t range)
1101fa9e4066Sahrens {
1102fa9e4066Sahrens 	uint64_t r;
1103fa9e4066Sahrens 
1104fa9e4066Sahrens 	ASSERT(range != 0);
1105fa9e4066Sahrens 
1106fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1107fa9e4066Sahrens 
1108fa9e4066Sahrens 	return (r % range);
1109fa9e4066Sahrens }
1110fa9e4066Sahrens 
1111fa9e4066Sahrens void
1112*b24ab676SJeff Bonwick sprintf_blkptr(char *buf, const blkptr_t *bp)
1113fa9e4066Sahrens {
1114*b24ab676SJeff Bonwick 	char *type = dmu_ot[BP_GET_TYPE(bp)].ot_name;
1115*b24ab676SJeff Bonwick 	char *checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1116*b24ab676SJeff Bonwick 	char *compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1117fa9e4066Sahrens 
1118*b24ab676SJeff Bonwick 	SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1119fa9e4066Sahrens }
1120fa9e4066Sahrens 
1121fa9e4066Sahrens void
1122fa9e4066Sahrens spa_freeze(spa_t *spa)
1123fa9e4066Sahrens {
1124fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1125fa9e4066Sahrens 
1126e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1127fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1128fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1129fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1130fa9e4066Sahrens 	}
1131e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1132fa9e4066Sahrens 	if (freeze_txg != 0)
1133fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1134fa9e4066Sahrens }
1135fa9e4066Sahrens 
11360125049cSahrens void
11370125049cSahrens zfs_panic_recover(const char *fmt, ...)
11380125049cSahrens {
11390125049cSahrens 	va_list adx;
11400125049cSahrens 
11410125049cSahrens 	va_start(adx, fmt);
11420125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
11430125049cSahrens 	va_end(adx);
11440125049cSahrens }
11450125049cSahrens 
1146fa9e4066Sahrens /*
1147fa9e4066Sahrens  * ==========================================================================
1148fa9e4066Sahrens  * Accessor functions
1149fa9e4066Sahrens  * ==========================================================================
1150fa9e4066Sahrens  */
1151fa9e4066Sahrens 
1152088f3894Sahrens boolean_t
115388b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1154fa9e4066Sahrens {
115588b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1156fa9e4066Sahrens }
1157fa9e4066Sahrens 
1158fa9e4066Sahrens dsl_pool_t *
1159fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1160fa9e4066Sahrens {
1161fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1162fa9e4066Sahrens }
1163fa9e4066Sahrens 
1164fa9e4066Sahrens blkptr_t *
1165fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1166fa9e4066Sahrens {
1167fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1168fa9e4066Sahrens }
1169fa9e4066Sahrens 
1170fa9e4066Sahrens void
1171fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1172fa9e4066Sahrens {
1173fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1174fa9e4066Sahrens }
1175fa9e4066Sahrens 
1176fa9e4066Sahrens void
1177fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1178fa9e4066Sahrens {
1179fa9e4066Sahrens 	if (spa->spa_root == NULL)
1180fa9e4066Sahrens 		buf[0] = '\0';
1181fa9e4066Sahrens 	else
1182fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1183fa9e4066Sahrens }
1184fa9e4066Sahrens 
1185fa9e4066Sahrens int
1186fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1187fa9e4066Sahrens {
1188fa9e4066Sahrens 	return (spa->spa_sync_pass);
1189fa9e4066Sahrens }
1190fa9e4066Sahrens 
1191fa9e4066Sahrens char *
1192fa9e4066Sahrens spa_name(spa_t *spa)
1193fa9e4066Sahrens {
1194fa9e4066Sahrens 	return (spa->spa_name);
1195fa9e4066Sahrens }
1196fa9e4066Sahrens 
1197fa9e4066Sahrens uint64_t
1198fa9e4066Sahrens spa_guid(spa_t *spa)
1199fa9e4066Sahrens {
1200b5989ec7Seschrock 	/*
1201b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1202b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1203b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1204b5989ec7Seschrock 	 * this case.
1205b5989ec7Seschrock 	 */
1206b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
1207b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1208b5989ec7Seschrock 	else
1209b5989ec7Seschrock 		return (spa->spa_load_guid);
1210fa9e4066Sahrens }
1211fa9e4066Sahrens 
1212fa9e4066Sahrens uint64_t
1213fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1214fa9e4066Sahrens {
1215fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1216fa9e4066Sahrens }
1217fa9e4066Sahrens 
1218fa9e4066Sahrens uint64_t
1219fa9e4066Sahrens spa_first_txg(spa_t *spa)
1220fa9e4066Sahrens {
1221fa9e4066Sahrens 	return (spa->spa_first_txg);
1222fa9e4066Sahrens }
1223fa9e4066Sahrens 
1224*b24ab676SJeff Bonwick uint64_t
1225*b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1226*b24ab676SJeff Bonwick {
1227*b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1228*b24ab676SJeff Bonwick }
1229*b24ab676SJeff Bonwick 
123088b7b0f2SMatthew Ahrens pool_state_t
1231fa9e4066Sahrens spa_state(spa_t *spa)
1232fa9e4066Sahrens {
1233fa9e4066Sahrens 	return (spa->spa_state);
1234fa9e4066Sahrens }
1235fa9e4066Sahrens 
1236fa9e4066Sahrens uint64_t
1237fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1238fa9e4066Sahrens {
1239fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1240fa9e4066Sahrens }
1241fa9e4066Sahrens 
1242fa9e4066Sahrens /* ARGSUSED */
1243fa9e4066Sahrens uint64_t
1244fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1245fa9e4066Sahrens {
1246fa9e4066Sahrens 	/*
1247*b24ab676SJeff Bonwick 	 * The worst case is single-sector max-parity RAID-Z blocks, in which
1248*b24ab676SJeff Bonwick 	 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1249*b24ab676SJeff Bonwick 	 * times the size; so just assume that.  Add to this the fact that
1250*b24ab676SJeff Bonwick 	 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1251*b24ab676SJeff Bonwick 	 * the block may be dittoed with up to 3 DVAs by ddt_sync().
125244cd46caSbillm 	 */
1253*b24ab676SJeff Bonwick 	return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
125444cd46caSbillm }
125544cd46caSbillm 
12560a4e9518Sgw /*
12570a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
12580a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
12590a4e9518Sgw  */
12600a4e9518Sgw uint8_t
12610a4e9518Sgw spa_get_failmode(spa_t *spa)
12620a4e9518Sgw {
12630a4e9518Sgw 	return (spa->spa_failmode);
12640a4e9518Sgw }
12650a4e9518Sgw 
1266e14bb325SJeff Bonwick boolean_t
1267e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1268e14bb325SJeff Bonwick {
1269e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1270e14bb325SJeff Bonwick }
1271e14bb325SJeff Bonwick 
127244cd46caSbillm uint64_t
127344cd46caSbillm spa_version(spa_t *spa)
127444cd46caSbillm {
127544cd46caSbillm 	return (spa->spa_ubsync.ub_version);
127644cd46caSbillm }
127744cd46caSbillm 
1278*b24ab676SJeff Bonwick boolean_t
1279*b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1280*b24ab676SJeff Bonwick {
1281*b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1282*b24ab676SJeff Bonwick }
1283*b24ab676SJeff Bonwick 
1284*b24ab676SJeff Bonwick metaslab_class_t *
1285*b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1286*b24ab676SJeff Bonwick {
1287*b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1288*b24ab676SJeff Bonwick }
1289*b24ab676SJeff Bonwick 
1290*b24ab676SJeff Bonwick metaslab_class_t *
1291*b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1292*b24ab676SJeff Bonwick {
1293*b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1294*b24ab676SJeff Bonwick }
1295*b24ab676SJeff Bonwick 
129644cd46caSbillm int
129744cd46caSbillm spa_max_replication(spa_t *spa)
129844cd46caSbillm {
129944cd46caSbillm 	/*
1300e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
130144cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
130244cd46caSbillm 	 * replication level accordingly.
1303fa9e4066Sahrens 	 */
1304e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
130544cd46caSbillm 		return (1);
130644cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1307fa9e4066Sahrens }
1308fa9e4066Sahrens 
130999653d4eSeschrock uint64_t
1310*b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
131199653d4eSeschrock {
1312*b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1313*b24ab676SJeff Bonwick 	uint64_t dsize = asize;
131499653d4eSeschrock 
1315*b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
131699653d4eSeschrock 
1317*b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1318*b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1319*b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
132099653d4eSeschrock 	}
1321*b24ab676SJeff Bonwick 
1322*b24ab676SJeff Bonwick 	return (dsize);
1323*b24ab676SJeff Bonwick }
1324*b24ab676SJeff Bonwick 
1325*b24ab676SJeff Bonwick uint64_t
1326*b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1327*b24ab676SJeff Bonwick {
1328*b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1329*b24ab676SJeff Bonwick 
1330*b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1331*b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1332*b24ab676SJeff Bonwick 
1333*b24ab676SJeff Bonwick 	return (dsize);
1334*b24ab676SJeff Bonwick }
1335*b24ab676SJeff Bonwick 
1336*b24ab676SJeff Bonwick uint64_t
1337*b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1338*b24ab676SJeff Bonwick {
1339*b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1340*b24ab676SJeff Bonwick 
1341*b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1342*b24ab676SJeff Bonwick 
1343*b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1344*b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1345*b24ab676SJeff Bonwick 
1346e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1347*b24ab676SJeff Bonwick 
1348*b24ab676SJeff Bonwick 	return (dsize);
134999653d4eSeschrock }
135099653d4eSeschrock 
1351fa9e4066Sahrens /*
1352fa9e4066Sahrens  * ==========================================================================
1353fa9e4066Sahrens  * Initialization and Termination
1354fa9e4066Sahrens  * ==========================================================================
1355fa9e4066Sahrens  */
1356fa9e4066Sahrens 
1357fa9e4066Sahrens static int
1358fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1359fa9e4066Sahrens {
1360fa9e4066Sahrens 	const spa_t *s1 = a1;
1361fa9e4066Sahrens 	const spa_t *s2 = a2;
1362fa9e4066Sahrens 	int s;
1363fa9e4066Sahrens 
1364fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1365fa9e4066Sahrens 	if (s > 0)
1366fa9e4066Sahrens 		return (1);
1367fa9e4066Sahrens 	if (s < 0)
1368fa9e4066Sahrens 		return (-1);
1369fa9e4066Sahrens 	return (0);
1370fa9e4066Sahrens }
1371fa9e4066Sahrens 
13720373e76bSbonwick int
13730373e76bSbonwick spa_busy(void)
13740373e76bSbonwick {
13750373e76bSbonwick 	return (spa_active_count);
13760373e76bSbonwick }
13770373e76bSbonwick 
1378e7cbe64fSgw void
1379e7cbe64fSgw spa_boot_init()
1380e7cbe64fSgw {
1381e7cbe64fSgw 	spa_config_load();
1382e7cbe64fSgw }
1383e7cbe64fSgw 
1384fa9e4066Sahrens void
1385fa9e4066Sahrens spa_init(int mode)
1386fa9e4066Sahrens {
1387fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1388c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1389fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1390fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1391fa9e4066Sahrens 
1392fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1393fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1394fa9e4066Sahrens 
1395fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1396fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1397fa94a07fSbrendan 
1398fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1399fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
140099653d4eSeschrock 
14018ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1402fa9e4066Sahrens 
1403fa9e4066Sahrens 	refcount_init();
1404fa9e4066Sahrens 	unique_init();
1405fa9e4066Sahrens 	zio_init();
1406fa9e4066Sahrens 	dmu_init();
1407fa9e4066Sahrens 	zil_init();
140887db74c1Sek 	vdev_cache_stat_init();
140991ebeef5Sahrens 	zfs_prop_init();
1410990b4856Slling 	zpool_prop_init();
1411fa9e4066Sahrens 	spa_config_load();
1412e14bb325SJeff Bonwick 	l2arc_start();
1413fa9e4066Sahrens }
1414fa9e4066Sahrens 
1415fa9e4066Sahrens void
1416fa9e4066Sahrens spa_fini(void)
1417fa9e4066Sahrens {
1418e14bb325SJeff Bonwick 	l2arc_stop();
1419e14bb325SJeff Bonwick 
1420fa9e4066Sahrens 	spa_evict_all();
1421fa9e4066Sahrens 
142287db74c1Sek 	vdev_cache_stat_fini();
1423fa9e4066Sahrens 	zil_fini();
1424fa9e4066Sahrens 	dmu_fini();
1425fa9e4066Sahrens 	zio_fini();
142691ebeef5Sahrens 	unique_fini();
1427fa9e4066Sahrens 	refcount_fini();
1428fa9e4066Sahrens 
1429fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
143099653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1431fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1432fa9e4066Sahrens 
1433fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1434fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1435c25056deSgw 	mutex_destroy(&spa_spare_lock);
1436fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1437fa9e4066Sahrens }
14386ce0521aSperrin 
14396ce0521aSperrin /*
14406ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
14416ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
14426ce0521aSperrin  * performance and not correctness
14436ce0521aSperrin  */
14446ce0521aSperrin boolean_t
14456ce0521aSperrin spa_has_slogs(spa_t *spa)
14466ce0521aSperrin {
14476ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
14486ce0521aSperrin }
1449bf82a41bSeschrock 
1450*b24ab676SJeff Bonwick spa_log_state_t
1451*b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1452*b24ab676SJeff Bonwick {
1453*b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1454*b24ab676SJeff Bonwick }
1455*b24ab676SJeff Bonwick 
1456*b24ab676SJeff Bonwick void
1457*b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1458*b24ab676SJeff Bonwick {
1459*b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1460*b24ab676SJeff Bonwick }
1461*b24ab676SJeff Bonwick 
1462bf82a41bSeschrock boolean_t
1463bf82a41bSeschrock spa_is_root(spa_t *spa)
1464bf82a41bSeschrock {
1465bf82a41bSeschrock 	return (spa->spa_is_root);
1466bf82a41bSeschrock }
14678ad4d6ddSJeff Bonwick 
14688ad4d6ddSJeff Bonwick boolean_t
14698ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
14708ad4d6ddSJeff Bonwick {
14718ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
14728ad4d6ddSJeff Bonwick }
14738ad4d6ddSJeff Bonwick 
14748ad4d6ddSJeff Bonwick int
14758ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
14768ad4d6ddSJeff Bonwick {
14778ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
14788ad4d6ddSJeff Bonwick }
1479*b24ab676SJeff Bonwick 
1480*b24ab676SJeff Bonwick uint64_t
1481*b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1482*b24ab676SJeff Bonwick {
1483*b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1484*b24ab676SJeff Bonwick }
1485*b24ab676SJeff Bonwick 
1486*b24ab676SJeff Bonwick uint64_t
1487*b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1488*b24ab676SJeff Bonwick {
1489*b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1490*b24ab676SJeff Bonwick }
1491*b24ab676SJeff Bonwick 
1492*b24ab676SJeff Bonwick objset_t *
1493*b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1494*b24ab676SJeff Bonwick {
1495*b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1496*b24ab676SJeff Bonwick }
1497*b24ab676SJeff Bonwick 
1498*b24ab676SJeff Bonwick enum zio_checksum
1499*b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1500*b24ab676SJeff Bonwick {
1501*b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1502*b24ab676SJeff Bonwick }
1503