spa_misc.c revision 89a89ebfd7c3b4056afe2c03e959e22824df777d
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
77088f389ahrens *	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
195fa9e406ahrens/*
196fa9e406ahrens * ==========================================================================
197e05725bbonwick * SPA config locking
198e05725bbonwick * ==========================================================================
199e05725bbonwick */
200e05725bbonwickstatic void
201e05725bbonwickspa_config_lock_init(spa_config_lock_t *scl)
202e05725bbonwick{
203e05725bbonwick	mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
204e05725bbonwick	scl->scl_writer = NULL;
205e05725bbonwick	cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
206e05725bbonwick	refcount_create(&scl->scl_count);
207e05725bbonwick}
208e05725bbonwick
209e05725bbonwickstatic void
210e05725bbonwickspa_config_lock_destroy(spa_config_lock_t *scl)
211e05725bbonwick{
212e05725bbonwick	mutex_destroy(&scl->scl_lock);
213e05725bbonwick	ASSERT(scl->scl_writer == NULL);
214e05725bbonwick	cv_destroy(&scl->scl_cv);
215e05725bbonwick	refcount_destroy(&scl->scl_count);
216e05725bbonwick}
217e05725bbonwick
218e05725bbonwickvoid
219e05725bbonwickspa_config_enter(spa_t *spa, krw_t rw, void *tag)
220e05725bbonwick{
221e05725bbonwick	spa_config_lock_t *scl = &spa->spa_config_lock;
222e05725bbonwick
223e05725bbonwick	mutex_enter(&scl->scl_lock);
224e05725bbonwick
225e05725bbonwick	if (rw == RW_READER) {
226e05725bbonwick		while (scl->scl_writer != NULL && scl->scl_writer != curthread)
227e05725bbonwick			cv_wait(&scl->scl_cv, &scl->scl_lock);
228e05725bbonwick	} else {
229e05725bbonwick		while (!refcount_is_zero(&scl->scl_count) &&
230e05725bbonwick		    scl->scl_writer != curthread)
231e05725bbonwick			cv_wait(&scl->scl_cv, &scl->scl_lock);
232e05725bbonwick		scl->scl_writer = curthread;
233e05725bbonwick	}
234e05725bbonwick
235e05725bbonwick	(void) refcount_add(&scl->scl_count, tag);
236e05725bbonwick
237e05725bbonwick	mutex_exit(&scl->scl_lock);
238e05725bbonwick}
239e05725bbonwick
240e05725bbonwickvoid
241e05725bbonwickspa_config_exit(spa_t *spa, void *tag)
242e05725bbonwick{
243e05725bbonwick	spa_config_lock_t *scl = &spa->spa_config_lock;
244e05725bbonwick
245e05725bbonwick	mutex_enter(&scl->scl_lock);
246e05725bbonwick
247e05725bbonwick	ASSERT(!refcount_is_zero(&scl->scl_count));
248e05725bbonwick
249e05725bbonwick	if (refcount_remove(&scl->scl_count, tag) == 0) {
250e05725bbonwick		cv_broadcast(&scl->scl_cv);
251e05725bbonwick		ASSERT(scl->scl_writer == NULL || scl->scl_writer == curthread);
252e05725bbonwick		scl->scl_writer = NULL;  /* OK in either case */
253e05725bbonwick	}
254e05725bbonwick
255e05725bbonwick	mutex_exit(&scl->scl_lock);
256e05725bbonwick}
257e05725bbonwick
258e05725bbonwickboolean_t
259e05725bbonwickspa_config_held(spa_t *spa, krw_t rw)
260e05725bbonwick{
261e05725bbonwick	spa_config_lock_t *scl = &spa->spa_config_lock;
262e05725bbonwick
263e05725bbonwick	if (rw == RW_READER)
264e05725bbonwick		return (!refcount_is_zero(&scl->scl_count));
265e05725bbonwick	else
266e05725bbonwick		return (scl->scl_writer == curthread);
267e05725bbonwick}
268e05725bbonwick
269e05725bbonwick/*
270e05725bbonwick * ==========================================================================
271fa9e406ahrens * SPA namespace functions
272fa9e406ahrens * ==========================================================================
273fa9e406ahrens */
274fa9e406ahrens
275fa9e406ahrens/*
276fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
277fa9e406ahrens * Returns NULL if no matching spa_t is found.
278fa9e406ahrens */
279fa9e406ahrensspa_t *
280fa9e406ahrensspa_lookup(const char *name)
281fa9e406ahrens{
282fa9e406ahrens	spa_t search, *spa;
283fa9e406ahrens	avl_index_t where;
28440feaa9ahrens	char c;
28540feaa9ahrens	char *cp;
286fa9e406ahrens
287fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
288fa9e406ahrens
28940feaa9ahrens	/*
29040feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
29140feaa9ahrens	 * just use that.
29240feaa9ahrens	 */
29340feaa9ahrens	cp = strpbrk(name, "/@");
29440feaa9ahrens	if (cp) {
29540feaa9ahrens		c = *cp;
29640feaa9ahrens		*cp = '\0';
29740feaa9ahrens	}
29840feaa9ahrens
299fa9e406ahrens	search.spa_name = (char *)name;
300fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
301fa9e406ahrens
30240feaa9ahrens	if (cp)
30340feaa9ahrens		*cp = c;
30440feaa9ahrens
305fa9e406ahrens	return (spa);
306fa9e406ahrens}
307fa9e406ahrens
308fa9e406ahrens/*
309fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
310fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
311fa9e406ahrens * exist by calling spa_lookup() first.
312fa9e406ahrens */
313fa9e406ahrensspa_t *
3140373e76bonwickspa_add(const char *name, const char *altroot)
315fa9e406ahrens{
316fa9e406ahrens	spa_t *spa;
317c5904d1eschrock	spa_config_dirent_t *dp;
318fa9e406ahrens
319fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
320fa9e406ahrens
321fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
322fa9e406ahrens
323c25056dgw	rw_init(&spa->spa_traverse_lock, NULL, RW_DEFAULT, NULL);
324c25056dgw
325c25056dgw	mutex_init(&spa->spa_uberblock_lock, NULL, MUTEX_DEFAULT, NULL);
326c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
327c25056dgw	mutex_init(&spa->spa_config_cache_lock, NULL, MUTEX_DEFAULT, NULL);
328c25056dgw	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
329c25056dgw	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
330c25056dgw	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
331c25056dgw	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
332c25056dgw	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
333c25056dgw	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
334c25056dgw
335c25056dgw	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
336c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
337c25056dgw
338fa9e406ahrens	spa->spa_name = spa_strdup(name);
339fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
340fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
3410373e76bonwick	spa->spa_final_txg = UINT64_MAX;
342fa9e406ahrens
343fa9e406ahrens	refcount_create(&spa->spa_refcount);
344e05725bbonwick	spa_config_lock_init(&spa->spa_config_lock);
345fa9e406ahrens
346fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
347fa9e406ahrens
3480a4e951gw	mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL);
3490a4e951gw
3500373e76bonwick	/*
3510373e76bonwick	 * Set the alternate root, if there is one.
3520373e76bonwick	 */
3530373e76bonwick	if (altroot) {
3540373e76bonwick		spa->spa_root = spa_strdup(altroot);
3550373e76bonwick		spa_active_count++;
3560373e76bonwick	}
3570373e76bonwick
358c5904d1eschrock	/*
359c5904d1eschrock	 * Every pool starts with the default cachefile
360c5904d1eschrock	 */
361c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
362c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
363c5904d1eschrock
364c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
365c5904d1eschrock	dp->scd_path = spa_strdup(spa_config_path);
366c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
367c5904d1eschrock
368fa9e406ahrens	return (spa);
369fa9e406ahrens}
370fa9e406ahrens
371fa9e406ahrens/*
372fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
373fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
374fa9e406ahrens * deactivated.
375fa9e406ahrens */
376fa9e406ahrensvoid
377fa9e406ahrensspa_remove(spa_t *spa)
378fa9e406ahrens{
379c5904d1eschrock	spa_config_dirent_t *dp;
380c5904d1eschrock
381fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
382fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
383fa9e406ahrens
384fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
385fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
386fa9e406ahrens
3870373e76bonwick	if (spa->spa_root) {
388fa9e406ahrens		spa_strfree(spa->spa_root);
3890373e76bonwick		spa_active_count--;
3900373e76bonwick	}
391fa9e406ahrens
392fa9e406ahrens	if (spa->spa_name)
393fa9e406ahrens		spa_strfree(spa->spa_name);
394fa9e406ahrens
395c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
396c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
397c5904d1eschrock		if (dp->scd_path != NULL)
398c5904d1eschrock			spa_strfree(dp->scd_path);
399c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
400c5904d1eschrock	}
401c5904d1eschrock
402c5904d1eschrock	list_destroy(&spa->spa_config_list);
4032f8aaabeschrock
404fa9e406ahrens	spa_config_set(spa, NULL);
405fa9e406ahrens
406fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
40791ebeefahrens
408e05725bbonwick	spa_config_lock_destroy(&spa->spa_config_lock);
409fa9e406ahrens
410c25056dgw	rw_destroy(&spa->spa_traverse_lock);
411c25056dgw
412c25056dgw	cv_destroy(&spa->spa_async_cv);
413c25056dgw	cv_destroy(&spa->spa_scrub_io_cv);
414c25056dgw
415c25056dgw	mutex_destroy(&spa->spa_uberblock_lock);
4165ad8204nd	mutex_destroy(&spa->spa_async_lock);
417c25056dgw	mutex_destroy(&spa->spa_config_cache_lock);
418c25056dgw	mutex_destroy(&spa->spa_scrub_lock);
419c25056dgw	mutex_destroy(&spa->spa_errlog_lock);
420c25056dgw	mutex_destroy(&spa->spa_errlist_lock);
421c25056dgw	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
42206eeb2aek	mutex_destroy(&spa->spa_history_lock);
423b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
4240a4e951gw	mutex_destroy(&spa->spa_zio_lock);
4255ad8204nd
426fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
427fa9e406ahrens}
428fa9e406ahrens
429fa9e406ahrens/*
430fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
431fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
432fa9e406ahrens */
433fa9e406ahrensspa_t *
434fa9e406ahrensspa_next(spa_t *prev)
435fa9e406ahrens{
436fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
437fa9e406ahrens
438fa9e406ahrens	if (prev)
439fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
440fa9e406ahrens	else
441fa9e406ahrens		return (avl_first(&spa_namespace_avl));
442fa9e406ahrens}
443fa9e406ahrens
444fa9e406ahrens/*
445fa9e406ahrens * ==========================================================================
446fa9e406ahrens * SPA refcount functions
447fa9e406ahrens * ==========================================================================
448fa9e406ahrens */
449fa9e406ahrens
450fa9e406ahrens/*
451fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
452fa9e406ahrens * have the namespace lock held.
453fa9e406ahrens */
454fa9e406ahrensvoid
455fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
456fa9e406ahrens{
457088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
458fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
459fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
460fa9e406ahrens}
461fa9e406ahrens
462fa9e406ahrens/*
463fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
464fa9e406ahrens * have the namespace lock held.
465fa9e406ahrens */
466fa9e406ahrensvoid
467fa9e406ahrensspa_close(spa_t *spa, void *tag)
468fa9e406ahrens{
469088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
470fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
471fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
472fa9e406ahrens}
473fa9e406ahrens
474fa9e406ahrens/*
475fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
476088f389ahrens * spa_namespace_lock held.  We really compare against spa_minref, which is the
477fa9e406ahrens * number of references acquired when opening a pool
478fa9e406ahrens */
479fa9e406ahrensboolean_t
480fa9e406ahrensspa_refcount_zero(spa_t *spa)
481fa9e406ahrens{
482fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
483fa9e406ahrens
484088f389ahrens	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
485fa9e406ahrens}
486fa9e406ahrens
487fa9e406ahrens/*
488fa9e406ahrens * ==========================================================================
489fa94a07brendan * SPA spare and l2cache tracking
49099653d4eschrock * ==========================================================================
49199653d4eschrock */
49299653d4eschrock
49399653d4eschrock/*
494fa94a07brendan * Hot spares and cache devices are tracked using the same code below,
495fa94a07brendan * for 'auxiliary' devices.
496fa94a07brendan */
497fa94a07brendan
498fa94a07brendantypedef struct spa_aux {
499fa94a07brendan	uint64_t	aux_guid;
500fa94a07brendan	uint64_t	aux_pool;
501fa94a07brendan	avl_node_t	aux_avl;
502fa94a07brendan	int		aux_count;
503fa94a07brendan} spa_aux_t;
504fa94a07brendan
505fa94a07brendanstatic int
506fa94a07brendanspa_aux_compare(const void *a, const void *b)
507fa94a07brendan{
508fa94a07brendan	const spa_aux_t *sa = a;
509fa94a07brendan	const spa_aux_t *sb = b;
510fa94a07brendan
511fa94a07brendan	if (sa->aux_guid < sb->aux_guid)
512fa94a07brendan		return (-1);
513fa94a07brendan	else if (sa->aux_guid > sb->aux_guid)
514fa94a07brendan		return (1);
515fa94a07brendan	else
516fa94a07brendan		return (0);
517fa94a07brendan}
518fa94a07brendan
519fa94a07brendanvoid
520fa94a07brendanspa_aux_add(vdev_t *vd, avl_tree_t *avl)
521fa94a07brendan{
522fa94a07brendan	avl_index_t where;
523fa94a07brendan	spa_aux_t search;
524fa94a07brendan	spa_aux_t *aux;
525fa94a07brendan
526fa94a07brendan	search.aux_guid = vd->vdev_guid;
527fa94a07brendan	if ((aux = avl_find(avl, &search, &where)) != NULL) {
528fa94a07brendan		aux->aux_count++;
529fa94a07brendan	} else {
530fa94a07brendan		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
531fa94a07brendan		aux->aux_guid = vd->vdev_guid;
532fa94a07brendan		aux->aux_count = 1;
533fa94a07brendan		avl_insert(avl, aux, where);
534fa94a07brendan	}
535fa94a07brendan}
536fa94a07brendan
537fa94a07brendanvoid
538fa94a07brendanspa_aux_remove(vdev_t *vd, avl_tree_t *avl)
539fa94a07brendan{
540fa94a07brendan	spa_aux_t search;
541fa94a07brendan	spa_aux_t *aux;
542fa94a07brendan	avl_index_t where;
543fa94a07brendan
544fa94a07brendan	search.aux_guid = vd->vdev_guid;
545fa94a07brendan	aux = avl_find(avl, &search, &where);
546fa94a07brendan
547fa94a07brendan	ASSERT(aux != NULL);
548fa94a07brendan
549fa94a07brendan	if (--aux->aux_count == 0) {
550fa94a07brendan		avl_remove(avl, aux);
551fa94a07brendan		kmem_free(aux, sizeof (spa_aux_t));
552fa94a07brendan	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
553fa94a07brendan		aux->aux_pool = 0ULL;
554fa94a07brendan	}
555fa94a07brendan}
556fa94a07brendan
557fa94a07brendanboolean_t
55889a89ebllingspa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
559fa94a07brendan{
560fa94a07brendan	spa_aux_t search, *found;
561fa94a07brendan
562fa94a07brendan	search.aux_guid = guid;
56389a89eblling	found = avl_find(avl, &search, NULL);
564fa94a07brendan
565fa94a07brendan	if (pool) {
566fa94a07brendan		if (found)
567fa94a07brendan			*pool = found->aux_pool;
568fa94a07brendan		else
569fa94a07brendan			*pool = 0ULL;
570fa94a07brendan	}
571fa94a07brendan
57289a89eblling	if (refcnt) {
57389a89eblling		if (found)
57489a89eblling			*refcnt = found->aux_count;
57589a89eblling		else
57689a89eblling			*refcnt = 0;
57789a89eblling	}
57889a89eblling
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
64589a89ebllingspa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
64699653d4eschrock{
647fa94a07brendan	boolean_t found;
64899653d4eschrock
64999653d4eschrock	mutex_enter(&spa_spare_lock);
65089a89eblling	found = spa_aux_exists(guid, pool, refcnt, &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);
70389a89eblling	found = spa_aux_exists(guid, pool, NULL, &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	spa_config_enter(spa, RW_WRITER, spa);
741fa9e406ahrens
742fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
743fa9e406ahrens}
744fa9e406ahrens
745fa9e406ahrens/*
746fa9e406ahrens * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
747fa9e406ahrens * locking of spa_vdev_enter(), we also want make sure the transactions have
748fa9e406ahrens * synced to disk, and then update the global configuration cache with the new
749fa9e406ahrens * information.
750fa9e406ahrens */
751fa9e406ahrensint
752fa9e406ahrensspa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
753fa9e406ahrens{
7540e34b6abonwick	int config_changed = B_FALSE;
755ea8dc4beschrock
7560373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
7570e34b6abonwick
7580e34b6abonwick	/*
7590e34b6abonwick	 * Reassess the DTLs.
7600e34b6abonwick	 */
7610373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
7620e34b6abonwick
7630e34b6abonwick	/*
7640373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
7650e34b6abonwick	 */
7660e34b6abonwick	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
767088f389ahrens		dsl_pool_scrub_restart(spa->spa_dsl_pool);
7680e34b6abonwick		config_changed = B_TRUE;
7690e34b6abonwick	}
770ea8dc4beschrock
771ea8dc4beschrock	spa_config_exit(spa, spa);
772fa9e406ahrens
773ea8dc4beschrock	/*
774fa9e406ahrens	 * Note: this txg_wait_synced() is important because it ensures
775fa9e406ahrens	 * that there won't be more than one config change per txg.
776fa9e406ahrens	 * This allows us to use the txg as the generation number.
777fa9e406ahrens	 */
778fa9e406ahrens	if (error == 0)
779fa9e406ahrens		txg_wait_synced(spa->spa_dsl_pool, txg);
780fa9e406ahrens
781fa9e406ahrens	if (vd != NULL) {
782fa9e406ahrens		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
783fa9e406ahrens		vdev_free(vd);
784fa9e406ahrens	}
785fa9e406ahrens
786fa9e406ahrens	/*
7870e34b6abonwick	 * If the config changed, update the config cache.
788fa9e406ahrens	 */
7890e34b6abonwick	if (config_changed)
790c5904d1eschrock		spa_config_sync(spa, B_FALSE, B_TRUE);
791ea8dc4beschrock
792ea8dc4beschrock	mutex_exit(&spa_namespace_lock);
793fa9e406ahrens
794fa9e406ahrens	return (error);
795fa9e406ahrens}
796fa9e406ahrens
797fa9e406ahrens/*
798fa9e406ahrens * ==========================================================================
799fa9e406ahrens * Miscellaneous functions
800fa9e406ahrens * ==========================================================================
801fa9e406ahrens */
802fa9e406ahrens
803fa9e406ahrens/*
804fa9e406ahrens * Rename a spa_t.
805fa9e406ahrens */
806fa9e406ahrensint
807fa9e406ahrensspa_rename(const char *name, const char *newname)
808fa9e406ahrens{
809fa9e406ahrens	spa_t *spa;
810fa9e406ahrens	int err;
811fa9e406ahrens
812fa9e406ahrens	/*
813fa9e406ahrens	 * Lookup the spa_t and grab the config lock for writing.  We need to
814fa9e406ahrens	 * actually open the pool so that we can sync out the necessary labels.
815fa9e406ahrens	 * It's OK to call spa_open() with the namespace lock held because we
816ea8dc4beschrock	 * allow recursive calls for other reasons.
817fa9e406ahrens	 */
818fa9e406ahrens	mutex_enter(&spa_namespace_lock);
819fa9e406ahrens	if ((err = spa_open(name, &spa, FTAG)) != 0) {
820fa9e406ahrens		mutex_exit(&spa_namespace_lock);
821fa9e406ahrens		return (err);
822fa9e406ahrens	}
823fa9e406ahrens
824ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
825fa9e406ahrens
826fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
827fa9e406ahrens	spa_strfree(spa->spa_name);
828fa9e406ahrens	spa->spa_name = spa_strdup(newname);
829fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
830fa9e406ahrens
831fa9e406ahrens	/*
832fa9e406ahrens	 * Sync all labels to disk with the new names by marking the root vdev
833fa9e406ahrens	 * dirty and waiting for it to sync.  It will pick up the new pool name
834fa9e406ahrens	 * during the sync.
835fa9e406ahrens	 */
836fa9e406ahrens	vdev_config_dirty(spa->spa_root_vdev);
837fa9e406ahrens
838ea8dc4beschrock	spa_config_exit(spa, FTAG);
839fa9e406ahrens
8400373e76bonwick	txg_wait_synced(spa->spa_dsl_pool, 0);
841fa9e406ahrens
842fa9e406ahrens	/*
843fa9e406ahrens	 * Sync the updated config cache.
844fa9e406ahrens	 */
845c5904d1eschrock	spa_config_sync(spa, B_FALSE, B_TRUE);
846fa9e406ahrens
847fa9e406ahrens	spa_close(spa, FTAG);
848fa9e406ahrens
849fa9e406ahrens	mutex_exit(&spa_namespace_lock);
850fa9e406ahrens
851fa9e406ahrens	return (0);
852fa9e406ahrens}
853fa9e406ahrens
854fa9e406ahrens
855fa9e406ahrens/*
856