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