xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 088f389458728c464569a5506b58070254fa4f7d)
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
77*088f3894Sahrens  *	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 
195e05725b1Sbonwick /*
196e05725b1Sbonwick  * ==========================================================================
197e05725b1Sbonwick  * SPA config locking
198e05725b1Sbonwick  * ==========================================================================
199e05725b1Sbonwick  */
200e05725b1Sbonwick static void
201e05725b1Sbonwick spa_config_lock_init(spa_config_lock_t *scl)
202e05725b1Sbonwick {
203e05725b1Sbonwick 	mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
204e05725b1Sbonwick 	scl->scl_writer = NULL;
205e05725b1Sbonwick 	cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
206e05725b1Sbonwick 	refcount_create(&scl->scl_count);
207e05725b1Sbonwick }
208e05725b1Sbonwick 
209e05725b1Sbonwick static void
210e05725b1Sbonwick spa_config_lock_destroy(spa_config_lock_t *scl)
211e05725b1Sbonwick {
212e05725b1Sbonwick 	mutex_destroy(&scl->scl_lock);
213e05725b1Sbonwick 	ASSERT(scl->scl_writer == NULL);
214e05725b1Sbonwick 	cv_destroy(&scl->scl_cv);
215e05725b1Sbonwick 	refcount_destroy(&scl->scl_count);
216e05725b1Sbonwick }
217e05725b1Sbonwick 
218e05725b1Sbonwick void
219e05725b1Sbonwick spa_config_enter(spa_t *spa, krw_t rw, void *tag)
220e05725b1Sbonwick {
221e05725b1Sbonwick 	spa_config_lock_t *scl = &spa->spa_config_lock;
222e05725b1Sbonwick 
223e05725b1Sbonwick 	mutex_enter(&scl->scl_lock);
224e05725b1Sbonwick 
225e05725b1Sbonwick 	if (rw == RW_READER) {
226e05725b1Sbonwick 		while (scl->scl_writer != NULL && scl->scl_writer != curthread)
227e05725b1Sbonwick 			cv_wait(&scl->scl_cv, &scl->scl_lock);
228e05725b1Sbonwick 	} else {
229e05725b1Sbonwick 		while (!refcount_is_zero(&scl->scl_count) &&
230e05725b1Sbonwick 		    scl->scl_writer != curthread)
231e05725b1Sbonwick 			cv_wait(&scl->scl_cv, &scl->scl_lock);
232e05725b1Sbonwick 		scl->scl_writer = curthread;
233e05725b1Sbonwick 	}
234e05725b1Sbonwick 
235e05725b1Sbonwick 	(void) refcount_add(&scl->scl_count, tag);
236e05725b1Sbonwick 
237e05725b1Sbonwick 	mutex_exit(&scl->scl_lock);
238e05725b1Sbonwick }
239e05725b1Sbonwick 
240e05725b1Sbonwick void
241e05725b1Sbonwick spa_config_exit(spa_t *spa, void *tag)
242e05725b1Sbonwick {
243e05725b1Sbonwick 	spa_config_lock_t *scl = &spa->spa_config_lock;
244e05725b1Sbonwick 
245e05725b1Sbonwick 	mutex_enter(&scl->scl_lock);
246e05725b1Sbonwick 
247e05725b1Sbonwick 	ASSERT(!refcount_is_zero(&scl->scl_count));
248e05725b1Sbonwick 
249e05725b1Sbonwick 	if (refcount_remove(&scl->scl_count, tag) == 0) {
250e05725b1Sbonwick 		cv_broadcast(&scl->scl_cv);
251e05725b1Sbonwick 		ASSERT(scl->scl_writer == NULL || scl->scl_writer == curthread);
252e05725b1Sbonwick 		scl->scl_writer = NULL;  /* OK in either case */
253e05725b1Sbonwick 	}
254e05725b1Sbonwick 
255e05725b1Sbonwick 	mutex_exit(&scl->scl_lock);
256e05725b1Sbonwick }
257e05725b1Sbonwick 
258e05725b1Sbonwick boolean_t
259e05725b1Sbonwick spa_config_held(spa_t *spa, krw_t rw)
260e05725b1Sbonwick {
261e05725b1Sbonwick 	spa_config_lock_t *scl = &spa->spa_config_lock;
262e05725b1Sbonwick 
263e05725b1Sbonwick 	if (rw == RW_READER)
264e05725b1Sbonwick 		return (!refcount_is_zero(&scl->scl_count));
265e05725b1Sbonwick 	else
266e05725b1Sbonwick 		return (scl->scl_writer == curthread);
267e05725b1Sbonwick }
268e05725b1Sbonwick 
269fa9e4066Sahrens /*
270fa9e4066Sahrens  * ==========================================================================
271fa9e4066Sahrens  * SPA namespace functions
272fa9e4066Sahrens  * ==========================================================================
273fa9e4066Sahrens  */
274fa9e4066Sahrens 
275fa9e4066Sahrens /*
276fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
277fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
278fa9e4066Sahrens  */
279fa9e4066Sahrens spa_t *
280fa9e4066Sahrens spa_lookup(const char *name)
281fa9e4066Sahrens {
282fa9e4066Sahrens 	spa_t search, *spa;
283fa9e4066Sahrens 	avl_index_t where;
28440feaa91Sahrens 	char c;
28540feaa91Sahrens 	char *cp;
286fa9e4066Sahrens 
287fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
288fa9e4066Sahrens 
28940feaa91Sahrens 	/*
29040feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
29140feaa91Sahrens 	 * just use that.
29240feaa91Sahrens 	 */
29340feaa91Sahrens 	cp = strpbrk(name, "/@");
29440feaa91Sahrens 	if (cp) {
29540feaa91Sahrens 		c = *cp;
29640feaa91Sahrens 		*cp = '\0';
29740feaa91Sahrens 	}
29840feaa91Sahrens 
299fa9e4066Sahrens 	search.spa_name = (char *)name;
300fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
301fa9e4066Sahrens 
30240feaa91Sahrens 	if (cp)
30340feaa91Sahrens 		*cp = c;
30440feaa91Sahrens 
305fa9e4066Sahrens 	return (spa);
306fa9e4066Sahrens }
307fa9e4066Sahrens 
308fa9e4066Sahrens /*
309fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
310fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
311fa9e4066Sahrens  * exist by calling spa_lookup() first.
312fa9e4066Sahrens  */
313fa9e4066Sahrens spa_t *
3140373e76bSbonwick spa_add(const char *name, const char *altroot)
315fa9e4066Sahrens {
316fa9e4066Sahrens 	spa_t *spa;
317c5904d13Seschrock 	spa_config_dirent_t *dp;
318fa9e4066Sahrens 
319fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
320fa9e4066Sahrens 
321fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
322fa9e4066Sahrens 
323c25056deSgw 	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
324c25056deSgw 
325c25056deSgw 	mutex_init(&spa->spa_uberblock_lock, NULL, MUTEX_DEFAULT, NULL);
326c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
327c25056deSgw 	mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL);
328c25056deSgw 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
329c25056deSgw 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
330c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
331c25056deSgw 	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
332c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
333c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
334c25056deSgw 
335c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
336c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
337c25056deSgw 
338fa9e4066Sahrens 	spa->spa_name = spa_strdup(name);
339fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
340fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
3410373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
342fa9e4066Sahrens 
343fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
344e05725b1Sbonwick 	spa_config_lock_init(&spa->spa_config_lock);
345fa9e4066Sahrens 
346fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
347fa9e4066Sahrens 
3480a4e9518Sgw 	mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL);
3490a4e9518Sgw 
3500373e76bSbonwick 	/*
3510373e76bSbonwick 	 * Set the alternate root, if there is one.
3520373e76bSbonwick 	 */
3530373e76bSbonwick 	if (altroot) {
3540373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
3550373e76bSbonwick 		spa_active_count++;
3560373e76bSbonwick 	}
3570373e76bSbonwick 
358c5904d13Seschrock 	/*
359c5904d13Seschrock 	 * Every pool starts with the default cachefile
360c5904d13Seschrock 	 */
361c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
362c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
363c5904d13Seschrock 
364c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
365c5904d13Seschrock 	dp->scd_path = spa_strdup(spa_config_path);
366c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
367c5904d13Seschrock 
368fa9e4066Sahrens 	return (spa);
369fa9e4066Sahrens }
370fa9e4066Sahrens 
371fa9e4066Sahrens /*
372fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
373fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
374fa9e4066Sahrens  * deactivated.
375fa9e4066Sahrens  */
376fa9e4066Sahrens void
377fa9e4066Sahrens spa_remove(spa_t *spa)
378fa9e4066Sahrens {
379c5904d13Seschrock 	spa_config_dirent_t *dp;
380c5904d13Seschrock 
381fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
382fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
383fa9e4066Sahrens 
384fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
385fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
386fa9e4066Sahrens 
3870373e76bSbonwick 	if (spa->spa_root) {
388fa9e4066Sahrens 		spa_strfree(spa->spa_root);
3890373e76bSbonwick 		spa_active_count--;
3900373e76bSbonwick 	}
391fa9e4066Sahrens 
392fa9e4066Sahrens 	if (spa->spa_name)
393fa9e4066Sahrens 		spa_strfree(spa->spa_name);
394fa9e4066Sahrens 
395c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
396c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
397c5904d13Seschrock 		if (dp->scd_path != NULL)
398c5904d13Seschrock 			spa_strfree(dp->scd_path);
399c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
400c5904d13Seschrock 	}
401c5904d13Seschrock 
402c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
4032f8aaab3Seschrock 
404fa9e4066Sahrens 	spa_config_set(spa, NULL);
405fa9e4066Sahrens 
406fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
40791ebeef5Sahrens 
408e05725b1Sbonwick 	spa_config_lock_destroy(&spa->spa_config_lock);
409fa9e4066Sahrens 
410c25056deSgw 	rw_destroy(&spa->spa_traverse_lock);
411c25056deSgw 
412c25056deSgw 	cv_destroy(&spa->spa_async_cv);
413c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
414c25056deSgw 
415c25056deSgw 	mutex_destroy(&spa->spa_uberblock_lock);
4165ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
417c25056deSgw 	mutex_destroy(&spa->spa_config_cache_lock);
418c25056deSgw 	mutex_destroy(&spa->spa_scrub_lock);
419c25056deSgw 	mutex_destroy(&spa->spa_errlog_lock);
420c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
421c25056deSgw 	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
42206eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
423b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
4240a4e9518Sgw 	mutex_destroy(&spa->spa_zio_lock);
4255ad82045Snd 
426fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
427fa9e4066Sahrens }
428fa9e4066Sahrens 
429fa9e4066Sahrens /*
430fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
431fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
432fa9e4066Sahrens  */
433fa9e4066Sahrens spa_t *
434fa9e4066Sahrens spa_next(spa_t *prev)
435fa9e4066Sahrens {
436fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
437fa9e4066Sahrens 
438fa9e4066Sahrens 	if (prev)
439fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
440fa9e4066Sahrens 	else
441fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
442fa9e4066Sahrens }
443fa9e4066Sahrens 
444fa9e4066Sahrens /*
445fa9e4066Sahrens  * ==========================================================================
446fa9e4066Sahrens  * SPA refcount functions
447fa9e4066Sahrens  * ==========================================================================
448fa9e4066Sahrens  */
449fa9e4066Sahrens 
450fa9e4066Sahrens /*
451fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
452fa9e4066Sahrens  * have the namespace lock held.
453fa9e4066Sahrens  */
454fa9e4066Sahrens void
455fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
456fa9e4066Sahrens {
457*088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
458fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
459fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
460fa9e4066Sahrens }
461fa9e4066Sahrens 
462fa9e4066Sahrens /*
463fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
464fa9e4066Sahrens  * have the namespace lock held.
465fa9e4066Sahrens  */
466fa9e4066Sahrens void
467fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
468fa9e4066Sahrens {
469*088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
470fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
471fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
472fa9e4066Sahrens }
473fa9e4066Sahrens 
474fa9e4066Sahrens /*
475fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
476*088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
477fa9e4066Sahrens  * number of references acquired when opening a pool
478fa9e4066Sahrens  */
479fa9e4066Sahrens boolean_t
480fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
481fa9e4066Sahrens {
482fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
483fa9e4066Sahrens 
484*088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
485fa9e4066Sahrens }
486fa9e4066Sahrens 
48799653d4eSeschrock /*
48899653d4eSeschrock  * ==========================================================================
489fa94a07fSbrendan  * SPA spare and l2cache tracking
49099653d4eSeschrock  * ==========================================================================
49199653d4eSeschrock  */
49299653d4eSeschrock 
493fa94a07fSbrendan /*
494fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
495fa94a07fSbrendan  * for 'auxiliary' devices.
496fa94a07fSbrendan  */
497fa94a07fSbrendan 
498fa94a07fSbrendan typedef struct spa_aux {
499fa94a07fSbrendan 	uint64_t	aux_guid;
500fa94a07fSbrendan 	uint64_t	aux_pool;
501fa94a07fSbrendan 	avl_node_t	aux_avl;
502fa94a07fSbrendan 	int		aux_count;
503fa94a07fSbrendan } spa_aux_t;
504fa94a07fSbrendan 
505fa94a07fSbrendan static int
506fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
507fa94a07fSbrendan {
508fa94a07fSbrendan 	const spa_aux_t *sa = a;
509fa94a07fSbrendan 	const spa_aux_t *sb = b;
510fa94a07fSbrendan 
511fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
512fa94a07fSbrendan 		return (-1);
513fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
514fa94a07fSbrendan 		return (1);
515fa94a07fSbrendan 	else
516fa94a07fSbrendan 		return (0);
517fa94a07fSbrendan }
518fa94a07fSbrendan 
519fa94a07fSbrendan void
520fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
521fa94a07fSbrendan {
522fa94a07fSbrendan 	avl_index_t where;
523fa94a07fSbrendan 	spa_aux_t search;
524fa94a07fSbrendan 	spa_aux_t *aux;
525fa94a07fSbrendan 
526fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
527fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
528fa94a07fSbrendan 		aux->aux_count++;
529fa94a07fSbrendan 	} else {
530fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
531fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
532fa94a07fSbrendan 		aux->aux_count = 1;
533fa94a07fSbrendan 		avl_insert(avl, aux, where);
534fa94a07fSbrendan 	}
535fa94a07fSbrendan }
536fa94a07fSbrendan 
537fa94a07fSbrendan void
538fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
539fa94a07fSbrendan {
540fa94a07fSbrendan 	spa_aux_t search;
541fa94a07fSbrendan 	spa_aux_t *aux;
542fa94a07fSbrendan 	avl_index_t where;
543fa94a07fSbrendan 
544fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
545fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
546fa94a07fSbrendan 
547fa94a07fSbrendan 	ASSERT(aux != NULL);
548fa94a07fSbrendan 
549fa94a07fSbrendan 	if (--aux->aux_count == 0) {
550fa94a07fSbrendan 		avl_remove(avl, aux);
551fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
552fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
553fa94a07fSbrendan 		aux->aux_pool = 0ULL;
554fa94a07fSbrendan 	}
555fa94a07fSbrendan }
556fa94a07fSbrendan 
557fa94a07fSbrendan boolean_t
558fa94a07fSbrendan spa_aux_exists(uint64_t guid, uint64_t *pool, avl_tree_t *avl)
559fa94a07fSbrendan {
560fa94a07fSbrendan 	spa_aux_t search, *found;
561fa94a07fSbrendan 	avl_index_t where;
562fa94a07fSbrendan 
563fa94a07fSbrendan 	search.aux_guid = guid;
564fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
565fa94a07fSbrendan 
566fa94a07fSbrendan 	if (pool) {
567fa94a07fSbrendan 		if (found)
568fa94a07fSbrendan 			*pool = found->aux_pool;
569fa94a07fSbrendan 		else
570fa94a07fSbrendan 			*pool = 0ULL;
571fa94a07fSbrendan 	}
572fa94a07fSbrendan 
573fa94a07fSbrendan 	return (found != NULL);
574fa94a07fSbrendan }
575fa94a07fSbrendan 
576fa94a07fSbrendan void
577fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
578fa94a07fSbrendan {
579fa94a07fSbrendan 	spa_aux_t search, *found;
580fa94a07fSbrendan 	avl_index_t where;
581fa94a07fSbrendan 
582fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
583fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
584fa94a07fSbrendan 	ASSERT(found != NULL);
585fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
586fa94a07fSbrendan 
587fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
588fa94a07fSbrendan }
589fa94a07fSbrendan 
59099653d4eSeschrock /*
59139c23413Seschrock  * Spares are tracked globally due to the following constraints:
59239c23413Seschrock  *
59339c23413Seschrock  * 	- A spare may be part of multiple pools.
59439c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
59539c23413Seschrock  *	  another pool.
59639c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
59739c23413Seschrock  *	  the target is a spare in the same pool.
59839c23413Seschrock  *
59939c23413Seschrock  * We keep track of all spares on the system through the use of a reference
60039c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
60139c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
60239c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
60339c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
60439c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
60539c23413Seschrock  *
60639c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
60739c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
60839c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
60939c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
61099653d4eSeschrock  */
61139c23413Seschrock 
61299653d4eSeschrock static int
61399653d4eSeschrock spa_spare_compare(const void *a, const void *b)
61499653d4eSeschrock {
615fa94a07fSbrendan 	return (spa_aux_compare(a, b));
61699653d4eSeschrock }
61799653d4eSeschrock 
61899653d4eSeschrock void
61939c23413Seschrock spa_spare_add(vdev_t *vd)
62099653d4eSeschrock {
62199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
62239c23413Seschrock 	ASSERT(!vd->vdev_isspare);
623fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
62439c23413Seschrock 	vd->vdev_isspare = B_TRUE;
62599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
62699653d4eSeschrock }
62799653d4eSeschrock 
62899653d4eSeschrock void
62939c23413Seschrock spa_spare_remove(vdev_t *vd)
63099653d4eSeschrock {
63199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
63239c23413Seschrock 	ASSERT(vd->vdev_isspare);
633fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
63439c23413Seschrock 	vd->vdev_isspare = B_FALSE;
63599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
63699653d4eSeschrock }
63799653d4eSeschrock 
63899653d4eSeschrock boolean_t
63939c23413Seschrock spa_spare_exists(uint64_t guid, uint64_t *pool)
64099653d4eSeschrock {
641fa94a07fSbrendan 	boolean_t found;
64299653d4eSeschrock 
64399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
644fa94a07fSbrendan 	found = spa_aux_exists(guid, pool, &spa_spare_avl);
64599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
64699653d4eSeschrock 
647fa94a07fSbrendan 	return (found);
64839c23413Seschrock }
64939c23413Seschrock 
65039c23413Seschrock void
65139c23413Seschrock spa_spare_activate(vdev_t *vd)
65239c23413Seschrock {
65339c23413Seschrock 	mutex_enter(&spa_spare_lock);
65439c23413Seschrock 	ASSERT(vd->vdev_isspare);
655fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
656fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
657fa94a07fSbrendan }
65839c23413Seschrock 
659fa94a07fSbrendan /*
660fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
661fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
662fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
663fa94a07fSbrendan  */
66439c23413Seschrock 
665fa94a07fSbrendan static int
666fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
667fa94a07fSbrendan {
668fa94a07fSbrendan 	return (spa_aux_compare(a, b));
669fa94a07fSbrendan }
670fa94a07fSbrendan 
671fa94a07fSbrendan void
672fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
673fa94a07fSbrendan {
674fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
675fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
676fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
677fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
678fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
679fa94a07fSbrendan }
680fa94a07fSbrendan 
681fa94a07fSbrendan void
682fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
683fa94a07fSbrendan {
684fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
685fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
686fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
687fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
688fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
689fa94a07fSbrendan }
690fa94a07fSbrendan 
691fa94a07fSbrendan boolean_t
692fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
693fa94a07fSbrendan {
694fa94a07fSbrendan 	boolean_t found;
695fa94a07fSbrendan 
696fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
697fa94a07fSbrendan 	found = spa_aux_exists(guid, pool, &spa_l2cache_avl);
698fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
699fa94a07fSbrendan 
700fa94a07fSbrendan 	return (found);
701fa94a07fSbrendan }
702fa94a07fSbrendan 
703fa94a07fSbrendan void
704fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
705fa94a07fSbrendan {
706fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
707fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
708fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
709fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
710fa94a07fSbrendan }
711fa94a07fSbrendan 
712fa94a07fSbrendan void
713fa94a07fSbrendan spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
714fa94a07fSbrendan {
715fa94a07fSbrendan 	vdev_space_update(vd, space, alloc, B_FALSE);
71699653d4eSeschrock }
71799653d4eSeschrock 
718fa9e4066Sahrens /*
719fa9e4066Sahrens  * ==========================================================================
720fa9e4066Sahrens  * SPA vdev locking
721fa9e4066Sahrens  * ==========================================================================
722fa9e4066Sahrens  */
723fa9e4066Sahrens 
724fa9e4066Sahrens /*
725ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
726ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
727fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
728fa9e4066Sahrens  */
729fa9e4066Sahrens uint64_t
730fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
731fa9e4066Sahrens {
7323d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
7333d7072f8Seschrock 
734ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, spa);
735fa9e4066Sahrens 
736fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
737fa9e4066Sahrens }
738fa9e4066Sahrens 
739fa9e4066Sahrens /*
740fa9e4066Sahrens  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
741fa9e4066Sahrens  * locking of spa_vdev_enter(), we also want make sure the transactions have
742fa9e4066Sahrens  * synced to disk, and then update the global configuration cache with the new
743fa9e4066Sahrens  * information.
744fa9e4066Sahrens  */
745fa9e4066Sahrens int
746fa9e4066Sahrens spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
747fa9e4066Sahrens {
7480e34b6a7Sbonwick 	int config_changed = B_FALSE;
749ea8dc4b6Seschrock 
7500373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
7510e34b6a7Sbonwick 
7520e34b6a7Sbonwick 	/*
7530e34b6a7Sbonwick 	 * Reassess the DTLs.
7540e34b6a7Sbonwick 	 */
7550373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
7560e34b6a7Sbonwick 
7570e34b6a7Sbonwick 	/*
7580373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
7590e34b6a7Sbonwick 	 */
7600e34b6a7Sbonwick 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
761*088f3894Sahrens 		dsl_pool_scrub_restart(spa->spa_dsl_pool);
7620e34b6a7Sbonwick 		config_changed = B_TRUE;
7630e34b6a7Sbonwick 	}
764ea8dc4b6Seschrock 
765ea8dc4b6Seschrock 	spa_config_exit(spa, spa);
766fa9e4066Sahrens 
767fa9e4066Sahrens 	/*
768fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
769fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
770fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
771fa9e4066Sahrens 	 */
772fa9e4066Sahrens 	if (error == 0)
773fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
774fa9e4066Sahrens 
775fa9e4066Sahrens 	if (vd != NULL) {
776fa9e4066Sahrens 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
777fa9e4066Sahrens 		vdev_free(vd);
778fa9e4066Sahrens 	}
779fa9e4066Sahrens 
780fa9e4066Sahrens 	/*
7810e34b6a7Sbonwick 	 * If the config changed, update the config cache.
782fa9e4066Sahrens 	 */
7830e34b6a7Sbonwick 	if (config_changed)
784c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
785ea8dc4b6Seschrock 
786ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
787fa9e4066Sahrens 
788fa9e4066Sahrens 	return (error);
789fa9e4066Sahrens }
790fa9e4066Sahrens 
791fa9e4066Sahrens /*
792fa9e4066Sahrens  * ==========================================================================
793fa9e4066Sahrens  * Miscellaneous functions
794fa9e4066Sahrens  * ==========================================================================
795fa9e4066Sahrens  */
796fa9e4066Sahrens 
797fa9e4066Sahrens /*
798fa9e4066Sahrens  * Rename a spa_t.
799fa9e4066Sahrens  */
800fa9e4066Sahrens int
801fa9e4066Sahrens spa_rename(const char *name, const char *newname)
802fa9e4066Sahrens {
803fa9e4066Sahrens 	spa_t *spa;
804fa9e4066Sahrens 	int err;
805fa9e4066Sahrens 
806fa9e4066Sahrens 	/*
807fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
808fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
809fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
810ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
811fa9e4066Sahrens 	 */
812fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
813fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
814fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
815fa9e4066Sahrens 		return (err);
816fa9e4066Sahrens 	}
817fa9e4066Sahrens 
818ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
819fa9e4066Sahrens 
820fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
821fa9e4066Sahrens 	spa_strfree(spa->spa_name);
822fa9e4066Sahrens 	spa->spa_name = spa_strdup(newname);
823fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
824fa9e4066Sahrens 
825fa9e4066Sahrens 	/*
826fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
827fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
828fa9e4066Sahrens 	 * during the sync.
829fa9e4066Sahrens 	 */
830fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
831fa9e4066Sahrens 
832ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
833fa9e4066Sahrens 
8340373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
835fa9e4066Sahrens 
836fa9e4066Sahrens 	/*
837fa9e4066Sahrens 	 * Sync the updated config cache.
838fa9e4066Sahrens 	 */
839c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
840fa9e4066Sahrens 
841fa9e4066Sahrens 	spa_close(spa, FTAG);
842fa9e4066Sahrens 
843fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
844fa9e4066Sahrens 
845fa9e4066Sahrens 	return (0);
846fa9e4066Sahrens }
847fa9e4066Sahrens 
848fa9e4066Sahrens 
849fa9e4066Sahrens /*
850fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
851fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
852fa9e4066Sahrens  * specified device_guid.
853fa9e4066Sahrens  */
854fa9e4066Sahrens boolean_t
855fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
856fa9e4066Sahrens {
857fa9e4066Sahrens 	spa_t *spa;
858fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
859fa9e4066Sahrens 
860ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
861fa9e4066Sahrens 
862fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
863fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
864fa9e4066Sahrens 			continue;
865fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
866fa9e4066Sahrens 			continue;
86739c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
86839c23413Seschrock 			if (device_guid == 0)
86939c23413Seschrock 				break;
87039c23413Seschrock 
87139c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
87239c23413Seschrock 			    device_guid) != NULL)
87339c23413Seschrock 				break;
87439c23413Seschrock 
87539c23413Seschrock 			/*
8768654d025Sperrin 			 * Check any devices we may be in the process of adding.
87739c23413Seschrock 			 */
87839c23413Seschrock 			if (spa->spa_pending_vdev) {
87939c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
88039c23413Seschrock 				    device_guid) != NULL)
88139c23413Seschrock 					break;
88239c23413Seschrock 			}
88339c23413Seschrock 		}
884fa9e4066Sahrens 	}
885fa9e4066Sahrens 
886fa9e4066Sahrens 	return (spa != NULL);
887fa9e4066Sahrens }
888fa9e4066Sahrens 
889fa9e4066Sahrens char *
890fa9e4066Sahrens spa_strdup(const char *s)
891fa9e4066Sahrens {
892fa9e4066Sahrens 	size_t len;
893fa9e4066Sahrens 	char *new;
894fa9e4066Sahrens 
895fa9e4066Sahrens 	len = strlen(s);
896fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
897fa9e4066Sahrens 	bcopy(s, new, len);
898fa9e4066Sahrens 	new[len] = '\0';
899fa9e4066Sahrens 
900fa9e4066Sahrens 	return (new);
901fa9e4066Sahrens }
902fa9e4066Sahrens 
903fa9e4066Sahrens void
904fa9e4066Sahrens spa_strfree(char *s)
905fa9e4066Sahrens {
906fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
907fa9e4066Sahrens }
908fa9e4066Sahrens 
909fa9e4066Sahrens uint64_t
910fa9e4066Sahrens spa_get_random(uint64_t range)
911fa9e4066Sahrens {
912fa9e4066Sahrens 	uint64_t r;
913fa9e4066Sahrens 
914fa9e4066Sahrens 	ASSERT(range != 0);
915fa9e4066Sahrens 
916fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
917fa9e4066Sahrens 
918fa9e4066Sahrens 	return (r % range);
919fa9e4066Sahrens }
920fa9e4066Sahrens 
921fa9e4066Sahrens void
922d80c45e0Sbonwick sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
923fa9e4066Sahrens {
92444cd46caSbillm 	int d;
925fa9e4066Sahrens 
926fa9e4066Sahrens 	if (bp == NULL) {
927fbabab8fSmaybee 		(void) snprintf(buf, len, "<NULL>");
928fa9e4066Sahrens 		return;
929fa9e4066Sahrens 	}
930fa9e4066Sahrens 
931fa9e4066Sahrens 	if (BP_IS_HOLE(bp)) {
932fbabab8fSmaybee 		(void) snprintf(buf, len, "<hole>");
933fa9e4066Sahrens 		return;
934fa9e4066Sahrens 	}
935fa9e4066Sahrens 
93644cd46caSbillm 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
937fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LEVEL(bp),
938fa9e4066Sahrens 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
939fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LSIZE(bp),
94044cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp));
94144cd46caSbillm 
94244cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
943d80c45e0Sbonwick 		const dva_t *dva = &bp->blk_dva[d];
94444cd46caSbillm 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
94544cd46caSbillm 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
94644cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(dva),
94744cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(dva),
94844cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(dva));
94944cd46caSbillm 	}
95044cd46caSbillm 
95144cd46caSbillm 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
95244cd46caSbillm 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
953fa9e4066Sahrens 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
954fa9e4066Sahrens 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
955fa9e4066Sahrens 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
95644cd46caSbillm 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
957fa9e4066Sahrens 	    (u_longlong_t)bp->blk_birth,
958fa9e4066Sahrens 	    (u_longlong_t)bp->blk_fill,
959fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
960fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
961fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
962fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
963fa9e4066Sahrens }
964fa9e4066Sahrens 
965fa9e4066Sahrens void
966fa9e4066Sahrens spa_freeze(spa_t *spa)
967fa9e4066Sahrens {
968fa9e4066Sahrens 	uint64_t freeze_txg = 0;
969fa9e4066Sahrens 
970ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
971fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
972fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
973fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
974fa9e4066Sahrens 	}
975ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
976fa9e4066Sahrens 	if (freeze_txg != 0)
977fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
978fa9e4066Sahrens }
979fa9e4066Sahrens 
9800125049cSahrens void
9810125049cSahrens zfs_panic_recover(const char *fmt, ...)
9820125049cSahrens {
9830125049cSahrens 	va_list adx;
9840125049cSahrens 
9850125049cSahrens 	va_start(adx, fmt);
9860125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
9870125049cSahrens 	va_end(adx);
9880125049cSahrens }
9890125049cSahrens 
990fa9e4066Sahrens /*
991fa9e4066Sahrens  * ==========================================================================
992fa9e4066Sahrens  * Accessor functions
993fa9e4066Sahrens  * ==========================================================================
994fa9e4066Sahrens  */
995fa9e4066Sahrens 
996fa9e4066Sahrens krwlock_t *
997fa9e4066Sahrens spa_traverse_rwlock(spa_t *spa)
998fa9e4066Sahrens {
999fa9e4066Sahrens 	return (&spa->spa_traverse_lock);
1000fa9e4066Sahrens }
1001fa9e4066Sahrens 
1002*088f3894Sahrens boolean_t
1003fa9e4066Sahrens spa_traverse_wanted(spa_t *spa)
1004fa9e4066Sahrens {
1005fa9e4066Sahrens 	return (spa->spa_traverse_wanted);
1006fa9e4066Sahrens }
1007fa9e4066Sahrens 
1008fa9e4066Sahrens dsl_pool_t *
1009fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1010fa9e4066Sahrens {
1011fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1012fa9e4066Sahrens }
1013fa9e4066Sahrens 
1014fa9e4066Sahrens blkptr_t *
1015fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1016fa9e4066Sahrens {
1017fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1018fa9e4066Sahrens }
1019fa9e4066Sahrens 
1020fa9e4066Sahrens void
1021fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1022fa9e4066Sahrens {
1023fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1024fa9e4066Sahrens }
1025fa9e4066Sahrens 
1026fa9e4066Sahrens void
1027fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1028fa9e4066Sahrens {
1029fa9e4066Sahrens 	if (spa->spa_root == NULL)
1030fa9e4066Sahrens 		buf[0] = '\0';
1031fa9e4066Sahrens 	else
1032fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1033fa9e4066Sahrens }
1034fa9e4066Sahrens 
1035fa9e4066Sahrens int
1036fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1037fa9e4066Sahrens {
1038fa9e4066Sahrens 	return (spa->spa_sync_pass);
1039fa9e4066Sahrens }
1040fa9e4066Sahrens 
1041fa9e4066Sahrens char *
1042fa9e4066Sahrens spa_name(spa_t *spa)
1043fa9e4066Sahrens {
1044fa9e4066Sahrens 	/*
1045fa9e4066Sahrens 	 * Accessing the name requires holding either the namespace lock or the
1046fa9e4066Sahrens 	 * config lock, both of which are required to do a rename.
1047fa9e4066Sahrens 	 */
1048fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1049e05725b1Sbonwick 	    spa_config_held(spa, RW_READER));
1050fa9e4066Sahrens 
1051fa9e4066Sahrens 	return (spa->spa_name);
1052fa9e4066Sahrens }
1053fa9e4066Sahrens 
1054fa9e4066Sahrens uint64_t
1055fa9e4066Sahrens spa_guid(spa_t *spa)
1056fa9e4066Sahrens {
1057b5989ec7Seschrock 	/*
1058b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1059b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1060b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1061b5989ec7Seschrock 	 * this case.
1062b5989ec7Seschrock 	 */
1063b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
1064b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1065b5989ec7Seschrock 	else
1066b5989ec7Seschrock 		return (spa->spa_load_guid);
1067fa9e4066Sahrens }
1068fa9e4066Sahrens 
1069fa9e4066Sahrens uint64_t
1070fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1071fa9e4066Sahrens {
1072fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1073fa9e4066Sahrens }
1074fa9e4066Sahrens 
1075fa9e4066Sahrens uint64_t
1076fa9e4066Sahrens spa_first_txg(spa_t *spa)
1077fa9e4066Sahrens {
1078fa9e4066Sahrens 	return (spa->spa_first_txg);
1079fa9e4066Sahrens }
1080fa9e4066Sahrens 
1081fa9e4066Sahrens int
1082fa9e4066Sahrens spa_state(spa_t *spa)
1083fa9e4066Sahrens {
1084fa9e4066Sahrens 	return (spa->spa_state);
1085fa9e4066Sahrens }
1086fa9e4066Sahrens 
1087fa9e4066Sahrens uint64_t
1088fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1089fa9e4066Sahrens {
1090fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1091fa9e4066Sahrens }
1092fa9e4066Sahrens 
1093fa9e4066Sahrens /*
109499653d4eSeschrock  * Return how much space is allocated in the pool (ie. sum of all asize)
1095fa9e4066Sahrens  */
1096fa9e4066Sahrens uint64_t
1097fa9e4066Sahrens spa_get_alloc(spa_t *spa)
1098fa9e4066Sahrens {
1099fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
1100fa9e4066Sahrens }
1101fa9e4066Sahrens 
1102fa9e4066Sahrens /*
110399653d4eSeschrock  * Return how much (raid-z inflated) space there is in the pool.
1104fa9e4066Sahrens  */
1105fa9e4066Sahrens uint64_t
1106fa9e4066Sahrens spa_get_space(spa_t *spa)
1107fa9e4066Sahrens {
1108fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_space);
1109fa9e4066Sahrens }
1110fa9e4066Sahrens 
111199653d4eSeschrock /*
111299653d4eSeschrock  * Return the amount of raid-z-deflated space in the pool.
111399653d4eSeschrock  */
111499653d4eSeschrock uint64_t
111599653d4eSeschrock spa_get_dspace(spa_t *spa)
111699653d4eSeschrock {
111799653d4eSeschrock 	if (spa->spa_deflate)
111899653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
111999653d4eSeschrock 	else
112099653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_space);
112199653d4eSeschrock }
112299653d4eSeschrock 
1123fa9e4066Sahrens /* ARGSUSED */
1124fa9e4066Sahrens uint64_t
1125fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1126fa9e4066Sahrens {
1127fa9e4066Sahrens 	/*
1128fa9e4066Sahrens 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
1129fa9e4066Sahrens 	 * case the space requirement is exactly 2x; so just assume that.
113044cd46caSbillm 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
113144cd46caSbillm 	 * we have to multiply by a total of 6x.
113244cd46caSbillm 	 */
113344cd46caSbillm 	return (lsize * 6);
113444cd46caSbillm }
113544cd46caSbillm 
11360a4e9518Sgw /*
11370a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
11380a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
11390a4e9518Sgw  */
11400a4e9518Sgw uint8_t
11410a4e9518Sgw spa_get_failmode(spa_t *spa)
11420a4e9518Sgw {
11430a4e9518Sgw 	return (spa->spa_failmode);
11440a4e9518Sgw }
11450a4e9518Sgw 
114644cd46caSbillm uint64_t
114744cd46caSbillm spa_version(spa_t *spa)
114844cd46caSbillm {
114944cd46caSbillm 	return (spa->spa_ubsync.ub_version);
115044cd46caSbillm }
115144cd46caSbillm 
115244cd46caSbillm int
115344cd46caSbillm spa_max_replication(spa_t *spa)
115444cd46caSbillm {
115544cd46caSbillm 	/*
1156e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
115744cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
115844cd46caSbillm 	 * replication level accordingly.
1159fa9e4066Sahrens 	 */
1160e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
116144cd46caSbillm 		return (1);
116244cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1163fa9e4066Sahrens }
1164fa9e4066Sahrens 
116599653d4eSeschrock uint64_t
116699653d4eSeschrock bp_get_dasize(spa_t *spa, const blkptr_t *bp)
116799653d4eSeschrock {
116899653d4eSeschrock 	int sz = 0, i;
116999653d4eSeschrock 
117099653d4eSeschrock 	if (!spa->spa_deflate)
117199653d4eSeschrock 		return (BP_GET_ASIZE(bp));
117299653d4eSeschrock 
11731934e92fSmaybee 	spa_config_enter(spa, RW_READER, FTAG);
117499653d4eSeschrock 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
117599653d4eSeschrock 		vdev_t *vd =
117699653d4eSeschrock 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
11771934e92fSmaybee 		if (vd)
11781934e92fSmaybee 			sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >>
11791934e92fSmaybee 			    SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
118099653d4eSeschrock 	}
11811934e92fSmaybee 	spa_config_exit(spa, FTAG);
118299653d4eSeschrock 	return (sz);
118399653d4eSeschrock }
118499653d4eSeschrock 
1185fa9e4066Sahrens /*
1186fa9e4066Sahrens  * ==========================================================================
1187fa9e4066Sahrens  * Initialization and Termination
1188fa9e4066Sahrens  * ==========================================================================
1189fa9e4066Sahrens  */
1190fa9e4066Sahrens 
1191fa9e4066Sahrens static int
1192fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1193fa9e4066Sahrens {
1194fa9e4066Sahrens 	const spa_t *s1 = a1;
1195fa9e4066Sahrens 	const spa_t *s2 = a2;
1196fa9e4066Sahrens 	int s;
1197fa9e4066Sahrens 
1198fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1199fa9e4066Sahrens 	if (s > 0)
1200fa9e4066Sahrens 		return (1);
1201fa9e4066Sahrens 	if (s < 0)
1202fa9e4066Sahrens 		return (-1);
1203fa9e4066Sahrens 	return (0);
1204fa9e4066Sahrens }
1205fa9e4066Sahrens 
12060373e76bSbonwick int
12070373e76bSbonwick spa_busy(void)
12080373e76bSbonwick {
12090373e76bSbonwick 	return (spa_active_count);
12100373e76bSbonwick }
12110373e76bSbonwick 
1212e7cbe64fSgw void
1213e7cbe64fSgw spa_boot_init()
1214e7cbe64fSgw {
1215e7cbe64fSgw 	spa_config_load();
1216e7cbe64fSgw }
1217e7cbe64fSgw 
1218fa9e4066Sahrens void
1219fa9e4066Sahrens spa_init(int mode)
1220fa9e4066Sahrens {
1221fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1222c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1223fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1224fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1225fa9e4066Sahrens 
1226fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1227fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1228fa9e4066Sahrens 
1229fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1230fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1231fa94a07fSbrendan 
1232fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1233fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
123499653d4eSeschrock 
1235fa9e4066Sahrens 	spa_mode = mode;
1236fa9e4066Sahrens 
1237fa9e4066Sahrens 	refcount_init();
1238fa9e4066Sahrens 	unique_init();
1239fa9e4066Sahrens 	zio_init();
1240fa9e4066Sahrens 	dmu_init();
1241fa9e4066Sahrens 	zil_init();
124287db74c1Sek 	vdev_cache_stat_init();
124391ebeef5Sahrens 	zfs_prop_init();
1244990b4856Slling 	zpool_prop_init();
1245fa9e4066Sahrens 	spa_config_load();
1246fa9e4066Sahrens }
1247fa9e4066Sahrens 
1248fa9e4066Sahrens void
1249fa9e4066Sahrens spa_fini(void)
1250fa9e4066Sahrens {
1251fa9e4066Sahrens 	spa_evict_all();
1252fa9e4066Sahrens 
125387db74c1Sek 	vdev_cache_stat_fini();
1254fa9e4066Sahrens 	zil_fini();
1255fa9e4066Sahrens 	dmu_fini();
1256fa9e4066Sahrens 	zio_fini();
125791ebeef5Sahrens 	unique_fini();
1258fa9e4066Sahrens 	refcount_fini();
1259fa9e4066Sahrens 
1260fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
126199653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1262fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1263fa9e4066Sahrens 
1264fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1265fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1266c25056deSgw 	mutex_destroy(&spa_spare_lock);
1267fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1268fa9e4066Sahrens }
12696ce0521aSperrin 
12706ce0521aSperrin /*
12716ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
12726ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
12736ce0521aSperrin  * performance and not correctness
12746ce0521aSperrin  */
12756ce0521aSperrin boolean_t
12766ce0521aSperrin spa_has_slogs(spa_t *spa)
12776ce0521aSperrin {
12786ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
12796ce0521aSperrin }
1280bf82a41bSeschrock 
1281bf82a41bSeschrock /*
1282bf82a41bSeschrock  * Return whether this pool is the root pool.
1283bf82a41bSeschrock  */
1284bf82a41bSeschrock boolean_t
1285bf82a41bSeschrock spa_is_root(spa_t *spa)
1286bf82a41bSeschrock {
1287bf82a41bSeschrock 	return (spa->spa_is_root);
1288bf82a41bSeschrock }
1289