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.
23814dcd43SSerapheim Dimitropoulos * Copyright (c) 2011, 2019 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.
29e830fb12SKody A Kantor * Copyright 2019 Joyent, Inc.
30663207adSDon Brady * Copyright (c) 2017, Intel Corporation.
31dd50e0ccSTony Hutter * Copyright 2020 Joyent, Inc.
32*b4fb0039SJoshua M. Clulow * Copyright 2022 Oxide Computer Company
33fa9e4066Sahrens */
34fa9e4066Sahrens
35fa9e4066Sahrens #include <sys/zfs_context.h>
36fa9e4066Sahrens #include <sys/spa_impl.h>
37283b8460SGeorge.Wilson #include <sys/spa_boot.h>
38fa9e4066Sahrens #include <sys/zio.h>
39fa9e4066Sahrens #include <sys/zio_checksum.h>
40fa9e4066Sahrens #include <sys/zio_compress.h>
41fa9e4066Sahrens #include <sys/dmu.h>
42fa9e4066Sahrens #include <sys/dmu_tx.h>
43fa9e4066Sahrens #include <sys/zap.h>
44fa9e4066Sahrens #include <sys/zil.h>
45fa9e4066Sahrens #include <sys/vdev_impl.h>
46094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
47084fd14fSBrian Behlendorf #include <sys/vdev_trim.h>
48e86372a0SGvozden Neskovic #include <sys/vdev_raidz.h>
49fa9e4066Sahrens #include <sys/metaslab.h>
50fa9e4066Sahrens #include <sys/uberblock_impl.h>
51fa9e4066Sahrens #include <sys/txg.h>
52fa9e4066Sahrens #include <sys/avl.h>
53fa9e4066Sahrens #include <sys/unique.h>
54fa9e4066Sahrens #include <sys/dsl_pool.h>
55fa9e4066Sahrens #include <sys/dsl_dir.h>
56fa9e4066Sahrens #include <sys/dsl_prop.h>
573f9d6ad7SLin Ling #include <sys/dsl_scan.h>
58fa9e4066Sahrens #include <sys/fs/zfs.h>
596ce0521aSperrin #include <sys/metaslab_impl.h>
60e14bb325SJeff Bonwick #include <sys/arc.h>
61485bbbf5SGeorge Wilson #include <sys/ddt.h>
6291ebeef5Sahrens #include "zfs_prop.h"
634d7988d6SPaul Dagnelie #include <sys/btree.h>
6445818ee1SMatthew Ahrens #include <sys/zfeature.h>
65fa9e4066Sahrens
66fa9e4066Sahrens /*
67fa9e4066Sahrens * SPA locking
68fa9e4066Sahrens *
69814dcd43SSerapheim Dimitropoulos * There are three basic locks for managing spa_t structures:
70fa9e4066Sahrens *
71fa9e4066Sahrens * spa_namespace_lock (global mutex)
72fa9e4066Sahrens *
7344cd46caSbillm * This lock must be acquired to do any of the following:
74fa9e4066Sahrens *
7544cd46caSbillm * - Lookup a spa_t by name
7644cd46caSbillm * - Add or remove a spa_t from the namespace
7744cd46caSbillm * - Increase spa_refcount from non-zero
7844cd46caSbillm * - Check if spa_refcount is zero
7944cd46caSbillm * - Rename a spa_t
80ea8dc4b6Seschrock * - add/remove/attach/detach devices
8144cd46caSbillm * - Held for the duration of create/destroy/import/export
82fa9e4066Sahrens *
8344cd46caSbillm * It does not need to handle recursion. A create or destroy may
8444cd46caSbillm * reference objects (files or zvols) in other pools, but by
8544cd46caSbillm * definition they must have an existing reference, and will never need
8644cd46caSbillm * to lookup a spa_t by name.
87fa9e4066Sahrens *
88e914ace2STim Schumacher * spa_refcount (per-spa zfs_refcount_t protected by mutex)
89fa9e4066Sahrens *
9044cd46caSbillm * This reference count keep track of any active users of the spa_t. The
9144cd46caSbillm * spa_t cannot be destroyed or freed while this is non-zero. Internally,
9244cd46caSbillm * the refcount is never really 'zero' - opening a pool implicitly keeps
93088f3894Sahrens * some references in the DMU. Internally we check against spa_minref, but
9444cd46caSbillm * present the image of a zero/non-zero value to consumers.
95fa9e4066Sahrens *
96e14bb325SJeff Bonwick * spa_config_lock[] (per-spa array of rwlocks)
97fa9e4066Sahrens *
9891ebeef5Sahrens * This protects the spa_t from config changes, and must be held in
9991ebeef5Sahrens * the following circumstances:
100fa9e4066Sahrens *
10144cd46caSbillm * - RW_READER to perform I/O to the spa
10244cd46caSbillm * - RW_WRITER to change the vdev config
103fa9e4066Sahrens *
104fa9e4066Sahrens * The locking order is fairly straightforward:
105fa9e4066Sahrens *
10644cd46caSbillm * spa_namespace_lock -> spa_refcount
107fa9e4066Sahrens *
10844cd46caSbillm * The namespace lock must be acquired to increase the refcount from 0
10944cd46caSbillm * or to check if it is zero.
110fa9e4066Sahrens *
111e14bb325SJeff Bonwick * spa_refcount -> spa_config_lock[]
112fa9e4066Sahrens *
11344cd46caSbillm * There must be at least one valid reference on the spa_t to acquire
11444cd46caSbillm * the config lock.
115fa9e4066Sahrens *
116e14bb325SJeff Bonwick * spa_namespace_lock -> spa_config_lock[]
117fa9e4066Sahrens *
11844cd46caSbillm * The namespace lock must always be taken before the config lock.
119fa9e4066Sahrens *
120fa9e4066Sahrens *
121e14bb325SJeff Bonwick * The spa_namespace_lock can be acquired directly and is globally visible.
122fa9e4066Sahrens *
123e14bb325SJeff Bonwick * The namespace is manipulated using the following functions, all of which
124e14bb325SJeff Bonwick * require the spa_namespace_lock to be held.
125fa9e4066Sahrens *
12644cd46caSbillm * spa_lookup() Lookup a spa_t by name.
127fa9e4066Sahrens *
12844cd46caSbillm * spa_add() Create a new spa_t in the namespace.
129fa9e4066Sahrens *
13044cd46caSbillm * spa_remove() Remove a spa_t from the namespace. This also
13144cd46caSbillm * frees up any memory associated with the spa_t.
132fa9e4066Sahrens *
13344cd46caSbillm * spa_next() Returns the next spa_t in the system, or the
13444cd46caSbillm * first if NULL is passed.
135fa9e4066Sahrens *
13644cd46caSbillm * spa_evict_all() Shutdown and remove all spa_t structures in
13744cd46caSbillm * the system.
138fa9e4066Sahrens *
139ea8dc4b6Seschrock * spa_guid_exists() Determine whether a pool/device guid exists.
140fa9e4066Sahrens *
141fa9e4066Sahrens * The spa_refcount is manipulated using the following functions:
142fa9e4066Sahrens *
14344cd46caSbillm * spa_open_ref() Adds a reference to the given spa_t. Must be
14444cd46caSbillm * called with spa_namespace_lock held if the
14544cd46caSbillm * refcount is currently zero.
146fa9e4066Sahrens *
14744cd46caSbillm * spa_close() Remove a reference from the spa_t. This will
14844cd46caSbillm * not free the spa_t or remove it from the
14944cd46caSbillm * namespace. No locking is required.
150fa9e4066Sahrens *
15144cd46caSbillm * spa_refcount_zero() Returns true if the refcount is currently
15244cd46caSbillm * zero. Must be called with spa_namespace_lock
15344cd46caSbillm * held.
154fa9e4066Sahrens *
155e14bb325SJeff Bonwick * The spa_config_lock[] is an array of rwlocks, ordered as follows:
156e14bb325SJeff Bonwick * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
157e14bb325SJeff Bonwick * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
158e14bb325SJeff Bonwick *
159e14bb325SJeff Bonwick * To read the configuration, it suffices to hold one of these locks as reader.
160e14bb325SJeff Bonwick * To modify the configuration, you must hold all locks as writer. To modify
161e14bb325SJeff Bonwick * vdev state without altering the vdev tree's topology (e.g. online/offline),
162e14bb325SJeff Bonwick * you must hold SCL_STATE and SCL_ZIO as writer.
163e14bb325SJeff Bonwick *
164e14bb325SJeff Bonwick * We use these distinct config locks to avoid recursive lock entry.
165e14bb325SJeff Bonwick * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
166e14bb325SJeff Bonwick * block allocations (SCL_ALLOC), which may require reading space maps
167e14bb325SJeff Bonwick * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
168e14bb325SJeff Bonwick *
169e14bb325SJeff Bonwick * The spa config locks cannot be normal rwlocks because we need the
170e14bb325SJeff Bonwick * ability to hand off ownership. For example, SCL_ZIO is acquired
171e14bb325SJeff Bonwick * by the issuing thread and later released by an interrupt thread.
172e14bb325SJeff Bonwick * They do, however, obey the usual write-wanted semantics to prevent
173e14bb325SJeff Bonwick * writer (i.e. system administrator) starvation.
174e14bb325SJeff Bonwick *
175e14bb325SJeff Bonwick * The lock acquisition rules are as follows:
176e14bb325SJeff Bonwick *
177e14bb325SJeff Bonwick * SCL_CONFIG
178e14bb325SJeff Bonwick * Protects changes to the vdev tree topology, such as vdev
179e14bb325SJeff Bonwick * add/remove/attach/detach. Protects the dirty config list
180e14bb325SJeff Bonwick * (spa_config_dirty_list) and the set of spares and l2arc devices.
181e14bb325SJeff Bonwick *
182e14bb325SJeff Bonwick * SCL_STATE
183e14bb325SJeff Bonwick * Protects changes to pool state and vdev state, such as vdev
184e14bb325SJeff Bonwick * online/offline/fault/degrade/clear. Protects the dirty state list
185e14bb325SJeff Bonwick * (spa_state_dirty_list) and global pool state (spa_state).
186e14bb325SJeff Bonwick *
187e14bb325SJeff Bonwick * SCL_ALLOC
188e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes.
189e14bb325SJeff Bonwick * Held as reader by metaslab_alloc() and metaslab_claim().
190e14bb325SJeff Bonwick *
191e14bb325SJeff Bonwick * SCL_ZIO
192e14bb325SJeff Bonwick * Held by bp-level zios (those which have no io_vd upon entry)
193e14bb325SJeff Bonwick * to prevent changes to the vdev tree. The bp-level zio implicitly
194e14bb325SJeff Bonwick * protects all of its vdev child zios, which do not hold SCL_ZIO.
195e14bb325SJeff Bonwick *
196e14bb325SJeff Bonwick * SCL_FREE
197e14bb325SJeff Bonwick * Protects changes to metaslab groups and classes.
198e14bb325SJeff Bonwick * Held as reader by metaslab_free(). SCL_FREE is distinct from
199e14bb325SJeff Bonwick * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
200e14bb325SJeff Bonwick * blocks in zio_done() while another i/o that holds either
201e14bb325SJeff Bonwick * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
202e14bb325SJeff Bonwick *
203e14bb325SJeff Bonwick * SCL_VDEV
204e14bb325SJeff Bonwick * Held as reader to prevent changes to the vdev tree during trivial
205b24ab676SJeff Bonwick * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
206e14bb325SJeff Bonwick * other locks, and lower than all of them, to ensure that it's safe
207e14bb325SJeff Bonwick * to acquire regardless of caller context.
208e14bb325SJeff Bonwick *
209e14bb325SJeff Bonwick * In addition, the following rules apply:
210e14bb325SJeff Bonwick *
211e14bb325SJeff Bonwick * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
212e14bb325SJeff Bonwick * The lock ordering is SCL_CONFIG > spa_props_lock.
213e14bb325SJeff Bonwick *
214e14bb325SJeff Bonwick * (b) I/O operations on leaf vdevs. For any zio operation that takes
215e14bb325SJeff Bonwick * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
216e14bb325SJeff Bonwick * or zio_write_phys() -- the caller must ensure that the config cannot
217e14bb325SJeff Bonwick * cannot change in the interim, and that the vdev cannot be reopened.
218e14bb325SJeff Bonwick * SCL_STATE as reader suffices for both.
219fa9e4066Sahrens *
220ea8dc4b6Seschrock * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
221fa9e4066Sahrens *
22244cd46caSbillm * spa_vdev_enter() Acquire the namespace lock and the config lock
223ea8dc4b6Seschrock * for writing.
224fa9e4066Sahrens *
22544cd46caSbillm * spa_vdev_exit() Release the config lock, wait for all I/O
22644cd46caSbillm * to complete, sync the updated configs to the
227ea8dc4b6Seschrock * cache, and release the namespace lock.
228fa9e4066Sahrens *
229e14bb325SJeff Bonwick * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
230e14bb325SJeff Bonwick * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
231e14bb325SJeff Bonwick * locking is, always, based on spa_namespace_lock and spa_config_lock[].
232fa9e4066Sahrens */
233fa9e4066Sahrens
234fa9e4066Sahrens static avl_tree_t spa_namespace_avl;
235fa9e4066Sahrens kmutex_t spa_namespace_lock;
236fa9e4066Sahrens static kcondvar_t spa_namespace_cv;
2370373e76bSbonwick static int spa_active_count;
238416e0cd8Sek int spa_max_replication_override = SPA_DVAS_PER_BP;
239fa9e4066Sahrens
24099653d4eSeschrock static kmutex_t spa_spare_lock;
24139c23413Seschrock static avl_tree_t spa_spare_avl;
242fa94a07fSbrendan static kmutex_t spa_l2cache_lock;
243fa94a07fSbrendan static avl_tree_t spa_l2cache_avl;
24499653d4eSeschrock
245fa9e4066Sahrens kmem_cache_t *spa_buffer_pool;
2468ad4d6ddSJeff Bonwick int spa_mode_global;
247fa9e4066Sahrens
248fa9e4066Sahrens #ifdef ZFS_DEBUG
2495cabbc6bSPrashanth Sreenivasa /*
2505cabbc6bSPrashanth Sreenivasa * Everything except dprintf, spa, and indirect_remap is on by default
2515cabbc6bSPrashanth Sreenivasa * in debug builds.
2525cabbc6bSPrashanth Sreenivasa */
25321f7c81cSMatthew Ahrens int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP);
254fa9e4066Sahrens #else
255fa9e4066Sahrens int zfs_flags = 0;
256fa9e4066Sahrens #endif
257fa9e4066Sahrens
2580125049cSahrens /*
2590125049cSahrens * zfs_recover can be set to nonzero to attempt to recover from
2600125049cSahrens * otherwise-fatal errors, typically caused by on-disk corruption. When
2610125049cSahrens * set, calls to zfs_panic_recover() will turn into warning messages.
2628b36997aSMatthew Ahrens * This should only be used as a last resort, as it typically results
2638b36997aSMatthew Ahrens * in leaked space, or worse.
2640125049cSahrens */
2657fd05ac4SMatthew Ahrens boolean_t zfs_recover = B_FALSE;
2667fd05ac4SMatthew Ahrens
2677fd05ac4SMatthew Ahrens /*
2687fd05ac4SMatthew Ahrens * If destroy encounters an EIO while reading metadata (e.g. indirect
2697fd05ac4SMatthew Ahrens * blocks), space referenced by the missing metadata can not be freed.
2707fd05ac4SMatthew Ahrens * Normally this causes the background destroy to become "stalled", as
2717fd05ac4SMatthew Ahrens * it is unable to make forward progress. While in this stalled state,
2727fd05ac4SMatthew Ahrens * all remaining space to free from the error-encountering filesystem is
2737fd05ac4SMatthew Ahrens * "temporarily leaked". Set this flag to cause it to ignore the EIO,
2747fd05ac4SMatthew Ahrens * permanently leak the space from indirect blocks that can not be read,
2757fd05ac4SMatthew Ahrens * and continue to free everything else that it can.
2767fd05ac4SMatthew Ahrens *
2777fd05ac4SMatthew Ahrens * The default, "stalling" behavior is useful if the storage partially
2787fd05ac4SMatthew Ahrens * fails (i.e. some but not all i/os fail), and then later recovers. In
2797fd05ac4SMatthew Ahrens * this case, we will be able to continue pool operations while it is
2807fd05ac4SMatthew Ahrens * partially failed, and when it recovers, we can continue to free the
2817fd05ac4SMatthew Ahrens * space, with no leaks. However, note that this case is actually
2827fd05ac4SMatthew Ahrens * fairly rare.
2837fd05ac4SMatthew Ahrens *
2847fd05ac4SMatthew Ahrens * Typically pools either (a) fail completely (but perhaps temporarily,
2857fd05ac4SMatthew Ahrens * e.g. a top-level vdev going offline), or (b) have localized,
2867fd05ac4SMatthew Ahrens * permanent errors (e.g. disk returns the wrong data due to bit flip or
2877fd05ac4SMatthew Ahrens * firmware bug). In case (a), this setting does not matter because the
2887fd05ac4SMatthew Ahrens * pool will be suspended and the sync thread will not be able to make
2897fd05ac4SMatthew Ahrens * forward progress regardless. In case (b), because the error is
2907fd05ac4SMatthew Ahrens * permanent, the best we can do is leak the minimum amount of space,
2917fd05ac4SMatthew Ahrens * which is what setting this flag will do. Therefore, it is reasonable
2927fd05ac4SMatthew Ahrens * for this flag to normally be set, but we chose the more conservative
2937fd05ac4SMatthew Ahrens * approach of not setting it, so that there is no possibility of
2947fd05ac4SMatthew Ahrens * leaking space in the "partial temporary" failure case.
2957fd05ac4SMatthew Ahrens */
2967fd05ac4SMatthew Ahrens boolean_t zfs_free_leak_on_eio = B_FALSE;
2970125049cSahrens
29869962b56SMatthew Ahrens /*
29969962b56SMatthew Ahrens * Expiration time in milliseconds. This value has two meanings. First it is
30069962b56SMatthew Ahrens * used to determine when the spa_deadman() logic should fire. By default the
30169962b56SMatthew Ahrens * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
30269962b56SMatthew Ahrens * Secondly, the value determines if an I/O is considered "hung". Any I/O that
30369962b56SMatthew Ahrens * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
30469962b56SMatthew Ahrens * in a system panic.
30569962b56SMatthew Ahrens */
30669962b56SMatthew Ahrens uint64_t zfs_deadman_synctime_ms = 1000000ULL;
307283b8460SGeorge.Wilson
308283b8460SGeorge.Wilson /*
30969962b56SMatthew Ahrens * Check time in milliseconds. This defines the frequency at which we check
31069962b56SMatthew Ahrens * for hung I/O.
311283b8460SGeorge.Wilson */
31269962b56SMatthew Ahrens uint64_t zfs_deadman_checktime_ms = 5000ULL;
313283b8460SGeorge.Wilson
314283b8460SGeorge.Wilson /*
315283b8460SGeorge.Wilson * Override the zfs deadman behavior via /etc/system. By default the
316283b8460SGeorge.Wilson * deadman is enabled except on VMware and sparc deployments.
317283b8460SGeorge.Wilson */
318283b8460SGeorge.Wilson int zfs_deadman_enabled = -1;
319283b8460SGeorge.Wilson
320fd440315SJoshua M. Clulow #if defined(__amd64__) || defined(__i386__)
321fd440315SJoshua M. Clulow /*
322fd440315SJoshua M. Clulow * Should we allow the use of mechanisms that depend on saving and restoring
323c21bd51dSDan McDonald * the FPU state? This was disabled initially due to stability issues in
324c21bd51dSDan McDonald * the kernel FPU routines; see bug 13717. As of the fixes for 13902 and
325c21bd51dSDan McDonald * 13915, it has once again been enabled.
326fd440315SJoshua M. Clulow */
327c21bd51dSDan McDonald int zfs_fpu_enabled = 1;
328fd440315SJoshua M. Clulow #endif
329fd440315SJoshua M. Clulow
33069962b56SMatthew Ahrens /*
33169962b56SMatthew Ahrens * The worst case is single-sector max-parity RAID-Z blocks, in which
33269962b56SMatthew Ahrens * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
33369962b56SMatthew Ahrens * times the size; so just assume that. Add to this the fact that
33469962b56SMatthew Ahrens * we can have up to 3 DVAs per bp, and one more factor of 2 because
33569962b56SMatthew Ahrens * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
33669962b56SMatthew Ahrens * the worst case is:
33769962b56SMatthew Ahrens * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
33869962b56SMatthew Ahrens */
33969962b56SMatthew Ahrens int spa_asize_inflation = 24;
340fa9e4066Sahrens
3417d46dc6cSMatthew Ahrens /*
3427d46dc6cSMatthew Ahrens * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
3437d46dc6cSMatthew Ahrens * the pool to be consumed. This ensures that we don't run the pool
3447d46dc6cSMatthew Ahrens * completely out of space, due to unaccounted changes (e.g. to the MOS).
3457d46dc6cSMatthew Ahrens * It also limits the worst-case time to allocate space. If we have
3467d46dc6cSMatthew Ahrens * less than this amount of free space, most ZPL operations (e.g. write,
3477d46dc6cSMatthew Ahrens * create) will return ENOSPC.
3487d46dc6cSMatthew Ahrens *
3497d46dc6cSMatthew Ahrens * Certain operations (e.g. file removal, most administrative actions) can
3507d46dc6cSMatthew Ahrens * use half the slop space. They will only return ENOSPC if less than half
3517d46dc6cSMatthew Ahrens * the slop space is free. Typically, once the pool has less than the slop
3527d46dc6cSMatthew Ahrens * space free, the user will use these operations to free up space in the pool.
3537d46dc6cSMatthew Ahrens * These are the operations that call dsl_pool_adjustedsize() with the netfree
3547d46dc6cSMatthew Ahrens * argument set to TRUE.
3557d46dc6cSMatthew Ahrens *
35686714001SSerapheim Dimitropoulos * Operations that are almost guaranteed to free up space in the absence of
35786714001SSerapheim Dimitropoulos * a pool checkpoint can use up to three quarters of the slop space
35886714001SSerapheim Dimitropoulos * (e.g zfs destroy).
35986714001SSerapheim Dimitropoulos *
3607d46dc6cSMatthew Ahrens * A very restricted set of operations are always permitted, regardless of
3617d46dc6cSMatthew Ahrens * the amount of free space. These are the operations that call
36286714001SSerapheim Dimitropoulos * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
36386714001SSerapheim Dimitropoulos * increase in the amount of space used, it is possible to run the pool
36486714001SSerapheim Dimitropoulos * completely out of space, causing it to be permanently read-only.
3657d46dc6cSMatthew Ahrens *
3664b5c8e93SMatthew Ahrens * Note that on very small pools, the slop space will be larger than
3674b5c8e93SMatthew Ahrens * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
3684b5c8e93SMatthew Ahrens * but we never allow it to be more than half the pool size.
3694b5c8e93SMatthew Ahrens *
3707d46dc6cSMatthew Ahrens * See also the comments in zfs_space_check_t.
3717d46dc6cSMatthew Ahrens */
3727d46dc6cSMatthew Ahrens int spa_slop_shift = 5;
3734b5c8e93SMatthew Ahrens uint64_t spa_min_slop = 128 * 1024 * 1024;
3747d46dc6cSMatthew Ahrens
375f78cdc34SPaul Dagnelie int spa_allocators = 4;
376f78cdc34SPaul Dagnelie
3773ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
3783ee8c80cSPavel Zakharov void
spa_load_failed(spa_t * spa,const char * fmt,...)3793ee8c80cSPavel Zakharov spa_load_failed(spa_t *spa, const char *fmt, ...)
3803ee8c80cSPavel Zakharov {
3813ee8c80cSPavel Zakharov va_list adx;
3823ee8c80cSPavel Zakharov char buf[256];
3833ee8c80cSPavel Zakharov
3843ee8c80cSPavel Zakharov va_start(adx, fmt);
3853ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx);
3863ee8c80cSPavel Zakharov va_end(adx);
3873ee8c80cSPavel Zakharov
3886f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
3896f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf);
3903ee8c80cSPavel Zakharov }
3913ee8c80cSPavel Zakharov
3923ee8c80cSPavel Zakharov /*PRINTFLIKE2*/
3933ee8c80cSPavel Zakharov void
spa_load_note(spa_t * spa,const char * fmt,...)3943ee8c80cSPavel Zakharov spa_load_note(spa_t *spa, const char *fmt, ...)
3953ee8c80cSPavel Zakharov {
3963ee8c80cSPavel Zakharov va_list adx;
3973ee8c80cSPavel Zakharov char buf[256];
3983ee8c80cSPavel Zakharov
3993ee8c80cSPavel Zakharov va_start(adx, fmt);
4003ee8c80cSPavel Zakharov (void) vsnprintf(buf, sizeof (buf), fmt, adx);
4013ee8c80cSPavel Zakharov va_end(adx);
4023ee8c80cSPavel Zakharov
4036f793812SPavel Zakharov zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
4046f793812SPavel Zakharov spa->spa_trust_config ? "trusted" : "untrusted", buf);
4053ee8c80cSPavel Zakharov }
4063ee8c80cSPavel Zakharov
407663207adSDon Brady /*
408663207adSDon Brady * By default dedup and user data indirects land in the special class
409663207adSDon Brady */
410663207adSDon Brady int zfs_ddt_data_is_special = B_TRUE;
411663207adSDon Brady int zfs_user_indirect_is_special = B_TRUE;
412663207adSDon Brady
413663207adSDon Brady /*
414663207adSDon Brady * The percentage of special class final space reserved for metadata only.
415663207adSDon Brady * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
416663207adSDon Brady * let metadata into the class.
417663207adSDon Brady */
418663207adSDon Brady int zfs_special_class_metadata_reserve_pct = 25;
419663207adSDon Brady
420e05725b1Sbonwick /*
421e05725b1Sbonwick * ==========================================================================
422e05725b1Sbonwick * SPA config locking
423e05725b1Sbonwick * ==========================================================================
424e05725b1Sbonwick */
425e05725b1Sbonwick static void
spa_config_lock_init(spa_t * spa)426e14bb325SJeff Bonwick spa_config_lock_init(spa_t *spa)
427e14bb325SJeff Bonwick {
428e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) {
429e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i];
430e14bb325SJeff Bonwick mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
431e14bb325SJeff Bonwick cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
432e914ace2STim Schumacher zfs_refcount_create_untracked(&scl->scl_count);
433e14bb325SJeff Bonwick scl->scl_writer = NULL;
434e14bb325SJeff Bonwick scl->scl_write_wanted = 0;
435e14bb325SJeff Bonwick }
436e05725b1Sbonwick }
437e05725b1Sbonwick
438e05725b1Sbonwick static void
spa_config_lock_destroy(spa_t * spa)439e14bb325SJeff Bonwick spa_config_lock_destroy(spa_t *spa)
440e14bb325SJeff Bonwick {
441e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) {
442e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i];
443e14bb325SJeff Bonwick mutex_destroy(&scl->scl_lock);
444e14bb325SJeff Bonwick cv_destroy(&scl->scl_cv);
445e914ace2STim Schumacher zfs_refcount_destroy(&scl->scl_count);
446e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL);
447e14bb325SJeff Bonwick ASSERT(scl->scl_write_wanted == 0);
448e14bb325SJeff Bonwick }
449e14bb325SJeff Bonwick }
450e14bb325SJeff Bonwick
451e14bb325SJeff Bonwick int
spa_config_tryenter(spa_t * spa,int locks,void * tag,krw_t rw)452e14bb325SJeff Bonwick spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
453e05725b1Sbonwick {
454e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) {
455e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i];
456e14bb325SJeff Bonwick if (!(locks & (1 << i)))
457e14bb325SJeff Bonwick continue;
458e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock);
459e14bb325SJeff Bonwick if (rw == RW_READER) {
460e14bb325SJeff Bonwick if (scl->scl_writer || scl->scl_write_wanted) {
461e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock);
462e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1),
463e495b6e6SSaso Kiselkov tag);
464e14bb325SJeff Bonwick return (0);
465e14bb325SJeff Bonwick }
466e14bb325SJeff Bonwick } else {
467e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread);
468e914ace2STim Schumacher if (!zfs_refcount_is_zero(&scl->scl_count)) {
469e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock);
470e495b6e6SSaso Kiselkov spa_config_exit(spa, locks & ((1 << i) - 1),
471e495b6e6SSaso Kiselkov tag);
472e14bb325SJeff Bonwick return (0);
473e14bb325SJeff Bonwick }
474e14bb325SJeff Bonwick scl->scl_writer = curthread;
475e14bb325SJeff Bonwick }
476e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag);
477e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock);
478e14bb325SJeff Bonwick }
479e14bb325SJeff Bonwick return (1);
480e05725b1Sbonwick }
481e05725b1Sbonwick
482e05725b1Sbonwick void
spa_config_enter(spa_t * spa,int locks,void * tag,krw_t rw)483e14bb325SJeff Bonwick spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
484e05725b1Sbonwick {
485f64c0e34SEric Taylor int wlocks_held = 0;
486f64c0e34SEric Taylor
4873b2aab18SMatthew Ahrens ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
4883b2aab18SMatthew Ahrens
489e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) {
490e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i];
491f64c0e34SEric Taylor if (scl->scl_writer == curthread)
492f64c0e34SEric Taylor wlocks_held |= (1 << i);
493e14bb325SJeff Bonwick if (!(locks & (1 << i)))
494e14bb325SJeff Bonwick continue;
495e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock);
496e14bb325SJeff Bonwick if (rw == RW_READER) {
497e14bb325SJeff Bonwick while (scl->scl_writer || scl->scl_write_wanted) {
498e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock);
499e14bb325SJeff Bonwick }
500e14bb325SJeff Bonwick } else {
501e14bb325SJeff Bonwick ASSERT(scl->scl_writer != curthread);
502e914ace2STim Schumacher while (!zfs_refcount_is_zero(&scl->scl_count)) {
503e14bb325SJeff Bonwick scl->scl_write_wanted++;
504e14bb325SJeff Bonwick cv_wait(&scl->scl_cv, &scl->scl_lock);
505e14bb325SJeff Bonwick scl->scl_write_wanted--;
506e14bb325SJeff Bonwick }
507e14bb325SJeff Bonwick scl->scl_writer = curthread;
508e14bb325SJeff Bonwick }
509e914ace2STim Schumacher (void) zfs_refcount_add(&scl->scl_count, tag);
510e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock);
511e05725b1Sbonwick }
5125cabbc6bSPrashanth Sreenivasa ASSERT3U(wlocks_held, <=, locks);
513e05725b1Sbonwick }
514e05725b1Sbonwick
515e05725b1Sbonwick void
spa_config_exit(spa_t * spa,int locks,void * tag)516e14bb325SJeff Bonwick spa_config_exit(spa_t *spa, int locks, void *tag)
517e05725b1Sbonwick {
518e14bb325SJeff Bonwick for (int i = SCL_LOCKS - 1; i >= 0; i--) {
519e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i];
520e14bb325SJeff Bonwick if (!(locks & (1 << i)))
521e14bb325SJeff Bonwick continue;
522e14bb325SJeff Bonwick mutex_enter(&scl->scl_lock);
523e914ace2STim Schumacher ASSERT(!zfs_refcount_is_zero(&scl->scl_count));
524e914ace2STim Schumacher if (zfs_refcount_remove(&scl->scl_count, tag) == 0) {
525e14bb325SJeff Bonwick ASSERT(scl->scl_writer == NULL ||
526e14bb325SJeff Bonwick scl->scl_writer == curthread);
527e14bb325SJeff Bonwick scl->scl_writer = NULL; /* OK in either case */
528e14bb325SJeff Bonwick cv_broadcast(&scl->scl_cv);
529e14bb325SJeff Bonwick }
530e14bb325SJeff Bonwick mutex_exit(&scl->scl_lock);
531e05725b1Sbonwick }
532e05725b1Sbonwick }
533e05725b1Sbonwick
534e14bb325SJeff Bonwick int
spa_config_held(spa_t * spa,int locks,krw_t rw)535e14bb325SJeff Bonwick spa_config_held(spa_t *spa, int locks, krw_t rw)
536e05725b1Sbonwick {
537e14bb325SJeff Bonwick int locks_held = 0;
538e05725b1Sbonwick
539e14bb325SJeff Bonwick for (int i = 0; i < SCL_LOCKS; i++) {
540e14bb325SJeff Bonwick spa_config_lock_t *scl = &spa->spa_config_lock[i];
541e14bb325SJeff Bonwick if (!(locks & (1 << i)))
542e14bb325SJeff Bonwick continue;
543e914ace2STim Schumacher if ((rw == RW_READER &&
544e914ace2STim Schumacher !zfs_refcount_is_zero(&scl->scl_count)) ||
545e14bb325SJeff Bonwick (rw == RW_WRITER && scl->scl_writer == curthread))
546e14bb325SJeff Bonwick locks_held |= 1 << i;
547e14bb325SJeff Bonwick }
548e14bb325SJeff Bonwick
549e14bb325SJeff Bonwick return (locks_held);
550e05725b1Sbonwick }
551e05725b1Sbonwick
552fa9e4066Sahrens /*
553fa9e4066Sahrens * ==========================================================================
554fa9e4066Sahrens * SPA namespace functions
555fa9e4066Sahrens * ==========================================================================
556fa9e4066Sahrens */
557fa9e4066Sahrens
558fa9e4066Sahrens /*
559fa9e4066Sahrens * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
560fa9e4066Sahrens * Returns NULL if no matching spa_t is found.
561fa9e4066Sahrens */
562fa9e4066Sahrens spa_t *
spa_lookup(const char * name)563fa9e4066Sahrens spa_lookup(const char *name)
564fa9e4066Sahrens {
565e14bb325SJeff Bonwick static spa_t search; /* spa_t is large; don't allocate on stack */
566e14bb325SJeff Bonwick spa_t *spa;
567fa9e4066Sahrens avl_index_t where;
56840feaa91Sahrens char *cp;
569fa9e4066Sahrens
570fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock));
571fa9e4066Sahrens
5723b2aab18SMatthew Ahrens (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
5733b2aab18SMatthew Ahrens
57440feaa91Sahrens /*
57540feaa91Sahrens * If it's a full dataset name, figure out the pool name and
57640feaa91Sahrens * just use that.
57740feaa91Sahrens */
57878f17100SMatthew Ahrens cp = strpbrk(search.spa_name, "/@#");
5793b2aab18SMatthew Ahrens if (cp != NULL)
58040feaa91Sahrens *cp = '\0';
58140feaa91Sahrens
582fa9e4066Sahrens spa = avl_find(&spa_namespace_avl, &search, &where);
583fa9e4066Sahrens
584fa9e4066Sahrens return (spa);
585fa9e4066Sahrens }
586fa9e4066Sahrens
587283b8460SGeorge.Wilson /*
588283b8460SGeorge.Wilson * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
589283b8460SGeorge.Wilson * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
590283b8460SGeorge.Wilson * looking for potentially hung I/Os.
591283b8460SGeorge.Wilson */
592283b8460SGeorge.Wilson void
spa_deadman(void * arg)593283b8460SGeorge.Wilson spa_deadman(void *arg)
594283b8460SGeorge.Wilson {
595283b8460SGeorge.Wilson spa_t *spa = arg;
596283b8460SGeorge.Wilson
5970713e232SGeorge Wilson /*
5980713e232SGeorge Wilson * Disable the deadman timer if the pool is suspended.
5990713e232SGeorge Wilson */
6000713e232SGeorge Wilson if (spa_suspended(spa)) {
6010713e232SGeorge Wilson VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6020713e232SGeorge Wilson return;
6030713e232SGeorge Wilson }
6040713e232SGeorge Wilson
605283b8460SGeorge.Wilson zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
606283b8460SGeorge.Wilson (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
607283b8460SGeorge.Wilson ++spa->spa_deadman_calls);
608283b8460SGeorge.Wilson if (zfs_deadman_enabled)
609283b8460SGeorge.Wilson vdev_deadman(spa->spa_root_vdev);
610283b8460SGeorge.Wilson }
611283b8460SGeorge.Wilson
612814dcd43SSerapheim Dimitropoulos int
spa_log_sm_sort_by_txg(const void * va,const void * vb)613814dcd43SSerapheim Dimitropoulos spa_log_sm_sort_by_txg(const void *va, const void *vb)
614814dcd43SSerapheim Dimitropoulos {
615814dcd43SSerapheim Dimitropoulos const spa_log_sm_t *a = va;
616814dcd43SSerapheim Dimitropoulos const spa_log_sm_t *b = vb;
617814dcd43SSerapheim Dimitropoulos
6184d7988d6SPaul Dagnelie return (TREE_CMP(a->sls_txg, b->sls_txg));
619814dcd43SSerapheim Dimitropoulos }
620814dcd43SSerapheim Dimitropoulos
621fa9e4066Sahrens /*
622fa9e4066Sahrens * Create an uninitialized spa_t with the given name. Requires
623fa9e4066Sahrens * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
624fa9e4066Sahrens * exist by calling spa_lookup() first.
625fa9e4066Sahrens */
626fa9e4066Sahrens spa_t *
spa_add(const char * name,nvlist_t * config,const char * altroot)627468c413aSTim Haley spa_add(const char *name, nvlist_t *config, const char *altroot)
628fa9e4066Sahrens {
629fa9e4066Sahrens spa_t *spa;
630c5904d13Seschrock spa_config_dirent_t *dp;
631283b8460SGeorge.Wilson cyc_handler_t hdlr;
632283b8460SGeorge.Wilson cyc_time_t when;
633fa9e4066Sahrens
634fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock));
635fa9e4066Sahrens
636fa9e4066Sahrens spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
637fa9e4066Sahrens
638c25056deSgw mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
639c25056deSgw mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
64035a5a358SJonathan Adams mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
641bc9014e6SJustin Gibbs mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
642c25056deSgw mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
64335a5a358SJonathan Adams mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
644c25056deSgw mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
64545818ee1SMatthew Ahrens mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
64635a5a358SJonathan Adams mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
647a1521560SJeff Bonwick mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
648a1521560SJeff Bonwick mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
649c3a66015SMatthew Ahrens mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL);
650814dcd43SSerapheim Dimitropoulos mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
651dd50e0ccSTony Hutter mutex_init(&spa->spa_imp_kstat_lock, NULL, MUTEX_DEFAULT, NULL);
652c25056deSgw
653c25056deSgw cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
654bc9014e6SJustin Gibbs cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
65535a5a358SJonathan Adams cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
656c25056deSgw cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
657e14bb325SJeff Bonwick cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
658c25056deSgw
659b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++)
660cde58dbcSMatthew Ahrens bplist_create(&spa->spa_free_bplist[t]);
661b24ab676SJeff Bonwick
662e14bb325SJeff Bonwick (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
663fa9e4066Sahrens spa->spa_state = POOL_STATE_UNINITIALIZED;
664fa9e4066Sahrens spa->spa_freeze_txg = UINT64_MAX;
6650373e76bSbonwick spa->spa_final_txg = UINT64_MAX;
666468c413aSTim Haley spa->spa_load_max_txg = UINT64_MAX;
66735a5a358SJonathan Adams spa->spa_proc = &p0;
66835a5a358SJonathan Adams spa->spa_proc_state = SPA_PROC_NONE;
6696f793812SPavel Zakharov spa->spa_trust_config = B_TRUE;
670fa9e4066Sahrens
671283b8460SGeorge.Wilson hdlr.cyh_func = spa_deadman;
672283b8460SGeorge.Wilson hdlr.cyh_arg = spa;
673283b8460SGeorge.Wilson hdlr.cyh_level = CY_LOW_LEVEL;
674283b8460SGeorge.Wilson
67569962b56SMatthew Ahrens spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
676283b8460SGeorge.Wilson
677283b8460SGeorge.Wilson /*
678283b8460SGeorge.Wilson * This determines how often we need to check for hung I/Os after
679283b8460SGeorge.Wilson * the cyclic has already fired. Since checking for hung I/Os is
680283b8460SGeorge.Wilson * an expensive operation we don't want to check too frequently.
68169962b56SMatthew Ahrens * Instead wait for 5 seconds before checking again.
682283b8460SGeorge.Wilson */
68369962b56SMatthew Ahrens when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
684283b8460SGeorge.Wilson when.cyt_when = CY_INFINITY;
685283b8460SGeorge.Wilson mutex_enter(&cpu_lock);
686283b8460SGeorge.Wilson spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
687283b8460SGeorge.Wilson mutex_exit(&cpu_lock);
688283b8460SGeorge.Wilson
689e914ace2STim Schumacher zfs_refcount_create(&spa->spa_refcount);
690e14bb325SJeff Bonwick spa_config_lock_init(spa);
691fa9e4066Sahrens
692fa9e4066Sahrens avl_add(&spa_namespace_avl, spa);
693fa9e4066Sahrens
6940373e76bSbonwick /*
6950373e76bSbonwick * Set the alternate root, if there is one.
6960373e76bSbonwick */
6970373e76bSbonwick if (altroot) {
6980373e76bSbonwick spa->spa_root = spa_strdup(altroot);
6990373e76bSbonwick spa_active_count++;
7000373e76bSbonwick }
7010373e76bSbonwick
702f78cdc34SPaul Dagnelie spa->spa_alloc_count = spa_allocators;
703f78cdc34SPaul Dagnelie spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
704f78cdc34SPaul Dagnelie sizeof (kmutex_t), KM_SLEEP);
705f78cdc34SPaul Dagnelie spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
706f78cdc34SPaul Dagnelie sizeof (avl_tree_t), KM_SLEEP);
707f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) {
708f78cdc34SPaul Dagnelie mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
709f78cdc34SPaul Dagnelie avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
710f78cdc34SPaul Dagnelie sizeof (zio_t), offsetof(zio_t, io_alloc_node));
711f78cdc34SPaul Dagnelie }
712814dcd43SSerapheim Dimitropoulos avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
713814dcd43SSerapheim Dimitropoulos sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
714814dcd43SSerapheim Dimitropoulos avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
715814dcd43SSerapheim Dimitropoulos sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
716814dcd43SSerapheim Dimitropoulos list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
717814dcd43SSerapheim Dimitropoulos offsetof(log_summary_entry_t, lse_node));
7180f7643c7SGeorge Wilson
719c5904d13Seschrock /*
720c5904d13Seschrock * Every pool starts with the default cachefile
721c5904d13Seschrock */
722c5904d13Seschrock list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
723c5904d13Seschrock offsetof(spa_config_dirent_t, scd_link));
724c5904d13Seschrock
725c5904d13Seschrock dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
726ef912c80STim Haley dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
727c5904d13Seschrock list_insert_head(&spa->spa_config_list, dp);
728c5904d13Seschrock
7294b964adaSGeorge Wilson VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
7304b964adaSGeorge Wilson KM_SLEEP) == 0);
7314b964adaSGeorge Wilson
732ad135b5dSChristopher Siden if (config != NULL) {
733ad135b5dSChristopher Siden nvlist_t *features;
734ad135b5dSChristopher Siden
735ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
736ad135b5dSChristopher Siden &features) == 0) {
737ad135b5dSChristopher Siden VERIFY(nvlist_dup(features, &spa->spa_label_features,
738ad135b5dSChristopher Siden 0) == 0);
739ad135b5dSChristopher Siden }
740ad135b5dSChristopher Siden
741468c413aSTim Haley VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
742ad135b5dSChristopher Siden }
743ad135b5dSChristopher Siden
744ad135b5dSChristopher Siden if (spa->spa_label_features == NULL) {
745ad135b5dSChristopher Siden VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
746ad135b5dSChristopher Siden KM_SLEEP) == 0);
747ad135b5dSChristopher Siden }
748468c413aSTim Haley
749c3a66015SMatthew Ahrens spa->spa_iokstat = kstat_create("zfs", 0, name,
750c3a66015SMatthew Ahrens "disk", KSTAT_TYPE_IO, 1, 0);
751c3a66015SMatthew Ahrens if (spa->spa_iokstat) {
752c3a66015SMatthew Ahrens spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock;
753c3a66015SMatthew Ahrens kstat_install(spa->spa_iokstat);
754c3a66015SMatthew Ahrens }
755c3a66015SMatthew Ahrens
75681cd5c55SMatthew Ahrens spa->spa_min_ashift = INT_MAX;
75781cd5c55SMatthew Ahrens spa->spa_max_ashift = 0;
75881cd5c55SMatthew Ahrens
75943466aaeSMax Grossman /*
76043466aaeSMax Grossman * As a pool is being created, treat all features as disabled by
76143466aaeSMax Grossman * setting SPA_FEATURE_DISABLED for all entries in the feature
76243466aaeSMax Grossman * refcount cache.
76343466aaeSMax Grossman */
76443466aaeSMax Grossman for (int i = 0; i < SPA_FEATURES; i++) {
76543466aaeSMax Grossman spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
76643466aaeSMax Grossman }
76743466aaeSMax Grossman
768e0f1c0afSOlaf Faaland list_create(&spa->spa_leaf_list, sizeof (vdev_t),
769e0f1c0afSOlaf Faaland offsetof(vdev_t, vdev_leaf_node));
770e0f1c0afSOlaf Faaland
771fa9e4066Sahrens return (spa);
772fa9e4066Sahrens }
773fa9e4066Sahrens
774fa9e4066Sahrens /*
775fa9e4066Sahrens * Removes a spa_t from the namespace, freeing up any memory used. Requires
776fa9e4066Sahrens * spa_namespace_lock. This is called only after the spa_t has been closed and
777fa9e4066Sahrens * deactivated.
778fa9e4066Sahrens */
779fa9e4066Sahrens void
spa_remove(spa_t * spa)780fa9e4066Sahrens spa_remove(spa_t *spa)
781fa9e4066Sahrens {
782c5904d13Seschrock spa_config_dirent_t *dp;
783c5904d13Seschrock
784fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock));
785814dcd43SSerapheim Dimitropoulos ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
786e914ace2STim Schumacher ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
787fa9e4066Sahrens
7881195e687SMark J Musante nvlist_free(spa->spa_config_splitting);
7891195e687SMark J Musante
790fa9e4066Sahrens avl_remove(&spa_namespace_avl, spa);
791fa9e4066Sahrens cv_broadcast(&spa_namespace_cv);
792fa9e4066Sahrens
7930373e76bSbonwick if (spa->spa_root) {
794fa9e4066Sahrens spa_strfree(spa->spa_root);
7950373e76bSbonwick spa_active_count--;
7960373e76bSbonwick }
797fa9e4066Sahrens
798c5904d13Seschrock while ((dp = list_head(&spa->spa_config_list)) != NULL) {
799c5904d13Seschrock list_remove(&spa->spa_config_list, dp);
800c5904d13Seschrock if (dp->scd_path != NULL)
801c5904d13Seschrock spa_strfree(dp->scd_path);
802c5904d13Seschrock kmem_free(dp, sizeof (spa_config_dirent_t));
803c5904d13Seschrock }
804c5904d13Seschrock
805f78cdc34SPaul Dagnelie for (int i = 0; i < spa->spa_alloc_count; i++) {
806f78cdc34SPaul Dagnelie avl_destroy(&spa->spa_alloc_trees[i]);
807f78cdc34SPaul Dagnelie mutex_destroy(&spa->spa_alloc_locks[i]);
808f78cdc34SPaul Dagnelie }
809f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count *
810f78cdc34SPaul Dagnelie sizeof (kmutex_t));
811f78cdc34SPaul Dagnelie kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count *
812f78cdc34SPaul Dagnelie sizeof (avl_tree_t));
813f78cdc34SPaul Dagnelie
814814dcd43SSerapheim Dimitropoulos avl_destroy(&spa->spa_metaslabs_by_flushed);
815814dcd43SSerapheim Dimitropoulos avl_destroy(&spa->spa_sm_logs_by_txg);
816814dcd43SSerapheim Dimitropoulos list_destroy(&spa->spa_log_summary);
817c5904d13Seschrock list_destroy(&spa->spa_config_list);
818e0f1c0afSOlaf Faaland list_destroy(&spa->spa_leaf_list);
8192f8aaab3Seschrock
820ad135b5dSChristopher Siden nvlist_free(spa->spa_label_features);
8214b964adaSGeorge Wilson nvlist_free(spa->spa_load_info);
822fa9e4066Sahrens spa_config_set(spa, NULL);
823fa9e4066Sahrens
824283b8460SGeorge.Wilson mutex_enter(&cpu_lock);
825283b8460SGeorge.Wilson if (spa->spa_deadman_cycid != CYCLIC_NONE)
826283b8460SGeorge.Wilson cyclic_remove(spa->spa_deadman_cycid);
827283b8460SGeorge.Wilson mutex_exit(&cpu_lock);
828283b8460SGeorge.Wilson spa->spa_deadman_cycid = CYCLIC_NONE;
829283b8460SGeorge.Wilson
830e914ace2STim Schumacher zfs_refcount_destroy(&spa->spa_refcount);
83191ebeef5Sahrens
832e14bb325SJeff Bonwick spa_config_lock_destroy(spa);
833fa9e4066Sahrens
834c3a66015SMatthew Ahrens kstat_delete(spa->spa_iokstat);
835c3a66015SMatthew Ahrens spa->spa_iokstat = NULL;
836c3a66015SMatthew Ahrens
837b24ab676SJeff Bonwick for (int t = 0; t < TXG_SIZE; t++)
838cde58dbcSMatthew Ahrens bplist_destroy(&spa->spa_free_bplist[t]);
839b24ab676SJeff Bonwick
84045818ee1SMatthew Ahrens zio_checksum_templates_free(spa);
84145818ee1SMatthew Ahrens
842c25056deSgw cv_destroy(&spa->spa_async_cv);
843bc9014e6SJustin Gibbs cv_destroy(&spa->spa_evicting_os_cv);
84435a5a358SJonathan Adams cv_destroy(&spa->spa_proc_cv);
845c25056deSgw cv_destroy(&spa->spa_scrub_io_cv);
846e14bb325SJeff Bonwick cv_destroy(&spa->spa_suspend_cv);
847c25056deSgw
848814dcd43SSerapheim Dimitropoulos mutex_destroy(&spa->spa_flushed_ms_lock);
8495ad82045Snd mutex_destroy(&spa->spa_async_lock);
850c25056deSgw mutex_destroy(&spa->spa_errlist_lock);
85135a5a358SJonathan Adams mutex_destroy(&spa->spa_errlog_lock);
852bc9014e6SJustin Gibbs mutex_destroy(&spa->spa_evicting_os_lock);
85306eeb2adSek mutex_destroy(&spa->spa_history_lock);
85435a5a358SJonathan Adams mutex_destroy(&spa->spa_proc_lock);
855b1b8ab34Slling mutex_destroy(&spa->spa_props_lock);
85645818ee1SMatthew Ahrens mutex_destroy(&spa->spa_cksum_tmpls_lock);
85735a5a358SJonathan Adams mutex_destroy(&spa->spa_scrub_lock);
858e14bb325SJeff Bonwick mutex_destroy(&spa->spa_suspend_lock);
859a1521560SJeff Bonwick mutex_destroy(&spa->spa_vdev_top_lock);
860c3a66015SMatthew Ahrens mutex_destroy(&spa->spa_iokstat_lock);
861dd50e0ccSTony Hutter mutex_destroy(&spa->spa_imp_kstat_lock);
8625ad82045Snd
863fa9e4066Sahrens kmem_free(spa, sizeof (spa_t));
864fa9e4066Sahrens }
865fa9e4066Sahrens
866fa9e4066Sahrens /*
867fa9e4066Sahrens * Given a pool, return the next pool in the namespace, or NULL if there is
868fa9e4066Sahrens * none. If 'prev' is NULL, return the first pool.
869fa9e4066Sahrens */
870fa9e4066Sahrens spa_t *
spa_next(spa_t * prev)871fa9e4066Sahrens spa_next(spa_t *prev)
872fa9e4066Sahrens {
873fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock));
874fa9e4066Sahrens
875fa9e4066Sahrens if (prev)
876fa9e4066Sahrens return (AVL_NEXT(&spa_namespace_avl, prev));
877fa9e4066Sahrens else
878fa9e4066Sahrens return (avl_first(&spa_namespace_avl));
879fa9e4066Sahrens }
880fa9e4066Sahrens
881fa9e4066Sahrens /*
882fa9e4066Sahrens * ==========================================================================
883fa9e4066Sahrens * SPA refcount functions
884fa9e4066Sahrens * ==========================================================================
885fa9e4066Sahrens */
886fa9e4066Sahrens
887fa9e4066Sahrens /*
888fa9e4066Sahrens * Add a reference to the given spa_t. Must have at least one reference, or
889fa9e4066Sahrens * have the namespace lock held.
890fa9e4066Sahrens */
891fa9e4066Sahrens void
spa_open_ref(spa_t * spa,void * tag)892fa9e4066Sahrens spa_open_ref(spa_t *spa, void *tag)
893fa9e4066Sahrens {
894e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
895fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock));
896e914ace2STim Schumacher (void) zfs_refcount_add(&spa->spa_refcount, tag);
897fa9e4066Sahrens }
898fa9e4066Sahrens
899fa9e4066Sahrens /*
900fa9e4066Sahrens * Remove a reference to the given spa_t. Must have at least one reference, or
901fa9e4066Sahrens * have the namespace lock held.
902fa9e4066Sahrens */
903fa9e4066Sahrens void
spa_close(spa_t * spa,void * tag)904fa9e4066Sahrens spa_close(spa_t *spa, void *tag)
905fa9e4066Sahrens {
906e914ace2STim Schumacher ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
907fa9e4066Sahrens MUTEX_HELD(&spa_namespace_lock));
908e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag);
909fa9e4066Sahrens }
910fa9e4066Sahrens
911bc9014e6SJustin Gibbs /*
912bc9014e6SJustin Gibbs * Remove a reference to the given spa_t held by a dsl dir that is
913bc9014e6SJustin Gibbs * being asynchronously released. Async releases occur from a taskq
914bc9014e6SJustin Gibbs * performing eviction of dsl datasets and dirs. The namespace lock
915bc9014e6SJustin Gibbs * isn't held and the hold by the object being evicted may contribute to
916bc9014e6SJustin Gibbs * spa_minref (e.g. dataset or directory released during pool export),
917bc9014e6SJustin Gibbs * so the asserts in spa_close() do not apply.
918bc9014e6SJustin Gibbs */
919bc9014e6SJustin Gibbs void
spa_async_close(spa_t * spa,void * tag)920bc9014e6SJustin Gibbs spa_async_close(spa_t *spa, void *tag)
921bc9014e6SJustin Gibbs {
922e914ace2STim Schumacher (void) zfs_refcount_remove(&spa->spa_refcount, tag);
923bc9014e6SJustin Gibbs }
924bc9014e6SJustin Gibbs
925fa9e4066Sahrens /*
926fa9e4066Sahrens * Check to see if the spa refcount is zero. Must be called with
927088f3894Sahrens * spa_namespace_lock held. We really compare against spa_minref, which is the
928fa9e4066Sahrens * number of references acquired when opening a pool
929fa9e4066Sahrens */
930fa9e4066Sahrens boolean_t
spa_refcount_zero(spa_t * spa)931fa9e4066Sahrens spa_refcount_zero(spa_t *spa)
932fa9e4066Sahrens {
933fa9e4066Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock));
934fa9e4066Sahrens
935e914ace2STim Schumacher return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
936fa9e4066Sahrens }
937fa9e4066Sahrens
93899653d4eSeschrock /*
93999653d4eSeschrock * ==========================================================================
940fa94a07fSbrendan * SPA spare and l2cache tracking
94199653d4eSeschrock * ==========================================================================
94299653d4eSeschrock */
94399653d4eSeschrock
944fa94a07fSbrendan /*
945fa94a07fSbrendan * Hot spares and cache devices are tracked using the same code below,
946fa94a07fSbrendan * for 'auxiliary' devices.
947fa94a07fSbrendan */
948fa94a07fSbrendan
949fa94a07fSbrendan typedef struct spa_aux {
950fa94a07fSbrendan uint64_t aux_guid;
951fa94a07fSbrendan uint64_t aux_pool;
952fa94a07fSbrendan avl_node_t aux_avl;
953fa94a07fSbrendan int aux_count;
954fa94a07fSbrendan } spa_aux_t;
955fa94a07fSbrendan
956c4ab0d3fSGvozden Neskovic static inline int
spa_aux_compare(const void * a,const void * b)957fa94a07fSbrendan spa_aux_compare(const void *a, const void *b)
958fa94a07fSbrendan {
959c4ab0d3fSGvozden Neskovic const spa_aux_t *sa = (const spa_aux_t *)a;
960c4ab0d3fSGvozden Neskovic const spa_aux_t *sb = (const spa_aux_t *)b;
961fa94a07fSbrendan
9624d7988d6SPaul Dagnelie return (TREE_CMP(sa->aux_guid, sb->aux_guid));
963fa94a07fSbrendan }
964fa94a07fSbrendan
965fa94a07fSbrendan void
spa_aux_add(vdev_t * vd,avl_tree_t * avl)966fa94a07fSbrendan spa_aux_add(vdev_t *vd, avl_tree_t *avl)
967fa94a07fSbrendan {
968fa94a07fSbrendan avl_index_t where;
969fa94a07fSbrendan spa_aux_t search;
970fa94a07fSbrendan spa_aux_t *aux;
971fa94a07fSbrendan
972fa94a07fSbrendan search.aux_guid = vd->vdev_guid;
973fa94a07fSbrendan if ((aux = avl_find(avl, &search, &where)) != NULL) {
974fa94a07fSbrendan aux->aux_count++;
975fa94a07fSbrendan } else {
976fa94a07fSbrendan aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
977fa94a07fSbrendan aux->aux_guid = vd->vdev_guid;
978fa94a07fSbrendan aux->aux_count = 1;
979fa94a07fSbrendan avl_insert(avl, aux, where);
980fa94a07fSbrendan }
981fa94a07fSbrendan }
982fa94a07fSbrendan
983fa94a07fSbrendan void
spa_aux_remove(vdev_t * vd,avl_tree_t * avl)984fa94a07fSbrendan spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
985fa94a07fSbrendan {
986fa94a07fSbrendan spa_aux_t search;
987fa94a07fSbrendan spa_aux_t *aux;
988fa94a07fSbrendan avl_index_t where;
989fa94a07fSbrendan
990fa94a07fSbrendan search.aux_guid = vd->vdev_guid;
991fa94a07fSbrendan aux = avl_find(avl, &search, &where);
992fa94a07fSbrendan
993fa94a07fSbrendan ASSERT(aux != NULL);
994fa94a07fSbrendan
995fa94a07fSbrendan if (--aux->aux_count == 0) {
996fa94a07fSbrendan avl_remove(avl, aux);
997fa94a07fSbrendan kmem_free(aux, sizeof (spa_aux_t));
998fa94a07fSbrendan } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
999fa94a07fSbrendan aux->aux_pool = 0ULL;
1000fa94a07fSbrendan }
1001fa94a07fSbrendan }
1002fa94a07fSbrendan
1003fa94a07fSbrendan boolean_t
spa_aux_exists(uint64_t guid,uint64_t * pool,int * refcnt,avl_tree_t * avl)100489a89ebfSlling spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
1005fa94a07fSbrendan {
1006fa94a07fSbrendan spa_aux_t search, *found;
1007fa94a07fSbrendan
1008fa94a07fSbrendan search.aux_guid = guid;
100989a89ebfSlling found = avl_find(avl, &search, NULL);
1010fa94a07fSbrendan
1011fa94a07fSbrendan if (pool) {
1012fa94a07fSbrendan if (found)
1013fa94a07fSbrendan *pool = found->aux_pool;
1014fa94a07fSbrendan else
1015fa94a07fSbrendan *pool = 0ULL;
1016fa94a07fSbrendan }
1017fa94a07fSbrendan
101889a89ebfSlling if (refcnt) {
101989a89ebfSlling if (found)
102089a89ebfSlling *refcnt = found->aux_count;
102189a89ebfSlling else
102289a89ebfSlling *refcnt = 0;
102389a89ebfSlling }
102489a89ebfSlling
1025fa94a07fSbrendan return (found != NULL);
1026fa94a07fSbrendan }
1027fa94a07fSbrendan
1028fa94a07fSbrendan void
spa_aux_activate(vdev_t * vd,avl_tree_t * avl)1029fa94a07fSbrendan spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1030fa94a07fSbrendan {
1031fa94a07fSbrendan spa_aux_t search, *found;
1032fa94a07fSbrendan avl_index_t where;
1033fa94a07fSbrendan
1034fa94a07fSbrendan search.aux_guid = vd->vdev_guid;
1035fa94a07fSbrendan found = avl_find(avl, &search, &where);
1036fa94a07fSbrendan ASSERT(found != NULL);
1037fa94a07fSbrendan ASSERT(found->aux_pool == 0ULL);
1038fa94a07fSbrendan
1039fa94a07fSbrendan found->aux_pool = spa_guid(vd->vdev_spa);
1040fa94a07fSbrendan }
1041fa94a07fSbrendan
104299653d4eSeschrock /*
104339c23413Seschrock * Spares are tracked globally due to the following constraints:
104439c23413Seschrock *
104554811da5SToomas Soome * - A spare may be part of multiple pools.
104654811da5SToomas Soome * - A spare may be added to a pool even if it's actively in use within
104739c23413Seschrock * another pool.
104854811da5SToomas Soome * - A spare in use in any pool can only be the source of a replacement if
104939c23413Seschrock * the target is a spare in the same pool.
105039c23413Seschrock *
105139c23413Seschrock * We keep track of all spares on the system through the use of a reference
105239c23413Seschrock * counted AVL tree. When a vdev is added as a spare, or used as a replacement
105339c23413Seschrock * spare, then we bump the reference count in the AVL tree. In addition, we set
105439c23413Seschrock * the 'vdev_isspare' member to indicate that the device is a spare (active or
105539c23413Seschrock * inactive). When a spare is made active (used to replace a device in the
105639c23413Seschrock * pool), we also keep track of which pool its been made a part of.
105739c23413Seschrock *
105839c23413Seschrock * The 'spa_spare_lock' protects the AVL tree. These functions are normally
105939c23413Seschrock * called under the spa_namespace lock as part of vdev reconfiguration. The
106039c23413Seschrock * separate spare lock exists for the status query path, which does not need to
106139c23413Seschrock * be completely consistent with respect to other vdev configuration changes.
106299653d4eSeschrock */
106339c23413Seschrock
1064e830fb12SKody A Kantor /*
1065e830fb12SKody A Kantor * Poll the spare vdevs to make sure they are not faulty.
1066e830fb12SKody A Kantor *
1067e830fb12SKody A Kantor * The probe operation will raise an ENXIO error and create an FM ereport if the
1068e830fb12SKody A Kantor * probe fails.
1069e830fb12SKody A Kantor */
1070e830fb12SKody A Kantor void
spa_spare_poll(spa_t * spa)1071e830fb12SKody A Kantor spa_spare_poll(spa_t *spa)
1072e830fb12SKody A Kantor {
1073e830fb12SKody A Kantor boolean_t async_request = B_FALSE;
1074e830fb12SKody A Kantor spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1075e830fb12SKody A Kantor for (int i = 0; i < spa->spa_spares.sav_count; i++) {
1076e830fb12SKody A Kantor spa_aux_t search, *found;
1077e830fb12SKody A Kantor vdev_t *vd = spa->spa_spares.sav_vdevs[i];
1078e830fb12SKody A Kantor
1079e830fb12SKody A Kantor search.aux_guid = vd->vdev_guid;
1080e830fb12SKody A Kantor
1081e830fb12SKody A Kantor mutex_enter(&spa_spare_lock);
1082e830fb12SKody A Kantor found = avl_find(&spa_spare_avl, &search, NULL);
1083e830fb12SKody A Kantor /* This spare is in use by a pool. */
1084e830fb12SKody A Kantor if (found != NULL && found->aux_pool != 0) {
1085e830fb12SKody A Kantor mutex_exit(&spa_spare_lock);
1086e830fb12SKody A Kantor continue;
1087e830fb12SKody A Kantor }
1088e830fb12SKody A Kantor mutex_exit(&spa_spare_lock);
1089e830fb12SKody A Kantor
1090e830fb12SKody A Kantor vd->vdev_probe_wanted = B_TRUE;
1091e830fb12SKody A Kantor async_request = B_TRUE;
1092e830fb12SKody A Kantor }
1093e830fb12SKody A Kantor if (async_request)
1094e830fb12SKody A Kantor spa_async_request(spa, SPA_ASYNC_PROBE);
1095e830fb12SKody A Kantor
1096e830fb12SKody A Kantor spa_config_exit(spa, SCL_STATE, FTAG);
1097e830fb12SKody A Kantor }
1098e830fb12SKody A Kantor
109999653d4eSeschrock static int
spa_spare_compare(const void * a,const void * b)110099653d4eSeschrock spa_spare_compare(const void *a, const void *b)
110199653d4eSeschrock {
1102fa94a07fSbrendan return (spa_aux_compare(a, b));
110399653d4eSeschrock }
110499653d4eSeschrock
110599653d4eSeschrock void
spa_spare_add(vdev_t * vd)110639c23413Seschrock spa_spare_add(vdev_t *vd)
110799653d4eSeschrock {
110899653d4eSeschrock mutex_enter(&spa_spare_lock);
110939c23413Seschrock ASSERT(!vd->vdev_isspare);
1110fa94a07fSbrendan spa_aux_add(vd, &spa_spare_avl);
111139c23413Seschrock vd->vdev_isspare = B_TRUE;
111299653d4eSeschrock mutex_exit(&spa_spare_lock);
111399653d4eSeschrock }
111499653d4eSeschrock
111599653d4eSeschrock void
spa_spare_remove(vdev_t * vd)111639c23413Seschrock spa_spare_remove(vdev_t *vd)
111799653d4eSeschrock {
111899653d4eSeschrock mutex_enter(&spa_spare_lock);
111939c23413Seschrock ASSERT(vd->vdev_isspare);
1120fa94a07fSbrendan spa_aux_remove(vd, &spa_spare_avl);
112139c23413Seschrock vd->vdev_isspare = B_FALSE;
112299653d4eSeschrock mutex_exit(&spa_spare_lock);
112399653d4eSeschrock }
112499653d4eSeschrock
112599653d4eSeschrock boolean_t
spa_spare_exists(uint64_t guid,uint64_t * pool,int * refcnt)112689a89ebfSlling spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
112799653d4eSeschrock {
1128fa94a07fSbrendan boolean_t found;
112999653d4eSeschrock
113099653d4eSeschrock mutex_enter(&spa_spare_lock);
113189a89ebfSlling found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
113299653d4eSeschrock mutex_exit(&spa_spare_lock);
113399653d4eSeschrock
1134fa94a07fSbrendan return (found);
113539c23413Seschrock }
113639c23413Seschrock
113739c23413Seschrock void
spa_spare_activate(vdev_t * vd)113839c23413Seschrock spa_spare_activate(vdev_t *vd)
113939c23413Seschrock {
114039c23413Seschrock mutex_enter(&spa_spare_lock);
114139c23413Seschrock ASSERT(vd->vdev_isspare);
1142fa94a07fSbrendan spa_aux_activate(vd, &spa_spare_avl);
1143fa94a07fSbrendan mutex_exit(&spa_spare_lock);
1144fa94a07fSbrendan }
114539c23413Seschrock
1146fa94a07fSbrendan /*
1147fa94a07fSbrendan * Level 2 ARC devices are tracked globally for the same reasons as spares.
1148fa94a07fSbrendan * Cache devices currently only support one pool per cache device, and so
1149fa94a07fSbrendan * for these devices the aux reference count is currently unused beyond 1.
1150fa94a07fSbrendan */
115139c23413Seschrock
1152fa94a07fSbrendan static int
spa_l2cache_compare(const void * a,const void * b)1153fa94a07fSbrendan spa_l2cache_compare(const void *a, const void *b)
1154fa94a07fSbrendan {
1155fa94a07fSbrendan return (spa_aux_compare(a, b));
1156fa94a07fSbrendan }
1157fa94a07fSbrendan
1158fa94a07fSbrendan void
spa_l2cache_add(vdev_t * vd)1159fa94a07fSbrendan spa_l2cache_add(vdev_t *vd)
1160fa94a07fSbrendan {
1161fa94a07fSbrendan mutex_enter(&spa_l2cache_lock);
1162fa94a07fSbrendan ASSERT(!vd->vdev_isl2cache);
1163fa94a07fSbrendan spa_aux_add(vd, &spa_l2cache_avl);
1164fa94a07fSbrendan vd->vdev_isl2cache = B_TRUE;
1165fa94a07fSbrendan mutex_exit(&spa_l2cache_lock);
1166fa94a07fSbrendan }
1167fa94a07fSbrendan
1168fa94a07fSbrendan void
spa_l2cache_remove(vdev_t * vd)1169fa94a07fSbrendan spa_l2cache_remove(vdev_t *vd)
1170fa94a07fSbrendan {
1171fa94a07fSbrendan mutex_enter(&spa_l2cache_lock);
1172fa94a07fSbrendan ASSERT(vd->vdev_isl2cache);
1173fa94a07fSbrendan spa_aux_remove(vd, &spa_l2cache_avl);
1174fa94a07fSbrendan vd->vdev_isl2cache = B_FALSE;
1175fa94a07fSbrendan mutex_exit(&spa_l2cache_lock);
1176fa94a07fSbrendan }
1177fa94a07fSbrendan
1178fa94a07fSbrendan boolean_t
spa_l2cache_exists(uint64_t guid,uint64_t * pool)1179fa94a07fSbrendan spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1180fa94a07fSbrendan {
1181fa94a07fSbrendan boolean_t found;
1182fa94a07fSbrendan
1183fa94a07fSbrendan mutex_enter(&spa_l2cache_lock);
118489a89ebfSlling found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1185fa94a07fSbrendan mutex_exit(&spa_l2cache_lock);
1186fa94a07fSbrendan
1187fa94a07fSbrendan return (found);
1188fa94a07fSbrendan }
1189fa94a07fSbrendan
1190fa94a07fSbrendan void
spa_l2cache_activate(vdev_t * vd)1191fa94a07fSbrendan spa_l2cache_activate(vdev_t *vd)
1192fa94a07fSbrendan {
1193fa94a07fSbrendan mutex_enter(&spa_l2cache_lock);
1194fa94a07fSbrendan ASSERT(vd->vdev_isl2cache);
1195fa94a07fSbrendan spa_aux_activate(vd, &spa_l2cache_avl);
1196fa94a07fSbrendan mutex_exit(&spa_l2cache_lock);
1197fa94a07fSbrendan }
1198fa94a07fSbrendan
1199fa9e4066Sahrens /*
1200fa9e4066Sahrens * ==========================================================================
1201fa9e4066Sahrens * SPA vdev locking
1202fa9e4066Sahrens * ==========================================================================
1203fa9e4066Sahrens */
1204fa9e4066Sahrens
1205fa9e4066Sahrens /*
1206ea8dc4b6Seschrock * Lock the given spa_t for the purpose of adding or removing a vdev.
1207ea8dc4b6Seschrock * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1208fa9e4066Sahrens * It returns the next transaction group for the spa_t.
1209fa9e4066Sahrens */
1210fa9e4066Sahrens uint64_t
spa_vdev_enter(spa_t * spa)1211fa9e4066Sahrens spa_vdev_enter(spa_t *spa)
1212fa9e4066Sahrens {
1213a1521560SJeff Bonwick mutex_enter(&spa->spa_vdev_top_lock);
1214bbfd46c4SJeff Bonwick mutex_enter(&spa_namespace_lock);
1215084fd14fSBrian Behlendorf
1216084fd14fSBrian Behlendorf vdev_autotrim_stop_all(spa);
1217084fd14fSBrian Behlendorf
121888ecc943SGeorge Wilson return (spa_vdev_config_enter(spa));
121988ecc943SGeorge Wilson }
122088ecc943SGeorge Wilson
122188ecc943SGeorge Wilson /*
122288ecc943SGeorge Wilson * Internal implementation for spa_vdev_enter(). Used when a vdev
122388ecc943SGeorge Wilson * operation requires multiple syncs (i.e. removing a device) while
122488ecc943SGeorge Wilson * keeping the spa_namespace_lock held.
122588ecc943SGeorge Wilson */
122688ecc943SGeorge Wilson uint64_t
spa_vdev_config_enter(spa_t * spa)122788ecc943SGeorge Wilson spa_vdev_config_enter(spa_t *spa)
122888ecc943SGeorge Wilson {
122988ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock));
12303d7072f8Seschrock
1231e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1232fa9e4066Sahrens
1233fa9e4066Sahrens return (spa_last_synced_txg(spa) + 1);
1234fa9e4066Sahrens }
1235fa9e4066Sahrens
1236fa9e4066Sahrens /*
123788ecc943SGeorge Wilson * Used in combination with spa_vdev_config_enter() to allow the syncing
123888ecc943SGeorge Wilson * of multiple transactions without releasing the spa_namespace_lock.
1239fa9e4066Sahrens */
124088ecc943SGeorge Wilson void
spa_vdev_config_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error,char * tag)124188ecc943SGeorge Wilson spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1242fa9e4066Sahrens {
124388ecc943SGeorge Wilson ASSERT(MUTEX_HELD(&spa_namespace_lock));
124488ecc943SGeorge Wilson
12450e34b6a7Sbonwick int config_changed = B_FALSE;
1246ea8dc4b6Seschrock
12470373e76bSbonwick ASSERT(txg > spa_last_synced_txg(spa));
12480e34b6a7Sbonwick
1249e14bb325SJeff Bonwick spa->spa_pending_vdev = NULL;
1250e14bb325SJeff Bonwick
12510e34b6a7Sbonwick /*
12520e34b6a7Sbonwick * Reassess the DTLs.
12530e34b6a7Sbonwick */
12540373e76bSbonwick vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
12550e34b6a7Sbonwick
1256e14bb325SJeff Bonwick if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
12570e34b6a7Sbonwick config_changed = B_TRUE;
12588f18d1faSGeorge Wilson spa->spa_config_generation++;
12590e34b6a7Sbonwick }
1260ea8dc4b6Seschrock
126188ecc943SGeorge Wilson /*
126288ecc943SGeorge Wilson * Verify the metaslab classes.
126388ecc943SGeorge Wilson */
1264b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1265b24ab676SJeff Bonwick ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1266663207adSDon Brady ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1267663207adSDon Brady ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
126888ecc943SGeorge Wilson
1269e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, spa);
1270fa9e4066Sahrens
127188ecc943SGeorge Wilson /*
127288ecc943SGeorge Wilson * Panic the system if the specified tag requires it. This
127388ecc943SGeorge Wilson * is useful for ensuring that configurations are updated
127488ecc943SGeorge Wilson * transactionally.
127588ecc943SGeorge Wilson */
127688ecc943SGeorge Wilson if (zio_injection_enabled)
12771195e687SMark J Musante zio_handle_panic_injection(spa, tag, 0);
127888ecc943SGeorge Wilson
1279fa9e4066Sahrens /*
1280fa9e4066Sahrens * Note: this txg_wait_synced() is important because it ensures
1281fa9e4066Sahrens * that there won't be more than one config change per txg.
1282fa9e4066Sahrens * This allows us to use the txg as the generation number.
1283fa9e4066Sahrens */
1284fa9e4066Sahrens if (error == 0)
1285fa9e4066Sahrens txg_wait_synced(spa->spa_dsl_pool, txg);
1286fa9e4066Sahrens
1287fa9e4066Sahrens if (vd != NULL) {
12880713e232SGeorge Wilson ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1289094e47e9SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) {
1290094e47e9SGeorge Wilson mutex_enter(&vd->vdev_initialize_lock);
1291084fd14fSBrian Behlendorf vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1292084fd14fSBrian Behlendorf NULL);
1293094e47e9SGeorge Wilson mutex_exit(&vd->vdev_initialize_lock);
1294084fd14fSBrian Behlendorf
1295084fd14fSBrian Behlendorf mutex_enter(&vd->vdev_trim_lock);
1296084fd14fSBrian Behlendorf vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1297084fd14fSBrian Behlendorf mutex_exit(&vd->vdev_trim_lock);
1298094e47e9SGeorge Wilson }
1299094e47e9SGeorge Wilson
1300084fd14fSBrian Behlendorf /*
1301084fd14fSBrian Behlendorf * The vdev may be both a leaf and top-level device.
1302084fd14fSBrian Behlendorf */
1303084fd14fSBrian Behlendorf vdev_autotrim_stop_wait(vd);
1304084fd14fSBrian Behlendorf
13058ad4d6ddSJeff Bonwick spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1306fa9e4066Sahrens vdev_free(vd);
13078ad4d6ddSJeff Bonwick spa_config_exit(spa, SCL_ALL, spa);
1308fa9e4066Sahrens }
1309fa9e4066Sahrens
1310fa9e4066Sahrens /*
13110e34b6a7Sbonwick * If the config changed, update the config cache.
1312fa9e4066Sahrens */
13130e34b6a7Sbonwick if (config_changed)
13145cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE);
131588ecc943SGeorge Wilson }
1316ea8dc4b6Seschrock
131788ecc943SGeorge Wilson /*
131888ecc943SGeorge Wilson * Unlock the spa_t after adding or removing a vdev. Besides undoing the
131988ecc943SGeorge Wilson * locking of spa_vdev_enter(), we also want make sure the transactions have
132088ecc943SGeorge Wilson * synced to disk, and then update the global configuration cache with the new
132188ecc943SGeorge Wilson * information.
132288ecc943SGeorge Wilson */
132388ecc943SGeorge Wilson int
spa_vdev_exit(spa_t * spa,vdev_t * vd,uint64_t txg,int error)132488ecc943SGeorge Wilson spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
132588ecc943SGeorge Wilson {
1326084fd14fSBrian Behlendorf vdev_autotrim_restart(spa);
1327084fd14fSBrian Behlendorf
132888ecc943SGeorge Wilson spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1329ea8dc4b6Seschrock mutex_exit(&spa_namespace_lock);
1330bbfd46c4SJeff Bonwick mutex_exit(&spa->spa_vdev_top_lock);
1331fa9e4066Sahrens
1332fa9e4066Sahrens return (error);
1333fa9e4066Sahrens }
1334fa9e4066Sahrens
1335e14bb325SJeff Bonwick /*
1336e14bb325SJeff Bonwick * Lock the given spa_t for the purpose of changing vdev state.
1337e14bb325SJeff Bonwick */
1338e14bb325SJeff Bonwick void
spa_vdev_state_enter(spa_t * spa,int oplocks)13398f18d1faSGeorge Wilson spa_vdev_state_enter(spa_t *spa, int oplocks)
1340e14bb325SJeff Bonwick {
13418f18d1faSGeorge Wilson int locks = SCL_STATE_ALL | oplocks;
13428f18d1faSGeorge Wilson
1343dcba9f3fSGeorge Wilson /*
1344dcba9f3fSGeorge Wilson * Root pools may need to read of the underlying devfs filesystem
1345dcba9f3fSGeorge Wilson * when opening up a vdev. Unfortunately if we're holding the
1346dcba9f3fSGeorge Wilson * SCL_ZIO lock it will result in a deadlock when we try to issue
1347dcba9f3fSGeorge Wilson * the read from the root filesystem. Instead we "prefetch"
1348dcba9f3fSGeorge Wilson * the associated vnodes that we need prior to opening the
1349dcba9f3fSGeorge Wilson * underlying devices and cache them so that we can prevent
1350dcba9f3fSGeorge Wilson * any I/O when we are doing the actual open.
1351dcba9f3fSGeorge Wilson */
1352dcba9f3fSGeorge Wilson if (spa_is_root(spa)) {
13539842588bSGeorge Wilson int low = locks & ~(SCL_ZIO - 1);
13549842588bSGeorge Wilson int high = locks & ~low;
13559842588bSGeorge Wilson
13569842588bSGeorge Wilson spa_config_enter(spa, high, spa, RW_WRITER);
1357dcba9f3fSGeorge Wilson vdev_hold(spa->spa_root_vdev);
13589842588bSGeorge Wilson spa_config_enter(spa, low, spa, RW_WRITER);
1359dcba9f3fSGeorge Wilson } else {
1360dcba9f3fSGeorge Wilson spa_config_enter(spa, locks, spa, RW_WRITER);
1361dcba9f3fSGeorge Wilson }
13628f18d1faSGeorge Wilson spa->spa_vdev_locks = locks;
1363e14bb325SJeff Bonwick }
1364e14bb325SJeff Bonwick
1365e14bb325SJeff Bonwick int
spa_vdev_state_exit(spa_t * spa,vdev_t * vd,int error)1366e14bb325SJeff Bonwick spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1367e14bb325SJeff Bonwick {
1368c6065d0fSGeorge Wilson boolean_t config_changed = B_FALSE;
1369c6065d0fSGeorge Wilson
1370b24ab676SJeff Bonwick if (vd != NULL || error == 0)
1371b24ab676SJeff Bonwick vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1372b24ab676SJeff Bonwick 0, 0, B_FALSE);
1373b24ab676SJeff Bonwick
13748f18d1faSGeorge Wilson if (vd != NULL) {
1375e14bb325SJeff Bonwick vdev_state_dirty(vd->vdev_top);
1376c6065d0fSGeorge Wilson config_changed = B_TRUE;
13778f18d1faSGeorge Wilson spa->spa_config_generation++;
13788f18d1faSGeorge Wilson }
1379e14bb325SJeff Bonwick
1380dcba9f3fSGeorge Wilson if (spa_is_root(spa))
1381dcba9f3fSGeorge Wilson vdev_rele(spa->spa_root_vdev);
1382dcba9f3fSGeorge Wilson
13838f18d1faSGeorge Wilson ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
13848f18d1faSGeorge Wilson spa_config_exit(spa, spa->spa_vdev_locks, spa);
1385e14bb325SJeff Bonwick
13868ad4d6ddSJeff Bonwick /*
13878ad4d6ddSJeff Bonwick * If anything changed, wait for it to sync. This ensures that,
1388bbf21555SRichard Lowe * from the system administrator's perspective, zpool(8) commands
13898ad4d6ddSJeff Bonwick * are synchronous. This is important for things like zpool offline:
13908ad4d6ddSJeff Bonwick * when the command completes, you expect no further I/O from ZFS.
13918ad4d6ddSJeff Bonwick */
13928ad4d6ddSJeff Bonwick if (vd != NULL)
13938ad4d6ddSJeff Bonwick txg_wait_synced(spa->spa_dsl_pool, 0);
13948ad4d6ddSJeff Bonwick
1395c6065d0fSGeorge Wilson /*
1396c6065d0fSGeorge Wilson * If the config changed, update the config cache.
1397c6065d0fSGeorge Wilson */
1398c6065d0fSGeorge Wilson if (config_changed) {
1399c6065d0fSGeorge Wilson mutex_enter(&spa_namespace_lock);
14005cabbc6bSPrashanth Sreenivasa spa_write_cachefile(spa, B_FALSE, B_TRUE);
1401c6065d0fSGeorge Wilson mutex_exit(&spa_namespace_lock);
1402c6065d0fSGeorge Wilson }
1403c6065d0fSGeorge Wilson
1404e14bb325SJeff Bonwick return (error);
1405e14bb325SJeff Bonwick }
1406e14bb325SJeff Bonwick
1407fa9e4066Sahrens /*
1408fa9e4066Sahrens * ==========================================================================
1409fa9e4066Sahrens * Miscellaneous functions
1410fa9e4066Sahrens * ==========================================================================
1411fa9e4066Sahrens */
1412fa9e4066Sahrens
1413ad135b5dSChristopher Siden void
spa_activate_mos_feature(spa_t * spa,const char * feature,dmu_tx_t * tx)141443466aaeSMax Grossman spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1415ad135b5dSChristopher Siden {
14162acef22dSMatthew Ahrens if (!nvlist_exists(spa->spa_label_features, feature)) {
14172acef22dSMatthew Ahrens fnvlist_add_boolean(spa->spa_label_features, feature);
141843466aaeSMax Grossman /*
141943466aaeSMax Grossman * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
142043466aaeSMax Grossman * dirty the vdev config because lock SCL_CONFIG is not held.
142143466aaeSMax Grossman * Thankfully, in this case we don't need to dirty the config
142243466aaeSMax Grossman * because it will be written out anyway when we finish
142343466aaeSMax Grossman * creating the pool.
142443466aaeSMax Grossman */
142543466aaeSMax Grossman if (tx->tx_txg != TXG_INITIAL)
142643466aaeSMax Grossman vdev_config_dirty(spa->spa_root_vdev);
14272acef22dSMatthew Ahrens }
1428ad135b5dSChristopher Siden }
1429ad135b5dSChristopher Siden
1430ad135b5dSChristopher Siden void
spa_deactivate_mos_feature(spa_t * spa,const char * feature)1431ad135b5dSChristopher Siden spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1432ad135b5dSChristopher Siden {
14332acef22dSMatthew Ahrens if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
14342acef22dSMatthew Ahrens vdev_config_dirty(spa->spa_root_vdev);
1435ad135b5dSChristopher Siden }
1436ad135b5dSChristopher Siden
1437fa9e4066Sahrens /*
1438f9af39baSGeorge Wilson * Return the spa_t associated with given pool_guid, if it exists. If
1439f9af39baSGeorge Wilson * device_guid is non-zero, determine whether the pool exists *and* contains
1440f9af39baSGeorge Wilson * a device with the specified device_guid.
1441fa9e4066Sahrens */
1442f9af39baSGeorge Wilson spa_t *
spa_by_guid(uint64_t pool_guid,uint64_t device_guid)1443f9af39baSGeorge Wilson spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1444fa9e4066Sahrens {
1445fa9e4066Sahrens spa_t *spa;
1446fa9e4066Sahrens avl_tree_t *t = &spa_namespace_avl;
1447fa9e4066Sahrens
1448ea8dc4b6Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock));
1449fa9e4066Sahrens
1450fa9e4066Sahrens for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1451fa9e4066Sahrens if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1452fa9e4066Sahrens continue;
1453fa9e4066Sahrens if (spa->spa_root_vdev == NULL)
1454fa9e4066Sahrens continue;
145539c23413Seschrock if (spa_guid(spa) == pool_guid) {
145639c23413Seschrock if (device_guid == 0)
145739c23413Seschrock break;
145839c23413Seschrock
145939c23413Seschrock if (vdev_lookup_by_guid(spa->spa_root_vdev,
146039c23413Seschrock device_guid) != NULL)
146139c23413Seschrock break;
146239c23413Seschrock
146339c23413Seschrock /*
14648654d025Sperrin * Check any devices we may be in the process of adding.
146539c23413Seschrock */
146639c23413Seschrock if (spa->spa_pending_vdev) {
146739c23413Seschrock if (vdev_lookup_by_guid(spa->spa_pending_vdev,
146839c23413Seschrock device_guid) != NULL)
146939c23413Seschrock break;
147039c23413Seschrock }
147139c23413Seschrock }
1472fa9e4066Sahrens }
1473fa9e4066Sahrens
1474f9af39baSGeorge Wilson return (spa);
1475f9af39baSGeorge Wilson }
1476f9af39baSGeorge Wilson
1477f9af39baSGeorge Wilson /*
1478f9af39baSGeorge Wilson * Determine whether a pool with the given pool_guid exists.
1479f9af39baSGeorge Wilson */
1480f9af39baSGeorge Wilson boolean_t
spa_guid_exists(uint64_t pool_guid,uint64_t device_guid)1481f9af39baSGeorge Wilson spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1482f9af39baSGeorge Wilson {
1483f9af39baSGeorge Wilson return (spa_by_guid(pool_guid, device_guid) != NULL);
1484fa9e4066Sahrens }
1485fa9e4066Sahrens
1486fa9e4066Sahrens char *
spa_strdup(const char * s)1487fa9e4066Sahrens spa_strdup(const char *s)
1488fa9e4066Sahrens {
1489fa9e4066Sahrens size_t len;
1490fa9e4066Sahrens char *new;
1491fa9e4066Sahrens
1492fa9e4066Sahrens len = strlen(s);
1493fa9e4066Sahrens new = kmem_alloc(len + 1, KM_SLEEP);
1494fa9e4066Sahrens bcopy(s, new, len);
1495fa9e4066Sahrens new[len] = '\0';
1496fa9e4066Sahrens
1497fa9e4066Sahrens return (new);
1498fa9e4066Sahrens }
1499fa9e4066Sahrens
1500fa9e4066Sahrens void
spa_strfree(char * s)1501fa9e4066Sahrens spa_strfree(char *s)
1502fa9e4066Sahrens {
1503fa9e4066Sahrens kmem_free(s, strlen(s) + 1);
1504fa9e4066Sahrens }
1505fa9e4066Sahrens
1506fa9e4066Sahrens uint64_t
spa_get_random(uint64_t range)1507fa9e4066Sahrens spa_get_random(uint64_t range)
1508fa9e4066Sahrens {
1509fa9e4066Sahrens uint64_t r;
1510fa9e4066Sahrens
1511fa9e4066Sahrens ASSERT(range != 0);
1512fa9e4066Sahrens
1513e0f1c0afSOlaf Faaland if (range == 1)
1514e0f1c0afSOlaf Faaland return (0);
1515e0f1c0afSOlaf Faaland
1516fa9e4066Sahrens (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1517fa9e4066Sahrens
1518fa9e4066Sahrens return (r % range);
1519fa9e4066Sahrens }
1520fa9e4066Sahrens
15211195e687SMark J Musante uint64_t
spa_generate_guid(spa_t * spa)15221195e687SMark J Musante spa_generate_guid(spa_t *spa)
15231195e687SMark J Musante {
15241195e687SMark J Musante uint64_t guid = spa_get_random(-1ULL);
15251195e687SMark J Musante
15261195e687SMark J Musante if (spa != NULL) {
15271195e687SMark J Musante while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
15281195e687SMark J Musante guid = spa_get_random(-1ULL);
15291195e687SMark J Musante } else {
15301195e687SMark J Musante while (guid == 0 || spa_guid_exists(guid, 0))
15311195e687SMark J Musante guid = spa_get_random(-1ULL);
15321195e687SMark J Musante }
15331195e687SMark J Musante
15341195e687SMark J Musante return (guid);
15351195e687SMark J Musante }
15361195e687SMark J Musante
1537fa9e4066Sahrens void
snprintf_blkptr(char * buf,size_t buflen,const blkptr_t * bp)153843466aaeSMax Grossman snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1539fa9e4066Sahrens {
1540ad135b5dSChristopher Siden char type[256];
1541f0ba89beSJeff Bonwick char *checksum = NULL;
1542f0ba89beSJeff Bonwick char *compress = NULL;
1543f0ba89beSJeff Bonwick
1544f0ba89beSJeff Bonwick if (bp != NULL) {
1545ad135b5dSChristopher Siden if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1546ad135b5dSChristopher Siden dmu_object_byteswap_t bswap =
1547ad135b5dSChristopher Siden DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1548ad135b5dSChristopher Siden (void) snprintf(type, sizeof (type), "bswap %s %s",
1549ad135b5dSChristopher Siden DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1550ad135b5dSChristopher Siden "metadata" : "data",
1551ad135b5dSChristopher Siden dmu_ot_byteswap[bswap].ob_name);
1552ad135b5dSChristopher Siden } else {
1553ad135b5dSChristopher Siden (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1554ad135b5dSChristopher Siden sizeof (type));
1555ad135b5dSChristopher Siden }
15565d7b4d43SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) {
15575d7b4d43SMatthew Ahrens checksum =
15585d7b4d43SMatthew Ahrens zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
15595d7b4d43SMatthew Ahrens }
1560f0ba89beSJeff Bonwick compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1561f0ba89beSJeff Bonwick }
1562fa9e4066Sahrens
156343466aaeSMax Grossman SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
156443466aaeSMax Grossman compress);
1565fa9e4066Sahrens }
1566fa9e4066Sahrens
1567fa9e4066Sahrens void
spa_freeze(spa_t * spa)1568fa9e4066Sahrens spa_freeze(spa_t *spa)
1569fa9e4066Sahrens {
1570fa9e4066Sahrens uint64_t freeze_txg = 0;
1571fa9e4066Sahrens
1572e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1573fa9e4066Sahrens if (spa->spa_freeze_txg == UINT64_MAX) {
1574fa9e4066Sahrens freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1575fa9e4066Sahrens spa->spa_freeze_txg = freeze_txg;
1576fa9e4066Sahrens }
1577e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
1578fa9e4066Sahrens if (freeze_txg != 0)
1579fa9e4066Sahrens txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1580fa9e4066Sahrens }
1581fa9e4066Sahrens
15820125049cSahrens void
zfs_panic_recover(const char * fmt,...)15830125049cSahrens zfs_panic_recover(const char *fmt, ...)
15840125049cSahrens {
15850125049cSahrens va_list adx;
15860125049cSahrens
15870125049cSahrens va_start(adx, fmt);
15880125049cSahrens vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
15890125049cSahrens va_end(adx);
15900125049cSahrens }
15910125049cSahrens
15923f9d6ad7SLin Ling /*
15933f9d6ad7SLin Ling * This is a stripped-down version of strtoull, suitable only for converting
1594f7170741SWill Andrews * lowercase hexadecimal numbers that don't overflow.
15953f9d6ad7SLin Ling */
15963f9d6ad7SLin Ling uint64_t
zfs_strtonum(const char * str,char ** nptr)15974585130bSYuri Pankov zfs_strtonum(const char *str, char **nptr)
15983f9d6ad7SLin Ling {
15993f9d6ad7SLin Ling uint64_t val = 0;
16003f9d6ad7SLin Ling char c;
16013f9d6ad7SLin Ling int digit;
16023f9d6ad7SLin Ling
16033f9d6ad7SLin Ling while ((c = *str) != '\0') {
16043f9d6ad7SLin Ling if (c >= '0' && c <= '9')
16053f9d6ad7SLin Ling digit = c - '0';
16063f9d6ad7SLin Ling else if (c >= 'a' && c <= 'f')
16073f9d6ad7SLin Ling digit = 10 + c - 'a';
16083f9d6ad7SLin Ling else
16093f9d6ad7SLin Ling break;
16103f9d6ad7SLin Ling
16113f9d6ad7SLin Ling val *= 16;
16123f9d6ad7SLin Ling val += digit;
16133f9d6ad7SLin Ling
16143f9d6ad7SLin Ling str++;
16153f9d6ad7SLin Ling }
16163f9d6ad7SLin Ling
16173f9d6ad7SLin Ling if (nptr)
16183f9d6ad7SLin Ling *nptr = (char *)str;
16193f9d6ad7SLin Ling
16203f9d6ad7SLin Ling return (val);
16213f9d6ad7SLin Ling }
16223f9d6ad7SLin Ling
1623663207adSDon Brady void
spa_activate_allocation_classes(spa_t * spa,dmu_tx_t * tx)1624663207adSDon Brady spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1625663207adSDon Brady {
1626663207adSDon Brady /*
1627663207adSDon Brady * We bump the feature refcount for each special vdev added to the pool
1628663207adSDon Brady */
1629663207adSDon Brady ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1630663207adSDon Brady spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1631663207adSDon Brady }
1632663207adSDon Brady
1633fa9e4066Sahrens /*
1634fa9e4066Sahrens * ==========================================================================
1635fa9e4066Sahrens * Accessor functions
1636fa9e4066Sahrens * ==========================================================================
1637fa9e4066Sahrens */
1638fa9e4066Sahrens
1639088f3894Sahrens boolean_t
spa_shutting_down(spa_t * spa)164088b7b0f2SMatthew Ahrens spa_shutting_down(spa_t *spa)
1641fa9e4066Sahrens {
164288b7b0f2SMatthew Ahrens return (spa->spa_async_suspended);
1643fa9e4066Sahrens }
1644fa9e4066Sahrens
1645fa9e4066Sahrens dsl_pool_t *
spa_get_dsl(spa_t * spa)1646fa9e4066Sahrens spa_get_dsl(spa_t *spa)
1647fa9e4066Sahrens {
1648fa9e4066Sahrens return (spa->spa_dsl_pool);
1649fa9e4066Sahrens }
1650fa9e4066Sahrens
1651ad135b5dSChristopher Siden boolean_t
spa_is_initializing(spa_t * spa)1652ad135b5dSChristopher Siden spa_is_initializing(spa_t *spa)
1653ad135b5dSChristopher Siden {
1654ad135b5dSChristopher Siden return (spa->spa_is_initializing);
1655ad135b5dSChristopher Siden }
1656ad135b5dSChristopher Siden
16575cabbc6bSPrashanth Sreenivasa boolean_t
spa_indirect_vdevs_loaded(spa_t * spa)16585cabbc6bSPrashanth Sreenivasa spa_indirect_vdevs_loaded(spa_t *spa)
16595cabbc6bSPrashanth Sreenivasa {
16605cabbc6bSPrashanth Sreenivasa return (spa->spa_indirect_vdevs_loaded);
16615cabbc6bSPrashanth Sreenivasa }
16625cabbc6bSPrashanth Sreenivasa
1663fa9e4066Sahrens blkptr_t *
spa_get_rootblkptr(spa_t * spa)1664fa9e4066Sahrens spa_get_rootblkptr(spa_t *spa)
1665fa9e4066Sahrens {
1666fa9e4066Sahrens return (&spa->spa_ubsync.ub_rootbp);
1667fa9e4066Sahrens }
1668fa9e4066Sahrens
1669fa9e4066Sahrens void
spa_set_rootblkptr(spa_t * spa,const blkptr_t * bp)1670fa9e4066Sahrens spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1671fa9e4066Sahrens {
1672fa9e4066Sahrens spa->spa_uberblock.ub_rootbp = *bp;
1673fa9e4066Sahrens }
1674fa9e4066Sahrens
1675fa9e4066Sahrens void
spa_altroot(spa_t * spa,char * buf,size_t buflen)1676fa9e4066Sahrens spa_altroot(spa_t *spa, char *buf, size_t buflen)
1677fa9e4066Sahrens {
1678fa9e4066Sahrens if (spa->spa_root == NULL)
1679fa9e4066Sahrens buf[0] = '\0';
1680fa9e4066Sahrens else
1681fa9e4066Sahrens (void) strncpy(buf, spa->spa_root, buflen);
1682fa9e4066Sahrens }
1683fa9e4066Sahrens
1684fa9e4066Sahrens int
spa_sync_pass(spa_t * spa)1685fa9e4066Sahrens spa_sync_pass(spa_t *spa)
1686fa9e4066Sahrens {
1687fa9e4066Sahrens return (spa->spa_sync_pass);
1688fa9e4066Sahrens }
1689fa9e4066Sahrens
1690fa9e4066Sahrens char *
spa_name(spa_t * spa)1691fa9e4066Sahrens spa_name(spa_t *spa)
1692fa9e4066Sahrens {
1693fa9e4066Sahrens return (spa->spa_name);
1694fa9e4066Sahrens }
1695fa9e4066Sahrens
1696fa9e4066Sahrens uint64_t
spa_guid(spa_t * spa)1697fa9e4066Sahrens spa_guid(spa_t *spa)
1698fa9e4066Sahrens {
1699dfbb9432SGeorge Wilson dsl_pool_t *dp = spa_get_dsl(spa);
1700dfbb9432SGeorge Wilson uint64_t guid;
1701dfbb9432SGeorge Wilson
1702b5989ec7Seschrock /*
1703b5989ec7Seschrock * If we fail to parse the config during spa_load(), we can go through
1704b5989ec7Seschrock * the error path (which posts an ereport) and end up here with no root
1705e9103aaeSGarrett D'Amore * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1706b5989ec7Seschrock * this case.
1707b5989ec7Seschrock */
1708dfbb9432SGeorge Wilson if (spa->spa_root_vdev == NULL)
1709dfbb9432SGeorge Wilson return (spa->spa_config_guid);
1710dfbb9432SGeorge Wilson
1711dfbb9432SGeorge Wilson guid = spa->spa_last_synced_guid != 0 ?
1712dfbb9432SGeorge Wilson spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1713dfbb9432SGeorge Wilson
1714dfbb9432SGeorge Wilson /*
1715dfbb9432SGeorge Wilson * Return the most recently synced out guid unless we're
1716dfbb9432SGeorge Wilson * in syncing context.
1717dfbb9432SGeorge Wilson */
1718dfbb9432SGeorge Wilson if (dp && dsl_pool_sync_context(dp))
1719b5989ec7Seschrock return (spa->spa_root_vdev->vdev_guid);
1720b5989ec7Seschrock else
1721dfbb9432SGeorge Wilson return (guid);
1722e9103aaeSGarrett D'Amore }
1723e9103aaeSGarrett D'Amore
1724e9103aaeSGarrett D'Amore uint64_t
spa_load_guid(spa_t * spa)1725e9103aaeSGarrett D'Amore spa_load_guid(spa_t *spa)
1726e9103aaeSGarrett D'Amore {
1727e9103aaeSGarrett D'Amore /*
1728e9103aaeSGarrett D'Amore * This is a GUID that exists solely as a reference for the
1729e9103aaeSGarrett D'Amore * purposes of the arc. It is generated at load time, and
1730e9103aaeSGarrett D'Amore * is never written to persistent storage.
1731e9103aaeSGarrett D'Amore */
1732e9103aaeSGarrett D'Amore return (spa->spa_load_guid);
1733fa9e4066Sahrens }
1734fa9e4066Sahrens
1735fa9e4066Sahrens uint64_t
spa_last_synced_txg(spa_t * spa)1736fa9e4066Sahrens spa_last_synced_txg(spa_t *spa)
1737fa9e4066Sahrens {
1738fa9e4066Sahrens return (spa->spa_ubsync.ub_txg);
1739fa9e4066Sahrens }
1740fa9e4066Sahrens
1741fa9e4066Sahrens uint64_t
spa_first_txg(spa_t * spa)1742fa9e4066Sahrens spa_first_txg(spa_t *spa)
1743fa9e4066Sahrens {
1744fa9e4066Sahrens return (spa->spa_first_txg);
1745fa9e4066Sahrens }
1746fa9e4066Sahrens
1747b24ab676SJeff Bonwick uint64_t
spa_syncing_txg(spa_t * spa)1748b24ab676SJeff Bonwick spa_syncing_txg(spa_t *spa)
1749b24ab676SJeff Bonwick {
1750b24ab676SJeff Bonwick return (spa->spa_syncing_txg);
1751b24ab676SJeff Bonwick }
1752b24ab676SJeff Bonwick
17533991b535SGeorge Wilson /*
17543991b535SGeorge Wilson * Return the last txg where data can be dirtied. The final txgs
17553991b535SGeorge Wilson * will be used to just clear out any deferred frees that remain.
17563991b535SGeorge Wilson */
17573991b535SGeorge Wilson uint64_t
spa_final_dirty_txg(spa_t * spa)17583991b535SGeorge Wilson spa_final_dirty_txg(spa_t *spa)
17593991b535SGeorge Wilson {
17603991b535SGeorge Wilson return (spa->spa_final_txg - TXG_DEFER_SIZE);
17613991b535SGeorge Wilson }
17623991b535SGeorge Wilson
176388b7b0f2SMatthew Ahrens pool_state_t
spa_state(spa_t * spa)1764fa9e4066Sahrens spa_state(spa_t *spa)
1765fa9e4066Sahrens {
1766fa9e4066Sahrens return (spa->spa_state);
1767fa9e4066Sahrens }
1768fa9e4066Sahrens
1769b16da2e2SGeorge Wilson spa_load_state_t
spa_load_state(spa_t * spa)1770b16da2e2SGeorge Wilson spa_load_state(spa_t *spa)
1771b16da2e2SGeorge Wilson {
1772b16da2e2SGeorge Wilson return (spa->spa_load_state);
1773b16da2e2SGeorge Wilson }
1774b16da2e2SGeorge Wilson
1775fa9e4066Sahrens uint64_t
spa_freeze_txg(spa_t * spa)1776fa9e4066Sahrens spa_freeze_txg(spa_t *spa)
1777fa9e4066Sahrens {
1778fa9e4066Sahrens return (spa->spa_freeze_txg);
1779fa9e4066Sahrens }
1780fa9e4066Sahrens
1781fa9e4066Sahrens /* ARGSUSED */
1782fa9e4066Sahrens uint64_t
spa_get_worst_case_asize(spa_t * spa,uint64_t lsize)178361e255ceSMatthew Ahrens spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1784fa9e4066Sahrens {
178569962b56SMatthew Ahrens return (lsize * spa_asize_inflation);
178644cd46caSbillm }
178744cd46caSbillm
17887d46dc6cSMatthew Ahrens /*
17897d46dc6cSMatthew Ahrens * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
17904b5c8e93SMatthew Ahrens * or at least 128MB, unless that would cause it to be more than half the
17914b5c8e93SMatthew Ahrens * pool size.
17927d46dc6cSMatthew Ahrens *
17937d46dc6cSMatthew Ahrens * See the comment above spa_slop_shift for details.
17947d46dc6cSMatthew Ahrens */
17957d46dc6cSMatthew Ahrens uint64_t
spa_get_slop_space(spa_t * spa)17964b5c8e93SMatthew Ahrens spa_get_slop_space(spa_t *spa)
17974b5c8e93SMatthew Ahrens {
17987d46dc6cSMatthew Ahrens uint64_t space = spa_get_dspace(spa);
17994b5c8e93SMatthew Ahrens return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
18007d46dc6cSMatthew Ahrens }
18017d46dc6cSMatthew Ahrens
1802485bbbf5SGeorge Wilson uint64_t
spa_get_dspace(spa_t * spa)1803485bbbf5SGeorge Wilson spa_get_dspace(spa_t *spa)
1804485bbbf5SGeorge Wilson {
1805485bbbf5SGeorge Wilson return (spa->spa_dspace);
1806485bbbf5SGeorge Wilson }
1807485bbbf5SGeorge Wilson
180886714001SSerapheim Dimitropoulos uint64_t
spa_get_checkpoint_space(spa_t * spa)180986714001SSerapheim Dimitropoulos spa_get_checkpoint_space(spa_t *spa)
181086714001SSerapheim Dimitropoulos {
181186714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_info.sci_dspace);
181286714001SSerapheim Dimitropoulos }
181386714001SSerapheim Dimitropoulos
1814485bbbf5SGeorge Wilson void
spa_update_dspace(spa_t * spa)1815485bbbf5SGeorge Wilson spa_update_dspace(spa_t *spa)
1816485bbbf5SGeorge Wilson {
1817485bbbf5SGeorge Wilson spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1818485bbbf5SGeorge Wilson ddt_get_dedup_dspace(spa);
18195cabbc6bSPrashanth Sreenivasa if (spa->spa_vdev_removal != NULL) {
18205cabbc6bSPrashanth Sreenivasa /*
18215cabbc6bSPrashanth Sreenivasa * We can't allocate from the removing device, so
18225cabbc6bSPrashanth Sreenivasa * subtract its size. This prevents the DMU/DSL from
18235cabbc6bSPrashanth Sreenivasa * filling up the (now smaller) pool while we are in the
18245cabbc6bSPrashanth Sreenivasa * middle of removing the device.
18255cabbc6bSPrashanth Sreenivasa *
18265cabbc6bSPrashanth Sreenivasa * Note that the DMU/DSL doesn't actually know or care
18275cabbc6bSPrashanth Sreenivasa * how much space is allocated (it does its own tracking
18285cabbc6bSPrashanth Sreenivasa * of how much space has been logically used). So it
18295cabbc6bSPrashanth Sreenivasa * doesn't matter that the data we are moving may be
18305cabbc6bSPrashanth Sreenivasa * allocated twice (on the old device and the new
18315cabbc6bSPrashanth Sreenivasa * device).
18325cabbc6bSPrashanth Sreenivasa */
18333a4b1be9SMatthew Ahrens spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
18343a4b1be9SMatthew Ahrens vdev_t *vd =
18353a4b1be9SMatthew Ahrens vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
18365cabbc6bSPrashanth Sreenivasa spa->spa_dspace -= spa_deflate(spa) ?
18375cabbc6bSPrashanth Sreenivasa vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
18383a4b1be9SMatthew Ahrens spa_config_exit(spa, SCL_VDEV, FTAG);
18395cabbc6bSPrashanth Sreenivasa }
1840485bbbf5SGeorge Wilson }
1841485bbbf5SGeorge Wilson
18420a4e9518Sgw /*
18430a4e9518Sgw * Return the failure mode that has been set to this pool. The default
18440a4e9518Sgw * behavior will be to block all I/Os when a complete failure occurs.
18450a4e9518Sgw */
18460a4e9518Sgw uint8_t
spa_get_failmode(spa_t * spa)18470a4e9518Sgw spa_get_failmode(spa_t *spa)
18480a4e9518Sgw {
18490a4e9518Sgw return (spa->spa_failmode);
18500a4e9518Sgw }
18510a4e9518Sgw
1852e14bb325SJeff Bonwick boolean_t
spa_suspended(spa_t * spa)1853e14bb325SJeff Bonwick spa_suspended(spa_t *spa)
1854e14bb325SJeff Bonwick {
1855e0f1c0afSOlaf Faaland return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1856e14bb325SJeff Bonwick }
1857e14bb325SJeff Bonwick
185844cd46caSbillm uint64_t
spa_version(spa_t * spa)185944cd46caSbillm spa_version(spa_t *spa)
186044cd46caSbillm {
186144cd46caSbillm return (spa->spa_ubsync.ub_version);
186244cd46caSbillm }
186344cd46caSbillm
1864b24ab676SJeff Bonwick boolean_t
spa_deflate(spa_t * spa)1865b24ab676SJeff Bonwick spa_deflate(spa_t *spa)
1866b24ab676SJeff Bonwick {
1867b24ab676SJeff Bonwick return (spa->spa_deflate);
1868b24ab676SJeff Bonwick }
1869b24ab676SJeff Bonwick
1870b24ab676SJeff Bonwick metaslab_class_t *
spa_normal_class(spa_t * spa)1871b24ab676SJeff Bonwick spa_normal_class(spa_t *spa)
1872b24ab676SJeff Bonwick {
1873b24ab676SJeff Bonwick return (spa->spa_normal_class);
1874b24ab676SJeff Bonwick }
1875b24ab676SJeff Bonwick
1876b24ab676SJeff Bonwick metaslab_class_t *
spa_log_class(spa_t * spa)1877b24ab676SJeff Bonwick spa_log_class(spa_t *spa)
1878b24ab676SJeff Bonwick {
1879b24ab676SJeff Bonwick return (spa->spa_log_class);
1880b24ab676SJeff Bonwick }
1881b24ab676SJeff Bonwick
1882663207adSDon Brady metaslab_class_t *
spa_special_class(spa_t * spa)1883663207adSDon Brady spa_special_class(spa_t *spa)
1884663207adSDon Brady {
1885663207adSDon Brady return (spa->spa_special_class);
1886663207adSDon Brady }
1887663207adSDon Brady
1888663207adSDon Brady metaslab_class_t *
spa_dedup_class(spa_t * spa)1889663207adSDon Brady spa_dedup_class(spa_t *spa)
1890663207adSDon Brady {
1891663207adSDon Brady return (spa->spa_dedup_class);
1892663207adSDon Brady }
1893663207adSDon Brady
1894663207adSDon Brady /*
1895663207adSDon Brady * Locate an appropriate allocation class
1896663207adSDon Brady */
1897663207adSDon Brady metaslab_class_t *
spa_preferred_class(spa_t * spa,uint64_t size,dmu_object_type_t objtype,uint_t level,uint_t special_smallblk)1898663207adSDon Brady spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1899663207adSDon Brady uint_t level, uint_t special_smallblk)
1900663207adSDon Brady {
1901663207adSDon Brady if (DMU_OT_IS_ZIL(objtype)) {
1902663207adSDon Brady if (spa->spa_log_class->mc_groups != 0)
1903663207adSDon Brady return (spa_log_class(spa));
1904663207adSDon Brady else
1905663207adSDon Brady return (spa_normal_class(spa));
1906663207adSDon Brady }
1907663207adSDon Brady
1908663207adSDon Brady boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1909663207adSDon Brady
1910663207adSDon Brady if (DMU_OT_IS_DDT(objtype)) {
1911663207adSDon Brady if (spa->spa_dedup_class->mc_groups != 0)
1912663207adSDon Brady return (spa_dedup_class(spa));
1913663207adSDon Brady else if (has_special_class && zfs_ddt_data_is_special)
1914663207adSDon Brady return (spa_special_class(spa));
1915663207adSDon Brady else
1916663207adSDon Brady return (spa_normal_class(spa));
1917663207adSDon Brady }
1918663207adSDon Brady
1919663207adSDon Brady /* Indirect blocks for user data can land in special if allowed */
1920663207adSDon Brady if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1921663207adSDon Brady if (has_special_class && zfs_user_indirect_is_special)
1922663207adSDon Brady return (spa_special_class(spa));
1923663207adSDon Brady else
1924663207adSDon Brady return (spa_normal_class(spa));
1925663207adSDon Brady }
1926663207adSDon Brady
1927663207adSDon Brady if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1928663207adSDon Brady if (has_special_class)
1929663207adSDon Brady return (spa_special_class(spa));
1930663207adSDon Brady else
1931663207adSDon Brady return (spa_normal_class(spa));
1932663207adSDon Brady }
1933663207adSDon Brady
1934663207adSDon Brady /*
1935663207adSDon Brady * Allow small file blocks in special class in some cases (like
1936663207adSDon Brady * for the dRAID vdev feature). But always leave a reserve of
1937663207adSDon Brady * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1938663207adSDon Brady */
1939663207adSDon Brady if (DMU_OT_IS_FILE(objtype) &&
1940663207adSDon Brady has_special_class && size <= special_smallblk) {
1941663207adSDon Brady metaslab_class_t *special = spa_special_class(spa);
1942663207adSDon Brady uint64_t alloc = metaslab_class_get_alloc(special);
1943663207adSDon Brady uint64_t space = metaslab_class_get_space(special);
1944663207adSDon Brady uint64_t limit =
1945663207adSDon Brady (space * (100 - zfs_special_class_metadata_reserve_pct))
1946663207adSDon Brady / 100;
1947663207adSDon Brady
1948663207adSDon Brady if (alloc < limit)
1949663207adSDon Brady return (special);
1950663207adSDon Brady }
1951663207adSDon Brady
1952663207adSDon Brady return (spa_normal_class(spa));
1953663207adSDon Brady }
1954663207adSDon Brady
1955bc9014e6SJustin Gibbs void
spa_evicting_os_register(spa_t * spa,objset_t * os)1956bc9014e6SJustin Gibbs spa_evicting_os_register(spa_t *spa, objset_t *os)
1957bc9014e6SJustin Gibbs {
1958bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock);
1959bc9014e6SJustin Gibbs list_insert_head(&spa->spa_evicting_os_list, os);
1960bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock);
1961bc9014e6SJustin Gibbs }
1962bc9014e6SJustin Gibbs
1963bc9014e6SJustin Gibbs void
spa_evicting_os_deregister(spa_t * spa,objset_t * os)1964bc9014e6SJustin Gibbs spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1965bc9014e6SJustin Gibbs {
1966bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock);
1967bc9014e6SJustin Gibbs list_remove(&spa->spa_evicting_os_list, os);
1968bc9014e6SJustin Gibbs cv_broadcast(&spa->spa_evicting_os_cv);
1969bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock);
1970bc9014e6SJustin Gibbs }
1971bc9014e6SJustin Gibbs
1972bc9014e6SJustin Gibbs void
spa_evicting_os_wait(spa_t * spa)1973bc9014e6SJustin Gibbs spa_evicting_os_wait(spa_t *spa)
1974bc9014e6SJustin Gibbs {
1975bc9014e6SJustin Gibbs mutex_enter(&spa->spa_evicting_os_lock);
1976bc9014e6SJustin Gibbs while (!list_is_empty(&spa->spa_evicting_os_list))
1977bc9014e6SJustin Gibbs cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1978bc9014e6SJustin Gibbs mutex_exit(&spa->spa_evicting_os_lock);
1979bc9014e6SJustin Gibbs
1980bc9014e6SJustin Gibbs dmu_buf_user_evict_wait();
1981bc9014e6SJustin Gibbs }
1982bc9014e6SJustin Gibbs
198344cd46caSbillm int
spa_max_replication(spa_t * spa)198444cd46caSbillm spa_max_replication(spa_t *spa)
198544cd46caSbillm {
198644cd46caSbillm /*
1987e7437265Sahrens * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
198844cd46caSbillm * handle BPs with more than one DVA allocated. Set our max
198944cd46caSbillm * replication level accordingly.
1990fa9e4066Sahrens */
1991e7437265Sahrens if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
199244cd46caSbillm return (1);
199344cd46caSbillm return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1994fa9e4066Sahrens }
1995fa9e4066Sahrens
19963f9d6ad7SLin Ling int
spa_prev_software_version(spa_t * spa)19973f9d6ad7SLin Ling spa_prev_software_version(spa_t *spa)
19983f9d6ad7SLin Ling {
19993f9d6ad7SLin Ling return (spa->spa_prev_software_version);
20003f9d6ad7SLin Ling }
20013f9d6ad7SLin Ling
2002283b8460SGeorge.Wilson uint64_t
spa_deadman_synctime(spa_t * spa)2003283b8460SGeorge.Wilson spa_deadman_synctime(spa_t *spa)
2004283b8460SGeorge.Wilson {
2005283b8460SGeorge.Wilson return (spa->spa_deadman_synctime);
2006283b8460SGeorge.Wilson }
2007283b8460SGeorge.Wilson
2008084fd14fSBrian Behlendorf spa_autotrim_t
spa_get_autotrim(spa_t * spa)2009084fd14fSBrian Behlendorf spa_get_autotrim(spa_t *spa)
2010084fd14fSBrian Behlendorf {
2011084fd14fSBrian Behlendorf return (spa->spa_autotrim);
2012084fd14fSBrian Behlendorf }
2013084fd14fSBrian Behlendorf
201499653d4eSeschrock uint64_t
dva_get_dsize_sync(spa_t * spa,const dva_t * dva)2015b24ab676SJeff Bonwick dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
201699653d4eSeschrock {
2017b24ab676SJeff Bonwick uint64_t asize = DVA_GET_ASIZE(dva);
2018b24ab676SJeff Bonwick uint64_t dsize = asize;
201999653d4eSeschrock
2020b24ab676SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
202199653d4eSeschrock
2022b24ab676SJeff Bonwick if (asize != 0 && spa->spa_deflate) {
2023b24ab676SJeff Bonwick vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2024b24ab676SJeff Bonwick dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
202599653d4eSeschrock }
2026b24ab676SJeff Bonwick
2027b24ab676SJeff Bonwick return (dsize);
2028b24ab676SJeff Bonwick }
2029b24ab676SJeff Bonwick
2030b24ab676SJeff Bonwick uint64_t
bp_get_dsize_sync(spa_t * spa,const blkptr_t * bp)2031b24ab676SJeff Bonwick bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
2032b24ab676SJeff Bonwick {
2033b24ab676SJeff Bonwick uint64_t dsize = 0;
2034b24ab676SJeff Bonwick
20355d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2036b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2037b24ab676SJeff Bonwick
2038b24ab676SJeff Bonwick return (dsize);
2039b24ab676SJeff Bonwick }
2040b24ab676SJeff Bonwick
2041b24ab676SJeff Bonwick uint64_t
bp_get_dsize(spa_t * spa,const blkptr_t * bp)2042b24ab676SJeff Bonwick bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2043b24ab676SJeff Bonwick {
2044b24ab676SJeff Bonwick uint64_t dsize = 0;
2045b24ab676SJeff Bonwick
2046b24ab676SJeff Bonwick spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2047b24ab676SJeff Bonwick
20485d7b4d43SMatthew Ahrens for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2049b24ab676SJeff Bonwick dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2050b24ab676SJeff Bonwick
2051e14bb325SJeff Bonwick spa_config_exit(spa, SCL_VDEV, FTAG);
2052b24ab676SJeff Bonwick
2053b24ab676SJeff Bonwick return (dsize);
205499653d4eSeschrock }
205599653d4eSeschrock
2056abe1fd01SDon Brady uint64_t
spa_dirty_data(spa_t * spa)2057abe1fd01SDon Brady spa_dirty_data(spa_t *spa)
2058abe1fd01SDon Brady {
2059abe1fd01SDon Brady return (spa->spa_dsl_pool->dp_dirty_total);
2060abe1fd01SDon Brady }
2061abe1fd01SDon Brady
2062dd50e0ccSTony Hutter /*
2063dd50e0ccSTony Hutter * ==========================================================================
2064dd50e0ccSTony Hutter * SPA Import Progress Routines
2065dd50e0ccSTony Hutter * The illumos implementation of these are different from OpenZFS. OpenZFS
2066dd50e0ccSTony Hutter * uses the Linux /proc fs, whereas we use a kstat on the spa.
2067dd50e0ccSTony Hutter * ==========================================================================
2068dd50e0ccSTony Hutter */
2069dd50e0ccSTony Hutter
2070dd50e0ccSTony Hutter typedef struct spa_import_progress {
2071dd50e0ccSTony Hutter kstat_named_t sip_load_state;
2072dd50e0ccSTony Hutter kstat_named_t sip_mmp_sec_remaining; /* MMP activity check */
2073dd50e0ccSTony Hutter kstat_named_t sip_load_max_txg; /* rewind txg */
2074dd50e0ccSTony Hutter } spa_import_progress_t;
2075dd50e0ccSTony Hutter
2076dd50e0ccSTony Hutter static void
spa_import_progress_init(void)2077dd50e0ccSTony Hutter spa_import_progress_init(void)
2078dd50e0ccSTony Hutter {
2079dd50e0ccSTony Hutter }
2080dd50e0ccSTony Hutter
2081dd50e0ccSTony Hutter static void
spa_import_progress_destroy(void)2082dd50e0ccSTony Hutter spa_import_progress_destroy(void)
2083dd50e0ccSTony Hutter {
2084dd50e0ccSTony Hutter }
2085dd50e0ccSTony Hutter
2086dd50e0ccSTony Hutter void spa_import_progress_add(spa_t *);
2087dd50e0ccSTony Hutter
2088dd50e0ccSTony Hutter int
spa_import_progress_set_state(spa_t * spa,spa_load_state_t load_state)2089dd50e0ccSTony Hutter spa_import_progress_set_state(spa_t *spa, spa_load_state_t load_state)
2090dd50e0ccSTony Hutter {
2091dd50e0ccSTony Hutter if (spa->spa_imp_kstat == NULL)
2092dd50e0ccSTony Hutter spa_import_progress_add(spa);
2093dd50e0ccSTony Hutter
2094dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock);
2095dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) {
2096dd50e0ccSTony Hutter spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data;
2097dd50e0ccSTony Hutter if (sip != NULL)
2098dd50e0ccSTony Hutter sip->sip_load_state.value.ui64 = (uint64_t)load_state;
2099dd50e0ccSTony Hutter }
2100dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock);
2101dd50e0ccSTony Hutter
2102dd50e0ccSTony Hutter return (0);
2103dd50e0ccSTony Hutter }
2104dd50e0ccSTony Hutter
2105dd50e0ccSTony Hutter int
spa_import_progress_set_max_txg(spa_t * spa,uint64_t load_max_txg)2106dd50e0ccSTony Hutter spa_import_progress_set_max_txg(spa_t *spa, uint64_t load_max_txg)
2107dd50e0ccSTony Hutter {
2108dd50e0ccSTony Hutter if (spa->spa_imp_kstat == NULL)
2109dd50e0ccSTony Hutter spa_import_progress_add(spa);
2110dd50e0ccSTony Hutter
2111dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock);
2112dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) {
2113dd50e0ccSTony Hutter spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data;
2114dd50e0ccSTony Hutter if (sip != NULL)
2115dd50e0ccSTony Hutter sip->sip_load_max_txg.value.ui64 = load_max_txg;
2116dd50e0ccSTony Hutter }
2117dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock);
2118dd50e0ccSTony Hutter
2119dd50e0ccSTony Hutter return (0);
2120dd50e0ccSTony Hutter }
2121dd50e0ccSTony Hutter
2122dd50e0ccSTony Hutter int
spa_import_progress_set_mmp_check(spa_t * spa,uint64_t mmp_sec_remaining)2123dd50e0ccSTony Hutter spa_import_progress_set_mmp_check(spa_t *spa, uint64_t mmp_sec_remaining)
2124dd50e0ccSTony Hutter {
2125dd50e0ccSTony Hutter if (spa->spa_imp_kstat == NULL)
2126dd50e0ccSTony Hutter spa_import_progress_add(spa);
2127dd50e0ccSTony Hutter
2128dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock);
2129dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) {
2130dd50e0ccSTony Hutter spa_import_progress_t *sip = spa->spa_imp_kstat->ks_data;
2131dd50e0ccSTony Hutter if (sip != NULL)
2132dd50e0ccSTony Hutter sip->sip_mmp_sec_remaining.value.ui64 =
2133dd50e0ccSTony Hutter mmp_sec_remaining;
2134dd50e0ccSTony Hutter }
2135dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock);
2136dd50e0ccSTony Hutter
2137dd50e0ccSTony Hutter return (0);
2138dd50e0ccSTony Hutter }
2139dd50e0ccSTony Hutter
2140dd50e0ccSTony Hutter /*
2141dd50e0ccSTony Hutter * A new import is in progress. Add an entry.
2142dd50e0ccSTony Hutter */
2143dd50e0ccSTony Hutter void
spa_import_progress_add(spa_t * spa)2144dd50e0ccSTony Hutter spa_import_progress_add(spa_t *spa)
2145dd50e0ccSTony Hutter {
2146dd50e0ccSTony Hutter char *poolname = NULL;
2147dd50e0ccSTony Hutter spa_import_progress_t *sip;
2148dd50e0ccSTony Hutter
2149dd50e0ccSTony Hutter mutex_enter(&spa->spa_imp_kstat_lock);
2150dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) {
2151dd50e0ccSTony Hutter sip = spa->spa_imp_kstat->ks_data;
2152dd50e0ccSTony Hutter sip->sip_load_state.value.ui64 = (uint64_t)spa_load_state(spa);
2153dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock);
2154dd50e0ccSTony Hutter return;
2155dd50e0ccSTony Hutter }
2156dd50e0ccSTony Hutter
2157dd50e0ccSTony Hutter (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME,
2158dd50e0ccSTony Hutter &poolname);
2159dd50e0ccSTony Hutter if (poolname == NULL)
2160dd50e0ccSTony Hutter poolname = spa_name(spa);
2161dd50e0ccSTony Hutter
2162dd50e0ccSTony Hutter spa->spa_imp_kstat = kstat_create("zfs_import", 0, poolname,
2163dd50e0ccSTony Hutter "zfs_misc", KSTAT_TYPE_NAMED,
2164dd50e0ccSTony Hutter sizeof (spa_import_progress_t) / sizeof (kstat_named_t),
2165dd50e0ccSTony Hutter KSTAT_FLAG_VIRTUAL);
2166dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) {
2167dd50e0ccSTony Hutter sip = kmem_alloc(sizeof (spa_import_progress_t), KM_SLEEP);
2168dd50e0ccSTony Hutter spa->spa_imp_kstat->ks_data = sip;
2169dd50e0ccSTony Hutter
2170dd50e0ccSTony Hutter sip->sip_load_state.value.ui64 = (uint64_t)spa_load_state(spa);
2171dd50e0ccSTony Hutter
2172dd50e0ccSTony Hutter kstat_named_init(&sip->sip_load_state,
2173dd50e0ccSTony Hutter "spa_load_state", KSTAT_DATA_UINT64);
2174dd50e0ccSTony Hutter kstat_named_init(&sip->sip_mmp_sec_remaining,
2175dd50e0ccSTony Hutter "mmp_sec_remaining", KSTAT_DATA_UINT64);
2176dd50e0ccSTony Hutter kstat_named_init(&sip->sip_load_max_txg,
2177dd50e0ccSTony Hutter "spa_load_max_txg", KSTAT_DATA_UINT64);
2178dd50e0ccSTony Hutter spa->spa_imp_kstat->ks_lock = &spa->spa_imp_kstat_lock;
2179dd50e0ccSTony Hutter kstat_install(spa->spa_imp_kstat);
2180dd50e0ccSTony Hutter }
2181dd50e0ccSTony Hutter mutex_exit(&spa->spa_imp_kstat_lock);
2182dd50e0ccSTony Hutter }
2183dd50e0ccSTony Hutter
2184dd50e0ccSTony Hutter void
spa_import_progress_remove(spa_t * spa)2185dd50e0ccSTony Hutter spa_import_progress_remove(spa_t *spa)
2186dd50e0ccSTony Hutter {
2187dd50e0ccSTony Hutter if (spa->spa_imp_kstat != NULL) {
2188dd50e0ccSTony Hutter void *data = spa->spa_imp_kstat->ks_data;
2189dd50e0ccSTony Hutter
2190dd50e0ccSTony Hutter kstat_delete(spa->spa_imp_kstat);
2191dd50e0ccSTony Hutter spa->spa_imp_kstat = NULL;
2192dd50e0ccSTony Hutter kmem_free(data, sizeof (spa_import_progress_t));
2193dd50e0ccSTony Hutter }
2194dd50e0ccSTony Hutter }
2195dd50e0ccSTony Hutter
2196fa9e4066Sahrens /*
2197fa9e4066Sahrens * ==========================================================================
2198fa9e4066Sahrens * Initialization and Termination
2199fa9e4066Sahrens * ==========================================================================
2200fa9e4066Sahrens */
2201fa9e4066Sahrens
2202fa9e4066Sahrens static int
spa_name_compare(const void * a1,const void * a2)2203fa9e4066Sahrens spa_name_compare(const void *a1, const void *a2)
2204fa9e4066Sahrens {
2205fa9e4066Sahrens const spa_t *s1 = a1;
2206fa9e4066Sahrens const spa_t *s2 = a2;
2207fa9e4066Sahrens int s;
2208fa9e4066Sahrens
2209fa9e4066Sahrens s = strcmp(s1->spa_name, s2->spa_name);
2210c4ab0d3fSGvozden Neskovic
22114d7988d6SPaul Dagnelie return (TREE_ISIGN(s));
2212fa9e4066Sahrens }
2213fa9e4066Sahrens
22140373e76bSbonwick int
spa_busy(void)22150373e76bSbonwick spa_busy(void)
22160373e76bSbonwick {
22170373e76bSbonwick return (spa_active_count);
22180373e76bSbonwick }
22190373e76bSbonwick
2220e7cbe64fSgw void
spa_boot_init()2221e7cbe64fSgw spa_boot_init()
2222e7cbe64fSgw {
2223e7cbe64fSgw spa_config_load();
2224e7cbe64fSgw }
2225e7cbe64fSgw
2226fa9e4066Sahrens void
spa_init(int mode)2227fa9e4066Sahrens spa_init(int mode)
2228fa9e4066Sahrens {
2229fa9e4066Sahrens mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2230c25056deSgw mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2231fa94a07fSbrendan mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2232fa9e4066Sahrens cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2233fa9e4066Sahrens
2234fa9e4066Sahrens avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2235fa9e4066Sahrens offsetof(spa_t, spa_avl));
2236fa9e4066Sahrens
2237fa94a07fSbrendan avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2238fa94a07fSbrendan offsetof(spa_aux_t, aux_avl));
2239fa94a07fSbrendan
2240fa94a07fSbrendan avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2241fa94a07fSbrendan offsetof(spa_aux_t, aux_avl));
224299653d4eSeschrock
22438ad4d6ddSJeff Bonwick spa_mode_global = mode;
2244fa9e4066Sahrens
2245283b8460SGeorge.Wilson #ifdef _KERNEL
2246283b8460SGeorge.Wilson spa_arch_init();
2247283b8460SGeorge.Wilson #else
2248cd1c8b85SMatthew Ahrens if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
2249cd1c8b85SMatthew Ahrens arc_procfd = open("/proc/self/ctl", O_WRONLY);
2250cd1c8b85SMatthew Ahrens if (arc_procfd == -1) {
2251cd1c8b85SMatthew Ahrens perror("could not enable watchpoints: "
2252cd1c8b85SMatthew Ahrens "opening /proc/self/ctl failed: ");
2253cd1c8b85SMatthew Ahrens } else {
2254cd1c8b85SMatthew Ahrens arc_watch = B_TRUE;
2255cd1c8b85SMatthew Ahrens }
2256cd1c8b85SMatthew Ahrens }
2257cd1c8b85SMatthew Ahrens #endif
2258cd1c8b85SMatthew Ahrens
2259e914ace2STim Schumacher zfs_refcount_init();
2260fa9e4066Sahrens unique_init();
22614d7988d6SPaul Dagnelie zfs_btree_init();
22624d7988d6SPaul Dagnelie metaslab_stat_init();
2263fa9e4066Sahrens zio_init();
2264fa9e4066Sahrens dmu_init();
2265fa9e4066Sahrens zil_init();
226687db74c1Sek vdev_cache_stat_init();
226712a8814cSTom Caputi vdev_mirror_stat_init();
2268e86372a0SGvozden Neskovic vdev_raidz_math_init();
226991ebeef5Sahrens zfs_prop_init();
2270990b4856Slling zpool_prop_init();
2271ad135b5dSChristopher Siden zpool_feature_init();
2272fa9e4066Sahrens spa_config_load();
2273e14bb325SJeff Bonwick l2arc_start();
2274a3874b8bSToomas Soome scan_init();
2275dd50e0ccSTony Hutter spa_import_progress_init();
2276fa9e4066Sahrens }
2277fa9e4066Sahrens
2278fa9e4066Sahrens void
spa_fini(void)2279fa9e4066Sahrens spa_fini(void)
2280fa9e4066Sahrens {
2281e14bb325SJeff Bonwick l2arc_stop();
2282e14bb325SJeff Bonwick
2283fa9e4066Sahrens spa_evict_all();
2284fa9e4066Sahrens
228587db74c1Sek vdev_cache_stat_fini();
228612a8814cSTom Caputi vdev_mirror_stat_fini();
2287e86372a0SGvozden Neskovic vdev_raidz_math_fini();
2288fa9e4066Sahrens zil_fini();
2289fa9e4066Sahrens dmu_fini();
2290fa9e4066Sahrens zio_fini();
22914d7988d6SPaul Dagnelie metaslab_stat_fini();
22924d7988d6SPaul Dagnelie zfs_btree_fini();
229391ebeef5Sahrens unique_fini();
2294e914ace2STim Schumacher zfs_refcount_fini();
2295a3874b8bSToomas Soome scan_fini();
2296dd50e0ccSTony Hutter spa_import_progress_destroy();
2297fa9e4066Sahrens
2298fa9e4066Sahrens avl_destroy(&spa_namespace_avl);
229999653d4eSeschrock avl_destroy(&spa_spare_avl);
2300fa94a07fSbrendan avl_destroy(&spa_l2cache_avl);
2301fa9e4066Sahrens
2302fa9e4066Sahrens cv_destroy(&spa_namespace_cv);
2303fa9e4066Sahrens mutex_destroy(&spa_namespace_lock);
2304c25056deSgw mutex_destroy(&spa_spare_lock);
2305fa94a07fSbrendan mutex_destroy(&spa_l2cache_lock);
2306fa9e4066Sahrens }
23076ce0521aSperrin
23086ce0521aSperrin /*
23096ce0521aSperrin * Return whether this pool has slogs. No locking needed.
23106ce0521aSperrin * It's not a problem if the wrong answer is returned as it's only for
23116ce0521aSperrin * performance and not correctness
23126ce0521aSperrin */
23136ce0521aSperrin boolean_t
spa_has_slogs(spa_t * spa)23146ce0521aSperrin spa_has_slogs(spa_t *spa)
23156ce0521aSperrin {
23166ce0521aSperrin return (spa->spa_log_class->mc_rotor != NULL);
23176ce0521aSperrin }
2318bf82a41bSeschrock
2319b24ab676SJeff Bonwick spa_log_state_t
spa_get_log_state(spa_t * spa)2320b24ab676SJeff Bonwick spa_get_log_state(spa_t *spa)
2321b24ab676SJeff Bonwick {
2322b24ab676SJeff Bonwick return (spa->spa_log_state);
2323b24ab676SJeff Bonwick }
2324b24ab676SJeff Bonwick
2325b24ab676SJeff Bonwick void
spa_set_log_state(spa_t * spa,spa_log_state_t state)2326b24ab676SJeff Bonwick spa_set_log_state(spa_t *spa, spa_log_state_t state)
2327b24ab676SJeff Bonwick {
2328b24ab676SJeff Bonwick spa->spa_log_state = state;
2329b24ab676SJeff Bonwick }
2330b24ab676SJeff Bonwick
2331bf82a41bSeschrock boolean_t
spa_is_root(spa_t * spa)2332bf82a41bSeschrock spa_is_root(spa_t *spa)
2333bf82a41bSeschrock {
2334bf82a41bSeschrock return (spa->spa_is_root);
2335bf82a41bSeschrock }
23368ad4d6ddSJeff Bonwick
23378ad4d6ddSJeff Bonwick boolean_t
spa_writeable(spa_t * spa)23388ad4d6ddSJeff Bonwick spa_writeable(spa_t *spa)
23398ad4d6ddSJeff Bonwick {
23406f793812SPavel Zakharov return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config);
23418ad4d6ddSJeff Bonwick }
23428ad4d6ddSJeff Bonwick
234373527f44SAlex Reece /*
234473527f44SAlex Reece * Returns true if there is a pending sync task in any of the current
234573527f44SAlex Reece * syncing txg, the current quiescing txg, or the current open txg.
234673527f44SAlex Reece */
234773527f44SAlex Reece boolean_t
spa_has_pending_synctask(spa_t * spa)234873527f44SAlex Reece spa_has_pending_synctask(spa_t *spa)
234973527f44SAlex Reece {
235086714001SSerapheim Dimitropoulos return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
235186714001SSerapheim Dimitropoulos !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
235273527f44SAlex Reece }
235373527f44SAlex Reece
23548ad4d6ddSJeff Bonwick int
spa_mode(spa_t * spa)23558ad4d6ddSJeff Bonwick spa_mode(spa_t *spa)
23568ad4d6ddSJeff Bonwick {
23578ad4d6ddSJeff Bonwick return (spa->spa_mode);
23588ad4d6ddSJeff Bonwick }
2359b24ab676SJeff Bonwick
2360b24ab676SJeff Bonwick uint64_t
spa_bootfs(spa_t * spa)2361b24ab676SJeff Bonwick spa_bootfs(spa_t *spa)
2362b24ab676SJeff Bonwick {
2363b24ab676SJeff Bonwick return (spa->spa_bootfs);
2364b24ab676SJeff Bonwick }
2365b24ab676SJeff Bonwick
2366b24ab676SJeff Bonwick uint64_t
spa_delegation(spa_t * spa)2367b24ab676SJeff Bonwick spa_delegation(spa_t *spa)
2368b24ab676SJeff Bonwick {
2369b24ab676SJeff Bonwick return (spa->spa_delegation);
2370b24ab676SJeff Bonwick }
2371b24ab676SJeff Bonwick
2372b24ab676SJeff Bonwick objset_t *
spa_meta_objset(spa_t * spa)2373b24ab676SJeff Bonwick spa_meta_objset(spa_t *spa)
2374b24ab676SJeff Bonwick {
2375b24ab676SJeff Bonwick return (spa->spa_meta_objset);
2376b24ab676SJeff Bonwick }
2377b24ab676SJeff Bonwick
2378b24ab676SJeff Bonwick enum zio_checksum
spa_dedup_checksum(spa_t * spa)2379b24ab676SJeff Bonwick spa_dedup_checksum(spa_t *spa)
2380b24ab676SJeff Bonwick {
2381b24ab676SJeff Bonwick return (spa->spa_dedup_checksum);
2382b24ab676SJeff Bonwick }
23833f9d6ad7SLin Ling
23843f9d6ad7SLin Ling /*
23853f9d6ad7SLin Ling * Reset pool scan stat per scan pass (or reboot).
23863f9d6ad7SLin Ling */
23873f9d6ad7SLin Ling void
spa_scan_stat_init(spa_t * spa)23883f9d6ad7SLin Ling spa_scan_stat_init(spa_t *spa)
23893f9d6ad7SLin Ling {
23903f9d6ad7SLin Ling /* data not stored on disk */
23913f9d6ad7SLin Ling spa->spa_scan_pass_start = gethrestime_sec();
23921702cce7SAlek Pinchuk if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
23931702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
23941702cce7SAlek Pinchuk else
23951702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_pause = 0;
23961702cce7SAlek Pinchuk spa->spa_scan_pass_scrub_spent_paused = 0;
23973f9d6ad7SLin Ling spa->spa_scan_pass_exam = 0;
2398a3874b8bSToomas Soome spa->spa_scan_pass_issued = 0;
23993f9d6ad7SLin Ling vdev_scan_stat_init(spa->spa_root_vdev);
24003f9d6ad7SLin Ling }
24013f9d6ad7SLin Ling
24023f9d6ad7SLin Ling /*
24033f9d6ad7SLin Ling * Get scan stats for zpool status reports
24043f9d6ad7SLin Ling */
24053f9d6ad7SLin Ling int
spa_scan_get_stats(spa_t * spa,pool_scan_stat_t * ps)24063f9d6ad7SLin Ling spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
24073f9d6ad7SLin Ling {
24083f9d6ad7SLin Ling dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
24093f9d6ad7SLin Ling
24103f9d6ad7SLin Ling if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2411be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
24123f9d6ad7SLin Ling bzero(ps, sizeof (pool_scan_stat_t));
24133f9d6ad7SLin Ling
24143f9d6ad7SLin Ling /* data stored on disk */
24153f9d6ad7SLin Ling ps->pss_func = scn->scn_phys.scn_func;
2416a3874b8bSToomas Soome ps->pss_state = scn->scn_phys.scn_state;
24173f9d6ad7SLin Ling ps->pss_start_time = scn->scn_phys.scn_start_time;
24183f9d6ad7SLin Ling ps->pss_end_time = scn->scn_phys.scn_end_time;
24193f9d6ad7SLin Ling ps->pss_to_examine = scn->scn_phys.scn_to_examine;
24203f9d6ad7SLin Ling ps->pss_to_process = scn->scn_phys.scn_to_process;
24213f9d6ad7SLin Ling ps->pss_processed = scn->scn_phys.scn_processed;
24223f9d6ad7SLin Ling ps->pss_errors = scn->scn_phys.scn_errors;
2423a3874b8bSToomas Soome ps->pss_examined = scn->scn_phys.scn_examined;
2424a3874b8bSToomas Soome ps->pss_issued =
2425a3874b8bSToomas Soome scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
24263f9d6ad7SLin Ling ps->pss_state = scn->scn_phys.scn_state;
24273f9d6ad7SLin Ling
24283f9d6ad7SLin Ling /* data not stored on disk */
24293f9d6ad7SLin Ling ps->pss_pass_start = spa->spa_scan_pass_start;
24303f9d6ad7SLin Ling ps->pss_pass_exam = spa->spa_scan_pass_exam;
2431a3874b8bSToomas Soome ps->pss_pass_issued = spa->spa_scan_pass_issued;
24321702cce7SAlek Pinchuk ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
24331702cce7SAlek Pinchuk ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
24343f9d6ad7SLin Ling
24353f9d6ad7SLin Ling return (0);
24363f9d6ad7SLin Ling }
243709c9d376SGeorge Wilson
2438b5152584SMatthew Ahrens int
spa_maxblocksize(spa_t * spa)2439b5152584SMatthew Ahrens spa_maxblocksize(spa_t *spa)
2440b5152584SMatthew Ahrens {
2441b5152584SMatthew Ahrens if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2442b5152584SMatthew Ahrens return (SPA_MAXBLOCKSIZE);
2443b5152584SMatthew Ahrens else
2444b5152584SMatthew Ahrens return (SPA_OLD_MAXBLOCKSIZE);
2445b5152584SMatthew Ahrens }
24465cabbc6bSPrashanth Sreenivasa
244754811da5SToomas Soome int
spa_maxdnodesize(spa_t * spa)244854811da5SToomas Soome spa_maxdnodesize(spa_t *spa)
244954811da5SToomas Soome {
245054811da5SToomas Soome if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
245154811da5SToomas Soome return (DNODE_MAX_SIZE);
245254811da5SToomas Soome else
245354811da5SToomas Soome return (DNODE_MIN_SIZE);
245454811da5SToomas Soome }
245554811da5SToomas Soome
2456e0f1c0afSOlaf Faaland boolean_t
spa_multihost(spa_t * spa)2457e0f1c0afSOlaf Faaland spa_multihost(spa_t *spa)
2458e0f1c0afSOlaf Faaland {
2459e0f1c0afSOlaf Faaland return (spa->spa_multihost ? B_TRUE : B_FALSE);
2460e0f1c0afSOlaf Faaland }
2461e0f1c0afSOlaf Faaland
2462e0f1c0afSOlaf Faaland unsigned long
spa_get_hostid(void)2463e0f1c0afSOlaf Faaland spa_get_hostid(void)
2464e0f1c0afSOlaf Faaland {
2465e0f1c0afSOlaf Faaland unsigned long myhostid;
2466e0f1c0afSOlaf Faaland
2467e0f1c0afSOlaf Faaland #ifdef _KERNEL
2468e0f1c0afSOlaf Faaland myhostid = zone_get_hostid(NULL);
2469e0f1c0afSOlaf Faaland #else /* _KERNEL */
2470e0f1c0afSOlaf Faaland /*
2471e0f1c0afSOlaf Faaland * We're emulating the system's hostid in userland, so
2472e0f1c0afSOlaf Faaland * we can't use zone_get_hostid().
2473e0f1c0afSOlaf Faaland */
2474e0f1c0afSOlaf Faaland (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2475e0f1c0afSOlaf Faaland #endif /* _KERNEL */
2476e0f1c0afSOlaf Faaland
2477e0f1c0afSOlaf Faaland return (myhostid);
2478e0f1c0afSOlaf Faaland }
2479e0f1c0afSOlaf Faaland
24805cabbc6bSPrashanth Sreenivasa /*
24815cabbc6bSPrashanth Sreenivasa * Returns the txg that the last device removal completed. No indirect mappings
24825cabbc6bSPrashanth Sreenivasa * have been added since this txg.
24835cabbc6bSPrashanth Sreenivasa */
24845cabbc6bSPrashanth Sreenivasa uint64_t
spa_get_last_removal_txg(spa_t * spa)24855cabbc6bSPrashanth Sreenivasa spa_get_last_removal_txg(spa_t *spa)
24865cabbc6bSPrashanth Sreenivasa {
24875cabbc6bSPrashanth Sreenivasa uint64_t vdevid;
24885cabbc6bSPrashanth Sreenivasa uint64_t ret = -1ULL;
24895cabbc6bSPrashanth Sreenivasa
24905cabbc6bSPrashanth Sreenivasa spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
24915cabbc6bSPrashanth Sreenivasa /*
24925cabbc6bSPrashanth Sreenivasa * sr_prev_indirect_vdev is only modified while holding all the
24935cabbc6bSPrashanth Sreenivasa * config locks, so it is sufficient to hold SCL_VDEV as reader when
24945cabbc6bSPrashanth Sreenivasa * examining it.
24955cabbc6bSPrashanth Sreenivasa */
24965cabbc6bSPrashanth Sreenivasa vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
24975cabbc6bSPrashanth Sreenivasa
24985cabbc6bSPrashanth Sreenivasa while (vdevid != -1ULL) {
24995cabbc6bSPrashanth Sreenivasa vdev_t *vd = vdev_lookup_top(spa, vdevid);
25005cabbc6bSPrashanth Sreenivasa vdev_indirect_births_t *vib = vd->vdev_indirect_births;
25015cabbc6bSPrashanth Sreenivasa
25025cabbc6bSPrashanth Sreenivasa ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
25035cabbc6bSPrashanth Sreenivasa
25045cabbc6bSPrashanth Sreenivasa /*
25055cabbc6bSPrashanth Sreenivasa * If the removal did not remap any data, we don't care.
25065cabbc6bSPrashanth Sreenivasa */
25075cabbc6bSPrashanth Sreenivasa if (vdev_indirect_births_count(vib) != 0) {
25085cabbc6bSPrashanth Sreenivasa ret = vdev_indirect_births_last_entry_txg(vib);
25095cabbc6bSPrashanth Sreenivasa break;
25105cabbc6bSPrashanth Sreenivasa }
25115cabbc6bSPrashanth Sreenivasa
25125cabbc6bSPrashanth Sreenivasa vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
25135cabbc6bSPrashanth Sreenivasa }
25145cabbc6bSPrashanth Sreenivasa spa_config_exit(spa, SCL_VDEV, FTAG);
25155cabbc6bSPrashanth Sreenivasa
25165cabbc6bSPrashanth Sreenivasa IMPLY(ret != -1ULL,
25175cabbc6bSPrashanth Sreenivasa spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
25185cabbc6bSPrashanth Sreenivasa
25195cabbc6bSPrashanth Sreenivasa return (ret);
25205cabbc6bSPrashanth Sreenivasa }
25216f793812SPavel Zakharov
25226f793812SPavel Zakharov boolean_t
spa_trust_config(spa_t * spa)25236f793812SPavel Zakharov spa_trust_config(spa_t *spa)
25246f793812SPavel Zakharov {
25256f793812SPavel Zakharov return (spa->spa_trust_config);
25266f793812SPavel Zakharov }
25276f793812SPavel Zakharov
25286f793812SPavel Zakharov uint64_t
spa_missing_tvds_allowed(spa_t * spa)25296f793812SPavel Zakharov spa_missing_tvds_allowed(spa_t *spa)
25306f793812SPavel Zakharov {
25316f793812SPavel Zakharov return (spa->spa_missing_tvds_allowed);
25326f793812SPavel Zakharov }
25336f793812SPavel Zakharov
2534814dcd43SSerapheim Dimitropoulos space_map_t *
spa_syncing_log_sm(spa_t * spa)2535814dcd43SSerapheim Dimitropoulos spa_syncing_log_sm(spa_t *spa)
2536814dcd43SSerapheim Dimitropoulos {
2537814dcd43SSerapheim Dimitropoulos return (spa->spa_syncing_log_sm);
2538814dcd43SSerapheim Dimitropoulos }
2539814dcd43SSerapheim Dimitropoulos
25406f793812SPavel Zakharov void
spa_set_missing_tvds(spa_t * spa,uint64_t missing)25416f793812SPavel Zakharov spa_set_missing_tvds(spa_t *spa, uint64_t missing)
25426f793812SPavel Zakharov {
25436f793812SPavel Zakharov spa->spa_missing_tvds = missing;
25446f793812SPavel Zakharov }
254586714001SSerapheim Dimitropoulos
254686714001SSerapheim Dimitropoulos boolean_t
spa_top_vdevs_spacemap_addressable(spa_t * spa)254786714001SSerapheim Dimitropoulos spa_top_vdevs_spacemap_addressable(spa_t *spa)
254886714001SSerapheim Dimitropoulos {
254986714001SSerapheim Dimitropoulos vdev_t *rvd = spa->spa_root_vdev;
255086714001SSerapheim Dimitropoulos for (uint64_t c = 0; c < rvd->vdev_children; c++) {
255186714001SSerapheim Dimitropoulos if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
255286714001SSerapheim Dimitropoulos return (B_FALSE);
255386714001SSerapheim Dimitropoulos }
255486714001SSerapheim Dimitropoulos return (B_TRUE);
255586714001SSerapheim Dimitropoulos }
255686714001SSerapheim Dimitropoulos
255786714001SSerapheim Dimitropoulos boolean_t
spa_has_checkpoint(spa_t * spa)255886714001SSerapheim Dimitropoulos spa_has_checkpoint(spa_t *spa)
255986714001SSerapheim Dimitropoulos {
256086714001SSerapheim Dimitropoulos return (spa->spa_checkpoint_txg != 0);
256186714001SSerapheim Dimitropoulos }
256286714001SSerapheim Dimitropoulos
256386714001SSerapheim Dimitropoulos boolean_t
spa_importing_readonly_checkpoint(spa_t * spa)256486714001SSerapheim Dimitropoulos spa_importing_readonly_checkpoint(spa_t *spa)
256586714001SSerapheim Dimitropoulos {
256686714001SSerapheim Dimitropoulos return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
256786714001SSerapheim Dimitropoulos spa->spa_mode == FREAD);
256886714001SSerapheim Dimitropoulos }
256986714001SSerapheim Dimitropoulos
257086714001SSerapheim Dimitropoulos uint64_t
spa_min_claim_txg(spa_t * spa)257186714001SSerapheim Dimitropoulos spa_min_claim_txg(spa_t *spa)
257286714001SSerapheim Dimitropoulos {
257386714001SSerapheim Dimitropoulos uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
257486714001SSerapheim Dimitropoulos
257586714001SSerapheim Dimitropoulos if (checkpoint_txg != 0)
257686714001SSerapheim Dimitropoulos return (checkpoint_txg + 1);
257786714001SSerapheim Dimitropoulos
257886714001SSerapheim Dimitropoulos return (spa->spa_first_txg);
257986714001SSerapheim Dimitropoulos }
258086714001SSerapheim Dimitropoulos
258186714001SSerapheim Dimitropoulos /*
258286714001SSerapheim Dimitropoulos * If there is a checkpoint, async destroys may consume more space from
258386714001SSerapheim Dimitropoulos * the pool instead of freeing it. In an attempt to save the pool from
258486714001SSerapheim Dimitropoulos * getting suspended when it is about to run out of space, we stop
258586714001SSerapheim Dimitropoulos * processing async destroys.
258686714001SSerapheim Dimitropoulos */
258786714001SSerapheim Dimitropoulos boolean_t
spa_suspend_async_destroy(spa_t * spa)258886714001SSerapheim Dimitropoulos spa_suspend_async_destroy(spa_t *spa)
258986714001SSerapheim Dimitropoulos {
259086714001SSerapheim Dimitropoulos dsl_pool_t *dp = spa_get_dsl(spa);
259186714001SSerapheim Dimitropoulos
259286714001SSerapheim Dimitropoulos uint64_t unreserved = dsl_pool_unreserved_space(dp,
259386714001SSerapheim Dimitropoulos ZFS_SPACE_CHECK_EXTRA_RESERVED);
259486714001SSerapheim Dimitropoulos uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
259586714001SSerapheim Dimitropoulos uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
259686714001SSerapheim Dimitropoulos
259786714001SSerapheim Dimitropoulos if (spa_has_checkpoint(spa) && avail == 0)
259886714001SSerapheim Dimitropoulos return (B_TRUE);
259986714001SSerapheim Dimitropoulos
260086714001SSerapheim Dimitropoulos return (B_FALSE);
260186714001SSerapheim Dimitropoulos }
2602*b4fb0039SJoshua M. Clulow
2603*b4fb0039SJoshua M. Clulow /*
2604*b4fb0039SJoshua M. Clulow * Generate a LUN expansion event. This routine does not use
2605*b4fb0039SJoshua M. Clulow * ddi_log_sysevent() because that would require a dev_info_t, and we may not
2606*b4fb0039SJoshua M. Clulow * have one available.
2607*b4fb0039SJoshua M. Clulow */
2608*b4fb0039SJoshua M. Clulow void
zfs_post_dle_sysevent(const char * physpath)2609*b4fb0039SJoshua M. Clulow zfs_post_dle_sysevent(const char *physpath)
2610*b4fb0039SJoshua M. Clulow {
2611*b4fb0039SJoshua M. Clulow #ifdef _KERNEL
2612*b4fb0039SJoshua M. Clulow sysevent_t *ev = sysevent_alloc(EC_DEV_STATUS, ESC_DEV_DLE,
2613*b4fb0039SJoshua M. Clulow SUNW_KERN_PUB "zfs", SE_SLEEP);
2614*b4fb0039SJoshua M. Clulow sysevent_attr_list_t *attr = NULL;
2615*b4fb0039SJoshua M. Clulow sysevent_id_t eid;
2616*b4fb0039SJoshua M. Clulow
2617*b4fb0039SJoshua M. Clulow VERIFY(ev != NULL);
2618*b4fb0039SJoshua M. Clulow
2619*b4fb0039SJoshua M. Clulow /*
2620*b4fb0039SJoshua M. Clulow * The only attribute is the /devices path of the expanding device:
2621*b4fb0039SJoshua M. Clulow */
2622*b4fb0039SJoshua M. Clulow sysevent_value_t value = {
2623*b4fb0039SJoshua M. Clulow .value_type = SE_DATA_TYPE_STRING,
2624*b4fb0039SJoshua M. Clulow .value = {
2625*b4fb0039SJoshua M. Clulow .sv_string = (char *)physpath,
2626*b4fb0039SJoshua M. Clulow },
2627*b4fb0039SJoshua M. Clulow };
2628*b4fb0039SJoshua M. Clulow if (sysevent_add_attr(&attr, DEV_PHYS_PATH, &value, SE_SLEEP) != 0) {
2629*b4fb0039SJoshua M. Clulow sysevent_free(ev);
2630*b4fb0039SJoshua M. Clulow return;
2631*b4fb0039SJoshua M. Clulow }
2632*b4fb0039SJoshua M. Clulow
2633*b4fb0039SJoshua M. Clulow if (sysevent_attach_attributes(ev, attr) != 0) {
2634*b4fb0039SJoshua M. Clulow sysevent_free_attr(attr);
2635*b4fb0039SJoshua M. Clulow sysevent_free(ev);
2636*b4fb0039SJoshua M. Clulow return;
2637*b4fb0039SJoshua M. Clulow }
2638*b4fb0039SJoshua M. Clulow
2639*b4fb0039SJoshua M. Clulow (void) log_sysevent(ev, SE_SLEEP, &eid);
2640*b4fb0039SJoshua M. Clulow sysevent_free(ev);
2641*b4fb0039SJoshua M. Clulow #endif
2642*b4fb0039SJoshua M. Clulow }
2643