xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 8b36997aa24d9817807faa4efa301ac9c07a2b78)
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.
23be6fd75aSMatthew Ahrens  * Copyright (c) 2013 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
2403b2aab18SMatthew Ahrens /* Everything except dprintf and spa is on by default in debug builds */
2413b2aab18SMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
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.
250*8b36997aSMatthew Ahrens  * This should only be used as a last resort, as it typically results
251*8b36997aSMatthew Ahrens  * in leaked space, or worse.
2520125049cSahrens  */
2530125049cSahrens int zfs_recover = 0;
2540125049cSahrens 
25569962b56SMatthew Ahrens /*
25669962b56SMatthew Ahrens  * Expiration time in milliseconds. This value has two meanings. First it is
25769962b56SMatthew Ahrens  * used to determine when the spa_deadman() logic should fire. By default the
25869962b56SMatthew Ahrens  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
25969962b56SMatthew Ahrens  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
26069962b56SMatthew Ahrens  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
26169962b56SMatthew Ahrens  * in a system panic.
26269962b56SMatthew Ahrens  */
26369962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL;
264283b8460SGeorge.Wilson 
265283b8460SGeorge.Wilson /*
26669962b56SMatthew Ahrens  * Check time in milliseconds. This defines the frequency at which we check
26769962b56SMatthew Ahrens  * for hung I/O.
268283b8460SGeorge.Wilson  */
26969962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL;
270283b8460SGeorge.Wilson 
271283b8460SGeorge.Wilson /*
272283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
273283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
274283b8460SGeorge.Wilson  */
275283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
276283b8460SGeorge.Wilson 
27769962b56SMatthew Ahrens /*
27869962b56SMatthew Ahrens  * The worst case is single-sector max-parity RAID-Z blocks, in which
27969962b56SMatthew Ahrens  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
28069962b56SMatthew Ahrens  * times the size; so just assume that.  Add to this the fact that
28169962b56SMatthew Ahrens  * we can have up to 3 DVAs per bp, and one more factor of 2 because
28269962b56SMatthew Ahrens  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
28369962b56SMatthew Ahrens  * the worst case is:
28469962b56SMatthew Ahrens  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
28569962b56SMatthew Ahrens  */
28669962b56SMatthew Ahrens int spa_asize_inflation = 24;
287fa9e4066Sahrens 
288e05725b1Sbonwick /*
289e05725b1Sbonwick  * ==========================================================================
290e05725b1Sbonwick  * SPA config locking
291e05725b1Sbonwick  * ==========================================================================
292e05725b1Sbonwick  */
293e05725b1Sbonwick static void
294e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
295e14bb325SJeff Bonwick {
296e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
297e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
298e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
299e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
3003b2aab18SMatthew Ahrens 		refcount_create_untracked(&scl->scl_count);
301e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
302e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
303e14bb325SJeff Bonwick 	}
304e05725b1Sbonwick }
305e05725b1Sbonwick 
306e05725b1Sbonwick static void
307e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
308e14bb325SJeff Bonwick {
309e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
310e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
311e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
312e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
313e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
314e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
315e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
316e14bb325SJeff Bonwick 	}
317e14bb325SJeff Bonwick }
318e14bb325SJeff Bonwick 
319e14bb325SJeff Bonwick int
320e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
321e05725b1Sbonwick {
322e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
323e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
324e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
325e14bb325SJeff Bonwick 			continue;
326e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
327e14bb325SJeff Bonwick 		if (rw == RW_READER) {
328e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
329e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
330e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
331e14bb325SJeff Bonwick 				return (0);
332e14bb325SJeff Bonwick 			}
333e14bb325SJeff Bonwick 		} else {
334e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
335e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
336e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
337e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
338e14bb325SJeff Bonwick 				return (0);
339e14bb325SJeff Bonwick 			}
340e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
341e14bb325SJeff Bonwick 		}
342e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
343e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
344e14bb325SJeff Bonwick 	}
345e14bb325SJeff Bonwick 	return (1);
346e05725b1Sbonwick }
347e05725b1Sbonwick 
348e05725b1Sbonwick void
349e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
350e05725b1Sbonwick {
351f64c0e34SEric Taylor 	int wlocks_held = 0;
352f64c0e34SEric Taylor 
3533b2aab18SMatthew Ahrens 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
3543b2aab18SMatthew Ahrens 
355e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
356e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
357f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
358f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
359e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
360e14bb325SJeff Bonwick 			continue;
361e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
362e14bb325SJeff Bonwick 		if (rw == RW_READER) {
363e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
364e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
365e14bb325SJeff Bonwick 			}
366e14bb325SJeff Bonwick 		} else {
367e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
368e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
369e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
370e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
371e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
372e14bb325SJeff Bonwick 			}
373e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
374e14bb325SJeff Bonwick 		}
375e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
376e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
377e05725b1Sbonwick 	}
378f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
379e05725b1Sbonwick }
380e05725b1Sbonwick 
381e05725b1Sbonwick void
382e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
383e05725b1Sbonwick {
384e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
385e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
386e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
387e14bb325SJeff Bonwick 			continue;
388e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
389e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
390e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
391e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
392e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
393e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
394e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
395e14bb325SJeff Bonwick 		}
396e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
397e05725b1Sbonwick 	}
398e05725b1Sbonwick }
399e05725b1Sbonwick 
400e14bb325SJeff Bonwick int
401e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
402e05725b1Sbonwick {
403e14bb325SJeff Bonwick 	int locks_held = 0;
404e05725b1Sbonwick 
405e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
406e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
407e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
408e14bb325SJeff Bonwick 			continue;
409e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
410e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
411e14bb325SJeff Bonwick 			locks_held |= 1 << i;
412e14bb325SJeff Bonwick 	}
413e14bb325SJeff Bonwick 
414e14bb325SJeff Bonwick 	return (locks_held);
415e05725b1Sbonwick }
416e05725b1Sbonwick 
417fa9e4066Sahrens /*
418fa9e4066Sahrens  * ==========================================================================
419fa9e4066Sahrens  * SPA namespace functions
420fa9e4066Sahrens  * ==========================================================================
421fa9e4066Sahrens  */
422fa9e4066Sahrens 
423fa9e4066Sahrens /*
424fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
425fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
426fa9e4066Sahrens  */
427fa9e4066Sahrens spa_t *
428fa9e4066Sahrens spa_lookup(const char *name)
429fa9e4066Sahrens {
430e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
431e14bb325SJeff Bonwick 	spa_t *spa;
432fa9e4066Sahrens 	avl_index_t where;
43340feaa91Sahrens 	char *cp;
434fa9e4066Sahrens 
435fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
436fa9e4066Sahrens 
4373b2aab18SMatthew Ahrens 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
4383b2aab18SMatthew Ahrens 
43940feaa91Sahrens 	/*
44040feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
44140feaa91Sahrens 	 * just use that.
44240feaa91Sahrens 	 */
44378f17100SMatthew Ahrens 	cp = strpbrk(search.spa_name, "/@#");
4443b2aab18SMatthew Ahrens 	if (cp != NULL)
44540feaa91Sahrens 		*cp = '\0';
44640feaa91Sahrens 
447fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
448fa9e4066Sahrens 
449fa9e4066Sahrens 	return (spa);
450fa9e4066Sahrens }
451fa9e4066Sahrens 
452283b8460SGeorge.Wilson /*
453283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
454283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
455283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
456283b8460SGeorge.Wilson  */
457283b8460SGeorge.Wilson void
458283b8460SGeorge.Wilson spa_deadman(void *arg)
459283b8460SGeorge.Wilson {
460283b8460SGeorge.Wilson 	spa_t *spa = arg;
461283b8460SGeorge.Wilson 
4620713e232SGeorge Wilson 	/*
4630713e232SGeorge Wilson 	 * Disable the deadman timer if the pool is suspended.
4640713e232SGeorge Wilson 	 */
4650713e232SGeorge Wilson 	if (spa_suspended(spa)) {
4660713e232SGeorge Wilson 		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
4670713e232SGeorge Wilson 		return;
4680713e232SGeorge Wilson 	}
4690713e232SGeorge Wilson 
470283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
471283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
472283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
473283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
474283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
475283b8460SGeorge.Wilson }
476283b8460SGeorge.Wilson 
477fa9e4066Sahrens /*
478fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
479fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
480fa9e4066Sahrens  * exist by calling spa_lookup() first.
481fa9e4066Sahrens  */
482fa9e4066Sahrens spa_t *
483468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
484fa9e4066Sahrens {
485fa9e4066Sahrens 	spa_t *spa;
486c5904d13Seschrock 	spa_config_dirent_t *dp;
487283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
488283b8460SGeorge.Wilson 	cyc_time_t when;
489fa9e4066Sahrens 
490fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
491fa9e4066Sahrens 
492fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
493fa9e4066Sahrens 
494c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
495c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
49635a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
497c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
49835a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
499c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
50035a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
501a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
502a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
503c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
504c25056deSgw 
505c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
50635a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
507c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
508e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
509c25056deSgw 
510b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
511cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
512b24ab676SJeff Bonwick 
513e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
514fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
515fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
5160373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
517468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
51835a5a358SJonathan Adams 	spa->spa_proc = &p0;
51935a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
520fa9e4066Sahrens 
521283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
522283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
523283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
524283b8460SGeorge.Wilson 
52569962b56SMatthew Ahrens 	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
526283b8460SGeorge.Wilson 
527283b8460SGeorge.Wilson 	/*
528283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
529283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
530283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
53169962b56SMatthew Ahrens 	 * Instead wait for 5 seconds before checking again.
532283b8460SGeorge.Wilson 	 */
53369962b56SMatthew Ahrens 	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
534283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
535283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
536283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
537283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
538283b8460SGeorge.Wilson 
539fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
540e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
541fa9e4066Sahrens 
542fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
543fa9e4066Sahrens 
5440373e76bSbonwick 	/*
5450373e76bSbonwick 	 * Set the alternate root, if there is one.
5460373e76bSbonwick 	 */
5470373e76bSbonwick 	if (altroot) {
5480373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
5490373e76bSbonwick 		spa_active_count++;
5500373e76bSbonwick 	}
5510373e76bSbonwick 
552c5904d13Seschrock 	/*
553c5904d13Seschrock 	 * Every pool starts with the default cachefile
554c5904d13Seschrock 	 */
555c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
556c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
557c5904d13Seschrock 
558c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
559ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
560c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
561c5904d13Seschrock 
5624b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
5634b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
5644b964adaSGeorge Wilson 
565ad135b5dSChristopher Siden 	if (config != NULL) {
566ad135b5dSChristopher Siden 		nvlist_t *features;
567ad135b5dSChristopher Siden 
568ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
569ad135b5dSChristopher Siden 		    &features) == 0) {
570ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
571ad135b5dSChristopher Siden 			    0) == 0);
572ad135b5dSChristopher Siden 		}
573ad135b5dSChristopher Siden 
574468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
575ad135b5dSChristopher Siden 	}
576ad135b5dSChristopher Siden 
577ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
578ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
579ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
580ad135b5dSChristopher Siden 	}
581468c413aSTim Haley 
582c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
583c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
584c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
585c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
586c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
587c3a66015SMatthew Ahrens 	}
588c3a66015SMatthew Ahrens 
5893b2aab18SMatthew Ahrens 	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
5903b2aab18SMatthew Ahrens 
59143466aaeSMax Grossman 	/*
59243466aaeSMax Grossman 	 * As a pool is being created, treat all features as disabled by
59343466aaeSMax Grossman 	 * setting SPA_FEATURE_DISABLED for all entries in the feature
59443466aaeSMax Grossman 	 * refcount cache.
59543466aaeSMax Grossman 	 */
59643466aaeSMax Grossman 	for (int i = 0; i < SPA_FEATURES; i++) {
59743466aaeSMax Grossman 		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
59843466aaeSMax Grossman 	}
59943466aaeSMax Grossman 
600fa9e4066Sahrens 	return (spa);
601fa9e4066Sahrens }
602fa9e4066Sahrens 
603fa9e4066Sahrens /*
604fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
605fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
606fa9e4066Sahrens  * deactivated.
607fa9e4066Sahrens  */
608fa9e4066Sahrens void
609fa9e4066Sahrens spa_remove(spa_t *spa)
610fa9e4066Sahrens {
611c5904d13Seschrock 	spa_config_dirent_t *dp;
612c5904d13Seschrock 
613fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
614fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
615fa9e4066Sahrens 
6161195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
6171195e687SMark J Musante 
618fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
619fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
620fa9e4066Sahrens 
6210373e76bSbonwick 	if (spa->spa_root) {
622fa9e4066Sahrens 		spa_strfree(spa->spa_root);
6230373e76bSbonwick 		spa_active_count--;
6240373e76bSbonwick 	}
625fa9e4066Sahrens 
626c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
627c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
628c5904d13Seschrock 		if (dp->scd_path != NULL)
629c5904d13Seschrock 			spa_strfree(dp->scd_path);
630c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
631c5904d13Seschrock 	}
632c5904d13Seschrock 
633c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
6342f8aaab3Seschrock 
635ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
6364b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
637fa9e4066Sahrens 	spa_config_set(spa, NULL);
638fa9e4066Sahrens 
639283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
640283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
641283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
642283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
643283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
644283b8460SGeorge.Wilson 
645fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
64691ebeef5Sahrens 
647e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
648fa9e4066Sahrens 
649c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
650c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
651c3a66015SMatthew Ahrens 
652b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
653cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
654b24ab676SJeff Bonwick 
655c25056deSgw 	cv_destroy(&spa->spa_async_cv);
65635a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
657c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
658e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
659c25056deSgw 
6605ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
661c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
66235a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
66306eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
66435a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
665b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
66635a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
667e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
668a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
669c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
6705ad82045Snd 
671fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
672fa9e4066Sahrens }
673fa9e4066Sahrens 
674fa9e4066Sahrens /*
675fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
676fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
677fa9e4066Sahrens  */
678fa9e4066Sahrens spa_t *
679fa9e4066Sahrens spa_next(spa_t *prev)
680fa9e4066Sahrens {
681fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
682fa9e4066Sahrens 
683fa9e4066Sahrens 	if (prev)
684fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
685fa9e4066Sahrens 	else
686fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
687fa9e4066Sahrens }
688fa9e4066Sahrens 
689fa9e4066Sahrens /*
690fa9e4066Sahrens  * ==========================================================================
691fa9e4066Sahrens  * SPA refcount functions
692fa9e4066Sahrens  * ==========================================================================
693fa9e4066Sahrens  */
694fa9e4066Sahrens 
695fa9e4066Sahrens /*
696fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
697fa9e4066Sahrens  * have the namespace lock held.
698fa9e4066Sahrens  */
699fa9e4066Sahrens void
700fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
701fa9e4066Sahrens {
702088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
703fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
704fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
705fa9e4066Sahrens }
706fa9e4066Sahrens 
707fa9e4066Sahrens /*
708fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
709fa9e4066Sahrens  * have the namespace lock held.
710fa9e4066Sahrens  */
711fa9e4066Sahrens void
712fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
713fa9e4066Sahrens {
714088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
715fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
716fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
717fa9e4066Sahrens }
718fa9e4066Sahrens 
719fa9e4066Sahrens /*
720fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
721088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
722fa9e4066Sahrens  * number of references acquired when opening a pool
723fa9e4066Sahrens  */
724fa9e4066Sahrens boolean_t
725fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
726fa9e4066Sahrens {
727fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
728fa9e4066Sahrens 
729088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
730fa9e4066Sahrens }
731fa9e4066Sahrens 
73299653d4eSeschrock /*
73399653d4eSeschrock  * ==========================================================================
734fa94a07fSbrendan  * SPA spare and l2cache tracking
73599653d4eSeschrock  * ==========================================================================
73699653d4eSeschrock  */
73799653d4eSeschrock 
738fa94a07fSbrendan /*
739fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
740fa94a07fSbrendan  * for 'auxiliary' devices.
741fa94a07fSbrendan  */
742fa94a07fSbrendan 
743fa94a07fSbrendan typedef struct spa_aux {
744fa94a07fSbrendan 	uint64_t	aux_guid;
745fa94a07fSbrendan 	uint64_t	aux_pool;
746fa94a07fSbrendan 	avl_node_t	aux_avl;
747fa94a07fSbrendan 	int		aux_count;
748fa94a07fSbrendan } spa_aux_t;
749fa94a07fSbrendan 
750fa94a07fSbrendan static int
751fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
752fa94a07fSbrendan {
753fa94a07fSbrendan 	const spa_aux_t *sa = a;
754fa94a07fSbrendan 	const spa_aux_t *sb = b;
755fa94a07fSbrendan 
756fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
757fa94a07fSbrendan 		return (-1);
758fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
759fa94a07fSbrendan 		return (1);
760fa94a07fSbrendan 	else
761fa94a07fSbrendan 		return (0);
762fa94a07fSbrendan }
763fa94a07fSbrendan 
764fa94a07fSbrendan void
765fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
766fa94a07fSbrendan {
767fa94a07fSbrendan 	avl_index_t where;
768fa94a07fSbrendan 	spa_aux_t search;
769fa94a07fSbrendan 	spa_aux_t *aux;
770fa94a07fSbrendan 
771fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
772fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
773fa94a07fSbrendan 		aux->aux_count++;
774fa94a07fSbrendan 	} else {
775fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
776fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
777fa94a07fSbrendan 		aux->aux_count = 1;
778fa94a07fSbrendan 		avl_insert(avl, aux, where);
779fa94a07fSbrendan 	}
780fa94a07fSbrendan }
781fa94a07fSbrendan 
782fa94a07fSbrendan void
783fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
784fa94a07fSbrendan {
785fa94a07fSbrendan 	spa_aux_t search;
786fa94a07fSbrendan 	spa_aux_t *aux;
787fa94a07fSbrendan 	avl_index_t where;
788fa94a07fSbrendan 
789fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
790fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
791fa94a07fSbrendan 
792fa94a07fSbrendan 	ASSERT(aux != NULL);
793fa94a07fSbrendan 
794fa94a07fSbrendan 	if (--aux->aux_count == 0) {
795fa94a07fSbrendan 		avl_remove(avl, aux);
796fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
797fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
798fa94a07fSbrendan 		aux->aux_pool = 0ULL;
799fa94a07fSbrendan 	}
800fa94a07fSbrendan }
801fa94a07fSbrendan 
802fa94a07fSbrendan boolean_t
80389a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
804fa94a07fSbrendan {
805fa94a07fSbrendan 	spa_aux_t search, *found;
806fa94a07fSbrendan 
807fa94a07fSbrendan 	search.aux_guid = guid;
80889a89ebfSlling 	found = avl_find(avl, &search, NULL);
809fa94a07fSbrendan 
810fa94a07fSbrendan 	if (pool) {
811fa94a07fSbrendan 		if (found)
812fa94a07fSbrendan 			*pool = found->aux_pool;
813fa94a07fSbrendan 		else
814fa94a07fSbrendan 			*pool = 0ULL;
815fa94a07fSbrendan 	}
816fa94a07fSbrendan 
81789a89ebfSlling 	if (refcnt) {
81889a89ebfSlling 		if (found)
81989a89ebfSlling 			*refcnt = found->aux_count;
82089a89ebfSlling 		else
82189a89ebfSlling 			*refcnt = 0;
82289a89ebfSlling 	}
82389a89ebfSlling 
824fa94a07fSbrendan 	return (found != NULL);
825fa94a07fSbrendan }
826fa94a07fSbrendan 
827fa94a07fSbrendan void
828fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
829fa94a07fSbrendan {
830fa94a07fSbrendan 	spa_aux_t search, *found;
831fa94a07fSbrendan 	avl_index_t where;
832fa94a07fSbrendan 
833fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
834fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
835fa94a07fSbrendan 	ASSERT(found != NULL);
836fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
837fa94a07fSbrendan 
838fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
839fa94a07fSbrendan }
840fa94a07fSbrendan 
84199653d4eSeschrock /*
84239c23413Seschrock  * Spares are tracked globally due to the following constraints:
84339c23413Seschrock  *
84439c23413Seschrock  * 	- A spare may be part of multiple pools.
84539c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
84639c23413Seschrock  *	  another pool.
84739c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
84839c23413Seschrock  *	  the target is a spare in the same pool.
84939c23413Seschrock  *
85039c23413Seschrock  * We keep track of all spares on the system through the use of a reference
85139c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
85239c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
85339c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
85439c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
85539c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
85639c23413Seschrock  *
85739c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
85839c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
85939c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
86039c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
86199653d4eSeschrock  */
86239c23413Seschrock 
86399653d4eSeschrock static int
86499653d4eSeschrock spa_spare_compare(const void *a, const void *b)
86599653d4eSeschrock {
866fa94a07fSbrendan 	return (spa_aux_compare(a, b));
86799653d4eSeschrock }
86899653d4eSeschrock 
86999653d4eSeschrock void
87039c23413Seschrock spa_spare_add(vdev_t *vd)
87199653d4eSeschrock {
87299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
87339c23413Seschrock 	ASSERT(!vd->vdev_isspare);
874fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
87539c23413Seschrock 	vd->vdev_isspare = B_TRUE;
87699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
87799653d4eSeschrock }
87899653d4eSeschrock 
87999653d4eSeschrock void
88039c23413Seschrock spa_spare_remove(vdev_t *vd)
88199653d4eSeschrock {
88299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
88339c23413Seschrock 	ASSERT(vd->vdev_isspare);
884fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
88539c23413Seschrock 	vd->vdev_isspare = B_FALSE;
88699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
88799653d4eSeschrock }
88899653d4eSeschrock 
88999653d4eSeschrock boolean_t
89089a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
89199653d4eSeschrock {
892fa94a07fSbrendan 	boolean_t found;
89399653d4eSeschrock 
89499653d4eSeschrock 	mutex_enter(&spa_spare_lock);
89589a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
89699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
89799653d4eSeschrock 
898fa94a07fSbrendan 	return (found);
89939c23413Seschrock }
90039c23413Seschrock 
90139c23413Seschrock void
90239c23413Seschrock spa_spare_activate(vdev_t *vd)
90339c23413Seschrock {
90439c23413Seschrock 	mutex_enter(&spa_spare_lock);
90539c23413Seschrock 	ASSERT(vd->vdev_isspare);
906fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
907fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
908fa94a07fSbrendan }
90939c23413Seschrock 
910fa94a07fSbrendan /*
911fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
912fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
913fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
914fa94a07fSbrendan  */
91539c23413Seschrock 
916fa94a07fSbrendan static int
917fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
918fa94a07fSbrendan {
919fa94a07fSbrendan 	return (spa_aux_compare(a, b));
920fa94a07fSbrendan }
921fa94a07fSbrendan 
922fa94a07fSbrendan void
923fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
924fa94a07fSbrendan {
925fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
926fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
927fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
928fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
929fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
930fa94a07fSbrendan }
931fa94a07fSbrendan 
932fa94a07fSbrendan void
933fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
934fa94a07fSbrendan {
935fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
936fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
937fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
938fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
939fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
940fa94a07fSbrendan }
941fa94a07fSbrendan 
942fa94a07fSbrendan boolean_t
943fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
944fa94a07fSbrendan {
945fa94a07fSbrendan 	boolean_t found;
946fa94a07fSbrendan 
947fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
94889a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
949fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
950fa94a07fSbrendan 
951fa94a07fSbrendan 	return (found);
952fa94a07fSbrendan }
953fa94a07fSbrendan 
954fa94a07fSbrendan void
955fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
956fa94a07fSbrendan {
957fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
958fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
959fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
960fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
961fa94a07fSbrendan }
962fa94a07fSbrendan 
963fa9e4066Sahrens /*
964fa9e4066Sahrens  * ==========================================================================
965fa9e4066Sahrens  * SPA vdev locking
966fa9e4066Sahrens  * ==========================================================================
967fa9e4066Sahrens  */
968fa9e4066Sahrens 
969fa9e4066Sahrens /*
970ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
971ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
972fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
973fa9e4066Sahrens  */
974fa9e4066Sahrens uint64_t
975fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
976fa9e4066Sahrens {
977a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
978bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
97988ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
98088ecc943SGeorge Wilson }
98188ecc943SGeorge Wilson 
98288ecc943SGeorge Wilson /*
98388ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
98488ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
98588ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
98688ecc943SGeorge Wilson  */
98788ecc943SGeorge Wilson uint64_t
98888ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
98988ecc943SGeorge Wilson {
99088ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
9913d7072f8Seschrock 
992e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
993fa9e4066Sahrens 
994fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
995fa9e4066Sahrens }
996fa9e4066Sahrens 
997fa9e4066Sahrens /*
99888ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
99988ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
1000fa9e4066Sahrens  */
100188ecc943SGeorge Wilson void
100288ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1003fa9e4066Sahrens {
100488ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
100588ecc943SGeorge Wilson 
10060e34b6a7Sbonwick 	int config_changed = B_FALSE;
1007ea8dc4b6Seschrock 
10080373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
10090e34b6a7Sbonwick 
1010e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
1011e14bb325SJeff Bonwick 
10120e34b6a7Sbonwick 	/*
10130e34b6a7Sbonwick 	 * Reassess the DTLs.
10140e34b6a7Sbonwick 	 */
10150373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
10160e34b6a7Sbonwick 
1017e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
10180e34b6a7Sbonwick 		config_changed = B_TRUE;
10198f18d1faSGeorge Wilson 		spa->spa_config_generation++;
10200e34b6a7Sbonwick 	}
1021ea8dc4b6Seschrock 
102288ecc943SGeorge Wilson 	/*
102388ecc943SGeorge Wilson 	 * Verify the metaslab classes.
102488ecc943SGeorge Wilson 	 */
1025b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1026b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
102788ecc943SGeorge Wilson 
1028e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
1029fa9e4066Sahrens 
103088ecc943SGeorge Wilson 	/*
103188ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
103288ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
103388ecc943SGeorge Wilson 	 * transactionally.
103488ecc943SGeorge Wilson 	 */
103588ecc943SGeorge Wilson 	if (zio_injection_enabled)
10361195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
103788ecc943SGeorge Wilson 
1038fa9e4066Sahrens 	/*
1039fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1040fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1041fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1042fa9e4066Sahrens 	 */
1043fa9e4066Sahrens 	if (error == 0)
1044fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1045fa9e4066Sahrens 
1046fa9e4066Sahrens 	if (vd != NULL) {
10470713e232SGeorge Wilson 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
10488ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1049fa9e4066Sahrens 		vdev_free(vd);
10508ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1051fa9e4066Sahrens 	}
1052fa9e4066Sahrens 
1053fa9e4066Sahrens 	/*
10540e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1055fa9e4066Sahrens 	 */
10560e34b6a7Sbonwick 	if (config_changed)
1057c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
105888ecc943SGeorge Wilson }
1059ea8dc4b6Seschrock 
106088ecc943SGeorge Wilson /*
106188ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
106288ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
106388ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
106488ecc943SGeorge Wilson  * information.
106588ecc943SGeorge Wilson  */
106688ecc943SGeorge Wilson int
106788ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
106888ecc943SGeorge Wilson {
106988ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1070ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1071bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1072fa9e4066Sahrens 
1073fa9e4066Sahrens 	return (error);
1074fa9e4066Sahrens }
1075fa9e4066Sahrens 
1076e14bb325SJeff Bonwick /*
1077e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1078e14bb325SJeff Bonwick  */
1079e14bb325SJeff Bonwick void
10808f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1081e14bb325SJeff Bonwick {
10828f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
10838f18d1faSGeorge Wilson 
1084dcba9f3fSGeorge Wilson 	/*
1085dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1086dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1087dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1088dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1089dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1090dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1091dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1092dcba9f3fSGeorge Wilson 	 */
1093dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
10949842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
10959842588bSGeorge Wilson 		int high = locks & ~low;
10969842588bSGeorge Wilson 
10979842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1098dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
10999842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1100dcba9f3fSGeorge Wilson 	} else {
1101dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1102dcba9f3fSGeorge Wilson 	}
11038f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1104e14bb325SJeff Bonwick }
1105e14bb325SJeff Bonwick 
1106e14bb325SJeff Bonwick int
1107e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1108e14bb325SJeff Bonwick {
1109c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1110c6065d0fSGeorge Wilson 
1111b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1112b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1113b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1114b24ab676SJeff Bonwick 
11158f18d1faSGeorge Wilson 	if (vd != NULL) {
1116e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1117c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
11188f18d1faSGeorge Wilson 		spa->spa_config_generation++;
11198f18d1faSGeorge Wilson 	}
1120e14bb325SJeff Bonwick 
1121dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1122dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1123dcba9f3fSGeorge Wilson 
11248f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
11258f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1126e14bb325SJeff Bonwick 
11278ad4d6ddSJeff Bonwick 	/*
11288ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
11298ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
11308ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
11318ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
11328ad4d6ddSJeff Bonwick 	 */
11338ad4d6ddSJeff Bonwick 	if (vd != NULL)
11348ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
11358ad4d6ddSJeff Bonwick 
1136c6065d0fSGeorge Wilson 	/*
1137c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1138c6065d0fSGeorge Wilson 	 */
1139c6065d0fSGeorge Wilson 	if (config_changed) {
1140c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1141c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1142c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1143c6065d0fSGeorge Wilson 	}
1144c6065d0fSGeorge Wilson 
1145e14bb325SJeff Bonwick 	return (error);
1146e14bb325SJeff Bonwick }
1147e14bb325SJeff Bonwick 
1148fa9e4066Sahrens /*
1149fa9e4066Sahrens  * ==========================================================================
1150fa9e4066Sahrens  * Miscellaneous functions
1151fa9e4066Sahrens  * ==========================================================================
1152fa9e4066Sahrens  */
1153fa9e4066Sahrens 
1154ad135b5dSChristopher Siden void
115543466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1156ad135b5dSChristopher Siden {
11572acef22dSMatthew Ahrens 	if (!nvlist_exists(spa->spa_label_features, feature)) {
11582acef22dSMatthew Ahrens 		fnvlist_add_boolean(spa->spa_label_features, feature);
115943466aaeSMax Grossman 		/*
116043466aaeSMax Grossman 		 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
116143466aaeSMax Grossman 		 * dirty the vdev config because lock SCL_CONFIG is not held.
116243466aaeSMax Grossman 		 * Thankfully, in this case we don't need to dirty the config
116343466aaeSMax Grossman 		 * because it will be written out anyway when we finish
116443466aaeSMax Grossman 		 * creating the pool.
116543466aaeSMax Grossman 		 */
116643466aaeSMax Grossman 		if (tx->tx_txg != TXG_INITIAL)
116743466aaeSMax Grossman 			vdev_config_dirty(spa->spa_root_vdev);
11682acef22dSMatthew Ahrens 	}
1169ad135b5dSChristopher Siden }
1170ad135b5dSChristopher Siden 
1171ad135b5dSChristopher Siden void
1172ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1173ad135b5dSChristopher Siden {
11742acef22dSMatthew Ahrens 	if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
11752acef22dSMatthew Ahrens 		vdev_config_dirty(spa->spa_root_vdev);
1176ad135b5dSChristopher Siden }
1177ad135b5dSChristopher Siden 
1178fa9e4066Sahrens /*
1179fa9e4066Sahrens  * Rename a spa_t.
1180fa9e4066Sahrens  */
1181fa9e4066Sahrens int
1182fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1183fa9e4066Sahrens {
1184fa9e4066Sahrens 	spa_t *spa;
1185fa9e4066Sahrens 	int err;
1186fa9e4066Sahrens 
1187fa9e4066Sahrens 	/*
1188fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1189fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1190fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1191ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1192fa9e4066Sahrens 	 */
1193fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1194fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1195fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1196fa9e4066Sahrens 		return (err);
1197fa9e4066Sahrens 	}
1198fa9e4066Sahrens 
1199e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1200fa9e4066Sahrens 
1201fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1202e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1203fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1204fa9e4066Sahrens 
1205fa9e4066Sahrens 	/*
1206fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1207fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1208fa9e4066Sahrens 	 * during the sync.
1209fa9e4066Sahrens 	 */
1210fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1211fa9e4066Sahrens 
1212e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1213fa9e4066Sahrens 
12140373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1215fa9e4066Sahrens 
1216fa9e4066Sahrens 	/*
1217fa9e4066Sahrens 	 * Sync the updated config cache.
1218fa9e4066Sahrens 	 */
1219c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1220fa9e4066Sahrens 
1221fa9e4066Sahrens 	spa_close(spa, FTAG);
1222fa9e4066Sahrens 
1223fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1224fa9e4066Sahrens 
1225fa9e4066Sahrens 	return (0);
1226fa9e4066Sahrens }
1227fa9e4066Sahrens 
1228fa9e4066Sahrens /*
1229f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1230f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1231f9af39baSGeorge Wilson  * a device with the specified device_guid.
1232fa9e4066Sahrens  */
1233f9af39baSGeorge Wilson spa_t *
1234f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1235fa9e4066Sahrens {
1236fa9e4066Sahrens 	spa_t *spa;
1237fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1238fa9e4066Sahrens 
1239ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1240fa9e4066Sahrens 
1241fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1242fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1243fa9e4066Sahrens 			continue;
1244fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1245fa9e4066Sahrens 			continue;
124639c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
124739c23413Seschrock 			if (device_guid == 0)
124839c23413Seschrock 				break;
124939c23413Seschrock 
125039c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
125139c23413Seschrock 			    device_guid) != NULL)
125239c23413Seschrock 				break;
125339c23413Seschrock 
125439c23413Seschrock 			/*
12558654d025Sperrin 			 * Check any devices we may be in the process of adding.
125639c23413Seschrock 			 */
125739c23413Seschrock 			if (spa->spa_pending_vdev) {
125839c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
125939c23413Seschrock 				    device_guid) != NULL)
126039c23413Seschrock 					break;
126139c23413Seschrock 			}
126239c23413Seschrock 		}
1263fa9e4066Sahrens 	}
1264fa9e4066Sahrens 
1265f9af39baSGeorge Wilson 	return (spa);
1266f9af39baSGeorge Wilson }
1267f9af39baSGeorge Wilson 
1268f9af39baSGeorge Wilson /*
1269f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1270f9af39baSGeorge Wilson  */
1271f9af39baSGeorge Wilson boolean_t
1272f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1273f9af39baSGeorge Wilson {
1274f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1275fa9e4066Sahrens }
1276fa9e4066Sahrens 
1277fa9e4066Sahrens char *
1278fa9e4066Sahrens spa_strdup(const char *s)
1279fa9e4066Sahrens {
1280fa9e4066Sahrens 	size_t len;
1281fa9e4066Sahrens 	char *new;
1282fa9e4066Sahrens 
1283fa9e4066Sahrens 	len = strlen(s);
1284fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1285fa9e4066Sahrens 	bcopy(s, new, len);
1286fa9e4066Sahrens 	new[len] = '\0';
1287fa9e4066Sahrens 
1288fa9e4066Sahrens 	return (new);
1289fa9e4066Sahrens }
1290fa9e4066Sahrens 
1291fa9e4066Sahrens void
1292fa9e4066Sahrens spa_strfree(char *s)
1293fa9e4066Sahrens {
1294fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1295fa9e4066Sahrens }
1296fa9e4066Sahrens 
1297fa9e4066Sahrens uint64_t
1298fa9e4066Sahrens spa_get_random(uint64_t range)
1299fa9e4066Sahrens {
1300fa9e4066Sahrens 	uint64_t r;
1301fa9e4066Sahrens 
1302fa9e4066Sahrens 	ASSERT(range != 0);
1303fa9e4066Sahrens 
1304fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1305fa9e4066Sahrens 
1306fa9e4066Sahrens 	return (r % range);
1307fa9e4066Sahrens }
1308fa9e4066Sahrens 
13091195e687SMark J Musante uint64_t
13101195e687SMark J Musante spa_generate_guid(spa_t *spa)
13111195e687SMark J Musante {
13121195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
13131195e687SMark J Musante 
13141195e687SMark J Musante 	if (spa != NULL) {
13151195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
13161195e687SMark J Musante 			guid = spa_get_random(-1ULL);
13171195e687SMark J Musante 	} else {
13181195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
13191195e687SMark J Musante 			guid = spa_get_random(-1ULL);
13201195e687SMark J Musante 	}
13211195e687SMark J Musante 
13221195e687SMark J Musante 	return (guid);
13231195e687SMark J Musante }
13241195e687SMark J Musante 
1325fa9e4066Sahrens void
132643466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1327fa9e4066Sahrens {
1328ad135b5dSChristopher Siden 	char type[256];
1329f0ba89beSJeff Bonwick 	char *checksum = NULL;
1330f0ba89beSJeff Bonwick 	char *compress = NULL;
1331f0ba89beSJeff Bonwick 
1332f0ba89beSJeff Bonwick 	if (bp != NULL) {
1333ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1334ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1335ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1336ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1337ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1338ad135b5dSChristopher Siden 			    "metadata" : "data",
1339ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1340ad135b5dSChristopher Siden 		} else {
1341ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1342ad135b5dSChristopher Siden 			    sizeof (type));
1343ad135b5dSChristopher Siden 		}
1344f0ba89beSJeff Bonwick 		checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1345f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1346f0ba89beSJeff Bonwick 	}
1347fa9e4066Sahrens 
134843466aaeSMax Grossman 	SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
134943466aaeSMax Grossman 	    compress);
1350fa9e4066Sahrens }
1351fa9e4066Sahrens 
1352fa9e4066Sahrens void
1353fa9e4066Sahrens spa_freeze(spa_t *spa)
1354fa9e4066Sahrens {
1355fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1356fa9e4066Sahrens 
1357e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1358fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1359fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1360fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1361fa9e4066Sahrens 	}
1362e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1363fa9e4066Sahrens 	if (freeze_txg != 0)
1364fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1365fa9e4066Sahrens }
1366fa9e4066Sahrens 
13670125049cSahrens void
13680125049cSahrens zfs_panic_recover(const char *fmt, ...)
13690125049cSahrens {
13700125049cSahrens 	va_list adx;
13710125049cSahrens 
13720125049cSahrens 	va_start(adx, fmt);
13730125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
13740125049cSahrens 	va_end(adx);
13750125049cSahrens }
13760125049cSahrens 
13773f9d6ad7SLin Ling /*
13783f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
1379f7170741SWill Andrews  * lowercase hexadecimal numbers that don't overflow.
13803f9d6ad7SLin Ling  */
13813f9d6ad7SLin Ling uint64_t
13823f9d6ad7SLin Ling strtonum(const char *str, char **nptr)
13833f9d6ad7SLin Ling {
13843f9d6ad7SLin Ling 	uint64_t val = 0;
13853f9d6ad7SLin Ling 	char c;
13863f9d6ad7SLin Ling 	int digit;
13873f9d6ad7SLin Ling 
13883f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
13893f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
13903f9d6ad7SLin Ling 			digit = c - '0';
13913f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
13923f9d6ad7SLin Ling 			digit = 10 + c - 'a';
13933f9d6ad7SLin Ling 		else
13943f9d6ad7SLin Ling 			break;
13953f9d6ad7SLin Ling 
13963f9d6ad7SLin Ling 		val *= 16;
13973f9d6ad7SLin Ling 		val += digit;
13983f9d6ad7SLin Ling 
13993f9d6ad7SLin Ling 		str++;
14003f9d6ad7SLin Ling 	}
14013f9d6ad7SLin Ling 
14023f9d6ad7SLin Ling 	if (nptr)
14033f9d6ad7SLin Ling 		*nptr = (char *)str;
14043f9d6ad7SLin Ling 
14053f9d6ad7SLin Ling 	return (val);
14063f9d6ad7SLin Ling }
14073f9d6ad7SLin Ling 
1408fa9e4066Sahrens /*
1409fa9e4066Sahrens  * ==========================================================================
1410fa9e4066Sahrens  * Accessor functions
1411fa9e4066Sahrens  * ==========================================================================
1412fa9e4066Sahrens  */
1413fa9e4066Sahrens 
1414088f3894Sahrens boolean_t
141588b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1416fa9e4066Sahrens {
141788b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1418fa9e4066Sahrens }
1419fa9e4066Sahrens 
1420fa9e4066Sahrens dsl_pool_t *
1421fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1422fa9e4066Sahrens {
1423fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1424fa9e4066Sahrens }
1425fa9e4066Sahrens 
1426ad135b5dSChristopher Siden boolean_t
1427ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1428ad135b5dSChristopher Siden {
1429ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1430ad135b5dSChristopher Siden }
1431ad135b5dSChristopher Siden 
1432fa9e4066Sahrens blkptr_t *
1433fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1434fa9e4066Sahrens {
1435fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1436fa9e4066Sahrens }
1437fa9e4066Sahrens 
1438fa9e4066Sahrens void
1439fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1440fa9e4066Sahrens {
1441fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1442fa9e4066Sahrens }
1443fa9e4066Sahrens 
1444fa9e4066Sahrens void
1445fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1446fa9e4066Sahrens {
1447fa9e4066Sahrens 	if (spa->spa_root == NULL)
1448fa9e4066Sahrens 		buf[0] = '\0';
1449fa9e4066Sahrens 	else
1450fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1451fa9e4066Sahrens }
1452fa9e4066Sahrens 
1453fa9e4066Sahrens int
1454fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1455fa9e4066Sahrens {
1456fa9e4066Sahrens 	return (spa->spa_sync_pass);
1457fa9e4066Sahrens }
1458fa9e4066Sahrens 
1459fa9e4066Sahrens char *
1460fa9e4066Sahrens spa_name(spa_t *spa)
1461fa9e4066Sahrens {
1462fa9e4066Sahrens 	return (spa->spa_name);
1463fa9e4066Sahrens }
1464fa9e4066Sahrens 
1465fa9e4066Sahrens uint64_t
1466fa9e4066Sahrens spa_guid(spa_t *spa)
1467fa9e4066Sahrens {
1468dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1469dfbb9432SGeorge Wilson 	uint64_t guid;
1470dfbb9432SGeorge Wilson 
1471b5989ec7Seschrock 	/*
1472b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1473b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1474e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1475b5989ec7Seschrock 	 * this case.
1476b5989ec7Seschrock 	 */
1477dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1478dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1479dfbb9432SGeorge Wilson 
1480dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1481dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1482dfbb9432SGeorge Wilson 
1483dfbb9432SGeorge Wilson 	/*
1484dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1485dfbb9432SGeorge Wilson 	 * in syncing context.
1486dfbb9432SGeorge Wilson 	 */
1487dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1488b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1489b5989ec7Seschrock 	else
1490dfbb9432SGeorge Wilson 		return (guid);
1491e9103aaeSGarrett D'Amore }
1492e9103aaeSGarrett D'Amore 
1493e9103aaeSGarrett D'Amore uint64_t
1494e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1495e9103aaeSGarrett D'Amore {
1496e9103aaeSGarrett D'Amore 	/*
1497e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1498e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1499e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1500e9103aaeSGarrett D'Amore 	 */
1501e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1502fa9e4066Sahrens }
1503fa9e4066Sahrens 
1504fa9e4066Sahrens uint64_t
1505fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1506fa9e4066Sahrens {
1507fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1508fa9e4066Sahrens }
1509fa9e4066Sahrens 
1510fa9e4066Sahrens uint64_t
1511fa9e4066Sahrens spa_first_txg(spa_t *spa)
1512fa9e4066Sahrens {
1513fa9e4066Sahrens 	return (spa->spa_first_txg);
1514fa9e4066Sahrens }
1515fa9e4066Sahrens 
1516b24ab676SJeff Bonwick uint64_t
1517b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1518b24ab676SJeff Bonwick {
1519b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1520b24ab676SJeff Bonwick }
1521b24ab676SJeff Bonwick 
152288b7b0f2SMatthew Ahrens pool_state_t
1523fa9e4066Sahrens spa_state(spa_t *spa)
1524fa9e4066Sahrens {
1525fa9e4066Sahrens 	return (spa->spa_state);
1526fa9e4066Sahrens }
1527fa9e4066Sahrens 
1528b16da2e2SGeorge Wilson spa_load_state_t
1529b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1530b16da2e2SGeorge Wilson {
1531b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1532b16da2e2SGeorge Wilson }
1533b16da2e2SGeorge Wilson 
1534fa9e4066Sahrens uint64_t
1535fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1536fa9e4066Sahrens {
1537fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1538fa9e4066Sahrens }
1539fa9e4066Sahrens 
1540fa9e4066Sahrens /* ARGSUSED */
1541fa9e4066Sahrens uint64_t
1542fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1543fa9e4066Sahrens {
154469962b56SMatthew Ahrens 	return (lsize * spa_asize_inflation);
154544cd46caSbillm }
154644cd46caSbillm 
1547485bbbf5SGeorge Wilson uint64_t
1548485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1549485bbbf5SGeorge Wilson {
1550485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1551485bbbf5SGeorge Wilson }
1552485bbbf5SGeorge Wilson 
1553485bbbf5SGeorge Wilson void
1554485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1555485bbbf5SGeorge Wilson {
1556485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1557485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1558485bbbf5SGeorge Wilson }
1559485bbbf5SGeorge Wilson 
15600a4e9518Sgw /*
15610a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
15620a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
15630a4e9518Sgw  */
15640a4e9518Sgw uint8_t
15650a4e9518Sgw spa_get_failmode(spa_t *spa)
15660a4e9518Sgw {
15670a4e9518Sgw 	return (spa->spa_failmode);
15680a4e9518Sgw }
15690a4e9518Sgw 
1570e14bb325SJeff Bonwick boolean_t
1571e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1572e14bb325SJeff Bonwick {
1573e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1574e14bb325SJeff Bonwick }
1575e14bb325SJeff Bonwick 
157644cd46caSbillm uint64_t
157744cd46caSbillm spa_version(spa_t *spa)
157844cd46caSbillm {
157944cd46caSbillm 	return (spa->spa_ubsync.ub_version);
158044cd46caSbillm }
158144cd46caSbillm 
1582b24ab676SJeff Bonwick boolean_t
1583b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1584b24ab676SJeff Bonwick {
1585b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1586b24ab676SJeff Bonwick }
1587b24ab676SJeff Bonwick 
1588b24ab676SJeff Bonwick metaslab_class_t *
1589b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1590b24ab676SJeff Bonwick {
1591b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1592b24ab676SJeff Bonwick }
1593b24ab676SJeff Bonwick 
1594b24ab676SJeff Bonwick metaslab_class_t *
1595b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1596b24ab676SJeff Bonwick {
1597b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1598b24ab676SJeff Bonwick }
1599b24ab676SJeff Bonwick 
160044cd46caSbillm int
160144cd46caSbillm spa_max_replication(spa_t *spa)
160244cd46caSbillm {
160344cd46caSbillm 	/*
1604e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
160544cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
160644cd46caSbillm 	 * replication level accordingly.
1607fa9e4066Sahrens 	 */
1608e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
160944cd46caSbillm 		return (1);
161044cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1611fa9e4066Sahrens }
1612fa9e4066Sahrens 
16133f9d6ad7SLin Ling int
16143f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
16153f9d6ad7SLin Ling {
16163f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
16173f9d6ad7SLin Ling }
16183f9d6ad7SLin Ling 
1619283b8460SGeorge.Wilson uint64_t
1620283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1621283b8460SGeorge.Wilson {
1622283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1623283b8460SGeorge.Wilson }
1624283b8460SGeorge.Wilson 
162599653d4eSeschrock uint64_t
1626b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
162799653d4eSeschrock {
1628b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1629b24ab676SJeff Bonwick 	uint64_t dsize = asize;
163099653d4eSeschrock 
1631b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
163299653d4eSeschrock 
1633b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1634b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1635b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
163699653d4eSeschrock 	}
1637b24ab676SJeff Bonwick 
1638b24ab676SJeff Bonwick 	return (dsize);
1639b24ab676SJeff Bonwick }
1640b24ab676SJeff Bonwick 
1641b24ab676SJeff Bonwick uint64_t
1642b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1643b24ab676SJeff Bonwick {
1644b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1645b24ab676SJeff Bonwick 
1646b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1647b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1648b24ab676SJeff Bonwick 
1649b24ab676SJeff Bonwick 	return (dsize);
1650b24ab676SJeff Bonwick }
1651b24ab676SJeff Bonwick 
1652b24ab676SJeff Bonwick uint64_t
1653b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1654b24ab676SJeff Bonwick {
1655b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1656b24ab676SJeff Bonwick 
1657b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1658b24ab676SJeff Bonwick 
1659b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1660b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1661b24ab676SJeff Bonwick 
1662e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1663b24ab676SJeff Bonwick 
1664b24ab676SJeff Bonwick 	return (dsize);
166599653d4eSeschrock }
166699653d4eSeschrock 
1667fa9e4066Sahrens /*
1668fa9e4066Sahrens  * ==========================================================================
1669fa9e4066Sahrens  * Initialization and Termination
1670fa9e4066Sahrens  * ==========================================================================
1671fa9e4066Sahrens  */
1672fa9e4066Sahrens 
1673fa9e4066Sahrens static int
1674fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1675fa9e4066Sahrens {
1676fa9e4066Sahrens 	const spa_t *s1 = a1;
1677fa9e4066Sahrens 	const spa_t *s2 = a2;
1678fa9e4066Sahrens 	int s;
1679fa9e4066Sahrens 
1680fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1681fa9e4066Sahrens 	if (s > 0)
1682fa9e4066Sahrens 		return (1);
1683fa9e4066Sahrens 	if (s < 0)
1684fa9e4066Sahrens 		return (-1);
1685fa9e4066Sahrens 	return (0);
1686fa9e4066Sahrens }
1687fa9e4066Sahrens 
16880373e76bSbonwick int
16890373e76bSbonwick spa_busy(void)
16900373e76bSbonwick {
16910373e76bSbonwick 	return (spa_active_count);
16920373e76bSbonwick }
16930373e76bSbonwick 
1694e7cbe64fSgw void
1695e7cbe64fSgw spa_boot_init()
1696e7cbe64fSgw {
1697e7cbe64fSgw 	spa_config_load();
1698e7cbe64fSgw }
1699e7cbe64fSgw 
1700fa9e4066Sahrens void
1701fa9e4066Sahrens spa_init(int mode)
1702fa9e4066Sahrens {
1703fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1704c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1705fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1706fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1707fa9e4066Sahrens 
1708fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1709fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1710fa9e4066Sahrens 
1711fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1712fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1713fa94a07fSbrendan 
1714fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1715fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
171699653d4eSeschrock 
17178ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1718fa9e4066Sahrens 
1719283b8460SGeorge.Wilson #ifdef _KERNEL
1720283b8460SGeorge.Wilson 	spa_arch_init();
1721283b8460SGeorge.Wilson #else
1722cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1723cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1724cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1725cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1726cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1727cd1c8b85SMatthew Ahrens 		} else {
1728cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1729cd1c8b85SMatthew Ahrens 		}
1730cd1c8b85SMatthew Ahrens 	}
1731cd1c8b85SMatthew Ahrens #endif
1732cd1c8b85SMatthew Ahrens 
1733fa9e4066Sahrens 	refcount_init();
1734fa9e4066Sahrens 	unique_init();
17350713e232SGeorge Wilson 	range_tree_init();
1736fa9e4066Sahrens 	zio_init();
1737fa9e4066Sahrens 	dmu_init();
1738fa9e4066Sahrens 	zil_init();
173987db74c1Sek 	vdev_cache_stat_init();
174091ebeef5Sahrens 	zfs_prop_init();
1741990b4856Slling 	zpool_prop_init();
1742ad135b5dSChristopher Siden 	zpool_feature_init();
1743fa9e4066Sahrens 	spa_config_load();
1744e14bb325SJeff Bonwick 	l2arc_start();
1745fa9e4066Sahrens }
1746fa9e4066Sahrens 
1747fa9e4066Sahrens void
1748fa9e4066Sahrens spa_fini(void)
1749fa9e4066Sahrens {
1750e14bb325SJeff Bonwick 	l2arc_stop();
1751e14bb325SJeff Bonwick 
1752fa9e4066Sahrens 	spa_evict_all();
1753fa9e4066Sahrens 
175487db74c1Sek 	vdev_cache_stat_fini();
1755fa9e4066Sahrens 	zil_fini();
1756fa9e4066Sahrens 	dmu_fini();
1757fa9e4066Sahrens 	zio_fini();
17580713e232SGeorge Wilson 	range_tree_fini();
175991ebeef5Sahrens 	unique_fini();
1760fa9e4066Sahrens 	refcount_fini();
1761fa9e4066Sahrens 
1762fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
176399653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1764fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1765fa9e4066Sahrens 
1766fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1767fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1768c25056deSgw 	mutex_destroy(&spa_spare_lock);
1769fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1770fa9e4066Sahrens }
17716ce0521aSperrin 
17726ce0521aSperrin /*
17736ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
17746ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
17756ce0521aSperrin  * performance and not correctness
17766ce0521aSperrin  */
17776ce0521aSperrin boolean_t
17786ce0521aSperrin spa_has_slogs(spa_t *spa)
17796ce0521aSperrin {
17806ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
17816ce0521aSperrin }
1782bf82a41bSeschrock 
1783b24ab676SJeff Bonwick spa_log_state_t
1784b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1785b24ab676SJeff Bonwick {
1786b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1787b24ab676SJeff Bonwick }
1788b24ab676SJeff Bonwick 
1789b24ab676SJeff Bonwick void
1790b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1791b24ab676SJeff Bonwick {
1792b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1793b24ab676SJeff Bonwick }
1794b24ab676SJeff Bonwick 
1795bf82a41bSeschrock boolean_t
1796bf82a41bSeschrock spa_is_root(spa_t *spa)
1797bf82a41bSeschrock {
1798bf82a41bSeschrock 	return (spa->spa_is_root);
1799bf82a41bSeschrock }
18008ad4d6ddSJeff Bonwick 
18018ad4d6ddSJeff Bonwick boolean_t
18028ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
18038ad4d6ddSJeff Bonwick {
18048ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
18058ad4d6ddSJeff Bonwick }
18068ad4d6ddSJeff Bonwick 
18078ad4d6ddSJeff Bonwick int
18088ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
18098ad4d6ddSJeff Bonwick {
18108ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
18118ad4d6ddSJeff Bonwick }
1812b24ab676SJeff Bonwick 
1813b24ab676SJeff Bonwick uint64_t
1814b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1815b24ab676SJeff Bonwick {
1816b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1817b24ab676SJeff Bonwick }
1818b24ab676SJeff Bonwick 
1819b24ab676SJeff Bonwick uint64_t
1820b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1821b24ab676SJeff Bonwick {
1822b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1823b24ab676SJeff Bonwick }
1824b24ab676SJeff Bonwick 
1825b24ab676SJeff Bonwick objset_t *
1826b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1827b24ab676SJeff Bonwick {
1828b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1829b24ab676SJeff Bonwick }
1830b24ab676SJeff Bonwick 
1831b24ab676SJeff Bonwick enum zio_checksum
1832b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1833b24ab676SJeff Bonwick {
1834b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1835b24ab676SJeff Bonwick }
18363f9d6ad7SLin Ling 
18373f9d6ad7SLin Ling /*
18383f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
18393f9d6ad7SLin Ling  */
18403f9d6ad7SLin Ling void
18413f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
18423f9d6ad7SLin Ling {
18433f9d6ad7SLin Ling 	/* data not stored on disk */
18443f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
18453f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
18463f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
18473f9d6ad7SLin Ling }
18483f9d6ad7SLin Ling 
18493f9d6ad7SLin Ling /*
18503f9d6ad7SLin Ling  * Get scan stats for zpool status reports
18513f9d6ad7SLin Ling  */
18523f9d6ad7SLin Ling int
18533f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
18543f9d6ad7SLin Ling {
18553f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
18563f9d6ad7SLin Ling 
18573f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1858be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
18593f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
18603f9d6ad7SLin Ling 
18613f9d6ad7SLin Ling 	/* data stored on disk */
18623f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
18633f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
18643f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
18653f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
18663f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
18673f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
18683f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
18693f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
18703f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
18713f9d6ad7SLin Ling 
18723f9d6ad7SLin Ling 	/* data not stored on disk */
18733f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
18743f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
18753f9d6ad7SLin Ling 
18763f9d6ad7SLin Ling 	return (0);
18773f9d6ad7SLin Ling }
187809c9d376SGeorge Wilson 
187909c9d376SGeorge Wilson boolean_t
188009c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
188109c9d376SGeorge Wilson {
188209c9d376SGeorge Wilson 	return (spa->spa_debug);
188309c9d376SGeorge Wilson }
1884