xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 0125049cd6136d1d2ca9e982382a915b6d7916ce)
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>
47fa9e4066Sahrens 
48fa9e4066Sahrens /*
49fa9e4066Sahrens  * SPA locking
50fa9e4066Sahrens  *
51fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * spa_namespace_lock (global mutex)
54fa9e4066Sahrens  *
5544cd46caSbillm  *	This lock must be acquired to do any of the following:
56fa9e4066Sahrens  *
5744cd46caSbillm  *		- Lookup a spa_t by name
5844cd46caSbillm  *		- Add or remove a spa_t from the namespace
5944cd46caSbillm  *		- Increase spa_refcount from non-zero
6044cd46caSbillm  *		- Check if spa_refcount is zero
6144cd46caSbillm  *		- Rename a spa_t
62ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
6344cd46caSbillm  *		- Held for the duration of create/destroy/import/export
64fa9e4066Sahrens  *
6544cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
6644cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
6744cd46caSbillm  *	definition they must have an existing reference, and will never need
6844cd46caSbillm  *	to lookup a spa_t by name.
69fa9e4066Sahrens  *
70fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
71fa9e4066Sahrens  *
7244cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
7344cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7444cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
7544cd46caSbillm  *	some references in the DMU.  Internally we check against SPA_MINREF, but
7644cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
77fa9e4066Sahrens  *
78fa9e4066Sahrens  * spa_config_lock (per-spa crazy rwlock)
79fa9e4066Sahrens  *
8044cd46caSbillm  *	This SPA special is a recursive rwlock, capable of being acquired from
8144cd46caSbillm  *	asynchronous threads.  It has protects the spa_t from config changes,
8244cd46caSbillm  *	and must be held in the following circumstances:
83fa9e4066Sahrens  *
8444cd46caSbillm  *		- RW_READER to perform I/O to the spa
8544cd46caSbillm  *		- RW_WRITER to change the vdev config
86fa9e4066Sahrens  *
87fa9e4066Sahrens  * spa_config_cache_lock (per-spa mutex)
88fa9e4066Sahrens  *
8944cd46caSbillm  *	This mutex prevents the spa_config nvlist from being updated.  No
90fa9e4066Sahrens  *      other locks are required to obtain this lock, although implicitly you
91fa9e4066Sahrens  *      must have the namespace lock or non-zero refcount to have any kind
92fa9e4066Sahrens  *      of spa_t pointer at all.
93fa9e4066Sahrens  *
94fa9e4066Sahrens  * The locking order is fairly straightforward:
95fa9e4066Sahrens  *
9644cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
97fa9e4066Sahrens  *
9844cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
9944cd46caSbillm  *	or to check if it is zero.
100fa9e4066Sahrens  *
10144cd46caSbillm  *		spa_refcount		->	spa_config_lock
102fa9e4066Sahrens  *
10344cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10444cd46caSbillm  *	the config lock.
105fa9e4066Sahrens  *
10644cd46caSbillm  *		spa_namespace_lock	->	spa_config_lock
107fa9e4066Sahrens  *
10844cd46caSbillm  *	The namespace lock must always be taken before the config lock.
109fa9e4066Sahrens  *
110fa9e4066Sahrens  *
111fa9e4066Sahrens  * The spa_namespace_lock and spa_config_cache_lock can be acquired directly and
112fa9e4066Sahrens  * are globally visible.
113fa9e4066Sahrens  *
114fa9e4066Sahrens  * The namespace is manipulated using the following functions, all which require
115fa9e4066Sahrens  * the spa_namespace_lock to be held.
116fa9e4066Sahrens  *
11744cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
118fa9e4066Sahrens  *
11944cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
120fa9e4066Sahrens  *
12144cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
12244cd46caSbillm  *				frees up any memory associated with the spa_t.
123fa9e4066Sahrens  *
12444cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12544cd46caSbillm  *				first if NULL is passed.
126fa9e4066Sahrens  *
12744cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12844cd46caSbillm  *				the system.
129fa9e4066Sahrens  *
130ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
131fa9e4066Sahrens  *
132fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
133fa9e4066Sahrens  *
13444cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13544cd46caSbillm  *				called with spa_namespace_lock held if the
13644cd46caSbillm  *				refcount is currently zero.
137fa9e4066Sahrens  *
13844cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13944cd46caSbillm  *				not free the spa_t or remove it from the
14044cd46caSbillm  *				namespace.  No locking is required.
141fa9e4066Sahrens  *
14244cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14344cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14444cd46caSbillm  *				held.
145fa9e4066Sahrens  *
146fa9e4066Sahrens  * The spa_config_lock is manipulated using the following functions:
147fa9e4066Sahrens  *
14844cd46caSbillm  *	spa_config_enter()	Acquire the config lock as RW_READER or
14944cd46caSbillm  *				RW_WRITER.  At least one reference on the spa_t
15044cd46caSbillm  *				must exist.
151fa9e4066Sahrens  *
15244cd46caSbillm  *	spa_config_exit()	Release the config lock.
153fa9e4066Sahrens  *
15444cd46caSbillm  *	spa_config_held()	Returns true if the config lock is currently
15544cd46caSbillm  *				held in the given state.
156fa9e4066Sahrens  *
157ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
158fa9e4066Sahrens  *
15944cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
160ea8dc4b6Seschrock  *				for writing.
161fa9e4066Sahrens  *
16244cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
16344cd46caSbillm  *				to complete, sync the updated configs to the
164ea8dc4b6Seschrock  *				cache, and release the namespace lock.
165fa9e4066Sahrens  *
166fa9e4066Sahrens  * The spa_name() function also requires either the spa_namespace_lock
167fa9e4066Sahrens  * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
168fa9e4066Sahrens  * also implemented within this file since is requires manipulation of the
169fa9e4066Sahrens  * namespace.
170fa9e4066Sahrens  */
171fa9e4066Sahrens 
172fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
173fa9e4066Sahrens kmutex_t spa_namespace_lock;
174fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
1750373e76bSbonwick static int spa_active_count;
176416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
177fa9e4066Sahrens 
17899653d4eSeschrock static kmutex_t spa_spare_lock;
17939c23413Seschrock static avl_tree_t spa_spare_avl;
18099653d4eSeschrock 
181fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
182fa9e4066Sahrens int spa_mode;
183fa9e4066Sahrens 
184fa9e4066Sahrens #ifdef ZFS_DEBUG
185fa9e4066Sahrens int zfs_flags = ~0;
186fa9e4066Sahrens #else
187fa9e4066Sahrens int zfs_flags = 0;
188fa9e4066Sahrens #endif
189fa9e4066Sahrens 
190*0125049cSahrens /*
191*0125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
192*0125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
193*0125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
194*0125049cSahrens  */
195*0125049cSahrens int zfs_recover = 0;
196*0125049cSahrens 
197fa9e4066Sahrens #define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
198fa9e4066Sahrens 
199fa9e4066Sahrens /*
200fa9e4066Sahrens  * ==========================================================================
201fa9e4066Sahrens  * SPA namespace functions
202fa9e4066Sahrens  * ==========================================================================
203fa9e4066Sahrens  */
204fa9e4066Sahrens 
205fa9e4066Sahrens /*
206fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
207fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
208fa9e4066Sahrens  */
209fa9e4066Sahrens spa_t *
210fa9e4066Sahrens spa_lookup(const char *name)
211fa9e4066Sahrens {
212fa9e4066Sahrens 	spa_t search, *spa;
213fa9e4066Sahrens 	avl_index_t where;
214fa9e4066Sahrens 
215fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
216fa9e4066Sahrens 
217fa9e4066Sahrens 	search.spa_name = (char *)name;
218fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	return (spa);
221fa9e4066Sahrens }
222fa9e4066Sahrens 
223fa9e4066Sahrens /*
224fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
225fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
226fa9e4066Sahrens  * exist by calling spa_lookup() first.
227fa9e4066Sahrens  */
228fa9e4066Sahrens spa_t *
2290373e76bSbonwick spa_add(const char *name, const char *altroot)
230fa9e4066Sahrens {
231fa9e4066Sahrens 	spa_t *spa;
232fa9e4066Sahrens 
233fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
234fa9e4066Sahrens 
235fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
236fa9e4066Sahrens 
237fa9e4066Sahrens 	spa->spa_name = spa_strdup(name);
238fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
239fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
2400373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
241fa9e4066Sahrens 
242fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
243ea8dc4b6Seschrock 	refcount_create(&spa->spa_config_lock.scl_count);
244fa9e4066Sahrens 
245fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
246fa9e4066Sahrens 
2470373e76bSbonwick 	/*
2480373e76bSbonwick 	 * Set the alternate root, if there is one.
2490373e76bSbonwick 	 */
2500373e76bSbonwick 	if (altroot) {
2510373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
2520373e76bSbonwick 		spa_active_count++;
2530373e76bSbonwick 	}
2540373e76bSbonwick 
255fa9e4066Sahrens 	return (spa);
256fa9e4066Sahrens }
257fa9e4066Sahrens 
258fa9e4066Sahrens /*
259fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
260fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
261fa9e4066Sahrens  * deactivated.
262fa9e4066Sahrens  */
263fa9e4066Sahrens void
264fa9e4066Sahrens spa_remove(spa_t *spa)
265fa9e4066Sahrens {
266fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
267fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
268fa9e4066Sahrens 	ASSERT(spa->spa_scrub_thread == NULL);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
271fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
272fa9e4066Sahrens 
2730373e76bSbonwick 	if (spa->spa_root) {
274fa9e4066Sahrens 		spa_strfree(spa->spa_root);
2750373e76bSbonwick 		spa_active_count--;
2760373e76bSbonwick 	}
277fa9e4066Sahrens 
278fa9e4066Sahrens 	if (spa->spa_name)
279fa9e4066Sahrens 		spa_strfree(spa->spa_name);
280fa9e4066Sahrens 
281fa9e4066Sahrens 	spa_config_set(spa, NULL);
282fa9e4066Sahrens 
283fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
284ea8dc4b6Seschrock 	refcount_destroy(&spa->spa_config_lock.scl_count);
285fa9e4066Sahrens 
2865ad82045Snd 	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
2875ad82045Snd 	mutex_destroy(&spa->spa_config_lock.scl_lock);
2885ad82045Snd 	mutex_destroy(&spa->spa_errlist_lock);
2895ad82045Snd 	mutex_destroy(&spa->spa_errlog_lock);
2905ad82045Snd 	mutex_destroy(&spa->spa_scrub_lock);
2915ad82045Snd 	mutex_destroy(&spa->spa_config_cache_lock);
2925ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
29306eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
2945ad82045Snd 
295fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
296fa9e4066Sahrens }
297fa9e4066Sahrens 
298fa9e4066Sahrens /*
299fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
300fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
301fa9e4066Sahrens  */
302fa9e4066Sahrens spa_t *
303fa9e4066Sahrens spa_next(spa_t *prev)
304fa9e4066Sahrens {
305fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
306fa9e4066Sahrens 
307fa9e4066Sahrens 	if (prev)
308fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
309fa9e4066Sahrens 	else
310fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
311fa9e4066Sahrens }
312fa9e4066Sahrens 
313fa9e4066Sahrens /*
314fa9e4066Sahrens  * ==========================================================================
315fa9e4066Sahrens  * SPA refcount functions
316fa9e4066Sahrens  * ==========================================================================
317fa9e4066Sahrens  */
318fa9e4066Sahrens 
319fa9e4066Sahrens /*
320fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
321fa9e4066Sahrens  * have the namespace lock held.
322fa9e4066Sahrens  */
323fa9e4066Sahrens void
324fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
325fa9e4066Sahrens {
326fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
327fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
328fa9e4066Sahrens 
329fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
330fa9e4066Sahrens }
331fa9e4066Sahrens 
332fa9e4066Sahrens /*
333fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
334fa9e4066Sahrens  * have the namespace lock held.
335fa9e4066Sahrens  */
336fa9e4066Sahrens void
337fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
338fa9e4066Sahrens {
339fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
340fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
341fa9e4066Sahrens 
342fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
343fa9e4066Sahrens }
344fa9e4066Sahrens 
345fa9e4066Sahrens /*
346fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
347fa9e4066Sahrens  * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
348fa9e4066Sahrens  * number of references acquired when opening a pool
349fa9e4066Sahrens  */
350fa9e4066Sahrens boolean_t
351fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
352fa9e4066Sahrens {
353fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
354fa9e4066Sahrens 
355fa9e4066Sahrens 	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
356fa9e4066Sahrens }
357fa9e4066Sahrens 
35899653d4eSeschrock /*
35999653d4eSeschrock  * ==========================================================================
36099653d4eSeschrock  * SPA spare tracking
36199653d4eSeschrock  * ==========================================================================
36299653d4eSeschrock  */
36399653d4eSeschrock 
36499653d4eSeschrock /*
36539c23413Seschrock  * Spares are tracked globally due to the following constraints:
36639c23413Seschrock  *
36739c23413Seschrock  * 	- A spare may be part of multiple pools.
36839c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
36939c23413Seschrock  *	  another pool.
37039c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
37139c23413Seschrock  *	  the target is a spare in the same pool.
37239c23413Seschrock  *
37339c23413Seschrock  * We keep track of all spares on the system through the use of a reference
37439c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
37539c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
37639c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
37739c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
37839c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
37939c23413Seschrock  *
38039c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
38139c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
38239c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
38339c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
38499653d4eSeschrock  */
38539c23413Seschrock 
38699653d4eSeschrock typedef struct spa_spare {
38799653d4eSeschrock 	uint64_t	spare_guid;
38839c23413Seschrock 	uint64_t	spare_pool;
38999653d4eSeschrock 	avl_node_t	spare_avl;
39099653d4eSeschrock 	int		spare_count;
39199653d4eSeschrock } spa_spare_t;
39299653d4eSeschrock 
39399653d4eSeschrock static int
39499653d4eSeschrock spa_spare_compare(const void *a, const void *b)
39599653d4eSeschrock {
39699653d4eSeschrock 	const spa_spare_t *sa = a;
39799653d4eSeschrock 	const spa_spare_t *sb = b;
39899653d4eSeschrock 
39999653d4eSeschrock 	if (sa->spare_guid < sb->spare_guid)
40099653d4eSeschrock 		return (-1);
40199653d4eSeschrock 	else if (sa->spare_guid > sb->spare_guid)
40299653d4eSeschrock 		return (1);
40399653d4eSeschrock 	else
40499653d4eSeschrock 		return (0);
40599653d4eSeschrock }
40699653d4eSeschrock 
40799653d4eSeschrock void
40839c23413Seschrock spa_spare_add(vdev_t *vd)
40999653d4eSeschrock {
41099653d4eSeschrock 	avl_index_t where;
41199653d4eSeschrock 	spa_spare_t search;
41299653d4eSeschrock 	spa_spare_t *spare;
41399653d4eSeschrock 
41499653d4eSeschrock 	mutex_enter(&spa_spare_lock);
41539c23413Seschrock 	ASSERT(!vd->vdev_isspare);
41699653d4eSeschrock 
41739c23413Seschrock 	search.spare_guid = vd->vdev_guid;
41899653d4eSeschrock 	if ((spare = avl_find(&spa_spare_avl, &search, &where)) != NULL) {
41999653d4eSeschrock 		spare->spare_count++;
42099653d4eSeschrock 	} else {
42139c23413Seschrock 		spare = kmem_zalloc(sizeof (spa_spare_t), KM_SLEEP);
42239c23413Seschrock 		spare->spare_guid = vd->vdev_guid;
42399653d4eSeschrock 		spare->spare_count = 1;
42499653d4eSeschrock 		avl_insert(&spa_spare_avl, spare, where);
42599653d4eSeschrock 	}
42639c23413Seschrock 	vd->vdev_isspare = B_TRUE;
42799653d4eSeschrock 
42899653d4eSeschrock 	mutex_exit(&spa_spare_lock);
42999653d4eSeschrock }
43099653d4eSeschrock 
43199653d4eSeschrock void
43239c23413Seschrock spa_spare_remove(vdev_t *vd)
43399653d4eSeschrock {
43499653d4eSeschrock 	spa_spare_t search;
43599653d4eSeschrock 	spa_spare_t *spare;
43699653d4eSeschrock 	avl_index_t where;
43799653d4eSeschrock 
43899653d4eSeschrock 	mutex_enter(&spa_spare_lock);
43999653d4eSeschrock 
44039c23413Seschrock 	search.spare_guid = vd->vdev_guid;
44199653d4eSeschrock 	spare = avl_find(&spa_spare_avl, &search, &where);
44299653d4eSeschrock 
44339c23413Seschrock 	ASSERT(vd->vdev_isspare);
44499653d4eSeschrock 	ASSERT(spare != NULL);
44599653d4eSeschrock 
44699653d4eSeschrock 	if (--spare->spare_count == 0) {
44799653d4eSeschrock 		avl_remove(&spa_spare_avl, spare);
44899653d4eSeschrock 		kmem_free(spare, sizeof (spa_spare_t));
44939c23413Seschrock 	} else if (spare->spare_pool == spa_guid(vd->vdev_spa)) {
45039c23413Seschrock 		spare->spare_pool = 0ULL;
45199653d4eSeschrock 	}
45299653d4eSeschrock 
45339c23413Seschrock 	vd->vdev_isspare = B_FALSE;
45499653d4eSeschrock 	mutex_exit(&spa_spare_lock);
45599653d4eSeschrock }
45699653d4eSeschrock 
45799653d4eSeschrock boolean_t
45839c23413Seschrock spa_spare_exists(uint64_t guid, uint64_t *pool)
45999653d4eSeschrock {
46039c23413Seschrock 	spa_spare_t search, *found;
46199653d4eSeschrock 	avl_index_t where;
46299653d4eSeschrock 
46399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
46499653d4eSeschrock 
46599653d4eSeschrock 	search.spare_guid = guid;
46639c23413Seschrock 	found = avl_find(&spa_spare_avl, &search, &where);
46739c23413Seschrock 
46839c23413Seschrock 	if (pool) {
46939c23413Seschrock 		if (found)
47039c23413Seschrock 			*pool = found->spare_pool;
47139c23413Seschrock 		else
47239c23413Seschrock 			*pool = 0ULL;
47339c23413Seschrock 	}
47499653d4eSeschrock 
47599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
47699653d4eSeschrock 
47739c23413Seschrock 	return (found != NULL);
47839c23413Seschrock }
47939c23413Seschrock 
48039c23413Seschrock void
48139c23413Seschrock spa_spare_activate(vdev_t *vd)
48239c23413Seschrock {
48339c23413Seschrock 	spa_spare_t search, *found;
48439c23413Seschrock 	avl_index_t where;
48539c23413Seschrock 
48639c23413Seschrock 	mutex_enter(&spa_spare_lock);
48739c23413Seschrock 	ASSERT(vd->vdev_isspare);
48839c23413Seschrock 
48939c23413Seschrock 	search.spare_guid = vd->vdev_guid;
49039c23413Seschrock 	found = avl_find(&spa_spare_avl, &search, &where);
49139c23413Seschrock 	ASSERT(found != NULL);
49239c23413Seschrock 	ASSERT(found->spare_pool == 0ULL);
49339c23413Seschrock 
49439c23413Seschrock 	found->spare_pool = spa_guid(vd->vdev_spa);
49539c23413Seschrock 	mutex_exit(&spa_spare_lock);
49699653d4eSeschrock }
49799653d4eSeschrock 
498fa9e4066Sahrens /*
499fa9e4066Sahrens  * ==========================================================================
500fa9e4066Sahrens  * SPA config locking
501fa9e4066Sahrens  * ==========================================================================
502fa9e4066Sahrens  */
503fa9e4066Sahrens 
504fa9e4066Sahrens /*
505fa9e4066Sahrens  * Acquire the config lock.  The config lock is a special rwlock that allows for
506fa9e4066Sahrens  * recursive enters.  Because these enters come from the same thread as well as
507fa9e4066Sahrens  * asynchronous threads working on behalf of the owner, we must unilaterally
508fa9e4066Sahrens  * allow all reads access as long at least one reader is held (even if a write
509fa9e4066Sahrens  * is requested).  This has the side effect of write starvation, but write locks
510fa9e4066Sahrens  * are extremely rare, and a solution to this problem would be significantly
511fa9e4066Sahrens  * more complex (if even possible).
512fa9e4066Sahrens  *
513fa9e4066Sahrens  * We would like to assert that the namespace lock isn't held, but this is a
514fa9e4066Sahrens  * valid use during create.
515fa9e4066Sahrens  */
516fa9e4066Sahrens void
517ea8dc4b6Seschrock spa_config_enter(spa_t *spa, krw_t rw, void *tag)
518fa9e4066Sahrens {
519fa9e4066Sahrens 	spa_config_lock_t *scl = &spa->spa_config_lock;
520fa9e4066Sahrens 
521fa9e4066Sahrens 	mutex_enter(&scl->scl_lock);
522fa9e4066Sahrens 
523fa9e4066Sahrens 	if (scl->scl_writer != curthread) {
524fa9e4066Sahrens 		if (rw == RW_READER) {
525fa9e4066Sahrens 			while (scl->scl_writer != NULL)
526fa9e4066Sahrens 				cv_wait(&scl->scl_cv, &scl->scl_lock);
527fa9e4066Sahrens 		} else {
528ea8dc4b6Seschrock 			while (scl->scl_writer != NULL ||
529ea8dc4b6Seschrock 			    !refcount_is_zero(&scl->scl_count))
530fa9e4066Sahrens 				cv_wait(&scl->scl_cv, &scl->scl_lock);
531fa9e4066Sahrens 			scl->scl_writer = curthread;
532fa9e4066Sahrens 		}
533fa9e4066Sahrens 	}
534fa9e4066Sahrens 
535ea8dc4b6Seschrock 	(void) refcount_add(&scl->scl_count, tag);
536fa9e4066Sahrens 
537fa9e4066Sahrens 	mutex_exit(&scl->scl_lock);
538fa9e4066Sahrens }
539fa9e4066Sahrens 
540fa9e4066Sahrens /*
541fa9e4066Sahrens  * Release the spa config lock, notifying any waiters in the process.
542fa9e4066Sahrens  */
543fa9e4066Sahrens void
544ea8dc4b6Seschrock spa_config_exit(spa_t *spa, void *tag)
545fa9e4066Sahrens {
546fa9e4066Sahrens 	spa_config_lock_t *scl = &spa->spa_config_lock;
547fa9e4066Sahrens 
548fa9e4066Sahrens 	mutex_enter(&scl->scl_lock);
549fa9e4066Sahrens 
550ea8dc4b6Seschrock 	ASSERT(!refcount_is_zero(&scl->scl_count));
551ea8dc4b6Seschrock 	if (refcount_remove(&scl->scl_count, tag) == 0) {
552fa9e4066Sahrens 		cv_broadcast(&scl->scl_cv);
553fa9e4066Sahrens 		scl->scl_writer = NULL;  /* OK in either case */
554fa9e4066Sahrens 	}
555fa9e4066Sahrens 
556fa9e4066Sahrens 	mutex_exit(&scl->scl_lock);
557fa9e4066Sahrens }
558fa9e4066Sahrens 
559fa9e4066Sahrens /*
560fa9e4066Sahrens  * Returns true if the config lock is held in the given manner.
561fa9e4066Sahrens  */
562fa9e4066Sahrens boolean_t
563fa9e4066Sahrens spa_config_held(spa_t *spa, krw_t rw)
564fa9e4066Sahrens {
565fa9e4066Sahrens 	spa_config_lock_t *scl = &spa->spa_config_lock;
566fa9e4066Sahrens 	boolean_t held;
567fa9e4066Sahrens 
568fa9e4066Sahrens 	mutex_enter(&scl->scl_lock);
569fa9e4066Sahrens 	if (rw == RW_WRITER)
570fa9e4066Sahrens 		held = (scl->scl_writer == curthread);
571fa9e4066Sahrens 	else
572ea8dc4b6Seschrock 		held = !refcount_is_zero(&scl->scl_count);
573fa9e4066Sahrens 	mutex_exit(&scl->scl_lock);
574fa9e4066Sahrens 
575fa9e4066Sahrens 	return (held);
576fa9e4066Sahrens }
577fa9e4066Sahrens 
578fa9e4066Sahrens /*
579fa9e4066Sahrens  * ==========================================================================
580fa9e4066Sahrens  * SPA vdev locking
581fa9e4066Sahrens  * ==========================================================================
582fa9e4066Sahrens  */
583fa9e4066Sahrens 
584fa9e4066Sahrens /*
585ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
586ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
587fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
588fa9e4066Sahrens  */
589fa9e4066Sahrens uint64_t
590fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
591fa9e4066Sahrens {
592ea8dc4b6Seschrock 	/*
593ea8dc4b6Seschrock 	 * Suspend scrub activity while we mess with the config.
594ea8dc4b6Seschrock 	 */
595ea8dc4b6Seschrock 	spa_scrub_suspend(spa);
596fa9e4066Sahrens 
5970373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
598ea8dc4b6Seschrock 
599ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, spa);
600fa9e4066Sahrens 
601fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
602fa9e4066Sahrens }
603fa9e4066Sahrens 
604fa9e4066Sahrens /*
605fa9e4066Sahrens  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
606fa9e4066Sahrens  * locking of spa_vdev_enter(), we also want make sure the transactions have
607fa9e4066Sahrens  * synced to disk, and then update the global configuration cache with the new
608fa9e4066Sahrens  * information.
609fa9e4066Sahrens  */
610fa9e4066Sahrens int
611fa9e4066Sahrens spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
612fa9e4066Sahrens {
6130e34b6a7Sbonwick 	int config_changed = B_FALSE;
614ea8dc4b6Seschrock 
6150373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
6160e34b6a7Sbonwick 
6170e34b6a7Sbonwick 	/*
6180e34b6a7Sbonwick 	 * Reassess the DTLs.
6190e34b6a7Sbonwick 	 */
6200373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
6210e34b6a7Sbonwick 
6220e34b6a7Sbonwick 	/*
6230373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
6240e34b6a7Sbonwick 	 */
6250e34b6a7Sbonwick 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
6260e34b6a7Sbonwick 		config_changed = B_TRUE;
6270373e76bSbonwick 		spa_scrub_restart(spa, txg);
6280e34b6a7Sbonwick 	}
629ea8dc4b6Seschrock 
630ea8dc4b6Seschrock 	spa_config_exit(spa, spa);
631fa9e4066Sahrens 
632ea8dc4b6Seschrock 	/*
6335dabedeeSbonwick 	 * Allow scrubbing to resume.
634ea8dc4b6Seschrock 	 */
635ea8dc4b6Seschrock 	spa_scrub_resume(spa);
636fa9e4066Sahrens 
637fa9e4066Sahrens 	/*
638fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
639fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
640fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
641fa9e4066Sahrens 	 */
642fa9e4066Sahrens 	if (error == 0)
643fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
644fa9e4066Sahrens 
645fa9e4066Sahrens 	if (vd != NULL) {
646fa9e4066Sahrens 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
647fa9e4066Sahrens 		vdev_free(vd);
648fa9e4066Sahrens 	}
649fa9e4066Sahrens 
650fa9e4066Sahrens 	/*
6510e34b6a7Sbonwick 	 * If the config changed, update the config cache.
652fa9e4066Sahrens 	 */
6530e34b6a7Sbonwick 	if (config_changed)
654fa9e4066Sahrens 		spa_config_sync();
655ea8dc4b6Seschrock 
656ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
657fa9e4066Sahrens 
658fa9e4066Sahrens 	return (error);
659fa9e4066Sahrens }
660fa9e4066Sahrens 
661fa9e4066Sahrens /*
662fa9e4066Sahrens  * ==========================================================================
663fa9e4066Sahrens  * Miscellaneous functions
664fa9e4066Sahrens  * ==========================================================================
665fa9e4066Sahrens  */
666fa9e4066Sahrens 
667fa9e4066Sahrens /*
668fa9e4066Sahrens  * Rename a spa_t.
669fa9e4066Sahrens  */
670fa9e4066Sahrens int
671fa9e4066Sahrens spa_rename(const char *name, const char *newname)
672fa9e4066Sahrens {
673fa9e4066Sahrens 	spa_t *spa;
674fa9e4066Sahrens 	int err;
675fa9e4066Sahrens 
676fa9e4066Sahrens 	/*
677fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
678fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
679fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
680ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
681fa9e4066Sahrens 	 */
682fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
683fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
684fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
685fa9e4066Sahrens 		return (err);
686fa9e4066Sahrens 	}
687fa9e4066Sahrens 
688ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
689fa9e4066Sahrens 
690fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
691fa9e4066Sahrens 	spa_strfree(spa->spa_name);
692fa9e4066Sahrens 	spa->spa_name = spa_strdup(newname);
693fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
694fa9e4066Sahrens 
695fa9e4066Sahrens 	/*
696fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
697fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
698fa9e4066Sahrens 	 * during the sync.
699fa9e4066Sahrens 	 */
700fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
701fa9e4066Sahrens 
702ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
703fa9e4066Sahrens 
7040373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
705fa9e4066Sahrens 
706fa9e4066Sahrens 	/*
707fa9e4066Sahrens 	 * Sync the updated config cache.
708fa9e4066Sahrens 	 */
709fa9e4066Sahrens 	spa_config_sync();
710fa9e4066Sahrens 
711fa9e4066Sahrens 	spa_close(spa, FTAG);
712fa9e4066Sahrens 
713fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
714fa9e4066Sahrens 
715fa9e4066Sahrens 	return (0);
716fa9e4066Sahrens }
717fa9e4066Sahrens 
718fa9e4066Sahrens 
719fa9e4066Sahrens /*
720fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
721fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
722fa9e4066Sahrens  * specified device_guid.
723fa9e4066Sahrens  */
724fa9e4066Sahrens boolean_t
725fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
726fa9e4066Sahrens {
727fa9e4066Sahrens 	spa_t *spa;
728fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
729fa9e4066Sahrens 
730ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
731fa9e4066Sahrens 
732fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
733fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
734fa9e4066Sahrens 			continue;
735fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
736fa9e4066Sahrens 			continue;
73739c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
73839c23413Seschrock 			if (device_guid == 0)
73939c23413Seschrock 				break;
74039c23413Seschrock 
74139c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
74239c23413Seschrock 			    device_guid) != NULL)
74339c23413Seschrock 				break;
74439c23413Seschrock 
74539c23413Seschrock 			/*
74639c23413Seschrock 			 * Check any devices we may in the process of adding.
74739c23413Seschrock 			 */
74839c23413Seschrock 			if (spa->spa_pending_vdev) {
74939c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
75039c23413Seschrock 				    device_guid) != NULL)
75139c23413Seschrock 					break;
75239c23413Seschrock 			}
75339c23413Seschrock 		}
754fa9e4066Sahrens 	}
755fa9e4066Sahrens 
756fa9e4066Sahrens 	return (spa != NULL);
757fa9e4066Sahrens }
758fa9e4066Sahrens 
759fa9e4066Sahrens char *
760fa9e4066Sahrens spa_strdup(const char *s)
761fa9e4066Sahrens {
762fa9e4066Sahrens 	size_t len;
763fa9e4066Sahrens 	char *new;
764fa9e4066Sahrens 
765fa9e4066Sahrens 	len = strlen(s);
766fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
767fa9e4066Sahrens 	bcopy(s, new, len);
768fa9e4066Sahrens 	new[len] = '\0';
769fa9e4066Sahrens 
770fa9e4066Sahrens 	return (new);
771fa9e4066Sahrens }
772fa9e4066Sahrens 
773fa9e4066Sahrens void
774fa9e4066Sahrens spa_strfree(char *s)
775fa9e4066Sahrens {
776fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
777fa9e4066Sahrens }
778fa9e4066Sahrens 
779fa9e4066Sahrens uint64_t
780fa9e4066Sahrens spa_get_random(uint64_t range)
781fa9e4066Sahrens {
782fa9e4066Sahrens 	uint64_t r;
783fa9e4066Sahrens 
784fa9e4066Sahrens 	ASSERT(range != 0);
785fa9e4066Sahrens 
786fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
787fa9e4066Sahrens 
788fa9e4066Sahrens 	return (r % range);
789fa9e4066Sahrens }
790fa9e4066Sahrens 
791fa9e4066Sahrens void
792d80c45e0Sbonwick sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
793fa9e4066Sahrens {
79444cd46caSbillm 	int d;
795fa9e4066Sahrens 
796fa9e4066Sahrens 	if (bp == NULL) {
797fbabab8fSmaybee 		(void) snprintf(buf, len, "<NULL>");
798fa9e4066Sahrens 		return;
799fa9e4066Sahrens 	}
800fa9e4066Sahrens 
801fa9e4066Sahrens 	if (BP_IS_HOLE(bp)) {
802fbabab8fSmaybee 		(void) snprintf(buf, len, "<hole>");
803fa9e4066Sahrens 		return;
804fa9e4066Sahrens 	}
805fa9e4066Sahrens 
80644cd46caSbillm 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
807fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LEVEL(bp),
808fa9e4066Sahrens 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
809fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LSIZE(bp),
81044cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp));
81144cd46caSbillm 
81244cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
813d80c45e0Sbonwick 		const dva_t *dva = &bp->blk_dva[d];
81444cd46caSbillm 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
81544cd46caSbillm 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
81644cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(dva),
81744cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(dva),
81844cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(dva));
81944cd46caSbillm 	}
82044cd46caSbillm 
82144cd46caSbillm 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
82244cd46caSbillm 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
823fa9e4066Sahrens 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
824fa9e4066Sahrens 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
825fa9e4066Sahrens 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
82644cd46caSbillm 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
827fa9e4066Sahrens 	    (u_longlong_t)bp->blk_birth,
828fa9e4066Sahrens 	    (u_longlong_t)bp->blk_fill,
829fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
830fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
831fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
832fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
833fa9e4066Sahrens }
834fa9e4066Sahrens 
835fa9e4066Sahrens void
836fa9e4066Sahrens spa_freeze(spa_t *spa)
837fa9e4066Sahrens {
838fa9e4066Sahrens 	uint64_t freeze_txg = 0;
839fa9e4066Sahrens 
840ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
841fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
842fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
843fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
844fa9e4066Sahrens 	}
845ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
846fa9e4066Sahrens 	if (freeze_txg != 0)
847fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
848fa9e4066Sahrens }
849fa9e4066Sahrens 
850*0125049cSahrens void
851*0125049cSahrens zfs_panic_recover(const char *fmt, ...)
852*0125049cSahrens {
853*0125049cSahrens 	va_list adx;
854*0125049cSahrens 
855*0125049cSahrens 	va_start(adx, fmt);
856*0125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
857*0125049cSahrens 	va_end(adx);
858*0125049cSahrens }
859*0125049cSahrens 
860fa9e4066Sahrens /*
861fa9e4066Sahrens  * ==========================================================================
862fa9e4066Sahrens  * Accessor functions
863fa9e4066Sahrens  * ==========================================================================
864fa9e4066Sahrens  */
865fa9e4066Sahrens 
866fa9e4066Sahrens krwlock_t *
867fa9e4066Sahrens spa_traverse_rwlock(spa_t *spa)
868fa9e4066Sahrens {
869fa9e4066Sahrens 	return (&spa->spa_traverse_lock);
870fa9e4066Sahrens }
871fa9e4066Sahrens 
872fa9e4066Sahrens int
873fa9e4066Sahrens spa_traverse_wanted(spa_t *spa)
874fa9e4066Sahrens {
875fa9e4066Sahrens 	return (spa->spa_traverse_wanted);
876fa9e4066Sahrens }
877fa9e4066Sahrens 
878fa9e4066Sahrens dsl_pool_t *
879fa9e4066Sahrens spa_get_dsl(spa_t *spa)
880fa9e4066Sahrens {
881fa9e4066Sahrens 	return (spa->spa_dsl_pool);
882fa9e4066Sahrens }
883fa9e4066Sahrens 
884fa9e4066Sahrens blkptr_t *
885fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
886fa9e4066Sahrens {
887fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
888fa9e4066Sahrens }
889fa9e4066Sahrens 
890fa9e4066Sahrens void
891fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
892fa9e4066Sahrens {
893fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
894fa9e4066Sahrens }
895fa9e4066Sahrens 
896fa9e4066Sahrens void
897fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
898fa9e4066Sahrens {
899fa9e4066Sahrens 	if (spa->spa_root == NULL)
900fa9e4066Sahrens 		buf[0] = '\0';
901fa9e4066Sahrens 	else
902fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
903fa9e4066Sahrens }
904fa9e4066Sahrens 
905fa9e4066Sahrens int
906fa9e4066Sahrens spa_sync_pass(spa_t *spa)
907fa9e4066Sahrens {
908fa9e4066Sahrens 	return (spa->spa_sync_pass);
909fa9e4066Sahrens }
910fa9e4066Sahrens 
911fa9e4066Sahrens char *
912fa9e4066Sahrens spa_name(spa_t *spa)
913fa9e4066Sahrens {
914fa9e4066Sahrens 	/*
915fa9e4066Sahrens 	 * Accessing the name requires holding either the namespace lock or the
916fa9e4066Sahrens 	 * config lock, both of which are required to do a rename.
917fa9e4066Sahrens 	 */
918fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
919fa9e4066Sahrens 	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
920fa9e4066Sahrens 
921fa9e4066Sahrens 	return (spa->spa_name);
922fa9e4066Sahrens }
923fa9e4066Sahrens 
924fa9e4066Sahrens uint64_t
925fa9e4066Sahrens spa_guid(spa_t *spa)
926fa9e4066Sahrens {
927b5989ec7Seschrock 	/*
928b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
929b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
930b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
931b5989ec7Seschrock 	 * this case.
932b5989ec7Seschrock 	 */
933b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
934b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
935b5989ec7Seschrock 	else
936b5989ec7Seschrock 		return (spa->spa_load_guid);
937fa9e4066Sahrens }
938fa9e4066Sahrens 
939fa9e4066Sahrens uint64_t
940fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
941fa9e4066Sahrens {
942fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
943fa9e4066Sahrens }
944fa9e4066Sahrens 
945fa9e4066Sahrens uint64_t
946fa9e4066Sahrens spa_first_txg(spa_t *spa)
947fa9e4066Sahrens {
948fa9e4066Sahrens 	return (spa->spa_first_txg);
949fa9e4066Sahrens }
950fa9e4066Sahrens 
951fa9e4066Sahrens int
952fa9e4066Sahrens spa_state(spa_t *spa)
953fa9e4066Sahrens {
954fa9e4066Sahrens 	return (spa->spa_state);
955fa9e4066Sahrens }
956fa9e4066Sahrens 
957fa9e4066Sahrens uint64_t
958fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
959fa9e4066Sahrens {
960fa9e4066Sahrens 	return (spa->spa_freeze_txg);
961fa9e4066Sahrens }
962fa9e4066Sahrens 
963fa9e4066Sahrens /*
964fa9e4066Sahrens  * In the future, this may select among different metaslab classes
965fa9e4066Sahrens  * depending on the zdp.  For now, there's no such distinction.
966fa9e4066Sahrens  */
967fa9e4066Sahrens metaslab_class_t *
968fa9e4066Sahrens spa_metaslab_class_select(spa_t *spa)
969fa9e4066Sahrens {
970fa9e4066Sahrens 	return (spa->spa_normal_class);
971fa9e4066Sahrens }
972fa9e4066Sahrens 
973fa9e4066Sahrens /*
97499653d4eSeschrock  * Return how much space is allocated in the pool (ie. sum of all asize)
975fa9e4066Sahrens  */
976fa9e4066Sahrens uint64_t
977fa9e4066Sahrens spa_get_alloc(spa_t *spa)
978fa9e4066Sahrens {
979fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
980fa9e4066Sahrens }
981fa9e4066Sahrens 
982fa9e4066Sahrens /*
98399653d4eSeschrock  * Return how much (raid-z inflated) space there is in the pool.
984fa9e4066Sahrens  */
985fa9e4066Sahrens uint64_t
986fa9e4066Sahrens spa_get_space(spa_t *spa)
987fa9e4066Sahrens {
988fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_space);
989fa9e4066Sahrens }
990fa9e4066Sahrens 
99199653d4eSeschrock /*
99299653d4eSeschrock  * Return the amount of raid-z-deflated space in the pool.
99399653d4eSeschrock  */
99499653d4eSeschrock uint64_t
99599653d4eSeschrock spa_get_dspace(spa_t *spa)
99699653d4eSeschrock {
99799653d4eSeschrock 	if (spa->spa_deflate)
99899653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
99999653d4eSeschrock 	else
100099653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_space);
100199653d4eSeschrock }
100299653d4eSeschrock 
1003fa9e4066Sahrens /* ARGSUSED */
1004fa9e4066Sahrens uint64_t
1005fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1006fa9e4066Sahrens {
1007fa9e4066Sahrens 	/*
1008fa9e4066Sahrens 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
1009fa9e4066Sahrens 	 * case the space requirement is exactly 2x; so just assume that.
101044cd46caSbillm 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
101144cd46caSbillm 	 * we have to multiply by a total of 6x.
101244cd46caSbillm 	 */
101344cd46caSbillm 	return (lsize * 6);
101444cd46caSbillm }
101544cd46caSbillm 
101644cd46caSbillm uint64_t
101744cd46caSbillm spa_version(spa_t *spa)
101844cd46caSbillm {
101944cd46caSbillm 	return (spa->spa_ubsync.ub_version);
102044cd46caSbillm }
102144cd46caSbillm 
102244cd46caSbillm int
102344cd46caSbillm spa_max_replication(spa_t *spa)
102444cd46caSbillm {
102544cd46caSbillm 	/*
102644cd46caSbillm 	 * As of ZFS_VERSION == ZFS_VERSION_DITTO_BLOCKS, we are able to
102744cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
102844cd46caSbillm 	 * replication level accordingly.
1029fa9e4066Sahrens 	 */
103044cd46caSbillm 	if (spa_version(spa) < ZFS_VERSION_DITTO_BLOCKS)
103144cd46caSbillm 		return (1);
103244cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1033fa9e4066Sahrens }
1034fa9e4066Sahrens 
103599653d4eSeschrock uint64_t
103699653d4eSeschrock bp_get_dasize(spa_t *spa, const blkptr_t *bp)
103799653d4eSeschrock {
103899653d4eSeschrock 	int sz = 0, i;
103999653d4eSeschrock 
104099653d4eSeschrock 	if (!spa->spa_deflate)
104199653d4eSeschrock 		return (BP_GET_ASIZE(bp));
104299653d4eSeschrock 
104399653d4eSeschrock 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
104499653d4eSeschrock 		vdev_t *vd =
104599653d4eSeschrock 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
104699653d4eSeschrock 		sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >> SPA_MINBLOCKSHIFT) *
104799653d4eSeschrock 		    vd->vdev_deflate_ratio;
104899653d4eSeschrock 	}
104999653d4eSeschrock 	return (sz);
105099653d4eSeschrock }
105199653d4eSeschrock 
1052fa9e4066Sahrens /*
1053fa9e4066Sahrens  * ==========================================================================
1054fa9e4066Sahrens  * Initialization and Termination
1055fa9e4066Sahrens  * ==========================================================================
1056fa9e4066Sahrens  */
1057fa9e4066Sahrens 
1058fa9e4066Sahrens static int
1059fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1060fa9e4066Sahrens {
1061fa9e4066Sahrens 	const spa_t *s1 = a1;
1062fa9e4066Sahrens 	const spa_t *s2 = a2;
1063fa9e4066Sahrens 	int s;
1064fa9e4066Sahrens 
1065fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1066fa9e4066Sahrens 	if (s > 0)
1067fa9e4066Sahrens 		return (1);
1068fa9e4066Sahrens 	if (s < 0)
1069fa9e4066Sahrens 		return (-1);
1070fa9e4066Sahrens 	return (0);
1071fa9e4066Sahrens }
1072fa9e4066Sahrens 
10730373e76bSbonwick int
10740373e76bSbonwick spa_busy(void)
10750373e76bSbonwick {
10760373e76bSbonwick 	return (spa_active_count);
10770373e76bSbonwick }
10780373e76bSbonwick 
1079fa9e4066Sahrens void
1080fa9e4066Sahrens spa_init(int mode)
1081fa9e4066Sahrens {
1082fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1083fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1086fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1087fa9e4066Sahrens 
108899653d4eSeschrock 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_spare_t),
108999653d4eSeschrock 	    offsetof(spa_spare_t, spare_avl));
109099653d4eSeschrock 
1091fa9e4066Sahrens 	spa_mode = mode;
1092fa9e4066Sahrens 
1093fa9e4066Sahrens 	refcount_init();
1094fa9e4066Sahrens 	unique_init();
1095fa9e4066Sahrens 	zio_init();
1096fa9e4066Sahrens 	dmu_init();
1097fa9e4066Sahrens 	zil_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();
1109fa9e4066Sahrens 	refcount_fini();
1110fa9e4066Sahrens 
1111fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
111299653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1113fa9e4066Sahrens 
1114fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1115fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1116fa9e4066Sahrens }
1117