spa_misc.c revision c5904d138f3bdf0762dbf452a43d5a5c387ea6a8
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/*
2287db74cek * Copyright 2008 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 *
147e05725bbonwick * The spa_config_lock is a form of rwlock.  It must be held as RW_READER
148e05725bbonwick * to perform I/O to the pool, and as RW_WRITER to change the vdev config.
149e05725bbonwick * The spa_config_lock is manipulated with spa_config_{enter,exit,held}().
150fa9e406ahrens *
151ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
152fa9e406ahrens *
15344cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
154ea8dc4beschrock *				for writing.
155fa9e406ahrens *
15644cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
15744cd46cbillm *				to complete, sync the updated configs to the
158ea8dc4beschrock *				cache, and release the namespace lock.
159fa9e406ahrens *
160fa9e406ahrens * The spa_name() function also requires either the spa_namespace_lock
161fa9e406ahrens * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
162fa9e406ahrens * also implemented within this file since is requires manipulation of the
163fa9e406ahrens * namespace.
164fa9e406ahrens */
165fa9e406ahrens
166fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
167fa9e406ahrenskmutex_t spa_namespace_lock;
168fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
1690373e76bonwickstatic int spa_active_count;
170416e0cdekint spa_max_replication_override = SPA_DVAS_PER_BP;
171fa9e406ahrens
17299653d4eschrockstatic kmutex_t spa_spare_lock;
17339c2341eschrockstatic avl_tree_t spa_spare_avl;
174fa94a07brendanstatic kmutex_t spa_l2cache_lock;
175fa94a07brendanstatic avl_tree_t spa_l2cache_avl;
17699653d4eschrock
177fa9e406ahrenskmem_cache_t *spa_buffer_pool;
178fa9e406ahrensint spa_mode;
179fa9e406ahrens
180fa9e406ahrens#ifdef ZFS_DEBUG
18140feaa9ahrens/* Everything except dprintf is on by default in debug builds */
18240feaa9ahrensint zfs_flags = ~ZFS_DEBUG_DPRINTF;
183fa9e406ahrens#else
184fa9e406ahrensint zfs_flags = 0;
185fa9e406ahrens#endif
186fa9e406ahrens
1870125049ahrens/*
1880125049ahrens * zfs_recover can be set to nonzero to attempt to recover from
1890125049ahrens * otherwise-fatal errors, typically caused by on-disk corruption.  When
1900125049ahrens * set, calls to zfs_panic_recover() will turn into warning messages.
1910125049ahrens */
1920125049ahrensint zfs_recover = 0;
1930125049ahrens
194fa9e406ahrens#define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
195fa9e406ahrens
196fa9e406ahrens/*
197fa9e406ahrens * ==========================================================================
198e05725bbonwick * SPA config locking
199e05725bbonwick * ==========================================================================
200e05725bbonwick */
201e05725bbonwickstatic void
202e05725bbonwickspa_config_lock_init(spa_config_lock_t *scl)
203e05725bbonwick{
204e05725bbonwick	mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
205e05725bbonwick	scl->scl_writer = NULL;
206e05725bbonwick	cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
207e05725bbonwick	refcount_create(&scl->scl_count);
208e05725bbonwick}
209e05725bbonwick
210e05725bbonwickstatic void
211e05725bbonwickspa_config_lock_destroy(spa_config_lock_t *scl)
212e05725bbonwick{
213e05725bbonwick	mutex_destroy(&scl->scl_lock);
214e05725bbonwick	ASSERT(scl->scl_writer == NULL);
215e05725bbonwick	cv_destroy(&scl->scl_cv);
216e05725bbonwick	refcount_destroy(&scl->scl_count);
217e05725bbonwick}
218e05725bbonwick
219e05725bbonwickvoid
220e05725bbonwickspa_config_enter(spa_t *spa, krw_t rw, void *tag)
221e05725bbonwick{
222e05725bbonwick	spa_config_lock_t *scl = &spa->spa_config_lock;
223e05725bbonwick
224e05725bbonwick	mutex_enter(&scl->scl_lock);
225e05725bbonwick
226e05725bbonwick	if (rw == RW_READER) {
227e05725bbonwick		while (scl->scl_writer != NULL && scl->scl_writer != curthread)
228e05725bbonwick			cv_wait(&scl->scl_cv, &scl->scl_lock);
229e05725bbonwick	} else {
230e05725bbonwick		while (!refcount_is_zero(&scl->scl_count) &&
231e05725bbonwick		    scl->scl_writer != curthread)
232e05725bbonwick			cv_wait(&scl->scl_cv, &scl->scl_lock);
233e05725bbonwick		scl->scl_writer = curthread;
234e05725bbonwick	}
235e05725bbonwick
236e05725bbonwick	(void) refcount_add(&scl->scl_count, tag);
237e05725bbonwick
238e05725bbonwick	mutex_exit(&scl->scl_lock);
239e05725bbonwick}
240e05725bbonwick
241e05725bbonwickvoid
242e05725bbonwickspa_config_exit(spa_t *spa, void *tag)
243e05725bbonwick{
244e05725bbonwick	spa_config_lock_t *scl = &spa->spa_config_lock;
245e05725bbonwick
246e05725bbonwick	mutex_enter(&scl->scl_lock);
247e05725bbonwick
248e05725bbonwick	ASSERT(!refcount_is_zero(&scl->scl_count));
249e05725bbonwick
250e05725bbonwick	if (refcount_remove(&scl->scl_count, tag) == 0) {
251e05725bbonwick		cv_broadcast(&scl->scl_cv);
252e05725bbonwick		ASSERT(scl->scl_writer == NULL || scl->scl_writer == curthread);
253e05725bbonwick		scl->scl_writer = NULL;  /* OK in either case */
254e05725bbonwick	}
255e05725bbonwick
256e05725bbonwick	mutex_exit(&scl->scl_lock);
257e05725bbonwick}
258e05725bbonwick
259e05725bbonwickboolean_t
260e05725bbonwickspa_config_held(spa_t *spa, krw_t rw)
261e05725bbonwick{
262e05725bbonwick	spa_config_lock_t *scl = &spa->spa_config_lock;
263e05725bbonwick
264e05725bbonwick	if (rw == RW_READER)
265e05725bbonwick		return (!refcount_is_zero(&scl->scl_count));
266e05725bbonwick	else
267e05725bbonwick		return (scl->scl_writer == curthread);
268e05725bbonwick}
269e05725bbonwick
270e05725bbonwick/*
271e05725bbonwick * ==========================================================================
272fa9e406ahrens * SPA namespace functions
273fa9e406ahrens * ==========================================================================
274fa9e406ahrens */
275fa9e406ahrens
276fa9e406ahrens/*
277fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
278fa9e406ahrens * Returns NULL if no matching spa_t is found.
279fa9e406ahrens */
280fa9e406ahrensspa_t *
281fa9e406ahrensspa_lookup(const char *name)
282fa9e406ahrens{
283fa9e406ahrens	spa_t search, *spa;
284fa9e406ahrens	avl_index_t where;
28540feaa9ahrens	char c;
28640feaa9ahrens	char *cp;
287fa9e406ahrens
288fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
289fa9e406ahrens
29040feaa9ahrens	/*
29140feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
29240feaa9ahrens	 * just use that.
29340feaa9ahrens	 */
29440feaa9ahrens	cp = strpbrk(name, "/@");
29540feaa9ahrens	if (cp) {
29640feaa9ahrens		c = *cp;
29740feaa9ahrens		*cp = '\0';
29840feaa9ahrens	}
29940feaa9ahrens
300fa9e406ahrens	search.spa_name = (char *)name;
301fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
302fa9e406ahrens
30340feaa9ahrens	if (cp)
30440feaa9ahrens		*cp = c;
30540feaa9ahrens
306fa9e406ahrens	return (spa);
307fa9e406ahrens}
308fa9e406ahrens
309fa9e406ahrens/*
310fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
311fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
312fa9e406ahrens * exist by calling spa_lookup() first.
313fa9e406ahrens */
314fa9e406ahrensspa_t *
3150373e76bonwickspa_add(const char *name, const char *altroot)
316fa9e406ahrens{
317fa9e406ahrens	spa_t *spa;
318c5904d1eschrock	spa_config_dirent_t *dp;
319fa9e406ahrens
320fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
321fa9e406ahrens
322fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
323fa9e406ahrens
324c25056dgw	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
325c25056dgw
326c25056dgw	mutex_init(&spa->spa_uberblock_lock, NULL, MUTEX_DEFAULT, NULL);
327c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
328c25056dgw	mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL);
329c25056dgw	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
330c25056dgw	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
331c25056dgw	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
332c25056dgw	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
333c25056dgw	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
334c25056dgw	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
335c25056dgw
336c25056dgw	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
337c25056dgw	cv_init(&spa->spa_scrub_cv, NULL, CV_DEFAULT, NULL);
338c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
339c25056dgw
340fa9e406ahrens	spa->spa_name = spa_strdup(name);
341fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
342fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
3430373e76bonwick	spa->spa_final_txg = UINT64_MAX;
344fa9e406ahrens
345fa9e406ahrens	refcount_create(&spa->spa_refcount);
346e05725bbonwick	spa_config_lock_init(&spa->spa_config_lock);
347fa9e406ahrens
348fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
349fa9e406ahrens
3500a4e951gw	mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL);
3510a4e951gw
3520373e76bonwick	/*
3530373e76bonwick	 * Set the alternate root, if there is one.
3540373e76bonwick	 */
3550373e76bonwick	if (altroot) {
3560373e76bonwick		spa->spa_root = spa_strdup(altroot);
3570373e76bonwick		spa_active_count++;
3580373e76bonwick	}
3590373e76bonwick
360c5904d1eschrock	/*
361c5904d1eschrock	 * Every pool starts with the default cachefile
362c5904d1eschrock	 */
363c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
364c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
365c5904d1eschrock
366c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
367c5904d1eschrock	dp->scd_path = spa_strdup(spa_config_path);
368c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
369c5904d1eschrock
370fa9e406ahrens	return (spa);
371fa9e406ahrens}
372fa9e406ahrens
373fa9e406ahrens/*
374fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
375fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
376fa9e406ahrens * deactivated.
377fa9e406ahrens */
378fa9e406ahrensvoid
379fa9e406ahrensspa_remove(spa_t *spa)
380fa9e406ahrens{
381c5904d1eschrock	spa_config_dirent_t *dp;
382c5904d1eschrock
383fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
384fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
385fa9e406ahrens	ASSERT(spa->spa_scrub_thread == NULL);
386fa9e406ahrens
387fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
388fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
389fa9e406ahrens
3900373e76bonwick	if (spa->spa_root) {
391fa9e406ahrens		spa_strfree(spa->spa_root);
3920373e76bonwick		spa_active_count--;
3930373e76bonwick	}
394fa9e406ahrens
395fa9e406ahrens	if (spa->spa_name)
396fa9e406ahrens		spa_strfree(spa->spa_name);
397fa9e406ahrens
398c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
399c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
400c5904d1eschrock		if (dp->scd_path != NULL)
401c5904d1eschrock			spa_strfree(dp->scd_path);
402c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
403c5904d1eschrock	}
404c5904d1eschrock
405c5904d1eschrock	list_destroy(&spa->spa_config_list);
4062f8aaabeschrock
407fa9e406ahrens	spa_config_set(spa, NULL);
408fa9e406ahrens
409fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
41091ebeefahrens
411e05725bbonwick	spa_config_lock_destroy(&spa->spa_config_lock);
412fa9e406ahrens
413c25056dgw	rw_destroy(&spa->spa_traverse_lock);
414c25056dgw
415c25056dgw	cv_destroy(&spa->spa_async_cv);
416c25056dgw	cv_destroy(&spa->spa_scrub_cv);
417c25056dgw	cv_destroy(&spa->spa_scrub_io_cv);
418c25056dgw
419c25056dgw	mutex_destroy(&spa->spa_uberblock_lock);
4205ad8204nd	mutex_destroy(&spa->spa_async_lock);
421c25056dgw	mutex_destroy(&spa->spa_config_cache_lock);
422c25056dgw	mutex_destroy(&spa->spa_scrub_lock);
423c25056dgw	mutex_destroy(&spa->spa_errlog_lock);
424c25056dgw	mutex_destroy(&spa->spa_errlist_lock);
425c25056dgw	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
42606eeb2aek	mutex_destroy(&spa->spa_history_lock);
427b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
4280a4e951gw	mutex_destroy(&spa->spa_zio_lock);
4295ad8204nd
430fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
431fa9e406ahrens}
432fa9e406ahrens
433fa9e406ahrens/*
434fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
435fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
436fa9e406ahrens */
437fa9e406ahrensspa_t *
438fa9e406ahrensspa_next(spa_t *prev)
439fa9e406ahrens{
440fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
441fa9e406ahrens
442fa9e406ahrens	if (prev)
443fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
444fa9e406ahrens	else
445fa9e406ahrens		return (avl_first(&spa_namespace_avl));
446fa9e406ahrens}
447fa9e406ahrens
448fa9e406ahrens/*
449fa9e406ahrens * ==========================================================================
450fa9e406ahrens * SPA refcount functions
451fa9e406ahrens * ==========================================================================
452fa9e406ahrens */
453fa9e406ahrens
454fa9e406ahrens/*
455fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
456fa9e406ahrens * have the namespace lock held.
457fa9e406ahrens */
458fa9e406ahrensvoid
459fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
460fa9e406ahrens{
461fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
462fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
463fa9e406ahrens
464fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
465fa9e406ahrens}
466fa9e406ahrens
467fa9e406ahrens/*
468fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
469fa9e406ahrens * have the namespace lock held.
470fa9e406ahrens */
471fa9e406ahrensvoid
472fa9e406ahrensspa_close(spa_t *spa, void *tag)
473fa9e406ahrens{
474fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
475fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
476fa9e406ahrens
477fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
478fa9e406ahrens}
479fa9e406ahrens
480fa9e406ahrens/*
481fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
482fa9e406ahrens * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
483fa9e406ahrens * number of references acquired when opening a pool
484fa9e406ahrens */
485fa9e406ahrensboolean_t
486fa9e406ahrensspa_refcount_zero(spa_t *spa)
487fa9e406ahrens{
488fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
489fa9e406ahrens
490fa9e406ahrens	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
491fa9e406ahrens}
492fa9e406ahrens
493fa9e406ahrens/*
494fa9e406ahrens * ==========================================================================
495fa94a07brendan * SPA spare and l2cache tracking
49699653d4eschrock * ==========================================================================
49799653d4eschrock */
49899653d4eschrock
49999653d4eschrock/*
500fa94a07brendan * Hot spares and cache devices are tracked using the same code below,
501fa94a07brendan * for 'auxiliary' devices.
502fa94a07brendan */
503fa94a07brendan
504fa94a07brendantypedef struct spa_aux {
505fa94a07brendan	uint64_t	aux_guid;
506fa94a07brendan	uint64_t	aux_pool;
507fa94a07brendan	avl_node_t	aux_avl;
508fa94a07brendan	int		aux_count;
509fa94a07brendan} spa_aux_t;
510fa94a07brendan
511fa94a07brendanstatic int
512fa94a07brendanspa_aux_compare(const void *a, const void *b)
513fa94a07brendan{
514fa94a07brendan	const spa_aux_t *sa = a;
515fa94a07brendan	const spa_aux_t *sb = b;
516fa94a07brendan
517fa94a07brendan	if (sa->aux_guid < sb->aux_guid)
518fa94a07brendan		return (-1);
519fa94a07brendan	else if (sa->aux_guid > sb->aux_guid)
520fa94a07brendan		return (1);
521fa94a07brendan	else
522fa94a07brendan		return (0);
523fa94a07brendan}
524fa94a07brendan
525fa94a07brendanvoid
526fa94a07brendanspa_aux_add(vdev_t *vd, avl_tree_t *avl)
527fa94a07brendan{
528fa94a07brendan	avl_index_t where;
529fa94a07brendan	spa_aux_t search;
530fa94a07brendan	spa_aux_t *aux;
531fa94a07brendan
532fa94a07brendan	search.aux_guid = vd->vdev_guid;
533fa94a07brendan	if ((aux = avl_find(avl, &search, &where)) != NULL) {
534fa94a07brendan		aux->aux_count++;
535fa94a07brendan	} else {
536fa94a07brendan		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
537fa94a07brendan		aux->aux_guid = vd->vdev_guid;
538fa94a07brendan		aux->aux_count = 1;
539fa94a07brendan		avl_insert(avl, aux, where);
540fa94a07brendan	}
541fa94a07brendan}
542fa94a07brendan
543fa94a07brendanvoid
544fa94a07brendanspa_aux_remove(vdev_t *vd, avl_tree_t *avl)
545fa94a07brendan{
546fa94a07brendan	spa_aux_t search;
547fa94a07brendan	spa_aux_t *aux;
548fa94a07brendan	avl_index_t where;
549fa94a07brendan
550fa94a07brendan	search.aux_guid = vd->vdev_guid;
551fa94a07brendan	aux = avl_find(avl, &search, &where);
552fa94a07brendan
553fa94a07brendan	ASSERT(aux != NULL);
554fa94a07brendan
555fa94a07brendan	if (--aux->aux_count == 0) {
556fa94a07brendan		avl_remove(avl, aux);
557fa94a07brendan		kmem_free(aux, sizeof (spa_aux_t));
558fa94a07brendan	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
559fa94a07brendan		aux->aux_pool = 0ULL;
560fa94a07brendan	}
561fa94a07brendan}
562fa94a07brendan
563fa94a07brendanboolean_t
564fa94a07brendanspa_aux_exists(uint64_t guid, uint64_t *pool, avl_tree_t *avl)
565fa94a07brendan{
566fa94a07brendan	spa_aux_t search, *found;
567fa94a07brendan	avl_index_t where;
568fa94a07brendan
569fa94a07brendan	search.aux_guid = guid;
570fa94a07brendan	found = avl_find(avl, &search, &where);
571fa94a07brendan
572fa94a07brendan	if (pool) {
573fa94a07brendan		if (found)
574fa94a07brendan			*pool = found->aux_pool;
575fa94a07brendan		else
576fa94a07brendan			*pool = 0ULL;
577fa94a07brendan	}
578fa94a07brendan
579fa94a07brendan	return (found != NULL);
580fa94a07brendan}
581fa94a07brendan
582fa94a07brendanvoid
583fa94a07brendanspa_aux_activate(vdev_t *vd, avl_tree_t *avl)
584fa94a07brendan{
585fa94a07brendan	spa_aux_t search, *found;
586fa94a07brendan	avl_index_t where;
587fa94a07brendan
588fa94a07brendan	search.aux_guid = vd->vdev_guid;
589fa94a07brendan	found = avl_find(avl, &search, &where);
590fa94a07brendan	ASSERT(found != NULL);
591fa94a07brendan	ASSERT(found->aux_pool == 0ULL);
592fa94a07brendan
593fa94a07brendan	found->aux_pool = spa_guid(vd->vdev_spa);
594fa94a07brendan}
595fa94a07brendan
596fa94a07brendan/*
59739c2341eschrock * Spares are tracked globally due to the following constraints:
59839c2341eschrock *
59939c2341eschrock * 	- A spare may be part of multiple pools.
60039c2341eschrock * 	- A spare may be added to a pool even if it's actively in use within
60139c2341eschrock *	  another pool.
60239c2341eschrock * 	- A spare in use in any pool can only be the source of a replacement if
60339c2341eschrock *	  the target is a spare in the same pool.
60439c2341eschrock *
60539c2341eschrock * We keep track of all spares on the system through the use of a reference
60639c2341eschrock * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
60739c2341eschrock * spare, then we bump the reference count in the AVL tree.  In addition, we set
60839c2341eschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
60939c2341eschrock * inactive).  When a spare is made active (used to replace a device in the
61039c2341eschrock * pool), we also keep track of which pool its been made a part of.
61139c2341eschrock *
61239c2341eschrock * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
61339c2341eschrock * called under the spa_namespace lock as part of vdev reconfiguration.  The
61439c2341eschrock * separate spare lock exists for the status query path, which does not need to
61539c2341eschrock * be completely consistent with respect to other vdev configuration changes.
61699653d4eschrock */
61739c2341eschrock
61899653d4eschrockstatic int
61999653d4eschrockspa_spare_compare(const void *a, const void *b)
62099653d4eschrock{
621fa94a07brendan	return (spa_aux_compare(a, b));
62299653d4eschrock}
62399653d4eschrock
62499653d4eschrockvoid
62539c2341eschrockspa_spare_add(vdev_t *vd)
62699653d4eschrock{
62799653d4eschrock	mutex_enter(&spa_spare_lock);
62839c2341eschrock	ASSERT(!vd->vdev_isspare);
629fa94a07brendan	spa_aux_add(vd, &spa_spare_avl);
63039c2341eschrock	vd->vdev_isspare = B_TRUE;
63199653d4eschrock	mutex_exit(&spa_spare_lock);
63299653d4eschrock}
63399653d4eschrock
63499653d4eschrockvoid
63539c2341eschrockspa_spare_remove(vdev_t *vd)
63699653d4eschrock{
63799653d4eschrock	mutex_enter(&spa_spare_lock);
63839c2341eschrock	ASSERT(vd->vdev_isspare);
639fa94a07brendan	spa_aux_remove(vd, &spa_spare_avl);
64039c2341eschrock	vd->vdev_isspare = B_FALSE;
64199653d4eschrock	mutex_exit(&spa_spare_lock);
64299653d4eschrock}
64399653d4eschrock
64499653d4eschrockboolean_t
64539c2341eschrockspa_spare_exists(uint64_t guid, uint64_t *pool)
64699653d4eschrock{
647fa94a07brendan	boolean_t found;
64899653d4eschrock
64999653d4eschrock	mutex_enter(&spa_spare_lock);
650fa94a07brendan	found = spa_aux_exists(guid, pool, &spa_spare_avl);
65199653d4eschrock	mutex_exit(&spa_spare_lock);
65299653d4eschrock
653fa94a07brendan	return (found);
65439c2341eschrock}
65539c2341eschrock
65639c2341eschrockvoid
65739c2341eschrockspa_spare_activate(vdev_t *vd)
65839c2341eschrock{
65939c2341eschrock	mutex_enter(&spa_spare_lock);
66039c2341eschrock	ASSERT(vd->vdev_isspare);
661fa94a07brendan	spa_aux_activate(vd, &spa_spare_avl);
662fa94a07brendan	mutex_exit(&spa_spare_lock);
663fa94a07brendan}
66439c2341eschrock
665fa94a07brendan/*
666fa94a07brendan * Level 2 ARC devices are tracked globally for the same reasons as spares.
667fa94a07brendan * Cache devices currently only support one pool per cache device, and so
668fa94a07brendan * for these devices the aux reference count is currently unused beyond 1.
669fa94a07brendan */
67039c2341eschrock
671fa94a07brendanstatic int
672fa94a07brendanspa_l2cache_compare(const void *a, const void *b)
673fa94a07brendan{
674fa94a07brendan	return (spa_aux_compare(a, b));
675fa94a07brendan}
676fa94a07brendan
677fa94a07brendanvoid
678fa94a07brendanspa_l2cache_add(vdev_t *vd)
679fa94a07brendan{
680fa94a07brendan	mutex_enter(&spa_l2cache_lock);
681fa94a07brendan	ASSERT(!vd->vdev_isl2cache);
682fa94a07brendan	spa_aux_add(vd, &spa_l2cache_avl);
683fa94a07brendan	vd->vdev_isl2cache = B_TRUE;
684fa94a07brendan	mutex_exit(&spa_l2cache_lock);
685fa94a07brendan}
686fa94a07brendan
687fa94a07brendanvoid
688fa94a07brendanspa_l2cache_remove(vdev_t *vd)
689fa94a07brendan{
690fa94a07brendan	mutex_enter(&spa_l2cache_lock);
691fa94a07brendan	ASSERT(vd->vdev_isl2cache);
692fa94a07brendan	spa_aux_remove(vd, &spa_l2cache_avl);
693fa94a07brendan	vd->vdev_isl2cache = B_FALSE;
694fa94a07brendan	mutex_exit(&spa_l2cache_lock);
695fa94a07brendan}
696fa94a07brendan
697fa94a07brendanboolean_t
698fa94a07brendanspa_l2cache_exists(uint64_t guid, uint64_t *pool)
699fa94a07brendan{
700fa94a07brendan	boolean_t found;
701fa94a07brendan
702fa94a07brendan	mutex_enter(&spa_l2cache_lock);
703fa94a07brendan	found = spa_aux_exists(guid, pool, &spa_l2cache_avl);
704fa94a07brendan	mutex_exit(&spa_l2cache_lock);
705fa94a07brendan
706fa94a07brendan	return (found);
707fa94a07brendan}
708fa94a07brendan
709fa94a07brendanvoid
710fa94a07brendanspa_l2cache_activate(vdev_t *vd)
711fa94a07brendan{
712fa94a07brendan	mutex_enter(&spa_l2cache_lock);
713fa94a07brendan	ASSERT(vd->vdev_isl2cache);
714fa94a07brendan	spa_aux_activate(vd, &spa_l2cache_avl);
715fa94a07brendan	mutex_exit(&spa_l2cache_lock);
716fa94a07brendan}
717fa94a07brendan
718fa94a07brendanvoid
719fa94a07brendanspa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
720fa94a07brendan{
721fa94a07brendan	vdev_space_update(vd, space, alloc, B_FALSE);
72299653d4eschrock}
72399653d4eschrock
72499653d4eschrock/*
72599653d4eschrock * ==========================================================================
726fa9e406ahrens * SPA vdev locking
727fa9e406ahrens * ==========================================================================
728fa9e406ahrens */
729fa9e406ahrens
730fa9e406ahrens/*
731ea8dc4beschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
732ea8dc4beschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
733fa9e406ahrens * It returns the next transaction group for the spa_t.
734fa9e406ahrens */
735fa9e406ahrensuint64_t
736fa9e406ahrensspa_vdev_enter(spa_t *spa)
737fa9e406ahrens{
7383d7072feschrock	mutex_enter(&spa_namespace_lock);
7393d7072feschrock
740ea8dc4beschrock	/*
7413d7072feschrock	 * Suspend scrub activity while we mess with the config.  We must do
7423d7072feschrock	 * this after acquiring the namespace lock to avoid a 3-way deadlock
7433d7072feschrock	 * with spa_scrub_stop() and the scrub thread.
744ea8dc4beschrock	 */
745ea8dc4beschrock	spa_scrub_suspend(spa);
746fa9e406ahrens
747ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, spa);
748fa9e406ahrens
749fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
750fa9e406ahrens}
751fa9e406ahrens
752fa9e406ahrens/*
753fa9e406ahrens * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
754fa9e406ahrens * locking of spa_vdev_enter(), we also want make sure the transactions have
755fa9e406ahrens * synced to disk, and then update the global configuration cache with the new
756fa9e406ahrens * information.
757fa9e406ahrens */
758fa9e406ahrensint
759fa9e406ahrensspa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
760fa9e406ahrens{
7610e34b6abonwick	int config_changed = B_FALSE;
762ea8dc4beschrock
7630373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
7640e34b6abonwick
7650e34b6abonwick	/*
7660e34b6abonwick	 * Reassess the DTLs.
7670e34b6abonwick	 */
7680373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
7690e34b6abonwick
7700e34b6abonwick	/*
7710373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
7720e34b6abonwick	 */
7730e34b6abonwick	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
7740e34b6abonwick		config_changed = B_TRUE;
7750373e76bonwick		spa_scrub_restart(spa, txg);
7760e34b6abonwick	}
777ea8dc4beschrock
778ea8dc4beschrock	spa_config_exit(spa, spa);
779fa9e406ahrens
780ea8dc4beschrock	/*
7815dabedebonwick	 * Allow scrubbing to resume.
782ea8dc4beschrock	 */
783ea8dc4beschrock	spa_scrub_resume(spa);
784fa9e406ahrens
785fa9e406ahrens	/*
786fa9e406ahrens	 * Note: this txg_wait_synced() is important because it ensures
787fa9e406ahrens	 * that there won't be more than one config change per txg.
788fa9e406ahrens	 * This allows us to use the txg as the generation number.
789fa9e406ahrens	 */
790fa9e406ahrens	if (error == 0)
791fa9e406ahrens		txg_wait_synced(spa->spa_dsl_pool, txg);
792fa9e406ahrens
793fa9e406ahrens	if (vd != NULL) {
794fa9e406ahrens		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
795fa9e406ahrens		vdev_free(vd);
796fa9e406ahrens	}
797fa9e406ahrens
798fa9e406ahrens	/*
7990e34b6abonwick	 * If the config changed, update the config cache.
800fa9e406ahrens	 */
8010e34b6abonwick	if (config_changed)
802c5904d1eschrock		spa_config_sync(spa, B_FALSE, B_TRUE);
803ea8dc4beschrock
804ea8dc4beschrock	mutex_exit(&spa_namespace_lock);
805fa9e406ahrens
806fa9e406ahrens	return (error);
807fa9e406ahrens}
808fa9e406ahrens
809fa9e406ahrens/*
810fa9e406ahrens * ==========================================================================
811fa9e406ahrens * Miscellaneous functions
812fa9e406ahrens * ==========================================================================
813fa9e406ahrens */
814fa9e406ahrens
815fa9e406ahrens/*
816fa9e406ahrens * Rename a spa_t.
817fa9e406ahrens */
818fa9e406ahrensint
819fa9e406ahrensspa_rename(const char *name, const char *newname)
820fa9e406ahrens{
821fa9e406ahrens	spa_t *spa;
822fa9e406ahrens	int err;
823fa9e406ahrens
824fa9e406ahrens	/*
825fa9e406ahrens	 * Lookup the spa_t and grab the config lock for writing.  We need to
826fa9e406ahrens	 * actually open the pool so that we can sync out the necessary labels.
827fa9e406ahrens	 * It's OK to call spa_open() with the namespace lock held because we
828ea8dc4beschrock	 * allow recursive calls for other reasons.
829fa9e406ahrens	 */
830fa9e406ahrens	mutex_enter(&spa_namespace_lock);
831fa9e406ahrens	if ((err = spa_open(name, &spa, FTAG)) != 0) {
832fa9e406ahrens		mutex_exit(&spa_namespace_lock);
833fa9e406ahrens		return (err);
834