xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 283b84606b6fc326692c03273de1774e8c122f9a)
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>
29*283b8460SGeorge.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 
253*283b8460SGeorge.Wilson extern int zfs_txg_synctime_ms;
254*283b8460SGeorge.Wilson 
255*283b8460SGeorge.Wilson /*
256*283b8460SGeorge.Wilson  * Expiration time in units of zfs_txg_synctime_ms. This value has two
257*283b8460SGeorge.Wilson  * meanings. First it is used to determine when the spa_deadman logic
258*283b8460SGeorge.Wilson  * should fire. By default the spa_deadman will fire if spa_sync has
259*283b8460SGeorge.Wilson  * not completed in 1000 * zfs_txg_synctime_ms (i.e. 1000 seconds).
260*283b8460SGeorge.Wilson  * Secondly, the value determines if an I/O is considered "hung".
261*283b8460SGeorge.Wilson  * Any I/O that has not completed in zfs_deadman_synctime is considered
262*283b8460SGeorge.Wilson  * "hung" resulting in a system panic.
263*283b8460SGeorge.Wilson  * 1000 zfs_txg_synctime_ms (i.e. 1000 seconds).
264*283b8460SGeorge.Wilson  */
265*283b8460SGeorge.Wilson uint64_t zfs_deadman_synctime = 1000ULL;
266*283b8460SGeorge.Wilson 
267*283b8460SGeorge.Wilson /*
268*283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
269*283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
270*283b8460SGeorge.Wilson  */
271*283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
272*283b8460SGeorge.Wilson 
273fa9e4066Sahrens 
274e05725b1Sbonwick /*
275e05725b1Sbonwick  * ==========================================================================
276e05725b1Sbonwick  * SPA config locking
277e05725b1Sbonwick  * ==========================================================================
278e05725b1Sbonwick  */
279e05725b1Sbonwick static void
280e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
281e14bb325SJeff Bonwick {
282e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
283e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
284e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
285e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
286e14bb325SJeff Bonwick 		refcount_create(&scl->scl_count);
287e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
288e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
289e14bb325SJeff Bonwick 	}
290e05725b1Sbonwick }
291e05725b1Sbonwick 
292e05725b1Sbonwick static void
293e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
294e14bb325SJeff Bonwick {
295e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
296e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
297e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
298e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
299e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
300e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
301e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
302e14bb325SJeff Bonwick 	}
303e14bb325SJeff Bonwick }
304e14bb325SJeff Bonwick 
305e14bb325SJeff Bonwick int
306e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
307e05725b1Sbonwick {
308e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
309e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
310e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
311e14bb325SJeff Bonwick 			continue;
312e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
313e14bb325SJeff Bonwick 		if (rw == RW_READER) {
314e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
315e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
316e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
317e14bb325SJeff Bonwick 				return (0);
318e14bb325SJeff Bonwick 			}
319e14bb325SJeff Bonwick 		} else {
320e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
321e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
322e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
323e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
324e14bb325SJeff Bonwick 				return (0);
325e14bb325SJeff Bonwick 			}
326e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
327e14bb325SJeff Bonwick 		}
328e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
329e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
330e14bb325SJeff Bonwick 	}
331e14bb325SJeff Bonwick 	return (1);
332e05725b1Sbonwick }
333e05725b1Sbonwick 
334e05725b1Sbonwick void
335e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
336e05725b1Sbonwick {
337f64c0e34SEric Taylor 	int wlocks_held = 0;
338f64c0e34SEric Taylor 
339e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
340e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
341f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
342f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
343e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
344e14bb325SJeff Bonwick 			continue;
345e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
346e14bb325SJeff Bonwick 		if (rw == RW_READER) {
347e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
348e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
349e14bb325SJeff Bonwick 			}
350e14bb325SJeff Bonwick 		} else {
351e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
352e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
353e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
354e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
355e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
356e14bb325SJeff Bonwick 			}
357e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
358e14bb325SJeff Bonwick 		}
359e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
360e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
361e05725b1Sbonwick 	}
362f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
363e05725b1Sbonwick }
364e05725b1Sbonwick 
365e05725b1Sbonwick void
366e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
367e05725b1Sbonwick {
368e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
369e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
370e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
371e14bb325SJeff Bonwick 			continue;
372e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
373e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
374e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
375e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
376e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
377e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
378e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
379e14bb325SJeff Bonwick 		}
380e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
381e05725b1Sbonwick 	}
382e05725b1Sbonwick }
383e05725b1Sbonwick 
384e14bb325SJeff Bonwick int
385e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
386e05725b1Sbonwick {
387e14bb325SJeff Bonwick 	int locks_held = 0;
388e05725b1Sbonwick 
389e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
390e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
391e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
392e14bb325SJeff Bonwick 			continue;
393e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
394e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
395e14bb325SJeff Bonwick 			locks_held |= 1 << i;
396e14bb325SJeff Bonwick 	}
397e14bb325SJeff Bonwick 
398e14bb325SJeff Bonwick 	return (locks_held);
399e05725b1Sbonwick }
400e05725b1Sbonwick 
401fa9e4066Sahrens /*
402fa9e4066Sahrens  * ==========================================================================
403fa9e4066Sahrens  * SPA namespace functions
404fa9e4066Sahrens  * ==========================================================================
405fa9e4066Sahrens  */
406fa9e4066Sahrens 
407fa9e4066Sahrens /*
408fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
409fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
410fa9e4066Sahrens  */
411fa9e4066Sahrens spa_t *
412fa9e4066Sahrens spa_lookup(const char *name)
413fa9e4066Sahrens {
414e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
415e14bb325SJeff Bonwick 	spa_t *spa;
416fa9e4066Sahrens 	avl_index_t where;
41740feaa91Sahrens 	char c;
41840feaa91Sahrens 	char *cp;
419fa9e4066Sahrens 
420fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
421fa9e4066Sahrens 
42240feaa91Sahrens 	/*
42340feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
42440feaa91Sahrens 	 * just use that.
42540feaa91Sahrens 	 */
42640feaa91Sahrens 	cp = strpbrk(name, "/@");
42740feaa91Sahrens 	if (cp) {
42840feaa91Sahrens 		c = *cp;
42940feaa91Sahrens 		*cp = '\0';
43040feaa91Sahrens 	}
43140feaa91Sahrens 
432e14bb325SJeff Bonwick 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
433fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
434fa9e4066Sahrens 
43540feaa91Sahrens 	if (cp)
43640feaa91Sahrens 		*cp = c;
43740feaa91Sahrens 
438fa9e4066Sahrens 	return (spa);
439fa9e4066Sahrens }
440fa9e4066Sahrens 
441*283b8460SGeorge.Wilson /*
442*283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
443*283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
444*283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
445*283b8460SGeorge.Wilson  */
446*283b8460SGeorge.Wilson void
447*283b8460SGeorge.Wilson spa_deadman(void *arg)
448*283b8460SGeorge.Wilson {
449*283b8460SGeorge.Wilson 	spa_t *spa = arg;
450*283b8460SGeorge.Wilson 
451*283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
452*283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
453*283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
454*283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
455*283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
456*283b8460SGeorge.Wilson }
457*283b8460SGeorge.Wilson 
458fa9e4066Sahrens /*
459fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
460fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
461fa9e4066Sahrens  * exist by calling spa_lookup() first.
462fa9e4066Sahrens  */
463fa9e4066Sahrens spa_t *
464468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
465fa9e4066Sahrens {
466fa9e4066Sahrens 	spa_t *spa;
467c5904d13Seschrock 	spa_config_dirent_t *dp;
468*283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
469*283b8460SGeorge.Wilson 	cyc_time_t when;
470fa9e4066Sahrens 
471fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
472fa9e4066Sahrens 
473fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
474fa9e4066Sahrens 
475c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
476c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
47735a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
478c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
47935a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
480c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
48135a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
482a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
483a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_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 
501*283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
502*283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
503*283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
504*283b8460SGeorge.Wilson 
505*283b8460SGeorge.Wilson 	spa->spa_deadman_synctime = zfs_deadman_synctime *
506*283b8460SGeorge.Wilson 	    zfs_txg_synctime_ms * MICROSEC;
507*283b8460SGeorge.Wilson 
508*283b8460SGeorge.Wilson 	/*
509*283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
510*283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
511*283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
512*283b8460SGeorge.Wilson 	 * Instead wait for 5 synctimes before checking again.
513*283b8460SGeorge.Wilson 	 */
514*283b8460SGeorge.Wilson 	when.cyt_interval = 5ULL * zfs_txg_synctime_ms * MICROSEC;
515*283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
516*283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
517*283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
518*283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
519*283b8460SGeorge.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 
563fa9e4066Sahrens 	return (spa);
564fa9e4066Sahrens }
565fa9e4066Sahrens 
566fa9e4066Sahrens /*
567fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
568fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
569fa9e4066Sahrens  * deactivated.
570fa9e4066Sahrens  */
571fa9e4066Sahrens void
572fa9e4066Sahrens spa_remove(spa_t *spa)
573fa9e4066Sahrens {
574c5904d13Seschrock 	spa_config_dirent_t *dp;
575c5904d13Seschrock 
576fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
577fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
578fa9e4066Sahrens 
5791195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
5801195e687SMark J Musante 
581fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
582fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
583fa9e4066Sahrens 
5840373e76bSbonwick 	if (spa->spa_root) {
585fa9e4066Sahrens 		spa_strfree(spa->spa_root);
5860373e76bSbonwick 		spa_active_count--;
5870373e76bSbonwick 	}
588fa9e4066Sahrens 
589c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
590c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
591c5904d13Seschrock 		if (dp->scd_path != NULL)
592c5904d13Seschrock 			spa_strfree(dp->scd_path);
593c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
594c5904d13Seschrock 	}
595c5904d13Seschrock 
596c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
5972f8aaab3Seschrock 
598ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
5994b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
600fa9e4066Sahrens 	spa_config_set(spa, NULL);
601fa9e4066Sahrens 
602*283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
603*283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
604*283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
605*283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
606*283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
607*283b8460SGeorge.Wilson 
608fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
60991ebeef5Sahrens 
610e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
611fa9e4066Sahrens 
612b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
613cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
614b24ab676SJeff Bonwick 
615c25056deSgw 	cv_destroy(&spa->spa_async_cv);
61635a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
617c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
618e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
619c25056deSgw 
6205ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
621c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
62235a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
62306eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
62435a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
625b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
62635a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
627e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
628a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
6295ad82045Snd 
630fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
631fa9e4066Sahrens }
632fa9e4066Sahrens 
633fa9e4066Sahrens /*
634fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
635fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
636fa9e4066Sahrens  */
637fa9e4066Sahrens spa_t *
638fa9e4066Sahrens spa_next(spa_t *prev)
639fa9e4066Sahrens {
640fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
641fa9e4066Sahrens 
642fa9e4066Sahrens 	if (prev)
643fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
644fa9e4066Sahrens 	else
645fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
646fa9e4066Sahrens }
647fa9e4066Sahrens 
648fa9e4066Sahrens /*
649fa9e4066Sahrens  * ==========================================================================
650fa9e4066Sahrens  * SPA refcount functions
651fa9e4066Sahrens  * ==========================================================================
652fa9e4066Sahrens  */
653fa9e4066Sahrens 
654fa9e4066Sahrens /*
655fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
656fa9e4066Sahrens  * have the namespace lock held.
657fa9e4066Sahrens  */
658fa9e4066Sahrens void
659fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
660fa9e4066Sahrens {
661088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
662fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
663fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
664fa9e4066Sahrens }
665fa9e4066Sahrens 
666fa9e4066Sahrens /*
667fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
668fa9e4066Sahrens  * have the namespace lock held.
669fa9e4066Sahrens  */
670fa9e4066Sahrens void
671fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
672fa9e4066Sahrens {
673088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
674fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
675fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
676fa9e4066Sahrens }
677fa9e4066Sahrens 
678fa9e4066Sahrens /*
679fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
680088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
681fa9e4066Sahrens  * number of references acquired when opening a pool
682fa9e4066Sahrens  */
683fa9e4066Sahrens boolean_t
684fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
685fa9e4066Sahrens {
686fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
687fa9e4066Sahrens 
688088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
689fa9e4066Sahrens }
690fa9e4066Sahrens 
69199653d4eSeschrock /*
69299653d4eSeschrock  * ==========================================================================
693fa94a07fSbrendan  * SPA spare and l2cache tracking
69499653d4eSeschrock  * ==========================================================================
69599653d4eSeschrock  */
69699653d4eSeschrock 
697fa94a07fSbrendan /*
698fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
699fa94a07fSbrendan  * for 'auxiliary' devices.
700fa94a07fSbrendan  */
701fa94a07fSbrendan 
702fa94a07fSbrendan typedef struct spa_aux {
703fa94a07fSbrendan 	uint64_t	aux_guid;
704fa94a07fSbrendan 	uint64_t	aux_pool;
705fa94a07fSbrendan 	avl_node_t	aux_avl;
706fa94a07fSbrendan 	int		aux_count;
707fa94a07fSbrendan } spa_aux_t;
708fa94a07fSbrendan 
709fa94a07fSbrendan static int
710fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
711fa94a07fSbrendan {
712fa94a07fSbrendan 	const spa_aux_t *sa = a;
713fa94a07fSbrendan 	const spa_aux_t *sb = b;
714fa94a07fSbrendan 
715fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
716fa94a07fSbrendan 		return (-1);
717fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
718fa94a07fSbrendan 		return (1);
719fa94a07fSbrendan 	else
720fa94a07fSbrendan 		return (0);
721fa94a07fSbrendan }
722fa94a07fSbrendan 
723fa94a07fSbrendan void
724fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
725fa94a07fSbrendan {
726fa94a07fSbrendan 	avl_index_t where;
727fa94a07fSbrendan 	spa_aux_t search;
728fa94a07fSbrendan 	spa_aux_t *aux;
729fa94a07fSbrendan 
730fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
731fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
732fa94a07fSbrendan 		aux->aux_count++;
733fa94a07fSbrendan 	} else {
734fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
735fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
736fa94a07fSbrendan 		aux->aux_count = 1;
737fa94a07fSbrendan 		avl_insert(avl, aux, where);
738fa94a07fSbrendan 	}
739fa94a07fSbrendan }
740fa94a07fSbrendan 
741fa94a07fSbrendan void
742fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
743fa94a07fSbrendan {
744fa94a07fSbrendan 	spa_aux_t search;
745fa94a07fSbrendan 	spa_aux_t *aux;
746fa94a07fSbrendan 	avl_index_t where;
747fa94a07fSbrendan 
748fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
749fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
750fa94a07fSbrendan 
751fa94a07fSbrendan 	ASSERT(aux != NULL);
752fa94a07fSbrendan 
753fa94a07fSbrendan 	if (--aux->aux_count == 0) {
754fa94a07fSbrendan 		avl_remove(avl, aux);
755fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
756fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
757fa94a07fSbrendan 		aux->aux_pool = 0ULL;
758fa94a07fSbrendan 	}
759fa94a07fSbrendan }
760fa94a07fSbrendan 
761fa94a07fSbrendan boolean_t
76289a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
763fa94a07fSbrendan {
764fa94a07fSbrendan 	spa_aux_t search, *found;
765fa94a07fSbrendan 
766fa94a07fSbrendan 	search.aux_guid = guid;
76789a89ebfSlling 	found = avl_find(avl, &search, NULL);
768fa94a07fSbrendan 
769fa94a07fSbrendan 	if (pool) {
770fa94a07fSbrendan 		if (found)
771fa94a07fSbrendan 			*pool = found->aux_pool;
772fa94a07fSbrendan 		else
773fa94a07fSbrendan 			*pool = 0ULL;
774fa94a07fSbrendan 	}
775fa94a07fSbrendan 
77689a89ebfSlling 	if (refcnt) {
77789a89ebfSlling 		if (found)
77889a89ebfSlling 			*refcnt = found->aux_count;
77989a89ebfSlling 		else
78089a89ebfSlling 			*refcnt = 0;
78189a89ebfSlling 	}
78289a89ebfSlling 
783fa94a07fSbrendan 	return (found != NULL);
784fa94a07fSbrendan }
785fa94a07fSbrendan 
786fa94a07fSbrendan void
787fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
788fa94a07fSbrendan {
789fa94a07fSbrendan 	spa_aux_t search, *found;
790fa94a07fSbrendan 	avl_index_t where;
791fa94a07fSbrendan 
792fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
793fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
794fa94a07fSbrendan 	ASSERT(found != NULL);
795fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
796fa94a07fSbrendan 
797fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
798fa94a07fSbrendan }
799fa94a07fSbrendan 
80099653d4eSeschrock /*
80139c23413Seschrock  * Spares are tracked globally due to the following constraints:
80239c23413Seschrock  *
80339c23413Seschrock  * 	- A spare may be part of multiple pools.
80439c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
80539c23413Seschrock  *	  another pool.
80639c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
80739c23413Seschrock  *	  the target is a spare in the same pool.
80839c23413Seschrock  *
80939c23413Seschrock  * We keep track of all spares on the system through the use of a reference
81039c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
81139c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
81239c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
81339c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
81439c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
81539c23413Seschrock  *
81639c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
81739c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
81839c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
81939c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
82099653d4eSeschrock  */
82139c23413Seschrock 
82299653d4eSeschrock static int
82399653d4eSeschrock spa_spare_compare(const void *a, const void *b)
82499653d4eSeschrock {
825fa94a07fSbrendan 	return (spa_aux_compare(a, b));
82699653d4eSeschrock }
82799653d4eSeschrock 
82899653d4eSeschrock void
82939c23413Seschrock spa_spare_add(vdev_t *vd)
83099653d4eSeschrock {
83199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
83239c23413Seschrock 	ASSERT(!vd->vdev_isspare);
833fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
83439c23413Seschrock 	vd->vdev_isspare = B_TRUE;
83599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
83699653d4eSeschrock }
83799653d4eSeschrock 
83899653d4eSeschrock void
83939c23413Seschrock spa_spare_remove(vdev_t *vd)
84099653d4eSeschrock {
84199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
84239c23413Seschrock 	ASSERT(vd->vdev_isspare);
843fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
84439c23413Seschrock 	vd->vdev_isspare = B_FALSE;
84599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
84699653d4eSeschrock }
84799653d4eSeschrock 
84899653d4eSeschrock boolean_t
84989a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
85099653d4eSeschrock {
851fa94a07fSbrendan 	boolean_t found;
85299653d4eSeschrock 
85399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
85489a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
85599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
85699653d4eSeschrock 
857fa94a07fSbrendan 	return (found);
85839c23413Seschrock }
85939c23413Seschrock 
86039c23413Seschrock void
86139c23413Seschrock spa_spare_activate(vdev_t *vd)
86239c23413Seschrock {
86339c23413Seschrock 	mutex_enter(&spa_spare_lock);
86439c23413Seschrock 	ASSERT(vd->vdev_isspare);
865fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
866fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
867fa94a07fSbrendan }
86839c23413Seschrock 
869fa94a07fSbrendan /*
870fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
871fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
872fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
873fa94a07fSbrendan  */
87439c23413Seschrock 
875fa94a07fSbrendan static int
876fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
877fa94a07fSbrendan {
878fa94a07fSbrendan 	return (spa_aux_compare(a, b));
879fa94a07fSbrendan }
880fa94a07fSbrendan 
881fa94a07fSbrendan void
882fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
883fa94a07fSbrendan {
884fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
885fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
886fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
887fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
888fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
889fa94a07fSbrendan }
890fa94a07fSbrendan 
891fa94a07fSbrendan void
892fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
893fa94a07fSbrendan {
894fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
895fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
896fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
897fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
898fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
899fa94a07fSbrendan }
900fa94a07fSbrendan 
901fa94a07fSbrendan boolean_t
902fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
903fa94a07fSbrendan {
904fa94a07fSbrendan 	boolean_t found;
905fa94a07fSbrendan 
906fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
90789a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
908fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
909fa94a07fSbrendan 
910fa94a07fSbrendan 	return (found);
911fa94a07fSbrendan }
912fa94a07fSbrendan 
913fa94a07fSbrendan void
914fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
915fa94a07fSbrendan {
916fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
917fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
918fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
919fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
920fa94a07fSbrendan }
921fa94a07fSbrendan 
922fa9e4066Sahrens /*
923fa9e4066Sahrens  * ==========================================================================
924fa9e4066Sahrens  * SPA vdev locking
925fa9e4066Sahrens  * ==========================================================================
926fa9e4066Sahrens  */
927fa9e4066Sahrens 
928fa9e4066Sahrens /*
929ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
930ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
931fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
932fa9e4066Sahrens  */
933fa9e4066Sahrens uint64_t
934fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
935fa9e4066Sahrens {
936a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
937bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
93888ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
93988ecc943SGeorge Wilson }
94088ecc943SGeorge Wilson 
94188ecc943SGeorge Wilson /*
94288ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
94388ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
94488ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
94588ecc943SGeorge Wilson  */
94688ecc943SGeorge Wilson uint64_t
94788ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
94888ecc943SGeorge Wilson {
94988ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
9503d7072f8Seschrock 
951e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
952fa9e4066Sahrens 
953fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
954fa9e4066Sahrens }
955fa9e4066Sahrens 
956fa9e4066Sahrens /*
95788ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
95888ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
959fa9e4066Sahrens  */
96088ecc943SGeorge Wilson void
96188ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
962fa9e4066Sahrens {
96388ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
96488ecc943SGeorge Wilson 
9650e34b6a7Sbonwick 	int config_changed = B_FALSE;
966ea8dc4b6Seschrock 
9670373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
9680e34b6a7Sbonwick 
969e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
970e14bb325SJeff Bonwick 
9710e34b6a7Sbonwick 	/*
9720e34b6a7Sbonwick 	 * Reassess the DTLs.
9730e34b6a7Sbonwick 	 */
9740373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
9750e34b6a7Sbonwick 
976e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
9770e34b6a7Sbonwick 		config_changed = B_TRUE;
9788f18d1faSGeorge Wilson 		spa->spa_config_generation++;
9790e34b6a7Sbonwick 	}
980ea8dc4b6Seschrock 
98188ecc943SGeorge Wilson 	/*
98288ecc943SGeorge Wilson 	 * Verify the metaslab classes.
98388ecc943SGeorge Wilson 	 */
984b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
985b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
98688ecc943SGeorge Wilson 
987e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
988fa9e4066Sahrens 
98988ecc943SGeorge Wilson 	/*
99088ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
99188ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
99288ecc943SGeorge Wilson 	 * transactionally.
99388ecc943SGeorge Wilson 	 */
99488ecc943SGeorge Wilson 	if (zio_injection_enabled)
9951195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
99688ecc943SGeorge Wilson 
997fa9e4066Sahrens 	/*
998fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
999fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1000fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1001fa9e4066Sahrens 	 */
1002fa9e4066Sahrens 	if (error == 0)
1003fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	if (vd != NULL) {
10068ad4d6ddSJeff Bonwick 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
10078ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1008fa9e4066Sahrens 		vdev_free(vd);
10098ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1010fa9e4066Sahrens 	}
1011fa9e4066Sahrens 
1012fa9e4066Sahrens 	/*
10130e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1014fa9e4066Sahrens 	 */
10150e34b6a7Sbonwick 	if (config_changed)
1016c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
101788ecc943SGeorge Wilson }
1018ea8dc4b6Seschrock 
101988ecc943SGeorge Wilson /*
102088ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
102188ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
102288ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
102388ecc943SGeorge Wilson  * information.
102488ecc943SGeorge Wilson  */
102588ecc943SGeorge Wilson int
102688ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
102788ecc943SGeorge Wilson {
102888ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1029ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1030bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1031fa9e4066Sahrens 
1032fa9e4066Sahrens 	return (error);
1033fa9e4066Sahrens }
1034fa9e4066Sahrens 
1035e14bb325SJeff Bonwick /*
1036e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1037e14bb325SJeff Bonwick  */
1038e14bb325SJeff Bonwick void
10398f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1040e14bb325SJeff Bonwick {
10418f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
10428f18d1faSGeorge Wilson 
1043dcba9f3fSGeorge Wilson 	/*
1044dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1045dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1046dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1047dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1048dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1049dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1050dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1051dcba9f3fSGeorge Wilson 	 */
1052dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
10539842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
10549842588bSGeorge Wilson 		int high = locks & ~low;
10559842588bSGeorge Wilson 
10569842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1057dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
10589842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1059dcba9f3fSGeorge Wilson 	} else {
1060dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1061dcba9f3fSGeorge Wilson 	}
10628f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1063e14bb325SJeff Bonwick }
1064e14bb325SJeff Bonwick 
1065e14bb325SJeff Bonwick int
1066e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1067e14bb325SJeff Bonwick {
1068c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1069c6065d0fSGeorge Wilson 
1070b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1071b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1072b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1073b24ab676SJeff Bonwick 
10748f18d1faSGeorge Wilson 	if (vd != NULL) {
1075e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1076c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
10778f18d1faSGeorge Wilson 		spa->spa_config_generation++;
10788f18d1faSGeorge Wilson 	}
1079e14bb325SJeff Bonwick 
1080dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1081dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1082dcba9f3fSGeorge Wilson 
10838f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
10848f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1085e14bb325SJeff Bonwick 
10868ad4d6ddSJeff Bonwick 	/*
10878ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
10888ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
10898ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
10908ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
10918ad4d6ddSJeff Bonwick 	 */
10928ad4d6ddSJeff Bonwick 	if (vd != NULL)
10938ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
10948ad4d6ddSJeff Bonwick 
1095c6065d0fSGeorge Wilson 	/*
1096c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1097c6065d0fSGeorge Wilson 	 */
1098c6065d0fSGeorge Wilson 	if (config_changed) {
1099c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1100c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1101c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1102c6065d0fSGeorge Wilson 	}
1103c6065d0fSGeorge Wilson 
1104e14bb325SJeff Bonwick 	return (error);
1105e14bb325SJeff Bonwick }
1106e14bb325SJeff Bonwick 
1107fa9e4066Sahrens /*
1108fa9e4066Sahrens  * ==========================================================================
1109fa9e4066Sahrens  * Miscellaneous functions
1110fa9e4066Sahrens  * ==========================================================================
1111fa9e4066Sahrens  */
1112fa9e4066Sahrens 
1113ad135b5dSChristopher Siden void
1114ad135b5dSChristopher Siden spa_activate_mos_feature(spa_t *spa, const char *feature)
1115ad135b5dSChristopher Siden {
1116ad135b5dSChristopher Siden 	(void) nvlist_add_boolean(spa->spa_label_features, feature);
1117ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1118ad135b5dSChristopher Siden }
1119ad135b5dSChristopher Siden 
1120ad135b5dSChristopher Siden void
1121ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1122ad135b5dSChristopher Siden {
1123ad135b5dSChristopher Siden 	(void) nvlist_remove_all(spa->spa_label_features, feature);
1124ad135b5dSChristopher Siden 	vdev_config_dirty(spa->spa_root_vdev);
1125ad135b5dSChristopher Siden }
1126ad135b5dSChristopher Siden 
1127fa9e4066Sahrens /*
1128fa9e4066Sahrens  * Rename a spa_t.
1129fa9e4066Sahrens  */
1130fa9e4066Sahrens int
1131fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1132fa9e4066Sahrens {
1133fa9e4066Sahrens 	spa_t *spa;
1134fa9e4066Sahrens 	int err;
1135fa9e4066Sahrens 
1136fa9e4066Sahrens 	/*
1137fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1138fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1139fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1140ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1141fa9e4066Sahrens 	 */
1142fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1143fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1144fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1145fa9e4066Sahrens 		return (err);
1146fa9e4066Sahrens 	}
1147fa9e4066Sahrens 
1148e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1149fa9e4066Sahrens 
1150fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1151e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1152fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1153fa9e4066Sahrens 
1154fa9e4066Sahrens 	/*
1155fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1156fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1157fa9e4066Sahrens 	 * during the sync.
1158fa9e4066Sahrens 	 */
1159fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1160fa9e4066Sahrens 
1161e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1162fa9e4066Sahrens 
11630373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1164fa9e4066Sahrens 
1165fa9e4066Sahrens 	/*
1166fa9e4066Sahrens 	 * Sync the updated config cache.
1167fa9e4066Sahrens 	 */
1168c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1169fa9e4066Sahrens 
1170fa9e4066Sahrens 	spa_close(spa, FTAG);
1171fa9e4066Sahrens 
1172fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1173fa9e4066Sahrens 
1174fa9e4066Sahrens 	return (0);
1175fa9e4066Sahrens }
1176fa9e4066Sahrens 
1177fa9e4066Sahrens /*
1178f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1179f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1180f9af39baSGeorge Wilson  * a device with the specified device_guid.
1181fa9e4066Sahrens  */
1182f9af39baSGeorge Wilson spa_t *
1183f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1184fa9e4066Sahrens {
1185fa9e4066Sahrens 	spa_t *spa;
1186fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1187fa9e4066Sahrens 
1188ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1189fa9e4066Sahrens 
1190fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1191fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1192fa9e4066Sahrens 			continue;
1193fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1194fa9e4066Sahrens 			continue;
119539c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
119639c23413Seschrock 			if (device_guid == 0)
119739c23413Seschrock 				break;
119839c23413Seschrock 
119939c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
120039c23413Seschrock 			    device_guid) != NULL)
120139c23413Seschrock 				break;
120239c23413Seschrock 
120339c23413Seschrock 			/*
12048654d025Sperrin 			 * Check any devices we may be in the process of adding.
120539c23413Seschrock 			 */
120639c23413Seschrock 			if (spa->spa_pending_vdev) {
120739c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
120839c23413Seschrock 				    device_guid) != NULL)
120939c23413Seschrock 					break;
121039c23413Seschrock 			}
121139c23413Seschrock 		}
1212fa9e4066Sahrens 	}
1213fa9e4066Sahrens 
1214f9af39baSGeorge Wilson 	return (spa);
1215f9af39baSGeorge Wilson }
1216f9af39baSGeorge Wilson 
1217f9af39baSGeorge Wilson /*
1218f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1219f9af39baSGeorge Wilson  */
1220f9af39baSGeorge Wilson boolean_t
1221f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1222f9af39baSGeorge Wilson {
1223f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1224fa9e4066Sahrens }
1225fa9e4066Sahrens 
1226fa9e4066Sahrens char *
1227fa9e4066Sahrens spa_strdup(const char *s)
1228fa9e4066Sahrens {
1229fa9e4066Sahrens 	size_t len;
1230fa9e4066Sahrens 	char *new;
1231fa9e4066Sahrens 
1232fa9e4066Sahrens 	len = strlen(s);
1233fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1234fa9e4066Sahrens 	bcopy(s, new, len);
1235fa9e4066Sahrens 	new[len] = '\0';
1236fa9e4066Sahrens 
1237fa9e4066Sahrens 	return (new);
1238fa9e4066Sahrens }
1239fa9e4066Sahrens 
1240fa9e4066Sahrens void
1241fa9e4066Sahrens spa_strfree(char *s)
1242fa9e4066Sahrens {
1243fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1244fa9e4066Sahrens }
1245fa9e4066Sahrens 
1246fa9e4066Sahrens uint64_t
1247fa9e4066Sahrens spa_get_random(uint64_t range)
1248fa9e4066Sahrens {
1249fa9e4066Sahrens 	uint64_t r;
1250fa9e4066Sahrens 
1251fa9e4066Sahrens 	ASSERT(range != 0);
1252fa9e4066Sahrens 
1253fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1254fa9e4066Sahrens 
1255fa9e4066Sahrens 	return (r % range);
1256fa9e4066Sahrens }
1257fa9e4066Sahrens 
12581195e687SMark J Musante uint64_t
12591195e687SMark J Musante spa_generate_guid(spa_t *spa)
12601195e687SMark J Musante {
12611195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
12621195e687SMark J Musante 
12631195e687SMark J Musante 	if (spa != NULL) {
12641195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
12651195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12661195e687SMark J Musante 	} else {
12671195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
12681195e687SMark J Musante 			guid = spa_get_random(-1ULL);
12691195e687SMark J Musante 	}
12701195e687SMark J Musante 
12711195e687SMark J Musante 	return (guid);
12721195e687SMark J Musante }
12731195e687SMark J Musante 
1274fa9e4066Sahrens void
1275b24ab676SJeff Bonwick sprintf_blkptr(char *buf, const blkptr_t *bp)
1276fa9e4066Sahrens {
1277ad135b5dSChristopher Siden 	char type[256];
1278f0ba89beSJeff Bonwick 	char *checksum = NULL;
1279f0ba89beSJeff Bonwick 	char *compress = NULL;
1280f0ba89beSJeff Bonwick 
1281f0ba89beSJeff Bonwick 	if (bp != NULL) {
1282ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1283ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1284ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1285ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1286ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1287ad135b5dSChristopher Siden 			    "metadata" : "data",
1288ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1289ad135b5dSChristopher Siden 		} else {
1290ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1291ad135b5dSChristopher Siden 			    sizeof (type));
1292ad135b5dSChristopher Siden 		}
1293f0ba89beSJeff Bonwick 		checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1294f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1295f0ba89beSJeff Bonwick 	}
1296fa9e4066Sahrens 
1297b24ab676SJeff Bonwick 	SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1298fa9e4066Sahrens }
1299fa9e4066Sahrens 
1300fa9e4066Sahrens void
1301fa9e4066Sahrens spa_freeze(spa_t *spa)
1302fa9e4066Sahrens {
1303fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1304fa9e4066Sahrens 
1305e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1306fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1307fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1308fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1309fa9e4066Sahrens 	}
1310e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1311fa9e4066Sahrens 	if (freeze_txg != 0)
1312fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1313fa9e4066Sahrens }
1314fa9e4066Sahrens 
13150125049cSahrens void
13160125049cSahrens zfs_panic_recover(const char *fmt, ...)
13170125049cSahrens {
13180125049cSahrens 	va_list adx;
13190125049cSahrens 
13200125049cSahrens 	va_start(adx, fmt);
13210125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
13220125049cSahrens 	va_end(adx);
13230125049cSahrens }
13240125049cSahrens 
13253f9d6ad7SLin Ling /*
13263f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
13273f9d6ad7SLin Ling  * lowercase hexidecimal numbers that don't overflow.
13283f9d6ad7SLin Ling  */
13293f9d6ad7SLin Ling uint64_t
13303f9d6ad7SLin Ling strtonum(const char *str, char **nptr)
13313f9d6ad7SLin Ling {
13323f9d6ad7SLin Ling 	uint64_t val = 0;
13333f9d6ad7SLin Ling 	char c;
13343f9d6ad7SLin Ling 	int digit;
13353f9d6ad7SLin Ling 
13363f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
13373f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
13383f9d6ad7SLin Ling 			digit = c - '0';
13393f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
13403f9d6ad7SLin Ling 			digit = 10 + c - 'a';
13413f9d6ad7SLin Ling 		else
13423f9d6ad7SLin Ling 			break;
13433f9d6ad7SLin Ling 
13443f9d6ad7SLin Ling 		val *= 16;
13453f9d6ad7SLin Ling 		val += digit;
13463f9d6ad7SLin Ling 
13473f9d6ad7SLin Ling 		str++;
13483f9d6ad7SLin Ling 	}
13493f9d6ad7SLin Ling 
13503f9d6ad7SLin Ling 	if (nptr)
13513f9d6ad7SLin Ling 		*nptr = (char *)str;
13523f9d6ad7SLin Ling 
13533f9d6ad7SLin Ling 	return (val);
13543f9d6ad7SLin Ling }
13553f9d6ad7SLin Ling 
1356fa9e4066Sahrens /*
1357fa9e4066Sahrens  * ==========================================================================
1358fa9e4066Sahrens  * Accessor functions
1359fa9e4066Sahrens  * ==========================================================================
1360fa9e4066Sahrens  */
1361fa9e4066Sahrens 
1362088f3894Sahrens boolean_t
136388b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1364fa9e4066Sahrens {
136588b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1366fa9e4066Sahrens }
1367fa9e4066Sahrens 
1368fa9e4066Sahrens dsl_pool_t *
1369fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1370fa9e4066Sahrens {
1371fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1372fa9e4066Sahrens }
1373fa9e4066Sahrens 
1374ad135b5dSChristopher Siden boolean_t
1375ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1376ad135b5dSChristopher Siden {
1377ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1378ad135b5dSChristopher Siden }
1379ad135b5dSChristopher Siden 
1380fa9e4066Sahrens blkptr_t *
1381fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1382fa9e4066Sahrens {
1383fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1384fa9e4066Sahrens }
1385fa9e4066Sahrens 
1386fa9e4066Sahrens void
1387fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1388fa9e4066Sahrens {
1389fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1390fa9e4066Sahrens }
1391fa9e4066Sahrens 
1392fa9e4066Sahrens void
1393fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1394fa9e4066Sahrens {
1395fa9e4066Sahrens 	if (spa->spa_root == NULL)
1396fa9e4066Sahrens 		buf[0] = '\0';
1397fa9e4066Sahrens 	else
1398fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1399fa9e4066Sahrens }
1400fa9e4066Sahrens 
1401fa9e4066Sahrens int
1402fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1403fa9e4066Sahrens {
1404fa9e4066Sahrens 	return (spa->spa_sync_pass);
1405fa9e4066Sahrens }
1406fa9e4066Sahrens 
1407fa9e4066Sahrens char *
1408fa9e4066Sahrens spa_name(spa_t *spa)
1409fa9e4066Sahrens {
1410fa9e4066Sahrens 	return (spa->spa_name);
1411fa9e4066Sahrens }
1412fa9e4066Sahrens 
1413fa9e4066Sahrens uint64_t
1414fa9e4066Sahrens spa_guid(spa_t *spa)
1415fa9e4066Sahrens {
1416dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1417dfbb9432SGeorge Wilson 	uint64_t guid;
1418dfbb9432SGeorge Wilson 
1419b5989ec7Seschrock 	/*
1420b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1421b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1422e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1423b5989ec7Seschrock 	 * this case.
1424b5989ec7Seschrock 	 */
1425dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1426dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1427dfbb9432SGeorge Wilson 
1428dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1429dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1430dfbb9432SGeorge Wilson 
1431dfbb9432SGeorge Wilson 	/*
1432dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1433dfbb9432SGeorge Wilson 	 * in syncing context.
1434dfbb9432SGeorge Wilson 	 */
1435dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1436b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1437b5989ec7Seschrock 	else
1438dfbb9432SGeorge Wilson 		return (guid);
1439e9103aaeSGarrett D'Amore }
1440e9103aaeSGarrett D'Amore 
1441e9103aaeSGarrett D'Amore uint64_t
1442e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1443e9103aaeSGarrett D'Amore {
1444e9103aaeSGarrett D'Amore 	/*
1445e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1446e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1447e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1448e9103aaeSGarrett D'Amore 	 */
1449e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1450fa9e4066Sahrens }
1451fa9e4066Sahrens 
1452fa9e4066Sahrens uint64_t
1453fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1454fa9e4066Sahrens {
1455fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1456fa9e4066Sahrens }
1457fa9e4066Sahrens 
1458fa9e4066Sahrens uint64_t
1459fa9e4066Sahrens spa_first_txg(spa_t *spa)
1460fa9e4066Sahrens {
1461fa9e4066Sahrens 	return (spa->spa_first_txg);
1462fa9e4066Sahrens }
1463fa9e4066Sahrens 
1464b24ab676SJeff Bonwick uint64_t
1465b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1466b24ab676SJeff Bonwick {
1467b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1468b24ab676SJeff Bonwick }
1469b24ab676SJeff Bonwick 
147088b7b0f2SMatthew Ahrens pool_state_t
1471fa9e4066Sahrens spa_state(spa_t *spa)
1472fa9e4066Sahrens {
1473fa9e4066Sahrens 	return (spa->spa_state);
1474fa9e4066Sahrens }
1475fa9e4066Sahrens 
1476b16da2e2SGeorge Wilson spa_load_state_t
1477b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1478b16da2e2SGeorge Wilson {
1479b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1480b16da2e2SGeorge Wilson }
1481b16da2e2SGeorge Wilson 
1482fa9e4066Sahrens uint64_t
1483fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1484fa9e4066Sahrens {
1485fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1486fa9e4066Sahrens }
1487fa9e4066Sahrens 
1488fa9e4066Sahrens /* ARGSUSED */
1489fa9e4066Sahrens uint64_t
1490fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1491fa9e4066Sahrens {
1492fa9e4066Sahrens 	/*
1493b24ab676SJeff Bonwick 	 * The worst case is single-sector max-parity RAID-Z blocks, in which
1494b24ab676SJeff Bonwick 	 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1495b24ab676SJeff Bonwick 	 * times the size; so just assume that.  Add to this the fact that
1496b24ab676SJeff Bonwick 	 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1497b24ab676SJeff Bonwick 	 * the block may be dittoed with up to 3 DVAs by ddt_sync().
149844cd46caSbillm 	 */
1499b24ab676SJeff Bonwick 	return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
150044cd46caSbillm }
150144cd46caSbillm 
1502485bbbf5SGeorge Wilson uint64_t
1503485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1504485bbbf5SGeorge Wilson {
1505485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1506485bbbf5SGeorge Wilson }
1507485bbbf5SGeorge Wilson 
1508485bbbf5SGeorge Wilson void
1509485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1510485bbbf5SGeorge Wilson {
1511485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1512485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1513485bbbf5SGeorge Wilson }
1514485bbbf5SGeorge Wilson 
15150a4e9518Sgw /*
15160a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
15170a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
15180a4e9518Sgw  */
15190a4e9518Sgw uint8_t
15200a4e9518Sgw spa_get_failmode(spa_t *spa)
15210a4e9518Sgw {
15220a4e9518Sgw 	return (spa->spa_failmode);
15230a4e9518Sgw }
15240a4e9518Sgw 
1525e14bb325SJeff Bonwick boolean_t
1526e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1527e14bb325SJeff Bonwick {
1528e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1529e14bb325SJeff Bonwick }
1530e14bb325SJeff Bonwick 
153144cd46caSbillm uint64_t
153244cd46caSbillm spa_version(spa_t *spa)
153344cd46caSbillm {
153444cd46caSbillm 	return (spa->spa_ubsync.ub_version);
153544cd46caSbillm }
153644cd46caSbillm 
1537b24ab676SJeff Bonwick boolean_t
1538b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1539b24ab676SJeff Bonwick {
1540b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1541b24ab676SJeff Bonwick }
1542b24ab676SJeff Bonwick 
1543b24ab676SJeff Bonwick metaslab_class_t *
1544b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1545b24ab676SJeff Bonwick {
1546b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1547b24ab676SJeff Bonwick }
1548b24ab676SJeff Bonwick 
1549b24ab676SJeff Bonwick metaslab_class_t *
1550b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1551b24ab676SJeff Bonwick {
1552b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1553b24ab676SJeff Bonwick }
1554b24ab676SJeff Bonwick 
155544cd46caSbillm int
155644cd46caSbillm spa_max_replication(spa_t *spa)
155744cd46caSbillm {
155844cd46caSbillm 	/*
1559e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
156044cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
156144cd46caSbillm 	 * replication level accordingly.
1562fa9e4066Sahrens 	 */
1563e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
156444cd46caSbillm 		return (1);
156544cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1566fa9e4066Sahrens }
1567fa9e4066Sahrens 
15683f9d6ad7SLin Ling int
15693f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
15703f9d6ad7SLin Ling {
15713f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
15723f9d6ad7SLin Ling }
15733f9d6ad7SLin Ling 
1574*283b8460SGeorge.Wilson uint64_t
1575*283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1576*283b8460SGeorge.Wilson {
1577*283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1578*283b8460SGeorge.Wilson }
1579*283b8460SGeorge.Wilson 
158099653d4eSeschrock uint64_t
1581b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
158299653d4eSeschrock {
1583b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1584b24ab676SJeff Bonwick 	uint64_t dsize = asize;
158599653d4eSeschrock 
1586b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
158799653d4eSeschrock 
1588b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1589b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1590b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
159199653d4eSeschrock 	}
1592b24ab676SJeff Bonwick 
1593b24ab676SJeff Bonwick 	return (dsize);
1594b24ab676SJeff Bonwick }
1595b24ab676SJeff Bonwick 
1596b24ab676SJeff Bonwick uint64_t
1597b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1598b24ab676SJeff Bonwick {
1599b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1600b24ab676SJeff Bonwick 
1601b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1602b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1603b24ab676SJeff Bonwick 
1604b24ab676SJeff Bonwick 	return (dsize);
1605b24ab676SJeff Bonwick }
1606b24ab676SJeff Bonwick 
1607b24ab676SJeff Bonwick uint64_t
1608b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1609b24ab676SJeff Bonwick {
1610b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1611b24ab676SJeff Bonwick 
1612b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1613b24ab676SJeff Bonwick 
1614b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1615b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1616b24ab676SJeff Bonwick 
1617e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1618b24ab676SJeff Bonwick 
1619b24ab676SJeff Bonwick 	return (dsize);
162099653d4eSeschrock }
162199653d4eSeschrock 
1622fa9e4066Sahrens /*
1623fa9e4066Sahrens  * ==========================================================================
1624fa9e4066Sahrens  * Initialization and Termination
1625fa9e4066Sahrens  * ==========================================================================
1626fa9e4066Sahrens  */
1627fa9e4066Sahrens 
1628fa9e4066Sahrens static int
1629fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1630fa9e4066Sahrens {
1631fa9e4066Sahrens 	const spa_t *s1 = a1;
1632fa9e4066Sahrens 	const spa_t *s2 = a2;
1633fa9e4066Sahrens 	int s;
1634fa9e4066Sahrens 
1635fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1636fa9e4066Sahrens 	if (s > 0)
1637fa9e4066Sahrens 		return (1);
1638fa9e4066Sahrens 	if (s < 0)
1639fa9e4066Sahrens 		return (-1);
1640fa9e4066Sahrens 	return (0);
1641fa9e4066Sahrens }
1642fa9e4066Sahrens 
16430373e76bSbonwick int
16440373e76bSbonwick spa_busy(void)
16450373e76bSbonwick {
16460373e76bSbonwick 	return (spa_active_count);
16470373e76bSbonwick }
16480373e76bSbonwick 
1649e7cbe64fSgw void
1650e7cbe64fSgw spa_boot_init()
1651e7cbe64fSgw {
1652e7cbe64fSgw 	spa_config_load();
1653e7cbe64fSgw }
1654e7cbe64fSgw 
1655fa9e4066Sahrens void
1656fa9e4066Sahrens spa_init(int mode)
1657fa9e4066Sahrens {
1658fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1659c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1660fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1661fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1662fa9e4066Sahrens 
1663fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1664fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1665fa9e4066Sahrens 
1666fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1667fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1668fa94a07fSbrendan 
1669fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1670fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
167199653d4eSeschrock 
16728ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1673fa9e4066Sahrens 
1674*283b8460SGeorge.Wilson #ifdef _KERNEL
1675*283b8460SGeorge.Wilson 	spa_arch_init();
1676*283b8460SGeorge.Wilson #else
1677cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1678cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1679cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1680cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1681cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1682cd1c8b85SMatthew Ahrens 		} else {
1683cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1684cd1c8b85SMatthew Ahrens 		}
1685cd1c8b85SMatthew Ahrens 	}
1686cd1c8b85SMatthew Ahrens #endif
1687cd1c8b85SMatthew Ahrens 
1688fa9e4066Sahrens 	refcount_init();
1689fa9e4066Sahrens 	unique_init();
1690fa9e4066Sahrens 	zio_init();
1691fa9e4066Sahrens 	dmu_init();
1692fa9e4066Sahrens 	zil_init();
169387db74c1Sek 	vdev_cache_stat_init();
169491ebeef5Sahrens 	zfs_prop_init();
1695990b4856Slling 	zpool_prop_init();
1696ad135b5dSChristopher Siden 	zpool_feature_init();
1697fa9e4066Sahrens 	spa_config_load();
1698e14bb325SJeff Bonwick 	l2arc_start();
1699fa9e4066Sahrens }
1700fa9e4066Sahrens 
1701fa9e4066Sahrens void
1702fa9e4066Sahrens spa_fini(void)
1703fa9e4066Sahrens {
1704e14bb325SJeff Bonwick 	l2arc_stop();
1705e14bb325SJeff Bonwick 
1706fa9e4066Sahrens 	spa_evict_all();
1707fa9e4066Sahrens 
170887db74c1Sek 	vdev_cache_stat_fini();
1709fa9e4066Sahrens 	zil_fini();
1710fa9e4066Sahrens 	dmu_fini();
1711fa9e4066Sahrens 	zio_fini();
171291ebeef5Sahrens 	unique_fini();
1713fa9e4066Sahrens 	refcount_fini();
1714fa9e4066Sahrens 
1715fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
171699653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1717fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1718fa9e4066Sahrens 
1719fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1720fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1721c25056deSgw 	mutex_destroy(&spa_spare_lock);
1722fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1723fa9e4066Sahrens }
17246ce0521aSperrin 
17256ce0521aSperrin /*
17266ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
17276ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
17286ce0521aSperrin  * performance and not correctness
17296ce0521aSperrin  */
17306ce0521aSperrin boolean_t
17316ce0521aSperrin spa_has_slogs(spa_t *spa)
17326ce0521aSperrin {
17336ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
17346ce0521aSperrin }
1735bf82a41bSeschrock 
1736b24ab676SJeff Bonwick spa_log_state_t
1737b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1738b24ab676SJeff Bonwick {
1739b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1740b24ab676SJeff Bonwick }
1741b24ab676SJeff Bonwick 
1742b24ab676SJeff Bonwick void
1743b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1744b24ab676SJeff Bonwick {
1745b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1746b24ab676SJeff Bonwick }
1747b24ab676SJeff Bonwick 
1748bf82a41bSeschrock boolean_t
1749bf82a41bSeschrock spa_is_root(spa_t *spa)
1750bf82a41bSeschrock {
1751bf82a41bSeschrock 	return (spa->spa_is_root);
1752bf82a41bSeschrock }
17538ad4d6ddSJeff Bonwick 
17548ad4d6ddSJeff Bonwick boolean_t
17558ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
17568ad4d6ddSJeff Bonwick {
17578ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
17588ad4d6ddSJeff Bonwick }
17598ad4d6ddSJeff Bonwick 
17608ad4d6ddSJeff Bonwick int
17618ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
17628ad4d6ddSJeff Bonwick {
17638ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
17648ad4d6ddSJeff Bonwick }
1765b24ab676SJeff Bonwick 
1766b24ab676SJeff Bonwick uint64_t
1767b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1768b24ab676SJeff Bonwick {
1769b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1770b24ab676SJeff Bonwick }
1771b24ab676SJeff Bonwick 
1772b24ab676SJeff Bonwick uint64_t
1773b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1774b24ab676SJeff Bonwick {
1775b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1776b24ab676SJeff Bonwick }
1777b24ab676SJeff Bonwick 
1778b24ab676SJeff Bonwick objset_t *
1779b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1780b24ab676SJeff Bonwick {
1781b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1782b24ab676SJeff Bonwick }
1783b24ab676SJeff Bonwick 
1784b24ab676SJeff Bonwick enum zio_checksum
1785b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1786b24ab676SJeff Bonwick {
1787b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1788b24ab676SJeff Bonwick }
17893f9d6ad7SLin Ling 
17903f9d6ad7SLin Ling /*
17913f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
17923f9d6ad7SLin Ling  */
17933f9d6ad7SLin Ling void
17943f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
17953f9d6ad7SLin Ling {
17963f9d6ad7SLin Ling 	/* data not stored on disk */
17973f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
17983f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
17993f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
18003f9d6ad7SLin Ling }
18013f9d6ad7SLin Ling 
18023f9d6ad7SLin Ling /*
18033f9d6ad7SLin Ling  * Get scan stats for zpool status reports
18043f9d6ad7SLin Ling  */
18053f9d6ad7SLin Ling int
18063f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
18073f9d6ad7SLin Ling {
18083f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
18093f9d6ad7SLin Ling 
18103f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
18113f9d6ad7SLin Ling 		return (ENOENT);
18123f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
18133f9d6ad7SLin Ling 
18143f9d6ad7SLin Ling 	/* data stored on disk */
18153f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
18163f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
18173f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
18183f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
18193f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
18203f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
18213f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
18223f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
18233f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
18243f9d6ad7SLin Ling 
18253f9d6ad7SLin Ling 	/* data not stored on disk */
18263f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
18273f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
18283f9d6ad7SLin Ling 
18293f9d6ad7SLin Ling 	return (0);
18303f9d6ad7SLin Ling }
183109c9d376SGeorge Wilson 
183209c9d376SGeorge Wilson boolean_t
183309c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
183409c9d376SGeorge Wilson {
183509c9d376SGeorge Wilson 	return (spa->spa_debug);
183609c9d376SGeorge Wilson }
1837