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.
29663207aDon Brady * Copyright (c) 2017, Intel Corporation.
30fa9e406ahrens */
31fa9e406ahrens
32fa9e406ahrens#include <sys/zfs_context.h>
33fa9e406ahrens#include <sys/spa_impl.h>
34283b846George.Wilson#include <sys/spa_boot.h>
35fa9e406ahrens#include <sys/zio.h>
36fa9e406ahrens#include <sys/zio_checksum.h>
37fa9e406ahrens#include <sys/zio_compress.h>
38fa9e406ahrens#include <sys/dmu.h>
39fa9e406ahrens#include <sys/dmu_tx.h>
40fa9e406ahrens#include <sys/zap.h>
41fa9e406ahrens#include <sys/zil.h>
42fa9e406ahrens#include <sys/vdev_impl.h>
43094e47eGeorge Wilson#include <sys/vdev_initialize.h>
44084fd14Brian Behlendorf#include <sys/vdev_trim.h>
45fa9e406ahrens#include <sys/metaslab.h>
46fa9e406ahrens#include <sys/uberblock_impl.h>
47fa9e406ahrens#include <sys/txg.h>
48fa9e406ahrens#include <sys/avl.h>
49fa9e406ahrens#include <sys/unique.h>
50fa9e406ahrens#include <sys/dsl_pool.h>
51fa9e406ahrens#include <sys/dsl_dir.h>
52fa9e406ahrens#include <sys/dsl_prop.h>
533f9d6adLin Ling#include <sys/dsl_scan.h>
54fa9e406ahrens#include <sys/fs/zfs.h>
556ce0521perrin#include <sys/metaslab_impl.h>
56e14bb32Jeff Bonwick#include <sys/arc.h>
57485bbbfGeorge Wilson#include <sys/ddt.h>
5891ebeefahrens#include "zfs_prop.h"
594d7988dPaul Dagnelie#include <sys/btree.h>
6045818eeMatthew Ahrens#include <sys/zfeature.h>
61fa9e406ahrens
62fa9e406ahrens/*
63fa9e406ahrens * SPA locking
64fa9e406ahrens *
65814dcd4Serapheim Dimitropoulos * There are three basic locks for managing spa_t structures:
66fa9e406ahrens *
67fa9e406ahrens * spa_namespace_lock (global mutex)
68fa9e406ahrens *
6944cd46cbillm *	This lock must be acquired to do any of the following:
70fa9e406ahrens *
7144cd46cbillm *		- Lookup a spa_t by name
7244cd46cbillm *		- Add or remove a spa_t from the namespace
7344cd46cbillm *		- Increase spa_refcount from non-zero
7444cd46cbillm *		- Check if spa_refcount is zero
7544cd46cbillm *		- Rename a spa_t
76ea8dc4beschrock *		- add/remove/attach/detach devices
7744cd46cbillm *		- Held for the duration of create/destroy/import/export
78fa9e406ahrens *
7944cd46cbillm *	It does not need to handle recursion.  A create or destroy may
8044cd46cbillm *	reference objects (files or zvols) in other pools, but by
8144cd46cbillm *	definition they must have an existing reference, and will never need
8244cd46cbillm *	to lookup a spa_t by name.
83fa9e406ahrens *
84e914aceTim Schumacher * spa_refcount (per-spa zfs_refcount_t protected by mutex)
85fa9e406ahrens *
8644cd46cbillm *	This reference count keep track of any active users of the spa_t.  The
8744cd46cbillm *	spa_t cannot be destroyed or freed while this is non-zero.  Internally,
8844cd46cbillm *	the refcount is never really 'zero' - opening a pool implicitly keeps
89088f389ahrens *	some references in the DMU.  Internally we check against spa_minref, but
9044cd46cbillm *	present the image of a zero/non-zero value to consumers.
91fa9e406ahrens *
92e14bb32Jeff Bonwick * spa_config_lock[] (per-spa array of rwlocks)
93fa9e406ahrens *
9491ebeefahrens *	This protects the spa_t from config changes, and must be held in
9591ebeefahrens *	the following circumstances:
96fa9e406ahrens *
9744cd46cbillm *		- RW_READER to perform I/O to the spa
9844cd46cbillm *		- RW_WRITER to change the vdev config
99fa9e406ahrens *
100fa9e406ahrens * The locking order is fairly straightforward:
101fa9e406ahrens *
10244cd46cbillm *		spa_namespace_lock	->	spa_refcount
103fa9e406ahrens *
10444cd46cbillm *	The namespace lock must be acquired to increase the refcount from 0
10544cd46cbillm *	or to check if it is zero.
106fa9e406ahrens *
107e14bb32Jeff Bonwick *		spa_refcount		->	spa_config_lock[]
108fa9e406ahrens *
10944cd46cbillm *	There must be at least one valid reference on the spa_t to acquire
11044cd46cbillm *	the config lock.
111fa9e406ahrens *
112e14bb32Jeff Bonwick *		spa_namespace_lock	->	spa_config_lock[]
113fa9e406ahrens *
11444cd46cbillm *	The namespace lock must always be taken before the config lock.
115fa9e406ahrens *
116fa9e406ahrens *
117e14bb32Jeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible.
118fa9e406ahrens *
119e14bb32Jeff Bonwick * The namespace is manipulated using the following functions, all of which
120e14bb32Jeff Bonwick * require the spa_namespace_lock to be held.
121fa9e406ahrens *
12244cd46cbillm *	spa_lookup()		Lookup a spa_t by name.
123fa9e406ahrens *
12444cd46cbillm *	spa_add()		Create a new spa_t in the namespace.
125fa9e406ahrens *
12644cd46cbillm *	spa_remove()		Remove a spa_t from the namespace.  This also
12744cd46cbillm *				frees up any memory associated with the spa_t.
128fa9e406ahrens *
12944cd46cbillm *	spa_next()		Returns the next spa_t in the system, or the
13044cd46cbillm *				first if NULL is passed.
131fa9e406ahrens *
13244cd46cbillm *	spa_evict_all()		Shutdown and remove all spa_t structures in
13344cd46cbillm *				the system.
134fa9e406ahrens *
135ea8dc4beschrock *	spa_guid_exists()	Determine whether a pool/device guid exists.
136fa9e406ahrens *
137fa9e406ahrens * The spa_refcount is manipulated using the following functions:
138fa9e406ahrens *
13944cd46cbillm *	spa_open_ref()		Adds a reference to the given spa_t.  Must be
14044cd46cbillm *				called with spa_namespace_lock held if the
14144cd46cbillm *				refcount is currently zero.
142fa9e406ahrens *
14344cd46cbillm *	spa_close()		Remove a reference from the spa_t.  This will
14444cd46cbillm *				not free the spa_t or remove it from the
14544cd46cbillm *				namespace.  No locking is required.
146fa9e406ahrens *
14744cd46cbillm *	spa_refcount_zero()	Returns true if the refcount is currently
14844cd46cbillm *				zero.  Must be called with spa_namespace_lock
14944cd46cbillm *				held.
150fa9e406ahrens *
151e14bb32Jeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows:
152e14bb32Jeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
153e14bb32Jeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
154e14bb32Jeff Bonwick *
155e14bb32Jeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader.
156e14bb32Jeff Bonwick * To modify the configuration, you must hold all locks as writer.  To modify
157e14bb32Jeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline),
158e14bb32Jeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer.
159e14bb32Jeff Bonwick *
160e14bb32Jeff Bonwick * We use these distinct config locks to avoid recursive lock entry.
161e14bb32Jeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
162e14bb32Jeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps
163e14bb32Jeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
164e14bb32Jeff Bonwick *
165e14bb32Jeff Bonwick * The spa config locks cannot be normal rwlocks because we need the
166e14bb32Jeff Bonwick * ability to hand off ownership.  For example, SCL_ZIO is acquired
167e14bb32Jeff Bonwick * by the issuing thread and later released by an interrupt thread.
168e14bb32Jeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent
169e14bb32Jeff Bonwick * writer (i.e. system administrator) starvation.
170e14bb32Jeff Bonwick *
171e14bb32Jeff Bonwick * The lock acquisition rules are as follows:
172e14bb32Jeff Bonwick *
173e14bb32Jeff Bonwick * SCL_CONFIG
174e14bb32Jeff Bonwick *	Protects changes to the vdev tree topology, such as vdev
175e14bb32Jeff Bonwick *	add/remove/attach/detach.  Protects the dirty config list
176e14bb32Jeff Bonwick *	(spa_config_dirty_list) and the set of spares and l2arc devices.
177e14bb32Jeff Bonwick *
178e14bb32Jeff Bonwick * SCL_STATE
179e14bb32Jeff Bonwick *	Protects changes to pool state and vdev state, such as vdev
180e14bb32Jeff Bonwick *	online/offline/fault/degrade/clear.  Protects the dirty state list
181e14bb32Jeff Bonwick *	(spa_state_dirty_list) and global pool state (spa_state).
182e14bb32Jeff Bonwick *
183e14bb32Jeff Bonwick * SCL_ALLOC
184e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
185e14bb32Jeff Bonwick *	Held as reader by metaslab_alloc() and metaslab_claim().
186e14bb32Jeff Bonwick *
187e14bb32Jeff Bonwick * SCL_ZIO
188e14bb32Jeff Bonwick *	Held by bp-level zios (those which have no io_vd upon entry)
189e14bb32Jeff Bonwick *	to prevent changes to the vdev tree.  The bp-level zio implicitly
190e14bb32Jeff Bonwick *	protects all of its vdev child zios, which do not hold SCL_ZIO.
191e14bb32Jeff Bonwick *
192e14bb32Jeff Bonwick * SCL_FREE
193e14bb32Jeff Bonwick *	Protects changes to metaslab groups and classes.
194e14bb32Jeff Bonwick *	Held as reader by metaslab_free().  SCL_FREE is distinct from
195e14bb32Jeff Bonwick *	SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
196e14bb32Jeff Bonwick *	blocks in zio_done() while another i/o that holds either
197e14bb32Jeff Bonwick *	SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
198e14bb32Jeff Bonwick *
199e14bb32Jeff Bonwick * SCL_VDEV
200e14bb32Jeff Bonwick *	Held as reader to prevent changes to the vdev tree during trivial
201b24ab67Jeff Bonwick *	inquiries such as bp_get_dsize().  SCL_VDEV is distinct from the
202e14bb32Jeff Bonwick *	other locks, and lower than all of them, to ensure that it's safe
203e14bb32Jeff Bonwick *	to acquire regardless of caller context.
204e14bb32Jeff Bonwick *
205e14bb32Jeff Bonwick * In addition, the following rules apply:
206e14bb32Jeff Bonwick *
207e14bb32Jeff Bonwick * (a)	spa_props_lock protects pool properties, spa_config and spa_config_list.
208e14bb32Jeff Bonwick *	The lock ordering is SCL_CONFIG > spa_props_lock.
209e14bb32Jeff Bonwick *
210e14bb32Jeff Bonwick * (b)	I/O operations on leaf vdevs.  For any zio operation that takes
211e14bb32Jeff Bonwick *	an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
212e14bb32Jeff Bonwick *	or zio_write_phys() -- the caller must ensure that the config cannot
213e14bb32Jeff Bonwick *	cannot change in the interim, and that the vdev cannot be reopened.
214e14bb32Jeff Bonwick *	SCL_STATE as reader suffices for both.
215fa9e406ahrens *
216ea8dc4beschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
217fa9e406ahrens *
21844cd46cbillm *	spa_vdev_enter()	Acquire the namespace lock and the config lock
219ea8dc4beschrock *				for writing.
220fa9e406ahrens *
22144cd46cbillm *	spa_vdev_exit()		Release the config lock, wait for all I/O
22244cd46cbillm *				to complete, sync the updated configs to the
223ea8dc4beschrock *				cache, and release the namespace lock.
224fa9e406ahrens *
225e14bb32Jeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
226e14bb32Jeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
227e14bb32Jeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[].
228fa9e406ahrens */
229fa9e406ahrens
230fa9e406ahrensstatic avl_tree_t spa_namespace_avl;
231fa9e406ahrenskmutex_t spa_namespace_lock;
232fa9e406ahrensstatic kcondvar_t spa_namespace_cv;
2330373e76bonwickstatic int spa_active_count;
234416e0cdekint spa_max_replication_override = SPA_DVAS_PER_BP;
235fa9e406ahrens
23699653d4eschrockstatic kmutex_t spa_spare_lock;
23739c2341eschrockstatic avl_tree_t spa_spare_avl;
238fa94a07brendanstatic kmutex_t spa_l2cache_lock;
239fa94a07brendanstatic avl_tree_t spa_l2cache_avl;
24099653d4eschrock
241fa9e406ahrenskmem_cache_t *spa_buffer_pool;
2428ad4d6dJeff Bonwickint spa_mode_global;
243fa9e406ahrens
244fa9e406ahrens#ifdef ZFS_DEBUG
2455cabbc6Prashanth Sreenivasa/*
2465cabbc6Prashanth Sreenivasa * Everything except dprintf, spa, and indirect_remap is on by default
2475cabbc6Prashanth Sreenivasa * in debug builds.
2485cabbc6Prashanth Sreenivasa */
24921f7c81Matthew Ahrensint zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP);
250fa9e406ahrens#else
251fa9e406ahrensint zfs_flags = 0;
252fa9e406ahrens#endif
253fa9e406ahrens
2540125049ahrens/*
2550125049ahrens * zfs_recover can be set to nonzero to attempt to recover from
2560125049ahrens * otherwise-fatal errors, typically caused by on-disk corruption.  When
2570125049ahrens * set, calls to zfs_panic_recover() will turn into warning messages.
2588b36997Matthew Ahrens * This should only be used as a last resort, as it typically results
2598b36997Matthew Ahrens * in leaked space, or worse.
2600125049ahrens */
2617fd05acMatthew Ahrensboolean_t zfs_recover = B_FALSE;
2627fd05acMatthew Ahrens
2637fd05acMatthew Ahrens/*
2647fd05acMatthew Ahrens * If destroy encounters an EIO while reading metadata (e.g. indirect
2657fd05acMatthew Ahrens * blocks), space referenced by the missing metadata can not be freed.
2667fd05acMatthew Ahrens * Normally this causes the background destroy to become "stalled", as
2677fd05acMatthew Ahrens * it is unable to make forward progress.  While in this stalled state,
2687fd05acMatthew Ahrens * all remaining space to free from the error-encountering filesystem is
2697fd05acMatthew Ahrens * "temporarily leaked".  Set this flag to cause it to ignore the EIO,
2707fd05acMatthew Ahrens * permanently leak the space from indirect blocks that can not be read,
2717fd05acMatthew Ahrens * and continue to free everything else that it can.
2727fd05acMatthew Ahrens *
2737fd05acMatthew Ahrens * The default, "stalling" behavior is useful if the storage partially
2747fd05acMatthew Ahrens * fails (i.e. some but not all i/os fail), and then later recovers.  In
2757fd05acMatthew Ahrens * this case, we will be able to continue pool operations while it is
2767fd05acMatthew Ahrens * partially failed, and when it recovers, we can continue to free the
2777fd05acMatthew Ahrens * space, with no leaks.  However, note that this case is actually
2787fd05acMatthew Ahrens * fairly rare.
2797fd05acMatthew Ahrens *
2807fd05acMatthew Ahrens * Typically pools either (a) fail completely (but perhaps temporarily,
2817fd05acMatthew Ahrens * e.g. a top-level vdev going offline), or (b) have localized,
2827fd05acMatthew Ahrens * permanent errors (e.g. disk returns the wrong data due to bit flip or
2837fd05acMatthew Ahrens * firmware bug).  In case (a), this setting does not matter because the
2847fd05acMatthew Ahrens * pool will be suspended and the sync thread will not be able to make
2857fd05acMatthew Ahrens * forward progress regardless.  In case (b), because the error is
2867fd05acMatthew Ahrens * permanent, the best we can do is leak the minimum amount of space,
2877fd05acMatthew Ahrens * which is what setting this flag will do.  Therefore, it is reasonable
2887fd05acMatthew Ahrens * for this flag to normally be set, but we chose the more conservative
2897fd05acMatthew Ahrens * approach of not setting it, so that there is no possibility of
2907fd05acMatthew Ahrens * leaking space in the "partial temporary" failure case.
2917fd05acMatthew Ahrens */
2927fd05acMatthew Ahrensboolean_t zfs_free_leak_on_eio = B_FALSE;
2930125049ahrens
29469962b5Matthew Ahrens/*
29569962b5Matthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is
29669962b5Matthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the
29769962b5Matthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
29869962b5Matthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that
29969962b5Matthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
30069962b5Matthew Ahrens * in a system panic.
30169962b5Matthew Ahrens */
30269962b5Matthew Ahrensuint64_t zfs_deadman_synctime_ms = 1000000ULL;
303283b846George.Wilson
304283b846George.Wilson/*
30569962b5Matthew Ahrens * Check time in milliseconds. This defines the frequency at which we check
30669962b5Matthew Ahrens * for hung I/O.
307283b846George.Wilson */
30869962b5Matthew Ahrensuint64_t zfs_deadman_checktime_ms = 5000ULL;
309283b846George.Wilson
310283b846George.Wilson/*
311283b846George.Wilson * Override the zfs deadman behavior via /etc/system. By default the
312283b846George.Wilson * deadman is enabled except on VMware and sparc deployments.
313283b846George.Wilson */
314283b846George.Wilsonint zfs_deadman_enabled = -1;
315283b846George.Wilson
31669962b5Matthew Ahrens/*
31769962b5Matthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which
31869962b5Matthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
31969962b5Matthew Ahrens * times the size; so just assume that.  Add to this the fact that
32069962b5Matthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because
32169962b5Matthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync().  All together,
32269962b5Matthew Ahrens * the worst case is:
32369962b5Matthew Ahrens *     (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
32469962b5Matthew Ahrens */
32569962b5Matthew Ahrensint spa_asize_inflation = 24;
326fa9e406ahrens
327fa9e406ahrens/*
3287d46dc6Matthew Ahrens * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3297d46dc6Matthew Ahrens * the pool to be consumed.  This ensures that we don't run the pool
3307d46dc6Matthew Ahrens * completely out of space, due to unaccounted changes (e.g. to the MOS).
3317d46dc6Matthew Ahrens * It also limits the worst-case time to allocate space.  If we have
3327d46dc6Matthew Ahrens * less than this amount of free space, most ZPL operations (e.g. write,
3337d46dc6Matthew Ahrens * create) will return ENOSPC.
3347d46dc6Matthew Ahrens *
3357d46dc6Matthew Ahrens * Certain operations (e.g. file removal, most administrative actions) can
3367d46dc6Matthew Ahrens * use half the slop space.  They will only return ENOSPC if less than half
3377d46dc6Matthew Ahrens * the slop space is free.  Typically, once the pool has less than the slop
3387d46dc6Matthew Ahrens * space free, the user will use these operations to free up space in the pool.
3397d46dc6Matthew Ahrens * These are the operations that call dsl_pool_adjustedsize() with the netfree
3407d46dc6Matthew Ahrens * argument set to TRUE.
3417d46dc6Matthew Ahrens *
3428671400Serapheim Dimitropoulos * Operations that are almost guaranteed to free up space in the absence of
3438671400Serapheim Dimitropoulos * a pool checkpoint can use up to three quarters of the slop space
3448671400Serapheim Dimitropoulos * (e.g zfs destroy).
3458671400Serapheim Dimitropoulos *
3467d46dc6Matthew Ahrens * A very restricted set of operations are always permitted, regardless of
3477d46dc6Matthew Ahrens * the amount of free space.  These are the operations that call
3488671400Serapheim Dimitropoulos * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
3498671400Serapheim Dimitropoulos * increase in the amount of space used, it is possible to run the pool
3508671400Serapheim Dimitropoulos * completely out of space, causing it to be permanently read-only.
3517d46dc6Matthew Ahrens *
3524b5c8e9Matthew Ahrens * Note that on very small pools, the slop space will be larger than
3534b5c8e9Matthew Ahrens * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
3544b5c8e9Matthew Ahrens * but we never allow it to be more than half the pool size.
3554b5c8e9Matthew Ahrens *
3567d46dc6Matthew Ahrens * See also the comments in zfs_space_check_t.
3577d46dc6Matthew Ahrens */
3587d46dc6Matthew Ahrensint spa_slop_shift = 5;
3594b5c8e9Matthew Ahrensuint64_t spa_min_slop = 128 * 1024 * 1024;
3607d46dc6Matthew Ahrens
361f78cdc3Paul Dagnelieint spa_allocators = 4;
362f78cdc3Paul Dagnelie
3633ee8c80Pavel Zakharov/*PRINTFLIKE2*/
3643ee8c80Pavel Zakharovvoid
3653ee8c80Pavel Zakharovspa_load_failed(spa_t *spa, const char *fmt, ...)
3663ee8c80Pavel Zakharov{
3673ee8c80Pavel Zakharov	va_list adx;
3683ee8c80Pavel Zakharov	char buf[256];
3693ee8c80Pavel Zakharov
3703ee8c80Pavel Zakharov	va_start(adx, fmt);
3713ee8c80Pavel Zakharov	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3723ee8c80Pavel Zakharov	va_end(adx);
3733ee8c80Pavel Zakharov
3746f79381Pavel Zakharov	zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
3756f79381Pavel Zakharov	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3763ee8c80Pavel Zakharov}
3773ee8c80Pavel Zakharov
3783ee8c80Pavel Zakharov/*PRINTFLIKE2*/
3793ee8c80Pavel Zakharovvoid
3803ee8c80Pavel Zakharovspa_load_note(spa_t *spa, const char *fmt, ...)
3813ee8c80Pavel Zakharov{
3823ee8c80Pavel Zakharov	va_list adx;
3833ee8c80Pavel Zakharov	char buf[256];
3843ee8c80Pavel Zakharov
3853ee8c80Pavel Zakharov	va_start(adx, fmt);
3863ee8c80Pavel Zakharov	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
3873ee8c80Pavel Zakharov	va_end(adx);
3883ee8c80Pavel Zakharov
3896f79381Pavel Zakharov	zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
3906f79381Pavel Zakharov	    spa->spa_trust_config ? "trusted" : "untrusted", buf);
3913ee8c80Pavel Zakharov}
3923ee8c80Pavel Zakharov
3937d46dc6Matthew Ahrens/*
394663207aDon Brady * By default dedup and user data indirects land in the special class
395663207aDon Brady */
396663207aDon Bradyint zfs_ddt_data_is_special = B_TRUE;
397663207aDon Bradyint zfs_user_indirect_is_special = B_TRUE;
398663207aDon Brady
399663207aDon Brady/*
400663207aDon Brady * The percentage of special class final space reserved for metadata only.
401663207aDon Brady * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
402663207aDon Brady * let metadata into the class.
403663207aDon Brady */
404663207aDon Bradyint zfs_special_class_metadata_reserve_pct = 25;
405663207aDon Brady
406663207aDon Brady/*
407fa9e406ahrens * ==========================================================================
408e05725bbonwick * SPA config locking
409e05725bbonwick * ==========================================================================
410e05725bbonwick */
411e05725bbonwickstatic void
412e14bb32Jeff Bonwickspa_config_lock_init(spa_t *spa)
413e14bb32Jeff Bonwick{
414e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
415e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
416e14bb32Jeff Bonwick		mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
417e14bb32Jeff Bonwick		cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
418e914aceTim Schumacher		zfs_refcount_create_untracked(&scl->scl_count);
419e14bb32Jeff Bonwick		scl->scl_writer = NULL;
420e14bb32Jeff Bonwick		scl->scl_write_wanted = 0;
421e14bb32Jeff Bonwick	}
422e05725bbonwick}
423e05725bbonwick
424e05725bbonwickstatic void
425e14bb32Jeff Bonwickspa_config_lock_destroy(spa_t *spa)
426e14bb32Jeff Bonwick{
427e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
428e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
429e14bb32Jeff Bonwick		mutex_destroy(&scl->scl_lock);
430e14bb32Jeff Bonwick		cv_destroy(&scl->scl_cv);
431e914aceTim Schumacher		zfs_refcount_destroy(&scl->scl_count);
432e14bb32Jeff Bonwick		ASSERT(scl->scl_writer == NULL);
433e14bb32Jeff Bonwick		ASSERT(scl->scl_write_wanted == 0);
434e14bb32Jeff Bonwick	}
435e14bb32Jeff Bonwick}
436e14bb32Jeff Bonwick
437e14bb32Jeff Bonwickint
438e14bb32Jeff Bonwickspa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
439e05725bbonwick{
440e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
441e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
442e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
443e14bb32Jeff Bonwick			continue;
444e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
445e14bb32Jeff Bonwick		if (rw == RW_READER) {
446e14bb32Jeff Bonwick			if (scl->scl_writer || scl->scl_write_wanted) {
447e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
448e495b6eSaso Kiselkov				spa_config_exit(spa, locks & ((1 << i) - 1),
449e495b6eSaso Kiselkov				    tag);
450e14bb32Jeff Bonwick				return (0);
451e14bb32Jeff Bonwick			}
452e14bb32Jeff Bonwick		} else {
453e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
454e914aceTim Schumacher			if (!zfs_refcount_is_zero(&scl->scl_count)) {
455e14bb32Jeff Bonwick				mutex_exit(&scl->scl_lock);
456e495b6eSaso Kiselkov				spa_config_exit(spa, locks & ((1 << i) - 1),
457e495b6eSaso Kiselkov				    tag);
458e14bb32Jeff Bonwick				return (0);
459e14bb32Jeff Bonwick			}
460e14bb32Jeff Bonwick			scl->scl_writer = curthread;
461e14bb32Jeff Bonwick		}
462e914aceTim Schumacher		(void) zfs_refcount_add(&scl->scl_count, tag);
463e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
464e14bb32Jeff Bonwick	}
465e14bb32Jeff Bonwick	return (1);
466e05725bbonwick}
467e05725bbonwick
468e05725bbonwickvoid
469e14bb32Jeff Bonwickspa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
470e05725bbonwick{
471f64c0e3Eric Taylor	int wlocks_held = 0;
472f64c0e3Eric Taylor
4733b2aab1Matthew Ahrens	ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4743b2aab1Matthew Ahrens
475e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
476e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
477f64c0e3Eric Taylor		if (scl->scl_writer == curthread)
478f64c0e3Eric Taylor			wlocks_held |= (1 << i);
479e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
480e14bb32Jeff Bonwick			continue;
481e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
482e14bb32Jeff Bonwick		if (rw == RW_READER) {
483e14bb32Jeff Bonwick			while (scl->scl_writer || scl->scl_write_wanted) {
484e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
485e14bb32Jeff Bonwick			}
486e14bb32Jeff Bonwick		} else {
487e14bb32Jeff Bonwick			ASSERT(scl->scl_writer != curthread);
488e914aceTim Schumacher			while (!zfs_refcount_is_zero(&scl->scl_count)) {
489e14bb32Jeff Bonwick				scl->scl_write_wanted++;
490e14bb32Jeff Bonwick				cv_wait(&scl->scl_cv, &scl->scl_lock);
491e14bb32Jeff Bonwick				scl->scl_write_wanted--;
492e14bb32Jeff Bonwick			}
493e14bb32Jeff Bonwick			scl->scl_writer = curthread;
494e14bb32Jeff Bonwick		}
495e914aceTim Schumacher		(void) zfs_refcount_add(&scl->scl_count, tag);
496e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
497e05725bbonwick	}
4985cabbc6Prashanth Sreenivasa	ASSERT3U(wlocks_held, <=, locks);
499e05725bbonwick}
500e05725bbonwick
501e05725bbonwickvoid
502e14bb32Jeff Bonwickspa_config_exit(spa_t *spa, int locks, void *tag)
503e05725bbonwick{
504e14bb32Jeff Bonwick	for (int i = SCL_LOCKS - 1; i >= 0; i--) {
505e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
506e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
507e14bb32Jeff Bonwick			continue;
508e14bb32Jeff Bonwick		mutex_enter(&scl->scl_lock);
509e914aceTim Schumacher		ASSERT(!zfs_refcount_is_zero(&scl->scl_count));
510e914aceTim Schumacher		if (zfs_refcount_remove(&scl->scl_count, tag) == 0) {
511e14bb32Jeff Bonwick			ASSERT(scl->scl_writer == NULL ||
512e14bb32Jeff Bonwick			    scl->scl_writer == curthread);
513e14bb32Jeff Bonwick			scl->scl_writer = NULL;	/* OK in either case */
514e14bb32Jeff Bonwick			cv_broadcast(&scl->scl_cv);
515e14bb32Jeff Bonwick		}
516e14bb32Jeff Bonwick		mutex_exit(&scl->scl_lock);
517e05725bbonwick	}
518e05725bbonwick}
519e05725bbonwick
520e14bb32Jeff Bonwickint
521e14bb32Jeff Bonwickspa_config_held(spa_t *spa, int locks, krw_t rw)
522e05725bbonwick{
523e14bb32Jeff Bonwick	int locks_held = 0;
524e05725bbonwick
525e14bb32Jeff Bonwick	for (int i = 0; i < SCL_LOCKS; i++) {
526e14bb32Jeff Bonwick		spa_config_lock_t *scl = &spa->spa_config_lock[i];
527e14bb32Jeff Bonwick		if (!(locks & (1 << i)))
528e14bb32Jeff Bonwick			continue;
529e914aceTim Schumacher		if ((rw == RW_READER &&
530e914aceTim Schumacher		    !zfs_refcount_is_zero(&scl->scl_count)) ||
531e14bb32Jeff Bonwick		    (rw == RW_WRITER && scl->scl_writer == curthread))
532e14bb32Jeff Bonwick			locks_held |= 1 << i;
533e14bb32Jeff Bonwick	}
534e14bb32Jeff Bonwick
535e14bb32Jeff Bonwick	return (locks_held);
536e05725bbonwick}
537e05725bbonwick
538e05725bbonwick/*
539e05725bbonwick * ==========================================================================
540fa9e406ahrens * SPA namespace functions
541fa9e406ahrens * ==========================================================================
542fa9e406ahrens */
543fa9e406ahrens
544fa9e406ahrens/*
545fa9e406ahrens * Lookup the named spa_t in the AVL tree.  The spa_namespace_lock must be held.
546fa9e406ahrens * Returns NULL if no matching spa_t is found.
547fa9e406ahrens */
548fa9e406ahrensspa_t *
549fa9e406ahrensspa_lookup(const char *name)
550fa9e406ahrens{
551e14bb32Jeff Bonwick	static spa_t search;	/* spa_t is large; don't allocate on stack */
552e14bb32Jeff Bonwick	spa_t *spa;
553fa9e406ahrens	avl_index_t where;
55440feaa9ahrens	char *cp;
555fa9e406ahrens
556fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
557fa9e406ahrens
5583b2aab1Matthew Ahrens	(void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
5593b2aab1Matthew Ahrens
56040feaa9ahrens	/*
56140feaa9ahrens	 * If it's a full dataset name, figure out the pool name and
56240feaa9ahrens	 * just use that.
56340feaa9ahrens	 */
56478f1710Matthew Ahrens	cp = strpbrk(search.spa_name, "/@#");
5653b2aab1Matthew Ahrens	if (cp != NULL)
56640feaa9ahrens		*cp = '\0';
56740feaa9ahrens
568fa9e406ahrens	spa = avl_find(&spa_namespace_avl, &search, &where);
569fa9e406ahrens
570fa9e406ahrens	return (spa);
571fa9e406ahrens}
572fa9e406ahrens
573fa9e406ahrens/*
574283b846George.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
575283b846George.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
576283b846George.Wilson * looking for potentially hung I/Os.
577283b846George.Wilson */
578283b846George.Wilsonvoid
579283b846George.Wilsonspa_deadman(void *arg)
580283b846George.Wilson{
581283b846George.Wilson	spa_t *spa = arg;
582283b846George.Wilson
5830713e23George Wilson	/*
5840713e23George Wilson	 * Disable the deadman timer if the pool is suspended.
5850713e23George Wilson	 */
5860713e23George Wilson	if (spa_suspended(spa)) {
5870713e23George Wilson		VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
5880713e23George Wilson		return;
5890713e23George Wilson	}
5900713e23George Wilson
591283b846George.Wilson	zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
592283b846George.Wilson	    (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
593283b846George.Wilson	    ++spa->spa_deadman_calls);
594283b846George.Wilson	if (zfs_deadman_enabled)
595283b846George.Wilson		vdev_deadman(spa->spa_root_vdev);
596283b846George.Wilson}
597283b846George.Wilson
598814dcd4Serapheim Dimitropoulosint
599814dcd4Serapheim Dimitropoulosspa_log_sm_sort_by_txg(const void *va, const void *vb)
600814dcd4Serapheim Dimitropoulos{
601814dcd4Serapheim Dimitropoulos	const spa_log_sm_t *a = va;
602814dcd4Serapheim Dimitropoulos	const spa_log_sm_t *b = vb;
603814dcd4Serapheim Dimitropoulos
6044d7988dPaul Dagnelie	return (TREE_CMP(a->sls_txg, b->sls_txg));
605814dcd4Serapheim Dimitropoulos}
606814dcd4Serapheim Dimitropoulos
607283b846George.Wilson/*
608fa9e406ahrens * Create an uninitialized spa_t with the given name.  Requires
609fa9e406ahrens * spa_namespace_lock.  The caller must ensure that the spa_t doesn't already
610fa9e406ahrens * exist by calling spa_lookup() first.
611fa9e406ahrens */
612fa9e406ahrensspa_t *
613468c413Tim Haleyspa_add(const char *name, nvlist_t *config, const char *altroot)
614fa9e406ahrens{
615fa9e406ahrens	spa_t *spa;
616c5904d1eschrock	spa_config_dirent_t *dp;
617283b846George.Wilson	cyc_handler_t hdlr;
618283b846George.Wilson	cyc_time_t when;
619fa9e406ahrens
620fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
621fa9e406ahrens
622fa9e406ahrens	spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
623fa9e406ahrens
624c25056dgw	mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
625c25056dgw	mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
62635a5a35Jonathan Adams	mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
627bc9014eJustin Gibbs	mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
628c25056dgw	mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
62935a5a35Jonathan Adams	mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
630c25056dgw	mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
63145818eeMatthew Ahrens	mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
63235a5a35Jonathan Adams	mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
633a152156Jeff Bonwick	mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
634a152156Jeff Bonwick	mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
635c3a6601Matthew Ahrens	mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
636814dcd4Serapheim Dimitropoulos	mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
637c25056dgw
638c25056dgw	cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
639bc9014eJustin Gibbs	cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
64035a5a35Jonathan Adams	cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
641c25056dgw	cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
642e14bb32Jeff Bonwick	cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
643c25056dgw
644b24ab67Jeff Bonwick	for (int t = 0; t < TXG_SIZE; t++)
645cde58dbMatthew Ahrens		bplist_create(&spa->spa_free_bplist[t]);
646b24ab67Jeff Bonwick
647e14bb32Jeff Bonwick	(void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
648fa9e406ahrens	spa->spa_state = POOL_STATE_UNINITIALIZED;
649fa9e406ahrens	spa->spa_freeze_txg = UINT64_MAX;
6500373e76bonwick	spa->spa_final_txg = UINT64_MAX;
651468c413Tim Haley	spa->spa_load_max_txg = UINT64_MAX;
65235a5a35Jonathan Adams	spa->spa_proc = &p0;
65335a5a35Jonathan Adams	spa->spa_proc_state = SPA_PROC_NONE;
6546f79381Pavel Zakharov	spa->spa_trust_config = B_TRUE;
655fa9e406ahrens
656283b846George.Wilson	hdlr.cyh_func = spa_deadman;
657283b846George.Wilson	hdlr.cyh_arg = spa;
658283b846George.Wilson	hdlr.cyh_level = CY_LOW_LEVEL;
659283b846George.Wilson
66069962b5Matthew Ahrens	spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
661283b846George.Wilson
662283b846George.Wilson	/*
663283b846George.Wilson	 * This determines how often we need to check for hung I/Os after
664283b846George.Wilson	 * the cyclic has already fired. Since checking for hung I/Os is
665283b846George.Wilson	 * an expensive operation we don't want to check too frequently.
66669962b5Matthew Ahrens	 * Instead wait for 5 seconds before checking again.
667283b846George.Wilson	 */
66869962b5Matthew Ahrens	when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
669283b846George.Wilson	when.cyt_when = CY_INFINITY;
670283b846George.Wilson	mutex_enter(&cpu_lock);
671283b846George.Wilson	spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
672283b846George.Wilson	mutex_exit(&cpu_lock);
673283b846George.Wilson
674e914aceTim Schumacher	zfs_refcount_create(&spa->spa_refcount);
675e14bb32Jeff Bonwick	spa_config_lock_init(spa);
676fa9e406ahrens
677fa9e406ahrens	avl_add(&spa_namespace_avl, spa);
678fa9e406ahrens
6790373e76bonwick	/*
6800373e76bonwick	 * Set the alternate root, if there is one.
6810373e76bonwick	 */
6820373e76bonwick	if (altroot) {
6830373e76bonwick		spa->spa_root = spa_strdup(altroot);
6840373e76bonwick		spa_active_count++;
6850373e76bonwick	}
6860373e76bonwick
687f78cdc3Paul Dagnelie	spa->spa_alloc_count = spa_allocators;
688f78cdc3Paul Dagnelie	spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
689f78cdc3Paul Dagnelie	    sizeof (kmutex_t), KM_SLEEP);
690f78cdc3Paul Dagnelie	spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
691f78cdc3Paul Dagnelie	    sizeof (avl_tree_t), KM_SLEEP);
692f78cdc3Paul Dagnelie	for (int i = 0; i < spa->spa_alloc_count; i++) {
693f78cdc3Paul Dagnelie		mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
694f78cdc3Paul Dagnelie		avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
695f78cdc3Paul Dagnelie		    sizeof (zio_t), offsetof(zio_t, io_alloc_node));
696f78cdc3Paul Dagnelie	}
697814dcd4Serapheim Dimitropoulos	avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
698814dcd4Serapheim Dimitropoulos	    sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
699814dcd4Serapheim Dimitropoulos	avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
700814dcd4Serapheim Dimitropoulos	    sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
701814dcd4Serapheim Dimitropoulos	list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
702814dcd4Serapheim Dimitropoulos	    offsetof(log_summary_entry_t, lse_node));
7030f7643cGeorge Wilson
704c5904d1eschrock	/*
705c5904d1eschrock	 * Every pool starts with the default cachefile
706c5904d1eschrock	 */
707c5904d1eschrock	list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
708c5904d1eschrock	    offsetof(spa_config_dirent_t, scd_link));
709c5904d1eschrock
710c5904d1eschrock	dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
711ef912c8Tim Haley	dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
712c5904d1eschrock	list_insert_head(&spa->spa_config_list, dp);
713c5904d1eschrock
7144b964adGeorge Wilson	VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
7154b964adGeorge Wilson	    KM_SLEEP) == 0);
7164b964adGeorge Wilson
717ad135b5Christopher Siden	if (config != NULL) {
718ad135b5Christopher Siden		nvlist_t *features;
719ad135b5Christopher Siden
720ad135b5Christopher Siden		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
721ad135b5Christopher Siden		    &features) == 0) {
722ad135b5Christopher Siden			VERIFY(nvlist_dup(features, &spa->spa_label_features,
723ad135b5Christopher Siden			    0) == 0);
724ad135b5Christopher Siden		}
725ad135b5Christopher Siden
726468c413Tim Haley		VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
727ad135b5Christopher Siden	}
728ad135b5Christopher Siden
729ad135b5Christopher Siden	if (spa->spa_label_features == NULL) {
730ad135b5Christopher Siden		VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
731ad135b5Christopher Siden		    KM_SLEEP) == 0);
732ad135b5Christopher Siden	}
733468c413Tim Haley
734c3a6601Matthew Ahrens	spa->spa_iokstat = kstat_create("zfs", 0, name,
735c3a6601Matthew Ahrens	    "disk", KSTAT_TYPE_IO, 1, 0);
736c3a6601Matthew Ahrens	if (spa->spa_iokstat) {
737c3a6601Matthew Ahrens		spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
738c3a6601Matthew Ahrens		kstat_install(spa->spa_iokstat);
739c3a6601Matthew Ahrens	}
740c3a6601Matthew Ahrens
74181cd5c5Matthew Ahrens	spa->spa_min_ashift = INT_MAX;
74281cd5c5Matthew Ahrens	spa->spa_max_ashift = 0;
74381cd5c5Matthew Ahrens
74443466aaMax Grossman	/*
74543466aaMax Grossman	 * As a pool is being created, treat all features as disabled by
74643466aaMax Grossman	 * setting SPA_FEATURE_DISABLED for all entries in the feature
74743466aaMax Grossman	 * refcount cache.
74843466aaMax Grossman	 */
74943466aaMax Grossman	for (int i = 0; i < SPA_FEATURES; i++) {
75043466aaMax Grossman		spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
75143466aaMax Grossman	}
75243466aaMax Grossman
753e0f1c0aOlaf Faaland	list_create(&spa->spa_leaf_list, sizeof (vdev_t),
754e0f1c0aOlaf Faaland	    offsetof(vdev_t, vdev_leaf_node));
755e0f1c0aOlaf Faaland
756fa9e406ahrens	return (spa);
757fa9e406ahrens}
758fa9e406ahrens
759fa9e406ahrens/*
760fa9e406ahrens * Removes a spa_t from the namespace, freeing up any memory used.  Requires
761fa9e406ahrens * spa_namespace_lock.  This is called only after the spa_t has been closed and
762fa9e406ahrens * deactivated.
763fa9e406ahrens */
764fa9e406ahrensvoid
765fa9e406ahrensspa_remove(spa_t *spa)
766fa9e406ahrens{
767c5904d1eschrock	spa_config_dirent_t *dp;
768c5904d1eschrock
769fa9e406ahrens	ASSERT(MUTEX_HELD(&spa_namespace_lock));
770814dcd4Serapheim Dimitropoulos	ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
771e914aceTim Schumacher	ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
772fa9e406ahrens
7731195e68Mark J Musante	nvlist_free(spa->spa_config_splitting);
7741195e68Mark J Musante
775fa9e406ahrens	avl_remove(&spa_namespace_avl, spa);
776fa9e406ahrens	cv_broadcast(&spa_namespace_cv);
777fa9e406ahrens
7780373e76bonwick	if (spa->spa_root) {
779fa9e406ahrens		spa_strfree(spa->spa_root);
7800373e76bonwick		spa_active_count--;
7810373e76bonwick	}
782fa9e406ahrens
783c5904d1eschrock	while ((dp = list_head(&spa->spa_config_list)) != NULL) {
784c5904d1eschrock		list_remove(&spa->spa_config_list, dp);
785c5904d1eschrock		if (dp->scd_path != NULL)
786c5904d1eschrock			spa_strfree(dp->scd_path);
787c5904d1eschrock		kmem_free(dp, sizeof (spa_config_dirent_t));
788c5904d1eschrock	}
789c5904d1eschrock
790f78cdc3Paul Dagnelie	for (int i = 0; i < spa->spa_alloc_count; i++) {
791f78cdc3Paul Dagnelie		avl_destroy(&spa->spa_alloc_trees[i]);
792f78cdc3Paul Dagnelie		mutex_destroy(&spa->spa_alloc_locks[i]);
793f78cdc3Paul Dagnelie	}
794f78cdc3Paul Dagnelie	kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count *
795f78cdc3Paul Dagnelie	    sizeof (kmutex_t));
796f78cdc3Paul Dagnelie	kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count *
797f78cdc3Paul Dagnelie	    sizeof (avl_tree_t));
798f78cdc3Paul Dagnelie
799814dcd4Serapheim Dimitropoulos	avl_destroy(&spa->