spa_misc.c revision 7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9e
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
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 */
229842588George Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23be6fd75Matthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
24e9103aaGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25fa9e406ahrens */
27fa9e406ahrens#include <sys/zfs_context.h>
28fa9e406ahrens#include <sys/spa_impl.h>
29283b846George.Wilson#include <sys/spa_boot.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>
463f9d6adLin Ling#include <sys/dsl_scan.h>
47fa9e406ahrens#include <sys/fs/zfs.h>
486ce0521perrin#include <sys/metaslab_impl.h>
49e14bb32Jeff Bonwick#include <sys/arc.h>
50485bbbfGeorge Wilson#include <sys/ddt.h>
5191ebeefahrens#include "zfs_prop.h"
52ad135b5Christopher Siden#include "zfeature_common.h"
55fa9e406ahrens * SPA locking
56fa9e406ahrens *
57fa9e406ahrens * There are four basic locks for managing spa_t structures:
58fa9e406ahrens *
59fa9e406ahrens * spa_namespace_lock (global mutex)
60fa9e406ahrens *
6144cd46cbillm *	This lock must be acquired to do any of the following:
62fa9e406ahrens *
6344cd46cbillm *		- Lookup a spa_t by name
6444cd46cbillm *		- Add or remove a spa_t from the namespace
6544cd46cbillm *		- Increase spa_refcount from non-zero
6644cd46cbillm *		- Check if spa_refcount is zero
6744cd46cbillm *		- Rename a spa_t
68ea8dc4beschrock *		- add/remove/attach/detach devices
6944cd46cbillm *		- Held for the duration of create/destroy/import/export
70fa9e406ahrens *
7144cd46cbillm *	It does not need to handle recursion.  A create or destroy may
7244cd46cbillm *	reference objects (files or zvols) in other pools, but by
7344cd46cbillm *	definition they must have an existing reference, and will never need
7444cd46cbillm *	to lookup a spa_t by name.
75fa9e406ahrens *
76fa9e406ahrens * spa_refcount (per-spa refcount_t protected by mutex)
77fa9e406ahrens *
7844cd46cbillm *	This reference count keep track of any active users of the spa_t.  The
7944cd46cbillm *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8044cd46cbillm *	the refcount is never really 'zero' - opening a pool implicitly keeps
81088f389ahrens *	some references in the DMU.  Internally we check against spa_minref, but
8244cd46cbillm *	present the image of a zero/non-zero value to consumers.
83fa9e406ahrens *
84e14bb32Jeff Bonwick * spa_config_lock[] (per-spa array of rwlocks)
85fa9e406ahrens *
8691ebeefahrens *	This protects the spa_t from config changes, and must be held in
8791ebeefahrens *	the following circumstances:
88fa9e406ahrens *
8944cd46cbillm *		- RW_READER to perform I/O to the spa
9044cd46cbillm *		- RW_WRITER to change the vdev config
91fa9e406ahrens *
92fa9e406ahrens * The locking order is fairly straightforward:
93fa9e406ahrens *
9444cd46cbillm *		spa_namespace_lock	->	spa_refcount
95fa9e406ahrens *
9644cd46cbillm *	The namespace lock must be acquired to increase the refcount from 0
9744cd46cbillm *	or to check if it is zero.
98fa9e406ahrens *
99e14bb32Jeff Bonwick *		spa_refcount		->	spa_config_lock[]
100fa9e406ahrens *
10144cd46cbillm *	There must be at least one valid reference on the spa_t to acquire
10244cd46cbillm *	the config lock.
103fa9e406ahrens *
104e14bb32Jeff Bonwick *		spa_namespace_lock	->	spa_config_lock[]
105fa9e406ahrens *
10644cd46cbillm *	The namespace lock must always be taken before the config lock.
107fa9e406ahrens *
108fa9e406ahrens *
109e14bb32Jeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible.
110fa9e406ahrens *
111e14bb32Jeff Bonwick * The namespace is manipulated using the following functions, all of which
112e14bb32Jeff Bonwick * require the spa_namespace_lock to be held.
113fa9e406ahrens *
11444cd46cbillm *	spa_lookup()		Lookup a spa_t by name.
115fa9e406ahrens *
11644cd46cbillm *	spa_add()		Create a new spa_t in the namespace.
117fa9e406ahrens *
11844cd46cbillm *	spa_remove()		Remove a spa_t from the namespace.  This also
11944cd46cbillm *				frees up any memory associated with the spa_t.
120fa9e406ahrens *
12144cd46cbillm *	spa_next()		Returns the next spa_t in the system, or the
12244cd46cbillm *				first if NULL is passed.
123fa9e406ahrens *
12444cd46cbillm *	spa_evict_all()		Shutdown and remove all spa_t structures in
12544cd46cbillm *				the system.
126fa9e406ahrens *
127ea8dc4beschrock *	spa_guid_exists()	Determine whether a pool/device guid exists.
128fa9e406ahrens *
129fa9e406ahrens * The spa_refcount is manipulated using the following functions:
130fa9e406ahrens *
13144cd46cbillm *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13244cd46cbillm *				called with spa_namespace_lock held if the
13344cd46cbillm *				refcount is currently zero.
134fa9e406ahrens *
13544cd46cbillm *	spa_close()		Remove a reference from the spa_t.  This will
13644cd46cbillm *				not free the spa_t or remove it from the
13744cd46cbillm *				namespace.  No locking is required.
138fa9e406ahrens *
13944cd46cbillm *	spa_refcount_zero()	Returns true if the refcount is currently
14044cd46cbillm *				zero.  Must be called with spa_namespace_lock
14144cd46cbillm *				held.
142fa9e406ahrens *
143e14bb32Jeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows:
145e14bb32Jeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
146e14bb32Jeff Bonwick *
147e14bb32Jeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader.
148e14bb32Jeff Bonwick * To modify the configuration, you must hold all locks as writer.  To modify
149e14bb32Jeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline),
150e14bb32Jeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer.
151e14bb32Jeff Bonwick *
152e14bb32Jeff Bonwick * We use these distinct config locks to avoid recursive lock entry.
153e14bb32Jeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
154e14bb32Jeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps
155e14bb32Jeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
156e14bb32Jeff Bonwick *
157e14bb32Jeff Bonwick * The spa config locks cannot be normal rwlocks because we need the
158e14bb32Jeff Bonwick * ability to hand off ownership.  For example, SCL_ZIO is acquired
159e14bb32Jeff Bonwick * by the issuing thread and later released by an interrupt thread.
160e14bb32Jeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent
161e14bb32Jeff Bonwick * writer (i.e. system administrator) starvation.
162e14bb32Jeff Bonwick *
163e14bb32Jeff Bonwick * The lock acquisition rules are as follows:
164e14bb32Jeff Bonwick *
165e14bb32Jeff Bonwick * SCL_CONFIG
166e14bb32Jeff Bonwick *	Protects changes to the vdev tree topology, such as vdev
167e14bb32Jeff Bonwick *	add/remove/attach/detach.  Protects the dirty config list
168e14bb32Jeff Bonwick *	(spa_config_dirty_list) and the set of spares and l2arc devices.
169e14bb32Jeff Bonwick *
170e14bb32Jeff Bonwick * SCL_STATE
171e14bb32Jeff Bonwick *	Protects changes to pool state and vdev state, such as vdev
172e14bb32Jeff Bonwick *	online/offline/fault/degrade/clear.  Protects the dirty state list
173e14bb32Jeff Bonwick *	(spa_state_dirty_list) and global pool state (spa_state).
174e14bb32Jeff Bonwick *
175e14bb32Jeff Bonwick * SCL_ALLOC
176e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
177e14bb32Jeff Bonwick *	Held as reader by metaslab_alloc() and metaslab_claim().
178e14bb32Jeff Bonwick *
179e14bb32Jeff Bonwick * SCL_ZIO
180e14bb32Jeff Bonwick *	Held by bp-level zios (those which have no io_vd upon entry)
181e14bb32Jeff Bonwick *	to prevent changes to the vdev tree.  The bp-level zio implicitly
182e14bb32Jeff Bonwick *	protects all of its vdev child zios, which do not hold SCL_ZIO.
183e14bb32Jeff Bonwick *
184e14bb32Jeff Bonwick * SCL_FREE
185e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
186e14bb32Jeff Bonwick *	Held as reader by metaslab_free().  SCL_FREE is distinct from
187e14bb32Jeff Bonwick *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
188e14bb32Jeff Bonwick *	blocks in zio_done() while another i/o that holds either
189e14bb32Jeff Bonwick *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
190e14bb32Jeff Bonwick *
191e14bb32Jeff Bonwick * SCL_VDEV
192e14bb32Jeff Bonwick *	Held as reader to prevent changes to the vdev tree during trivial
193b24ab67Jeff Bonwick *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
194e14bb32Jeff Bonwick *	other locks, and lower than all of them, to ensure that it's safe
195e14bb32Jeff Bonwick *	to acquire regardless of caller context.
196e14bb32Jeff Bonwick *
197e14bb32Jeff Bonwick * In addition, the following rules apply:
198e14bb32Jeff Bonwick *
199e14bb32Jeff Bonwick * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
200e14bb32Jeff Bonwick *	The lock ordering is SCL_CONFIG > spa_props_lock.
201e14bb32Jeff Bonwick *
202e14bb32Jeff Bonwick * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
203e14bb32Jeff Bonwick *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
204e14bb32Jeff Bonwick *	or zio_write_phys() -- the caller must ensure that the config cannot
205e14bb32Jeff Bonwick *	cannot change in the interim, and that the vdev cannot be reopened.
206e14bb32Jeff Bonwick *	SCL_STATE as reader suffices for both.
207fa9e406ahrens *
208ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
209fa9e406ahrens *
21044cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
211ea8dc4beschrock *				for writing.
212fa9e406ahrens *
21344cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
21444cd46cbillm *				to complete, sync the updated configs to the
215ea8dc4beschrock *				cache, and release the namespace lock.
216fa9e406ahrens *
217e14bb32Jeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
218e14bb32Jeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
219e14bb32Jeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[].
220e14bb32Jeff Bonwick *
221ad135b5Christopher Siden * spa_rename() is also implemented within this file since it requires
222e14bb32Jeff Bonwick * manipulation of the namespace.
223fa9e406ahrens */
225fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
226fa9e406ahrenskmutex_t spa_namespace_lock;
227fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
2280373e76bonwickstatic int spa_active_count;
229416e0cdekint spa_max_replication_override = SPA_DVAS_PER_BP;
23199653d4eschrockstatic kmutex_t spa_spare_lock;
23239c2341eschrockstatic avl_tree_t spa_spare_avl;
233fa94a07brendanstatic kmutex_t spa_l2cache_lock;
234fa94a07brendanstatic avl_tree_t spa_l2cache_avl;
236fa9e406ahrenskmem_cache_t *spa_buffer_pool;
2378ad4d6dJeff Bonwickint spa_mode_global;
239fa9e406ahrens#ifdef ZFS_DEBUG
2403b2aab1Matthew Ahrens/* Everything except dprintf and spa is on by default in debug builds */
2413b2aab1Matthew Ahrensint zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
243fa9e406ahrensint zfs_flags = 0;
2470125049ahrens * zfs_recover can be set to nonzero to attempt to recover from
2480125049ahrens * otherwise-fatal errors, typically caused by on-disk corruption.  When
2490125049ahrens * set, calls to zfs_panic_recover() will turn into warning messages.
2508b36997Matthew Ahrens * This should only be used as a last resort, as it typically results
2518b36997Matthew Ahrens * in leaked space, or worse.
2520125049ahrens */
2537fd05acMatthew Ahrensboolean_t zfs_recover = B_FALSE;
2547fd05acMatthew Ahrens
2557fd05acMatthew Ahrens/*
2567fd05acMatthew Ahrens * If destroy encounters an EIO while reading metadata (e.g. indirect
2577fd05acMatthew Ahrens * blocks), space referenced by the missing metadata can not be freed.
2587fd05acMatthew Ahrens * Normally this causes the background destroy to become "stalled", as
2597fd05acMatthew Ahrens * it is unable to make forward progress.  While in this stalled state,
2607fd05acMatthew Ahrens * all remaining space to free from the error-encountering filesystem is
2617fd05acMatthew Ahrens * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
2627fd05acMatthew Ahrens * permanently leak the space from indirect blocks that can not be read,
2637fd05acMatthew Ahrens * and continue to free everything else that it can.
2647fd05acMatthew Ahrens *
2657fd05acMatthew Ahrens * The default, "stalling" behavior is useful if the storage partially
2667fd05acMatthew Ahrens * fails (i.e. some but not all i/os fail), and then later recovers.  In
2677fd05acMatthew Ahrens * this case, we will be able to continue pool operations while it is
2687fd05acMatthew Ahrens * partially failed, and when it recovers, we can continue to free the
2697fd05acMatthew Ahrens * space, with no leaks.  However, note that this case is actually
2707fd05acMatthew Ahrens * fairly rare.
2717fd05acMatthew Ahrens *
2727fd05acMatthew Ahrens * Typically pools either (a) fail completely (but perhaps temporarily,
2737fd05acMatthew Ahrens * e.g. a top-level vdev going offline), or (b) have localized,
2747fd05acMatthew Ahrens * permanent errors (e.g. disk returns the wrong data due to bit flip or
2757fd05acMatthew Ahrens * firmware bug).  In case (a), this setting does not matter because the
2767fd05acMatthew Ahrens * pool will be suspended and the sync thread will not be able to make
2777fd05acMatthew Ahrens * forward progress regardless.  In case (b), because the error is
2787fd05acMatthew Ahrens * permanent, the best we can do is leak the minimum amount of space,
2797fd05acMatthew Ahrens * which is what setting this flag will do.  Therefore, it is reasonable
2807fd05acMatthew Ahrens * for this flag to normally be set, but we chose the more conservative
2817fd05acMatthew Ahrens * approach of not setting it, so that there is no possibility of
2827fd05acMatthew Ahrens * leaking space in the "partial temporary" failure case.
2837fd05acMatthew Ahrens */
2847fd05acMatthew Ahrensboolean_t zfs_free_leak_on_eio = B_FALSE;
28669962b5Matthew Ahrens/*
28769962b5Matthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is
28869962b5Matthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the
28969962b5Matthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
29069962b5Matthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that
29169962b5Matthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
29269962b5Matthew Ahrens * in a system panic.
29369962b5Matthew Ahrens */
29469962b5Matthew Ahrensuint64_t zfs_deadman_synctime_ms = 1000000ULL;
29769962b5Matthew Ahrens * Check time in milliseconds. This defines the frequency at which we check
29869962b5Matthew Ahrens * for hung I/O.
299283b846George.Wilson */
30069962b5Matthew Ahrensuint64_t zfs_deadman_checktime_ms = 5000ULL;
303283b846George.Wilson * Override the zfs deadman behavior via /etc/system. By default the
304283b846George.Wilson * deadman is enabled except on VMware and sparc deployments.
305283b846George.Wilson */
306283b846George.Wilsonint zfs_deadman_enabled = -1;
30869962b5Matthew Ahrens/*
30969962b5Matthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which
31069962b5Matthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
31169962b5Matthew Ahrens * times the size; so just assume that.  Add to this the fact that
31269962b5Matthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because
31369962b5Matthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
31469962b5Matthew Ahrens * the worst case is:
31569962b5Matthew Ahrens *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
31669962b5Matthew Ahrens */
31769962b5Matthew Ahrensint spa_asize_inflation = 24;
3207d46dc6Matthew Ahrens * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3217d46dc6Matthew Ahrens * the pool to be consumed.  This ensures that we don't run the pool
3227d46dc6Matthew Ahrens * completely out of space, due to unaccounted changes (e.g. to the MOS).
3237d46dc6Matthew Ahrens * It also limits the worst-case time to allocate space.  If we have
3247d46dc6Matthew Ahrens * less than this amount of free space, most ZPL operations (e.g. write,
3257d46dc6Matthew Ahrens * create) will return ENOSPC.
3267d46dc6Matthew Ahrens *
3277d46dc6Matthew Ahrens * Certain operations (e.g. file removal, most administrative actions) can
3287d46dc6Matthew Ahrens * use half the slop space.  They will only return ENOSPC if less than half
3297d46dc6Matthew Ahrens * the slop space is free.  Typically, once the pool has less than the slop
3307d46dc6Matthew Ahrens * space free, the user will use these operations to free up space in the pool.
3317d46dc6Matthew Ahrens * These are the operations that call dsl_pool_adjustedsize() with the netfree
3327d46dc6Matthew Ahrens * argument set to TRUE.
3337d46dc6Matthew Ahrens *
3347d46dc6Matthew Ahrens * A very restricted set of operations are always permitted, regardless of
3357d46dc6Matthew Ahrens * the amount of free space.  These are the operations that call
3367d46dc6Matthew Ahrens * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy".  If these
3377d46dc6Matthew Ahrens * operations result in a net increase in the amount of space used,
3387d46dc6Matthew Ahrens * it is possible to run the pool completely out of space, causing it to
3397d46dc6Matthew Ahrens * be permanently read-only.
3407d46dc6Matthew Ahrens *
3417d46dc6Matthew Ahrens * See also the comments in zfs_space_check_t.
3427d46dc6Matthew Ahrens */
3437d46dc6Matthew Ahrensint spa_slop_shift = 5;
3447d46dc6Matthew Ahrens
3457d46dc6Matthew Ahrens/*
346fa9e406ahrens * ==========================================================================
347e05725bbonwick * SPA config locking
348e05725bbonwick * ==========================================================================
349e05725bbonwick */
350e05725bbonwickstatic void
351e14bb32Jeff Bonwickspa_config_lock_init(spa_t *spa)
352e14bb32Jeff Bonwick{
353e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
354e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
355e14bb32Jeff Bonwick		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
356e14bb32Jeff Bonwick		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
3573b2aab1Matthew Ahrens		refcount_create_untracked(&scl->scl_count);
358e14bb32Jeff Bonwick		scl->scl_writer = NULL;
359e14bb32Jeff Bonwick		scl->scl_write_wanted = 0;
360e14bb32Jeff Bonwick	}
363e05725bbonwickstatic void
364e14bb32Jeff Bonwickspa_config_lock_destroy(spa_t *spa)
365e14bb32Jeff Bonwick{
366e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
367e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
368e14bb32Jeff Bonwick		mutex_destroy(&scl->scl_lock);
369e14bb32Jeff Bonwick		cv_destroy(&scl->scl_cv);
370e14bb32Jeff Bonwick		refcount_destroy(&scl->scl_count);
371e14bb32Jeff Bonwick		ASSERT(scl->scl_writer == NULL);
372e14bb32Jeff Bonwick		ASSERT(scl->scl_write_wanted == 0);
373e14bb32Jeff Bonwick	}
374e14bb32Jeff Bonwick}
375e14bb32Jeff Bonwick
376e14bb32Jeff Bonwickint
377e14bb32Jeff Bonwickspa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
379e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
380e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
381e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
382e14bb32Jeff Bonwick			continue;
383e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
384e14bb32Jeff Bonwick		if (rw == RW_READER) {
385e14bb32Jeff Bonwick			if (scl->scl_writer || scl->scl_write_wanted) {
386e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
387e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
388e14bb32Jeff Bonwick				return (0);
389e14bb32Jeff Bonwick			}
390e14bb32Jeff Bonwick		} else {
391e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
392e14bb32Jeff Bonwick			if (!refcount_is_zero(&scl->scl_count)) {
393e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
394e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
395e14bb32Jeff Bonwick				return (0);
396e14bb32Jeff Bonwick			}
397e14bb32Jeff Bonwick			scl->scl_writer = curthread;
398e14bb32Jeff Bonwick		}
399e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
400e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
401e14bb32Jeff Bonwick	}
402e14bb32Jeff Bonwick	return (1);
406e14bb32Jeff Bonwickspa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
408f64c0e3Eric Taylor	int wlocks_held = 0;
409f64c0e3Eric Taylor
4103b2aab1Matthew Ahrens	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4113b2aab1Matthew Ahrens
412e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
413e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
414f64c0e3Eric Taylor		if (scl->scl_writer == curthread)
415f64c0e3Eric Taylor			wlocks_held |= (1 << i);
416e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
417e14bb32Jeff Bonwick			continue;
418e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
419e14bb32Jeff Bonwick		if (rw == RW_READER) {
420e14bb32Jeff Bonwick			while (scl->scl_writer || scl->scl_write_wanted) {
421e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
422e14bb32Jeff Bonwick			}
423e14bb32Jeff Bonwick		} else {
424e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
425e14bb32Jeff Bonwick			while (!refcount_is_zero(&scl->scl_count)) {
426e14bb32Jeff Bonwick				scl->scl_write_wanted++;
427e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
428e14bb32Jeff Bonwick				scl->scl_write_wanted--;
429e14bb32Jeff Bonwick			}
430e14bb32Jeff Bonwick			scl->scl_writer = curthread;
431e14bb32Jeff Bonwick		}
432e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
433e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
434e05725bbonwick	}
435f64c0e3Eric Taylor	ASSERT(wlocks_held <= locks);
439e14bb32Jeff Bonwickspa_config_exit(spa_t *spa, int locks, void *tag)
441e14bb32Jeff Bonwick	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
442e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
443e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
444e14bb32Jeff Bonwick			continue;
445e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
446e14bb32Jeff Bonwick		ASSERT(!refcount_is_zero(&scl->scl_count));
447e14bb32Jeff Bonwick		if (refcount_remove(&scl->scl_count, tag) == 0) {
448e14bb32Jeff Bonwick			ASSERT(scl->scl_writer == NULL ||
449e14bb32Jeff Bonwick			    scl->scl_writer == curthread);
450e14bb32Jeff Bonwick			scl->scl_writer = NULL;	/* OK in either case */
451e14bb32Jeff Bonwick			cv_broadcast(&scl->scl_cv);
452e14bb32Jeff Bonwick		}
453e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
454e05725bbonwick	}
457e14bb32Jeff Bonwickint
458e14bb32Jeff Bonwickspa_config_held(spa_t *spa, int locks, krw_t rw)
460e14bb32Jeff Bonwick	int locks_held = 0;
462e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
463e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
464e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
465e14bb32Jeff Bonwick			continue;
466e14bb32Jeff Bonwick		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
467e14bb32Jeff Bonwick		    (rw == RW_WRITER && scl->scl_writer == curthread))
468e14bb32Jeff Bonwick			locks_held |= 1 << i;
469e14bb32Jeff Bonwick	}
470e14bb32Jeff Bonwick
471e14bb32Jeff Bonwick	return (locks_held);
475e05725bbonwick * ==========================================================================
476fa9e406ahrens * SPA namespace functions
477fa9e406ahrens * ==========================================================================
478fa9e406ahrens */
481fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
482fa9e406ahrens * Returns NULL if no matching spa_t is found.
483fa9e406ahrens */
484fa9e406ahrensspa_t *
485fa9e406ahrensspa_lookup(const char *name)
487e14bb32Jeff Bonwick	static spa_t search;	/* spa_t is large; don't allocate on stack */
488e14bb32Jeff Bonwick	spa_t *spa;
489fa9e406ahrens	avl_index_t where;
49040feaa9ahrens	char *cp;
492fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
4943b2aab1Matthew Ahrens	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
4953b2aab1Matthew Ahrens
49640feaa9ahrens	/*
49740feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
49840feaa9ahrens	 * just use that.
49940feaa9ahrens	 */
50078f1710Matthew Ahrens	cp = strpbrk(search.spa_name, "/@#");
5013b2aab1Matthew Ahrens	if (cp != NULL)
50240feaa9ahrens		*cp = '\0';
504fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
506fa9e406ahrens	return (spa);
510283b846George.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
511283b846George.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
512283b846George.Wilson * looking for potentially hung I/Os.
513283b846George.Wilson */
515283b846George.Wilsonspa_deadman(void *arg)
517283b846George.Wilson	spa_t *spa = arg;
5190713e23George Wilson	/*
5200713e23George Wilson	 * Disable the deadman timer if the pool is suspended.
5210713e23George Wilson	 */
5220713e23George Wilson	if (spa_suspended(spa)) {
5230713e23George Wilson		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5240713e23George Wilson		return;
5250713e23George Wilson	}
5260713e23George Wilson
527283b846George.Wilson	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
528283b846George.Wilson	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
529283b846George.Wilson	    ++spa->spa_deadman_calls);
530283b846George.Wilson	if (zfs_deadman_enabled)
531283b846George.Wilson		vdev_deadman(spa->spa_root_vdev);
535fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
536fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
537fa9e406ahrens * exist by calling spa_lookup() first.
538fa9e406ahrens */
539fa9e406ahrensspa_t *
540468c413Tim Haleyspa_add(const char *name, nvlist_t *config, const char *altroot)
542fa9e406ahrens	spa_t *spa;
543c5904d1eschrock	spa_config_dirent_t *dp;
544283b846George.Wilson	cyc_handler_t hdlr;
545283b846George.Wilson	cyc_time_t when;
547fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
549fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
551c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
552c25056dgw	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
55335a5a35Jonathan Adams	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
554c25056dgw	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
55535a5a35Jonathan Adams	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
556c25056dgw	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
55735a5a35Jonathan Adams	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
558a152156Jeff Bonwick	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
559a152156Jeff Bonwick	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
560c3a6601Matthew Ahrens	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
562c25056dgw	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
56335a5a35Jonathan Adams	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
564c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
565e14bb32Jeff Bonwick	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
567b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
568cde58dbMatthew Ahrens		bplist_create(&spa->spa_free_bplist[t]);
569b24ab67Jeff Bonwick
570e14bb32Jeff Bonwick	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
571fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
572fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
5730373e76bonwick	spa->spa_final_txg = UINT64_MAX;
574468c413Tim Haley	spa->spa_load_max_txg = UINT64_MAX;
57535a5a35Jonathan Adams	spa->spa_proc = &p0;
57635a5a35Jonathan Adams	spa->spa_proc_state = SPA_PROC_NONE;
578283b846George.Wilson	hdlr.cyh_func = spa_deadman;
579283b846George.Wilson	hdlr.cyh_arg = spa;
580283b846George.Wilson	hdlr.cyh_level = CY_LOW_LEVEL;
58269962b5Matthew Ahrens	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
584283b846George.Wilson	/*
585283b846George.Wilson	 * This determines how often we need to check for hung I/Os after
586283b846George.Wilson	 * the cyclic has already fired. Since checking for hung I/Os is
587283b846George.Wilson	 * an expensive operation we don't want to check too frequently.
58869962b5Matthew Ahrens	 * Instead wait for 5 seconds before checking again.
589283b846George.Wilson	 */
59069962b5Matthew Ahrens	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
591283b846George.Wilson	when.cyt_when = CY_INFINITY;
592283b846George.Wilson	mutex_enter(&cpu_lock);
593283b846George.Wilson	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
594283b846George.Wilson	mutex_exit(&cpu_lock);
596fa9e406ahrens	refcount_create(&spa->spa_refcount);
597e14bb32Jeff Bonwick	spa_config_lock_init(spa);
599fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
6010373e76bonwick	/*
6020373e76bonwick	 * Set the alternate root, if there is one.
6030373e76bonwick	 */
6040373e76bonwick	if (altroot) {
6050373e76bonwick		spa->spa_root = spa_strdup(altroot);
6060373e76bonwick		spa_active_count++;
6070373e76bonwick	}
609c5904d1eschrock	/*
610c5904d1eschrock	 * Every pool starts with the default cachefile
611c5904d1eschrock	 */
612c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
613c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
615c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
616ef912c8Tim Haley	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
617c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
6194b964adGeorge Wilson	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
6204b964adGeorge Wilson	    KM_SLEEP) == 0);
6214b964adGeorge Wilson
622ad135b5Christopher Siden	if (config != NULL) {
623ad135b5Christopher Siden		nvlist_t *features;
624ad135b5Christopher Siden
625ad135b5Christopher Siden		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
626ad135b5Christopher Siden		    &features) == 0) {
627ad135b5Christopher Siden			VERIFY(nvlist_dup(features, &spa->spa_label_features,
628ad135b5Christopher Siden			    0) == 0);
629ad135b5Christopher Siden		}
630ad135b5Christopher Siden
631468c413Tim Haley		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
632ad135b5Christopher Siden	}
633ad135b5Christopher Siden
634ad135b5Christopher Siden	if (spa->spa_label_features == NULL) {
635ad135b5Christopher Siden		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
636ad135b5Christopher Siden		    KM_SLEEP) == 0);
637ad135b5Christopher Siden	}
638468c413Tim Haley
639c3a6601Matthew Ahrens	spa->spa_iokstat = kstat_create("zfs", 0, name,
640c3a6601Matthew Ahrens	    "disk", KSTAT_TYPE_IO, 1, 0);
641c3a6601Matthew Ahrens	if (spa->spa_iokstat) {
642c3a6601Matthew Ahrens		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
643c3a6601Matthew Ahrens		kstat_install(spa->spa_iokstat);
644c3a6601Matthew Ahrens	}
645c3a6601Matthew Ahrens
6463b2aab1Matthew Ahrens	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
6473b2aab1Matthew Ahrens
64843466aaMax Grossman	/*
64943466aaMax Grossman	 * As a pool is being created, treat all features as disabled by
65043466aaMax Grossman	 * setting SPA_FEATURE_DISABLED for all entries in the feature
65143466aaMax Grossman	 * refcount cache.
65243466aaMax Grossman	 */
65343466aaMax Grossman	for (int i = 0; i < SPA_FEATURES; i++) {
65443466aaMax Grossman		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
65543466aaMax Grossman	}
65643466aaMax Grossman
657fa9e406ahrens	return (spa);
661fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
662fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
663fa9e406ahrens * deactivated.
664fa9e406ahrens */
666fa9e406ahrensspa_remove(spa_t *spa)
668c5904d1eschrock	spa_config_dirent_t *dp;
670fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
671fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
6731195e68Mark J Musante	nvlist_free(spa->spa_config_splitting);
6741195e68Mark J Musante
675fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
676fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
6780373e76bonwick	if (spa->spa_root) {
679fa9e406ahrens		spa_strfree(spa->spa_root);
6800373e76bonwick		spa_active_count--;
6810373e76bonwick	}
683c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
684c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
685c5904d1eschrock		if (dp->scd_path != NULL)
686c5904d1eschrock			spa_strfree(dp->scd_path);
687c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
688c5904d1eschrock	}
690c5904d1eschrock	list_destroy(&spa->spa_config_list);
692ad135b5Christopher Siden	nvlist_free(spa->spa_label_features);
6934b964adGeorge Wilson	nvlist_free(spa->spa_load_info);
694fa9e406ahrens	spa_config_set(spa, NULL);
696283b846George.Wilson	mutex_enter(&cpu_lock);
697283b846George.Wilson	if (spa->spa_deadman_cycid != CYCLIC_NONE)
698283b846George.Wilson		cyclic_remove(spa->spa_deadman_cycid);
699283b846George.Wilson	mutex_exit(&cpu_lock);
700283b846George.Wilson	spa->spa_deadman_cycid = CYCLIC_NONE;
702fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
704e14bb32Jeff Bonwick	spa_config_lock_destroy(spa);
706c3a6601Matthew Ahrens	kstat_delete(spa->spa_iokstat);
707c3a6601Matthew Ahrens	spa->spa_iokstat = NULL;
708c3a6601Matthew Ahrens
709b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
710cde58dbMatthew Ahrens		bplist_destroy(&spa->spa_free_bplist[t]);
711b24ab67Jeff Bonwick
712c25056dgw	cv_destroy(&spa->spa_async_cv);
71335a5a35Jonathan Adams	cv_destroy(&spa->spa_proc_cv);
714c25056dgw	cv_destroy(&spa->spa_scrub_io_cv);
715e14bb32Jeff Bonwick	cv_destroy(&spa->spa_suspend_cv);
7175ad8204nd	mutex_destroy(&spa->spa_async_lock);
718c25056dgw	mutex_destroy(&spa->spa_errlist_lock);
71935a5a35Jonathan Adams	mutex_destroy(&spa->spa_errlog_lock);
72006eeb2aek	mutex_destroy(&spa->spa_history_lock);
72135a5a35Jonathan Adams	mutex_destroy(&spa->spa_proc_lock);
722b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
72335a5a35Jonathan Adams	mutex_destroy(&spa->spa_scrub_lock);
724e14bb32Jeff Bonwick	mutex_destroy(&spa->spa_suspend_lock);
725a152156Jeff Bonwick	mutex_destroy(&spa->spa_vdev_top_lock);
726c3a6601Matthew Ahrens	mutex_destroy(&spa->spa_iokstat_lock);
728fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
732fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
733fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
734fa9e406ahrens */
735fa9e406ahrensspa_t *
736fa9e406ahrensspa_next(spa_t *prev)
738fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
740fa9e406ahrens	if (prev)
741fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
742fa9e406ahrens	else
743fa9e406ahrens		return (avl_first(&spa_namespace_avl));
747fa9e406ahrens * ==========================================================================
748fa9e406ahrens * SPA refcount functions
749fa9e406ahrens * ==========================================================================
750fa9e406ahrens */
753fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
754fa9e406ahrens * have the namespace lock held.
755fa9e406ahrens */
757fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
759088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
760fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
761fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
765fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
766fa9e406ahrens * have the namespace lock held.
767fa9e406ahrens */
769fa9e406ahrensspa_close(spa_t *spa, void *tag)
771088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
772fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
773fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
777fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
778088f389ahrens * spa_namespace_lock held.  We really compare against spa_minref, which is the
779fa9e406ahrens * number of references acquired when opening a pool
780fa9e406ahrens */
782fa9e406ahrensspa_refcount_zero(spa_t *spa)
784fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
786088f389ahrens	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
790fa9e406ahrens * ==========================================================================
791fa94a07brendan * SPA spare and l2cache tracking
79299653d4eschrock * ==========================================================================
79399653d4eschrock */
796fa94a07brendan * Hot spares and cache devices are tracked using the same code below,
797fa94a07brendan * for 'auxiliary' devices.
798fa94a07brendan */
800fa94a07brendantypedef struct spa_aux {
801fa94a07brendan	uint64_t	aux_guid;
802fa94a07brendan	uint64_t	aux_pool;
803fa94a07brendan	avl_node_t	aux_avl;
804fa94a07brendan	int		aux_count;
805fa94a07brendan} spa_aux_t;
807fa94a07brendanstatic int
808fa94a07brendanspa_aux_compare(const void *a, const void *b)
810fa94a07brendan	const spa_aux_t *sa = a;
811fa94a07brendan	const spa_aux_t *sb = b;
813fa94a07brendan	if (sa->aux_guid < sb->aux_guid)
814fa94a07brendan		return (-1);
815fa94a07brendan	else if (sa->aux_guid > sb->aux_guid)
816fa94a07brendan		return (1);
817fa94a07brendan	else
818fa94a07brendan		return (0);
822fa94a07brendanspa_aux_add(vdev_t *vd, avl_tree_t *avl)
824fa94a07brendan	avl_index_t where;
825fa94a07brendan	spa_aux_t search;
826fa94a07brendan	spa_aux_t *aux;
828fa94a07brendan	search.aux_guid = vd->vdev_guid;
829fa94a07brendan	if ((aux = avl_find(avl, &search, &where)) != NULL) {
830fa94a07brendan		aux->aux_count++;
831fa94a07brendan	} else {
832fa94a07brendan		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
833fa94a07brendan		aux->aux_guid = vd->vdev_guid;
834fa94a07brendan		aux->aux_count = 1;
835fa94a07brendan		avl_insert(avl, aux, where);
836fa94a07brendan	}
840fa94a07brendanspa_aux_remove(vdev_t *vd, avl_tree_t *avl)
842fa94a07brendan	spa_aux_t search;
843fa94a07brendan	spa_aux_t *aux;
844fa94a07brendan	avl_index_t where;
846fa94a07brendan	search.aux_guid = vd->vdev_guid;
847fa94a07brendan	aux = avl_find(avl, &search, &where);
849fa94a07brendan	ASSERT(aux != NULL);
851fa94a07brendan	if (--aux->aux_count == 0) {
852fa94a07brendan		avl_remove(avl, aux);
853fa94a07brendan		kmem_free(aux, sizeof (spa_aux_t));
854fa94a07brendan	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
855fa94a07brendan		aux->aux_pool = 0ULL;
856fa94a07brendan	}
86089a89ebllingspa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
862fa94a07brendan	spa_aux_t search, *found;
864fa94a07brendan	search.aux_guid = guid;
86589a89eblling	found = avl_find(avl, &search, NULL);
867fa94a07brendan	if (pool) {
868fa94a07brendan		if (found)
869fa94a07brendan			*pool = found->aux_pool;
870fa94a07brendan		else
871fa94a07brendan			*pool = 0ULL;
872fa94a07brendan	}
87489a89eblling	if (refcnt) {
87589a89eblling		if (found)
87689a89eblling			*refcnt = found->aux_count;
87789a89eblling		else
87889a89eblling			*refcnt = 0;
87989a89eblling	}
881fa94a07brendan	return (found != NULL);
885fa94a07brendanspa_aux_activate(vdev_t *vd, avl_tree_t *avl)
887fa94a07brendan	spa_aux_t search, *found;
888fa94a07brendan	avl_index_t where;
890fa94a07brendan	search.aux_guid = vd->vdev_guid;
891fa94a07brendan	found = avl_find(avl, &search, &where);
892fa94a07brendan	ASSERT(found != NULL);
893fa94a07brendan	ASSERT(found->aux_pool == 0ULL);
895fa94a07brendan	found->aux_pool = spa_guid(vd->vdev_spa);
89939c2341eschrock * Spares are tracked globally due to the following constraints:
90039c2341eschrock *
90139c2341eschrock * 	- A spare may be part of multiple pools.
90239c2341eschrock * 	- A spare may be added to a pool even if it's actively in use within
90339c2341eschrock *	  another pool.
90439c2341eschrock * 	- A spare in use in any pool can only be the source of a replacement if
90539c2341eschrock *	  the target is a spare in the same pool.
90639c2341eschrock *
90739c2341eschrock * We keep track of all spares on the system through the use of a reference
90839c2341eschrock * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
90939c2341eschrock * spare, then we bump the reference count in the AVL tree.  In addition, we set
91039c2341eschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
91139c2341eschrock * inactive).  When a spare is made active (used to replace a device in the
91239c2341eschrock * pool), we also keep track of which pool its been made a part of.