xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 44cd46cadd9aab751dae6a4023c1cb5bf316d274)
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 /*
22ea8dc4b6Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/spa_impl.h>
30fa9e4066Sahrens #include <sys/zio.h>
31fa9e4066Sahrens #include <sys/zio_checksum.h>
32fa9e4066Sahrens #include <sys/zio_compress.h>
33fa9e4066Sahrens #include <sys/dmu.h>
34fa9e4066Sahrens #include <sys/dmu_tx.h>
35fa9e4066Sahrens #include <sys/zap.h>
36fa9e4066Sahrens #include <sys/zil.h>
37fa9e4066Sahrens #include <sys/vdev_impl.h>
38fa9e4066Sahrens #include <sys/metaslab.h>
39fa9e4066Sahrens #include <sys/uberblock_impl.h>
40fa9e4066Sahrens #include <sys/txg.h>
41fa9e4066Sahrens #include <sys/avl.h>
42fa9e4066Sahrens #include <sys/unique.h>
43fa9e4066Sahrens #include <sys/dsl_pool.h>
44fa9e4066Sahrens #include <sys/dsl_dir.h>
45fa9e4066Sahrens #include <sys/dsl_prop.h>
46fa9e4066Sahrens #include <sys/fs/zfs.h>
47fa9e4066Sahrens 
48fa9e4066Sahrens /*
49fa9e4066Sahrens  * SPA locking
50fa9e4066Sahrens  *
51fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * spa_namespace_lock (global mutex)
54fa9e4066Sahrens  *
55*44cd46caSbillm  *	This lock must be acquired to do any of the following:
56fa9e4066Sahrens  *
57*44cd46caSbillm  *		- Lookup a spa_t by name
58*44cd46caSbillm  *		- Add or remove a spa_t from the namespace
59*44cd46caSbillm  *		- Increase spa_refcount from non-zero
60*44cd46caSbillm  *		- Check if spa_refcount is zero
61*44cd46caSbillm  *		- Rename a spa_t
62ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
63*44cd46caSbillm  *		- Held for the duration of create/destroy/import/export
64fa9e4066Sahrens  *
65*44cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
66*44cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
67*44cd46caSbillm  *	definition they must have an existing reference, and will never need
68*44cd46caSbillm  *	to lookup a spa_t by name.
69fa9e4066Sahrens  *
70fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
71fa9e4066Sahrens  *
72*44cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
73*44cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
74*44cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
75*44cd46caSbillm  *	some references in the DMU.  Internally we check against SPA_MINREF, but
76*44cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
77fa9e4066Sahrens  *
78fa9e4066Sahrens  * spa_config_lock (per-spa crazy rwlock)
79fa9e4066Sahrens  *
80*44cd46caSbillm  *	This SPA special is a recursive rwlock, capable of being acquired from
81*44cd46caSbillm  *	asynchronous threads.  It has protects the spa_t from config changes,
82*44cd46caSbillm  *	and must be held in the following circumstances:
83fa9e4066Sahrens  *
84*44cd46caSbillm  *		- RW_READER to perform I/O to the spa
85*44cd46caSbillm  *		- RW_WRITER to change the vdev config
86fa9e4066Sahrens  *
87fa9e4066Sahrens  * spa_config_cache_lock (per-spa mutex)
88fa9e4066Sahrens  *
89*44cd46caSbillm  *	This mutex prevents the spa_config nvlist from being updated.  No
90fa9e4066Sahrens  *      other locks are required to obtain this lock, although implicitly you
91fa9e4066Sahrens  *      must have the namespace lock or non-zero refcount to have any kind
92fa9e4066Sahrens  *      of spa_t pointer at all.
93fa9e4066Sahrens  *
94fa9e4066Sahrens  * The locking order is fairly straightforward:
95fa9e4066Sahrens  *
96*44cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
97fa9e4066Sahrens  *
98*44cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
99*44cd46caSbillm  *	or to check if it is zero.
100fa9e4066Sahrens  *
101*44cd46caSbillm  *		spa_refcount		->	spa_config_lock
102fa9e4066Sahrens  *
103*44cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
104*44cd46caSbillm  *	the config lock.
105fa9e4066Sahrens  *
106*44cd46caSbillm  *		spa_namespace_lock	->	spa_config_lock
107fa9e4066Sahrens  *
108*44cd46caSbillm  *	The namespace lock must always be taken before the config lock.
109fa9e4066Sahrens  *
110fa9e4066Sahrens  *
111fa9e4066Sahrens  * The spa_namespace_lock and spa_config_cache_lock can be acquired directly and
112fa9e4066Sahrens  * are globally visible.
113fa9e4066Sahrens  *
114fa9e4066Sahrens  * The namespace is manipulated using the following functions, all which require
115fa9e4066Sahrens  * the spa_namespace_lock to be held.
116fa9e4066Sahrens  *
117*44cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
118fa9e4066Sahrens  *
119*44cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
120fa9e4066Sahrens  *
121*44cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
122*44cd46caSbillm  *				frees up any memory associated with the spa_t.
123fa9e4066Sahrens  *
124*44cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
125*44cd46caSbillm  *				first if NULL is passed.
126fa9e4066Sahrens  *
127*44cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
128*44cd46caSbillm  *				the system.
129fa9e4066Sahrens  *
130ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
131fa9e4066Sahrens  *
132fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
133fa9e4066Sahrens  *
134*44cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
135*44cd46caSbillm  *				called with spa_namespace_lock held if the
136*44cd46caSbillm  *				refcount is currently zero.
137fa9e4066Sahrens  *
138*44cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
139*44cd46caSbillm  *				not free the spa_t or remove it from the
140*44cd46caSbillm  *				namespace.  No locking is required.
141fa9e4066Sahrens  *
142*44cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
143*44cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
144*44cd46caSbillm  *				held.
145fa9e4066Sahrens  *
146fa9e4066Sahrens  * The spa_config_lock is manipulated using the following functions:
147fa9e4066Sahrens  *
148*44cd46caSbillm  *	spa_config_enter()	Acquire the config lock as RW_READER or
149*44cd46caSbillm  *				RW_WRITER.  At least one reference on the spa_t
150*44cd46caSbillm  *				must exist.
151fa9e4066Sahrens  *
152*44cd46caSbillm  *	spa_config_exit()	Release the config lock.
153fa9e4066Sahrens  *
154*44cd46caSbillm  *	spa_config_held()	Returns true if the config lock is currently
155*44cd46caSbillm  *				held in the given state.
156fa9e4066Sahrens  *
157ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
158fa9e4066Sahrens  *
159*44cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
160ea8dc4b6Seschrock  *				for writing.
161fa9e4066Sahrens  *
162*44cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
163*44cd46caSbillm  *				to complete, sync the updated configs to the
164ea8dc4b6Seschrock  *				cache, and release the namespace lock.
165fa9e4066Sahrens  *
166fa9e4066Sahrens  * The spa_name() function also requires either the spa_namespace_lock
167fa9e4066Sahrens  * or the spa_config_lock, as both are needed to do a rename.  spa_rename() is
168fa9e4066Sahrens  * also implemented within this file since is requires manipulation of the
169fa9e4066Sahrens  * namespace.
170fa9e4066Sahrens  */
171fa9e4066Sahrens 
172fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
173fa9e4066Sahrens kmutex_t spa_namespace_lock;
174fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
1750373e76bSbonwick static int spa_active_count;
176*44cd46caSbillm static int spa_max_replication_override = SPA_DVAS_PER_BP;
177fa9e4066Sahrens 
178fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
179fa9e4066Sahrens int spa_mode;
180fa9e4066Sahrens 
181fa9e4066Sahrens #ifdef ZFS_DEBUG
182fa9e4066Sahrens int zfs_flags = ~0;
183fa9e4066Sahrens #else
184fa9e4066Sahrens int zfs_flags = 0;
185fa9e4066Sahrens #endif
186fa9e4066Sahrens 
187fa9e4066Sahrens #define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
188fa9e4066Sahrens 
189fa9e4066Sahrens /*
190fa9e4066Sahrens  * ==========================================================================
191fa9e4066Sahrens  * SPA namespace functions
192fa9e4066Sahrens  * ==========================================================================
193fa9e4066Sahrens  */
194fa9e4066Sahrens 
195fa9e4066Sahrens /*
196fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
197fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
198fa9e4066Sahrens  */
199fa9e4066Sahrens spa_t *
200fa9e4066Sahrens spa_lookup(const char *name)
201fa9e4066Sahrens {
202fa9e4066Sahrens 	spa_t search, *spa;
203fa9e4066Sahrens 	avl_index_t where;
204fa9e4066Sahrens 
205fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
206fa9e4066Sahrens 
207fa9e4066Sahrens 	search.spa_name = (char *)name;
208fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
209fa9e4066Sahrens 
210fa9e4066Sahrens 	return (spa);
211fa9e4066Sahrens }
212fa9e4066Sahrens 
213fa9e4066Sahrens /*
214fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
215fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
216fa9e4066Sahrens  * exist by calling spa_lookup() first.
217fa9e4066Sahrens  */
218fa9e4066Sahrens spa_t *
2190373e76bSbonwick spa_add(const char *name, const char *altroot)
220fa9e4066Sahrens {
221fa9e4066Sahrens 	spa_t *spa;
222fa9e4066Sahrens 
223fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
224fa9e4066Sahrens 
225fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
226fa9e4066Sahrens 
227fa9e4066Sahrens 	spa->spa_name = spa_strdup(name);
228fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
229fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
2300373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
231fa9e4066Sahrens 
232fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
233ea8dc4b6Seschrock 	refcount_create(&spa->spa_config_lock.scl_count);
234fa9e4066Sahrens 
235fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
236fa9e4066Sahrens 
2370373e76bSbonwick 	/*
2380373e76bSbonwick 	 * Set the alternate root, if there is one.
2390373e76bSbonwick 	 */
2400373e76bSbonwick 	if (altroot) {
2410373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
2420373e76bSbonwick 		spa_active_count++;
2430373e76bSbonwick 	}
2440373e76bSbonwick 
245fa9e4066Sahrens 	return (spa);
246fa9e4066Sahrens }
247fa9e4066Sahrens 
248fa9e4066Sahrens /*
249fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
250fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
251fa9e4066Sahrens  * deactivated.
252fa9e4066Sahrens  */
253fa9e4066Sahrens void
254fa9e4066Sahrens spa_remove(spa_t *spa)
255fa9e4066Sahrens {
256fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
257fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
258fa9e4066Sahrens 	ASSERT(spa->spa_scrub_thread == NULL);
259fa9e4066Sahrens 
260fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
261fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
262fa9e4066Sahrens 
2630373e76bSbonwick 	if (spa->spa_root) {
264fa9e4066Sahrens 		spa_strfree(spa->spa_root);
2650373e76bSbonwick 		spa_active_count--;
2660373e76bSbonwick 	}
267fa9e4066Sahrens 
268fa9e4066Sahrens 	if (spa->spa_name)
269fa9e4066Sahrens 		spa_strfree(spa->spa_name);
270fa9e4066Sahrens 
271fa9e4066Sahrens 	spa_config_set(spa, NULL);
272fa9e4066Sahrens 
273fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
274ea8dc4b6Seschrock 	refcount_destroy(&spa->spa_config_lock.scl_count);
275fa9e4066Sahrens 
276fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
277fa9e4066Sahrens }
278fa9e4066Sahrens 
279fa9e4066Sahrens /*
280fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
281fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
282fa9e4066Sahrens  */
283fa9e4066Sahrens spa_t *
284fa9e4066Sahrens spa_next(spa_t *prev)
285fa9e4066Sahrens {
286fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
287fa9e4066Sahrens 
288fa9e4066Sahrens 	if (prev)
289fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
290fa9e4066Sahrens 	else
291fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
292fa9e4066Sahrens }
293fa9e4066Sahrens 
294fa9e4066Sahrens /*
295fa9e4066Sahrens  * ==========================================================================
296fa9e4066Sahrens  * SPA refcount functions
297fa9e4066Sahrens  * ==========================================================================
298fa9e4066Sahrens  */
299fa9e4066Sahrens 
300fa9e4066Sahrens /*
301fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
302fa9e4066Sahrens  * have the namespace lock held.
303fa9e4066Sahrens  */
304fa9e4066Sahrens void
305fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
306fa9e4066Sahrens {
307fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
308fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
309fa9e4066Sahrens 
310fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
311fa9e4066Sahrens }
312fa9e4066Sahrens 
313fa9e4066Sahrens /*
314fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
315fa9e4066Sahrens  * have the namespace lock held.
316fa9e4066Sahrens  */
317fa9e4066Sahrens void
318fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
319fa9e4066Sahrens {
320fa9e4066Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
321fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
322fa9e4066Sahrens 
323fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
324fa9e4066Sahrens }
325fa9e4066Sahrens 
326fa9e4066Sahrens /*
327fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
328fa9e4066Sahrens  * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
329fa9e4066Sahrens  * number of references acquired when opening a pool
330fa9e4066Sahrens  */
331fa9e4066Sahrens boolean_t
332fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
333fa9e4066Sahrens {
334fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
335fa9e4066Sahrens 
336fa9e4066Sahrens 	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
337fa9e4066Sahrens }
338fa9e4066Sahrens 
339fa9e4066Sahrens /*
340fa9e4066Sahrens  * ==========================================================================
341fa9e4066Sahrens  * SPA config locking
342fa9e4066Sahrens  * ==========================================================================
343fa9e4066Sahrens  */
344fa9e4066Sahrens 
345fa9e4066Sahrens /*
346fa9e4066Sahrens  * Acquire the config lock.  The config lock is a special rwlock that allows for
347fa9e4066Sahrens  * recursive enters.  Because these enters come from the same thread as well as
348fa9e4066Sahrens  * asynchronous threads working on behalf of the owner, we must unilaterally
349fa9e4066Sahrens  * allow all reads access as long at least one reader is held (even if a write
350fa9e4066Sahrens  * is requested).  This has the side effect of write starvation, but write locks
351fa9e4066Sahrens  * are extremely rare, and a solution to this problem would be significantly
352fa9e4066Sahrens  * more complex (if even possible).
353fa9e4066Sahrens  *
354fa9e4066Sahrens  * We would like to assert that the namespace lock isn't held, but this is a
355fa9e4066Sahrens  * valid use during create.
356fa9e4066Sahrens  */
357fa9e4066Sahrens void
358ea8dc4b6Seschrock spa_config_enter(spa_t *spa, krw_t rw, void *tag)
359fa9e4066Sahrens {
360fa9e4066Sahrens 	spa_config_lock_t *scl = &spa->spa_config_lock;
361fa9e4066Sahrens 
362fa9e4066Sahrens 	mutex_enter(&scl->scl_lock);
363fa9e4066Sahrens 
364fa9e4066Sahrens 	if (scl->scl_writer != curthread) {
365fa9e4066Sahrens 		if (rw == RW_READER) {
366fa9e4066Sahrens 			while (scl->scl_writer != NULL)
367fa9e4066Sahrens 				cv_wait(&scl->scl_cv, &scl->scl_lock);
368fa9e4066Sahrens 		} else {
369ea8dc4b6Seschrock 			while (scl->scl_writer != NULL ||
370ea8dc4b6Seschrock 			    !refcount_is_zero(&scl->scl_count))
371fa9e4066Sahrens 				cv_wait(&scl->scl_cv, &scl->scl_lock);
372fa9e4066Sahrens 			scl->scl_writer = curthread;
373fa9e4066Sahrens 		}
374fa9e4066Sahrens 	}
375fa9e4066Sahrens 
376ea8dc4b6Seschrock 	(void) refcount_add(&scl->scl_count, tag);
377fa9e4066Sahrens 
378fa9e4066Sahrens 	mutex_exit(&scl->scl_lock);
379fa9e4066Sahrens }
380fa9e4066Sahrens 
381fa9e4066Sahrens /*
382fa9e4066Sahrens  * Release the spa config lock, notifying any waiters in the process.
383fa9e4066Sahrens  */
384fa9e4066Sahrens void
385ea8dc4b6Seschrock spa_config_exit(spa_t *spa, void *tag)
386fa9e4066Sahrens {
387fa9e4066Sahrens 	spa_config_lock_t *scl = &spa->spa_config_lock;
388fa9e4066Sahrens 
389fa9e4066Sahrens 	mutex_enter(&scl->scl_lock);
390fa9e4066Sahrens 
391ea8dc4b6Seschrock 	ASSERT(!refcount_is_zero(&scl->scl_count));
392ea8dc4b6Seschrock 	if (refcount_remove(&scl->scl_count, tag) == 0) {
393fa9e4066Sahrens 		cv_broadcast(&scl->scl_cv);
394fa9e4066Sahrens 		scl->scl_writer = NULL;  /* OK in either case */
395fa9e4066Sahrens 	}
396fa9e4066Sahrens 
397fa9e4066Sahrens 	mutex_exit(&scl->scl_lock);
398fa9e4066Sahrens }
399fa9e4066Sahrens 
400fa9e4066Sahrens /*
401fa9e4066Sahrens  * Returns true if the config lock is held in the given manner.
402fa9e4066Sahrens  */
403fa9e4066Sahrens boolean_t
404fa9e4066Sahrens spa_config_held(spa_t *spa, krw_t rw)
405fa9e4066Sahrens {
406fa9e4066Sahrens 	spa_config_lock_t *scl = &spa->spa_config_lock;
407fa9e4066Sahrens 	boolean_t held;
408fa9e4066Sahrens 
409fa9e4066Sahrens 	mutex_enter(&scl->scl_lock);
410fa9e4066Sahrens 	if (rw == RW_WRITER)
411fa9e4066Sahrens 		held = (scl->scl_writer == curthread);
412fa9e4066Sahrens 	else
413ea8dc4b6Seschrock 		held = !refcount_is_zero(&scl->scl_count);
414fa9e4066Sahrens 	mutex_exit(&scl->scl_lock);
415fa9e4066Sahrens 
416fa9e4066Sahrens 	return (held);
417fa9e4066Sahrens }
418fa9e4066Sahrens 
419fa9e4066Sahrens /*
420fa9e4066Sahrens  * ==========================================================================
421fa9e4066Sahrens  * SPA vdev locking
422fa9e4066Sahrens  * ==========================================================================
423fa9e4066Sahrens  */
424fa9e4066Sahrens 
425fa9e4066Sahrens /*
426ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
427ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
428fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
429fa9e4066Sahrens  */
430fa9e4066Sahrens uint64_t
431fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
432fa9e4066Sahrens {
433ea8dc4b6Seschrock 	/*
434ea8dc4b6Seschrock 	 * Suspend scrub activity while we mess with the config.
435ea8dc4b6Seschrock 	 */
436ea8dc4b6Seschrock 	spa_scrub_suspend(spa);
437fa9e4066Sahrens 
4380373e76bSbonwick 	mutex_enter(&spa_namespace_lock);
439ea8dc4b6Seschrock 
440ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, spa);
441fa9e4066Sahrens 
442fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
443fa9e4066Sahrens }
444fa9e4066Sahrens 
445fa9e4066Sahrens /*
446fa9e4066Sahrens  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
447fa9e4066Sahrens  * locking of spa_vdev_enter(), we also want make sure the transactions have
448fa9e4066Sahrens  * synced to disk, and then update the global configuration cache with the new
449fa9e4066Sahrens  * information.
450fa9e4066Sahrens  */
451fa9e4066Sahrens int
452fa9e4066Sahrens spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
453fa9e4066Sahrens {
4540e34b6a7Sbonwick 	int config_changed = B_FALSE;
455ea8dc4b6Seschrock 
4560373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
4570e34b6a7Sbonwick 
4580e34b6a7Sbonwick 	/*
4590e34b6a7Sbonwick 	 * Reassess the DTLs.
4600e34b6a7Sbonwick 	 */
4610373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
4620e34b6a7Sbonwick 
4630e34b6a7Sbonwick 	/*
4640373e76bSbonwick 	 * If the config changed, notify the scrub thread that it must restart.
4650e34b6a7Sbonwick 	 */
4660e34b6a7Sbonwick 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
4670e34b6a7Sbonwick 		config_changed = B_TRUE;
4680373e76bSbonwick 		spa_scrub_restart(spa, txg);
4690e34b6a7Sbonwick 	}
470ea8dc4b6Seschrock 
471ea8dc4b6Seschrock 	spa_config_exit(spa, spa);
472fa9e4066Sahrens 
473ea8dc4b6Seschrock 	/*
4745dabedeeSbonwick 	 * Allow scrubbing to resume.
475ea8dc4b6Seschrock 	 */
476ea8dc4b6Seschrock 	spa_scrub_resume(spa);
477fa9e4066Sahrens 
478fa9e4066Sahrens 	/*
479fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
480fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
481fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
482fa9e4066Sahrens 	 */
483fa9e4066Sahrens 	if (error == 0)
484fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
485fa9e4066Sahrens 
486fa9e4066Sahrens 	if (vd != NULL) {
487fa9e4066Sahrens 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
488fa9e4066Sahrens 		vdev_free(vd);
489fa9e4066Sahrens 	}
490fa9e4066Sahrens 
491fa9e4066Sahrens 	/*
4920e34b6a7Sbonwick 	 * If the config changed, update the config cache.
493fa9e4066Sahrens 	 */
4940e34b6a7Sbonwick 	if (config_changed)
495fa9e4066Sahrens 		spa_config_sync();
496ea8dc4b6Seschrock 
497ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
498fa9e4066Sahrens 
499fa9e4066Sahrens 	return (error);
500fa9e4066Sahrens }
501fa9e4066Sahrens 
502fa9e4066Sahrens /*
503fa9e4066Sahrens  * ==========================================================================
504fa9e4066Sahrens  * Miscellaneous functions
505fa9e4066Sahrens  * ==========================================================================
506fa9e4066Sahrens  */
507fa9e4066Sahrens 
508fa9e4066Sahrens /*
509fa9e4066Sahrens  * Rename a spa_t.
510fa9e4066Sahrens  */
511fa9e4066Sahrens int
512fa9e4066Sahrens spa_rename(const char *name, const char *newname)
513fa9e4066Sahrens {
514fa9e4066Sahrens 	spa_t *spa;
515fa9e4066Sahrens 	int err;
516fa9e4066Sahrens 
517fa9e4066Sahrens 	/*
518fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
519fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
520fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
521ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
522fa9e4066Sahrens 	 */
523fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
524fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
525fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
526fa9e4066Sahrens 		return (err);
527fa9e4066Sahrens 	}
528fa9e4066Sahrens 
529ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
530fa9e4066Sahrens 
531fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
532fa9e4066Sahrens 	spa_strfree(spa->spa_name);
533fa9e4066Sahrens 	spa->spa_name = spa_strdup(newname);
534fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
535fa9e4066Sahrens 
536fa9e4066Sahrens 	/*
537fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
538fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
539fa9e4066Sahrens 	 * during the sync.
540fa9e4066Sahrens 	 */
541fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
542fa9e4066Sahrens 
543ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
544fa9e4066Sahrens 
5450373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
546fa9e4066Sahrens 
547fa9e4066Sahrens 	/*
548fa9e4066Sahrens 	 * Sync the updated config cache.
549fa9e4066Sahrens 	 */
550fa9e4066Sahrens 	spa_config_sync();
551fa9e4066Sahrens 
552fa9e4066Sahrens 	spa_close(spa, FTAG);
553fa9e4066Sahrens 
554fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
555fa9e4066Sahrens 
556fa9e4066Sahrens 	return (0);
557fa9e4066Sahrens }
558fa9e4066Sahrens 
559fa9e4066Sahrens 
560fa9e4066Sahrens /*
561fa9e4066Sahrens  * Determine whether a pool with given pool_guid exists.  If device_guid is
562fa9e4066Sahrens  * non-zero, determine whether the pool exists *and* contains a device with the
563fa9e4066Sahrens  * specified device_guid.
564fa9e4066Sahrens  */
565fa9e4066Sahrens boolean_t
566fa9e4066Sahrens spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
567fa9e4066Sahrens {
568fa9e4066Sahrens 	spa_t *spa;
569fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
570fa9e4066Sahrens 
571ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
572fa9e4066Sahrens 
573fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
574fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
575fa9e4066Sahrens 			continue;
576fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
577fa9e4066Sahrens 			continue;
578fa9e4066Sahrens 		if (spa_guid(spa) == pool_guid && (device_guid == 0 ||
579fa9e4066Sahrens 		    vdev_lookup_by_guid(spa->spa_root_vdev, device_guid)))
580fa9e4066Sahrens 			break;
581fa9e4066Sahrens 	}
582fa9e4066Sahrens 
583fa9e4066Sahrens 	return (spa != NULL);
584fa9e4066Sahrens }
585fa9e4066Sahrens 
586fa9e4066Sahrens char *
587fa9e4066Sahrens spa_strdup(const char *s)
588fa9e4066Sahrens {
589fa9e4066Sahrens 	size_t len;
590fa9e4066Sahrens 	char *new;
591fa9e4066Sahrens 
592fa9e4066Sahrens 	len = strlen(s);
593fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
594fa9e4066Sahrens 	bcopy(s, new, len);
595fa9e4066Sahrens 	new[len] = '\0';
596fa9e4066Sahrens 
597fa9e4066Sahrens 	return (new);
598fa9e4066Sahrens }
599fa9e4066Sahrens 
600fa9e4066Sahrens void
601fa9e4066Sahrens spa_strfree(char *s)
602fa9e4066Sahrens {
603fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
604fa9e4066Sahrens }
605fa9e4066Sahrens 
606fa9e4066Sahrens uint64_t
607fa9e4066Sahrens spa_get_random(uint64_t range)
608fa9e4066Sahrens {
609fa9e4066Sahrens 	uint64_t r;
610fa9e4066Sahrens 
611fa9e4066Sahrens 	ASSERT(range != 0);
612fa9e4066Sahrens 
613fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
614fa9e4066Sahrens 
615fa9e4066Sahrens 	return (r % range);
616fa9e4066Sahrens }
617fa9e4066Sahrens 
618fa9e4066Sahrens void
619fbabab8fSmaybee sprintf_blkptr(char *buf, int len, blkptr_t *bp)
620fa9e4066Sahrens {
621*44cd46caSbillm 	int d;
622fa9e4066Sahrens 
623fa9e4066Sahrens 	if (bp == NULL) {
624fbabab8fSmaybee 		(void) snprintf(buf, len, "<NULL>");
625fa9e4066Sahrens 		return;
626fa9e4066Sahrens 	}
627fa9e4066Sahrens 
628fa9e4066Sahrens 	if (BP_IS_HOLE(bp)) {
629fbabab8fSmaybee 		(void) snprintf(buf, len, "<hole>");
630fa9e4066Sahrens 		return;
631fa9e4066Sahrens 	}
632fa9e4066Sahrens 
633*44cd46caSbillm 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
634fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LEVEL(bp),
635fa9e4066Sahrens 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
636fa9e4066Sahrens 	    (u_longlong_t)BP_GET_LSIZE(bp),
637*44cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp));
638*44cd46caSbillm 
639*44cd46caSbillm 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
640*44cd46caSbillm 		dva_t *dva = &bp->blk_dva[d];
641*44cd46caSbillm 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
642*44cd46caSbillm 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
643*44cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(dva),
644*44cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(dva),
645*44cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(dva));
646*44cd46caSbillm 	}
647*44cd46caSbillm 
648*44cd46caSbillm 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
649*44cd46caSbillm 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
650fa9e4066Sahrens 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
651fa9e4066Sahrens 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
652fa9e4066Sahrens 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
653*44cd46caSbillm 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
654fa9e4066Sahrens 	    (u_longlong_t)bp->blk_birth,
655fa9e4066Sahrens 	    (u_longlong_t)bp->blk_fill,
656fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
657fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
658fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
659fa9e4066Sahrens 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
660fa9e4066Sahrens }
661fa9e4066Sahrens 
662fa9e4066Sahrens void
663fa9e4066Sahrens spa_freeze(spa_t *spa)
664fa9e4066Sahrens {
665fa9e4066Sahrens 	uint64_t freeze_txg = 0;
666fa9e4066Sahrens 
667ea8dc4b6Seschrock 	spa_config_enter(spa, RW_WRITER, FTAG);
668fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
669fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
670fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
671fa9e4066Sahrens 	}
672ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
673fa9e4066Sahrens 	if (freeze_txg != 0)
674fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
675fa9e4066Sahrens }
676fa9e4066Sahrens 
677fa9e4066Sahrens /*
678fa9e4066Sahrens  * ==========================================================================
679fa9e4066Sahrens  * Accessor functions
680fa9e4066Sahrens  * ==========================================================================
681fa9e4066Sahrens  */
682fa9e4066Sahrens 
683fa9e4066Sahrens krwlock_t *
684fa9e4066Sahrens spa_traverse_rwlock(spa_t *spa)
685fa9e4066Sahrens {
686fa9e4066Sahrens 	return (&spa->spa_traverse_lock);
687fa9e4066Sahrens }
688fa9e4066Sahrens 
689fa9e4066Sahrens int
690fa9e4066Sahrens spa_traverse_wanted(spa_t *spa)
691fa9e4066Sahrens {
692fa9e4066Sahrens 	return (spa->spa_traverse_wanted);
693fa9e4066Sahrens }
694fa9e4066Sahrens 
695fa9e4066Sahrens dsl_pool_t *
696fa9e4066Sahrens spa_get_dsl(spa_t *spa)
697fa9e4066Sahrens {
698fa9e4066Sahrens 	return (spa->spa_dsl_pool);
699fa9e4066Sahrens }
700fa9e4066Sahrens 
701fa9e4066Sahrens blkptr_t *
702fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
703fa9e4066Sahrens {
704fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
705fa9e4066Sahrens }
706fa9e4066Sahrens 
707fa9e4066Sahrens void
708fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
709fa9e4066Sahrens {
710fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
711fa9e4066Sahrens }
712fa9e4066Sahrens 
713fa9e4066Sahrens void
714fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
715fa9e4066Sahrens {
716fa9e4066Sahrens 	if (spa->spa_root == NULL)
717fa9e4066Sahrens 		buf[0] = '\0';
718fa9e4066Sahrens 	else
719fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
720fa9e4066Sahrens }
721fa9e4066Sahrens 
722fa9e4066Sahrens int
723fa9e4066Sahrens spa_sync_pass(spa_t *spa)
724fa9e4066Sahrens {
725fa9e4066Sahrens 	return (spa->spa_sync_pass);
726fa9e4066Sahrens }
727fa9e4066Sahrens 
728fa9e4066Sahrens char *
729fa9e4066Sahrens spa_name(spa_t *spa)
730fa9e4066Sahrens {
731fa9e4066Sahrens 	/*
732fa9e4066Sahrens 	 * Accessing the name requires holding either the namespace lock or the
733fa9e4066Sahrens 	 * config lock, both of which are required to do a rename.
734fa9e4066Sahrens 	 */
735fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
736fa9e4066Sahrens 	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
737fa9e4066Sahrens 
738fa9e4066Sahrens 	return (spa->spa_name);
739fa9e4066Sahrens }
740fa9e4066Sahrens 
741fa9e4066Sahrens uint64_t
742fa9e4066Sahrens spa_guid(spa_t *spa)
743fa9e4066Sahrens {
744fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_guid);
745fa9e4066Sahrens }
746fa9e4066Sahrens 
747fa9e4066Sahrens uint64_t
748fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
749fa9e4066Sahrens {
750fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
751fa9e4066Sahrens }
752fa9e4066Sahrens 
753fa9e4066Sahrens uint64_t
754fa9e4066Sahrens spa_first_txg(spa_t *spa)
755fa9e4066Sahrens {
756fa9e4066Sahrens 	return (spa->spa_first_txg);
757fa9e4066Sahrens }
758fa9e4066Sahrens 
759fa9e4066Sahrens int
760fa9e4066Sahrens spa_state(spa_t *spa)
761fa9e4066Sahrens {
762fa9e4066Sahrens 	return (spa->spa_state);
763fa9e4066Sahrens }
764fa9e4066Sahrens 
765fa9e4066Sahrens uint64_t
766fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
767fa9e4066Sahrens {
768fa9e4066Sahrens 	return (spa->spa_freeze_txg);
769fa9e4066Sahrens }
770fa9e4066Sahrens 
771fa9e4066Sahrens /*
772fa9e4066Sahrens  * In the future, this may select among different metaslab classes
773fa9e4066Sahrens  * depending on the zdp.  For now, there's no such distinction.
774fa9e4066Sahrens  */
775fa9e4066Sahrens metaslab_class_t *
776fa9e4066Sahrens spa_metaslab_class_select(spa_t *spa)
777fa9e4066Sahrens {
778fa9e4066Sahrens 	return (spa->spa_normal_class);
779fa9e4066Sahrens }
780fa9e4066Sahrens 
781fa9e4066Sahrens /*
782fa9e4066Sahrens  * Return pool-wide allocated space.
783fa9e4066Sahrens  */
784fa9e4066Sahrens uint64_t
785fa9e4066Sahrens spa_get_alloc(spa_t *spa)
786fa9e4066Sahrens {
787fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
788fa9e4066Sahrens }
789fa9e4066Sahrens 
790fa9e4066Sahrens /*
791fa9e4066Sahrens  * Return pool-wide allocated space.
792fa9e4066Sahrens  */
793fa9e4066Sahrens uint64_t
794fa9e4066Sahrens spa_get_space(spa_t *spa)
795fa9e4066Sahrens {
796fa9e4066Sahrens 	return (spa->spa_root_vdev->vdev_stat.vs_space);
797fa9e4066Sahrens }
798fa9e4066Sahrens 
799fa9e4066Sahrens /* ARGSUSED */
800fa9e4066Sahrens uint64_t
801fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
802fa9e4066Sahrens {
803fa9e4066Sahrens 	/*
804fa9e4066Sahrens 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
805fa9e4066Sahrens 	 * case the space requirement is exactly 2x; so just assume that.
806*44cd46caSbillm 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
807*44cd46caSbillm 	 * we have to multiply by a total of 6x.
808*44cd46caSbillm 	 */
809*44cd46caSbillm 	return (lsize * 6);
810*44cd46caSbillm }
811*44cd46caSbillm 
812*44cd46caSbillm uint64_t
813*44cd46caSbillm spa_version(spa_t *spa)
814*44cd46caSbillm {
815*44cd46caSbillm 	return (spa->spa_ubsync.ub_version);
816*44cd46caSbillm }
817*44cd46caSbillm 
818*44cd46caSbillm int
819*44cd46caSbillm spa_max_replication(spa_t *spa)
820*44cd46caSbillm {
821*44cd46caSbillm 	/*
822*44cd46caSbillm 	 * As of ZFS_VERSION == ZFS_VERSION_DITTO_BLOCKS, we are able to
823*44cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
824*44cd46caSbillm 	 * replication level accordingly.
825fa9e4066Sahrens 	 */
826*44cd46caSbillm 	if (spa_version(spa) < ZFS_VERSION_DITTO_BLOCKS)
827*44cd46caSbillm 		return (1);
828*44cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
829fa9e4066Sahrens }
830fa9e4066Sahrens 
831fa9e4066Sahrens /*
832fa9e4066Sahrens  * ==========================================================================
833fa9e4066Sahrens  * Initialization and Termination
834fa9e4066Sahrens  * ==========================================================================
835fa9e4066Sahrens  */
836fa9e4066Sahrens 
837fa9e4066Sahrens static int
838fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
839fa9e4066Sahrens {
840fa9e4066Sahrens 	const spa_t *s1 = a1;
841fa9e4066Sahrens 	const spa_t *s2 = a2;
842fa9e4066Sahrens 	int s;
843fa9e4066Sahrens 
844fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
845fa9e4066Sahrens 	if (s > 0)
846fa9e4066Sahrens 		return (1);
847fa9e4066Sahrens 	if (s < 0)
848fa9e4066Sahrens 		return (-1);
849fa9e4066Sahrens 	return (0);
850fa9e4066Sahrens }
851fa9e4066Sahrens 
8520373e76bSbonwick int
8530373e76bSbonwick spa_busy(void)
8540373e76bSbonwick {
8550373e76bSbonwick 	return (spa_active_count);
8560373e76bSbonwick }
8570373e76bSbonwick 
858fa9e4066Sahrens void
859fa9e4066Sahrens spa_init(int mode)
860fa9e4066Sahrens {
861fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
862fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
863fa9e4066Sahrens 
864fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
865fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
866fa9e4066Sahrens 
867fa9e4066Sahrens 	spa_mode = mode;
868fa9e4066Sahrens 
869fa9e4066Sahrens 	refcount_init();
870fa9e4066Sahrens 	unique_init();
871fa9e4066Sahrens 	zio_init();
872fa9e4066Sahrens 	dmu_init();
873fa9e4066Sahrens 	zil_init();
874fa9e4066Sahrens 	spa_config_load();
875fa9e4066Sahrens }
876fa9e4066Sahrens 
877fa9e4066Sahrens void
878fa9e4066Sahrens spa_fini(void)
879fa9e4066Sahrens {
880fa9e4066Sahrens 	spa_evict_all();
881fa9e4066Sahrens 
882fa9e4066Sahrens 	zil_fini();
883fa9e4066Sahrens 	dmu_fini();
884fa9e4066Sahrens 	zio_fini();
885fa9e4066Sahrens 	refcount_fini();
886fa9e4066Sahrens 
887fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
888fa9e4066Sahrens 
889fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
890fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
891fa9e4066Sahrens }
892