spa_misc.c revision 69962b5647e4a8b9b14998733b765925381b727e
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/*
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 */
26fa9e406ahrens
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"
53fa9e406ahrens
54fa9e406ahrens/*
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:
144e14bb32Jeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
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 */
224fa9e406ahrens
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;
230fa9e406ahrens
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;
23599653d4eschrock
236fa9e406ahrenskmem_cache_t *spa_buffer_pool;
2378ad4d6dJeff Bonwickint spa_mode_global;
238fa9e406ahrens
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);
242fa9e406ahrens#else
243fa9e406ahrensint zfs_flags = 0;
244fa9e406ahrens#endif
245fa9e406ahrens
2460125049ahrens/*
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.
2500125049ahrens */
2510125049ahrensint zfs_recover = 0;
2520125049ahrens
25369962b5Matthew Ahrens/*
25469962b5Matthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is
25569962b5Matthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the
25669962b5Matthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
25769962b5Matthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that
25869962b5Matthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
25969962b5Matthew Ahrens * in a system panic.
26069962b5Matthew Ahrens */
26169962b5Matthew Ahrensuint64_t zfs_deadman_synctime_ms = 1000000ULL;
262283b846George.Wilson
263283b846George.Wilson/*
26469962b5Matthew Ahrens * Check time in milliseconds. This defines the frequency at which we check
26569962b5Matthew Ahrens * for hung I/O.
266283b846George.Wilson */
26769962b5Matthew Ahrensuint64_t zfs_deadman_checktime_ms = 5000ULL;
268283b846George.Wilson
269283b846George.Wilson/*
270283b846George.Wilson * Override the zfs deadman behavior via /etc/system. By default the
271283b846George.Wilson * deadman is enabled except on VMware and sparc deployments.
272283b846George.Wilson */
273283b846George.Wilsonint zfs_deadman_enabled = -1;
274283b846George.Wilson
27569962b5Matthew Ahrens/*
27669962b5Matthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which
27769962b5Matthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
27869962b5Matthew Ahrens * times the size; so just assume that.  Add to this the fact that
27969962b5Matthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because
28069962b5Matthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
28169962b5Matthew Ahrens * the worst case is:
28269962b5Matthew Ahrens *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
28369962b5Matthew Ahrens */
28469962b5Matthew Ahrensint spa_asize_inflation = 24;
285fa9e406ahrens
286fa9e406ahrens/*
287fa9e406ahrens * ==========================================================================
288e05725bbonwick * SPA config locking
289e05725bbonwick * ==========================================================================
290e05725bbonwick */
291e05725bbonwickstatic void
292e14bb32Jeff Bonwickspa_config_lock_init(spa_t *spa)
293e14bb32Jeff Bonwick{
294e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
295e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
296e14bb32Jeff Bonwick		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
297e14bb32Jeff Bonwick		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
2983b2aab1Matthew Ahrens		refcount_create_untracked(&scl->scl_count);
299e14bb32Jeff Bonwick		scl->scl_writer = NULL;
300e14bb32Jeff Bonwick		scl->scl_write_wanted = 0;
301e14bb32Jeff Bonwick	}
302e05725bbonwick}
303e05725bbonwick
304e05725bbonwickstatic void
305e14bb32Jeff Bonwickspa_config_lock_destroy(spa_t *spa)
306e14bb32Jeff Bonwick{
307e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
308e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
309e14bb32Jeff Bonwick		mutex_destroy(&scl->scl_lock);
310e14bb32Jeff Bonwick		cv_destroy(&scl->scl_cv);
311e14bb32Jeff Bonwick		refcount_destroy(&scl->scl_count);
312e14bb32Jeff Bonwick		ASSERT(scl->scl_writer == NULL);
313e14bb32Jeff Bonwick		ASSERT(scl->scl_write_wanted == 0);
314e14bb32Jeff Bonwick	}
315e14bb32Jeff Bonwick}
316e14bb32Jeff Bonwick
317e14bb32Jeff Bonwickint
318e14bb32Jeff Bonwickspa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
319e05725bbonwick{
320e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
321e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
322e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
323e14bb32Jeff Bonwick			continue;
324e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
325e14bb32Jeff Bonwick		if (rw == RW_READER) {
326e14bb32Jeff Bonwick			if (scl->scl_writer || scl->scl_write_wanted) {
327e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
328e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
329e14bb32Jeff Bonwick				return (0);
330e14bb32Jeff Bonwick			}
331e14bb32Jeff Bonwick		} else {
332e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
333e14bb32Jeff Bonwick			if (!refcount_is_zero(&scl->scl_count)) {
334e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
335e14bb32Jeff Bonwick				spa_config_exit(spa, locks ^ (1 << i), tag);
336e14bb32Jeff Bonwick				return (0);
337e14bb32Jeff Bonwick			}
338e14bb32Jeff Bonwick			scl->scl_writer = curthread;
339e14bb32Jeff Bonwick		}
340e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
341e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
342e14bb32Jeff Bonwick	}
343e14bb32Jeff Bonwick	return (1);
344e05725bbonwick}
345e05725bbonwick
346e05725bbonwickvoid
347e14bb32Jeff Bonwickspa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
348e05725bbonwick{
349f64c0e3Eric Taylor	int wlocks_held = 0;
350f64c0e3Eric Taylor
3513b2aab1Matthew Ahrens	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
3523b2aab1Matthew Ahrens
353e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
354e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
355f64c0e3Eric Taylor		if (scl->scl_writer == curthread)
356f64c0e3Eric Taylor			wlocks_held |= (1 << i);
357e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
358e14bb32Jeff Bonwick			continue;
359e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
360e14bb32Jeff Bonwick		if (rw == RW_READER) {
361e14bb32Jeff Bonwick			while (scl->scl_writer || scl->scl_write_wanted) {
362e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
363e14bb32Jeff Bonwick			}
364e14bb32Jeff Bonwick		} else {
365e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
366e14bb32Jeff Bonwick			while (!refcount_is_zero(&scl->scl_count)) {
367e14bb32Jeff Bonwick				scl->scl_write_wanted++;
368e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
369e14bb32Jeff Bonwick				scl->scl_write_wanted--;
370e14bb32Jeff Bonwick			}
371e14bb32Jeff Bonwick			scl->scl_writer = curthread;
372e14bb32Jeff Bonwick		}
373e14bb32Jeff Bonwick		(void) refcount_add(&scl->scl_count, tag);
374e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
375e05725bbonwick	}
376f64c0e3Eric Taylor	ASSERT(wlocks_held <= locks);
377e05725bbonwick}
378e05725bbonwick
379e05725bbonwickvoid
380e14bb32Jeff Bonwickspa_config_exit(spa_t *spa, int locks, void *tag)
381e05725bbonwick{
382e14bb32Jeff Bonwick	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
383e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
384e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
385e14bb32Jeff Bonwick			continue;
386e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
387e14bb32Jeff Bonwick		ASSERT(!refcount_is_zero(&scl->scl_count));
388e14bb32Jeff Bonwick		if (refcount_remove(&scl->scl_count, tag) == 0) {
389e14bb32Jeff Bonwick			ASSERT(scl->scl_writer == NULL ||
390e14bb32Jeff Bonwick			    scl->scl_writer == curthread);
391e14bb32Jeff Bonwick			scl->scl_writer = NULL;	/* OK in either case */
392e14bb32Jeff Bonwick			cv_broadcast(&scl->scl_cv);
393e14bb32Jeff Bonwick		}
394e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
395e05725bbonwick	}
396e05725bbonwick}
397e05725bbonwick
398e14bb32Jeff Bonwickint
399e14bb32Jeff Bonwickspa_config_held(spa_t *spa, int locks, krw_t rw)
400e05725bbonwick{
401e14bb32Jeff Bonwick	int locks_held = 0;
402e05725bbonwick
403e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
404e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
405e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
406e14bb32Jeff Bonwick			continue;
407e14bb32Jeff Bonwick		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
408e14bb32Jeff Bonwick		    (rw == RW_WRITER && scl->scl_writer == curthread))
409e14bb32Jeff Bonwick			locks_held |= 1 << i;
410e14bb32Jeff Bonwick	}
411e14bb32Jeff Bonwick
412e14bb32Jeff Bonwick	return (locks_held);
413e05725bbonwick}
414e05725bbonwick
415e05725bbonwick/*
416e05725bbonwick * ==========================================================================
417fa9e406ahrens * SPA namespace functions
418fa9e406ahrens * ==========================================================================
419fa9e406ahrens */
420fa9e406ahrens
421fa9e406ahrens/*
422fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
423fa9e406ahrens * Returns NULL if no matching spa_t is found.
424fa9e406ahrens */
425fa9e406ahrensspa_t *
426fa9e406ahrensspa_lookup(const char *name)
427fa9e406ahrens{
428e14bb32Jeff Bonwick	static spa_t search;	/* spa_t is large; don't allocate on stack */
429e14bb32Jeff Bonwick	spa_t *spa;
430fa9e406ahrens	avl_index_t where;
43140feaa9ahrens	char *cp;
432fa9e406ahrens
433fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
434fa9e406ahrens
4353b2aab1Matthew Ahrens	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
4363b2aab1Matthew Ahrens
43740feaa9ahrens	/*
43840feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
43940feaa9ahrens	 * just use that.
44040feaa9ahrens	 */
4413b2aab1Matthew Ahrens	cp = strpbrk(search.spa_name, "/@");
4423b2aab1Matthew Ahrens	if (cp != NULL)
44340feaa9ahrens		*cp = '\0';
44440feaa9ahrens
445fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
446fa9e406ahrens
447fa9e406ahrens	return (spa);
448fa9e406ahrens}
449fa9e406ahrens
450fa9e406ahrens/*
451283b846George.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
452283b846George.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
453283b846George.Wilson * looking for potentially hung I/Os.
454283b846George.Wilson */
455283b846George.Wilsonvoid
456283b846George.Wilsonspa_deadman(void *arg)
457283b846George.Wilson{
458283b846George.Wilson	spa_t *spa = arg;
459283b846George.Wilson
460283b846George.Wilson	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
461283b846George.Wilson	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
462283b846George.Wilson	    ++spa->spa_deadman_calls);
463283b846George.Wilson	if (zfs_deadman_enabled)
464283b846George.Wilson		vdev_deadman(spa->spa_root_vdev);
465283b846George.Wilson}
466283b846George.Wilson
467283b846George.Wilson/*
468fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
469fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
470fa9e406ahrens * exist by calling spa_lookup() first.
471fa9e406ahrens */
472fa9e406ahrensspa_t *
473468c413Tim Haleyspa_add(const char *name, nvlist_t *config, const char *altroot)
474fa9e406ahrens{
475fa9e406ahrens	spa_t *spa;
476c5904d1eschrock	spa_config_dirent_t *dp;
477283b846George.Wilson	cyc_handler_t hdlr;
478283b846George.Wilson	cyc_time_t when;
479fa9e406ahrens
480fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
481fa9e406ahrens
482fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
483fa9e406ahrens
484c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
485c25056dgw	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
48635a5a35Jonathan Adams	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
487c25056dgw	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
48835a5a35Jonathan Adams	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
489c25056dgw	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
49035a5a35Jonathan Adams	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
491a152156Jeff Bonwick	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
492a152156Jeff Bonwick	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
493c3a6601Matthew Ahrens	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
494c25056dgw
495c25056dgw	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
49635a5a35Jonathan Adams	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
497c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
498e14bb32Jeff Bonwick	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
499c25056dgw
500b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
501cde58dbMatthew Ahrens		bplist_create(&spa->spa_free_bplist[t]);
502b24ab67Jeff Bonwick
503e14bb32Jeff Bonwick	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
504fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
505fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
5060373e76bonwick	spa->spa_final_txg = UINT64_MAX;
507468c413Tim Haley	spa->spa_load_max_txg = UINT64_MAX;
50835a5a35Jonathan Adams	spa->spa_proc = &p0;
50935a5a35Jonathan Adams	spa->spa_proc_state = SPA_PROC_NONE;
510fa9e406ahrens
511283b846George.Wilson	hdlr.cyh_func = spa_deadman;
512283b846George.Wilson	hdlr.cyh_arg = spa;
513283b846George.Wilson	hdlr.cyh_level = CY_LOW_LEVEL;
514283b846George.Wilson
51569962b5Matthew Ahrens	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
516283b846George.Wilson
517283b846George.Wilson	/*
518283b846George.Wilson	 * This determines how often we need to check for hung I/Os after
519283b846George.Wilson	 * the cyclic has already fired. Since checking for hung I/Os is
520283b846George.Wilson	 * an expensive operation we don't want to check too frequently.
52169962b5Matthew Ahrens	 * Instead wait for 5 seconds before checking again.
522283b846George.Wilson	 */
52369962b5Matthew Ahrens	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
524283b846George.Wilson	when.cyt_when = CY_INFINITY;
525283b846George.Wilson	mutex_enter(&cpu_lock);
526283b846George.Wilson	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
527283b846George.Wilson	mutex_exit(&cpu_lock);
528283b846George.Wilson
529fa9e406ahrens	refcount_create(&spa->spa_refcount);
530e14bb32Jeff Bonwick	spa_config_lock_init(spa);
531fa9e406ahrens
532fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
533fa9e406ahrens
5340373e76bonwick	/*
5350373e76bonwick	 * Set the alternate root, if there is one.
5360373e76bonwick	 */
5370373e76bonwick	if (altroot) {
5380373e76bonwick		spa->spa_root = spa_strdup(altroot);
5390373e76bonwick		spa_active_count++;
5400373e76bonwick	}
5410373e76bonwick
542c5904d1eschrock	/*
543c5904d1eschrock	 * Every pool starts with the default cachefile
544c5904d1eschrock	 */
545c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
546c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
547c5904d1eschrock
548c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
549ef912c8Tim Haley	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
550c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
551c5904d1eschrock
5524b964adGeorge Wilson	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
5534b964adGeorge Wilson	    KM_SLEEP) == 0);
5544b964adGeorge Wilson
555ad135b5Christopher Siden	if (config != NULL) {
556ad135b5Christopher Siden		nvlist_t *features;
557ad135b5Christopher Siden
558ad135b5Christopher Siden		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
559ad135b5Christopher Siden		    &features) == 0) {
560ad135b5Christopher Siden			VERIFY(nvlist_dup(features, &spa->spa_label_features,
561ad135b5Christopher Siden			    0) == 0);
562ad135b5Christopher Siden		}
563ad135b5Christopher Siden
564468c413Tim Haley		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
565ad135b5Christopher Siden	}
566ad135b5Christopher Siden
567ad135b5Christopher Siden	if (spa->spa_label_features == NULL) {
568ad135b5Christopher Siden		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
569ad135b5Christopher Siden		    KM_SLEEP) == 0);
570ad135b5Christopher Siden	}
571468c413Tim Haley
572c3a6601Matthew Ahrens	spa->spa_iokstat = kstat_create("zfs", 0, name,
573c3a6601Matthew Ahrens	    "disk", KSTAT_TYPE_IO, 1, 0);
574c3a6601Matthew Ahrens	if (spa->spa_iokstat) {
575c3a6601Matthew Ahrens		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
576c3a6601Matthew Ahrens		kstat_install(spa->spa_iokstat);
577c3a6601Matthew Ahrens	}
578c3a6601Matthew Ahrens
5793b2aab1Matthew Ahrens	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
5803b2aab1Matthew Ahrens
581fa9e406ahrens	return (spa);
582fa9e406ahrens}
583fa9e406ahrens
584fa9e406ahrens/*
585fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
586fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
587fa9e406ahrens * deactivated.
588fa9e406ahrens */
589fa9e406ahrensvoid
590fa9e406ahrensspa_remove(spa_t *spa)
591fa9e406ahrens{
592c5904d1eschrock	spa_config_dirent_t *dp;
593c5904d1eschrock
594fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
595fa9e406ahrens	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
596fa9e406ahrens
5971195e68Mark J Musante	nvlist_free(spa->spa_config_splitting);
5981195e68Mark J Musante
599fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
600fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
601fa9e406ahrens
6020373e76bonwick	if (spa->spa_root) {
603fa9e406ahrens		spa_strfree(spa->spa_root);
6040373e76bonwick		spa_active_count--;
6050373e76bonwick	}
606fa9e406ahrens
607c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
608c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
609c5904d1eschrock		if (dp->scd_path != NULL)
610c5904d1eschrock			spa_strfree(dp->scd_path);
611c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
612c5904d1eschrock	}
613c5904d1eschrock
614c5904d1eschrock	list_destroy(&spa->spa_config_list);
6152f8aaabeschrock
616ad135b5Christopher Siden	nvlist_free(spa->spa_label_features);
6174b964adGeorge Wilson	nvlist_free(spa->spa_load_info);
618fa9e406ahrens	spa_config_set(spa, NULL);
619fa9e406ahrens
620283b846George.Wilson	mutex_enter(&cpu_lock);
621283b846George.Wilson	if (spa->spa_deadman_cycid != CYCLIC_NONE)
622283b846George.Wilson		cyclic_remove(spa->spa_deadman_cycid);
623283b846George.Wilson	mutex_exit(&cpu_lock);
624283b846George.Wilson	spa->spa_deadman_cycid = CYCLIC_NONE;
625283b846George.Wilson
626fa9e406ahrens	refcount_destroy(&spa->spa_refcount);
62791ebeefahrens
628e14bb32Jeff Bonwick	spa_config_lock_destroy(spa);
629fa9e406ahrens
630c3a6601Matthew Ahrens	kstat_delete(spa->spa_iokstat);
631c3a6601Matthew Ahrens	spa->spa_iokstat = NULL;
632c3a6601Matthew Ahrens
633b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
634cde58dbMatthew Ahrens		bplist_destroy(&spa->spa_free_bplist[t]);
635b24ab67Jeff Bonwick
636c25056dgw	cv_destroy(&spa->spa_async_cv);
63735a5a35Jonathan Adams	cv_destroy(&spa->spa_proc_cv);
638c25056dgw	cv_destroy(&spa->spa_scrub_io_cv);
639e14bb32Jeff Bonwick	cv_destroy(&spa->spa_suspend_cv);
640c25056dgw
6415ad8204nd	mutex_destroy(&spa->spa_async_lock);
642c25056dgw	mutex_destroy(&spa->spa_errlist_lock);
64335a5a35Jonathan Adams	mutex_destroy(&spa->spa_errlog_lock);
64406eeb2aek	mutex_destroy(&spa->spa_history_lock);
64535a5a35Jonathan Adams	mutex_destroy(&spa->spa_proc_lock);
646b1b8ab3lling	mutex_destroy(&spa->spa_props_lock);
64735a5a35Jonathan Adams	mutex_destroy(&spa->spa_scrub_lock);
648e14bb32Jeff Bonwick	mutex_destroy(&spa->spa_suspend_lock);
649a152156Jeff Bonwick	mutex_destroy(&spa->spa_vdev_top_lock);
650c3a6601Matthew Ahrens	mutex_destroy(&spa->spa_iokstat_lock);
6515ad8204nd
652fa9e406ahrens	kmem_free(spa, sizeof (spa_t));
653fa9e406ahrens}
654fa9e406ahrens
655fa9e406ahrens/*
656fa9e406ahrens * Given a pool, return the next pool in the namespace, or NULL if there is
657fa9e406ahrens * none.  If 'prev' is NULL, return the first pool.
658fa9e406ahrens */
659fa9e406ahrensspa_t *
660fa9e406ahrensspa_next(spa_t *prev)
661fa9e406ahrens{
662fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
663fa9e406ahrens
664fa9e406ahrens	if (prev)
665fa9e406ahrens		return (AVL_NEXT(&spa_namespace_avl, prev));
666fa9e406ahrens	else
667fa9e406ahrens		return (avl_first(&spa_namespace_avl));
668fa9e406ahrens}
669fa9e406ahrens
670fa9e406ahrens/*
671fa9e406ahrens * ==========================================================================
672fa9e406ahrens * SPA refcount functions
673fa9e406ahrens * ==========================================================================
674fa9e406ahrens */
675fa9e406ahrens
676fa9e406ahrens/*
677fa9e406ahrens * Add a reference to the given spa_t.  Must have at least one reference, or
678fa9e406ahrens * have the namespace lock held.
679fa9e406ahrens */
680fa9e406ahrensvoid
681fa9e406ahrensspa_open_ref(spa_t *spa, void *tag)
682fa9e406ahrens{
683088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
684fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
685fa9e406ahrens	(void) refcount_add(&spa->spa_refcount, tag);
686fa9e406ahrens}
687fa9e406ahrens
688fa9e406ahrens/*
689fa9e406ahrens * Remove a reference to the given spa_t.  Must have at least one reference, or
690fa9e406ahrens * have the namespace lock held.
691fa9e406ahrens */
692fa9e406ahrensvoid
693fa9e406ahrensspa_close(spa_t *spa, void *tag)
694fa9e406ahrens{
695088f389ahrens	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
696fa9e406ahrens	    MUTEX_HELD(&spa_namespace_lock));
697fa9e406ahrens	(void) refcount_remove(&spa->spa_refcount, tag);
698fa9e406ahrens}
699fa9e406ahrens
700fa9e406ahrens/*
701fa9e406ahrens * Check to see if the spa refcount is zero.  Must be called with
702088f389ahrens * spa_namespace_lock held.  We really compare against spa_minref, which is the
703fa9e406ahrens * number of references acquired when opening a pool
704fa9e406ahrens */
705fa9e406ahrensboolean_t
706fa9e406ahrensspa_refcount_zero(spa_t *spa)
707fa9e406ahrens{
708fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
709fa9e406ahrens
710088f389ahrens	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
711fa9e406ahrens}
712fa9e406ahrens
713fa9e406ahrens/*
714fa9e406ahrens * ==========================================================================
715fa94a07brendan * SPA spare and l2cache tracking
71699653d4eschrock * ==========================================================================
71799653d4eschrock */
71899653d4eschrock
71999653d4eschrock/*
720fa94a07brendan * Hot spares and cache devices are tracked using the same code below,
721fa94a07brendan * for 'auxiliary' devices.
722fa94a07brendan */
723fa94a07brendan
724fa94a07brendantypedef struct spa_aux {
725fa94a07brendan	uint64_t	aux_guid;
726fa94a07brendan	uint64_t	aux_pool;
727fa94a07brendan	avl_node_t	aux_avl;
728fa94a07brendan	int		aux_count;
729fa94a07brendan} spa_aux_t;
730fa94a07brendan
731fa94a07brendanstatic int
732fa94a07brendanspa_aux_compare(const void *a, const void *b)
733fa94a07brendan{
734fa94a07brendan	const spa_aux_t *sa = a;
735fa94a07brendan	const spa_aux_t *sb = b;
736fa94a07brendan
737fa94a07brendan	if (sa->aux_guid < sb->aux_guid)
738fa94a07brendan		return (-1);
739fa94a07brendan	else if (sa->aux_guid > sb->aux_guid)
740fa94a07brendan		return (1);
741fa94a07brendan	else
742fa94a07brendan		return (0);
743fa94a07brendan}
744fa94a07brendan
745fa94a07brendanvoid
746fa94a07brendanspa_aux_add(vdev_t *vd, avl_tree_t *avl)
747fa94a07brendan{
748fa94a07brendan	avl_index_t where;
749fa94a07brendan	spa_aux_t search;
750fa94a07brendan	spa_aux_t *aux;
751fa94a07brendan
752fa94a07brendan	search.aux_guid = vd->vdev_guid;
753fa94a07brendan	if ((aux = avl_find(avl, &search, &where)) != NULL) {
754fa94a07brendan		aux->aux_count++;
755fa94a07brendan	} else {
756fa94a07brendan		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
757fa94a07brendan		aux->aux_guid = vd->vdev_guid;
758fa94a07brendan		aux->aux_count = 1;
759fa94a07brendan		avl_insert(avl, aux, where);
760fa94a07brendan	}
761fa94a07brendan}
762fa94a07brendan
763fa94a07brendanvoid
764fa94a07brendanspa_aux_remove(vdev_t *vd, avl_tree_t *avl)
765fa94a07brendan{
766fa94a07brendan	spa_aux_t search;
767fa94a07brendan	spa_aux_t *aux;
768fa94a07brendan	avl_index_t where;
769fa94a07brendan
770fa94a07brendan	search.aux_guid = vd->vdev_guid;
771fa94a07brendan	aux = avl_find(avl, &search, &where);
772fa94a07brendan
773fa94a07brendan	ASSERT(aux != NULL);
774fa94a07brendan
775fa94a07brendan	if (--aux->aux_count == 0) {
776fa94a07brendan		avl_remove(avl, aux);
777fa94a07brendan		kmem_free(aux, sizeof (spa_aux_t));
778fa94a07brendan	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
779fa94a07brendan		aux->aux_pool = 0ULL;
780fa94a07brendan	}
781fa94a07brendan}
782fa94a07brendan
783fa94a07brendanboolean_t
78489a89ebllingspa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
785fa94a07brendan{
786fa94a07brendan	spa_aux_t search, *found;
787fa94a07brendan
788fa94a07brendan	search.aux_guid = guid;
78989a89eblling	found = avl_find(avl, &search, NULL);
790fa94a07brendan
791fa94a07brendan	if (pool) {
792fa94a07brendan		if (found)
793fa94a07brendan			*pool = found->aux_pool;
794fa94a07brendan		else
795fa94a07brendan			*pool = 0ULL;
796fa94a07brendan	}
797fa94a07brendan
79889a89eblling	if (refcnt) {
79989a89eblling		if (found)
80089a89eblling			*refcnt = found->aux_count;
80189a89eblling		else
80289a89eblling			*refcnt = 0;
80389a89eblling	}
80489a89eblling
805fa94a07brendan	return (found != NULL);
806fa94a07brendan}
807fa94a07brendan
808fa94a07brendanvoid
809fa94a07brendanspa_aux_activate(vdev_t *vd, avl_tree_t *avl)
810fa94a07brendan{
811fa94a07brendan	spa_aux_t search, *found;
812fa94a07brendan	avl_index_t where;
813fa94a07brendan
814fa94a07brendan	search.aux_guid = vd->vdev_guid;
815fa94a07brendan	found = avl_find(avl, &search, &where);
816fa94a07brendan	ASSERT(found != NULL);
817fa94a07brendan	ASSERT(found->aux_pool == 0ULL);
818fa94a07brendan
819fa94a07brendan	found->aux_pool = spa_guid(vd->vdev_spa);
820fa94a07brendan}
821fa94a07brendan
822fa94a07brendan/*
82339c2341eschrock * Spares are tracked globally due to the following constraints:
82439c2341eschrock *
82539c2341eschrock * 	- A spare may be part of multiple pools.
82639c2341eschrock * 	- A spare may be added to a pool even if it's actively in use within
82739c2341eschrock *	  another pool.
82839c2341eschrock * 	- A spare in use in any pool can only be the source of a replacement if
82939c2341eschrock *	  the target is a spare in the same pool.
83039c2341eschrock *
83139c2341eschrock * We keep track of all spares on the system through the use of a reference
83239c2341eschrock * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
83339c2341eschrock * spare, then we bump the reference count in the AVL tree.  In addition, we set
83439c2341eschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
83539c2341eschrock * inactive).  When a spare is made active (used to replace a device in the
83639c2341eschrock * pool), we also keep track of which pool its been made a part of.
83739c2341eschrock *
83839c2341eschrock * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
83939c2341eschrock * called under the spa_namespace lock as part of vdev reconfiguration.  The
84039c2341eschrock * separate spare lock exists for the status query path, which does not need to
84139c2341eschrock * be completely consistent with respect to other vdev configuration changes.
84299653d4eschrock */
84339c2341eschrock
84499653d4eschrockstatic int
84599653d4eschrockspa_spare_compare(const void *a, const void *b)
84699653d4eschrock{
847fa94a07brendan	return (spa_aux_compare(a, b));
84899653d4eschrock}
84999653d4eschrock
85099653d4eschrockvoid
85139c2341eschrockspa_spare_add(vdev_t *vd)
85299653d4eschrock{
85399653d4eschrock	mutex_enter(&spa_spare_lock);
85439c2341eschrock	ASSERT(!vd->vdev_isspare);
855fa94a07brendan	spa_aux_add(vd, &spa_spare_avl);
85639c2341eschrock	vd->vdev_isspare = B_TRUE;
85799653d4eschrock	mutex_exit(&spa_spare_lock);
85899653d4eschrock}
85999653d4eschrock
86099653d4eschrockvoid
86139c2341eschrockspa_spare_remove(vdev_t *vd)
86299653d4eschrock{
86399653d4eschrock	mutex_enter(&spa_spare_lock);
86439c2341eschrock	ASSERT(vd->vdev_isspare);
865fa94a07brendan	spa_aux_remove(vd, &spa_spare_avl);
86639c2341eschrock	vd->vdev_isspare = B_FALSE;
86799653d4eschrock	mutex_exit(&spa_spare_lock);
86899653d4eschrock}
86999653d4eschrock
87099653d4eschrockboolean_t
87189a89ebllingspa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
87299653d4eschrock{
873fa94a07brendan	boolean_t found;
87499653d4eschrock
87599653d4eschrock	mutex_enter(&spa_spare_lock);
87689a89eblling	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
87799653d4eschrock	mutex_exit(&spa_spare_lock);
87899653d4eschrock
879fa94a07brendan	return (found);
88039c2341eschrock}
88139c2341eschrock
88239c2341eschrockvoid
88339c2341eschrockspa_spare_activate(vdev_t *vd)
88439c2341eschrock{
88539c2341eschrock	mutex_enter(&spa_spare_lock);
88639c2341eschrock	ASSERT(vd->vdev_isspare);
887fa94a07brendan	spa_aux_activate(vd, &spa_spare_avl);
888fa94a07brendan	mutex_exit(&spa_spare_lock);
889fa94a07brendan}
89039c2341eschrock
891fa94a07brendan/*
892fa94a07brendan * Level 2 ARC devices are tracked globally for the same reasons as spares.
893fa94a07brendan * Cache devices currently only support one pool per cache device, and so
894fa94a07brendan * for these devices the aux reference count is currently unused beyond 1.
895fa94a07brendan */
89639c2341eschrock
897fa94a07brendanstatic int
898fa94a07brendanspa_l2cache_compare(const void *a, const void *b)
899fa94a07brendan{
900fa94a07brendan	return (spa_aux_compare(a, b));
901fa94a07brendan}
902