xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 2f8aaab38e6371ad39ed90a1211ba8921acbb4d5)
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;
18199653d4eSeschrock 
182fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
183fa9e4066Sahrens int spa_mode;
184fa9e4066Sahrens 
185fa9e4066Sahrens #ifdef ZFS_DEBUG
18640feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
18740feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
188fa9e4066Sahrens #else
189fa9e4066Sahrens int zfs_flags = 0;
190fa9e4066Sahrens #endif
191fa9e4066Sahrens 
1920125049cSahrens /*
1930125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
1940125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
1950125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
1960125049cSahrens  */
1970125049cSahrens int zfs_recover = 0;
1980125049cSahrens 
199fa9e4066Sahrens #define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
200fa9e4066Sahrens 
201fa9e4066Sahrens /*
202fa9e4066Sahrens  * ==========================================================================
203fa9e4066Sahrens  * SPA namespace functions
204fa9e4066Sahrens  * ==========================================================================
205fa9e4066Sahrens  */
206fa9e4066Sahrens 
207fa9e4066Sahrens /*
208fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
209fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
210fa9e4066Sahrens  */
211fa9e4066Sahrens spa_t *
212fa9e4066Sahrens spa_lookup(const char *name)
213fa9e4066Sahrens {
214fa9e4066Sahrens 	spa_t search, *spa;
215fa9e4066Sahrens 	avl_index_t where;
21640feaa91Sahrens 	char c;
21740feaa91Sahrens 	char *cp;
218fa9e4066Sahrens 
219fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
220fa9e4066Sahrens 
22140feaa91Sahrens 	/*
22240feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
22340feaa91Sahrens 	 * just use that.
22440feaa91Sahrens 	 */
22540feaa91Sahrens 	cp = strpbrk(name, "/@");
22640feaa91Sahrens 	if (cp) {
22740feaa91Sahrens 		c = *cp;
22840feaa91Sahrens 		*cp = '\0';
22940feaa91Sahrens 	}
23040feaa91Sahrens 
231fa9e4066Sahrens 	search.spa_name = (char *)name;
232fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
233fa9e4066Sahrens 
23440feaa91Sahrens 	if (cp)
23540feaa91Sahrens 		*cp = c;
23640feaa91Sahrens 
237fa9e4066Sahrens 	return (spa);
238fa9e4066Sahrens }
239fa9e4066Sahrens 
240fa9e4066Sahrens /*
241fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
242fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
243fa9e4066Sahrens  * exist by calling spa_lookup() first.
244fa9e4066Sahrens  */
245fa9e4066Sahrens spa_t *
2460373e76bSbonwick spa_add(const char *name, const char *altroot)
247fa9e4066Sahrens {
248fa9e4066Sahrens 	spa_t *spa;
249fa9e4066Sahrens 
250fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
251fa9e4066Sahrens 
252fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
253fa9e4066Sahrens 
254c25056deSgw 	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
255c25056deSgw 
256c25056deSgw 	mutex_init(&spa->spa_uberblock_lock, NULL, MUTEX_DEFAULT, NULL);
257c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
258c25056deSgw 	mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL);
259c25056deSgw 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
260c25056deSgw 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
261c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
262c25056deSgw 	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
263c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
264c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
265c25056deSgw 
266c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
267c25056deSgw 	cv_init(&spa->spa_scrub_cv, NULL, CV_DEFAULT, NULL);
268c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
269c25056deSgw 
270fa9e4066Sahrens 	spa->spa_name = spa_strdup(name);
271fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
272fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
2730373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
274fa9e4066Sahrens 
275fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
27691ebeef5Sahrens 	rprw_init(&spa->spa_config_lock);
277fa9e4066Sahrens 
278fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
279fa9e4066Sahrens 
2800a4e9518Sgw 	mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL);
2810a4e9518Sgw 
2820373e76bSbonwick 	/*
2830373e76bSbonwick 	 * Set the alternate root, if there is one.
2840373e76bSbonwick 	 */
2850373e76bSbonwick 	if (altroot) {
2860373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
2870373e76bSbonwick 		spa_active_count++;
2880373e76bSbonwick 	}
2890373e76bSbonwick 
290fa9e4066Sahrens 	return (spa);
291fa9e4066Sahrens }
292fa9e4066Sahrens 
293fa9e4066Sahrens /*
294fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
295fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
296fa9e4066Sahrens  * deactivated.
297fa9e4066Sahrens  */
298fa9e4066Sahrens void
299fa9e4066Sahrens spa_remove(spa_t *spa)
300fa9e4066Sahrens {
301fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
302fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
303fa9e4066Sahrens 	ASSERT(spa->spa_scrub_thread == NULL);
304fa9e4066Sahrens 
305fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
306fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
307fa9e4066Sahrens 
3080373e76bSbonwick 	if (spa->spa_root) {
309fa9e4066Sahrens 		spa_strfree(spa->spa_root);
3100373e76bSbonwick 		spa_active_count--;
3110373e76bSbonwick 	}
312fa9e4066Sahrens 
313fa9e4066Sahrens 	if (spa->spa_name)
314fa9e4066Sahrens 		spa_strfree(spa->spa_name);
315fa9e4066Sahrens 
316*2f8aaab3Seschrock 	if (spa->spa_config_dir)
317*2f8aaab3Seschrock 		spa_strfree(spa->spa_config_dir);
318*2f8aaab3Seschrock 	if (spa->spa_config_file)
319*2f8aaab3Seschrock 		spa_strfree(spa->spa_config_file);
320*2f8aaab3Seschrock 
321fa9e4066Sahrens 	spa_config_set(spa, NULL);
322fa9e4066Sahrens 
323fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
32491ebeef5Sahrens 
32591ebeef5Sahrens 	rprw_destroy(&spa->spa_config_lock);
326fa9e4066Sahrens 
327c25056deSgw 	rw_destroy(&spa->spa_traverse_lock);
328c25056deSgw 
329c25056deSgw 	cv_destroy(&spa->spa_async_cv);
330c25056deSgw 	cv_destroy(&spa->spa_scrub_cv);
331c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
332c25056deSgw 
333c25056deSgw 	mutex_destroy(&spa->spa_uberblock_lock);
3345ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
335c25056deSgw 	mutex_destroy(&spa->spa_config_cache_lock);
336c25056deSgw 	mutex_destroy(&spa->spa_scrub_lock);
337c25056deSgw 	mutex_destroy(&spa->spa_errlog_lock);
338c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
339c25056deSgw 	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
34006eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
341b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
3420a4e9518Sgw 	mutex_destroy(&spa->spa_zio_lock);
3435ad82045Snd 
344fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
345fa9e4066Sahrens }
346fa9e4066Sahrens 
347fa9e4066Sahrens /*
348fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
349fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
350fa9e4066Sahrens  */
351fa9e4066Sahrens spa_t *
352fa9e4066Sahrens spa_next(spa_t *prev)
353fa9e4066Sahrens {
354fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
355fa9e4066Sahrens 
356fa9e4066Sahrens 	if (prev)
357fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
358fa9e4066Sahrens 	else
359fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
360fa9e4066Sahrens }
361fa9e4066Sahrens 
362fa9e4066Sahrens /*
363fa9e4066Sahrens  * ==========================================================================
364fa9e4066Sahrens  * SPA refcount functions
365fa9e4066Sahrens  * ==========================================================================
366fa9e4066Sahrens  */
367fa9e4066Sahrens 
368fa9e4066Sahrens /*
369fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
370fa9e4066Sahrens  * have the namespace lock held.
371fa9e4066Sahrens  */
372fa9e4066Sahrens void
373fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
374fa9e4066Sahrens {
375fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
376fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
377fa9e4066Sahrens 
378fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
379fa9e4066Sahrens }
380fa9e4066Sahrens 
381fa9e4066Sahrens /*
382fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
383fa9e4066Sahrens  * have the namespace lock held.
384fa9e4066Sahrens  */
385fa9e4066Sahrens void
386fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
387fa9e4066Sahrens {
388fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
389fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
390fa9e4066Sahrens 
391fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
392fa9e4066Sahrens }
393fa9e4066Sahrens 
394fa9e4066Sahrens /*
395fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
396fa9e4066Sahrens  * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
397fa9e4066Sahrens  * number of references acquired when opening a pool
398fa9e4066Sahrens  */
399fa9e4066Sahrens boolean_t
400fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
401fa9e4066Sahrens {
402fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
403fa9e4066Sahrens 
404fa9e4066Sahrens 	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
405fa9e4066Sahrens }
406fa9e4066Sahrens 
40799653d4eSeschrock /*
40899653d4eSeschrock  * ==========================================================================
40999653d4eSeschrock  * SPA spare tracking
41099653d4eSeschrock  * ==========================================================================
41199653d4eSeschrock  */
41299653d4eSeschrock 
41399653d4eSeschrock /*
41439c23413Seschrock  * Spares are tracked globally due to the following constraints:
41539c23413Seschrock  *
41639c23413Seschrock  * 	- A spare may be part of multiple pools.
41739c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
41839c23413Seschrock  *	  another pool.
41939c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
42039c23413Seschrock  *	  the target is a spare in the same pool.
42139c23413Seschrock  *
42239c23413Seschrock  * We keep track of all spares on the system through the use of a reference
42339c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
42439c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
42539c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
42639c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
42739c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
42839c23413Seschrock  *
42939c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
43039c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
43139c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
43239c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
43399653d4eSeschrock  */
43439c23413Seschrock 
43599653d4eSeschrock typedef struct spa_spare {
43699653d4eSeschrock 	uint64_t	spare_guid;
43739c23413Seschrock 	uint64_t	spare_pool;
43899653d4eSeschrock 	avl_node_t	spare_avl;
43999653d4eSeschrock 	int		spare_count;
44099653d4eSeschrock } spa_spare_t;
44199653d4eSeschrock 
44299653d4eSeschrock static int
44399653d4eSeschrock spa_spare_compare(const void *a, const void *b)
44499653d4eSeschrock {
44599653d4eSeschrock 	const spa_spare_t *sa = a;
44699653d4eSeschrock 	const spa_spare_t *sb = b;
44799653d4eSeschrock 
44899653d4eSeschrock 	if (sa->spare_guid < sb->spare_guid)
44999653d4eSeschrock 		return (-1);
45099653d4eSeschrock 	else if (sa->spare_guid > sb->spare_guid)
45199653d4eSeschrock 		return (1);
45299653d4eSeschrock 	else
45399653d4eSeschrock 		return (0);
45499653d4eSeschrock }
45599653d4eSeschrock 
45699653d4eSeschrock void
45739c23413Seschrock spa_spare_add(vdev_t *vd)
45899653d4eSeschrock {
45999653d4eSeschrock 	avl_index_t where;
46099653d4eSeschrock 	spa_spare_t search;
46199653d4eSeschrock 	spa_spare_t *spare;
46299653d4eSeschrock 
46399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
46439c23413Seschrock 	ASSERT(!vd->vdev_isspare);
46599653d4eSeschrock 
46639c23413Seschrock 	search.spare_guid = vd->vdev_guid;
46799653d4eSeschrock 	if ((spare = avl_find(&spa_spare_avl, &search, &where)) != NULL) {
46899653d4eSeschrock 		spare->spare_count++;
46999653d4eSeschrock 	} else {
47039c23413Seschrock 		spare = kmem_zalloc(sizeof (spa_spare_t), KM_SLEEP);
47139c23413Seschrock 		spare->spare_guid = vd->vdev_guid;
47299653d4eSeschrock 		spare->spare_count = 1;
47399653d4eSeschrock 		avl_insert(&spa_spare_avl, spare, where);
47499653d4eSeschrock 	}
47539c23413Seschrock 	vd->vdev_isspare = B_TRUE;
47699653d4eSeschrock 
47799653d4eSeschrock 	mutex_exit(&spa_spare_lock);
47899653d4eSeschrock }
47999653d4eSeschrock 
48099653d4eSeschrock void
48139c23413Seschrock spa_spare_remove(vdev_t *vd)
48299653d4eSeschrock {
48399653d4eSeschrock 	spa_spare_t search;
48499653d4eSeschrock 	spa_spare_t *spare;
48599653d4eSeschrock 	avl_index_t where;
48699653d4eSeschrock 
48799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
48899653d4eSeschrock 
48939c23413Seschrock 	search.spare_guid = vd->vdev_guid;
49099653d4eSeschrock 	spare = avl_find(&spa_spare_avl, &search, &where);
49199653d4eSeschrock 
49239c23413Seschrock 	ASSERT(vd->vdev_isspare);
49399653d4eSeschrock 	ASSERT(spare != NULL);
49499653d4eSeschrock 
49599653d4eSeschrock 	if (--spare->spare_count == 0) {
49699653d4eSeschrock 		avl_remove(&spa_spare_avl, spare);
49799653d4eSeschrock 		kmem_free(spare, sizeof (spa_spare_t));
49839c23413Seschrock 	} else if (spare->spare_pool == spa_guid(vd->vdev_spa)) {
49939c23413Seschrock 		spare->spare_pool = 0ULL;
50099653d4eSeschrock 	}
50199653d4eSeschrock 
50239c23413Seschrock 	vd->vdev_isspare = B_FALSE;
50399653d4eSeschrock 	mutex_exit(&spa_spare_lock);
50499653d4eSeschrock }
50599653d4eSeschrock 
50699653d4eSeschrock boolean_t
50739c23413Seschrock spa_spare_exists(uint64_t guid, uint64_t *pool)
50899653d4eSeschrock {
50939c23413Seschrock 	spa_spare_t search, *found;
51099653d4eSeschrock 	avl_index_t where;
51199653d4eSeschrock 
51299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
51399653d4eSeschrock 
51499653d4eSeschrock 	search.spare_guid = guid;
51539c23413Seschrock 	found = avl_find(&spa_spare_avl, &search, &where);
51639c23413Seschrock 
51739c23413Seschrock 	if (pool) {
51839c23413Seschrock 		if (found)
51939c23413Seschrock 			*pool = found->spare_pool;
52039c23413Seschrock 		else
52139c23413Seschrock 			*pool = 0ULL;
52239c23413Seschrock 	}
52399653d4eSeschrock 
52499653d4eSeschrock 	mutex_exit(&spa_spare_lock);
52599653d4eSeschrock 
52639c23413Seschrock 	return (found != NULL);
52739c23413Seschrock }
52839c23413Seschrock 
52939c23413Seschrock void
53039c23413Seschrock spa_spare_activate(vdev_t *vd)
53139c23413Seschrock {
53239c23413Seschrock 	spa_spare_t search, *found;
53339c23413Seschrock 	avl_index_t where;
53439c23413Seschrock 
53539c23413Seschrock 	mutex_enter(&spa_spare_lock);
53639c23413Seschrock 	ASSERT(vd->vdev_isspare);
53739c23413Seschrock 
53839c23413Seschrock 	search.spare_guid = vd->vdev_guid;
53939c23413Seschrock 	found = avl_find(&spa_spare_avl, &search, &where);
54039c23413Seschrock 	ASSERT(found != NULL);
54139c23413Seschrock 	ASSERT(found->spare_pool == 0ULL);
54239c23413Seschrock 
54339c23413Seschrock 	found->spare_pool = spa_guid(vd->vdev_spa);
54439c23413Seschrock 	mutex_exit(&spa_spare_lock);
54599653d4eSeschrock }
54699653d4eSeschrock 
547fa9e4066Sahrens /*
548fa9e4066Sahrens  * ==========================================================================
549fa9e4066Sahrens  * SPA config locking
550fa9e4066Sahrens  * ==========================================================================
551fa9e4066Sahrens  */
552fa9e4066Sahrens void
553ea8dc4b6Seschrock spa_config_enter(spa_t *spa, krw_t rw, void *tag)
554fa9e4066Sahrens {
55591ebeef5Sahrens 	rprw_enter(&spa->spa_config_lock, rw, tag);
556fa9e4066Sahrens }
557fa9e4066Sahrens 
558fa9e4066Sahrens void
559ea8dc4b6Seschrock spa_config_exit(spa_t *spa, void *tag)
560fa9e4066Sahrens {
56191ebeef5Sahrens 	rprw_exit(&spa->spa_config_lock, tag);
562fa9e4066Sahrens }
563fa9e4066Sahrens 
564fa9e4066Sahrens boolean_t
565fa9e4066Sahrens spa_config_held(spa_t *spa, krw_t rw)
566fa9e4066Sahrens {
56791ebeef5Sahrens 	return (rprw_held(&spa->spa_config_lock, rw));
568fa9e4066Sahrens }
569fa9e4066Sahrens 
570fa9e4066Sahrens /*
571fa9e4066Sahrens  * ==========================================================================
572fa9e4066Sahrens  * SPA vdev locking
573fa9e4066Sahrens  * ==========================================================================
574fa9e4066Sahrens  */
575fa9e4066Sahrens 
576fa9e4066Sahrens /*
577ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
578ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
579fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
580fa9e4066Sahrens  */
581fa9e4066Sahrens uint64_t
582fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
583fa9e4066Sahrens {
5843d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
5853d7072f8Seschrock 
586ea8dc4b6Seschrock 	/*
5873d7072f8Seschrock 	 * Suspend scrub activity while we mess with the config.  We must do
5883d7072f8Seschrock 	 * this after acquiring the namespace lock to avoid a 3-way deadlock
5893d7072f8Seschrock 	 * with spa_scrub_stop() and the scrub thread.
590ea8dc4b6Seschrock 	 */
591ea8dc4b6Seschrock 	spa_scrub_suspend(spa);
592fa9e4066Sahrens 
593ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, spa);
594fa9e4066Sahrens 
595fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
596fa9e4066Sahrens }
597fa9e4066Sahrens 
598fa9e4066Sahrens /*
599fa9e4066Sahrens  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
600fa9e4066Sahrens  * locking of spa_vdev_enter(), we also want make sure the transactions have
601fa9e4066Sahrens  * synced to disk, and then update the global configuration cache with the new
602fa9e4066Sahrens  * information.
603fa9e4066Sahrens  */
604fa9e4066Sahrens int
605fa9e4066Sahrens spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
606fa9e4066Sahrens {
6070e34b6a7Sbonwick 	int config_changed = B_FALSE;
608ea8dc4b6Seschrock 
6090373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
6100e34b6a7Sbonwick 
6110e34b6a7Sbonwick 	/*
6120e34b6a7Sbonwick 	 * Reassess the DTLs.
6130e34b6a7Sbonwick 	 */
6140373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
6150e34b6a7Sbonwick 
6160e34b6a7Sbonwick 	/*
6170373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
6180e34b6a7Sbonwick 	 */
6190e34b6a7Sbonwick 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
6200e34b6a7Sbonwick 		config_changed = B_TRUE;
6210373e76bSbonwick 		spa_scrub_restart(spa, txg);
6220e34b6a7Sbonwick 	}
623ea8dc4b6Seschrock 
624ea8dc4b6Seschrock 	spa_config_exit(spa, spa);
625fa9e4066Sahrens 
626ea8dc4b6Seschrock 	/*
6275dabedeeSbonwick 	 * Allow scrubbing to resume.
628ea8dc4b6Seschrock 	 */
629ea8dc4b6Seschrock 	spa_scrub_resume(spa);
630fa9e4066Sahrens 
631fa9e4066Sahrens 	/*
632fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
633fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
634fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
635fa9e4066Sahrens 	 */
636fa9e4066Sahrens 	if (error == 0)
637fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
638fa9e4066Sahrens 
639fa9e4066Sahrens 	if (vd != NULL) {
640fa9e4066Sahrens 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
641fa9e4066Sahrens 		vdev_free(vd);
642fa9e4066Sahrens 	}
643fa9e4066Sahrens 
644fa9e4066Sahrens 	/*
6450e34b6a7Sbonwick 	 * If the config changed, update the config cache.
646fa9e4066Sahrens 	 */
6470e34b6a7Sbonwick 	if (config_changed)
648fa9e4066Sahrens 		spa_config_sync();
649ea8dc4b6Seschrock 
650ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
651fa9e4066Sahrens 
652fa9e4066Sahrens 	return (error);
653fa9e4066Sahrens }
654fa9e4066Sahrens 
655fa9e4066Sahrens /*
656fa9e4066Sahrens  * ==========================================================================
657fa9e4066Sahrens  * Miscellaneous functions
658fa9e4066Sahrens  * ==========================================================================
659fa9e4066Sahrens  */
660fa9e4066Sahrens 
661fa9e4066Sahrens /*
662fa9e4066Sahrens  * Rename a spa_t.
663fa9e4066Sahrens  */
664fa9e4066Sahrens int
665fa9e4066Sahrens spa_rename(const char *name, const char *newname)
666fa9e4066Sahrens {
667fa9e4066Sahrens 	spa_t *spa;
668fa9e4066Sahrens 	int err;
669fa9e4066Sahrens 
670fa9e4066Sahrens 	/*
671fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
672fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
673fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
674ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
675fa9e4066Sahrens 	 */
676fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
677fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
678fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
679fa9e4066Sahrens 		return (err);
680fa9e4066Sahrens 	}
681fa9e4066Sahrens 
682ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
683fa9e4066Sahrens 
684fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
685fa9e4066Sahrens 	spa_strfree(spa->spa_name);
686fa9e4066Sahrens 	spa->spa_name = spa_strdup(newname);
687fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
688fa9e4066Sahrens 
689fa9e4066Sahrens 	/*
690fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
691fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
692fa9e4066Sahrens 	 * during the sync.
693fa9e4066Sahrens 	 */
694fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
695fa9e4066Sahrens 
696ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
697fa9e4066Sahrens 
6980373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
699fa9e4066Sahrens 
700fa9e4066Sahrens 	/*
701fa9e4066Sahrens 	 * Sync the updated config cache.
702fa9e4066Sahrens 	 */
703fa9e4066Sahrens 	spa_config_sync();
704fa9e4066Sahrens 
705fa9e4066Sahrens 	spa_close(spa, FTAG);
706fa9e4066Sahrens 
707fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
708fa9e4066Sahrens 
709fa9e4066Sahrens 	return (0);
710fa9e4066Sahrens }
711fa9e4066Sahrens 
712fa9e4066Sahrens 
713fa9e4066Sahrens /*
714fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
715fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
716fa9e4066Sahrens  * specified device_guid.
717fa9e4066Sahrens  */
718fa9e4066Sahrens boolean_t
719fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
720fa9e4066Sahrens {
721fa9e4066Sahrens 	spa_t *spa;
722fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
723fa9e4066Sahrens 
724ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
725fa9e4066Sahrens 
726fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
727fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
728fa9e4066Sahrens 			continue;
729fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
730fa9e4066Sahrens 			continue;
73139c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
73239c23413Seschrock 			if (device_guid == 0)
73339c23413Seschrock 				break;
73439c23413Seschrock 
73539c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
73639c23413Seschrock 			    device_guid) != NULL)
73739c23413Seschrock 				break;
73839c23413Seschrock 
73939c23413Seschrock 			/*
7408654d025Sperrin 			 * Check any devices we may be in the process of adding.
74139c23413Seschrock 			 */
74239c23413Seschrock 			if (spa->spa_pending_vdev) {
74339c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
74439c23413Seschrock 				    device_guid) != NULL)
74539c23413Seschrock 					break;
74639c23413Seschrock 			}
74739c23413Seschrock 		}
748fa9e4066Sahrens 	}
749fa9e4066Sahrens 
750fa9e4066Sahrens 	return (spa != NULL);
751fa9e4066Sahrens }
752fa9e4066Sahrens 
753fa9e4066Sahrens char *
754fa9e4066Sahrens spa_strdup(const char *s)
755fa9e4066Sahrens {
756fa9e4066Sahrens 	size_t len;
757fa9e4066Sahrens 	char *new;
758fa9e4066Sahrens 
759fa9e4066Sahrens 	len = strlen(s);
760fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
761fa9e4066Sahrens 	bcopy(s, new, len);
762fa9e4066Sahrens 	new[len] = '\0';
763fa9e4066Sahrens 
764fa9e4066Sahrens 	return (new);
765fa9e4066Sahrens }
766fa9e4066Sahrens 
767fa9e4066Sahrens void
768fa9e4066Sahrens spa_strfree(char *s)
769fa9e4066Sahrens {
770fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
771fa9e4066Sahrens }
772fa9e4066Sahrens 
773fa9e4066Sahrens uint64_t
774fa9e4066Sahrens spa_get_random(uint64_t range)
775fa9e4066Sahrens {
776fa9e4066Sahrens 	uint64_t r;
777fa9e4066Sahrens 
778fa9e4066Sahrens 	ASSERT(range != 0);
779fa9e4066Sahrens 
780fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
781fa9e4066Sahrens 
782fa9e4066Sahrens 	return (r % range);
783fa9e4066Sahrens }
784fa9e4066Sahrens 
785fa9e4066Sahrens void
786d80c45e0Sbonwick sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
787fa9e4066Sahrens {
78844cd46caSbillm 	int d;
789fa9e4066Sahrens 
790fa9e4066Sahrens 	if (bp == NULL) {
791fbabab8fSmaybee 		(void) snprintf(buf, len, "<NULL>");
792fa9e4066Sahrens 		return;
793fa9e4066Sahrens 	}
794fa9e4066Sahrens 
795fa9e4066Sahrens 	if (BP_IS_HOLE(bp)) {
796fbabab8fSmaybee 		(void) snprintf(buf, len, "<hole>");
797fa9e4066Sahrens 		return;
798fa9e4066Sahrens 	}
799fa9e4066Sahrens 
80044cd46caSbillm 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
801fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LEVEL(bp),
802fa9e4066Sahrens 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
803fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LSIZE(bp),
80444cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp));
80544cd46caSbillm 
80644cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
807d80c45e0Sbonwick 		const dva_t *dva = &bp->blk_dva[d];
80844cd46caSbillm 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
80944cd46caSbillm 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
81044cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(dva),
81144cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(dva),
81244cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(dva));
81344cd46caSbillm 	}
81444cd46caSbillm 
81544cd46caSbillm 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
81644cd46caSbillm 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
817fa9e4066Sahrens 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
818fa9e4066Sahrens 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
819fa9e4066Sahrens 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
82044cd46caSbillm 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
821fa9e4066Sahrens 	    (u_longlong_t)bp->blk_birth,
822fa9e4066Sahrens 	    (u_longlong_t)bp->blk_fill,
823fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
824fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
825fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
826fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
827fa9e4066Sahrens }
828fa9e4066Sahrens 
829fa9e4066Sahrens void
830fa9e4066Sahrens spa_freeze(spa_t *spa)
831fa9e4066Sahrens {
832fa9e4066Sahrens 	uint64_t freeze_txg = 0;
833fa9e4066Sahrens 
834ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
835fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
836fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
837fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
838fa9e4066Sahrens 	}
839ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
840fa9e4066Sahrens 	if (freeze_txg != 0)
841fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
842fa9e4066Sahrens }
843fa9e4066Sahrens 
8440125049cSahrens void
8450125049cSahrens zfs_panic_recover(const char *fmt, ...)
8460125049cSahrens {
8470125049cSahrens 	va_list adx;
8480125049cSahrens 
8490125049cSahrens 	va_start(adx, fmt);
8500125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
8510125049cSahrens 	va_end(adx);
8520125049cSahrens }
8530125049cSahrens 
854fa9e4066Sahrens /*
855fa9e4066Sahrens  * ==========================================================================
856fa9e4066Sahrens  * Accessor functions
857fa9e4066Sahrens  * ==========================================================================
858fa9e4066Sahrens  */
859fa9e4066Sahrens 
860fa9e4066Sahrens krwlock_t *
861fa9e4066Sahrens spa_traverse_rwlock(spa_t *spa)
862fa9e4066Sahrens {
863fa9e4066Sahrens 	return (&spa->spa_traverse_lock);
864fa9e4066Sahrens }
865fa9e4066Sahrens 
866fa9e4066Sahrens int
867fa9e4066Sahrens spa_traverse_wanted(spa_t *spa)
868fa9e4066Sahrens {
869fa9e4066Sahrens 	return (spa->spa_traverse_wanted);
870fa9e4066Sahrens }
871fa9e4066Sahrens 
872fa9e4066Sahrens dsl_pool_t *
873fa9e4066Sahrens spa_get_dsl(spa_t *spa)
874fa9e4066Sahrens {
875fa9e4066Sahrens 	return (spa->spa_dsl_pool);
876fa9e4066Sahrens }
877fa9e4066Sahrens 
878fa9e4066Sahrens blkptr_t *
879fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
880fa9e4066Sahrens {
881fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
882fa9e4066Sahrens }
883fa9e4066Sahrens 
884fa9e4066Sahrens void
885fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
886fa9e4066Sahrens {
887fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
888fa9e4066Sahrens }
889fa9e4066Sahrens 
890fa9e4066Sahrens void
891fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
892fa9e4066Sahrens {
893fa9e4066Sahrens 	if (spa->spa_root == NULL)
894fa9e4066Sahrens 		buf[0] = '\0';
895fa9e4066Sahrens 	else
896fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
897fa9e4066Sahrens }
898fa9e4066Sahrens 
899fa9e4066Sahrens int
900fa9e4066Sahrens spa_sync_pass(spa_t *spa)
901fa9e4066Sahrens {
902fa9e4066Sahrens 	return (spa->spa_sync_pass);
903fa9e4066Sahrens }
904fa9e4066Sahrens 
905fa9e4066Sahrens char *
906fa9e4066Sahrens spa_name(spa_t *spa)
907fa9e4066Sahrens {
908fa9e4066Sahrens 	/*
909fa9e4066Sahrens 	 * Accessing the name requires holding either the namespace lock or the
910fa9e4066Sahrens 	 * config lock, both of which are required to do a rename.
911fa9e4066Sahrens 	 */
912fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
913fa9e4066Sahrens 	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
914fa9e4066Sahrens 
915fa9e4066Sahrens 	return (spa->spa_name);
916fa9e4066Sahrens }
917fa9e4066Sahrens 
918fa9e4066Sahrens uint64_t
919fa9e4066Sahrens spa_guid(spa_t *spa)
920fa9e4066Sahrens {
921b5989ec7Seschrock 	/*
922b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
923b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
924b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
925b5989ec7Seschrock 	 * this case.
926b5989ec7Seschrock 	 */
927b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
928b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
929b5989ec7Seschrock 	else
930b5989ec7Seschrock 		return (spa->spa_load_guid);
931fa9e4066Sahrens }
932fa9e4066Sahrens 
933fa9e4066Sahrens uint64_t
934fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
935fa9e4066Sahrens {
936fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
937fa9e4066Sahrens }
938fa9e4066Sahrens 
939fa9e4066Sahrens uint64_t
940fa9e4066Sahrens spa_first_txg(spa_t *spa)
941fa9e4066Sahrens {
942fa9e4066Sahrens 	return (spa->spa_first_txg);
943fa9e4066Sahrens }
944fa9e4066Sahrens 
945fa9e4066Sahrens int
946fa9e4066Sahrens spa_state(spa_t *spa)
947fa9e4066Sahrens {
948fa9e4066Sahrens 	return (spa->spa_state);
949fa9e4066Sahrens }
950fa9e4066Sahrens 
951fa9e4066Sahrens uint64_t
952fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
953fa9e4066Sahrens {
954fa9e4066Sahrens 	return (spa->spa_freeze_txg);
955fa9e4066Sahrens }
956fa9e4066Sahrens 
957fa9e4066Sahrens /*
95899653d4eSeschrock  * Return how much space is allocated in the pool (ie. sum of all asize)
959fa9e4066Sahrens  */
960fa9e4066Sahrens uint64_t
961fa9e4066Sahrens spa_get_alloc(spa_t *spa)
962fa9e4066Sahrens {
963fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
964fa9e4066Sahrens }
965fa9e4066Sahrens 
966fa9e4066Sahrens /*
96799653d4eSeschrock  * Return how much (raid-z inflated) space there is in the pool.
968fa9e4066Sahrens  */
969fa9e4066Sahrens uint64_t
970fa9e4066Sahrens spa_get_space(spa_t *spa)
971fa9e4066Sahrens {
972fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_space);
973fa9e4066Sahrens }
974fa9e4066Sahrens 
97599653d4eSeschrock /*
97699653d4eSeschrock  * Return the amount of raid-z-deflated space in the pool.
97799653d4eSeschrock  */
97899653d4eSeschrock uint64_t
97999653d4eSeschrock spa_get_dspace(spa_t *spa)
98099653d4eSeschrock {
98199653d4eSeschrock 	if (spa->spa_deflate)
98299653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
98399653d4eSeschrock 	else
98499653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_space);
98599653d4eSeschrock }
98699653d4eSeschrock 
987fa9e4066Sahrens /* ARGSUSED */
988fa9e4066Sahrens uint64_t
989fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
990fa9e4066Sahrens {
991fa9e4066Sahrens 	/*
992fa9e4066Sahrens 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
993fa9e4066Sahrens 	 * case the space requirement is exactly 2x; so just assume that.
99444cd46caSbillm 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
99544cd46caSbillm 	 * we have to multiply by a total of 6x.
99644cd46caSbillm 	 */
99744cd46caSbillm 	return (lsize * 6);
99844cd46caSbillm }
99944cd46caSbillm 
10000a4e9518Sgw /*
10010a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
10020a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
10030a4e9518Sgw  */
10040a4e9518Sgw uint8_t
10050a4e9518Sgw spa_get_failmode(spa_t *spa)
10060a4e9518Sgw {
10070a4e9518Sgw 	return (spa->spa_failmode);
10080a4e9518Sgw }
10090a4e9518Sgw 
101044cd46caSbillm uint64_t
101144cd46caSbillm spa_version(spa_t *spa)
101244cd46caSbillm {
101344cd46caSbillm 	return (spa->spa_ubsync.ub_version);
101444cd46caSbillm }
101544cd46caSbillm 
101644cd46caSbillm int
101744cd46caSbillm spa_max_replication(spa_t *spa)
101844cd46caSbillm {
101944cd46caSbillm 	/*
1020e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
102144cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
102244cd46caSbillm 	 * replication level accordingly.
1023fa9e4066Sahrens 	 */
1024e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
102544cd46caSbillm 		return (1);
102644cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1027fa9e4066Sahrens }
1028fa9e4066Sahrens 
102999653d4eSeschrock uint64_t
103099653d4eSeschrock bp_get_dasize(spa_t *spa, const blkptr_t *bp)
103199653d4eSeschrock {
103299653d4eSeschrock 	int sz = 0, i;
103399653d4eSeschrock 
103499653d4eSeschrock 	if (!spa->spa_deflate)
103599653d4eSeschrock 		return (BP_GET_ASIZE(bp));
103699653d4eSeschrock 
10371934e92fSmaybee 	spa_config_enter(spa, RW_READER, FTAG);
103899653d4eSeschrock 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
103999653d4eSeschrock 		vdev_t *vd =
104099653d4eSeschrock 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
10411934e92fSmaybee 		if (vd)
10421934e92fSmaybee 			sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >>
10431934e92fSmaybee 			    SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
104499653d4eSeschrock 	}
10451934e92fSmaybee 	spa_config_exit(spa, FTAG);
104699653d4eSeschrock 	return (sz);
104799653d4eSeschrock }
104899653d4eSeschrock 
1049fa9e4066Sahrens /*
1050fa9e4066Sahrens  * ==========================================================================
1051fa9e4066Sahrens  * Initialization and Termination
1052fa9e4066Sahrens  * ==========================================================================
1053fa9e4066Sahrens  */
1054fa9e4066Sahrens 
1055fa9e4066Sahrens static int
1056fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1057fa9e4066Sahrens {
1058fa9e4066Sahrens 	const spa_t *s1 = a1;
1059fa9e4066Sahrens 	const spa_t *s2 = a2;
1060fa9e4066Sahrens 	int s;
1061fa9e4066Sahrens 
1062fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1063fa9e4066Sahrens 	if (s > 0)
1064fa9e4066Sahrens 		return (1);
1065fa9e4066Sahrens 	if (s < 0)
1066fa9e4066Sahrens 		return (-1);
1067fa9e4066Sahrens 	return (0);
1068fa9e4066Sahrens }
1069fa9e4066Sahrens 
10700373e76bSbonwick int
10710373e76bSbonwick spa_busy(void)
10720373e76bSbonwick {
10730373e76bSbonwick 	return (spa_active_count);
10740373e76bSbonwick }
10750373e76bSbonwick 
1076fa9e4066Sahrens void
1077fa9e4066Sahrens spa_init(int mode)
1078fa9e4066Sahrens {
1079fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1080c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1081fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1082fa9e4066Sahrens 
1083fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1084fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1085fa9e4066Sahrens 
108699653d4eSeschrock 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_spare_t),
108799653d4eSeschrock 	    offsetof(spa_spare_t, spare_avl));
108899653d4eSeschrock 
1089fa9e4066Sahrens 	spa_mode = mode;
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 	refcount_init();
1092fa9e4066Sahrens 	unique_init();
1093fa9e4066Sahrens 	zio_init();
1094fa9e4066Sahrens 	dmu_init();
1095fa9e4066Sahrens 	zil_init();
109691ebeef5Sahrens 	zfs_prop_init();
1097990b4856Slling 	zpool_prop_init();
1098fa9e4066Sahrens 	spa_config_load();
1099fa9e4066Sahrens }
1100fa9e4066Sahrens 
1101fa9e4066Sahrens void
1102fa9e4066Sahrens spa_fini(void)
1103fa9e4066Sahrens {
1104fa9e4066Sahrens 	spa_evict_all();
1105fa9e4066Sahrens 
1106fa9e4066Sahrens 	zil_fini();
1107fa9e4066Sahrens 	dmu_fini();
1108fa9e4066Sahrens 	zio_fini();
110991ebeef5Sahrens 	unique_fini();
1110fa9e4066Sahrens 	refcount_fini();
1111fa9e4066Sahrens 
1112fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
111399653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1114fa9e4066Sahrens 
1115fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1116fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1117c25056deSgw 	mutex_destroy(&spa_spare_lock);
1118fa9e4066Sahrens }
11196ce0521aSperrin 
11206ce0521aSperrin /*
11216ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
11226ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
11236ce0521aSperrin  * performance and not correctness
11246ce0521aSperrin  */
11256ce0521aSperrin boolean_t
11266ce0521aSperrin spa_has_slogs(spa_t *spa)
11276ce0521aSperrin {
11286ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
11296ce0521aSperrin }
1130