xref: /titanic_50/usr/src/uts/common/fs/zfs/spa_misc.c (revision adecd3c68045d04dc367d30faf2eb5cac1f45d5a)
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 int spa_max_replication_override = SPA_DVAS_PER_BP;
177 
178 static avl_tree_t spa_spare_avl;
179 static kmutex_t spa_spare_lock;
180 
181 kmem_cache_t *spa_buffer_pool;
182 int spa_mode;
183 
184 #ifdef ZFS_DEBUG
185 int zfs_flags = ~0;
186 #else
187 int zfs_flags = 0;
188 #endif
189 
190 #define	SPA_MINREF	5	/* spa_refcnt for an open-but-idle pool */
191 
192 /*
193  * ==========================================================================
194  * SPA namespace functions
195  * ==========================================================================
196  */
197 
198 /*
199  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
200  * Returns NULL if no matching spa_t is found.
201  */
202 spa_t *
203 spa_lookup(const char *name)
204 {
205 	spa_t search, *spa;
206 	avl_index_t where;
207 
208 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
209 
210 	search.spa_name = (char *)name;
211 	spa = avl_find(&spa_namespace_avl, &search, &where);
212 
213 	return (spa);
214 }
215 
216 /*
217  * Create an uninitialized spa_t with the given name.  Requires
218  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
219  * exist by calling spa_lookup() first.
220  */
221 spa_t *
222 spa_add(const char *name, const char *altroot)
223 {
224 	spa_t *spa;
225 
226 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
227 
228 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
229 
230 	spa->spa_name = spa_strdup(name);
231 	spa->spa_state = POOL_STATE_UNINITIALIZED;
232 	spa->spa_freeze_txg = UINT64_MAX;
233 	spa->spa_final_txg = UINT64_MAX;
234 
235 	refcount_create(&spa->spa_refcount);
236 	refcount_create(&spa->spa_config_lock.scl_count);
237 
238 	avl_add(&spa_namespace_avl, spa);
239 
240 	/*
241 	 * Set the alternate root, if there is one.
242 	 */
243 	if (altroot) {
244 		spa->spa_root = spa_strdup(altroot);
245 		spa_active_count++;
246 	}
247 
248 	return (spa);
249 }
250 
251 /*
252  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
253  * spa_namespace_lock.  This is called only after the spa_t has been closed and
254  * deactivated.
255  */
256 void
257 spa_remove(spa_t *spa)
258 {
259 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
260 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
261 	ASSERT(spa->spa_scrub_thread == NULL);
262 
263 	avl_remove(&spa_namespace_avl, spa);
264 	cv_broadcast(&spa_namespace_cv);
265 
266 	if (spa->spa_root) {
267 		spa_strfree(spa->spa_root);
268 		spa_active_count--;
269 	}
270 
271 	if (spa->spa_name)
272 		spa_strfree(spa->spa_name);
273 
274 	spa_config_set(spa, NULL);
275 
276 	refcount_destroy(&spa->spa_refcount);
277 	refcount_destroy(&spa->spa_config_lock.scl_count);
278 
279 	mutex_destroy(&spa->spa_sync_bplist.bpl_lock);
280 	mutex_destroy(&spa->spa_config_lock.scl_lock);
281 	mutex_destroy(&spa->spa_errlist_lock);
282 	mutex_destroy(&spa->spa_errlog_lock);
283 	mutex_destroy(&spa->spa_scrub_lock);
284 	mutex_destroy(&spa->spa_config_cache_lock);
285 	mutex_destroy(&spa->spa_async_lock);
286 	mutex_destroy(&spa->spa_history_lock);
287 
288 	kmem_free(spa, sizeof (spa_t));
289 }
290 
291 /*
292  * Given a pool, return the next pool in the namespace, or NULL if there is
293  * none.  If 'prev' is NULL, return the first pool.
294  */
295 spa_t *
296 spa_next(spa_t *prev)
297 {
298 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
299 
300 	if (prev)
301 		return (AVL_NEXT(&spa_namespace_avl, prev));
302 	else
303 		return (avl_first(&spa_namespace_avl));
304 }
305 
306 /*
307  * ==========================================================================
308  * SPA refcount functions
309  * ==========================================================================
310  */
311 
312 /*
313  * Add 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_open_ref(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_add(&spa->spa_refcount, tag);
323 }
324 
325 /*
326  * Remove a reference to the given spa_t.  Must have at least one reference, or
327  * have the namespace lock held.
328  */
329 void
330 spa_close(spa_t *spa, void *tag)
331 {
332 	ASSERT(refcount_count(&spa->spa_refcount) > SPA_MINREF ||
333 	    MUTEX_HELD(&spa_namespace_lock));
334 
335 	(void) refcount_remove(&spa->spa_refcount, tag);
336 }
337 
338 /*
339  * Check to see if the spa refcount is zero.  Must be called with
340  * spa_namespace_lock held.  We really compare against SPA_MINREF, which is the
341  * number of references acquired when opening a pool
342  */
343 boolean_t
344 spa_refcount_zero(spa_t *spa)
345 {
346 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
347 
348 	return (refcount_count(&spa->spa_refcount) == SPA_MINREF);
349 }
350 
351 /*
352  * ==========================================================================
353  * SPA spare tracking
354  * ==========================================================================
355  */
356 
357 /*
358  * We track spare information on a global basis.  This allows us to do two
359  * things: determine when a spare is no longer referenced by any active pool,
360  * and (quickly) determine if a spare is currently in use in another pool on the
361  * system.
362  */
363 typedef struct spa_spare {
364 	uint64_t	spare_guid;
365 	avl_node_t	spare_avl;
366 	int		spare_count;
367 } spa_spare_t;
368 
369 static int
370 spa_spare_compare(const void *a, const void *b)
371 {
372 	const spa_spare_t *sa = a;
373 	const spa_spare_t *sb = b;
374 
375 	if (sa->spare_guid < sb->spare_guid)
376 		return (-1);
377 	else if (sa->spare_guid > sb->spare_guid)
378 		return (1);
379 	else
380 		return (0);
381 }
382 
383 void
384 spa_spare_add(uint64_t guid)
385 {
386 	avl_index_t where;
387 	spa_spare_t search;
388 	spa_spare_t *spare;
389 
390 	mutex_enter(&spa_spare_lock);
391 
392 	search.spare_guid = guid;
393 	if ((spare = avl_find(&spa_spare_avl, &search, &where)) != NULL) {
394 		spare->spare_count++;
395 	} else {
396 		spare = kmem_alloc(sizeof (spa_spare_t), KM_SLEEP);
397 		spare->spare_guid = guid;
398 		spare->spare_count = 1;
399 		avl_insert(&spa_spare_avl, spare, where);
400 	}
401 
402 	mutex_exit(&spa_spare_lock);
403 }
404 
405 void
406 spa_spare_remove(uint64_t guid)
407 {
408 	spa_spare_t search;
409 	spa_spare_t *spare;
410 	avl_index_t where;
411 
412 	mutex_enter(&spa_spare_lock);
413 
414 	search.spare_guid = guid;
415 	spare = avl_find(&spa_spare_avl, &search, &where);
416 
417 	ASSERT(spare != NULL);
418 
419 	if (--spare->spare_count == 0) {
420 		avl_remove(&spa_spare_avl, spare);
421 		kmem_free(spare, sizeof (spa_spare_t));
422 	}
423 
424 	mutex_exit(&spa_spare_lock);
425 }
426 
427 boolean_t
428 spa_spare_inuse(uint64_t guid)
429 {
430 	spa_spare_t search;
431 	avl_index_t where;
432 	boolean_t ret;
433 
434 	mutex_enter(&spa_spare_lock);
435 
436 	search.spare_guid = guid;
437 	ret = (avl_find(&spa_spare_avl, &search, &where) != NULL);
438 
439 	mutex_exit(&spa_spare_lock);
440 
441 	return (ret);
442 }
443 
444 /*
445  * ==========================================================================
446  * SPA config locking
447  * ==========================================================================
448  */
449 
450 /*
451  * Acquire the config lock.  The config lock is a special rwlock that allows for
452  * recursive enters.  Because these enters come from the same thread as well as
453  * asynchronous threads working on behalf of the owner, we must unilaterally
454  * allow all reads access as long at least one reader is held (even if a write
455  * is requested).  This has the side effect of write starvation, but write locks
456  * are extremely rare, and a solution to this problem would be significantly
457  * more complex (if even possible).
458  *
459  * We would like to assert that the namespace lock isn't held, but this is a
460  * valid use during create.
461  */
462 void
463 spa_config_enter(spa_t *spa, krw_t rw, void *tag)
464 {
465 	spa_config_lock_t *scl = &spa->spa_config_lock;
466 
467 	mutex_enter(&scl->scl_lock);
468 
469 	if (scl->scl_writer != curthread) {
470 		if (rw == RW_READER) {
471 			while (scl->scl_writer != NULL)
472 				cv_wait(&scl->scl_cv, &scl->scl_lock);
473 		} else {
474 			while (scl->scl_writer != NULL ||
475 			    !refcount_is_zero(&scl->scl_count))
476 				cv_wait(&scl->scl_cv, &scl->scl_lock);
477 			scl->scl_writer = curthread;
478 		}
479 	}
480 
481 	(void) refcount_add(&scl->scl_count, tag);
482 
483 	mutex_exit(&scl->scl_lock);
484 }
485 
486 /*
487  * Release the spa config lock, notifying any waiters in the process.
488  */
489 void
490 spa_config_exit(spa_t *spa, void *tag)
491 {
492 	spa_config_lock_t *scl = &spa->spa_config_lock;
493 
494 	mutex_enter(&scl->scl_lock);
495 
496 	ASSERT(!refcount_is_zero(&scl->scl_count));
497 	if (refcount_remove(&scl->scl_count, tag) == 0) {
498 		cv_broadcast(&scl->scl_cv);
499 		scl->scl_writer = NULL;  /* OK in either case */
500 	}
501 
502 	mutex_exit(&scl->scl_lock);
503 }
504 
505 /*
506  * Returns true if the config lock is held in the given manner.
507  */
508 boolean_t
509 spa_config_held(spa_t *spa, krw_t rw)
510 {
511 	spa_config_lock_t *scl = &spa->spa_config_lock;
512 	boolean_t held;
513 
514 	mutex_enter(&scl->scl_lock);
515 	if (rw == RW_WRITER)
516 		held = (scl->scl_writer == curthread);
517 	else
518 		held = !refcount_is_zero(&scl->scl_count);
519 	mutex_exit(&scl->scl_lock);
520 
521 	return (held);
522 }
523 
524 /*
525  * ==========================================================================
526  * SPA vdev locking
527  * ==========================================================================
528  */
529 
530 /*
531  * Lock the given spa_t for the purpose of adding or removing a vdev.
532  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
533  * It returns the next transaction group for the spa_t.
534  */
535 uint64_t
536 spa_vdev_enter(spa_t *spa)
537 {
538 	/*
539 	 * Suspend scrub activity while we mess with the config.
540 	 */
541 	spa_scrub_suspend(spa);
542 
543 	mutex_enter(&spa_namespace_lock);
544 
545 	spa_config_enter(spa, RW_WRITER, spa);
546 
547 	return (spa_last_synced_txg(spa) + 1);
548 }
549 
550 /*
551  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
552  * locking of spa_vdev_enter(), we also want make sure the transactions have
553  * synced to disk, and then update the global configuration cache with the new
554  * information.
555  */
556 int
557 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
558 {
559 	int config_changed = B_FALSE;
560 
561 	ASSERT(txg > spa_last_synced_txg(spa));
562 
563 	/*
564 	 * Reassess the DTLs.
565 	 */
566 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
567 
568 	/*
569 	 * If the config changed, notify the scrub thread that it must restart.
570 	 */
571 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
572 		config_changed = B_TRUE;
573 		spa_scrub_restart(spa, txg);
574 	}
575 
576 	spa_config_exit(spa, spa);
577 
578 	/*
579 	 * Allow scrubbing to resume.
580 	 */
581 	spa_scrub_resume(spa);
582 
583 	/*
584 	 * Note: this txg_wait_synced() is important because it ensures
585 	 * that there won't be more than one config change per txg.
586 	 * This allows us to use the txg as the generation number.
587 	 */
588 	if (error == 0)
589 		txg_wait_synced(spa->spa_dsl_pool, txg);
590 
591 	if (vd != NULL) {
592 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
593 		vdev_free(vd);
594 	}
595 
596 	/*
597 	 * If the config changed, update the config cache.
598 	 */
599 	if (config_changed)
600 		spa_config_sync();
601 
602 	mutex_exit(&spa_namespace_lock);
603 
604 	return (error);
605 }
606 
607 /*
608  * ==========================================================================
609  * Miscellaneous functions
610  * ==========================================================================
611  */
612 
613 /*
614  * Rename a spa_t.
615  */
616 int
617 spa_rename(const char *name, const char *newname)
618 {
619 	spa_t *spa;
620 	int err;
621 
622 	/*
623 	 * Lookup the spa_t and grab the config lock for writing.  We need to
624 	 * actually open the pool so that we can sync out the necessary labels.
625 	 * It's OK to call spa_open() with the namespace lock held because we
626 	 * allow recursive calls for other reasons.
627 	 */
628 	mutex_enter(&spa_namespace_lock);
629 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
630 		mutex_exit(&spa_namespace_lock);
631 		return (err);
632 	}
633 
634 	spa_config_enter(spa, RW_WRITER, FTAG);
635 
636 	avl_remove(&spa_namespace_avl, spa);
637 	spa_strfree(spa->spa_name);
638 	spa->spa_name = spa_strdup(newname);
639 	avl_add(&spa_namespace_avl, spa);
640 
641 	/*
642 	 * Sync all labels to disk with the new names by marking the root vdev
643 	 * dirty and waiting for it to sync.  It will pick up the new pool name
644 	 * during the sync.
645 	 */
646 	vdev_config_dirty(spa->spa_root_vdev);
647 
648 	spa_config_exit(spa, FTAG);
649 
650 	txg_wait_synced(spa->spa_dsl_pool, 0);
651 
652 	/*
653 	 * Sync the updated config cache.
654 	 */
655 	spa_config_sync();
656 
657 	spa_close(spa, FTAG);
658 
659 	mutex_exit(&spa_namespace_lock);
660 
661 	return (0);
662 }
663 
664 
665 /*
666  * Determine whether a pool with given pool_guid exists.  If device_guid is
667  * non-zero, determine whether the pool exists *and* contains a device with the
668  * specified device_guid.
669  */
670 boolean_t
671 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
672 {
673 	spa_t *spa;
674 	avl_tree_t *t = &spa_namespace_avl;
675 
676 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
677 
678 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
679 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
680 			continue;
681 		if (spa->spa_root_vdev == NULL)
682 			continue;
683 		if (spa_guid(spa) == pool_guid && (device_guid == 0 ||
684 		    vdev_lookup_by_guid(spa->spa_root_vdev, device_guid)))
685 			break;
686 	}
687 
688 	return (spa != NULL);
689 }
690 
691 char *
692 spa_strdup(const char *s)
693 {
694 	size_t len;
695 	char *new;
696 
697 	len = strlen(s);
698 	new = kmem_alloc(len + 1, KM_SLEEP);
699 	bcopy(s, new, len);
700 	new[len] = '\0';
701 
702 	return (new);
703 }
704 
705 void
706 spa_strfree(char *s)
707 {
708 	kmem_free(s, strlen(s) + 1);
709 }
710 
711 uint64_t
712 spa_get_random(uint64_t range)
713 {
714 	uint64_t r;
715 
716 	ASSERT(range != 0);
717 
718 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
719 
720 	return (r % range);
721 }
722 
723 void
724 sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
725 {
726 	int d;
727 
728 	if (bp == NULL) {
729 		(void) snprintf(buf, len, "<NULL>");
730 		return;
731 	}
732 
733 	if (BP_IS_HOLE(bp)) {
734 		(void) snprintf(buf, len, "<hole>");
735 		return;
736 	}
737 
738 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
739 	    (u_longlong_t)BP_GET_LEVEL(bp),
740 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
741 	    (u_longlong_t)BP_GET_LSIZE(bp),
742 	    (u_longlong_t)BP_GET_PSIZE(bp));
743 
744 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
745 		const dva_t *dva = &bp->blk_dva[d];
746 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
747 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
748 		    (u_longlong_t)DVA_GET_VDEV(dva),
749 		    (u_longlong_t)DVA_GET_OFFSET(dva),
750 		    (u_longlong_t)DVA_GET_ASIZE(dva));
751 	}
752 
753 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
754 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
755 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
756 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
757 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
758 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
759 	    (u_longlong_t)bp->blk_birth,
760 	    (u_longlong_t)bp->blk_fill,
761 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
762 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
763 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
764 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
765 }
766 
767 void
768 spa_freeze(spa_t *spa)
769 {
770 	uint64_t freeze_txg = 0;
771 
772 	spa_config_enter(spa, RW_WRITER, FTAG);
773 	if (spa->spa_freeze_txg == UINT64_MAX) {
774 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
775 		spa->spa_freeze_txg = freeze_txg;
776 	}
777 	spa_config_exit(spa, FTAG);
778 	if (freeze_txg != 0)
779 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
780 }
781 
782 /*
783  * ==========================================================================
784  * Accessor functions
785  * ==========================================================================
786  */
787 
788 krwlock_t *
789 spa_traverse_rwlock(spa_t *spa)
790 {
791 	return (&spa->spa_traverse_lock);
792 }
793 
794 int
795 spa_traverse_wanted(spa_t *spa)
796 {
797 	return (spa->spa_traverse_wanted);
798 }
799 
800 dsl_pool_t *
801 spa_get_dsl(spa_t *spa)
802 {
803 	return (spa->spa_dsl_pool);
804 }
805 
806 blkptr_t *
807 spa_get_rootblkptr(spa_t *spa)
808 {
809 	return (&spa->spa_ubsync.ub_rootbp);
810 }
811 
812 void
813 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
814 {
815 	spa->spa_uberblock.ub_rootbp = *bp;
816 }
817 
818 void
819 spa_altroot(spa_t *spa, char *buf, size_t buflen)
820 {
821 	if (spa->spa_root == NULL)
822 		buf[0] = '\0';
823 	else
824 		(void) strncpy(buf, spa->spa_root, buflen);
825 }
826 
827 int
828 spa_sync_pass(spa_t *spa)
829 {
830 	return (spa->spa_sync_pass);
831 }
832 
833 char *
834 spa_name(spa_t *spa)
835 {
836 	/*
837 	 * Accessing the name requires holding either the namespace lock or the
838 	 * config lock, both of which are required to do a rename.
839 	 */
840 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
841 	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
842 
843 	return (spa->spa_name);
844 }
845 
846 uint64_t
847 spa_guid(spa_t *spa)
848 {
849 	/*
850 	 * If we fail to parse the config during spa_load(), we can go through
851 	 * the error path (which posts an ereport) and end up here with no root
852 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
853 	 * this case.
854 	 */
855 	if (spa->spa_root_vdev != NULL)
856 		return (spa->spa_root_vdev->vdev_guid);
857 	else
858 		return (spa->spa_load_guid);
859 }
860 
861 uint64_t
862 spa_last_synced_txg(spa_t *spa)
863 {
864 	return (spa->spa_ubsync.ub_txg);
865 }
866 
867 uint64_t
868 spa_first_txg(spa_t *spa)
869 {
870 	return (spa->spa_first_txg);
871 }
872 
873 int
874 spa_state(spa_t *spa)
875 {
876 	return (spa->spa_state);
877 }
878 
879 uint64_t
880 spa_freeze_txg(spa_t *spa)
881 {
882 	return (spa->spa_freeze_txg);
883 }
884 
885 /*
886  * In the future, this may select among different metaslab classes
887  * depending on the zdp.  For now, there's no such distinction.
888  */
889 metaslab_class_t *
890 spa_metaslab_class_select(spa_t *spa)
891 {
892 	return (spa->spa_normal_class);
893 }
894 
895 /*
896  * Return how much space is allocated in the pool (ie. sum of all asize)
897  */
898 uint64_t
899 spa_get_alloc(spa_t *spa)
900 {
901 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
902 }
903 
904 /*
905  * Return how much (raid-z inflated) space there is in the pool.
906  */
907 uint64_t
908 spa_get_space(spa_t *spa)
909 {
910 	return (spa->spa_root_vdev->vdev_stat.vs_space);
911 }
912 
913 /*
914  * Return the amount of raid-z-deflated space in the pool.
915  */
916 uint64_t
917 spa_get_dspace(spa_t *spa)
918 {
919 	if (spa->spa_deflate)
920 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
921 	else
922 		return (spa->spa_root_vdev->vdev_stat.vs_space);
923 }
924 
925 /* ARGSUSED */
926 uint64_t
927 spa_get_asize(spa_t *spa, uint64_t lsize)
928 {
929 	/*
930 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
931 	 * case the space requirement is exactly 2x; so just assume that.
932 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
933 	 * we have to multiply by a total of 6x.
934 	 */
935 	return (lsize * 6);
936 }
937 
938 uint64_t
939 spa_version(spa_t *spa)
940 {
941 	return (spa->spa_ubsync.ub_version);
942 }
943 
944 int
945 spa_max_replication(spa_t *spa)
946 {
947 	/*
948 	 * As of ZFS_VERSION == ZFS_VERSION_DITTO_BLOCKS, we are able to
949 	 * handle BPs with more than one DVA allocated.  Set our max
950 	 * replication level accordingly.
951 	 */
952 	if (spa_version(spa) < ZFS_VERSION_DITTO_BLOCKS)
953 		return (1);
954 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
955 }
956 
957 uint64_t
958 bp_get_dasize(spa_t *spa, const blkptr_t *bp)
959 {
960 	int sz = 0, i;
961 
962 	if (!spa->spa_deflate)
963 		return (BP_GET_ASIZE(bp));
964 
965 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
966 		vdev_t *vd =
967 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
968 		sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >> SPA_MINBLOCKSHIFT) *
969 		    vd->vdev_deflate_ratio;
970 	}
971 	return (sz);
972 }
973 
974 /*
975  * ==========================================================================
976  * Initialization and Termination
977  * ==========================================================================
978  */
979 
980 static int
981 spa_name_compare(const void *a1, const void *a2)
982 {
983 	const spa_t *s1 = a1;
984 	const spa_t *s2 = a2;
985 	int s;
986 
987 	s = strcmp(s1->spa_name, s2->spa_name);
988 	if (s > 0)
989 		return (1);
990 	if (s < 0)
991 		return (-1);
992 	return (0);
993 }
994 
995 int
996 spa_busy(void)
997 {
998 	return (spa_active_count);
999 }
1000 
1001 void
1002 spa_init(int mode)
1003 {
1004 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1005 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1006 
1007 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1008 	    offsetof(spa_t, spa_avl));
1009 
1010 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_spare_t),
1011 	    offsetof(spa_spare_t, spare_avl));
1012 
1013 	spa_mode = mode;
1014 
1015 	refcount_init();
1016 	unique_init();
1017 	zio_init();
1018 	dmu_init();
1019 	zil_init();
1020 	spa_config_load();
1021 }
1022 
1023 void
1024 spa_fini(void)
1025 {
1026 	spa_evict_all();
1027 
1028 	zil_fini();
1029 	dmu_fini();
1030 	zio_fini();
1031 	refcount_fini();
1032 
1033 	avl_destroy(&spa_namespace_avl);
1034 	avl_destroy(&spa_spare_avl);
1035 
1036 	cv_destroy(&spa_namespace_cv);
1037 	mutex_destroy(&spa_namespace_lock);
1038 }
1039