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