xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision c3a66015)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
229842588bSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23ad135b5dSChristopher Siden  * Copyright (c) 2012 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/spa_impl.h>
29283b8460SGeorge.Wilson #include <sys/spa_boot.h>
30fa9e4066Sahrens #include <sys/zio.h>
31fa9e4066Sahrens #include <sys/zio_checksum.h>
32fa9e4066Sahrens #include <sys/zio_compress.h>
33fa9e4066Sahrens #include <sys/dmu.h>
34fa9e4066Sahrens #include <sys/dmu_tx.h>
35fa9e4066Sahrens #include <sys/zap.h>
36fa9e4066Sahrens #include <sys/zil.h>
37fa9e4066Sahrens #include <sys/vdev_impl.h>
38fa9e4066Sahrens #include <sys/metaslab.h>
39fa9e4066Sahrens #include <sys/uberblock_impl.h>
40fa9e4066Sahrens #include <sys/txg.h>
41fa9e4066Sahrens #include <sys/avl.h>
42fa9e4066Sahrens #include <sys/unique.h>
43fa9e4066Sahrens #include <sys/dsl_pool.h>
44fa9e4066Sahrens #include <sys/dsl_dir.h>
45fa9e4066Sahrens #include <sys/dsl_prop.h>
463f9d6ad7SLin Ling #include <sys/dsl_scan.h>
47fa9e4066Sahrens #include <sys/fs/zfs.h>
486ce0521aSperrin #include <sys/metaslab_impl.h>
49e14bb325SJeff Bonwick #include <sys/arc.h>
50485bbbf5SGeorge Wilson #include <sys/ddt.h>
5191ebeef5Sahrens #include "zfs_prop.h"
52ad135b5dSChristopher Siden #include "zfeature_common.h"
53fa9e4066Sahrens 
54fa9e4066Sahrens /*
55fa9e4066Sahrens  * SPA locking
56fa9e4066Sahrens  *
57fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
58fa9e4066Sahrens  *
59fa9e4066Sahrens  * spa_namespace_lock (global mutex)
60fa9e4066Sahrens  *
6144cd46caSbillm  *	This lock must be acquired to do any of the following:
62fa9e4066Sahrens  *
6344cd46caSbillm  *		- Lookup a spa_t by name
6444cd46caSbillm  *		- Add or remove a spa_t from the namespace
6544cd46caSbillm  *		- Increase spa_refcount from non-zero
6644cd46caSbillm  *		- Check if spa_refcount is zero
6744cd46caSbillm  *		- Rename a spa_t
68ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
6944cd46caSbillm  *		- Held for the duration of create/destroy/import/export
70fa9e4066Sahrens  *
7144cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
7244cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
7344cd46caSbillm  *	definition they must have an existing reference, and will never need
7444cd46caSbillm  *	to lookup a spa_t by name.
75fa9e4066Sahrens  *
76fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
77fa9e4066Sahrens  *
7844cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
7944cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8044cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
81088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
8244cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
83fa9e4066Sahrens  *
84e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
85fa9e4066Sahrens  *
8691ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
8791ebeef5Sahrens  *	the following circumstances:
88fa9e4066Sahrens  *
8944cd46caSbillm  *		- RW_READER to perform I/O to the spa
9044cd46caSbillm  *		- RW_WRITER to change the vdev config
91fa9e4066Sahrens  *
92fa9e4066Sahrens  * The locking order is fairly straightforward:
93fa9e4066Sahrens  *
9444cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
95fa9e4066Sahrens  *
9644cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
9744cd46caSbillm  *	or to check if it is zero.
98fa9e4066Sahrens  *
99e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
100fa9e4066Sahrens  *
10144cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10244cd46caSbillm  *	the config lock.
103fa9e4066Sahrens  *
104e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
105fa9e4066Sahrens  *
10644cd46caSbillm  *	The namespace lock must always be taken before the config lock.
107fa9e4066Sahrens  *
108fa9e4066Sahrens  *
109e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
110fa9e4066Sahrens  *
111e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
112e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
113fa9e4066Sahrens  *
11444cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
115fa9e4066Sahrens  *
11644cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
117fa9e4066Sahrens  *
11844cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
11944cd46caSbillm  *				frees up any memory associated with the spa_t.
120fa9e4066Sahrens  *
12144cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12244cd46caSbillm  *				first if NULL is passed.
123fa9e4066Sahrens  *
12444cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12544cd46caSbillm  *				the system.
126fa9e4066Sahrens  *
127ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
128fa9e4066Sahrens  *
129fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
130fa9e4066Sahrens  *
13144cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13244cd46caSbillm  *				called with spa_namespace_lock held if the
13344cd46caSbillm  *				refcount is currently zero.
134fa9e4066Sahrens  *
13544cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13644cd46caSbillm  *				not free the spa_t or remove it from the
13744cd46caSbillm  *				namespace.  No locking is required.
138fa9e4066Sahrens  *
13944cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14044cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14144cd46caSbillm  *				held.
142fa9e4066Sahrens  *
143e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
144e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
145e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
146e14bb325SJeff Bonwick  *
147e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
148e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
149e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
150e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
151e14bb325SJeff Bonwick  *
152e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
153e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
154e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
155e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
156e14bb325SJeff Bonwick  *
157e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
158e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
159e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
160e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
161e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
162e14bb325SJeff Bonwick  *
163e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
164e14bb325SJeff Bonwick  *
165e14bb325SJeff Bonwick  * SCL_CONFIG
166e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
167e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
168e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
169e14bb325SJeff Bonwick  *
170e14bb325SJeff Bonwick  * SCL_STATE
171e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
172e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
173e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
174e14bb325SJeff Bonwick  *
175e14bb325SJeff Bonwick  * SCL_ALLOC
176e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
177e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
178e14bb325SJeff Bonwick  *
179e14bb325SJeff Bonwick  * SCL_ZIO
180e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
181e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
182e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
183e14bb325SJeff Bonwick  *
184e14bb325SJeff Bonwick  * SCL_FREE
185e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
186e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
187e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
188e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
189e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
190e14bb325SJeff Bonwick  *
191e14bb325SJeff Bonwick  * SCL_VDEV
192e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
193b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
194e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
195e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
196e14bb325SJeff Bonwick  *
197e14bb325SJeff Bonwick  * In addition, the following rules apply:
198e14bb325SJeff Bonwick  *
199e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
200e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
201e14bb325SJeff Bonwick  *
202e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
203e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
204e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
205e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
206e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
207fa9e4066Sahrens  *
208ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
209fa9e4066Sahrens  *
21044cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
211ea8dc4b6Seschrock  *				for writing.
212fa9e4066Sahrens  *
21344cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21444cd46caSbillm  *				to complete, sync the updated configs to the
215ea8dc4b6Seschrock  *				cache, and release the namespace lock.
216fa9e4066Sahrens  *
217e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
218e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
219e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
220e14bb325SJeff Bonwick  *
221ad135b5dSChristopher Siden  * spa_rename() is also implemented within this file since it requires
222e14bb325SJeff Bonwick  * manipulation of the namespace.
223fa9e4066Sahrens  */
224fa9e4066Sahrens 
225fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
226fa9e4066Sahrens kmutex_t spa_namespace_lock;
227fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2280373e76bSbonwick static int spa_active_count;
229416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
230fa9e4066Sahrens 
23199653d4eSeschrock static kmutex_t spa_spare_lock;
23239c23413Seschrock static avl_tree_t spa_spare_avl;
233fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
234fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23599653d4eSeschrock 
236fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2378ad4d6ddSJeff Bonwick int spa_mode_global;
238fa9e4066Sahrens 
239fa9e4066Sahrens #ifdef ZFS_DEBUG
24040feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
24140feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
242fa9e4066Sahrens #else
243fa9e4066Sahrens int zfs_flags = 0;
244fa9e4066Sahrens #endif
245fa9e4066Sahrens 
2460125049cSahrens /*
2470125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2480125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2490125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2500125049cSahrens  */
2510125049cSahrens int zfs_recover = 0;
2520125049cSahrens 
253283b8460SGeorge.Wilson extern int zfs_txg_synctime_ms;
254283b8460SGeorge.Wilson 
255283b8460SGeorge.Wilson /*
256283b8460SGeorge.Wilson  * Expiration time in units of zfs_txg_synctime_ms. This value has two
257283b8460SGeorge.Wilson  * meanings. First it is used to determine when the spa_deadman logic
258283b8460SGeorge.Wilson  * should fire. By default the spa_deadman will fire if spa_sync has
259283b8460SGeorge.Wilson  * not completed in 1000 * zfs_txg_synctime_ms (i.e. 1000 seconds).
260283b8460SGeorge.Wilson  * Secondly, the value determines if an I/O is considered "hung".
261283b8460SGeorge.Wilson  * Any I/O that has not completed in zfs_deadman_synctime is considered
262283b8460SGeorge.Wilson  * "hung" resulting in a system panic.
263283b8460SGeorge.Wilson  */
264283b8460SGeorge.Wilson uint64_t zfs_deadman_synctime = 1000ULL;
265283b8460SGeorge.Wilson 
266283b8460SGeorge.Wilson /*
267283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
268283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
269283b8460SGeorge.Wilson  */
270283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
271283b8460SGeorge.Wilson 
272fa9e4066Sahrens 
273e05725b1Sbonwick /*
274e05725b1Sbonwick  * ==========================================================================
275e05725b1Sbonwick  * SPA config locking
276e05725b1Sbonwick  * ==========================================================================
277e05725b1Sbonwick  */
278e05725b1Sbonwick static void
279e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
280e14bb325SJeff Bonwick {
281e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
282e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
283e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
284e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
285e14bb325SJeff Bonwick 		refcount_create(&scl->scl_count);
286e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
287e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
288e14bb325SJeff Bonwick 	}
289e05725b1Sbonwick }
290e05725b1Sbonwick 
291e05725b1Sbonwick static void
292e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
293e14bb325SJeff Bonwick {
294e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
295e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
296e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
297e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
298e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
299e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
300e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
301e14bb325SJeff Bonwick 	}
302e14bb325SJeff Bonwick }
303e14bb325SJeff Bonwick 
304e14bb325SJeff Bonwick int
305e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
306e05725b1Sbonwick {
307e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
308e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
309e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
310e14bb325SJeff Bonwick 			continue;
311e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
312e14bb325SJeff Bonwick 		if (rw == RW_READER) {
313e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
314e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
315e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
316e14bb325SJeff Bonwick 				return (0);
317e14bb325SJeff Bonwick 			}
318e14bb325SJeff Bonwick 		} else {
319e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
320e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
321e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
322e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
323e14bb325SJeff Bonwick 				return (0);
324e14bb325SJeff Bonwick 			}
325e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
326e14bb325SJeff Bonwick 		}
327e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
328e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
329e14bb325SJeff Bonwick 	}
330e14bb325SJeff Bonwick 	return (1);
331e05725b1Sbonwick }
332e05725b1Sbonwick 
333e05725b1Sbonwick void
334e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
335e05725b1Sbonwick {
336f64c0e34SEric Taylor 	int wlocks_held = 0;
337f64c0e34SEric Taylor 
338e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
339e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
340f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
341f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
342e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
343e14bb325SJeff Bonwick 			continue;
344e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
345e14bb325SJeff Bonwick 		if (rw == RW_READER) {
346e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
347e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
348e14bb325SJeff Bonwick 			}
349e14bb325SJeff Bonwick 		} else {
350e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
351e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
352e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
353e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
354e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
355e14bb325SJeff Bonwick 			}
356e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
357e14bb325SJeff Bonwick 		}
358e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
359e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
360e05725b1Sbonwick 	}
361f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
362e05725b1Sbonwick }
363e05725b1Sbonwick 
364e05725b1Sbonwick void
365e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
366e05725b1Sbonwick {
367e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
368e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
369e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
370e14bb325SJeff Bonwick 			continue;
371e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
372e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
373e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
374e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
375e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
376e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
377e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
378e14bb325SJeff Bonwick 		}
379e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
380e05725b1Sbonwick 	}
381e05725b1Sbonwick }
382e05725b1Sbonwick 
383e14bb325SJeff Bonwick int
384e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
385e05725b1Sbonwick {
386e14bb325SJeff Bonwick 	int locks_held = 0;
387e05725b1Sbonwick 
388e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
389e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
390e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
391e14bb325SJeff Bonwick 			continue;
392e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
393e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
394e14bb325SJeff Bonwick 			locks_held |= 1 << i;
395e14bb325SJeff Bonwick 	}
396e14bb325SJeff Bonwick 
397e14bb325SJeff Bonwick 	return (locks_held);
398e05725b1Sbonwick }
399e05725b1Sbonwick 
400fa9e4066Sahrens /*
401fa9e4066Sahrens  * ==========================================================================
402fa9e4066Sahrens  * SPA namespace functions
403fa9e4066Sahrens  * ==========================================================================
404fa9e4066Sahrens  */
405fa9e4066Sahrens 
406fa9e4066Sahrens /*
407fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
408fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
409fa9e4066Sahrens  */
410fa9e4066Sahrens spa_t *
411fa9e4066Sahrens spa_lookup(const char *name)
412fa9e4066Sahrens {
413e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
414e14bb325SJeff Bonwick 	spa_t *spa;
415fa9e4066Sahrens 	avl_index_t where;
41640feaa91Sahrens 	char c;
41740feaa91Sahrens 	char *cp;
418fa9e4066Sahrens 
419fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
420fa9e4066Sahrens 
42140feaa91Sahrens 	/*
42240feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
42340feaa91Sahrens 	 * just use that.
42440feaa91Sahrens 	 */
42540feaa91Sahrens 	cp = strpbrk(name, "/@");
42640feaa91Sahrens 	if (cp) {
42740feaa91Sahrens 		c = *cp;
42840feaa91Sahrens 		*cp = '\0';
42940feaa91Sahrens 	}
43040feaa91Sahrens 
431e14bb325SJeff Bonwick 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
432fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
433fa9e4066Sahrens 
43440feaa91Sahrens 	if (cp)
43540feaa91Sahrens 		*cp = c;
43640feaa91Sahrens 
437fa9e4066Sahrens 	return (spa);
438fa9e4066Sahrens }
439fa9e4066Sahrens 
440283b8460SGeorge.Wilson /*
441283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
442283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
443283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
444283b8460SGeorge.Wilson  */
445283b8460SGeorge.Wilson void
446283b8460SGeorge.Wilson spa_deadman(void *arg)
447283b8460SGeorge.Wilson {
448283b8460SGeorge.Wilson 	spa_t *spa = arg;
449283b8460SGeorge.Wilson 
450283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
451283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
452283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
453283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
454283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
455283b8460SGeorge.Wilson }
456283b8460SGeorge.Wilson 
457fa9e4066Sahrens /*
458fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
459fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
460fa9e4066Sahrens  * exist by calling spa_lookup() first.
461fa9e4066Sahrens  */
462fa9e4066Sahrens spa_t *
463468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
464fa9e4066Sahrens {
465fa9e4066Sahrens 	spa_t *spa;
466c5904d13Seschrock 	spa_config_dirent_t *dp;
467283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
468283b8460SGeorge.Wilson 	cyc_time_t when;
469fa9e4066Sahrens 
470fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
471fa9e4066Sahrens 
472fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
473fa9e4066Sahrens 
474c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
475c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
47635a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
477c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
47835a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
479c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
48035a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
481a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
482a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
483*c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
484c25056deSgw 
485c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
48635a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
487c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
488e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
489c25056deSgw 
490b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
491cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
492b24ab676SJeff Bonwick 
493e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
494fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
495fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
4960373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
497468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
49835a5a358SJonathan Adams 	spa->spa_proc = &p0;
49935a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
500fa9e4066Sahrens 
501283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
502283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
503283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
504283b8460SGeorge.Wilson 
505283b8460SGeorge.Wilson 	spa->spa_deadman_synctime = zfs_deadman_synctime *
506283b8460SGeorge.Wilson 	    zfs_txg_synctime_ms * MICROSEC;
507283b8460SGeorge.Wilson 
508283b8460SGeorge.Wilson 	/*
509283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
510283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
511283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
512283b8460SGeorge.Wilson 	 * Instead wait for 5 synctimes before checking again.
513283b8460SGeorge.Wilson 	 */
514283b8460SGeorge.Wilson 	when.cyt_interval = 5ULL * zfs_txg_synctime_ms * MICROSEC;
515283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
516283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
517283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
518283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
519283b8460SGeorge.Wilson 
520fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
521e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
522fa9e4066Sahrens 
523fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
524fa9e4066Sahrens 
5250373e76bSbonwick 	/*
5260373e76bSbonwick 	 * Set the alternate root, if there is one.
5270373e76bSbonwick 	 */
5280373e76bSbonwick 	if (altroot) {
5290373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
5300373e76bSbonwick 		spa_active_count++;
5310373e76bSbonwick 	}
5320373e76bSbonwick 
533c5904d13Seschrock 	/*
534c5904d13Seschrock 	 * Every pool starts with the default cachefile
535c5904d13Seschrock 	 */
536c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
537c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
538c5904d13Seschrock 
539c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
540ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
541c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
542c5904d13Seschrock 
5434b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
5444b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
5454b964adaSGeorge Wilson 
546ad135b5dSChristopher Siden 	if (config != NULL) {
547ad135b5dSChristopher Siden 		nvlist_t *features;
548ad135b5dSChristopher Siden 
549ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
550ad135b5dSChristopher Siden 		    &features) == 0) {
551ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
552ad135b5dSChristopher Siden 			    0) == 0);
553ad135b5dSChristopher Siden 		}
554ad135b5dSChristopher Siden 
555468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
556ad135b5dSChristopher Siden 	}
557ad135b5dSChristopher Siden 
558ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
559ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
560ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
561ad135b5dSChristopher Siden 	}
562468c413aSTim Haley 
563*c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
564*c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
565*c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
566*c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
567*c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
568*c3a66015SMatthew Ahrens 	}
569*c3a66015SMatthew Ahrens 
570fa9e4066Sahrens 	return (spa);
571fa9e4066Sahrens }
572fa9e4066Sahrens 
573fa9e4066Sahrens /*
574fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
575fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
576fa9e4066Sahrens  * deactivated.
577fa9e4066Sahrens  */
578fa9e4066Sahrens void
579fa9e4066Sahrens spa_remove(spa_t *spa)
580fa9e4066Sahrens {
581c5904d13Seschrock 	spa_config_dirent_t *dp;
582c5904d13Seschrock 
583fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
584fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
585fa9e4066Sahrens 
5861195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
5871195e687SMark J Musante 
588fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
589fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
590fa9e4066Sahrens 
5910373e76bSbonwick 	if (spa->spa_root) {
592fa9e4066Sahrens 		spa_strfree(spa->spa_root);
5930373e76bSbonwick 		spa_active_count--;
5940373e76bSbonwick 	}
595fa9e4066Sahrens 
596c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
597c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
598c5904d13Seschrock 		if (dp->scd_path != NULL)
599c5904d13Seschrock 			spa_strfree(dp->scd_path);
600c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
601c5904d13Seschrock 	}
602c5904d13Seschrock 
603c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
6042f8aaab3Seschrock 
605ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
6064b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
607fa9e4066Sahrens 	spa_config_set(spa, NULL);
608fa9e4066Sahrens 
609283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
610283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
611283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
612283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
613283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
614283b8460SGeorge.Wilson 
615fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
61691ebeef5Sahrens 
617e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
618fa9e4066Sahrens 
619*c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
620*c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
621*c3a66015SMatthew Ahrens 
622b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
623cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
624b24ab676SJeff Bonwick 
625c25056deSgw 	cv_destroy(&spa->spa_async_cv);
62635a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
627c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
628e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
629c25056deSgw 
6305ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
631c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
63235a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
63306eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
63435a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
635b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
63635a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
637e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
638a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
639*c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
6405ad82045Snd 
641fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
642fa9e4066Sahrens }
643fa9e4066Sahrens 
644fa9e4066Sahrens /*
645fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
646fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
647fa9e4066Sahrens  */
648fa9e4066Sahrens spa_t *
649fa9e4066Sahrens spa_next(spa_t *prev)
650fa9e4066Sahrens {
651fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
652fa9e4066Sahrens 
653fa9e4066Sahrens 	if (prev)
654fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
655fa9e4066Sahrens 	else
656fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
657fa9e4066Sahrens }
658fa9e4066Sahrens 
659fa9e4066Sahrens /*
660fa9e4066Sahrens  * ==========================================================================
661fa9e4066Sahrens  * SPA refcount functions
662fa9e4066Sahrens  * ==========================================================================
663fa9e4066Sahrens  */
664fa9e4066Sahrens 
665fa9e4066Sahrens /*
666fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
667fa9e4066Sahrens  * have the namespace lock held.
668fa9e4066Sahrens  */
669fa9e4066Sahrens void
670fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
671fa9e4066Sahrens {
672088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
673fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
674fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
675fa9e4066Sahrens }
676fa9e4066Sahrens 
677fa9e4066Sahrens /*
678fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
679fa9e4066Sahrens  * have the namespace lock held.
680fa9e4066Sahrens  */
681fa9e4066Sahrens void
682fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
683fa9e4066Sahrens {
684088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
685fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
686fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
687fa9e4066Sahrens }
688fa9e4066Sahrens 
689fa9e4066Sahrens /*
690fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
691088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
692fa9e4066Sahrens  * number of references acquired when opening a pool
693fa9e4066Sahrens  */
694fa9e4066Sahrens boolean_t
695fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
696fa9e4066Sahrens {
697fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
698fa9e4066Sahrens 
699088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
700fa9e4066Sahrens }
701fa9e4066Sahrens 
70299653d4eSeschrock /*
70399653d4eSeschrock  * ==========================================================================
704fa94a07fSbrendan  * SPA spare and l2cache tracking
70599653d4eSeschrock  * ==========================================================================
70699653d4eSeschrock  */
70799653d4eSeschrock 
708fa94a07fSbrendan /*
709fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
710fa94a07fSbrendan  * for 'auxiliary' devices.
711fa94a07fSbrendan  */
712fa94a07fSbrendan 
713fa94a07fSbrendan typedef struct spa_aux {
714fa94a07fSbrendan 	uint64_t	aux_guid;
715fa94a07fSbrendan 	uint64_t	aux_pool;
716fa94a07fSbrendan 	avl_node_t	aux_avl;
717fa94a07fSbrendan 	int		aux_count;
718fa94a07fSbrendan } spa_aux_t;
719fa94a07fSbrendan 
720fa94a07fSbrendan static int
721fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
722fa94a07fSbrendan {
723fa94a07fSbrendan 	const spa_aux_t *sa = a;
724fa94a07fSbrendan 	const spa_aux_t *sb = b;
725fa94a07fSbrendan 
726fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
727fa94a07fSbrendan 		return (-1);
728fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
729fa94a07fSbrendan 		return (1);
730fa94a07fSbrendan 	else
731fa94a07fSbrendan 		return (0);
732fa94a07fSbrendan }
733fa94a07fSbrendan 
734fa94a07fSbrendan void
735fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
736fa94a07fSbrendan {
737fa94a07fSbrendan 	avl_index_t where;
738fa94a07fSbrendan 	spa_aux_t search;
739fa94a07fSbrendan 	spa_aux_t *aux;
740fa94a07fSbrendan 
741fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
742fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
743fa94a07fSbrendan 		aux->aux_count++;
744fa94a07fSbrendan 	} else {
745fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
746fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
747fa94a07fSbrendan 		aux->aux_count = 1;
748fa94a07fSbrendan 		avl_insert(avl, aux, where);
749fa94a07fSbrendan 	}
750fa94a07fSbrendan }
751fa94a07fSbrendan 
752fa94a07fSbrendan void
753fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
754fa94a07fSbrendan {
755fa94a07fSbrendan 	spa_aux_t search;
756fa94a07fSbrendan 	spa_aux_t *aux;
757fa94a07fSbrendan 	avl_index_t where;
758fa94a07fSbrendan 
759fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
760fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
761fa94a07fSbrendan 
762fa94a07fSbrendan 	ASSERT(aux != NULL);
763fa94a07fSbrendan 
764fa94a07fSbrendan 	if (--aux->aux_count == 0) {
765fa94a07fSbrendan 		avl_remove(avl, aux);
766fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
767fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
768fa94a07fSbrendan 		aux->aux_pool = 0ULL;
769fa94a07fSbrendan 	}
770fa94a07fSbrendan }
771fa94a07fSbrendan 
772fa94a07fSbrendan boolean_t
77389a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
774fa94a07fSbrendan {
775fa94a07fSbrendan 	spa_aux_t search, *found;
776fa94a07fSbrendan 
777fa94a07fSbrendan 	search.aux_guid = guid;
77889a89ebfSlling 	found = avl_find(avl, &search, NULL);
779fa94a07fSbrendan 
780fa94a07fSbrendan 	if (pool) {
781fa94a07fSbrendan 		if (found)
782fa94a07fSbrendan 			*pool = found->aux_pool;
783fa94a07fSbrendan 		else
784fa94a07fSbrendan 			*pool = 0ULL;
785fa94a07fSbrendan 	}
786fa94a07fSbrendan 
78789a89ebfSlling 	if (refcnt) {
78889a89ebfSlling 		if (found)
78989a89ebfSlling 			*refcnt = found->aux_count;
79089a89ebfSlling 		else
79189a89ebfSlling 			*refcnt = 0;
79289a89ebfSlling 	}
79389a89ebfSlling 
794fa94a07fSbrendan 	return (found != NULL);
795fa94a07fSbrendan }
796fa94a07fSbrendan 
797fa94a07fSbrendan void
798fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
799fa94a07fSbrendan {
800fa94a07fSbrendan 	spa_aux_t search, *found;
801fa94a07fSbrendan 	avl_index_t where;
802fa94a07fSbrendan 
803fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
804fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
805fa94a07fSbrendan 	ASSERT(found != NULL);
806fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
807fa94a07fSbrendan 
808fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
809fa94a07fSbrendan }
810fa94a07fSbrendan 
81199653d4eSeschrock /*
81239c23413Seschrock  * Spares are tracked globally due to the following constraints:
81339c23413Seschrock  *
81439c23413Seschrock  * 	- A spare may be part of multiple pools.
81539c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
81639c23413Seschrock  *	  another pool.
81739c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
81839c23413Seschrock  *	  the target is a spare in the same pool.
81939c23413Seschrock  *
82039c23413Seschrock  * We keep track of all spares on the system through the use of a reference
82139c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
82239c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
82339c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
82439c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
82539c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
82639c23413Seschrock  *
82739c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
82839c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
82939c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
83039c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
83199653d4eSeschrock  */
83239c23413Seschrock 
83399653d4eSeschrock static int
83499653d4eSeschrock spa_spare_compare(const void *a, const void *b)
83599653d4eSeschrock {
836fa94a07fSbrendan 	return (spa_aux_compare(a, b));
83799653d4eSeschrock }
83899653d4eSeschrock 
83999653d4eSeschrock void
84039c23413Seschrock spa_spare_add(vdev_t *vd)
84199653d4eSeschrock {
84299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
84339c23413Seschrock 	ASSERT(!vd->vdev_isspare);
844fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
84539c23413Seschrock 	vd->vdev_isspare = B_TRUE;
84699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
84799653d4eSeschrock }
84899653d4eSeschrock 
84999653d4eSeschrock void
85039c23413Seschrock spa_spare_remove(vdev_t *vd)
85199653d4eSeschrock {
85299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
85339c23413Seschrock 	ASSERT(vd->vdev_isspare);
854fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
85539c23413Seschrock 	vd->vdev_isspare = B_FALSE;
85699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
85799653d4eSeschrock }
85899653d4eSeschrock 
85999653d4eSeschrock boolean_t
86089a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
86199653d4eSeschrock {
862fa94a07fSbrendan 	boolean_t found;
86399653d4eSeschrock 
86499653d4eSeschrock 	mutex_enter(&spa_spare_lock);
86589a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
86699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
86799653d4eSeschrock 
868fa94a07fSbrendan 	return (found);
86939c23413Seschrock }
87039c23413Seschrock 
87139c23413Seschrock void
87239c23413Seschrock spa_spare_activate(vdev_t *vd)
87339c23413Seschrock {
87439c23413Seschrock 	mutex_enter(&spa_spare_lock);
87539c23413Seschrock 	ASSERT(vd->vdev_isspare);
876fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
877fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
878fa94a07fSbrendan }
87939c23413Seschrock 
880fa94a07fSbrendan /*
881fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
882fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
883fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
884fa94a07fSbrendan  */
88539c23413Seschrock 
886fa94a07fSbrendan static int
887fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
888fa94a07fSbrendan {
889fa94a07fSbrendan 	return (spa_aux_compare(a, b));
890fa94a07fSbrendan }
891fa94a07fSbrendan 
892fa94a07fSbrendan void
893fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
894fa94a07fSbrendan {
895fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
896fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
897fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
898fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
899fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
900fa94a07fSbrendan }
901fa94a07fSbrendan 
902fa94a07fSbrendan void
903fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
904fa94a07fSbrendan {
905fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
906fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
907fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
908fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
909fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
910fa94a07fSbrendan }
911fa94a07fSbrendan 
912fa94a07fSbrendan boolean_t
913fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
914fa94a07fSbrendan {
915fa94a07fSbrendan 	boolean_t found;
916fa94a07fSbrendan 
917fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
91889a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
919fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
920fa94a07fSbrendan 
921fa94a07fSbrendan 	return (found);
922fa94a07fSbrendan }
923fa94a07fSbrendan 
924fa94a07fSbrendan void
925fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
926fa94a07fSbrendan {
927fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
928fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
929fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
930fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
931fa94a07fSbrendan }
932fa94a07fSbrendan 
933fa9e4066Sahrens /*
934fa9e4066Sahrens  * ==========================================================================
935fa9e4066Sahrens  * SPA vdev locking
936fa9e4066Sahrens  * ==========================================================================
937fa9e4066Sahrens  */
938fa9e4066Sahrens 
939fa9e4066Sahrens /*
940ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
941ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
942fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
943fa9e4066Sahrens  */
944fa9e4066Sahrens uint64_t
945fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
946fa9e4066Sahrens {
947a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
948bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
94988ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
95088ecc943SGeorge Wilson }
95188ecc943SGeorge Wilson 
95288ecc943SGeorge Wilson /*
95388ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
95488ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
95588ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
95688ecc943SGeorge Wilson  */
95788ecc943SGeorge Wilson uint64_t
95888ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
95988ecc943SGeorge Wilson {
96088ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
9613d7072f8Seschrock 
962e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
963fa9e4066Sahrens 
964fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
965fa9e4066Sahrens }
966fa9e4066Sahrens 
967fa9e4066Sahrens /*
96888ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
96988ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
970fa9e4066Sahrens  */
97188ecc943SGeorge Wilson void
97288ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
973fa9e4066Sahrens {
97488ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
97588ecc943SGeorge Wilson 
9760e34b6a7Sbonwick 	int config_changed = B_FALSE;
977ea8dc4b6Seschrock 
9780373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
9790e34b6a7Sbonwick 
980e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
981e14bb325SJeff Bonwick 
9820e34b6a7Sbonwick 	/*
9830e34b6a7Sbonwick 	 * Reassess the DTLs.
9840e34b6a7Sbonwick 	 */
9850373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
9860e34b6a7Sbonwick 
987e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
9880e34b6a7Sbonwick 		config_changed = B_TRUE;
9898f18d1faSGeorge Wilson 		spa->spa_config_generation++;
9900e34b6a7Sbonwick 	}
991ea8dc4b6Seschrock 
99288ecc943SGeorge Wilson 	/*
99388ecc943SGeorge Wilson 	 * Verify the metaslab classes.
99488ecc943SGeorge Wilson 	 */
995b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
996b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
99788ecc943SGeorge Wilson 
998e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
999fa9e4066Sahrens 
100088ecc943SGeorge Wilson 	/*
100188ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
100288ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
100388ecc943SGeorge Wilson 	 * transactionally.
100488ecc943SGeorge Wilson 	 */
100588ecc943SGeorge Wilson 	if (zio_injection_enabled)
10061195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
100788ecc943SGeorge Wilson 
1008fa9e4066Sahrens 	/*
1009fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1010fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1011fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1012fa9e4066Sahrens 	 */
1013fa9e4066Sahrens 	if (error == 0)
1014fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1015fa9e4066Sahrens 
1016fa9e4066Sahrens 	if (vd != NULL) {
10178ad4d6ddSJeff Bonwick 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
10188ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1019fa9e4066Sahrens 		vdev_free(vd);
10208ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1021fa9e4066Sahrens 	}
1022fa9e4066Sahrens 
1023fa9e4066Sahrens 	/*
10240e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1025fa9e4066Sahrens 	 */
10260e34b6a7Sbonwick 	if (config_changed)
1027c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
102888ecc943SGeorge Wilson }
1029ea8dc4b6Seschrock 
103088ecc943SGeorge Wilson /*
103188ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
103288ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
103388ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
103488ecc943SGeorge Wilson  * information.
103588ecc943SGeorge Wilson  */
103688ecc943SGeorge Wilson int
103788ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
103888ecc943SGeorge Wilson {
103988ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1040ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1041bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1042fa9e4066Sahrens 
1043fa9e4066Sahrens 	return (error);
1044fa9e4066Sahrens }
1045fa9e4066Sahrens 
1046e14bb325SJeff Bonwick /*
1047e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1048e14bb325SJeff Bonwick  */
1049e14bb325SJeff Bonwick void
10508f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1051e14bb325SJeff Bonwick {
10528f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
10538f18d1faSGeorge Wilson 
1054dcba9f3fSGeorge Wilson 	/*
1055dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1056dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1057dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1058dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1059dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1060dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1061dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1062dcba9f3fSGeorge Wilson 	 */
1063dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
10649842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
10659842588bSGeorge Wilson 		int high = locks & ~low;
10669842588bSGeorge Wilson 
10679842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1068dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
10699842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1070dcba9f3fSGeorge Wilson 	} else {
1071dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1072dcba9f3fSGeorge Wilson 	}
10738f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1074e14bb325SJeff Bonwick }
1075e14bb325SJeff Bonwick 
1076e14bb325SJeff Bonwick int
1077e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1078e14bb325SJeff Bonwick {
1079c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1080c6065d0fSGeorge Wilson 
1081b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1082b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1083b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1084b24ab676SJeff Bonwick 
10858f18d1faSGeorge Wilson 	if (vd != NULL) {
1086e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1087c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
10888f18d1faSGeorge Wilson 		spa->spa_config_generation++;
10898f18d1faSGeorge Wilson 	}
1090e14bb325SJeff Bonwick 
1091dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1092dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1093dcba9f3fSGeorge Wilson 
10948f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
10958f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1096e14bb325SJeff Bonwick 
10978ad4d6ddSJeff Bonwick 	/*
10988ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
10998ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
11008ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
11018ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
11028ad4d6ddSJeff Bonwick 	 */
11038ad4d6ddSJeff Bonwick 	if (vd != NULL)
11048ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
11058ad4d6ddSJeff Bonwick 
1106c6065d0fSGeorge Wilson 	/*
1107c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1108c6065d0fSGeorge Wilson 	 */
1109c6065d0fSGeorge Wilson 	if (config_changed) {
1110c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1111c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1112c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1113c6065d0fSGeorge Wilson 	}
1114c6065d0fSGeorge Wilson 
1115e14bb325SJeff Bonwick 	return (error);
1116e14bb325SJeff Bonwick }
1117e14bb325SJeff Bonwick 
1118fa9e4066Sahrens /*
1119fa9e4066Sahrens  * ==========================================================================
1120fa9e4066Sahrens  * Miscellaneous functions
1121fa9e4066Sahrens  * ==========================================================================
1122fa9e4066Sahrens  */
1123fa9e4066Sahrens 
1124ad135b5dSChristopher Siden void
1125ad135b5dSChristopher Siden spa_activate_mos_feature(spa_t *spa, const char *feature)
1126ad135b5dSChristopher Siden {
1127ad135b5dSChristopher Siden 	(void) nvlist_add_boolean(spa->spa_label_features, feature);
1128ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1129ad135b5dSChristopher Siden }
1130ad135b5dSChristopher Siden 
1131ad135b5dSChristopher Siden void
1132ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1133ad135b5dSChristopher Siden {
1134ad135b5dSChristopher Siden 	(void) nvlist_remove_all(spa->spa_label_features, feature);
1135ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1136ad135b5dSChristopher Siden }
1137ad135b5dSChristopher Siden 
1138fa9e4066Sahrens /*
1139fa9e4066Sahrens  * Rename a spa_t.
1140fa9e4066Sahrens  */
1141fa9e4066Sahrens int
1142fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1143fa9e4066Sahrens {
1144fa9e4066Sahrens 	spa_t *spa;
1145fa9e4066Sahrens 	int err;
1146fa9e4066Sahrens 
1147fa9e4066Sahrens 	/*
1148fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1149fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1150fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1151ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1152fa9e4066Sahrens 	 */
1153fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1154fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1155fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1156fa9e4066Sahrens 		return (err);
1157fa9e4066Sahrens 	}
1158fa9e4066Sahrens 
1159e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1160fa9e4066Sahrens 
1161fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1162e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1163fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1164fa9e4066Sahrens 
1165fa9e4066Sahrens 	/*
1166fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1167fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1168fa9e4066Sahrens 	 * during the sync.
1169fa9e4066Sahrens 	 */
1170fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1171fa9e4066Sahrens 
1172e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1173fa9e4066Sahrens 
11740373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1175fa9e4066Sahrens 
1176fa9e4066Sahrens 	/*
1177fa9e4066Sahrens 	 * Sync the updated config cache.
1178fa9e4066Sahrens 	 */
1179c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1180fa9e4066Sahrens 
1181fa9e4066Sahrens 	spa_close(spa, FTAG);
1182fa9e4066Sahrens 
1183fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1184fa9e4066Sahrens 
1185fa9e4066Sahrens 	return (0);
1186fa9e4066Sahrens }
1187fa9e4066Sahrens 
1188fa9e4066Sahrens /*
1189f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1190f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1191f9af39baSGeorge Wilson  * a device with the specified device_guid.
1192fa9e4066Sahrens  */
1193f9af39baSGeorge Wilson spa_t *
1194f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1195fa9e4066Sahrens {
1196fa9e4066Sahrens 	spa_t *spa;
1197fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1198fa9e4066Sahrens 
1199ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1200fa9e4066Sahrens 
1201fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1202fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1203fa9e4066Sahrens 			continue;
1204fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1205fa9e4066Sahrens 			continue;
120639c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
120739c23413Seschrock 			if (device_guid == 0)
120839c23413Seschrock 				break;
120939c23413Seschrock 
121039c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
121139c23413Seschrock 			    device_guid) != NULL)
121239c23413Seschrock 				break;
121339c23413Seschrock 
121439c23413Seschrock 			/*
12158654d025Sperrin 			 * Check any devices we may be in the process of adding.
121639c23413Seschrock 			 */
121739c23413Seschrock 			if (spa->spa_pending_vdev) {
121839c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
121939c23413Seschrock 				    device_guid) != NULL)
122039c23413Seschrock 					break;
122139c23413Seschrock 			}
122239c23413Seschrock 		}
1223fa9e4066Sahrens 	}
1224fa9e4066Sahrens 
1225f9af39baSGeorge Wilson 	return (spa);
1226f9af39baSGeorge Wilson }
1227f9af39baSGeorge Wilson 
1228f9af39baSGeorge Wilson /*
1229f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1230f9af39baSGeorge Wilson  */
1231f9af39baSGeorge Wilson boolean_t
1232f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1233f9af39baSGeorge Wilson {
1234f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1235fa9e4066Sahrens }
1236fa9e4066Sahrens 
1237fa9e4066Sahrens char *
1238fa9e4066Sahrens spa_strdup(const char *s)
1239fa9e4066Sahrens {
1240fa9e4066Sahrens 	size_t len;
1241fa9e4066Sahrens 	char *new;
1242fa9e4066Sahrens 
1243fa9e4066Sahrens 	len = strlen(s);
1244fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1245fa9e4066Sahrens 	bcopy(s, new, len);
1246fa9e4066Sahrens 	new[len] = '\0';
1247fa9e4066Sahrens 
1248fa9e4066Sahrens 	return (new);
1249fa9e4066Sahrens }
1250fa9e4066Sahrens 
1251fa9e4066Sahrens void
1252fa9e4066Sahrens spa_strfree(char *s)
1253fa9e4066Sahrens {
1254fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1255fa9e4066Sahrens }
1256fa9e4066Sahrens 
1257fa9e4066Sahrens uint64_t
1258fa9e4066Sahrens spa_get_random(uint64_t range)
1259fa9e4066Sahrens {
1260fa9e4066Sahrens 	uint64_t r;
1261fa9e4066Sahrens 
1262fa9e4066Sahrens 	ASSERT(range != 0);
1263fa9e4066Sahrens 
1264fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1265fa9e4066Sahrens 
1266fa9e4066Sahrens 	return (r % range);
1267fa9e4066Sahrens }
1268fa9e4066Sahrens 
12691195e687SMark J Musante uint64_t
12701195e687SMark J Musante spa_generate_guid(spa_t *spa)
12711195e687SMark J Musante {
12721195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
12731195e687SMark J Musante 
12741195e687SMark J Musante 	if (spa != NULL) {
12751195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
12761195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12771195e687SMark J Musante 	} else {
12781195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
12791195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12801195e687SMark J Musante 	}
12811195e687SMark J Musante 
12821195e687SMark J Musante 	return (guid);
12831195e687SMark J Musante }
12841195e687SMark J Musante 
1285fa9e4066Sahrens void
1286b24ab676SJeff Bonwick sprintf_blkptr(char *buf, const blkptr_t *bp)
1287fa9e4066Sahrens {
1288ad135b5dSChristopher Siden 	char type[256];
1289f0ba89beSJeff Bonwick 	char *checksum = NULL;
1290f0ba89beSJeff Bonwick 	char *compress = NULL;
1291f0ba89beSJeff Bonwick 
1292f0ba89beSJeff Bonwick 	if (bp != NULL) {
1293ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1294ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1295ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1296ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1297ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1298ad135b5dSChristopher Siden 			    "metadata" : "data",
1299ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1300ad135b5dSChristopher Siden 		} else {
1301ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1302ad135b5dSChristopher Siden 			    sizeof (type));
1303ad135b5dSChristopher Siden 		}
1304f0ba89beSJeff Bonwick 		checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1305f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1306f0ba89beSJeff Bonwick 	}
1307fa9e4066Sahrens 
1308b24ab676SJeff Bonwick 	SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1309fa9e4066Sahrens }
1310fa9e4066Sahrens 
1311fa9e4066Sahrens void
1312fa9e4066Sahrens spa_freeze(spa_t *spa)
1313fa9e4066Sahrens {
1314fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1315fa9e4066Sahrens 
1316e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1317fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1318fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1319fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1320fa9e4066Sahrens 	}
1321e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1322fa9e4066Sahrens 	if (freeze_txg != 0)
1323fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1324fa9e4066Sahrens }
1325fa9e4066Sahrens 
13260125049cSahrens void
13270125049cSahrens zfs_panic_recover(const char *fmt, ...)
13280125049cSahrens {
13290125049cSahrens 	va_list adx;
13300125049cSahrens 
13310125049cSahrens 	va_start(adx, fmt);
13320125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
13330125049cSahrens 	va_end(adx);
13340125049cSahrens }
13350125049cSahrens 
13363f9d6ad7SLin Ling /*
13373f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
13383f9d6ad7SLin Ling  * lowercase hexidecimal numbers that don't overflow.
13393f9d6ad7SLin Ling  */
13403f9d6ad7SLin Ling uint64_t
13413f9d6ad7SLin Ling strtonum(const char *str, char **nptr)
13423f9d6ad7SLin Ling {
13433f9d6ad7SLin Ling 	uint64_t val = 0;
13443f9d6ad7SLin Ling 	char c;
13453f9d6ad7SLin Ling 	int digit;
13463f9d6ad7SLin Ling 
13473f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
13483f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
13493f9d6ad7SLin Ling 			digit = c - '0';
13503f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
13513f9d6ad7SLin Ling 			digit = 10 + c - 'a';
13523f9d6ad7SLin Ling 		else
13533f9d6ad7SLin Ling 			break;
13543f9d6ad7SLin Ling 
13553f9d6ad7SLin Ling 		val *= 16;
13563f9d6ad7SLin Ling 		val += digit;
13573f9d6ad7SLin Ling 
13583f9d6ad7SLin Ling 		str++;
13593f9d6ad7SLin Ling 	}
13603f9d6ad7SLin Ling 
13613f9d6ad7SLin Ling 	if (nptr)
13623f9d6ad7SLin Ling 		*nptr = (char *)str;
13633f9d6ad7SLin Ling 
13643f9d6ad7SLin Ling 	return (val);
13653f9d6ad7SLin Ling }
13663f9d6ad7SLin Ling 
1367fa9e4066Sahrens /*
1368fa9e4066Sahrens  * ==========================================================================
1369fa9e4066Sahrens  * Accessor functions
1370fa9e4066Sahrens  * ==========================================================================
1371fa9e4066Sahrens  */
1372fa9e4066Sahrens 
1373088f3894Sahrens boolean_t
137488b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1375fa9e4066Sahrens {
137688b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1377fa9e4066Sahrens }
1378fa9e4066Sahrens 
1379fa9e4066Sahrens dsl_pool_t *
1380fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1381fa9e4066Sahrens {
1382fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1383fa9e4066Sahrens }
1384fa9e4066Sahrens 
1385ad135b5dSChristopher Siden boolean_t
1386ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1387ad135b5dSChristopher Siden {
1388ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1389ad135b5dSChristopher Siden }
1390ad135b5dSChristopher Siden 
1391fa9e4066Sahrens blkptr_t *
1392fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1393fa9e4066Sahrens {
1394fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1395fa9e4066Sahrens }
1396fa9e4066Sahrens 
1397fa9e4066Sahrens void
1398fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1399fa9e4066Sahrens {
1400fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1401fa9e4066Sahrens }
1402fa9e4066Sahrens 
1403fa9e4066Sahrens void
1404fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1405fa9e4066Sahrens {
1406fa9e4066Sahrens 	if (spa->spa_root == NULL)
1407fa9e4066Sahrens 		buf[0] = '\0';
1408fa9e4066Sahrens 	else
1409fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1410fa9e4066Sahrens }
1411fa9e4066Sahrens 
1412fa9e4066Sahrens int
1413fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1414fa9e4066Sahrens {
1415fa9e4066Sahrens 	return (spa->spa_sync_pass);
1416fa9e4066Sahrens }
1417fa9e4066Sahrens 
1418fa9e4066Sahrens char *
1419fa9e4066Sahrens spa_name(spa_t *spa)
1420fa9e4066Sahrens {
1421fa9e4066Sahrens 	return (spa->spa_name);
1422fa9e4066Sahrens }
1423fa9e4066Sahrens 
1424fa9e4066Sahrens uint64_t
1425fa9e4066Sahrens spa_guid(spa_t *spa)
1426fa9e4066Sahrens {
1427dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1428dfbb9432SGeorge Wilson 	uint64_t guid;
1429dfbb9432SGeorge Wilson 
1430b5989ec7Seschrock 	/*
1431b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1432b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1433e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1434b5989ec7Seschrock 	 * this case.
1435b5989ec7Seschrock 	 */
1436dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1437dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1438dfbb9432SGeorge Wilson 
1439dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1440dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1441dfbb9432SGeorge Wilson 
1442dfbb9432SGeorge Wilson 	/*
1443dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1444dfbb9432SGeorge Wilson 	 * in syncing context.
1445dfbb9432SGeorge Wilson 	 */
1446dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1447b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1448b5989ec7Seschrock 	else
1449dfbb9432SGeorge Wilson 		return (guid);
1450e9103aaeSGarrett D'Amore }
1451e9103aaeSGarrett D'Amore 
1452e9103aaeSGarrett D'Amore uint64_t
1453e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1454e9103aaeSGarrett D'Amore {
1455e9103aaeSGarrett D'Amore 	/*
1456e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1457e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1458e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1459e9103aaeSGarrett D'Amore 	 */
1460e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1461fa9e4066Sahrens }
1462fa9e4066Sahrens 
1463fa9e4066Sahrens uint64_t
1464fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1465fa9e4066Sahrens {
1466fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1467fa9e4066Sahrens }
1468fa9e4066Sahrens 
1469fa9e4066Sahrens uint64_t
1470fa9e4066Sahrens spa_first_txg(spa_t *spa)
1471fa9e4066Sahrens {
1472fa9e4066Sahrens 	return (spa->spa_first_txg);
1473fa9e4066Sahrens }
1474fa9e4066Sahrens 
1475b24ab676SJeff Bonwick uint64_t
1476b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1477b24ab676SJeff Bonwick {
1478b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1479b24ab676SJeff Bonwick }
1480b24ab676SJeff Bonwick 
148188b7b0f2SMatthew Ahrens pool_state_t
1482fa9e4066Sahrens spa_state(spa_t *spa)
1483fa9e4066Sahrens {
1484fa9e4066Sahrens 	return (spa->spa_state);
1485fa9e4066Sahrens }
1486fa9e4066Sahrens 
1487b16da2e2SGeorge Wilson spa_load_state_t
1488b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1489b16da2e2SGeorge Wilson {
1490b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1491b16da2e2SGeorge Wilson }
1492b16da2e2SGeorge Wilson 
1493fa9e4066Sahrens uint64_t
1494fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1495fa9e4066Sahrens {
1496fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1497fa9e4066Sahrens }
1498fa9e4066Sahrens 
1499fa9e4066Sahrens /* ARGSUSED */
1500fa9e4066Sahrens uint64_t
1501fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1502fa9e4066Sahrens {
1503fa9e4066Sahrens 	/*
1504b24ab676SJeff Bonwick 	 * The worst case is single-sector max-parity RAID-Z blocks, in which
1505b24ab676SJeff Bonwick 	 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1506b24ab676SJeff Bonwick 	 * times the size; so just assume that.  Add to this the fact that
1507b24ab676SJeff Bonwick 	 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1508b24ab676SJeff Bonwick 	 * the block may be dittoed with up to 3 DVAs by ddt_sync().
150944cd46caSbillm 	 */
1510b24ab676SJeff Bonwick 	return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
151144cd46caSbillm }
151244cd46caSbillm 
1513485bbbf5SGeorge Wilson uint64_t
1514485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1515485bbbf5SGeorge Wilson {
1516485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1517485bbbf5SGeorge Wilson }
1518485bbbf5SGeorge Wilson 
1519485bbbf5SGeorge Wilson void
1520485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1521485bbbf5SGeorge Wilson {
1522485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1523485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1524485bbbf5SGeorge Wilson }
1525485bbbf5SGeorge Wilson 
15260a4e9518Sgw /*
15270a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
15280a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
15290a4e9518Sgw  */
15300a4e9518Sgw uint8_t
15310a4e9518Sgw spa_get_failmode(spa_t *spa)
15320a4e9518Sgw {
15330a4e9518Sgw 	return (spa->spa_failmode);
15340a4e9518Sgw }
15350a4e9518Sgw 
1536e14bb325SJeff Bonwick boolean_t
1537e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1538e14bb325SJeff Bonwick {
1539e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1540e14bb325SJeff Bonwick }
1541e14bb325SJeff Bonwick 
154244cd46caSbillm uint64_t
154344cd46caSbillm spa_version(spa_t *spa)
154444cd46caSbillm {
154544cd46caSbillm 	return (spa->spa_ubsync.ub_version);
154644cd46caSbillm }
154744cd46caSbillm 
1548b24ab676SJeff Bonwick boolean_t
1549b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1550b24ab676SJeff Bonwick {
1551b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1552b24ab676SJeff Bonwick }
1553b24ab676SJeff Bonwick 
1554b24ab676SJeff Bonwick metaslab_class_t *
1555b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1556b24ab676SJeff Bonwick {
1557b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1558b24ab676SJeff Bonwick }
1559b24ab676SJeff Bonwick 
1560b24ab676SJeff Bonwick metaslab_class_t *
1561b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1562b24ab676SJeff Bonwick {
1563b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1564b24ab676SJeff Bonwick }
1565b24ab676SJeff Bonwick 
156644cd46caSbillm int
156744cd46caSbillm spa_max_replication(spa_t *spa)
156844cd46caSbillm {
156944cd46caSbillm 	/*
1570e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
157144cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
157244cd46caSbillm 	 * replication level accordingly.
1573fa9e4066Sahrens 	 */
1574e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
157544cd46caSbillm 		return (1);
157644cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1577fa9e4066Sahrens }
1578fa9e4066Sahrens 
15793f9d6ad7SLin Ling int
15803f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
15813f9d6ad7SLin Ling {
15823f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
15833f9d6ad7SLin Ling }
15843f9d6ad7SLin Ling 
1585283b8460SGeorge.Wilson uint64_t
1586283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1587283b8460SGeorge.Wilson {
1588283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1589283b8460SGeorge.Wilson }
1590283b8460SGeorge.Wilson 
159199653d4eSeschrock uint64_t
1592b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
159399653d4eSeschrock {
1594b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1595b24ab676SJeff Bonwick 	uint64_t dsize = asize;
159699653d4eSeschrock 
1597b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
159899653d4eSeschrock 
1599b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1600b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1601b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
160299653d4eSeschrock 	}
1603b24ab676SJeff Bonwick 
1604b24ab676SJeff Bonwick 	return (dsize);
1605b24ab676SJeff Bonwick }
1606b24ab676SJeff Bonwick 
1607b24ab676SJeff Bonwick uint64_t
1608b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1609b24ab676SJeff Bonwick {
1610b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1611b24ab676SJeff Bonwick 
1612b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1613b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1614b24ab676SJeff Bonwick 
1615b24ab676SJeff Bonwick 	return (dsize);
1616b24ab676SJeff Bonwick }
1617b24ab676SJeff Bonwick 
1618b24ab676SJeff Bonwick uint64_t
1619b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1620b24ab676SJeff Bonwick {
1621b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1622b24ab676SJeff Bonwick 
1623b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1624b24ab676SJeff Bonwick 
1625b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1626b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1627b24ab676SJeff Bonwick 
1628e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1629b24ab676SJeff Bonwick 
1630b24ab676SJeff Bonwick 	return (dsize);
163199653d4eSeschrock }
163299653d4eSeschrock 
1633fa9e4066Sahrens /*
1634fa9e4066Sahrens  * ==========================================================================
1635fa9e4066Sahrens  * Initialization and Termination
1636fa9e4066Sahrens  * ==========================================================================
1637fa9e4066Sahrens  */
1638fa9e4066Sahrens 
1639fa9e4066Sahrens static int
1640fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1641fa9e4066Sahrens {
1642fa9e4066Sahrens 	const spa_t *s1 = a1;
1643fa9e4066Sahrens 	const spa_t *s2 = a2;
1644fa9e4066Sahrens 	int s;
1645fa9e4066Sahrens 
1646fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1647fa9e4066Sahrens 	if (s > 0)
1648fa9e4066Sahrens 		return (1);
1649fa9e4066Sahrens 	if (s < 0)
1650fa9e4066Sahrens 		return (-1);
1651fa9e4066Sahrens 	return (0);
1652fa9e4066Sahrens }
1653fa9e4066Sahrens 
16540373e76bSbonwick int
16550373e76bSbonwick spa_busy(void)
16560373e76bSbonwick {
16570373e76bSbonwick 	return (spa_active_count);
16580373e76bSbonwick }
16590373e76bSbonwick 
1660e7cbe64fSgw void
1661e7cbe64fSgw spa_boot_init()
1662e7cbe64fSgw {
1663e7cbe64fSgw 	spa_config_load();
1664e7cbe64fSgw }
1665e7cbe64fSgw 
1666fa9e4066Sahrens void
1667fa9e4066Sahrens spa_init(int mode)
1668fa9e4066Sahrens {
1669fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1670c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1671fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1672fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1673fa9e4066Sahrens 
1674fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1675fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1676fa9e4066Sahrens 
1677fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1678fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1679fa94a07fSbrendan 
1680fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1681fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
168299653d4eSeschrock 
16838ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1684fa9e4066Sahrens 
1685283b8460SGeorge.Wilson #ifdef _KERNEL
1686283b8460SGeorge.Wilson 	spa_arch_init();
1687283b8460SGeorge.Wilson #else
1688cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1689cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1690cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1691cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1692cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1693cd1c8b85SMatthew Ahrens 		} else {
1694cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1695cd1c8b85SMatthew Ahrens 		}
1696cd1c8b85SMatthew Ahrens 	}
1697cd1c8b85SMatthew Ahrens #endif
1698cd1c8b85SMatthew Ahrens 
1699fa9e4066Sahrens 	refcount_init();
1700fa9e4066Sahrens 	unique_init();
170101f55e48SGeorge Wilson 	space_map_init();
1702fa9e4066Sahrens 	zio_init();
1703fa9e4066Sahrens 	dmu_init();
1704fa9e4066Sahrens 	zil_init();
170587db74c1Sek 	vdev_cache_stat_init();
170691ebeef5Sahrens 	zfs_prop_init();
1707990b4856Slling 	zpool_prop_init();
1708ad135b5dSChristopher Siden 	zpool_feature_init();
1709fa9e4066Sahrens 	spa_config_load();
1710e14bb325SJeff Bonwick 	l2arc_start();
1711fa9e4066Sahrens }
1712fa9e4066Sahrens 
1713fa9e4066Sahrens void
1714fa9e4066Sahrens spa_fini(void)
1715fa9e4066Sahrens {
1716e14bb325SJeff Bonwick 	l2arc_stop();
1717e14bb325SJeff Bonwick 
1718fa9e4066Sahrens 	spa_evict_all();
1719fa9e4066Sahrens 
172087db74c1Sek 	vdev_cache_stat_fini();
1721fa9e4066Sahrens 	zil_fini();
1722fa9e4066Sahrens 	dmu_fini();
1723fa9e4066Sahrens 	zio_fini();
172401f55e48SGeorge Wilson 	space_map_fini();
172591ebeef5Sahrens 	unique_fini();
1726fa9e4066Sahrens 	refcount_fini();
1727fa9e4066Sahrens 
1728fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
172999653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1730fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1731fa9e4066Sahrens 
1732fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1733fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1734c25056deSgw 	mutex_destroy(&spa_spare_lock);
1735fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1736fa9e4066Sahrens }
17376ce0521aSperrin 
17386ce0521aSperrin /*
17396ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
17406ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
17416ce0521aSperrin  * performance and not correctness
17426ce0521aSperrin  */
17436ce0521aSperrin boolean_t
17446ce0521aSperrin spa_has_slogs(spa_t *spa)
17456ce0521aSperrin {
17466ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
17476ce0521aSperrin }
1748bf82a41bSeschrock 
1749b24ab676SJeff Bonwick spa_log_state_t
1750b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1751b24ab676SJeff Bonwick {
1752b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1753b24ab676SJeff Bonwick }
1754b24ab676SJeff Bonwick 
1755b24ab676SJeff Bonwick void
1756b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1757b24ab676SJeff Bonwick {
1758b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1759b24ab676SJeff Bonwick }
1760b24ab676SJeff Bonwick 
1761bf82a41bSeschrock boolean_t
1762bf82a41bSeschrock spa_is_root(spa_t *spa)
1763bf82a41bSeschrock {
1764bf82a41bSeschrock 	return (spa->spa_is_root);
1765bf82a41bSeschrock }
17668ad4d6ddSJeff Bonwick 
17678ad4d6ddSJeff Bonwick boolean_t
17688ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
17698ad4d6ddSJeff Bonwick {
17708ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
17718ad4d6ddSJeff Bonwick }
17728ad4d6ddSJeff Bonwick 
17738ad4d6ddSJeff Bonwick int
17748ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
17758ad4d6ddSJeff Bonwick {
17768ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
17778ad4d6ddSJeff Bonwick }
1778b24ab676SJeff Bonwick 
1779b24ab676SJeff Bonwick uint64_t
1780b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1781b24ab676SJeff Bonwick {
1782b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1783b24ab676SJeff Bonwick }
1784b24ab676SJeff Bonwick 
1785b24ab676SJeff Bonwick uint64_t
1786b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1787b24ab676SJeff Bonwick {
1788b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1789b24ab676SJeff Bonwick }
1790b24ab676SJeff Bonwick 
1791b24ab676SJeff Bonwick objset_t *
1792b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1793b24ab676SJeff Bonwick {
1794b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1795b24ab676SJeff Bonwick }
1796b24ab676SJeff Bonwick 
1797b24ab676SJeff Bonwick enum zio_checksum
1798b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1799b24ab676SJeff Bonwick {
1800b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1801b24ab676SJeff Bonwick }
18023f9d6ad7SLin Ling 
18033f9d6ad7SLin Ling /*
18043f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
18053f9d6ad7SLin Ling  */
18063f9d6ad7SLin Ling void
18073f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
18083f9d6ad7SLin Ling {
18093f9d6ad7SLin Ling 	/* data not stored on disk */
18103f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
18113f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
18123f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
18133f9d6ad7SLin Ling }
18143f9d6ad7SLin Ling 
18153f9d6ad7SLin Ling /*
18163f9d6ad7SLin Ling  * Get scan stats for zpool status reports
18173f9d6ad7SLin Ling  */
18183f9d6ad7SLin Ling int
18193f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
18203f9d6ad7SLin Ling {
18213f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
18223f9d6ad7SLin Ling 
18233f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
18243f9d6ad7SLin Ling 		return (ENOENT);
18253f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
18263f9d6ad7SLin Ling 
18273f9d6ad7SLin Ling 	/* data stored on disk */
18283f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
18293f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
18303f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
18313f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
18323f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
18333f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
18343f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
18353f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
18363f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
18373f9d6ad7SLin Ling 
18383f9d6ad7SLin Ling 	/* data not stored on disk */
18393f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
18403f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
18413f9d6ad7SLin Ling 
18423f9d6ad7SLin Ling 	return (0);
18433f9d6ad7SLin Ling }
184409c9d376SGeorge Wilson 
184509c9d376SGeorge Wilson boolean_t
184609c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
184709c9d376SGeorge Wilson {
184809c9d376SGeorge Wilson 	return (spa->spa_debug);
184909c9d376SGeorge Wilson }
1850