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