xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision bd93c05d)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26  * Copyright 2013 Saso Kiselkov. All rights reserved.
27  */
28 
29 #include <sys/zfs_context.h>
30 #include <sys/spa_impl.h>
31 #include <sys/spa_boot.h>
32 #include <sys/zio.h>
33 #include <sys/zio_checksum.h>
34 #include <sys/zio_compress.h>
35 #include <sys/dmu.h>
36 #include <sys/dmu_tx.h>
37 #include <sys/zap.h>
38 #include <sys/zil.h>
39 #include <sys/vdev_impl.h>
40 #include <sys/metaslab.h>
41 #include <sys/uberblock_impl.h>
42 #include <sys/txg.h>
43 #include <sys/avl.h>
44 #include <sys/unique.h>
45 #include <sys/dsl_pool.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dsl_scan.h>
49 #include <sys/fs/zfs.h>
50 #include <sys/metaslab_impl.h>
51 #include <sys/arc.h>
52 #include <sys/ddt.h>
53 #include "zfs_prop.h"
54 #include <sys/zfeature.h>
55 
56 /*
57  * SPA locking
58  *
59  * There are four basic locks for managing spa_t structures:
60  *
61  * spa_namespace_lock (global mutex)
62  *
63  *	This lock must be acquired to do any of the following:
64  *
65  *		- Lookup a spa_t by name
66  *		- Add or remove a spa_t from the namespace
67  *		- Increase spa_refcount from non-zero
68  *		- Check if spa_refcount is zero
69  *		- Rename a spa_t
70  *		- add/remove/attach/detach devices
71  *		- Held for the duration of create/destroy/import/export
72  *
73  *	It does not need to handle recursion.  A create or destroy may
74  *	reference objects (files or zvols) in other pools, but by
75  *	definition they must have an existing reference, and will never need
76  *	to lookup a spa_t by name.
77  *
78  * spa_refcount (per-spa refcount_t protected by mutex)
79  *
80  *	This reference count keep track of any active users of the spa_t.  The
81  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
82  *	the refcount is never really 'zero' - opening a pool implicitly keeps
83  *	some references in the DMU.  Internally we check against spa_minref, but
84  *	present the image of a zero/non-zero value to consumers.
85  *
86  * spa_config_lock[] (per-spa array of rwlocks)
87  *
88  *	This protects the spa_t from config changes, and must be held in
89  *	the following circumstances:
90  *
91  *		- RW_READER to perform I/O to the spa
92  *		- RW_WRITER to change the vdev config
93  *
94  * The locking order is fairly straightforward:
95  *
96  *		spa_namespace_lock	->	spa_refcount
97  *
98  *	The namespace lock must be acquired to increase the refcount from 0
99  *	or to check if it is zero.
100  *
101  *		spa_refcount		->	spa_config_lock[]
102  *
103  *	There must be at least one valid reference on the spa_t to acquire
104  *	the config lock.
105  *
106  *		spa_namespace_lock	->	spa_config_lock[]
107  *
108  *	The namespace lock must always be taken before the config lock.
109  *
110  *
111  * The spa_namespace_lock can be acquired directly and is globally visible.
112  *
113  * The namespace is manipulated using the following functions, all of which
114  * require the spa_namespace_lock to be held.
115  *
116  *	spa_lookup()		Lookup a spa_t by name.
117  *
118  *	spa_add()		Create a new spa_t in the namespace.
119  *
120  *	spa_remove()		Remove a spa_t from the namespace.  This also
121  *				frees up any memory associated with the spa_t.
122  *
123  *	spa_next()		Returns the next spa_t in the system, or the
124  *				first if NULL is passed.
125  *
126  *	spa_evict_all()		Shutdown and remove all spa_t structures in
127  *				the system.
128  *
129  *	spa_guid_exists()	Determine whether a pool/device guid exists.
130  *
131  * The spa_refcount is manipulated using the following functions:
132  *
133  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
134  *				called with spa_namespace_lock held if the
135  *				refcount is currently zero.
136  *
137  *	spa_close()		Remove a reference from the spa_t.  This will
138  *				not free the spa_t or remove it from the
139  *				namespace.  No locking is required.
140  *
141  *	spa_refcount_zero()	Returns true if the refcount is currently
142  *				zero.  Must be called with spa_namespace_lock
143  *				held.
144  *
145  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
146  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
147  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
148  *
149  * To read the configuration, it suffices to hold one of these locks as reader.
150  * To modify the configuration, you must hold all locks as writer.  To modify
151  * vdev state without altering the vdev tree's topology (e.g. online/offline),
152  * you must hold SCL_STATE and SCL_ZIO as writer.
153  *
154  * We use these distinct config locks to avoid recursive lock entry.
155  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
156  * block allocations (SCL_ALLOC), which may require reading space maps
157  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
158  *
159  * The spa config locks cannot be normal rwlocks because we need the
160  * ability to hand off ownership.  For example, SCL_ZIO is acquired
161  * by the issuing thread and later released by an interrupt thread.
162  * They do, however, obey the usual write-wanted semantics to prevent
163  * writer (i.e. system administrator) starvation.
164  *
165  * The lock acquisition rules are as follows:
166  *
167  * SCL_CONFIG
168  *	Protects changes to the vdev tree topology, such as vdev
169  *	add/remove/attach/detach.  Protects the dirty config list
170  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
171  *
172  * SCL_STATE
173  *	Protects changes to pool state and vdev state, such as vdev
174  *	online/offline/fault/degrade/clear.  Protects the dirty state list
175  *	(spa_state_dirty_list) and global pool state (spa_state).
176  *
177  * SCL_ALLOC
178  *	Protects changes to metaslab groups and classes.
179  *	Held as reader by metaslab_alloc() and metaslab_claim().
180  *
181  * SCL_ZIO
182  *	Held by bp-level zios (those which have no io_vd upon entry)
183  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
184  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
185  *
186  * SCL_FREE
187  *	Protects changes to metaslab groups and classes.
188  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
189  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
190  *	blocks in zio_done() while another i/o that holds either
191  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
192  *
193  * SCL_VDEV
194  *	Held as reader to prevent changes to the vdev tree during trivial
195  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
196  *	other locks, and lower than all of them, to ensure that it's safe
197  *	to acquire regardless of caller context.
198  *
199  * In addition, the following rules apply:
200  *
201  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
202  *	The lock ordering is SCL_CONFIG > spa_props_lock.
203  *
204  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
205  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
206  *	or zio_write_phys() -- the caller must ensure that the config cannot
207  *	cannot change in the interim, and that the vdev cannot be reopened.
208  *	SCL_STATE as reader suffices for both.
209  *
210  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
211  *
212  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
213  *				for writing.
214  *
215  *	spa_vdev_exit()		Release the config lock, wait for all I/O
216  *				to complete, sync the updated configs to the
217  *				cache, and release the namespace lock.
218  *
219  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
220  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
221  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
222  *
223  * spa_rename() is also implemented within this file since it requires
224  * manipulation of the namespace.
225  */
226 
227 static avl_tree_t spa_namespace_avl;
228 kmutex_t spa_namespace_lock;
229 static kcondvar_t spa_namespace_cv;
230 static int spa_active_count;
231 int spa_max_replication_override = SPA_DVAS_PER_BP;
232 
233 static kmutex_t spa_spare_lock;
234 static avl_tree_t spa_spare_avl;
235 static kmutex_t spa_l2cache_lock;
236 static avl_tree_t spa_l2cache_avl;
237 
238 kmem_cache_t *spa_buffer_pool;
239 int spa_mode_global;
240 
241 #ifdef ZFS_DEBUG
242 /* Everything except dprintf and spa is on by default in debug builds */
243 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
244 #else
245 int zfs_flags = 0;
246 #endif
247 
248 /*
249  * zfs_recover can be set to nonzero to attempt to recover from
250  * otherwise-fatal errors, typically caused by on-disk corruption.  When
251  * set, calls to zfs_panic_recover() will turn into warning messages.
252  * This should only be used as a last resort, as it typically results
253  * in leaked space, or worse.
254  */
255 boolean_t zfs_recover = B_FALSE;
256 
257 /*
258  * If destroy encounters an EIO while reading metadata (e.g. indirect
259  * blocks), space referenced by the missing metadata can not be freed.
260  * Normally this causes the background destroy to become "stalled", as
261  * it is unable to make forward progress.  While in this stalled state,
262  * all remaining space to free from the error-encountering filesystem is
263  * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
264  * permanently leak the space from indirect blocks that can not be read,
265  * and continue to free everything else that it can.
266  *
267  * The default, "stalling" behavior is useful if the storage partially
268  * fails (i.e. some but not all i/os fail), and then later recovers.  In
269  * this case, we will be able to continue pool operations while it is
270  * partially failed, and when it recovers, we can continue to free the
271  * space, with no leaks.  However, note that this case is actually
272  * fairly rare.
273  *
274  * Typically pools either (a) fail completely (but perhaps temporarily,
275  * e.g. a top-level vdev going offline), or (b) have localized,
276  * permanent errors (e.g. disk returns the wrong data due to bit flip or
277  * firmware bug).  In case (a), this setting does not matter because the
278  * pool will be suspended and the sync thread will not be able to make
279  * forward progress regardless.  In case (b), because the error is
280  * permanent, the best we can do is leak the minimum amount of space,
281  * which is what setting this flag will do.  Therefore, it is reasonable
282  * for this flag to normally be set, but we chose the more conservative
283  * approach of not setting it, so that there is no possibility of
284  * leaking space in the "partial temporary" failure case.
285  */
286 boolean_t zfs_free_leak_on_eio = B_FALSE;
287 
288 /*
289  * Expiration time in milliseconds. This value has two meanings. First it is
290  * used to determine when the spa_deadman() logic should fire. By default the
291  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
292  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
293  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
294  * in a system panic.
295  */
296 uint64_t zfs_deadman_synctime_ms = 1000000ULL;
297 
298 /*
299  * Check time in milliseconds. This defines the frequency at which we check
300  * for hung I/O.
301  */
302 uint64_t zfs_deadman_checktime_ms = 5000ULL;
303 
304 /*
305  * Override the zfs deadman behavior via /etc/system. By default the
306  * deadman is enabled except on VMware and sparc deployments.
307  */
308 int zfs_deadman_enabled = -1;
309 
310 /*
311  * The worst case is single-sector max-parity RAID-Z blocks, in which
312  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
313  * times the size; so just assume that.  Add to this the fact that
314  * we can have up to 3 DVAs per bp, and one more factor of 2 because
315  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
316  * the worst case is:
317  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
318  */
319 int spa_asize_inflation = 24;
320 
321 /*
322  * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
323  * the pool to be consumed.  This ensures that we don't run the pool
324  * completely out of space, due to unaccounted changes (e.g. to the MOS).
325  * It also limits the worst-case time to allocate space.  If we have
326  * less than this amount of free space, most ZPL operations (e.g. write,
327  * create) will return ENOSPC.
328  *
329  * Certain operations (e.g. file removal, most administrative actions) can
330  * use half the slop space.  They will only return ENOSPC if less than half
331  * the slop space is free.  Typically, once the pool has less than the slop
332  * space free, the user will use these operations to free up space in the pool.
333  * These are the operations that call dsl_pool_adjustedsize() with the netfree
334  * argument set to TRUE.
335  *
336  * A very restricted set of operations are always permitted, regardless of
337  * the amount of free space.  These are the operations that call
338  * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy".  If these
339  * operations result in a net increase in the amount of space used,
340  * it is possible to run the pool completely out of space, causing it to
341  * be permanently read-only.
342  *
343  * See also the comments in zfs_space_check_t.
344  */
345 int spa_slop_shift = 5;
346 
347 /*
348  * ==========================================================================
349  * SPA config locking
350  * ==========================================================================
351  */
352 static void
353 spa_config_lock_init(spa_t *spa)
354 {
355 	for (int i = 0; i < SCL_LOCKS; i++) {
356 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
357 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
358 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
359 		refcount_create_untracked(&scl->scl_count);
360 		scl->scl_writer = NULL;
361 		scl->scl_write_wanted = 0;
362 	}
363 }
364 
365 static void
366 spa_config_lock_destroy(spa_t *spa)
367 {
368 	for (int i = 0; i < SCL_LOCKS; i++) {
369 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
370 		mutex_destroy(&scl->scl_lock);
371 		cv_destroy(&scl->scl_cv);
372 		refcount_destroy(&scl->scl_count);
373 		ASSERT(scl->scl_writer == NULL);
374 		ASSERT(scl->scl_write_wanted == 0);
375 	}
376 }
377 
378 int
379 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
380 {
381 	for (int i = 0; i < SCL_LOCKS; i++) {
382 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
383 		if (!(locks & (1 << i)))
384 			continue;
385 		mutex_enter(&scl->scl_lock);
386 		if (rw == RW_READER) {
387 			if (scl->scl_writer || scl->scl_write_wanted) {
388 				mutex_exit(&scl->scl_lock);
389 				spa_config_exit(spa, locks ^ (1 << i), tag);
390 				return (0);
391 			}
392 		} else {
393 			ASSERT(scl->scl_writer != curthread);
394 			if (!refcount_is_zero(&scl->scl_count)) {
395 				mutex_exit(&scl->scl_lock);
396 				spa_config_exit(spa, locks ^ (1 << i), tag);
397 				return (0);
398 			}
399 			scl->scl_writer = curthread;
400 		}
401 		(void) refcount_add(&scl->scl_count, tag);
402 		mutex_exit(&scl->scl_lock);
403 	}
404 	return (1);
405 }
406 
407 void
408 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
409 {
410 	int wlocks_held = 0;
411 
412 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
413 
414 	for (int i = 0; i < SCL_LOCKS; i++) {
415 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
416 		if (scl->scl_writer == curthread)
417 			wlocks_held |= (1 << i);
418 		if (!(locks & (1 << i)))
419 			continue;
420 		mutex_enter(&scl->scl_lock);
421 		if (rw == RW_READER) {
422 			while (scl->scl_writer || scl->scl_write_wanted) {
423 				cv_wait(&scl->scl_cv, &scl->scl_lock);
424 			}
425 		} else {
426 			ASSERT(scl->scl_writer != curthread);
427 			while (!refcount_is_zero(&scl->scl_count)) {
428 				scl->scl_write_wanted++;
429 				cv_wait(&scl->scl_cv, &scl->scl_lock);
430 				scl->scl_write_wanted--;
431 			}
432 			scl->scl_writer = curthread;
433 		}
434 		(void) refcount_add(&scl->scl_count, tag);
435 		mutex_exit(&scl->scl_lock);
436 	}
437 	ASSERT(wlocks_held <= locks);
438 }
439 
440 void
441 spa_config_exit(spa_t *spa, int locks, void *tag)
442 {
443 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
444 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
445 		if (!(locks & (1 << i)))
446 			continue;
447 		mutex_enter(&scl->scl_lock);
448 		ASSERT(!refcount_is_zero(&scl->scl_count));
449 		if (refcount_remove(&scl->scl_count, tag) == 0) {
450 			ASSERT(scl->scl_writer == NULL ||
451 			    scl->scl_writer == curthread);
452 			scl->scl_writer = NULL;	/* OK in either case */
453 			cv_broadcast(&scl->scl_cv);
454 		}
455 		mutex_exit(&scl->scl_lock);
456 	}
457 }
458 
459 int
460 spa_config_held(spa_t *spa, int locks, krw_t rw)
461 {
462 	int locks_held = 0;
463 
464 	for (int i = 0; i < SCL_LOCKS; i++) {
465 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
466 		if (!(locks & (1 << i)))
467 			continue;
468 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
469 		    (rw == RW_WRITER && scl->scl_writer == curthread))
470 			locks_held |= 1 << i;
471 	}
472 
473 	return (locks_held);
474 }
475 
476 /*
477  * ==========================================================================
478  * SPA namespace functions
479  * ==========================================================================
480  */
481 
482 /*
483  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
484  * Returns NULL if no matching spa_t is found.
485  */
486 spa_t *
487 spa_lookup(const char *name)
488 {
489 	static spa_t search;	/* spa_t is large; don't allocate on stack */
490 	spa_t *spa;
491 	avl_index_t where;
492 	char *cp;
493 
494 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
495 
496 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
497 
498 	/*
499 	 * If it's a full dataset name, figure out the pool name and
500 	 * just use that.
501 	 */
502 	cp = strpbrk(search.spa_name, "/@#");
503 	if (cp != NULL)
504 		*cp = '\0';
505 
506 	spa = avl_find(&spa_namespace_avl, &search, &where);
507 
508 	return (spa);
509 }
510 
511 /*
512  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
513  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
514  * looking for potentially hung I/Os.
515  */
516 void
517 spa_deadman(void *arg)
518 {
519 	spa_t *spa = arg;
520 
521 	/*
522 	 * Disable the deadman timer if the pool is suspended.
523 	 */
524 	if (spa_suspended(spa)) {
525 		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
526 		return;
527 	}
528 
529 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
530 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
531 	    ++spa->spa_deadman_calls);
532 	if (zfs_deadman_enabled)
533 		vdev_deadman(spa->spa_root_vdev);
534 }
535 
536 /*
537  * Create an uninitialized spa_t with the given name.  Requires
538  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
539  * exist by calling spa_lookup() first.
540  */
541 spa_t *
542 spa_add(const char *name, nvlist_t *config, const char *altroot)
543 {
544 	spa_t *spa;
545 	spa_config_dirent_t *dp;
546 	cyc_handler_t hdlr;
547 	cyc_time_t when;
548 
549 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
550 
551 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
552 
553 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
554 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
555 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
556 	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
557 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
558 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
559 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
560 	mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
561 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
562 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
563 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
564 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
565 
566 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
567 	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
568 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
569 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
570 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
571 
572 	for (int t = 0; t < TXG_SIZE; t++)
573 		bplist_create(&spa->spa_free_bplist[t]);
574 
575 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
576 	spa->spa_state = POOL_STATE_UNINITIALIZED;
577 	spa->spa_freeze_txg = UINT64_MAX;
578 	spa->spa_final_txg = UINT64_MAX;
579 	spa->spa_load_max_txg = UINT64_MAX;
580 	spa->spa_proc = &p0;
581 	spa->spa_proc_state = SPA_PROC_NONE;
582 
583 	hdlr.cyh_func = spa_deadman;
584 	hdlr.cyh_arg = spa;
585 	hdlr.cyh_level = CY_LOW_LEVEL;
586 
587 	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
588 
589 	/*
590 	 * This determines how often we need to check for hung I/Os after
591 	 * the cyclic has already fired. Since checking for hung I/Os is
592 	 * an expensive operation we don't want to check too frequently.
593 	 * Instead wait for 5 seconds before checking again.
594 	 */
595 	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
596 	when.cyt_when = CY_INFINITY;
597 	mutex_enter(&cpu_lock);
598 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
599 	mutex_exit(&cpu_lock);
600 
601 	refcount_create(&spa->spa_refcount);
602 	spa_config_lock_init(spa);
603 
604 	avl_add(&spa_namespace_avl, spa);
605 
606 	/*
607 	 * Set the alternate root, if there is one.
608 	 */
609 	if (altroot) {
610 		spa->spa_root = spa_strdup(altroot);
611 		spa_active_count++;
612 	}
613 
614 	/*
615 	 * Every pool starts with the default cachefile
616 	 */
617 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
618 	    offsetof(spa_config_dirent_t, scd_link));
619 
620 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
621 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
622 	list_insert_head(&spa->spa_config_list, dp);
623 
624 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
625 	    KM_SLEEP) == 0);
626 
627 	if (config != NULL) {
628 		nvlist_t *features;
629 
630 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
631 		    &features) == 0) {
632 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
633 			    0) == 0);
634 		}
635 
636 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
637 	}
638 
639 	if (spa->spa_label_features == NULL) {
640 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
641 		    KM_SLEEP) == 0);
642 	}
643 
644 	spa->spa_iokstat = kstat_create("zfs", 0, name,
645 	    "disk", KSTAT_TYPE_IO, 1, 0);
646 	if (spa->spa_iokstat) {
647 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
648 		kstat_install(spa->spa_iokstat);
649 	}
650 
651 	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
652 
653 	spa->spa_min_ashift = INT_MAX;
654 	spa->spa_max_ashift = 0;
655 
656 	/*
657 	 * As a pool is being created, treat all features as disabled by
658 	 * setting SPA_FEATURE_DISABLED for all entries in the feature
659 	 * refcount cache.
660 	 */
661 	for (int i = 0; i < SPA_FEATURES; i++) {
662 		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
663 	}
664 
665 	return (spa);
666 }
667 
668 /*
669  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
670  * spa_namespace_lock.  This is called only after the spa_t has been closed and
671  * deactivated.
672  */
673 void
674 spa_remove(spa_t *spa)
675 {
676 	spa_config_dirent_t *dp;
677 
678 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
679 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
680 	ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
681 
682 	nvlist_free(spa->spa_config_splitting);
683 
684 	avl_remove(&spa_namespace_avl, spa);
685 	cv_broadcast(&spa_namespace_cv);
686 
687 	if (spa->spa_root) {
688 		spa_strfree(spa->spa_root);
689 		spa_active_count--;
690 	}
691 
692 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
693 		list_remove(&spa->spa_config_list, dp);
694 		if (dp->scd_path != NULL)
695 			spa_strfree(dp->scd_path);
696 		kmem_free(dp, sizeof (spa_config_dirent_t));
697 	}
698 
699 	list_destroy(&spa->spa_config_list);
700 
701 	nvlist_free(spa->spa_label_features);
702 	nvlist_free(spa->spa_load_info);
703 	spa_config_set(spa, NULL);
704 
705 	mutex_enter(&cpu_lock);
706 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
707 		cyclic_remove(spa->spa_deadman_cycid);
708 	mutex_exit(&cpu_lock);
709 	spa->spa_deadman_cycid = CYCLIC_NONE;
710 
711 	refcount_destroy(&spa->spa_refcount);
712 
713 	spa_config_lock_destroy(spa);
714 
715 	kstat_delete(spa->spa_iokstat);
716 	spa->spa_iokstat = NULL;
717 
718 	for (int t = 0; t < TXG_SIZE; t++)
719 		bplist_destroy(&spa->spa_free_bplist[t]);
720 
721 	zio_checksum_templates_free(spa);
722 
723 	cv_destroy(&spa->spa_async_cv);
724 	cv_destroy(&spa->spa_evicting_os_cv);
725 	cv_destroy(&spa->spa_proc_cv);
726 	cv_destroy(&spa->spa_scrub_io_cv);
727 	cv_destroy(&spa->spa_suspend_cv);
728 
729 	mutex_destroy(&spa->spa_async_lock);
730 	mutex_destroy(&spa->spa_errlist_lock);
731 	mutex_destroy(&spa->spa_errlog_lock);
732 	mutex_destroy(&spa->spa_evicting_os_lock);
733 	mutex_destroy(&spa->spa_history_lock);
734 	mutex_destroy(&spa->spa_proc_lock);
735 	mutex_destroy(&spa->spa_props_lock);
736 	mutex_destroy(&spa->spa_cksum_tmpls_lock);
737 	mutex_destroy(&spa->spa_scrub_lock);
738 	mutex_destroy(&spa->spa_suspend_lock);
739 	mutex_destroy(&spa->spa_vdev_top_lock);
740 	mutex_destroy(&spa->spa_iokstat_lock);
741 
742 	kmem_free(spa, sizeof (spa_t));
743 }
744 
745 /*
746  * Given a pool, return the next pool in the namespace, or NULL if there is
747  * none.  If 'prev' is NULL, return the first pool.
748  */
749 spa_t *
750 spa_next(spa_t *prev)
751 {
752 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
753 
754 	if (prev)
755 		return (AVL_NEXT(&spa_namespace_avl, prev));
756 	else
757 		return (avl_first(&spa_namespace_avl));
758 }
759 
760 /*
761  * ==========================================================================
762  * SPA refcount functions
763  * ==========================================================================
764  */
765 
766 /*
767  * Add a reference to the given spa_t.  Must have at least one reference, or
768  * have the namespace lock held.
769  */
770 void
771 spa_open_ref(spa_t *spa, void *tag)
772 {
773 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
774 	    MUTEX_HELD(&spa_namespace_lock));
775 	(void) refcount_add(&spa->spa_refcount, tag);
776 }
777 
778 /*
779  * Remove a reference to the given spa_t.  Must have at least one reference, or
780  * have the namespace lock held.
781  */
782 void
783 spa_close(spa_t *spa, void *tag)
784 {
785 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
786 	    MUTEX_HELD(&spa_namespace_lock));
787 	(void) refcount_remove(&spa->spa_refcount, tag);
788 }
789 
790 /*
791  * Remove a reference to the given spa_t held by a dsl dir that is
792  * being asynchronously released.  Async releases occur from a taskq
793  * performing eviction of dsl datasets and dirs.  The namespace lock
794  * isn't held and the hold by the object being evicted may contribute to
795  * spa_minref (e.g. dataset or directory released during pool export),
796  * so the asserts in spa_close() do not apply.
797  */
798 void
799 spa_async_close(spa_t *spa, void *tag)
800 {
801 	(void) refcount_remove(&spa->spa_refcount, tag);
802 }
803 
804 /*
805  * Check to see if the spa refcount is zero.  Must be called with
806  * spa_namespace_lock held.  We really compare against spa_minref, which is the
807  * number of references acquired when opening a pool
808  */
809 boolean_t
810 spa_refcount_zero(spa_t *spa)
811 {
812 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
813 
814 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
815 }
816 
817 /*
818  * ==========================================================================
819  * SPA spare and l2cache tracking
820  * ==========================================================================
821  */
822 
823 /*
824  * Hot spares and cache devices are tracked using the same code below,
825  * for 'auxiliary' devices.
826  */
827 
828 typedef struct spa_aux {
829 	uint64_t	aux_guid;
830 	uint64_t	aux_pool;
831 	avl_node_t	aux_avl;
832 	int		aux_count;
833 } spa_aux_t;
834 
835 static int
836 spa_aux_compare(const void *a, const void *b)
837 {
838 	const spa_aux_t *sa = a;
839 	const spa_aux_t *sb = b;
840 
841 	if (sa->aux_guid < sb->aux_guid)
842 		return (-1);
843 	else if (sa->aux_guid > sb->aux_guid)
844 		return (1);
845 	else
846 		return (0);
847 }
848 
849 void
850 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
851 {
852 	avl_index_t where;
853 	spa_aux_t search;
854 	spa_aux_t *aux;
855 
856 	search.aux_guid = vd->vdev_guid;
857 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
858 		aux->aux_count++;
859 	} else {
860 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
861 		aux->aux_guid = vd->vdev_guid;
862 		aux->aux_count = 1;
863 		avl_insert(avl, aux, where);
864 	}
865 }
866 
867 void
868 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
869 {
870 	spa_aux_t search;
871 	spa_aux_t *aux;
872 	avl_index_t where;
873 
874 	search.aux_guid = vd->vdev_guid;
875 	aux = avl_find(avl, &search, &where);
876 
877 	ASSERT(aux != NULL);
878 
879 	if (--aux->aux_count == 0) {
880 		avl_remove(avl, aux);
881 		kmem_free(aux, sizeof (spa_aux_t));
882 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
883 		aux->aux_pool = 0ULL;
884 	}
885 }
886 
887 boolean_t
888 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
889 {
890 	spa_aux_t search, *found;
891 
892 	search.aux_guid = guid;
893 	found = avl_find(avl, &search, NULL);
894 
895 	if (pool) {
896 		if (found)
897 			*pool = found->aux_pool;
898 		else
899 			*pool = 0ULL;
900 	}
901 
902 	if (refcnt) {
903 		if (found)
904 			*refcnt = found->aux_count;
905 		else
906 			*refcnt = 0;
907 	}
908 
909 	return (found != NULL);
910 }
911 
912 void
913 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
914 {
915 	spa_aux_t search, *found;
916 	avl_index_t where;
917 
918 	search.aux_guid = vd->vdev_guid;
919 	found = avl_find(avl, &search, &where);
920 	ASSERT(found != NULL);
921 	ASSERT(found->aux_pool == 0ULL);
922 
923 	found->aux_pool = spa_guid(vd->vdev_spa);
924 }
925 
926 /*
927  * Spares are tracked globally due to the following constraints:
928  *
929  * 	- A spare may be part of multiple pools.
930  * 	- A spare may be added to a pool even if it's actively in use within
931  *	  another pool.
932  * 	- A spare in use in any pool can only be the source of a replacement if
933  *	  the target is a spare in the same pool.
934  *
935  * We keep track of all spares on the system through the use of a reference
936  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
937  * spare, then we bump the reference count in the AVL tree.  In addition, we set
938  * the 'vdev_isspare' member to indicate that the device is a spare (active or
939  * inactive).  When a spare is made active (used to replace a device in the
940  * pool), we also keep track of which pool its been made a part of.
941  *
942  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
943  * called under the spa_namespace lock as part of vdev reconfiguration.  The
944  * separate spare lock exists for the status query path, which does not need to
945  * be completely consistent with respect to other vdev configuration changes.
946  */
947 
948 static int
949 spa_spare_compare(const void *a, const void *b)
950 {
951 	return (spa_aux_compare(a, b));
952 }
953 
954 void
955 spa_spare_add(vdev_t *vd)
956 {
957 	mutex_enter(&spa_spare_lock);
958 	ASSERT(!vd->vdev_isspare);
959 	spa_aux_add(vd, &spa_spare_avl);
960 	vd->vdev_isspare = B_TRUE;
961 	mutex_exit(&spa_spare_lock);
962 }
963 
964 void
965 spa_spare_remove(vdev_t *vd)
966 {
967 	mutex_enter(&spa_spare_lock);
968 	ASSERT(vd->vdev_isspare);
969 	spa_aux_remove(vd, &spa_spare_avl);
970 	vd->vdev_isspare = B_FALSE;
971 	mutex_exit(&spa_spare_lock);
972 }
973 
974 boolean_t
975 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
976 {
977 	boolean_t found;
978 
979 	mutex_enter(&spa_spare_lock);
980 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
981 	mutex_exit(&spa_spare_lock);
982 
983 	return (found);
984 }
985 
986 void
987 spa_spare_activate(vdev_t *vd)
988 {
989 	mutex_enter(&spa_spare_lock);
990 	ASSERT(vd->vdev_isspare);
991 	spa_aux_activate(vd, &spa_spare_avl);
992 	mutex_exit(&spa_spare_lock);
993 }
994 
995 /*
996  * Level 2 ARC devices are tracked globally for the same reasons as spares.
997  * Cache devices currently only support one pool per cache device, and so
998  * for these devices the aux reference count is currently unused beyond 1.
999  */
1000 
1001 static int
1002 spa_l2cache_compare(const void *a, const void *b)
1003 {
1004 	return (spa_aux_compare(a, b));
1005 }
1006 
1007 void
1008 spa_l2cache_add(vdev_t *vd)
1009 {
1010 	mutex_enter(&spa_l2cache_lock);
1011 	ASSERT(!vd->vdev_isl2cache);
1012 	spa_aux_add(vd, &spa_l2cache_avl);
1013 	vd->vdev_isl2cache = B_TRUE;
1014 	mutex_exit(&spa_l2cache_lock);
1015 }
1016 
1017 void
1018 spa_l2cache_remove(vdev_t *vd)
1019 {
1020 	mutex_enter(&spa_l2cache_lock);
1021 	ASSERT(vd->vdev_isl2cache);
1022 	spa_aux_remove(vd, &spa_l2cache_avl);
1023 	vd->vdev_isl2cache = B_FALSE;
1024 	mutex_exit(&spa_l2cache_lock);
1025 }
1026 
1027 boolean_t
1028 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1029 {
1030 	boolean_t found;
1031 
1032 	mutex_enter(&spa_l2cache_lock);
1033 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1034 	mutex_exit(&spa_l2cache_lock);
1035 
1036 	return (found);
1037 }
1038 
1039 void
1040 spa_l2cache_activate(vdev_t *vd)
1041 {
1042 	mutex_enter(&spa_l2cache_lock);
1043 	ASSERT(vd->vdev_isl2cache);
1044 	spa_aux_activate(vd, &spa_l2cache_avl);
1045 	mutex_exit(&spa_l2cache_lock);
1046 }
1047 
1048 /*
1049  * ==========================================================================
1050  * SPA vdev locking
1051  * ==========================================================================
1052  */
1053 
1054 /*
1055  * Lock the given spa_t for the purpose of adding or removing a vdev.
1056  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1057  * It returns the next transaction group for the spa_t.
1058  */
1059 uint64_t
1060 spa_vdev_enter(spa_t *spa)
1061 {
1062 	mutex_enter(&spa->spa_vdev_top_lock);
1063 	mutex_enter(&spa_namespace_lock);
1064 	return (spa_vdev_config_enter(spa));
1065 }
1066 
1067 /*
1068  * Internal implementation for spa_vdev_enter().  Used when a vdev
1069  * operation requires multiple syncs (i.e. removing a device) while
1070  * keeping the spa_namespace_lock held.
1071  */
1072 uint64_t
1073 spa_vdev_config_enter(spa_t *spa)
1074 {
1075 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1076 
1077 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1078 
1079 	return (spa_last_synced_txg(spa) + 1);
1080 }
1081 
1082 /*
1083  * Used in combination with spa_vdev_config_enter() to allow the syncing
1084  * of multiple transactions without releasing the spa_namespace_lock.
1085  */
1086 void
1087 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1088 {
1089 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1090 
1091 	int config_changed = B_FALSE;
1092 
1093 	ASSERT(txg > spa_last_synced_txg(spa));
1094 
1095 	spa->spa_pending_vdev = NULL;
1096 
1097 	/*
1098 	 * Reassess the DTLs.
1099 	 */
1100 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1101 
1102 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1103 		config_changed = B_TRUE;
1104 		spa->spa_config_generation++;
1105 	}
1106 
1107 	/*
1108 	 * Verify the metaslab classes.
1109 	 */
1110 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1111 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1112 
1113 	spa_config_exit(spa, SCL_ALL, spa);
1114 
1115 	/*
1116 	 * Panic the system if the specified tag requires it.  This
1117 	 * is useful for ensuring that configurations are updated
1118 	 * transactionally.
1119 	 */
1120 	if (zio_injection_enabled)
1121 		zio_handle_panic_injection(spa, tag, 0);
1122 
1123 	/*
1124 	 * Note: this txg_wait_synced() is important because it ensures
1125 	 * that there won't be more than one config change per txg.
1126 	 * This allows us to use the txg as the generation number.
1127 	 */
1128 	if (error == 0)
1129 		txg_wait_synced(spa->spa_dsl_pool, txg);
1130 
1131 	if (vd != NULL) {
1132 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1133 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1134 		vdev_free(vd);
1135 		spa_config_exit(spa, SCL_ALL, spa);
1136 	}
1137 
1138 	/*
1139 	 * If the config changed, update the config cache.
1140 	 */
1141 	if (config_changed)
1142 		spa_config_sync(spa, B_FALSE, B_TRUE);
1143 }
1144 
1145 /*
1146  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
1147  * locking of spa_vdev_enter(), we also want make sure the transactions have
1148  * synced to disk, and then update the global configuration cache with the new
1149  * information.
1150  */
1151 int
1152 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1153 {
1154 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1155 	mutex_exit(&spa_namespace_lock);
1156 	mutex_exit(&spa->spa_vdev_top_lock);
1157 
1158 	return (error);
1159 }
1160 
1161 /*
1162  * Lock the given spa_t for the purpose of changing vdev state.
1163  */
1164 void
1165 spa_vdev_state_enter(spa_t *spa, int oplocks)
1166 {
1167 	int locks = SCL_STATE_ALL | oplocks;
1168 
1169 	/*
1170 	 * Root pools may need to read of the underlying devfs filesystem
1171 	 * when opening up a vdev.  Unfortunately if we're holding the
1172 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1173 	 * the read from the root filesystem.  Instead we "prefetch"
1174 	 * the associated vnodes that we need prior to opening the
1175 	 * underlying devices and cache them so that we can prevent
1176 	 * any I/O when we are doing the actual open.
1177 	 */
1178 	if (spa_is_root(spa)) {
1179 		int low = locks & ~(SCL_ZIO - 1);
1180 		int high = locks & ~low;
1181 
1182 		spa_config_enter(spa, high, spa, RW_WRITER);
1183 		vdev_hold(spa->spa_root_vdev);
1184 		spa_config_enter(spa, low, spa, RW_WRITER);
1185 	} else {
1186 		spa_config_enter(spa, locks, spa, RW_WRITER);
1187 	}
1188 	spa->spa_vdev_locks = locks;
1189 }
1190 
1191 int
1192 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1193 {
1194 	boolean_t config_changed = B_FALSE;
1195 
1196 	if (vd != NULL || error == 0)
1197 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1198 		    0, 0, B_FALSE);
1199 
1200 	if (vd != NULL) {
1201 		vdev_state_dirty(vd->vdev_top);
1202 		config_changed = B_TRUE;
1203 		spa->spa_config_generation++;
1204 	}
1205 
1206 	if (spa_is_root(spa))
1207 		vdev_rele(spa->spa_root_vdev);
1208 
1209 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1210 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1211 
1212 	/*
1213 	 * If anything changed, wait for it to sync.  This ensures that,
1214 	 * from the system administrator's perspective, zpool(1M) commands
1215 	 * are synchronous.  This is important for things like zpool offline:
1216 	 * when the command completes, you expect no further I/O from ZFS.
1217 	 */
1218 	if (vd != NULL)
1219 		txg_wait_synced(spa->spa_dsl_pool, 0);
1220 
1221 	/*
1222 	 * If the config changed, update the config cache.
1223 	 */
1224 	if (config_changed) {
1225 		mutex_enter(&spa_namespace_lock);
1226 		spa_config_sync(spa, B_FALSE, B_TRUE);
1227 		mutex_exit(&spa_namespace_lock);
1228 	}
1229 
1230 	return (error);
1231 }
1232 
1233 /*
1234  * ==========================================================================
1235  * Miscellaneous functions
1236  * ==========================================================================
1237  */
1238 
1239 void
1240 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1241 {
1242 	if (!nvlist_exists(spa->spa_label_features, feature)) {
1243 		fnvlist_add_boolean(spa->spa_label_features, feature);
1244 		/*
1245 		 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1246 		 * dirty the vdev config because lock SCL_CONFIG is not held.
1247 		 * Thankfully, in this case we don't need to dirty the config
1248 		 * because it will be written out anyway when we finish
1249 		 * creating the pool.
1250 		 */
1251 		if (tx->tx_txg != TXG_INITIAL)
1252 			vdev_config_dirty(spa->spa_root_vdev);
1253 	}
1254 }
1255 
1256 void
1257 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1258 {
1259 	if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1260 		vdev_config_dirty(spa->spa_root_vdev);
1261 }
1262 
1263 /*
1264  * Rename a spa_t.
1265  */
1266 int
1267 spa_rename(const char *name, const char *newname)
1268 {
1269 	spa_t *spa;
1270 	int err;
1271 
1272 	/*
1273 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1274 	 * actually open the pool so that we can sync out the necessary labels.
1275 	 * It's OK to call spa_open() with the namespace lock held because we
1276 	 * allow recursive calls for other reasons.
1277 	 */
1278 	mutex_enter(&spa_namespace_lock);
1279 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1280 		mutex_exit(&spa_namespace_lock);
1281 		return (err);
1282 	}
1283 
1284 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1285 
1286 	avl_remove(&spa_namespace_avl, spa);
1287 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1288 	avl_add(&spa_namespace_avl, spa);
1289 
1290 	/*
1291 	 * Sync all labels to disk with the new names by marking the root vdev
1292 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1293 	 * during the sync.
1294 	 */
1295 	vdev_config_dirty(spa->spa_root_vdev);
1296 
1297 	spa_config_exit(spa, SCL_ALL, FTAG);
1298 
1299 	txg_wait_synced(spa->spa_dsl_pool, 0);
1300 
1301 	/*
1302 	 * Sync the updated config cache.
1303 	 */
1304 	spa_config_sync(spa, B_FALSE, B_TRUE);
1305 
1306 	spa_close(spa, FTAG);
1307 
1308 	mutex_exit(&spa_namespace_lock);
1309 
1310 	return (0);
1311 }
1312 
1313 /*
1314  * Return the spa_t associated with given pool_guid, if it exists.  If
1315  * device_guid is non-zero, determine whether the pool exists *and* contains
1316  * a device with the specified device_guid.
1317  */
1318 spa_t *
1319 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1320 {
1321 	spa_t *spa;
1322 	avl_tree_t *t = &spa_namespace_avl;
1323 
1324 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1325 
1326 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1327 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1328 			continue;
1329 		if (spa->spa_root_vdev == NULL)
1330 			continue;
1331 		if (spa_guid(spa) == pool_guid) {
1332 			if (device_guid == 0)
1333 				break;
1334 
1335 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
1336 			    device_guid) != NULL)
1337 				break;
1338 
1339 			/*
1340 			 * Check any devices we may be in the process of adding.
1341 			 */
1342 			if (spa->spa_pending_vdev) {
1343 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1344 				    device_guid) != NULL)
1345 					break;
1346 			}
1347 		}
1348 	}
1349 
1350 	return (spa);
1351 }
1352 
1353 /*
1354  * Determine whether a pool with the given pool_guid exists.
1355  */
1356 boolean_t
1357 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1358 {
1359 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1360 }
1361 
1362 char *
1363 spa_strdup(const char *s)
1364 {
1365 	size_t len;
1366 	char *new;
1367 
1368 	len = strlen(s);
1369 	new = kmem_alloc(len + 1, KM_SLEEP);
1370 	bcopy(s, new, len);
1371 	new[len] = '\0';
1372 
1373 	return (new);
1374 }
1375 
1376 void
1377 spa_strfree(char *s)
1378 {
1379 	kmem_free(s, strlen(s) + 1);
1380 }
1381 
1382 uint64_t
1383 spa_get_random(uint64_t range)
1384 {
1385 	uint64_t r;
1386 
1387 	ASSERT(range != 0);
1388 
1389 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1390 
1391 	return (r % range);
1392 }
1393 
1394 uint64_t
1395 spa_generate_guid(spa_t *spa)
1396 {
1397 	uint64_t guid = spa_get_random(-1ULL);
1398 
1399 	if (spa != NULL) {
1400 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1401 			guid = spa_get_random(-1ULL);
1402 	} else {
1403 		while (guid == 0 || spa_guid_exists(guid, 0))
1404 			guid = spa_get_random(-1ULL);
1405 	}
1406 
1407 	return (guid);
1408 }
1409 
1410 void
1411 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1412 {
1413 	char type[256];
1414 	char *checksum = NULL;
1415 	char *compress = NULL;
1416 
1417 	if (bp != NULL) {
1418 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1419 			dmu_object_byteswap_t bswap =
1420 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1421 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1422 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1423 			    "metadata" : "data",
1424 			    dmu_ot_byteswap[bswap].ob_name);
1425 		} else {
1426 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1427 			    sizeof (type));
1428 		}
1429 		if (!BP_IS_EMBEDDED(bp)) {
1430 			checksum =
1431 			    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1432 		}
1433 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1434 	}
1435 
1436 	SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1437 	    compress);
1438 }
1439 
1440 void
1441 spa_freeze(spa_t *spa)
1442 {
1443 	uint64_t freeze_txg = 0;
1444 
1445 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1446 	if (spa->spa_freeze_txg == UINT64_MAX) {
1447 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1448 		spa->spa_freeze_txg = freeze_txg;
1449 	}
1450 	spa_config_exit(spa, SCL_ALL, FTAG);
1451 	if (freeze_txg != 0)
1452 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1453 }
1454 
1455 void
1456 zfs_panic_recover(const char *fmt, ...)
1457 {
1458 	va_list adx;
1459 
1460 	va_start(adx, fmt);
1461 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1462 	va_end(adx);
1463 }
1464 
1465 /*
1466  * This is a stripped-down version of strtoull, suitable only for converting
1467  * lowercase hexadecimal numbers that don't overflow.
1468  */
1469 uint64_t
1470 strtonum(const char *str, char **nptr)
1471 {
1472 	uint64_t val = 0;
1473 	char c;
1474 	int digit;
1475 
1476 	while ((c = *str) != '\0') {
1477 		if (c >= '0' && c <= '9')
1478 			digit = c - '0';
1479 		else if (c >= 'a' && c <= 'f')
1480 			digit = 10 + c - 'a';
1481 		else
1482 			break;
1483 
1484 		val *= 16;
1485 		val += digit;
1486 
1487 		str++;
1488 	}
1489 
1490 	if (nptr)
1491 		*nptr = (char *)str;
1492 
1493 	return (val);
1494 }
1495 
1496 /*
1497  * ==========================================================================
1498  * Accessor functions
1499  * ==========================================================================
1500  */
1501 
1502 boolean_t
1503 spa_shutting_down(spa_t *spa)
1504 {
1505 	return (spa->spa_async_suspended);
1506 }
1507 
1508 dsl_pool_t *
1509 spa_get_dsl(spa_t *spa)
1510 {
1511 	return (spa->spa_dsl_pool);
1512 }
1513 
1514 boolean_t
1515 spa_is_initializing(spa_t *spa)
1516 {
1517 	return (spa->spa_is_initializing);
1518 }
1519 
1520 blkptr_t *
1521 spa_get_rootblkptr(spa_t *spa)
1522 {
1523 	return (&spa->spa_ubsync.ub_rootbp);
1524 }
1525 
1526 void
1527 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1528 {
1529 	spa->spa_uberblock.ub_rootbp = *bp;
1530 }
1531 
1532 void
1533 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1534 {
1535 	if (spa->spa_root == NULL)
1536 		buf[0] = '\0';
1537 	else
1538 		(void) strncpy(buf, spa->spa_root, buflen);
1539 }
1540 
1541 int
1542 spa_sync_pass(spa_t *spa)
1543 {
1544 	return (spa->spa_sync_pass);
1545 }
1546 
1547 char *
1548 spa_name(spa_t *spa)
1549 {
1550 	return (spa->spa_name);
1551 }
1552 
1553 uint64_t
1554 spa_guid(spa_t *spa)
1555 {
1556 	dsl_pool_t *dp = spa_get_dsl(spa);
1557 	uint64_t guid;
1558 
1559 	/*
1560 	 * If we fail to parse the config during spa_load(), we can go through
1561 	 * the error path (which posts an ereport) and end up here with no root
1562 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1563 	 * this case.
1564 	 */
1565 	if (spa->spa_root_vdev == NULL)
1566 		return (spa->spa_config_guid);
1567 
1568 	guid = spa->spa_last_synced_guid != 0 ?
1569 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1570 
1571 	/*
1572 	 * Return the most recently synced out guid unless we're
1573 	 * in syncing context.
1574 	 */
1575 	if (dp && dsl_pool_sync_context(dp))
1576 		return (spa->spa_root_vdev->vdev_guid);
1577 	else
1578 		return (guid);
1579 }
1580 
1581 uint64_t
1582 spa_load_guid(spa_t *spa)
1583 {
1584 	/*
1585 	 * This is a GUID that exists solely as a reference for the
1586 	 * purposes of the arc.  It is generated at load time, and
1587 	 * is never written to persistent storage.
1588 	 */
1589 	return (spa->spa_load_guid);
1590 }
1591 
1592 uint64_t
1593 spa_last_synced_txg(spa_t *spa)
1594 {
1595 	return (spa->spa_ubsync.ub_txg);
1596 }
1597 
1598 uint64_t
1599 spa_first_txg(spa_t *spa)
1600 {
1601 	return (spa->spa_first_txg);
1602 }
1603 
1604 uint64_t
1605 spa_syncing_txg(spa_t *spa)
1606 {
1607 	return (spa->spa_syncing_txg);
1608 }
1609 
1610 pool_state_t
1611 spa_state(spa_t *spa)
1612 {
1613 	return (spa->spa_state);
1614 }
1615 
1616 spa_load_state_t
1617 spa_load_state(spa_t *spa)
1618 {
1619 	return (spa->spa_load_state);
1620 }
1621 
1622 uint64_t
1623 spa_freeze_txg(spa_t *spa)
1624 {
1625 	return (spa->spa_freeze_txg);
1626 }
1627 
1628 /* ARGSUSED */
1629 uint64_t
1630 spa_get_asize(spa_t *spa, uint64_t lsize)
1631 {
1632 	return (lsize * spa_asize_inflation);
1633 }
1634 
1635 /*
1636  * Return the amount of slop space in bytes.  It is 1/32 of the pool (3.2%),
1637  * or at least 32MB.
1638  *
1639  * See the comment above spa_slop_shift for details.
1640  */
1641 uint64_t
1642 spa_get_slop_space(spa_t *spa) {
1643 	uint64_t space = spa_get_dspace(spa);
1644 	return (MAX(space >> spa_slop_shift, SPA_MINDEVSIZE >> 1));
1645 }
1646 
1647 uint64_t
1648 spa_get_dspace(spa_t *spa)
1649 {
1650 	return (spa->spa_dspace);
1651 }
1652 
1653 void
1654 spa_update_dspace(spa_t *spa)
1655 {
1656 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1657 	    ddt_get_dedup_dspace(spa);
1658 }
1659 
1660 /*
1661  * Return the failure mode that has been set to this pool. The default
1662  * behavior will be to block all I/Os when a complete failure occurs.
1663  */
1664 uint8_t
1665 spa_get_failmode(spa_t *spa)
1666 {
1667 	return (spa->spa_failmode);
1668 }
1669 
1670 boolean_t
1671 spa_suspended(spa_t *spa)
1672 {
1673 	return (spa->spa_suspended);
1674 }
1675 
1676 uint64_t
1677 spa_version(spa_t *spa)
1678 {
1679 	return (spa->spa_ubsync.ub_version);
1680 }
1681 
1682 boolean_t
1683 spa_deflate(spa_t *spa)
1684 {
1685 	return (spa->spa_deflate);
1686 }
1687 
1688 metaslab_class_t *
1689 spa_normal_class(spa_t *spa)
1690 {
1691 	return (spa->spa_normal_class);
1692 }
1693 
1694 metaslab_class_t *
1695 spa_log_class(spa_t *spa)
1696 {
1697 	return (spa->spa_log_class);
1698 }
1699 
1700 void
1701 spa_evicting_os_register(spa_t *spa, objset_t *os)
1702 {
1703 	mutex_enter(&spa->spa_evicting_os_lock);
1704 	list_insert_head(&spa->spa_evicting_os_list, os);
1705 	mutex_exit(&spa->spa_evicting_os_lock);
1706 }
1707 
1708 void
1709 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1710 {
1711 	mutex_enter(&spa->spa_evicting_os_lock);
1712 	list_remove(&spa->spa_evicting_os_list, os);
1713 	cv_broadcast(&spa->spa_evicting_os_cv);
1714 	mutex_exit(&spa->spa_evicting_os_lock);
1715 }
1716 
1717 void
1718 spa_evicting_os_wait(spa_t *spa)
1719 {
1720 	mutex_enter(&spa->spa_evicting_os_lock);
1721 	while (!list_is_empty(&spa->spa_evicting_os_list))
1722 		cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1723 	mutex_exit(&spa->spa_evicting_os_lock);
1724 
1725 	dmu_buf_user_evict_wait();
1726 }
1727 
1728 int
1729 spa_max_replication(spa_t *spa)
1730 {
1731 	/*
1732 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1733 	 * handle BPs with more than one DVA allocated.  Set our max
1734 	 * replication level accordingly.
1735 	 */
1736 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1737 		return (1);
1738 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1739 }
1740 
1741 int
1742 spa_prev_software_version(spa_t *spa)
1743 {
1744 	return (spa->spa_prev_software_version);
1745 }
1746 
1747 uint64_t
1748 spa_deadman_synctime(spa_t *spa)
1749 {
1750 	return (spa->spa_deadman_synctime);
1751 }
1752 
1753 uint64_t
1754 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1755 {
1756 	uint64_t asize = DVA_GET_ASIZE(dva);
1757 	uint64_t dsize = asize;
1758 
1759 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1760 
1761 	if (asize != 0 && spa->spa_deflate) {
1762 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1763 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1764 	}
1765 
1766 	return (dsize);
1767 }
1768 
1769 uint64_t
1770 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1771 {
1772 	uint64_t dsize = 0;
1773 
1774 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1775 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1776 
1777 	return (dsize);
1778 }
1779 
1780 uint64_t
1781 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1782 {
1783 	uint64_t dsize = 0;
1784 
1785 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1786 
1787 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1788 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1789 
1790 	spa_config_exit(spa, SCL_VDEV, FTAG);
1791 
1792 	return (dsize);
1793 }
1794 
1795 /*
1796  * ==========================================================================
1797  * Initialization and Termination
1798  * ==========================================================================
1799  */
1800 
1801 static int
1802 spa_name_compare(const void *a1, const void *a2)
1803 {
1804 	const spa_t *s1 = a1;
1805 	const spa_t *s2 = a2;
1806 	int s;
1807 
1808 	s = strcmp(s1->spa_name, s2->spa_name);
1809 	if (s > 0)
1810 		return (1);
1811 	if (s < 0)
1812 		return (-1);
1813 	return (0);
1814 }
1815 
1816 int
1817 spa_busy(void)
1818 {
1819 	return (spa_active_count);
1820 }
1821 
1822 void
1823 spa_boot_init()
1824 {
1825 	spa_config_load();
1826 }
1827 
1828 void
1829 spa_init(int mode)
1830 {
1831 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1832 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1833 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1834 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1835 
1836 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1837 	    offsetof(spa_t, spa_avl));
1838 
1839 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1840 	    offsetof(spa_aux_t, aux_avl));
1841 
1842 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1843 	    offsetof(spa_aux_t, aux_avl));
1844 
1845 	spa_mode_global = mode;
1846 
1847 #ifdef _KERNEL
1848 	spa_arch_init();
1849 #else
1850 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1851 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1852 		if (arc_procfd == -1) {
1853 			perror("could not enable watchpoints: "
1854 			    "opening /proc/self/ctl failed: ");
1855 		} else {
1856 			arc_watch = B_TRUE;
1857 		}
1858 	}
1859 #endif
1860 
1861 	refcount_init();
1862 	unique_init();
1863 	range_tree_init();
1864 	zio_init();
1865 	dmu_init();
1866 	zil_init();
1867 	vdev_cache_stat_init();
1868 	zfs_prop_init();
1869 	zpool_prop_init();
1870 	zpool_feature_init();
1871 	spa_config_load();
1872 	l2arc_start();
1873 }
1874 
1875 void
1876 spa_fini(void)
1877 {
1878 	l2arc_stop();
1879 
1880 	spa_evict_all();
1881 
1882 	vdev_cache_stat_fini();
1883 	zil_fini();
1884 	dmu_fini();
1885 	zio_fini();
1886 	range_tree_fini();
1887 	unique_fini();
1888 	refcount_fini();
1889 
1890 	avl_destroy(&spa_namespace_avl);
1891 	avl_destroy(&spa_spare_avl);
1892 	avl_destroy(&spa_l2cache_avl);
1893 
1894 	cv_destroy(&spa_namespace_cv);
1895 	mutex_destroy(&spa_namespace_lock);
1896 	mutex_destroy(&spa_spare_lock);
1897 	mutex_destroy(&spa_l2cache_lock);
1898 }
1899 
1900 /*
1901  * Return whether this pool has slogs. No locking needed.
1902  * It's not a problem if the wrong answer is returned as it's only for
1903  * performance and not correctness
1904  */
1905 boolean_t
1906 spa_has_slogs(spa_t *spa)
1907 {
1908 	return (spa->spa_log_class->mc_rotor != NULL);
1909 }
1910 
1911 spa_log_state_t
1912 spa_get_log_state(spa_t *spa)
1913 {
1914 	return (spa->spa_log_state);
1915 }
1916 
1917 void
1918 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1919 {
1920 	spa->spa_log_state = state;
1921 }
1922 
1923 boolean_t
1924 spa_is_root(spa_t *spa)
1925 {
1926 	return (spa->spa_is_root);
1927 }
1928 
1929 boolean_t
1930 spa_writeable(spa_t *spa)
1931 {
1932 	return (!!(spa->spa_mode & FWRITE));
1933 }
1934 
1935 /*
1936  * Returns true if there is a pending sync task in any of the current
1937  * syncing txg, the current quiescing txg, or the current open txg.
1938  */
1939 boolean_t
1940 spa_has_pending_synctask(spa_t *spa)
1941 {
1942 	return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
1943 }
1944 
1945 int
1946 spa_mode(spa_t *spa)
1947 {
1948 	return (spa->spa_mode);
1949 }
1950 
1951 uint64_t
1952 spa_bootfs(spa_t *spa)
1953 {
1954 	return (spa->spa_bootfs);
1955 }
1956 
1957 uint64_t
1958 spa_delegation(spa_t *spa)
1959 {
1960 	return (spa->spa_delegation);
1961 }
1962 
1963 objset_t *
1964 spa_meta_objset(spa_t *spa)
1965 {
1966 	return (spa->spa_meta_objset);
1967 }
1968 
1969 enum zio_checksum
1970 spa_dedup_checksum(spa_t *spa)
1971 {
1972 	return (spa->spa_dedup_checksum);
1973 }
1974 
1975 /*
1976  * Reset pool scan stat per scan pass (or reboot).
1977  */
1978 void
1979 spa_scan_stat_init(spa_t *spa)
1980 {
1981 	/* data not stored on disk */
1982 	spa->spa_scan_pass_start = gethrestime_sec();
1983 	spa->spa_scan_pass_exam = 0;
1984 	vdev_scan_stat_init(spa->spa_root_vdev);
1985 }
1986 
1987 /*
1988  * Get scan stats for zpool status reports
1989  */
1990 int
1991 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1992 {
1993 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1994 
1995 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1996 		return (SET_ERROR(ENOENT));
1997 	bzero(ps, sizeof (pool_scan_stat_t));
1998 
1999 	/* data stored on disk */
2000 	ps->pss_func = scn->scn_phys.scn_func;
2001 	ps->pss_start_time = scn->scn_phys.scn_start_time;
2002 	ps->pss_end_time = scn->scn_phys.scn_end_time;
2003 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2004 	ps->pss_examined = scn->scn_phys.scn_examined;
2005 	ps->pss_to_process = scn->scn_phys.scn_to_process;
2006 	ps->pss_processed = scn->scn_phys.scn_processed;
2007 	ps->pss_errors = scn->scn_phys.scn_errors;
2008 	ps->pss_state = scn->scn_phys.scn_state;
2009 
2010 	/* data not stored on disk */
2011 	ps->pss_pass_start = spa->spa_scan_pass_start;
2012 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
2013 
2014 	return (0);
2015 }
2016 
2017 boolean_t
2018 spa_debug_enabled(spa_t *spa)
2019 {
2020 	return (spa->spa_debug);
2021 }
2022 
2023 int
2024 spa_maxblocksize(spa_t *spa)
2025 {
2026 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2027 		return (SPA_MAXBLOCKSIZE);
2028 	else
2029 		return (SPA_OLD_MAXBLOCKSIZE);
2030 }
2031