xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision c5904d138f3bdf0762dbf452a43d5a5c387ea6a8)
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 /*
2287db74c1Sek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/spa_impl.h>
30fa9e4066Sahrens #include <sys/zio.h>
31fa9e4066Sahrens #include <sys/zio_checksum.h>
32fa9e4066Sahrens #include <sys/zio_compress.h>
33fa9e4066Sahrens #include <sys/dmu.h>
34fa9e4066Sahrens #include <sys/dmu_tx.h>
35fa9e4066Sahrens #include <sys/zap.h>
36fa9e4066Sahrens #include <sys/zil.h>
37fa9e4066Sahrens #include <sys/vdev_impl.h>
38fa9e4066Sahrens #include <sys/metaslab.h>
39fa9e4066Sahrens #include <sys/uberblock_impl.h>
40fa9e4066Sahrens #include <sys/txg.h>
41fa9e4066Sahrens #include <sys/avl.h>
42fa9e4066Sahrens #include <sys/unique.h>
43fa9e4066Sahrens #include <sys/dsl_pool.h>
44fa9e4066Sahrens #include <sys/dsl_dir.h>
45fa9e4066Sahrens #include <sys/dsl_prop.h>
46fa9e4066Sahrens #include <sys/fs/zfs.h>
476ce0521aSperrin #include <sys/metaslab_impl.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
7744cd46caSbillm  *	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  *
8091ebeef5Sahrens  * spa_config_lock (per-spa read-priority rwlock)
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  * spa_config_cache_lock (per-spa mutex)
89fa9e4066Sahrens  *
9044cd46caSbillm  *	This mutex prevents the spa_config nvlist from being updated.  No
91fa9e4066Sahrens  *      other locks are required to obtain this lock, although implicitly you
92fa9e4066Sahrens  *      must have the namespace lock or non-zero refcount to have any kind
93fa9e4066Sahrens  *      of spa_t pointer at all.
94fa9e4066Sahrens  *
95fa9e4066Sahrens  * The locking order is fairly straightforward:
96fa9e4066Sahrens  *
9744cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
98fa9e4066Sahrens  *
9944cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
10044cd46caSbillm  *	or to check if it is zero.
101fa9e4066Sahrens  *
10244cd46caSbillm  *		spa_refcount		->	spa_config_lock
103fa9e4066Sahrens  *
10444cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10544cd46caSbillm  *	the config lock.
106fa9e4066Sahrens  *
10744cd46caSbillm  *		spa_namespace_lock	->	spa_config_lock
108fa9e4066Sahrens  *
10944cd46caSbillm  *	The namespace lock must always be taken before the config lock.
110fa9e4066Sahrens  *
111fa9e4066Sahrens  *
112fa9e4066Sahrens  * The spa_namespace_lock and spa_config_cache_lock can be acquired directly and
113fa9e4066Sahrens  * are globally visible.
114fa9e4066Sahrens  *
115fa9e4066Sahrens  * The namespace is manipulated using the following functions, all which require
116fa9e4066Sahrens  * the spa_namespace_lock to be held.
117fa9e4066Sahrens  *
11844cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
119fa9e4066Sahrens  *
12044cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
121fa9e4066Sahrens  *
12244cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
12344cd46caSbillm  *				frees up any memory associated with the spa_t.
124fa9e4066Sahrens  *
12544cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12644cd46caSbillm  *				first if NULL is passed.
127fa9e4066Sahrens  *
12844cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12944cd46caSbillm  *				the system.
130fa9e4066Sahrens  *
131ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
132fa9e4066Sahrens  *
133fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
134fa9e4066Sahrens  *
13544cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13644cd46caSbillm  *				called with spa_namespace_lock held if the
13744cd46caSbillm  *				refcount is currently zero.
138fa9e4066Sahrens  *
13944cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
14044cd46caSbillm  *				not free the spa_t or remove it from the
14144cd46caSbillm  *				namespace.  No locking is required.
142fa9e4066Sahrens  *
14344cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14444cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14544cd46caSbillm  *				held.
146fa9e4066Sahrens  *
147e05725b1Sbonwick  * The spa_config_lock is a form of rwlock.  It must be held as RW_READER
148e05725b1Sbonwick  * to perform I/O to the pool, and as RW_WRITER to change the vdev config.
149e05725b1Sbonwick  * The spa_config_lock is manipulated with spa_config_{enter,exit,held}().
150fa9e4066Sahrens  *
151ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
152fa9e4066Sahrens  *
15344cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
154ea8dc4b6Seschrock  *				for writing.
155fa9e4066Sahrens  *
15644cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
15744cd46caSbillm  *				to complete, sync the updated configs to the
158ea8dc4b6Seschrock  *				cache, and release the namespace lock.
159fa9e4066Sahrens  *
160fa9e4066Sahrens  * The spa_name() function also requires either the spa_namespace_lock
161fa9e4066Sahrens  * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
162fa9e4066Sahrens  * also implemented within this file since is requires manipulation of the
163fa9e4066Sahrens  * namespace.
164fa9e4066Sahrens  */
165fa9e4066Sahrens 
166fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
167fa9e4066Sahrens kmutex_t spa_namespace_lock;
168fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
1690373e76bSbonwick static int spa_active_count;
170416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
171fa9e4066Sahrens 
17299653d4eSeschrock static kmutex_t spa_spare_lock;
17339c23413Seschrock static avl_tree_t spa_spare_avl;
174fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
175fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
17699653d4eSeschrock 
177fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
178fa9e4066Sahrens int spa_mode;
179fa9e4066Sahrens 
180fa9e4066Sahrens #ifdef ZFS_DEBUG
18140feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
18240feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
183fa9e4066Sahrens #else
184fa9e4066Sahrens int zfs_flags = 0;
185fa9e4066Sahrens #endif
186fa9e4066Sahrens 
1870125049cSahrens /*
1880125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
1890125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
1900125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
1910125049cSahrens  */
1920125049cSahrens int zfs_recover = 0;
1930125049cSahrens 
194fa9e4066Sahrens #define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
195fa9e4066Sahrens 
196e05725b1Sbonwick /*
197e05725b1Sbonwick  * ==========================================================================
198e05725b1Sbonwick  * SPA config locking
199e05725b1Sbonwick  * ==========================================================================
200e05725b1Sbonwick  */
201e05725b1Sbonwick static void
202e05725b1Sbonwick spa_config_lock_init(spa_config_lock_t *scl)
203e05725b1Sbonwick {
204e05725b1Sbonwick 	mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
205e05725b1Sbonwick 	scl->scl_writer = NULL;
206e05725b1Sbonwick 	cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
207e05725b1Sbonwick 	refcount_create(&scl->scl_count);
208e05725b1Sbonwick }
209e05725b1Sbonwick 
210e05725b1Sbonwick static void
211e05725b1Sbonwick spa_config_lock_destroy(spa_config_lock_t *scl)
212e05725b1Sbonwick {
213e05725b1Sbonwick 	mutex_destroy(&scl->scl_lock);
214e05725b1Sbonwick 	ASSERT(scl->scl_writer == NULL);
215e05725b1Sbonwick 	cv_destroy(&scl->scl_cv);
216e05725b1Sbonwick 	refcount_destroy(&scl->scl_count);
217e05725b1Sbonwick }
218e05725b1Sbonwick 
219e05725b1Sbonwick void
220e05725b1Sbonwick spa_config_enter(spa_t *spa, krw_t rw, void *tag)
221e05725b1Sbonwick {
222e05725b1Sbonwick 	spa_config_lock_t *scl = &spa->spa_config_lock;
223e05725b1Sbonwick 
224e05725b1Sbonwick 	mutex_enter(&scl->scl_lock);
225e05725b1Sbonwick 
226e05725b1Sbonwick 	if (rw == RW_READER) {
227e05725b1Sbonwick 		while (scl->scl_writer != NULL && scl->scl_writer != curthread)
228e05725b1Sbonwick 			cv_wait(&scl->scl_cv, &scl->scl_lock);
229e05725b1Sbonwick 	} else {
230e05725b1Sbonwick 		while (!refcount_is_zero(&scl->scl_count) &&
231e05725b1Sbonwick 		    scl->scl_writer != curthread)
232e05725b1Sbonwick 			cv_wait(&scl->scl_cv, &scl->scl_lock);
233e05725b1Sbonwick 		scl->scl_writer = curthread;
234e05725b1Sbonwick 	}
235e05725b1Sbonwick 
236e05725b1Sbonwick 	(void) refcount_add(&scl->scl_count, tag);
237e05725b1Sbonwick 
238e05725b1Sbonwick 	mutex_exit(&scl->scl_lock);
239e05725b1Sbonwick }
240e05725b1Sbonwick 
241e05725b1Sbonwick void
242e05725b1Sbonwick spa_config_exit(spa_t *spa, void *tag)
243e05725b1Sbonwick {
244e05725b1Sbonwick 	spa_config_lock_t *scl = &spa->spa_config_lock;
245e05725b1Sbonwick 
246e05725b1Sbonwick 	mutex_enter(&scl->scl_lock);
247e05725b1Sbonwick 
248e05725b1Sbonwick 	ASSERT(!refcount_is_zero(&scl->scl_count));
249e05725b1Sbonwick 
250e05725b1Sbonwick 	if (refcount_remove(&scl->scl_count, tag) == 0) {
251e05725b1Sbonwick 		cv_broadcast(&scl->scl_cv);
252e05725b1Sbonwick 		ASSERT(scl->scl_writer == NULL || scl->scl_writer == curthread);
253e05725b1Sbonwick 		scl->scl_writer = NULL;  /* OK in either case */
254e05725b1Sbonwick 	}
255e05725b1Sbonwick 
256e05725b1Sbonwick 	mutex_exit(&scl->scl_lock);
257e05725b1Sbonwick }
258e05725b1Sbonwick 
259e05725b1Sbonwick boolean_t
260e05725b1Sbonwick spa_config_held(spa_t *spa, krw_t rw)
261e05725b1Sbonwick {
262e05725b1Sbonwick 	spa_config_lock_t *scl = &spa->spa_config_lock;
263e05725b1Sbonwick 
264e05725b1Sbonwick 	if (rw == RW_READER)
265e05725b1Sbonwick 		return (!refcount_is_zero(&scl->scl_count));
266e05725b1Sbonwick 	else
267e05725b1Sbonwick 		return (scl->scl_writer == curthread);
268e05725b1Sbonwick }
269e05725b1Sbonwick 
270fa9e4066Sahrens /*
271fa9e4066Sahrens  * ==========================================================================
272fa9e4066Sahrens  * SPA namespace functions
273fa9e4066Sahrens  * ==========================================================================
274fa9e4066Sahrens  */
275fa9e4066Sahrens 
276fa9e4066Sahrens /*
277fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
278fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
279fa9e4066Sahrens  */
280fa9e4066Sahrens spa_t *
281fa9e4066Sahrens spa_lookup(const char *name)
282fa9e4066Sahrens {
283fa9e4066Sahrens 	spa_t search, *spa;
284fa9e4066Sahrens 	avl_index_t where;
28540feaa91Sahrens 	char c;
28640feaa91Sahrens 	char *cp;
287fa9e4066Sahrens 
288fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
289fa9e4066Sahrens 
29040feaa91Sahrens 	/*
29140feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
29240feaa91Sahrens 	 * just use that.
29340feaa91Sahrens 	 */
29440feaa91Sahrens 	cp = strpbrk(name, "/@");
29540feaa91Sahrens 	if (cp) {
29640feaa91Sahrens 		c = *cp;
29740feaa91Sahrens 		*cp = '\0';
29840feaa91Sahrens 	}
29940feaa91Sahrens 
300fa9e4066Sahrens 	search.spa_name = (char *)name;
301fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
302fa9e4066Sahrens 
30340feaa91Sahrens 	if (cp)
30440feaa91Sahrens 		*cp = c;
30540feaa91Sahrens 
306fa9e4066Sahrens 	return (spa);
307fa9e4066Sahrens }
308fa9e4066Sahrens 
309fa9e4066Sahrens /*
310fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
311fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
312fa9e4066Sahrens  * exist by calling spa_lookup() first.
313fa9e4066Sahrens  */
314fa9e4066Sahrens spa_t *
3150373e76bSbonwick spa_add(const char *name, const char *altroot)
316fa9e4066Sahrens {
317fa9e4066Sahrens 	spa_t *spa;
318*c5904d13Seschrock 	spa_config_dirent_t *dp;
319fa9e4066Sahrens 
320fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
321fa9e4066Sahrens 
322fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
323fa9e4066Sahrens 
324c25056deSgw 	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
325c25056deSgw 
326c25056deSgw 	mutex_init(&spa->spa_uberblock_lock, NULL, MUTEX_DEFAULT, NULL);
327c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
328c25056deSgw 	mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL);
329c25056deSgw 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
330c25056deSgw 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
331c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
332c25056deSgw 	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
333c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
334c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
335c25056deSgw 
336c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
337c25056deSgw 	cv_init(&spa->spa_scrub_cv, NULL, CV_DEFAULT, NULL);
338c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
339c25056deSgw 
340fa9e4066Sahrens 	spa->spa_name = spa_strdup(name);
341fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
342fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
3430373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
344fa9e4066Sahrens 
345fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
346e05725b1Sbonwick 	spa_config_lock_init(&spa->spa_config_lock);
347fa9e4066Sahrens 
348fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
349fa9e4066Sahrens 
3500a4e9518Sgw 	mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL);
3510a4e9518Sgw 
3520373e76bSbonwick 	/*
3530373e76bSbonwick 	 * Set the alternate root, if there is one.
3540373e76bSbonwick 	 */
3550373e76bSbonwick 	if (altroot) {
3560373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
3570373e76bSbonwick 		spa_active_count++;
3580373e76bSbonwick 	}
3590373e76bSbonwick 
360*c5904d13Seschrock 	/*
361*c5904d13Seschrock 	 * Every pool starts with the default cachefile
362*c5904d13Seschrock 	 */
363*c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
364*c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
365*c5904d13Seschrock 
366*c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
367*c5904d13Seschrock 	dp->scd_path = spa_strdup(spa_config_path);
368*c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
369*c5904d13Seschrock 
370fa9e4066Sahrens 	return (spa);
371fa9e4066Sahrens }
372fa9e4066Sahrens 
373fa9e4066Sahrens /*
374fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
375fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
376fa9e4066Sahrens  * deactivated.
377fa9e4066Sahrens  */
378fa9e4066Sahrens void
379fa9e4066Sahrens spa_remove(spa_t *spa)
380fa9e4066Sahrens {
381*c5904d13Seschrock 	spa_config_dirent_t *dp;
382*c5904d13Seschrock 
383fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
384fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
385fa9e4066Sahrens 	ASSERT(spa->spa_scrub_thread == NULL);
386fa9e4066Sahrens 
387fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
388fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
389fa9e4066Sahrens 
3900373e76bSbonwick 	if (spa->spa_root) {
391fa9e4066Sahrens 		spa_strfree(spa->spa_root);
3920373e76bSbonwick 		spa_active_count--;
3930373e76bSbonwick 	}
394fa9e4066Sahrens 
395fa9e4066Sahrens 	if (spa->spa_name)
396fa9e4066Sahrens 		spa_strfree(spa->spa_name);
397fa9e4066Sahrens 
398*c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
399*c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
400*c5904d13Seschrock 		if (dp->scd_path != NULL)
401*c5904d13Seschrock 			spa_strfree(dp->scd_path);
402*c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
403*c5904d13Seschrock 	}
404*c5904d13Seschrock 
405*c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
4062f8aaab3Seschrock 
407fa9e4066Sahrens 	spa_config_set(spa, NULL);
408fa9e4066Sahrens 
409fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
41091ebeef5Sahrens 
411e05725b1Sbonwick 	spa_config_lock_destroy(&spa->spa_config_lock);
412fa9e4066Sahrens 
413c25056deSgw 	rw_destroy(&spa->spa_traverse_lock);
414c25056deSgw 
415c25056deSgw 	cv_destroy(&spa->spa_async_cv);
416c25056deSgw 	cv_destroy(&spa->spa_scrub_cv);
417c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
418c25056deSgw 
419c25056deSgw 	mutex_destroy(&spa->spa_uberblock_lock);
4205ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
421c25056deSgw 	mutex_destroy(&spa->spa_config_cache_lock);
422c25056deSgw 	mutex_destroy(&spa->spa_scrub_lock);
423c25056deSgw 	mutex_destroy(&spa->spa_errlog_lock);
424c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
425c25056deSgw 	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
42606eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
427b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
4280a4e9518Sgw 	mutex_destroy(&spa->spa_zio_lock);
4295ad82045Snd 
430fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
431fa9e4066Sahrens }
432fa9e4066Sahrens 
433fa9e4066Sahrens /*
434fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
435fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
436fa9e4066Sahrens  */
437fa9e4066Sahrens spa_t *
438fa9e4066Sahrens spa_next(spa_t *prev)
439fa9e4066Sahrens {
440fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
441fa9e4066Sahrens 
442fa9e4066Sahrens 	if (prev)
443fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
444fa9e4066Sahrens 	else
445fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
446fa9e4066Sahrens }
447fa9e4066Sahrens 
448fa9e4066Sahrens /*
449fa9e4066Sahrens  * ==========================================================================
450fa9e4066Sahrens  * SPA refcount functions
451fa9e4066Sahrens  * ==========================================================================
452fa9e4066Sahrens  */
453fa9e4066Sahrens 
454fa9e4066Sahrens /*
455fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
456fa9e4066Sahrens  * have the namespace lock held.
457fa9e4066Sahrens  */
458fa9e4066Sahrens void
459fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
460fa9e4066Sahrens {
461fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
462fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
463fa9e4066Sahrens 
464fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
465fa9e4066Sahrens }
466fa9e4066Sahrens 
467fa9e4066Sahrens /*
468fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
469fa9e4066Sahrens  * have the namespace lock held.
470fa9e4066Sahrens  */
471fa9e4066Sahrens void
472fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
473fa9e4066Sahrens {
474fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
475fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
476fa9e4066Sahrens 
477fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
478fa9e4066Sahrens }
479fa9e4066Sahrens 
480fa9e4066Sahrens /*
481fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
482fa9e4066Sahrens  * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
483fa9e4066Sahrens  * number of references acquired when opening a pool
484fa9e4066Sahrens  */
485fa9e4066Sahrens boolean_t
486fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
487fa9e4066Sahrens {
488fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
489fa9e4066Sahrens 
490fa9e4066Sahrens 	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
491fa9e4066Sahrens }
492fa9e4066Sahrens 
49399653d4eSeschrock /*
49499653d4eSeschrock  * ==========================================================================
495fa94a07fSbrendan  * SPA spare and l2cache tracking
49699653d4eSeschrock  * ==========================================================================
49799653d4eSeschrock  */
49899653d4eSeschrock 
499fa94a07fSbrendan /*
500fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
501fa94a07fSbrendan  * for 'auxiliary' devices.
502fa94a07fSbrendan  */
503fa94a07fSbrendan 
504fa94a07fSbrendan typedef struct spa_aux {
505fa94a07fSbrendan 	uint64_t	aux_guid;
506fa94a07fSbrendan 	uint64_t	aux_pool;
507fa94a07fSbrendan 	avl_node_t	aux_avl;
508fa94a07fSbrendan 	int		aux_count;
509fa94a07fSbrendan } spa_aux_t;
510fa94a07fSbrendan 
511fa94a07fSbrendan static int
512fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
513fa94a07fSbrendan {
514fa94a07fSbrendan 	const spa_aux_t *sa = a;
515fa94a07fSbrendan 	const spa_aux_t *sb = b;
516fa94a07fSbrendan 
517fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
518fa94a07fSbrendan 		return (-1);
519fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
520fa94a07fSbrendan 		return (1);
521fa94a07fSbrendan 	else
522fa94a07fSbrendan 		return (0);
523fa94a07fSbrendan }
524fa94a07fSbrendan 
525fa94a07fSbrendan void
526fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
527fa94a07fSbrendan {
528fa94a07fSbrendan 	avl_index_t where;
529fa94a07fSbrendan 	spa_aux_t search;
530fa94a07fSbrendan 	spa_aux_t *aux;
531fa94a07fSbrendan 
532fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
533fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
534fa94a07fSbrendan 		aux->aux_count++;
535fa94a07fSbrendan 	} else {
536fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
537fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
538fa94a07fSbrendan 		aux->aux_count = 1;
539fa94a07fSbrendan 		avl_insert(avl, aux, where);
540fa94a07fSbrendan 	}
541fa94a07fSbrendan }
542fa94a07fSbrendan 
543fa94a07fSbrendan void
544fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
545fa94a07fSbrendan {
546fa94a07fSbrendan 	spa_aux_t search;
547fa94a07fSbrendan 	spa_aux_t *aux;
548fa94a07fSbrendan 	avl_index_t where;
549fa94a07fSbrendan 
550fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
551fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
552fa94a07fSbrendan 
553fa94a07fSbrendan 	ASSERT(aux != NULL);
554fa94a07fSbrendan 
555fa94a07fSbrendan 	if (--aux->aux_count == 0) {
556fa94a07fSbrendan 		avl_remove(avl, aux);
557fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
558fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
559fa94a07fSbrendan 		aux->aux_pool = 0ULL;
560fa94a07fSbrendan 	}
561fa94a07fSbrendan }
562fa94a07fSbrendan 
563fa94a07fSbrendan boolean_t
564fa94a07fSbrendan spa_aux_exists(uint64_t guid, uint64_t *pool, avl_tree_t *avl)
565fa94a07fSbrendan {
566fa94a07fSbrendan 	spa_aux_t search, *found;
567fa94a07fSbrendan 	avl_index_t where;
568fa94a07fSbrendan 
569fa94a07fSbrendan 	search.aux_guid = guid;
570fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
571fa94a07fSbrendan 
572fa94a07fSbrendan 	if (pool) {
573fa94a07fSbrendan 		if (found)
574fa94a07fSbrendan 			*pool = found->aux_pool;
575fa94a07fSbrendan 		else
576fa94a07fSbrendan 			*pool = 0ULL;
577fa94a07fSbrendan 	}
578fa94a07fSbrendan 
579fa94a07fSbrendan 	return (found != NULL);
580fa94a07fSbrendan }
581fa94a07fSbrendan 
582fa94a07fSbrendan void
583fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
584fa94a07fSbrendan {
585fa94a07fSbrendan 	spa_aux_t search, *found;
586fa94a07fSbrendan 	avl_index_t where;
587fa94a07fSbrendan 
588fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
589fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
590fa94a07fSbrendan 	ASSERT(found != NULL);
591fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
592fa94a07fSbrendan 
593fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
594fa94a07fSbrendan }
595fa94a07fSbrendan 
59699653d4eSeschrock /*
59739c23413Seschrock  * Spares are tracked globally due to the following constraints:
59839c23413Seschrock  *
59939c23413Seschrock  * 	- A spare may be part of multiple pools.
60039c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
60139c23413Seschrock  *	  another pool.
60239c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
60339c23413Seschrock  *	  the target is a spare in the same pool.
60439c23413Seschrock  *
60539c23413Seschrock  * We keep track of all spares on the system through the use of a reference
60639c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
60739c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
60839c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
60939c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
61039c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
61139c23413Seschrock  *
61239c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
61339c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
61439c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
61539c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
61699653d4eSeschrock  */
61739c23413Seschrock 
61899653d4eSeschrock static int
61999653d4eSeschrock spa_spare_compare(const void *a, const void *b)
62099653d4eSeschrock {
621fa94a07fSbrendan 	return (spa_aux_compare(a, b));
62299653d4eSeschrock }
62399653d4eSeschrock 
62499653d4eSeschrock void
62539c23413Seschrock spa_spare_add(vdev_t *vd)
62699653d4eSeschrock {
62799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
62839c23413Seschrock 	ASSERT(!vd->vdev_isspare);
629fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
63039c23413Seschrock 	vd->vdev_isspare = B_TRUE;
63199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
63299653d4eSeschrock }
63399653d4eSeschrock 
63499653d4eSeschrock void
63539c23413Seschrock spa_spare_remove(vdev_t *vd)
63699653d4eSeschrock {
63799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
63839c23413Seschrock 	ASSERT(vd->vdev_isspare);
639fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
64039c23413Seschrock 	vd->vdev_isspare = B_FALSE;
64199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
64299653d4eSeschrock }
64399653d4eSeschrock 
64499653d4eSeschrock boolean_t
64539c23413Seschrock spa_spare_exists(uint64_t guid, uint64_t *pool)
64699653d4eSeschrock {
647fa94a07fSbrendan 	boolean_t found;
64899653d4eSeschrock 
64999653d4eSeschrock 	mutex_enter(&spa_spare_lock);
650fa94a07fSbrendan 	found = spa_aux_exists(guid, pool, &spa_spare_avl);
65199653d4eSeschrock 	mutex_exit(&spa_spare_lock);
65299653d4eSeschrock 
653fa94a07fSbrendan 	return (found);
65439c23413Seschrock }
65539c23413Seschrock 
65639c23413Seschrock void
65739c23413Seschrock spa_spare_activate(vdev_t *vd)
65839c23413Seschrock {
65939c23413Seschrock 	mutex_enter(&spa_spare_lock);
66039c23413Seschrock 	ASSERT(vd->vdev_isspare);
661fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
662fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
663fa94a07fSbrendan }
66439c23413Seschrock 
665fa94a07fSbrendan /*
666fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
667fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
668fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
669fa94a07fSbrendan  */
67039c23413Seschrock 
671fa94a07fSbrendan static int
672fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
673fa94a07fSbrendan {
674fa94a07fSbrendan 	return (spa_aux_compare(a, b));
675fa94a07fSbrendan }
676fa94a07fSbrendan 
677fa94a07fSbrendan void
678fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
679fa94a07fSbrendan {
680fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
681fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
682fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
683fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
684fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
685fa94a07fSbrendan }
686fa94a07fSbrendan 
687fa94a07fSbrendan void
688fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
689fa94a07fSbrendan {
690fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
691fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
692fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
693fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
694fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
695fa94a07fSbrendan }
696fa94a07fSbrendan 
697fa94a07fSbrendan boolean_t
698fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
699fa94a07fSbrendan {
700fa94a07fSbrendan 	boolean_t found;
701fa94a07fSbrendan 
702fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
703fa94a07fSbrendan 	found = spa_aux_exists(guid, pool, &spa_l2cache_avl);
704fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
705fa94a07fSbrendan 
706fa94a07fSbrendan 	return (found);
707fa94a07fSbrendan }
708fa94a07fSbrendan 
709fa94a07fSbrendan void
710fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
711fa94a07fSbrendan {
712fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
713fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
714fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
715fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
716fa94a07fSbrendan }
717fa94a07fSbrendan 
718fa94a07fSbrendan void
719fa94a07fSbrendan spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
720fa94a07fSbrendan {
721fa94a07fSbrendan 	vdev_space_update(vd, space, alloc, B_FALSE);
72299653d4eSeschrock }
72399653d4eSeschrock 
724fa9e4066Sahrens /*
725fa9e4066Sahrens  * ==========================================================================
726fa9e4066Sahrens  * SPA vdev locking
727fa9e4066Sahrens  * ==========================================================================
728fa9e4066Sahrens  */
729fa9e4066Sahrens 
730fa9e4066Sahrens /*
731ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
732ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
733fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
734fa9e4066Sahrens  */
735fa9e4066Sahrens uint64_t
736fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
737fa9e4066Sahrens {
7383d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
7393d7072f8Seschrock 
740ea8dc4b6Seschrock 	/*
7413d7072f8Seschrock 	 * Suspend scrub activity while we mess with the config.  We must do
7423d7072f8Seschrock 	 * this after acquiring the namespace lock to avoid a 3-way deadlock
7433d7072f8Seschrock 	 * with spa_scrub_stop() and the scrub thread.
744ea8dc4b6Seschrock 	 */
745ea8dc4b6Seschrock 	spa_scrub_suspend(spa);
746fa9e4066Sahrens 
747ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, spa);
748fa9e4066Sahrens 
749fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
750fa9e4066Sahrens }
751fa9e4066Sahrens 
752fa9e4066Sahrens /*
753fa9e4066Sahrens  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
754fa9e4066Sahrens  * locking of spa_vdev_enter(), we also want make sure the transactions have
755fa9e4066Sahrens  * synced to disk, and then update the global configuration cache with the new
756fa9e4066Sahrens  * information.
757fa9e4066Sahrens  */
758fa9e4066Sahrens int
759fa9e4066Sahrens spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
760fa9e4066Sahrens {
7610e34b6a7Sbonwick 	int config_changed = B_FALSE;
762ea8dc4b6Seschrock 
7630373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
7640e34b6a7Sbonwick 
7650e34b6a7Sbonwick 	/*
7660e34b6a7Sbonwick 	 * Reassess the DTLs.
7670e34b6a7Sbonwick 	 */
7680373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
7690e34b6a7Sbonwick 
7700e34b6a7Sbonwick 	/*
7710373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
7720e34b6a7Sbonwick 	 */
7730e34b6a7Sbonwick 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
7740e34b6a7Sbonwick 		config_changed = B_TRUE;
7750373e76bSbonwick 		spa_scrub_restart(spa, txg);
7760e34b6a7Sbonwick 	}
777ea8dc4b6Seschrock 
778ea8dc4b6Seschrock 	spa_config_exit(spa, spa);
779fa9e4066Sahrens 
780ea8dc4b6Seschrock 	/*
7815dabedeeSbonwick 	 * Allow scrubbing to resume.
782ea8dc4b6Seschrock 	 */
783ea8dc4b6Seschrock 	spa_scrub_resume(spa);
784fa9e4066Sahrens 
785fa9e4066Sahrens 	/*
786fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
787fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
788fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
789fa9e4066Sahrens 	 */
790fa9e4066Sahrens 	if (error == 0)
791fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
792fa9e4066Sahrens 
793fa9e4066Sahrens 	if (vd != NULL) {
794fa9e4066Sahrens 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
795fa9e4066Sahrens 		vdev_free(vd);
796fa9e4066Sahrens 	}
797fa9e4066Sahrens 
798fa9e4066Sahrens 	/*
7990e34b6a7Sbonwick 	 * If the config changed, update the config cache.
800fa9e4066Sahrens 	 */
8010e34b6a7Sbonwick 	if (config_changed)
802*c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
803ea8dc4b6Seschrock 
804ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
805fa9e4066Sahrens 
806fa9e4066Sahrens 	return (error);
807fa9e4066Sahrens }
808fa9e4066Sahrens 
809fa9e4066Sahrens /*
810fa9e4066Sahrens  * ==========================================================================
811fa9e4066Sahrens  * Miscellaneous functions
812fa9e4066Sahrens  * ==========================================================================
813fa9e4066Sahrens  */
814fa9e4066Sahrens 
815fa9e4066Sahrens /*
816fa9e4066Sahrens  * Rename a spa_t.
817fa9e4066Sahrens  */
818fa9e4066Sahrens int
819fa9e4066Sahrens spa_rename(const char *name, const char *newname)
820fa9e4066Sahrens {
821fa9e4066Sahrens 	spa_t *spa;
822fa9e4066Sahrens 	int err;
823fa9e4066Sahrens 
824fa9e4066Sahrens 	/*
825fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
826fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
827fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
828ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
829fa9e4066Sahrens 	 */
830fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
831fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
832fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
833fa9e4066Sahrens 		return (err);
834fa9e4066Sahrens 	}
835fa9e4066Sahrens 
836ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
837fa9e4066Sahrens 
838fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
839fa9e4066Sahrens 	spa_strfree(spa->spa_name);
840fa9e4066Sahrens 	spa->spa_name = spa_strdup(newname);
841fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
842fa9e4066Sahrens 
843fa9e4066Sahrens 	/*
844fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
845fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
846fa9e4066Sahrens 	 * during the sync.
847fa9e4066Sahrens 	 */
848fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
849fa9e4066Sahrens 
850ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
851fa9e4066Sahrens 
8520373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
853fa9e4066Sahrens 
854fa9e4066Sahrens 	/*
855fa9e4066Sahrens 	 * Sync the updated config cache.
856fa9e4066Sahrens 	 */
857*c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
858fa9e4066Sahrens 
859fa9e4066Sahrens 	spa_close(spa, FTAG);
860fa9e4066Sahrens 
861fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
862fa9e4066Sahrens 
863fa9e4066Sahrens 	return (0);
864fa9e4066Sahrens }
865fa9e4066Sahrens 
866fa9e4066Sahrens 
867fa9e4066Sahrens /*
868fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
869fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
870fa9e4066Sahrens  * specified device_guid.
871fa9e4066Sahrens  */
872fa9e4066Sahrens boolean_t
873fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
874fa9e4066Sahrens {
875fa9e4066Sahrens 	spa_t *spa;
876fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
877fa9e4066Sahrens 
878ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
879fa9e4066Sahrens 
880fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
881fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
882fa9e4066Sahrens 			continue;
883fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
884fa9e4066Sahrens 			continue;
88539c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
88639c23413Seschrock 			if (device_guid == 0)
88739c23413Seschrock 				break;
88839c23413Seschrock 
88939c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
89039c23413Seschrock 			    device_guid) != NULL)
89139c23413Seschrock 				break;
89239c23413Seschrock 
89339c23413Seschrock 			/*
8948654d025Sperrin 			 * Check any devices we may be in the process of adding.
89539c23413Seschrock 			 */
89639c23413Seschrock 			if (spa->spa_pending_vdev) {
89739c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
89839c23413Seschrock 				    device_guid) != NULL)
89939c23413Seschrock 					break;
90039c23413Seschrock 			}
90139c23413Seschrock 		}
902fa9e4066Sahrens 	}
903fa9e4066Sahrens 
904fa9e4066Sahrens 	return (spa != NULL);
905fa9e4066Sahrens }
906fa9e4066Sahrens 
907fa9e4066Sahrens char *
908fa9e4066Sahrens spa_strdup(const char *s)
909fa9e4066Sahrens {
910fa9e4066Sahrens 	size_t len;
911fa9e4066Sahrens 	char *new;
912fa9e4066Sahrens 
913fa9e4066Sahrens 	len = strlen(s);
914fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
915fa9e4066Sahrens 	bcopy(s, new, len);
916fa9e4066Sahrens 	new[len] = '\0';
917fa9e4066Sahrens 
918fa9e4066Sahrens 	return (new);
919fa9e4066Sahrens }
920fa9e4066Sahrens 
921fa9e4066Sahrens void
922fa9e4066Sahrens spa_strfree(char *s)
923fa9e4066Sahrens {
924fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
925fa9e4066Sahrens }
926fa9e4066Sahrens 
927fa9e4066Sahrens uint64_t
928fa9e4066Sahrens spa_get_random(uint64_t range)
929fa9e4066Sahrens {
930fa9e4066Sahrens 	uint64_t r;
931fa9e4066Sahrens 
932fa9e4066Sahrens 	ASSERT(range != 0);
933fa9e4066Sahrens 
934fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
935fa9e4066Sahrens 
936fa9e4066Sahrens 	return (r % range);
937fa9e4066Sahrens }
938fa9e4066Sahrens 
939fa9e4066Sahrens void
940d80c45e0Sbonwick sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
941fa9e4066Sahrens {
94244cd46caSbillm 	int d;
943fa9e4066Sahrens 
944fa9e4066Sahrens 	if (bp == NULL) {
945fbabab8fSmaybee 		(void) snprintf(buf, len, "<NULL>");
946fa9e4066Sahrens 		return;
947fa9e4066Sahrens 	}
948fa9e4066Sahrens 
949fa9e4066Sahrens 	if (BP_IS_HOLE(bp)) {
950fbabab8fSmaybee 		(void) snprintf(buf, len, "<hole>");
951fa9e4066Sahrens 		return;
952fa9e4066Sahrens 	}
953fa9e4066Sahrens 
95444cd46caSbillm 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
955fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LEVEL(bp),
956fa9e4066Sahrens 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
957fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LSIZE(bp),
95844cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp));
95944cd46caSbillm 
96044cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
961d80c45e0Sbonwick 		const dva_t *dva = &bp->blk_dva[d];
96244cd46caSbillm 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
96344cd46caSbillm 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
96444cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(dva),
96544cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(dva),
96644cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(dva));
96744cd46caSbillm 	}
96844cd46caSbillm 
96944cd46caSbillm 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
97044cd46caSbillm 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
971fa9e4066Sahrens 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
972fa9e4066Sahrens 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
973fa9e4066Sahrens 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
97444cd46caSbillm 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
975fa9e4066Sahrens 	    (u_longlong_t)bp->blk_birth,
976fa9e4066Sahrens 	    (u_longlong_t)bp->blk_fill,
977fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
978fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
979fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
980fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
981fa9e4066Sahrens }
982fa9e4066Sahrens 
983fa9e4066Sahrens void
984fa9e4066Sahrens spa_freeze(spa_t *spa)
985fa9e4066Sahrens {
986fa9e4066Sahrens 	uint64_t freeze_txg = 0;
987fa9e4066Sahrens 
988ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
989fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
990fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
991fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
992fa9e4066Sahrens 	}
993ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
994fa9e4066Sahrens 	if (freeze_txg != 0)
995fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
996fa9e4066Sahrens }
997fa9e4066Sahrens 
9980125049cSahrens void
9990125049cSahrens zfs_panic_recover(const char *fmt, ...)
10000125049cSahrens {
10010125049cSahrens 	va_list adx;
10020125049cSahrens 
10030125049cSahrens 	va_start(adx, fmt);
10040125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
10050125049cSahrens 	va_end(adx);
10060125049cSahrens }
10070125049cSahrens 
1008fa9e4066Sahrens /*
1009fa9e4066Sahrens  * ==========================================================================
1010fa9e4066Sahrens  * Accessor functions
1011fa9e4066Sahrens  * ==========================================================================
1012fa9e4066Sahrens  */
1013fa9e4066Sahrens 
1014fa9e4066Sahrens krwlock_t *
1015fa9e4066Sahrens spa_traverse_rwlock(spa_t *spa)
1016fa9e4066Sahrens {
1017fa9e4066Sahrens 	return (&spa->spa_traverse_lock);
1018fa9e4066Sahrens }
1019fa9e4066Sahrens 
1020fa9e4066Sahrens int
1021fa9e4066Sahrens spa_traverse_wanted(spa_t *spa)
1022fa9e4066Sahrens {
1023fa9e4066Sahrens 	return (spa->spa_traverse_wanted);
1024fa9e4066Sahrens }
1025fa9e4066Sahrens 
1026fa9e4066Sahrens dsl_pool_t *
1027fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1028fa9e4066Sahrens {
1029fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1030fa9e4066Sahrens }
1031fa9e4066Sahrens 
1032fa9e4066Sahrens blkptr_t *
1033fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1034fa9e4066Sahrens {
1035fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1036fa9e4066Sahrens }
1037fa9e4066Sahrens 
1038fa9e4066Sahrens void
1039fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1040fa9e4066Sahrens {
1041fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1042fa9e4066Sahrens }
1043fa9e4066Sahrens 
1044fa9e4066Sahrens void
1045fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1046fa9e4066Sahrens {
1047fa9e4066Sahrens 	if (spa->spa_root == NULL)
1048fa9e4066Sahrens 		buf[0] = '\0';
1049fa9e4066Sahrens 	else
1050fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1051fa9e4066Sahrens }
1052fa9e4066Sahrens 
1053fa9e4066Sahrens int
1054fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1055fa9e4066Sahrens {
1056fa9e4066Sahrens 	return (spa->spa_sync_pass);
1057fa9e4066Sahrens }
1058fa9e4066Sahrens 
1059fa9e4066Sahrens char *
1060fa9e4066Sahrens spa_name(spa_t *spa)
1061fa9e4066Sahrens {
1062fa9e4066Sahrens 	/*
1063fa9e4066Sahrens 	 * Accessing the name requires holding either the namespace lock or the
1064fa9e4066Sahrens 	 * config lock, both of which are required to do a rename.
1065fa9e4066Sahrens 	 */
1066fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1067e05725b1Sbonwick 	    spa_config_held(spa, RW_READER));
1068fa9e4066Sahrens 
1069fa9e4066Sahrens 	return (spa->spa_name);
1070fa9e4066Sahrens }
1071fa9e4066Sahrens 
1072fa9e4066Sahrens uint64_t
1073fa9e4066Sahrens spa_guid(spa_t *spa)
1074fa9e4066Sahrens {
1075b5989ec7Seschrock 	/*
1076b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1077b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1078b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1079b5989ec7Seschrock 	 * this case.
1080b5989ec7Seschrock 	 */
1081b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
1082b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1083b5989ec7Seschrock 	else
1084b5989ec7Seschrock 		return (spa->spa_load_guid);
1085fa9e4066Sahrens }
1086fa9e4066Sahrens 
1087fa9e4066Sahrens uint64_t
1088fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1089fa9e4066Sahrens {
1090fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1091fa9e4066Sahrens }
1092fa9e4066Sahrens 
1093fa9e4066Sahrens uint64_t
1094fa9e4066Sahrens spa_first_txg(spa_t *spa)
1095fa9e4066Sahrens {
1096fa9e4066Sahrens 	return (spa->spa_first_txg);
1097fa9e4066Sahrens }
1098fa9e4066Sahrens 
1099fa9e4066Sahrens int
1100fa9e4066Sahrens spa_state(spa_t *spa)
1101fa9e4066Sahrens {
1102fa9e4066Sahrens 	return (spa->spa_state);
1103fa9e4066Sahrens }
1104fa9e4066Sahrens 
1105fa9e4066Sahrens uint64_t
1106fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1107fa9e4066Sahrens {
1108fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1109fa9e4066Sahrens }
1110fa9e4066Sahrens 
1111fa9e4066Sahrens /*
111299653d4eSeschrock  * Return how much space is allocated in the pool (ie. sum of all asize)
1113fa9e4066Sahrens  */
1114fa9e4066Sahrens uint64_t
1115fa9e4066Sahrens spa_get_alloc(spa_t *spa)
1116fa9e4066Sahrens {
1117fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
1118fa9e4066Sahrens }
1119fa9e4066Sahrens 
1120fa9e4066Sahrens /*
112199653d4eSeschrock  * Return how much (raid-z inflated) space there is in the pool.
1122fa9e4066Sahrens  */
1123fa9e4066Sahrens uint64_t
1124fa9e4066Sahrens spa_get_space(spa_t *spa)
1125fa9e4066Sahrens {
1126fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_space);
1127fa9e4066Sahrens }
1128fa9e4066Sahrens 
112999653d4eSeschrock /*
113099653d4eSeschrock  * Return the amount of raid-z-deflated space in the pool.
113199653d4eSeschrock  */
113299653d4eSeschrock uint64_t
113399653d4eSeschrock spa_get_dspace(spa_t *spa)
113499653d4eSeschrock {
113599653d4eSeschrock 	if (spa->spa_deflate)
113699653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
113799653d4eSeschrock 	else
113899653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_space);
113999653d4eSeschrock }
114099653d4eSeschrock 
1141fa9e4066Sahrens /* ARGSUSED */
1142fa9e4066Sahrens uint64_t
1143fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1144fa9e4066Sahrens {
1145fa9e4066Sahrens 	/*
1146fa9e4066Sahrens 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
1147fa9e4066Sahrens 	 * case the space requirement is exactly 2x; so just assume that.
114844cd46caSbillm 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
114944cd46caSbillm 	 * we have to multiply by a total of 6x.
115044cd46caSbillm 	 */
115144cd46caSbillm 	return (lsize * 6);
115244cd46caSbillm }
115344cd46caSbillm 
11540a4e9518Sgw /*
11550a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
11560a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
11570a4e9518Sgw  */
11580a4e9518Sgw uint8_t
11590a4e9518Sgw spa_get_failmode(spa_t *spa)
11600a4e9518Sgw {
11610a4e9518Sgw 	return (spa->spa_failmode);
11620a4e9518Sgw }
11630a4e9518Sgw 
116444cd46caSbillm uint64_t
116544cd46caSbillm spa_version(spa_t *spa)
116644cd46caSbillm {
116744cd46caSbillm 	return (spa->spa_ubsync.ub_version);
116844cd46caSbillm }
116944cd46caSbillm 
117044cd46caSbillm int
117144cd46caSbillm spa_max_replication(spa_t *spa)
117244cd46caSbillm {
117344cd46caSbillm 	/*
1174e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
117544cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
117644cd46caSbillm 	 * replication level accordingly.
1177fa9e4066Sahrens 	 */
1178e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
117944cd46caSbillm 		return (1);
118044cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1181fa9e4066Sahrens }
1182fa9e4066Sahrens 
118399653d4eSeschrock uint64_t
118499653d4eSeschrock bp_get_dasize(spa_t *spa, const blkptr_t *bp)
118599653d4eSeschrock {
118699653d4eSeschrock 	int sz = 0, i;
118799653d4eSeschrock 
118899653d4eSeschrock 	if (!spa->spa_deflate)
118999653d4eSeschrock 		return (BP_GET_ASIZE(bp));
119099653d4eSeschrock 
11911934e92fSmaybee 	spa_config_enter(spa, RW_READER, FTAG);
119299653d4eSeschrock 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
119399653d4eSeschrock 		vdev_t *vd =
119499653d4eSeschrock 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
11951934e92fSmaybee 		if (vd)
11961934e92fSmaybee 			sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >>
11971934e92fSmaybee 			    SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
119899653d4eSeschrock 	}
11991934e92fSmaybee 	spa_config_exit(spa, FTAG);
120099653d4eSeschrock 	return (sz);
120199653d4eSeschrock }
120299653d4eSeschrock 
1203fa9e4066Sahrens /*
1204fa9e4066Sahrens  * ==========================================================================
1205fa9e4066Sahrens  * Initialization and Termination
1206fa9e4066Sahrens  * ==========================================================================
1207fa9e4066Sahrens  */
1208fa9e4066Sahrens 
1209fa9e4066Sahrens static int
1210fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1211fa9e4066Sahrens {
1212fa9e4066Sahrens 	const spa_t *s1 = a1;
1213fa9e4066Sahrens 	const spa_t *s2 = a2;
1214fa9e4066Sahrens 	int s;
1215fa9e4066Sahrens 
1216fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1217fa9e4066Sahrens 	if (s > 0)
1218fa9e4066Sahrens 		return (1);
1219fa9e4066Sahrens 	if (s < 0)
1220fa9e4066Sahrens 		return (-1);
1221fa9e4066Sahrens 	return (0);
1222fa9e4066Sahrens }
1223fa9e4066Sahrens 
12240373e76bSbonwick int
12250373e76bSbonwick spa_busy(void)
12260373e76bSbonwick {
12270373e76bSbonwick 	return (spa_active_count);
12280373e76bSbonwick }
12290373e76bSbonwick 
1230e7cbe64fSgw void
1231e7cbe64fSgw spa_boot_init()
1232e7cbe64fSgw {
1233e7cbe64fSgw 	spa_config_load();
1234e7cbe64fSgw }
1235e7cbe64fSgw 
1236fa9e4066Sahrens void
1237fa9e4066Sahrens spa_init(int mode)
1238fa9e4066Sahrens {
1239fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1240c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1241fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1242fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1243fa9e4066Sahrens 
1244fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1245fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1246fa9e4066Sahrens 
1247fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1248fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1249fa94a07fSbrendan 
1250fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1251fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
125299653d4eSeschrock 
1253fa9e4066Sahrens 	spa_mode = mode;
1254fa9e4066Sahrens 
1255fa9e4066Sahrens 	refcount_init();
1256fa9e4066Sahrens 	unique_init();
1257fa9e4066Sahrens 	zio_init();
1258fa9e4066Sahrens 	dmu_init();
1259fa9e4066Sahrens 	zil_init();
126087db74c1Sek 	vdev_cache_stat_init();
126191ebeef5Sahrens 	zfs_prop_init();
1262990b4856Slling 	zpool_prop_init();
1263fa9e4066Sahrens 	spa_config_load();
1264fa9e4066Sahrens }
1265fa9e4066Sahrens 
1266fa9e4066Sahrens void
1267fa9e4066Sahrens spa_fini(void)
1268fa9e4066Sahrens {
1269fa9e4066Sahrens 	spa_evict_all();
1270fa9e4066Sahrens 
127187db74c1Sek 	vdev_cache_stat_fini();
1272fa9e4066Sahrens 	zil_fini();
1273fa9e4066Sahrens 	dmu_fini();
1274fa9e4066Sahrens 	zio_fini();
127591ebeef5Sahrens 	unique_fini();
1276fa9e4066Sahrens 	refcount_fini();
1277fa9e4066Sahrens 
1278fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
127999653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1280fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1281fa9e4066Sahrens 
1282fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1283fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1284c25056deSgw 	mutex_destroy(&spa_spare_lock);
1285fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1286fa9e4066Sahrens }
12876ce0521aSperrin 
12886ce0521aSperrin /*
12896ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
12906ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
12916ce0521aSperrin  * performance and not correctness
12926ce0521aSperrin  */
12936ce0521aSperrin boolean_t
12946ce0521aSperrin spa_has_slogs(spa_t *spa)
12956ce0521aSperrin {
12966ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
12976ce0521aSperrin }
1298