xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 8ad4d6dd86f5bc65fb3afa566c8133f3bac21648)
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 /*
2287db74c1Sek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/zfs_context.h>
27fa9e4066Sahrens #include <sys/spa_impl.h>
28fa9e4066Sahrens #include <sys/zio.h>
29fa9e4066Sahrens #include <sys/zio_checksum.h>
30fa9e4066Sahrens #include <sys/zio_compress.h>
31fa9e4066Sahrens #include <sys/dmu.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/zap.h>
34fa9e4066Sahrens #include <sys/zil.h>
35fa9e4066Sahrens #include <sys/vdev_impl.h>
36fa9e4066Sahrens #include <sys/metaslab.h>
37fa9e4066Sahrens #include <sys/uberblock_impl.h>
38fa9e4066Sahrens #include <sys/txg.h>
39fa9e4066Sahrens #include <sys/avl.h>
40fa9e4066Sahrens #include <sys/unique.h>
41fa9e4066Sahrens #include <sys/dsl_pool.h>
42fa9e4066Sahrens #include <sys/dsl_dir.h>
43fa9e4066Sahrens #include <sys/dsl_prop.h>
44fa9e4066Sahrens #include <sys/fs/zfs.h>
456ce0521aSperrin #include <sys/metaslab_impl.h>
46e14bb325SJeff Bonwick #include <sys/sunddi.h>
47e14bb325SJeff Bonwick #include <sys/arc.h>
4891ebeef5Sahrens #include "zfs_prop.h"
49fa9e4066Sahrens 
50fa9e4066Sahrens /*
51fa9e4066Sahrens  * SPA locking
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
54fa9e4066Sahrens  *
55fa9e4066Sahrens  * spa_namespace_lock (global mutex)
56fa9e4066Sahrens  *
5744cd46caSbillm  *	This lock must be acquired to do any of the following:
58fa9e4066Sahrens  *
5944cd46caSbillm  *		- Lookup a spa_t by name
6044cd46caSbillm  *		- Add or remove a spa_t from the namespace
6144cd46caSbillm  *		- Increase spa_refcount from non-zero
6244cd46caSbillm  *		- Check if spa_refcount is zero
6344cd46caSbillm  *		- Rename a spa_t
64ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
6544cd46caSbillm  *		- Held for the duration of create/destroy/import/export
66fa9e4066Sahrens  *
6744cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
6844cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
6944cd46caSbillm  *	definition they must have an existing reference, and will never need
7044cd46caSbillm  *	to lookup a spa_t by name.
71fa9e4066Sahrens  *
72fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
73fa9e4066Sahrens  *
7444cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
7544cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
7644cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
77088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
7844cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
79fa9e4066Sahrens  *
80e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
81fa9e4066Sahrens  *
8291ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
8391ebeef5Sahrens  *	the following circumstances:
84fa9e4066Sahrens  *
8544cd46caSbillm  *		- RW_READER to perform I/O to the spa
8644cd46caSbillm  *		- RW_WRITER to change the vdev config
87fa9e4066Sahrens  *
88fa9e4066Sahrens  * The locking order is fairly straightforward:
89fa9e4066Sahrens  *
9044cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
91fa9e4066Sahrens  *
9244cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
9344cd46caSbillm  *	or to check if it is zero.
94fa9e4066Sahrens  *
95e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
96fa9e4066Sahrens  *
9744cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
9844cd46caSbillm  *	the config lock.
99fa9e4066Sahrens  *
100e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
101fa9e4066Sahrens  *
10244cd46caSbillm  *	The namespace lock must always be taken before the config lock.
103fa9e4066Sahrens  *
104fa9e4066Sahrens  *
105e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
106fa9e4066Sahrens  *
107e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
108e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
109fa9e4066Sahrens  *
11044cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
111fa9e4066Sahrens  *
11244cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
113fa9e4066Sahrens  *
11444cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
11544cd46caSbillm  *				frees up any memory associated with the spa_t.
116fa9e4066Sahrens  *
11744cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
11844cd46caSbillm  *				first if NULL is passed.
119fa9e4066Sahrens  *
12044cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12144cd46caSbillm  *				the system.
122fa9e4066Sahrens  *
123ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
124fa9e4066Sahrens  *
125fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
126fa9e4066Sahrens  *
12744cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
12844cd46caSbillm  *				called with spa_namespace_lock held if the
12944cd46caSbillm  *				refcount is currently zero.
130fa9e4066Sahrens  *
13144cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13244cd46caSbillm  *				not free the spa_t or remove it from the
13344cd46caSbillm  *				namespace.  No locking is required.
134fa9e4066Sahrens  *
13544cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
13644cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
13744cd46caSbillm  *				held.
138fa9e4066Sahrens  *
139e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
140e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
141e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
142e14bb325SJeff Bonwick  *
143e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
144e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
145e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
146e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
147e14bb325SJeff Bonwick  *
148e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
149e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
150e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
151e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
152e14bb325SJeff Bonwick  *
153e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
154e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
155e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
156e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
157e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
158e14bb325SJeff Bonwick  *
159e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
160e14bb325SJeff Bonwick  *
161e14bb325SJeff Bonwick  * SCL_CONFIG
162e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
163e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
164e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
165e14bb325SJeff Bonwick  *
166e14bb325SJeff Bonwick  * SCL_STATE
167e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
168e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
169e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
170e14bb325SJeff Bonwick  *
171e14bb325SJeff Bonwick  * SCL_ALLOC
172e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
173e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
174e14bb325SJeff Bonwick  *
175e14bb325SJeff Bonwick  * SCL_ZIO
176e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
177e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
178e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
179e14bb325SJeff Bonwick  *
180e14bb325SJeff Bonwick  * SCL_FREE
181e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
182e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
183e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
184e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
185e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
186e14bb325SJeff Bonwick  *
187e14bb325SJeff Bonwick  * SCL_VDEV
188e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
189e14bb325SJeff Bonwick  *	inquiries such as bp_get_dasize().  SCL_VDEV is distinct from the
190e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
191e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
192e14bb325SJeff Bonwick  *
193e14bb325SJeff Bonwick  * In addition, the following rules apply:
194e14bb325SJeff Bonwick  *
195e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
196e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
197e14bb325SJeff Bonwick  *
198e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
199e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
200e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
201e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
202e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
203fa9e4066Sahrens  *
204ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
205fa9e4066Sahrens  *
20644cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
207ea8dc4b6Seschrock  *				for writing.
208fa9e4066Sahrens  *
20944cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21044cd46caSbillm  *				to complete, sync the updated configs to the
211ea8dc4b6Seschrock  *				cache, and release the namespace lock.
212fa9e4066Sahrens  *
213e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
214e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
215e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
216e14bb325SJeff Bonwick  *
217e14bb325SJeff Bonwick  * spa_rename() is also implemented within this file since is requires
218e14bb325SJeff Bonwick  * manipulation of the namespace.
219fa9e4066Sahrens  */
220fa9e4066Sahrens 
221fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
222fa9e4066Sahrens kmutex_t spa_namespace_lock;
223fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2240373e76bSbonwick static int spa_active_count;
225416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
226fa9e4066Sahrens 
22799653d4eSeschrock static kmutex_t spa_spare_lock;
22839c23413Seschrock static avl_tree_t spa_spare_avl;
229fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
230fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23199653d4eSeschrock 
232fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
233*8ad4d6ddSJeff Bonwick int spa_mode_global;
234fa9e4066Sahrens 
235fa9e4066Sahrens #ifdef ZFS_DEBUG
23640feaa91Sahrens /* Everything except dprintf is on by default in debug builds */
23740feaa91Sahrens int zfs_flags = ~ZFS_DEBUG_DPRINTF;
238fa9e4066Sahrens #else
239fa9e4066Sahrens int zfs_flags = 0;
240fa9e4066Sahrens #endif
241fa9e4066Sahrens 
2420125049cSahrens /*
2430125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2440125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2450125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2460125049cSahrens  */
2470125049cSahrens int zfs_recover = 0;
2480125049cSahrens 
249fa9e4066Sahrens 
250e05725b1Sbonwick /*
251e05725b1Sbonwick  * ==========================================================================
252e05725b1Sbonwick  * SPA config locking
253e05725b1Sbonwick  * ==========================================================================
254e05725b1Sbonwick  */
255e05725b1Sbonwick static void
256e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
257e14bb325SJeff Bonwick {
258e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
259e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
260e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
261e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
262e14bb325SJeff Bonwick 		refcount_create(&scl->scl_count);
263e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
264e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
265e14bb325SJeff Bonwick 	}
266e05725b1Sbonwick }
267e05725b1Sbonwick 
268e05725b1Sbonwick static void
269e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
270e14bb325SJeff Bonwick {
271e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
272e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
273e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
274e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
275e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
276e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
277e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
278e14bb325SJeff Bonwick 	}
279e14bb325SJeff Bonwick }
280e14bb325SJeff Bonwick 
281e14bb325SJeff Bonwick int
282e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
283e05725b1Sbonwick {
284e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
285e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
286e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
287e14bb325SJeff Bonwick 			continue;
288e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
289e14bb325SJeff Bonwick 		if (rw == RW_READER) {
290e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
291e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
292e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
293e14bb325SJeff Bonwick 				return (0);
294e14bb325SJeff Bonwick 			}
295e14bb325SJeff Bonwick 		} else {
296e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
297e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
298e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
299e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
300e14bb325SJeff Bonwick 				return (0);
301e14bb325SJeff Bonwick 			}
302e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
303e14bb325SJeff Bonwick 		}
304e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
305e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
306e14bb325SJeff Bonwick 	}
307e14bb325SJeff Bonwick 	return (1);
308e05725b1Sbonwick }
309e05725b1Sbonwick 
310e05725b1Sbonwick void
311e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
312e05725b1Sbonwick {
313e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
314e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
315e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
316e14bb325SJeff Bonwick 			continue;
317e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
318e14bb325SJeff Bonwick 		if (rw == RW_READER) {
319e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
320e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
321e14bb325SJeff Bonwick 			}
322e14bb325SJeff Bonwick 		} else {
323e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
324e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
325e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
326e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
327e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
328e14bb325SJeff Bonwick 			}
329e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
330e14bb325SJeff Bonwick 		}
331e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
332e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
333e05725b1Sbonwick 	}
334e05725b1Sbonwick }
335e05725b1Sbonwick 
336e05725b1Sbonwick void
337e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
338e05725b1Sbonwick {
339e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
340e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
341e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
342e14bb325SJeff Bonwick 			continue;
343e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
344e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
345e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
346e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
347e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
348e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
349e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
350e14bb325SJeff Bonwick 		}
351e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
352e05725b1Sbonwick 	}
353e05725b1Sbonwick }
354e05725b1Sbonwick 
355e14bb325SJeff Bonwick int
356e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
357e05725b1Sbonwick {
358e14bb325SJeff Bonwick 	int locks_held = 0;
359e05725b1Sbonwick 
360e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
361e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
362e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
363e14bb325SJeff Bonwick 			continue;
364e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
365e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
366e14bb325SJeff Bonwick 			locks_held |= 1 << i;
367e14bb325SJeff Bonwick 	}
368e14bb325SJeff Bonwick 
369e14bb325SJeff Bonwick 	return (locks_held);
370e05725b1Sbonwick }
371e05725b1Sbonwick 
372fa9e4066Sahrens /*
373fa9e4066Sahrens  * ==========================================================================
374fa9e4066Sahrens  * SPA namespace functions
375fa9e4066Sahrens  * ==========================================================================
376fa9e4066Sahrens  */
377fa9e4066Sahrens 
378fa9e4066Sahrens /*
379fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
380fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
381fa9e4066Sahrens  */
382fa9e4066Sahrens spa_t *
383fa9e4066Sahrens spa_lookup(const char *name)
384fa9e4066Sahrens {
385e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
386e14bb325SJeff Bonwick 	spa_t *spa;
387fa9e4066Sahrens 	avl_index_t where;
38840feaa91Sahrens 	char c;
38940feaa91Sahrens 	char *cp;
390fa9e4066Sahrens 
391fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
392fa9e4066Sahrens 
39340feaa91Sahrens 	/*
39440feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
39540feaa91Sahrens 	 * just use that.
39640feaa91Sahrens 	 */
39740feaa91Sahrens 	cp = strpbrk(name, "/@");
39840feaa91Sahrens 	if (cp) {
39940feaa91Sahrens 		c = *cp;
40040feaa91Sahrens 		*cp = '\0';
40140feaa91Sahrens 	}
40240feaa91Sahrens 
403e14bb325SJeff Bonwick 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
404fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
405fa9e4066Sahrens 
40640feaa91Sahrens 	if (cp)
40740feaa91Sahrens 		*cp = c;
40840feaa91Sahrens 
409fa9e4066Sahrens 	return (spa);
410fa9e4066Sahrens }
411fa9e4066Sahrens 
412fa9e4066Sahrens /*
413fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
414fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
415fa9e4066Sahrens  * exist by calling spa_lookup() first.
416fa9e4066Sahrens  */
417fa9e4066Sahrens spa_t *
4180373e76bSbonwick spa_add(const char *name, const char *altroot)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	spa_t *spa;
421c5904d13Seschrock 	spa_config_dirent_t *dp;
422fa9e4066Sahrens 
423fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
424fa9e4066Sahrens 
425fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
426fa9e4066Sahrens 
427c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
428e14bb325SJeff Bonwick 	mutex_init(&spa->spa_async_root_lock, NULL, MUTEX_DEFAULT, NULL);
429c25056deSgw 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
430c25056deSgw 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
431c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
432c25056deSgw 	mutex_init(&spa->spa_sync_bplist.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
433c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
434c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
435c25056deSgw 
436c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
437e14bb325SJeff Bonwick 	cv_init(&spa->spa_async_root_cv, NULL, CV_DEFAULT, NULL);
438c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
439e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
440c25056deSgw 
441e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
442fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
443fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
4440373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
445fa9e4066Sahrens 
446fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
447e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
448fa9e4066Sahrens 
449fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
450fa9e4066Sahrens 
451e14bb325SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
4520a4e9518Sgw 
4530373e76bSbonwick 	/*
4540373e76bSbonwick 	 * Set the alternate root, if there is one.
4550373e76bSbonwick 	 */
4560373e76bSbonwick 	if (altroot) {
4570373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
4580373e76bSbonwick 		spa_active_count++;
4590373e76bSbonwick 	}
4600373e76bSbonwick 
461c5904d13Seschrock 	/*
462c5904d13Seschrock 	 * Every pool starts with the default cachefile
463c5904d13Seschrock 	 */
464c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
465c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
466c5904d13Seschrock 
467c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
468c5904d13Seschrock 	dp->scd_path = spa_strdup(spa_config_path);
469c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
470c5904d13Seschrock 
471fa9e4066Sahrens 	return (spa);
472fa9e4066Sahrens }
473fa9e4066Sahrens 
474fa9e4066Sahrens /*
475fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
476fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
477fa9e4066Sahrens  * deactivated.
478fa9e4066Sahrens  */
479fa9e4066Sahrens void
480fa9e4066Sahrens spa_remove(spa_t *spa)
481fa9e4066Sahrens {
482c5904d13Seschrock 	spa_config_dirent_t *dp;
483c5904d13Seschrock 
484fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
485fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
486fa9e4066Sahrens 
487fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
488fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
489fa9e4066Sahrens 
4900373e76bSbonwick 	if (spa->spa_root) {
491fa9e4066Sahrens 		spa_strfree(spa->spa_root);
4920373e76bSbonwick 		spa_active_count--;
4930373e76bSbonwick 	}
494fa9e4066Sahrens 
495c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
496c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
497c5904d13Seschrock 		if (dp->scd_path != NULL)
498c5904d13Seschrock 			spa_strfree(dp->scd_path);
499c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
500c5904d13Seschrock 	}
501c5904d13Seschrock 
502c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
5032f8aaab3Seschrock 
504fa9e4066Sahrens 	spa_config_set(spa, NULL);
505fa9e4066Sahrens 
506fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
50791ebeef5Sahrens 
508e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
509fa9e4066Sahrens 
510c25056deSgw 	cv_destroy(&spa->spa_async_cv);
511e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_async_root_cv);
512c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
513e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
514c25056deSgw 
5155ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
516e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_async_root_lock);
517c25056deSgw 	mutex_destroy(&spa->spa_scrub_lock);
518c25056deSgw 	mutex_destroy(&spa->spa_errlog_lock);
519c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
520c25056deSgw 	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
52106eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
522b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
523e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
5245ad82045Snd 
525fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
526fa9e4066Sahrens }
527fa9e4066Sahrens 
528fa9e4066Sahrens /*
529fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
530fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
531fa9e4066Sahrens  */
532fa9e4066Sahrens spa_t *
533fa9e4066Sahrens spa_next(spa_t *prev)
534fa9e4066Sahrens {
535fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
536fa9e4066Sahrens 
537fa9e4066Sahrens 	if (prev)
538fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
539fa9e4066Sahrens 	else
540fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
541fa9e4066Sahrens }
542fa9e4066Sahrens 
543fa9e4066Sahrens /*
544fa9e4066Sahrens  * ==========================================================================
545fa9e4066Sahrens  * SPA refcount functions
546fa9e4066Sahrens  * ==========================================================================
547fa9e4066Sahrens  */
548fa9e4066Sahrens 
549fa9e4066Sahrens /*
550fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
551fa9e4066Sahrens  * have the namespace lock held.
552fa9e4066Sahrens  */
553fa9e4066Sahrens void
554fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
555fa9e4066Sahrens {
556088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
557fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
558fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
559fa9e4066Sahrens }
560fa9e4066Sahrens 
561fa9e4066Sahrens /*
562fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
563fa9e4066Sahrens  * have the namespace lock held.
564fa9e4066Sahrens  */
565fa9e4066Sahrens void
566fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
567fa9e4066Sahrens {
568088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
569fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
570fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
571fa9e4066Sahrens }
572fa9e4066Sahrens 
573fa9e4066Sahrens /*
574fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
575088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
576fa9e4066Sahrens  * number of references acquired when opening a pool
577fa9e4066Sahrens  */
578fa9e4066Sahrens boolean_t
579fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
580fa9e4066Sahrens {
581fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
582fa9e4066Sahrens 
583088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
584fa9e4066Sahrens }
585fa9e4066Sahrens 
58699653d4eSeschrock /*
58799653d4eSeschrock  * ==========================================================================
588fa94a07fSbrendan  * SPA spare and l2cache tracking
58999653d4eSeschrock  * ==========================================================================
59099653d4eSeschrock  */
59199653d4eSeschrock 
592fa94a07fSbrendan /*
593fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
594fa94a07fSbrendan  * for 'auxiliary' devices.
595fa94a07fSbrendan  */
596fa94a07fSbrendan 
597fa94a07fSbrendan typedef struct spa_aux {
598fa94a07fSbrendan 	uint64_t	aux_guid;
599fa94a07fSbrendan 	uint64_t	aux_pool;
600fa94a07fSbrendan 	avl_node_t	aux_avl;
601fa94a07fSbrendan 	int		aux_count;
602fa94a07fSbrendan } spa_aux_t;
603fa94a07fSbrendan 
604fa94a07fSbrendan static int
605fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
606fa94a07fSbrendan {
607fa94a07fSbrendan 	const spa_aux_t *sa = a;
608fa94a07fSbrendan 	const spa_aux_t *sb = b;
609fa94a07fSbrendan 
610fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
611fa94a07fSbrendan 		return (-1);
612fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
613fa94a07fSbrendan 		return (1);
614fa94a07fSbrendan 	else
615fa94a07fSbrendan 		return (0);
616fa94a07fSbrendan }
617fa94a07fSbrendan 
618fa94a07fSbrendan void
619fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
620fa94a07fSbrendan {
621fa94a07fSbrendan 	avl_index_t where;
622fa94a07fSbrendan 	spa_aux_t search;
623fa94a07fSbrendan 	spa_aux_t *aux;
624fa94a07fSbrendan 
625fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
626fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
627fa94a07fSbrendan 		aux->aux_count++;
628fa94a07fSbrendan 	} else {
629fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
630fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
631fa94a07fSbrendan 		aux->aux_count = 1;
632fa94a07fSbrendan 		avl_insert(avl, aux, where);
633fa94a07fSbrendan 	}
634fa94a07fSbrendan }
635fa94a07fSbrendan 
636fa94a07fSbrendan void
637fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
638fa94a07fSbrendan {
639fa94a07fSbrendan 	spa_aux_t search;
640fa94a07fSbrendan 	spa_aux_t *aux;
641fa94a07fSbrendan 	avl_index_t where;
642fa94a07fSbrendan 
643fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
644fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
645fa94a07fSbrendan 
646fa94a07fSbrendan 	ASSERT(aux != NULL);
647fa94a07fSbrendan 
648fa94a07fSbrendan 	if (--aux->aux_count == 0) {
649fa94a07fSbrendan 		avl_remove(avl, aux);
650fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
651fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
652fa94a07fSbrendan 		aux->aux_pool = 0ULL;
653fa94a07fSbrendan 	}
654fa94a07fSbrendan }
655fa94a07fSbrendan 
656fa94a07fSbrendan boolean_t
65789a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
658fa94a07fSbrendan {
659fa94a07fSbrendan 	spa_aux_t search, *found;
660fa94a07fSbrendan 
661fa94a07fSbrendan 	search.aux_guid = guid;
66289a89ebfSlling 	found = avl_find(avl, &search, NULL);
663fa94a07fSbrendan 
664fa94a07fSbrendan 	if (pool) {
665fa94a07fSbrendan 		if (found)
666fa94a07fSbrendan 			*pool = found->aux_pool;
667fa94a07fSbrendan 		else
668fa94a07fSbrendan 			*pool = 0ULL;
669fa94a07fSbrendan 	}
670fa94a07fSbrendan 
67189a89ebfSlling 	if (refcnt) {
67289a89ebfSlling 		if (found)
67389a89ebfSlling 			*refcnt = found->aux_count;
67489a89ebfSlling 		else
67589a89ebfSlling 			*refcnt = 0;
67689a89ebfSlling 	}
67789a89ebfSlling 
678fa94a07fSbrendan 	return (found != NULL);
679fa94a07fSbrendan }
680fa94a07fSbrendan 
681fa94a07fSbrendan void
682fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
683fa94a07fSbrendan {
684fa94a07fSbrendan 	spa_aux_t search, *found;
685fa94a07fSbrendan 	avl_index_t where;
686fa94a07fSbrendan 
687fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
688fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
689fa94a07fSbrendan 	ASSERT(found != NULL);
690fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
691fa94a07fSbrendan 
692fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
693fa94a07fSbrendan }
694fa94a07fSbrendan 
69599653d4eSeschrock /*
69639c23413Seschrock  * Spares are tracked globally due to the following constraints:
69739c23413Seschrock  *
69839c23413Seschrock  * 	- A spare may be part of multiple pools.
69939c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
70039c23413Seschrock  *	  another pool.
70139c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
70239c23413Seschrock  *	  the target is a spare in the same pool.
70339c23413Seschrock  *
70439c23413Seschrock  * We keep track of all spares on the system through the use of a reference
70539c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
70639c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
70739c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
70839c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
70939c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
71039c23413Seschrock  *
71139c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
71239c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
71339c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
71439c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
71599653d4eSeschrock  */
71639c23413Seschrock 
71799653d4eSeschrock static int
71899653d4eSeschrock spa_spare_compare(const void *a, const void *b)
71999653d4eSeschrock {
720fa94a07fSbrendan 	return (spa_aux_compare(a, b));
72199653d4eSeschrock }
72299653d4eSeschrock 
72399653d4eSeschrock void
72439c23413Seschrock spa_spare_add(vdev_t *vd)
72599653d4eSeschrock {
72699653d4eSeschrock 	mutex_enter(&spa_spare_lock);
72739c23413Seschrock 	ASSERT(!vd->vdev_isspare);
728fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
72939c23413Seschrock 	vd->vdev_isspare = B_TRUE;
73099653d4eSeschrock 	mutex_exit(&spa_spare_lock);
73199653d4eSeschrock }
73299653d4eSeschrock 
73399653d4eSeschrock void
73439c23413Seschrock spa_spare_remove(vdev_t *vd)
73599653d4eSeschrock {
73699653d4eSeschrock 	mutex_enter(&spa_spare_lock);
73739c23413Seschrock 	ASSERT(vd->vdev_isspare);
738fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
73939c23413Seschrock 	vd->vdev_isspare = B_FALSE;
74099653d4eSeschrock 	mutex_exit(&spa_spare_lock);
74199653d4eSeschrock }
74299653d4eSeschrock 
74399653d4eSeschrock boolean_t
74489a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
74599653d4eSeschrock {
746fa94a07fSbrendan 	boolean_t found;
74799653d4eSeschrock 
74899653d4eSeschrock 	mutex_enter(&spa_spare_lock);
74989a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
75099653d4eSeschrock 	mutex_exit(&spa_spare_lock);
75199653d4eSeschrock 
752fa94a07fSbrendan 	return (found);
75339c23413Seschrock }
75439c23413Seschrock 
75539c23413Seschrock void
75639c23413Seschrock spa_spare_activate(vdev_t *vd)
75739c23413Seschrock {
75839c23413Seschrock 	mutex_enter(&spa_spare_lock);
75939c23413Seschrock 	ASSERT(vd->vdev_isspare);
760fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
761fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
762fa94a07fSbrendan }
76339c23413Seschrock 
764fa94a07fSbrendan /*
765fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
766fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
767fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
768fa94a07fSbrendan  */
76939c23413Seschrock 
770fa94a07fSbrendan static int
771fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
772fa94a07fSbrendan {
773fa94a07fSbrendan 	return (spa_aux_compare(a, b));
774fa94a07fSbrendan }
775fa94a07fSbrendan 
776fa94a07fSbrendan void
777fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
778fa94a07fSbrendan {
779fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
780fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
781fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
782fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
783fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
784fa94a07fSbrendan }
785fa94a07fSbrendan 
786fa94a07fSbrendan void
787fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
788fa94a07fSbrendan {
789fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
790fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
791fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
792fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
793fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
794fa94a07fSbrendan }
795fa94a07fSbrendan 
796fa94a07fSbrendan boolean_t
797fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
798fa94a07fSbrendan {
799fa94a07fSbrendan 	boolean_t found;
800fa94a07fSbrendan 
801fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
80289a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
803fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
804fa94a07fSbrendan 
805fa94a07fSbrendan 	return (found);
806fa94a07fSbrendan }
807fa94a07fSbrendan 
808fa94a07fSbrendan void
809fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
810fa94a07fSbrendan {
811fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
812fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
813fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
814fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
815fa94a07fSbrendan }
816fa94a07fSbrendan 
817fa94a07fSbrendan void
818fa94a07fSbrendan spa_l2cache_space_update(vdev_t *vd, int64_t space, int64_t alloc)
819fa94a07fSbrendan {
820fa94a07fSbrendan 	vdev_space_update(vd, space, alloc, B_FALSE);
82199653d4eSeschrock }
82299653d4eSeschrock 
823fa9e4066Sahrens /*
824fa9e4066Sahrens  * ==========================================================================
825fa9e4066Sahrens  * SPA vdev locking
826fa9e4066Sahrens  * ==========================================================================
827fa9e4066Sahrens  */
828fa9e4066Sahrens 
829fa9e4066Sahrens /*
830ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
831ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
832fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
833fa9e4066Sahrens  */
834fa9e4066Sahrens uint64_t
835fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
836fa9e4066Sahrens {
8373d7072f8Seschrock 	mutex_enter(&spa_namespace_lock);
8383d7072f8Seschrock 
839e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
840fa9e4066Sahrens 
841fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
842fa9e4066Sahrens }
843fa9e4066Sahrens 
844fa9e4066Sahrens /*
845fa9e4066Sahrens  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
846fa9e4066Sahrens  * locking of spa_vdev_enter(), we also want make sure the transactions have
847fa9e4066Sahrens  * synced to disk, and then update the global configuration cache with the new
848fa9e4066Sahrens  * information.
849fa9e4066Sahrens  */
850fa9e4066Sahrens int
851fa9e4066Sahrens spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
852fa9e4066Sahrens {
8530e34b6a7Sbonwick 	int config_changed = B_FALSE;
854ea8dc4b6Seschrock 
8550373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
8560e34b6a7Sbonwick 
857e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
858e14bb325SJeff Bonwick 
8590e34b6a7Sbonwick 	/*
8600e34b6a7Sbonwick 	 * Reassess the DTLs.
8610e34b6a7Sbonwick 	 */
8620373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
8630e34b6a7Sbonwick 
8640e34b6a7Sbonwick 	/*
8650373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
8660e34b6a7Sbonwick 	 */
867e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
868088f3894Sahrens 		dsl_pool_scrub_restart(spa->spa_dsl_pool);
8690e34b6a7Sbonwick 		config_changed = B_TRUE;
8700e34b6a7Sbonwick 	}
871ea8dc4b6Seschrock 
872e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
873fa9e4066Sahrens 
874fa9e4066Sahrens 	/*
875fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
876fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
877fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
878fa9e4066Sahrens 	 */
879fa9e4066Sahrens 	if (error == 0)
880fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
881fa9e4066Sahrens 
882fa9e4066Sahrens 	if (vd != NULL) {
883*8ad4d6ddSJeff Bonwick 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
884*8ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
885fa9e4066Sahrens 		vdev_free(vd);
886*8ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
887fa9e4066Sahrens 	}
888fa9e4066Sahrens 
889fa9e4066Sahrens 	/*
8900e34b6a7Sbonwick 	 * If the config changed, update the config cache.
891fa9e4066Sahrens 	 */
8920e34b6a7Sbonwick 	if (config_changed)
893c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
894ea8dc4b6Seschrock 
895ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
896fa9e4066Sahrens 
897fa9e4066Sahrens 	return (error);
898fa9e4066Sahrens }
899fa9e4066Sahrens 
900e14bb325SJeff Bonwick /*
901e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
902e14bb325SJeff Bonwick  */
903e14bb325SJeff Bonwick void
904e14bb325SJeff Bonwick spa_vdev_state_enter(spa_t *spa)
905e14bb325SJeff Bonwick {
906e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE_ALL, spa, RW_WRITER);
907e14bb325SJeff Bonwick }
908e14bb325SJeff Bonwick 
909e14bb325SJeff Bonwick int
910e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
911e14bb325SJeff Bonwick {
912e14bb325SJeff Bonwick 	if (vd != NULL)
913e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
914e14bb325SJeff Bonwick 
915e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE_ALL, spa);
916e14bb325SJeff Bonwick 
917*8ad4d6ddSJeff Bonwick 	/*
918*8ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
919*8ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
920*8ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
921*8ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
922*8ad4d6ddSJeff Bonwick 	 */
923*8ad4d6ddSJeff Bonwick 	if (vd != NULL)
924*8ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
925*8ad4d6ddSJeff Bonwick 
926e14bb325SJeff Bonwick 	return (error);
927e14bb325SJeff Bonwick }
928e14bb325SJeff Bonwick 
929fa9e4066Sahrens /*
930fa9e4066Sahrens  * ==========================================================================
931fa9e4066Sahrens  * Miscellaneous functions
932fa9e4066Sahrens  * ==========================================================================
933fa9e4066Sahrens  */
934fa9e4066Sahrens 
935fa9e4066Sahrens /*
936fa9e4066Sahrens  * Rename a spa_t.
937fa9e4066Sahrens  */
938fa9e4066Sahrens int
939fa9e4066Sahrens spa_rename(const char *name, const char *newname)
940fa9e4066Sahrens {
941fa9e4066Sahrens 	spa_t *spa;
942fa9e4066Sahrens 	int err;
943fa9e4066Sahrens 
944fa9e4066Sahrens 	/*
945fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
946fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
947fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
948ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
949fa9e4066Sahrens 	 */
950fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
951fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
952fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
953fa9e4066Sahrens 		return (err);
954fa9e4066Sahrens 	}
955fa9e4066Sahrens 
956e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
957fa9e4066Sahrens 
958fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
959e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
960fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
961fa9e4066Sahrens 
962fa9e4066Sahrens 	/*
963fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
964fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
965fa9e4066Sahrens 	 * during the sync.
966fa9e4066Sahrens 	 */
967fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
968fa9e4066Sahrens 
969e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
970fa9e4066Sahrens 
9710373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
972fa9e4066Sahrens 
973fa9e4066Sahrens 	/*
974fa9e4066Sahrens 	 * Sync the updated config cache.
975fa9e4066Sahrens 	 */
976c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
977fa9e4066Sahrens 
978fa9e4066Sahrens 	spa_close(spa, FTAG);
979fa9e4066Sahrens 
980fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
981fa9e4066Sahrens 
982fa9e4066Sahrens 	return (0);
983fa9e4066Sahrens }
984fa9e4066Sahrens 
985fa9e4066Sahrens 
986fa9e4066Sahrens /*
987fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
988fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
989fa9e4066Sahrens  * specified device_guid.
990fa9e4066Sahrens  */
991fa9e4066Sahrens boolean_t
992fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
993fa9e4066Sahrens {
994fa9e4066Sahrens 	spa_t *spa;
995fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
996fa9e4066Sahrens 
997ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
998fa9e4066Sahrens 
999fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1000fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1001fa9e4066Sahrens 			continue;
1002fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1003fa9e4066Sahrens 			continue;
100439c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
100539c23413Seschrock 			if (device_guid == 0)
100639c23413Seschrock 				break;
100739c23413Seschrock 
100839c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
100939c23413Seschrock 			    device_guid) != NULL)
101039c23413Seschrock 				break;
101139c23413Seschrock 
101239c23413Seschrock 			/*
10138654d025Sperrin 			 * Check any devices we may be in the process of adding.
101439c23413Seschrock 			 */
101539c23413Seschrock 			if (spa->spa_pending_vdev) {
101639c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
101739c23413Seschrock 				    device_guid) != NULL)
101839c23413Seschrock 					break;
101939c23413Seschrock 			}
102039c23413Seschrock 		}
1021fa9e4066Sahrens 	}
1022fa9e4066Sahrens 
1023fa9e4066Sahrens 	return (spa != NULL);
1024fa9e4066Sahrens }
1025fa9e4066Sahrens 
1026fa9e4066Sahrens char *
1027fa9e4066Sahrens spa_strdup(const char *s)
1028fa9e4066Sahrens {
1029fa9e4066Sahrens 	size_t len;
1030fa9e4066Sahrens 	char *new;
1031fa9e4066Sahrens 
1032fa9e4066Sahrens 	len = strlen(s);
1033fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1034fa9e4066Sahrens 	bcopy(s, new, len);
1035fa9e4066Sahrens 	new[len] = '\0';
1036fa9e4066Sahrens 
1037fa9e4066Sahrens 	return (new);
1038fa9e4066Sahrens }
1039fa9e4066Sahrens 
1040fa9e4066Sahrens void
1041fa9e4066Sahrens spa_strfree(char *s)
1042fa9e4066Sahrens {
1043fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1044fa9e4066Sahrens }
1045fa9e4066Sahrens 
1046fa9e4066Sahrens uint64_t
1047fa9e4066Sahrens spa_get_random(uint64_t range)
1048fa9e4066Sahrens {
1049fa9e4066Sahrens 	uint64_t r;
1050fa9e4066Sahrens 
1051fa9e4066Sahrens 	ASSERT(range != 0);
1052fa9e4066Sahrens 
1053fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1054fa9e4066Sahrens 
1055fa9e4066Sahrens 	return (r % range);
1056fa9e4066Sahrens }
1057fa9e4066Sahrens 
1058fa9e4066Sahrens void
1059d80c45e0Sbonwick sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
1060fa9e4066Sahrens {
106144cd46caSbillm 	int d;
1062fa9e4066Sahrens 
1063fa9e4066Sahrens 	if (bp == NULL) {
1064fbabab8fSmaybee 		(void) snprintf(buf, len, "<NULL>");
1065fa9e4066Sahrens 		return;
1066fa9e4066Sahrens 	}
1067fa9e4066Sahrens 
1068fa9e4066Sahrens 	if (BP_IS_HOLE(bp)) {
1069fbabab8fSmaybee 		(void) snprintf(buf, len, "<hole>");
1070fa9e4066Sahrens 		return;
1071fa9e4066Sahrens 	}
1072fa9e4066Sahrens 
107344cd46caSbillm 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
1074fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LEVEL(bp),
1075fa9e4066Sahrens 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
1076fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LSIZE(bp),
107744cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp));
107844cd46caSbillm 
107944cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
1080d80c45e0Sbonwick 		const dva_t *dva = &bp->blk_dva[d];
108144cd46caSbillm 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
108244cd46caSbillm 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
108344cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(dva),
108444cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(dva),
108544cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(dva));
108644cd46caSbillm 	}
108744cd46caSbillm 
108844cd46caSbillm 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
108944cd46caSbillm 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
1090fa9e4066Sahrens 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
1091fa9e4066Sahrens 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
1092fa9e4066Sahrens 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
109344cd46caSbillm 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
1094fa9e4066Sahrens 	    (u_longlong_t)bp->blk_birth,
1095fa9e4066Sahrens 	    (u_longlong_t)bp->blk_fill,
1096fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
1097fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
1098fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
1099fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
1100fa9e4066Sahrens }
1101fa9e4066Sahrens 
1102fa9e4066Sahrens void
1103fa9e4066Sahrens spa_freeze(spa_t *spa)
1104fa9e4066Sahrens {
1105fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1106fa9e4066Sahrens 
1107e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1108fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1109fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1110fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1111fa9e4066Sahrens 	}
1112e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1113fa9e4066Sahrens 	if (freeze_txg != 0)
1114fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1115fa9e4066Sahrens }
1116fa9e4066Sahrens 
11170125049cSahrens void
11180125049cSahrens zfs_panic_recover(const char *fmt, ...)
11190125049cSahrens {
11200125049cSahrens 	va_list adx;
11210125049cSahrens 
11220125049cSahrens 	va_start(adx, fmt);
11230125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
11240125049cSahrens 	va_end(adx);
11250125049cSahrens }
11260125049cSahrens 
1127fa9e4066Sahrens /*
1128fa9e4066Sahrens  * ==========================================================================
1129fa9e4066Sahrens  * Accessor functions
1130fa9e4066Sahrens  * ==========================================================================
1131fa9e4066Sahrens  */
1132fa9e4066Sahrens 
1133088f3894Sahrens boolean_t
113488b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1135fa9e4066Sahrens {
113688b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1137fa9e4066Sahrens }
1138fa9e4066Sahrens 
1139fa9e4066Sahrens dsl_pool_t *
1140fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1141fa9e4066Sahrens {
1142fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1143fa9e4066Sahrens }
1144fa9e4066Sahrens 
1145fa9e4066Sahrens blkptr_t *
1146fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1147fa9e4066Sahrens {
1148fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1149fa9e4066Sahrens }
1150fa9e4066Sahrens 
1151fa9e4066Sahrens void
1152fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1153fa9e4066Sahrens {
1154fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1155fa9e4066Sahrens }
1156fa9e4066Sahrens 
1157fa9e4066Sahrens void
1158fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1159fa9e4066Sahrens {
1160fa9e4066Sahrens 	if (spa->spa_root == NULL)
1161fa9e4066Sahrens 		buf[0] = '\0';
1162fa9e4066Sahrens 	else
1163fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1164fa9e4066Sahrens }
1165fa9e4066Sahrens 
1166fa9e4066Sahrens int
1167fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1168fa9e4066Sahrens {
1169fa9e4066Sahrens 	return (spa->spa_sync_pass);
1170fa9e4066Sahrens }
1171fa9e4066Sahrens 
1172fa9e4066Sahrens char *
1173fa9e4066Sahrens spa_name(spa_t *spa)
1174fa9e4066Sahrens {
1175fa9e4066Sahrens 	return (spa->spa_name);
1176fa9e4066Sahrens }
1177fa9e4066Sahrens 
1178fa9e4066Sahrens uint64_t
1179fa9e4066Sahrens spa_guid(spa_t *spa)
1180fa9e4066Sahrens {
1181b5989ec7Seschrock 	/*
1182b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1183b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1184b5989ec7Seschrock 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1185b5989ec7Seschrock 	 * this case.
1186b5989ec7Seschrock 	 */
1187b5989ec7Seschrock 	if (spa->spa_root_vdev != NULL)
1188b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1189b5989ec7Seschrock 	else
1190b5989ec7Seschrock 		return (spa->spa_load_guid);
1191fa9e4066Sahrens }
1192fa9e4066Sahrens 
1193fa9e4066Sahrens uint64_t
1194fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1195fa9e4066Sahrens {
1196fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1197fa9e4066Sahrens }
1198fa9e4066Sahrens 
1199fa9e4066Sahrens uint64_t
1200fa9e4066Sahrens spa_first_txg(spa_t *spa)
1201fa9e4066Sahrens {
1202fa9e4066Sahrens 	return (spa->spa_first_txg);
1203fa9e4066Sahrens }
1204fa9e4066Sahrens 
120588b7b0f2SMatthew Ahrens pool_state_t
1206fa9e4066Sahrens spa_state(spa_t *spa)
1207fa9e4066Sahrens {
1208fa9e4066Sahrens 	return (spa->spa_state);
1209fa9e4066Sahrens }
1210fa9e4066Sahrens 
1211fa9e4066Sahrens uint64_t
1212fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1213fa9e4066Sahrens {
1214fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1215fa9e4066Sahrens }
1216fa9e4066Sahrens 
1217fa9e4066Sahrens /*
121899653d4eSeschrock  * Return how much space is allocated in the pool (ie. sum of all asize)
1219fa9e4066Sahrens  */
1220fa9e4066Sahrens uint64_t
1221fa9e4066Sahrens spa_get_alloc(spa_t *spa)
1222fa9e4066Sahrens {
1223fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
1224fa9e4066Sahrens }
1225fa9e4066Sahrens 
1226fa9e4066Sahrens /*
122799653d4eSeschrock  * Return how much (raid-z inflated) space there is in the pool.
1228fa9e4066Sahrens  */
1229fa9e4066Sahrens uint64_t
1230fa9e4066Sahrens spa_get_space(spa_t *spa)
1231fa9e4066Sahrens {
1232fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_space);
1233fa9e4066Sahrens }
1234fa9e4066Sahrens 
123599653d4eSeschrock /*
123699653d4eSeschrock  * Return the amount of raid-z-deflated space in the pool.
123799653d4eSeschrock  */
123899653d4eSeschrock uint64_t
123999653d4eSeschrock spa_get_dspace(spa_t *spa)
124099653d4eSeschrock {
124199653d4eSeschrock 	if (spa->spa_deflate)
124299653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
124399653d4eSeschrock 	else
124499653d4eSeschrock 		return (spa->spa_root_vdev->vdev_stat.vs_space);
124599653d4eSeschrock }
124699653d4eSeschrock 
1247fa9e4066Sahrens /* ARGSUSED */
1248fa9e4066Sahrens uint64_t
1249fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1250fa9e4066Sahrens {
1251fa9e4066Sahrens 	/*
1252fa9e4066Sahrens 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
1253fa9e4066Sahrens 	 * case the space requirement is exactly 2x; so just assume that.
125444cd46caSbillm 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
125544cd46caSbillm 	 * we have to multiply by a total of 6x.
125644cd46caSbillm 	 */
125744cd46caSbillm 	return (lsize * 6);
125844cd46caSbillm }
125944cd46caSbillm 
12600a4e9518Sgw /*
12610a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
12620a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
12630a4e9518Sgw  */
12640a4e9518Sgw uint8_t
12650a4e9518Sgw spa_get_failmode(spa_t *spa)
12660a4e9518Sgw {
12670a4e9518Sgw 	return (spa->spa_failmode);
12680a4e9518Sgw }
12690a4e9518Sgw 
1270e14bb325SJeff Bonwick boolean_t
1271e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1272e14bb325SJeff Bonwick {
1273e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1274e14bb325SJeff Bonwick }
1275e14bb325SJeff Bonwick 
127644cd46caSbillm uint64_t
127744cd46caSbillm spa_version(spa_t *spa)
127844cd46caSbillm {
127944cd46caSbillm 	return (spa->spa_ubsync.ub_version);
128044cd46caSbillm }
128144cd46caSbillm 
128244cd46caSbillm int
128344cd46caSbillm spa_max_replication(spa_t *spa)
128444cd46caSbillm {
128544cd46caSbillm 	/*
1286e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
128744cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
128844cd46caSbillm 	 * replication level accordingly.
1289fa9e4066Sahrens 	 */
1290e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
129144cd46caSbillm 		return (1);
129244cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1293fa9e4066Sahrens }
1294fa9e4066Sahrens 
129599653d4eSeschrock uint64_t
129699653d4eSeschrock bp_get_dasize(spa_t *spa, const blkptr_t *bp)
129799653d4eSeschrock {
129899653d4eSeschrock 	int sz = 0, i;
129999653d4eSeschrock 
130099653d4eSeschrock 	if (!spa->spa_deflate)
130199653d4eSeschrock 		return (BP_GET_ASIZE(bp));
130299653d4eSeschrock 
1303e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
130499653d4eSeschrock 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
130599653d4eSeschrock 		vdev_t *vd =
130699653d4eSeschrock 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
13071934e92fSmaybee 		if (vd)
13081934e92fSmaybee 			sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >>
13091934e92fSmaybee 			    SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
131099653d4eSeschrock 	}
1311e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
131299653d4eSeschrock 	return (sz);
131399653d4eSeschrock }
131499653d4eSeschrock 
1315fa9e4066Sahrens /*
1316fa9e4066Sahrens  * ==========================================================================
1317fa9e4066Sahrens  * Initialization and Termination
1318fa9e4066Sahrens  * ==========================================================================
1319fa9e4066Sahrens  */
1320fa9e4066Sahrens 
1321fa9e4066Sahrens static int
1322fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1323fa9e4066Sahrens {
1324fa9e4066Sahrens 	const spa_t *s1 = a1;
1325fa9e4066Sahrens 	const spa_t *s2 = a2;
1326fa9e4066Sahrens 	int s;
1327fa9e4066Sahrens 
1328fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1329fa9e4066Sahrens 	if (s > 0)
1330fa9e4066Sahrens 		return (1);
1331fa9e4066Sahrens 	if (s < 0)
1332fa9e4066Sahrens 		return (-1);
1333fa9e4066Sahrens 	return (0);
1334fa9e4066Sahrens }
1335fa9e4066Sahrens 
13360373e76bSbonwick int
13370373e76bSbonwick spa_busy(void)
13380373e76bSbonwick {
13390373e76bSbonwick 	return (spa_active_count);
13400373e76bSbonwick }
13410373e76bSbonwick 
1342e7cbe64fSgw void
1343e7cbe64fSgw spa_boot_init()
1344e7cbe64fSgw {
1345e7cbe64fSgw 	spa_config_load();
1346e7cbe64fSgw }
1347e7cbe64fSgw 
1348fa9e4066Sahrens void
1349fa9e4066Sahrens spa_init(int mode)
1350fa9e4066Sahrens {
1351fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1352c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1353fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1354fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1355fa9e4066Sahrens 
1356fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1357fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1358fa9e4066Sahrens 
1359fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1360fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1361fa94a07fSbrendan 
1362fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1363fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
136499653d4eSeschrock 
1365*8ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1366fa9e4066Sahrens 
1367fa9e4066Sahrens 	refcount_init();
1368fa9e4066Sahrens 	unique_init();
1369fa9e4066Sahrens 	zio_init();
1370fa9e4066Sahrens 	dmu_init();
1371fa9e4066Sahrens 	zil_init();
137287db74c1Sek 	vdev_cache_stat_init();
137391ebeef5Sahrens 	zfs_prop_init();
1374990b4856Slling 	zpool_prop_init();
1375fa9e4066Sahrens 	spa_config_load();
1376e14bb325SJeff Bonwick 	l2arc_start();
1377fa9e4066Sahrens }
1378fa9e4066Sahrens 
1379fa9e4066Sahrens void
1380fa9e4066Sahrens spa_fini(void)
1381fa9e4066Sahrens {
1382e14bb325SJeff Bonwick 	l2arc_stop();
1383e14bb325SJeff Bonwick 
1384fa9e4066Sahrens 	spa_evict_all();
1385fa9e4066Sahrens 
138687db74c1Sek 	vdev_cache_stat_fini();
1387fa9e4066Sahrens 	zil_fini();
1388fa9e4066Sahrens 	dmu_fini();
1389fa9e4066Sahrens 	zio_fini();
139091ebeef5Sahrens 	unique_fini();
1391fa9e4066Sahrens 	refcount_fini();
1392fa9e4066Sahrens 
1393fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
139499653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1395fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1396fa9e4066Sahrens 
1397fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1398fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1399c25056deSgw 	mutex_destroy(&spa_spare_lock);
1400fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1401fa9e4066Sahrens }
14026ce0521aSperrin 
14036ce0521aSperrin /*
14046ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
14056ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
14066ce0521aSperrin  * performance and not correctness
14076ce0521aSperrin  */
14086ce0521aSperrin boolean_t
14096ce0521aSperrin spa_has_slogs(spa_t *spa)
14106ce0521aSperrin {
14116ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
14126ce0521aSperrin }
1413bf82a41bSeschrock 
1414bf82a41bSeschrock /*
1415bf82a41bSeschrock  * Return whether this pool is the root pool.
1416bf82a41bSeschrock  */
1417bf82a41bSeschrock boolean_t
1418bf82a41bSeschrock spa_is_root(spa_t *spa)
1419bf82a41bSeschrock {
1420bf82a41bSeschrock 	return (spa->spa_is_root);
1421bf82a41bSeschrock }
1422*8ad4d6ddSJeff Bonwick 
1423*8ad4d6ddSJeff Bonwick boolean_t
1424*8ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
1425*8ad4d6ddSJeff Bonwick {
1426*8ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
1427*8ad4d6ddSJeff Bonwick }
1428*8ad4d6ddSJeff Bonwick 
1429*8ad4d6ddSJeff Bonwick int
1430*8ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
1431*8ad4d6ddSJeff Bonwick {
1432*8ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
1433*8ad4d6ddSJeff Bonwick }
1434