spa_misc.c revision 91ebeef555ce7f899b6270a3c2df47b51f7ad59a
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
2239c2341eschrock * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens#pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e406ahrens
28fa9e406ahrens#include <sys/zfs_context.h>
29fa9e406ahrens#include <sys/spa_impl.h>
30fa9e406ahrens#include <sys/zio.h>
31fa9e406ahrens#include <sys/zio_checksum.h>
32fa9e406ahrens#include <sys/zio_compress.h>
33fa9e406ahrens#include <sys/dmu.h>
34fa9e406ahrens#include <sys/dmu_tx.h>
35fa9e406ahrens#include <sys/zap.h>
36fa9e406ahrens#include <sys/zil.h>
37fa9e406ahrens#include <sys/vdev_impl.h>
38fa9e406ahrens#include <sys/metaslab.h>
39fa9e406ahrens#include <sys/uberblock_impl.h>
40fa9e406ahrens#include <sys/txg.h>
41fa9e406ahrens#include <sys/avl.h>
42fa9e406ahrens#include <sys/unique.h>
43fa9e406ahrens#include <sys/dsl_pool.h>
44fa9e406ahrens#include <sys/dsl_dir.h>
45fa9e406ahrens#include <sys/dsl_prop.h>
46fa9e406ahrens#include <sys/fs/zfs.h>
476ce0521perrin#include <sys/metaslab_impl.h>
4891ebeefahrens#include "zfs_prop.h"
49fa9e406ahrens
50fa9e406ahrens/*
51fa9e406ahrens * SPA locking
52fa9e406ahrens *
53fa9e406ahrens * There are four basic locks for managing spa_t structures:
54fa9e406ahrens *
55fa9e406ahrens * spa_namespace_lock (global mutex)
56fa9e406ahrens *
5744cd46cbillm *	This lock must be acquired to do any of the following:
58fa9e406ahrens *
5944cd46cbillm *		- Lookup a spa_t by name
6044cd46cbillm *		- Add or remove a spa_t from the namespace
6144cd46cbillm *		- Increase spa_refcount from non-zero
6244cd46cbillm *		- Check if spa_refcount is zero
6344cd46cbillm *		- Rename a spa_t
64ea8dc4beschrock *		- add/remove/attach/detach devices
6544cd46cbillm *		- Held for the duration of create/destroy/import/export
66fa9e406ahrens *
6744cd46cbillm *	It does not need to handle recursion.  A create or destroy may
6844cd46cbillm *	reference objects (files or zvols) in other pools, but by
6944cd46cbillm *	definition they must have an existing reference, and will never need
7044cd46cbillm *	to lookup a spa_t by name.
71fa9e406ahrens *
72fa9e406ahrens * spa_refcount (per-spa refcount_t protected by mutex)
73fa9e406ahrens *
7444cd46cbillm *	This reference count keep track of any active users of the spa_t.  The
7544cd46cbillm *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7644cd46cbillm *	the refcount is never really 'zero' - opening a pool implicitly keeps
7744cd46cbillm *	some references in the DMU.  Internally we check against SPA_MINREF, but
7844cd46cbillm *	present the image of a zero/non-zero value to consumers.
79fa9e406ahrens *
8091ebeefahrens * spa_config_lock (per-spa read-priority rwlock)
81fa9e406ahrens *
8291ebeefahrens *	This protects the spa_t from config changes, and must be held in
8391ebeefahrens *	the following circumstances:
84fa9e406ahrens *
8544cd46cbillm *		- RW_READER to perform I/O to the spa
8644cd46cbillm *		- RW_WRITER to change the vdev config
87fa9e406ahrens *
88fa9e406ahrens * spa_config_cache_lock (per-spa mutex)
89fa9e406ahrens *
9044cd46cbillm *	This mutex prevents the spa_config nvlist from being updated.  No
91fa9e406ahrens *      other locks are required to obtain this lock, although implicitly you
92fa9e406ahrens *      must have the namespace lock or non-zero refcount to have any kind
93fa9e406ahrens *      of spa_t pointer at all.
94fa9e406ahrens *
95fa9e406ahrens * The locking order is fairly straightforward:
96fa9e406ahrens *
9744cd46cbillm *		spa_namespace_lock	->	spa_refcount
98fa9e406ahrens *
9944cd46cbillm *	The namespace lock must be acquired to increase the refcount from 0
10044cd46cbillm *	or to check if it is zero.
101fa9e406ahrens *
10244cd46cbillm *		spa_refcount		->	spa_config_lock
103fa9e406ahrens *
10444cd46cbillm *	There must be at least one valid reference on the spa_t to acquire
10544cd46cbillm *	the config lock.
106fa9e406ahrens *
10744cd46cbillm *		spa_namespace_lock	->	spa_config_lock
108fa9e406ahrens *
10944cd46cbillm *	The namespace lock must always be taken before the config lock.
110fa9e406ahrens *
111fa9e406ahrens *
112fa9e406ahrens * The spa_namespace_lock and spa_config_cache_lock can be acquired directly and
113fa9e406ahrens * are globally visible.
114fa9e406ahrens *
115fa9e406ahrens * The namespace is manipulated using the following functions, all which require
116fa9e406ahrens * the spa_namespace_lock to be held.
117fa9e406ahrens *
11844cd46cbillm *	spa_lookup()		Lookup a spa_t by name.
119fa9e406ahrens *
12044cd46cbillm *	spa_add()		Create a new spa_t in the namespace.
121fa9e406ahrens *
12244cd46cbillm *	spa_remove()		Remove a spa_t from the namespace.  This also
12344cd46cbillm *				frees up any memory associated with the spa_t.
124fa9e406ahrens *
12544cd46cbillm *	spa_next()		Returns the next spa_t in the system, or the
12644cd46cbillm *				first if NULL is passed.
127fa9e406ahrens *
12844cd46cbillm *	spa_evict_all()		Shutdown and remove all spa_t structures in
12944cd46cbillm *				the system.
130fa9e406ahrens *
131ea8dc4beschrock *	spa_guid_exists()	Determine whether a pool/device guid exists.
132fa9e406ahrens *
133fa9e406ahrens * The spa_refcount is manipulated using the following functions:
134fa9e406ahrens *
13544cd46cbillm *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13644cd46cbillm *				called with spa_namespace_lock held if the
13744cd46cbillm *				refcount is currently zero.
138fa9e406ahrens *
13944cd46cbillm *	spa_close()		Remove a reference from the spa_t.  This will
14044cd46cbillm *				not free the spa_t or remove it from the
14144cd46cbillm *				namespace.  No locking is required.
142fa9e406ahrens *
14344cd46cbillm *	spa_refcount_zero()	Returns true if the refcount is currently
14444cd46cbillm *				zero.  Must be called with spa_namespace_lock
14544cd46cbillm *				held.
146fa9e406ahrens *
147fa9e406ahrens * The spa_config_lock is manipulated using the following functions:
148fa9e406ahrens *
14944cd46cbillm *	spa_config_enter()	Acquire the config lock as RW_READER or
15044cd46cbillm *				RW_WRITER.  At least one reference on the spa_t
15144cd46cbillm *				must exist.
152fa9e406ahrens *
15344cd46cbillm *	spa_config_exit()	Release the config lock.
154fa9e406ahrens *
15544cd46cbillm *	spa_config_held()	Returns true if the config lock is currently
15644cd46cbillm *				held in the given state.
157fa9e406ahrens *
158ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
159fa9e406ahrens *
16044cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
161ea8dc4beschrock *				for writing.
162fa9e406ahrens *
16344cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
16444cd46cbillm *				to complete, sync the updated configs to the
165ea8dc4beschrock *				cache, and release the namespace lock.
166fa9e406ahrens *
167fa9e406ahrens * The spa_name() function also requires either the spa_namespace_lock
168fa9e406ahrens * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
169fa9e406ahrens * also implemented within this file since is requires manipulation of the
170fa9e406ahrens * namespace.
171fa9e406ahrens */
172fa9e406ahrens
173fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
174fa9e406ahrenskmutex_t spa_namespace_lock;
175fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
1760373e76bonwickstatic int spa_active_count;
177416e0cdekint spa_max_replication_override = SPA_DVAS_PER_BP;
178fa9e406ahrens
17999653d4eschrockstatic kmutex_t spa_spare_lock;
18039c2341eschrockstatic avl_tree_t spa_spare_avl;
18199653d4eschrock
182fa9e406ahrenskmem_cache_t *spa_buffer_pool;
183fa9e406ahrensint spa_mode;
184fa9e406ahrens
185fa9e406ahrens#ifdef ZFS_DEBUG
18640feaa9ahrens/* Everything except dprintf is on by default in debug builds */
18740feaa9ahrensint zfs_flags = ~ZFS_DEBUG_DPRINTF;
188fa9e406ahrens#else
189fa9e406ahrensint zfs_flags = 0;
190fa9e406ahrens#endif
191fa9e406ahrens
1920125049ahrens/*
1930125049ahrens * zfs_recover can be set to nonzero to attempt to recover from
1940125049ahrens * otherwise-fatal errors, typically caused by on-disk corruption.  When
1950125049ahrens * set, calls to zfs_panic_recover() will turn into warning messages.
1960125049ahrens */
1970125049ahrensint zfs_recover = 0;
1980125049ahrens
199fa9e406ahrens#define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
200fa9e406ahrens
201fa9e406ahrens/*
202fa9e406ahrens * ==========================================================================
203fa9e406ahrens * SPA namespace functions
204fa9e406ahrens * ==========================================================================
205fa9e406ahrens */
206fa9e406ahrens
207fa9e406ahrens/*
208fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
209fa9e406ahrens * Returns NULL if no matching spa_t is found.
210fa9e406ahrens */
211fa9e406ahrensspa_t *
212fa9e406ahrensspa_lookup(const char *name)
213fa9e406ahrens{
214fa9e406ahrens	spa_t search, *spa;
215fa9e406ahrens	avl_index_t where;
21640feaa9ahrens	char c;
21740feaa9ahrens	char *cp;
218fa9e406ahrens
219fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
220fa9e406ahrens
22140feaa9ahrens	/*
22240feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
22340feaa9ahrens	 * just use that.
22440feaa9ahrens	 */
22540feaa9ahrens	cp = strpbrk(name, "/@");
22640feaa9ahrens	if (cp) {
22740feaa9ahrens		c = *cp;
22840feaa9ahrens		*cp = '\0';
22940feaa9ahrens	}
23040feaa9ahrens
231fa9e406ahrens	search.spa_name = (char *)name;
232fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
233fa9e406ahrens
23440feaa9ahrens	if (cp)
23540feaa9ahrens		*cp = c;
23640feaa9ahrens
237fa9e406ahrens	return (spa);
238fa9e406ahrens}
239fa9e406ahrens
240fa9e406ahrens/*
241fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
242fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
243fa9e406ahrens * exist by calling spa_lookup() first.
244fa9e406ahrens */
245fa9e406ahrensspa_t *
2460373e76bonwickspa_add(const char *name, const char *altroot)
247fa9e406ahrens{
248fa9e406ahrens	spa_t *spa;
249fa9e406ahrens
250fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
251fa9e406ahrens
252fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
253fa9e406ahrens
254fa9e406ahrens	spa->spa_name = spa_strdup(name);
255fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
256fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
2570373e76bonwick	spa->spa_final_txg = UINT64_MAX;
258fa9e406ahrens
259fa9e406ahrens	refcount_create(&spa->spa_refcount);
26091ebeefahrens	rprw_init(&spa->spa_config_lock);
261fa9e406ahrens
262fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
263fa9e406ahrens
2640373e76bonwick	/*
2650373e76bonwick	 * Set the alternate root, if there is one.
2660373e76bonwick	 */
2670373e76bonwick	if (altroot) {
2680373e76bonwick		spa->spa_root = spa_strdup(altroot);
2690373e76bonwick		spa_active_count++;
2700373e76bonwick	}
2710373e76bonwick
272fa9e406ahrens	return (spa);
273fa9e406ahrens}
274fa9e406ahrens
275fa9e406ahrens/*
276fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
277fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
278fa9e406ahrens * deactivated.
279fa9e406ahrens */
280fa9e406ahrensvoid
281fa9e406ahrensspa_remove(spa_t *spa)
282fa9e406ahrens{
283fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
284fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
285fa9e406ahrens	ASSERT(spa->spa_scrub_thread == NULL);
286fa9e406ahrens
287fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
288fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
289fa9e406ahrens
2900373e76bonwick	if (spa->spa_root) {
291fa9e406ahrens		spa_strfree(spa->spa_root);
2920373e76bonwick		spa_active_count--;
2930373e76bonwick	}
294fa9e406ahrens
295fa9e406ahrens	if (spa->spa_name)
296fa9e406ahrens		spa_strfree(spa->spa_name);
297fa9e406ahrens
298fa9e406ahrens	spa_config_set(spa, NULL);
299fa9e406ahrens
300fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
30191ebeefahrens
30291ebeefahrens	rprw_destroy(&spa->spa_config_lock);
303fa9e406ahrens
3045ad8204nd	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
3055ad8204nd	mutex_destroy(&spa->spa_errlist_lock);
3065ad8204nd	mutex_destroy(&spa->spa_errlog_lock);
3075ad8204nd	mutex_destroy(&spa->spa_scrub_lock);
3085ad8204nd	mutex_destroy(&spa->spa_config_cache_lock);
3095ad8204nd	mutex_destroy(&spa->spa_async_lock);
31006eeb2aek	mutex_destroy(&spa->spa_history_lock);
311b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
3125ad8204nd
313fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
314fa9e406ahrens}
315fa9e406ahrens
316fa9e406ahrens/*
317fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
318fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
319fa9e406ahrens */
320fa9e406ahrensspa_t *
321fa9e406ahrensspa_next(spa_t *prev)
322fa9e406ahrens{
323fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
324fa9e406ahrens
325fa9e406ahrens	if (prev)
326fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
327fa9e406ahrens	else
328fa9e406ahrens		return (avl_first(&spa_namespace_avl));
329fa9e406ahrens}
330fa9e406ahrens
331fa9e406ahrens/*
332fa9e406ahrens * ==========================================================================
333fa9e406ahrens * SPA refcount functions
334fa9e406ahrens * ==========================================================================
335fa9e406ahrens */
336fa9e406ahrens
337fa9e406ahrens/*
338fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
339fa9e406ahrens * have the namespace lock held.
340fa9e406ahrens */
341fa9e406ahrensvoid
342fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
343fa9e406ahrens{
344fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
345fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
346fa9e406ahrens
347fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
348fa9e406ahrens}
349fa9e406ahrens
350fa9e406ahrens/*
351fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
352fa9e406ahrens * have the namespace lock held.
353fa9e406ahrens */
354fa9e406ahrensvoid
355fa9e406ahrensspa_close(spa_t *spa, void *tag)
356fa9e406ahrens{
357fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
358fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
359fa9e406ahrens
360fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
361fa9e406ahrens}
362fa9e406ahrens
363fa9e406ahrens/*
364fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
365fa9e406ahrens * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
366fa9e406ahrens * number of references acquired when opening a pool
367fa9e406ahrens */
368fa9e406ahrensboolean_t
369fa9e406ahrensspa_refcount_zero(spa_t *spa)
370fa9e406ahrens{
371fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
372fa9e406ahrens
373fa9e406ahrens	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
374fa9e406ahrens}
375fa9e406ahrens
376fa9e406ahrens/*
377fa9e406ahrens * ==========================================================================
37899653d4eschrock * SPA spare tracking
37999653d4eschrock * ==========================================================================
38099653d4eschrock */
38199653d4eschrock
38299653d4eschrock/*
38339c2341eschrock * Spares are tracked globally due to the following constraints:
38439c2341eschrock *
38539c2341eschrock * 	- A spare may be part of multiple pools.
38639c2341eschrock * 	- A spare may be added to a pool even if it's actively in use within
38739c2341eschrock *	  another pool.
38839c2341eschrock * 	- A spare in use in any pool can only be the source of a replacement if
38939c2341eschrock *	  the target is a spare in the same pool.
39039c2341eschrock *
39139c2341eschrock * We keep track of all spares on the system through the use of a reference
39239c2341eschrock * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
39339c2341eschrock * spare, then we bump the reference count in the AVL tree.  In addition, we set
39439c2341eschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
39539c2341eschrock * inactive).  When a spare is made active (used to replace a device in the
39639c2341eschrock * pool), we also keep track of which pool its been made a part of.
39739c2341eschrock *
39839c2341eschrock * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
39939c2341eschrock * called under the spa_namespace lock as part of vdev reconfiguration.  The
40039c2341eschrock * separate spare lock exists for the status query path, which does not need to
40139c2341eschrock * be completely consistent with respect to other vdev configuration changes.
40299653d4eschrock */
40339c2341eschrock
40499653d4eschrocktypedef struct spa_spare {
40599653d4eschrock	uint64_t	spare_guid;
40639c2341eschrock	uint64_t	spare_pool;
40799653d4eschrock	avl_node_t	spare_avl;
40899653d4eschrock	int		spare_count;
40999653d4eschrock} spa_spare_t;
41099653d4eschrock
41199653d4eschrockstatic int
41299653d4eschrockspa_spare_compare(const void *a, const void *b)
41399653d4eschrock{
41499653d4eschrock	const spa_spare_t *sa = a;
41599653d4eschrock	const spa_spare_t *sb = b;
41699653d4eschrock
41799653d4eschrock	if (sa->spare_guid < sb->spare_guid)
41899653d4eschrock		return (-1);
41999653d4eschrock	else if (sa->spare_guid > sb->spare_guid)
42099653d4eschrock		return (1);
42199653d4eschrock	else
42299653d4eschrock		return (0);
42399653d4eschrock}
42499653d4eschrock
42599653d4eschrockvoid
42639c2341eschrockspa_spare_add(vdev_t *vd)
42799653d4eschrock{
42899653d4eschrock	avl_index_t where;
42999653d4eschrock	spa_spare_t search;
43099653d4eschrock	spa_spare_t *spare;
43199653d4eschrock
43299653d4eschrock	mutex_enter(&spa_spare_lock);
43339c2341eschrock	ASSERT(!vd->vdev_isspare);
43499653d4eschrock
43539c2341eschrock	search.spare_guid = vd->vdev_guid;
43699653d4eschrock	if ((spare = avl_find(&spa_spare_avl, &search, &where)) != NULL) {
43799653d4eschrock		spare->spare_count++;
43899653d4eschrock	} else {
43939c2341eschrock		spare = kmem_zalloc(sizeof (spa_spare_t), KM_SLEEP);
44039c2341eschrock		spare->spare_guid = vd->vdev_guid;
44199653d4eschrock		spare->spare_count = 1;
44299653d4eschrock		avl_insert(&spa_spare_avl, spare, where);
44399653d4eschrock	}
44439c2341eschrock	vd->vdev_isspare = B_TRUE;
44599653d4eschrock
44699653d4eschrock	mutex_exit(&spa_spare_lock);
44799653d4eschrock}
44899653d4eschrock
44999653d4eschrockvoid
45039c2341eschrockspa_spare_remove(vdev_t *vd)
45199653d4eschrock{
45299653d4eschrock	spa_spare_t search;
45399653d4eschrock	spa_spare_t *spare;
45499653d4eschrock	avl_index_t where;
45599653d4eschrock
45699653d4eschrock	mutex_enter(&spa_spare_lock);
45799653d4eschrock
45839c2341eschrock	search.spare_guid = vd->vdev_guid;
45999653d4eschrock	spare = avl_find(&spa_spare_avl, &search, &where);
46099653d4eschrock
46139c2341eschrock	ASSERT(vd->vdev_isspare);
46299653d4eschrock	ASSERT(spare != NULL);
46399653d4eschrock
46499653d4eschrock	if (--spare->spare_count == 0) {
46599653d4eschrock		avl_remove(&spa_spare_avl, spare);
46699653d4eschrock		kmem_free(spare, sizeof (spa_spare_t));
46739c2341eschrock	} else if (spare->spare_pool == spa_guid(vd->vdev_spa)) {
46839c2341eschrock		spare->spare_pool = 0ULL;
46999653d4eschrock	}
47099653d4eschrock
47139c2341eschrock	vd->vdev_isspare = B_FALSE;
47299653d4eschrock	mutex_exit(&spa_spare_lock);
47399653d4eschrock}
47499653d4eschrock
47599653d4eschrockboolean_t
47639c2341eschrockspa_spare_exists(uint64_t guid, uint64_t *pool)
47799653d4eschrock{
47839c2341eschrock	spa_spare_t search, *found;
47999653d4eschrock	avl_index_t where;
48099653d4eschrock
48199653d4eschrock	mutex_enter(&spa_spare_lock);
48299653d4eschrock
48399653d4eschrock	search.spare_guid = guid;
48439c2341eschrock	found = avl_find(&spa_spare_avl, &search, &where);
48539c2341eschrock
48639c2341eschrock	if (pool) {
48739c2341eschrock		if (found)
48839c2341eschrock			*pool = found->spare_pool;
48939c2341eschrock		else
49039c2341eschrock			*pool = 0ULL;
49139c2341eschrock	}
49299653d4eschrock
49399653d4eschrock	mutex_exit(&spa_spare_lock);
49499653d4eschrock
49539c2341eschrock	return (found != NULL);
49639c2341eschrock}
49739c2341eschrock
49839c2341eschrockvoid
49939c2341eschrockspa_spare_activate(vdev_t *vd)
50039c2341eschrock{
50139c2341eschrock	spa_spare_t search, *found;
50239c2341eschrock	avl_index_t where;
50339c2341eschrock
50439c2341eschrock	mutex_enter(&spa_spare_lock);
50539c2341eschrock	ASSERT(vd->vdev_isspare);
50639c2341eschrock
50739c2341eschrock	search.spare_guid = vd->vdev_guid;
50839c2341eschrock	found = avl_find(&spa_spare_avl, &search, &where);
50939c2341eschrock	ASSERT(found != NULL);
51039c2341eschrock	ASSERT(found->spare_pool == 0ULL);
51139c2341eschrock
51239c2341eschrock	found->spare_pool = spa_guid(vd->vdev_spa);
51339c2341eschrock	mutex_exit(&spa_spare_lock);
51499653d4eschrock}
51599653d4eschrock
51699653d4eschrock/*
51799653d4eschrock * ==========================================================================
518fa9e406ahrens * SPA config locking
519fa9e406ahrens * ==========================================================================
520fa9e406ahrens */
521fa9e406ahrensvoid
522ea8dc4beschrockspa_config_enter(spa_t *spa, krw_t rw, void *tag)
523fa9e406ahrens{
52491ebeefahrens	rprw_enter(&spa->spa_config_lock, rw, tag);
525fa9e406ahrens}
526fa9e406ahrens
527fa9e406ahrensvoid
528ea8dc4beschrockspa_config_exit(spa_t *spa, void *tag)
529fa9e406ahrens{
53091ebeefahrens	rprw_exit(&spa->spa_config_lock, tag);
531fa9e406ahrens}
532fa9e406ahrens
533fa9e406ahrensboolean_t
534fa9e406ahrensspa_config_held(spa_t *spa, krw_t rw)
535fa9e406ahrens{
53691ebeefahrens	return (rprw_held(&spa->spa_config_lock, rw));
537fa9e406ahrens}
538fa9e406ahrens
539fa9e406ahrens/*
540fa9e406ahrens * ==========================================================================
541fa9e406ahrens * SPA vdev locking
542fa9e406ahrens * ==========================================================================
543fa9e406ahrens */
544fa9e406ahrens
545fa9e406ahrens/*
546ea8dc4beschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
547ea8dc4beschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
548fa9e406ahrens * It returns the next transaction group for the spa_t.
549fa9e406ahrens */
550fa9e406ahrensuint64_t
551fa9e406ahrensspa_vdev_enter(spa_t *spa)
552fa9e406ahrens{
5533d7072feschrock	mutex_enter(&spa_namespace_lock);
5543d7072feschrock
555ea8dc4beschrock	/*
5563d7072feschrock	 * Suspend scrub activity while we mess with the config.  We must do
5573d7072feschrock	 * this after acquiring the namespace lock to avoid a 3-way deadlock
5583d7072feschrock	 * with spa_scrub_stop() and the scrub thread.
559ea8dc4beschrock	 */
560ea8dc4beschrock	spa_scrub_suspend(spa);
561fa9e406ahrens
562ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, spa);
563fa9e406ahrens
564fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
565fa9e406ahrens}
566fa9e406ahrens
567fa9e406ahrens/*
568fa9e406ahrens * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
569fa9e406ahrens * locking of spa_vdev_enter(), we also want make sure the transactions have
570fa9e406ahrens * synced to disk, and then update the global configuration cache with the new
571fa9e406ahrens * information.
572fa9e406ahrens */
573fa9e406ahrensint
574fa9e406ahrensspa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
575fa9e406ahrens{
5760e34b6abonwick	int config_changed = B_FALSE;
577ea8dc4beschrock
5780373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
5790e34b6abonwick
5800e34b6abonwick	/*
5810e34b6abonwick	 * Reassess the DTLs.
5820e34b6abonwick	 */
5830373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
5840e34b6abonwick
5850e34b6abonwick	/*
5860373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
5870e34b6abonwick	 */
5880e34b6abonwick	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
5890e34b6abonwick		config_changed = B_TRUE;
5900373e76bonwick		spa_scrub_restart(spa, txg);
5910e34b6abonwick	}
592ea8dc4beschrock
593ea8dc4beschrock	spa_config_exit(spa, spa);
594fa9e406ahrens
595ea8dc4beschrock	/*
5965dabedebonwick	 * Allow scrubbing to resume.
597ea8dc4beschrock	 */
598ea8dc4beschrock	spa_scrub_resume(spa);
599fa9e406ahrens
600fa9e406ahrens	/*
601fa9e406ahrens	 * Note: this txg_wait_synced() is important because it ensures
602fa9e406ahrens	 * that there won't be more than one config change per txg.
603fa9e406ahrens	 * This allows us to use the txg as the generation number.
604fa9e406ahrens	 */
605fa9e406ahrens	if (error == 0)
606fa9e406ahrens		txg_wait_synced(spa->spa_dsl_pool, txg);
607fa9e406ahrens
608fa9e406ahrens	if (vd != NULL) {
609fa9e406ahrens		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
610fa9e406ahrens		vdev_free(vd);
611fa9e406ahrens	}
612fa9e406ahrens
613fa9e406ahrens	/*
6140e34b6abonwick	 * If the config changed, update the config cache.
615fa9e406ahrens	 */
6160e34b6abonwick	if (config_changed)
617fa9e406ahrens		spa_config_sync();
618ea8dc4beschrock
619ea8dc4beschrock	mutex_exit(&spa_namespace_lock);
620fa9e406ahrens
621fa9e406ahrens	return (error);
622fa9e406ahrens}
623fa9e406ahrens
624fa9e406ahrens/*
625fa9e406ahrens * ==========================================================================
626fa9e406ahrens * Miscellaneous functions
627fa9e406ahrens * ==========================================================================
628fa9e406ahrens */
629fa9e406ahrens
630fa9e406ahrens/*
631fa9e406ahrens * Rename a spa_t.
632fa9e406ahrens */
633fa9e406ahrensint
634fa9e406ahrensspa_rename(const char *name, const char *newname)
635fa9e406ahrens{
636fa9e406ahrens	spa_t *spa;
637fa9e406ahrens	int err;
638fa9e406ahrens
639fa9e406ahrens	/*
640fa9e406ahrens	 * Lookup the spa_t and grab the config lock for writing.  We need to
641fa9e406ahrens	 * actually open the pool so that we can sync out the necessary labels.
642fa9e406ahrens	 * It's OK to call spa_open() with the namespace lock held because we
643ea8dc4beschrock	 * allow recursive calls for other reasons.
644fa9e406ahrens	 */
645fa9e406ahrens	mutex_enter(&spa_namespace_lock);
646fa9e406ahrens	if ((err = spa_open(name, &spa, FTAG)) != 0) {
647fa9e406ahrens		mutex_exit(&spa_namespace_lock);
648fa9e406ahrens		return (err);
649fa9e406ahrens	}
650fa9e406ahrens
651ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
652fa9e406ahrens
653fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
654fa9e406ahrens	spa_strfree(spa->spa_name);
655fa9e406ahrens	spa->spa_name = spa_strdup(newname);
656fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
657fa9e406ahrens
658fa9e406ahrens	/*
659fa9e406ahrens	 * Sync all labels to disk with the new names by marking the root vdev
660fa9e406ahrens	 * dirty and waiting for it to sync.  It will pick up the new pool name
661fa9e406ahrens	 * during the sync.
662fa9e406ahrens	 */
663fa9e406ahrens	vdev_config_dirty(spa->spa_root_vdev);
664fa9e406ahrens
665ea8dc4beschrock	spa_config_exit(spa, FTAG);
666fa9e406ahrens
6670373e76bonwick	txg_wait_synced(spa->spa_dsl_pool, 0);
668fa9e406ahrens
669fa9e406ahrens	/*
670fa9e406ahrens	 * Sync the updated config cache.
671fa9e406ahrens	 */
672fa9e406ahrens	spa_config_sync();
673fa9e406ahrens
674fa9e406ahrens	spa_close(spa, FTAG);
675fa9e406ahrens
676fa9e406ahrens	mutex_exit(&spa_namespace_lock);
677fa9e406ahrens
678fa9e406ahrens	return (0);
679fa9e406ahrens}
680fa9e406ahrens
681fa9e406ahrens
682fa9e406ahrens/*
683fa9e406ahrens * Determine whether a pool with given pool_guid exists.  If device_guid is
684fa9e406ahrens * non-zero, determine whether the pool exists *and* contains a device with the
685fa9e406ahrens * specified device_guid.
686fa9e406ahrens */
687fa9e406ahrensboolean_t
688fa9e406ahrensspa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
689fa9e406ahrens{
690fa9e406ahrens	spa_t *spa;
691fa9e406ahrens	avl_tree_t *t = &spa_namespace_avl;
692fa9e406ahrens
693ea8dc4beschrock	ASSERT(MUTEX_HELD(&spa_namespace_lock));
694fa9e406ahrens
695fa9e406ahrens	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
696fa9e406ahrens		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
697fa9e406ahrens			continue;
698fa9e406ahrens		if (spa->spa_root_vdev == NULL)
699fa9e406ahrens			continue;
70039c2341eschrock		if (spa_guid(spa) == pool_guid) {
70139c2341eschrock			if (device_guid == 0)
70239c2341eschrock				break;
70339c2341eschrock
70439c2341eschrock			if (vdev_lookup_by_guid(spa->spa_root_vdev,
70539c2341eschrock			    device_guid) != NULL)
70639c2341eschrock				break;
70739c2341eschrock
70839c2341eschrock			/*
7098654d02perrin			 * Check any devices we may be in the process of adding.
71039c2341eschrock			 */
71139c2341eschrock			if (spa->spa_pending_vdev) {
71239c2341eschrock				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
71339c2341eschrock				    device_guid) != NULL)
71439c2341eschrock					break;
71539c2341eschrock			}
71639c2341eschrock		}
717fa9e406ahrens	}
718fa9e406ahrens
719fa9e406ahrens	return (spa != NULL);
720fa9e406ahrens}
721fa9e406ahrens
722fa9e406ahrenschar *
723fa9e406ahrensspa_strdup(const char *s)
724fa9e406ahrens{
725fa9e406ahrens	size_t len;
726fa9e406ahrens	char *new;
727fa9e406ahrens
728fa9e406ahrens	len = strlen(s);
729fa9e406ahrens	new = kmem_alloc(len + 1, KM_SLEEP);
730fa9e406ahrens	bcopy(s, new, len);
731fa9e406ahrens	new[len] = '\0';
732fa9e406ahrens
733fa9e406ahrens	return (new);
734fa9e406ahrens}
735fa9e406ahrens
736fa9e406ahrensvoid
737fa9e406ahrensspa_strfree(char *s)
738fa9e406ahrens{
739fa9e406ahrens	kmem_free(s, strlen(s) + 1);
740fa9e406ahrens}
741fa9e406ahrens
742fa9e406ahrensuint64_t
743fa9e406ahrensspa_get_random(uint64_t range)
744fa9e406ahrens{
745fa9e406ahrens	uint64_t r;
746fa9e406ahrens
747fa9e406ahrens	ASSERT(range != 0);
748fa9e406ahrens
749fa9e406ahrens	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
750fa9e406ahrens
751fa9e406ahrens	return (r % range);
752fa9e406ahrens}
753fa9e406ahrens
754fa9e406ahrensvoid
755d80c45ebonwicksprintf_blkptr(char *buf, int len, const blkptr_t *bp)
756fa9e406ahrens{
75744cd46cbillm	int d;
758fa9e406ahrens
759fa9e406ahrens	if (bp == NULL) {
760fbabab8maybee		(void) snprintf(buf, len, "<NULL>");
761fa9e406ahrens		return;
762fa9e406ahrens	}
763fa9e406ahrens
764fa9e406ahrens	if (BP_IS_HOLE(bp)) {
765fbabab8maybee		(void) snprintf(buf, len, "<hole>");
766fa9e406ahrens		return;
767fa9e406ahrens	}
768fa9e406ahrens
76944cd46cbillm	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
770fa9e406ahrens	    (u_longlong_t)BP_GET_LEVEL(bp),
771fa9e406ahrens	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
772fa9e406ahrens	    (u_longlong_t)BP_GET_LSIZE(bp),
77344cd46cbillm	    (u_longlong_t)BP_GET_PSIZE(bp));
77444cd46cbillm
77544cd46cbillm	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
776d80c45ebonwick		const dva_t *dva = &bp->blk_dva[d];
77744cd46cbillm		(void) snprintf(buf + strlen(buf), len - strlen(buf),
77844cd46cbillm		    "DVA[%d]=<%llu:%llx:%llx> ", d,
77944cd46cbillm		    (u_longlong_t)DVA_GET_VDEV(dva),
78044cd46cbillm		    (u_longlong_t)DVA_GET_OFFSET(dva),
78144cd46cbillm		    (u_longlong_t)DVA_GET_ASIZE(dva));
78244cd46cbillm	}
783