xref: /titanic_41/usr/src/uts/common/fs/zfs/spa_misc.c (revision d7d4af51b4d115490d97f0b89993fdbaaf441c94)
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 2007 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 kmutex_t spa_spare_lock;
179 static avl_tree_t spa_spare_avl;
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  * Spares are tracked globally due to the following constraints:
359  *
360  * 	- A spare may be part of multiple pools.
361  * 	- A spare may be added to a pool even if it's actively in use within
362  *	  another pool.
363  * 	- A spare in use in any pool can only be the source of a replacement if
364  *	  the target is a spare in the same pool.
365  *
366  * We keep track of all spares on the system through the use of a reference
367  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
368  * spare, then we bump the reference count in the AVL tree.  In addition, we set
369  * the 'vdev_isspare' member to indicate that the device is a spare (active or
370  * inactive).  When a spare is made active (used to replace a device in the
371  * pool), we also keep track of which pool its been made a part of.
372  *
373  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
374  * called under the spa_namespace lock as part of vdev reconfiguration.  The
375  * separate spare lock exists for the status query path, which does not need to
376  * be completely consistent with respect to other vdev configuration changes.
377  */
378 
379 typedef struct spa_spare {
380 	uint64_t	spare_guid;
381 	uint64_t	spare_pool;
382 	avl_node_t	spare_avl;
383 	int		spare_count;
384 } spa_spare_t;
385 
386 static int
387 spa_spare_compare(const void *a, const void *b)
388 {
389 	const spa_spare_t *sa = a;
390 	const spa_spare_t *sb = b;
391 
392 	if (sa->spare_guid < sb->spare_guid)
393 		return (-1);
394 	else if (sa->spare_guid > sb->spare_guid)
395 		return (1);
396 	else
397 		return (0);
398 }
399 
400 void
401 spa_spare_add(vdev_t *vd)
402 {
403 	avl_index_t where;
404 	spa_spare_t search;
405 	spa_spare_t *spare;
406 
407 	mutex_enter(&spa_spare_lock);
408 	ASSERT(!vd->vdev_isspare);
409 
410 	search.spare_guid = vd->vdev_guid;
411 	if ((spare = avl_find(&spa_spare_avl, &search, &where)) != NULL) {
412 		spare->spare_count++;
413 	} else {
414 		spare = kmem_zalloc(sizeof (spa_spare_t), KM_SLEEP);
415 		spare->spare_guid = vd->vdev_guid;
416 		spare->spare_count = 1;
417 		avl_insert(&spa_spare_avl, spare, where);
418 	}
419 	vd->vdev_isspare = B_TRUE;
420 
421 	mutex_exit(&spa_spare_lock);
422 }
423 
424 void
425 spa_spare_remove(vdev_t *vd)
426 {
427 	spa_spare_t search;
428 	spa_spare_t *spare;
429 	avl_index_t where;
430 
431 	mutex_enter(&spa_spare_lock);
432 
433 	search.spare_guid = vd->vdev_guid;
434 	spare = avl_find(&spa_spare_avl, &search, &where);
435 
436 	ASSERT(vd->vdev_isspare);
437 	ASSERT(spare != NULL);
438 
439 	if (--spare->spare_count == 0) {
440 		avl_remove(&spa_spare_avl, spare);
441 		kmem_free(spare, sizeof (spa_spare_t));
442 	} else if (spare->spare_pool == spa_guid(vd->vdev_spa)) {
443 		spare->spare_pool = 0ULL;
444 	}
445 
446 	vd->vdev_isspare = B_FALSE;
447 	mutex_exit(&spa_spare_lock);
448 }
449 
450 boolean_t
451 spa_spare_exists(uint64_t guid, uint64_t *pool)
452 {
453 	spa_spare_t search, *found;
454 	avl_index_t where;
455 
456 	mutex_enter(&spa_spare_lock);
457 
458 	search.spare_guid = guid;
459 	found = avl_find(&spa_spare_avl, &search, &where);
460 
461 	if (pool) {
462 		if (found)
463 			*pool = found->spare_pool;
464 		else
465 			*pool = 0ULL;
466 	}
467 
468 	mutex_exit(&spa_spare_lock);
469 
470 	return (found != NULL);
471 }
472 
473 void
474 spa_spare_activate(vdev_t *vd)
475 {
476 	spa_spare_t search, *found;
477 	avl_index_t where;
478 
479 	mutex_enter(&spa_spare_lock);
480 	ASSERT(vd->vdev_isspare);
481 
482 	search.spare_guid = vd->vdev_guid;
483 	found = avl_find(&spa_spare_avl, &search, &where);
484 	ASSERT(found != NULL);
485 	ASSERT(found->spare_pool == 0ULL);
486 
487 	found->spare_pool = spa_guid(vd->vdev_spa);
488 	mutex_exit(&spa_spare_lock);
489 }
490 
491 /*
492  * ==========================================================================
493  * SPA config locking
494  * ==========================================================================
495  */
496 
497 /*
498  * Acquire the config lock.  The config lock is a special rwlock that allows for
499  * recursive enters.  Because these enters come from the same thread as well as
500  * asynchronous threads working on behalf of the owner, we must unilaterally
501  * allow all reads access as long at least one reader is held (even if a write
502  * is requested).  This has the side effect of write starvation, but write locks
503  * are extremely rare, and a solution to this problem would be significantly
504  * more complex (if even possible).
505  *
506  * We would like to assert that the namespace lock isn't held, but this is a
507  * valid use during create.
508  */
509 void
510 spa_config_enter(spa_t *spa, krw_t rw, void *tag)
511 {
512 	spa_config_lock_t *scl = &spa->spa_config_lock;
513 
514 	mutex_enter(&scl->scl_lock);
515 
516 	if (scl->scl_writer != curthread) {
517 		if (rw == RW_READER) {
518 			while (scl->scl_writer != NULL)
519 				cv_wait(&scl->scl_cv, &scl->scl_lock);
520 		} else {
521 			while (scl->scl_writer != NULL ||
522 			    !refcount_is_zero(&scl->scl_count))
523 				cv_wait(&scl->scl_cv, &scl->scl_lock);
524 			scl->scl_writer = curthread;
525 		}
526 	}
527 
528 	(void) refcount_add(&scl->scl_count, tag);
529 
530 	mutex_exit(&scl->scl_lock);
531 }
532 
533 /*
534  * Release the spa config lock, notifying any waiters in the process.
535  */
536 void
537 spa_config_exit(spa_t *spa, void *tag)
538 {
539 	spa_config_lock_t *scl = &spa->spa_config_lock;
540 
541 	mutex_enter(&scl->scl_lock);
542 
543 	ASSERT(!refcount_is_zero(&scl->scl_count));
544 	if (refcount_remove(&scl->scl_count, tag) == 0) {
545 		cv_broadcast(&scl->scl_cv);
546 		scl->scl_writer = NULL;  /* OK in either case */
547 	}
548 
549 	mutex_exit(&scl->scl_lock);
550 }
551 
552 /*
553  * Returns true if the config lock is held in the given manner.
554  */
555 boolean_t
556 spa_config_held(spa_t *spa, krw_t rw)
557 {
558 	spa_config_lock_t *scl = &spa->spa_config_lock;
559 	boolean_t held;
560 
561 	mutex_enter(&scl->scl_lock);
562 	if (rw == RW_WRITER)
563 		held = (scl->scl_writer == curthread);
564 	else
565 		held = !refcount_is_zero(&scl->scl_count);
566 	mutex_exit(&scl->scl_lock);
567 
568 	return (held);
569 }
570 
571 /*
572  * ==========================================================================
573  * SPA vdev locking
574  * ==========================================================================
575  */
576 
577 /*
578  * Lock the given spa_t for the purpose of adding or removing a vdev.
579  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
580  * It returns the next transaction group for the spa_t.
581  */
582 uint64_t
583 spa_vdev_enter(spa_t *spa)
584 {
585 	/*
586 	 * Suspend scrub activity while we mess with the config.
587 	 */
588 	spa_scrub_suspend(spa);
589 
590 	mutex_enter(&spa_namespace_lock);
591 
592 	spa_config_enter(spa, RW_WRITER, spa);
593 
594 	return (spa_last_synced_txg(spa) + 1);
595 }
596 
597 /*
598  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
599  * locking of spa_vdev_enter(), we also want make sure the transactions have
600  * synced to disk, and then update the global configuration cache with the new
601  * information.
602  */
603 int
604 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
605 {
606 	int config_changed = B_FALSE;
607 
608 	ASSERT(txg > spa_last_synced_txg(spa));
609 
610 	/*
611 	 * Reassess the DTLs.
612 	 */
613 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
614 
615 	/*
616 	 * If the config changed, notify the scrub thread that it must restart.
617 	 */
618 	if (error == 0 && !list_is_empty(&spa->spa_dirty_list)) {
619 		config_changed = B_TRUE;
620 		spa_scrub_restart(spa, txg);
621 	}
622 
623 	spa_config_exit(spa, spa);
624 
625 	/*
626 	 * Allow scrubbing to resume.
627 	 */
628 	spa_scrub_resume(spa);
629 
630 	/*
631 	 * Note: this txg_wait_synced() is important because it ensures
632 	 * that there won't be more than one config change per txg.
633 	 * This allows us to use the txg as the generation number.
634 	 */
635 	if (error == 0)
636 		txg_wait_synced(spa->spa_dsl_pool, txg);
637 
638 	if (vd != NULL) {
639 		ASSERT(!vd->vdev_detached || vd->vdev_dtl.smo_object == 0);
640 		vdev_free(vd);
641 	}
642 
643 	/*
644 	 * If the config changed, update the config cache.
645 	 */
646 	if (config_changed)
647 		spa_config_sync();
648 
649 	mutex_exit(&spa_namespace_lock);
650 
651 	return (error);
652 }
653 
654 /*
655  * ==========================================================================
656  * Miscellaneous functions
657  * ==========================================================================
658  */
659 
660 /*
661  * Rename a spa_t.
662  */
663 int
664 spa_rename(const char *name, const char *newname)
665 {
666 	spa_t *spa;
667 	int err;
668 
669 	/*
670 	 * Lookup the spa_t and grab the config lock for writing.  We need to
671 	 * actually open the pool so that we can sync out the necessary labels.
672 	 * It's OK to call spa_open() with the namespace lock held because we
673 	 * allow recursive calls for other reasons.
674 	 */
675 	mutex_enter(&spa_namespace_lock);
676 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
677 		mutex_exit(&spa_namespace_lock);
678 		return (err);
679 	}
680 
681 	spa_config_enter(spa, RW_WRITER, FTAG);
682 
683 	avl_remove(&spa_namespace_avl, spa);
684 	spa_strfree(spa->spa_name);
685 	spa->spa_name = spa_strdup(newname);
686 	avl_add(&spa_namespace_avl, spa);
687 
688 	/*
689 	 * Sync all labels to disk with the new names by marking the root vdev
690 	 * dirty and waiting for it to sync.  It will pick up the new pool name
691 	 * during the sync.
692 	 */
693 	vdev_config_dirty(spa->spa_root_vdev);
694 
695 	spa_config_exit(spa, FTAG);
696 
697 	txg_wait_synced(spa->spa_dsl_pool, 0);
698 
699 	/*
700 	 * Sync the updated config cache.
701 	 */
702 	spa_config_sync();
703 
704 	spa_close(spa, FTAG);
705 
706 	mutex_exit(&spa_namespace_lock);
707 
708 	return (0);
709 }
710 
711 
712 /*
713  * Determine whether a pool with given pool_guid exists.  If device_guid is
714  * non-zero, determine whether the pool exists *and* contains a device with the
715  * specified device_guid.
716  */
717 boolean_t
718 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
719 {
720 	spa_t *spa;
721 	avl_tree_t *t = &spa_namespace_avl;
722 
723 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
724 
725 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
726 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
727 			continue;
728 		if (spa->spa_root_vdev == NULL)
729 			continue;
730 		if (spa_guid(spa) == pool_guid) {
731 			if (device_guid == 0)
732 				break;
733 
734 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
735 			    device_guid) != NULL)
736 				break;
737 
738 			/*
739 			 * Check any devices we may in the process of adding.
740 			 */
741 			if (spa->spa_pending_vdev) {
742 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
743 				    device_guid) != NULL)
744 					break;
745 			}
746 		}
747 	}
748 
749 	return (spa != NULL);
750 }
751 
752 char *
753 spa_strdup(const char *s)
754 {
755 	size_t len;
756 	char *new;
757 
758 	len = strlen(s);
759 	new = kmem_alloc(len + 1, KM_SLEEP);
760 	bcopy(s, new, len);
761 	new[len] = '\0';
762 
763 	return (new);
764 }
765 
766 void
767 spa_strfree(char *s)
768 {
769 	kmem_free(s, strlen(s) + 1);
770 }
771 
772 uint64_t
773 spa_get_random(uint64_t range)
774 {
775 	uint64_t r;
776 
777 	ASSERT(range != 0);
778 
779 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
780 
781 	return (r % range);
782 }
783 
784 void
785 sprintf_blkptr(char *buf, int len, const blkptr_t *bp)
786 {
787 	int d;
788 
789 	if (bp == NULL) {
790 		(void) snprintf(buf, len, "<NULL>");
791 		return;
792 	}
793 
794 	if (BP_IS_HOLE(bp)) {
795 		(void) snprintf(buf, len, "<hole>");
796 		return;
797 	}
798 
799 	(void) snprintf(buf, len, "[L%llu %s] %llxL/%llxP ",
800 	    (u_longlong_t)BP_GET_LEVEL(bp),
801 	    dmu_ot[BP_GET_TYPE(bp)].ot_name,
802 	    (u_longlong_t)BP_GET_LSIZE(bp),
803 	    (u_longlong_t)BP_GET_PSIZE(bp));
804 
805 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
806 		const dva_t *dva = &bp->blk_dva[d];
807 		(void) snprintf(buf + strlen(buf), len - strlen(buf),
808 		    "DVA[%d]=<%llu:%llx:%llx> ", d,
809 		    (u_longlong_t)DVA_GET_VDEV(dva),
810 		    (u_longlong_t)DVA_GET_OFFSET(dva),
811 		    (u_longlong_t)DVA_GET_ASIZE(dva));
812 	}
813 
814 	(void) snprintf(buf + strlen(buf), len - strlen(buf),
815 	    "%s %s %s %s birth=%llu fill=%llu cksum=%llx:%llx:%llx:%llx",
816 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
817 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name,
818 	    BP_GET_BYTEORDER(bp) == 0 ? "BE" : "LE",
819 	    BP_IS_GANG(bp) ? "gang" : "contiguous",
820 	    (u_longlong_t)bp->blk_birth,
821 	    (u_longlong_t)bp->blk_fill,
822 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
823 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
824 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
825 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
826 }
827 
828 void
829 spa_freeze(spa_t *spa)
830 {
831 	uint64_t freeze_txg = 0;
832 
833 	spa_config_enter(spa, RW_WRITER, FTAG);
834 	if (spa->spa_freeze_txg == UINT64_MAX) {
835 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
836 		spa->spa_freeze_txg = freeze_txg;
837 	}
838 	spa_config_exit(spa, FTAG);
839 	if (freeze_txg != 0)
840 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
841 }
842 
843 /*
844  * ==========================================================================
845  * Accessor functions
846  * ==========================================================================
847  */
848 
849 krwlock_t *
850 spa_traverse_rwlock(spa_t *spa)
851 {
852 	return (&spa->spa_traverse_lock);
853 }
854 
855 int
856 spa_traverse_wanted(spa_t *spa)
857 {
858 	return (spa->spa_traverse_wanted);
859 }
860 
861 dsl_pool_t *
862 spa_get_dsl(spa_t *spa)
863 {
864 	return (spa->spa_dsl_pool);
865 }
866 
867 blkptr_t *
868 spa_get_rootblkptr(spa_t *spa)
869 {
870 	return (&spa->spa_ubsync.ub_rootbp);
871 }
872 
873 void
874 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
875 {
876 	spa->spa_uberblock.ub_rootbp = *bp;
877 }
878 
879 void
880 spa_altroot(spa_t *spa, char *buf, size_t buflen)
881 {
882 	if (spa->spa_root == NULL)
883 		buf[0] = '\0';
884 	else
885 		(void) strncpy(buf, spa->spa_root, buflen);
886 }
887 
888 int
889 spa_sync_pass(spa_t *spa)
890 {
891 	return (spa->spa_sync_pass);
892 }
893 
894 char *
895 spa_name(spa_t *spa)
896 {
897 	/*
898 	 * Accessing the name requires holding either the namespace lock or the
899 	 * config lock, both of which are required to do a rename.
900 	 */
901 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
902 	    spa_config_held(spa, RW_READER) || spa_config_held(spa, RW_WRITER));
903 
904 	return (spa->spa_name);
905 }
906 
907 uint64_t
908 spa_guid(spa_t *spa)
909 {
910 	/*
911 	 * If we fail to parse the config during spa_load(), we can go through
912 	 * the error path (which posts an ereport) and end up here with no root
913 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
914 	 * this case.
915 	 */
916 	if (spa->spa_root_vdev != NULL)
917 		return (spa->spa_root_vdev->vdev_guid);
918 	else
919 		return (spa->spa_load_guid);
920 }
921 
922 uint64_t
923 spa_last_synced_txg(spa_t *spa)
924 {
925 	return (spa->spa_ubsync.ub_txg);
926 }
927 
928 uint64_t
929 spa_first_txg(spa_t *spa)
930 {
931 	return (spa->spa_first_txg);
932 }
933 
934 int
935 spa_state(spa_t *spa)
936 {
937 	return (spa->spa_state);
938 }
939 
940 uint64_t
941 spa_freeze_txg(spa_t *spa)
942 {
943 	return (spa->spa_freeze_txg);
944 }
945 
946 /*
947  * In the future, this may select among different metaslab classes
948  * depending on the zdp.  For now, there's no such distinction.
949  */
950 metaslab_class_t *
951 spa_metaslab_class_select(spa_t *spa)
952 {
953 	return (spa->spa_normal_class);
954 }
955 
956 /*
957  * Return how much space is allocated in the pool (ie. sum of all asize)
958  */
959 uint64_t
960 spa_get_alloc(spa_t *spa)
961 {
962 	return (spa->spa_root_vdev->vdev_stat.vs_alloc);
963 }
964 
965 /*
966  * Return how much (raid-z inflated) space there is in the pool.
967  */
968 uint64_t
969 spa_get_space(spa_t *spa)
970 {
971 	return (spa->spa_root_vdev->vdev_stat.vs_space);
972 }
973 
974 /*
975  * Return the amount of raid-z-deflated space in the pool.
976  */
977 uint64_t
978 spa_get_dspace(spa_t *spa)
979 {
980 	if (spa->spa_deflate)
981 		return (spa->spa_root_vdev->vdev_stat.vs_dspace);
982 	else
983 		return (spa->spa_root_vdev->vdev_stat.vs_space);
984 }
985 
986 /* ARGSUSED */
987 uint64_t
988 spa_get_asize(spa_t *spa, uint64_t lsize)
989 {
990 	/*
991 	 * For now, the worst case is 512-byte RAID-Z blocks, in which
992 	 * case the space requirement is exactly 2x; so just assume that.
993 	 * Add to this the fact that we can have up to 3 DVAs per bp, and
994 	 * we have to multiply by a total of 6x.
995 	 */
996 	return (lsize * 6);
997 }
998 
999 uint64_t
1000 spa_version(spa_t *spa)
1001 {
1002 	return (spa->spa_ubsync.ub_version);
1003 }
1004 
1005 int
1006 spa_max_replication(spa_t *spa)
1007 {
1008 	/*
1009 	 * As of ZFS_VERSION == ZFS_VERSION_DITTO_BLOCKS, we are able to
1010 	 * handle BPs with more than one DVA allocated.  Set our max
1011 	 * replication level accordingly.
1012 	 */
1013 	if (spa_version(spa) < ZFS_VERSION_DITTO_BLOCKS)
1014 		return (1);
1015 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1016 }
1017 
1018 uint64_t
1019 bp_get_dasize(spa_t *spa, const blkptr_t *bp)
1020 {
1021 	int sz = 0, i;
1022 
1023 	if (!spa->spa_deflate)
1024 		return (BP_GET_ASIZE(bp));
1025 
1026 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1027 		vdev_t *vd =
1028 		    vdev_lookup_top(spa, DVA_GET_VDEV(&bp->blk_dva[i]));
1029 		sz += (DVA_GET_ASIZE(&bp->blk_dva[i]) >> SPA_MINBLOCKSHIFT) *
1030 		    vd->vdev_deflate_ratio;
1031 	}
1032 	return (sz);
1033 }
1034 
1035 /*
1036  * ==========================================================================
1037  * Initialization and Termination
1038  * ==========================================================================
1039  */
1040 
1041 static int
1042 spa_name_compare(const void *a1, const void *a2)
1043 {
1044 	const spa_t *s1 = a1;
1045 	const spa_t *s2 = a2;
1046 	int s;
1047 
1048 	s = strcmp(s1->spa_name, s2->spa_name);
1049 	if (s > 0)
1050 		return (1);
1051 	if (s < 0)
1052 		return (-1);
1053 	return (0);
1054 }
1055 
1056 int
1057 spa_busy(void)
1058 {
1059 	return (spa_active_count);
1060 }
1061 
1062 void
1063 spa_init(int mode)
1064 {
1065 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1066 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1067 
1068 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1069 	    offsetof(spa_t, spa_avl));
1070 
1071 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_spare_t),
1072 	    offsetof(spa_spare_t, spare_avl));
1073 
1074 	spa_mode = mode;
1075 
1076 	refcount_init();
1077 	unique_init();
1078 	zio_init();
1079 	dmu_init();
1080 	zil_init();
1081 	spa_config_load();
1082 }
1083 
1084 void
1085 spa_fini(void)
1086 {
1087 	spa_evict_all();
1088 
1089 	zil_fini();
1090 	dmu_fini();
1091 	zio_fini();
1092 	refcount_fini();
1093 
1094 	avl_destroy(&spa_namespace_avl);
1095 	avl_destroy(&spa_spare_avl);
1096 
1097 	cv_destroy(&spa_namespace_cv);
1098 	mutex_destroy(&spa_namespace_lock);
1099 }
1100