1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
229842588George Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23814dcd4Serapheim Dimitropoulos * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24e495b6eSaso Kiselkov * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2645818eeMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved.
27c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
281702cceAlek Pinchuk * Copyright (c) 2017 Datto Inc.
29e830fb1Kody A Kantor * Copyright 2019 Joyent, Inc.
30663207aDon Brady * Copyright (c) 2017, Intel Corporation.
31dd50e0cTony Hutter * Copyright 2020 Joyent, Inc.
32fa9e406ahrens */
33fa9e406ahrens
34fa9e406ahrens#include <sys/zfs_context.h>
35fa9e406ahrens#include <sys/spa_impl.h>
36283b846George.Wilson#include <sys/spa_boot.h>
37fa9e406ahrens#include <sys/zio.h>
38fa9e406ahrens#include <sys/zio_checksum.h>
39fa9e406ahrens#include <sys/zio_compress.h>
40fa9e406ahrens#include <sys/dmu.h>
41fa9e406ahrens#include <sys/dmu_tx.h>
42fa9e406ahrens#include <sys/zap.h>
43fa9e406ahrens#include <sys/zil.h>
44fa9e406ahrens#include <sys/vdev_impl.h>
45094e47eGeorge Wilson#include <sys/vdev_initialize.h>
46084fd14Brian Behlendorf#include <sys/vdev_trim.h>
47e86372aGvozden Neskovic#include <sys/vdev_raidz.h>
48fa9e406ahrens#include <sys/metaslab.h>
49fa9e406ahrens#include <sys/uberblock_impl.h>
50fa9e406ahrens#include <sys/txg.h>
51fa9e406ahrens#include <sys/avl.h>
52fa9e406ahrens#include <sys/unique.h>
53fa9e406ahrens#include <sys/dsl_pool.h>
54fa9e406ahrens#include <sys/dsl_dir.h>
55fa9e406ahrens#include <sys/dsl_prop.h>
563f9d6adLin Ling#include <sys/dsl_scan.h>
57fa9e406ahrens#include <sys/fs/zfs.h>
586ce0521perrin#include <sys/metaslab_impl.h>
59e14bb32Jeff Bonwick#include <sys/arc.h>
60485bbbfGeorge Wilson#include <sys/ddt.h>
6191ebeefahrens#include "zfs_prop.h"
624d7988dPaul Dagnelie#include <sys/btree.h>
6345818eeMatthew Ahrens#include <sys/zfeature.h>
64fa9e406ahrens
65fa9e406ahrens/*
66fa9e406ahrens * SPA locking
67fa9e406ahrens *
68814dcd4Serapheim Dimitropoulos * There are three basic locks for managing spa_t structures:
69fa9e406ahrens *
70fa9e406ahrens * spa_namespace_lock (global mutex)
71fa9e406ahrens *
7244cd46cbillm *	This lock must be acquired to do any of the following:
73fa9e406ahrens *
7444cd46cbillm *		- Lookup a spa_t by name
7544cd46cbillm *		- Add or remove a spa_t from the namespace
7644cd46cbillm *		- Increase spa_refcount from non-zero
7744cd46cbillm *		- Check if spa_refcount is zero
7844cd46cbillm *		- Rename a spa_t
79ea8dc4beschrock *		- add/remove/attach/detach devices
8044cd46cbillm *		- Held for the duration of create/destroy/import/export
81fa9e406ahrens *
8244cd46cbillm *	It does not need to handle recursion.  A create or destroy may
8344cd46cbillm *	reference objects (files or zvols) in other pools, but by
8444cd46cbillm *	definition they must have an existing reference, and will never need
8544cd46cbillm *	to lookup a spa_t by name.
86fa9e406ahrens *
87e914aceTim Schumacher * spa_refcount (per-spa zfs_refcount_t protected by mutex)
88fa9e406ahrens *
8944cd46cbillm *	This reference count keep track of any active users of the spa_t.  The
9044cd46cbillm *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
9144cd46cbillm *	the refcount is never really 'zero' - opening a pool implicitly keeps
92088f389ahrens *	some references in the DMU.  Internally we check against spa_minref, but
9344cd46cbillm *	present the image of a zero/non-zero value to consumers.
94fa9e406ahrens *
95e14bb32Jeff Bonwick * spa_config_lock[] (per-spa array of rwlocks)
96fa9e406ahrens *
9791ebeefahrens *	This protects the spa_t from config changes, and must be held in
9891ebeefahrens *	the following circumstances:
99fa9e406ahrens *
10044cd46cbillm *		- RW_READER to perform I/O to the spa
10144cd46cbillm *		- RW_WRITER to change the vdev config
102fa9e406ahrens *
103fa9e406ahrens * The locking order is fairly straightforward:
104fa9e406ahrens *
10544cd46cbillm *		spa_namespace_lock	->	spa_refcount
106fa9e406ahrens *
10744cd46cbillm *	The namespace lock must be acquired to increase the refcount from 0
10844cd46cbillm *	or to check if it is zero.
109fa9e406ahrens *
110e14bb32Jeff Bonwick *		spa_refcount		->	spa_config_lock[]
111fa9e406ahrens *
11244cd46cbillm *	There must be at least one valid reference on the spa_t to acquire
11344cd46cbillm *	the config lock.
114fa9e406ahrens *
115e14bb32Jeff Bonwick *		spa_namespace_lock	->	spa_config_lock[]
116fa9e406ahrens *
11744cd46cbillm *	The namespace lock must always be taken before the config lock.
118fa9e406ahrens *
119fa9e406ahrens *
120e14bb32Jeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible.
121fa9e406ahrens *
122e14bb32Jeff Bonwick * The namespace is manipulated using the following functions, all of which
123e14bb32Jeff Bonwick * require the spa_namespace_lock to be held.
124fa9e406ahrens *
12544cd46cbillm *	spa_lookup()		Lookup a spa_t by name.
126fa9e406ahrens *
12744cd46cbillm *	spa_add()		Create a new spa_t in the namespace.
128fa9e406ahrens *
12944cd46cbillm *	spa_remove()		Remove a spa_t from the namespace.  This also
13044cd46cbillm *				frees up any memory associated with the spa_t.
131fa9e406ahrens *
13244cd46cbillm *	spa_next()		Returns the next spa_t in the system, or the
13344cd46cbillm *				first if NULL is passed.
134fa9e406ahrens *
13544cd46cbillm *	spa_evict_all()		Shutdown and remove all spa_t structures in
13644cd46cbillm *				the system.
137fa9e406ahrens *
138ea8dc4beschrock *	spa_guid_exists()	Determine whether a pool/device guid exists.
139fa9e406ahrens *
140fa9e406ahrens * The spa_refcount is manipulated using the following functions:
141fa9e406ahrens *
14244cd46cbillm *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
14344cd46cbillm *				called with spa_namespace_lock held if the
14444cd46cbillm *				refcount is currently zero.
145fa9e406ahrens *
14644cd46cbillm *	spa_close()		Remove a reference from the spa_t.  This will
14744cd46cbillm *				not free the spa_t or remove it from the
14844cd46cbillm *				namespace.  No locking is required.
149fa9e406ahrens *
15044cd46cbillm *	spa_refcount_zero()	Returns true if the refcount is currently
15144cd46cbillm *				zero.  Must be called with spa_namespace_lock
15244cd46cbillm *				held.
153fa9e406ahrens *
154e14bb32Jeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows:
155e14bb32Jeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
156e14bb32Jeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
157e14bb32Jeff Bonwick *
158e14bb32Jeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader.
159e14bb32Jeff Bonwick * To modify the configuration, you must hold all locks as writer.  To modify
160e14bb32Jeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline),
161e14bb32Jeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer.
162e14bb32Jeff Bonwick *
163e14bb32Jeff Bonwick * We use these distinct config locks to avoid recursive lock entry.
164e14bb32Jeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
165e14bb32Jeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps
166e14bb32Jeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
167e14bb32Jeff Bonwick *
168e14bb32Jeff Bonwick * The spa config locks cannot be normal rwlocks because we need the
169e14bb32Jeff Bonwick * ability to hand off ownership.  For example, SCL_ZIO is acquired
170e14bb32Jeff Bonwick * by the issuing thread and later released by an interrupt thread.
171e14bb32Jeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent
172e14bb32Jeff Bonwick * writer (i.e. system administrator) starvation.
173e14bb32Jeff Bonwick *
174e14bb32Jeff Bonwick * The lock acquisition rules are as follows:
175e14bb32Jeff Bonwick *
176e14bb32Jeff Bonwick * SCL_CONFIG
177e14bb32Jeff Bonwick *	Protects changes to the vdev tree topology, such as vdev
178e14bb32Jeff Bonwick *	add/remove/attach/detach.  Protects the dirty config list
179e14bb32Jeff Bonwick *	(spa_config_dirty_list) and the set of spares and l2arc devices.
180e14bb32Jeff Bonwick *
181e14bb32Jeff Bonwick * SCL_STATE
182e14bb32Jeff Bonwick *	Protects changes to pool state and vdev state, such as vdev
183e14bb32Jeff Bonwick *	online/offline/fault/degrade/clear.  Protects the dirty state list
184e14bb32Jeff Bonwick *	(spa_state_dirty_list) and global pool state (spa_state).
185e14bb32Jeff Bonwick *
186e14bb32Jeff Bonwick * SCL_ALLOC
187e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
188e14bb32Jeff Bonwick *	Held as reader by metaslab_alloc() and metaslab_claim().
189e14bb32Jeff Bonwick *
190e14bb32Jeff Bonwick * SCL_ZIO
191e14bb32Jeff Bonwick *	Held by bp-level zios (those which have no io_vd upon entry)
192e14bb32Jeff Bonwick *	to prevent changes to the vdev tree.  The bp-level zio implicitly
193e14bb32Jeff Bonwick *	protects all of its vdev child zios, which do not hold SCL_ZIO.
194e14bb32Jeff Bonwick *
195e14bb32Jeff Bonwick * SCL_FREE
196e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
197e14bb32Jeff Bonwick *	Held as reader by metaslab_free().  SCL_FREE is distinct from
198e14bb32Jeff Bonwick *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
199e14bb32Jeff Bonwick *	blocks in zio_done() while another i/o that holds either
200e14bb32Jeff Bonwick *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
201e14bb32Jeff Bonwick *
202e14bb32Jeff Bonwick * SCL_VDEV
203e14bb32Jeff Bonwick *	Held as reader to prevent changes to the vdev tree during trivial
204b24ab67Jeff Bonwick *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
205e14bb32Jeff Bonwick *	other locks, and lower than all of them, to ensure that it's safe
206e14bb32Jeff Bonwick *	to acquire regardless of caller context.
207e14bb32Jeff Bonwick *
208e14bb32Jeff Bonwick * In addition, the following rules apply:
209e14bb32Jeff Bonwick *
210e14bb32Jeff Bonwick * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
211e14bb32Jeff Bonwick *	The lock ordering is SCL_CONFIG > spa_props_lock.
212e14bb32Jeff Bonwick *
213e14bb32Jeff Bonwick * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
214e14bb32Jeff Bonwick *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
215e14bb32Jeff Bonwick *	or zio_write_phys() -- the caller must ensure that the config cannot
216e14bb32Jeff Bonwick *	cannot change in the interim, and that the vdev cannot be reopened.
217e14bb32Jeff Bonwick *	SCL_STATE as reader suffices for both.
218fa9e406ahrens *
219ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
220fa9e406ahrens *
22144cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
222ea8dc4beschrock *				for writing.
223fa9e406ahrens *
22444cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
22544cd46cbillm *				to complete, sync the updated configs to the
226ea8dc4beschrock *				cache, and release the namespace lock.
227fa9e406ahrens *
228e14bb32Jeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
229e14bb32Jeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
230e14bb32Jeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[].
231fa9e406ahrens */
232fa9e406ahrens
233fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
234fa9e406ahrenskmutex_t spa_namespace_lock;
235fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
2360373e76bonwickstatic int spa_active_count;
237416e0cdekint spa_max_replication_override = SPA_DVAS_PER_BP;
238fa9e406ahrens
23999653d4eschrockstatic kmutex_t spa_spare_lock;
24039c2341eschrockstatic avl_tree_t spa_spare_avl;
241fa94a07brendanstatic kmutex_t spa_l2cache_lock;
242fa94a07brendanstatic avl_tree_t spa_l2cache_avl;
24399653d4eschrock
244fa9e406ahrenskmem_cache_t *spa_buffer_pool;
2458ad4d6dJeff Bonwickint spa_mode_global;
246fa9e406ahrens
247fa9e406ahrens#ifdef ZFS_DEBUG
2485cabbc6Prashanth Sreenivasa/*
2495cabbc6Prashanth Sreenivasa * Everything except dprintf, spa, and indirect_remap is on by default
2505cabbc6Prashanth Sreenivasa * in debug builds.
2515cabbc6Prashanth Sreenivasa */
25221f7c81Matthew Ahrensint zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP);
253fa9e406ahrens#else
254fa9e406ahrensint zfs_flags = 0;
255fa9e406ahrens#endif
256fa9e406ahrens
2570125049ahrens/*
2580125049ahrens * zfs_recover can be set to nonzero to attempt to recover from
2590125049ahrens * otherwise-fatal errors, typically caused by on-disk corruption.  When
2600125049ahrens * set, calls to zfs_panic_recover() will turn into warning messages.
2618b36997Matthew Ahrens * This should only be used as a last resort, as it typically results
2628b36997Matthew Ahrens * in leaked space, or worse.
2630125049ahrens */
2647fd05acMatthew Ahrensboolean_t zfs_recover = B_FALSE;
2657fd05acMatthew Ahrens
2667fd05acMatthew Ahrens/*
2677fd05acMatthew Ahrens * If destroy encounters an EIO while reading metadata (e.g. indirect
2687fd05acMatthew Ahrens * blocks), space referenced by the missing metadata can not be freed.
2697fd05acMatthew Ahrens * Normally this causes the background destroy to become "stalled", as
2707fd05acMatthew Ahrens * it is unable to make forward progress.  While in this stalled state,
2717fd05acMatthew Ahrens * all remaining space to free from the error-encountering filesystem is
2727fd05acMatthew Ahrens * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
2737fd05acMatthew Ahrens * permanently leak the space from indirect blocks that can not be read,
2747fd05acMatthew Ahrens * and continue to free everything else that it can.
2757fd05acMatthew Ahrens *
2767fd05acMatthew Ahrens * The default, "stalling" behavior is useful if the storage partially
2777fd05acMatthew Ahrens * fails (i.e. some but not all i/os fail), and then later recovers.  In
2787fd05acMatthew Ahrens * this case, we will be able to continue pool operations while it is
2797fd05acMatthew Ahrens * partially failed, and when it recovers, we can continue to free the
2807fd05acMatthew Ahrens * space, with no leaks.  However, note that this case is actually
2817fd05acMatthew Ahrens * fairly rare.
2827fd05acMatthew Ahrens *
2837fd05acMatthew Ahrens * Typically pools either (a) fail completely (but perhaps temporarily,
2847fd05acMatthew Ahrens * e.g. a top-level vdev going offline), or (b) have localized,
2857fd05acMatthew Ahrens * permanent errors (e.g. disk returns the wrong data due to bit flip or
2867fd05acMatthew Ahrens * firmware bug).  In case (a), this setting does not matter because the
2877fd05acMatthew Ahrens * pool will be suspended and the sync thread will not be able to make
2887fd05acMatthew Ahrens * forward progress regardless.  In case (b), because the error is
2897fd05acMatthew Ahrens * permanent, the best we can do is leak the minimum amount of space,
2907fd05acMatthew Ahrens * which is what setting this flag will do.  Therefore, it is reasonable
2917fd05acMatthew Ahrens * for this flag to normally be set, but we chose the more conservative
2927fd05acMatthew Ahrens * approach of not setting it, so that there is no possibility of
2937fd05acMatthew Ahrens * leaking space in the "partial temporary" failure case.
2947fd05acMatthew Ahrens */
2957fd05acMatthew Ahrensboolean_t zfs_free_leak_on_eio = B_FALSE;
2960125049ahrens
29769962b5Matthew Ahrens/*
29869962b5Matthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is
29969962b5Matthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the
30069962b5Matthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
30169962b5Matthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that
30269962b5Matthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
30369962b5Matthew Ahrens * in a system panic.
30469962b5Matthew Ahrens */
30569962b5Matthew Ahrensuint64_t zfs_deadman_synctime_ms = 1000000ULL;
306283b846George.Wilson
307283b846George.Wilson/*
30869962b5Matthew Ahrens * Check time in milliseconds. This defines the frequency at which we check
30969962b5Matthew Ahrens * for hung I/O.
310283b846George.Wilson */
31169962b5Matthew Ahrensuint64_t zfs_deadman_checktime_ms = 5000ULL;
312283b846George.Wilson
313283b846George.Wilson/*
314283b846George.Wilson * Override the zfs deadman behavior via /etc/system. By default the
315283b846George.Wilson * deadman is enabled except on VMware and sparc deployments.
316283b846George.Wilson */
317283b846George.Wilsonint zfs_deadman_enabled = -1;
318283b846George.Wilson
31969962b5Matthew Ahrens/*
32069962b5Matthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which
32169962b5Matthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
32269962b5Matthew Ahrens * times the size; so just assume that.  Add to this the fact that
32369962b5Matthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because
32469962b5Matthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
32569962b5Matthew Ahrens * the worst case is:
32669962b5Matthew Ahrens *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
32769962b5Matthew Ahrens */
32869962b5Matthew Ahrensint spa_asize_inflation = 24;
329fa9e406ahrens
330fa9e406ahrens/*
3317d46dc6Matthew Ahrens * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3327d46dc6Matthew Ahrens * the pool to be consumed.  This ensures that we don't run the pool
3337d46dc6Matthew Ahrens * completely out of space, due to unaccounted changes (e.g. to the MOS).
3347d46dc6Matthew Ahrens * It also limits the worst-case time to allocate space.  If we have
3357d46dc6Matthew Ahrens * less than this amount of free space, most ZPL operations (e.g. write,
3367d46dc6Matthew Ahrens * create) will return ENOSPC.
3377d46dc6Matthew Ahrens *
3387d46dc6Matthew Ahrens * Certain operations (e.g. file removal, most administrative actions) can
3397d46dc6Matthew Ahrens * use half the slop space.  They will only return ENOSPC if less than half
3407d46dc6Matthew Ahrens * the slop space is free.  Typically, once the pool has less than the slop
3417d46dc6Matthew Ahrens * space free, the user will use these operations to free up space in the pool.
3427d46dc6Matthew Ahrens * These are the operations that call dsl_pool_adjustedsize() with the netfree
3437d46dc6Matthew Ahrens * argument set to TRUE.
3447d46dc6Matthew Ahrens *
3458671400Serapheim Dimitropoulos * Operations that are almost guaranteed to free up space in the absence of
3468671400Serapheim Dimitropoulos * a pool checkpoint can use up to three quarters of the slop space
3478671400Serapheim Dimitropoulos * (e.g zfs destroy).
3488671400Serapheim Dimitropoulos *
3497d46dc6Matthew Ahrens * A very restricted set of operations are always permitted, regardless of
3507d46dc6Matthew Ahrens * the amount of free space.  These are the operations that call
3518671400Serapheim Dimitropoulos * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
3528671400Serapheim Dimitropoulos * increase in the amount of space used, it is possible to run the pool
3538671400Serapheim Dimitropoulos * completely out of space, causing it to be permanently read-only.
3547d46dc6Matthew Ahrens *
3554b5c8e9Matthew Ahrens * Note that on very small pools, the slop space will be larger than
3564b5c8e9Matthew Ahrens * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
3574b5c8e9Matthew Ahrens * but we never allow it to be more than half the pool size.
3584b5c8e9Matthew Ahrens *
3597d46dc6Matthew Ahrens * See also the comments in zfs_space_check_t.
3607d46dc6Matthew Ahrens */
3617d46dc6Matthew Ahrensint spa_slop_shift = 5;
3624b5c8e9Matthew Ahrensuint64_t spa_min_slop = 128 * 1024 * 1024;
3637d46dc6Matthew Ahrens
364f78cdc3Paul Dagnelieint spa_allocators = 4;
365f78cdc3Paul Dagnelie
3663ee8c80Pavel Zakharov/*PRINTFLIKE2*/
3673ee8c80Pavel Zakharovvoid
3683ee8c80Pavel Zakharovspa_load_failed(spa_t *spa, const char *fmt, ...)
3693ee8c80Pavel Zakharov{
3703ee8c80Pavel Zakharov	va_list adx;
3713ee8c80Pavel Zakharov	char buf[256];
3723ee8c80Pavel Zakharov
3733ee8c80Pavel Zakharov	va_start(adx, fmt);
3743ee8c80Pavel Zakharov	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3753ee8c80Pavel Zakharov	va_end(adx);
3763ee8c80Pavel Zakharov
3776f79381Pavel Zakharov	zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
3786f79381Pavel Zakharov	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3793ee8c80Pavel Zakharov}
3803ee8c80Pavel Zakharov
3813ee8c80Pavel Zakharov/*PRINTFLIKE2*/
3823ee8c80Pavel Zakharovvoid
3833ee8c80Pavel Zakharovspa_load_note(spa_t *spa, const char *fmt, ...)
3843ee8c80Pavel Zakharov{
3853ee8c80Pavel Zakharov	va_list adx;
3863ee8c80Pavel Zakharov	char buf[256];
3873ee8c80Pavel Zakharov
3883ee8c80Pavel Zakharov	va_start(adx, fmt);
3893ee8c80Pavel Zakharov	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3903ee8c80Pavel Zakharov	va_end(adx);
3913ee8c80Pavel Zakharov
3926f79381Pavel Zakharov	zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
3936f79381Pavel Zakharov	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3943ee8c80Pavel Zakharov}
3953ee8c80Pavel Zakharov
3967d46dc6Matthew Ahrens/*
397663207aDon Brady * By default dedup and user data indirects land in the special class
398663207aDon Brady */
399663207aDon Bradyint zfs_ddt_data_is_special = B_TRUE;
400663207aDon Bradyint zfs_user_indirect_is_special = B_TRUE;
401663207aDon Brady
402663207aDon Brady/*
403663207aDon Brady * The percentage of special class final space reserved for metadata only.
404663207aDon Brady * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
405663207aDon Brady * let metadata into the class.
406663207aDon Brady */
407663207aDon Bradyint zfs_special_class_metadata_reserve_pct = 25;
408663207aDon Brady
409663207aDon Brady/*
410fa9e406ahrens * ==========================================================================
411e05725bbonwick * SPA config locking
412e05725bbonwick * ==========================================================================
413e05725bbonwick */
414e05725bbonwickstatic void
415e14bb32Jeff Bonwickspa_config_lock_init(spa_t *spa)
416e14bb32Jeff Bonwick{
417e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
418e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
419e14bb32Jeff Bonwick		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
420e14bb32Jeff Bonwick		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
421e914aceTim Schumacher		zfs_refcount_create_untracked(&scl->scl_count);
422e14bb32Jeff Bonwick		scl->scl_writer = NULL;
423e14bb32Jeff Bonwick		scl->scl_write_wanted = 0;
424e14bb32Jeff Bonwick	}
425e05725bbonwick}
426e05725bbonwick
427e05725bbonwickstatic void
428e14bb32Jeff Bonwickspa_config_lock_destroy(spa_t *spa)
429e14bb32Jeff Bonwick{
430e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
431e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
432e14bb32Jeff Bonwick		mutex_destroy(&scl->scl_lock);
433e14bb32Jeff Bonwick		cv_destroy(&scl->scl_cv);
434e914aceTim Schumacher		zfs_refcount_destroy(&scl->scl_count);
435e14bb32Jeff Bonwick		ASSERT(scl->scl_writer == NULL);
436e14bb32Jeff Bonwick		ASSERT(scl->scl_write_wanted == 0);
437e14bb32Jeff Bonwick	}
438e14bb32Jeff Bonwick}
439e14bb32Jeff Bonwick
440e14bb32Jeff Bonwickint
441e14bb32Jeff Bonwickspa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
442e05725bbonwick{
443e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
444e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
445e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
446e14bb32Jeff Bonwick			continue;
447e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
448e14bb32Jeff Bonwick		if (rw == RW_READER) {
449e14bb32Jeff Bonwick			if (scl->scl_writer || scl->scl_write_wanted) {
450e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
451e495b6eSaso Kiselkov				spa_config_exit(spa, locks & ((1 << i) - 1),
452e495b6eSaso Kiselkov				    tag);
453e14bb32Jeff Bonwick				return (0);
454e14bb32Jeff Bonwick			}
455e14bb32Jeff Bonwick		} else {
456e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
457e914aceTim Schumacher			if (!zfs_refcount_is_zero(&scl->scl_count)) {
458e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
459e495b6eSaso Kiselkov				spa_config_exit(spa, locks & ((1 << i) - 1),
460e495b6eSaso Kiselkov				    tag);
461e14bb32Jeff Bonwick				return (0);
462e14bb32Jeff Bonwick			}
463e14bb32Jeff Bonwick			scl->scl_writer = curthread;
464e14bb32Jeff Bonwick		}
465e914aceTim Schumacher		(void) zfs_refcount_add(&scl->scl_count, tag);
466e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
467e14bb32Jeff Bonwick	}
468e14bb32Jeff Bonwick	return (1);
469e05725bbonwick}
470e05725bbonwick
471e05725bbonwickvoid
472e14bb32Jeff Bonwickspa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
473e05725bbonwick{
474f64c0e3Eric Taylor	int wlocks_held = 0;
475f64c0e3Eric Taylor
4763b2aab1Matthew Ahrens	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4773b2aab1Matthew Ahrens
478e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
479e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
480f64c0e3Eric Taylor		if (scl->scl_writer == curthread)
481f64c0e3Eric Taylor			wlocks_held |= (1 << i);
482e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
483e14bb32Jeff Bonwick			continue;
484e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
485e14bb32Jeff Bonwick		if (rw == RW_READER) {
486e14bb32Jeff Bonwick			while (scl->scl_writer || scl->scl_write_wanted) {
487e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
488e14bb32Jeff Bonwick			}
489e14bb32Jeff Bonwick		} else {
490e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
491e914aceTim Schumacher			while (!zfs_refcount_is_zero(&scl->scl_count)) {
492e14bb32Jeff Bonwick				scl->scl_write_wanted++;
493e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
494e14bb32Jeff Bonwick				scl->scl_write_wanted--;
495e14bb32Jeff Bonwick			}
496e14bb32Jeff Bonwick			scl->scl_writer = curthread;
497e14bb32Jeff Bonwick		}
498e914aceTim Schumacher		(void) zfs_refcount_add(&scl->scl_count, tag);
499e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
500e05725bbonwick	}
5015cabbc6Prashanth Sreenivasa	ASSERT3U(wlocks_held, <=, locks);
502e05725bbonwick}
503e05725bbonwick
504e05725bbonwickvoid
505e14bb32Jeff Bonwickspa_config_exit(spa_t *spa, int locks, void *tag)
506e05725bbonwick{
507e14bb32Jeff Bonwick	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
508e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
509e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
510e14bb32Jeff Bonwick			continue;
511e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
512e914aceTim Schumacher		ASSERT(!zfs_refcount_is_zero(&scl->scl_count));
513e914aceTim Schumacher		if (zfs_refcount_remove(&scl->scl_count, tag) == 0) {
514e14bb32Jeff Bonwick			ASSERT(scl->scl_writer == NULL ||
515e14bb32Jeff Bonwick			    scl->scl_writer == curthread);
516e14bb32Jeff Bonwick			scl->scl_writer = NULL;	/* OK in either case */
517e14bb32Jeff Bonwick			cv_broadcast(&scl->scl_cv);
518e14bb32Jeff Bonwick		}
519e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
520e05725bbonwick	}
521e05725bbonwick}
522e05725bbonwick
523e14bb32Jeff Bonwickint
524e14bb32Jeff Bonwickspa_config_held(spa_t *spa, int locks, krw_t rw)
525e05725bbonwick{
526e14bb32Jeff Bonwick	int locks_held = 0;
527e05725bbonwick
528e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
529e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
530e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
531e14bb32Jeff Bonwick			continue;
532e914aceTim Schumacher		if ((rw == RW_READER &&
533e914aceTim Schumacher		    !zfs_refcount_is_zero(&scl->scl_count)) ||
534e14bb32Jeff Bonwick		    (rw == RW_WRITER && scl->scl_writer == curthread))
535e14bb32Jeff Bonwick			locks_held |= 1 << i;
536e14bb32Jeff Bonwick	}
537e14bb32Jeff Bonwick
538e14bb32Jeff Bonwick	return (locks_held);
539e05725bbonwick}
540e05725bbonwick
541e05725bbonwick/*
542e05725bbonwick * ==========================================================================
543fa9e406ahrens * SPA namespace functions
544fa9e406ahrens * ==========================================================================
545fa9e406ahrens */
546fa9e406ahrens
547fa9e406ahrens/*
548fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
549fa9e406ahrens * Returns NULL if no matching spa_t is found.
550fa9e406ahrens */
551fa9e406ahrensspa_t *
552fa9e406ahrensspa_lookup(const char *name)
553fa9e406ahrens{
554e14bb32Jeff Bonwick	static spa_t search;	/* spa_t is large; don't allocate on stack */
555e14bb32Jeff Bonwick	spa_t *spa;
556fa9e406ahrens	avl_index_t where;
55740feaa9ahrens	char *cp;
558fa9e406ahrens
559fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
560fa9e406ahrens
5613b2aab1Matthew Ahrens	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
5623b2aab1Matthew Ahrens
56340feaa9ahrens	/*
56440feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
56540feaa9ahrens	 * just use that.
56640feaa9ahrens	 */
56778f1710Matthew Ahrens	cp = strpbrk(search.spa_name, "/@#");
5683b2aab1Matthew Ahrens	if (cp != NULL)
56940feaa9ahrens		*cp = '\0';
57040feaa9ahrens
571fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
572fa9e406ahrens
573fa9e406ahrens	return (spa);
574fa9e406ahrens}
575fa9e406ahrens
576fa9e406ahrens/*
577283b846George.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
578283b846George.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
579283b846George.Wilson * looking for potentially hung I/Os.
580283b846George.Wilson */
581283b846George.Wilsonvoid
582283b846George.Wilsonspa_deadman(void *arg)
583283b846George.Wilson{
584283b846George.Wilson	spa_t *spa = arg;
585283b846George.Wilson
5860713e23George Wilson	/*
5870713e23George Wilson	 * Disable the deadman timer if the pool is suspended.
5880713e23George Wilson	 */
5890713e23George Wilson	if (spa_suspended(spa)) {
5900713e23George Wilson		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5910713e23George Wilson		return;
5920713e23George Wilson	}
5930713e23George Wilson
594283b846George.Wilson	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
595283b846George.Wilson	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
596283b846George.Wilson	    ++spa->spa_deadman_calls);
597283b846George.Wilson	if (zfs_deadman_enabled)
598283b846George.Wilson		vdev_deadman(spa->spa_root_vdev);
599283b846George.Wilson}
600283b846George.Wilson
601814dcd4Serapheim Dimitropoulosint
602814dcd4Serapheim Dimitropoulosspa_log_sm_sort_by_txg(const void *va, const void *vb)
603814dcd4Serapheim Dimitropoulos{
604814dcd4Serapheim Dimitropoulos	const spa_log_sm_t *a = va;
605814dcd4Serapheim Dimitropoulos	const spa_log_sm_t *b = vb;
606814dcd4Serapheim Dimitropoulos
6074d7988dPaul Dagnelie	return (TREE_CMP(a->sls_txg, b->sls_txg));
608814dcd4Serapheim Dimitropoulos}
609814dcd4Serapheim Dimitropoulos
610283b846George.Wilson/*
611fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
612fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
613fa9e406ahrens * exist by calling spa_lookup() first.
614fa9e406ahrens */
615fa9e406ahrensspa_t *
616468c413Tim Haleyspa_add(const char *name, nvlist_t *config, const char *altroot)
617fa9e406ahrens{
618fa9e406ahrens	spa_t *spa;
619c5904d1eschrock	spa_config_dirent_t *dp;
620283b846George.Wilson	cyc_handler_t hdlr;
621283b846George.Wilson	cyc_time_t when;
622fa9e406ahrens
623fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
624fa9e406ahrens
625fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
626fa9e406ahrens
627c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
628c25056dgw	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
62935a5a35Jonathan Adams	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
630bc9014eJustin Gibbs	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
631c25056dgw	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
63235a5a35Jonathan Adams	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
633c25056dgw	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
63445818eeMatthew Ahrens	mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
63535a5a35Jonathan Adams	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
636a152156Jeff Bonwick	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
637a152156Jeff Bonwick	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
638c3a6601Matthew Ahrens	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
639814dcd4Serapheim Dimitropoulos	mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
640dd50e0cTony Hutter	mutex_init(&spa->spa_imp_kstat_lock, NULL, MUTEX_DEFAULT, NULL);
641c25056dgw
642c25056dgw	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
643bc9014eJustin Gibbs	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
64435a5a35Jonathan Adams	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
645c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
646e14bb32Jeff Bonwick	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
647c25056dgw
648b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
649cde58dbMatthew Ahrens		bplist_create(&spa->spa_free_bplist[t]);
650b24ab67Jeff Bonwick
651e14bb32Jeff Bonwick	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
652fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
653fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
6540373e76bonwick	spa->spa_final_txg = UINT64_MAX;
655468c413Tim Haley	spa->spa_load_max_txg = UINT64_MAX;
65635a5a35Jonathan Adams	spa->spa_proc = &p0;
65735a5a35Jonathan Adams	spa->spa_proc_state = SPA_PROC_NONE;
6586f79381Pavel Zakharov	spa->spa_trust_config = B_TRUE;
659fa9e406ahrens
660283b846George.Wilson	hdlr.cyh_func = spa_deadman;
661283b846George.Wilson	hdlr.cyh_arg = spa;
662283b846George.Wilson	hdlr.cyh_level = CY_LOW_LEVEL;
663283b846George.Wilson
66469962b5Matthew Ahrens	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
665283b846George.Wilson
666283b846George.Wilson	/*
667283b846George.Wilson	 * This determines how often we need to check for hung I/Os after
668283b846George.Wilson	 * the cyclic has already fired. Since checking for hung I/Os is
669283b846George.Wilson	 * an expensive operation we don't want to check too frequently.
67069962b5Matthew Ahrens	 * Instead wait for 5 seconds before checking again.
671283b846George.Wilson	 */
67269962b5Matthew Ahrens	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
673283b846George.Wilson	when.cyt_when = CY_INFINITY;
674283b846George.Wilson	mutex_enter(&cpu_lock);
675283b846George.Wilson	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
676283b846George.Wilson	mutex_exit(&cpu_lock);
677283b846George.Wilson
678e914aceTim Schumacher	zfs_refcount_create(&spa->spa_refcount);
679e14bb32Jeff Bonwick	spa_config_lock_init(spa);
680fa9e406ahrens
681fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
682fa9e406ahrens
6830373e76bonwick	/*
6840373e76bonwick	 * Set the alternate root, if there is one.
6850373e76bonwick	 */
6860373e76bonwick	if (altroot) {
6870373e76bonwick		spa->spa_root = spa_strdup(altroot);
6880373e76bonwick		spa_active_count++;
6890373e76bonwick	}
6900373e76bonwick
691f78cdc3Paul Dagnelie	spa->spa_alloc_count = spa_allocators;
692f78cdc3Paul Dagnelie	spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
693f78cdc3Paul Dagnelie	    sizeof (kmutex_t), KM_SLEEP);
694f78cdc3Paul Dagnelie	spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
695f78cdc3Paul Dagnelie	    sizeof (avl_tree_t), KM_SLEEP);
696f78cdc3Paul Dagnelie	for (int i = 0; i < spa->spa_alloc_count; i++) {
697f78cdc3Paul Dagnelie		mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
698f78cdc3Paul Dagnelie		avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
699f78cdc3Paul Dagnelie		    sizeof (zio_t), offsetof(zio_t, io_alloc_node));
700f78cdc3Paul Dagnelie	}
701814dcd4Serapheim Dimitropoulos	avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
702814dcd4Serapheim Dimitropoulos	    sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
703814dcd4Serapheim Dimitropoulos	avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
704814dcd4Serapheim Dimitropoulos	    sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
705814dcd4Serapheim Dimitropoulos	list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
706814dcd4Serapheim Dimitropoulos	    offsetof(log_summary_entry_t, lse_node));
7070f7643cGeorge Wilson
708c5904d1eschrock	/*
709c5904d1eschrock	 * Every pool starts with the default cachefile
710c5904d1eschrock	 */
711c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
712c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
713c5904d1eschrock
714c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
715ef912c8Tim Haley	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
716c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
717c5904d1eschrock
7184b964adGeorge Wilson	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
7194b964adGeorge Wilson	    KM_SLEEP) == 0);
7204b964adGeorge Wilson
721ad135b5Christopher Siden	if (config != NULL) {
722ad135b5Christopher Siden		nvlist_t *features;
723ad135b5Christopher Siden
724ad135b5Christopher Siden		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
725ad135b5Christopher Siden		    &features) == 0) {
726ad135b5Christopher Siden			VERIFY(nvlist_dup(features, &spa->spa_label_features,
727ad135b5Christopher Siden			    0) == 0);
728ad135b5Christopher Siden		}
729ad135b5Christopher Siden
730468c413Tim Haley		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
731ad135b5Christopher Siden	}
732ad135b5Christopher Siden
733ad135b5Christopher Siden	if (spa->spa_label_features == NULL) {
734ad135b5Christopher Siden		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
735ad135b5Christopher Siden		    KM_SLEEP) == 0);
736ad135b5Christopher Siden	}
737468c413Tim Haley
738c3a6601Matthew Ahrens	spa->spa_iokstat = kstat_create("zfs", 0, name,
739c3a6601Matthew Ahrens	    "disk", KSTAT_TYPE_IO, 1, 0);
740c3a6601Matthew Ahrens	if (spa->spa_iokstat) {
741c3a6601Matthew Ahrens		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
742c3a6601Matthew Ahrens		kstat_install(spa->spa_iokstat);
743c3a6601Matthew Ahrens	}
744c3a6601Matthew Ahrens
74581cd5c5Matthew Ahrens	spa->spa_min_ashift = INT_MAX;
74681cd5c5Matthew Ahrens	spa->spa_max_ashift = 0;
74781cd5c5Matthew Ahrens
74843466aaMax Grossman	/*
74943466aaMax Grossman	 * As a pool is being created, treat all features as disabled by
75043466aaMax Grossman	 * setting SPA_FEATURE_DISABLED for all entries in the feature
75143466aaMax Grossman	 * refcount cache.
75243466aaMax Grossman	 */
75343466aaMax Grossman	for (int i = 0; i < SPA_FEATURES; i++) {
75443466aaMax Grossman		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
75543466aaMax Grossman	}
75643466aaMax Grossman
757e0f1c0aOlaf Faaland	list_create(&spa->spa_leaf_list, sizeof (vdev_t),
758e0f1c0aOlaf Faaland	    offsetof(vdev_t, vdev_leaf_node));
759e0f1c0aOlaf Faaland
760fa9e406ahrens	return (spa);
761fa9e406ahrens}
762fa9e406ahrens
763fa9e406ahrens/*
764fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
765fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
766fa9e406ahrens * deactivated.
767fa9e406ahrens */
768fa9e406ahrensvoid
769fa9e406ahrensspa_remove(spa_t *spa)
770fa9e406ahrens{
771c5904d1eschrock	spa_config_dirent_t *dp;
772c5904d1eschrock
773fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
774814dcd4Serapheim Dimitropoulos	ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
775e914aceTim Schumacher	ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
776fa9e406ahrens
7771195e68Mark J Musante	nvlist_free(spa->spa_config_splitting);
7781195e68Mark J Musante
779fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
780fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
781fa9e406ahrens
7820373e76bonwick	if (spa->spa_root) {
783fa9e406ahrens		spa_strfree(spa->spa_root);
7840373e76bonwick		spa_active_count--;
7850373e76bonwick	}
786fa9e406ahrens
787c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
788c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
789c5904d1eschrock		if (dp->scd_path != NULL)
790c5904d1eschrock			spa_strfree(dp->scd_path);
791c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
792c5904d1eschrock	}
793c5904d1eschrock
794f78cdc3Paul Dagnelie	for (int i = 0; i < spa->spa_alloc_count; i++) {
795f78cdc3Paul Dagnelie		avl_destroy(&spa->spa_alloc_trees[i]);
796f78cdc3Paul Dagnelie		mutex_destroy(&spa->spa_alloc_locks[i]);
797