xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_misc.c (revision 3ee8c80c747c4aa3f83351a6920f30c411236e1b)
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.
2394c2d0ebSMatthew Ahrens  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
24e495b6e6SSaso Kiselkov  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2645818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
281702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/spa_impl.h>
33283b8460SGeorge.Wilson #include <sys/spa_boot.h>
34fa9e4066Sahrens #include <sys/zio.h>
35fa9e4066Sahrens #include <sys/zio_checksum.h>
36fa9e4066Sahrens #include <sys/zio_compress.h>
37fa9e4066Sahrens #include <sys/dmu.h>
38fa9e4066Sahrens #include <sys/dmu_tx.h>
39fa9e4066Sahrens #include <sys/zap.h>
40fa9e4066Sahrens #include <sys/zil.h>
41fa9e4066Sahrens #include <sys/vdev_impl.h>
42fa9e4066Sahrens #include <sys/metaslab.h>
43fa9e4066Sahrens #include <sys/uberblock_impl.h>
44fa9e4066Sahrens #include <sys/txg.h>
45fa9e4066Sahrens #include <sys/avl.h>
46fa9e4066Sahrens #include <sys/unique.h>
47fa9e4066Sahrens #include <sys/dsl_pool.h>
48fa9e4066Sahrens #include <sys/dsl_dir.h>
49fa9e4066Sahrens #include <sys/dsl_prop.h>
503f9d6ad7SLin Ling #include <sys/dsl_scan.h>
51fa9e4066Sahrens #include <sys/fs/zfs.h>
526ce0521aSperrin #include <sys/metaslab_impl.h>
53e14bb325SJeff Bonwick #include <sys/arc.h>
54485bbbf5SGeorge Wilson #include <sys/ddt.h>
5591ebeef5Sahrens #include "zfs_prop.h"
5645818ee1SMatthew Ahrens #include <sys/zfeature.h>
57fa9e4066Sahrens 
58fa9e4066Sahrens /*
59fa9e4066Sahrens  * SPA locking
60fa9e4066Sahrens  *
61fa9e4066Sahrens  * There are four basic locks for managing spa_t structures:
62fa9e4066Sahrens  *
63fa9e4066Sahrens  * spa_namespace_lock (global mutex)
64fa9e4066Sahrens  *
6544cd46caSbillm  *	This lock must be acquired to do any of the following:
66fa9e4066Sahrens  *
6744cd46caSbillm  *		- Lookup a spa_t by name
6844cd46caSbillm  *		- Add or remove a spa_t from the namespace
6944cd46caSbillm  *		- Increase spa_refcount from non-zero
7044cd46caSbillm  *		- Check if spa_refcount is zero
7144cd46caSbillm  *		- Rename a spa_t
72ea8dc4b6Seschrock  *		- add/remove/attach/detach devices
7344cd46caSbillm  *		- Held for the duration of create/destroy/import/export
74fa9e4066Sahrens  *
7544cd46caSbillm  *	It does not need to handle recursion.  A create or destroy may
7644cd46caSbillm  *	reference objects (files or zvols) in other pools, but by
7744cd46caSbillm  *	definition they must have an existing reference, and will never need
7844cd46caSbillm  *	to lookup a spa_t by name.
79fa9e4066Sahrens  *
80fa9e4066Sahrens  * spa_refcount (per-spa refcount_t protected by mutex)
81fa9e4066Sahrens  *
8244cd46caSbillm  *	This reference count keep track of any active users of the spa_t.  The
8344cd46caSbillm  *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8444cd46caSbillm  *	the refcount is never really 'zero' - opening a pool implicitly keeps
85088f3894Sahrens  *	some references in the DMU.  Internally we check against spa_minref, but
8644cd46caSbillm  *	present the image of a zero/non-zero value to consumers.
87fa9e4066Sahrens  *
88e14bb325SJeff Bonwick  * spa_config_lock[] (per-spa array of rwlocks)
89fa9e4066Sahrens  *
9091ebeef5Sahrens  *	This protects the spa_t from config changes, and must be held in
9191ebeef5Sahrens  *	the following circumstances:
92fa9e4066Sahrens  *
9344cd46caSbillm  *		- RW_READER to perform I/O to the spa
9444cd46caSbillm  *		- RW_WRITER to change the vdev config
95fa9e4066Sahrens  *
96fa9e4066Sahrens  * The locking order is fairly straightforward:
97fa9e4066Sahrens  *
9844cd46caSbillm  *		spa_namespace_lock	->	spa_refcount
99fa9e4066Sahrens  *
10044cd46caSbillm  *	The namespace lock must be acquired to increase the refcount from 0
10144cd46caSbillm  *	or to check if it is zero.
102fa9e4066Sahrens  *
103e14bb325SJeff Bonwick  *		spa_refcount		->	spa_config_lock[]
104fa9e4066Sahrens  *
10544cd46caSbillm  *	There must be at least one valid reference on the spa_t to acquire
10644cd46caSbillm  *	the config lock.
107fa9e4066Sahrens  *
108e14bb325SJeff Bonwick  *		spa_namespace_lock	->	spa_config_lock[]
109fa9e4066Sahrens  *
11044cd46caSbillm  *	The namespace lock must always be taken before the config lock.
111fa9e4066Sahrens  *
112fa9e4066Sahrens  *
113e14bb325SJeff Bonwick  * The spa_namespace_lock can be acquired directly and is globally visible.
114fa9e4066Sahrens  *
115e14bb325SJeff Bonwick  * The namespace is manipulated using the following functions, all of which
116e14bb325SJeff Bonwick  * require the spa_namespace_lock to be held.
117fa9e4066Sahrens  *
11844cd46caSbillm  *	spa_lookup()		Lookup a spa_t by name.
119fa9e4066Sahrens  *
12044cd46caSbillm  *	spa_add()		Create a new spa_t in the namespace.
121fa9e4066Sahrens  *
12244cd46caSbillm  *	spa_remove()		Remove a spa_t from the namespace.  This also
12344cd46caSbillm  *				frees up any memory associated with the spa_t.
124fa9e4066Sahrens  *
12544cd46caSbillm  *	spa_next()		Returns the next spa_t in the system, or the
12644cd46caSbillm  *				first if NULL is passed.
127fa9e4066Sahrens  *
12844cd46caSbillm  *	spa_evict_all()		Shutdown and remove all spa_t structures in
12944cd46caSbillm  *				the system.
130fa9e4066Sahrens  *
131ea8dc4b6Seschrock  *	spa_guid_exists()	Determine whether a pool/device guid exists.
132fa9e4066Sahrens  *
133fa9e4066Sahrens  * The spa_refcount is manipulated using the following functions:
134fa9e4066Sahrens  *
13544cd46caSbillm  *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
13644cd46caSbillm  *				called with spa_namespace_lock held if the
13744cd46caSbillm  *				refcount is currently zero.
138fa9e4066Sahrens  *
13944cd46caSbillm  *	spa_close()		Remove a reference from the spa_t.  This will
14044cd46caSbillm  *				not free the spa_t or remove it from the
14144cd46caSbillm  *				namespace.  No locking is required.
142fa9e4066Sahrens  *
14344cd46caSbillm  *	spa_refcount_zero()	Returns true if the refcount is currently
14444cd46caSbillm  *				zero.  Must be called with spa_namespace_lock
14544cd46caSbillm  *				held.
146fa9e4066Sahrens  *
147e14bb325SJeff Bonwick  * The spa_config_lock[] is an array of rwlocks, ordered as follows:
148e14bb325SJeff Bonwick  * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
149e14bb325SJeff Bonwick  * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
150e14bb325SJeff Bonwick  *
151e14bb325SJeff Bonwick  * To read the configuration, it suffices to hold one of these locks as reader.
152e14bb325SJeff Bonwick  * To modify the configuration, you must hold all locks as writer.  To modify
153e14bb325SJeff Bonwick  * vdev state without altering the vdev tree's topology (e.g. online/offline),
154e14bb325SJeff Bonwick  * you must hold SCL_STATE and SCL_ZIO as writer.
155e14bb325SJeff Bonwick  *
156e14bb325SJeff Bonwick  * We use these distinct config locks to avoid recursive lock entry.
157e14bb325SJeff Bonwick  * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
158e14bb325SJeff Bonwick  * block allocations (SCL_ALLOC), which may require reading space maps
159e14bb325SJeff Bonwick  * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
160e14bb325SJeff Bonwick  *
161e14bb325SJeff Bonwick  * The spa config locks cannot be normal rwlocks because we need the
162e14bb325SJeff Bonwick  * ability to hand off ownership.  For example, SCL_ZIO is acquired
163e14bb325SJeff Bonwick  * by the issuing thread and later released by an interrupt thread.
164e14bb325SJeff Bonwick  * They do, however, obey the usual write-wanted semantics to prevent
165e14bb325SJeff Bonwick  * writer (i.e. system administrator) starvation.
166e14bb325SJeff Bonwick  *
167e14bb325SJeff Bonwick  * The lock acquisition rules are as follows:
168e14bb325SJeff Bonwick  *
169e14bb325SJeff Bonwick  * SCL_CONFIG
170e14bb325SJeff Bonwick  *	Protects changes to the vdev tree topology, such as vdev
171e14bb325SJeff Bonwick  *	add/remove/attach/detach.  Protects the dirty config list
172e14bb325SJeff Bonwick  *	(spa_config_dirty_list) and the set of spares and l2arc devices.
173e14bb325SJeff Bonwick  *
174e14bb325SJeff Bonwick  * SCL_STATE
175e14bb325SJeff Bonwick  *	Protects changes to pool state and vdev state, such as vdev
176e14bb325SJeff Bonwick  *	online/offline/fault/degrade/clear.  Protects the dirty state list
177e14bb325SJeff Bonwick  *	(spa_state_dirty_list) and global pool state (spa_state).
178e14bb325SJeff Bonwick  *
179e14bb325SJeff Bonwick  * SCL_ALLOC
180e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
181e14bb325SJeff Bonwick  *	Held as reader by metaslab_alloc() and metaslab_claim().
182e14bb325SJeff Bonwick  *
183e14bb325SJeff Bonwick  * SCL_ZIO
184e14bb325SJeff Bonwick  *	Held by bp-level zios (those which have no io_vd upon entry)
185e14bb325SJeff Bonwick  *	to prevent changes to the vdev tree.  The bp-level zio implicitly
186e14bb325SJeff Bonwick  *	protects all of its vdev child zios, which do not hold SCL_ZIO.
187e14bb325SJeff Bonwick  *
188e14bb325SJeff Bonwick  * SCL_FREE
189e14bb325SJeff Bonwick  *	Protects changes to metaslab groups and classes.
190e14bb325SJeff Bonwick  *	Held as reader by metaslab_free().  SCL_FREE is distinct from
191e14bb325SJeff Bonwick  *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
192e14bb325SJeff Bonwick  *	blocks in zio_done() while another i/o that holds either
193e14bb325SJeff Bonwick  *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
194e14bb325SJeff Bonwick  *
195e14bb325SJeff Bonwick  * SCL_VDEV
196e14bb325SJeff Bonwick  *	Held as reader to prevent changes to the vdev tree during trivial
197b24ab676SJeff Bonwick  *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
198e14bb325SJeff Bonwick  *	other locks, and lower than all of them, to ensure that it's safe
199e14bb325SJeff Bonwick  *	to acquire regardless of caller context.
200e14bb325SJeff Bonwick  *
201e14bb325SJeff Bonwick  * In addition, the following rules apply:
202e14bb325SJeff Bonwick  *
203e14bb325SJeff Bonwick  * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
204e14bb325SJeff Bonwick  *	The lock ordering is SCL_CONFIG > spa_props_lock.
205e14bb325SJeff Bonwick  *
206e14bb325SJeff Bonwick  * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
207e14bb325SJeff Bonwick  *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
208e14bb325SJeff Bonwick  *	or zio_write_phys() -- the caller must ensure that the config cannot
209e14bb325SJeff Bonwick  *	cannot change in the interim, and that the vdev cannot be reopened.
210e14bb325SJeff Bonwick  *	SCL_STATE as reader suffices for both.
211fa9e4066Sahrens  *
212ea8dc4b6Seschrock  * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
213fa9e4066Sahrens  *
21444cd46caSbillm  *	spa_vdev_enter()	Acquire the namespace lock and the config lock
215ea8dc4b6Seschrock  *				for writing.
216fa9e4066Sahrens  *
21744cd46caSbillm  *	spa_vdev_exit()		Release the config lock, wait for all I/O
21844cd46caSbillm  *				to complete, sync the updated configs to the
219ea8dc4b6Seschrock  *				cache, and release the namespace lock.
220fa9e4066Sahrens  *
221e14bb325SJeff Bonwick  * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
222e14bb325SJeff Bonwick  * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
223e14bb325SJeff Bonwick  * locking is, always, based on spa_namespace_lock and spa_config_lock[].
224e14bb325SJeff Bonwick  *
225ad135b5dSChristopher Siden  * spa_rename() is also implemented within this file since it requires
226e14bb325SJeff Bonwick  * manipulation of the namespace.
227fa9e4066Sahrens  */
228fa9e4066Sahrens 
229fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
230fa9e4066Sahrens kmutex_t spa_namespace_lock;
231fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2320373e76bSbonwick static int spa_active_count;
233416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
234fa9e4066Sahrens 
23599653d4eSeschrock static kmutex_t spa_spare_lock;
23639c23413Seschrock static avl_tree_t spa_spare_avl;
237fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
238fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
23999653d4eSeschrock 
240fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2418ad4d6ddSJeff Bonwick int spa_mode_global;
242fa9e4066Sahrens 
243fa9e4066Sahrens #ifdef ZFS_DEBUG
2445cabbc6bSPrashanth Sreenivasa /*
2455cabbc6bSPrashanth Sreenivasa  * Everything except dprintf, spa, and indirect_remap is on by default
2465cabbc6bSPrashanth Sreenivasa  * in debug builds.
2475cabbc6bSPrashanth Sreenivasa  */
2485cabbc6bSPrashanth Sreenivasa int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA | ZFS_DEBUG_INDIRECT_REMAP);
249fa9e4066Sahrens #else
250fa9e4066Sahrens int zfs_flags = 0;
251fa9e4066Sahrens #endif
252fa9e4066Sahrens 
2530125049cSahrens /*
2540125049cSahrens  * zfs_recover can be set to nonzero to attempt to recover from
2550125049cSahrens  * otherwise-fatal errors, typically caused by on-disk corruption.  When
2560125049cSahrens  * set, calls to zfs_panic_recover() will turn into warning messages.
2578b36997aSMatthew Ahrens  * This should only be used as a last resort, as it typically results
2588b36997aSMatthew Ahrens  * in leaked space, or worse.
2590125049cSahrens  */
2607fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE;
2617fd05ac4SMatthew Ahrens 
2627fd05ac4SMatthew Ahrens /*
2637fd05ac4SMatthew Ahrens  * If destroy encounters an EIO while reading metadata (e.g. indirect
2647fd05ac4SMatthew Ahrens  * blocks), space referenced by the missing metadata can not be freed.
2657fd05ac4SMatthew Ahrens  * Normally this causes the background destroy to become "stalled", as
2667fd05ac4SMatthew Ahrens  * it is unable to make forward progress.  While in this stalled state,
2677fd05ac4SMatthew Ahrens  * all remaining space to free from the error-encountering filesystem is
2687fd05ac4SMatthew Ahrens  * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
2697fd05ac4SMatthew Ahrens  * permanently leak the space from indirect blocks that can not be read,
2707fd05ac4SMatthew Ahrens  * and continue to free everything else that it can.
2717fd05ac4SMatthew Ahrens  *
2727fd05ac4SMatthew Ahrens  * The default, "stalling" behavior is useful if the storage partially
2737fd05ac4SMatthew Ahrens  * fails (i.e. some but not all i/os fail), and then later recovers.  In
2747fd05ac4SMatthew Ahrens  * this case, we will be able to continue pool operations while it is
2757fd05ac4SMatthew Ahrens  * partially failed, and when it recovers, we can continue to free the
2767fd05ac4SMatthew Ahrens  * space, with no leaks.  However, note that this case is actually
2777fd05ac4SMatthew Ahrens  * fairly rare.
2787fd05ac4SMatthew Ahrens  *
2797fd05ac4SMatthew Ahrens  * Typically pools either (a) fail completely (but perhaps temporarily,
2807fd05ac4SMatthew Ahrens  * e.g. a top-level vdev going offline), or (b) have localized,
2817fd05ac4SMatthew Ahrens  * permanent errors (e.g. disk returns the wrong data due to bit flip or
2827fd05ac4SMatthew Ahrens  * firmware bug).  In case (a), this setting does not matter because the
2837fd05ac4SMatthew Ahrens  * pool will be suspended and the sync thread will not be able to make
2847fd05ac4SMatthew Ahrens  * forward progress regardless.  In case (b), because the error is
2857fd05ac4SMatthew Ahrens  * permanent, the best we can do is leak the minimum amount of space,
2867fd05ac4SMatthew Ahrens  * which is what setting this flag will do.  Therefore, it is reasonable
2877fd05ac4SMatthew Ahrens  * for this flag to normally be set, but we chose the more conservative
2887fd05ac4SMatthew Ahrens  * approach of not setting it, so that there is no possibility of
2897fd05ac4SMatthew Ahrens  * leaking space in the "partial temporary" failure case.
2907fd05ac4SMatthew Ahrens  */
2917fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE;
2920125049cSahrens 
29369962b56SMatthew Ahrens /*
29469962b56SMatthew Ahrens  * Expiration time in milliseconds. This value has two meanings. First it is
29569962b56SMatthew Ahrens  * used to determine when the spa_deadman() logic should fire. By default the
29669962b56SMatthew Ahrens  * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
29769962b56SMatthew Ahrens  * Secondly, the value determines if an I/O is considered "hung". Any I/O that
29869962b56SMatthew Ahrens  * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
29969962b56SMatthew Ahrens  * in a system panic.
30069962b56SMatthew Ahrens  */
30169962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL;
302283b8460SGeorge.Wilson 
303283b8460SGeorge.Wilson /*
30469962b56SMatthew Ahrens  * Check time in milliseconds. This defines the frequency at which we check
30569962b56SMatthew Ahrens  * for hung I/O.
306283b8460SGeorge.Wilson  */
30769962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL;
308283b8460SGeorge.Wilson 
309283b8460SGeorge.Wilson /*
310283b8460SGeorge.Wilson  * Override the zfs deadman behavior via /etc/system. By default the
311283b8460SGeorge.Wilson  * deadman is enabled except on VMware and sparc deployments.
312283b8460SGeorge.Wilson  */
313283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
314283b8460SGeorge.Wilson 
31569962b56SMatthew Ahrens /*
31669962b56SMatthew Ahrens  * The worst case is single-sector max-parity RAID-Z blocks, in which
31769962b56SMatthew Ahrens  * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
31869962b56SMatthew Ahrens  * times the size; so just assume that.  Add to this the fact that
31969962b56SMatthew Ahrens  * we can have up to 3 DVAs per bp, and one more factor of 2 because
32069962b56SMatthew Ahrens  * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
32169962b56SMatthew Ahrens  * the worst case is:
32269962b56SMatthew Ahrens  *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
32369962b56SMatthew Ahrens  */
32469962b56SMatthew Ahrens int spa_asize_inflation = 24;
325fa9e4066Sahrens 
3267d46dc6cSMatthew Ahrens /*
3277d46dc6cSMatthew Ahrens  * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3287d46dc6cSMatthew Ahrens  * the pool to be consumed.  This ensures that we don't run the pool
3297d46dc6cSMatthew Ahrens  * completely out of space, due to unaccounted changes (e.g. to the MOS).
3307d46dc6cSMatthew Ahrens  * It also limits the worst-case time to allocate space.  If we have
3317d46dc6cSMatthew Ahrens  * less than this amount of free space, most ZPL operations (e.g. write,
3327d46dc6cSMatthew Ahrens  * create) will return ENOSPC.
3337d46dc6cSMatthew Ahrens  *
3347d46dc6cSMatthew Ahrens  * Certain operations (e.g. file removal, most administrative actions) can
3357d46dc6cSMatthew Ahrens  * use half the slop space.  They will only return ENOSPC if less than half
3367d46dc6cSMatthew Ahrens  * the slop space is free.  Typically, once the pool has less than the slop
3377d46dc6cSMatthew Ahrens  * space free, the user will use these operations to free up space in the pool.
3387d46dc6cSMatthew Ahrens  * These are the operations that call dsl_pool_adjustedsize() with the netfree
3397d46dc6cSMatthew Ahrens  * argument set to TRUE.
3407d46dc6cSMatthew Ahrens  *
3417d46dc6cSMatthew Ahrens  * A very restricted set of operations are always permitted, regardless of
3427d46dc6cSMatthew Ahrens  * the amount of free space.  These are the operations that call
3437d46dc6cSMatthew Ahrens  * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy".  If these
3447d46dc6cSMatthew Ahrens  * operations result in a net increase in the amount of space used,
3457d46dc6cSMatthew Ahrens  * it is possible to run the pool completely out of space, causing it to
3467d46dc6cSMatthew Ahrens  * be permanently read-only.
3477d46dc6cSMatthew Ahrens  *
3484b5c8e93SMatthew Ahrens  * Note that on very small pools, the slop space will be larger than
3494b5c8e93SMatthew Ahrens  * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
3504b5c8e93SMatthew Ahrens  * but we never allow it to be more than half the pool size.
3514b5c8e93SMatthew Ahrens  *
3527d46dc6cSMatthew Ahrens  * See also the comments in zfs_space_check_t.
3537d46dc6cSMatthew Ahrens  */
3547d46dc6cSMatthew Ahrens int spa_slop_shift = 5;
3554b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024;
3567d46dc6cSMatthew Ahrens 
357*3ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
358*3ee8c80cSPavel Zakharov void
359*3ee8c80cSPavel Zakharov spa_load_failed(spa_t *spa, const char *fmt, ...)
360*3ee8c80cSPavel Zakharov {
361*3ee8c80cSPavel Zakharov 	va_list adx;
362*3ee8c80cSPavel Zakharov 	char buf[256];
363*3ee8c80cSPavel Zakharov 
364*3ee8c80cSPavel Zakharov 	va_start(adx, fmt);
365*3ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
366*3ee8c80cSPavel Zakharov 	va_end(adx);
367*3ee8c80cSPavel Zakharov 
368*3ee8c80cSPavel Zakharov 	zfs_dbgmsg("spa_load(%s): FAILED: %s", spa->spa_name, buf);
369*3ee8c80cSPavel Zakharov }
370*3ee8c80cSPavel Zakharov 
371*3ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
372*3ee8c80cSPavel Zakharov void
373*3ee8c80cSPavel Zakharov spa_load_note(spa_t *spa, const char *fmt, ...)
374*3ee8c80cSPavel Zakharov {
375*3ee8c80cSPavel Zakharov 	va_list adx;
376*3ee8c80cSPavel Zakharov 	char buf[256];
377*3ee8c80cSPavel Zakharov 
378*3ee8c80cSPavel Zakharov 	va_start(adx, fmt);
379*3ee8c80cSPavel Zakharov 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
380*3ee8c80cSPavel Zakharov 	va_end(adx);
381*3ee8c80cSPavel Zakharov 
382*3ee8c80cSPavel Zakharov 	zfs_dbgmsg("spa_load(%s): %s", spa->spa_name, buf);
383*3ee8c80cSPavel Zakharov }
384*3ee8c80cSPavel Zakharov 
385e05725b1Sbonwick /*
386e05725b1Sbonwick  * ==========================================================================
387e05725b1Sbonwick  * SPA config locking
388e05725b1Sbonwick  * ==========================================================================
389e05725b1Sbonwick  */
390e05725b1Sbonwick static void
391e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
392e14bb325SJeff Bonwick {
393e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
394e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
395e14bb325SJeff Bonwick 		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
396e14bb325SJeff Bonwick 		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
3973b2aab18SMatthew Ahrens 		refcount_create_untracked(&scl->scl_count);
398e14bb325SJeff Bonwick 		scl->scl_writer = NULL;
399e14bb325SJeff Bonwick 		scl->scl_write_wanted = 0;
400e14bb325SJeff Bonwick 	}
401e05725b1Sbonwick }
402e05725b1Sbonwick 
403e05725b1Sbonwick static void
404e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
405e14bb325SJeff Bonwick {
406e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
407e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
408e14bb325SJeff Bonwick 		mutex_destroy(&scl->scl_lock);
409e14bb325SJeff Bonwick 		cv_destroy(&scl->scl_cv);
410e14bb325SJeff Bonwick 		refcount_destroy(&scl->scl_count);
411e14bb325SJeff Bonwick 		ASSERT(scl->scl_writer == NULL);
412e14bb325SJeff Bonwick 		ASSERT(scl->scl_write_wanted == 0);
413e14bb325SJeff Bonwick 	}
414e14bb325SJeff Bonwick }
415e14bb325SJeff Bonwick 
416e14bb325SJeff Bonwick int
417e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
418e05725b1Sbonwick {
419e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
420e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
421e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
422e14bb325SJeff Bonwick 			continue;
423e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
424e14bb325SJeff Bonwick 		if (rw == RW_READER) {
425e14bb325SJeff Bonwick 			if (scl->scl_writer || scl->scl_write_wanted) {
426e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
427e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
428e495b6e6SSaso Kiselkov 				    tag);
429e14bb325SJeff Bonwick 				return (0);
430e14bb325SJeff Bonwick 			}
431e14bb325SJeff Bonwick 		} else {
432e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
433e14bb325SJeff Bonwick 			if (!refcount_is_zero(&scl->scl_count)) {
434e14bb325SJeff Bonwick 				mutex_exit(&scl->scl_lock);
435e495b6e6SSaso Kiselkov 				spa_config_exit(spa, locks & ((1 << i) - 1),
436e495b6e6SSaso Kiselkov 				    tag);
437e14bb325SJeff Bonwick 				return (0);
438e14bb325SJeff Bonwick 			}
439e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
440e14bb325SJeff Bonwick 		}
441e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
442e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
443e14bb325SJeff Bonwick 	}
444e14bb325SJeff Bonwick 	return (1);
445e05725b1Sbonwick }
446e05725b1Sbonwick 
447e05725b1Sbonwick void
448e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
449e05725b1Sbonwick {
450f64c0e34SEric Taylor 	int wlocks_held = 0;
451f64c0e34SEric Taylor 
4523b2aab18SMatthew Ahrens 	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4533b2aab18SMatthew Ahrens 
454e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
455e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
456f64c0e34SEric Taylor 		if (scl->scl_writer == curthread)
457f64c0e34SEric Taylor 			wlocks_held |= (1 << i);
458e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
459e14bb325SJeff Bonwick 			continue;
460e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
461e14bb325SJeff Bonwick 		if (rw == RW_READER) {
462e14bb325SJeff Bonwick 			while (scl->scl_writer || scl->scl_write_wanted) {
463e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
464e14bb325SJeff Bonwick 			}
465e14bb325SJeff Bonwick 		} else {
466e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer != curthread);
467e14bb325SJeff Bonwick 			while (!refcount_is_zero(&scl->scl_count)) {
468e14bb325SJeff Bonwick 				scl->scl_write_wanted++;
469e14bb325SJeff Bonwick 				cv_wait(&scl->scl_cv, &scl->scl_lock);
470e14bb325SJeff Bonwick 				scl->scl_write_wanted--;
471e14bb325SJeff Bonwick 			}
472e14bb325SJeff Bonwick 			scl->scl_writer = curthread;
473e14bb325SJeff Bonwick 		}
474e14bb325SJeff Bonwick 		(void) refcount_add(&scl->scl_count, tag);
475e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
476e05725b1Sbonwick 	}
4775cabbc6bSPrashanth Sreenivasa 	ASSERT3U(wlocks_held, <=, locks);
478e05725b1Sbonwick }
479e05725b1Sbonwick 
480e05725b1Sbonwick void
481e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
482e05725b1Sbonwick {
483e14bb325SJeff Bonwick 	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
484e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
485e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
486e14bb325SJeff Bonwick 			continue;
487e14bb325SJeff Bonwick 		mutex_enter(&scl->scl_lock);
488e14bb325SJeff Bonwick 		ASSERT(!refcount_is_zero(&scl->scl_count));
489e14bb325SJeff Bonwick 		if (refcount_remove(&scl->scl_count, tag) == 0) {
490e14bb325SJeff Bonwick 			ASSERT(scl->scl_writer == NULL ||
491e14bb325SJeff Bonwick 			    scl->scl_writer == curthread);
492e14bb325SJeff Bonwick 			scl->scl_writer = NULL;	/* OK in either case */
493e14bb325SJeff Bonwick 			cv_broadcast(&scl->scl_cv);
494e14bb325SJeff Bonwick 		}
495e14bb325SJeff Bonwick 		mutex_exit(&scl->scl_lock);
496e05725b1Sbonwick 	}
497e05725b1Sbonwick }
498e05725b1Sbonwick 
499e14bb325SJeff Bonwick int
500e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
501e05725b1Sbonwick {
502e14bb325SJeff Bonwick 	int locks_held = 0;
503e05725b1Sbonwick 
504e14bb325SJeff Bonwick 	for (int i = 0; i < SCL_LOCKS; i++) {
505e14bb325SJeff Bonwick 		spa_config_lock_t *scl = &spa->spa_config_lock[i];
506e14bb325SJeff Bonwick 		if (!(locks & (1 << i)))
507e14bb325SJeff Bonwick 			continue;
508e14bb325SJeff Bonwick 		if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
509e14bb325SJeff Bonwick 		    (rw == RW_WRITER && scl->scl_writer == curthread))
510e14bb325SJeff Bonwick 			locks_held |= 1 << i;
511e14bb325SJeff Bonwick 	}
512e14bb325SJeff Bonwick 
513e14bb325SJeff Bonwick 	return (locks_held);
514e05725b1Sbonwick }
515e05725b1Sbonwick 
516fa9e4066Sahrens /*
517fa9e4066Sahrens  * ==========================================================================
518fa9e4066Sahrens  * SPA namespace functions
519fa9e4066Sahrens  * ==========================================================================
520fa9e4066Sahrens  */
521fa9e4066Sahrens 
522fa9e4066Sahrens /*
523fa9e4066Sahrens  * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
524fa9e4066Sahrens  * Returns NULL if no matching spa_t is found.
525fa9e4066Sahrens  */
526fa9e4066Sahrens spa_t *
527fa9e4066Sahrens spa_lookup(const char *name)
528fa9e4066Sahrens {
529e14bb325SJeff Bonwick 	static spa_t search;	/* spa_t is large; don't allocate on stack */
530e14bb325SJeff Bonwick 	spa_t *spa;
531fa9e4066Sahrens 	avl_index_t where;
53240feaa91Sahrens 	char *cp;
533fa9e4066Sahrens 
534fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
535fa9e4066Sahrens 
5363b2aab18SMatthew Ahrens 	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
5373b2aab18SMatthew Ahrens 
53840feaa91Sahrens 	/*
53940feaa91Sahrens 	 * If it's a full dataset name, figure out the pool name and
54040feaa91Sahrens 	 * just use that.
54140feaa91Sahrens 	 */
54278f17100SMatthew Ahrens 	cp = strpbrk(search.spa_name, "/@#");
5433b2aab18SMatthew Ahrens 	if (cp != NULL)
54440feaa91Sahrens 		*cp = '\0';
54540feaa91Sahrens 
546fa9e4066Sahrens 	spa = avl_find(&spa_namespace_avl, &search, &where);
547fa9e4066Sahrens 
548fa9e4066Sahrens 	return (spa);
549fa9e4066Sahrens }
550fa9e4066Sahrens 
551283b8460SGeorge.Wilson /*
552283b8460SGeorge.Wilson  * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
553283b8460SGeorge.Wilson  * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
554283b8460SGeorge.Wilson  * looking for potentially hung I/Os.
555283b8460SGeorge.Wilson  */
556283b8460SGeorge.Wilson void
557283b8460SGeorge.Wilson spa_deadman(void *arg)
558283b8460SGeorge.Wilson {
559283b8460SGeorge.Wilson 	spa_t *spa = arg;
560283b8460SGeorge.Wilson 
5610713e232SGeorge Wilson 	/*
5620713e232SGeorge Wilson 	 * Disable the deadman timer if the pool is suspended.
5630713e232SGeorge Wilson 	 */
5640713e232SGeorge Wilson 	if (spa_suspended(spa)) {
5650713e232SGeorge Wilson 		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5660713e232SGeorge Wilson 		return;
5670713e232SGeorge Wilson 	}
5680713e232SGeorge Wilson 
569283b8460SGeorge.Wilson 	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
570283b8460SGeorge.Wilson 	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
571283b8460SGeorge.Wilson 	    ++spa->spa_deadman_calls);
572283b8460SGeorge.Wilson 	if (zfs_deadman_enabled)
573283b8460SGeorge.Wilson 		vdev_deadman(spa->spa_root_vdev);
574283b8460SGeorge.Wilson }
575283b8460SGeorge.Wilson 
576fa9e4066Sahrens /*
577fa9e4066Sahrens  * Create an uninitialized spa_t with the given name.  Requires
578fa9e4066Sahrens  * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
579fa9e4066Sahrens  * exist by calling spa_lookup() first.
580fa9e4066Sahrens  */
581fa9e4066Sahrens spa_t *
582468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
583fa9e4066Sahrens {
584fa9e4066Sahrens 	spa_t *spa;
585c5904d13Seschrock 	spa_config_dirent_t *dp;
586283b8460SGeorge.Wilson 	cyc_handler_t hdlr;
587283b8460SGeorge.Wilson 	cyc_time_t when;
588fa9e4066Sahrens 
589fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
590fa9e4066Sahrens 
591fa9e4066Sahrens 	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
592fa9e4066Sahrens 
593c25056deSgw 	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
594c25056deSgw 	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
59535a5a358SJonathan Adams 	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
596bc9014e6SJustin Gibbs 	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
597c25056deSgw 	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
59835a5a358SJonathan Adams 	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
599c25056deSgw 	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
60045818ee1SMatthew Ahrens 	mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
60135a5a358SJonathan Adams 	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
602a1521560SJeff Bonwick 	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
603a1521560SJeff Bonwick 	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
604c3a66015SMatthew Ahrens 	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
6050f7643c7SGeorge Wilson 	mutex_init(&spa->spa_alloc_lock, NULL, MUTEX_DEFAULT, NULL);
606c25056deSgw 
607c25056deSgw 	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
608bc9014e6SJustin Gibbs 	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
60935a5a358SJonathan Adams 	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
610c25056deSgw 	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
611e14bb325SJeff Bonwick 	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
612c25056deSgw 
613b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
614cde58dbcSMatthew Ahrens 		bplist_create(&spa->spa_free_bplist[t]);
615b24ab676SJeff Bonwick 
616e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
617fa9e4066Sahrens 	spa->spa_state = POOL_STATE_UNINITIALIZED;
618fa9e4066Sahrens 	spa->spa_freeze_txg = UINT64_MAX;
6190373e76bSbonwick 	spa->spa_final_txg = UINT64_MAX;
620468c413aSTim Haley 	spa->spa_load_max_txg = UINT64_MAX;
62135a5a358SJonathan Adams 	spa->spa_proc = &p0;
62235a5a358SJonathan Adams 	spa->spa_proc_state = SPA_PROC_NONE;
623fa9e4066Sahrens 
624283b8460SGeorge.Wilson 	hdlr.cyh_func = spa_deadman;
625283b8460SGeorge.Wilson 	hdlr.cyh_arg = spa;
626283b8460SGeorge.Wilson 	hdlr.cyh_level = CY_LOW_LEVEL;
627283b8460SGeorge.Wilson 
62869962b56SMatthew Ahrens 	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
629283b8460SGeorge.Wilson 
630283b8460SGeorge.Wilson 	/*
631283b8460SGeorge.Wilson 	 * This determines how often we need to check for hung I/Os after
632283b8460SGeorge.Wilson 	 * the cyclic has already fired. Since checking for hung I/Os is
633283b8460SGeorge.Wilson 	 * an expensive operation we don't want to check too frequently.
63469962b56SMatthew Ahrens 	 * Instead wait for 5 seconds before checking again.
635283b8460SGeorge.Wilson 	 */
63669962b56SMatthew Ahrens 	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
637283b8460SGeorge.Wilson 	when.cyt_when = CY_INFINITY;
638283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
639283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
640283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
641283b8460SGeorge.Wilson 
642fa9e4066Sahrens 	refcount_create(&spa->spa_refcount);
643e14bb325SJeff Bonwick 	spa_config_lock_init(spa);
644fa9e4066Sahrens 
645fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
646fa9e4066Sahrens 
6470373e76bSbonwick 	/*
6480373e76bSbonwick 	 * Set the alternate root, if there is one.
6490373e76bSbonwick 	 */
6500373e76bSbonwick 	if (altroot) {
6510373e76bSbonwick 		spa->spa_root = spa_strdup(altroot);
6520373e76bSbonwick 		spa_active_count++;
6530373e76bSbonwick 	}
6540373e76bSbonwick 
65594c2d0ebSMatthew Ahrens 	avl_create(&spa->spa_alloc_tree, zio_bookmark_compare,
6560f7643c7SGeorge Wilson 	    sizeof (zio_t), offsetof(zio_t, io_alloc_node));
6570f7643c7SGeorge Wilson 
658c5904d13Seschrock 	/*
659c5904d13Seschrock 	 * Every pool starts with the default cachefile
660c5904d13Seschrock 	 */
661c5904d13Seschrock 	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
662c5904d13Seschrock 	    offsetof(spa_config_dirent_t, scd_link));
663c5904d13Seschrock 
664c5904d13Seschrock 	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
665ef912c80STim Haley 	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
666c5904d13Seschrock 	list_insert_head(&spa->spa_config_list, dp);
667c5904d13Seschrock 
6684b964adaSGeorge Wilson 	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
6694b964adaSGeorge Wilson 	    KM_SLEEP) == 0);
6704b964adaSGeorge Wilson 
671ad135b5dSChristopher Siden 	if (config != NULL) {
672ad135b5dSChristopher Siden 		nvlist_t *features;
673ad135b5dSChristopher Siden 
674ad135b5dSChristopher Siden 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
675ad135b5dSChristopher Siden 		    &features) == 0) {
676ad135b5dSChristopher Siden 			VERIFY(nvlist_dup(features, &spa->spa_label_features,
677ad135b5dSChristopher Siden 			    0) == 0);
678ad135b5dSChristopher Siden 		}
679ad135b5dSChristopher Siden 
680468c413aSTim Haley 		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
681ad135b5dSChristopher Siden 	}
682ad135b5dSChristopher Siden 
683ad135b5dSChristopher Siden 	if (spa->spa_label_features == NULL) {
684ad135b5dSChristopher Siden 		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
685ad135b5dSChristopher Siden 		    KM_SLEEP) == 0);
686ad135b5dSChristopher Siden 	}
687468c413aSTim Haley 
688c3a66015SMatthew Ahrens 	spa->spa_iokstat = kstat_create("zfs", 0, name,
689c3a66015SMatthew Ahrens 	    "disk", KSTAT_TYPE_IO, 1, 0);
690c3a66015SMatthew Ahrens 	if (spa->spa_iokstat) {
691c3a66015SMatthew Ahrens 		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
692c3a66015SMatthew Ahrens 		kstat_install(spa->spa_iokstat);
693c3a66015SMatthew Ahrens 	}
694c3a66015SMatthew Ahrens 
6953b2aab18SMatthew Ahrens 	spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
6963b2aab18SMatthew Ahrens 
69781cd5c55SMatthew Ahrens 	spa->spa_min_ashift = INT_MAX;
69881cd5c55SMatthew Ahrens 	spa->spa_max_ashift = 0;
69981cd5c55SMatthew Ahrens 
70043466aaeSMax Grossman 	/*
70143466aaeSMax Grossman 	 * As a pool is being created, treat all features as disabled by
70243466aaeSMax Grossman 	 * setting SPA_FEATURE_DISABLED for all entries in the feature
70343466aaeSMax Grossman 	 * refcount cache.
70443466aaeSMax Grossman 	 */
70543466aaeSMax Grossman 	for (int i = 0; i < SPA_FEATURES; i++) {
70643466aaeSMax Grossman 		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
70743466aaeSMax Grossman 	}
70843466aaeSMax Grossman 
709fa9e4066Sahrens 	return (spa);
710fa9e4066Sahrens }
711fa9e4066Sahrens 
712fa9e4066Sahrens /*
713fa9e4066Sahrens  * Removes a spa_t from the namespace, freeing up any memory used.  Requires
714fa9e4066Sahrens  * spa_namespace_lock.  This is called only after the spa_t has been closed and
715fa9e4066Sahrens  * deactivated.
716fa9e4066Sahrens  */
717fa9e4066Sahrens void
718fa9e4066Sahrens spa_remove(spa_t *spa)
719fa9e4066Sahrens {
720c5904d13Seschrock 	spa_config_dirent_t *dp;
721c5904d13Seschrock 
722fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
723fa9e4066Sahrens 	ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
724bc9014e6SJustin Gibbs 	ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
725fa9e4066Sahrens 
7261195e687SMark J Musante 	nvlist_free(spa->spa_config_splitting);
7271195e687SMark J Musante 
728fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
729fa9e4066Sahrens 	cv_broadcast(&spa_namespace_cv);
730fa9e4066Sahrens 
7310373e76bSbonwick 	if (spa->spa_root) {
732fa9e4066Sahrens 		spa_strfree(spa->spa_root);
7330373e76bSbonwick 		spa_active_count--;
7340373e76bSbonwick 	}
735fa9e4066Sahrens 
736c5904d13Seschrock 	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
737c5904d13Seschrock 		list_remove(&spa->spa_config_list, dp);
738c5904d13Seschrock 		if (dp->scd_path != NULL)
739c5904d13Seschrock 			spa_strfree(dp->scd_path);
740c5904d13Seschrock 		kmem_free(dp, sizeof (spa_config_dirent_t));
741c5904d13Seschrock 	}
742c5904d13Seschrock 
7430f7643c7SGeorge Wilson 	avl_destroy(&spa->spa_alloc_tree);
744c5904d13Seschrock 	list_destroy(&spa->spa_config_list);
7452f8aaab3Seschrock 
746ad135b5dSChristopher Siden 	nvlist_free(spa->spa_label_features);
7474b964adaSGeorge Wilson 	nvlist_free(spa->spa_load_info);
748fa9e4066Sahrens 	spa_config_set(spa, NULL);
749fa9e4066Sahrens 
750283b8460SGeorge.Wilson 	mutex_enter(&cpu_lock);
751283b8460SGeorge.Wilson 	if (spa->spa_deadman_cycid != CYCLIC_NONE)
752283b8460SGeorge.Wilson 		cyclic_remove(spa->spa_deadman_cycid);
753283b8460SGeorge.Wilson 	mutex_exit(&cpu_lock);
754283b8460SGeorge.Wilson 	spa->spa_deadman_cycid = CYCLIC_NONE;
755283b8460SGeorge.Wilson 
756fa9e4066Sahrens 	refcount_destroy(&spa->spa_refcount);
75791ebeef5Sahrens 
758e14bb325SJeff Bonwick 	spa_config_lock_destroy(spa);
759fa9e4066Sahrens 
760c3a66015SMatthew Ahrens 	kstat_delete(spa->spa_iokstat);
761c3a66015SMatthew Ahrens 	spa->spa_iokstat = NULL;
762c3a66015SMatthew Ahrens 
763b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
764cde58dbcSMatthew Ahrens 		bplist_destroy(&spa->spa_free_bplist[t]);
765b24ab676SJeff Bonwick 
76645818ee1SMatthew Ahrens 	zio_checksum_templates_free(spa);
76745818ee1SMatthew Ahrens 
768c25056deSgw 	cv_destroy(&spa->spa_async_cv);
769bc9014e6SJustin Gibbs 	cv_destroy(&spa->spa_evicting_os_cv);
77035a5a358SJonathan Adams 	cv_destroy(&spa->spa_proc_cv);
771c25056deSgw 	cv_destroy(&spa->spa_scrub_io_cv);
772e14bb325SJeff Bonwick 	cv_destroy(&spa->spa_suspend_cv);
773c25056deSgw 
7740f7643c7SGeorge Wilson 	mutex_destroy(&spa->spa_alloc_lock);
7755ad82045Snd 	mutex_destroy(&spa->spa_async_lock);
776c25056deSgw 	mutex_destroy(&spa->spa_errlist_lock);
77735a5a358SJonathan Adams 	mutex_destroy(&spa->spa_errlog_lock);
778bc9014e6SJustin Gibbs 	mutex_destroy(&spa->spa_evicting_os_lock);
77906eeb2adSek 	mutex_destroy(&spa->spa_history_lock);
78035a5a358SJonathan Adams 	mutex_destroy(&spa->spa_proc_lock);
781b1b8ab34Slling 	mutex_destroy(&spa->spa_props_lock);
78245818ee1SMatthew Ahrens 	mutex_destroy(&spa->spa_cksum_tmpls_lock);
78335a5a358SJonathan Adams 	mutex_destroy(&spa->spa_scrub_lock);
784e14bb325SJeff Bonwick 	mutex_destroy(&spa->spa_suspend_lock);
785a1521560SJeff Bonwick 	mutex_destroy(&spa->spa_vdev_top_lock);
786c3a66015SMatthew Ahrens 	mutex_destroy(&spa->spa_iokstat_lock);
7875ad82045Snd 
788fa9e4066Sahrens 	kmem_free(spa, sizeof (spa_t));
789fa9e4066Sahrens }
790fa9e4066Sahrens 
791fa9e4066Sahrens /*
792fa9e4066Sahrens  * Given a pool, return the next pool in the namespace, or NULL if there is
793fa9e4066Sahrens  * none.  If 'prev' is NULL, return the first pool.
794fa9e4066Sahrens  */
795fa9e4066Sahrens spa_t *
796fa9e4066Sahrens spa_next(spa_t *prev)
797fa9e4066Sahrens {
798fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
799fa9e4066Sahrens 
800fa9e4066Sahrens 	if (prev)
801fa9e4066Sahrens 		return (AVL_NEXT(&spa_namespace_avl, prev));
802fa9e4066Sahrens 	else
803fa9e4066Sahrens 		return (avl_first(&spa_namespace_avl));
804fa9e4066Sahrens }
805fa9e4066Sahrens 
806fa9e4066Sahrens /*
807fa9e4066Sahrens  * ==========================================================================
808fa9e4066Sahrens  * SPA refcount functions
809fa9e4066Sahrens  * ==========================================================================
810fa9e4066Sahrens  */
811fa9e4066Sahrens 
812fa9e4066Sahrens /*
813fa9e4066Sahrens  * Add a reference to the given spa_t.  Must have at least one reference, or
814fa9e4066Sahrens  * have the namespace lock held.
815fa9e4066Sahrens  */
816fa9e4066Sahrens void
817fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
818fa9e4066Sahrens {
819088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
820fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
821fa9e4066Sahrens 	(void) refcount_add(&spa->spa_refcount, tag);
822fa9e4066Sahrens }
823fa9e4066Sahrens 
824fa9e4066Sahrens /*
825fa9e4066Sahrens  * Remove a reference to the given spa_t.  Must have at least one reference, or
826fa9e4066Sahrens  * have the namespace lock held.
827fa9e4066Sahrens  */
828fa9e4066Sahrens void
829fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
830fa9e4066Sahrens {
831088f3894Sahrens 	ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
832fa9e4066Sahrens 	    MUTEX_HELD(&spa_namespace_lock));
833fa9e4066Sahrens 	(void) refcount_remove(&spa->spa_refcount, tag);
834fa9e4066Sahrens }
835fa9e4066Sahrens 
836bc9014e6SJustin Gibbs /*
837bc9014e6SJustin Gibbs  * Remove a reference to the given spa_t held by a dsl dir that is
838bc9014e6SJustin Gibbs  * being asynchronously released.  Async releases occur from a taskq
839bc9014e6SJustin Gibbs  * performing eviction of dsl datasets and dirs.  The namespace lock
840bc9014e6SJustin Gibbs  * isn't held and the hold by the object being evicted may contribute to
841bc9014e6SJustin Gibbs  * spa_minref (e.g. dataset or directory released during pool export),
842bc9014e6SJustin Gibbs  * so the asserts in spa_close() do not apply.
843bc9014e6SJustin Gibbs  */
844bc9014e6SJustin Gibbs void
845bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag)
846bc9014e6SJustin Gibbs {
847bc9014e6SJustin Gibbs 	(void) refcount_remove(&spa->spa_refcount, tag);
848bc9014e6SJustin Gibbs }
849bc9014e6SJustin Gibbs 
850fa9e4066Sahrens /*
851fa9e4066Sahrens  * Check to see if the spa refcount is zero.  Must be called with
852088f3894Sahrens  * spa_namespace_lock held.  We really compare against spa_minref, which is the
853fa9e4066Sahrens  * number of references acquired when opening a pool
854fa9e4066Sahrens  */
855fa9e4066Sahrens boolean_t
856fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
857fa9e4066Sahrens {
858fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
859fa9e4066Sahrens 
860088f3894Sahrens 	return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
861fa9e4066Sahrens }
862fa9e4066Sahrens 
86399653d4eSeschrock /*
86499653d4eSeschrock  * ==========================================================================
865fa94a07fSbrendan  * SPA spare and l2cache tracking
86699653d4eSeschrock  * ==========================================================================
86799653d4eSeschrock  */
86899653d4eSeschrock 
869fa94a07fSbrendan /*
870fa94a07fSbrendan  * Hot spares and cache devices are tracked using the same code below,
871fa94a07fSbrendan  * for 'auxiliary' devices.
872fa94a07fSbrendan  */
873fa94a07fSbrendan 
874fa94a07fSbrendan typedef struct spa_aux {
875fa94a07fSbrendan 	uint64_t	aux_guid;
876fa94a07fSbrendan 	uint64_t	aux_pool;
877fa94a07fSbrendan 	avl_node_t	aux_avl;
878fa94a07fSbrendan 	int		aux_count;
879fa94a07fSbrendan } spa_aux_t;
880fa94a07fSbrendan 
881fa94a07fSbrendan static int
882fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
883fa94a07fSbrendan {
884fa94a07fSbrendan 	const spa_aux_t *sa = a;
885fa94a07fSbrendan 	const spa_aux_t *sb = b;
886fa94a07fSbrendan 
887fa94a07fSbrendan 	if (sa->aux_guid < sb->aux_guid)
888fa94a07fSbrendan 		return (-1);
889fa94a07fSbrendan 	else if (sa->aux_guid > sb->aux_guid)
890fa94a07fSbrendan 		return (1);
891fa94a07fSbrendan 	else
892fa94a07fSbrendan 		return (0);
893fa94a07fSbrendan }
894fa94a07fSbrendan 
895fa94a07fSbrendan void
896fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
897fa94a07fSbrendan {
898fa94a07fSbrendan 	avl_index_t where;
899fa94a07fSbrendan 	spa_aux_t search;
900fa94a07fSbrendan 	spa_aux_t *aux;
901fa94a07fSbrendan 
902fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
903fa94a07fSbrendan 	if ((aux = avl_find(avl, &search, &where)) != NULL) {
904fa94a07fSbrendan 		aux->aux_count++;
905fa94a07fSbrendan 	} else {
906fa94a07fSbrendan 		aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
907fa94a07fSbrendan 		aux->aux_guid = vd->vdev_guid;
908fa94a07fSbrendan 		aux->aux_count = 1;
909fa94a07fSbrendan 		avl_insert(avl, aux, where);
910fa94a07fSbrendan 	}
911fa94a07fSbrendan }
912fa94a07fSbrendan 
913fa94a07fSbrendan void
914fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
915fa94a07fSbrendan {
916fa94a07fSbrendan 	spa_aux_t search;
917fa94a07fSbrendan 	spa_aux_t *aux;
918fa94a07fSbrendan 	avl_index_t where;
919fa94a07fSbrendan 
920fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
921fa94a07fSbrendan 	aux = avl_find(avl, &search, &where);
922fa94a07fSbrendan 
923fa94a07fSbrendan 	ASSERT(aux != NULL);
924fa94a07fSbrendan 
925fa94a07fSbrendan 	if (--aux->aux_count == 0) {
926fa94a07fSbrendan 		avl_remove(avl, aux);
927fa94a07fSbrendan 		kmem_free(aux, sizeof (spa_aux_t));
928fa94a07fSbrendan 	} else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
929fa94a07fSbrendan 		aux->aux_pool = 0ULL;
930fa94a07fSbrendan 	}
931fa94a07fSbrendan }
932fa94a07fSbrendan 
933fa94a07fSbrendan boolean_t
93489a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
935fa94a07fSbrendan {
936fa94a07fSbrendan 	spa_aux_t search, *found;
937fa94a07fSbrendan 
938fa94a07fSbrendan 	search.aux_guid = guid;
93989a89ebfSlling 	found = avl_find(avl, &search, NULL);
940fa94a07fSbrendan 
941fa94a07fSbrendan 	if (pool) {
942fa94a07fSbrendan 		if (found)
943fa94a07fSbrendan 			*pool = found->aux_pool;
944fa94a07fSbrendan 		else
945fa94a07fSbrendan 			*pool = 0ULL;
946fa94a07fSbrendan 	}
947fa94a07fSbrendan 
94889a89ebfSlling 	if (refcnt) {
94989a89ebfSlling 		if (found)
95089a89ebfSlling 			*refcnt = found->aux_count;
95189a89ebfSlling 		else
95289a89ebfSlling 			*refcnt = 0;
95389a89ebfSlling 	}
95489a89ebfSlling 
955fa94a07fSbrendan 	return (found != NULL);
956fa94a07fSbrendan }
957fa94a07fSbrendan 
958fa94a07fSbrendan void
959fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
960fa94a07fSbrendan {
961fa94a07fSbrendan 	spa_aux_t search, *found;
962fa94a07fSbrendan 	avl_index_t where;
963fa94a07fSbrendan 
964fa94a07fSbrendan 	search.aux_guid = vd->vdev_guid;
965fa94a07fSbrendan 	found = avl_find(avl, &search, &where);
966fa94a07fSbrendan 	ASSERT(found != NULL);
967fa94a07fSbrendan 	ASSERT(found->aux_pool == 0ULL);
968fa94a07fSbrendan 
969fa94a07fSbrendan 	found->aux_pool = spa_guid(vd->vdev_spa);
970fa94a07fSbrendan }
971fa94a07fSbrendan 
97299653d4eSeschrock /*
97339c23413Seschrock  * Spares are tracked globally due to the following constraints:
97439c23413Seschrock  *
97539c23413Seschrock  * 	- A spare may be part of multiple pools.
97639c23413Seschrock  * 	- A spare may be added to a pool even if it's actively in use within
97739c23413Seschrock  *	  another pool.
97839c23413Seschrock  * 	- A spare in use in any pool can only be the source of a replacement if
97939c23413Seschrock  *	  the target is a spare in the same pool.
98039c23413Seschrock  *
98139c23413Seschrock  * We keep track of all spares on the system through the use of a reference
98239c23413Seschrock  * counted AVL tree.  When a vdev is added as a spare, or used as a replacement
98339c23413Seschrock  * spare, then we bump the reference count in the AVL tree.  In addition, we set
98439c23413Seschrock  * the 'vdev_isspare' member to indicate that the device is a spare (active or
98539c23413Seschrock  * inactive).  When a spare is made active (used to replace a device in the
98639c23413Seschrock  * pool), we also keep track of which pool its been made a part of.
98739c23413Seschrock  *
98839c23413Seschrock  * The 'spa_spare_lock' protects the AVL tree.  These functions are normally
98939c23413Seschrock  * called under the spa_namespace lock as part of vdev reconfiguration.  The
99039c23413Seschrock  * separate spare lock exists for the status query path, which does not need to
99139c23413Seschrock  * be completely consistent with respect to other vdev configuration changes.
99299653d4eSeschrock  */
99339c23413Seschrock 
99499653d4eSeschrock static int
99599653d4eSeschrock spa_spare_compare(const void *a, const void *b)
99699653d4eSeschrock {
997fa94a07fSbrendan 	return (spa_aux_compare(a, b));
99899653d4eSeschrock }
99999653d4eSeschrock 
100099653d4eSeschrock void
100139c23413Seschrock spa_spare_add(vdev_t *vd)
100299653d4eSeschrock {
100399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
100439c23413Seschrock 	ASSERT(!vd->vdev_isspare);
1005fa94a07fSbrendan 	spa_aux_add(vd, &spa_spare_avl);
100639c23413Seschrock 	vd->vdev_isspare = B_TRUE;
100799653d4eSeschrock 	mutex_exit(&spa_spare_lock);
100899653d4eSeschrock }
100999653d4eSeschrock 
101099653d4eSeschrock void
101139c23413Seschrock spa_spare_remove(vdev_t *vd)
101299653d4eSeschrock {
101399653d4eSeschrock 	mutex_enter(&spa_spare_lock);
101439c23413Seschrock 	ASSERT(vd->vdev_isspare);
1015fa94a07fSbrendan 	spa_aux_remove(vd, &spa_spare_avl);
101639c23413Seschrock 	vd->vdev_isspare = B_FALSE;
101799653d4eSeschrock 	mutex_exit(&spa_spare_lock);
101899653d4eSeschrock }
101999653d4eSeschrock 
102099653d4eSeschrock boolean_t
102189a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
102299653d4eSeschrock {
1023fa94a07fSbrendan 	boolean_t found;
102499653d4eSeschrock 
102599653d4eSeschrock 	mutex_enter(&spa_spare_lock);
102689a89ebfSlling 	found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
102799653d4eSeschrock 	mutex_exit(&spa_spare_lock);
102899653d4eSeschrock 
1029fa94a07fSbrendan 	return (found);
103039c23413Seschrock }
103139c23413Seschrock 
103239c23413Seschrock void
103339c23413Seschrock spa_spare_activate(vdev_t *vd)
103439c23413Seschrock {
103539c23413Seschrock 	mutex_enter(&spa_spare_lock);
103639c23413Seschrock 	ASSERT(vd->vdev_isspare);
1037fa94a07fSbrendan 	spa_aux_activate(vd, &spa_spare_avl);
1038fa94a07fSbrendan 	mutex_exit(&spa_spare_lock);
1039fa94a07fSbrendan }
104039c23413Seschrock 
1041fa94a07fSbrendan /*
1042fa94a07fSbrendan  * Level 2 ARC devices are tracked globally for the same reasons as spares.
1043fa94a07fSbrendan  * Cache devices currently only support one pool per cache device, and so
1044fa94a07fSbrendan  * for these devices the aux reference count is currently unused beyond 1.
1045fa94a07fSbrendan  */
104639c23413Seschrock 
1047fa94a07fSbrendan static int
1048fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
1049fa94a07fSbrendan {
1050fa94a07fSbrendan 	return (spa_aux_compare(a, b));
1051fa94a07fSbrendan }
1052fa94a07fSbrendan 
1053fa94a07fSbrendan void
1054fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
1055fa94a07fSbrendan {
1056fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1057fa94a07fSbrendan 	ASSERT(!vd->vdev_isl2cache);
1058fa94a07fSbrendan 	spa_aux_add(vd, &spa_l2cache_avl);
1059fa94a07fSbrendan 	vd->vdev_isl2cache = B_TRUE;
1060fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1061fa94a07fSbrendan }
1062fa94a07fSbrendan 
1063fa94a07fSbrendan void
1064fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
1065fa94a07fSbrendan {
1066fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1067fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1068fa94a07fSbrendan 	spa_aux_remove(vd, &spa_l2cache_avl);
1069fa94a07fSbrendan 	vd->vdev_isl2cache = B_FALSE;
1070fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1071fa94a07fSbrendan }
1072fa94a07fSbrendan 
1073fa94a07fSbrendan boolean_t
1074fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1075fa94a07fSbrendan {
1076fa94a07fSbrendan 	boolean_t found;
1077fa94a07fSbrendan 
1078fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
107989a89ebfSlling 	found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1080fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1081fa94a07fSbrendan 
1082fa94a07fSbrendan 	return (found);
1083fa94a07fSbrendan }
1084fa94a07fSbrendan 
1085fa94a07fSbrendan void
1086fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
1087fa94a07fSbrendan {
1088fa94a07fSbrendan 	mutex_enter(&spa_l2cache_lock);
1089fa94a07fSbrendan 	ASSERT(vd->vdev_isl2cache);
1090fa94a07fSbrendan 	spa_aux_activate(vd, &spa_l2cache_avl);
1091fa94a07fSbrendan 	mutex_exit(&spa_l2cache_lock);
1092fa94a07fSbrendan }
1093fa94a07fSbrendan 
1094fa9e4066Sahrens /*
1095fa9e4066Sahrens  * ==========================================================================
1096fa9e4066Sahrens  * SPA vdev locking
1097fa9e4066Sahrens  * ==========================================================================
1098fa9e4066Sahrens  */
1099fa9e4066Sahrens 
1100fa9e4066Sahrens /*
1101ea8dc4b6Seschrock  * Lock the given spa_t for the purpose of adding or removing a vdev.
1102ea8dc4b6Seschrock  * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1103fa9e4066Sahrens  * It returns the next transaction group for the spa_t.
1104fa9e4066Sahrens  */
1105fa9e4066Sahrens uint64_t
1106fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
1107fa9e4066Sahrens {
1108a1521560SJeff Bonwick 	mutex_enter(&spa->spa_vdev_top_lock);
1109bbfd46c4SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
111088ecc943SGeorge Wilson 	return (spa_vdev_config_enter(spa));
111188ecc943SGeorge Wilson }
111288ecc943SGeorge Wilson 
111388ecc943SGeorge Wilson /*
111488ecc943SGeorge Wilson  * Internal implementation for spa_vdev_enter().  Used when a vdev
111588ecc943SGeorge Wilson  * operation requires multiple syncs (i.e. removing a device) while
111688ecc943SGeorge Wilson  * keeping the spa_namespace_lock held.
111788ecc943SGeorge Wilson  */
111888ecc943SGeorge Wilson uint64_t
111988ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
112088ecc943SGeorge Wilson {
112188ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
11223d7072f8Seschrock 
1123e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1124fa9e4066Sahrens 
1125fa9e4066Sahrens 	return (spa_last_synced_txg(spa) + 1);
1126fa9e4066Sahrens }
1127fa9e4066Sahrens 
1128fa9e4066Sahrens /*
112988ecc943SGeorge Wilson  * Used in combination with spa_vdev_config_enter() to allow the syncing
113088ecc943SGeorge Wilson  * of multiple transactions without releasing the spa_namespace_lock.
1131fa9e4066Sahrens  */
113288ecc943SGeorge Wilson void
113388ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1134fa9e4066Sahrens {
113588ecc943SGeorge Wilson 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
113688ecc943SGeorge Wilson 
11370e34b6a7Sbonwick 	int config_changed = B_FALSE;
1138ea8dc4b6Seschrock 
11390373e76bSbonwick 	ASSERT(txg > spa_last_synced_txg(spa));
11400e34b6a7Sbonwick 
1141e14bb325SJeff Bonwick 	spa->spa_pending_vdev = NULL;
1142e14bb325SJeff Bonwick 
11430e34b6a7Sbonwick 	/*
11440e34b6a7Sbonwick 	 * Reassess the DTLs.
11450e34b6a7Sbonwick 	 */
11460373e76bSbonwick 	vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
11470e34b6a7Sbonwick 
1148e14bb325SJeff Bonwick 	if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
11490e34b6a7Sbonwick 		config_changed = B_TRUE;
11508f18d1faSGeorge Wilson 		spa->spa_config_generation++;
11510e34b6a7Sbonwick 	}
1152ea8dc4b6Seschrock 
115388ecc943SGeorge Wilson 	/*
115488ecc943SGeorge Wilson 	 * Verify the metaslab classes.
115588ecc943SGeorge Wilson 	 */
1156b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1157b24ab676SJeff Bonwick 	ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
115888ecc943SGeorge Wilson 
1159e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, spa);
1160fa9e4066Sahrens 
116188ecc943SGeorge Wilson 	/*
116288ecc943SGeorge Wilson 	 * Panic the system if the specified tag requires it.  This
116388ecc943SGeorge Wilson 	 * is useful for ensuring that configurations are updated
116488ecc943SGeorge Wilson 	 * transactionally.
116588ecc943SGeorge Wilson 	 */
116688ecc943SGeorge Wilson 	if (zio_injection_enabled)
11671195e687SMark J Musante 		zio_handle_panic_injection(spa, tag, 0);
116888ecc943SGeorge Wilson 
1169fa9e4066Sahrens 	/*
1170fa9e4066Sahrens 	 * Note: this txg_wait_synced() is important because it ensures
1171fa9e4066Sahrens 	 * that there won't be more than one config change per txg.
1172fa9e4066Sahrens 	 * This allows us to use the txg as the generation number.
1173fa9e4066Sahrens 	 */
1174fa9e4066Sahrens 	if (error == 0)
1175fa9e4066Sahrens 		txg_wait_synced(spa->spa_dsl_pool, txg);
1176fa9e4066Sahrens 
1177fa9e4066Sahrens 	if (vd != NULL) {
11780713e232SGeorge Wilson 		ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
11798ad4d6ddSJeff Bonwick 		spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1180fa9e4066Sahrens 		vdev_free(vd);
11818ad4d6ddSJeff Bonwick 		spa_config_exit(spa, SCL_ALL, spa);
1182fa9e4066Sahrens 	}
1183fa9e4066Sahrens 
1184fa9e4066Sahrens 	/*
11850e34b6a7Sbonwick 	 * If the config changed, update the config cache.
1186fa9e4066Sahrens 	 */
11870e34b6a7Sbonwick 	if (config_changed)
11885cabbc6bSPrashanth Sreenivasa 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
118988ecc943SGeorge Wilson }
1190ea8dc4b6Seschrock 
119188ecc943SGeorge Wilson /*
119288ecc943SGeorge Wilson  * Unlock the spa_t after adding or removing a vdev.  Besides undoing the
119388ecc943SGeorge Wilson  * locking of spa_vdev_enter(), we also want make sure the transactions have
119488ecc943SGeorge Wilson  * synced to disk, and then update the global configuration cache with the new
119588ecc943SGeorge Wilson  * information.
119688ecc943SGeorge Wilson  */
119788ecc943SGeorge Wilson int
119888ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
119988ecc943SGeorge Wilson {
120088ecc943SGeorge Wilson 	spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1201ea8dc4b6Seschrock 	mutex_exit(&spa_namespace_lock);
1202bbfd46c4SJeff Bonwick 	mutex_exit(&spa->spa_vdev_top_lock);
1203fa9e4066Sahrens 
1204fa9e4066Sahrens 	return (error);
1205fa9e4066Sahrens }
1206fa9e4066Sahrens 
1207e14bb325SJeff Bonwick /*
1208e14bb325SJeff Bonwick  * Lock the given spa_t for the purpose of changing vdev state.
1209e14bb325SJeff Bonwick  */
1210e14bb325SJeff Bonwick void
12118f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1212e14bb325SJeff Bonwick {
12138f18d1faSGeorge Wilson 	int locks = SCL_STATE_ALL | oplocks;
12148f18d1faSGeorge Wilson 
1215dcba9f3fSGeorge Wilson 	/*
1216dcba9f3fSGeorge Wilson 	 * Root pools may need to read of the underlying devfs filesystem
1217dcba9f3fSGeorge Wilson 	 * when opening up a vdev.  Unfortunately if we're holding the
1218dcba9f3fSGeorge Wilson 	 * SCL_ZIO lock it will result in a deadlock when we try to issue
1219dcba9f3fSGeorge Wilson 	 * the read from the root filesystem.  Instead we "prefetch"
1220dcba9f3fSGeorge Wilson 	 * the associated vnodes that we need prior to opening the
1221dcba9f3fSGeorge Wilson 	 * underlying devices and cache them so that we can prevent
1222dcba9f3fSGeorge Wilson 	 * any I/O when we are doing the actual open.
1223dcba9f3fSGeorge Wilson 	 */
1224dcba9f3fSGeorge Wilson 	if (spa_is_root(spa)) {
12259842588bSGeorge Wilson 		int low = locks & ~(SCL_ZIO - 1);
12269842588bSGeorge Wilson 		int high = locks & ~low;
12279842588bSGeorge Wilson 
12289842588bSGeorge Wilson 		spa_config_enter(spa, high, spa, RW_WRITER);
1229dcba9f3fSGeorge Wilson 		vdev_hold(spa->spa_root_vdev);
12309842588bSGeorge Wilson 		spa_config_enter(spa, low, spa, RW_WRITER);
1231dcba9f3fSGeorge Wilson 	} else {
1232dcba9f3fSGeorge Wilson 		spa_config_enter(spa, locks, spa, RW_WRITER);
1233dcba9f3fSGeorge Wilson 	}
12348f18d1faSGeorge Wilson 	spa->spa_vdev_locks = locks;
1235e14bb325SJeff Bonwick }
1236e14bb325SJeff Bonwick 
1237e14bb325SJeff Bonwick int
1238e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1239e14bb325SJeff Bonwick {
1240c6065d0fSGeorge Wilson 	boolean_t config_changed = B_FALSE;
1241c6065d0fSGeorge Wilson 
1242b24ab676SJeff Bonwick 	if (vd != NULL || error == 0)
1243b24ab676SJeff Bonwick 		vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1244b24ab676SJeff Bonwick 		    0, 0, B_FALSE);
1245b24ab676SJeff Bonwick 
12468f18d1faSGeorge Wilson 	if (vd != NULL) {
1247e14bb325SJeff Bonwick 		vdev_state_dirty(vd->vdev_top);
1248c6065d0fSGeorge Wilson 		config_changed = B_TRUE;
12498f18d1faSGeorge Wilson 		spa->spa_config_generation++;
12508f18d1faSGeorge Wilson 	}
1251e14bb325SJeff Bonwick 
1252dcba9f3fSGeorge Wilson 	if (spa_is_root(spa))
1253dcba9f3fSGeorge Wilson 		vdev_rele(spa->spa_root_vdev);
1254dcba9f3fSGeorge Wilson 
12558f18d1faSGeorge Wilson 	ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
12568f18d1faSGeorge Wilson 	spa_config_exit(spa, spa->spa_vdev_locks, spa);
1257e14bb325SJeff Bonwick 
12588ad4d6ddSJeff Bonwick 	/*
12598ad4d6ddSJeff Bonwick 	 * If anything changed, wait for it to sync.  This ensures that,
12608ad4d6ddSJeff Bonwick 	 * from the system administrator's perspective, zpool(1M) commands
12618ad4d6ddSJeff Bonwick 	 * are synchronous.  This is important for things like zpool offline:
12628ad4d6ddSJeff Bonwick 	 * when the command completes, you expect no further I/O from ZFS.
12638ad4d6ddSJeff Bonwick 	 */
12648ad4d6ddSJeff Bonwick 	if (vd != NULL)
12658ad4d6ddSJeff Bonwick 		txg_wait_synced(spa->spa_dsl_pool, 0);
12668ad4d6ddSJeff Bonwick 
1267c6065d0fSGeorge Wilson 	/*
1268c6065d0fSGeorge Wilson 	 * If the config changed, update the config cache.
1269c6065d0fSGeorge Wilson 	 */
1270c6065d0fSGeorge Wilson 	if (config_changed) {
1271c6065d0fSGeorge Wilson 		mutex_enter(&spa_namespace_lock);
12725cabbc6bSPrashanth Sreenivasa 		spa_write_cachefile(spa, B_FALSE, B_TRUE);
1273c6065d0fSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1274c6065d0fSGeorge Wilson 	}
1275c6065d0fSGeorge Wilson 
1276e14bb325SJeff Bonwick 	return (error);
1277e14bb325SJeff Bonwick }
1278e14bb325SJeff Bonwick 
1279fa9e4066Sahrens /*
1280fa9e4066Sahrens  * ==========================================================================
1281fa9e4066Sahrens  * Miscellaneous functions
1282fa9e4066Sahrens  * ==========================================================================
1283fa9e4066Sahrens  */
1284fa9e4066Sahrens 
1285ad135b5dSChristopher Siden void
128643466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1287ad135b5dSChristopher Siden {
12882acef22dSMatthew Ahrens 	if (!nvlist_exists(spa->spa_label_features, feature)) {
12892acef22dSMatthew Ahrens 		fnvlist_add_boolean(spa->spa_label_features, feature);
129043466aaeSMax Grossman 		/*
129143466aaeSMax Grossman 		 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
129243466aaeSMax Grossman 		 * dirty the vdev config because lock SCL_CONFIG is not held.
129343466aaeSMax Grossman 		 * Thankfully, in this case we don't need to dirty the config
129443466aaeSMax Grossman 		 * because it will be written out anyway when we finish
129543466aaeSMax Grossman 		 * creating the pool.
129643466aaeSMax Grossman 		 */
129743466aaeSMax Grossman 		if (tx->tx_txg != TXG_INITIAL)
129843466aaeSMax Grossman 			vdev_config_dirty(spa->spa_root_vdev);
12992acef22dSMatthew Ahrens 	}
1300ad135b5dSChristopher Siden }
1301ad135b5dSChristopher Siden 
1302ad135b5dSChristopher Siden void
1303ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1304ad135b5dSChristopher Siden {
13052acef22dSMatthew Ahrens 	if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
13062acef22dSMatthew Ahrens 		vdev_config_dirty(spa->spa_root_vdev);
1307ad135b5dSChristopher Siden }
1308ad135b5dSChristopher Siden 
1309fa9e4066Sahrens /*
1310fa9e4066Sahrens  * Rename a spa_t.
1311fa9e4066Sahrens  */
1312fa9e4066Sahrens int
1313fa9e4066Sahrens spa_rename(const char *name, const char *newname)
1314fa9e4066Sahrens {
1315fa9e4066Sahrens 	spa_t *spa;
1316fa9e4066Sahrens 	int err;
1317fa9e4066Sahrens 
1318fa9e4066Sahrens 	/*
1319fa9e4066Sahrens 	 * Lookup the spa_t and grab the config lock for writing.  We need to
1320fa9e4066Sahrens 	 * actually open the pool so that we can sync out the necessary labels.
1321fa9e4066Sahrens 	 * It's OK to call spa_open() with the namespace lock held because we
1322ea8dc4b6Seschrock 	 * allow recursive calls for other reasons.
1323fa9e4066Sahrens 	 */
1324fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
1325fa9e4066Sahrens 	if ((err = spa_open(name, &spa, FTAG)) != 0) {
1326fa9e4066Sahrens 		mutex_exit(&spa_namespace_lock);
1327fa9e4066Sahrens 		return (err);
1328fa9e4066Sahrens 	}
1329fa9e4066Sahrens 
1330e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	avl_remove(&spa_namespace_avl, spa);
1333e14bb325SJeff Bonwick 	(void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1334fa9e4066Sahrens 	avl_add(&spa_namespace_avl, spa);
1335fa9e4066Sahrens 
1336fa9e4066Sahrens 	/*
1337fa9e4066Sahrens 	 * Sync all labels to disk with the new names by marking the root vdev
1338fa9e4066Sahrens 	 * dirty and waiting for it to sync.  It will pick up the new pool name
1339fa9e4066Sahrens 	 * during the sync.
1340fa9e4066Sahrens 	 */
1341fa9e4066Sahrens 	vdev_config_dirty(spa->spa_root_vdev);
1342fa9e4066Sahrens 
1343e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1344fa9e4066Sahrens 
13450373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, 0);
1346fa9e4066Sahrens 
1347fa9e4066Sahrens 	/*
1348fa9e4066Sahrens 	 * Sync the updated config cache.
1349fa9e4066Sahrens 	 */
13505cabbc6bSPrashanth Sreenivasa 	spa_write_cachefile(spa, B_FALSE, B_TRUE);
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	spa_close(spa, FTAG);
1353fa9e4066Sahrens 
1354fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
1355fa9e4066Sahrens 
1356fa9e4066Sahrens 	return (0);
1357fa9e4066Sahrens }
1358fa9e4066Sahrens 
1359fa9e4066Sahrens /*
1360f9af39baSGeorge Wilson  * Return the spa_t associated with given pool_guid, if it exists.  If
1361f9af39baSGeorge Wilson  * device_guid is non-zero, determine whether the pool exists *and* contains
1362f9af39baSGeorge Wilson  * a device with the specified device_guid.
1363fa9e4066Sahrens  */
1364f9af39baSGeorge Wilson spa_t *
1365f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1366fa9e4066Sahrens {
1367fa9e4066Sahrens 	spa_t *spa;
1368fa9e4066Sahrens 	avl_tree_t *t = &spa_namespace_avl;
1369fa9e4066Sahrens 
1370ea8dc4b6Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
1371fa9e4066Sahrens 
1372fa9e4066Sahrens 	for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1373fa9e4066Sahrens 		if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1374fa9e4066Sahrens 			continue;
1375fa9e4066Sahrens 		if (spa->spa_root_vdev == NULL)
1376fa9e4066Sahrens 			continue;
137739c23413Seschrock 		if (spa_guid(spa) == pool_guid) {
137839c23413Seschrock 			if (device_guid == 0)
137939c23413Seschrock 				break;
138039c23413Seschrock 
138139c23413Seschrock 			if (vdev_lookup_by_guid(spa->spa_root_vdev,
138239c23413Seschrock 			    device_guid) != NULL)
138339c23413Seschrock 				break;
138439c23413Seschrock 
138539c23413Seschrock 			/*
13868654d025Sperrin 			 * Check any devices we may be in the process of adding.
138739c23413Seschrock 			 */
138839c23413Seschrock 			if (spa->spa_pending_vdev) {
138939c23413Seschrock 				if (vdev_lookup_by_guid(spa->spa_pending_vdev,
139039c23413Seschrock 				    device_guid) != NULL)
139139c23413Seschrock 					break;
139239c23413Seschrock 			}
139339c23413Seschrock 		}
1394fa9e4066Sahrens 	}
1395fa9e4066Sahrens 
1396f9af39baSGeorge Wilson 	return (spa);
1397f9af39baSGeorge Wilson }
1398f9af39baSGeorge Wilson 
1399f9af39baSGeorge Wilson /*
1400f9af39baSGeorge Wilson  * Determine whether a pool with the given pool_guid exists.
1401f9af39baSGeorge Wilson  */
1402f9af39baSGeorge Wilson boolean_t
1403f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1404f9af39baSGeorge Wilson {
1405f9af39baSGeorge Wilson 	return (spa_by_guid(pool_guid, device_guid) != NULL);
1406fa9e4066Sahrens }
1407fa9e4066Sahrens 
1408fa9e4066Sahrens char *
1409fa9e4066Sahrens spa_strdup(const char *s)
1410fa9e4066Sahrens {
1411fa9e4066Sahrens 	size_t len;
1412fa9e4066Sahrens 	char *new;
1413fa9e4066Sahrens 
1414fa9e4066Sahrens 	len = strlen(s);
1415fa9e4066Sahrens 	new = kmem_alloc(len + 1, KM_SLEEP);
1416fa9e4066Sahrens 	bcopy(s, new, len);
1417fa9e4066Sahrens 	new[len] = '\0';
1418fa9e4066Sahrens 
1419fa9e4066Sahrens 	return (new);
1420fa9e4066Sahrens }
1421fa9e4066Sahrens 
1422fa9e4066Sahrens void
1423fa9e4066Sahrens spa_strfree(char *s)
1424fa9e4066Sahrens {
1425fa9e4066Sahrens 	kmem_free(s, strlen(s) + 1);
1426fa9e4066Sahrens }
1427fa9e4066Sahrens 
1428fa9e4066Sahrens uint64_t
1429fa9e4066Sahrens spa_get_random(uint64_t range)
1430fa9e4066Sahrens {
1431fa9e4066Sahrens 	uint64_t r;
1432fa9e4066Sahrens 
1433fa9e4066Sahrens 	ASSERT(range != 0);
1434fa9e4066Sahrens 
1435fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1436fa9e4066Sahrens 
1437fa9e4066Sahrens 	return (r % range);
1438fa9e4066Sahrens }
1439fa9e4066Sahrens 
14401195e687SMark J Musante uint64_t
14411195e687SMark J Musante spa_generate_guid(spa_t *spa)
14421195e687SMark J Musante {
14431195e687SMark J Musante 	uint64_t guid = spa_get_random(-1ULL);
14441195e687SMark J Musante 
14451195e687SMark J Musante 	if (spa != NULL) {
14461195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
14471195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14481195e687SMark J Musante 	} else {
14491195e687SMark J Musante 		while (guid == 0 || spa_guid_exists(guid, 0))
14501195e687SMark J Musante 			guid = spa_get_random(-1ULL);
14511195e687SMark J Musante 	}
14521195e687SMark J Musante 
14531195e687SMark J Musante 	return (guid);
14541195e687SMark J Musante }
14551195e687SMark J Musante 
1456fa9e4066Sahrens void
145743466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1458fa9e4066Sahrens {
1459ad135b5dSChristopher Siden 	char type[256];
1460f0ba89beSJeff Bonwick 	char *checksum = NULL;
1461f0ba89beSJeff Bonwick 	char *compress = NULL;
1462f0ba89beSJeff Bonwick 
1463f0ba89beSJeff Bonwick 	if (bp != NULL) {
1464ad135b5dSChristopher Siden 		if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1465ad135b5dSChristopher Siden 			dmu_object_byteswap_t bswap =
1466ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1467ad135b5dSChristopher Siden 			(void) snprintf(type, sizeof (type), "bswap %s %s",
1468ad135b5dSChristopher Siden 			    DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1469ad135b5dSChristopher Siden 			    "metadata" : "data",
1470ad135b5dSChristopher Siden 			    dmu_ot_byteswap[bswap].ob_name);
1471ad135b5dSChristopher Siden 		} else {
1472ad135b5dSChristopher Siden 			(void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1473ad135b5dSChristopher Siden 			    sizeof (type));
1474ad135b5dSChristopher Siden 		}
14755d7b4d43SMatthew Ahrens 		if (!BP_IS_EMBEDDED(bp)) {
14765d7b4d43SMatthew Ahrens 			checksum =
14775d7b4d43SMatthew Ahrens 			    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
14785d7b4d43SMatthew Ahrens 		}
1479f0ba89beSJeff Bonwick 		compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1480f0ba89beSJeff Bonwick 	}
1481fa9e4066Sahrens 
148243466aaeSMax Grossman 	SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
148343466aaeSMax Grossman 	    compress);
1484fa9e4066Sahrens }
1485fa9e4066Sahrens 
1486fa9e4066Sahrens void
1487fa9e4066Sahrens spa_freeze(spa_t *spa)
1488fa9e4066Sahrens {
1489fa9e4066Sahrens 	uint64_t freeze_txg = 0;
1490fa9e4066Sahrens 
1491e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1492fa9e4066Sahrens 	if (spa->spa_freeze_txg == UINT64_MAX) {
1493fa9e4066Sahrens 		freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1494fa9e4066Sahrens 		spa->spa_freeze_txg = freeze_txg;
1495fa9e4066Sahrens 	}
1496e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
1497fa9e4066Sahrens 	if (freeze_txg != 0)
1498fa9e4066Sahrens 		txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1499fa9e4066Sahrens }
1500fa9e4066Sahrens 
15010125049cSahrens void
15020125049cSahrens zfs_panic_recover(const char *fmt, ...)
15030125049cSahrens {
15040125049cSahrens 	va_list adx;
15050125049cSahrens 
15060125049cSahrens 	va_start(adx, fmt);
15070125049cSahrens 	vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
15080125049cSahrens 	va_end(adx);
15090125049cSahrens }
15100125049cSahrens 
15113f9d6ad7SLin Ling /*
15123f9d6ad7SLin Ling  * This is a stripped-down version of strtoull, suitable only for converting
1513f7170741SWill Andrews  * lowercase hexadecimal numbers that don't overflow.
15143f9d6ad7SLin Ling  */
15153f9d6ad7SLin Ling uint64_t
15164585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr)
15173f9d6ad7SLin Ling {
15183f9d6ad7SLin Ling 	uint64_t val = 0;
15193f9d6ad7SLin Ling 	char c;
15203f9d6ad7SLin Ling 	int digit;
15213f9d6ad7SLin Ling 
15223f9d6ad7SLin Ling 	while ((c = *str) != '\0') {
15233f9d6ad7SLin Ling 		if (c >= '0' && c <= '9')
15243f9d6ad7SLin Ling 			digit = c - '0';
15253f9d6ad7SLin Ling 		else if (c >= 'a' && c <= 'f')
15263f9d6ad7SLin Ling 			digit = 10 + c - 'a';
15273f9d6ad7SLin Ling 		else
15283f9d6ad7SLin Ling 			break;
15293f9d6ad7SLin Ling 
15303f9d6ad7SLin Ling 		val *= 16;
15313f9d6ad7SLin Ling 		val += digit;
15323f9d6ad7SLin Ling 
15333f9d6ad7SLin Ling 		str++;
15343f9d6ad7SLin Ling 	}
15353f9d6ad7SLin Ling 
15363f9d6ad7SLin Ling 	if (nptr)
15373f9d6ad7SLin Ling 		*nptr = (char *)str;
15383f9d6ad7SLin Ling 
15393f9d6ad7SLin Ling 	return (val);
15403f9d6ad7SLin Ling }
15413f9d6ad7SLin Ling 
1542fa9e4066Sahrens /*
1543fa9e4066Sahrens  * ==========================================================================
1544fa9e4066Sahrens  * Accessor functions
1545fa9e4066Sahrens  * ==========================================================================
1546fa9e4066Sahrens  */
1547fa9e4066Sahrens 
1548088f3894Sahrens boolean_t
154988b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1550fa9e4066Sahrens {
155188b7b0f2SMatthew Ahrens 	return (spa->spa_async_suspended);
1552fa9e4066Sahrens }
1553fa9e4066Sahrens 
1554fa9e4066Sahrens dsl_pool_t *
1555fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1556fa9e4066Sahrens {
1557fa9e4066Sahrens 	return (spa->spa_dsl_pool);
1558fa9e4066Sahrens }
1559fa9e4066Sahrens 
1560ad135b5dSChristopher Siden boolean_t
1561ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1562ad135b5dSChristopher Siden {
1563ad135b5dSChristopher Siden 	return (spa->spa_is_initializing);
1564ad135b5dSChristopher Siden }
1565ad135b5dSChristopher Siden 
15665cabbc6bSPrashanth Sreenivasa boolean_t
15675cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa)
15685cabbc6bSPrashanth Sreenivasa {
15695cabbc6bSPrashanth Sreenivasa 	return (spa->spa_indirect_vdevs_loaded);
15705cabbc6bSPrashanth Sreenivasa }
15715cabbc6bSPrashanth Sreenivasa 
1572fa9e4066Sahrens blkptr_t *
1573fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1574fa9e4066Sahrens {
1575fa9e4066Sahrens 	return (&spa->spa_ubsync.ub_rootbp);
1576fa9e4066Sahrens }
1577fa9e4066Sahrens 
1578fa9e4066Sahrens void
1579fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1580fa9e4066Sahrens {
1581fa9e4066Sahrens 	spa->spa_uberblock.ub_rootbp = *bp;
1582fa9e4066Sahrens }
1583fa9e4066Sahrens 
1584fa9e4066Sahrens void
1585fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1586fa9e4066Sahrens {
1587fa9e4066Sahrens 	if (spa->spa_root == NULL)
1588fa9e4066Sahrens 		buf[0] = '\0';
1589fa9e4066Sahrens 	else
1590fa9e4066Sahrens 		(void) strncpy(buf, spa->spa_root, buflen);
1591fa9e4066Sahrens }
1592fa9e4066Sahrens 
1593fa9e4066Sahrens int
1594fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1595fa9e4066Sahrens {
1596fa9e4066Sahrens 	return (spa->spa_sync_pass);
1597fa9e4066Sahrens }
1598fa9e4066Sahrens 
1599fa9e4066Sahrens char *
1600fa9e4066Sahrens spa_name(spa_t *spa)
1601fa9e4066Sahrens {
1602fa9e4066Sahrens 	return (spa->spa_name);
1603fa9e4066Sahrens }
1604fa9e4066Sahrens 
1605fa9e4066Sahrens uint64_t
1606fa9e4066Sahrens spa_guid(spa_t *spa)
1607fa9e4066Sahrens {
1608dfbb9432SGeorge Wilson 	dsl_pool_t *dp = spa_get_dsl(spa);
1609dfbb9432SGeorge Wilson 	uint64_t guid;
1610dfbb9432SGeorge Wilson 
1611b5989ec7Seschrock 	/*
1612b5989ec7Seschrock 	 * If we fail to parse the config during spa_load(), we can go through
1613b5989ec7Seschrock 	 * the error path (which posts an ereport) and end up here with no root
1614e9103aaeSGarrett D'Amore 	 * vdev.  We stash the original pool guid in 'spa_config_guid' to handle
1615b5989ec7Seschrock 	 * this case.
1616b5989ec7Seschrock 	 */
1617dfbb9432SGeorge Wilson 	if (spa->spa_root_vdev == NULL)
1618dfbb9432SGeorge Wilson 		return (spa->spa_config_guid);
1619dfbb9432SGeorge Wilson 
1620dfbb9432SGeorge Wilson 	guid = spa->spa_last_synced_guid != 0 ?
1621dfbb9432SGeorge Wilson 	    spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1622dfbb9432SGeorge Wilson 
1623dfbb9432SGeorge Wilson 	/*
1624dfbb9432SGeorge Wilson 	 * Return the most recently synced out guid unless we're
1625dfbb9432SGeorge Wilson 	 * in syncing context.
1626dfbb9432SGeorge Wilson 	 */
1627dfbb9432SGeorge Wilson 	if (dp && dsl_pool_sync_context(dp))
1628b5989ec7Seschrock 		return (spa->spa_root_vdev->vdev_guid);
1629b5989ec7Seschrock 	else
1630dfbb9432SGeorge Wilson 		return (guid);
1631e9103aaeSGarrett D'Amore }
1632e9103aaeSGarrett D'Amore 
1633e9103aaeSGarrett D'Amore uint64_t
1634e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1635e9103aaeSGarrett D'Amore {
1636e9103aaeSGarrett D'Amore 	/*
1637e9103aaeSGarrett D'Amore 	 * This is a GUID that exists solely as a reference for the
1638e9103aaeSGarrett D'Amore 	 * purposes of the arc.  It is generated at load time, and
1639e9103aaeSGarrett D'Amore 	 * is never written to persistent storage.
1640e9103aaeSGarrett D'Amore 	 */
1641e9103aaeSGarrett D'Amore 	return (spa->spa_load_guid);
1642fa9e4066Sahrens }
1643fa9e4066Sahrens 
1644fa9e4066Sahrens uint64_t
1645fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1646fa9e4066Sahrens {
1647fa9e4066Sahrens 	return (spa->spa_ubsync.ub_txg);
1648fa9e4066Sahrens }
1649fa9e4066Sahrens 
1650fa9e4066Sahrens uint64_t
1651fa9e4066Sahrens spa_first_txg(spa_t *spa)
1652fa9e4066Sahrens {
1653fa9e4066Sahrens 	return (spa->spa_first_txg);
1654fa9e4066Sahrens }
1655fa9e4066Sahrens 
1656b24ab676SJeff Bonwick uint64_t
1657b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1658b24ab676SJeff Bonwick {
1659b24ab676SJeff Bonwick 	return (spa->spa_syncing_txg);
1660b24ab676SJeff Bonwick }
1661b24ab676SJeff Bonwick 
16623991b535SGeorge Wilson /*
16633991b535SGeorge Wilson  * Return the last txg where data can be dirtied. The final txgs
16643991b535SGeorge Wilson  * will be used to just clear out any deferred frees that remain.
16653991b535SGeorge Wilson  */
16663991b535SGeorge Wilson uint64_t
16673991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa)
16683991b535SGeorge Wilson {
16693991b535SGeorge Wilson 	return (spa->spa_final_txg - TXG_DEFER_SIZE);
16703991b535SGeorge Wilson }
16713991b535SGeorge Wilson 
167288b7b0f2SMatthew Ahrens pool_state_t
1673fa9e4066Sahrens spa_state(spa_t *spa)
1674fa9e4066Sahrens {
1675fa9e4066Sahrens 	return (spa->spa_state);
1676fa9e4066Sahrens }
1677fa9e4066Sahrens 
1678b16da2e2SGeorge Wilson spa_load_state_t
1679b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1680b16da2e2SGeorge Wilson {
1681b16da2e2SGeorge Wilson 	return (spa->spa_load_state);
1682b16da2e2SGeorge Wilson }
1683b16da2e2SGeorge Wilson 
1684fa9e4066Sahrens uint64_t
1685fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1686fa9e4066Sahrens {
1687fa9e4066Sahrens 	return (spa->spa_freeze_txg);
1688fa9e4066Sahrens }
1689fa9e4066Sahrens 
1690fa9e4066Sahrens /* ARGSUSED */
1691fa9e4066Sahrens uint64_t
169261e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1693fa9e4066Sahrens {
169469962b56SMatthew Ahrens 	return (lsize * spa_asize_inflation);
169544cd46caSbillm }
169644cd46caSbillm 
16977d46dc6cSMatthew Ahrens /*
16987d46dc6cSMatthew Ahrens  * Return the amount of slop space in bytes.  It is 1/32 of the pool (3.2%),
16994b5c8e93SMatthew Ahrens  * or at least 128MB, unless that would cause it to be more than half the
17004b5c8e93SMatthew Ahrens  * pool size.
17017d46dc6cSMatthew Ahrens  *
17027d46dc6cSMatthew Ahrens  * See the comment above spa_slop_shift for details.
17037d46dc6cSMatthew Ahrens  */
17047d46dc6cSMatthew Ahrens uint64_t
17054b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa)
17064b5c8e93SMatthew Ahrens {
17077d46dc6cSMatthew Ahrens 	uint64_t space = spa_get_dspace(spa);
17084b5c8e93SMatthew Ahrens 	return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
17097d46dc6cSMatthew Ahrens }
17107d46dc6cSMatthew Ahrens 
1711485bbbf5SGeorge Wilson uint64_t
1712485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1713485bbbf5SGeorge Wilson {
1714485bbbf5SGeorge Wilson 	return (spa->spa_dspace);
1715485bbbf5SGeorge Wilson }
1716485bbbf5SGeorge Wilson 
1717485bbbf5SGeorge Wilson void
1718485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1719485bbbf5SGeorge Wilson {
1720485bbbf5SGeorge Wilson 	spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1721485bbbf5SGeorge Wilson 	    ddt_get_dedup_dspace(spa);
17225cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal != NULL) {
17235cabbc6bSPrashanth Sreenivasa 		/*
17245cabbc6bSPrashanth Sreenivasa 		 * We can't allocate from the removing device, so
17255cabbc6bSPrashanth Sreenivasa 		 * subtract its size.  This prevents the DMU/DSL from
17265cabbc6bSPrashanth Sreenivasa 		 * filling up the (now smaller) pool while we are in the
17275cabbc6bSPrashanth Sreenivasa 		 * middle of removing the device.
17285cabbc6bSPrashanth Sreenivasa 		 *
17295cabbc6bSPrashanth Sreenivasa 		 * Note that the DMU/DSL doesn't actually know or care
17305cabbc6bSPrashanth Sreenivasa 		 * how much space is allocated (it does its own tracking
17315cabbc6bSPrashanth Sreenivasa 		 * of how much space has been logically used).  So it
17325cabbc6bSPrashanth Sreenivasa 		 * doesn't matter that the data we are moving may be
17335cabbc6bSPrashanth Sreenivasa 		 * allocated twice (on the old device and the new
17345cabbc6bSPrashanth Sreenivasa 		 * device).
17355cabbc6bSPrashanth Sreenivasa 		 */
17365cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_vdev_removal->svr_vdev;
17375cabbc6bSPrashanth Sreenivasa 		spa->spa_dspace -= spa_deflate(spa) ?
17385cabbc6bSPrashanth Sreenivasa 		    vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
17395cabbc6bSPrashanth Sreenivasa 	}
1740485bbbf5SGeorge Wilson }
1741485bbbf5SGeorge Wilson 
17420a4e9518Sgw /*
17430a4e9518Sgw  * Return the failure mode that has been set to this pool. The default
17440a4e9518Sgw  * behavior will be to block all I/Os when a complete failure occurs.
17450a4e9518Sgw  */
17460a4e9518Sgw uint8_t
17470a4e9518Sgw spa_get_failmode(spa_t *spa)
17480a4e9518Sgw {
17490a4e9518Sgw 	return (spa->spa_failmode);
17500a4e9518Sgw }
17510a4e9518Sgw 
1752e14bb325SJeff Bonwick boolean_t
1753e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1754e14bb325SJeff Bonwick {
1755e14bb325SJeff Bonwick 	return (spa->spa_suspended);
1756e14bb325SJeff Bonwick }
1757e14bb325SJeff Bonwick 
175844cd46caSbillm uint64_t
175944cd46caSbillm spa_version(spa_t *spa)
176044cd46caSbillm {
176144cd46caSbillm 	return (spa->spa_ubsync.ub_version);
176244cd46caSbillm }
176344cd46caSbillm 
1764b24ab676SJeff Bonwick boolean_t
1765b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1766b24ab676SJeff Bonwick {
1767b24ab676SJeff Bonwick 	return (spa->spa_deflate);
1768b24ab676SJeff Bonwick }
1769b24ab676SJeff Bonwick 
1770b24ab676SJeff Bonwick metaslab_class_t *
1771b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1772b24ab676SJeff Bonwick {
1773b24ab676SJeff Bonwick 	return (spa->spa_normal_class);
1774b24ab676SJeff Bonwick }
1775b24ab676SJeff Bonwick 
1776b24ab676SJeff Bonwick metaslab_class_t *
1777b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1778b24ab676SJeff Bonwick {
1779b24ab676SJeff Bonwick 	return (spa->spa_log_class);
1780b24ab676SJeff Bonwick }
1781b24ab676SJeff Bonwick 
1782bc9014e6SJustin Gibbs void
1783bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os)
1784bc9014e6SJustin Gibbs {
1785bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1786bc9014e6SJustin Gibbs 	list_insert_head(&spa->spa_evicting_os_list, os);
1787bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1788bc9014e6SJustin Gibbs }
1789bc9014e6SJustin Gibbs 
1790bc9014e6SJustin Gibbs void
1791bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1792bc9014e6SJustin Gibbs {
1793bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1794bc9014e6SJustin Gibbs 	list_remove(&spa->spa_evicting_os_list, os);
1795bc9014e6SJustin Gibbs 	cv_broadcast(&spa->spa_evicting_os_cv);
1796bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1797bc9014e6SJustin Gibbs }
1798bc9014e6SJustin Gibbs 
1799bc9014e6SJustin Gibbs void
1800bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa)
1801bc9014e6SJustin Gibbs {
1802bc9014e6SJustin Gibbs 	mutex_enter(&spa->spa_evicting_os_lock);
1803bc9014e6SJustin Gibbs 	while (!list_is_empty(&spa->spa_evicting_os_list))
1804bc9014e6SJustin Gibbs 		cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1805bc9014e6SJustin Gibbs 	mutex_exit(&spa->spa_evicting_os_lock);
1806bc9014e6SJustin Gibbs 
1807bc9014e6SJustin Gibbs 	dmu_buf_user_evict_wait();
1808bc9014e6SJustin Gibbs }
1809bc9014e6SJustin Gibbs 
181044cd46caSbillm int
181144cd46caSbillm spa_max_replication(spa_t *spa)
181244cd46caSbillm {
181344cd46caSbillm 	/*
1814e7437265Sahrens 	 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
181544cd46caSbillm 	 * handle BPs with more than one DVA allocated.  Set our max
181644cd46caSbillm 	 * replication level accordingly.
1817fa9e4066Sahrens 	 */
1818e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
181944cd46caSbillm 		return (1);
182044cd46caSbillm 	return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1821fa9e4066Sahrens }
1822fa9e4066Sahrens 
18233f9d6ad7SLin Ling int
18243f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
18253f9d6ad7SLin Ling {
18263f9d6ad7SLin Ling 	return (spa->spa_prev_software_version);
18273f9d6ad7SLin Ling }
18283f9d6ad7SLin Ling 
1829283b8460SGeorge.Wilson uint64_t
1830283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
1831283b8460SGeorge.Wilson {
1832283b8460SGeorge.Wilson 	return (spa->spa_deadman_synctime);
1833283b8460SGeorge.Wilson }
1834283b8460SGeorge.Wilson 
183599653d4eSeschrock uint64_t
1836b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
183799653d4eSeschrock {
1838b24ab676SJeff Bonwick 	uint64_t asize = DVA_GET_ASIZE(dva);
1839b24ab676SJeff Bonwick 	uint64_t dsize = asize;
184099653d4eSeschrock 
1841b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
184299653d4eSeschrock 
1843b24ab676SJeff Bonwick 	if (asize != 0 && spa->spa_deflate) {
1844b24ab676SJeff Bonwick 		vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1845b24ab676SJeff Bonwick 		dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
184699653d4eSeschrock 	}
1847b24ab676SJeff Bonwick 
1848b24ab676SJeff Bonwick 	return (dsize);
1849b24ab676SJeff Bonwick }
1850b24ab676SJeff Bonwick 
1851b24ab676SJeff Bonwick uint64_t
1852b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1853b24ab676SJeff Bonwick {
1854b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1855b24ab676SJeff Bonwick 
18565d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1857b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1858b24ab676SJeff Bonwick 
1859b24ab676SJeff Bonwick 	return (dsize);
1860b24ab676SJeff Bonwick }
1861b24ab676SJeff Bonwick 
1862b24ab676SJeff Bonwick uint64_t
1863b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1864b24ab676SJeff Bonwick {
1865b24ab676SJeff Bonwick 	uint64_t dsize = 0;
1866b24ab676SJeff Bonwick 
1867b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1868b24ab676SJeff Bonwick 
18695d7b4d43SMatthew Ahrens 	for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1870b24ab676SJeff Bonwick 		dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1871b24ab676SJeff Bonwick 
1872e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1873b24ab676SJeff Bonwick 
1874b24ab676SJeff Bonwick 	return (dsize);
187599653d4eSeschrock }
187699653d4eSeschrock 
1877fa9e4066Sahrens /*
1878fa9e4066Sahrens  * ==========================================================================
1879fa9e4066Sahrens  * Initialization and Termination
1880fa9e4066Sahrens  * ==========================================================================
1881fa9e4066Sahrens  */
1882fa9e4066Sahrens 
1883fa9e4066Sahrens static int
1884fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
1885fa9e4066Sahrens {
1886fa9e4066Sahrens 	const spa_t *s1 = a1;
1887fa9e4066Sahrens 	const spa_t *s2 = a2;
1888fa9e4066Sahrens 	int s;
1889fa9e4066Sahrens 
1890fa9e4066Sahrens 	s = strcmp(s1->spa_name, s2->spa_name);
1891fa9e4066Sahrens 	if (s > 0)
1892fa9e4066Sahrens 		return (1);
1893fa9e4066Sahrens 	if (s < 0)
1894fa9e4066Sahrens 		return (-1);
1895fa9e4066Sahrens 	return (0);
1896fa9e4066Sahrens }
1897fa9e4066Sahrens 
18980373e76bSbonwick int
18990373e76bSbonwick spa_busy(void)
19000373e76bSbonwick {
19010373e76bSbonwick 	return (spa_active_count);
19020373e76bSbonwick }
19030373e76bSbonwick 
1904e7cbe64fSgw void
1905e7cbe64fSgw spa_boot_init()
1906e7cbe64fSgw {
1907e7cbe64fSgw 	spa_config_load();
1908e7cbe64fSgw }
1909e7cbe64fSgw 
1910fa9e4066Sahrens void
1911fa9e4066Sahrens spa_init(int mode)
1912fa9e4066Sahrens {
1913fa9e4066Sahrens 	mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1914c25056deSgw 	mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1915fa94a07fSbrendan 	mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1916fa9e4066Sahrens 	cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1917fa9e4066Sahrens 
1918fa9e4066Sahrens 	avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1919fa9e4066Sahrens 	    offsetof(spa_t, spa_avl));
1920fa9e4066Sahrens 
1921fa94a07fSbrendan 	avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1922fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
1923fa94a07fSbrendan 
1924fa94a07fSbrendan 	avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1925fa94a07fSbrendan 	    offsetof(spa_aux_t, aux_avl));
192699653d4eSeschrock 
19278ad4d6ddSJeff Bonwick 	spa_mode_global = mode;
1928fa9e4066Sahrens 
1929283b8460SGeorge.Wilson #ifdef _KERNEL
1930283b8460SGeorge.Wilson 	spa_arch_init();
1931283b8460SGeorge.Wilson #else
1932cd1c8b85SMatthew Ahrens 	if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1933cd1c8b85SMatthew Ahrens 		arc_procfd = open("/proc/self/ctl", O_WRONLY);
1934cd1c8b85SMatthew Ahrens 		if (arc_procfd == -1) {
1935cd1c8b85SMatthew Ahrens 			perror("could not enable watchpoints: "
1936cd1c8b85SMatthew Ahrens 			    "opening /proc/self/ctl failed: ");
1937cd1c8b85SMatthew Ahrens 		} else {
1938cd1c8b85SMatthew Ahrens 			arc_watch = B_TRUE;
1939cd1c8b85SMatthew Ahrens 		}
1940cd1c8b85SMatthew Ahrens 	}
1941cd1c8b85SMatthew Ahrens #endif
1942cd1c8b85SMatthew Ahrens 
1943fa9e4066Sahrens 	refcount_init();
1944fa9e4066Sahrens 	unique_init();
19450713e232SGeorge Wilson 	range_tree_init();
19468363e80aSGeorge Wilson 	metaslab_alloc_trace_init();
1947fa9e4066Sahrens 	zio_init();
1948fa9e4066Sahrens 	dmu_init();
1949fa9e4066Sahrens 	zil_init();
195087db74c1Sek 	vdev_cache_stat_init();
195191ebeef5Sahrens 	zfs_prop_init();
1952990b4856Slling 	zpool_prop_init();
1953ad135b5dSChristopher Siden 	zpool_feature_init();
1954fa9e4066Sahrens 	spa_config_load();
1955e14bb325SJeff Bonwick 	l2arc_start();
1956fa9e4066Sahrens }
1957fa9e4066Sahrens 
1958fa9e4066Sahrens void
1959fa9e4066Sahrens spa_fini(void)
1960fa9e4066Sahrens {
1961e14bb325SJeff Bonwick 	l2arc_stop();
1962e14bb325SJeff Bonwick 
1963fa9e4066Sahrens 	spa_evict_all();
1964fa9e4066Sahrens 
196587db74c1Sek 	vdev_cache_stat_fini();
1966fa9e4066Sahrens 	zil_fini();
1967fa9e4066Sahrens 	dmu_fini();
1968fa9e4066Sahrens 	zio_fini();
19698363e80aSGeorge Wilson 	metaslab_alloc_trace_fini();
19700713e232SGeorge Wilson 	range_tree_fini();
197191ebeef5Sahrens 	unique_fini();
1972fa9e4066Sahrens 	refcount_fini();
1973fa9e4066Sahrens 
1974fa9e4066Sahrens 	avl_destroy(&spa_namespace_avl);
197599653d4eSeschrock 	avl_destroy(&spa_spare_avl);
1976fa94a07fSbrendan 	avl_destroy(&spa_l2cache_avl);
1977fa9e4066Sahrens 
1978fa9e4066Sahrens 	cv_destroy(&spa_namespace_cv);
1979fa9e4066Sahrens 	mutex_destroy(&spa_namespace_lock);
1980c25056deSgw 	mutex_destroy(&spa_spare_lock);
1981fa94a07fSbrendan 	mutex_destroy(&spa_l2cache_lock);
1982fa9e4066Sahrens }
19836ce0521aSperrin 
19846ce0521aSperrin /*
19856ce0521aSperrin  * Return whether this pool has slogs. No locking needed.
19866ce0521aSperrin  * It's not a problem if the wrong answer is returned as it's only for
19876ce0521aSperrin  * performance and not correctness
19886ce0521aSperrin  */
19896ce0521aSperrin boolean_t
19906ce0521aSperrin spa_has_slogs(spa_t *spa)
19916ce0521aSperrin {
19926ce0521aSperrin 	return (spa->spa_log_class->mc_rotor != NULL);
19936ce0521aSperrin }
1994bf82a41bSeschrock 
1995b24ab676SJeff Bonwick spa_log_state_t
1996b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
1997b24ab676SJeff Bonwick {
1998b24ab676SJeff Bonwick 	return (spa->spa_log_state);
1999b24ab676SJeff Bonwick }
2000b24ab676SJeff Bonwick 
2001b24ab676SJeff Bonwick void
2002b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
2003b24ab676SJeff Bonwick {
2004b24ab676SJeff Bonwick 	spa->spa_log_state = state;
2005b24ab676SJeff Bonwick }
2006b24ab676SJeff Bonwick 
2007bf82a41bSeschrock boolean_t
2008bf82a41bSeschrock spa_is_root(spa_t *spa)
2009bf82a41bSeschrock {
2010bf82a41bSeschrock 	return (spa->spa_is_root);
2011bf82a41bSeschrock }
20128ad4d6ddSJeff Bonwick 
20138ad4d6ddSJeff Bonwick boolean_t
20148ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
20158ad4d6ddSJeff Bonwick {
20168ad4d6ddSJeff Bonwick 	return (!!(spa->spa_mode & FWRITE));
20178ad4d6ddSJeff Bonwick }
20188ad4d6ddSJeff Bonwick 
201973527f44SAlex Reece /*
202073527f44SAlex Reece  * Returns true if there is a pending sync task in any of the current
202173527f44SAlex Reece  * syncing txg, the current quiescing txg, or the current open txg.
202273527f44SAlex Reece  */
202373527f44SAlex Reece boolean_t
202473527f44SAlex Reece spa_has_pending_synctask(spa_t *spa)
202573527f44SAlex Reece {
202673527f44SAlex Reece 	return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
202773527f44SAlex Reece }
202873527f44SAlex Reece 
20298ad4d6ddSJeff Bonwick int
20308ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
20318ad4d6ddSJeff Bonwick {
20328ad4d6ddSJeff Bonwick 	return (spa->spa_mode);
20338ad4d6ddSJeff Bonwick }
2034b24ab676SJeff Bonwick 
2035b24ab676SJeff Bonwick uint64_t
2036b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
2037b24ab676SJeff Bonwick {
2038b24ab676SJeff Bonwick 	return (spa->spa_bootfs);
2039b24ab676SJeff Bonwick }
2040b24ab676SJeff Bonwick 
2041b24ab676SJeff Bonwick uint64_t
2042b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
2043b24ab676SJeff Bonwick {
2044b24ab676SJeff Bonwick 	return (spa->spa_delegation);
2045b24ab676SJeff Bonwick }
2046b24ab676SJeff Bonwick 
2047b24ab676SJeff Bonwick objset_t *
2048b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
2049b24ab676SJeff Bonwick {
2050b24ab676SJeff Bonwick 	return (spa->spa_meta_objset);
2051b24ab676SJeff Bonwick }
2052b24ab676SJeff Bonwick 
2053b24ab676SJeff Bonwick enum zio_checksum
2054b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
2055b24ab676SJeff Bonwick {
2056b24ab676SJeff Bonwick 	return (spa->spa_dedup_checksum);
2057b24ab676SJeff Bonwick }
20583f9d6ad7SLin Ling 
20593f9d6ad7SLin Ling /*
20603f9d6ad7SLin Ling  * Reset pool scan stat per scan pass (or reboot).
20613f9d6ad7SLin Ling  */
20623f9d6ad7SLin Ling void
20633f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
20643f9d6ad7SLin Ling {
20653f9d6ad7SLin Ling 	/* data not stored on disk */
20663f9d6ad7SLin Ling 	spa->spa_scan_pass_start = gethrestime_sec();
20671702cce7SAlek Pinchuk 	if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
20681702cce7SAlek Pinchuk 		spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
20691702cce7SAlek Pinchuk 	else
20701702cce7SAlek Pinchuk 		spa->spa_scan_pass_scrub_pause = 0;
20711702cce7SAlek Pinchuk 	spa->spa_scan_pass_scrub_spent_paused = 0;
20723f9d6ad7SLin Ling 	spa->spa_scan_pass_exam = 0;
20733f9d6ad7SLin Ling 	vdev_scan_stat_init(spa->spa_root_vdev);
20743f9d6ad7SLin Ling }
20753f9d6ad7SLin Ling 
20763f9d6ad7SLin Ling /*
20773f9d6ad7SLin Ling  * Get scan stats for zpool status reports
20783f9d6ad7SLin Ling  */
20793f9d6ad7SLin Ling int
20803f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
20813f9d6ad7SLin Ling {
20823f9d6ad7SLin Ling 	dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
20833f9d6ad7SLin Ling 
20843f9d6ad7SLin Ling 	if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2085be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
20863f9d6ad7SLin Ling 	bzero(ps, sizeof (pool_scan_stat_t));
20873f9d6ad7SLin Ling 
20883f9d6ad7SLin Ling 	/* data stored on disk */
20893f9d6ad7SLin Ling 	ps->pss_func = scn->scn_phys.scn_func;
20903f9d6ad7SLin Ling 	ps->pss_start_time = scn->scn_phys.scn_start_time;
20913f9d6ad7SLin Ling 	ps->pss_end_time = scn->scn_phys.scn_end_time;
20923f9d6ad7SLin Ling 	ps->pss_to_examine = scn->scn_phys.scn_to_examine;
20933f9d6ad7SLin Ling 	ps->pss_examined = scn->scn_phys.scn_examined;
20943f9d6ad7SLin Ling 	ps->pss_to_process = scn->scn_phys.scn_to_process;
20953f9d6ad7SLin Ling 	ps->pss_processed = scn->scn_phys.scn_processed;
20963f9d6ad7SLin Ling 	ps->pss_errors = scn->scn_phys.scn_errors;
20973f9d6ad7SLin Ling 	ps->pss_state = scn->scn_phys.scn_state;
20983f9d6ad7SLin Ling 
20993f9d6ad7SLin Ling 	/* data not stored on disk */
21003f9d6ad7SLin Ling 	ps->pss_pass_start = spa->spa_scan_pass_start;
21013f9d6ad7SLin Ling 	ps->pss_pass_exam = spa->spa_scan_pass_exam;
21021702cce7SAlek Pinchuk 	ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
21031702cce7SAlek Pinchuk 	ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
21043f9d6ad7SLin Ling 
21053f9d6ad7SLin Ling 	return (0);
21063f9d6ad7SLin Ling }
210709c9d376SGeorge Wilson 
210809c9d376SGeorge Wilson boolean_t
210909c9d376SGeorge Wilson spa_debug_enabled(spa_t *spa)
211009c9d376SGeorge Wilson {
211109c9d376SGeorge Wilson 	return (spa->spa_debug);
211209c9d376SGeorge Wilson }
2113b5152584SMatthew Ahrens 
2114b5152584SMatthew Ahrens int
2115b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa)
2116b5152584SMatthew Ahrens {
2117b5152584SMatthew Ahrens 	if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2118b5152584SMatthew Ahrens 		return (SPA_MAXBLOCKSIZE);
2119b5152584SMatthew Ahrens 	else
2120b5152584SMatthew Ahrens 		return (SPA_OLD_MAXBLOCKSIZE);
2121b5152584SMatthew Ahrens }
21225cabbc6bSPrashanth Sreenivasa 
21235cabbc6bSPrashanth Sreenivasa /*
21245cabbc6bSPrashanth Sreenivasa  * Returns the txg that the last device removal completed. No indirect mappings
21255cabbc6bSPrashanth Sreenivasa  * have been added since this txg.
21265cabbc6bSPrashanth Sreenivasa  */
21275cabbc6bSPrashanth Sreenivasa uint64_t
21285cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa)
21295cabbc6bSPrashanth Sreenivasa {
21305cabbc6bSPrashanth Sreenivasa 	uint64_t vdevid;
21315cabbc6bSPrashanth Sreenivasa 	uint64_t ret = -1ULL;
21325cabbc6bSPrashanth Sreenivasa 
21335cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
21345cabbc6bSPrashanth Sreenivasa 	/*
21355cabbc6bSPrashanth Sreenivasa 	 * sr_prev_indirect_vdev is only modified while holding all the
21365cabbc6bSPrashanth Sreenivasa 	 * config locks, so it is sufficient to hold SCL_VDEV as reader when
21375cabbc6bSPrashanth Sreenivasa 	 * examining it.
21385cabbc6bSPrashanth Sreenivasa 	 */
21395cabbc6bSPrashanth Sreenivasa 	vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
21405cabbc6bSPrashanth Sreenivasa 
21415cabbc6bSPrashanth Sreenivasa 	while (vdevid != -1ULL) {
21425cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = vdev_lookup_top(spa, vdevid);
21435cabbc6bSPrashanth Sreenivasa 		vdev_indirect_births_t *vib = vd->vdev_indirect_births;
21445cabbc6bSPrashanth Sreenivasa 
21455cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
21465cabbc6bSPrashanth Sreenivasa 
21475cabbc6bSPrashanth Sreenivasa 		/*
21485cabbc6bSPrashanth Sreenivasa 		 * If the removal did not remap any data, we don't care.
21495cabbc6bSPrashanth Sreenivasa 		 */
21505cabbc6bSPrashanth Sreenivasa 		if (vdev_indirect_births_count(vib) != 0) {
21515cabbc6bSPrashanth Sreenivasa 			ret = vdev_indirect_births_last_entry_txg(vib);
21525cabbc6bSPrashanth Sreenivasa 			break;
21535cabbc6bSPrashanth Sreenivasa 		}
21545cabbc6bSPrashanth Sreenivasa 
21555cabbc6bSPrashanth Sreenivasa 		vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
21565cabbc6bSPrashanth Sreenivasa 	}
21575cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
21585cabbc6bSPrashanth Sreenivasa 
21595cabbc6bSPrashanth Sreenivasa 	IMPLY(ret != -1ULL,
21605cabbc6bSPrashanth Sreenivasa 	    spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
21615cabbc6bSPrashanth Sreenivasa 
21625cabbc6bSPrashanth Sreenivasa 	return (ret);
21635cabbc6bSPrashanth Sreenivasa }
2164