spa_misc.c revision e05725b117836db173257fae43fb0746eb857fb5
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 *
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;
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_cv, NULL, CV_DEFAULT, NULL);
337c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
338c25056dgw
339fa9e406ahrens	spa->spa_name = spa_strdup(name);
340fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
341fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
3420373e76bonwick	spa->spa_final_txg = UINT64_MAX;
343fa9e406ahrens
344fa9e406ahrens	refcount_create(&spa->spa_refcount);
345e05725bbonwick	spa_config_lock_init(&spa->spa_config_lock);
346fa9e406ahrens
347fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
348fa9e406ahrens
3490a4e951gw	mutex_init(&spa->spa_zio_lock, NULL, MUTEX_DEFAULT, NULL);
3500a4e951gw
3510373e76bonwick	/*
3520373e76bonwick	 * Set the alternate root, if there is one.
3530373e76bonwick	 */
3540373e76bonwick	if (altroot) {
3550373e76bonwick		spa->spa_root = spa_strdup(altroot);
3560373e76bonwick		spa_active_count++;
3570373e76bonwick	}
3580373e76bonwick
359fa9e406ahrens	return (spa);
360fa9e406ahrens}
361fa9e406ahrens
362fa9e406ahrens/*
363fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
364fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
365fa9e406ahrens * deactivated.
366fa9e406ahrens */
367fa9e406ahrensvoid
368fa9e406ahrensspa_remove(spa_t *spa)
369fa9e406ahrens{
370fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
371fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
372fa9e406ahrens	ASSERT(spa->spa_scrub_thread == NULL);
373fa9e406ahrens
374fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
375fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
376fa9e406ahrens
3770373e76bonwick	if (spa->spa_root) {
378fa9e406ahrens		spa_strfree(spa->spa_root);
3790373e76bonwick		spa_active_count--;
3800373e76bonwick	}
381fa9e406ahrens
382fa9e406ahrens	if (spa->spa_name)
383fa9e406ahrens		spa_strfree(spa->spa_name);
384fa9e406ahrens
3852f8aaabeschrock	if (spa->spa_config_dir)
3862f8aaabeschrock		spa_strfree(spa->spa_config_dir);
3872f8aaabeschrock	if (spa->spa_config_file)
3882f8aaabeschrock		spa_strfree(spa->spa_config_file);
3892f8aaabeschrock
390fa9e406ahrens	spa_config_set(spa, NULL);
391fa9e406ahrens
392fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
39391ebeefahrens
394e05725bbonwick	spa_config_lock_destroy(&spa->spa_config_lock);
395fa9e406ahrens
396c25056dgw	rw_destroy(&spa->spa_traverse_lock);
397c25056dgw
398c25056dgw	cv_destroy(&spa->spa_async_cv);
399c25056dgw	cv_destroy(&spa->spa_scrub_cv);
400c25056dgw	cv_destroy(&spa->spa_scrub_io_cv);
401c25056dgw
402c25056dgw	mutex_destroy(&spa->spa_uberblock_lock);
4035ad8204nd	mutex_destroy(&spa->spa_async_lock);
404c25056dgw	mutex_destroy(&spa->spa_config_cache_lock);
405c25056dgw	mutex_destroy(&spa->spa_scrub_lock);
406c25056dgw	mutex_destroy(&spa->spa_errlog_lock);
407c25056dgw	mutex_destroy(&spa->spa_errlist_lock);
408c25056dgw	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
40906eeb2aek	mutex_destroy(&spa->spa_history_lock);
410b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
4110a4e951gw	mutex_destroy(&spa->spa_zio_lock);
4125ad8204nd
413fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
414fa9e406ahrens}
415fa9e406ahrens
416fa9e406ahrens/*
417fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
418fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
419fa9e406ahrens */
420fa9e406ahrensspa_t *
421fa9e406ahrensspa_next(spa_t *prev)
422fa9e406ahrens{
423fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
424fa9e406ahrens
425fa9e406ahrens	if (prev)
426fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
427fa9e406ahrens	else
428fa9e406ahrens		return (avl_first(&spa_namespace_avl));
429fa9e406ahrens}
430fa9e406ahrens
431fa9e406ahrens/*
432fa9e406ahrens * ==========================================================================
433fa9e406ahrens * SPA refcount functions
434fa9e406ahrens * ==========================================================================
435fa9e406ahrens */
436fa9e406ahrens
437fa9e406ahrens/*
438fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
439fa9e406ahrens * have the namespace lock held.
440fa9e406ahrens */
441fa9e406ahrensvoid
442fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
443fa9e406ahrens{
444fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
445fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
446fa9e406ahrens
447fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
448fa9e406ahrens}
449fa9e406ahrens
450fa9e406ahrens/*
451fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
452fa9e406ahrens * have the namespace lock held.
453fa9e406ahrens */
454fa9e406ahrensvoid
455fa9e406ahrensspa_close(spa_t *spa, void *tag)
456fa9e406ahrens{
457fa9e406ahrens	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
458fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
459fa9e406ahrens
460fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
461fa9e406ahrens}
462fa9e406ahrens
463fa9e406ahrens/*
464fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
465fa9e406ahrens * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
466fa9e406ahrens * number of references acquired when opening a pool
467fa9e406ahrens */
468fa9e406ahrensboolean_t
469fa9e406ahrensspa_refcount_zero(spa_t *spa)
470fa9e406ahrens{
471fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
472fa9e406ahrens
473fa9e406ahrens	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
474fa9e406ahrens}
475fa9e406ahrens
476fa9e406ahrens/*
477fa9e406ahrens * ==========================================================================
478fa94a07brendan * SPA spare and l2cache tracking
47999653d4eschrock * ==========================================================================
48099653d4eschrock */
48199653d4eschrock
48299653d4eschrock/*
483fa94a07brendan * Hot spares and cache devices are tracked using the same code below,
484fa94a07brendan * for 'auxiliary' devices.
485fa94a07brendan */
486fa94a07brendan
487fa94a07brendantypedef struct spa_aux {
488fa94a07brendan	uint64_t	aux_guid;
489fa94a07brendan	uint64_t	aux_pool;
490fa94a07brendan	avl_node_t	aux_avl;
491fa94a07brendan	int		aux_count;
492fa94a07brendan} spa_aux_t;
493fa94a07brendan
494fa94a07brendanstatic int
495fa94a07brendanspa_aux_compare(const void *a, const void *b)
496fa94a07brendan{
497fa94a07brendan	const spa_aux_t *sa = a;
498fa94a07brendan	const spa_aux_t *sb = b;
499fa94a07brendan
500fa94a07brendan	if (sa->aux_guid < sb->aux_guid)
501fa94a07brendan		return (-1);
502fa94a07brendan	else if (sa->aux_guid > sb->aux_guid)
503fa94a07brendan		return (1);
504fa94a07brendan	else
505fa94a07brendan		return (0);
506fa94a07brendan}
507fa94a07brendan
508fa94a07brendanvoid
509fa94a07brendanspa_aux_add(vdev_t *vd, avl_tree_t *avl)
510fa94a07brendan{
511fa94a07brendan	avl_index_t where;
512fa94a07brendan	spa_aux_t search;
513fa94a07brendan	spa_aux_t *aux;
514fa94a07brendan
515fa94a07brendan	search.aux_guid = vd->vdev_guid;
516fa94a07brendan	if ((aux = avl_find(avl, &search, &where)) != NULL) {
517fa94a07brendan		aux->aux_count++;
518fa94a07brendan	} else {
519fa94a07brendan		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
520fa94a07brendan		aux->aux_guid = vd->vdev_guid;
521fa94a07brendan		aux->aux_count = 1;
522fa94a07brendan		avl_insert(avl, aux, where);
523fa94a07brendan	}
524fa94a07brendan}
525fa94a07brendan
526fa94a07brendanvoid
527fa94a07brendanspa_aux_remove(vdev_t *vd, avl_tree_t *avl)
528fa94a07brendan{
529fa94a07brendan	spa_aux_t search;
530fa94a07brendan	spa_aux_t *aux;
531fa94a07brendan	avl_index_t where;
532fa94a07brendan
533fa94a07brendan	search.aux_guid = vd->vdev_guid;
534fa94a07brendan	aux = avl_find(avl, &search, &where);
535fa94a07brendan
536fa94a07brendan	ASSERT(aux != NULL);
537fa94a07brendan
538fa94a07brendan	if (--aux->aux_count == 0) {
539fa94a07brendan		avl_remove(avl, aux);
540fa94a07brendan		kmem_free(aux, sizeof (spa_aux_t));
541fa94a07brendan	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
542fa94a07brendan		aux->aux_pool = 0ULL;
543fa94a07brendan	}
544fa94a07brendan}
545fa94a07brendan
546fa94a07brendanboolean_t
547fa94a07brendanspa_aux_exists(uint64_t guid, uint64_t *pool, avl_tree_t *avl)
548fa94a07brendan{
549fa94a07brendan	spa_aux_t search, *found;
550fa94a07brendan	avl_index_t where;
551fa94a07brendan
552fa94a07brendan	search.aux_guid = guid;
553fa94a07brendan	found = avl_find(avl, &search, &where);
554fa94a07brendan
555fa94a07brendan	if (pool) {
556fa94a07brendan		if (found)
557fa94a07brendan			*pool = found->aux_pool;
558fa94a07brendan		else
559fa94a07brendan			*pool = 0ULL;
560fa94a07brendan	}
561fa94a07brendan
562fa94a07brendan	return (found != NULL);
563fa94a07brendan}
564fa94a07brendan
565fa94a07brendanvoid
566fa94a07brendanspa_aux_activate(vdev_t *vd, avl_tree_t *avl)
567fa94a07brendan{
568fa94a07brendan	spa_aux_t search, *found;
569fa94a07brendan	avl_index_t where;
570fa94a07brendan
571fa94a07brendan	search.aux_guid = vd->vdev_guid;
572fa94a07brendan	found = avl_find(avl, &search, &where);
573fa94a07brendan	ASSERT(found != NULL);
574fa94a07brendan	ASSERT(found->aux_pool == 0ULL);
575fa94a07brendan
576fa94a07brendan	found->aux_pool = spa_guid(vd->vdev_spa);
577fa94a07brendan}
578fa94a07brendan
579fa94a07brendan/*
58039c2341eschrock * Spares are tracked globally due to the following constraints:
58139c2341eschrock *
58239c2341eschrock * 	- A spare may be part of multiple pools.
58339c2341eschrock * 	- A spare may be added to a pool even if it's actively in use within
58439c2341eschrock *	  another pool.
58539c2341eschrock * 	- A spare in use in any pool can only be the source of a replacement if
58639c2341eschrock *	  the target is a spare in the same pool.
58739c2341eschrock *
58839c2341eschrock * We keep track of all spares on the system through the use of a reference
58939c2341eschrock * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
59039c2341eschrock * spare, then we bump the reference count in the AVL tree.  In addition, we set
59139c2341eschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
59239c2341eschrock * inactive).  When a spare is made active (used to replace a device in the
59339c2341eschrock * pool), we also keep track of which pool its been made a part of.
59439c2341eschrock *
59539c2341eschrock * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
59639c2341eschrock * called under the spa_namespace lock as part of vdev reconfiguration.  The
59739c2341eschrock * separate spare lock exists for the status query path, which does not need to
59839c2341eschrock * be completely consistent with respect to other vdev configuration changes.
59999653d4eschrock */
60039c2341eschrock
60199653d4eschrockstatic int
60299653d4eschrockspa_spare_compare(const void *a, const void *b)
60399653d4eschrock{
604fa94a07brendan	return (spa_aux_compare(a, b));
60599653d4eschrock}
60699653d4eschrock
60799653d4eschrockvoid
60839c2341eschrockspa_spare_add(vdev_t *vd)
60999653d4eschrock{
61099653d4eschrock	mutex_enter(&spa_spare_lock);
61139c2341eschrock	ASSERT(!vd->vdev_isspare);
612fa94a07brendan	spa_aux_add(vd, &spa_spare_avl);
61339c2341eschrock	vd->vdev_isspare = B_TRUE;
61499653d4eschrock	mutex_exit(&spa_spare_lock);
61599653d4eschrock}
61699653d4eschrock
61799653d4eschrockvoid
61839c2341eschrockspa_spare_remove(vdev_t *vd)
61999653d4eschrock{
62099653d4eschrock	mutex_enter(&spa_spare_lock);
62139c2341eschrock	ASSERT(vd->vdev_isspare);
622fa94a07brendan	spa_aux_remove(vd, &spa_spare_avl);
62339c2341eschrock	vd->vdev_isspare = B_FALSE;
62499653d4eschrock	mutex_exit(&spa_spare_lock);
62599653d4eschrock}
62699653d4eschrock
62799653d4eschrockboolean_t
62839c2341eschrockspa_spare_exists(uint64_t guid, uint64_t *pool)
62999653d4eschrock{
630fa94a07brendan	boolean_t found;
63199653d4eschrock
63299653d4eschrock	mutex_enter(&spa_spare_lock);
633fa94a07brendan	found = spa_aux_exists(guid, pool, &spa_spare_avl);
63499653d4eschrock	mutex_exit(&spa_spare_lock);
63599653d4eschrock
636fa94a07brendan	return (found);
63739c2341eschrock}
63839c2341eschrock
63939c2341eschrockvoid
64039c2341eschrockspa_spare_activate(vdev_t *vd)
64139c2341eschrock{
64239c2341eschrock	mutex_enter(&spa_spare_lock);
64339c2341eschrock	ASSERT(vd->vdev_isspare);
644fa94a07brendan	spa_aux_activate(vd, &spa_spare_avl);
645fa94a07brendan	mutex_exit(&spa_spare_lock);
646fa94a07brendan}
64739c2341eschrock
648fa94a07brendan/*
649fa94a07brendan * Level 2 ARC devices are tracked globally for the same reasons as spares.
650fa94a07brendan * Cache devices currently only support one pool per cache device, and so
651fa94a07brendan * for these devices the aux reference count is currently unused beyond 1.
652fa94a07brendan */
65339c2341eschrock
654fa94a07brendanstatic int
655fa94a07brendanspa_l2cache_compare(const void *a, const void *b)
656fa94a07brendan{
657fa94a07brendan	return (spa_aux_compare(a, b));
658fa94a07brendan}
659fa94a07brendan
660fa94a07brendanvoid
661fa94a07brendanspa_l2cache_add(vdev_t *vd)
662fa94a07brendan{
663fa94a07brendan	mutex_enter(&spa_l2cache_lock);
664fa94a07brendan	ASSERT(!vd->vdev_isl2cache);
665fa94a07brendan	spa_aux_add(vd, &spa_l2cache_avl);
666fa94a07brendan	vd->vdev_isl2cache = B_TRUE;
667fa94a07brendan	mutex_exit(&spa_l2cache_lock);
668fa94a07brendan}
669fa94a07brendan
670fa94a07brendanvoid
671fa94a07brendanspa_l2cache_remove(vdev_t *vd)
672fa94a07brendan{
673fa94a07brendan	mutex_enter(&spa_l2cache_lock);
674fa94a07brendan	ASSERT(vd->vdev_isl2cache);
675fa94a07brendan	spa_aux_remove(vd, &spa_l2cache_avl);
676fa94a07brendan	vd->vdev_isl2cache = B_FALSE;
677fa94a07brendan	mutex_exit(&spa_l2cache_lock);
678fa94a07brendan}
679fa94a07brendan
680fa94a07brendanboolean_t
681fa94a07brendanspa_l2cache_exists(uint64_t guid, uint64_t *pool)
682fa94a07brendan{
683fa94a07brendan	boolean_t found;
684fa94a07brendan
685fa94a07brendan	mutex_enter(&spa_l2cache_lock);
686fa94a07brendan	found = spa_aux_exists(guid, pool, &spa_l2cache_avl);
687fa94a07brendan	mutex_exit(&spa_l2cache_lock);
688fa94a07brendan
689fa94a07brendan	return (found);
690fa94a07brendan}
691fa94a07brendan
692fa94a07brendanvoid
693fa94a07brendanspa_l2cache_activate(vdev_t *vd)
694fa94a07brendan{
695fa94a07brendan	mutex_enter(&spa_l2cache_lock);
696fa94a07brendan	ASSERT(vd->vdev_isl2cache);
697fa94a07brendan	spa_aux_activate(vd, &spa_l2cache_avl);
698fa94a07brendan	mutex_exit(&spa_l2cache_lock);
699fa94a07brendan}
700fa94a07brendan
701fa94a07brendanvoid
702fa94a07brendanspa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
703fa94a07brendan{
704fa94a07brendan	vdev_space_update(vd, space, alloc, B_FALSE);
70599653d4eschrock}
70699653d4eschrock
70799653d4eschrock/*
70899653d4eschrock * ==========================================================================
709fa9e406ahrens * SPA vdev locking
710fa9e406ahrens * ==========================================================================
711fa9e406ahrens */
712fa9e406ahrens
713fa9e406ahrens/*
714ea8dc4beschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
715ea8dc4beschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
716fa9e406ahrens * It returns the next transaction group for the spa_t.
717fa9e406ahrens */
718fa9e406ahrensuint64_t
719fa9e406ahrensspa_vdev_enter(spa_t *spa)
720fa9e406ahrens{
7213d7072feschrock	mutex_enter(&spa_namespace_lock);
7223d7072feschrock
723ea8dc4beschrock	/*
7243d7072feschrock	 * Suspend scrub activity while we mess with the config.  We must do
7253d7072feschrock	 * this after acquiring the namespace lock to avoid a 3-way deadlock
7263d7072feschrock	 * with spa_scrub_stop() and the scrub thread.
727ea8dc4beschrock	 */
728ea8dc4beschrock	spa_scrub_suspend(spa);
729fa9e406ahrens
730ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, spa);
731fa9e406ahrens
732fa9e406ahrens	return (spa_last_synced_txg(spa) + 1);
733fa9e406ahrens}
734fa9e406ahrens
735fa9e406ahrens/*
736fa9e406ahrens * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
737fa9e406ahrens * locking of spa_vdev_enter(), we also want make sure the transactions have
738fa9e406ahrens * synced to disk, and then update the global configuration cache with the new
739fa9e406ahrens * information.
740fa9e406ahrens */
741fa9e406ahrensint
742fa9e406ahrensspa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
743fa9e406ahrens{
7440e34b6abonwick	int config_changed = B_FALSE;
745ea8dc4beschrock
7460373e76bonwick	ASSERT(txg > spa_last_synced_txg(spa));
7470e34b6abonwick
7480e34b6abonwick	/*
7490e34b6abonwick	 * Reassess the DTLs.
7500e34b6abonwick	 */
7510373e76bonwick	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
7520e34b6abonwick
7530e34b6abonwick	/*
7540373e76bonwick	 * If the config changed, notify the scrub thread that it must restart.
7550e34b6abonwick	 */
7560e34b6abonwick	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
7570e34b6abonwick		config_changed = B_TRUE;
7580373e76bonwick		spa_scrub_restart(spa, txg);
7590e34b6abonwick	}
760ea8dc4beschrock
761ea8dc4beschrock	spa_config_exit(spa, spa);
762fa9e406ahrens
763ea8dc4beschrock	/*
7645dabedebonwick	 * Allow scrubbing to resume.
765ea8dc4beschrock	 */
766ea8dc4beschrock	spa_scrub_resume(spa);
767fa9e406ahrens
768fa9e406ahrens	/*
769fa9e406ahrens	 * Note: this txg_wait_synced() is important because it ensures
770fa9e406ahrens	 * that there won't be more than one config change per txg.
771fa9e406ahrens	 * This allows us to use the txg as the generation number.
772fa9e406ahrens	 */
773fa9e406ahrens	if (error == 0)
774fa9e406ahrens		txg_wait_synced(spa->spa_dsl_pool, txg);
775fa9e406ahrens
776fa9e406ahrens	if (vd != NULL) {
777fa9e406ahrens		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
778fa9e406ahrens		vdev_free(vd);
779fa9e406ahrens	}
780fa9e406ahrens
781fa9e406ahrens	/*
7820e34b6abonwick	 * If the config changed, update the config cache.
783fa9e406ahrens	 */
7840e34b6abonwick	if (config_changed)
785fa9e406ahrens		spa_config_sync();
786ea8dc4beschrock
787ea8dc4beschrock	mutex_exit(&spa_namespace_lock);
788fa9e406ahrens
789fa9e406ahrens	return (error);
790fa9e406ahrens}
791fa9e406ahrens
792fa9e406ahrens/*
793fa9e406ahrens * ==========================================================================
794fa9e406ahrens * Miscellaneous functions
795fa9e406ahrens * ==========================================================================
796fa9e406ahrens */
797fa9e406ahrens
798fa9e406ahrens/*
799fa9e406ahrens * Rename a spa_t.
800fa9e406ahrens */
801fa9e406ahrensint
802fa9e406ahrensspa_rename(const char *name, const char *newname)
803fa9e406ahrens{
804fa9e406ahrens	spa_t *spa;
805fa9e406ahrens	int err;
806fa9e406ahrens
807fa9e406ahrens	/*
808fa9e406ahrens	 * Lookup the spa_t and grab the config lock for writing.  We need to
809fa9e406ahrens	 * actually open the pool so that we can sync out the necessary labels.
810fa9e406ahrens	 * It's OK to call spa_open() with the namespace lock held because we
811ea8dc4beschrock	 * allow recursive calls for other reasons.
812fa9e406ahrens	 */
813fa9e406ahrens	mutex_enter(&spa_namespace_lock);
814fa9e406ahrens	if ((err = spa_open(name, &spa, FTAG)) != 0) {
815fa9e406ahrens		mutex_exit(&spa_namespace_lock);
816fa9e406ahrens		return (err);
817fa9e406ahrens	}
818fa9e406ahrens
819ea8dc4beschrock	spa_config_enter(spa, RW_WRITER, FTAG);
820fa9e406ahrens
821fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
822fa9e406ahrens	spa_strfree(spa->spa_name);
823fa9e406ahrens	spa->spa_name = spa_strdup(newname);
824fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
825fa9e406ahrens
826fa9e406ahrens	/*
827fa9e406ahrens	 * Sync all labels to disk with the new names by marking the root vdev
828fa9e406ahrens	 * dirty and waiting for it to sync.  It will pick up the new pool name
829fa9e406ahrens	 * during the sync.
830fa9e406ahrens	 */
831fa9e406ahrens	vdev_config_dirty(spa->spa_root_vdev);
832fa9e406ahrens
833ea8dc4beschrock	spa_config_exit(spa, FTAG);
834fa9e406ahrens
8350373e76bonwick	txg_wait_synced(spa->spa_dsl_pool, 0);
836fa9e406ahrens
837fa9e406ahrens	/*
838fa9e406ahrens	 * Sync the updated config cache.
839fa9e406ahrens	 */
840fa9e406ahrens	spa_config_sync();
841fa9e406ahrens
842fa9e406ahrens	spa_close(spa, FTAG);
843fa9e406ahrens
844fa9e406ahrens	mutex_exit(&spa_namespace_lock);
845fa9e406ahrens
846fa9e406ahrens	return (0);
847fa9e406ahrens}
848fa9e406ahrens
849fa9e406ahrens
850fa9e406ahrens/*
851fa9e406ahrens * Determine whether a pool with given pool_guid exists.  If device_guid is
852fa9e406ahrens * non-zero, determine whether the pool exists *and* contains a device with the
853fa9e406ahrens * specified device_guid.
854fa9e406ahrens */
855fa9e406ahrensboolean_t
856fa9e406ahrensspa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
857fa9e406ahrens{
858fa9e406ahrens	spa_t *spa;
859fa9e406ahrens	avl_tree_t *t = &spa_namespace_avl;
860fa9e406ahrens
861