xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 0e34b6a7bff4918432f0aa6b1dfaf73ac9df45b1)
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 
176 kmem_cache_t *spa_buffer_pool;
177 int spa_mode;
178 
179 #ifdef ZFS_DEBUG
180 int zfs_flags = ~0;
181 #else
182 int zfs_flags = 0;
183 #endif
184 
185 #define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
186 
187 /*
188  * ==========================================================================
189  * SPA namespace functions
190  * ==========================================================================
191  */
192 
193 /*
194  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
195  * Returns NULL if no matching spa_t is found.
196  */
197 spa_t *
198 spa_lookup(const char *name)
199 {
200 	spa_t search, *spa;
201 	avl_index_t where;
202 
203 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
204 
205 	search.spa_name = (char *)name;
206 	spa = avl_find(&spa_namespace_avl, &search, &where);
207 
208 	return (spa);
209 }
210 
211 /*
212  * Create an uninitialized spa_t with the given name.  Requires
213  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
214  * exist by calling spa_lookup() first.
215  */
216 spa_t *
217 spa_add(const char *name)
218 {
219 	spa_t *spa;
220 
221 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
222 
223 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
224 
225 	spa->spa_name = spa_strdup(name);
226 	spa->spa_state = POOL_STATE_UNINITIALIZED;
227 	spa->spa_freeze_txg = UINT64_MAX;
228 
229 	refcount_create(&spa->spa_refcount);
230 	refcount_create(&spa->spa_config_lock.scl_count);
231 
232 	avl_add(&spa_namespace_avl, spa);
233 
234 	return (spa);
235 }
236 
237 /*
238  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
239  * spa_namespace_lock.  This is called only after the spa_t has been closed and
240  * deactivated.
241  */
242 void
243 spa_remove(spa_t *spa)
244 {
245 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
246 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
247 	ASSERT(spa->spa_scrub_thread == NULL);
248 
249 	avl_remove(&spa_namespace_avl, spa);
250 	cv_broadcast(&spa_namespace_cv);
251 
252 	if (spa->spa_root)
253 		spa_strfree(spa->spa_root);
254 
255 	if (spa->spa_name)
256 		spa_strfree(spa->spa_name);
257 
258 	spa_config_set(spa, NULL);
259 
260 	refcount_destroy(&spa->spa_refcount);
261 	refcount_destroy(&spa->spa_config_lock.scl_count);
262 
263 	kmem_free(spa, sizeof (spa_t));
264 }
265 
266 /*
267  * Given a pool, return the next pool in the namespace, or NULL if there is
268  * none.  If 'prev' is NULL, return the first pool.
269  */
270 spa_t *
271 spa_next(spa_t *prev)
272 {
273 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
274 
275 	if (prev)
276 		return (AVL_NEXT(&spa_namespace_avl, prev));
277 	else
278 		return (avl_first(&spa_namespace_avl));
279 }
280 
281 /*
282  * ==========================================================================
283  * SPA refcount functions
284  * ==========================================================================
285  */
286 
287 /*
288  * Add a reference to the given spa_t.  Must have at least one reference, or
289  * have the namespace lock held.
290  */
291 void
292 spa_open_ref(spa_t *spa, void *tag)
293 {
294 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
295 	    MUTEX_HELD(&spa_namespace_lock));
296 
297 	(void) refcount_add(&spa->spa_refcount, tag);
298 }
299 
300 /*
301  * Remove a reference to the given spa_t.  Must have at least one reference, or
302  * have the namespace lock held.
303  */
304 void
305 spa_close(spa_t *spa, void *tag)
306 {
307 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
308 	    MUTEX_HELD(&spa_namespace_lock));
309 
310 	(void) refcount_remove(&spa->spa_refcount, tag);
311 }
312 
313 /*
314  * Check to see if the spa refcount is zero.  Must be called with
315  * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
316  * number of references acquired when opening a pool
317  */
318 boolean_t
319 spa_refcount_zero(spa_t *spa)
320 {
321 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
322 
323 	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
324 }
325 
326 /*
327  * ==========================================================================
328  * SPA config locking
329  * ==========================================================================
330  */
331 
332 /*
333  * Acquire the config lock.  The config lock is a special rwlock that allows for
334  * recursive enters.  Because these enters come from the same thread as well as
335  * asynchronous threads working on behalf of the owner, we must unilaterally
336  * allow all reads access as long at least one reader is held (even if a write
337  * is requested).  This has the side effect of write starvation, but write locks
338  * are extremely rare, and a solution to this problem would be significantly
339  * more complex (if even possible).
340  *
341  * We would like to assert that the namespace lock isn't held, but this is a
342  * valid use during create.
343  */
344 void
345 spa_config_enter(spa_t *spa, krw_t rw, void *tag)
346 {
347 	spa_config_lock_t *scl = &spa->spa_config_lock;
348 
349 	mutex_enter(&scl->scl_lock);
350 
351 	if (scl->scl_writer != curthread) {
352 		if (rw == RW_READER) {
353 			while (scl->scl_writer != NULL)
354 				cv_wait(&scl->scl_cv, &scl->scl_lock);
355 		} else {
356 			while (scl->scl_writer != NULL ||
357 			    !refcount_is_zero(&scl->scl_count))
358 				cv_wait(&scl->scl_cv, &scl->scl_lock);
359 			scl->scl_writer = curthread;
360 		}
361 	}
362 
363 	(void) refcount_add(&scl->scl_count, tag);
364 
365 	mutex_exit(&scl->scl_lock);
366 }
367 
368 /*
369  * Release the spa config lock, notifying any waiters in the process.
370  */
371 void
372 spa_config_exit(spa_t *spa, void *tag)
373 {
374 	spa_config_lock_t *scl = &spa->spa_config_lock;
375 
376 	mutex_enter(&scl->scl_lock);
377 
378 	ASSERT(!refcount_is_zero(&scl->scl_count));
379 	if (refcount_remove(&scl->scl_count, tag) == 0) {
380 		cv_broadcast(&scl->scl_cv);
381 		scl->scl_writer = NULL;  /* OK in either case */
382 	}
383 
384 	mutex_exit(&scl->scl_lock);
385 }
386 
387 /*
388  * Returns true if the config lock is held in the given manner.
389  */
390 boolean_t
391 spa_config_held(spa_t *spa, krw_t rw)
392 {
393 	spa_config_lock_t *scl = &spa->spa_config_lock;
394 	boolean_t held;
395 
396 	mutex_enter(&scl->scl_lock);
397 	if (rw == RW_WRITER)
398 		held = (scl->scl_writer == curthread);
399 	else
400 		held = !refcount_is_zero(&scl->scl_count);
401 	mutex_exit(&scl->scl_lock);
402 
403 	return (held);
404 }
405 
406 /*
407  * ==========================================================================
408  * SPA vdev locking
409  * ==========================================================================
410  */
411 
412 /*
413  * Lock the given spa_t for the purpose of adding or removing a vdev.
414  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
415  * It returns the next transaction group for the spa_t.
416  */
417 uint64_t
418 spa_vdev_enter(spa_t *spa)
419 {
420 	/*
421 	 * Suspend scrub activity while we mess with the config.
422 	 */
423 	spa_scrub_suspend(spa);
424 
425 	if (spa->spa_root_vdev != NULL)		/* not spa_create() */
426 		mutex_enter(&spa_namespace_lock);
427 
428 	spa_config_enter(spa, RW_WRITER, spa);
429 
430 	return (spa_last_synced_txg(spa) + 1);
431 }
432 
433 /*
434  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
435  * locking of spa_vdev_enter(), we also want make sure the transactions have
436  * synced to disk, and then update the global configuration cache with the new
437  * information.
438  */
439 int
440 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
441 {
442 	vdev_t *rvd = spa->spa_root_vdev;
443 	uint64_t next_txg = spa_last_synced_txg(spa) + 1;
444 	int config_changed = B_FALSE;
445 
446 	/*
447 	 * Usually txg == next_txg, but spa_vdev_attach()
448 	 * actually needs to wait for the open txg to sync.
449 	 */
450 	ASSERT(txg >= next_txg);
451 
452 	/*
453 	 * Reassess the DTLs.
454 	 */
455 	if (rvd != NULL)
456 		vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
457 
458 	/*
459 	 * Update the in-core config if it changed.
460 	 */
461 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
462 		config_changed = B_TRUE;
463 		spa_config_set(spa, spa_config_generate(spa, rvd, next_txg, 0));
464 	}
465 
466 	spa_config_exit(spa, spa);
467 
468 	/*
469 	 * If there was a scrub or resilver in progress, indicate that
470 	 * it must restart, and then allow it to resume.
471 	 */
472 	spa_scrub_restart(spa, txg);
473 	spa_scrub_resume(spa);
474 
475 	if (vd == rvd)				/* spa_create() */
476 		return (error);
477 
478 	/*
479 	 * Note: this txg_wait_synced() is important because it ensures
480 	 * that there won't be more than one config change per txg.
481 	 * This allows us to use the txg as the generation number.
482 	 */
483 	if (error == 0)
484 		txg_wait_synced(spa->spa_dsl_pool, txg);
485 
486 	if (vd != NULL) {
487 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
488 		vdev_free(vd);
489 	}
490 
491 	/*
492 	 * If the config changed, update the config cache.
493 	 */
494 	if (config_changed)
495 		spa_config_sync();
496 
497 	mutex_exit(&spa_namespace_lock);
498 
499 	return (error);
500 }
501 
502 /*
503  * ==========================================================================
504  * Miscellaneous functions
505  * ==========================================================================
506  */
507 
508 /*
509  * Rename a spa_t.
510  */
511 int
512 spa_rename(const char *name, const char *newname)
513 {
514 	spa_t *spa;
515 	int err;
516 
517 	/*
518 	 * Lookup the spa_t and grab the config lock for writing.  We need to
519 	 * actually open the pool so that we can sync out the necessary labels.
520 	 * It's OK to call spa_open() with the namespace lock held because we
521 	 * allow recursive calls for other reasons.
522 	 */
523 	mutex_enter(&spa_namespace_lock);
524 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
525 		mutex_exit(&spa_namespace_lock);
526 		return (err);
527 	}
528 
529 	spa_config_enter(spa, RW_WRITER, FTAG);
530 
531 	avl_remove(&spa_namespace_avl, spa);
532 	spa_strfree(spa->spa_name);
533 	spa->spa_name = spa_strdup(newname);
534 	avl_add(&spa_namespace_avl, spa);
535 
536 	/*
537 	 * Sync all labels to disk with the new names by marking the root vdev
538 	 * dirty and waiting for it to sync.  It will pick up the new pool name
539 	 * during the sync.
540 	 */
541 	vdev_config_dirty(spa->spa_root_vdev);
542 
543 	spa_config_exit(spa, FTAG);
544 
545 	txg_wait_synced(spa->spa_dsl_pool, 0);
546 
547 	/*
548 	 * Sync the updated config cache.
549 	 */
550 	spa_config_set(spa,
551 	    spa_config_generate(spa, NULL, spa_last_synced_txg(spa), 0));
552 	spa_config_sync();
553 
554 	spa_close(spa, FTAG);
555 
556 	mutex_exit(&spa_namespace_lock);
557 
558 	return (0);
559 }
560 
561 
562 /*
563  * Determine whether a pool with given pool_guid exists.  If device_guid is
564  * non-zero, determine whether the pool exists *and* contains a device with the
565  * specified device_guid.
566  */
567 boolean_t
568 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
569 {
570 	spa_t *spa;
571 	avl_tree_t *t = &spa_namespace_avl;
572 
573 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
574 
575 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
576 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
577 			continue;
578 		if (spa->spa_root_vdev == NULL)
579 			continue;
580 		if (spa_guid(spa) == pool_guid && (device_guid == 0 ||
581 		    vdev_lookup_by_guid(spa->spa_root_vdev, device_guid)))
582 			break;
583 	}
584 
585 	return (spa != NULL);
586 }
587 
588 char *
589 spa_strdup(const char *s)
590 {
591 	size_t len;
592 	char *new;
593 
594 	len = strlen(s);
595 	new = kmem_alloc(len + 1, KM_SLEEP);
596 	bcopy(s, new, len);
597 	new[len] = '\0';
598 
599 	return (new);
600 }
601 
602 void
603 spa_strfree(char *s)
604 {
605 	kmem_free(s, strlen(s) + 1);
606 }
607 
608 uint64_t
609 spa_get_random(uint64_t range)
610 {
611 	uint64_t r;
612 
613 	ASSERT(range != 0);
614 
615 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
616 
617 	return (r % range);
618 }
619 
620 void
621 sprintf_blkptr(char *buf, int len, blkptr_t *bp)
622 {
623 	/* XXBP - Need to see if we want all DVAs or not */
624 	dva_t *dva = BP_IDENTITY(bp);
625 
626 	if (bp == NULL) {
627 		(void) snprintf(buf, len, "<NULL>");
628 		return;
629 	}
630 
631 	if (BP_IS_HOLE(bp)) {
632 		(void) snprintf(buf, len, "<hole>");
633 		return;
634 	}
635 
636 	(void) snprintf(buf, len, "[L%llu %s] vdev=%llu offset=%llx "
637 	    "size=%llxL/%llxP/%llxA %s %s %s %s "
638 	    "birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
639 	    (u_longlong_t)BP_GET_LEVEL(bp),
640 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
641 	    (u_longlong_t)DVA_GET_VDEV(dva),
642 	    (u_longlong_t)DVA_GET_OFFSET(dva),
643 	    (u_longlong_t)BP_GET_LSIZE(bp),
644 	    (u_longlong_t)BP_GET_PSIZE(bp),
645 	    (u_longlong_t)DVA_GET_ASIZE(dva),
646 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
647 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
648 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
649 	    DVA_GET_GANG(dva) == 0 ? "contiguous" : "gang",
650 	    (u_longlong_t)bp->blk_birth,
651 	    (u_longlong_t)bp->blk_fill,
652 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
653 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
654 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
655 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
656 }
657 
658 void
659 spa_freeze(spa_t *spa)
660 {
661 	uint64_t freeze_txg = 0;
662 
663 	spa_config_enter(spa, RW_WRITER, FTAG);
664 	if (spa->spa_freeze_txg == UINT64_MAX) {
665 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
666 		spa->spa_freeze_txg = freeze_txg;
667 	}
668 	spa_config_exit(spa, FTAG);
669 	if (freeze_txg != 0)
670 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
671 }
672 
673 /*
674  * ==========================================================================
675  * Accessor functions
676  * ==========================================================================
677  */
678 
679 krwlock_t *
680 spa_traverse_rwlock(spa_t *spa)
681 {
682 	return (&spa->spa_traverse_lock);
683 }
684 
685 int
686 spa_traverse_wanted(spa_t *spa)
687 {
688 	return (spa->spa_traverse_wanted);
689 }
690 
691 dsl_pool_t *
692 spa_get_dsl(spa_t *spa)
693 {
694 	return (spa->spa_dsl_pool);
695 }
696 
697 blkptr_t *
698 spa_get_rootblkptr(spa_t *spa)
699 {
700 	return (&spa->spa_ubsync.ub_rootbp);
701 }
702 
703 void
704 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
705 {
706 	spa->spa_uberblock.ub_rootbp = *bp;
707 }
708 
709 void
710 spa_altroot(spa_t *spa, char *buf, size_t buflen)
711 {
712 	if (spa->spa_root == NULL)
713 		buf[0] = '\0';
714 	else
715 		(void) strncpy(buf, spa->spa_root, buflen);
716 }
717 
718 int
719 spa_sync_pass(spa_t *spa)
720 {
721 	return (spa->spa_sync_pass);
722 }
723 
724 char *
725 spa_name(spa_t *spa)
726 {
727 	/*
728 	 * Accessing the name requires holding either the namespace lock or the
729 	 * config lock, both of which are required to do a rename.
730 	 */
731 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
732 	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
733 
734 	return (spa->spa_name);
735 }
736 
737 uint64_t
738 spa_guid(spa_t *spa)
739 {
740 	return (spa->spa_root_vdev->vdev_guid);
741 }
742 
743 uint64_t
744 spa_last_synced_txg(spa_t *spa)
745 {
746 	return (spa->spa_ubsync.ub_txg);
747 }
748 
749 uint64_t
750 spa_first_txg(spa_t *spa)
751 {
752 	return (spa->spa_first_txg);
753 }
754 
755 int
756 spa_state(spa_t *spa)
757 {
758 	return (spa->spa_state);
759 }
760 
761 uint64_t
762 spa_freeze_txg(spa_t *spa)
763 {
764 	return (spa->spa_freeze_txg);
765 }
766 
767 /*
768  * In the future, this may select among different metaslab classes
769  * depending on the zdp.  For now, there's no such distinction.
770  */
771 metaslab_class_t *
772 spa_metaslab_class_select(spa_t *spa)
773 {
774 	return (spa->spa_normal_class);
775 }
776 
777 /*
778  * Return pool-wide allocated space.
779  */
780 uint64_t
781 spa_get_alloc(spa_t *spa)
782 {
783 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
784 }
785 
786 /*
787  * Return pool-wide allocated space.
788  */
789 uint64_t
790 spa_get_space(spa_t *spa)
791 {
792 	return (spa->spa_root_vdev->vdev_stat.vs_space);
793 }
794 
795 /* ARGSUSED */
796 uint64_t
797 spa_get_asize(spa_t *spa, uint64_t lsize)
798 {
799 	/*
800 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
801 	 * case the space requirement is exactly 2x; so just assume that.
802 	 */
803 	return (lsize << 1);
804 }
805 
806 /*
807  * ==========================================================================
808  * Initialization and Termination
809  * ==========================================================================
810  */
811 
812 static int
813 spa_name_compare(const void *a1, const void *a2)
814 {
815 	const spa_t *s1 = a1;
816 	const spa_t *s2 = a2;
817 	int s;
818 
819 	s = strcmp(s1->spa_name, s2->spa_name);
820 	if (s > 0)
821 		return (1);
822 	if (s < 0)
823 		return (-1);
824 	return (0);
825 }
826 
827 void
828 spa_init(int mode)
829 {
830 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
831 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
832 
833 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
834 	    offsetof(spa_t, spa_avl));
835 
836 	spa_mode = mode;
837 
838 	refcount_init();
839 	unique_init();
840 	zio_init();
841 	dmu_init();
842 	zil_init();
843 	spa_config_load();
844 }
845 
846 void
847 spa_fini(void)
848 {
849 	spa_evict_all();
850 
851 	zil_fini();
852 	dmu_fini();
853 	zio_fini();
854 	refcount_fini();
855 
856 	avl_destroy(&spa_namespace_avl);
857 
858 	cv_destroy(&spa_namespace_cv);
859 	mutex_destroy(&spa_namespace_lock);
860 }
861