xref: /titanic_41/usr/src/uts/common/fs/zfs/spa_misc.c (revision 005d1a3dd72ef324e7265f4a05e4168e4e39b8e2)
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*005d1a3dSMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
250521c5ebSJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/spa_impl.h>
30283b8460SGeorge.Wilson #include <sys/spa_boot.h>
31fa9e4066Sahrens #include <sys/zio.h>
32fa9e4066Sahrens #include <sys/zio_checksum.h>
33fa9e4066Sahrens #include <sys/zio_compress.h>
34fa9e4066Sahrens #include <sys/dmu.h>
35fa9e4066Sahrens #include <sys/dmu_tx.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/zil.h>
38fa9e4066Sahrens #include <sys/vdev_impl.h>
39fa9e4066Sahrens #include <sys/metaslab.h>
40fa9e4066Sahrens #include <sys/uberblock_impl.h>
41fa9e4066Sahrens #include <sys/txg.h>
42fa9e4066Sahrens #include <sys/avl.h>
43fa9e4066Sahrens #include <sys/unique.h>
44fa9e4066Sahrens #include <sys/dsl_pool.h>
45fa9e4066Sahrens #include <sys/dsl_dir.h>
46fa9e4066Sahrens #include <sys/dsl_prop.h>
473f9d6ad7SLin Ling #include <sys/dsl_scan.h>
48fa9e4066Sahrens #include <sys/fs/zfs.h>
496ce0521aSperrin #include <sys/metaslab_impl.h>
50e14bb325SJeff Bonwick #include <sys/arc.h>
51485bbbf5SGeorge Wilson #include <sys/ddt.h>
5291ebeef5Sahrens #include "zfs_prop.h"
53ad135b5dSChristopher Siden #include "zfeature_common.h"
54fa9e4066Sahrens 
55fa9e4066Sahrens /*
56fa9e4066Sahrens  * SPA locking
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
59fa9e4066Sahrens  *
60fa9e4066Sahrens  * spa_namespace_lock (global mutex)
61fa9e4066Sahrens  *
62fa9e4066Sahrens  *	This lock must be acquired to do any of the following:
63fa9e4066Sahrens  *
64fa9e4066Sahrens  *		- Lookup a spa_t by name
65fa9e4066Sahrens  *		- Add or remove a spa_t from the namespace
66fa9e4066Sahrens  *		- Increase spa_refcount from non-zero
67fa9e4066Sahrens  *		- Check if spa_refcount is zero
68fa9e4066Sahrens  *		- Rename a spa_t
69ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
70fa9e4066Sahrens  *		- Held for the duration of create/destroy/import/export
71fa9e4066Sahrens  *
72fa9e4066Sahrens  *	It does not need to handle recursion.  A create or destroy may
73fa9e4066Sahrens  *	reference objects (files or zvols) in other pools, but by
74fa9e4066Sahrens  *	definition they must have an existing reference, and will never need
75fa9e4066Sahrens  *	to lookup a spa_t by name.
76fa9e4066Sahrens  *
77fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
78fa9e4066Sahrens  *
79fa9e4066Sahrens  *	This reference count keep track of any active users of the spa_t.  The
80fa9e4066Sahrens  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
81fa9e4066Sahrens  *	the refcount is never really 'zero' - opening a pool implicitly keeps
82088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
83fa9e4066Sahrens  *	present the image of a zero/non-zero value to consumers.
84fa9e4066Sahrens  *
85e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
86fa9e4066Sahrens  *
8791ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
8891ebeef5Sahrens  *	the following circumstances:
89fa9e4066Sahrens  *
90fa9e4066Sahrens  *		- RW_READER to perform I/O to the spa
91fa9e4066Sahrens  *		- RW_WRITER to change the vdev config
92fa9e4066Sahrens  *
93fa9e4066Sahrens  * The locking order is fairly straightforward:
94fa9e4066Sahrens  *
95fa9e4066Sahrens  *		spa_namespace_lock	->	spa_refcount
96fa9e4066Sahrens  *
97fa9e4066Sahrens  *	The namespace lock must be acquired to increase the refcount from 0
98fa9e4066Sahrens  *	or to check if it is zero.
99fa9e4066Sahrens  *
100e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
101fa9e4066Sahrens  *
102fa9e4066Sahrens  *	There must be at least one valid reference on the spa_t to acquire
103fa9e4066Sahrens  *	the config lock.
104fa9e4066Sahrens  *
105e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
106fa9e4066Sahrens  *
107ea8dc4b6Seschrock  *	The namespace lock must always be taken before the config lock.
108fa9e4066Sahrens  *
109fa9e4066Sahrens  *
110e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
111fa9e4066Sahrens  *
112e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
113e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
114fa9e4066Sahrens  *
115fa9e4066Sahrens  *	spa_lookup()		Lookup a spa_t by name.
116fa9e4066Sahrens  *
117fa9e4066Sahrens  *	spa_add()		Create a new spa_t in the namespace.
118fa9e4066Sahrens  *
119fa9e4066Sahrens  *	spa_remove()		Remove a spa_t from the namespace.  This also
120fa9e4066Sahrens  *				frees up any memory associated with the spa_t.
121fa9e4066Sahrens  *
122fa9e4066Sahrens  *	spa_next()		Returns the next spa_t in the system, or the
123fa9e4066Sahrens  *				first if NULL is passed.
124fa9e4066Sahrens  *
125fa9e4066Sahrens  *	spa_evict_all()		Shutdown and remove all spa_t structures in
126fa9e4066Sahrens  *				the system.
127fa9e4066Sahrens  *
128ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
129fa9e4066Sahrens  *
130fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
131fa9e4066Sahrens  *
132fa9e4066Sahrens  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
133fa9e4066Sahrens  *				called with spa_namespace_lock held if the
134fa9e4066Sahrens  *				refcount is currently zero.
135fa9e4066Sahrens  *
136fa9e4066Sahrens  *	spa_close()		Remove a reference from the spa_t.  This will
137fa9e4066Sahrens  *				not free the spa_t or remove it from the
138fa9e4066Sahrens  *				namespace.  No locking is required.
139fa9e4066Sahrens  *
140fa9e4066Sahrens  *	spa_refcount_zero()	Returns true if the refcount is currently
141fa9e4066Sahrens  *				zero.  Must be called with spa_namespace_lock
142fa9e4066Sahrens  *				held.
143fa9e4066Sahrens  *
144e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
145e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
146e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
147e14bb325SJeff Bonwick  *
148e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
149e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
150e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
151e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
152e14bb325SJeff Bonwick  *
153e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
154e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
155e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
156e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
157e14bb325SJeff Bonwick  *
158e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
159e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
160e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
161e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
162e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
163e14bb325SJeff Bonwick  *
164e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
165e14bb325SJeff Bonwick  *
166e14bb325SJeff Bonwick  * SCL_CONFIG
167e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
168e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
169e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
170e14bb325SJeff Bonwick  *
171e14bb325SJeff Bonwick  * SCL_STATE
172e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
173e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
174e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
175e14bb325SJeff Bonwick  *
176e14bb325SJeff Bonwick  * SCL_ALLOC
177e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
178e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
179e14bb325SJeff Bonwick  *
180e14bb325SJeff Bonwick  * SCL_ZIO
181e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
182e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
183e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
184e14bb325SJeff Bonwick  *
185e14bb325SJeff Bonwick  * SCL_FREE
186e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
187e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
188e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
189e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
190e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
191e14bb325SJeff Bonwick  *
192e14bb325SJeff Bonwick  * SCL_VDEV
193e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
194b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
195e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
196e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
197e14bb325SJeff Bonwick  *
198e14bb325SJeff Bonwick  * In addition, the following rules apply:
199e14bb325SJeff Bonwick  *
200e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
201e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
202e14bb325SJeff Bonwick  *
203e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
204e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
205e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
206e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
207e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
208fa9e4066Sahrens  *
209ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
210fa9e4066Sahrens  *
211ea8dc4b6Seschrock  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
212ea8dc4b6Seschrock  *				for writing.
213fa9e4066Sahrens  *
214fa9e4066Sahrens  *	spa_vdev_exit()		Release the config lock, wait for all I/O
215ea8dc4b6Seschrock  *				to complete, sync the updated configs to the
216ea8dc4b6Seschrock  *				cache, and release the namespace lock.
217fa9e4066Sahrens  *
218e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
219e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
220e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
221e14bb325SJeff Bonwick  *
222ad135b5dSChristopher Siden  * spa_rename() is also implemented within this file since it requires
223e14bb325SJeff Bonwick  * manipulation of the namespace.
224fa9e4066Sahrens  */
225fa9e4066Sahrens 
226fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
227fa9e4066Sahrens kmutex_t spa_namespace_lock;
228fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2290373e76bSbonwick static int spa_active_count;
230416e0cd8Sek110237 int spa_max_replication_override = SPA_DVAS_PER_BP;
231fa9e4066Sahrens 
23299653d4eSeschrock static kmutex_t spa_spare_lock;
23339c23413Seschrock static avl_tree_t spa_spare_avl;
234fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
235fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23699653d4eSeschrock 
237fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2388ad4d6ddSJeff Bonwick int spa_mode_global;
239fa9e4066Sahrens 
240fa9e4066Sahrens #ifdef ZFS_DEBUG
2413b2aab18SMatthew Ahrens /* Everything except dprintf and spa is on by default in debug builds */
2423b2aab18SMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
243fa9e4066Sahrens #else
244fa9e4066Sahrens int zfs_flags = 0;
245fa9e4066Sahrens #endif
246fa9e4066Sahrens 
2470125049cSahrens /*
2480125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2490125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2500125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
251d61a30d6SMatthew Ahrens  * This should only be used as a last resort, as it typically results
252d61a30d6SMatthew Ahrens  * in leaked space, or worse.
2530125049cSahrens  */
254994617e7SMatthew Ahrens boolean_t zfs_recover = B_FALSE;
255994617e7SMatthew Ahrens 
256994617e7SMatthew Ahrens /*
257994617e7SMatthew Ahrens  * If destroy encounters an EIO while reading metadata (e.g. indirect
258994617e7SMatthew Ahrens  * blocks), space referenced by the missing metadata can not be freed.
259994617e7SMatthew Ahrens  * Normally this causes the background destroy to become "stalled", as
260994617e7SMatthew Ahrens  * it is unable to make forward progress.  While in this stalled state,
261994617e7SMatthew Ahrens  * all remaining space to free from the error-encountering filesystem is
262994617e7SMatthew Ahrens  * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
263994617e7SMatthew Ahrens  * permanently leak the space from indirect blocks that can not be read,
264994617e7SMatthew Ahrens  * and continue to free everything else that it can.
265994617e7SMatthew Ahrens  *
266994617e7SMatthew Ahrens  * The default, "stalling" behavior is useful if the storage partially
267994617e7SMatthew Ahrens  * fails (i.e. some but not all i/os fail), and then later recovers.  In
268994617e7SMatthew Ahrens  * this case, we will be able to continue pool operations while it is
269994617e7SMatthew Ahrens  * partially failed, and when it recovers, we can continue to free the
270994617e7SMatthew Ahrens  * space, with no leaks.  However, note that this case is actually
271994617e7SMatthew Ahrens  * fairly rare.
272994617e7SMatthew Ahrens  *
273994617e7SMatthew Ahrens  * Typically pools either (a) fail completely (but perhaps temporarily,
274994617e7SMatthew Ahrens  * e.g. a top-level vdev going offline), or (b) have localized,
275994617e7SMatthew Ahrens  * permanent errors (e.g. disk returns the wrong data due to bit flip or
276994617e7SMatthew Ahrens  * firmware bug).  In case (a), this setting does not matter because the
277994617e7SMatthew Ahrens  * pool will be suspended and the sync thread will not be able to make
278994617e7SMatthew Ahrens  * forward progress regardless.  In case (b), because the error is
279994617e7SMatthew Ahrens  * permanent, the best we can do is leak the minimum amount of space,
280994617e7SMatthew Ahrens  * which is what setting this flag will do.  Therefore, it is reasonable
281994617e7SMatthew Ahrens  * for this flag to normally be set, but we chose the more conservative
282994617e7SMatthew Ahrens  * approach of not setting it, so that there is no possibility of
283994617e7SMatthew Ahrens  * leaking space in the "partial temporary" failure case.
284994617e7SMatthew Ahrens  */
285994617e7SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE;
2860125049cSahrens 
28769962b56SMatthew Ahrens /*
28869962b56SMatthew Ahrens  * Expiration time in milliseconds. This value has two meanings. First it is
28969962b56SMatthew Ahrens  * used to determine when the spa_deadman() logic should fire. By default the
29069962b56SMatthew Ahrens  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
29169962b56SMatthew Ahrens  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
29269962b56SMatthew Ahrens  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
29369962b56SMatthew Ahrens  * in a system panic.
29469962b56SMatthew Ahrens  */
29569962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL;
296283b8460SGeorge.Wilson 
297283b8460SGeorge.Wilson /*
29869962b56SMatthew Ahrens  * Check time in milliseconds. This defines the frequency at which we check
29969962b56SMatthew Ahrens  * for hung I/O.
300283b8460SGeorge.Wilson  */
30169962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL;
302283b8460SGeorge.Wilson 
303283b8460SGeorge.Wilson /*
304283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
305283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
306283b8460SGeorge.Wilson  */
307283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
308283b8460SGeorge.Wilson 
30969962b56SMatthew Ahrens /*
31069962b56SMatthew Ahrens  * The worst case is single-sector max-parity RAID-Z blocks, in which
31169962b56SMatthew Ahrens  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
31269962b56SMatthew Ahrens  * times the size; so just assume that.  Add to this the fact that
31369962b56SMatthew Ahrens  * we can have up to 3 DVAs per bp, and one more factor of 2 because
31469962b56SMatthew Ahrens  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
31569962b56SMatthew Ahrens  * the worst case is:
31669962b56SMatthew Ahrens  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
31769962b56SMatthew Ahrens  */
31869962b56SMatthew Ahrens int spa_asize_inflation = 24;
319fa9e4066Sahrens 
320fa9e4066Sahrens /*
32143d5e60eSMatthew Ahrens  * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
32243d5e60eSMatthew Ahrens  * the pool to be consumed.  This ensures that we don't run the pool
32343d5e60eSMatthew Ahrens  * completely out of space, due to unaccounted changes (e.g. to the MOS).
32443d5e60eSMatthew Ahrens  * It also limits the worst-case time to allocate space.  If we have
32543d5e60eSMatthew Ahrens  * less than this amount of free space, most ZPL operations (e.g. write,
32643d5e60eSMatthew Ahrens  * create) will return ENOSPC.
32743d5e60eSMatthew Ahrens  *
32843d5e60eSMatthew Ahrens  * Certain operations (e.g. file removal, most administrative actions) can
32943d5e60eSMatthew Ahrens  * use half the slop space.  They will only return ENOSPC if less than half
33043d5e60eSMatthew Ahrens  * the slop space is free.  Typically, once the pool has less than the slop
33143d5e60eSMatthew Ahrens  * space free, the user will use these operations to free up space in the pool.
33243d5e60eSMatthew Ahrens  * These are the operations that call dsl_pool_adjustedsize() with the netfree
33343d5e60eSMatthew Ahrens  * argument set to TRUE.
33443d5e60eSMatthew Ahrens  *
33543d5e60eSMatthew Ahrens  * A very restricted set of operations are always permitted, regardless of
33643d5e60eSMatthew Ahrens  * the amount of free space.  These are the operations that call
33743d5e60eSMatthew Ahrens  * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy".  If these
33843d5e60eSMatthew Ahrens  * operations result in a net increase in the amount of space used,
33943d5e60eSMatthew Ahrens  * it is possible to run the pool completely out of space, causing it to
34043d5e60eSMatthew Ahrens  * be permanently read-only.
34143d5e60eSMatthew Ahrens  *
34243d5e60eSMatthew Ahrens  * See also the comments in zfs_space_check_t.
34343d5e60eSMatthew Ahrens  */
34443d5e60eSMatthew Ahrens int spa_slop_shift = 5;
34543d5e60eSMatthew Ahrens 
34643d5e60eSMatthew Ahrens /*
347fa9e4066Sahrens  * ==========================================================================
348e05725b1Sbonwick  * SPA config locking
349e05725b1Sbonwick  * ==========================================================================
350e05725b1Sbonwick  */
351e05725b1Sbonwick static void
spa_config_lock_init(spa_t * spa)352e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
353e05725b1Sbonwick {
354e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
355e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
356e05725b1Sbonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
357e05725b1Sbonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
3583b2aab18SMatthew Ahrens 		refcount_create_untracked(&scl->scl_count);
359e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
360e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
361e14bb325SJeff Bonwick 	}
362e05725b1Sbonwick }
363e05725b1Sbonwick 
364e05725b1Sbonwick static void
spa_config_lock_destroy(spa_t * spa)365e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
366e05725b1Sbonwick {
367e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
368e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
369e05725b1Sbonwick 		mutex_destroy(&scl->scl_lock);
370e05725b1Sbonwick 		cv_destroy(&scl->scl_cv);
371e05725b1Sbonwick 		refcount_destroy(&scl->scl_count);
372e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
373e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
374e14bb325SJeff Bonwick 	}
375e05725b1Sbonwick }
376e05725b1Sbonwick 
377e14bb325SJeff Bonwick int
spa_config_tryenter(spa_t * spa,int locks,void * tag,krw_t rw)378e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
379e05725b1Sbonwick {
380e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
381e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
382e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
383e14bb325SJeff Bonwick 			continue;
384e05725b1Sbonwick 		mutex_enter(&scl->scl_lock);
385e05725b1Sbonwick 		if (rw == RW_READER) {
386e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
387e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
388e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
389e14bb325SJeff Bonwick 				return (0);
390e14bb325SJeff Bonwick 			}
391e05725b1Sbonwick 		} else {
392e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
393e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
394e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
395e14bb325SJeff Bonwick 				spa_config_exit(spa, locks ^ (1 << i), tag);
396e14bb325SJeff Bonwick 				return (0);
397e14bb325SJeff Bonwick 			}
398e05725b1Sbonwick 			scl->scl_writer = curthread;
399e05725b1Sbonwick 		}
400e05725b1Sbonwick 		(void) refcount_add(&scl->scl_count, tag);
401e05725b1Sbonwick 		mutex_exit(&scl->scl_lock);
402e05725b1Sbonwick 	}
403e14bb325SJeff Bonwick 	return (1);
404e14bb325SJeff Bonwick }
405e05725b1Sbonwick 
406e05725b1Sbonwick void
spa_config_enter(spa_t * spa,int locks,void * tag,krw_t rw)407e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
408e05725b1Sbonwick {
409f64c0e34SEric Taylor 	int wlocks_held = 0;
410f64c0e34SEric Taylor 
4113b2aab18SMatthew Ahrens 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4123b2aab18SMatthew Ahrens 
413e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
414e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
415f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
416f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
417e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
418e14bb325SJeff Bonwick 			continue;
419e05725b1Sbonwick 		mutex_enter(&scl->scl_lock);
420e14bb325SJeff Bonwick 		if (rw == RW_READER) {
421e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
422e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
423e05725b1Sbonwick 			}
424e14bb325SJeff Bonwick 		} else {
425e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
426e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
427e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
428e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
429e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
430e14bb325SJeff Bonwick 			}
431e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
432e14bb325SJeff Bonwick 		}
433e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
434e05725b1Sbonwick 		mutex_exit(&scl->scl_lock);
435e05725b1Sbonwick 	}
436f64c0e34SEric Taylor 	ASSERT(wlocks_held <= locks);
437e14bb325SJeff Bonwick }
438e05725b1Sbonwick 
439e14bb325SJeff Bonwick void
spa_config_exit(spa_t * spa,int locks,void * tag)440e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
441e05725b1Sbonwick {
442e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
443e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
444e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
445e14bb325SJeff Bonwick 			continue;
446e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
447e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
448e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
449e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
450e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
451e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
452e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
453e14bb325SJeff Bonwick 		}
454e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
455e14bb325SJeff Bonwick 	}
456e14bb325SJeff Bonwick }
457e05725b1Sbonwick 
458e14bb325SJeff Bonwick int
spa_config_held(spa_t * spa,int locks,krw_t rw)459e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
460e14bb325SJeff Bonwick {
461e14bb325SJeff Bonwick 	int locks_held = 0;
462e14bb325SJeff Bonwick 
463e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
464e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
465e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
466e14bb325SJeff Bonwick 			continue;
467e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
468e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
469e14bb325SJeff Bonwick 			locks_held |= 1 << i;
470e14bb325SJeff Bonwick 	}
471e14bb325SJeff Bonwick 
472e14bb325SJeff Bonwick 	return (locks_held);
473e05725b1Sbonwick }
474e05725b1Sbonwick 
475e05725b1Sbonwick /*
476e05725b1Sbonwick  * ==========================================================================
477fa9e4066Sahrens  * SPA namespace functions
478fa9e4066Sahrens  * ==========================================================================
479fa9e4066Sahrens  */
480fa9e4066Sahrens 
481fa9e4066Sahrens /*
482fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
483fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
484fa9e4066Sahrens  */
485fa9e4066Sahrens spa_t *
spa_lookup(const char * name)486fa9e4066Sahrens spa_lookup(const char *name)
487fa9e4066Sahrens {
488e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
489e14bb325SJeff Bonwick 	spa_t *spa;
490fa9e4066Sahrens 	avl_index_t where;
49140feaa91Sahrens 	char *cp;
492fa9e4066Sahrens 
493fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
494fa9e4066Sahrens 
4953b2aab18SMatthew Ahrens 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
4963b2aab18SMatthew Ahrens 
49740feaa91Sahrens 	/*
49840feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
49940feaa91Sahrens 	 * just use that.
50040feaa91Sahrens 	 */
501e69d9941SMatthew Ahrens 	cp = strpbrk(search.spa_name, "/@#");
5023b2aab18SMatthew Ahrens 	if (cp != NULL)
50340feaa91Sahrens 		*cp = '\0';
50440feaa91Sahrens 
505fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
506fa9e4066Sahrens 
507fa9e4066Sahrens 	return (spa);
508fa9e4066Sahrens }
509fa9e4066Sahrens 
510fa9e4066Sahrens /*
511283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
512283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
513283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
514283b8460SGeorge.Wilson  */
515283b8460SGeorge.Wilson void
spa_deadman(void * arg)516283b8460SGeorge.Wilson spa_deadman(void *arg)
517283b8460SGeorge.Wilson {
518283b8460SGeorge.Wilson 	spa_t *spa = arg;
519283b8460SGeorge.Wilson 
5200713e232SGeorge Wilson 	/*
5210713e232SGeorge Wilson 	 * Disable the deadman timer if the pool is suspended.
5220713e232SGeorge Wilson 	 */
5230713e232SGeorge Wilson 	if (spa_suspended(spa)) {
5240713e232SGeorge Wilson 		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5250713e232SGeorge Wilson 		return;
5260713e232SGeorge Wilson 	}
5270713e232SGeorge Wilson 
528283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
529283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
530283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
531283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
532283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
533283b8460SGeorge.Wilson }
534283b8460SGeorge.Wilson 
535283b8460SGeorge.Wilson /*
536fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
537fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
538fa9e4066Sahrens  * exist by calling spa_lookup() first.
539fa9e4066Sahrens  */
540fa9e4066Sahrens spa_t *
spa_add(const char * name,nvlist_t * config,const char * altroot)541468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
542fa9e4066Sahrens {
543fa9e4066Sahrens 	spa_t *spa;
544c5904d13Seschrock 	spa_config_dirent_t *dp;
545283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
546283b8460SGeorge.Wilson 	cyc_time_t when;
547fa9e4066Sahrens 
548fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
549fa9e4066Sahrens 
550fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
551fa9e4066Sahrens 
552c25056deSgw25295 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
553c25056deSgw25295 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
55435a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
5550521c5ebSJustin Gibbs 	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
556c25056deSgw25295 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
55735a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
558c25056deSgw25295 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
55935a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
560a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
561a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
562c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
563c25056deSgw25295 
564c25056deSgw25295 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
5650521c5ebSJustin Gibbs 	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
56635a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
567c25056deSgw25295 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
568e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
569c25056deSgw25295 
570b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
571cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
572b24ab676SJeff Bonwick 
573e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
574fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
575fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
5760373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
577468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
57835a5a358SJonathan Adams 	spa->spa_proc = &p0;
57935a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
580fa9e4066Sahrens 
581283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
582283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
583283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
584283b8460SGeorge.Wilson 
58569962b56SMatthew Ahrens 	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
586283b8460SGeorge.Wilson 
587283b8460SGeorge.Wilson 	/*
588283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
589283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
590283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
59169962b56SMatthew Ahrens 	 * Instead wait for 5 seconds before checking again.
592283b8460SGeorge.Wilson 	 */
59369962b56SMatthew Ahrens 	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
594283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
595283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
596283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
597283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
598283b8460SGeorge.Wilson 
599fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
600e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
601fa9e4066Sahrens 
602fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
603fa9e4066Sahrens 
6040373e76bSbonwick 	/*
6050373e76bSbonwick 	 * Set the alternate root, if there is one.
6060373e76bSbonwick 	 */
6070373e76bSbonwick 	if (altroot) {
6080373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
6090373e76bSbonwick 		spa_active_count++;
6100373e76bSbonwick 	}
6110373e76bSbonwick 
612c5904d13Seschrock 	/*
613c5904d13Seschrock 	 * Every pool starts with the default cachefile
614c5904d13Seschrock 	 */
615c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
616c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
617c5904d13Seschrock 
618c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
619ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
620c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
621c5904d13Seschrock 
6224b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
6234b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
6244b964adaSGeorge Wilson 
625ad135b5dSChristopher Siden 	if (config != NULL) {
626ad135b5dSChristopher Siden 		nvlist_t *features;
627ad135b5dSChristopher Siden 
628ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
629ad135b5dSChristopher Siden 		    &features) == 0) {
630ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
631ad135b5dSChristopher Siden 			    0) == 0);
632ad135b5dSChristopher Siden 		}
633ad135b5dSChristopher Siden 
634468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
635ad135b5dSChristopher Siden 	}
636ad135b5dSChristopher Siden 
637ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
638ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
639ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
640ad135b5dSChristopher Siden 	}
641468c413aSTim Haley 
642c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
643c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
644c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
645c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
646c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
647c3a66015SMatthew Ahrens 	}
648c3a66015SMatthew Ahrens 
6493b2aab18SMatthew Ahrens 	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
6503b2aab18SMatthew Ahrens 
651*005d1a3dSMatthew Ahrens 	spa->spa_min_ashift = INT_MAX;
652*005d1a3dSMatthew Ahrens 	spa->spa_max_ashift = 0;
653*005d1a3dSMatthew Ahrens 
65449064978SMax Grossman 	/*
65549064978SMax Grossman 	 * As a pool is being created, treat all features as disabled by
65649064978SMax Grossman 	 * setting SPA_FEATURE_DISABLED for all entries in the feature
65749064978SMax Grossman 	 * refcount cache.
65849064978SMax Grossman 	 */
65949064978SMax Grossman 	for (int i = 0; i < SPA_FEATURES; i++) {
66049064978SMax Grossman 		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
66149064978SMax Grossman 	}
66249064978SMax Grossman 
663fa9e4066Sahrens 	return (spa);
664fa9e4066Sahrens }
665fa9e4066Sahrens 
666fa9e4066Sahrens /*
667fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
668fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
669fa9e4066Sahrens  * deactivated.
670fa9e4066Sahrens  */
671fa9e4066Sahrens void
spa_remove(spa_t * spa)672fa9e4066Sahrens spa_remove(spa_t *spa)
673fa9e4066Sahrens {
674c5904d13Seschrock 	spa_config_dirent_t *dp;
675c5904d13Seschrock 
676fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
677fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
6780521c5ebSJustin Gibbs 	ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
679fa9e4066Sahrens 
6801195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
6811195e687SMark J Musante 
682fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
683fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
684fa9e4066Sahrens 
6850373e76bSbonwick 	if (spa->spa_root) {
686fa9e4066Sahrens 		spa_strfree(spa->spa_root);
6870373e76bSbonwick 		spa_active_count--;
6880373e76bSbonwick 	}
689fa9e4066Sahrens 
690c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
691c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
692c5904d13Seschrock 		if (dp->scd_path != NULL)
693c5904d13Seschrock 			spa_strfree(dp->scd_path);
694c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
695c5904d13Seschrock 	}
696c5904d13Seschrock 
697c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
6982f8aaab3Seschrock 
699ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
7004b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
701fa9e4066Sahrens 	spa_config_set(spa, NULL);
702fa9e4066Sahrens 
703283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
704283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
705283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
706283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
707283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
708283b8460SGeorge.Wilson 
709fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
71091ebeef5Sahrens 
711e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
712fa9e4066Sahrens 
713c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
714c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
715c3a66015SMatthew Ahrens 
716b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
717cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
718b24ab676SJeff Bonwick 
719c25056deSgw25295 	cv_destroy(&spa->spa_async_cv);
7200521c5ebSJustin Gibbs 	cv_destroy(&spa->spa_evicting_os_cv);
72135a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
722c25056deSgw25295 	cv_destroy(&spa->spa_scrub_io_cv);
723e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
724c25056deSgw25295 
7255ad82045Snd150628 	mutex_destroy(&spa->spa_async_lock);
726c25056deSgw25295 	mutex_destroy(&spa->spa_errlist_lock);
72735a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
7280521c5ebSJustin Gibbs 	mutex_destroy(&spa->spa_evicting_os_lock);
72906eeb2adSek110237 	mutex_destroy(&spa->spa_history_lock);
73035a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
731b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
73235a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
733e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
734a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
735c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
7365ad82045Snd150628 
737fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
738fa9e4066Sahrens }
739fa9e4066Sahrens 
740fa9e4066Sahrens /*
741fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
742fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
743fa9e4066Sahrens  */
744fa9e4066Sahrens spa_t *
spa_next(spa_t * prev)745fa9e4066Sahrens spa_next(spa_t *prev)
746fa9e4066Sahrens {
747fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
748fa9e4066Sahrens 
749fa9e4066Sahrens 	if (prev)
750fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
751fa9e4066Sahrens 	else
752fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
753fa9e4066Sahrens }
754fa9e4066Sahrens 
755fa9e4066Sahrens /*
756fa9e4066Sahrens  * ==========================================================================
757fa9e4066Sahrens  * SPA refcount functions
758fa9e4066Sahrens  * ==========================================================================
759fa9e4066Sahrens  */
760fa9e4066Sahrens 
761fa9e4066Sahrens /*
762fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
763fa9e4066Sahrens  * have the namespace lock held.
764fa9e4066Sahrens  */
765fa9e4066Sahrens void
spa_open_ref(spa_t * spa,void * tag)766fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
767fa9e4066Sahrens {
768088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
769fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
770fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
771fa9e4066Sahrens }
772fa9e4066Sahrens 
773fa9e4066Sahrens /*
774fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
775fa9e4066Sahrens  * have the namespace lock held.
776fa9e4066Sahrens  */
777fa9e4066Sahrens void
spa_close(spa_t * spa,void * tag)778fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
779fa9e4066Sahrens {
780088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
781fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
782fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
783fa9e4066Sahrens }
784fa9e4066Sahrens 
785fa9e4066Sahrens /*
7860521c5ebSJustin Gibbs  * Remove a reference to the given spa_t held by a dsl dir that is
7870521c5ebSJustin Gibbs  * being asynchronously released.  Async releases occur from a taskq
7880521c5ebSJustin Gibbs  * performing eviction of dsl datasets and dirs.  The namespace lock
7890521c5ebSJustin Gibbs  * isn't held and the hold by the object being evicted may contribute to
7900521c5ebSJustin Gibbs  * spa_minref (e.g. dataset or directory released during pool export),
7910521c5ebSJustin Gibbs  * so the asserts in spa_close() do not apply.
7920521c5ebSJustin Gibbs  */
7930521c5ebSJustin Gibbs void
spa_async_close(spa_t * spa,void * tag)7940521c5ebSJustin Gibbs spa_async_close(spa_t *spa, void *tag)
7950521c5ebSJustin Gibbs {
7960521c5ebSJustin Gibbs 	(void) refcount_remove(&spa->spa_refcount, tag);
7970521c5ebSJustin Gibbs }
7980521c5ebSJustin Gibbs 
7990521c5ebSJustin Gibbs /*
800fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
801088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
802fa9e4066Sahrens  * number of references acquired when opening a pool
803fa9e4066Sahrens  */
804fa9e4066Sahrens boolean_t
spa_refcount_zero(spa_t * spa)805fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
806fa9e4066Sahrens {
807fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
808fa9e4066Sahrens 
809088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
810fa9e4066Sahrens }
811fa9e4066Sahrens 
812fa9e4066Sahrens /*
813fa9e4066Sahrens  * ==========================================================================
814fa94a07fSbrendan  * SPA spare and l2cache tracking
81599653d4eSeschrock  * ==========================================================================
81699653d4eSeschrock  */
81799653d4eSeschrock 
81899653d4eSeschrock /*
819fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
820fa94a07fSbrendan  * for 'auxiliary' devices.
821fa94a07fSbrendan  */
822fa94a07fSbrendan 
823fa94a07fSbrendan typedef struct spa_aux {
824fa94a07fSbrendan 	uint64_t	aux_guid;
825fa94a07fSbrendan 	uint64_t	aux_pool;
826fa94a07fSbrendan 	avl_node_t	aux_avl;
827fa94a07fSbrendan 	int		aux_count;
828fa94a07fSbrendan } spa_aux_t;
829fa94a07fSbrendan 
830fa94a07fSbrendan static int
spa_aux_compare(const void * a,const void * b)831fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
832fa94a07fSbrendan {
833fa94a07fSbrendan 	const spa_aux_t *sa = a;
834fa94a07fSbrendan 	const spa_aux_t *sb = b;
835fa94a07fSbrendan 
836fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
837fa94a07fSbrendan 		return (-1);
838fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
839fa94a07fSbrendan 		return (1);
840fa94a07fSbrendan 	else
841fa94a07fSbrendan 		return (0);
842fa94a07fSbrendan }
843fa94a07fSbrendan 
844fa94a07fSbrendan void
spa_aux_add(vdev_t * vd,avl_tree_t * avl)845fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
846fa94a07fSbrendan {
847fa94a07fSbrendan 	avl_index_t where;
848fa94a07fSbrendan 	spa_aux_t search;
849fa94a07fSbrendan 	spa_aux_t *aux;
850fa94a07fSbrendan 
851fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
852fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
853fa94a07fSbrendan 		aux->aux_count++;
854fa94a07fSbrendan 	} else {
855fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
856fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
857fa94a07fSbrendan 		aux->aux_count = 1;
858fa94a07fSbrendan 		avl_insert(avl, aux, where);
859fa94a07fSbrendan 	}
860fa94a07fSbrendan }
861fa94a07fSbrendan 
862fa94a07fSbrendan void
spa_aux_remove(vdev_t * vd,avl_tree_t * avl)863fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
864fa94a07fSbrendan {
865fa94a07fSbrendan 	spa_aux_t search;
866fa94a07fSbrendan 	spa_aux_t *aux;
867fa94a07fSbrendan 	avl_index_t where;
868fa94a07fSbrendan 
869fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
870fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
871fa94a07fSbrendan 
872fa94a07fSbrendan 	ASSERT(aux != NULL);
873fa94a07fSbrendan 
874fa94a07fSbrendan 	if (--aux->aux_count == 0) {
875fa94a07fSbrendan 		avl_remove(avl, aux);
876fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
877fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
878fa94a07fSbrendan 		aux->aux_pool = 0ULL;
879fa94a07fSbrendan 	}
880fa94a07fSbrendan }
881fa94a07fSbrendan 
882fa94a07fSbrendan boolean_t
spa_aux_exists(uint64_t guid,uint64_t * pool,int * refcnt,avl_tree_t * avl)88389a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
884fa94a07fSbrendan {
885fa94a07fSbrendan 	spa_aux_t search, *found;
886fa94a07fSbrendan 
887fa94a07fSbrendan 	search.aux_guid = guid;
88889a89ebfSlling 	found = avl_find(avl, &search, NULL);
889fa94a07fSbrendan 
890fa94a07fSbrendan 	if (pool) {
891fa94a07fSbrendan 		if (found)
892fa94a07fSbrendan 			*pool = found->aux_pool;
893fa94a07fSbrendan 		else
894fa94a07fSbrendan 			*pool = 0ULL;
895fa94a07fSbrendan 	}
896fa94a07fSbrendan 
89789a89ebfSlling 	if (refcnt) {
89889a89ebfSlling 		if (found)
89989a89ebfSlling 			*refcnt = found->aux_count;
90089a89ebfSlling 		else
90189a89ebfSlling 			*refcnt = 0;
90289a89ebfSlling 	}
90389a89ebfSlling 
904fa94a07fSbrendan 	return (found != NULL);
905fa94a07fSbrendan }
906fa94a07fSbrendan 
907fa94a07fSbrendan void
spa_aux_activate(vdev_t * vd,avl_tree_t * avl)908fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
909fa94a07fSbrendan {
910fa94a07fSbrendan 	spa_aux_t search, *found;
911fa94a07fSbrendan 	avl_index_t where;
912fa94a07fSbrendan 
913fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
914fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
915fa94a07fSbrendan 	ASSERT(found != NULL);
916fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
917fa94a07fSbrendan 
918fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
919fa94a07fSbrendan }
920fa94a07fSbrendan 
921fa94a07fSbrendan /*
92239c23413Seschrock  * Spares are tracked globally due to the following constraints:
92339c23413Seschrock  *
92439c23413Seschrock  * 	- A spare may be part of multiple pools.
92539c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
92639c23413Seschrock  *	  another pool.
92739c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
92839c23413Seschrock  *	  the target is a spare in the same pool.
92939c23413Seschrock  *
93039c23413Seschrock  * We keep track of all spares on the system through the use of a reference
93139c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
93239c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
93339c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
93439c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
93539c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
93639c23413Seschrock  *
93739c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
93839c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
93939c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
94039c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
94199653d4eSeschrock  */
94239c23413Seschrock 
94399653d4eSeschrock static int
spa_spare_compare(const void * a,const void * b)94499653d4eSeschrock spa_spare_compare(const void *a, const void *b)
94599653d4eSeschrock {
946fa94a07fSbrendan 	return (spa_aux_compare(a, b));
94799653d4eSeschrock }
94899653d4eSeschrock 
94999653d4eSeschrock void
spa_spare_add(vdev_t * vd)95039c23413Seschrock spa_spare_add(vdev_t *vd)
95199653d4eSeschrock {
95299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
95339c23413Seschrock 	ASSERT(!vd->vdev_isspare);
954fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
95539c23413Seschrock 	vd->vdev_isspare = B_TRUE;
95699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
95799653d4eSeschrock }
95899653d4eSeschrock 
95999653d4eSeschrock void
spa_spare_remove(vdev_t * vd)96039c23413Seschrock spa_spare_remove(vdev_t *vd)
96199653d4eSeschrock {
96299653d4eSeschrock 	mutex_enter(&spa_spare_lock);
96339c23413Seschrock 	ASSERT(vd->vdev_isspare);
964fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
96539c23413Seschrock 	vd->vdev_isspare = B_FALSE;
96699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
96799653d4eSeschrock }
96899653d4eSeschrock 
96999653d4eSeschrock boolean_t
spa_spare_exists(uint64_t guid,uint64_t * pool,int * refcnt)97089a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
97199653d4eSeschrock {
972fa94a07fSbrendan 	boolean_t found;
97399653d4eSeschrock 
97499653d4eSeschrock 	mutex_enter(&spa_spare_lock);
97589a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
97699653d4eSeschrock 	mutex_exit(&spa_spare_lock);
97799653d4eSeschrock 
978fa94a07fSbrendan 	return (found);
97939c23413Seschrock }
98039c23413Seschrock 
98139c23413Seschrock void
spa_spare_activate(vdev_t * vd)98239c23413Seschrock spa_spare_activate(vdev_t *vd)
98339c23413Seschrock {
98439c23413Seschrock 	mutex_enter(&spa_spare_lock);
98539c23413Seschrock 	ASSERT(vd->vdev_isspare);
986fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
98739c23413Seschrock 	mutex_exit(&spa_spare_lock);
98899653d4eSeschrock }
98999653d4eSeschrock 
99099653d4eSeschrock /*
991fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
992fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
993fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
994fa94a07fSbrendan  */
995fa94a07fSbrendan 
996fa94a07fSbrendan static int
spa_l2cache_compare(const void * a,const void * b)997fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
998fa94a07fSbrendan {
999fa94a07fSbrendan 	return (spa_aux_compare(a, b));
1000fa94a07fSbrendan }
1001fa94a07fSbrendan 
1002fa94a07fSbrendan void
spa_l2cache_add(vdev_t * vd)1003fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
1004fa94a07fSbrendan {
1005fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1006fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
1007fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
1008fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
1009fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1010fa94a07fSbrendan }
1011fa94a07fSbrendan 
1012fa94a07fSbrendan void
spa_l2cache_remove(vdev_t * vd)1013fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
1014fa94a07fSbrendan {
1015fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1016fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1017fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
1018fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
1019fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1020fa94a07fSbrendan }
1021fa94a07fSbrendan 
1022fa94a07fSbrendan boolean_t
spa_l2cache_exists(uint64_t guid,uint64_t * pool)1023fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1024fa94a07fSbrendan {
1025fa94a07fSbrendan 	boolean_t found;
1026fa94a07fSbrendan 
1027fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
102889a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1029fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1030fa94a07fSbrendan 
1031fa94a07fSbrendan 	return (found);
1032fa94a07fSbrendan }
1033fa94a07fSbrendan 
1034fa94a07fSbrendan void
spa_l2cache_activate(vdev_t * vd)1035fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
1036fa94a07fSbrendan {
1037fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1038fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1039fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
1040fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1041fa94a07fSbrendan }
1042fa94a07fSbrendan 
1043fa94a07fSbrendan /*
104499653d4eSeschrock  * ==========================================================================
1045fa9e4066Sahrens  * SPA vdev locking
1046fa9e4066Sahrens  * ==========================================================================
1047fa9e4066Sahrens  */
1048fa9e4066Sahrens 
1049fa9e4066Sahrens /*
1050ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
1051ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1052fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
1053fa9e4066Sahrens  */
1054fa9e4066Sahrens uint64_t
spa_vdev_enter(spa_t * spa)1055fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
1056fa9e4066Sahrens {
1057a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
1058bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
105988ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
106088ecc943SGeorge Wilson }
106188ecc943SGeorge Wilson 
106288ecc943SGeorge Wilson /*
106388ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
106488ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
106588ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
106688ecc943SGeorge Wilson  */
106788ecc943SGeorge Wilson uint64_t
spa_vdev_config_enter(spa_t * spa)106888ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
106988ecc943SGeorge Wilson {
107088ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
10713d7072f8Seschrock 
1072e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1073fa9e4066Sahrens 
1074fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
1075fa9e4066Sahrens }
1076fa9e4066Sahrens 
1077fa9e4066Sahrens /*
107888ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
107988ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
1080fa9e4066Sahrens  */
108188ecc943SGeorge Wilson void
spa_vdev_config_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error,char * tag)108288ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1083fa9e4066Sahrens {
108488ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
108588ecc943SGeorge Wilson 
10860e34b6a7Sbonwick 	int config_changed = B_FALSE;
1087ea8dc4b6Seschrock 
10880373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
10890e34b6a7Sbonwick 
1090e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
1091e14bb325SJeff Bonwick 
10920e34b6a7Sbonwick 	/*
10930e34b6a7Sbonwick 	 * Reassess the DTLs.
10940e34b6a7Sbonwick 	 */
10950373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
10960e34b6a7Sbonwick 
1097e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
10980e34b6a7Sbonwick 		config_changed = B_TRUE;
10998f18d1faSGeorge Wilson 		spa->spa_config_generation++;
11000e34b6a7Sbonwick 	}
1101fa9e4066Sahrens 
110288ecc943SGeorge Wilson 	/*
110388ecc943SGeorge Wilson 	 * Verify the metaslab classes.
110488ecc943SGeorge Wilson 	 */
1105b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1106b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
110788ecc943SGeorge Wilson 
1108e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
1109fa9e4066Sahrens 
1110ea8dc4b6Seschrock 	/*
111188ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
111288ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
111388ecc943SGeorge Wilson 	 * transactionally.
111488ecc943SGeorge Wilson 	 */
111588ecc943SGeorge Wilson 	if (zio_injection_enabled)
11161195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
111788ecc943SGeorge Wilson 
111888ecc943SGeorge Wilson 	/*
1119fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1120fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1121fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1122fa9e4066Sahrens 	 */
1123fa9e4066Sahrens 	if (error == 0)
1124fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1125fa9e4066Sahrens 
1126fa9e4066Sahrens 	if (vd != NULL) {
11270713e232SGeorge Wilson 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
11288ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1129fa9e4066Sahrens 		vdev_free(vd);
11308ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1131fa9e4066Sahrens 	}
1132fa9e4066Sahrens 
1133fa9e4066Sahrens 	/*
11340e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1135fa9e4066Sahrens 	 */
11360e34b6a7Sbonwick 	if (config_changed)
1137c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, B_TRUE);
113888ecc943SGeorge Wilson }
1139ea8dc4b6Seschrock 
114088ecc943SGeorge Wilson /*
114188ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
114288ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
114388ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
114488ecc943SGeorge Wilson  * information.
114588ecc943SGeorge Wilson  */
114688ecc943SGeorge Wilson int
spa_vdev_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error)114788ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
114888ecc943SGeorge Wilson {
114988ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1150fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1151bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1152fa9e4066Sahrens 
1153fa9e4066Sahrens 	return (error);
1154fa9e4066Sahrens }
1155fa9e4066Sahrens 
1156fa9e4066Sahrens /*
1157e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1158e14bb325SJeff Bonwick  */
1159e14bb325SJeff Bonwick void
spa_vdev_state_enter(spa_t * spa,int oplocks)11608f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1161e14bb325SJeff Bonwick {
11628f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
11638f18d1faSGeorge Wilson 
1164dcba9f3fSGeorge Wilson 	/*
1165dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1166dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1167dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1168dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1169dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1170dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1171dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1172dcba9f3fSGeorge Wilson 	 */
1173dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
11749842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
11759842588bSGeorge Wilson 		int high = locks & ~low;
11769842588bSGeorge Wilson 
11779842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1178dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
11799842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1180dcba9f3fSGeorge Wilson 	} else {
11818f18d1faSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1182dcba9f3fSGeorge Wilson 	}
11838f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1184e14bb325SJeff Bonwick }
1185e14bb325SJeff Bonwick 
1186e14bb325SJeff Bonwick int
spa_vdev_state_exit(spa_t * spa,vdev_t * vd,int error)1187e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1188e14bb325SJeff Bonwick {
1189c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1190c6065d0fSGeorge Wilson 
1191b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1192b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1193b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1194b24ab676SJeff Bonwick 
11958f18d1faSGeorge Wilson 	if (vd != NULL) {
1196e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1197c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
11988f18d1faSGeorge Wilson 		spa->spa_config_generation++;
11998f18d1faSGeorge Wilson 	}
1200e14bb325SJeff Bonwick 
1201dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1202dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1203dcba9f3fSGeorge Wilson 
12048f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
12058f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1206e14bb325SJeff Bonwick 
12078ad4d6ddSJeff Bonwick 	/*
12088ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
12098ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
12108ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
12118ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
12128ad4d6ddSJeff Bonwick 	 */
12138ad4d6ddSJeff Bonwick 	if (vd != NULL)
12148ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
12158ad4d6ddSJeff Bonwick 
1216c6065d0fSGeorge Wilson 	/*
1217c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1218c6065d0fSGeorge Wilson 	 */
1219c6065d0fSGeorge Wilson 	if (config_changed) {
1220c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
1221c6065d0fSGeorge Wilson 		spa_config_sync(spa, B_FALSE, B_TRUE);
1222c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1223c6065d0fSGeorge Wilson 	}
1224c6065d0fSGeorge Wilson 
1225e14bb325SJeff Bonwick 	return (error);
1226e14bb325SJeff Bonwick }
1227e14bb325SJeff Bonwick 
1228e14bb325SJeff Bonwick /*
1229fa9e4066Sahrens  * ==========================================================================
1230fa9e4066Sahrens  * Miscellaneous functions
1231fa9e4066Sahrens  * ==========================================================================
1232fa9e4066Sahrens  */
1233fa9e4066Sahrens 
1234ad135b5dSChristopher Siden void
spa_activate_mos_feature(spa_t * spa,const char * feature,dmu_tx_t * tx)123549064978SMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1236ad135b5dSChristopher Siden {
1237dbc43af2SMatthew Ahrens 	if (!nvlist_exists(spa->spa_label_features, feature)) {
1238dbc43af2SMatthew Ahrens 		fnvlist_add_boolean(spa->spa_label_features, feature);
123949064978SMax Grossman 		/*
124049064978SMax Grossman 		 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
124149064978SMax Grossman 		 * dirty the vdev config because lock SCL_CONFIG is not held.
124249064978SMax Grossman 		 * Thankfully, in this case we don't need to dirty the config
124349064978SMax Grossman 		 * because it will be written out anyway when we finish
124449064978SMax Grossman 		 * creating the pool.
124549064978SMax Grossman 		 */
124649064978SMax Grossman 		if (tx->tx_txg != TXG_INITIAL)
1247ad135b5dSChristopher Siden 			vdev_config_dirty(spa->spa_root_vdev);
1248ad135b5dSChristopher Siden 	}
1249dbc43af2SMatthew Ahrens }
1250ad135b5dSChristopher Siden 
1251ad135b5dSChristopher Siden void
spa_deactivate_mos_feature(spa_t * spa,const char * feature)1252ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1253ad135b5dSChristopher Siden {
1254dbc43af2SMatthew Ahrens 	if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1255ad135b5dSChristopher Siden 		vdev_config_dirty(spa->spa_root_vdev);
1256ad135b5dSChristopher Siden }
1257ad135b5dSChristopher Siden 
1258fa9e4066Sahrens /*
1259fa9e4066Sahrens  * Rename a spa_t.
1260fa9e4066Sahrens  */
1261fa9e4066Sahrens int
spa_rename(const char * name,const char * newname)1262fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1263fa9e4066Sahrens {
1264fa9e4066Sahrens 	spa_t *spa;
1265fa9e4066Sahrens 	int err;
1266fa9e4066Sahrens 
1267fa9e4066Sahrens 	/*
1268fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1269fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1270fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1271ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1272fa9e4066Sahrens 	 */
1273fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1274fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1275fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1276fa9e4066Sahrens 		return (err);
1277fa9e4066Sahrens 	}
1278fa9e4066Sahrens 
1279e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1280fa9e4066Sahrens 
1281fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1282e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1283fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1284fa9e4066Sahrens 
1285fa9e4066Sahrens 	/*
1286fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1287fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1288fa9e4066Sahrens 	 * during the sync.
1289fa9e4066Sahrens 	 */
1290fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1291fa9e4066Sahrens 
1292e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1293fa9e4066Sahrens 
12940373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1295fa9e4066Sahrens 
1296fa9e4066Sahrens 	/*
1297fa9e4066Sahrens 	 * Sync the updated config cache.
1298fa9e4066Sahrens 	 */
1299c5904d13Seschrock 	spa_config_sync(spa, B_FALSE, B_TRUE);
1300fa9e4066Sahrens 
1301fa9e4066Sahrens 	spa_close(spa, FTAG);
1302fa9e4066Sahrens 
1303fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1304fa9e4066Sahrens 
1305fa9e4066Sahrens 	return (0);
1306fa9e4066Sahrens }
1307fa9e4066Sahrens 
1308fa9e4066Sahrens /*
1309f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1310f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1311f9af39baSGeorge Wilson  * a device with the specified device_guid.
1312fa9e4066Sahrens  */
1313f9af39baSGeorge Wilson spa_t *
spa_by_guid(uint64_t pool_guid,uint64_t device_guid)1314f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1315fa9e4066Sahrens {
1316fa9e4066Sahrens 	spa_t *spa;
1317fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1318fa9e4066Sahrens 
1319ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1320fa9e4066Sahrens 
1321fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1322fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1323fa9e4066Sahrens 			continue;
1324fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1325fa9e4066Sahrens 			continue;
132639c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
132739c23413Seschrock 			if (device_guid == 0)
1328fa9e4066Sahrens 				break;
132939c23413Seschrock 
133039c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
133139c23413Seschrock 			    device_guid) != NULL)
133239c23413Seschrock 				break;
133339c23413Seschrock 
133439c23413Seschrock 			/*
13358654d025Sperrin 			 * Check any devices we may be in the process of adding.
133639c23413Seschrock 			 */
133739c23413Seschrock 			if (spa->spa_pending_vdev) {
133839c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
133939c23413Seschrock 				    device_guid) != NULL)
134039c23413Seschrock 					break;
134139c23413Seschrock 			}
134239c23413Seschrock 		}
1343fa9e4066Sahrens 	}
1344fa9e4066Sahrens 
1345f9af39baSGeorge Wilson 	return (spa);
1346f9af39baSGeorge Wilson }
1347f9af39baSGeorge Wilson 
1348f9af39baSGeorge Wilson /*
1349f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1350f9af39baSGeorge Wilson  */
1351f9af39baSGeorge Wilson boolean_t
spa_guid_exists(uint64_t pool_guid,uint64_t device_guid)1352f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1353f9af39baSGeorge Wilson {
1354f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1355fa9e4066Sahrens }
1356fa9e4066Sahrens 
1357fa9e4066Sahrens char *
spa_strdup(const char * s)1358fa9e4066Sahrens spa_strdup(const char *s)
1359fa9e4066Sahrens {
1360fa9e4066Sahrens 	size_t len;
1361fa9e4066Sahrens 	char *new;
1362fa9e4066Sahrens 
1363fa9e4066Sahrens 	len = strlen(s);
1364fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1365fa9e4066Sahrens 	bcopy(s, new, len);
1366fa9e4066Sahrens 	new[len] = '\0';
1367fa9e4066Sahrens 
1368fa9e4066Sahrens 	return (new);
1369fa9e4066Sahrens }
1370fa9e4066Sahrens 
1371fa9e4066Sahrens void
spa_strfree(char * s)1372fa9e4066Sahrens spa_strfree(char *s)
1373fa9e4066Sahrens {
1374fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1375fa9e4066Sahrens }
1376fa9e4066Sahrens 
1377fa9e4066Sahrens uint64_t
spa_get_random(uint64_t range)1378fa9e4066Sahrens spa_get_random(uint64_t range)
1379fa9e4066Sahrens {
1380fa9e4066Sahrens 	uint64_t r;
1381fa9e4066Sahrens 
1382fa9e4066Sahrens 	ASSERT(range != 0);
1383fa9e4066Sahrens 
1384fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1385fa9e4066Sahrens 
1386fa9e4066Sahrens 	return (r % range);
1387fa9e4066Sahrens }
1388fa9e4066Sahrens 
13891195e687SMark J Musante uint64_t
spa_generate_guid(spa_t * spa)13901195e687SMark J Musante spa_generate_guid(spa_t *spa)
13911195e687SMark J Musante {
13921195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
13931195e687SMark J Musante 
13941195e687SMark J Musante 	if (spa != NULL) {
13951195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
13961195e687SMark J Musante 			guid = spa_get_random(-1ULL);
13971195e687SMark J Musante 	} else {
13981195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
13991195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14001195e687SMark J Musante 	}
14011195e687SMark J Musante 
14021195e687SMark J Musante 	return (guid);
14031195e687SMark J Musante }
14041195e687SMark J Musante 
1405fa9e4066Sahrens void
snprintf_blkptr(char * buf,size_t buflen,const blkptr_t * bp)140649064978SMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1407fa9e4066Sahrens {
1408ad135b5dSChristopher Siden 	char type[256];
1409f0ba89beSJeff Bonwick 	char *checksum = NULL;
1410f0ba89beSJeff Bonwick 	char *compress = NULL;
1411f0ba89beSJeff Bonwick 
1412f0ba89beSJeff Bonwick 	if (bp != NULL) {
1413ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1414ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1415ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1416ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1417ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1418ad135b5dSChristopher Siden 			    "metadata" : "data",
1419ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1420ad135b5dSChristopher Siden 		} else {
1421ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1422ad135b5dSChristopher Siden 			    sizeof (type));
1423ad135b5dSChristopher Siden 		}
1424e54e0be9SMatthew Ahrens 		if (!BP_IS_EMBEDDED(bp)) {
1425e54e0be9SMatthew Ahrens 			checksum =
1426e54e0be9SMatthew Ahrens 			    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1427e54e0be9SMatthew Ahrens 		}
1428f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1429f0ba89beSJeff Bonwick 	}
1430fa9e4066Sahrens 
143149064978SMax Grossman 	SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
143249064978SMax Grossman 	    compress);
1433fa9e4066Sahrens }
1434fa9e4066Sahrens 
1435fa9e4066Sahrens void
spa_freeze(spa_t * spa)1436fa9e4066Sahrens spa_freeze(spa_t *spa)
1437fa9e4066Sahrens {
1438fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1439fa9e4066Sahrens 
1440e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1441fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1442fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1443fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1444fa9e4066Sahrens 	}
1445e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1446fa9e4066Sahrens 	if (freeze_txg != 0)
1447fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1448fa9e4066Sahrens }
1449fa9e4066Sahrens 
14500125049cSahrens void
zfs_panic_recover(const char * fmt,...)14510125049cSahrens zfs_panic_recover(const char *fmt, ...)
14520125049cSahrens {
14530125049cSahrens 	va_list adx;
14540125049cSahrens 
14550125049cSahrens 	va_start(adx, fmt);
14560125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
14570125049cSahrens 	va_end(adx);
14580125049cSahrens }
14590125049cSahrens 
1460fa9e4066Sahrens /*
14613f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
1462f7170741SWill Andrews  * lowercase hexadecimal numbers that don't overflow.
14633f9d6ad7SLin Ling  */
14643f9d6ad7SLin Ling uint64_t
strtonum(const char * str,char ** nptr)14653f9d6ad7SLin Ling strtonum(const char *str, char **nptr)
14663f9d6ad7SLin Ling {
14673f9d6ad7SLin Ling 	uint64_t val = 0;
14683f9d6ad7SLin Ling 	char c;
14693f9d6ad7SLin Ling 	int digit;
14703f9d6ad7SLin Ling 
14713f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
14723f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
14733f9d6ad7SLin Ling 			digit = c - '0';
14743f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
14753f9d6ad7SLin Ling 			digit = 10 + c - 'a';
14763f9d6ad7SLin Ling 		else
14773f9d6ad7SLin Ling 			break;
14783f9d6ad7SLin Ling 
14793f9d6ad7SLin Ling 		val *= 16;
14803f9d6ad7SLin Ling 		val += digit;
14813f9d6ad7SLin Ling 
14823f9d6ad7SLin Ling 		str++;
14833f9d6ad7SLin Ling 	}
14843f9d6ad7SLin Ling 
14853f9d6ad7SLin Ling 	if (nptr)
14863f9d6ad7SLin Ling 		*nptr = (char *)str;
14873f9d6ad7SLin Ling 
14883f9d6ad7SLin Ling 	return (val);
14893f9d6ad7SLin Ling }
14903f9d6ad7SLin Ling 
14913f9d6ad7SLin Ling /*
1492fa9e4066Sahrens  * ==========================================================================
1493fa9e4066Sahrens  * Accessor functions
1494fa9e4066Sahrens  * ==========================================================================
1495fa9e4066Sahrens  */
1496fa9e4066Sahrens 
1497088f3894Sahrens boolean_t
spa_shutting_down(spa_t * spa)149888b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1499fa9e4066Sahrens {
150088b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1501fa9e4066Sahrens }
1502fa9e4066Sahrens 
1503fa9e4066Sahrens dsl_pool_t *
spa_get_dsl(spa_t * spa)1504fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1505fa9e4066Sahrens {
1506fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1507fa9e4066Sahrens }
1508fa9e4066Sahrens 
1509ad135b5dSChristopher Siden boolean_t
spa_is_initializing(spa_t * spa)1510ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1511ad135b5dSChristopher Siden {
1512ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1513ad135b5dSChristopher Siden }
1514ad135b5dSChristopher Siden 
1515fa9e4066Sahrens blkptr_t *
spa_get_rootblkptr(spa_t * spa)1516fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1517fa9e4066Sahrens {
1518fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1519fa9e4066Sahrens }
1520fa9e4066Sahrens 
1521fa9e4066Sahrens void
spa_set_rootblkptr(spa_t * spa,const blkptr_t * bp)1522fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1523fa9e4066Sahrens {
1524fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1525fa9e4066Sahrens }
1526fa9e4066Sahrens 
1527fa9e4066Sahrens void
spa_altroot(spa_t * spa,char * buf,size_t buflen)1528fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1529fa9e4066Sahrens {
1530fa9e4066Sahrens 	if (spa->spa_root == NULL)
1531fa9e4066Sahrens 		buf[0] = '\0';
1532fa9e4066Sahrens 	else
1533fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1534fa9e4066Sahrens }
1535fa9e4066Sahrens 
1536fa9e4066Sahrens int
spa_sync_pass(spa_t * spa)1537fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1538fa9e4066Sahrens {
1539fa9e4066Sahrens 	return (spa->spa_sync_pass);
1540fa9e4066Sahrens }
1541fa9e4066Sahrens 
1542fa9e4066Sahrens char *
spa_name(spa_t * spa)1543fa9e4066Sahrens spa_name(spa_t *spa)
1544fa9e4066Sahrens {
1545fa9e4066Sahrens 	return (spa->spa_name);
1546fa9e4066Sahrens }
1547fa9e4066Sahrens 
1548fa9e4066Sahrens uint64_t
spa_guid(spa_t * spa)1549fa9e4066Sahrens spa_guid(spa_t *spa)
1550fa9e4066Sahrens {
1551dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1552dfbb9432SGeorge Wilson 	uint64_t guid;
1553dfbb9432SGeorge Wilson 
1554b5989ec7Seschrock 	/*
1555b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1556b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1557e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1558b5989ec7Seschrock 	 * this case.
1559b5989ec7Seschrock 	 */
1560dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1561dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1562dfbb9432SGeorge Wilson 
1563dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1564dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1565dfbb9432SGeorge Wilson 
1566dfbb9432SGeorge Wilson 	/*
1567dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1568dfbb9432SGeorge Wilson 	 * in syncing context.
1569dfbb9432SGeorge Wilson 	 */
1570dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1571fa9e4066Sahrens 		return (spa->spa_root_vdev->vdev_guid);
1572b5989ec7Seschrock 	else
1573dfbb9432SGeorge Wilson 		return (guid);
1574e9103aaeSGarrett D'Amore }
1575e9103aaeSGarrett D'Amore 
1576e9103aaeSGarrett D'Amore uint64_t
spa_load_guid(spa_t * spa)1577e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1578e9103aaeSGarrett D'Amore {
1579e9103aaeSGarrett D'Amore 	/*
1580e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1581e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1582e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1583e9103aaeSGarrett D'Amore 	 */
1584b5989ec7Seschrock 	return (spa->spa_load_guid);
1585fa9e4066Sahrens }
1586fa9e4066Sahrens 
1587fa9e4066Sahrens uint64_t
spa_last_synced_txg(spa_t * spa)1588fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1589fa9e4066Sahrens {
1590fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1591fa9e4066Sahrens }
1592fa9e4066Sahrens 
1593fa9e4066Sahrens uint64_t
spa_first_txg(spa_t * spa)1594fa9e4066Sahrens spa_first_txg(spa_t *spa)
1595fa9e4066Sahrens {
1596fa9e4066Sahrens 	return (spa->spa_first_txg);
1597fa9e4066Sahrens }
1598fa9e4066Sahrens 
1599b24ab676SJeff Bonwick uint64_t
spa_syncing_txg(spa_t * spa)1600b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1601b24ab676SJeff Bonwick {
1602b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1603b24ab676SJeff Bonwick }
1604b24ab676SJeff Bonwick 
160588b7b0f2SMatthew Ahrens pool_state_t
spa_state(spa_t * spa)1606fa9e4066Sahrens spa_state(spa_t *spa)
1607fa9e4066Sahrens {
1608fa9e4066Sahrens 	return (spa->spa_state);
1609fa9e4066Sahrens }
1610fa9e4066Sahrens 
1611b16da2e2SGeorge Wilson spa_load_state_t
spa_load_state(spa_t * spa)1612b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1613b16da2e2SGeorge Wilson {
1614b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1615b16da2e2SGeorge Wilson }
1616b16da2e2SGeorge Wilson 
1617fa9e4066Sahrens uint64_t
spa_freeze_txg(spa_t * spa)1618fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1619fa9e4066Sahrens {
1620fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1621fa9e4066Sahrens }
1622fa9e4066Sahrens 
1623fa9e4066Sahrens /* ARGSUSED */
1624fa9e4066Sahrens uint64_t
spa_get_asize(spa_t * spa,uint64_t lsize)1625fa9e4066Sahrens spa_get_asize(spa_t *spa, uint64_t lsize)
1626fa9e4066Sahrens {
162769962b56SMatthew Ahrens 	return (lsize * spa_asize_inflation);
162844cd46caSbillm }
162944cd46caSbillm 
163043d5e60eSMatthew Ahrens /*
163143d5e60eSMatthew Ahrens  * Return the amount of slop space in bytes.  It is 1/32 of the pool (3.2%),
163243d5e60eSMatthew Ahrens  * or at least 32MB.
163343d5e60eSMatthew Ahrens  *
163443d5e60eSMatthew Ahrens  * See the comment above spa_slop_shift for details.
163543d5e60eSMatthew Ahrens  */
163643d5e60eSMatthew Ahrens uint64_t
spa_get_slop_space(spa_t * spa)163743d5e60eSMatthew Ahrens spa_get_slop_space(spa_t *spa) {
163843d5e60eSMatthew Ahrens 	uint64_t space = spa_get_dspace(spa);
163943d5e60eSMatthew Ahrens 	return (MAX(space >> spa_slop_shift, SPA_MINDEVSIZE >> 1));
164043d5e60eSMatthew Ahrens }
164143d5e60eSMatthew Ahrens 
1642485bbbf5SGeorge Wilson uint64_t
spa_get_dspace(spa_t * spa)1643485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1644485bbbf5SGeorge Wilson {
1645485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1646485bbbf5SGeorge Wilson }
1647485bbbf5SGeorge Wilson 
1648485bbbf5SGeorge Wilson void
spa_update_dspace(spa_t * spa)1649485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1650485bbbf5SGeorge Wilson {
1651485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1652485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
1653485bbbf5SGeorge Wilson }
1654485bbbf5SGeorge Wilson 
16550a4e9518Sgw25295 /*
16560a4e9518Sgw25295  * Return the failure mode that has been set to this pool. The default
16570a4e9518Sgw25295  * behavior will be to block all I/Os when a complete failure occurs.
16580a4e9518Sgw25295  */
16590a4e9518Sgw25295 uint8_t
spa_get_failmode(spa_t * spa)16600a4e9518Sgw25295 spa_get_failmode(spa_t *spa)
16610a4e9518Sgw25295 {
16620a4e9518Sgw25295 	return (spa->spa_failmode);
16630a4e9518Sgw25295 }
16640a4e9518Sgw25295 
1665e14bb325SJeff Bonwick boolean_t
spa_suspended(spa_t * spa)1666e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1667e14bb325SJeff Bonwick {
1668e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1669e14bb325SJeff Bonwick }
1670e14bb325SJeff Bonwick 
167144cd46caSbillm uint64_t
spa_version(spa_t * spa)167244cd46caSbillm spa_version(spa_t *spa)
167344cd46caSbillm {
167444cd46caSbillm 	return (spa->spa_ubsync.ub_version);
167544cd46caSbillm }
167644cd46caSbillm 
1677b24ab676SJeff Bonwick boolean_t
spa_deflate(spa_t * spa)1678b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1679b24ab676SJeff Bonwick {
1680b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1681b24ab676SJeff Bonwick }
1682b24ab676SJeff Bonwick 
1683b24ab676SJeff Bonwick metaslab_class_t *
spa_normal_class(spa_t * spa)1684b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1685b24ab676SJeff Bonwick {
1686b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1687b24ab676SJeff Bonwick }
1688b24ab676SJeff Bonwick 
1689b24ab676SJeff Bonwick metaslab_class_t *
spa_log_class(spa_t * spa)1690b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1691b24ab676SJeff Bonwick {
1692b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1693b24ab676SJeff Bonwick }
1694b24ab676SJeff Bonwick 
16950521c5ebSJustin Gibbs void
spa_evicting_os_register(spa_t * spa,objset_t * os)16960521c5ebSJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os)
16970521c5ebSJustin Gibbs {
16980521c5ebSJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
16990521c5ebSJustin Gibbs 	list_insert_head(&spa->spa_evicting_os_list, os);
17000521c5ebSJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
17010521c5ebSJustin Gibbs }
17020521c5ebSJustin Gibbs 
17030521c5ebSJustin Gibbs void
spa_evicting_os_deregister(spa_t * spa,objset_t * os)17040521c5ebSJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os)
17050521c5ebSJustin Gibbs {
17060521c5ebSJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
17070521c5ebSJustin Gibbs 	list_remove(&spa->spa_evicting_os_list, os);
17080521c5ebSJustin Gibbs 	cv_broadcast(&spa->spa_evicting_os_cv);
17090521c5ebSJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
17100521c5ebSJustin Gibbs }
17110521c5ebSJustin Gibbs 
17120521c5ebSJustin Gibbs void
spa_evicting_os_wait(spa_t * spa)17130521c5ebSJustin Gibbs spa_evicting_os_wait(spa_t *spa)
17140521c5ebSJustin Gibbs {
17150521c5ebSJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
17160521c5ebSJustin Gibbs 	while (!list_is_empty(&spa->spa_evicting_os_list))
17170521c5ebSJustin Gibbs 		cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
17180521c5ebSJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
17190521c5ebSJustin Gibbs 
17200521c5ebSJustin Gibbs 	dmu_buf_user_evict_wait();
17210521c5ebSJustin Gibbs }
17220521c5ebSJustin Gibbs 
172344cd46caSbillm int
spa_max_replication(spa_t * spa)172444cd46caSbillm spa_max_replication(spa_t *spa)
172544cd46caSbillm {
172644cd46caSbillm 	/*
1727e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
172844cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
172944cd46caSbillm 	 * replication level accordingly.
173044cd46caSbillm 	 */
1731e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
173244cd46caSbillm 		return (1);
173344cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1734fa9e4066Sahrens }
1735fa9e4066Sahrens 
17363f9d6ad7SLin Ling int
spa_prev_software_version(spa_t * spa)17373f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
17383f9d6ad7SLin Ling {
17393f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
17403f9d6ad7SLin Ling }
17413f9d6ad7SLin Ling 
174299653d4eSeschrock uint64_t
spa_deadman_synctime(spa_t * spa)1743283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1744283b8460SGeorge.Wilson {
1745283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1746283b8460SGeorge.Wilson }
1747283b8460SGeorge.Wilson 
1748283b8460SGeorge.Wilson uint64_t
dva_get_dsize_sync(spa_t * spa,const dva_t * dva)1749b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
175099653d4eSeschrock {
1751b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1752b24ab676SJeff Bonwick 	uint64_t dsize = asize;
175399653d4eSeschrock 
1754b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1755b24ab676SJeff Bonwick 
1756b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1757b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1758b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1759b24ab676SJeff Bonwick 	}
1760b24ab676SJeff Bonwick 
1761b24ab676SJeff Bonwick 	return (dsize);
1762b24ab676SJeff Bonwick }
1763b24ab676SJeff Bonwick 
1764b24ab676SJeff Bonwick uint64_t
bp_get_dsize_sync(spa_t * spa,const blkptr_t * bp)1765b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1766b24ab676SJeff Bonwick {
1767b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1768b24ab676SJeff Bonwick 
1769e54e0be9SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1770b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1771b24ab676SJeff Bonwick 
1772b24ab676SJeff Bonwick 	return (dsize);
1773b24ab676SJeff Bonwick }
1774b24ab676SJeff Bonwick 
1775b24ab676SJeff Bonwick uint64_t
bp_get_dsize(spa_t * spa,const blkptr_t * bp)1776b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1777b24ab676SJeff Bonwick {
1778b24ab676SJeff Bonwick 	uint64_t dsize = 0;
177999653d4eSeschrock 
1780e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1781b24ab676SJeff Bonwick 
1782e54e0be9SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1783b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1784b24ab676SJeff Bonwick 
1785e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1786b24ab676SJeff Bonwick 
1787b24ab676SJeff Bonwick 	return (dsize);
178899653d4eSeschrock }
178999653d4eSeschrock 
1790fa9e4066Sahrens /*
1791fa9e4066Sahrens  * ==========================================================================
1792fa9e4066Sahrens  * Initialization and Termination
1793fa9e4066Sahrens  * ==========================================================================
1794fa9e4066Sahrens  */
1795fa9e4066Sahrens 
1796fa9e4066Sahrens static int
spa_name_compare(const void * a1,const void * a2)1797fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1798fa9e4066Sahrens {
1799fa9e4066Sahrens 	const spa_t *s1 = a1;
1800fa9e4066Sahrens 	const spa_t *s2 = a2;
1801fa9e4066Sahrens 	int s;
1802fa9e4066Sahrens 
1803fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1804fa9e4066Sahrens 	if (s > 0)
1805fa9e4066Sahrens 		return (1);
1806fa9e4066Sahrens 	if (s < 0)
1807fa9e4066Sahrens 		return (-1);
1808fa9e4066Sahrens 	return (0);
1809fa9e4066Sahrens }
1810fa9e4066Sahrens 
18110373e76bSbonwick int
spa_busy(void)18120373e76bSbonwick spa_busy(void)
18130373e76bSbonwick {
18140373e76bSbonwick 	return (spa_active_count);
18150373e76bSbonwick }
18160373e76bSbonwick 
1817fa9e4066Sahrens void
spa_boot_init()1818e7cbe64fSgw25295 spa_boot_init()
1819e7cbe64fSgw25295 {
1820e7cbe64fSgw25295 	spa_config_load();
1821e7cbe64fSgw25295 }
1822e7cbe64fSgw25295 
1823e7cbe64fSgw25295 void
spa_init(int mode)1824fa9e4066Sahrens spa_init(int mode)
1825fa9e4066Sahrens {
1826fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1827c25056deSgw25295 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1828fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1829fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1830fa9e4066Sahrens 
1831fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1832fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1833fa9e4066Sahrens 
1834fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1835fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1836fa94a07fSbrendan 
1837fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1838fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
183999653d4eSeschrock 
18408ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1841fa9e4066Sahrens 
1842283b8460SGeorge.Wilson #ifdef _KERNEL
1843283b8460SGeorge.Wilson 	spa_arch_init();
1844283b8460SGeorge.Wilson #else
1845cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1846cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1847cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1848cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1849cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1850cd1c8b85SMatthew Ahrens 		} else {
1851cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1852cd1c8b85SMatthew Ahrens 		}
1853cd1c8b85SMatthew Ahrens 	}
1854cd1c8b85SMatthew Ahrens #endif
1855cd1c8b85SMatthew Ahrens 
1856fa9e4066Sahrens 	refcount_init();
1857fa9e4066Sahrens 	unique_init();
18580713e232SGeorge Wilson 	range_tree_init();
1859fa9e4066Sahrens 	zio_init();
1860fa9e4066Sahrens 	dmu_init();
1861fa9e4066Sahrens 	zil_init();
186287db74c1Sek110237 	vdev_cache_stat_init();
186391ebeef5Sahrens 	zfs_prop_init();
1864990b4856Slling 	zpool_prop_init();
1865ad135b5dSChristopher Siden 	zpool_feature_init();
1866fa9e4066Sahrens 	spa_config_load();
1867e14bb325SJeff Bonwick 	l2arc_start();
1868fa9e4066Sahrens }
1869fa9e4066Sahrens 
1870fa9e4066Sahrens void
spa_fini(void)1871fa9e4066Sahrens spa_fini(void)
1872fa9e4066Sahrens {
1873e14bb325SJeff Bonwick 	l2arc_stop();
1874e14bb325SJeff Bonwick 
1875fa9e4066Sahrens 	spa_evict_all();
1876fa9e4066Sahrens 
187787db74c1Sek110237 	vdev_cache_stat_fini();
1878fa9e4066Sahrens 	zil_fini();
1879fa9e4066Sahrens 	dmu_fini();
1880fa9e4066Sahrens 	zio_fini();
18810713e232SGeorge Wilson 	range_tree_fini();
188291ebeef5Sahrens 	unique_fini();
1883fa9e4066Sahrens 	refcount_fini();
1884fa9e4066Sahrens 
1885fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
188699653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1887fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1888fa9e4066Sahrens 
1889fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1890fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1891c25056deSgw25295 	mutex_destroy(&spa_spare_lock);
1892fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1893fa9e4066Sahrens }
18946ce0521aSperrin 
18956ce0521aSperrin /*
18966ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
18976ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
18986ce0521aSperrin  * performance and not correctness
18996ce0521aSperrin  */
19006ce0521aSperrin boolean_t
spa_has_slogs(spa_t * spa)19016ce0521aSperrin spa_has_slogs(spa_t *spa)
19026ce0521aSperrin {
19036ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
19046ce0521aSperrin }
1905bf82a41bSeschrock 
1906b24ab676SJeff Bonwick spa_log_state_t
spa_get_log_state(spa_t * spa)1907b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1908b24ab676SJeff Bonwick {
1909b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1910b24ab676SJeff Bonwick }
1911b24ab676SJeff Bonwick 
1912b24ab676SJeff Bonwick void
spa_set_log_state(spa_t * spa,spa_log_state_t state)1913b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
1914b24ab676SJeff Bonwick {
1915b24ab676SJeff Bonwick 	spa->spa_log_state = state;
1916b24ab676SJeff Bonwick }
1917b24ab676SJeff Bonwick 
1918bf82a41bSeschrock boolean_t
spa_is_root(spa_t * spa)1919bf82a41bSeschrock spa_is_root(spa_t *spa)
1920bf82a41bSeschrock {
1921bf82a41bSeschrock 	return (spa->spa_is_root);
1922bf82a41bSeschrock }
19238ad4d6ddSJeff Bonwick 
19248ad4d6ddSJeff Bonwick boolean_t
spa_writeable(spa_t * spa)19258ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
19268ad4d6ddSJeff Bonwick {
19278ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
19288ad4d6ddSJeff Bonwick }
19298ad4d6ddSJeff Bonwick 
1930fa4888e9SAlex Reece /*
1931fa4888e9SAlex Reece  * Returns true if there is a pending sync task in any of the current
1932fa4888e9SAlex Reece  * syncing txg, the current quiescing txg, or the current open txg.
1933fa4888e9SAlex Reece  */
1934fa4888e9SAlex Reece boolean_t
spa_has_pending_synctask(spa_t * spa)1935fa4888e9SAlex Reece spa_has_pending_synctask(spa_t *spa)
1936fa4888e9SAlex Reece {
1937fa4888e9SAlex Reece 	return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
1938fa4888e9SAlex Reece }
1939fa4888e9SAlex Reece 
19408ad4d6ddSJeff Bonwick int
spa_mode(spa_t * spa)19418ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
19428ad4d6ddSJeff Bonwick {
19438ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
19448ad4d6ddSJeff Bonwick }
1945b24ab676SJeff Bonwick 
1946b24ab676SJeff Bonwick uint64_t
spa_bootfs(spa_t * spa)1947b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
1948b24ab676SJeff Bonwick {
1949b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
1950b24ab676SJeff Bonwick }
1951b24ab676SJeff Bonwick 
1952b24ab676SJeff Bonwick uint64_t
spa_delegation(spa_t * spa)1953b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
1954b24ab676SJeff Bonwick {
1955b24ab676SJeff Bonwick 	return (spa->spa_delegation);
1956b24ab676SJeff Bonwick }
1957b24ab676SJeff Bonwick 
1958b24ab676SJeff Bonwick objset_t *
spa_meta_objset(spa_t * spa)1959b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
1960b24ab676SJeff Bonwick {
1961b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
1962b24ab676SJeff Bonwick }
1963b24ab676SJeff Bonwick 
1964b24ab676SJeff Bonwick enum zio_checksum
spa_dedup_checksum(spa_t * spa)1965b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
1966b24ab676SJeff Bonwick {
1967b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
1968b24ab676SJeff Bonwick }
19693f9d6ad7SLin Ling 
19703f9d6ad7SLin Ling /*
19713f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
19723f9d6ad7SLin Ling  */
19733f9d6ad7SLin Ling void
spa_scan_stat_init(spa_t * spa)19743f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
19753f9d6ad7SLin Ling {
19763f9d6ad7SLin Ling 	/* data not stored on disk */
19773f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
19783f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
19793f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
19803f9d6ad7SLin Ling }
19813f9d6ad7SLin Ling 
19823f9d6ad7SLin Ling /*
19833f9d6ad7SLin Ling  * Get scan stats for zpool status reports
19843f9d6ad7SLin Ling  */
19853f9d6ad7SLin Ling int
spa_scan_get_stats(spa_t * spa,pool_scan_stat_t * ps)19863f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
19873f9d6ad7SLin Ling {
19883f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
19893f9d6ad7SLin Ling 
19903f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1991be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
19923f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
19933f9d6ad7SLin Ling 
19943f9d6ad7SLin Ling 	/* data stored on disk */
19953f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
19963f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
19973f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
19983f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
19993f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
20003f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
20013f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
20023f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
20033f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
20043f9d6ad7SLin Ling 
20053f9d6ad7SLin Ling 	/* data not stored on disk */
20063f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
20073f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
20083f9d6ad7SLin Ling 
20093f9d6ad7SLin Ling 	return (0);
20103f9d6ad7SLin Ling }
201109c9d376SGeorge Wilson 
201209c9d376SGeorge Wilson boolean_t
spa_debug_enabled(spa_t * spa)201309c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
201409c9d376SGeorge Wilson {
201509c9d376SGeorge Wilson 	return (spa->spa_debug);
201609c9d376SGeorge Wilson }
2017d1a98260SMatthew Ahrens 
2018d1a98260SMatthew Ahrens int
spa_maxblocksize(spa_t * spa)2019d1a98260SMatthew Ahrens spa_maxblocksize(spa_t *spa)
2020d1a98260SMatthew Ahrens {
2021d1a98260SMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2022d1a98260SMatthew Ahrens 		return (SPA_MAXBLOCKSIZE);
2023d1a98260SMatthew Ahrens 	else
2024d1a98260SMatthew Ahrens 		return (SPA_OLD_MAXBLOCKSIZE);
2025d1a98260SMatthew Ahrens }
2026