xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 94c2d0eb22e9624151ee84a7edbf7178e1bf4087)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
229842588bSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*94c2d0ebSMatthew Ahrens  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
24e495b6e6SSaso Kiselkov  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2645818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/zfs_context.h>
31fa9e4066Sahrens #include <sys/spa_impl.h>
32283b8460SGeorge.Wilson #include <sys/spa_boot.h>
33fa9e4066Sahrens #include <sys/zio.h>
34fa9e4066Sahrens #include <sys/zio_checksum.h>
35fa9e4066Sahrens #include <sys/zio_compress.h>
36fa9e4066Sahrens #include <sys/dmu.h>
37fa9e4066Sahrens #include <sys/dmu_tx.h>
38fa9e4066Sahrens #include <sys/zap.h>
39fa9e4066Sahrens #include <sys/zil.h>
40fa9e4066Sahrens #include <sys/vdev_impl.h>
41fa9e4066Sahrens #include <sys/metaslab.h>
42fa9e4066Sahrens #include <sys/uberblock_impl.h>
43fa9e4066Sahrens #include <sys/txg.h>
44fa9e4066Sahrens #include <sys/avl.h>
45fa9e4066Sahrens #include <sys/unique.h>
46fa9e4066Sahrens #include <sys/dsl_pool.h>
47fa9e4066Sahrens #include <sys/dsl_dir.h>
48fa9e4066Sahrens #include <sys/dsl_prop.h>
493f9d6ad7SLin Ling #include <sys/dsl_scan.h>
50fa9e4066Sahrens #include <sys/fs/zfs.h>
516ce0521aSperrin #include <sys/metaslab_impl.h>
52e14bb325SJeff Bonwick #include <sys/arc.h>
53485bbbf5SGeorge Wilson #include <sys/ddt.h>
5491ebeef5Sahrens #include "zfs_prop.h"
5545818ee1SMatthew Ahrens #include <sys/zfeature.h>
56fa9e4066Sahrens 
57fa9e4066Sahrens /*
58fa9e4066Sahrens  * SPA locking
59fa9e4066Sahrens  *
60fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
61fa9e4066Sahrens  *
62fa9e4066Sahrens  * spa_namespace_lock (global mutex)
63fa9e4066Sahrens  *
6444cd46caSbillm  *	This lock must be acquired to do any of the following:
65fa9e4066Sahrens  *
6644cd46caSbillm  *		- Lookup a spa_t by name
6744cd46caSbillm  *		- Add or remove a spa_t from the namespace
6844cd46caSbillm  *		- Increase spa_refcount from non-zero
6944cd46caSbillm  *		- Check if spa_refcount is zero
7044cd46caSbillm  *		- Rename a spa_t
71ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
7244cd46caSbillm  *		- Held for the duration of create/destroy/import/export
73fa9e4066Sahrens  *
7444cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
7544cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
7644cd46caSbillm  *	definition they must have an existing reference, and will never need
7744cd46caSbillm  *	to lookup a spa_t by name.
78fa9e4066Sahrens  *
79fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
80fa9e4066Sahrens  *
8144cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
8244cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8344cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
84088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
8544cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
86fa9e4066Sahrens  *
87e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
88fa9e4066Sahrens  *
8991ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
9091ebeef5Sahrens  *	the following circumstances:
91fa9e4066Sahrens  *
9244cd46caSbillm  *		- RW_READER to perform I/O to the spa
9344cd46caSbillm  *		- RW_WRITER to change the vdev config
94fa9e4066Sahrens  *
95fa9e4066Sahrens  * The locking order is fairly straightforward:
96fa9e4066Sahrens  *
9744cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
98fa9e4066Sahrens  *
9944cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
10044cd46caSbillm  *	or to check if it is zero.
101fa9e4066Sahrens  *
102e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
103fa9e4066Sahrens  *
10444cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10544cd46caSbillm  *	the config lock.
106fa9e4066Sahrens  *
107e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
108fa9e4066Sahrens  *
10944cd46caSbillm  *	The namespace lock must always be taken before the config lock.
110fa9e4066Sahrens  *
111fa9e4066Sahrens  *
112e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
113fa9e4066Sahrens  *
114e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
115e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
116fa9e4066Sahrens  *
11744cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
118fa9e4066Sahrens  *
11944cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
120fa9e4066Sahrens  *
12144cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
12244cd46caSbillm  *				frees up any memory associated with the spa_t.
123fa9e4066Sahrens  *
12444cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12544cd46caSbillm  *				first if NULL is passed.
126fa9e4066Sahrens  *
12744cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12844cd46caSbillm  *				the system.
129fa9e4066Sahrens  *
130ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
131fa9e4066Sahrens  *
132fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
133fa9e4066Sahrens  *
13444cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13544cd46caSbillm  *				called with spa_namespace_lock held if the
13644cd46caSbillm  *				refcount is currently zero.
137fa9e4066Sahrens  *
13844cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
13944cd46caSbillm  *				not free the spa_t or remove it from the
14044cd46caSbillm  *				namespace.  No locking is required.
141fa9e4066Sahrens  *
14244cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14344cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14444cd46caSbillm  *				held.
145fa9e4066Sahrens  *
146e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
147e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
148e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
149e14bb325SJeff Bonwick  *
150e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
151e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
152e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
153e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
154e14bb325SJeff Bonwick  *
155e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
156e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
157e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
158e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
159e14bb325SJeff Bonwick  *
160e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
161e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
162e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
163e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
164e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
165e14bb325SJeff Bonwick  *
166e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
167e14bb325SJeff Bonwick  *
168e14bb325SJeff Bonwick  * SCL_CONFIG
169e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
170e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
171e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
172e14bb325SJeff Bonwick  *
173e14bb325SJeff Bonwick  * SCL_STATE
174e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
175e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
176e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
177e14bb325SJeff Bonwick  *
178e14bb325SJeff Bonwick  * SCL_ALLOC
179e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
180e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
181e14bb325SJeff Bonwick  *
182e14bb325SJeff Bonwick  * SCL_ZIO
183e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
184e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
185e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
186e14bb325SJeff Bonwick  *
187e14bb325SJeff Bonwick  * SCL_FREE
188e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
189e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
190e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
191e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
192e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
193e14bb325SJeff Bonwick  *
194e14bb325SJeff Bonwick  * SCL_VDEV
195e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
196b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
197e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
198e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
199e14bb325SJeff Bonwick  *
200e14bb325SJeff Bonwick  * In addition, the following rules apply:
201e14bb325SJeff Bonwick  *
202e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
203e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
204e14bb325SJeff Bonwick  *
205e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
206e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
207e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
208e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
209e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
210fa9e4066Sahrens  *
211ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
212fa9e4066Sahrens  *
21344cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
214ea8dc4b6Seschrock  *				for writing.
215fa9e4066Sahrens  *
21644cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21744cd46caSbillm  *				to complete, sync the updated configs to the
218ea8dc4b6Seschrock  *				cache, and release the namespace lock.
219fa9e4066Sahrens  *
220e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
221e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
222e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
223e14bb325SJeff Bonwick  *
224ad135b5dSChristopher Siden  * spa_rename() is also implemented within this file since it requires
225e14bb325SJeff Bonwick  * manipulation of the namespace.
226fa9e4066Sahrens  */
227fa9e4066Sahrens 
228fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
229fa9e4066Sahrens kmutex_t spa_namespace_lock;
230fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2310373e76bSbonwick static int spa_active_count;
232416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
233fa9e4066Sahrens 
23499653d4eSeschrock static kmutex_t spa_spare_lock;
23539c23413Seschrock static avl_tree_t spa_spare_avl;
236fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
237fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23899653d4eSeschrock 
239fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2408ad4d6ddSJeff Bonwick int spa_mode_global;
241fa9e4066Sahrens 
242fa9e4066Sahrens #ifdef ZFS_DEBUG
2433b2aab18SMatthew Ahrens /* Everything except dprintf and spa is on by default in debug builds */
2443b2aab18SMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
245fa9e4066Sahrens #else
246fa9e4066Sahrens int zfs_flags = 0;
247fa9e4066Sahrens #endif
248fa9e4066Sahrens 
2490125049cSahrens /*
2500125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2510125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2520125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2538b36997aSMatthew Ahrens  * This should only be used as a last resort, as it typically results
2548b36997aSMatthew Ahrens  * in leaked space, or worse.
2550125049cSahrens  */
2567fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE;
2577fd05ac4SMatthew Ahrens 
2587fd05ac4SMatthew Ahrens /*
2597fd05ac4SMatthew Ahrens  * If destroy encounters an EIO while reading metadata (e.g. indirect
2607fd05ac4SMatthew Ahrens  * blocks), space referenced by the missing metadata can not be freed.
2617fd05ac4SMatthew Ahrens  * Normally this causes the background destroy to become "stalled", as
2627fd05ac4SMatthew Ahrens  * it is unable to make forward progress.  While in this stalled state,
2637fd05ac4SMatthew Ahrens  * all remaining space to free from the error-encountering filesystem is
2647fd05ac4SMatthew Ahrens  * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
2657fd05ac4SMatthew Ahrens  * permanently leak the space from indirect blocks that can not be read,
2667fd05ac4SMatthew Ahrens  * and continue to free everything else that it can.
2677fd05ac4SMatthew Ahrens  *
2687fd05ac4SMatthew Ahrens  * The default, "stalling" behavior is useful if the storage partially
2697fd05ac4SMatthew Ahrens  * fails (i.e. some but not all i/os fail), and then later recovers.  In
2707fd05ac4SMatthew Ahrens  * this case, we will be able to continue pool operations while it is
2717fd05ac4SMatthew Ahrens  * partially failed, and when it recovers, we can continue to free the
2727fd05ac4SMatthew Ahrens  * space, with no leaks.  However, note that this case is actually
2737fd05ac4SMatthew Ahrens  * fairly rare.
2747fd05ac4SMatthew Ahrens  *
2757fd05ac4SMatthew Ahrens  * Typically pools either (a) fail completely (but perhaps temporarily,
2767fd05ac4SMatthew Ahrens  * e.g. a top-level vdev going offline), or (b) have localized,
2777fd05ac4SMatthew Ahrens  * permanent errors (e.g. disk returns the wrong data due to bit flip or
2787fd05ac4SMatthew Ahrens  * firmware bug).  In case (a), this setting does not matter because the
2797fd05ac4SMatthew Ahrens  * pool will be suspended and the sync thread will not be able to make
2807fd05ac4SMatthew Ahrens  * forward progress regardless.  In case (b), because the error is
2817fd05ac4SMatthew Ahrens  * permanent, the best we can do is leak the minimum amount of space,
2827fd05ac4SMatthew Ahrens  * which is what setting this flag will do.  Therefore, it is reasonable
2837fd05ac4SMatthew Ahrens  * for this flag to normally be set, but we chose the more conservative
2847fd05ac4SMatthew Ahrens  * approach of not setting it, so that there is no possibility of
2857fd05ac4SMatthew Ahrens  * leaking space in the "partial temporary" failure case.
2867fd05ac4SMatthew Ahrens  */
2877fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE;
2880125049cSahrens 
28969962b56SMatthew Ahrens /*
29069962b56SMatthew Ahrens  * Expiration time in milliseconds. This value has two meanings. First it is
29169962b56SMatthew Ahrens  * used to determine when the spa_deadman() logic should fire. By default the
29269962b56SMatthew Ahrens  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
29369962b56SMatthew Ahrens  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
29469962b56SMatthew Ahrens  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
29569962b56SMatthew Ahrens  * in a system panic.
29669962b56SMatthew Ahrens  */
29769962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL;
298283b8460SGeorge.Wilson 
299283b8460SGeorge.Wilson /*
30069962b56SMatthew Ahrens  * Check time in milliseconds. This defines the frequency at which we check
30169962b56SMatthew Ahrens  * for hung I/O.
302283b8460SGeorge.Wilson  */
30369962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL;
304283b8460SGeorge.Wilson 
305283b8460SGeorge.Wilson /*
306283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
307283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
308283b8460SGeorge.Wilson  */
309283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
310283b8460SGeorge.Wilson 
31169962b56SMatthew Ahrens /*
31269962b56SMatthew Ahrens  * The worst case is single-sector max-parity RAID-Z blocks, in which
31369962b56SMatthew Ahrens  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
31469962b56SMatthew Ahrens  * times the size; so just assume that.  Add to this the fact that
31569962b56SMatthew Ahrens  * we can have up to 3 DVAs per bp, and one more factor of 2 because
31669962b56SMatthew Ahrens  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
31769962b56SMatthew Ahrens  * the worst case is:
31869962b56SMatthew Ahrens  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
31969962b56SMatthew Ahrens  */
32069962b56SMatthew Ahrens int spa_asize_inflation = 24;
321fa9e4066Sahrens 
3227d46dc6cSMatthew Ahrens /*
3237d46dc6cSMatthew Ahrens  * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3247d46dc6cSMatthew Ahrens  * the pool to be consumed.  This ensures that we don't run the pool
3257d46dc6cSMatthew Ahrens  * completely out of space, due to unaccounted changes (e.g. to the MOS).
3267d46dc6cSMatthew Ahrens  * It also limits the worst-case time to allocate space.  If we have
3277d46dc6cSMatthew Ahrens  * less than this amount of free space, most ZPL operations (e.g. write,
3287d46dc6cSMatthew Ahrens  * create) will return ENOSPC.
3297d46dc6cSMatthew Ahrens  *
3307d46dc6cSMatthew Ahrens  * Certain operations (e.g. file removal, most administrative actions) can
3317d46dc6cSMatthew Ahrens  * use half the slop space.  They will only return ENOSPC if less than half
3327d46dc6cSMatthew Ahrens  * the slop space is free.  Typically, once the pool has less than the slop
3337d46dc6cSMatthew Ahrens  * space free, the user will use these operations to free up space in the pool.
3347d46dc6cSMatthew Ahrens  * These are the operations that call dsl_pool_adjustedsize() with the netfree
3357d46dc6cSMatthew Ahrens  * argument set to TRUE.
3367d46dc6cSMatthew Ahrens  *
3377d46dc6cSMatthew Ahrens  * A very restricted set of operations are always permitted, regardless of
3387d46dc6cSMatthew Ahrens  * the amount of free space.  These are the operations that call
3397d46dc6cSMatthew Ahrens  * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy".  If these
3407d46dc6cSMatthew Ahrens  * operations result in a net increase in the amount of space used,
3417d46dc6cSMatthew Ahrens  * it is possible to run the pool completely out of space, causing it to
3427d46dc6cSMatthew Ahrens  * be permanently read-only.
3437d46dc6cSMatthew Ahrens  *
3444b5c8e93SMatthew Ahrens  * Note that on very small pools, the slop space will be larger than
3454b5c8e93SMatthew Ahrens  * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
3464b5c8e93SMatthew Ahrens  * but we never allow it to be more than half the pool size.
3474b5c8e93SMatthew Ahrens  *
3487d46dc6cSMatthew Ahrens  * See also the comments in zfs_space_check_t.
3497d46dc6cSMatthew Ahrens  */
3507d46dc6cSMatthew Ahrens int spa_slop_shift = 5;
3514b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024;
3527d46dc6cSMatthew Ahrens 
353e05725b1Sbonwick /*
354e05725b1Sbonwick  * ==========================================================================
355e05725b1Sbonwick  * SPA config locking
356e05725b1Sbonwick  * ==========================================================================
357e05725b1Sbonwick  */
358e05725b1Sbonwick static void
359e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
360e14bb325SJeff Bonwick {
361e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
362e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
363e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
364e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
3653b2aab18SMatthew Ahrens 		refcount_create_untracked(&scl->scl_count);
366e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
367e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
368e14bb325SJeff Bonwick 	}
369e05725b1Sbonwick }
370e05725b1Sbonwick 
371e05725b1Sbonwick static void
372e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
373e14bb325SJeff Bonwick {
374e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
375e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
376e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
377e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
378e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
379e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
380e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
381e14bb325SJeff Bonwick 	}
382e14bb325SJeff Bonwick }
383e14bb325SJeff Bonwick 
384e14bb325SJeff Bonwick int
385e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
386e05725b1Sbonwick {
387e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
388e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
389e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
390e14bb325SJeff Bonwick 			continue;
391e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
392e14bb325SJeff Bonwick 		if (rw == RW_READER) {
393e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
394e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
395e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
396e495b6e6SSaso Kiselkov 				    tag);
397e14bb325SJeff Bonwick 				return (0);
398e14bb325SJeff Bonwick 			}
399e14bb325SJeff Bonwick 		} else {
400e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
401e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
402e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
403e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
404e495b6e6SSaso Kiselkov 				    tag);
405e14bb325SJeff Bonwick 				return (0);
406e14bb325SJeff Bonwick 			}
407e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
408e14bb325SJeff Bonwick 		}
409e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
410e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
411e14bb325SJeff Bonwick 	}
412e14bb325SJeff Bonwick 	return (1);
413e05725b1Sbonwick }
414e05725b1Sbonwick 
415e05725b1Sbonwick void
416e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
417e05725b1Sbonwick {
418f64c0e34SEric Taylor 	int wlocks_held = 0;
419f64c0e34SEric Taylor 
4203b2aab18SMatthew Ahrens 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4213b2aab18SMatthew Ahrens 
422e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
423e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
424f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
425f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
426e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
427e14bb325SJeff Bonwick 			continue;
428e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
429e14bb325SJeff Bonwick 		if (rw == RW_READER) {
430e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
431e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
432e14bb325SJeff Bonwick 			}
433e14bb325SJeff Bonwick 		} else {
434e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
435e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
436e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
437e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
438e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
439e14bb325SJeff Bonwick 			}
440e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
441e14bb325SJeff Bonwick 		}
442e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
443e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
444e05725b1Sbonwick 	}
445f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
446e05725b1Sbonwick }
447e05725b1Sbonwick 
448e05725b1Sbonwick void
449e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
450e05725b1Sbonwick {
451e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
452e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
453e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
454e14bb325SJeff Bonwick 			continue;
455e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
456e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
457e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
458e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
459e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
460e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
461e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
462e14bb325SJeff Bonwick 		}
463e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
464e05725b1Sbonwick 	}
465e05725b1Sbonwick }
466e05725b1Sbonwick 
467e14bb325SJeff Bonwick int
468e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
469e05725b1Sbonwick {
470e14bb325SJeff Bonwick 	int locks_held = 0;
471e05725b1Sbonwick 
472e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
473e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
474e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
475e14bb325SJeff Bonwick 			continue;
476e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
477e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
478e14bb325SJeff Bonwick 			locks_held |= 1 << i;
479e14bb325SJeff Bonwick 	}
480e14bb325SJeff Bonwick 
481e14bb325SJeff Bonwick 	return (locks_held);
482e05725b1Sbonwick }
483e05725b1Sbonwick 
484fa9e4066Sahrens /*
485fa9e4066Sahrens  * ==========================================================================
486fa9e4066Sahrens  * SPA namespace functions
487fa9e4066Sahrens  * ==========================================================================
488fa9e4066Sahrens  */
489fa9e4066Sahrens 
490fa9e4066Sahrens /*
491fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
492fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
493fa9e4066Sahrens  */
494fa9e4066Sahrens spa_t *
495fa9e4066Sahrens spa_lookup(const char *name)
496fa9e4066Sahrens {
497e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
498e14bb325SJeff Bonwick 	spa_t *spa;
499fa9e4066Sahrens 	avl_index_t where;
50040feaa91Sahrens 	char *cp;
501fa9e4066Sahrens 
502fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
503fa9e4066Sahrens 
5043b2aab18SMatthew Ahrens 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
5053b2aab18SMatthew Ahrens 
50640feaa91Sahrens 	/*
50740feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
50840feaa91Sahrens 	 * just use that.
50940feaa91Sahrens 	 */
51078f17100SMatthew Ahrens 	cp = strpbrk(search.spa_name, "/@#");
5113b2aab18SMatthew Ahrens 	if (cp != NULL)
51240feaa91Sahrens 		*cp = '\0';
51340feaa91Sahrens 
514fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
515fa9e4066Sahrens 
516fa9e4066Sahrens 	return (spa);
517fa9e4066Sahrens }
518fa9e4066Sahrens 
519283b8460SGeorge.Wilson /*
520283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
521283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
522283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
523283b8460SGeorge.Wilson  */
524283b8460SGeorge.Wilson void
525283b8460SGeorge.Wilson spa_deadman(void *arg)
526283b8460SGeorge.Wilson {
527283b8460SGeorge.Wilson 	spa_t *spa = arg;
528283b8460SGeorge.Wilson 
5290713e232SGeorge Wilson 	/*
5300713e232SGeorge Wilson 	 * Disable the deadman timer if the pool is suspended.
5310713e232SGeorge Wilson 	 */
5320713e232SGeorge Wilson 	if (spa_suspended(spa)) {
5330713e232SGeorge Wilson 		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5340713e232SGeorge Wilson 		return;
5350713e232SGeorge Wilson 	}
5360713e232SGeorge Wilson 
537283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
538283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
539283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
540283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
541283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
542283b8460SGeorge.Wilson }
543283b8460SGeorge.Wilson 
544fa9e4066Sahrens /*
545fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
546fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
547fa9e4066Sahrens  * exist by calling spa_lookup() first.
548fa9e4066Sahrens  */
549fa9e4066Sahrens spa_t *
550468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
551fa9e4066Sahrens {
552fa9e4066Sahrens 	spa_t *spa;
553c5904d13Seschrock 	spa_config_dirent_t *dp;
554283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
555283b8460SGeorge.Wilson 	cyc_time_t when;
556fa9e4066Sahrens 
557fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
558fa9e4066Sahrens 
559fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
560fa9e4066Sahrens 
561c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
562c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
56335a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
564bc9014e6SJustin Gibbs 	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
565c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
56635a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
567c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
56845818ee1SMatthew Ahrens 	mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
56935a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
570a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
571a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
572c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
5730f7643c7SGeorge Wilson 	mutex_init(&spa->spa_alloc_lock, NULL, MUTEX_DEFAULT, NULL);
574c25056deSgw 
575c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
576bc9014e6SJustin Gibbs 	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
57735a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
578c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
579e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
580c25056deSgw 
581b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
582cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
583b24ab676SJeff Bonwick 
584e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
585fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
586fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
5870373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
588468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
58935a5a358SJonathan Adams 	spa->spa_proc = &p0;
59035a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
591fa9e4066Sahrens 
592283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
593283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
594283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
595283b8460SGeorge.Wilson 
59669962b56SMatthew Ahrens 	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
597283b8460SGeorge.Wilson 
598283b8460SGeorge.Wilson 	/*
599283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
600283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
601283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
60269962b56SMatthew Ahrens 	 * Instead wait for 5 seconds before checking again.
603283b8460SGeorge.Wilson 	 */
60469962b56SMatthew Ahrens 	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
605283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
606283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
607283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
608283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
609283b8460SGeorge.Wilson 
610fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
611e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
612fa9e4066Sahrens 
613fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
614fa9e4066Sahrens 
6150373e76bSbonwick 	/*
6160373e76bSbonwick 	 * Set the alternate root, if there is one.
6170373e76bSbonwick 	 */
6180373e76bSbonwick 	if (altroot) {
6190373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
6200373e76bSbonwick 		spa_active_count++;
6210373e76bSbonwick 	}
6220373e76bSbonwick 
623*94c2d0ebSMatthew Ahrens 	avl_create(&spa->spa_alloc_tree, zio_bookmark_compare,
6240f7643c7SGeorge Wilson 	    sizeof (zio_t), offsetof(zio_t, io_alloc_node));
6250f7643c7SGeorge Wilson 
626c5904d13Seschrock 	/*
627c5904d13Seschrock 	 * Every pool starts with the default cachefile
628c5904d13Seschrock 	 */
629c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
630c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
631c5904d13Seschrock 
632c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
633ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
634c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
635c5904d13Seschrock 
6364b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
6374b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
6384b964adaSGeorge Wilson 
639ad135b5dSChristopher Siden 	if (config != NULL) {
640ad135b5dSChristopher Siden 		nvlist_t *features;
641ad135b5dSChristopher Siden 
642ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
643ad135b5dSChristopher Siden 		    &features) == 0) {
644ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
645ad135b5dSChristopher Siden 			    0) == 0);
646ad135b5dSChristopher Siden 		}
647ad135b5dSChristopher Siden 
648468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
649ad135b5dSChristopher Siden 	}
650ad135b5dSChristopher Siden 
651ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
652ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
653ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
654ad135b5dSChristopher Siden 	}
655468c413aSTim Haley 
656c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
657c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
658c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
659c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
660c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
661c3a66015SMatthew Ahrens 	}
662c3a66015SMatthew Ahrens 
6633b2aab18SMatthew Ahrens 	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
6643b2aab18SMatthew Ahrens 
66581cd5c55SMatthew Ahrens 	spa->spa_min_ashift = INT_MAX;
66681cd5c55SMatthew Ahrens 	spa->spa_max_ashift = 0;
66781cd5c55SMatthew Ahrens 
66843466aaeSMax Grossman 	/*
66943466aaeSMax Grossman 	 * As a pool is being created, treat all features as disabled by
67043466aaeSMax Grossman 	 * setting SPA_FEATURE_DISABLED for all entries in the feature
67143466aaeSMax Grossman 	 * refcount cache.
67243466aaeSMax Grossman 	 */
67343466aaeSMax Grossman 	for (int i = 0; i < SPA_FEATURES; i++) {
67443466aaeSMax Grossman 		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
67543466aaeSMax Grossman 	}
67643466aaeSMax Grossman 
677fa9e4066Sahrens 	return (spa);
678fa9e4066Sahrens }
679fa9e4066Sahrens 
680fa9e4066Sahrens /*
681fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
682fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
683fa9e4066Sahrens  * deactivated.
684fa9e4066Sahrens  */
685fa9e4066Sahrens void
686fa9e4066Sahrens spa_remove(spa_t *spa)
687fa9e4066Sahrens {
688c5904d13Seschrock 	spa_config_dirent_t *dp;
689c5904d13Seschrock 
690fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
691fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
692bc9014e6SJustin Gibbs 	ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
693fa9e4066Sahrens 
6941195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
6951195e687SMark J Musante 
696fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
697fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
698fa9e4066Sahrens 
6990373e76bSbonwick 	if (spa->spa_root) {
700fa9e4066Sahrens 		spa_strfree(spa->spa_root);
7010373e76bSbonwick 		spa_active_count--;
7020373e76bSbonwick 	}
703fa9e4066Sahrens 
704c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
705c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
706c5904d13Seschrock 		if (dp->scd_path != NULL)
707c5904d13Seschrock 			spa_strfree(dp->scd_path);
708c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
709c5904d13Seschrock 	}
710c5904d13Seschrock 
7110f7643c7SGeorge Wilson 	avl_destroy(&spa->spa_alloc_tree);
712c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
7132f8aaab3Seschrock 
714ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
7154b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
716fa9e4066Sahrens 	spa_config_set(spa, NULL);
717fa9e4066Sahrens 
718283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
719283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
720283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
721283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
722283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
723283b8460SGeorge.Wilson 
724fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
72591ebeef5Sahrens 
726e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
727fa9e4066Sahrens 
728c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
729c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
730c3a66015SMatthew Ahrens 
731b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
732cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
733b24ab676SJeff Bonwick 
73445818ee1SMatthew Ahrens 	zio_checksum_templates_free(spa);
73545818ee1SMatthew Ahrens 
736c25056deSgw 	cv_destroy(&spa->spa_async_cv);
737bc9014e6SJustin Gibbs 	cv_destroy(&spa->spa_evicting_os_cv);
73835a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
739c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
740e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
741c25056deSgw 
7420f7643c7SGeorge Wilson 	mutex_destroy(&spa->spa_alloc_lock);
7435ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
744c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
74535a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
746bc9014e6SJustin Gibbs 	mutex_destroy(&spa->spa_evicting_os_lock);
74706eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
74835a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
749b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
75045818ee1SMatthew Ahrens 	mutex_destroy(&spa->spa_cksum_tmpls_lock);
75135a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
752e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
753a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
754c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
7555ad82045Snd 
756fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
757fa9e4066Sahrens }
758fa9e4066Sahrens 
759fa9e4066Sahrens /*
760fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
761fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
762fa9e4066Sahrens  */
763fa9e4066Sahrens spa_t *
764fa9e4066Sahrens spa_next(spa_t *prev)
765fa9e4066Sahrens {
766fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
767fa9e4066Sahrens 
768fa9e4066Sahrens 	if (prev)
769fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
770fa9e4066Sahrens 	else
771fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
772fa9e4066Sahrens }
773fa9e4066Sahrens 
774fa9e4066Sahrens /*
775fa9e4066Sahrens  * ==========================================================================
776fa9e4066Sahrens  * SPA refcount functions
777fa9e4066Sahrens  * ==========================================================================
778fa9e4066Sahrens  */
779fa9e4066Sahrens 
780fa9e4066Sahrens /*
781fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
782fa9e4066Sahrens  * have the namespace lock held.
783fa9e4066Sahrens  */
784fa9e4066Sahrens void
785fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
786fa9e4066Sahrens {
787088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
788fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
789fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
790fa9e4066Sahrens }
791fa9e4066Sahrens 
792fa9e4066Sahrens /*
793fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
794fa9e4066Sahrens  * have the namespace lock held.
795fa9e4066Sahrens  */
796fa9e4066Sahrens void
797fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
798fa9e4066Sahrens {
799088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
800fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
801fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
802fa9e4066Sahrens }
803fa9e4066Sahrens 
804bc9014e6SJustin Gibbs /*
805bc9014e6SJustin Gibbs  * Remove a reference to the given spa_t held by a dsl dir that is
806bc9014e6SJustin Gibbs  * being asynchronously released.  Async releases occur from a taskq
807bc9014e6SJustin Gibbs  * performing eviction of dsl datasets and dirs.  The namespace lock
808bc9014e6SJustin Gibbs  * isn't held and the hold by the object being evicted may contribute to
809bc9014e6SJustin Gibbs  * spa_minref (e.g. dataset or directory released during pool export),
810bc9014e6SJustin Gibbs  * so the asserts in spa_close() do not apply.
811bc9014e6SJustin Gibbs  */
812bc9014e6SJustin Gibbs void
813bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag)
814bc9014e6SJustin Gibbs {
815bc9014e6SJustin Gibbs 	(void) refcount_remove(&spa->spa_refcount, tag);
816bc9014e6SJustin Gibbs }
817bc9014e6SJustin Gibbs 
818fa9e4066Sahrens /*
819fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
820088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
821fa9e4066Sahrens  * number of references acquired when opening a pool
822fa9e4066Sahrens  */
823fa9e4066Sahrens boolean_t
824fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
825fa9e4066Sahrens {
826fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
827fa9e4066Sahrens 
828088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
829fa9e4066Sahrens }
830fa9e4066Sahrens 
83199653d4eSeschrock /*
83299653d4eSeschrock  * ==========================================================================
833fa94a07fSbrendan  * SPA spare and l2cache tracking
83499653d4eSeschrock  * ==========================================================================
83599653d4eSeschrock  */
83699653d4eSeschrock 
837fa94a07fSbrendan /*
838fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
839fa94a07fSbrendan  * for 'auxiliary' devices.
840fa94a07fSbrendan  */
841fa94a07fSbrendan 
842fa94a07fSbrendan typedef struct spa_aux {
843fa94a07fSbrendan 	uint64_t	aux_guid;
844fa94a07fSbrendan 	uint64_t	aux_pool;
845fa94a07fSbrendan 	avl_node_t	aux_avl;
846fa94a07fSbrendan 	int		aux_count;
847fa94a07fSbrendan } spa_aux_t;
848fa94a07fSbrendan 
849fa94a07fSbrendan static int
850fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
851fa94a07fSbrendan {
852fa94a07fSbrendan 	const spa_aux_t *sa = a;
853fa94a07fSbrendan 	const spa_aux_t *sb = b;
854fa94a07fSbrendan 
855fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
856fa94a07fSbrendan 		return (-1);
857fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
858fa94a07fSbrendan 		return (1);
859fa94a07fSbrendan 	else
860fa94a07fSbrendan 		return (0);
861fa94a07fSbrendan }
862fa94a07fSbrendan 
863fa94a07fSbrendan void
864fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
865fa94a07fSbrendan {
866fa94a07fSbrendan 	avl_index_t where;
867fa94a07fSbrendan 	spa_aux_t search;
868fa94a07fSbrendan 	spa_aux_t *aux;
869fa94a07fSbrendan 
870fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
871fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
872fa94a07fSbrendan 		aux->aux_count++;
873fa94a07fSbrendan 	} else {
874fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
875fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
876fa94a07fSbrendan 		aux->aux_count = 1;
877fa94a07fSbrendan 		avl_insert(avl, aux, where);
878fa94a07fSbrendan 	}
879fa94a07fSbrendan }
880fa94a07fSbrendan 
881fa94a07fSbrendan void
882fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
883fa94a07fSbrendan {
884fa94a07fSbrendan 	spa_aux_t search;
885fa94a07fSbrendan 	spa_aux_t *aux;
886fa94a07fSbrendan 	avl_index_t where;
887fa94a07fSbrendan 
888fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
889fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
890fa94a07fSbrendan 
891fa94a07fSbrendan 	ASSERT(aux != NULL);
892fa94a07fSbrendan 
893fa94a07fSbrendan 	if (--aux->aux_count == 0) {
894fa94a07fSbrendan 		avl_remove(avl, aux);
895fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
896fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
897fa94a07fSbrendan 		aux->aux_pool = 0ULL;
898fa94a07fSbrendan 	}
899fa94a07fSbrendan }
900fa94a07fSbrendan 
901fa94a07fSbrendan boolean_t
90289a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
903fa94a07fSbrendan {
904fa94a07fSbrendan 	spa_aux_t search, *found;
905fa94a07fSbrendan 
906fa94a07fSbrendan 	search.aux_guid = guid;
90789a89ebfSlling 	found = avl_find(avl, &search, NULL);
908fa94a07fSbrendan 
909fa94a07fSbrendan 	if (pool) {
910fa94a07fSbrendan 		if (found)
911fa94a07fSbrendan 			*pool = found->aux_pool;
912fa94a07fSbrendan 		else
913fa94a07fSbrendan 			*pool = 0ULL;
914fa94a07fSbrendan 	}
915fa94a07fSbrendan 
91689a89ebfSlling 	if (refcnt) {
91789a89ebfSlling 		if (found)
91889a89ebfSlling 			*refcnt = found->aux_count;
91989a89ebfSlling 		else
92089a89ebfSlling 			*refcnt = 0;
92189a89ebfSlling 	}
92289a89ebfSlling 
923fa94a07fSbrendan 	return (found != NULL);
924fa94a07fSbrendan }
925fa94a07fSbrendan 
926fa94a07fSbrendan void
927fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
928fa94a07fSbrendan {
929fa94a07fSbrendan 	spa_aux_t search, *found;
930fa94a07fSbrendan 	avl_index_t where;
931fa94a07fSbrendan 
932fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
933fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
934fa94a07fSbrendan 	ASSERT(found != NULL);
935fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
936fa94a07fSbrendan 
937fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
938fa94a07fSbrendan }
939fa94a07fSbrendan 
94099653d4eSeschrock /*
94139c23413Seschrock  * Spares are tracked globally due to the following constraints:
94239c23413Seschrock  *
94339c23413Seschrock  * 	- A spare may be part of multiple pools.
94439c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
94539c23413Seschrock  *	  another pool.
94639c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
94739c23413Seschrock  *	  the target is a spare in the same pool.
94839c23413Seschrock  *
94939c23413Seschrock  * We keep track of all spares on the system through the use of a reference
95039c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
95139c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
95239c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
95339c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
95439c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
95539c23413Seschrock  *
95639c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
95739c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
95839c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
95939c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
96099653d4eSeschrock  */
96139c23413Seschrock 
96299653d4eSeschrock static int
96399653d4eSeschrock spa_spare_compare(const void *a, const void *b)
96499653d4eSeschrock {
965fa94a07fSbrendan 	return (spa_aux_compare(a, b));
96699653d4eSeschrock }
96799653d4eSeschrock 
96899653d4eSeschrock void
96939c23413Seschrock spa_spare_add(vdev_t *vd)
97099653d4eSeschrock {
97199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
97239c23413Seschrock 	ASSERT(!vd->vdev_isspare);
973fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
97439c23413Seschrock 	vd->vdev_isspare = B_TRUE;
97599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
97699653d4eSeschrock }
97799653d4eSeschrock 
97899653d4eSeschrock void
97939c23413Seschrock spa_spare_remove(vdev_t *vd)
98099653d4eSeschrock {
98199653d4eSeschrock 	mutex_enter(&spa_spare_lock);
98239c23413Seschrock 	ASSERT(vd->vdev_isspare);
983fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
98439c23413Seschrock 	vd->vdev_isspare = B_FALSE;
98599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
98699653d4eSeschrock }
98799653d4eSeschrock 
98899653d4eSeschrock boolean_t
98989a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
99099653d4eSeschrock {
991fa94a07fSbrendan 	boolean_t found;
99299653d4eSeschrock 
99399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
99489a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
99599653d4eSeschrock 	mutex_exit(&spa_spare_lock);
99699653d4eSeschrock 
997fa94a07fSbrendan 	return (found);
99839c23413Seschrock }
99939c23413Seschrock 
100039c23413Seschrock void
100139c23413Seschrock spa_spare_activate(vdev_t *vd)
100239c23413Seschrock {
100339c23413Seschrock 	mutex_enter(&spa_spare_lock);
100439c23413Seschrock 	ASSERT(vd->vdev_isspare);
1005fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
1006fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
1007fa94a07fSbrendan }
100839c23413Seschrock 
1009fa94a07fSbrendan /*
1010fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
1011fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
1012fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
1013fa94a07fSbrendan  */
101439c23413Seschrock 
1015fa94a07fSbrendan static int
1016fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
1017fa94a07fSbrendan {
1018fa94a07fSbrendan 	return (spa_aux_compare(a, b));
1019fa94a07fSbrendan }
1020fa94a07fSbrendan 
1021fa94a07fSbrendan void
1022fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
1023fa94a07fSbrendan {
1024fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1025fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
1026fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
1027fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
1028fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1029fa94a07fSbrendan }
1030fa94a07fSbrendan 
1031fa94a07fSbrendan void
1032fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
1033fa94a07fSbrendan {
1034fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1035fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1036fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
1037fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
1038fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1039fa94a07fSbrendan }
1040fa94a07fSbrendan 
1041fa94a07fSbrendan boolean_t
1042fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1043fa94a07fSbrendan {
1044fa94a07fSbrendan 	boolean_t found;
1045fa94a07fSbrendan 
1046fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
104789a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1048fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1049fa94a07fSbrendan 
1050fa94a07fSbrendan 	return (found);
1051fa94a07fSbrendan }
1052fa94a07fSbrendan 
1053fa94a07fSbrendan void
1054fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
1055fa94a07fSbrendan {
1056fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1057fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1058fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
1059fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1060fa94a07fSbrendan }
1061fa94a07fSbrendan 
1062fa9e4066Sahrens /*
1063fa9e4066Sahrens  * ==========================================================================
1064fa9e4066Sahrens  * SPA vdev locking
1065fa9e4066Sahrens  * ==========================================================================
1066fa9e4066Sahrens  */
1067fa9e4066Sahrens 
1068fa9e4066Sahrens /*
1069ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
1070ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1071fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
1072fa9e4066Sahrens  */
1073fa9e4066Sahrens uint64_t
1074fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
1075fa9e4066Sahrens {
1076a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
1077bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
107888ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
107988ecc943SGeorge Wilson }
108088ecc943SGeorge Wilson 
108188ecc943SGeorge Wilson /*
108288ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
108388ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
108488ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
108588ecc943SGeorge Wilson  */
108688ecc943SGeorge Wilson uint64_t
108788ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
108888ecc943SGeorge Wilson {
108988ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
10903d7072f8Seschrock 
1091e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1092fa9e4066Sahrens 
1093fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
1094fa9e4066Sahrens }
1095fa9e4066Sahrens 
1096fa9e4066Sahrens /*
109788ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
109888ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
1099fa9e4066Sahrens  */
110088ecc943SGeorge Wilson void
110188ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1102fa9e4066Sahrens {
110388ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
110488ecc943SGeorge Wilson 
11050e34b6a7Sbonwick 	int config_changed = B_FALSE;
1106ea8dc4b6Seschrock 
11070373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
11080e34b6a7Sbonwick 
1109e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
1110e14bb325SJeff Bonwick 
11110e34b6a7Sbonwick 	/*
11120e34b6a7Sbonwick 	 * Reassess the DTLs.
11130e34b6a7Sbonwick 	 */
11140373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
11150e34b6a7Sbonwick 
1116e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
11170e34b6a7Sbonwick 		config_changed = B_TRUE;
11188f18d1faSGeorge Wilson 		spa->spa_config_generation++;
11190e34b6a7Sbonwick 	}
1120ea8dc4b6Seschrock 
112188ecc943SGeorge Wilson 	/*
112288ecc943SGeorge Wilson 	 * Verify the metaslab classes.
112388ecc943SGeorge Wilson 	 */
1124b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1125b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
112688ecc943SGeorge Wilson 
1127e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
1128fa9e4066Sahrens 
112988ecc943SGeorge Wilson 	/*
113088ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
113188ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
113288ecc943SGeorge Wilson 	 * transactionally.
113388ecc943SGeorge Wilson 	 */
113488ecc943SGeorge Wilson 	if (zio_injection_enabled)
11351195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
113688ecc943SGeorge Wilson 
1137fa9e4066Sahrens 	/*
1138fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1139fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1140fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1141fa9e4066Sahrens 	 */
1142fa9e4066Sahrens 	if (error == 0)
1143fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1144fa9e4066Sahrens 
1145fa9e4066Sahrens 	if (vd != NULL) {
11460713e232SGeorge Wilson 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
11478ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1148fa9e4066Sahrens 		vdev_free(vd);
11498ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1150fa9e4066Sahrens 	}
1151fa9e4066Sahrens 
1152fa9e4066Sahrens 	/*
11530e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1154fa9e4066Sahrens 	 */
11550e34b6a7Sbonwick 	if (config_changed)
1156c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
115788ecc943SGeorge Wilson }
1158ea8dc4b6Seschrock 
115988ecc943SGeorge Wilson /*
116088ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
116188ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
116288ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
116388ecc943SGeorge Wilson  * information.
116488ecc943SGeorge Wilson  */
116588ecc943SGeorge Wilson int
116688ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
116788ecc943SGeorge Wilson {
116888ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1169ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1170bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1171fa9e4066Sahrens 
1172fa9e4066Sahrens 	return (error);
1173fa9e4066Sahrens }
1174fa9e4066Sahrens 
1175e14bb325SJeff Bonwick /*
1176e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1177e14bb325SJeff Bonwick  */
1178e14bb325SJeff Bonwick void
11798f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1180e14bb325SJeff Bonwick {
11818f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
11828f18d1faSGeorge Wilson 
1183dcba9f3fSGeorge Wilson 	/*
1184dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1185dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1186dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1187dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1188dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1189dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1190dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1191dcba9f3fSGeorge Wilson 	 */
1192dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
11939842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
11949842588bSGeorge Wilson 		int high = locks & ~low;
11959842588bSGeorge Wilson 
11969842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1197dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
11989842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1199dcba9f3fSGeorge Wilson 	} else {
1200dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1201dcba9f3fSGeorge Wilson 	}
12028f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1203e14bb325SJeff Bonwick }
1204e14bb325SJeff Bonwick 
1205e14bb325SJeff Bonwick int
1206e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1207e14bb325SJeff Bonwick {
1208c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1209c6065d0fSGeorge Wilson 
1210b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1211b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1212b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1213b24ab676SJeff Bonwick 
12148f18d1faSGeorge Wilson 	if (vd != NULL) {
1215e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1216c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
12178f18d1faSGeorge Wilson 		spa->spa_config_generation++;
12188f18d1faSGeorge Wilson 	}
1219e14bb325SJeff Bonwick 
1220dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1221dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1222dcba9f3fSGeorge Wilson 
12238f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
12248f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1225e14bb325SJeff Bonwick 
12268ad4d6ddSJeff Bonwick 	/*
12278ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
12288ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
12298ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
12308ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
12318ad4d6ddSJeff Bonwick 	 */
12328ad4d6ddSJeff Bonwick 	if (vd != NULL)
12338ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
12348ad4d6ddSJeff Bonwick 
1235c6065d0fSGeorge Wilson 	/*
1236c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1237c6065d0fSGeorge Wilson 	 */
1238c6065d0fSGeorge Wilson 	if (config_changed) {
1239c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1240c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1241c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1242c6065d0fSGeorge Wilson 	}
1243c6065d0fSGeorge Wilson 
1244e14bb325SJeff Bonwick 	return (error);
1245e14bb325SJeff Bonwick }
1246e14bb325SJeff Bonwick 
1247fa9e4066Sahrens /*
1248fa9e4066Sahrens  * ==========================================================================
1249fa9e4066Sahrens  * Miscellaneous functions
1250fa9e4066Sahrens  * ==========================================================================
1251fa9e4066Sahrens  */
1252fa9e4066Sahrens 
1253ad135b5dSChristopher Siden void
125443466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1255ad135b5dSChristopher Siden {
12562acef22dSMatthew Ahrens 	if (!nvlist_exists(spa->spa_label_features, feature)) {
12572acef22dSMatthew Ahrens 		fnvlist_add_boolean(spa->spa_label_features, feature);
125843466aaeSMax Grossman 		/*
125943466aaeSMax Grossman 		 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
126043466aaeSMax Grossman 		 * dirty the vdev config because lock SCL_CONFIG is not held.
126143466aaeSMax Grossman 		 * Thankfully, in this case we don't need to dirty the config
126243466aaeSMax Grossman 		 * because it will be written out anyway when we finish
126343466aaeSMax Grossman 		 * creating the pool.
126443466aaeSMax Grossman 		 */
126543466aaeSMax Grossman 		if (tx->tx_txg != TXG_INITIAL)
126643466aaeSMax Grossman 			vdev_config_dirty(spa->spa_root_vdev);
12672acef22dSMatthew Ahrens 	}
1268ad135b5dSChristopher Siden }
1269ad135b5dSChristopher Siden 
1270ad135b5dSChristopher Siden void
1271ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1272ad135b5dSChristopher Siden {
12732acef22dSMatthew Ahrens 	if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
12742acef22dSMatthew Ahrens 		vdev_config_dirty(spa->spa_root_vdev);
1275ad135b5dSChristopher Siden }
1276ad135b5dSChristopher Siden 
1277fa9e4066Sahrens /*
1278fa9e4066Sahrens  * Rename a spa_t.
1279fa9e4066Sahrens  */
1280fa9e4066Sahrens int
1281fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1282fa9e4066Sahrens {
1283fa9e4066Sahrens 	spa_t *spa;
1284fa9e4066Sahrens 	int err;
1285fa9e4066Sahrens 
1286fa9e4066Sahrens 	/*
1287fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1288fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1289fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1290ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1291fa9e4066Sahrens 	 */
1292fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1293fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1294fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1295fa9e4066Sahrens 		return (err);
1296fa9e4066Sahrens 	}
1297fa9e4066Sahrens 
1298e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1299fa9e4066Sahrens 
1300fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1301e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1302fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1303fa9e4066Sahrens 
1304fa9e4066Sahrens 	/*
1305fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1306fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1307fa9e4066Sahrens 	 * during the sync.
1308fa9e4066Sahrens 	 */
1309fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1310fa9e4066Sahrens 
1311e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1312fa9e4066Sahrens 
13130373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1314fa9e4066Sahrens 
1315fa9e4066Sahrens 	/*
1316fa9e4066Sahrens 	 * Sync the updated config cache.
1317fa9e4066Sahrens 	 */
1318c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1319fa9e4066Sahrens 
1320fa9e4066Sahrens 	spa_close(spa, FTAG);
1321fa9e4066Sahrens 
1322fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1323fa9e4066Sahrens 
1324fa9e4066Sahrens 	return (0);
1325fa9e4066Sahrens }
1326fa9e4066Sahrens 
1327fa9e4066Sahrens /*
1328f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1329f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1330f9af39baSGeorge Wilson  * a device with the specified device_guid.
1331fa9e4066Sahrens  */
1332f9af39baSGeorge Wilson spa_t *
1333f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1334fa9e4066Sahrens {
1335fa9e4066Sahrens 	spa_t *spa;
1336fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1337fa9e4066Sahrens 
1338ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1339fa9e4066Sahrens 
1340fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1341fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1342fa9e4066Sahrens 			continue;
1343fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1344fa9e4066Sahrens 			continue;
134539c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
134639c23413Seschrock 			if (device_guid == 0)
134739c23413Seschrock 				break;
134839c23413Seschrock 
134939c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
135039c23413Seschrock 			    device_guid) != NULL)
135139c23413Seschrock 				break;
135239c23413Seschrock 
135339c23413Seschrock 			/*
13548654d025Sperrin 			 * Check any devices we may be in the process of adding.
135539c23413Seschrock 			 */
135639c23413Seschrock 			if (spa->spa_pending_vdev) {
135739c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
135839c23413Seschrock 				    device_guid) != NULL)
135939c23413Seschrock 					break;
136039c23413Seschrock 			}
136139c23413Seschrock 		}
1362fa9e4066Sahrens 	}
1363fa9e4066Sahrens 
1364f9af39baSGeorge Wilson 	return (spa);
1365f9af39baSGeorge Wilson }
1366f9af39baSGeorge Wilson 
1367f9af39baSGeorge Wilson /*
1368f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1369f9af39baSGeorge Wilson  */
1370f9af39baSGeorge Wilson boolean_t
1371f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1372f9af39baSGeorge Wilson {
1373f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1374fa9e4066Sahrens }
1375fa9e4066Sahrens 
1376fa9e4066Sahrens char *
1377fa9e4066Sahrens spa_strdup(const char *s)
1378fa9e4066Sahrens {
1379fa9e4066Sahrens 	size_t len;
1380fa9e4066Sahrens 	char *new;
1381fa9e4066Sahrens 
1382fa9e4066Sahrens 	len = strlen(s);
1383fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1384fa9e4066Sahrens 	bcopy(s, new, len);
1385fa9e4066Sahrens 	new[len] = '\0';
1386fa9e4066Sahrens 
1387fa9e4066Sahrens 	return (new);
1388fa9e4066Sahrens }
1389fa9e4066Sahrens 
1390fa9e4066Sahrens void
1391fa9e4066Sahrens spa_strfree(char *s)
1392fa9e4066Sahrens {
1393fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1394fa9e4066Sahrens }
1395fa9e4066Sahrens 
1396fa9e4066Sahrens uint64_t
1397fa9e4066Sahrens spa_get_random(uint64_t range)
1398fa9e4066Sahrens {
1399fa9e4066Sahrens 	uint64_t r;
1400fa9e4066Sahrens 
1401fa9e4066Sahrens 	ASSERT(range != 0);
1402fa9e4066Sahrens 
1403fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1404fa9e4066Sahrens 
1405fa9e4066Sahrens 	return (r % range);
1406fa9e4066Sahrens }
1407fa9e4066Sahrens 
14081195e687SMark J Musante uint64_t
14091195e687SMark J Musante spa_generate_guid(spa_t *spa)
14101195e687SMark J Musante {
14111195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
14121195e687SMark J Musante 
14131195e687SMark J Musante 	if (spa != NULL) {
14141195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
14151195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14161195e687SMark J Musante 	} else {
14171195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
14181195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14191195e687SMark J Musante 	}
14201195e687SMark J Musante 
14211195e687SMark J Musante 	return (guid);
14221195e687SMark J Musante }
14231195e687SMark J Musante 
1424fa9e4066Sahrens void
142543466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1426fa9e4066Sahrens {
1427ad135b5dSChristopher Siden 	char type[256];
1428f0ba89beSJeff Bonwick 	char *checksum = NULL;
1429f0ba89beSJeff Bonwick 	char *compress = NULL;
1430f0ba89beSJeff Bonwick 
1431f0ba89beSJeff Bonwick 	if (bp != NULL) {
1432ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1433ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1434ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1435ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1436ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1437ad135b5dSChristopher Siden 			    "metadata" : "data",
1438ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1439ad135b5dSChristopher Siden 		} else {
1440ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1441ad135b5dSChristopher Siden 			    sizeof (type));
1442ad135b5dSChristopher Siden 		}
14435d7b4d43SMatthew Ahrens 		if (!BP_IS_EMBEDDED(bp)) {
14445d7b4d43SMatthew Ahrens 			checksum =
14455d7b4d43SMatthew Ahrens 			    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
14465d7b4d43SMatthew Ahrens 		}
1447f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1448f0ba89beSJeff Bonwick 	}
1449fa9e4066Sahrens 
145043466aaeSMax Grossman 	SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
145143466aaeSMax Grossman 	    compress);
1452fa9e4066Sahrens }
1453fa9e4066Sahrens 
1454fa9e4066Sahrens void
1455fa9e4066Sahrens spa_freeze(spa_t *spa)
1456fa9e4066Sahrens {
1457fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1458fa9e4066Sahrens 
1459e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1460fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1461fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1462fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1463fa9e4066Sahrens 	}
1464e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1465fa9e4066Sahrens 	if (freeze_txg != 0)
1466fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1467fa9e4066Sahrens }
1468fa9e4066Sahrens 
14690125049cSahrens void
14700125049cSahrens zfs_panic_recover(const char *fmt, ...)
14710125049cSahrens {
14720125049cSahrens 	va_list adx;
14730125049cSahrens 
14740125049cSahrens 	va_start(adx, fmt);
14750125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
14760125049cSahrens 	va_end(adx);
14770125049cSahrens }
14780125049cSahrens 
14793f9d6ad7SLin Ling /*
14803f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
1481f7170741SWill Andrews  * lowercase hexadecimal numbers that don't overflow.
14823f9d6ad7SLin Ling  */
14833f9d6ad7SLin Ling uint64_t
14843f9d6ad7SLin Ling strtonum(const char *str, char **nptr)
14853f9d6ad7SLin Ling {
14863f9d6ad7SLin Ling 	uint64_t val = 0;
14873f9d6ad7SLin Ling 	char c;
14883f9d6ad7SLin Ling 	int digit;
14893f9d6ad7SLin Ling 
14903f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
14913f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
14923f9d6ad7SLin Ling 			digit = c - '0';
14933f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
14943f9d6ad7SLin Ling 			digit = 10 + c - 'a';
14953f9d6ad7SLin Ling 		else
14963f9d6ad7SLin Ling 			break;
14973f9d6ad7SLin Ling 
14983f9d6ad7SLin Ling 		val *= 16;
14993f9d6ad7SLin Ling 		val += digit;
15003f9d6ad7SLin Ling 
15013f9d6ad7SLin Ling 		str++;
15023f9d6ad7SLin Ling 	}
15033f9d6ad7SLin Ling 
15043f9d6ad7SLin Ling 	if (nptr)
15053f9d6ad7SLin Ling 		*nptr = (char *)str;
15063f9d6ad7SLin Ling 
15073f9d6ad7SLin Ling 	return (val);
15083f9d6ad7SLin Ling }
15093f9d6ad7SLin Ling 
1510fa9e4066Sahrens /*
1511fa9e4066Sahrens  * ==========================================================================
1512fa9e4066Sahrens  * Accessor functions
1513fa9e4066Sahrens  * ==========================================================================
1514fa9e4066Sahrens  */
1515fa9e4066Sahrens 
1516088f3894Sahrens boolean_t
151788b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1518fa9e4066Sahrens {
151988b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1520fa9e4066Sahrens }
1521fa9e4066Sahrens 
1522fa9e4066Sahrens dsl_pool_t *
1523fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1524fa9e4066Sahrens {
1525fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1526fa9e4066Sahrens }
1527fa9e4066Sahrens 
1528ad135b5dSChristopher Siden boolean_t
1529ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1530ad135b5dSChristopher Siden {
1531ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1532ad135b5dSChristopher Siden }
1533ad135b5dSChristopher Siden 
1534fa9e4066Sahrens blkptr_t *
1535fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1536fa9e4066Sahrens {
1537fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1538fa9e4066Sahrens }
1539fa9e4066Sahrens 
1540fa9e4066Sahrens void
1541fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1542fa9e4066Sahrens {
1543fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1544fa9e4066Sahrens }
1545fa9e4066Sahrens 
1546fa9e4066Sahrens void
1547fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1548fa9e4066Sahrens {
1549fa9e4066Sahrens 	if (spa->spa_root == NULL)
1550fa9e4066Sahrens 		buf[0] = '\0';
1551fa9e4066Sahrens 	else
1552fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1553fa9e4066Sahrens }
1554fa9e4066Sahrens 
1555fa9e4066Sahrens int
1556fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1557fa9e4066Sahrens {
1558fa9e4066Sahrens 	return (spa->spa_sync_pass);
1559fa9e4066Sahrens }
1560fa9e4066Sahrens 
1561fa9e4066Sahrens char *
1562fa9e4066Sahrens spa_name(spa_t *spa)
1563fa9e4066Sahrens {
1564fa9e4066Sahrens 	return (spa->spa_name);
1565fa9e4066Sahrens }
1566fa9e4066Sahrens 
1567fa9e4066Sahrens uint64_t
1568fa9e4066Sahrens spa_guid(spa_t *spa)
1569fa9e4066Sahrens {
1570dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1571dfbb9432SGeorge Wilson 	uint64_t guid;
1572dfbb9432SGeorge Wilson 
1573b5989ec7Seschrock 	/*
1574b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1575b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1576e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1577b5989ec7Seschrock 	 * this case.
1578b5989ec7Seschrock 	 */
1579dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1580dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1581dfbb9432SGeorge Wilson 
1582dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1583dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1584dfbb9432SGeorge Wilson 
1585dfbb9432SGeorge Wilson 	/*
1586dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1587dfbb9432SGeorge Wilson 	 * in syncing context.
1588dfbb9432SGeorge Wilson 	 */
1589dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1590b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1591b5989ec7Seschrock 	else
1592dfbb9432SGeorge Wilson 		return (guid);
1593e9103aaeSGarrett D'Amore }
1594e9103aaeSGarrett D'Amore 
1595e9103aaeSGarrett D'Amore uint64_t
1596e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1597e9103aaeSGarrett D'Amore {
1598e9103aaeSGarrett D'Amore 	/*
1599e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1600e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1601e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1602e9103aaeSGarrett D'Amore 	 */
1603e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1604fa9e4066Sahrens }
1605fa9e4066Sahrens 
1606fa9e4066Sahrens uint64_t
1607fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1608fa9e4066Sahrens {
1609fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1610fa9e4066Sahrens }
1611fa9e4066Sahrens 
1612fa9e4066Sahrens uint64_t
1613fa9e4066Sahrens spa_first_txg(spa_t *spa)
1614fa9e4066Sahrens {
1615fa9e4066Sahrens 	return (spa->spa_first_txg);
1616fa9e4066Sahrens }
1617fa9e4066Sahrens 
1618b24ab676SJeff Bonwick uint64_t
1619b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1620b24ab676SJeff Bonwick {
1621b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1622b24ab676SJeff Bonwick }
1623b24ab676SJeff Bonwick 
162488b7b0f2SMatthew Ahrens pool_state_t
1625fa9e4066Sahrens spa_state(spa_t *spa)
1626fa9e4066Sahrens {
1627fa9e4066Sahrens 	return (spa->spa_state);
1628fa9e4066Sahrens }
1629fa9e4066Sahrens 
1630b16da2e2SGeorge Wilson spa_load_state_t
1631b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1632b16da2e2SGeorge Wilson {
1633b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1634b16da2e2SGeorge Wilson }
1635b16da2e2SGeorge Wilson 
1636fa9e4066Sahrens uint64_t
1637fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1638fa9e4066Sahrens {
1639fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1640fa9e4066Sahrens }
1641fa9e4066Sahrens 
1642fa9e4066Sahrens /* ARGSUSED */
1643fa9e4066Sahrens uint64_t
164461e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1645fa9e4066Sahrens {
164669962b56SMatthew Ahrens 	return (lsize * spa_asize_inflation);
164744cd46caSbillm }
164844cd46caSbillm 
16497d46dc6cSMatthew Ahrens /*
16507d46dc6cSMatthew Ahrens  * Return the amount of slop space in bytes.  It is 1/32 of the pool (3.2%),
16514b5c8e93SMatthew Ahrens  * or at least 128MB, unless that would cause it to be more than half the
16524b5c8e93SMatthew Ahrens  * pool size.
16537d46dc6cSMatthew Ahrens  *
16547d46dc6cSMatthew Ahrens  * See the comment above spa_slop_shift for details.
16557d46dc6cSMatthew Ahrens  */
16567d46dc6cSMatthew Ahrens uint64_t
16574b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa)
16584b5c8e93SMatthew Ahrens {
16597d46dc6cSMatthew Ahrens 	uint64_t space = spa_get_dspace(spa);
16604b5c8e93SMatthew Ahrens 	return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
16617d46dc6cSMatthew Ahrens }
16627d46dc6cSMatthew Ahrens 
1663485bbbf5SGeorge Wilson uint64_t
1664485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1665485bbbf5SGeorge Wilson {
1666485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1667485bbbf5SGeorge Wilson }
1668485bbbf5SGeorge Wilson 
1669485bbbf5SGeorge Wilson void
1670485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1671485bbbf5SGeorge Wilson {
1672485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1673485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1674485bbbf5SGeorge Wilson }
1675485bbbf5SGeorge Wilson 
16760a4e9518Sgw /*
16770a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
16780a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
16790a4e9518Sgw  */
16800a4e9518Sgw uint8_t
16810a4e9518Sgw spa_get_failmode(spa_t *spa)
16820a4e9518Sgw {
16830a4e9518Sgw 	return (spa->spa_failmode);
16840a4e9518Sgw }
16850a4e9518Sgw 
1686e14bb325SJeff Bonwick boolean_t
1687e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1688e14bb325SJeff Bonwick {
1689e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1690e14bb325SJeff Bonwick }
1691e14bb325SJeff Bonwick 
169244cd46caSbillm uint64_t
169344cd46caSbillm spa_version(spa_t *spa)
169444cd46caSbillm {
169544cd46caSbillm 	return (spa->spa_ubsync.ub_version);
169644cd46caSbillm }
169744cd46caSbillm 
1698b24ab676SJeff Bonwick boolean_t
1699b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1700b24ab676SJeff Bonwick {
1701b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1702b24ab676SJeff Bonwick }
1703b24ab676SJeff Bonwick 
1704b24ab676SJeff Bonwick metaslab_class_t *
1705b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1706b24ab676SJeff Bonwick {
1707b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1708b24ab676SJeff Bonwick }
1709b24ab676SJeff Bonwick 
1710b24ab676SJeff Bonwick metaslab_class_t *
1711b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1712b24ab676SJeff Bonwick {
1713b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1714b24ab676SJeff Bonwick }
1715b24ab676SJeff Bonwick 
1716bc9014e6SJustin Gibbs void
1717bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os)
1718bc9014e6SJustin Gibbs {
1719bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1720bc9014e6SJustin Gibbs 	list_insert_head(&spa->spa_evicting_os_list, os);
1721bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1722bc9014e6SJustin Gibbs }
1723bc9014e6SJustin Gibbs 
1724bc9014e6SJustin Gibbs void
1725bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1726bc9014e6SJustin Gibbs {
1727bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1728bc9014e6SJustin Gibbs 	list_remove(&spa->spa_evicting_os_list, os);
1729bc9014e6SJustin Gibbs 	cv_broadcast(&spa->spa_evicting_os_cv);
1730bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1731bc9014e6SJustin Gibbs }
1732bc9014e6SJustin Gibbs 
1733bc9014e6SJustin Gibbs void
1734bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa)
1735bc9014e6SJustin Gibbs {
1736bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1737bc9014e6SJustin Gibbs 	while (!list_is_empty(&spa->spa_evicting_os_list))
1738bc9014e6SJustin Gibbs 		cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1739bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1740bc9014e6SJustin Gibbs 
1741bc9014e6SJustin Gibbs 	dmu_buf_user_evict_wait();
1742bc9014e6SJustin Gibbs }
1743bc9014e6SJustin Gibbs 
174444cd46caSbillm int
174544cd46caSbillm spa_max_replication(spa_t *spa)
174644cd46caSbillm {
174744cd46caSbillm 	/*
1748e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
174944cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
175044cd46caSbillm 	 * replication level accordingly.
1751fa9e4066Sahrens 	 */
1752e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
175344cd46caSbillm 		return (1);
175444cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1755fa9e4066Sahrens }
1756fa9e4066Sahrens 
17573f9d6ad7SLin Ling int
17583f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
17593f9d6ad7SLin Ling {
17603f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
17613f9d6ad7SLin Ling }
17623f9d6ad7SLin Ling 
1763283b8460SGeorge.Wilson uint64_t
1764283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1765283b8460SGeorge.Wilson {
1766283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1767283b8460SGeorge.Wilson }
1768283b8460SGeorge.Wilson 
176999653d4eSeschrock uint64_t
1770b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
177199653d4eSeschrock {
1772b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1773b24ab676SJeff Bonwick 	uint64_t dsize = asize;
177499653d4eSeschrock 
1775b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
177699653d4eSeschrock 
1777b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1778b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1779b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
178099653d4eSeschrock 	}
1781b24ab676SJeff Bonwick 
1782b24ab676SJeff Bonwick 	return (dsize);
1783b24ab676SJeff Bonwick }
1784b24ab676SJeff Bonwick 
1785b24ab676SJeff Bonwick uint64_t
1786b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1787b24ab676SJeff Bonwick {
1788b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1789b24ab676SJeff Bonwick 
17905d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1791b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1792b24ab676SJeff Bonwick 
1793b24ab676SJeff Bonwick 	return (dsize);
1794b24ab676SJeff Bonwick }
1795b24ab676SJeff Bonwick 
1796b24ab676SJeff Bonwick uint64_t
1797b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1798b24ab676SJeff Bonwick {
1799b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1800b24ab676SJeff Bonwick 
1801b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1802b24ab676SJeff Bonwick 
18035d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1804b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1805b24ab676SJeff Bonwick 
1806e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1807b24ab676SJeff Bonwick 
1808b24ab676SJeff Bonwick 	return (dsize);
180999653d4eSeschrock }
181099653d4eSeschrock 
1811fa9e4066Sahrens /*
1812fa9e4066Sahrens  * ==========================================================================
1813fa9e4066Sahrens  * Initialization and Termination
1814fa9e4066Sahrens  * ==========================================================================
1815fa9e4066Sahrens  */
1816fa9e4066Sahrens 
1817fa9e4066Sahrens static int
1818fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1819fa9e4066Sahrens {
1820fa9e4066Sahrens 	const spa_t *s1 = a1;
1821fa9e4066Sahrens 	const spa_t *s2 = a2;
1822fa9e4066Sahrens 	int s;
1823fa9e4066Sahrens 
1824fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1825fa9e4066Sahrens 	if (s > 0)
1826fa9e4066Sahrens 		return (1);
1827fa9e4066Sahrens 	if (s < 0)
1828fa9e4066Sahrens 		return (-1);
1829fa9e4066Sahrens 	return (0);
1830fa9e4066Sahrens }
1831fa9e4066Sahrens 
18320373e76bSbonwick int
18330373e76bSbonwick spa_busy(void)
18340373e76bSbonwick {
18350373e76bSbonwick 	return (spa_active_count);
18360373e76bSbonwick }
18370373e76bSbonwick 
1838e7cbe64fSgw void
1839e7cbe64fSgw spa_boot_init()
1840e7cbe64fSgw {
1841e7cbe64fSgw 	spa_config_load();
1842e7cbe64fSgw }
1843e7cbe64fSgw 
1844fa9e4066Sahrens void
1845fa9e4066Sahrens spa_init(int mode)
1846fa9e4066Sahrens {
1847fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1848c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1849fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1850fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1851fa9e4066Sahrens 
1852fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1853fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1854fa9e4066Sahrens 
1855fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1856fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1857fa94a07fSbrendan 
1858fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1859fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
186099653d4eSeschrock 
18618ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1862fa9e4066Sahrens 
1863283b8460SGeorge.Wilson #ifdef _KERNEL
1864283b8460SGeorge.Wilson 	spa_arch_init();
1865283b8460SGeorge.Wilson #else
1866cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1867cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1868cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1869cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1870cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1871cd1c8b85SMatthew Ahrens 		} else {
1872cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1873cd1c8b85SMatthew Ahrens 		}
1874cd1c8b85SMatthew Ahrens 	}
1875cd1c8b85SMatthew Ahrens #endif
1876cd1c8b85SMatthew Ahrens 
1877fa9e4066Sahrens 	refcount_init();
1878fa9e4066Sahrens 	unique_init();
18790713e232SGeorge Wilson 	range_tree_init();
18808363e80aSGeorge Wilson 	metaslab_alloc_trace_init();
1881fa9e4066Sahrens 	zio_init();
1882fa9e4066Sahrens 	dmu_init();
1883fa9e4066Sahrens 	zil_init();
188487db74c1Sek 	vdev_cache_stat_init();
188591ebeef5Sahrens 	zfs_prop_init();
1886990b4856Slling 	zpool_prop_init();
1887ad135b5dSChristopher Siden 	zpool_feature_init();
1888fa9e4066Sahrens 	spa_config_load();
1889e14bb325SJeff Bonwick 	l2arc_start();
1890fa9e4066Sahrens }
1891fa9e4066Sahrens 
1892fa9e4066Sahrens void
1893fa9e4066Sahrens spa_fini(void)
1894fa9e4066Sahrens {
1895e14bb325SJeff Bonwick 	l2arc_stop();
1896e14bb325SJeff Bonwick 
1897fa9e4066Sahrens 	spa_evict_all();
1898fa9e4066Sahrens 
189987db74c1Sek 	vdev_cache_stat_fini();
1900fa9e4066Sahrens 	zil_fini();
1901fa9e4066Sahrens 	dmu_fini();
1902fa9e4066Sahrens 	zio_fini();
19038363e80aSGeorge Wilson 	metaslab_alloc_trace_fini();
19040713e232SGeorge Wilson 	range_tree_fini();
190591ebeef5Sahrens 	unique_fini();
1906fa9e4066Sahrens 	refcount_fini();
1907fa9e4066Sahrens 
1908fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
190999653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1910fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1911fa9e4066Sahrens 
1912fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1913fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1914c25056deSgw 	mutex_destroy(&spa_spare_lock);
1915fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1916fa9e4066Sahrens }
19176ce0521aSperrin 
19186ce0521aSperrin /*
19196ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
19206ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
19216ce0521aSperrin  * performance and not correctness
19226ce0521aSperrin  */
19236ce0521aSperrin boolean_t
19246ce0521aSperrin spa_has_slogs(spa_t *spa)
19256ce0521aSperrin {
19266ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
19276ce0521aSperrin }
1928bf82a41bSeschrock 
1929b24ab676SJeff Bonwick spa_log_state_t
1930b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1931b24ab676SJeff Bonwick {
1932b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1933b24ab676SJeff Bonwick }
1934b24ab676SJeff Bonwick 
1935b24ab676SJeff Bonwick void
1936b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1937b24ab676SJeff Bonwick {
1938b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1939b24ab676SJeff Bonwick }
1940b24ab676SJeff Bonwick 
1941bf82a41bSeschrock boolean_t
1942bf82a41bSeschrock spa_is_root(spa_t *spa)
1943bf82a41bSeschrock {
1944bf82a41bSeschrock 	return (spa->spa_is_root);
1945bf82a41bSeschrock }
19468ad4d6ddSJeff Bonwick 
19478ad4d6ddSJeff Bonwick boolean_t
19488ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
19498ad4d6ddSJeff Bonwick {
19508ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
19518ad4d6ddSJeff Bonwick }
19528ad4d6ddSJeff Bonwick 
195373527f44SAlex Reece /*
195473527f44SAlex Reece  * Returns true if there is a pending sync task in any of the current
195573527f44SAlex Reece  * syncing txg, the current quiescing txg, or the current open txg.
195673527f44SAlex Reece  */
195773527f44SAlex Reece boolean_t
195873527f44SAlex Reece spa_has_pending_synctask(spa_t *spa)
195973527f44SAlex Reece {
196073527f44SAlex Reece 	return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
196173527f44SAlex Reece }
196273527f44SAlex Reece 
19638ad4d6ddSJeff Bonwick int
19648ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
19658ad4d6ddSJeff Bonwick {
19668ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
19678ad4d6ddSJeff Bonwick }
1968b24ab676SJeff Bonwick 
1969b24ab676SJeff Bonwick uint64_t
1970b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1971b24ab676SJeff Bonwick {
1972b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1973b24ab676SJeff Bonwick }
1974b24ab676SJeff Bonwick 
1975b24ab676SJeff Bonwick uint64_t
1976b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1977b24ab676SJeff Bonwick {
1978b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1979b24ab676SJeff Bonwick }
1980b24ab676SJeff Bonwick 
1981b24ab676SJeff Bonwick objset_t *
1982b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1983b24ab676SJeff Bonwick {
1984b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1985b24ab676SJeff Bonwick }
1986b24ab676SJeff Bonwick 
1987b24ab676SJeff Bonwick enum zio_checksum
1988b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1989b24ab676SJeff Bonwick {
1990b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1991b24ab676SJeff Bonwick }
19923f9d6ad7SLin Ling 
19933f9d6ad7SLin Ling /*
19943f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
19953f9d6ad7SLin Ling  */
19963f9d6ad7SLin Ling void
19973f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
19983f9d6ad7SLin Ling {
19993f9d6ad7SLin Ling 	/* data not stored on disk */
20003f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
20013f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
20023f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
20033f9d6ad7SLin Ling }
20043f9d6ad7SLin Ling 
20053f9d6ad7SLin Ling /*
20063f9d6ad7SLin Ling  * Get scan stats for zpool status reports
20073f9d6ad7SLin Ling  */
20083f9d6ad7SLin Ling int
20093f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
20103f9d6ad7SLin Ling {
20113f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
20123f9d6ad7SLin Ling 
20133f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2014be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
20153f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
20163f9d6ad7SLin Ling 
20173f9d6ad7SLin Ling 	/* data stored on disk */
20183f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
20193f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
20203f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
20213f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
20223f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
20233f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
20243f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
20253f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
20263f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
20273f9d6ad7SLin Ling 
20283f9d6ad7SLin Ling 	/* data not stored on disk */
20293f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
20303f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
20313f9d6ad7SLin Ling 
20323f9d6ad7SLin Ling 	return (0);
20333f9d6ad7SLin Ling }
203409c9d376SGeorge Wilson 
203509c9d376SGeorge Wilson boolean_t
203609c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
203709c9d376SGeorge Wilson {
203809c9d376SGeorge Wilson 	return (spa->spa_debug);
203909c9d376SGeorge Wilson }
2040b5152584SMatthew Ahrens 
2041b5152584SMatthew Ahrens int
2042b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa)
2043b5152584SMatthew Ahrens {
2044b5152584SMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2045b5152584SMatthew Ahrens 		return (SPA_MAXBLOCKSIZE);
2046b5152584SMatthew Ahrens 	else
2047b5152584SMatthew Ahrens 		return (SPA_OLD_MAXBLOCKSIZE);
2048b5152584SMatthew Ahrens }
2049