xref: /titanic_51/usr/src/uts/common/fs/zfs/spa_misc.c (revision f0b62587229842fad8c5df20795bf9bca17327bd)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/spa_impl.h>
28 #include <sys/zio.h>
29 #include <sys/zio_checksum.h>
30 #include <sys/zio_compress.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/zap.h>
34 #include <sys/zil.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/metaslab.h>
37 #include <sys/uberblock_impl.h>
38 #include <sys/txg.h>
39 #include <sys/avl.h>
40 #include <sys/unique.h>
41 #include <sys/dsl_pool.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/arc.h>
47 #include <sys/ddt.h>
48 #include "zfs_prop.h"
49 
50 /*
51  * SPA locking
52  *
53  * There are four basic locks for managing spa_t structures:
54  *
55  * spa_namespace_lock (global mutex)
56  *
57  *	This lock must be acquired to do any of the following:
58  *
59  *		- Lookup a spa_t by name
60  *		- Add or remove a spa_t from the namespace
61  *		- Increase spa_refcount from non-zero
62  *		- Check if spa_refcount is zero
63  *		- Rename a spa_t
64  *		- add/remove/attach/detach devices
65  *		- Held for the duration of create/destroy/import/export
66  *
67  *	It does not need to handle recursion.  A create or destroy may
68  *	reference objects (files or zvols) in other pools, but by
69  *	definition they must have an existing reference, and will never need
70  *	to lookup a spa_t by name.
71  *
72  * spa_refcount (per-spa refcount_t protected by mutex)
73  *
74  *	This reference count keep track of any active users of the spa_t.  The
75  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
76  *	the refcount is never really 'zero' - opening a pool implicitly keeps
77  *	some references in the DMU.  Internally we check against spa_minref, but
78  *	present the image of a zero/non-zero value to consumers.
79  *
80  * spa_config_lock[] (per-spa array of rwlocks)
81  *
82  *	This protects the spa_t from config changes, and must be held in
83  *	the following circumstances:
84  *
85  *		- RW_READER to perform I/O to the spa
86  *		- RW_WRITER to change the vdev config
87  *
88  * The locking order is fairly straightforward:
89  *
90  *		spa_namespace_lock	->	spa_refcount
91  *
92  *	The namespace lock must be acquired to increase the refcount from 0
93  *	or to check if it is zero.
94  *
95  *		spa_refcount		->	spa_config_lock[]
96  *
97  *	There must be at least one valid reference on the spa_t to acquire
98  *	the config lock.
99  *
100  *		spa_namespace_lock	->	spa_config_lock[]
101  *
102  *	The namespace lock must always be taken before the config lock.
103  *
104  *
105  * The spa_namespace_lock can be acquired directly and is globally visible.
106  *
107  * The namespace is manipulated using the following functions, all of which
108  * require the spa_namespace_lock to be held.
109  *
110  *	spa_lookup()		Lookup a spa_t by name.
111  *
112  *	spa_add()		Create a new spa_t in the namespace.
113  *
114  *	spa_remove()		Remove a spa_t from the namespace.  This also
115  *				frees up any memory associated with the spa_t.
116  *
117  *	spa_next()		Returns the next spa_t in the system, or the
118  *				first if NULL is passed.
119  *
120  *	spa_evict_all()		Shutdown and remove all spa_t structures in
121  *				the system.
122  *
123  *	spa_guid_exists()	Determine whether a pool/device guid exists.
124  *
125  * The spa_refcount is manipulated using the following functions:
126  *
127  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
128  *				called with spa_namespace_lock held if the
129  *				refcount is currently zero.
130  *
131  *	spa_close()		Remove a reference from the spa_t.  This will
132  *				not free the spa_t or remove it from the
133  *				namespace.  No locking is required.
134  *
135  *	spa_refcount_zero()	Returns true if the refcount is currently
136  *				zero.  Must be called with spa_namespace_lock
137  *				held.
138  *
139  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
140  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
141  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
142  *
143  * To read the configuration, it suffices to hold one of these locks as reader.
144  * To modify the configuration, you must hold all locks as writer.  To modify
145  * vdev state without altering the vdev tree's topology (e.g. online/offline),
146  * you must hold SCL_STATE and SCL_ZIO as writer.
147  *
148  * We use these distinct config locks to avoid recursive lock entry.
149  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
150  * block allocations (SCL_ALLOC), which may require reading space maps
151  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
152  *
153  * The spa config locks cannot be normal rwlocks because we need the
154  * ability to hand off ownership.  For example, SCL_ZIO is acquired
155  * by the issuing thread and later released by an interrupt thread.
156  * They do, however, obey the usual write-wanted semantics to prevent
157  * writer (i.e. system administrator) starvation.
158  *
159  * The lock acquisition rules are as follows:
160  *
161  * SCL_CONFIG
162  *	Protects changes to the vdev tree topology, such as vdev
163  *	add/remove/attach/detach.  Protects the dirty config list
164  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
165  *
166  * SCL_STATE
167  *	Protects changes to pool state and vdev state, such as vdev
168  *	online/offline/fault/degrade/clear.  Protects the dirty state list
169  *	(spa_state_dirty_list) and global pool state (spa_state).
170  *
171  * SCL_ALLOC
172  *	Protects changes to metaslab groups and classes.
173  *	Held as reader by metaslab_alloc() and metaslab_claim().
174  *
175  * SCL_ZIO
176  *	Held by bp-level zios (those which have no io_vd upon entry)
177  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
178  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
179  *
180  * SCL_FREE
181  *	Protects changes to metaslab groups and classes.
182  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
183  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
184  *	blocks in zio_done() while another i/o that holds either
185  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
186  *
187  * SCL_VDEV
188  *	Held as reader to prevent changes to the vdev tree during trivial
189  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
190  *	other locks, and lower than all of them, to ensure that it's safe
191  *	to acquire regardless of caller context.
192  *
193  * In addition, the following rules apply:
194  *
195  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
196  *	The lock ordering is SCL_CONFIG > spa_props_lock.
197  *
198  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
199  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
200  *	or zio_write_phys() -- the caller must ensure that the config cannot
201  *	cannot change in the interim, and that the vdev cannot be reopened.
202  *	SCL_STATE as reader suffices for both.
203  *
204  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
205  *
206  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
207  *				for writing.
208  *
209  *	spa_vdev_exit()		Release the config lock, wait for all I/O
210  *				to complete, sync the updated configs to the
211  *				cache, and release the namespace lock.
212  *
213  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
214  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
215  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
216  *
217  * spa_rename() is also implemented within this file since is requires
218  * manipulation of the namespace.
219  */
220 
221 static avl_tree_t spa_namespace_avl;
222 kmutex_t spa_namespace_lock;
223 static kcondvar_t spa_namespace_cv;
224 static int spa_active_count;
225 int spa_max_replication_override = SPA_DVAS_PER_BP;
226 
227 static kmutex_t spa_spare_lock;
228 static avl_tree_t spa_spare_avl;
229 static kmutex_t spa_l2cache_lock;
230 static avl_tree_t spa_l2cache_avl;
231 
232 kmem_cache_t *spa_buffer_pool;
233 int spa_mode_global;
234 
235 #ifdef ZFS_DEBUG
236 /* Everything except dprintf is on by default in debug builds */
237 int zfs_flags = ~ZFS_DEBUG_DPRINTF;
238 #else
239 int zfs_flags = 0;
240 #endif
241 
242 /*
243  * zfs_recover can be set to nonzero to attempt to recover from
244  * otherwise-fatal errors, typically caused by on-disk corruption.  When
245  * set, calls to zfs_panic_recover() will turn into warning messages.
246  */
247 int zfs_recover = 0;
248 
249 
250 /*
251  * ==========================================================================
252  * SPA config locking
253  * ==========================================================================
254  */
255 static void
256 spa_config_lock_init(spa_t *spa)
257 {
258 	for (int i = 0; i < SCL_LOCKS; i++) {
259 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
260 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
261 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
262 		refcount_create(&scl->scl_count);
263 		scl->scl_writer = NULL;
264 		scl->scl_write_wanted = 0;
265 	}
266 }
267 
268 static void
269 spa_config_lock_destroy(spa_t *spa)
270 {
271 	for (int i = 0; i < SCL_LOCKS; i++) {
272 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
273 		mutex_destroy(&scl->scl_lock);
274 		cv_destroy(&scl->scl_cv);
275 		refcount_destroy(&scl->scl_count);
276 		ASSERT(scl->scl_writer == NULL);
277 		ASSERT(scl->scl_write_wanted == 0);
278 	}
279 }
280 
281 int
282 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
283 {
284 	for (int i = 0; i < SCL_LOCKS; i++) {
285 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
286 		if (!(locks & (1 << i)))
287 			continue;
288 		mutex_enter(&scl->scl_lock);
289 		if (rw == RW_READER) {
290 			if (scl->scl_writer || scl->scl_write_wanted) {
291 				mutex_exit(&scl->scl_lock);
292 				spa_config_exit(spa, locks ^ (1 << i), tag);
293 				return (0);
294 			}
295 		} else {
296 			ASSERT(scl->scl_writer != curthread);
297 			if (!refcount_is_zero(&scl->scl_count)) {
298 				mutex_exit(&scl->scl_lock);
299 				spa_config_exit(spa, locks ^ (1 << i), tag);
300 				return (0);
301 			}
302 			scl->scl_writer = curthread;
303 		}
304 		(void) refcount_add(&scl->scl_count, tag);
305 		mutex_exit(&scl->scl_lock);
306 	}
307 	return (1);
308 }
309 
310 void
311 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
312 {
313 	int wlocks_held = 0;
314 
315 	for (int i = 0; i < SCL_LOCKS; i++) {
316 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
317 		if (scl->scl_writer == curthread)
318 			wlocks_held |= (1 << i);
319 		if (!(locks & (1 << i)))
320 			continue;
321 		mutex_enter(&scl->scl_lock);
322 		if (rw == RW_READER) {
323 			while (scl->scl_writer || scl->scl_write_wanted) {
324 				cv_wait(&scl->scl_cv, &scl->scl_lock);
325 			}
326 		} else {
327 			ASSERT(scl->scl_writer != curthread);
328 			while (!refcount_is_zero(&scl->scl_count)) {
329 				scl->scl_write_wanted++;
330 				cv_wait(&scl->scl_cv, &scl->scl_lock);
331 				scl->scl_write_wanted--;
332 			}
333 			scl->scl_writer = curthread;
334 		}
335 		(void) refcount_add(&scl->scl_count, tag);
336 		mutex_exit(&scl->scl_lock);
337 	}
338 	ASSERT(wlocks_held <= locks);
339 }
340 
341 void
342 spa_config_exit(spa_t *spa, int locks, void *tag)
343 {
344 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
345 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
346 		if (!(locks & (1 << i)))
347 			continue;
348 		mutex_enter(&scl->scl_lock);
349 		ASSERT(!refcount_is_zero(&scl->scl_count));
350 		if (refcount_remove(&scl->scl_count, tag) == 0) {
351 			ASSERT(scl->scl_writer == NULL ||
352 			    scl->scl_writer == curthread);
353 			scl->scl_writer = NULL;	/* OK in either case */
354 			cv_broadcast(&scl->scl_cv);
355 		}
356 		mutex_exit(&scl->scl_lock);
357 	}
358 }
359 
360 int
361 spa_config_held(spa_t *spa, int locks, krw_t rw)
362 {
363 	int locks_held = 0;
364 
365 	for (int i = 0; i < SCL_LOCKS; i++) {
366 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
367 		if (!(locks & (1 << i)))
368 			continue;
369 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
370 		    (rw == RW_WRITER && scl->scl_writer == curthread))
371 			locks_held |= 1 << i;
372 	}
373 
374 	return (locks_held);
375 }
376 
377 /*
378  * ==========================================================================
379  * SPA namespace functions
380  * ==========================================================================
381  */
382 
383 /*
384  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
385  * Returns NULL if no matching spa_t is found.
386  */
387 spa_t *
388 spa_lookup(const char *name)
389 {
390 	static spa_t search;	/* spa_t is large; don't allocate on stack */
391 	spa_t *spa;
392 	avl_index_t where;
393 	char c;
394 	char *cp;
395 
396 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
397 
398 	/*
399 	 * If it's a full dataset name, figure out the pool name and
400 	 * just use that.
401 	 */
402 	cp = strpbrk(name, "/@");
403 	if (cp) {
404 		c = *cp;
405 		*cp = '\0';
406 	}
407 
408 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
409 	spa = avl_find(&spa_namespace_avl, &search, &where);
410 
411 	if (cp)
412 		*cp = c;
413 
414 	return (spa);
415 }
416 
417 /*
418  * Create an uninitialized spa_t with the given name.  Requires
419  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
420  * exist by calling spa_lookup() first.
421  */
422 spa_t *
423 spa_add(const char *name, nvlist_t *config, const char *altroot)
424 {
425 	spa_t *spa;
426 	spa_config_dirent_t *dp;
427 
428 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
429 
430 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
431 
432 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
433 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
434 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
435 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
436 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
437 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
438 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
439 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
440 
441 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
442 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
443 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
444 
445 	for (int t = 0; t < TXG_SIZE; t++)
446 		bplist_init(&spa->spa_free_bplist[t]);
447 	bplist_init(&spa->spa_deferred_bplist);
448 
449 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
450 	spa->spa_state = POOL_STATE_UNINITIALIZED;
451 	spa->spa_freeze_txg = UINT64_MAX;
452 	spa->spa_final_txg = UINT64_MAX;
453 	spa->spa_load_max_txg = UINT64_MAX;
454 
455 	refcount_create(&spa->spa_refcount);
456 	spa_config_lock_init(spa);
457 
458 	avl_add(&spa_namespace_avl, spa);
459 
460 	/*
461 	 * Set the alternate root, if there is one.
462 	 */
463 	if (altroot) {
464 		spa->spa_root = spa_strdup(altroot);
465 		spa_active_count++;
466 	}
467 
468 	/*
469 	 * Every pool starts with the default cachefile
470 	 */
471 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
472 	    offsetof(spa_config_dirent_t, scd_link));
473 
474 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
475 	dp->scd_path = spa_strdup(spa_config_path);
476 	list_insert_head(&spa->spa_config_list, dp);
477 
478 	if (config != NULL)
479 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
480 
481 	return (spa);
482 }
483 
484 /*
485  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
486  * spa_namespace_lock.  This is called only after the spa_t has been closed and
487  * deactivated.
488  */
489 void
490 spa_remove(spa_t *spa)
491 {
492 	spa_config_dirent_t *dp;
493 
494 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
495 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
496 
497 	avl_remove(&spa_namespace_avl, spa);
498 	cv_broadcast(&spa_namespace_cv);
499 
500 	if (spa->spa_root) {
501 		spa_strfree(spa->spa_root);
502 		spa_active_count--;
503 	}
504 
505 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
506 		list_remove(&spa->spa_config_list, dp);
507 		if (dp->scd_path != NULL)
508 			spa_strfree(dp->scd_path);
509 		kmem_free(dp, sizeof (spa_config_dirent_t));
510 	}
511 
512 	list_destroy(&spa->spa_config_list);
513 
514 	spa_config_set(spa, NULL);
515 
516 	refcount_destroy(&spa->spa_refcount);
517 
518 	spa_config_lock_destroy(spa);
519 
520 	for (int t = 0; t < TXG_SIZE; t++)
521 		bplist_fini(&spa->spa_free_bplist[t]);
522 	bplist_fini(&spa->spa_deferred_bplist);
523 
524 	cv_destroy(&spa->spa_async_cv);
525 	cv_destroy(&spa->spa_scrub_io_cv);
526 	cv_destroy(&spa->spa_suspend_cv);
527 
528 	mutex_destroy(&spa->spa_async_lock);
529 	mutex_destroy(&spa->spa_scrub_lock);
530 	mutex_destroy(&spa->spa_errlog_lock);
531 	mutex_destroy(&spa->spa_errlist_lock);
532 	mutex_destroy(&spa->spa_history_lock);
533 	mutex_destroy(&spa->spa_props_lock);
534 	mutex_destroy(&spa->spa_suspend_lock);
535 	mutex_destroy(&spa->spa_vdev_top_lock);
536 
537 	kmem_free(spa, sizeof (spa_t));
538 }
539 
540 /*
541  * Given a pool, return the next pool in the namespace, or NULL if there is
542  * none.  If 'prev' is NULL, return the first pool.
543  */
544 spa_t *
545 spa_next(spa_t *prev)
546 {
547 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
548 
549 	if (prev)
550 		return (AVL_NEXT(&spa_namespace_avl, prev));
551 	else
552 		return (avl_first(&spa_namespace_avl));
553 }
554 
555 /*
556  * ==========================================================================
557  * SPA refcount functions
558  * ==========================================================================
559  */
560 
561 /*
562  * Add a reference to the given spa_t.  Must have at least one reference, or
563  * have the namespace lock held.
564  */
565 void
566 spa_open_ref(spa_t *spa, void *tag)
567 {
568 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
569 	    MUTEX_HELD(&spa_namespace_lock));
570 	(void) refcount_add(&spa->spa_refcount, tag);
571 }
572 
573 /*
574  * Remove a reference to the given spa_t.  Must have at least one reference, or
575  * have the namespace lock held.
576  */
577 void
578 spa_close(spa_t *spa, void *tag)
579 {
580 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
581 	    MUTEX_HELD(&spa_namespace_lock));
582 	(void) refcount_remove(&spa->spa_refcount, tag);
583 }
584 
585 /*
586  * Check to see if the spa refcount is zero.  Must be called with
587  * spa_namespace_lock held.  We really compare against spa_minref, which is the
588  * number of references acquired when opening a pool
589  */
590 boolean_t
591 spa_refcount_zero(spa_t *spa)
592 {
593 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
594 
595 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
596 }
597 
598 /*
599  * ==========================================================================
600  * SPA spare and l2cache tracking
601  * ==========================================================================
602  */
603 
604 /*
605  * Hot spares and cache devices are tracked using the same code below,
606  * for 'auxiliary' devices.
607  */
608 
609 typedef struct spa_aux {
610 	uint64_t	aux_guid;
611 	uint64_t	aux_pool;
612 	avl_node_t	aux_avl;
613 	int		aux_count;
614 } spa_aux_t;
615 
616 static int
617 spa_aux_compare(const void *a, const void *b)
618 {
619 	const spa_aux_t *sa = a;
620 	const spa_aux_t *sb = b;
621 
622 	if (sa->aux_guid < sb->aux_guid)
623 		return (-1);
624 	else if (sa->aux_guid > sb->aux_guid)
625 		return (1);
626 	else
627 		return (0);
628 }
629 
630 void
631 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
632 {
633 	avl_index_t where;
634 	spa_aux_t search;
635 	spa_aux_t *aux;
636 
637 	search.aux_guid = vd->vdev_guid;
638 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
639 		aux->aux_count++;
640 	} else {
641 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
642 		aux->aux_guid = vd->vdev_guid;
643 		aux->aux_count = 1;
644 		avl_insert(avl, aux, where);
645 	}
646 }
647 
648 void
649 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
650 {
651 	spa_aux_t search;
652 	spa_aux_t *aux;
653 	avl_index_t where;
654 
655 	search.aux_guid = vd->vdev_guid;
656 	aux = avl_find(avl, &search, &where);
657 
658 	ASSERT(aux != NULL);
659 
660 	if (--aux->aux_count == 0) {
661 		avl_remove(avl, aux);
662 		kmem_free(aux, sizeof (spa_aux_t));
663 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
664 		aux->aux_pool = 0ULL;
665 	}
666 }
667 
668 boolean_t
669 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
670 {
671 	spa_aux_t search, *found;
672 
673 	search.aux_guid = guid;
674 	found = avl_find(avl, &search, NULL);
675 
676 	if (pool) {
677 		if (found)
678 			*pool = found->aux_pool;
679 		else
680 			*pool = 0ULL;
681 	}
682 
683 	if (refcnt) {
684 		if (found)
685 			*refcnt = found->aux_count;
686 		else
687 			*refcnt = 0;
688 	}
689 
690 	return (found != NULL);
691 }
692 
693 void
694 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
695 {
696 	spa_aux_t search, *found;
697 	avl_index_t where;
698 
699 	search.aux_guid = vd->vdev_guid;
700 	found = avl_find(avl, &search, &where);
701 	ASSERT(found != NULL);
702 	ASSERT(found->aux_pool == 0ULL);
703 
704 	found->aux_pool = spa_guid(vd->vdev_spa);
705 }
706 
707 /*
708  * Spares are tracked globally due to the following constraints:
709  *
710  * 	- A spare may be part of multiple pools.
711  * 	- A spare may be added to a pool even if it's actively in use within
712  *	  another pool.
713  * 	- A spare in use in any pool can only be the source of a replacement if
714  *	  the target is a spare in the same pool.
715  *
716  * We keep track of all spares on the system through the use of a reference
717  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
718  * spare, then we bump the reference count in the AVL tree.  In addition, we set
719  * the 'vdev_isspare' member to indicate that the device is a spare (active or
720  * inactive).  When a spare is made active (used to replace a device in the
721  * pool), we also keep track of which pool its been made a part of.
722  *
723  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
724  * called under the spa_namespace lock as part of vdev reconfiguration.  The
725  * separate spare lock exists for the status query path, which does not need to
726  * be completely consistent with respect to other vdev configuration changes.
727  */
728 
729 static int
730 spa_spare_compare(const void *a, const void *b)
731 {
732 	return (spa_aux_compare(a, b));
733 }
734 
735 void
736 spa_spare_add(vdev_t *vd)
737 {
738 	mutex_enter(&spa_spare_lock);
739 	ASSERT(!vd->vdev_isspare);
740 	spa_aux_add(vd, &spa_spare_avl);
741 	vd->vdev_isspare = B_TRUE;
742 	mutex_exit(&spa_spare_lock);
743 }
744 
745 void
746 spa_spare_remove(vdev_t *vd)
747 {
748 	mutex_enter(&spa_spare_lock);
749 	ASSERT(vd->vdev_isspare);
750 	spa_aux_remove(vd, &spa_spare_avl);
751 	vd->vdev_isspare = B_FALSE;
752 	mutex_exit(&spa_spare_lock);
753 }
754 
755 boolean_t
756 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
757 {
758 	boolean_t found;
759 
760 	mutex_enter(&spa_spare_lock);
761 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
762 	mutex_exit(&spa_spare_lock);
763 
764 	return (found);
765 }
766 
767 void
768 spa_spare_activate(vdev_t *vd)
769 {
770 	mutex_enter(&spa_spare_lock);
771 	ASSERT(vd->vdev_isspare);
772 	spa_aux_activate(vd, &spa_spare_avl);
773 	mutex_exit(&spa_spare_lock);
774 }
775 
776 /*
777  * Level 2 ARC devices are tracked globally for the same reasons as spares.
778  * Cache devices currently only support one pool per cache device, and so
779  * for these devices the aux reference count is currently unused beyond 1.
780  */
781 
782 static int
783 spa_l2cache_compare(const void *a, const void *b)
784 {
785 	return (spa_aux_compare(a, b));
786 }
787 
788 void
789 spa_l2cache_add(vdev_t *vd)
790 {
791 	mutex_enter(&spa_l2cache_lock);
792 	ASSERT(!vd->vdev_isl2cache);
793 	spa_aux_add(vd, &spa_l2cache_avl);
794 	vd->vdev_isl2cache = B_TRUE;
795 	mutex_exit(&spa_l2cache_lock);
796 }
797 
798 void
799 spa_l2cache_remove(vdev_t *vd)
800 {
801 	mutex_enter(&spa_l2cache_lock);
802 	ASSERT(vd->vdev_isl2cache);
803 	spa_aux_remove(vd, &spa_l2cache_avl);
804 	vd->vdev_isl2cache = B_FALSE;
805 	mutex_exit(&spa_l2cache_lock);
806 }
807 
808 boolean_t
809 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
810 {
811 	boolean_t found;
812 
813 	mutex_enter(&spa_l2cache_lock);
814 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
815 	mutex_exit(&spa_l2cache_lock);
816 
817 	return (found);
818 }
819 
820 void
821 spa_l2cache_activate(vdev_t *vd)
822 {
823 	mutex_enter(&spa_l2cache_lock);
824 	ASSERT(vd->vdev_isl2cache);
825 	spa_aux_activate(vd, &spa_l2cache_avl);
826 	mutex_exit(&spa_l2cache_lock);
827 }
828 
829 /*
830  * ==========================================================================
831  * SPA vdev locking
832  * ==========================================================================
833  */
834 
835 /*
836  * Lock the given spa_t for the purpose of adding or removing a vdev.
837  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
838  * It returns the next transaction group for the spa_t.
839  */
840 uint64_t
841 spa_vdev_enter(spa_t *spa)
842 {
843 	mutex_enter(&spa->spa_vdev_top_lock);
844 	mutex_enter(&spa_namespace_lock);
845 	return (spa_vdev_config_enter(spa));
846 }
847 
848 /*
849  * Internal implementation for spa_vdev_enter().  Used when a vdev
850  * operation requires multiple syncs (i.e. removing a device) while
851  * keeping the spa_namespace_lock held.
852  */
853 uint64_t
854 spa_vdev_config_enter(spa_t *spa)
855 {
856 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
857 
858 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
859 
860 	return (spa_last_synced_txg(spa) + 1);
861 }
862 
863 /*
864  * Used in combination with spa_vdev_config_enter() to allow the syncing
865  * of multiple transactions without releasing the spa_namespace_lock.
866  */
867 void
868 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
869 {
870 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
871 
872 	int config_changed = B_FALSE;
873 
874 	ASSERT(txg > spa_last_synced_txg(spa));
875 
876 	spa->spa_pending_vdev = NULL;
877 
878 	/*
879 	 * Reassess the DTLs.
880 	 */
881 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
882 
883 	/*
884 	 * If the config changed, notify the scrub thread that it must restart.
885 	 */
886 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
887 		dsl_pool_scrub_restart(spa->spa_dsl_pool);
888 		config_changed = B_TRUE;
889 		spa->spa_config_generation++;
890 	}
891 
892 	/*
893 	 * Verify the metaslab classes.
894 	 */
895 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
896 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
897 
898 	spa_config_exit(spa, SCL_ALL, spa);
899 
900 	/*
901 	 * Panic the system if the specified tag requires it.  This
902 	 * is useful for ensuring that configurations are updated
903 	 * transactionally.
904 	 */
905 	if (zio_injection_enabled)
906 		zio_handle_panic_injection(spa, tag);
907 
908 	/*
909 	 * Note: this txg_wait_synced() is important because it ensures
910 	 * that there won't be more than one config change per txg.
911 	 * This allows us to use the txg as the generation number.
912 	 */
913 	if (error == 0)
914 		txg_wait_synced(spa->spa_dsl_pool, txg);
915 
916 	if (vd != NULL) {
917 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
918 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
919 		vdev_free(vd);
920 		spa_config_exit(spa, SCL_ALL, spa);
921 	}
922 
923 	/*
924 	 * If the config changed, update the config cache.
925 	 */
926 	if (config_changed)
927 		spa_config_sync(spa, B_FALSE, B_TRUE);
928 }
929 
930 /*
931  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
932  * locking of spa_vdev_enter(), we also want make sure the transactions have
933  * synced to disk, and then update the global configuration cache with the new
934  * information.
935  */
936 int
937 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
938 {
939 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
940 	mutex_exit(&spa_namespace_lock);
941 	mutex_exit(&spa->spa_vdev_top_lock);
942 
943 	return (error);
944 }
945 
946 /*
947  * Lock the given spa_t for the purpose of changing vdev state.
948  */
949 void
950 spa_vdev_state_enter(spa_t *spa, int oplocks)
951 {
952 	int locks = SCL_STATE_ALL | oplocks;
953 
954 	spa_config_enter(spa, locks, spa, RW_WRITER);
955 	spa->spa_vdev_locks = locks;
956 }
957 
958 int
959 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
960 {
961 	if (vd != NULL || error == 0)
962 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
963 		    0, 0, B_FALSE);
964 
965 	if (vd != NULL) {
966 		vdev_state_dirty(vd->vdev_top);
967 		spa->spa_config_generation++;
968 	}
969 
970 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
971 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
972 
973 	/*
974 	 * If anything changed, wait for it to sync.  This ensures that,
975 	 * from the system administrator's perspective, zpool(1M) commands
976 	 * are synchronous.  This is important for things like zpool offline:
977 	 * when the command completes, you expect no further I/O from ZFS.
978 	 */
979 	if (vd != NULL)
980 		txg_wait_synced(spa->spa_dsl_pool, 0);
981 
982 	return (error);
983 }
984 
985 /*
986  * ==========================================================================
987  * Miscellaneous functions
988  * ==========================================================================
989  */
990 
991 /*
992  * Rename a spa_t.
993  */
994 int
995 spa_rename(const char *name, const char *newname)
996 {
997 	spa_t *spa;
998 	int err;
999 
1000 	/*
1001 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1002 	 * actually open the pool so that we can sync out the necessary labels.
1003 	 * It's OK to call spa_open() with the namespace lock held because we
1004 	 * allow recursive calls for other reasons.
1005 	 */
1006 	mutex_enter(&spa_namespace_lock);
1007 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1008 		mutex_exit(&spa_namespace_lock);
1009 		return (err);
1010 	}
1011 
1012 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1013 
1014 	avl_remove(&spa_namespace_avl, spa);
1015 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1016 	avl_add(&spa_namespace_avl, spa);
1017 
1018 	/*
1019 	 * Sync all labels to disk with the new names by marking the root vdev
1020 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1021 	 * during the sync.
1022 	 */
1023 	vdev_config_dirty(spa->spa_root_vdev);
1024 
1025 	spa_config_exit(spa, SCL_ALL, FTAG);
1026 
1027 	txg_wait_synced(spa->spa_dsl_pool, 0);
1028 
1029 	/*
1030 	 * Sync the updated config cache.
1031 	 */
1032 	spa_config_sync(spa, B_FALSE, B_TRUE);
1033 
1034 	spa_close(spa, FTAG);
1035 
1036 	mutex_exit(&spa_namespace_lock);
1037 
1038 	return (0);
1039 }
1040 
1041 
1042 /*
1043  * Determine whether a pool with given pool_guid exists.  If device_guid is
1044  * non-zero, determine whether the pool exists *and* contains a device with the
1045  * specified device_guid.
1046  */
1047 boolean_t
1048 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1049 {
1050 	spa_t *spa;
1051 	avl_tree_t *t = &spa_namespace_avl;
1052 
1053 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1054 
1055 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1056 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1057 			continue;
1058 		if (spa->spa_root_vdev == NULL)
1059 			continue;
1060 		if (spa_guid(spa) == pool_guid) {
1061 			if (device_guid == 0)
1062 				break;
1063 
1064 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
1065 			    device_guid) != NULL)
1066 				break;
1067 
1068 			/*
1069 			 * Check any devices we may be in the process of adding.
1070 			 */
1071 			if (spa->spa_pending_vdev) {
1072 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1073 				    device_guid) != NULL)
1074 					break;
1075 			}
1076 		}
1077 	}
1078 
1079 	return (spa != NULL);
1080 }
1081 
1082 char *
1083 spa_strdup(const char *s)
1084 {
1085 	size_t len;
1086 	char *new;
1087 
1088 	len = strlen(s);
1089 	new = kmem_alloc(len + 1, KM_SLEEP);
1090 	bcopy(s, new, len);
1091 	new[len] = '\0';
1092 
1093 	return (new);
1094 }
1095 
1096 void
1097 spa_strfree(char *s)
1098 {
1099 	kmem_free(s, strlen(s) + 1);
1100 }
1101 
1102 uint64_t
1103 spa_get_random(uint64_t range)
1104 {
1105 	uint64_t r;
1106 
1107 	ASSERT(range != 0);
1108 
1109 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1110 
1111 	return (r % range);
1112 }
1113 
1114 void
1115 sprintf_blkptr(char *buf, const blkptr_t *bp)
1116 {
1117 	char *type = dmu_ot[BP_GET_TYPE(bp)].ot_name;
1118 	char *checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1119 	char *compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1120 
1121 	SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1122 }
1123 
1124 void
1125 spa_freeze(spa_t *spa)
1126 {
1127 	uint64_t freeze_txg = 0;
1128 
1129 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1130 	if (spa->spa_freeze_txg == UINT64_MAX) {
1131 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1132 		spa->spa_freeze_txg = freeze_txg;
1133 	}
1134 	spa_config_exit(spa, SCL_ALL, FTAG);
1135 	if (freeze_txg != 0)
1136 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1137 }
1138 
1139 void
1140 zfs_panic_recover(const char *fmt, ...)
1141 {
1142 	va_list adx;
1143 
1144 	va_start(adx, fmt);
1145 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1146 	va_end(adx);
1147 }
1148 
1149 /*
1150  * ==========================================================================
1151  * Accessor functions
1152  * ==========================================================================
1153  */
1154 
1155 boolean_t
1156 spa_shutting_down(spa_t *spa)
1157 {
1158 	return (spa->spa_async_suspended);
1159 }
1160 
1161 dsl_pool_t *
1162 spa_get_dsl(spa_t *spa)
1163 {
1164 	return (spa->spa_dsl_pool);
1165 }
1166 
1167 blkptr_t *
1168 spa_get_rootblkptr(spa_t *spa)
1169 {
1170 	return (&spa->spa_ubsync.ub_rootbp);
1171 }
1172 
1173 void
1174 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1175 {
1176 	spa->spa_uberblock.ub_rootbp = *bp;
1177 }
1178 
1179 void
1180 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1181 {
1182 	if (spa->spa_root == NULL)
1183 		buf[0] = '\0';
1184 	else
1185 		(void) strncpy(buf, spa->spa_root, buflen);
1186 }
1187 
1188 int
1189 spa_sync_pass(spa_t *spa)
1190 {
1191 	return (spa->spa_sync_pass);
1192 }
1193 
1194 char *
1195 spa_name(spa_t *spa)
1196 {
1197 	return (spa->spa_name);
1198 }
1199 
1200 uint64_t
1201 spa_guid(spa_t *spa)
1202 {
1203 	/*
1204 	 * If we fail to parse the config during spa_load(), we can go through
1205 	 * the error path (which posts an ereport) and end up here with no root
1206 	 * vdev.  We stash the original pool guid in 'spa_load_guid' to handle
1207 	 * this case.
1208 	 */
1209 	if (spa->spa_root_vdev != NULL)
1210 		return (spa->spa_root_vdev->vdev_guid);
1211 	else
1212 		return (spa->spa_load_guid);
1213 }
1214 
1215 uint64_t
1216 spa_last_synced_txg(spa_t *spa)
1217 {
1218 	return (spa->spa_ubsync.ub_txg);
1219 }
1220 
1221 uint64_t
1222 spa_first_txg(spa_t *spa)
1223 {
1224 	return (spa->spa_first_txg);
1225 }
1226 
1227 uint64_t
1228 spa_syncing_txg(spa_t *spa)
1229 {
1230 	return (spa->spa_syncing_txg);
1231 }
1232 
1233 pool_state_t
1234 spa_state(spa_t *spa)
1235 {
1236 	return (spa->spa_state);
1237 }
1238 
1239 spa_load_state_t
1240 spa_load_state(spa_t *spa)
1241 {
1242 	return (spa->spa_load_state);
1243 }
1244 
1245 uint64_t
1246 spa_freeze_txg(spa_t *spa)
1247 {
1248 	return (spa->spa_freeze_txg);
1249 }
1250 
1251 /* ARGSUSED */
1252 uint64_t
1253 spa_get_asize(spa_t *spa, uint64_t lsize)
1254 {
1255 	/*
1256 	 * The worst case is single-sector max-parity RAID-Z blocks, in which
1257 	 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1258 	 * times the size; so just assume that.  Add to this the fact that
1259 	 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1260 	 * the block may be dittoed with up to 3 DVAs by ddt_sync().
1261 	 */
1262 	return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
1263 }
1264 
1265 uint64_t
1266 spa_get_dspace(spa_t *spa)
1267 {
1268 	return (spa->spa_dspace);
1269 }
1270 
1271 void
1272 spa_update_dspace(spa_t *spa)
1273 {
1274 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1275 	    ddt_get_dedup_dspace(spa);
1276 }
1277 
1278 /*
1279  * Return the failure mode that has been set to this pool. The default
1280  * behavior will be to block all I/Os when a complete failure occurs.
1281  */
1282 uint8_t
1283 spa_get_failmode(spa_t *spa)
1284 {
1285 	return (spa->spa_failmode);
1286 }
1287 
1288 boolean_t
1289 spa_suspended(spa_t *spa)
1290 {
1291 	return (spa->spa_suspended);
1292 }
1293 
1294 uint64_t
1295 spa_version(spa_t *spa)
1296 {
1297 	return (spa->spa_ubsync.ub_version);
1298 }
1299 
1300 boolean_t
1301 spa_deflate(spa_t *spa)
1302 {
1303 	return (spa->spa_deflate);
1304 }
1305 
1306 metaslab_class_t *
1307 spa_normal_class(spa_t *spa)
1308 {
1309 	return (spa->spa_normal_class);
1310 }
1311 
1312 metaslab_class_t *
1313 spa_log_class(spa_t *spa)
1314 {
1315 	return (spa->spa_log_class);
1316 }
1317 
1318 int
1319 spa_max_replication(spa_t *spa)
1320 {
1321 	/*
1322 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1323 	 * handle BPs with more than one DVA allocated.  Set our max
1324 	 * replication level accordingly.
1325 	 */
1326 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1327 		return (1);
1328 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1329 }
1330 
1331 uint64_t
1332 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1333 {
1334 	uint64_t asize = DVA_GET_ASIZE(dva);
1335 	uint64_t dsize = asize;
1336 
1337 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1338 
1339 	if (asize != 0 && spa->spa_deflate) {
1340 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1341 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1342 	}
1343 
1344 	return (dsize);
1345 }
1346 
1347 uint64_t
1348 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1349 {
1350 	uint64_t dsize = 0;
1351 
1352 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1353 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1354 
1355 	return (dsize);
1356 }
1357 
1358 uint64_t
1359 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1360 {
1361 	uint64_t dsize = 0;
1362 
1363 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1364 
1365 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1366 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1367 
1368 	spa_config_exit(spa, SCL_VDEV, FTAG);
1369 
1370 	return (dsize);
1371 }
1372 
1373 /*
1374  * ==========================================================================
1375  * Initialization and Termination
1376  * ==========================================================================
1377  */
1378 
1379 static int
1380 spa_name_compare(const void *a1, const void *a2)
1381 {
1382 	const spa_t *s1 = a1;
1383 	const spa_t *s2 = a2;
1384 	int s;
1385 
1386 	s = strcmp(s1->spa_name, s2->spa_name);
1387 	if (s > 0)
1388 		return (1);
1389 	if (s < 0)
1390 		return (-1);
1391 	return (0);
1392 }
1393 
1394 int
1395 spa_busy(void)
1396 {
1397 	return (spa_active_count);
1398 }
1399 
1400 void
1401 spa_boot_init()
1402 {
1403 	spa_config_load();
1404 }
1405 
1406 void
1407 spa_init(int mode)
1408 {
1409 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1410 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1411 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1412 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1413 
1414 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1415 	    offsetof(spa_t, spa_avl));
1416 
1417 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1418 	    offsetof(spa_aux_t, aux_avl));
1419 
1420 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1421 	    offsetof(spa_aux_t, aux_avl));
1422 
1423 	spa_mode_global = mode;
1424 
1425 	refcount_init();
1426 	unique_init();
1427 	zio_init();
1428 	dmu_init();
1429 	zil_init();
1430 	vdev_cache_stat_init();
1431 	zfs_prop_init();
1432 	zpool_prop_init();
1433 	spa_config_load();
1434 	l2arc_start();
1435 }
1436 
1437 void
1438 spa_fini(void)
1439 {
1440 	l2arc_stop();
1441 
1442 	spa_evict_all();
1443 
1444 	vdev_cache_stat_fini();
1445 	zil_fini();
1446 	dmu_fini();
1447 	zio_fini();
1448 	unique_fini();
1449 	refcount_fini();
1450 
1451 	avl_destroy(&spa_namespace_avl);
1452 	avl_destroy(&spa_spare_avl);
1453 	avl_destroy(&spa_l2cache_avl);
1454 
1455 	cv_destroy(&spa_namespace_cv);
1456 	mutex_destroy(&spa_namespace_lock);
1457 	mutex_destroy(&spa_spare_lock);
1458 	mutex_destroy(&spa_l2cache_lock);
1459 }
1460 
1461 /*
1462  * Return whether this pool has slogs. No locking needed.
1463  * It's not a problem if the wrong answer is returned as it's only for
1464  * performance and not correctness
1465  */
1466 boolean_t
1467 spa_has_slogs(spa_t *spa)
1468 {
1469 	return (spa->spa_log_class->mc_rotor != NULL);
1470 }
1471 
1472 spa_log_state_t
1473 spa_get_log_state(spa_t *spa)
1474 {
1475 	return (spa->spa_log_state);
1476 }
1477 
1478 void
1479 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1480 {
1481 	spa->spa_log_state = state;
1482 }
1483 
1484 boolean_t
1485 spa_is_root(spa_t *spa)
1486 {
1487 	return (spa->spa_is_root);
1488 }
1489 
1490 boolean_t
1491 spa_writeable(spa_t *spa)
1492 {
1493 	return (!!(spa->spa_mode & FWRITE));
1494 }
1495 
1496 int
1497 spa_mode(spa_t *spa)
1498 {
1499 	return (spa->spa_mode);
1500 }
1501 
1502 uint64_t
1503 spa_bootfs(spa_t *spa)
1504 {
1505 	return (spa->spa_bootfs);
1506 }
1507 
1508 uint64_t
1509 spa_delegation(spa_t *spa)
1510 {
1511 	return (spa->spa_delegation);
1512 }
1513 
1514 objset_t *
1515 spa_meta_objset(spa_t *spa)
1516 {
1517 	return (spa->spa_meta_objset);
1518 }
1519 
1520 enum zio_checksum
1521 spa_dedup_checksum(spa_t *spa)
1522 {
1523 	return (spa->spa_dedup_checksum);
1524 }
1525