xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 9842588ba779c98194a9c238d52784a2e45a850e)
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 /*
22*9842588bSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23fa9e4066Sahrens  */
24fa9e4066Sahrens 
25fa9e4066Sahrens #include <sys/zfs_context.h>
26fa9e4066Sahrens #include <sys/spa_impl.h>
27fa9e4066Sahrens #include <sys/zio.h>
28fa9e4066Sahrens #include <sys/zio_checksum.h>
29fa9e4066Sahrens #include <sys/zio_compress.h>
30fa9e4066Sahrens #include <sys/dmu.h>
31fa9e4066Sahrens #include <sys/dmu_tx.h>
32fa9e4066Sahrens #include <sys/zap.h>
33fa9e4066Sahrens #include <sys/zil.h>
34fa9e4066Sahrens #include <sys/vdev_impl.h>
35fa9e4066Sahrens #include <sys/metaslab.h>
36fa9e4066Sahrens #include <sys/uberblock_impl.h>
37fa9e4066Sahrens #include <sys/txg.h>
38fa9e4066Sahrens #include <sys/avl.h>
39fa9e4066Sahrens #include <sys/unique.h>
40fa9e4066Sahrens #include <sys/dsl_pool.h>
41fa9e4066Sahrens #include <sys/dsl_dir.h>
42fa9e4066Sahrens #include <sys/dsl_prop.h>
43fa9e4066Sahrens #include <sys/fs/zfs.h>
446ce0521aSperrin #include <sys/metaslab_impl.h>
45e14bb325SJeff Bonwick #include <sys/arc.h>
46485bbbf5SGeorge Wilson #include <sys/ddt.h>
4791ebeef5Sahrens #include "zfs_prop.h"
48fa9e4066Sahrens 
49fa9e4066Sahrens /*
50fa9e4066Sahrens  * SPA locking
51fa9e4066Sahrens  *
52fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * spa_namespace_lock (global mutex)
55fa9e4066Sahrens  *
5644cd46caSbillm  *	This lock must be acquired to do any of the following:
57fa9e4066Sahrens  *
5844cd46caSbillm  *		- Lookup a spa_t by name
5944cd46caSbillm  *		- Add or remove a spa_t from the namespace
6044cd46caSbillm  *		- Increase spa_refcount from non-zero
6144cd46caSbillm  *		- Check if spa_refcount is zero
6244cd46caSbillm  *		- Rename a spa_t
63ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
6444cd46caSbillm  *		- Held for the duration of create/destroy/import/export
65fa9e4066Sahrens  *
6644cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
6744cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
6844cd46caSbillm  *	definition they must have an existing reference, and will never need
6944cd46caSbillm  *	to lookup a spa_t by name.
70fa9e4066Sahrens  *
71fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
72fa9e4066Sahrens  *
7344cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
7444cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7544cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
76088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
7744cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
78fa9e4066Sahrens  *
79e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
80fa9e4066Sahrens  *
8191ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
8291ebeef5Sahrens  *	the following circumstances:
83fa9e4066Sahrens  *
8444cd46caSbillm  *		- RW_READER to perform I/O to the spa
8544cd46caSbillm  *		- RW_WRITER to change the vdev config
86fa9e4066Sahrens  *
87fa9e4066Sahrens  * The locking order is fairly straightforward:
88fa9e4066Sahrens  *
8944cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
90fa9e4066Sahrens  *
9144cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
9244cd46caSbillm  *	or to check if it is zero.
93fa9e4066Sahrens  *
94e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
95fa9e4066Sahrens  *
9644cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
9744cd46caSbillm  *	the config lock.
98fa9e4066Sahrens  *
99e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
100fa9e4066Sahrens  *
10144cd46caSbillm  *	The namespace lock must always be taken before the config lock.
102fa9e4066Sahrens  *
103fa9e4066Sahrens  *
104e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
105fa9e4066Sahrens  *
106e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
107e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
108fa9e4066Sahrens  *
10944cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
110fa9e4066Sahrens  *
11144cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
112fa9e4066Sahrens  *
11344cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
11444cd46caSbillm  *				frees up any memory associated with the spa_t.
115fa9e4066Sahrens  *
11644cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
11744cd46caSbillm  *				first if NULL is passed.
118fa9e4066Sahrens  *
11944cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12044cd46caSbillm  *				the system.
121fa9e4066Sahrens  *
122ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
123fa9e4066Sahrens  *
124fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
125fa9e4066Sahrens  *
12644cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
12744cd46caSbillm  *				called with spa_namespace_lock held if the
12844cd46caSbillm  *				refcount is currently zero.
129fa9e4066Sahrens  *
13044cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13144cd46caSbillm  *				not free the spa_t or remove it from the
13244cd46caSbillm  *				namespace.  No locking is required.
133fa9e4066Sahrens  *
13444cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
13544cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
13644cd46caSbillm  *				held.
137fa9e4066Sahrens  *
138e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
139e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
140e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
141e14bb325SJeff Bonwick  *
142e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
143e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
144e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
145e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
146e14bb325SJeff Bonwick  *
147e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
148e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
149e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
150e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
151e14bb325SJeff Bonwick  *
152e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
153e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
154e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
155e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
156e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
157e14bb325SJeff Bonwick  *
158e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
159e14bb325SJeff Bonwick  *
160e14bb325SJeff Bonwick  * SCL_CONFIG
161e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
162e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
163e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
164e14bb325SJeff Bonwick  *
165e14bb325SJeff Bonwick  * SCL_STATE
166e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
167e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
168e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
169e14bb325SJeff Bonwick  *
170e14bb325SJeff Bonwick  * SCL_ALLOC
171e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
172e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
173e14bb325SJeff Bonwick  *
174e14bb325SJeff Bonwick  * SCL_ZIO
175e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
176e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
177e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
178e14bb325SJeff Bonwick  *
179e14bb325SJeff Bonwick  * SCL_FREE
180e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
181e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
182e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
183e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
184e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
185e14bb325SJeff Bonwick  *
186e14bb325SJeff Bonwick  * SCL_VDEV
187e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
188b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
189e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
190e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
191e14bb325SJeff Bonwick  *
192e14bb325SJeff Bonwick  * In addition, the following rules apply:
193e14bb325SJeff Bonwick  *
194e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
195e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
196e14bb325SJeff Bonwick  *
197e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
198e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
199e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
200e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
201e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
202fa9e4066Sahrens  *
203ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
204fa9e4066Sahrens  *
20544cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
206ea8dc4b6Seschrock  *				for writing.
207fa9e4066Sahrens  *
20844cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
20944cd46caSbillm  *				to complete, sync the updated configs to the
210ea8dc4b6Seschrock  *				cache, and release the namespace lock.
211fa9e4066Sahrens  *
212e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
213e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
214e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
215e14bb325SJeff Bonwick  *
216e14bb325SJeff Bonwick  * spa_rename() is also implemented within this file since is requires
217e14bb325SJeff Bonwick  * manipulation of the namespace.
218fa9e4066Sahrens  */
219fa9e4066Sahrens 
220fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
221fa9e4066Sahrens kmutex_t spa_namespace_lock;
222fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2230373e76bSbonwick static int spa_active_count;
224416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
225fa9e4066Sahrens 
22699653d4eSeschrock static kmutex_t spa_spare_lock;
22739c23413Seschrock static avl_tree_t spa_spare_avl;
228fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
229fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23099653d4eSeschrock 
231fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2328ad4d6ddSJeff Bonwick int spa_mode_global;
233fa9e4066Sahrens 
234fa9e4066Sahrens #ifdef ZFS_DEBUG
23540feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
23640feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
237fa9e4066Sahrens #else
238fa9e4066Sahrens int zfs_flags = 0;
239fa9e4066Sahrens #endif
240fa9e4066Sahrens 
2410125049cSahrens /*
2420125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2430125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2440125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2450125049cSahrens  */
2460125049cSahrens int zfs_recover = 0;
2470125049cSahrens 
248fa9e4066Sahrens 
249e05725b1Sbonwick /*
250e05725b1Sbonwick  * ==========================================================================
251e05725b1Sbonwick  * SPA config locking
252e05725b1Sbonwick  * ==========================================================================
253e05725b1Sbonwick  */
254e05725b1Sbonwick static void
255e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
256e14bb325SJeff Bonwick {
257e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
258e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
259e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
260e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
261e14bb325SJeff Bonwick 		refcount_create(&scl->scl_count);
262e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
263e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
264e14bb325SJeff Bonwick 	}
265e05725b1Sbonwick }
266e05725b1Sbonwick 
267e05725b1Sbonwick static void
268e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
269e14bb325SJeff Bonwick {
270e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
271e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
272e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
273e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
274e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
275e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
276e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
277e14bb325SJeff Bonwick 	}
278e14bb325SJeff Bonwick }
279e14bb325SJeff Bonwick 
280e14bb325SJeff Bonwick int
281e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
282e05725b1Sbonwick {
283e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
284e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
285e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
286e14bb325SJeff Bonwick 			continue;
287e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
288e14bb325SJeff Bonwick 		if (rw == RW_READER) {
289e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
290e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
291e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
292e14bb325SJeff Bonwick 				return (0);
293e14bb325SJeff Bonwick 			}
294e14bb325SJeff Bonwick 		} else {
295e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
296e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
297e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
298e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
299e14bb325SJeff Bonwick 				return (0);
300e14bb325SJeff Bonwick 			}
301e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
302e14bb325SJeff Bonwick 		}
303e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
304e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
305e14bb325SJeff Bonwick 	}
306e14bb325SJeff Bonwick 	return (1);
307e05725b1Sbonwick }
308e05725b1Sbonwick 
309e05725b1Sbonwick void
310e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
311e05725b1Sbonwick {
312f64c0e34SEric Taylor 	int wlocks_held = 0;
313f64c0e34SEric Taylor 
314e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
315e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
316f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
317f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
318e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
319e14bb325SJeff Bonwick 			continue;
320e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
321e14bb325SJeff Bonwick 		if (rw == RW_READER) {
322e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
323e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
324e14bb325SJeff Bonwick 			}
325e14bb325SJeff Bonwick 		} else {
326e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
327e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
328e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
329e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
330e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
331e14bb325SJeff Bonwick 			}
332e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
333e14bb325SJeff Bonwick 		}
334e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
335e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
336e05725b1Sbonwick 	}
337f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
338e05725b1Sbonwick }
339e05725b1Sbonwick 
340e05725b1Sbonwick void
341e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
342e05725b1Sbonwick {
343e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
344e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
345e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
346e14bb325SJeff Bonwick 			continue;
347e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
348e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
349e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
350e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
351e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
352e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
353e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
354e14bb325SJeff Bonwick 		}
355e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
356e05725b1Sbonwick 	}
357e05725b1Sbonwick }
358e05725b1Sbonwick 
359e14bb325SJeff Bonwick int
360e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
361e05725b1Sbonwick {
362e14bb325SJeff Bonwick 	int locks_held = 0;
363e05725b1Sbonwick 
364e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
365e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
366e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
367e14bb325SJeff Bonwick 			continue;
368e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
369e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
370e14bb325SJeff Bonwick 			locks_held |= 1 << i;
371e14bb325SJeff Bonwick 	}
372e14bb325SJeff Bonwick 
373e14bb325SJeff Bonwick 	return (locks_held);
374e05725b1Sbonwick }
375e05725b1Sbonwick 
376fa9e4066Sahrens /*
377fa9e4066Sahrens  * ==========================================================================
378fa9e4066Sahrens  * SPA namespace functions
379fa9e4066Sahrens  * ==========================================================================
380fa9e4066Sahrens  */
381fa9e4066Sahrens 
382fa9e4066Sahrens /*
383fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
384fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
385fa9e4066Sahrens  */
386fa9e4066Sahrens spa_t *
387fa9e4066Sahrens spa_lookup(const char *name)
388fa9e4066Sahrens {
389e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
390e14bb325SJeff Bonwick 	spa_t *spa;
391fa9e4066Sahrens 	avl_index_t where;
39240feaa91Sahrens 	char c;
39340feaa91Sahrens 	char *cp;
394fa9e4066Sahrens 
395fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
396fa9e4066Sahrens 
39740feaa91Sahrens 	/*
39840feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
39940feaa91Sahrens 	 * just use that.
40040feaa91Sahrens 	 */
40140feaa91Sahrens 	cp = strpbrk(name, "/@");
40240feaa91Sahrens 	if (cp) {
40340feaa91Sahrens 		c = *cp;
40440feaa91Sahrens 		*cp = '\0';
40540feaa91Sahrens 	}
40640feaa91Sahrens 
407e14bb325SJeff Bonwick 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
408fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
409fa9e4066Sahrens 
41040feaa91Sahrens 	if (cp)
41140feaa91Sahrens 		*cp = c;
41240feaa91Sahrens 
413fa9e4066Sahrens 	return (spa);
414fa9e4066Sahrens }
415fa9e4066Sahrens 
416fa9e4066Sahrens /*
417fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
418fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
419fa9e4066Sahrens  * exist by calling spa_lookup() first.
420fa9e4066Sahrens  */
421fa9e4066Sahrens spa_t *
422468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
423fa9e4066Sahrens {
424fa9e4066Sahrens 	spa_t *spa;
425c5904d13Seschrock 	spa_config_dirent_t *dp;
426fa9e4066Sahrens 
427fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
428fa9e4066Sahrens 
429fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
430fa9e4066Sahrens 
431c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
432c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
43335a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
434c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
43535a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
436c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
43735a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
438a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
439a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
440c25056deSgw 
441c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
44235a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
443c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
444e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
445c25056deSgw 
446b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
447b24ab676SJeff Bonwick 		bplist_init(&spa->spa_free_bplist[t]);
448b24ab676SJeff Bonwick 	bplist_init(&spa->spa_deferred_bplist);
449b24ab676SJeff Bonwick 
450e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
451fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
452fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
4530373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
454468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
45535a5a358SJonathan Adams 	spa->spa_proc = &p0;
45635a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
457fa9e4066Sahrens 
458fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
459e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
460fa9e4066Sahrens 
461fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
462fa9e4066Sahrens 
4630373e76bSbonwick 	/*
4640373e76bSbonwick 	 * Set the alternate root, if there is one.
4650373e76bSbonwick 	 */
4660373e76bSbonwick 	if (altroot) {
4670373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
4680373e76bSbonwick 		spa_active_count++;
4690373e76bSbonwick 	}
4700373e76bSbonwick 
471c5904d13Seschrock 	/*
472c5904d13Seschrock 	 * Every pool starts with the default cachefile
473c5904d13Seschrock 	 */
474c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
475c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
476c5904d13Seschrock 
477c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
478ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
479c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
480c5904d13Seschrock 
481468c413aSTim Haley 	if (config != NULL)
482468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
483468c413aSTim Haley 
484fa9e4066Sahrens 	return (spa);
485fa9e4066Sahrens }
486fa9e4066Sahrens 
487fa9e4066Sahrens /*
488fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
489fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
490fa9e4066Sahrens  * deactivated.
491fa9e4066Sahrens  */
492fa9e4066Sahrens void
493fa9e4066Sahrens spa_remove(spa_t *spa)
494fa9e4066Sahrens {
495c5904d13Seschrock 	spa_config_dirent_t *dp;
496c5904d13Seschrock 
497fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
498fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
499fa9e4066Sahrens 
5001195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
5011195e687SMark J Musante 
502fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
503fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
504fa9e4066Sahrens 
5050373e76bSbonwick 	if (spa->spa_root) {
506fa9e4066Sahrens 		spa_strfree(spa->spa_root);
5070373e76bSbonwick 		spa_active_count--;
5080373e76bSbonwick 	}
509fa9e4066Sahrens 
510c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
511c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
512c5904d13Seschrock 		if (dp->scd_path != NULL)
513c5904d13Seschrock 			spa_strfree(dp->scd_path);
514c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
515c5904d13Seschrock 	}
516c5904d13Seschrock 
517c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
5182f8aaab3Seschrock 
519fa9e4066Sahrens 	spa_config_set(spa, NULL);
520fa9e4066Sahrens 
521fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
52291ebeef5Sahrens 
523e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
524fa9e4066Sahrens 
525b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
526b24ab676SJeff Bonwick 		bplist_fini(&spa->spa_free_bplist[t]);
527b24ab676SJeff Bonwick 	bplist_fini(&spa->spa_deferred_bplist);
528b24ab676SJeff Bonwick 
529c25056deSgw 	cv_destroy(&spa->spa_async_cv);
53035a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
531c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
532e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
533c25056deSgw 
5345ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
535c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
53635a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
53706eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
53835a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
539b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
54035a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
541e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
542a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
5435ad82045Snd 
544fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
545fa9e4066Sahrens }
546fa9e4066Sahrens 
547fa9e4066Sahrens /*
548fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
549fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
550fa9e4066Sahrens  */
551fa9e4066Sahrens spa_t *
552fa9e4066Sahrens spa_next(spa_t *prev)
553fa9e4066Sahrens {
554fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
555fa9e4066Sahrens 
556fa9e4066Sahrens 	if (prev)
557fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
558fa9e4066Sahrens 	else
559fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
560fa9e4066Sahrens }
561fa9e4066Sahrens 
562fa9e4066Sahrens /*
563fa9e4066Sahrens  * ==========================================================================
564fa9e4066Sahrens  * SPA refcount functions
565fa9e4066Sahrens  * ==========================================================================
566fa9e4066Sahrens  */
567fa9e4066Sahrens 
568fa9e4066Sahrens /*
569fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
570fa9e4066Sahrens  * have the namespace lock held.
571fa9e4066Sahrens  */
572fa9e4066Sahrens void
573fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
574fa9e4066Sahrens {
575088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
576fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
577fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
578fa9e4066Sahrens }
579fa9e4066Sahrens 
580fa9e4066Sahrens /*
581fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
582fa9e4066Sahrens  * have the namespace lock held.
583fa9e4066Sahrens  */
584fa9e4066Sahrens void
585fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
586fa9e4066Sahrens {
587088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
588fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
589fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
590fa9e4066Sahrens }
591fa9e4066Sahrens 
592fa9e4066Sahrens /*
593fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
594088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
595fa9e4066Sahrens  * number of references acquired when opening a pool
596fa9e4066Sahrens  */
597fa9e4066Sahrens boolean_t
598fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
599fa9e4066Sahrens {
600fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
601fa9e4066Sahrens 
602088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
603fa9e4066Sahrens }
604fa9e4066Sahrens 
60599653d4eSeschrock /*
60699653d4eSeschrock  * ==========================================================================
607fa94a07fSbrendan  * SPA spare and l2cache tracking
60899653d4eSeschrock  * ==========================================================================
60999653d4eSeschrock  */
61099653d4eSeschrock 
611fa94a07fSbrendan /*
612fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
613fa94a07fSbrendan  * for 'auxiliary' devices.
614fa94a07fSbrendan  */
615fa94a07fSbrendan 
616fa94a07fSbrendan typedef struct spa_aux {
617fa94a07fSbrendan 	uint64_t	aux_guid;
618fa94a07fSbrendan 	uint64_t	aux_pool;
619fa94a07fSbrendan 	avl_node_t	aux_avl;
620fa94a07fSbrendan 	int		aux_count;
621fa94a07fSbrendan } spa_aux_t;
622fa94a07fSbrendan 
623fa94a07fSbrendan static int
624fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
625fa94a07fSbrendan {
626fa94a07fSbrendan 	const spa_aux_t *sa = a;
627fa94a07fSbrendan 	const spa_aux_t *sb = b;
628fa94a07fSbrendan 
629fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
630fa94a07fSbrendan 		return (-1);
631fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
632fa94a07fSbrendan 		return (1);
633fa94a07fSbrendan 	else
634fa94a07fSbrendan 		return (0);
635fa94a07fSbrendan }
636fa94a07fSbrendan 
637fa94a07fSbrendan void
638fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
639fa94a07fSbrendan {
640fa94a07fSbrendan 	avl_index_t where;
641fa94a07fSbrendan 	spa_aux_t search;
642fa94a07fSbrendan 	spa_aux_t *aux;
643fa94a07fSbrendan 
644fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
645fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
646fa94a07fSbrendan 		aux->aux_count++;
647fa94a07fSbrendan 	} else {
648fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
649fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
650fa94a07fSbrendan 		aux->aux_count = 1;
651fa94a07fSbrendan 		avl_insert(avl, aux, where);
652fa94a07fSbrendan 	}
653fa94a07fSbrendan }
654fa94a07fSbrendan 
655fa94a07fSbrendan void
656fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
657fa94a07fSbrendan {
658fa94a07fSbrendan 	spa_aux_t search;
659fa94a07fSbrendan 	spa_aux_t *aux;
660fa94a07fSbrendan 	avl_index_t where;
661fa94a07fSbrendan 
662fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
663fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
664fa94a07fSbrendan 
665fa94a07fSbrendan 	ASSERT(aux != NULL);
666fa94a07fSbrendan 
667fa94a07fSbrendan 	if (--aux->aux_count == 0) {
668fa94a07fSbrendan 		avl_remove(avl, aux);
669fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
670fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
671fa94a07fSbrendan 		aux->aux_pool = 0ULL;
672fa94a07fSbrendan 	}
673fa94a07fSbrendan }
674fa94a07fSbrendan 
675fa94a07fSbrendan boolean_t
67689a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
677fa94a07fSbrendan {
678fa94a07fSbrendan 	spa_aux_t search, *found;
679fa94a07fSbrendan 
680fa94a07fSbrendan 	search.aux_guid = guid;
68189a89ebfSlling 	found = avl_find(avl, &search, NULL);
682fa94a07fSbrendan 
683fa94a07fSbrendan 	if (pool) {
684fa94a07fSbrendan 		if (found)
685fa94a07fSbrendan 			*pool = found->aux_pool;
686fa94a07fSbrendan 		else
687fa94a07fSbrendan 			*pool = 0ULL;
688fa94a07fSbrendan 	}
689fa94a07fSbrendan 
69089a89ebfSlling 	if (refcnt) {
69189a89ebfSlling 		if (found)
69289a89ebfSlling 			*refcnt = found->aux_count;
69389a89ebfSlling 		else
69489a89ebfSlling 			*refcnt = 0;
69589a89ebfSlling 	}
69689a89ebfSlling 
697fa94a07fSbrendan 	return (found != NULL);
698fa94a07fSbrendan }
699fa94a07fSbrendan 
700fa94a07fSbrendan void
701fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
702fa94a07fSbrendan {
703fa94a07fSbrendan 	spa_aux_t search, *found;
704fa94a07fSbrendan 	avl_index_t where;
705fa94a07fSbrendan 
706fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
707fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
708fa94a07fSbrendan 	ASSERT(found != NULL);
709fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
710fa94a07fSbrendan 
711fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
712fa94a07fSbrendan }
713fa94a07fSbrendan 
71499653d4eSeschrock /*
71539c23413Seschrock  * Spares are tracked globally due to the following constraints:
71639c23413Seschrock  *
71739c23413Seschrock  * 	- A spare may be part of multiple pools.
71839c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
71939c23413Seschrock  *	  another pool.
72039c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
72139c23413Seschrock  *	  the target is a spare in the same pool.
72239c23413Seschrock  *
72339c23413Seschrock  * We keep track of all spares on the system through the use of a reference
72439c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
72539c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
72639c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
72739c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
72839c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
72939c23413Seschrock  *
73039c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
73139c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
73239c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
73339c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
73499653d4eSeschrock  */
73539c23413Seschrock 
73699653d4eSeschrock static int
73799653d4eSeschrock spa_spare_compare(const void *a, const void *b)
73899653d4eSeschrock {
739fa94a07fSbrendan 	return (spa_aux_compare(a, b));
74099653d4eSeschrock }
74199653d4eSeschrock 
74299653d4eSeschrock void
74339c23413Seschrock spa_spare_add(vdev_t *vd)
74499653d4eSeschrock {
74599653d4eSeschrock 	mutex_enter(&spa_spare_lock);
74639c23413Seschrock 	ASSERT(!vd->vdev_isspare);
747fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
74839c23413Seschrock 	vd->vdev_isspare = B_TRUE;
74999653d4eSeschrock 	mutex_exit(&spa_spare_lock);
75099653d4eSeschrock }
75199653d4eSeschrock 
75299653d4eSeschrock void
75339c23413Seschrock spa_spare_remove(vdev_t *vd)
75499653d4eSeschrock {
75599653d4eSeschrock 	mutex_enter(&spa_spare_lock);
75639c23413Seschrock 	ASSERT(vd->vdev_isspare);
757fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
75839c23413Seschrock 	vd->vdev_isspare = B_FALSE;
75999653d4eSeschrock 	mutex_exit(&spa_spare_lock);
76099653d4eSeschrock }
76199653d4eSeschrock 
76299653d4eSeschrock boolean_t
76389a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
76499653d4eSeschrock {
765fa94a07fSbrendan 	boolean_t found;
76699653d4eSeschrock 
76799653d4eSeschrock 	mutex_enter(&spa_spare_lock);
76889a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
76999653d4eSeschrock 	mutex_exit(&spa_spare_lock);
77099653d4eSeschrock 
771fa94a07fSbrendan 	return (found);
77239c23413Seschrock }
77339c23413Seschrock 
77439c23413Seschrock void
77539c23413Seschrock spa_spare_activate(vdev_t *vd)
77639c23413Seschrock {
77739c23413Seschrock 	mutex_enter(&spa_spare_lock);
77839c23413Seschrock 	ASSERT(vd->vdev_isspare);
779fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
780fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
781fa94a07fSbrendan }
78239c23413Seschrock 
783fa94a07fSbrendan /*
784fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
785fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
786fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
787fa94a07fSbrendan  */
78839c23413Seschrock 
789fa94a07fSbrendan static int
790fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
791fa94a07fSbrendan {
792fa94a07fSbrendan 	return (spa_aux_compare(a, b));
793fa94a07fSbrendan }
794fa94a07fSbrendan 
795fa94a07fSbrendan void
796fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
797fa94a07fSbrendan {
798fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
799fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
800fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
801fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
802fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
803fa94a07fSbrendan }
804fa94a07fSbrendan 
805fa94a07fSbrendan void
806fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
807fa94a07fSbrendan {
808fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
809fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
810fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
811fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
812fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
813fa94a07fSbrendan }
814fa94a07fSbrendan 
815fa94a07fSbrendan boolean_t
816fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
817fa94a07fSbrendan {
818fa94a07fSbrendan 	boolean_t found;
819fa94a07fSbrendan 
820fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
82189a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
822fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
823fa94a07fSbrendan 
824fa94a07fSbrendan 	return (found);
825fa94a07fSbrendan }
826fa94a07fSbrendan 
827fa94a07fSbrendan void
828fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
829fa94a07fSbrendan {
830fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
831fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
832fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
833fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
834fa94a07fSbrendan }
835fa94a07fSbrendan 
836fa9e4066Sahrens /*
837fa9e4066Sahrens  * ==========================================================================
838fa9e4066Sahrens  * SPA vdev locking
839fa9e4066Sahrens  * ==========================================================================
840fa9e4066Sahrens  */
841fa9e4066Sahrens 
842fa9e4066Sahrens /*
843ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
844ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
845fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
846fa9e4066Sahrens  */
847fa9e4066Sahrens uint64_t
848fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
849fa9e4066Sahrens {
850a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
851bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
85288ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
85388ecc943SGeorge Wilson }
85488ecc943SGeorge Wilson 
85588ecc943SGeorge Wilson /*
85688ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
85788ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
85888ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
85988ecc943SGeorge Wilson  */
86088ecc943SGeorge Wilson uint64_t
86188ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
86288ecc943SGeorge Wilson {
86388ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
8643d7072f8Seschrock 
865e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
866fa9e4066Sahrens 
867fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
868fa9e4066Sahrens }
869fa9e4066Sahrens 
870fa9e4066Sahrens /*
87188ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
87288ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
873fa9e4066Sahrens  */
87488ecc943SGeorge Wilson void
87588ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
876fa9e4066Sahrens {
87788ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
87888ecc943SGeorge Wilson 
8790e34b6a7Sbonwick 	int config_changed = B_FALSE;
880ea8dc4b6Seschrock 
8810373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
8820e34b6a7Sbonwick 
883e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
884e14bb325SJeff Bonwick 
8850e34b6a7Sbonwick 	/*
8860e34b6a7Sbonwick 	 * Reassess the DTLs.
8870e34b6a7Sbonwick 	 */
8880373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
8890e34b6a7Sbonwick 
8900e34b6a7Sbonwick 	/*
8910373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
8920e34b6a7Sbonwick 	 */
893e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
894088f3894Sahrens 		dsl_pool_scrub_restart(spa->spa_dsl_pool);
8950e34b6a7Sbonwick 		config_changed = B_TRUE;
8968f18d1faSGeorge Wilson 		spa->spa_config_generation++;
8970e34b6a7Sbonwick 	}
898ea8dc4b6Seschrock 
89988ecc943SGeorge Wilson 	/*
90088ecc943SGeorge Wilson 	 * Verify the metaslab classes.
90188ecc943SGeorge Wilson 	 */
902b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
903b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
90488ecc943SGeorge Wilson 
905e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
906fa9e4066Sahrens 
90788ecc943SGeorge Wilson 	/*
90888ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
90988ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
91088ecc943SGeorge Wilson 	 * transactionally.
91188ecc943SGeorge Wilson 	 */
91288ecc943SGeorge Wilson 	if (zio_injection_enabled)
9131195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
91488ecc943SGeorge Wilson 
915fa9e4066Sahrens 	/*
916fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
917fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
918fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
919fa9e4066Sahrens 	 */
920fa9e4066Sahrens 	if (error == 0)
921fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
922fa9e4066Sahrens 
923fa9e4066Sahrens 	if (vd != NULL) {
9248ad4d6ddSJeff Bonwick 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
9258ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
926fa9e4066Sahrens 		vdev_free(vd);
9278ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
928fa9e4066Sahrens 	}
929fa9e4066Sahrens 
930fa9e4066Sahrens 	/*
9310e34b6a7Sbonwick 	 * If the config changed, update the config cache.
932fa9e4066Sahrens 	 */
9330e34b6a7Sbonwick 	if (config_changed)
934c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
93588ecc943SGeorge Wilson }
936ea8dc4b6Seschrock 
93788ecc943SGeorge Wilson /*
93888ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
93988ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
94088ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
94188ecc943SGeorge Wilson  * information.
94288ecc943SGeorge Wilson  */
94388ecc943SGeorge Wilson int
94488ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
94588ecc943SGeorge Wilson {
94688ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
947ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
948bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
949fa9e4066Sahrens 
950fa9e4066Sahrens 	return (error);
951fa9e4066Sahrens }
952fa9e4066Sahrens 
953e14bb325SJeff Bonwick /*
954e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
955e14bb325SJeff Bonwick  */
956e14bb325SJeff Bonwick void
9578f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
958e14bb325SJeff Bonwick {
9598f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
9608f18d1faSGeorge Wilson 
961dcba9f3fSGeorge Wilson 	/*
962dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
963dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
964dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
965dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
966dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
967dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
968dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
969dcba9f3fSGeorge Wilson 	 */
970dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
971*9842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
972*9842588bSGeorge Wilson 		int high = locks & ~low;
973*9842588bSGeorge Wilson 
974*9842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
975dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
976*9842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
977dcba9f3fSGeorge Wilson 	} else {
978dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
979dcba9f3fSGeorge Wilson 	}
9808f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
981e14bb325SJeff Bonwick }
982e14bb325SJeff Bonwick 
983e14bb325SJeff Bonwick int
984e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
985e14bb325SJeff Bonwick {
986c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
987c6065d0fSGeorge Wilson 
988b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
989b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
990b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
991b24ab676SJeff Bonwick 
9928f18d1faSGeorge Wilson 	if (vd != NULL) {
993e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
994c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
9958f18d1faSGeorge Wilson 		spa->spa_config_generation++;
9968f18d1faSGeorge Wilson 	}
997e14bb325SJeff Bonwick 
998dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
999dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1000dcba9f3fSGeorge Wilson 
10018f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
10028f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1003e14bb325SJeff Bonwick 
10048ad4d6ddSJeff Bonwick 	/*
10058ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
10068ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
10078ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
10088ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
10098ad4d6ddSJeff Bonwick 	 */
10108ad4d6ddSJeff Bonwick 	if (vd != NULL)
10118ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
10128ad4d6ddSJeff Bonwick 
1013c6065d0fSGeorge Wilson 	/*
1014c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1015c6065d0fSGeorge Wilson 	 */
1016c6065d0fSGeorge Wilson 	if (config_changed) {
1017c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1018c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1019c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1020c6065d0fSGeorge Wilson 	}
1021c6065d0fSGeorge Wilson 
1022e14bb325SJeff Bonwick 	return (error);
1023e14bb325SJeff Bonwick }
1024e14bb325SJeff Bonwick 
1025fa9e4066Sahrens /*
1026fa9e4066Sahrens  * ==========================================================================
1027fa9e4066Sahrens  * Miscellaneous functions
1028fa9e4066Sahrens  * ==========================================================================
1029fa9e4066Sahrens  */
1030fa9e4066Sahrens 
1031fa9e4066Sahrens /*
1032fa9e4066Sahrens  * Rename a spa_t.
1033fa9e4066Sahrens  */
1034fa9e4066Sahrens int
1035fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1036fa9e4066Sahrens {
1037fa9e4066Sahrens 	spa_t *spa;
1038fa9e4066Sahrens 	int err;
1039fa9e4066Sahrens 
1040fa9e4066Sahrens 	/*
1041fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1042fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1043fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1044ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1045fa9e4066Sahrens 	 */
1046fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1047fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1048fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1049fa9e4066Sahrens 		return (err);
1050fa9e4066Sahrens 	}
1051fa9e4066Sahrens 
1052e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1055e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1056fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 	/*
1059fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1060fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1061fa9e4066Sahrens 	 * during the sync.
1062fa9e4066Sahrens 	 */
1063fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1064fa9e4066Sahrens 
1065e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1066fa9e4066Sahrens 
10670373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1068fa9e4066Sahrens 
1069fa9e4066Sahrens 	/*
1070fa9e4066Sahrens 	 * Sync the updated config cache.
1071fa9e4066Sahrens 	 */
1072c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1073fa9e4066Sahrens 
1074fa9e4066Sahrens 	spa_close(spa, FTAG);
1075fa9e4066Sahrens 
1076fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1077fa9e4066Sahrens 
1078fa9e4066Sahrens 	return (0);
1079fa9e4066Sahrens }
1080fa9e4066Sahrens 
1081fa9e4066Sahrens 
1082fa9e4066Sahrens /*
1083fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
1084fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
1085fa9e4066Sahrens  * specified device_guid.
1086fa9e4066Sahrens  */
1087fa9e4066Sahrens boolean_t
1088fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1089fa9e4066Sahrens {
1090fa9e4066Sahrens 	spa_t *spa;
1091fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1092fa9e4066Sahrens 
1093ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1094fa9e4066Sahrens 
1095fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1096fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1097fa9e4066Sahrens 			continue;
1098fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1099fa9e4066Sahrens 			continue;
110039c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
110139c23413Seschrock 			if (device_guid == 0)
110239c23413Seschrock 				break;
110339c23413Seschrock 
110439c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
110539c23413Seschrock 			    device_guid) != NULL)
110639c23413Seschrock 				break;
110739c23413Seschrock 
110839c23413Seschrock 			/*
11098654d025Sperrin 			 * Check any devices we may be in the process of adding.
111039c23413Seschrock 			 */
111139c23413Seschrock 			if (spa->spa_pending_vdev) {
111239c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
111339c23413Seschrock 				    device_guid) != NULL)
111439c23413Seschrock 					break;
111539c23413Seschrock 			}
111639c23413Seschrock 		}
1117fa9e4066Sahrens 	}
1118fa9e4066Sahrens 
1119fa9e4066Sahrens 	return (spa != NULL);
1120fa9e4066Sahrens }
1121fa9e4066Sahrens 
1122fa9e4066Sahrens char *
1123fa9e4066Sahrens spa_strdup(const char *s)
1124fa9e4066Sahrens {
1125fa9e4066Sahrens 	size_t len;
1126fa9e4066Sahrens 	char *new;
1127fa9e4066Sahrens 
1128fa9e4066Sahrens 	len = strlen(s);
1129fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1130fa9e4066Sahrens 	bcopy(s, new, len);
1131fa9e4066Sahrens 	new[len] = '\0';
1132fa9e4066Sahrens 
1133fa9e4066Sahrens 	return (new);
1134fa9e4066Sahrens }
1135fa9e4066Sahrens 
1136fa9e4066Sahrens void
1137fa9e4066Sahrens spa_strfree(char *s)
1138fa9e4066Sahrens {
1139fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1140fa9e4066Sahrens }
1141fa9e4066Sahrens 
1142fa9e4066Sahrens uint64_t
1143fa9e4066Sahrens spa_get_random(uint64_t range)
1144fa9e4066Sahrens {
1145fa9e4066Sahrens 	uint64_t r;
1146fa9e4066Sahrens 
1147fa9e4066Sahrens 	ASSERT(range != 0);
1148fa9e4066Sahrens 
1149fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1150fa9e4066Sahrens 
1151fa9e4066Sahrens 	return (r % range);
1152fa9e4066Sahrens }
1153fa9e4066Sahrens 
11541195e687SMark J Musante uint64_t
11551195e687SMark J Musante spa_generate_guid(spa_t *spa)
11561195e687SMark J Musante {
11571195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
11581195e687SMark J Musante 
11591195e687SMark J Musante 	if (spa != NULL) {
11601195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
11611195e687SMark J Musante 			guid = spa_get_random(-1ULL);
11621195e687SMark J Musante 	} else {
11631195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
11641195e687SMark J Musante 			guid = spa_get_random(-1ULL);
11651195e687SMark J Musante 	}
11661195e687SMark J Musante 
11671195e687SMark J Musante 	return (guid);
11681195e687SMark J Musante }
11691195e687SMark J Musante 
1170fa9e4066Sahrens void
1171b24ab676SJeff Bonwick sprintf_blkptr(char *buf, const blkptr_t *bp)
1172fa9e4066Sahrens {
1173f0ba89beSJeff Bonwick 	char *type = NULL;
1174f0ba89beSJeff Bonwick 	char *checksum = NULL;
1175f0ba89beSJeff Bonwick 	char *compress = NULL;
1176f0ba89beSJeff Bonwick 
1177f0ba89beSJeff Bonwick 	if (bp != NULL) {
1178f0ba89beSJeff Bonwick 		type = dmu_ot[BP_GET_TYPE(bp)].ot_name;
1179f0ba89beSJeff Bonwick 		checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1180f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1181f0ba89beSJeff Bonwick 	}
1182fa9e4066Sahrens 
1183b24ab676SJeff Bonwick 	SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1184fa9e4066Sahrens }
1185fa9e4066Sahrens 
1186fa9e4066Sahrens void
1187fa9e4066Sahrens spa_freeze(spa_t *spa)
1188fa9e4066Sahrens {
1189fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1190fa9e4066Sahrens 
1191e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1192fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1193fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1194fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1195fa9e4066Sahrens 	}
1196e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1197fa9e4066Sahrens 	if (freeze_txg != 0)
1198fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1199fa9e4066Sahrens }
1200fa9e4066Sahrens 
12010125049cSahrens void
12020125049cSahrens zfs_panic_recover(const char *fmt, ...)
12030125049cSahrens {
12040125049cSahrens 	va_list adx;
12050125049cSahrens 
12060125049cSahrens 	va_start(adx, fmt);
12070125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
12080125049cSahrens 	va_end(adx);
12090125049cSahrens }
12100125049cSahrens 
1211fa9e4066Sahrens /*
1212fa9e4066Sahrens  * ==========================================================================
1213fa9e4066Sahrens  * Accessor functions
1214fa9e4066Sahrens  * ==========================================================================
1215fa9e4066Sahrens  */
1216fa9e4066Sahrens 
1217088f3894Sahrens boolean_t
121888b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1219fa9e4066Sahrens {
122088b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1221fa9e4066Sahrens }
1222fa9e4066Sahrens 
1223fa9e4066Sahrens dsl_pool_t *
1224fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1225fa9e4066Sahrens {
1226fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1227fa9e4066Sahrens }
1228fa9e4066Sahrens 
1229fa9e4066Sahrens blkptr_t *
1230fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1231fa9e4066Sahrens {
1232fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1233fa9e4066Sahrens }
1234fa9e4066Sahrens 
1235fa9e4066Sahrens void
1236fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1237fa9e4066Sahrens {
1238fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1239fa9e4066Sahrens }
1240fa9e4066Sahrens 
1241fa9e4066Sahrens void
1242fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1243fa9e4066Sahrens {
1244fa9e4066Sahrens 	if (spa->spa_root == NULL)
1245fa9e4066Sahrens 		buf[0] = '\0';
1246fa9e4066Sahrens 	else
1247fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1248fa9e4066Sahrens }
1249fa9e4066Sahrens 
1250fa9e4066Sahrens int
1251fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1252fa9e4066Sahrens {
1253fa9e4066Sahrens 	return (spa->spa_sync_pass);
1254fa9e4066Sahrens }
1255fa9e4066Sahrens 
1256fa9e4066Sahrens char *
1257fa9e4066Sahrens spa_name(spa_t *spa)
1258fa9e4066Sahrens {
1259fa9e4066Sahrens 	return (spa->spa_name);
1260fa9e4066Sahrens }
1261fa9e4066Sahrens 
1262fa9e4066Sahrens uint64_t
1263fa9e4066Sahrens spa_guid(spa_t *spa)
1264fa9e4066Sahrens {
1265b5989ec7Seschrock 	/*
1266b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1267b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1268b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1269b5989ec7Seschrock 	 * this case.
1270b5989ec7Seschrock 	 */
1271b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
1272b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1273b5989ec7Seschrock 	else
1274b5989ec7Seschrock 		return (spa->spa_load_guid);
1275fa9e4066Sahrens }
1276fa9e4066Sahrens 
1277fa9e4066Sahrens uint64_t
1278fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1279fa9e4066Sahrens {
1280fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1281fa9e4066Sahrens }
1282fa9e4066Sahrens 
1283fa9e4066Sahrens uint64_t
1284fa9e4066Sahrens spa_first_txg(spa_t *spa)
1285fa9e4066Sahrens {
1286fa9e4066Sahrens 	return (spa->spa_first_txg);
1287fa9e4066Sahrens }
1288fa9e4066Sahrens 
1289b24ab676SJeff Bonwick uint64_t
1290b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1291b24ab676SJeff Bonwick {
1292b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1293b24ab676SJeff Bonwick }
1294b24ab676SJeff Bonwick 
129588b7b0f2SMatthew Ahrens pool_state_t
1296fa9e4066Sahrens spa_state(spa_t *spa)
1297fa9e4066Sahrens {
1298fa9e4066Sahrens 	return (spa->spa_state);
1299fa9e4066Sahrens }
1300fa9e4066Sahrens 
1301b16da2e2SGeorge Wilson spa_load_state_t
1302b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1303b16da2e2SGeorge Wilson {
1304b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1305b16da2e2SGeorge Wilson }
1306b16da2e2SGeorge Wilson 
1307fa9e4066Sahrens uint64_t
1308fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1309fa9e4066Sahrens {
1310fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1311fa9e4066Sahrens }
1312fa9e4066Sahrens 
1313fa9e4066Sahrens /* ARGSUSED */
1314fa9e4066Sahrens uint64_t
1315fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1316fa9e4066Sahrens {
1317fa9e4066Sahrens 	/*
1318b24ab676SJeff Bonwick 	 * The worst case is single-sector max-parity RAID-Z blocks, in which
1319b24ab676SJeff Bonwick 	 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1320b24ab676SJeff Bonwick 	 * times the size; so just assume that.  Add to this the fact that
1321b24ab676SJeff Bonwick 	 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1322b24ab676SJeff Bonwick 	 * the block may be dittoed with up to 3 DVAs by ddt_sync().
132344cd46caSbillm 	 */
1324b24ab676SJeff Bonwick 	return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
132544cd46caSbillm }
132644cd46caSbillm 
1327485bbbf5SGeorge Wilson uint64_t
1328485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1329485bbbf5SGeorge Wilson {
1330485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1331485bbbf5SGeorge Wilson }
1332485bbbf5SGeorge Wilson 
1333485bbbf5SGeorge Wilson void
1334485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1335485bbbf5SGeorge Wilson {
1336485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1337485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1338485bbbf5SGeorge Wilson }
1339485bbbf5SGeorge Wilson 
13400a4e9518Sgw /*
13410a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
13420a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
13430a4e9518Sgw  */
13440a4e9518Sgw uint8_t
13450a4e9518Sgw spa_get_failmode(spa_t *spa)
13460a4e9518Sgw {
13470a4e9518Sgw 	return (spa->spa_failmode);
13480a4e9518Sgw }
13490a4e9518Sgw 
1350e14bb325SJeff Bonwick boolean_t
1351e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1352e14bb325SJeff Bonwick {
1353e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1354e14bb325SJeff Bonwick }
1355e14bb325SJeff Bonwick 
135644cd46caSbillm uint64_t
135744cd46caSbillm spa_version(spa_t *spa)
135844cd46caSbillm {
135944cd46caSbillm 	return (spa->spa_ubsync.ub_version);
136044cd46caSbillm }
136144cd46caSbillm 
1362b24ab676SJeff Bonwick boolean_t
1363b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1364b24ab676SJeff Bonwick {
1365b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1366b24ab676SJeff Bonwick }
1367b24ab676SJeff Bonwick 
1368b24ab676SJeff Bonwick metaslab_class_t *
1369b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1370b24ab676SJeff Bonwick {
1371b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1372b24ab676SJeff Bonwick }
1373b24ab676SJeff Bonwick 
1374b24ab676SJeff Bonwick metaslab_class_t *
1375b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1376b24ab676SJeff Bonwick {
1377b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1378b24ab676SJeff Bonwick }
1379b24ab676SJeff Bonwick 
138044cd46caSbillm int
138144cd46caSbillm spa_max_replication(spa_t *spa)
138244cd46caSbillm {
138344cd46caSbillm 	/*
1384e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
138544cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
138644cd46caSbillm 	 * replication level accordingly.
1387fa9e4066Sahrens 	 */
1388e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
138944cd46caSbillm 		return (1);
139044cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1391fa9e4066Sahrens }
1392fa9e4066Sahrens 
139399653d4eSeschrock uint64_t
1394b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
139599653d4eSeschrock {
1396b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1397b24ab676SJeff Bonwick 	uint64_t dsize = asize;
139899653d4eSeschrock 
1399b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
140099653d4eSeschrock 
1401b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1402b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1403b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
140499653d4eSeschrock 	}
1405b24ab676SJeff Bonwick 
1406b24ab676SJeff Bonwick 	return (dsize);
1407b24ab676SJeff Bonwick }
1408b24ab676SJeff Bonwick 
1409b24ab676SJeff Bonwick uint64_t
1410b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1411b24ab676SJeff Bonwick {
1412b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1413b24ab676SJeff Bonwick 
1414b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1415b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1416b24ab676SJeff Bonwick 
1417b24ab676SJeff Bonwick 	return (dsize);
1418b24ab676SJeff Bonwick }
1419b24ab676SJeff Bonwick 
1420b24ab676SJeff Bonwick uint64_t
1421b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1422b24ab676SJeff Bonwick {
1423b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1424b24ab676SJeff Bonwick 
1425b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1426b24ab676SJeff Bonwick 
1427b24ab676SJeff Bonwick 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1428b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1429b24ab676SJeff Bonwick 
1430e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1431b24ab676SJeff Bonwick 
1432b24ab676SJeff Bonwick 	return (dsize);
143399653d4eSeschrock }
143499653d4eSeschrock 
1435fa9e4066Sahrens /*
1436fa9e4066Sahrens  * ==========================================================================
1437fa9e4066Sahrens  * Initialization and Termination
1438fa9e4066Sahrens  * ==========================================================================
1439fa9e4066Sahrens  */
1440fa9e4066Sahrens 
1441fa9e4066Sahrens static int
1442fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1443fa9e4066Sahrens {
1444fa9e4066Sahrens 	const spa_t *s1 = a1;
1445fa9e4066Sahrens 	const spa_t *s2 = a2;
1446fa9e4066Sahrens 	int s;
1447fa9e4066Sahrens 
1448fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1449fa9e4066Sahrens 	if (s > 0)
1450fa9e4066Sahrens 		return (1);
1451fa9e4066Sahrens 	if (s < 0)
1452fa9e4066Sahrens 		return (-1);
1453fa9e4066Sahrens 	return (0);
1454fa9e4066Sahrens }
1455fa9e4066Sahrens 
14560373e76bSbonwick int
14570373e76bSbonwick spa_busy(void)
14580373e76bSbonwick {
14590373e76bSbonwick 	return (spa_active_count);
14600373e76bSbonwick }
14610373e76bSbonwick 
1462e7cbe64fSgw void
1463e7cbe64fSgw spa_boot_init()
1464e7cbe64fSgw {
1465e7cbe64fSgw 	spa_config_load();
1466e7cbe64fSgw }
1467e7cbe64fSgw 
1468fa9e4066Sahrens void
1469fa9e4066Sahrens spa_init(int mode)
1470fa9e4066Sahrens {
1471fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1472c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1473fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1474fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1477fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1478fa9e4066Sahrens 
1479fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1480fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1481fa94a07fSbrendan 
1482fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1483fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
148499653d4eSeschrock 
14858ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1486fa9e4066Sahrens 
1487fa9e4066Sahrens 	refcount_init();
1488fa9e4066Sahrens 	unique_init();
1489fa9e4066Sahrens 	zio_init();
1490fa9e4066Sahrens 	dmu_init();
1491fa9e4066Sahrens 	zil_init();
149287db74c1Sek 	vdev_cache_stat_init();
149391ebeef5Sahrens 	zfs_prop_init();
1494990b4856Slling 	zpool_prop_init();
1495fa9e4066Sahrens 	spa_config_load();
1496e14bb325SJeff Bonwick 	l2arc_start();
1497fa9e4066Sahrens }
1498fa9e4066Sahrens 
1499fa9e4066Sahrens void
1500fa9e4066Sahrens spa_fini(void)
1501fa9e4066Sahrens {
1502e14bb325SJeff Bonwick 	l2arc_stop();
1503e14bb325SJeff Bonwick 
1504fa9e4066Sahrens 	spa_evict_all();
1505fa9e4066Sahrens 
150687db74c1Sek 	vdev_cache_stat_fini();
1507fa9e4066Sahrens 	zil_fini();
1508fa9e4066Sahrens 	dmu_fini();
1509fa9e4066Sahrens 	zio_fini();
151091ebeef5Sahrens 	unique_fini();
1511fa9e4066Sahrens 	refcount_fini();
1512fa9e4066Sahrens 
1513fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
151499653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1515fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1516fa9e4066Sahrens 
1517fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1518fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1519c25056deSgw 	mutex_destroy(&spa_spare_lock);
1520fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1521fa9e4066Sahrens }
15226ce0521aSperrin 
15236ce0521aSperrin /*
15246ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
15256ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
15266ce0521aSperrin  * performance and not correctness
15276ce0521aSperrin  */
15286ce0521aSperrin boolean_t
15296ce0521aSperrin spa_has_slogs(spa_t *spa)
15306ce0521aSperrin {
15316ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
15326ce0521aSperrin }
1533bf82a41bSeschrock 
1534b24ab676SJeff Bonwick spa_log_state_t
1535b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1536b24ab676SJeff Bonwick {
1537b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1538b24ab676SJeff Bonwick }
1539b24ab676SJeff Bonwick 
1540b24ab676SJeff Bonwick void
1541b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1542b24ab676SJeff Bonwick {
1543b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1544b24ab676SJeff Bonwick }
1545b24ab676SJeff Bonwick 
1546bf82a41bSeschrock boolean_t
1547bf82a41bSeschrock spa_is_root(spa_t *spa)
1548bf82a41bSeschrock {
1549bf82a41bSeschrock 	return (spa->spa_is_root);
1550bf82a41bSeschrock }
15518ad4d6ddSJeff Bonwick 
15528ad4d6ddSJeff Bonwick boolean_t
15538ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
15548ad4d6ddSJeff Bonwick {
15558ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
15568ad4d6ddSJeff Bonwick }
15578ad4d6ddSJeff Bonwick 
15588ad4d6ddSJeff Bonwick int
15598ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
15608ad4d6ddSJeff Bonwick {
15618ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
15628ad4d6ddSJeff Bonwick }
1563b24ab676SJeff Bonwick 
1564b24ab676SJeff Bonwick uint64_t
1565b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1566b24ab676SJeff Bonwick {
1567b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1568b24ab676SJeff Bonwick }
1569b24ab676SJeff Bonwick 
1570b24ab676SJeff Bonwick uint64_t
1571b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1572b24ab676SJeff Bonwick {
1573b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1574b24ab676SJeff Bonwick }
1575b24ab676SJeff Bonwick 
1576b24ab676SJeff Bonwick objset_t *
1577b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1578b24ab676SJeff Bonwick {
1579b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1580b24ab676SJeff Bonwick }
1581b24ab676SJeff Bonwick 
1582b24ab676SJeff Bonwick enum zio_checksum
1583b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1584b24ab676SJeff Bonwick {
1585b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1586b24ab676SJeff Bonwick }
1587