xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision fa94a07fd0519b8abfd871ad8fe60e6bebe1e2bb)
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 /*
2239c23413Seschrock  * Copyright 2007 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  *
147fa9e4066Sahrens  * The spa_config_lock is manipulated using the following functions:
148fa9e4066Sahrens  *
14944cd46caSbillm  *	spa_config_enter()	Acquire the config lock as RW_READER or
15044cd46caSbillm  *				RW_WRITER.  At least one reference on the spa_t
15144cd46caSbillm  *				must exist.
152fa9e4066Sahrens  *
15344cd46caSbillm  *	spa_config_exit()	Release the config lock.
154fa9e4066Sahrens  *
15544cd46caSbillm  *	spa_config_held()	Returns true if the config lock is currently
15644cd46caSbillm  *				held in the given state.
157fa9e4066Sahrens  *
158ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
159fa9e4066Sahrens  *
16044cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
161ea8dc4b6Seschrock  *				for writing.
162fa9e4066Sahrens  *
16344cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
16444cd46caSbillm  *				to complete, sync the updated configs to the
165ea8dc4b6Seschrock  *				cache, and release the namespace lock.
166fa9e4066Sahrens  *
167fa9e4066Sahrens  * The spa_name() function also requires either the spa_namespace_lock
168fa9e4066Sahrens  * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
169fa9e4066Sahrens  * also implemented within this file since is requires manipulation of the
170fa9e4066Sahrens  * namespace.
171fa9e4066Sahrens  */
172fa9e4066Sahrens 
173fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
174fa9e4066Sahrens kmutex_t spa_namespace_lock;
175fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
1760373e76bSbonwick static int spa_active_count;
177416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
178fa9e4066Sahrens 
17999653d4eSeschrock static kmutex_t spa_spare_lock;
18039c23413Seschrock static avl_tree_t spa_spare_avl;
181*fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
182*fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
18399653d4eSeschrock 
184fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
185fa9e4066Sahrens int spa_mode;
186fa9e4066Sahrens 
187fa9e4066Sahrens #ifdef ZFS_DEBUG
18840feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
18940feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
190fa9e4066Sahrens #else
191fa9e4066Sahrens int zfs_flags = 0;
192fa9e4066Sahrens #endif
193fa9e4066Sahrens 
1940125049cSahrens /*
1950125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
1960125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
1970125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
1980125049cSahrens  */
1990125049cSahrens int zfs_recover = 0;
2000125049cSahrens 
201fa9e4066Sahrens #define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
202fa9e4066Sahrens 
203fa9e4066Sahrens /*
204fa9e4066Sahrens  * ==========================================================================
205fa9e4066Sahrens  * SPA namespace functions
206fa9e4066Sahrens  * ==========================================================================
207fa9e4066Sahrens  */
208fa9e4066Sahrens 
209fa9e4066Sahrens /*
210fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
211fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
212fa9e4066Sahrens  */
213fa9e4066Sahrens spa_t *
214fa9e4066Sahrens spa_lookup(const char *name)
215fa9e4066Sahrens {
216fa9e4066Sahrens 	spa_t search, *spa;
217fa9e4066Sahrens 	avl_index_t where;
21840feaa91Sahrens 	char c;
21940feaa91Sahrens 	char *cp;
220fa9e4066Sahrens 
221fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
222fa9e4066Sahrens 
22340feaa91Sahrens 	/*
22440feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
22540feaa91Sahrens 	 * just use that.
22640feaa91Sahrens 	 */
22740feaa91Sahrens 	cp = strpbrk(name, "/@");
22840feaa91Sahrens 	if (cp) {
22940feaa91Sahrens 		c = *cp;
23040feaa91Sahrens 		*cp = '\0';
23140feaa91Sahrens 	}
23240feaa91Sahrens 
233fa9e4066Sahrens 	search.spa_name = (char *)name;
234fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
235fa9e4066Sahrens 
23640feaa91Sahrens 	if (cp)
23740feaa91Sahrens 		*cp = c;
23840feaa91Sahrens 
239fa9e4066Sahrens 	return (spa);
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242fa9e4066Sahrens /*
243fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
244fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
245fa9e4066Sahrens  * exist by calling spa_lookup() first.
246fa9e4066Sahrens  */
247fa9e4066Sahrens spa_t *
2480373e76bSbonwick spa_add(const char *name, const char *altroot)
249fa9e4066Sahrens {
250fa9e4066Sahrens 	spa_t *spa;
251fa9e4066Sahrens 
252fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
253fa9e4066Sahrens 
254fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
255fa9e4066Sahrens 
256c25056deSgw 	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
257c25056deSgw 
258c25056deSgw 	mutex_init(&spa->spa_uberblock_lock, NULL, MUTEX_DEFAULT, NULL);
259c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
260c25056deSgw 	mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL);
261c25056deSgw 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
262c25056deSgw 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
263c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
264c25056deSgw 	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
265c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
266c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
267c25056deSgw 
268c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
269c25056deSgw 	cv_init(&spa->spa_scrub_cv, NULL, CV_DEFAULT, NULL);
270c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
271c25056deSgw 
272fa9e4066Sahrens 	spa->spa_name = spa_strdup(name);
273fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
274fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
2750373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
276fa9e4066Sahrens 
277fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
27891ebeef5Sahrens 	rprw_init(&spa->spa_config_lock);
279fa9e4066Sahrens 
280fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
281fa9e4066Sahrens 
2820a4e9518Sgw 	mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL);
2830a4e9518Sgw 
2840373e76bSbonwick 	/*
2850373e76bSbonwick 	 * Set the alternate root, if there is one.
2860373e76bSbonwick 	 */
2870373e76bSbonwick 	if (altroot) {
2880373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
2890373e76bSbonwick 		spa_active_count++;
2900373e76bSbonwick 	}
2910373e76bSbonwick 
292fa9e4066Sahrens 	return (spa);
293fa9e4066Sahrens }
294fa9e4066Sahrens 
295fa9e4066Sahrens /*
296fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
297fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
298fa9e4066Sahrens  * deactivated.
299fa9e4066Sahrens  */
300fa9e4066Sahrens void
301fa9e4066Sahrens spa_remove(spa_t *spa)
302fa9e4066Sahrens {
303fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
304fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
305fa9e4066Sahrens 	ASSERT(spa->spa_scrub_thread == NULL);
306fa9e4066Sahrens 
307fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
308fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
309fa9e4066Sahrens 
3100373e76bSbonwick 	if (spa->spa_root) {
311fa9e4066Sahrens 		spa_strfree(spa->spa_root);
3120373e76bSbonwick 		spa_active_count--;
3130373e76bSbonwick 	}
314fa9e4066Sahrens 
315fa9e4066Sahrens 	if (spa->spa_name)
316fa9e4066Sahrens 		spa_strfree(spa->spa_name);
317fa9e4066Sahrens 
3182f8aaab3Seschrock 	if (spa->spa_config_dir)
3192f8aaab3Seschrock 		spa_strfree(spa->spa_config_dir);
3202f8aaab3Seschrock 	if (spa->spa_config_file)
3212f8aaab3Seschrock 		spa_strfree(spa->spa_config_file);
3222f8aaab3Seschrock 
323fa9e4066Sahrens 	spa_config_set(spa, NULL);
324fa9e4066Sahrens 
325fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
32691ebeef5Sahrens 
32791ebeef5Sahrens 	rprw_destroy(&spa->spa_config_lock);
328fa9e4066Sahrens 
329c25056deSgw 	rw_destroy(&spa->spa_traverse_lock);
330c25056deSgw 
331c25056deSgw 	cv_destroy(&spa->spa_async_cv);
332c25056deSgw 	cv_destroy(&spa->spa_scrub_cv);
333c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
334c25056deSgw 
335c25056deSgw 	mutex_destroy(&spa->spa_uberblock_lock);
3365ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
337c25056deSgw 	mutex_destroy(&spa->spa_config_cache_lock);
338c25056deSgw 	mutex_destroy(&spa->spa_scrub_lock);
339c25056deSgw 	mutex_destroy(&spa->spa_errlog_lock);
340c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
341c25056deSgw 	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
34206eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
343b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
3440a4e9518Sgw 	mutex_destroy(&spa->spa_zio_lock);
3455ad82045Snd 
346fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
347fa9e4066Sahrens }
348fa9e4066Sahrens 
349fa9e4066Sahrens /*
350fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
351fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
352fa9e4066Sahrens  */
353fa9e4066Sahrens spa_t *
354fa9e4066Sahrens spa_next(spa_t *prev)
355fa9e4066Sahrens {
356fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
357fa9e4066Sahrens 
358fa9e4066Sahrens 	if (prev)
359fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
360fa9e4066Sahrens 	else
361fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
362fa9e4066Sahrens }
363fa9e4066Sahrens 
364fa9e4066Sahrens /*
365fa9e4066Sahrens  * ==========================================================================
366fa9e4066Sahrens  * SPA refcount functions
367fa9e4066Sahrens  * ==========================================================================
368fa9e4066Sahrens  */
369fa9e4066Sahrens 
370fa9e4066Sahrens /*
371fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
372fa9e4066Sahrens  * have the namespace lock held.
373fa9e4066Sahrens  */
374fa9e4066Sahrens void
375fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
376fa9e4066Sahrens {
377fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
378fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
379fa9e4066Sahrens 
380fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
381fa9e4066Sahrens }
382fa9e4066Sahrens 
383fa9e4066Sahrens /*
384fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
385fa9e4066Sahrens  * have the namespace lock held.
386fa9e4066Sahrens  */
387fa9e4066Sahrens void
388fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
389fa9e4066Sahrens {
390fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
391fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
392fa9e4066Sahrens 
393fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
394fa9e4066Sahrens }
395fa9e4066Sahrens 
396fa9e4066Sahrens /*
397fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
398fa9e4066Sahrens  * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
399fa9e4066Sahrens  * number of references acquired when opening a pool
400fa9e4066Sahrens  */
401fa9e4066Sahrens boolean_t
402fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
403fa9e4066Sahrens {
404fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
405fa9e4066Sahrens 
406fa9e4066Sahrens 	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
407fa9e4066Sahrens }
408fa9e4066Sahrens 
40999653d4eSeschrock /*
41099653d4eSeschrock  * ==========================================================================
411*fa94a07fSbrendan  * SPA spare and l2cache tracking
41299653d4eSeschrock  * ==========================================================================
41399653d4eSeschrock  */
41499653d4eSeschrock 
415*fa94a07fSbrendan /*
416*fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
417*fa94a07fSbrendan  * for 'auxiliary' devices.
418*fa94a07fSbrendan  */
419*fa94a07fSbrendan 
420*fa94a07fSbrendan typedef struct spa_aux {
421*fa94a07fSbrendan 	uint64_t	aux_guid;
422*fa94a07fSbrendan 	uint64_t	aux_pool;
423*fa94a07fSbrendan 	avl_node_t	aux_avl;
424*fa94a07fSbrendan 	int		aux_count;
425*fa94a07fSbrendan } spa_aux_t;
426*fa94a07fSbrendan 
427*fa94a07fSbrendan static int
428*fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
429*fa94a07fSbrendan {
430*fa94a07fSbrendan 	const spa_aux_t *sa = a;
431*fa94a07fSbrendan 	const spa_aux_t *sb = b;
432*fa94a07fSbrendan 
433*fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
434*fa94a07fSbrendan 		return (-1);
435*fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
436*fa94a07fSbrendan 		return (1);
437*fa94a07fSbrendan 	else
438*fa94a07fSbrendan 		return (0);
439*fa94a07fSbrendan }
440*fa94a07fSbrendan 
441*fa94a07fSbrendan void
442*fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
443*fa94a07fSbrendan {
444*fa94a07fSbrendan 	avl_index_t where;
445*fa94a07fSbrendan 	spa_aux_t search;
446*fa94a07fSbrendan 	spa_aux_t *aux;
447*fa94a07fSbrendan 
448*fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
449*fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
450*fa94a07fSbrendan 		aux->aux_count++;
451*fa94a07fSbrendan 	} else {
452*fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
453*fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
454*fa94a07fSbrendan 		aux->aux_count = 1;
455*fa94a07fSbrendan 		avl_insert(avl, aux, where);
456*fa94a07fSbrendan 	}
457*fa94a07fSbrendan }
458*fa94a07fSbrendan 
459*fa94a07fSbrendan void
460*fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
461*fa94a07fSbrendan {
462*fa94a07fSbrendan 	spa_aux_t search;
463*fa94a07fSbrendan 	spa_aux_t *aux;
464*fa94a07fSbrendan 	avl_index_t where;
465*fa94a07fSbrendan 
466*fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
467*fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
468*fa94a07fSbrendan 
469*fa94a07fSbrendan 	ASSERT(aux != NULL);
470*fa94a07fSbrendan 
471*fa94a07fSbrendan 	if (--aux->aux_count == 0) {
472*fa94a07fSbrendan 		avl_remove(avl, aux);
473*fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
474*fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
475*fa94a07fSbrendan 		aux->aux_pool = 0ULL;
476*fa94a07fSbrendan 	}
477*fa94a07fSbrendan }
478*fa94a07fSbrendan 
479*fa94a07fSbrendan boolean_t
480*fa94a07fSbrendan spa_aux_exists(uint64_t guid, uint64_t *pool, avl_tree_t *avl)
481*fa94a07fSbrendan {
482*fa94a07fSbrendan 	spa_aux_t search, *found;
483*fa94a07fSbrendan 	avl_index_t where;
484*fa94a07fSbrendan 
485*fa94a07fSbrendan 	search.aux_guid = guid;
486*fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
487*fa94a07fSbrendan 
488*fa94a07fSbrendan 	if (pool) {
489*fa94a07fSbrendan 		if (found)
490*fa94a07fSbrendan 			*pool = found->aux_pool;
491*fa94a07fSbrendan 		else
492*fa94a07fSbrendan 			*pool = 0ULL;
493*fa94a07fSbrendan 	}
494*fa94a07fSbrendan 
495*fa94a07fSbrendan 	return (found != NULL);
496*fa94a07fSbrendan }
497*fa94a07fSbrendan 
498*fa94a07fSbrendan void
499*fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
500*fa94a07fSbrendan {
501*fa94a07fSbrendan 	spa_aux_t search, *found;
502*fa94a07fSbrendan 	avl_index_t where;
503*fa94a07fSbrendan 
504*fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
505*fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
506*fa94a07fSbrendan 	ASSERT(found != NULL);
507*fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
508*fa94a07fSbrendan 
509*fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
510*fa94a07fSbrendan }
511*fa94a07fSbrendan 
51299653d4eSeschrock /*
51339c23413Seschrock  * Spares are tracked globally due to the following constraints:
51439c23413Seschrock  *
51539c23413Seschrock  * 	- A spare may be part of multiple pools.
51639c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
51739c23413Seschrock  *	  another pool.
51839c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
51939c23413Seschrock  *	  the target is a spare in the same pool.
52039c23413Seschrock  *
52139c23413Seschrock  * We keep track of all spares on the system through the use of a reference
52239c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
52339c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
52439c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
52539c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
52639c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
52739c23413Seschrock  *
52839c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
52939c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
53039c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
53139c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
53299653d4eSeschrock  */
53339c23413Seschrock 
53499653d4eSeschrock static int
53599653d4eSeschrock spa_spare_compare(const void *a, const void *b)
53699653d4eSeschrock {
537*fa94a07fSbrendan 	return (spa_aux_compare(a, b));
53899653d4eSeschrock }
53999653d4eSeschrock 
54099653d4eSeschrock void
54139c23413Seschrock spa_spare_add(vdev_t *vd)
54299653d4eSeschrock {
54399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
54439c23413Seschrock 	ASSERT(!vd->vdev_isspare);
545*fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
54639c23413Seschrock 	vd->vdev_isspare = B_TRUE;
54799653d4eSeschrock 	mutex_exit(&spa_spare_lock);
54899653d4eSeschrock }
54999653d4eSeschrock 
55099653d4eSeschrock void
55139c23413Seschrock spa_spare_remove(vdev_t *vd)
55299653d4eSeschrock {
55399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
55439c23413Seschrock 	ASSERT(vd->vdev_isspare);
555*fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
55639c23413Seschrock 	vd->vdev_isspare = B_FALSE;
55799653d4eSeschrock 	mutex_exit(&spa_spare_lock);
55899653d4eSeschrock }
55999653d4eSeschrock 
56099653d4eSeschrock boolean_t
56139c23413Seschrock spa_spare_exists(uint64_t guid, uint64_t *pool)
56299653d4eSeschrock {
563*fa94a07fSbrendan 	boolean_t found;
56499653d4eSeschrock 
56599653d4eSeschrock 	mutex_enter(&spa_spare_lock);
566*fa94a07fSbrendan 	found = spa_aux_exists(guid, pool, &spa_spare_avl);
56799653d4eSeschrock 	mutex_exit(&spa_spare_lock);
56899653d4eSeschrock 
569*fa94a07fSbrendan 	return (found);
57039c23413Seschrock }
57139c23413Seschrock 
57239c23413Seschrock void
57339c23413Seschrock spa_spare_activate(vdev_t *vd)
57439c23413Seschrock {
57539c23413Seschrock 	mutex_enter(&spa_spare_lock);
57639c23413Seschrock 	ASSERT(vd->vdev_isspare);
577*fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
578*fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
579*fa94a07fSbrendan }
58039c23413Seschrock 
581*fa94a07fSbrendan /*
582*fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
583*fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
584*fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
585*fa94a07fSbrendan  */
58639c23413Seschrock 
587*fa94a07fSbrendan static int
588*fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
589*fa94a07fSbrendan {
590*fa94a07fSbrendan 	return (spa_aux_compare(a, b));
591*fa94a07fSbrendan }
592*fa94a07fSbrendan 
593*fa94a07fSbrendan void
594*fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
595*fa94a07fSbrendan {
596*fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
597*fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
598*fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
599*fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
600*fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
601*fa94a07fSbrendan }
602*fa94a07fSbrendan 
603*fa94a07fSbrendan void
604*fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
605*fa94a07fSbrendan {
606*fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
607*fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
608*fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
609*fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
610*fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
611*fa94a07fSbrendan }
612*fa94a07fSbrendan 
613*fa94a07fSbrendan boolean_t
614*fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
615*fa94a07fSbrendan {
616*fa94a07fSbrendan 	boolean_t found;
617*fa94a07fSbrendan 
618*fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
619*fa94a07fSbrendan 	found = spa_aux_exists(guid, pool, &spa_l2cache_avl);
620*fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
621*fa94a07fSbrendan 
622*fa94a07fSbrendan 	return (found);
623*fa94a07fSbrendan }
624*fa94a07fSbrendan 
625*fa94a07fSbrendan void
626*fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
627*fa94a07fSbrendan {
628*fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
629*fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
630*fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
631*fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
632*fa94a07fSbrendan }
633*fa94a07fSbrendan 
634*fa94a07fSbrendan void
635*fa94a07fSbrendan spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
636*fa94a07fSbrendan {
637*fa94a07fSbrendan 	vdev_space_update(vd, space, alloc, B_FALSE);
63899653d4eSeschrock }
63999653d4eSeschrock 
640fa9e4066Sahrens /*
641fa9e4066Sahrens  * ==========================================================================
642fa9e4066Sahrens  * SPA config locking
643fa9e4066Sahrens  * ==========================================================================
644fa9e4066Sahrens  */
645fa9e4066Sahrens void
646ea8dc4b6Seschrock spa_config_enter(spa_t *spa, krw_t rw, void *tag)
647fa9e4066Sahrens {
64891ebeef5Sahrens 	rprw_enter(&spa->spa_config_lock, rw, tag);
649fa9e4066Sahrens }
650fa9e4066Sahrens 
651fa9e4066Sahrens void
652ea8dc4b6Seschrock spa_config_exit(spa_t *spa, void *tag)
653fa9e4066Sahrens {
65491ebeef5Sahrens 	rprw_exit(&spa->spa_config_lock, tag);
655fa9e4066Sahrens }
656fa9e4066Sahrens 
657fa9e4066Sahrens boolean_t
658fa9e4066Sahrens spa_config_held(spa_t *spa, krw_t rw)
659fa9e4066Sahrens {
66091ebeef5Sahrens 	return (rprw_held(&spa->spa_config_lock, rw));
661fa9e4066Sahrens }
662fa9e4066Sahrens 
663fa9e4066Sahrens /*
664fa9e4066Sahrens  * ==========================================================================
665fa9e4066Sahrens  * SPA vdev locking
666fa9e4066Sahrens  * ==========================================================================
667fa9e4066Sahrens  */
668fa9e4066Sahrens 
669fa9e4066Sahrens /*
670ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
671ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
672fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
673fa9e4066Sahrens  */
674fa9e4066Sahrens uint64_t
675fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
676fa9e4066Sahrens {
6773d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
6783d7072f8Seschrock 
679ea8dc4b6Seschrock 	/*
6803d7072f8Seschrock 	 * Suspend scrub activity while we mess with the config.  We must do
6813d7072f8Seschrock 	 * this after acquiring the namespace lock to avoid a 3-way deadlock
6823d7072f8Seschrock 	 * with spa_scrub_stop() and the scrub thread.
683ea8dc4b6Seschrock 	 */
684ea8dc4b6Seschrock 	spa_scrub_suspend(spa);
685fa9e4066Sahrens 
686ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, spa);
687fa9e4066Sahrens 
688fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
689fa9e4066Sahrens }
690fa9e4066Sahrens 
691fa9e4066Sahrens /*
692fa9e4066Sahrens  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
693fa9e4066Sahrens  * locking of spa_vdev_enter(), we also want make sure the transactions have
694fa9e4066Sahrens  * synced to disk, and then update the global configuration cache with the new
695fa9e4066Sahrens  * information.
696fa9e4066Sahrens  */
697fa9e4066Sahrens int
698fa9e4066Sahrens spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
699fa9e4066Sahrens {
7000e34b6a7Sbonwick 	int config_changed = B_FALSE;
701ea8dc4b6Seschrock 
7020373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
7030e34b6a7Sbonwick 
7040e34b6a7Sbonwick 	/*
7050e34b6a7Sbonwick 	 * Reassess the DTLs.
7060e34b6a7Sbonwick 	 */
7070373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
7080e34b6a7Sbonwick 
7090e34b6a7Sbonwick 	/*
7100373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
7110e34b6a7Sbonwick 	 */
7120e34b6a7Sbonwick 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
7130e34b6a7Sbonwick 		config_changed = B_TRUE;
7140373e76bSbonwick 		spa_scrub_restart(spa, txg);
7150e34b6a7Sbonwick 	}
716ea8dc4b6Seschrock 
717ea8dc4b6Seschrock 	spa_config_exit(spa, spa);
718fa9e4066Sahrens 
719ea8dc4b6Seschrock 	/*
7205dabedeeSbonwick 	 * Allow scrubbing to resume.
721ea8dc4b6Seschrock 	 */
722ea8dc4b6Seschrock 	spa_scrub_resume(spa);
723fa9e4066Sahrens 
724fa9e4066Sahrens 	/*
725fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
726fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
727fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
728fa9e4066Sahrens 	 */
729fa9e4066Sahrens 	if (error == 0)
730fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
731fa9e4066Sahrens 
732fa9e4066Sahrens 	if (vd != NULL) {
733fa9e4066Sahrens 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
734fa9e4066Sahrens 		vdev_free(vd);
735fa9e4066Sahrens 	}
736fa9e4066Sahrens 
737fa9e4066Sahrens 	/*
7380e34b6a7Sbonwick 	 * If the config changed, update the config cache.
739fa9e4066Sahrens 	 */
7400e34b6a7Sbonwick 	if (config_changed)
741fa9e4066Sahrens 		spa_config_sync();
742ea8dc4b6Seschrock 
743ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
744fa9e4066Sahrens 
745fa9e4066Sahrens 	return (error);
746fa9e4066Sahrens }
747fa9e4066Sahrens 
748fa9e4066Sahrens /*
749fa9e4066Sahrens  * ==========================================================================
750fa9e4066Sahrens  * Miscellaneous functions
751fa9e4066Sahrens  * ==========================================================================
752fa9e4066Sahrens  */
753fa9e4066Sahrens 
754fa9e4066Sahrens /*
755fa9e4066Sahrens  * Rename a spa_t.
756fa9e4066Sahrens  */
757fa9e4066Sahrens int
758fa9e4066Sahrens spa_rename(const char *name, const char *newname)
759fa9e4066Sahrens {
760fa9e4066Sahrens 	spa_t *spa;
761fa9e4066Sahrens 	int err;
762fa9e4066Sahrens 
763fa9e4066Sahrens 	/*
764fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
765fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
766fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
767ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
768fa9e4066Sahrens 	 */
769fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
770fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
771fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
772fa9e4066Sahrens 		return (err);
773fa9e4066Sahrens 	}
774fa9e4066Sahrens 
775ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
776fa9e4066Sahrens 
777fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
778fa9e4066Sahrens 	spa_strfree(spa->spa_name);
779fa9e4066Sahrens 	spa->spa_name = spa_strdup(newname);
780fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
781fa9e4066Sahrens 
782fa9e4066Sahrens 	/*
783fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
784fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
785fa9e4066Sahrens 	 * during the sync.
786fa9e4066Sahrens 	 */
787fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
788fa9e4066Sahrens 
789ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
790fa9e4066Sahrens 
7910373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
792fa9e4066Sahrens 
793fa9e4066Sahrens 	/*
794fa9e4066Sahrens 	 * Sync the updated config cache.
795fa9e4066Sahrens 	 */
796fa9e4066Sahrens 	spa_config_sync();
797fa9e4066Sahrens 
798fa9e4066Sahrens 	spa_close(spa, FTAG);
799fa9e4066Sahrens 
800fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
801fa9e4066Sahrens 
802fa9e4066Sahrens 	return (0);
803fa9e4066Sahrens }
804fa9e4066Sahrens 
805fa9e4066Sahrens 
806fa9e4066Sahrens /*
807fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
808fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
809fa9e4066Sahrens  * specified device_guid.
810fa9e4066Sahrens  */
811fa9e4066Sahrens boolean_t
812fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
813fa9e4066Sahrens {
814fa9e4066Sahrens 	spa_t *spa;
815fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
816fa9e4066Sahrens 
817ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
818fa9e4066Sahrens 
819fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
820fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
821fa9e4066Sahrens 			continue;
822fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
823fa9e4066Sahrens 			continue;
82439c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
82539c23413Seschrock 			if (device_guid == 0)
82639c23413Seschrock 				break;
82739c23413Seschrock 
82839c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
82939c23413Seschrock 			    device_guid) != NULL)
83039c23413Seschrock 				break;
83139c23413Seschrock 
83239c23413Seschrock 			/*
8338654d025Sperrin 			 * Check any devices we may be in the process of adding.
83439c23413Seschrock 			 */
83539c23413Seschrock 			if (spa->spa_pending_vdev) {
83639c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
83739c23413Seschrock 				    device_guid) != NULL)
83839c23413Seschrock 					break;
83939c23413Seschrock 			}
84039c23413Seschrock 		}
841fa9e4066Sahrens 	}
842fa9e4066Sahrens 
843fa9e4066Sahrens 	return (spa != NULL);
844fa9e4066Sahrens }
845fa9e4066Sahrens 
846fa9e4066Sahrens char *
847fa9e4066Sahrens spa_strdup(const char *s)
848fa9e4066Sahrens {
849fa9e4066Sahrens 	size_t len;
850fa9e4066Sahrens 	char *new;
851fa9e4066Sahrens 
852fa9e4066Sahrens 	len = strlen(s);
853fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
854fa9e4066Sahrens 	bcopy(s, new, len);
855fa9e4066Sahrens 	new[len] = '\0';
856fa9e4066Sahrens 
857fa9e4066Sahrens 	return (new);
858fa9e4066Sahrens }
859fa9e4066Sahrens 
860fa9e4066Sahrens void
861fa9e4066Sahrens spa_strfree(char *s)
862fa9e4066Sahrens {
863fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
864fa9e4066Sahrens }
865fa9e4066Sahrens 
866fa9e4066Sahrens uint64_t
867fa9e4066Sahrens spa_get_random(uint64_t range)
868fa9e4066Sahrens {
869fa9e4066Sahrens 	uint64_t r;
870fa9e4066Sahrens 
871fa9e4066Sahrens 	ASSERT(range != 0);
872fa9e4066Sahrens 
873fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
874fa9e4066Sahrens 
875fa9e4066Sahrens 	return (r % range);
876fa9e4066Sahrens }
877fa9e4066Sahrens 
878fa9e4066Sahrens void
879d80c45e0Sbonwick sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
880fa9e4066Sahrens {
88144cd46caSbillm 	int d;
882fa9e4066Sahrens 
883fa9e4066Sahrens 	if (bp == NULL) {
884fbabab8fSmaybee 		(void) snprintf(buf, len, "<NULL>");
885fa9e4066Sahrens 		return;
886fa9e4066Sahrens 	}
887fa9e4066Sahrens 
888fa9e4066Sahrens 	if (BP_IS_HOLE(bp)) {
889fbabab8fSmaybee 		(void) snprintf(buf, len, "<hole>");
890fa9e4066Sahrens 		return;
891fa9e4066Sahrens 	}
892fa9e4066Sahrens 
89344cd46caSbillm 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
894fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LEVEL(bp),
895fa9e4066Sahrens 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
896fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LSIZE(bp),
89744cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp));
89844cd46caSbillm 
89944cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
900d80c45e0Sbonwick 		const dva_t *dva = &bp->blk_dva[d];
90144cd46caSbillm 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
90244cd46caSbillm 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
90344cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(dva),
90444cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(dva),
90544cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(dva));
90644cd46caSbillm 	}
90744cd46caSbillm 
90844cd46caSbillm 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
90944cd46caSbillm 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
910fa9e4066Sahrens 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
911fa9e4066Sahrens 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
912fa9e4066Sahrens 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
91344cd46caSbillm 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
914fa9e4066Sahrens 	    (u_longlong_t)bp->blk_birth,
915fa9e4066Sahrens 	    (u_longlong_t)bp->blk_fill,
916fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
917fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
918fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
919fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
920fa9e4066Sahrens }
921fa9e4066Sahrens 
922fa9e4066Sahrens void
923fa9e4066Sahrens spa_freeze(spa_t *spa)
924fa9e4066Sahrens {
925fa9e4066Sahrens 	uint64_t freeze_txg = 0;
926fa9e4066Sahrens 
927ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
928fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
929fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
930fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
931fa9e4066Sahrens 	}
932ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
933fa9e4066Sahrens 	if (freeze_txg != 0)
934fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
935fa9e4066Sahrens }
936fa9e4066Sahrens 
9370125049cSahrens void
9380125049cSahrens zfs_panic_recover(const char *fmt, ...)
9390125049cSahrens {
9400125049cSahrens 	va_list adx;
9410125049cSahrens 
9420125049cSahrens 	va_start(adx, fmt);
9430125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
9440125049cSahrens 	va_end(adx);
9450125049cSahrens }
9460125049cSahrens 
947fa9e4066Sahrens /*
948fa9e4066Sahrens  * ==========================================================================
949fa9e4066Sahrens  * Accessor functions
950fa9e4066Sahrens  * ==========================================================================
951fa9e4066Sahrens  */
952fa9e4066Sahrens 
953fa9e4066Sahrens krwlock_t *
954fa9e4066Sahrens spa_traverse_rwlock(spa_t *spa)
955fa9e4066Sahrens {
956fa9e4066Sahrens 	return (&spa->spa_traverse_lock);
957fa9e4066Sahrens }
958fa9e4066Sahrens 
959fa9e4066Sahrens int
960fa9e4066Sahrens spa_traverse_wanted(spa_t *spa)
961fa9e4066Sahrens {
962fa9e4066Sahrens 	return (spa->spa_traverse_wanted);
963fa9e4066Sahrens }
964fa9e4066Sahrens 
965fa9e4066Sahrens dsl_pool_t *
966fa9e4066Sahrens spa_get_dsl(spa_t *spa)
967fa9e4066Sahrens {
968fa9e4066Sahrens 	return (spa->spa_dsl_pool);
969fa9e4066Sahrens }
970fa9e4066Sahrens 
971fa9e4066Sahrens blkptr_t *
972fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
973fa9e4066Sahrens {
974fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
975fa9e4066Sahrens }
976fa9e4066Sahrens 
977fa9e4066Sahrens void
978fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
979fa9e4066Sahrens {
980fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
981fa9e4066Sahrens }
982fa9e4066Sahrens 
983fa9e4066Sahrens void
984fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
985fa9e4066Sahrens {
986fa9e4066Sahrens 	if (spa->spa_root == NULL)
987fa9e4066Sahrens 		buf[0] = '\0';
988fa9e4066Sahrens 	else
989fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
990fa9e4066Sahrens }
991fa9e4066Sahrens 
992fa9e4066Sahrens int
993fa9e4066Sahrens spa_sync_pass(spa_t *spa)
994fa9e4066Sahrens {
995fa9e4066Sahrens 	return (spa->spa_sync_pass);
996fa9e4066Sahrens }
997fa9e4066Sahrens 
998fa9e4066Sahrens char *
999fa9e4066Sahrens spa_name(spa_t *spa)
1000fa9e4066Sahrens {
1001fa9e4066Sahrens 	/*
1002fa9e4066Sahrens 	 * Accessing the name requires holding either the namespace lock or the
1003fa9e4066Sahrens 	 * config lock, both of which are required to do a rename.
1004fa9e4066Sahrens 	 */
1005fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
1006fa9e4066Sahrens 	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
1007fa9e4066Sahrens 
1008fa9e4066Sahrens 	return (spa->spa_name);
1009fa9e4066Sahrens }
1010fa9e4066Sahrens 
1011fa9e4066Sahrens uint64_t
1012fa9e4066Sahrens spa_guid(spa_t *spa)
1013fa9e4066Sahrens {
1014b5989ec7Seschrock 	/*
1015b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1016b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1017b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1018b5989ec7Seschrock 	 * this case.
1019b5989ec7Seschrock 	 */
1020b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
1021b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1022b5989ec7Seschrock 	else
1023b5989ec7Seschrock 		return (spa->spa_load_guid);
1024fa9e4066Sahrens }
1025fa9e4066Sahrens 
1026fa9e4066Sahrens uint64_t
1027fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1028fa9e4066Sahrens {
1029fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1030fa9e4066Sahrens }
1031fa9e4066Sahrens 
1032fa9e4066Sahrens uint64_t
1033fa9e4066Sahrens spa_first_txg(spa_t *spa)
1034fa9e4066Sahrens {
1035fa9e4066Sahrens 	return (spa->spa_first_txg);
1036fa9e4066Sahrens }
1037fa9e4066Sahrens 
1038fa9e4066Sahrens int
1039fa9e4066Sahrens spa_state(spa_t *spa)
1040fa9e4066Sahrens {
1041fa9e4066Sahrens 	return (spa->spa_state);
1042fa9e4066Sahrens }
1043fa9e4066Sahrens 
1044fa9e4066Sahrens uint64_t
1045fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1046fa9e4066Sahrens {
1047fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1048fa9e4066Sahrens }
1049fa9e4066Sahrens 
1050fa9e4066Sahrens /*
105199653d4eSeschrock  * Return how much space is allocated in the pool (ie. sum of all asize)
1052fa9e4066Sahrens  */
1053fa9e4066Sahrens uint64_t
1054fa9e4066Sahrens spa_get_alloc(spa_t *spa)
1055fa9e4066Sahrens {
1056fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
1057fa9e4066Sahrens }
1058fa9e4066Sahrens 
1059fa9e4066Sahrens /*
106099653d4eSeschrock  * Return how much (raid-z inflated) space there is in the pool.
1061fa9e4066Sahrens  */
1062fa9e4066Sahrens uint64_t
1063fa9e4066Sahrens spa_get_space(spa_t *spa)
1064fa9e4066Sahrens {
1065fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_space);
1066fa9e4066Sahrens }
1067fa9e4066Sahrens 
106899653d4eSeschrock /*
106999653d4eSeschrock  * Return the amount of raid-z-deflated space in the pool.
107099653d4eSeschrock  */
107199653d4eSeschrock uint64_t
107299653d4eSeschrock spa_get_dspace(spa_t *spa)
107399653d4eSeschrock {
107499653d4eSeschrock 	if (spa->spa_deflate)
107599653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
107699653d4eSeschrock 	else
107799653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_space);
107899653d4eSeschrock }
107999653d4eSeschrock 
1080fa9e4066Sahrens /* ARGSUSED */
1081fa9e4066Sahrens uint64_t
1082fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1083fa9e4066Sahrens {
1084fa9e4066Sahrens 	/*
1085fa9e4066Sahrens 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
1086fa9e4066Sahrens 	 * case the space requirement is exactly 2x; so just assume that.
108744cd46caSbillm 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
108844cd46caSbillm 	 * we have to multiply by a total of 6x.
108944cd46caSbillm 	 */
109044cd46caSbillm 	return (lsize * 6);
109144cd46caSbillm }
109244cd46caSbillm 
10930a4e9518Sgw /*
10940a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
10950a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
10960a4e9518Sgw  */
10970a4e9518Sgw uint8_t
10980a4e9518Sgw spa_get_failmode(spa_t *spa)
10990a4e9518Sgw {
11000a4e9518Sgw 	return (spa->spa_failmode);
11010a4e9518Sgw }
11020a4e9518Sgw 
110344cd46caSbillm uint64_t
110444cd46caSbillm spa_version(spa_t *spa)
110544cd46caSbillm {
110644cd46caSbillm 	return (spa->spa_ubsync.ub_version);
110744cd46caSbillm }
110844cd46caSbillm 
110944cd46caSbillm int
111044cd46caSbillm spa_max_replication(spa_t *spa)
111144cd46caSbillm {
111244cd46caSbillm 	/*
1113e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
111444cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
111544cd46caSbillm 	 * replication level accordingly.
1116fa9e4066Sahrens 	 */
1117e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
111844cd46caSbillm 		return (1);
111944cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1120fa9e4066Sahrens }
1121fa9e4066Sahrens 
112299653d4eSeschrock uint64_t
112399653d4eSeschrock bp_get_dasize(spa_t *spa, const blkptr_t *bp)
112499653d4eSeschrock {
112599653d4eSeschrock 	int sz = 0, i;
112699653d4eSeschrock 
112799653d4eSeschrock 	if (!spa->spa_deflate)
112899653d4eSeschrock 		return (BP_GET_ASIZE(bp));
112999653d4eSeschrock 
11301934e92fSmaybee 	spa_config_enter(spa, RW_READER, FTAG);
113199653d4eSeschrock 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
113299653d4eSeschrock 		vdev_t *vd =
113399653d4eSeschrock 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
11341934e92fSmaybee 		if (vd)
11351934e92fSmaybee 			sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >>
11361934e92fSmaybee 			    SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
113799653d4eSeschrock 	}
11381934e92fSmaybee 	spa_config_exit(spa, FTAG);
113999653d4eSeschrock 	return (sz);
114099653d4eSeschrock }
114199653d4eSeschrock 
1142fa9e4066Sahrens /*
1143fa9e4066Sahrens  * ==========================================================================
1144fa9e4066Sahrens  * Initialization and Termination
1145fa9e4066Sahrens  * ==========================================================================
1146fa9e4066Sahrens  */
1147fa9e4066Sahrens 
1148fa9e4066Sahrens static int
1149fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1150fa9e4066Sahrens {
1151fa9e4066Sahrens 	const spa_t *s1 = a1;
1152fa9e4066Sahrens 	const spa_t *s2 = a2;
1153fa9e4066Sahrens 	int s;
1154fa9e4066Sahrens 
1155fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1156fa9e4066Sahrens 	if (s > 0)
1157fa9e4066Sahrens 		return (1);
1158fa9e4066Sahrens 	if (s < 0)
1159fa9e4066Sahrens 		return (-1);
1160fa9e4066Sahrens 	return (0);
1161fa9e4066Sahrens }
1162fa9e4066Sahrens 
11630373e76bSbonwick int
11640373e76bSbonwick spa_busy(void)
11650373e76bSbonwick {
11660373e76bSbonwick 	return (spa_active_count);
11670373e76bSbonwick }
11680373e76bSbonwick 
1169fa9e4066Sahrens void
1170fa9e4066Sahrens spa_init(int mode)
1171fa9e4066Sahrens {
1172fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1173c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1174*fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1175fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1176fa9e4066Sahrens 
1177fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1178fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1179fa9e4066Sahrens 
1180*fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1181*fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1182*fa94a07fSbrendan 
1183*fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1184*fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
118599653d4eSeschrock 
1186fa9e4066Sahrens 	spa_mode = mode;
1187fa9e4066Sahrens 
1188fa9e4066Sahrens 	refcount_init();
1189fa9e4066Sahrens 	unique_init();
1190fa9e4066Sahrens 	zio_init();
1191fa9e4066Sahrens 	dmu_init();
1192fa9e4066Sahrens 	zil_init();
119391ebeef5Sahrens 	zfs_prop_init();
1194990b4856Slling 	zpool_prop_init();
1195fa9e4066Sahrens 	spa_config_load();
1196fa9e4066Sahrens }
1197fa9e4066Sahrens 
1198fa9e4066Sahrens void
1199fa9e4066Sahrens spa_fini(void)
1200fa9e4066Sahrens {
1201fa9e4066Sahrens 	spa_evict_all();
1202fa9e4066Sahrens 
1203fa9e4066Sahrens 	zil_fini();
1204fa9e4066Sahrens 	dmu_fini();
1205fa9e4066Sahrens 	zio_fini();
120691ebeef5Sahrens 	unique_fini();
1207fa9e4066Sahrens 	refcount_fini();
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
121099653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1211*fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1214fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1215c25056deSgw 	mutex_destroy(&spa_spare_lock);
1216*fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1217fa9e4066Sahrens }
12186ce0521aSperrin 
12196ce0521aSperrin /*
12206ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
12216ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
12226ce0521aSperrin  * performance and not correctness
12236ce0521aSperrin  */
12246ce0521aSperrin boolean_t
12256ce0521aSperrin spa_has_slogs(spa_t *spa)
12266ce0521aSperrin {
12276ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
12286ce0521aSperrin }
1229