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