]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa_misc.c
module: zfs: freebsd: fix unused, remove argsused
[mirror_zfs.git] / module / zfs / spa_misc.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
93e28d66 23 * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
adfe9d93 24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
0c66c32d 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
3c67d83a 26 * Copyright 2013 Saso Kiselkov. All rights reserved.
0ea05c64 27 * Copyright (c) 2017 Datto Inc.
cc99f275 28 * Copyright (c) 2017, Intel Corporation.
e3570464 29 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
34dc7c2f
BB
30 */
31
34dc7c2f
BB
32#include <sys/zfs_context.h>
33#include <sys/spa_impl.h>
34#include <sys/zio.h>
35#include <sys/zio_checksum.h>
36#include <sys/zio_compress.h>
37#include <sys/dmu.h>
38#include <sys/dmu_tx.h>
39#include <sys/zap.h>
40#include <sys/zil.h>
41#include <sys/vdev_impl.h>
619f0976 42#include <sys/vdev_initialize.h>
1b939560 43#include <sys/vdev_trim.h>
bc25c932 44#include <sys/vdev_file.h>
ab9f4b0b 45#include <sys/vdev_raidz.h>
34dc7c2f
BB
46#include <sys/metaslab.h>
47#include <sys/uberblock_impl.h>
48#include <sys/txg.h>
49#include <sys/avl.h>
50#include <sys/unique.h>
51#include <sys/dsl_pool.h>
52#include <sys/dsl_dir.h>
53#include <sys/dsl_prop.h>
26685276 54#include <sys/fm/util.h>
428870ff 55#include <sys/dsl_scan.h>
34dc7c2f
BB
56#include <sys/fs/zfs.h>
57#include <sys/metaslab_impl.h>
b128c09f 58#include <sys/arc.h>
428870ff 59#include <sys/ddt.h>
1421c891 60#include <sys/kstat.h>
34dc7c2f 61#include "zfs_prop.h"
ca577779 62#include <sys/btree.h>
3c67d83a 63#include <sys/zfeature.h>
bced7e3a 64#include <sys/qat.h>
10b3c7f5 65#include <sys/zstd/zstd.h>
34dc7c2f
BB
66
67/*
68 * SPA locking
69 *
93e28d66 70 * There are three basic locks for managing spa_t structures:
34dc7c2f
BB
71 *
72 * spa_namespace_lock (global mutex)
73 *
74 * This lock must be acquired to do any of the following:
75 *
76 * - Lookup a spa_t by name
77 * - Add or remove a spa_t from the namespace
78 * - Increase spa_refcount from non-zero
79 * - Check if spa_refcount is zero
80 * - Rename a spa_t
81 * - add/remove/attach/detach devices
82 * - Held for the duration of create/destroy/import/export
83 *
84 * It does not need to handle recursion. A create or destroy may
85 * reference objects (files or zvols) in other pools, but by
86 * definition they must have an existing reference, and will never need
87 * to lookup a spa_t by name.
88 *
c13060e4 89 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
34dc7c2f
BB
90 *
91 * This reference count keep track of any active users of the spa_t. The
92 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
93 * the refcount is never really 'zero' - opening a pool implicitly keeps
b128c09f 94 * some references in the DMU. Internally we check against spa_minref, but
34dc7c2f
BB
95 * present the image of a zero/non-zero value to consumers.
96 *
b128c09f 97 * spa_config_lock[] (per-spa array of rwlocks)
34dc7c2f
BB
98 *
99 * This protects the spa_t from config changes, and must be held in
100 * the following circumstances:
101 *
102 * - RW_READER to perform I/O to the spa
103 * - RW_WRITER to change the vdev config
104 *
34dc7c2f
BB
105 * The locking order is fairly straightforward:
106 *
107 * spa_namespace_lock -> spa_refcount
108 *
109 * The namespace lock must be acquired to increase the refcount from 0
110 * or to check if it is zero.
111 *
b128c09f 112 * spa_refcount -> spa_config_lock[]
34dc7c2f
BB
113 *
114 * There must be at least one valid reference on the spa_t to acquire
115 * the config lock.
116 *
b128c09f 117 * spa_namespace_lock -> spa_config_lock[]
34dc7c2f
BB
118 *
119 * The namespace lock must always be taken before the config lock.
120 *
121 *
b128c09f 122 * The spa_namespace_lock can be acquired directly and is globally visible.
34dc7c2f 123 *
b128c09f
BB
124 * The namespace is manipulated using the following functions, all of which
125 * require the spa_namespace_lock to be held.
34dc7c2f
BB
126 *
127 * spa_lookup() Lookup a spa_t by name.
128 *
129 * spa_add() Create a new spa_t in the namespace.
130 *
131 * spa_remove() Remove a spa_t from the namespace. This also
132 * frees up any memory associated with the spa_t.
133 *
134 * spa_next() Returns the next spa_t in the system, or the
135 * first if NULL is passed.
136 *
137 * spa_evict_all() Shutdown and remove all spa_t structures in
138 * the system.
139 *
140 * spa_guid_exists() Determine whether a pool/device guid exists.
141 *
142 * The spa_refcount is manipulated using the following functions:
143 *
144 * spa_open_ref() Adds a reference to the given spa_t. Must be
145 * called with spa_namespace_lock held if the
146 * refcount is currently zero.
147 *
148 * spa_close() Remove a reference from the spa_t. This will
149 * not free the spa_t or remove it from the
150 * namespace. No locking is required.
151 *
152 * spa_refcount_zero() Returns true if the refcount is currently
153 * zero. Must be called with spa_namespace_lock
154 * held.
155 *
b128c09f
BB
156 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
157 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
158 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
159 *
160 * To read the configuration, it suffices to hold one of these locks as reader.
161 * To modify the configuration, you must hold all locks as writer. To modify
162 * vdev state without altering the vdev tree's topology (e.g. online/offline),
163 * you must hold SCL_STATE and SCL_ZIO as writer.
164 *
165 * We use these distinct config locks to avoid recursive lock entry.
166 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
167 * block allocations (SCL_ALLOC), which may require reading space maps
168 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
169 *
170 * The spa config locks cannot be normal rwlocks because we need the
171 * ability to hand off ownership. For example, SCL_ZIO is acquired
172 * by the issuing thread and later released by an interrupt thread.
173 * They do, however, obey the usual write-wanted semantics to prevent
174 * writer (i.e. system administrator) starvation.
175 *
176 * The lock acquisition rules are as follows:
177 *
178 * SCL_CONFIG
179 * Protects changes to the vdev tree topology, such as vdev
180 * add/remove/attach/detach. Protects the dirty config list
181 * (spa_config_dirty_list) and the set of spares and l2arc devices.
182 *
183 * SCL_STATE
184 * Protects changes to pool state and vdev state, such as vdev
185 * online/offline/fault/degrade/clear. Protects the dirty state list
186 * (spa_state_dirty_list) and global pool state (spa_state).
187 *
188 * SCL_ALLOC
189 * Protects changes to metaslab groups and classes.
190 * Held as reader by metaslab_alloc() and metaslab_claim().
191 *
192 * SCL_ZIO
193 * Held by bp-level zios (those which have no io_vd upon entry)
194 * to prevent changes to the vdev tree. The bp-level zio implicitly
195 * protects all of its vdev child zios, which do not hold SCL_ZIO.
196 *
197 * SCL_FREE
198 * Protects changes to metaslab groups and classes.
199 * Held as reader by metaslab_free(). SCL_FREE is distinct from
200 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
201 * blocks in zio_done() while another i/o that holds either
202 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
203 *
204 * SCL_VDEV
205 * Held as reader to prevent changes to the vdev tree during trivial
428870ff 206 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
b128c09f
BB
207 * other locks, and lower than all of them, to ensure that it's safe
208 * to acquire regardless of caller context.
209 *
210 * In addition, the following rules apply:
211 *
212 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
213 * The lock ordering is SCL_CONFIG > spa_props_lock.
214 *
215 * (b) I/O operations on leaf vdevs. For any zio operation that takes
216 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
217 * or zio_write_phys() -- the caller must ensure that the config cannot
218 * cannot change in the interim, and that the vdev cannot be reopened.
219 * SCL_STATE as reader suffices for both.
34dc7c2f
BB
220 *
221 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
222 *
223 * spa_vdev_enter() Acquire the namespace lock and the config lock
224 * for writing.
225 *
226 * spa_vdev_exit() Release the config lock, wait for all I/O
227 * to complete, sync the updated configs to the
228 * cache, and release the namespace lock.
229 *
b128c09f
BB
230 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
231 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
232 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
34dc7c2f
BB
233 */
234
235static avl_tree_t spa_namespace_avl;
236kmutex_t spa_namespace_lock;
237static kcondvar_t spa_namespace_cv;
34dc7c2f
BB
238int spa_max_replication_override = SPA_DVAS_PER_BP;
239
240static kmutex_t spa_spare_lock;
241static avl_tree_t spa_spare_avl;
242static kmutex_t spa_l2cache_lock;
243static avl_tree_t spa_l2cache_avl;
244
245kmem_cache_t *spa_buffer_pool;
da92d5cb 246spa_mode_t spa_mode_global = SPA_MODE_UNINIT;
34dc7c2f 247
0b39b9f9 248#ifdef ZFS_DEBUG
a1d477c2
MA
249/*
250 * Everything except dprintf, set_error, spa, and indirect_remap is on
251 * by default in debug builds.
252 */
253int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR |
964c2d69 254 ZFS_DEBUG_INDIRECT_REMAP);
0b39b9f9
PS
255#else
256int zfs_flags = 0;
257#endif
258
259/*
260 * zfs_recover can be set to nonzero to attempt to recover from
261 * otherwise-fatal errors, typically caused by on-disk corruption. When
262 * set, calls to zfs_panic_recover() will turn into warning messages.
263 * This should only be used as a last resort, as it typically results
264 * in leaked space, or worse.
265 */
266int zfs_recover = B_FALSE;
267
268/*
269 * If destroy encounters an EIO while reading metadata (e.g. indirect
270 * blocks), space referenced by the missing metadata can not be freed.
271 * Normally this causes the background destroy to become "stalled", as
272 * it is unable to make forward progress. While in this stalled state,
273 * all remaining space to free from the error-encountering filesystem is
274 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
275 * permanently leak the space from indirect blocks that can not be read,
276 * and continue to free everything else that it can.
277 *
278 * The default, "stalling" behavior is useful if the storage partially
279 * fails (i.e. some but not all i/os fail), and then later recovers. In
280 * this case, we will be able to continue pool operations while it is
281 * partially failed, and when it recovers, we can continue to free the
282 * space, with no leaks. However, note that this case is actually
283 * fairly rare.
284 *
285 * Typically pools either (a) fail completely (but perhaps temporarily,
286 * e.g. a top-level vdev going offline), or (b) have localized,
287 * permanent errors (e.g. disk returns the wrong data due to bit flip or
288 * firmware bug). In case (a), this setting does not matter because the
289 * pool will be suspended and the sync thread will not be able to make
290 * forward progress regardless. In case (b), because the error is
291 * permanent, the best we can do is leak the minimum amount of space,
292 * which is what setting this flag will do. Therefore, it is reasonable
293 * for this flag to normally be set, but we chose the more conservative
294 * approach of not setting it, so that there is no possibility of
295 * leaking space in the "partial temporary" failure case.
296 */
297int zfs_free_leak_on_eio = B_FALSE;
298
cc92e9d0 299/*
e8b96c60
MA
300 * Expiration time in milliseconds. This value has two meanings. First it is
301 * used to determine when the spa_deadman() logic should fire. By default the
8fb1ede1 302 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
e8b96c60
MA
303 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
304 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
8fb1ede1 305 * in one of three behaviors controlled by zfs_deadman_failmode.
cc92e9d0 306 */
57940b43 307unsigned long zfs_deadman_synctime_ms = 600000UL;
8fb1ede1
BB
308
309/*
310 * This value controls the maximum amount of time zio_wait() will block for an
311 * outstanding IO. By default this is 300 seconds at which point the "hung"
312 * behavior will be applied as described for zfs_deadman_synctime_ms.
313 */
57940b43 314unsigned long zfs_deadman_ziotime_ms = 300000UL;
cc92e9d0 315
b81a3ddc
TC
316/*
317 * Check time in milliseconds. This defines the frequency at which we check
318 * for hung I/O.
319 */
57940b43 320unsigned long zfs_deadman_checktime_ms = 60000UL;
b81a3ddc 321
cc92e9d0
GW
322/*
323 * By default the deadman is enabled.
324 */
325int zfs_deadman_enabled = 1;
326
8fb1ede1
BB
327/*
328 * Controls the behavior of the deadman when it detects a "hung" I/O.
329 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
330 *
331 * wait - Wait for the "hung" I/O (default)
332 * continue - Attempt to recover from a "hung" I/O
333 * panic - Panic the system
334 */
335char *zfs_deadman_failmode = "wait";
336
e8b96c60
MA
337/*
338 * The worst case is single-sector max-parity RAID-Z blocks, in which
339 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
340 * times the size; so just assume that. Add to this the fact that
341 * we can have up to 3 DVAs per bp, and one more factor of 2 because
342 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
343 * the worst case is:
344 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
345 */
346int spa_asize_inflation = 24;
347
3d45fdd6
MA
348/*
349 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
f01eaed4
PS
350 * the pool to be consumed (bounded by spa_max_slop). This ensures that we
351 * don't run the pool completely out of space, due to unaccounted changes (e.g.
352 * to the MOS). It also limits the worst-case time to allocate space. If we
353 * have less than this amount of free space, most ZPL operations (e.g. write,
354 * create) will return ENOSPC. The ZIL metaslabs (spa_embedded_log_class) are
355 * also part of this 3.2% of space which can't be consumed by normal writes;
356 * the slop space "proper" (spa_get_slop_space()) is decreased by the embedded
357 * log space.
3d45fdd6
MA
358 *
359 * Certain operations (e.g. file removal, most administrative actions) can
360 * use half the slop space. They will only return ENOSPC if less than half
361 * the slop space is free. Typically, once the pool has less than the slop
362 * space free, the user will use these operations to free up space in the pool.
363 * These are the operations that call dsl_pool_adjustedsize() with the netfree
364 * argument set to TRUE.
365 *
d2734cce
SD
366 * Operations that are almost guaranteed to free up space in the absence of
367 * a pool checkpoint can use up to three quarters of the slop space
368 * (e.g zfs destroy).
369 *
3d45fdd6
MA
370 * A very restricted set of operations are always permitted, regardless of
371 * the amount of free space. These are the operations that call
d2734cce
SD
372 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
373 * increase in the amount of space used, it is possible to run the pool
374 * completely out of space, causing it to be permanently read-only.
3d45fdd6 375 *
d7958b4c
MA
376 * Note that on very small pools, the slop space will be larger than
377 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
378 * but we never allow it to be more than half the pool size.
379 *
f01eaed4
PS
380 * Further, on very large pools, the slop space will be smaller than
381 * 3.2%, to avoid reserving much more space than we actually need; bounded
382 * by spa_max_slop (128GB).
383 *
3d45fdd6
MA
384 * See also the comments in zfs_space_check_t.
385 */
386int spa_slop_shift = 5;
f01eaed4
PS
387uint64_t spa_min_slop = 128ULL * 1024 * 1024;
388uint64_t spa_max_slop = 128ULL * 1024 * 1024 * 1024;
492f64e9
PD
389int spa_allocators = 4;
390
3d45fdd6 391
4a0ee12a
PZ
392void
393spa_load_failed(spa_t *spa, const char *fmt, ...)
394{
395 va_list adx;
396 char buf[256];
397
398 va_start(adx, fmt);
399 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
400 va_end(adx);
401
6cb8e530
PZ
402 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
403 spa->spa_trust_config ? "trusted" : "untrusted", buf);
4a0ee12a
PZ
404}
405
4a0ee12a
PZ
406void
407spa_load_note(spa_t *spa, const char *fmt, ...)
408{
409 va_list adx;
410 char buf[256];
411
412 va_start(adx, fmt);
413 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
414 va_end(adx);
415
6cb8e530
PZ
416 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
417 spa->spa_trust_config ? "trusted" : "untrusted", buf);
4a0ee12a
PZ
418}
419
cc99f275
DB
420/*
421 * By default dedup and user data indirects land in the special class
422 */
423int zfs_ddt_data_is_special = B_TRUE;
424int zfs_user_indirect_is_special = B_TRUE;
425
426/*
427 * The percentage of special class final space reserved for metadata only.
428 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
429 * let metadata into the class.
430 */
431int zfs_special_class_metadata_reserve_pct = 25;
432
34dc7c2f
BB
433/*
434 * ==========================================================================
435 * SPA config locking
436 * ==========================================================================
437 */
438static void
b128c09f
BB
439spa_config_lock_init(spa_t *spa)
440{
1c27024e 441 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
442 spa_config_lock_t *scl = &spa->spa_config_lock[i];
443 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
444 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
b128c09f
BB
445 scl->scl_writer = NULL;
446 scl->scl_write_wanted = 0;
42afb12d 447 scl->scl_count = 0;
b128c09f 448 }
34dc7c2f
BB
449}
450
451static void
b128c09f
BB
452spa_config_lock_destroy(spa_t *spa)
453{
1c27024e 454 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
455 spa_config_lock_t *scl = &spa->spa_config_lock[i];
456 mutex_destroy(&scl->scl_lock);
457 cv_destroy(&scl->scl_cv);
b128c09f
BB
458 ASSERT(scl->scl_writer == NULL);
459 ASSERT(scl->scl_write_wanted == 0);
42afb12d 460 ASSERT(scl->scl_count == 0);
b128c09f
BB
461 }
462}
463
464int
465spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
34dc7c2f 466{
1c27024e 467 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
468 spa_config_lock_t *scl = &spa->spa_config_lock[i];
469 if (!(locks & (1 << i)))
470 continue;
471 mutex_enter(&scl->scl_lock);
472 if (rw == RW_READER) {
473 if (scl->scl_writer || scl->scl_write_wanted) {
474 mutex_exit(&scl->scl_lock);
adfe9d93
SK
475 spa_config_exit(spa, locks & ((1 << i) - 1),
476 tag);
b128c09f
BB
477 return (0);
478 }
479 } else {
480 ASSERT(scl->scl_writer != curthread);
42afb12d 481 if (scl->scl_count != 0) {
b128c09f 482 mutex_exit(&scl->scl_lock);
adfe9d93
SK
483 spa_config_exit(spa, locks & ((1 << i) - 1),
484 tag);
b128c09f
BB
485 return (0);
486 }
487 scl->scl_writer = curthread;
488 }
42afb12d 489 scl->scl_count++;
b128c09f
BB
490 mutex_exit(&scl->scl_lock);
491 }
492 return (1);
34dc7c2f
BB
493}
494
495void
dc04a8c7 496spa_config_enter(spa_t *spa, int locks, const void *tag, krw_t rw)
34dc7c2f 497{
45d1cae3
BB
498 int wlocks_held = 0;
499
13fe0198
MA
500 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
501
1c27024e 502 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f 503 spa_config_lock_t *scl = &spa->spa_config_lock[i];
45d1cae3
BB
504 if (scl->scl_writer == curthread)
505 wlocks_held |= (1 << i);
b128c09f
BB
506 if (!(locks & (1 << i)))
507 continue;
508 mutex_enter(&scl->scl_lock);
509 if (rw == RW_READER) {
510 while (scl->scl_writer || scl->scl_write_wanted) {
511 cv_wait(&scl->scl_cv, &scl->scl_lock);
512 }
513 } else {
514 ASSERT(scl->scl_writer != curthread);
42afb12d 515 while (scl->scl_count != 0) {
b128c09f
BB
516 scl->scl_write_wanted++;
517 cv_wait(&scl->scl_cv, &scl->scl_lock);
518 scl->scl_write_wanted--;
519 }
520 scl->scl_writer = curthread;
521 }
42afb12d 522 scl->scl_count++;
b128c09f 523 mutex_exit(&scl->scl_lock);
34dc7c2f 524 }
a1d477c2 525 ASSERT3U(wlocks_held, <=, locks);
34dc7c2f
BB
526}
527
528void
dc04a8c7 529spa_config_exit(spa_t *spa, int locks, const void *tag)
34dc7c2f 530{
1c27024e 531 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
b128c09f
BB
532 spa_config_lock_t *scl = &spa->spa_config_lock[i];
533 if (!(locks & (1 << i)))
534 continue;
535 mutex_enter(&scl->scl_lock);
42afb12d
AM
536 ASSERT(scl->scl_count > 0);
537 if (--scl->scl_count == 0) {
b128c09f
BB
538 ASSERT(scl->scl_writer == NULL ||
539 scl->scl_writer == curthread);
540 scl->scl_writer = NULL; /* OK in either case */
541 cv_broadcast(&scl->scl_cv);
542 }
543 mutex_exit(&scl->scl_lock);
34dc7c2f 544 }
34dc7c2f
BB
545}
546
b128c09f
BB
547int
548spa_config_held(spa_t *spa, int locks, krw_t rw)
34dc7c2f 549{
1c27024e 550 int locks_held = 0;
34dc7c2f 551
1c27024e 552 for (int i = 0; i < SCL_LOCKS; i++) {
b128c09f
BB
553 spa_config_lock_t *scl = &spa->spa_config_lock[i];
554 if (!(locks & (1 << i)))
555 continue;
42afb12d 556 if ((rw == RW_READER && scl->scl_count != 0) ||
b128c09f
BB
557 (rw == RW_WRITER && scl->scl_writer == curthread))
558 locks_held |= 1 << i;
559 }
560
561 return (locks_held);
34dc7c2f
BB
562}
563
564/*
565 * ==========================================================================
566 * SPA namespace functions
567 * ==========================================================================
568 */
569
570/*
571 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
572 * Returns NULL if no matching spa_t is found.
573 */
574spa_t *
575spa_lookup(const char *name)
576{
b128c09f
BB
577 static spa_t search; /* spa_t is large; don't allocate on stack */
578 spa_t *spa;
34dc7c2f 579 avl_index_t where;
34dc7c2f
BB
580 char *cp;
581
582 ASSERT(MUTEX_HELD(&spa_namespace_lock));
583
13fe0198
MA
584 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
585
34dc7c2f
BB
586 /*
587 * If it's a full dataset name, figure out the pool name and
588 * just use that.
589 */
da536844 590 cp = strpbrk(search.spa_name, "/@#");
13fe0198 591 if (cp != NULL)
34dc7c2f 592 *cp = '\0';
34dc7c2f 593
34dc7c2f
BB
594 spa = avl_find(&spa_namespace_avl, &search, &where);
595
34dc7c2f
BB
596 return (spa);
597}
598
cc92e9d0
GW
599/*
600 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
601 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
602 * looking for potentially hung I/Os.
603 */
604void
605spa_deadman(void *arg)
606{
607 spa_t *spa = arg;
608
b81a3ddc
TC
609 /* Disable the deadman if the pool is suspended. */
610 if (spa_suspended(spa))
611 return;
612
cc92e9d0
GW
613 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
614 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
8e739b2c 615 (u_longlong_t)++spa->spa_deadman_calls);
cc92e9d0 616 if (zfs_deadman_enabled)
8fb1ede1 617 vdev_deadman(spa->spa_root_vdev, FTAG);
cc92e9d0 618
57ddcda1 619 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
f764edf0 620 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
b81a3ddc 621 MSEC_TO_TICK(zfs_deadman_checktime_ms));
cc92e9d0
GW
622}
623
65c7cc49 624static int
93e28d66
SD
625spa_log_sm_sort_by_txg(const void *va, const void *vb)
626{
627 const spa_log_sm_t *a = va;
628 const spa_log_sm_t *b = vb;
629
ca577779 630 return (TREE_CMP(a->sls_txg, b->sls_txg));
93e28d66
SD
631}
632
34dc7c2f
BB
633/*
634 * Create an uninitialized spa_t with the given name. Requires
635 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
636 * exist by calling spa_lookup() first.
637 */
638spa_t *
428870ff 639spa_add(const char *name, nvlist_t *config, const char *altroot)
34dc7c2f
BB
640{
641 spa_t *spa;
b128c09f 642 spa_config_dirent_t *dp;
34dc7c2f
BB
643
644 ASSERT(MUTEX_HELD(&spa_namespace_lock));
645
79c76d5b 646 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
34dc7c2f 647
34dc7c2f 648 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 649 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff 650 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
0c66c32d 651 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 652 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff 653 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 654 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
3c67d83a 655 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff
BB
656 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
657 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
658 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
4eb30c68 659 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
93e28d66 660 mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
e60e158e 661 mutex_init(&spa->spa_activities_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
662
663 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
0c66c32d 664 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
428870ff 665 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f 666 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
b128c09f 667 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
e60e158e
JG
668 cv_init(&spa->spa_activities_cv, NULL, CV_DEFAULT, NULL);
669 cv_init(&spa->spa_waiters_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f 670
1c27024e 671 for (int t = 0; t < TXG_SIZE; t++)
428870ff
BB
672 bplist_create(&spa->spa_free_bplist[t]);
673
b128c09f 674 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
34dc7c2f
BB
675 spa->spa_state = POOL_STATE_UNINITIALIZED;
676 spa->spa_freeze_txg = UINT64_MAX;
677 spa->spa_final_txg = UINT64_MAX;
428870ff
BB
678 spa->spa_load_max_txg = UINT64_MAX;
679 spa->spa_proc = &p0;
680 spa->spa_proc_state = SPA_PROC_NONE;
6cb8e530 681 spa->spa_trust_config = B_TRUE;
25f06d67 682 spa->spa_hostid = zone_get_hostid(NULL);
34dc7c2f 683
e8b96c60 684 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
8fb1ede1
BB
685 spa->spa_deadman_ziotime = MSEC2NSEC(zfs_deadman_ziotime_ms);
686 spa_set_deadman_failmode(spa, zfs_deadman_failmode);
cc92e9d0 687
424fd7c3 688 zfs_refcount_create(&spa->spa_refcount);
b128c09f 689 spa_config_lock_init(spa);
1421c891 690 spa_stats_init(spa);
34dc7c2f
BB
691
692 avl_add(&spa_namespace_avl, spa);
693
34dc7c2f
BB
694 /*
695 * Set the alternate root, if there is one.
696 */
0336f3d0 697 if (altroot)
34dc7c2f 698 spa->spa_root = spa_strdup(altroot);
34dc7c2f 699
492f64e9 700 spa->spa_alloc_count = spa_allocators;
1b50749c
AM
701 spa->spa_allocs = kmem_zalloc(spa->spa_alloc_count *
702 sizeof (spa_alloc_t), KM_SLEEP);
492f64e9 703 for (int i = 0; i < spa->spa_alloc_count; i++) {
1b50749c
AM
704 mutex_init(&spa->spa_allocs[i].spaa_lock, NULL, MUTEX_DEFAULT,
705 NULL);
706 avl_create(&spa->spa_allocs[i].spaa_tree, zio_bookmark_compare,
492f64e9
PD
707 sizeof (zio_t), offsetof(zio_t, io_alloc_node));
708 }
93e28d66
SD
709 avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
710 sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
711 avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
712 sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
713 list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
714 offsetof(log_summary_entry_t, lse_node));
3dfb57a3 715
b128c09f
BB
716 /*
717 * Every pool starts with the default cachefile
718 */
719 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
720 offsetof(spa_config_dirent_t, scd_link));
721
79c76d5b 722 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
428870ff 723 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
b128c09f
BB
724 list_insert_head(&spa->spa_config_list, dp);
725
572e2857 726 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
79c76d5b 727 KM_SLEEP) == 0);
572e2857 728
9ae529ec
CS
729 if (config != NULL) {
730 nvlist_t *features;
731
732 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
733 &features) == 0) {
734 VERIFY(nvlist_dup(features, &spa->spa_label_features,
735 0) == 0);
736 }
737
428870ff 738 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
9ae529ec
CS
739 }
740
741 if (spa->spa_label_features == NULL) {
742 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
79c76d5b 743 KM_SLEEP) == 0);
9ae529ec 744 }
428870ff 745
c3520e7f
MA
746 spa->spa_min_ashift = INT_MAX;
747 spa->spa_max_ashift = 0;
b2255edc 748 spa->spa_min_alloc = INT_MAX;
c3520e7f 749
e8a20144
GN
750 /* Reset cached value */
751 spa->spa_dedup_dspace = ~0ULL;
752
b0bc7a84
MG
753 /*
754 * As a pool is being created, treat all features as disabled by
755 * setting SPA_FEATURE_DISABLED for all entries in the feature
756 * refcount cache.
757 */
1c27024e 758 for (int i = 0; i < SPA_FEATURES; i++) {
b0bc7a84
MG
759 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
760 }
761
3d31aad8
OF
762 list_create(&spa->spa_leaf_list, sizeof (vdev_t),
763 offsetof(vdev_t, vdev_leaf_node));
764
34dc7c2f
BB
765 return (spa);
766}
767
768/*
769 * Removes a spa_t from the namespace, freeing up any memory used. Requires
770 * spa_namespace_lock. This is called only after the spa_t has been closed and
771 * deactivated.
772 */
773void
774spa_remove(spa_t *spa)
775{
b128c09f
BB
776 spa_config_dirent_t *dp;
777
34dc7c2f 778 ASSERT(MUTEX_HELD(&spa_namespace_lock));
93e28d66 779 ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
424fd7c3 780 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
e60e158e 781 ASSERT0(spa->spa_waiters);
34dc7c2f 782
428870ff
BB
783 nvlist_free(spa->spa_config_splitting);
784
34dc7c2f
BB
785 avl_remove(&spa_namespace_avl, spa);
786 cv_broadcast(&spa_namespace_cv);
787
0336f3d0 788 if (spa->spa_root)
34dc7c2f 789 spa_strfree(spa->spa_root);
34dc7c2f 790
b128c09f
BB
791 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
792 list_remove(&spa->spa_config_list, dp);
793 if (dp->scd_path != NULL)
794 spa_strfree(dp->scd_path);
795 kmem_free(dp, sizeof (spa_config_dirent_t));
796 }
34dc7c2f 797
492f64e9 798 for (int i = 0; i < spa->spa_alloc_count; i++) {
1b50749c
AM
799 avl_destroy(&spa->spa_allocs[i].spaa_tree);
800 mutex_destroy(&spa->spa_allocs[i].spaa_lock);
492f64e9 801 }
1b50749c
AM
802 kmem_free(spa->spa_allocs, spa->spa_alloc_count *
803 sizeof (spa_alloc_t));
492f64e9 804
93e28d66
SD
805 avl_destroy(&spa->spa_metaslabs_by_flushed);
806 avl_destroy(&spa->spa_sm_logs_by_txg);
807 list_destroy(&spa->spa_log_summary);
b128c09f 808 list_destroy(&spa->spa_config_list);
3d31aad8 809 list_destroy(&spa->spa_leaf_list);
34dc7c2f 810
9ae529ec 811 nvlist_free(spa->spa_label_features);
572e2857 812 nvlist_free(spa->spa_load_info);
417104bd 813 nvlist_free(spa->spa_feat_stats);
34dc7c2f
BB
814 spa_config_set(spa, NULL);
815
424fd7c3 816 zfs_refcount_destroy(&spa->spa_refcount);
34dc7c2f 817
1421c891 818 spa_stats_destroy(spa);
b128c09f 819 spa_config_lock_destroy(spa);
34dc7c2f 820
1c27024e 821 for (int t = 0; t < TXG_SIZE; t++)
428870ff
BB
822 bplist_destroy(&spa->spa_free_bplist[t]);
823
3c67d83a
TH
824 zio_checksum_templates_free(spa);
825
34dc7c2f 826 cv_destroy(&spa->spa_async_cv);
0c66c32d 827 cv_destroy(&spa->spa_evicting_os_cv);
428870ff 828 cv_destroy(&spa->spa_proc_cv);
34dc7c2f 829 cv_destroy(&spa->spa_scrub_io_cv);
b128c09f 830 cv_destroy(&spa->spa_suspend_cv);
e60e158e
JG
831 cv_destroy(&spa->spa_activities_cv);
832 cv_destroy(&spa->spa_waiters_cv);
34dc7c2f 833
93e28d66 834 mutex_destroy(&spa->spa_flushed_ms_lock);
34dc7c2f 835 mutex_destroy(&spa->spa_async_lock);
34dc7c2f 836 mutex_destroy(&spa->spa_errlist_lock);
428870ff 837 mutex_destroy(&spa->spa_errlog_lock);
0c66c32d 838 mutex_destroy(&spa->spa_evicting_os_lock);
34dc7c2f 839 mutex_destroy(&spa->spa_history_lock);
428870ff 840 mutex_destroy(&spa->spa_proc_lock);
34dc7c2f 841 mutex_destroy(&spa->spa_props_lock);
3c67d83a 842 mutex_destroy(&spa->spa_cksum_tmpls_lock);
428870ff 843 mutex_destroy(&spa->spa_scrub_lock);
b128c09f 844 mutex_destroy(&spa->spa_suspend_lock);
428870ff 845 mutex_destroy(&spa->spa_vdev_top_lock);
4eb30c68 846 mutex_destroy(&spa->spa_feat_stats_lock);
e60e158e 847 mutex_destroy(&spa->spa_activities_lock);
34dc7c2f
BB
848
849 kmem_free(spa, sizeof (spa_t));
850}
851
852/*
853 * Given a pool, return the next pool in the namespace, or NULL if there is
854 * none. If 'prev' is NULL, return the first pool.
855 */
856spa_t *
857spa_next(spa_t *prev)
858{
859 ASSERT(MUTEX_HELD(&spa_namespace_lock));
860
861 if (prev)
862 return (AVL_NEXT(&spa_namespace_avl, prev));
863 else
864 return (avl_first(&spa_namespace_avl));
865}
866
867/*
868 * ==========================================================================
869 * SPA refcount functions
870 * ==========================================================================
871 */
872
873/*
874 * Add a reference to the given spa_t. Must have at least one reference, or
875 * have the namespace lock held.
876 */
877void
878spa_open_ref(spa_t *spa, void *tag)
879{
424fd7c3 880 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
34dc7c2f 881 MUTEX_HELD(&spa_namespace_lock));
c13060e4 882 (void) zfs_refcount_add(&spa->spa_refcount, tag);
34dc7c2f
BB
883}
884
885/*
886 * Remove a reference to the given spa_t. Must have at least one reference, or
887 * have the namespace lock held.
888 */
889void
890spa_close(spa_t *spa, void *tag)
891{
424fd7c3 892 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
34dc7c2f 893 MUTEX_HELD(&spa_namespace_lock));
424fd7c3 894 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
34dc7c2f
BB
895}
896
0c66c32d
JG
897/*
898 * Remove a reference to the given spa_t held by a dsl dir that is
899 * being asynchronously released. Async releases occur from a taskq
900 * performing eviction of dsl datasets and dirs. The namespace lock
901 * isn't held and the hold by the object being evicted may contribute to
902 * spa_minref (e.g. dataset or directory released during pool export),
903 * so the asserts in spa_close() do not apply.
904 */
905void
906spa_async_close(spa_t *spa, void *tag)
907{
424fd7c3 908 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
0c66c32d
JG
909}
910
34dc7c2f
BB
911/*
912 * Check to see if the spa refcount is zero. Must be called with
b128c09f 913 * spa_namespace_lock held. We really compare against spa_minref, which is the
34dc7c2f
BB
914 * number of references acquired when opening a pool
915 */
916boolean_t
917spa_refcount_zero(spa_t *spa)
918{
919 ASSERT(MUTEX_HELD(&spa_namespace_lock));
920
424fd7c3 921 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
34dc7c2f
BB
922}
923
924/*
925 * ==========================================================================
926 * SPA spare and l2cache tracking
927 * ==========================================================================
928 */
929
930/*
931 * Hot spares and cache devices are tracked using the same code below,
932 * for 'auxiliary' devices.
933 */
934
935typedef struct spa_aux {
936 uint64_t aux_guid;
937 uint64_t aux_pool;
938 avl_node_t aux_avl;
939 int aux_count;
940} spa_aux_t;
941
ee36c709 942static inline int
34dc7c2f
BB
943spa_aux_compare(const void *a, const void *b)
944{
ee36c709
GN
945 const spa_aux_t *sa = (const spa_aux_t *)a;
946 const spa_aux_t *sb = (const spa_aux_t *)b;
34dc7c2f 947
ca577779 948 return (TREE_CMP(sa->aux_guid, sb->aux_guid));
34dc7c2f
BB
949}
950
65c7cc49 951static void
34dc7c2f
BB
952spa_aux_add(vdev_t *vd, avl_tree_t *avl)
953{
954 avl_index_t where;
955 spa_aux_t search;
956 spa_aux_t *aux;
957
958 search.aux_guid = vd->vdev_guid;
959 if ((aux = avl_find(avl, &search, &where)) != NULL) {
960 aux->aux_count++;
961 } else {
79c76d5b 962 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
34dc7c2f
BB
963 aux->aux_guid = vd->vdev_guid;
964 aux->aux_count = 1;
965 avl_insert(avl, aux, where);
966 }
967}
968
65c7cc49 969static void
34dc7c2f
BB
970spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
971{
972 spa_aux_t search;
973 spa_aux_t *aux;
974 avl_index_t where;
975
976 search.aux_guid = vd->vdev_guid;
977 aux = avl_find(avl, &search, &where);
978
979 ASSERT(aux != NULL);
980
981 if (--aux->aux_count == 0) {
982 avl_remove(avl, aux);
983 kmem_free(aux, sizeof (spa_aux_t));
984 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
985 aux->aux_pool = 0ULL;
986 }
987}
988
65c7cc49 989static boolean_t
b128c09f 990spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
34dc7c2f
BB
991{
992 spa_aux_t search, *found;
34dc7c2f
BB
993
994 search.aux_guid = guid;
b128c09f 995 found = avl_find(avl, &search, NULL);
34dc7c2f
BB
996
997 if (pool) {
998 if (found)
999 *pool = found->aux_pool;
1000 else
1001 *pool = 0ULL;
1002 }
1003
b128c09f
BB
1004 if (refcnt) {
1005 if (found)
1006 *refcnt = found->aux_count;
1007 else
1008 *refcnt = 0;
1009 }
1010
34dc7c2f
BB
1011 return (found != NULL);
1012}
1013
65c7cc49 1014static void
34dc7c2f
BB
1015spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1016{
1017 spa_aux_t search, *found;
1018 avl_index_t where;
1019
1020 search.aux_guid = vd->vdev_guid;
1021 found = avl_find(avl, &search, &where);
1022 ASSERT(found != NULL);
1023 ASSERT(found->aux_pool == 0ULL);
1024
1025 found->aux_pool = spa_guid(vd->vdev_spa);
1026}
1027
1028/*
1029 * Spares are tracked globally due to the following constraints:
1030 *
aa755b35
MA
1031 * - A spare may be part of multiple pools.
1032 * - A spare may be added to a pool even if it's actively in use within
34dc7c2f 1033 * another pool.
aa755b35 1034 * - A spare in use in any pool can only be the source of a replacement if
34dc7c2f
BB
1035 * the target is a spare in the same pool.
1036 *
1037 * We keep track of all spares on the system through the use of a reference
1038 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1039 * spare, then we bump the reference count in the AVL tree. In addition, we set
1040 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1041 * inactive). When a spare is made active (used to replace a device in the
1042 * pool), we also keep track of which pool its been made a part of.
1043 *
1044 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1045 * called under the spa_namespace lock as part of vdev reconfiguration. The
1046 * separate spare lock exists for the status query path, which does not need to
1047 * be completely consistent with respect to other vdev configuration changes.
1048 */
1049
1050static int
1051spa_spare_compare(const void *a, const void *b)
1052{
1053 return (spa_aux_compare(a, b));
1054}
1055
1056void
1057spa_spare_add(vdev_t *vd)
1058{
1059 mutex_enter(&spa_spare_lock);
1060 ASSERT(!vd->vdev_isspare);
1061 spa_aux_add(vd, &spa_spare_avl);
1062 vd->vdev_isspare = B_TRUE;
1063 mutex_exit(&spa_spare_lock);
1064}
1065
1066void
1067spa_spare_remove(vdev_t *vd)
1068{
1069 mutex_enter(&spa_spare_lock);
1070 ASSERT(vd->vdev_isspare);
1071 spa_aux_remove(vd, &spa_spare_avl);
1072 vd->vdev_isspare = B_FALSE;
1073 mutex_exit(&spa_spare_lock);
1074}
1075
1076boolean_t
b128c09f 1077spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
34dc7c2f
BB
1078{
1079 boolean_t found;
1080
1081 mutex_enter(&spa_spare_lock);
b128c09f 1082 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
34dc7c2f
BB
1083 mutex_exit(&spa_spare_lock);
1084
1085 return (found);
1086}
1087
1088void
1089spa_spare_activate(vdev_t *vd)
1090{
1091 mutex_enter(&spa_spare_lock);
1092 ASSERT(vd->vdev_isspare);
1093 spa_aux_activate(vd, &spa_spare_avl);
1094 mutex_exit(&spa_spare_lock);
1095}
1096
1097/*
1098 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1099 * Cache devices currently only support one pool per cache device, and so
1100 * for these devices the aux reference count is currently unused beyond 1.
1101 */
1102
1103static int
1104spa_l2cache_compare(const void *a, const void *b)
1105{
1106 return (spa_aux_compare(a, b));
1107}
1108
1109void
1110spa_l2cache_add(vdev_t *vd)
1111{
1112 mutex_enter(&spa_l2cache_lock);
1113 ASSERT(!vd->vdev_isl2cache);
1114 spa_aux_add(vd, &spa_l2cache_avl);
1115 vd->vdev_isl2cache = B_TRUE;
1116 mutex_exit(&spa_l2cache_lock);
1117}
1118
1119void
1120spa_l2cache_remove(vdev_t *vd)
1121{
1122 mutex_enter(&spa_l2cache_lock);
1123 ASSERT(vd->vdev_isl2cache);
1124 spa_aux_remove(vd, &spa_l2cache_avl);
1125 vd->vdev_isl2cache = B_FALSE;
1126 mutex_exit(&spa_l2cache_lock);
1127}
1128
1129boolean_t
1130spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1131{
1132 boolean_t found;
1133
1134 mutex_enter(&spa_l2cache_lock);
b128c09f 1135 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
34dc7c2f
BB
1136 mutex_exit(&spa_l2cache_lock);
1137
1138 return (found);
1139}
1140
1141void
1142spa_l2cache_activate(vdev_t *vd)
1143{
1144 mutex_enter(&spa_l2cache_lock);
1145 ASSERT(vd->vdev_isl2cache);
1146 spa_aux_activate(vd, &spa_l2cache_avl);
1147 mutex_exit(&spa_l2cache_lock);
1148}
1149
34dc7c2f
BB
1150/*
1151 * ==========================================================================
1152 * SPA vdev locking
1153 * ==========================================================================
1154 */
1155
1156/*
1157 * Lock the given spa_t for the purpose of adding or removing a vdev.
1158 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1159 * It returns the next transaction group for the spa_t.
1160 */
1161uint64_t
1162spa_vdev_enter(spa_t *spa)
1163{
428870ff 1164 mutex_enter(&spa->spa_vdev_top_lock);
34dc7c2f 1165 mutex_enter(&spa_namespace_lock);
1b939560
BB
1166
1167 vdev_autotrim_stop_all(spa);
1168
428870ff
BB
1169 return (spa_vdev_config_enter(spa));
1170}
1171
9a49d3f3
BB
1172/*
1173 * The same as spa_vdev_enter() above but additionally takes the guid of
1174 * the vdev being detached. When there is a rebuild in process it will be
1175 * suspended while the vdev tree is modified then resumed by spa_vdev_exit().
1176 * The rebuild is canceled if only a single child remains after the detach.
1177 */
1178uint64_t
1179spa_vdev_detach_enter(spa_t *spa, uint64_t guid)
1180{
1181 mutex_enter(&spa->spa_vdev_top_lock);
1182 mutex_enter(&spa_namespace_lock);
1183
1184 vdev_autotrim_stop_all(spa);
1185
1186 if (guid != 0) {
1187 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
1188 if (vd) {
1189 vdev_rebuild_stop_wait(vd->vdev_top);
1190 }
1191 }
1192
1193 return (spa_vdev_config_enter(spa));
1194}
1195
428870ff
BB
1196/*
1197 * Internal implementation for spa_vdev_enter(). Used when a vdev
1198 * operation requires multiple syncs (i.e. removing a device) while
1199 * keeping the spa_namespace_lock held.
1200 */
1201uint64_t
1202spa_vdev_config_enter(spa_t *spa)
1203{
1204 ASSERT(MUTEX_HELD(&spa_namespace_lock));
34dc7c2f 1205
b128c09f 1206 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
34dc7c2f
BB
1207
1208 return (spa_last_synced_txg(spa) + 1);
1209}
1210
1211/*
428870ff
BB
1212 * Used in combination with spa_vdev_config_enter() to allow the syncing
1213 * of multiple transactions without releasing the spa_namespace_lock.
34dc7c2f 1214 */
428870ff
BB
1215void
1216spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
34dc7c2f 1217{
1c27024e
DB
1218 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1219
34dc7c2f
BB
1220 int config_changed = B_FALSE;
1221
1222 ASSERT(txg > spa_last_synced_txg(spa));
1223
b128c09f
BB
1224 spa->spa_pending_vdev = NULL;
1225
34dc7c2f
BB
1226 /*
1227 * Reassess the DTLs.
1228 */
9a49d3f3 1229 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE, B_FALSE);
34dc7c2f 1230
b128c09f 1231 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
34dc7c2f 1232 config_changed = B_TRUE;
428870ff 1233 spa->spa_config_generation++;
34dc7c2f
BB
1234 }
1235
428870ff
BB
1236 /*
1237 * Verify the metaslab classes.
1238 */
1239 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1240 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
aa755b35 1241 ASSERT(metaslab_class_validate(spa_embedded_log_class(spa)) == 0);
cc99f275
DB
1242 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1243 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
428870ff 1244
b128c09f 1245 spa_config_exit(spa, SCL_ALL, spa);
34dc7c2f 1246
428870ff
BB
1247 /*
1248 * Panic the system if the specified tag requires it. This
1249 * is useful for ensuring that configurations are updated
1250 * transactionally.
1251 */
1252 if (zio_injection_enabled)
1253 zio_handle_panic_injection(spa, tag, 0);
1254
34dc7c2f
BB
1255 /*
1256 * Note: this txg_wait_synced() is important because it ensures
1257 * that there won't be more than one config change per txg.
1258 * This allows us to use the txg as the generation number.
1259 */
1260 if (error == 0)
1261 txg_wait_synced(spa->spa_dsl_pool, txg);
1262
1263 if (vd != NULL) {
93cf2076 1264 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
619f0976
GW
1265 if (vd->vdev_ops->vdev_op_leaf) {
1266 mutex_enter(&vd->vdev_initialize_lock);
c10d37dd
GW
1267 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1268 NULL);
619f0976 1269 mutex_exit(&vd->vdev_initialize_lock);
1b939560
BB
1270
1271 mutex_enter(&vd->vdev_trim_lock);
1272 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1273 mutex_exit(&vd->vdev_trim_lock);
619f0976
GW
1274 }
1275
1b939560
BB
1276 /*
1277 * The vdev may be both a leaf and top-level device.
1278 */
1279 vdev_autotrim_stop_wait(vd);
1280
75a089ed 1281 spa_config_enter(spa, SCL_STATE_ALL, spa, RW_WRITER);
34dc7c2f 1282 vdev_free(vd);
75a089ed 1283 spa_config_exit(spa, SCL_STATE_ALL, spa);
34dc7c2f
BB
1284 }
1285
1286 /*
1287 * If the config changed, update the config cache.
1288 */
1289 if (config_changed)
a1d477c2 1290 spa_write_cachefile(spa, B_FALSE, B_TRUE);
428870ff 1291}
34dc7c2f 1292
428870ff
BB
1293/*
1294 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1295 * locking of spa_vdev_enter(), we also want make sure the transactions have
1296 * synced to disk, and then update the global configuration cache with the new
1297 * information.
1298 */
1299int
1300spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1301{
1b939560 1302 vdev_autotrim_restart(spa);
9a49d3f3 1303 vdev_rebuild_restart(spa);
1b939560 1304
428870ff 1305 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
34dc7c2f 1306 mutex_exit(&spa_namespace_lock);
428870ff 1307 mutex_exit(&spa->spa_vdev_top_lock);
34dc7c2f
BB
1308
1309 return (error);
1310}
1311
b128c09f
BB
1312/*
1313 * Lock the given spa_t for the purpose of changing vdev state.
1314 */
1315void
428870ff 1316spa_vdev_state_enter(spa_t *spa, int oplocks)
b128c09f 1317{
428870ff
BB
1318 int locks = SCL_STATE_ALL | oplocks;
1319
1320 /*
1321 * Root pools may need to read of the underlying devfs filesystem
1322 * when opening up a vdev. Unfortunately if we're holding the
1323 * SCL_ZIO lock it will result in a deadlock when we try to issue
1324 * the read from the root filesystem. Instead we "prefetch"
1325 * the associated vnodes that we need prior to opening the
1326 * underlying devices and cache them so that we can prevent
1327 * any I/O when we are doing the actual open.
1328 */
1329 if (spa_is_root(spa)) {
1330 int low = locks & ~(SCL_ZIO - 1);
1331 int high = locks & ~low;
1332
1333 spa_config_enter(spa, high, spa, RW_WRITER);
1334 vdev_hold(spa->spa_root_vdev);
1335 spa_config_enter(spa, low, spa, RW_WRITER);
1336 } else {
1337 spa_config_enter(spa, locks, spa, RW_WRITER);
1338 }
1339 spa->spa_vdev_locks = locks;
b128c09f
BB
1340}
1341
1342int
1343spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1344{
428870ff 1345 boolean_t config_changed = B_FALSE;
4a283c7f
TH
1346 vdev_t *vdev_top;
1347
1348 if (vd == NULL || vd == spa->spa_root_vdev) {
1349 vdev_top = spa->spa_root_vdev;
1350 } else {
1351 vdev_top = vd->vdev_top;
1352 }
428870ff
BB
1353
1354 if (vd != NULL || error == 0)
9a49d3f3 1355 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE, B_FALSE);
428870ff
BB
1356
1357 if (vd != NULL) {
4a283c7f
TH
1358 if (vd != spa->spa_root_vdev)
1359 vdev_state_dirty(vdev_top);
1360
428870ff
BB
1361 config_changed = B_TRUE;
1362 spa->spa_config_generation++;
1363 }
b128c09f 1364
428870ff
BB
1365 if (spa_is_root(spa))
1366 vdev_rele(spa->spa_root_vdev);
1367
1368 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1369 spa_config_exit(spa, spa->spa_vdev_locks, spa);
b128c09f 1370
fb5f0bc8
BB
1371 /*
1372 * If anything changed, wait for it to sync. This ensures that,
76d04993 1373 * from the system administrator's perspective, zpool(8) commands
fb5f0bc8
BB
1374 * are synchronous. This is important for things like zpool offline:
1375 * when the command completes, you expect no further I/O from ZFS.
1376 */
1377 if (vd != NULL)
1378 txg_wait_synced(spa->spa_dsl_pool, 0);
1379
428870ff
BB
1380 /*
1381 * If the config changed, update the config cache.
1382 */
1383 if (config_changed) {
1384 mutex_enter(&spa_namespace_lock);
a1d477c2 1385 spa_write_cachefile(spa, B_FALSE, B_TRUE);
428870ff
BB
1386 mutex_exit(&spa_namespace_lock);
1387 }
1388
b128c09f
BB
1389 return (error);
1390}
1391
34dc7c2f
BB
1392/*
1393 * ==========================================================================
1394 * Miscellaneous functions
1395 * ==========================================================================
1396 */
1397
9ae529ec 1398void
b0bc7a84 1399spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
9ae529ec 1400{
fa86b5db
MA
1401 if (!nvlist_exists(spa->spa_label_features, feature)) {
1402 fnvlist_add_boolean(spa->spa_label_features, feature);
b0bc7a84
MG
1403 /*
1404 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1405 * dirty the vdev config because lock SCL_CONFIG is not held.
1406 * Thankfully, in this case we don't need to dirty the config
1407 * because it will be written out anyway when we finish
1408 * creating the pool.
1409 */
1410 if (tx->tx_txg != TXG_INITIAL)
1411 vdev_config_dirty(spa->spa_root_vdev);
fa86b5db 1412 }
9ae529ec
CS
1413}
1414
1415void
1416spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1417{
fa86b5db
MA
1418 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1419 vdev_config_dirty(spa->spa_root_vdev);
9ae529ec
CS
1420}
1421
34dc7c2f 1422/*
572e2857
BB
1423 * Return the spa_t associated with given pool_guid, if it exists. If
1424 * device_guid is non-zero, determine whether the pool exists *and* contains
1425 * a device with the specified device_guid.
34dc7c2f 1426 */
572e2857
BB
1427spa_t *
1428spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
34dc7c2f
BB
1429{
1430 spa_t *spa;
1431 avl_tree_t *t = &spa_namespace_avl;
1432
1433 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1434
1435 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1436 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1437 continue;
1438 if (spa->spa_root_vdev == NULL)
1439 continue;
1440 if (spa_guid(spa) == pool_guid) {
1441 if (device_guid == 0)
1442 break;
1443
1444 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1445 device_guid) != NULL)
1446 break;
1447
1448 /*
1449 * Check any devices we may be in the process of adding.
1450 */
1451 if (spa->spa_pending_vdev) {
1452 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1453 device_guid) != NULL)
1454 break;
1455 }
1456 }
1457 }
1458
572e2857
BB
1459 return (spa);
1460}
1461
1462/*
1463 * Determine whether a pool with the given pool_guid exists.
1464 */
1465boolean_t
1466spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1467{
1468 return (spa_by_guid(pool_guid, device_guid) != NULL);
34dc7c2f
BB
1469}
1470
1471char *
1472spa_strdup(const char *s)
1473{
1474 size_t len;
1475 char *new;
1476
1477 len = strlen(s);
79c76d5b 1478 new = kmem_alloc(len + 1, KM_SLEEP);
34dc7c2f
BB
1479 bcopy(s, new, len);
1480 new[len] = '\0';
1481
1482 return (new);
1483}
1484
1485void
1486spa_strfree(char *s)
1487{
1488 kmem_free(s, strlen(s) + 1);
1489}
1490
428870ff
BB
1491uint64_t
1492spa_generate_guid(spa_t *spa)
34dc7c2f 1493{
29274c9f 1494 uint64_t guid;
34dc7c2f 1495
428870ff 1496 if (spa != NULL) {
29274c9f
AM
1497 do {
1498 (void) random_get_pseudo_bytes((void *)&guid,
1499 sizeof (guid));
1500 } while (guid == 0 || spa_guid_exists(spa_guid(spa), guid));
428870ff 1501 } else {
29274c9f
AM
1502 do {
1503 (void) random_get_pseudo_bytes((void *)&guid,
1504 sizeof (guid));
1505 } while (guid == 0 || spa_guid_exists(guid, 0));
34dc7c2f
BB
1506 }
1507
428870ff
BB
1508 return (guid);
1509}
1510
1511void
b0bc7a84 1512snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
428870ff 1513{
9ae529ec 1514 char type[256];
428870ff
BB
1515 char *checksum = NULL;
1516 char *compress = NULL;
34dc7c2f 1517
428870ff 1518 if (bp != NULL) {
9ae529ec
CS
1519 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1520 dmu_object_byteswap_t bswap =
1521 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1522 (void) snprintf(type, sizeof (type), "bswap %s %s",
1523 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1524 "metadata" : "data",
1525 dmu_ot_byteswap[bswap].ob_name);
1526 } else {
1527 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1528 sizeof (type));
1529 }
9b67f605
MA
1530 if (!BP_IS_EMBEDDED(bp)) {
1531 checksum =
1532 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1533 }
428870ff 1534 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
34dc7c2f
BB
1535 }
1536
b0bc7a84 1537 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
5c27ec10 1538 compress);
34dc7c2f
BB
1539}
1540
1541void
1542spa_freeze(spa_t *spa)
1543{
1544 uint64_t freeze_txg = 0;
1545
b128c09f 1546 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
1547 if (spa->spa_freeze_txg == UINT64_MAX) {
1548 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1549 spa->spa_freeze_txg = freeze_txg;
1550 }
b128c09f 1551 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1552 if (freeze_txg != 0)
1553 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1554}
1555
0b39b9f9
PS
1556void
1557zfs_panic_recover(const char *fmt, ...)
1558{
1559 va_list adx;
1560
1561 va_start(adx, fmt);
1562 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1563 va_end(adx);
1564}
1565
428870ff
BB
1566/*
1567 * This is a stripped-down version of strtoull, suitable only for converting
d3cc8b15 1568 * lowercase hexadecimal numbers that don't overflow.
428870ff
BB
1569 */
1570uint64_t
e19572e4 1571zfs_strtonum(const char *str, char **nptr)
428870ff
BB
1572{
1573 uint64_t val = 0;
1574 char c;
1575 int digit;
1576
1577 while ((c = *str) != '\0') {
1578 if (c >= '0' && c <= '9')
1579 digit = c - '0';
1580 else if (c >= 'a' && c <= 'f')
1581 digit = 10 + c - 'a';
1582 else
1583 break;
1584
1585 val *= 16;
1586 val += digit;
1587
1588 str++;
1589 }
1590
1591 if (nptr)
1592 *nptr = (char *)str;
1593
1594 return (val);
1595}
1596
cc99f275
DB
1597void
1598spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1599{
1600 /*
1601 * We bump the feature refcount for each special vdev added to the pool
1602 */
1603 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1604 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1605}
1606
34dc7c2f
BB
1607/*
1608 * ==========================================================================
1609 * Accessor functions
1610 * ==========================================================================
1611 */
1612
b128c09f
BB
1613boolean_t
1614spa_shutting_down(spa_t *spa)
34dc7c2f 1615{
b128c09f 1616 return (spa->spa_async_suspended);
34dc7c2f
BB
1617}
1618
1619dsl_pool_t *
1620spa_get_dsl(spa_t *spa)
1621{
1622 return (spa->spa_dsl_pool);
1623}
1624
9ae529ec
CS
1625boolean_t
1626spa_is_initializing(spa_t *spa)
1627{
1628 return (spa->spa_is_initializing);
1629}
1630
a1d477c2
MA
1631boolean_t
1632spa_indirect_vdevs_loaded(spa_t *spa)
1633{
1634 return (spa->spa_indirect_vdevs_loaded);
1635}
1636
34dc7c2f
BB
1637blkptr_t *
1638spa_get_rootblkptr(spa_t *spa)
1639{
1640 return (&spa->spa_ubsync.ub_rootbp);
1641}
1642
1643void
1644spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1645{
1646 spa->spa_uberblock.ub_rootbp = *bp;
1647}
1648
1649void
1650spa_altroot(spa_t *spa, char *buf, size_t buflen)
1651{
1652 if (spa->spa_root == NULL)
1653 buf[0] = '\0';
1654 else
1655 (void) strncpy(buf, spa->spa_root, buflen);
1656}
1657
1658int
1659spa_sync_pass(spa_t *spa)
1660{
1661 return (spa->spa_sync_pass);
1662}
1663
1664char *
1665spa_name(spa_t *spa)
1666{
34dc7c2f
BB
1667 return (spa->spa_name);
1668}
1669
1670uint64_t
1671spa_guid(spa_t *spa)
1672{
3bc7e0fb
GW
1673 dsl_pool_t *dp = spa_get_dsl(spa);
1674 uint64_t guid;
1675
34dc7c2f
BB
1676 /*
1677 * If we fail to parse the config during spa_load(), we can go through
1678 * the error path (which posts an ereport) and end up here with no root
3541dc6d 1679 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
34dc7c2f
BB
1680 * this case.
1681 */
3bc7e0fb
GW
1682 if (spa->spa_root_vdev == NULL)
1683 return (spa->spa_config_guid);
1684
1685 guid = spa->spa_last_synced_guid != 0 ?
1686 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1687
1688 /*
1689 * Return the most recently synced out guid unless we're
1690 * in syncing context.
1691 */
1692 if (dp && dsl_pool_sync_context(dp))
34dc7c2f
BB
1693 return (spa->spa_root_vdev->vdev_guid);
1694 else
3bc7e0fb 1695 return (guid);
3541dc6d
GA
1696}
1697
1698uint64_t
1699spa_load_guid(spa_t *spa)
1700{
1701 /*
1702 * This is a GUID that exists solely as a reference for the
1703 * purposes of the arc. It is generated at load time, and
1704 * is never written to persistent storage.
1705 */
1706 return (spa->spa_load_guid);
34dc7c2f
BB
1707}
1708
1709uint64_t
1710spa_last_synced_txg(spa_t *spa)
1711{
1712 return (spa->spa_ubsync.ub_txg);
1713}
1714
1715uint64_t
1716spa_first_txg(spa_t *spa)
1717{
1718 return (spa->spa_first_txg);
1719}
1720
428870ff
BB
1721uint64_t
1722spa_syncing_txg(spa_t *spa)
1723{
1724 return (spa->spa_syncing_txg);
1725}
1726
3b7f360c
GW
1727/*
1728 * Return the last txg where data can be dirtied. The final txgs
1729 * will be used to just clear out any deferred frees that remain.
1730 */
1731uint64_t
1732spa_final_dirty_txg(spa_t *spa)
1733{
1734 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1735}
1736
b128c09f 1737pool_state_t
34dc7c2f
BB
1738spa_state(spa_t *spa)
1739{
1740 return (spa->spa_state);
1741}
1742
428870ff
BB
1743spa_load_state_t
1744spa_load_state(spa_t *spa)
34dc7c2f 1745{
428870ff 1746 return (spa->spa_load_state);
34dc7c2f
BB
1747}
1748
34dc7c2f 1749uint64_t
428870ff 1750spa_freeze_txg(spa_t *spa)
34dc7c2f 1751{
428870ff 1752 return (spa->spa_freeze_txg);
34dc7c2f
BB
1753}
1754
047187c1 1755/*
1756 * Return the inflated asize for a logical write in bytes. This is used by the
1757 * DMU to calculate the space a logical write will require on disk.
1758 * If lsize is smaller than the largest physical block size allocatable on this
1759 * pool we use its value instead, since the write will end up using the whole
1760 * block anyway.
1761 */
34dc7c2f 1762uint64_t
3ec3bc21 1763spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
34dc7c2f 1764{
047187c1 1765 if (lsize == 0)
1766 return (0); /* No inflation needed */
1767 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
34dc7c2f
BB
1768}
1769
3d45fdd6 1770/*
aa755b35
MA
1771 * Return the amount of slop space in bytes. It is typically 1/32 of the pool
1772 * (3.2%), minus the embedded log space. On very small pools, it may be
f01eaed4
PS
1773 * slightly larger than this. On very large pools, it will be capped to
1774 * the value of spa_max_slop. The embedded log space is not included in
aa755b35
MA
1775 * spa_dspace. By subtracting it, the usable space (per "zfs list") is a
1776 * constant 97% of the total space, regardless of metaslab size (assuming the
1777 * default spa_slop_shift=5 and a non-tiny pool).
1778 *
1779 * See the comment above spa_slop_shift for more details.
3d45fdd6
MA
1780 */
1781uint64_t
4ea3f864
GM
1782spa_get_slop_space(spa_t *spa)
1783{
1325434b
RE
1784 uint64_t space = 0;
1785 uint64_t slop = 0;
1786
1787 /*
1788 * Make sure spa_dedup_dspace has been set.
1789 */
1790 if (spa->spa_dedup_dspace == ~0ULL)
1791 spa_update_dspace(spa);
1792
1793 /*
1794 * spa_get_dspace() includes the space only logically "used" by
1795 * deduplicated data, so since it's not useful to reserve more
1796 * space with more deduplicated data, we subtract that out here.
1797 */
1798 space = spa_get_dspace(spa) - spa->spa_dedup_dspace;
1799 slop = MIN(space >> spa_slop_shift, spa_max_slop);
aa755b35
MA
1800
1801 /*
1802 * Subtract the embedded log space, but no more than half the (3.2%)
1803 * unusable space. Note, the "no more than half" is only relevant if
1804 * zfs_embedded_slog_min_ms >> spa_slop_shift < 2, which is not true by
1805 * default.
1806 */
1807 uint64_t embedded_log =
1808 metaslab_class_get_dspace(spa_embedded_log_class(spa));
1809 slop -= MIN(embedded_log, slop >> 1);
1810
1811 /*
1812 * Slop space should be at least spa_min_slop, but no more than half
1813 * the entire pool.
1814 */
1815 slop = MAX(slop, MIN(space >> 1, spa_min_slop));
1816 return (slop);
3d45fdd6
MA
1817}
1818
34dc7c2f
BB
1819uint64_t
1820spa_get_dspace(spa_t *spa)
1821{
428870ff 1822 return (spa->spa_dspace);
34dc7c2f
BB
1823}
1824
d2734cce
SD
1825uint64_t
1826spa_get_checkpoint_space(spa_t *spa)
1827{
1828 return (spa->spa_checkpoint_info.sci_dspace);
1829}
1830
428870ff
BB
1831void
1832spa_update_dspace(spa_t *spa)
34dc7c2f 1833{
428870ff
BB
1834 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1835 ddt_get_dedup_dspace(spa);
2a673e76 1836 if (spa->spa_nonallocating_dspace > 0) {
a1d477c2 1837 /*
2a673e76
AJ
1838 * Subtract the space provided by all non-allocating vdevs that
1839 * contribute to dspace. If a file is overwritten, its old
1840 * blocks are freed and new blocks are allocated. If there are
1841 * no snapshots of the file, the available space should remain
1842 * the same. The old blocks could be freed from the
1843 * non-allocating vdev, but the new blocks must be allocated on
1844 * other (allocating) vdevs. By reserving the entire size of
1845 * the non-allocating vdevs (including allocated space), we
1846 * ensure that there will be enough space on the allocating
1847 * vdevs for this file overwrite to succeed.
a1d477c2
MA
1848 *
1849 * Note that the DMU/DSL doesn't actually know or care
1850 * how much space is allocated (it does its own tracking
1851 * of how much space has been logically used). So it
1852 * doesn't matter that the data we are moving may be
2a673e76 1853 * allocated twice (on the old device and the new device).
a1d477c2 1854 */
2a673e76
AJ
1855 ASSERT3U(spa->spa_dspace, >=, spa->spa_nonallocating_dspace);
1856 spa->spa_dspace -= spa->spa_nonallocating_dspace;
a1d477c2 1857 }
34dc7c2f
BB
1858}
1859
1860/*
1861 * Return the failure mode that has been set to this pool. The default
1862 * behavior will be to block all I/Os when a complete failure occurs.
1863 */
8fb1ede1 1864uint64_t
34dc7c2f
BB
1865spa_get_failmode(spa_t *spa)
1866{
1867 return (spa->spa_failmode);
1868}
1869
b128c09f
BB
1870boolean_t
1871spa_suspended(spa_t *spa)
1872{
cec3a0a1 1873 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
b128c09f
BB
1874}
1875
34dc7c2f
BB
1876uint64_t
1877spa_version(spa_t *spa)
1878{
1879 return (spa->spa_ubsync.ub_version);
1880}
1881
428870ff
BB
1882boolean_t
1883spa_deflate(spa_t *spa)
1884{
1885 return (spa->spa_deflate);
1886}
1887
1888metaslab_class_t *
1889spa_normal_class(spa_t *spa)
1890{
1891 return (spa->spa_normal_class);
1892}
1893
1894metaslab_class_t *
1895spa_log_class(spa_t *spa)
1896{
1897 return (spa->spa_log_class);
1898}
1899
aa755b35
MA
1900metaslab_class_t *
1901spa_embedded_log_class(spa_t *spa)
1902{
1903 return (spa->spa_embedded_log_class);
1904}
1905
cc99f275
DB
1906metaslab_class_t *
1907spa_special_class(spa_t *spa)
1908{
1909 return (spa->spa_special_class);
1910}
1911
1912metaslab_class_t *
1913spa_dedup_class(spa_t *spa)
1914{
1915 return (spa->spa_dedup_class);
1916}
1917
1918/*
1919 * Locate an appropriate allocation class
1920 */
1921metaslab_class_t *
1922spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1923 uint_t level, uint_t special_smallblk)
1924{
aa755b35
MA
1925 /*
1926 * ZIL allocations determine their class in zio_alloc_zil().
1927 */
1928 ASSERT(objtype != DMU_OT_INTENT_LOG);
cc99f275
DB
1929
1930 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1931
1932 if (DMU_OT_IS_DDT(objtype)) {
1933 if (spa->spa_dedup_class->mc_groups != 0)
1934 return (spa_dedup_class(spa));
1935 else if (has_special_class && zfs_ddt_data_is_special)
1936 return (spa_special_class(spa));
1937 else
1938 return (spa_normal_class(spa));
1939 }
1940
1941 /* Indirect blocks for user data can land in special if allowed */
1942 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1943 if (has_special_class && zfs_user_indirect_is_special)
1944 return (spa_special_class(spa));
1945 else
1946 return (spa_normal_class(spa));
1947 }
1948
1949 if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1950 if (has_special_class)
1951 return (spa_special_class(spa));
1952 else
1953 return (spa_normal_class(spa));
1954 }
1955
1956 /*
1957 * Allow small file blocks in special class in some cases (like
1958 * for the dRAID vdev feature). But always leave a reserve of
1959 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1960 */
1961 if (DMU_OT_IS_FILE(objtype) &&
44170969 1962 has_special_class && size <= special_smallblk) {
cc99f275
DB
1963 metaslab_class_t *special = spa_special_class(spa);
1964 uint64_t alloc = metaslab_class_get_alloc(special);
1965 uint64_t space = metaslab_class_get_space(special);
1966 uint64_t limit =
1967 (space * (100 - zfs_special_class_metadata_reserve_pct))
1968 / 100;
1969
1970 if (alloc < limit)
1971 return (special);
1972 }
1973
1974 return (spa_normal_class(spa));
1975}
1976
0c66c32d
JG
1977void
1978spa_evicting_os_register(spa_t *spa, objset_t *os)
1979{
1980 mutex_enter(&spa->spa_evicting_os_lock);
1981 list_insert_head(&spa->spa_evicting_os_list, os);
1982 mutex_exit(&spa->spa_evicting_os_lock);
1983}
1984
1985void
1986spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1987{
1988 mutex_enter(&spa->spa_evicting_os_lock);
1989 list_remove(&spa->spa_evicting_os_list, os);
1990 cv_broadcast(&spa->spa_evicting_os_cv);
1991 mutex_exit(&spa->spa_evicting_os_lock);
1992}
1993
1994void
1995spa_evicting_os_wait(spa_t *spa)
1996{
1997 mutex_enter(&spa->spa_evicting_os_lock);
1998 while (!list_is_empty(&spa->spa_evicting_os_list))
1999 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
2000 mutex_exit(&spa->spa_evicting_os_lock);
2001
2002 dmu_buf_user_evict_wait();
2003}
2004
34dc7c2f
BB
2005int
2006spa_max_replication(spa_t *spa)
2007{
2008 /*
2009 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
2010 * handle BPs with more than one DVA allocated. Set our max
2011 * replication level accordingly.
2012 */
2013 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
2014 return (1);
2015 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
2016}
2017
428870ff
BB
2018int
2019spa_prev_software_version(spa_t *spa)
2020{
2021 return (spa->spa_prev_software_version);
2022}
2023
cc92e9d0
GW
2024uint64_t
2025spa_deadman_synctime(spa_t *spa)
2026{
2027 return (spa->spa_deadman_synctime);
2028}
2029
1b939560
BB
2030spa_autotrim_t
2031spa_get_autotrim(spa_t *spa)
2032{
2033 return (spa->spa_autotrim);
2034}
2035
8fb1ede1
BB
2036uint64_t
2037spa_deadman_ziotime(spa_t *spa)
2038{
2039 return (spa->spa_deadman_ziotime);
2040}
2041
2042uint64_t
2043spa_get_deadman_failmode(spa_t *spa)
2044{
2045 return (spa->spa_deadman_failmode);
2046}
2047
2048void
2049spa_set_deadman_failmode(spa_t *spa, const char *failmode)
2050{
2051 if (strcmp(failmode, "wait") == 0)
2052 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2053 else if (strcmp(failmode, "continue") == 0)
2054 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
2055 else if (strcmp(failmode, "panic") == 0)
2056 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
2057 else
2058 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2059}
2060
57940b43
RM
2061void
2062spa_set_deadman_ziotime(hrtime_t ns)
2063{
2064 spa_t *spa = NULL;
2065
2066 if (spa_mode_global != SPA_MODE_UNINIT) {
2067 mutex_enter(&spa_namespace_lock);
2068 while ((spa = spa_next(spa)) != NULL)
2069 spa->spa_deadman_ziotime = ns;
2070 mutex_exit(&spa_namespace_lock);
2071 }
2072}
2073
2074void
2075spa_set_deadman_synctime(hrtime_t ns)
2076{
2077 spa_t *spa = NULL;
2078
2079 if (spa_mode_global != SPA_MODE_UNINIT) {
2080 mutex_enter(&spa_namespace_lock);
2081 while ((spa = spa_next(spa)) != NULL)
2082 spa->spa_deadman_synctime = ns;
2083 mutex_exit(&spa_namespace_lock);
2084 }
2085}
2086
34dc7c2f 2087uint64_t
428870ff 2088dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
34dc7c2f 2089{
428870ff
BB
2090 uint64_t asize = DVA_GET_ASIZE(dva);
2091 uint64_t dsize = asize;
34dc7c2f 2092
428870ff 2093 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 2094
428870ff
BB
2095 if (asize != 0 && spa->spa_deflate) {
2096 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2c33b912
BB
2097 if (vd != NULL)
2098 dsize = (asize >> SPA_MINBLOCKSHIFT) *
2099 vd->vdev_deflate_ratio;
34dc7c2f 2100 }
428870ff
BB
2101
2102 return (dsize);
2103}
2104
2105uint64_t
2106bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
2107{
2108 uint64_t dsize = 0;
2109
1c27024e 2110 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
2111 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2112
2113 return (dsize);
2114}
2115
2116uint64_t
2117bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2118{
2119 uint64_t dsize = 0;
2120
2121 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2122
1c27024e 2123 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
428870ff
BB
2124 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2125
b128c09f 2126 spa_config_exit(spa, SCL_VDEV, FTAG);
428870ff
BB
2127
2128 return (dsize);
34dc7c2f
BB
2129}
2130
dae3e9ea
DB
2131uint64_t
2132spa_dirty_data(spa_t *spa)
2133{
2134 return (spa->spa_dsl_pool->dp_dirty_total);
2135}
2136
ca95f70d
OF
2137/*
2138 * ==========================================================================
2139 * SPA Import Progress Routines
2140 * ==========================================================================
2141 */
2142
2143typedef struct spa_import_progress {
2144 uint64_t pool_guid; /* unique id for updates */
2145 char *pool_name;
2146 spa_load_state_t spa_load_state;
2147 uint64_t mmp_sec_remaining; /* MMP activity check */
2148 uint64_t spa_load_max_txg; /* rewind txg */
2149 procfs_list_node_t smh_node;
2150} spa_import_progress_t;
2151
2152spa_history_list_t *spa_import_progress_list = NULL;
2153
2154static int
2155spa_import_progress_show_header(struct seq_file *f)
2156{
2157 seq_printf(f, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2158 "load_state", "multihost_secs", "max_txg",
2159 "pool_name");
2160 return (0);
2161}
2162
2163static int
2164spa_import_progress_show(struct seq_file *f, void *data)
2165{
2166 spa_import_progress_t *sip = (spa_import_progress_t *)data;
2167
2168 seq_printf(f, "%-20llu %-14llu %-14llu %-12llu %s\n",
2169 (u_longlong_t)sip->pool_guid, (u_longlong_t)sip->spa_load_state,
2170 (u_longlong_t)sip->mmp_sec_remaining,
2171 (u_longlong_t)sip->spa_load_max_txg,
2172 (sip->pool_name ? sip->pool_name : "-"));
2173
2174 return (0);
2175}
2176
2177/* Remove oldest elements from list until there are no more than 'size' left */
2178static void
2179spa_import_progress_truncate(spa_history_list_t *shl, unsigned int size)
2180{
2181 spa_import_progress_t *sip;
2182 while (shl->size > size) {
2183 sip = list_remove_head(&shl->procfs_list.pl_list);
2184 if (sip->pool_name)
2185 spa_strfree(sip->pool_name);
2186 kmem_free(sip, sizeof (spa_import_progress_t));
2187 shl->size--;
2188 }
2189
2190 IMPLY(size == 0, list_is_empty(&shl->procfs_list.pl_list));
2191}
2192
2193static void
2194spa_import_progress_init(void)
2195{
2196 spa_import_progress_list = kmem_zalloc(sizeof (spa_history_list_t),
2197 KM_SLEEP);
2198
2199 spa_import_progress_list->size = 0;
2200
2201 spa_import_progress_list->procfs_list.pl_private =
2202 spa_import_progress_list;
2203
2204 procfs_list_install("zfs",
7b8363d7 2205 NULL,
ca95f70d
OF
2206 "import_progress",
2207 0644,
2208 &spa_import_progress_list->procfs_list,
2209 spa_import_progress_show,
2210 spa_import_progress_show_header,
2211 NULL,
2212 offsetof(spa_import_progress_t, smh_node));
2213}
2214
2215static void
2216spa_import_progress_destroy(void)
2217{
2218 spa_history_list_t *shl = spa_import_progress_list;
2219 procfs_list_uninstall(&shl->procfs_list);
2220 spa_import_progress_truncate(shl, 0);
ca95f70d 2221 procfs_list_destroy(&shl->procfs_list);
75c09c50 2222 kmem_free(shl, sizeof (spa_history_list_t));
ca95f70d
OF
2223}
2224
2225int
2226spa_import_progress_set_state(uint64_t pool_guid,
2227 spa_load_state_t load_state)
2228{
2229 spa_history_list_t *shl = spa_import_progress_list;
2230 spa_import_progress_t *sip;
2231 int error = ENOENT;
2232
2233 if (shl->size == 0)
2234 return (0);
2235
2236 mutex_enter(&shl->procfs_list.pl_lock);
2237 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2238 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2239 if (sip->pool_guid == pool_guid) {
2240 sip->spa_load_state = load_state;
2241 error = 0;
2242 break;
2243 }
2244 }
2245 mutex_exit(&shl->procfs_list.pl_lock);
2246
2247 return (error);
2248}
2249
2250int
2251spa_import_progress_set_max_txg(uint64_t pool_guid, uint64_t load_max_txg)
2252{
2253 spa_history_list_t *shl = spa_import_progress_list;
2254 spa_import_progress_t *sip;
2255 int error = ENOENT;
2256
2257 if (shl->size == 0)
2258 return (0);
2259
2260 mutex_enter(&shl->procfs_list.pl_lock);
2261 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2262 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2263 if (sip->pool_guid == pool_guid) {
2264 sip->spa_load_max_txg = load_max_txg;
2265 error = 0;
2266 break;
2267 }
2268 }
2269 mutex_exit(&shl->procfs_list.pl_lock);
2270
2271 return (error);
2272}
2273
2274int
2275spa_import_progress_set_mmp_check(uint64_t pool_guid,
2276 uint64_t mmp_sec_remaining)
2277{
2278 spa_history_list_t *shl = spa_import_progress_list;
2279 spa_import_progress_t *sip;
2280 int error = ENOENT;
2281
2282 if (shl->size == 0)
2283 return (0);
2284
2285 mutex_enter(&shl->procfs_list.pl_lock);
2286 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2287 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2288 if (sip->pool_guid == pool_guid) {
2289 sip->mmp_sec_remaining = mmp_sec_remaining;
2290 error = 0;
2291 break;
2292 }
2293 }
2294 mutex_exit(&shl->procfs_list.pl_lock);
2295
2296 return (error);
2297}
2298
2299/*
2300 * A new import is in progress, add an entry.
2301 */
2302void
2303spa_import_progress_add(spa_t *spa)
2304{
2305 spa_history_list_t *shl = spa_import_progress_list;
2306 spa_import_progress_t *sip;
2307 char *poolname = NULL;
2308
2309 sip = kmem_zalloc(sizeof (spa_import_progress_t), KM_SLEEP);
2310 sip->pool_guid = spa_guid(spa);
2311
2312 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME,
2313 &poolname);
2314 if (poolname == NULL)
2315 poolname = spa_name(spa);
2316 sip->pool_name = spa_strdup(poolname);
2317 sip->spa_load_state = spa_load_state(spa);
2318
2319 mutex_enter(&shl->procfs_list.pl_lock);
2320 procfs_list_add(&shl->procfs_list, sip);
2321 shl->size++;
2322 mutex_exit(&shl->procfs_list.pl_lock);
2323}
2324
2325void
2326spa_import_progress_remove(uint64_t pool_guid)
2327{
2328 spa_history_list_t *shl = spa_import_progress_list;
2329 spa_import_progress_t *sip;
2330
2331 mutex_enter(&shl->procfs_list.pl_lock);
2332 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2333 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2334 if (sip->pool_guid == pool_guid) {
2335 if (sip->pool_name)
2336 spa_strfree(sip->pool_name);
2337 list_remove(&shl->procfs_list.pl_list, sip);
2338 shl->size--;
2339 kmem_free(sip, sizeof (spa_import_progress_t));
2340 break;
2341 }
2342 }
2343 mutex_exit(&shl->procfs_list.pl_lock);
2344}
2345
34dc7c2f
BB
2346/*
2347 * ==========================================================================
2348 * Initialization and Termination
2349 * ==========================================================================
2350 */
2351
2352static int
2353spa_name_compare(const void *a1, const void *a2)
2354{
2355 const spa_t *s1 = a1;
2356 const spa_t *s2 = a2;
2357 int s;
2358
2359 s = strcmp(s1->spa_name, s2->spa_name);
ee36c709 2360
ca577779 2361 return (TREE_ISIGN(s));
34dc7c2f
BB
2362}
2363
34dc7c2f 2364void
0bc8fd78 2365spa_boot_init(void)
34dc7c2f
BB
2366{
2367 spa_config_load();
2368}
2369
2370void
da92d5cb 2371spa_init(spa_mode_t mode)
34dc7c2f
BB
2372{
2373 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2374 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2375 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2376 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2377
2378 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2379 offsetof(spa_t, spa_avl));
2380
2381 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2382 offsetof(spa_aux_t, aux_avl));
2383
2384 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2385 offsetof(spa_aux_t, aux_avl));
2386
fb5f0bc8 2387 spa_mode_global = mode;
34dc7c2f 2388
498877ba 2389#ifndef _KERNEL
da92d5cb 2390 if (spa_mode_global != SPA_MODE_READ && dprintf_find_string("watch")) {
498877ba
MA
2391 struct sigaction sa;
2392
2393 sa.sa_flags = SA_SIGINFO;
2394 sigemptyset(&sa.sa_mask);
2395 sa.sa_sigaction = arc_buf_sigsegv;
2396
2397 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
2398 perror("could not enable watchpoints: "
2399 "sigaction(SIGSEGV, ...) = ");
2400 } else {
2401 arc_watch = B_TRUE;
2402 }
2403 }
2404#endif
2405
26685276 2406 fm_init();
424fd7c3 2407 zfs_refcount_init();
34dc7c2f 2408 unique_init();
ca577779
PD
2409 zfs_btree_init();
2410 metaslab_stat_init();
ecf3d9b8 2411 ddt_init();
34dc7c2f
BB
2412 zio_init();
2413 dmu_init();
2414 zil_init();
2415 vdev_cache_stat_init();
551905dd 2416 vdev_mirror_stat_init();
ab9f4b0b 2417 vdev_raidz_math_init();
da8f51e1 2418 vdev_file_init();
34dc7c2f
BB
2419 zfs_prop_init();
2420 zpool_prop_init();
9ae529ec 2421 zpool_feature_init();
34dc7c2f 2422 spa_config_load();
2a673e76 2423 vdev_prop_init();
b128c09f 2424 l2arc_start();
d4a72f23 2425 scan_init();
6a9d6359 2426 qat_init();
ca95f70d 2427 spa_import_progress_init();
34dc7c2f
BB
2428}
2429
2430void
2431spa_fini(void)
2432{
b128c09f
BB
2433 l2arc_stop();
2434
34dc7c2f
BB
2435 spa_evict_all();
2436
da8f51e1 2437 vdev_file_fini();
34dc7c2f 2438 vdev_cache_stat_fini();
551905dd 2439 vdev_mirror_stat_fini();
ab9f4b0b 2440 vdev_raidz_math_fini();
34dc7c2f
BB
2441 zil_fini();
2442 dmu_fini();
2443 zio_fini();
ecf3d9b8 2444 ddt_fini();
ca577779
PD
2445 metaslab_stat_fini();
2446 zfs_btree_fini();
34dc7c2f 2447 unique_fini();
424fd7c3 2448 zfs_refcount_fini();
26685276 2449 fm_fini();
d4a72f23 2450 scan_fini();
6a9d6359 2451 qat_fini();
ca95f70d 2452 spa_import_progress_destroy();
34dc7c2f
BB
2453
2454 avl_destroy(&spa_namespace_avl);
2455 avl_destroy(&spa_spare_avl);
2456 avl_destroy(&spa_l2cache_avl);
2457
2458 cv_destroy(&spa_namespace_cv);
2459 mutex_destroy(&spa_namespace_lock);
2460 mutex_destroy(&spa_spare_lock);
2461 mutex_destroy(&spa_l2cache_lock);
2462}
2463
2464/*
aa755b35 2465 * Return whether this pool has a dedicated slog device. No locking needed.
34dc7c2f 2466 * It's not a problem if the wrong answer is returned as it's only for
aa755b35 2467 * performance and not correctness.
34dc7c2f
BB
2468 */
2469boolean_t
2470spa_has_slogs(spa_t *spa)
2471{
f8020c93 2472 return (spa->spa_log_class->mc_groups != 0);
34dc7c2f 2473}
b128c09f 2474
428870ff
BB
2475spa_log_state_t
2476spa_get_log_state(spa_t *spa)
2477{
2478 return (spa->spa_log_state);
2479}
2480
2481void
2482spa_set_log_state(spa_t *spa, spa_log_state_t state)
2483{
2484 spa->spa_log_state = state;
2485}
2486
b128c09f
BB
2487boolean_t
2488spa_is_root(spa_t *spa)
2489{
2490 return (spa->spa_is_root);
2491}
fb5f0bc8
BB
2492
2493boolean_t
2494spa_writeable(spa_t *spa)
2495{
da92d5cb 2496 return (!!(spa->spa_mode & SPA_MODE_WRITE) && spa->spa_trust_config);
fb5f0bc8
BB
2497}
2498
acbad6ff
AR
2499/*
2500 * Returns true if there is a pending sync task in any of the current
2501 * syncing txg, the current quiescing txg, or the current open txg.
2502 */
2503boolean_t
2504spa_has_pending_synctask(spa_t *spa)
2505{
d2734cce
SD
2506 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2507 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
acbad6ff
AR
2508}
2509
da92d5cb 2510spa_mode_t
fb5f0bc8
BB
2511spa_mode(spa_t *spa)
2512{
2513 return (spa->spa_mode);
2514}
428870ff
BB
2515
2516uint64_t
2517spa_bootfs(spa_t *spa)
2518{
2519 return (spa->spa_bootfs);
2520}
2521
2522uint64_t
2523spa_delegation(spa_t *spa)
2524{
2525 return (spa->spa_delegation);
2526}
2527
2528objset_t *
2529spa_meta_objset(spa_t *spa)
2530{
2531 return (spa->spa_meta_objset);
2532}
2533
2534enum zio_checksum
2535spa_dedup_checksum(spa_t *spa)
2536{
2537 return (spa->spa_dedup_checksum);
2538}
2539
2540/*
2541 * Reset pool scan stat per scan pass (or reboot).
2542 */
2543void
2544spa_scan_stat_init(spa_t *spa)
2545{
2546 /* data not stored on disk */
2547 spa->spa_scan_pass_start = gethrestime_sec();
0ea05c64
AP
2548 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2549 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2550 else
2551 spa->spa_scan_pass_scrub_pause = 0;
2552 spa->spa_scan_pass_scrub_spent_paused = 0;
428870ff 2553 spa->spa_scan_pass_exam = 0;
d4a72f23 2554 spa->spa_scan_pass_issued = 0;
428870ff
BB
2555 vdev_scan_stat_init(spa->spa_root_vdev);
2556}
2557
2558/*
2559 * Get scan stats for zpool status reports
2560 */
2561int
2562spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2563{
2564 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2565
2566 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2e528b49 2567 return (SET_ERROR(ENOENT));
428870ff
BB
2568 bzero(ps, sizeof (pool_scan_stat_t));
2569
2570 /* data stored on disk */
2571 ps->pss_func = scn->scn_phys.scn_func;
d4a72f23 2572 ps->pss_state = scn->scn_phys.scn_state;
428870ff
BB
2573 ps->pss_start_time = scn->scn_phys.scn_start_time;
2574 ps->pss_end_time = scn->scn_phys.scn_end_time;
2575 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
d4677269 2576 ps->pss_examined = scn->scn_phys.scn_examined;
428870ff
BB
2577 ps->pss_to_process = scn->scn_phys.scn_to_process;
2578 ps->pss_processed = scn->scn_phys.scn_processed;
2579 ps->pss_errors = scn->scn_phys.scn_errors;
428870ff
BB
2580
2581 /* data not stored on disk */
428870ff 2582 ps->pss_pass_exam = spa->spa_scan_pass_exam;
d4677269 2583 ps->pss_pass_start = spa->spa_scan_pass_start;
0ea05c64
AP
2584 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2585 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
d4677269
TC
2586 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2587 ps->pss_issued =
2588 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
428870ff
BB
2589
2590 return (0);
2591}
c28b2279 2592
f1512ee6
MA
2593int
2594spa_maxblocksize(spa_t *spa)
2595{
2596 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2597 return (SPA_MAXBLOCKSIZE);
2598 else
2599 return (SPA_OLD_MAXBLOCKSIZE);
2600}
2601
a1d477c2
MA
2602
2603/*
2604 * Returns the txg that the last device removal completed. No indirect mappings
2605 * have been added since this txg.
2606 */
2607uint64_t
2608spa_get_last_removal_txg(spa_t *spa)
2609{
2610 uint64_t vdevid;
2611 uint64_t ret = -1ULL;
2612
2613 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2614 /*
2615 * sr_prev_indirect_vdev is only modified while holding all the
2616 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2617 * examining it.
2618 */
2619 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2620
2621 while (vdevid != -1ULL) {
2622 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2623 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2624
2625 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2626
2627 /*
2628 * If the removal did not remap any data, we don't care.
2629 */
2630 if (vdev_indirect_births_count(vib) != 0) {
2631 ret = vdev_indirect_births_last_entry_txg(vib);
2632 break;
2633 }
2634
2635 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2636 }
2637 spa_config_exit(spa, SCL_VDEV, FTAG);
2638
2639 IMPLY(ret != -1ULL,
2640 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2641
2642 return (ret);
2643}
2644
50c957f7
NB
2645int
2646spa_maxdnodesize(spa_t *spa)
2647{
2648 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2649 return (DNODE_MAX_SIZE);
2650 else
2651 return (DNODE_MIN_SIZE);
2652}
2653
379ca9cf
OF
2654boolean_t
2655spa_multihost(spa_t *spa)
2656{
2657 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2658}
2659
25f06d67
BB
2660uint32_t
2661spa_get_hostid(spa_t *spa)
379ca9cf 2662{
25f06d67 2663 return (spa->spa_hostid);
379ca9cf
OF
2664}
2665
6cb8e530
PZ
2666boolean_t
2667spa_trust_config(spa_t *spa)
2668{
2669 return (spa->spa_trust_config);
2670}
2671
2672uint64_t
2673spa_missing_tvds_allowed(spa_t *spa)
2674{
2675 return (spa->spa_missing_tvds_allowed);
2676}
2677
93e28d66
SD
2678space_map_t *
2679spa_syncing_log_sm(spa_t *spa)
2680{
2681 return (spa->spa_syncing_log_sm);
2682}
2683
6cb8e530
PZ
2684void
2685spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2686{
2687 spa->spa_missing_tvds = missing;
2688}
2689
f0ed6c74
TH
2690/*
2691 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2692 */
2693const char *
2694spa_state_to_name(spa_t *spa)
2695{
78fac8d9
RE
2696 ASSERT3P(spa, !=, NULL);
2697
2698 /*
2699 * it is possible for the spa to exist, without root vdev
2700 * as the spa transitions during import/export
2701 */
2702 vdev_t *rvd = spa->spa_root_vdev;
2703 if (rvd == NULL) {
2704 return ("TRANSITIONING");
2705 }
2706 vdev_state_t state = rvd->vdev_state;
2707 vdev_aux_t aux = rvd->vdev_stat.vs_aux;
f0ed6c74
TH
2708
2709 if (spa_suspended(spa) &&
2710 (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE))
2711 return ("SUSPENDED");
2712
2713 switch (state) {
2714 case VDEV_STATE_CLOSED:
2715 case VDEV_STATE_OFFLINE:
2716 return ("OFFLINE");
2717 case VDEV_STATE_REMOVED:
2718 return ("REMOVED");
2719 case VDEV_STATE_CANT_OPEN:
2720 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2721 return ("FAULTED");
2722 else if (aux == VDEV_AUX_SPLIT_POOL)
2723 return ("SPLIT");
2724 else
2725 return ("UNAVAIL");
2726 case VDEV_STATE_FAULTED:
2727 return ("FAULTED");
2728 case VDEV_STATE_DEGRADED:
2729 return ("DEGRADED");
2730 case VDEV_STATE_HEALTHY:
2731 return ("ONLINE");
2732 default:
2733 break;
2734 }
2735
2736 return ("UNKNOWN");
2737}
2738
d2734cce
SD
2739boolean_t
2740spa_top_vdevs_spacemap_addressable(spa_t *spa)
2741{
2742 vdev_t *rvd = spa->spa_root_vdev;
2743 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2744 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2745 return (B_FALSE);
2746 }
2747 return (B_TRUE);
2748}
2749
2750boolean_t
2751spa_has_checkpoint(spa_t *spa)
2752{
2753 return (spa->spa_checkpoint_txg != 0);
2754}
2755
2756boolean_t
2757spa_importing_readonly_checkpoint(spa_t *spa)
2758{
2759 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
da92d5cb 2760 spa->spa_mode == SPA_MODE_READ);
d2734cce
SD
2761}
2762
2763uint64_t
2764spa_min_claim_txg(spa_t *spa)
2765{
2766 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2767
2768 if (checkpoint_txg != 0)
2769 return (checkpoint_txg + 1);
2770
2771 return (spa->spa_first_txg);
2772}
2773
2774/*
2775 * If there is a checkpoint, async destroys may consume more space from
2776 * the pool instead of freeing it. In an attempt to save the pool from
2777 * getting suspended when it is about to run out of space, we stop
2778 * processing async destroys.
2779 */
2780boolean_t
2781spa_suspend_async_destroy(spa_t *spa)
2782{
2783 dsl_pool_t *dp = spa_get_dsl(spa);
2784
2785 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2786 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2787 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2788 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2789
2790 if (spa_has_checkpoint(spa) && avail == 0)
2791 return (B_TRUE);
2792
2793 return (B_FALSE);
2794}
2795
93ce2b4c 2796#if defined(_KERNEL)
8fb1ede1 2797
e64e84ec
MM
2798int
2799param_set_deadman_failmode_common(const char *val)
8fb1ede1
BB
2800{
2801 spa_t *spa = NULL;
2802 char *p;
2803
2804 if (val == NULL)
e64e84ec 2805 return (SET_ERROR(EINVAL));
8fb1ede1
BB
2806
2807 if ((p = strchr(val, '\n')) != NULL)
2808 *p = '\0';
2809
2810 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2811 strcmp(val, "panic"))
e64e84ec 2812 return (SET_ERROR(EINVAL));
8fb1ede1 2813
da92d5cb 2814 if (spa_mode_global != SPA_MODE_UNINIT) {
d1043e2f
TC
2815 mutex_enter(&spa_namespace_lock);
2816 while ((spa = spa_next(spa)) != NULL)
2817 spa_set_deadman_failmode(spa, val);
2818 mutex_exit(&spa_namespace_lock);
2819 }
8fb1ede1 2820
e64e84ec 2821 return (0);
8fb1ede1 2822}
03fdcb9a
MM
2823#endif
2824
c28b2279
BB
2825/* Namespace manipulation */
2826EXPORT_SYMBOL(spa_lookup);
2827EXPORT_SYMBOL(spa_add);
2828EXPORT_SYMBOL(spa_remove);
2829EXPORT_SYMBOL(spa_next);
2830
2831/* Refcount functions */
2832EXPORT_SYMBOL(spa_open_ref);
2833EXPORT_SYMBOL(spa_close);
2834EXPORT_SYMBOL(spa_refcount_zero);
2835
2836/* Pool configuration lock */
2837EXPORT_SYMBOL(spa_config_tryenter);
2838EXPORT_SYMBOL(spa_config_enter);
2839EXPORT_SYMBOL(spa_config_exit);
2840EXPORT_SYMBOL(spa_config_held);
2841
2842/* Pool vdev add/remove lock */
2843EXPORT_SYMBOL(spa_vdev_enter);
2844EXPORT_SYMBOL(spa_vdev_exit);
2845
2846/* Pool vdev state change lock */
2847EXPORT_SYMBOL(spa_vdev_state_enter);
2848EXPORT_SYMBOL(spa_vdev_state_exit);
2849
2850/* Accessor functions */
2851EXPORT_SYMBOL(spa_shutting_down);
2852EXPORT_SYMBOL(spa_get_dsl);
2853EXPORT_SYMBOL(spa_get_rootblkptr);
2854EXPORT_SYMBOL(spa_set_rootblkptr);
2855EXPORT_SYMBOL(spa_altroot);
2856EXPORT_SYMBOL(spa_sync_pass);
2857EXPORT_SYMBOL(spa_name);
2858EXPORT_SYMBOL(spa_guid);
2859EXPORT_SYMBOL(spa_last_synced_txg);
2860EXPORT_SYMBOL(spa_first_txg);
2861EXPORT_SYMBOL(spa_syncing_txg);
2862EXPORT_SYMBOL(spa_version);
2863EXPORT_SYMBOL(spa_state);
2864EXPORT_SYMBOL(spa_load_state);
2865EXPORT_SYMBOL(spa_freeze_txg);
c28b2279
BB
2866EXPORT_SYMBOL(spa_get_dspace);
2867EXPORT_SYMBOL(spa_update_dspace);
2868EXPORT_SYMBOL(spa_deflate);
2869EXPORT_SYMBOL(spa_normal_class);
2870EXPORT_SYMBOL(spa_log_class);
cc99f275
DB
2871EXPORT_SYMBOL(spa_special_class);
2872EXPORT_SYMBOL(spa_preferred_class);
c28b2279
BB
2873EXPORT_SYMBOL(spa_max_replication);
2874EXPORT_SYMBOL(spa_prev_software_version);
2875EXPORT_SYMBOL(spa_get_failmode);
2876EXPORT_SYMBOL(spa_suspended);
2877EXPORT_SYMBOL(spa_bootfs);
2878EXPORT_SYMBOL(spa_delegation);
2879EXPORT_SYMBOL(spa_meta_objset);
f1512ee6 2880EXPORT_SYMBOL(spa_maxblocksize);
50c957f7 2881EXPORT_SYMBOL(spa_maxdnodesize);
c28b2279
BB
2882
2883/* Miscellaneous support routines */
c28b2279
BB
2884EXPORT_SYMBOL(spa_guid_exists);
2885EXPORT_SYMBOL(spa_strdup);
2886EXPORT_SYMBOL(spa_strfree);
c28b2279 2887EXPORT_SYMBOL(spa_generate_guid);
b0bc7a84 2888EXPORT_SYMBOL(snprintf_blkptr);
c28b2279
BB
2889EXPORT_SYMBOL(spa_freeze);
2890EXPORT_SYMBOL(spa_upgrade);
2891EXPORT_SYMBOL(spa_evict_all);
2892EXPORT_SYMBOL(spa_lookup_by_guid);
2893EXPORT_SYMBOL(spa_has_spare);
2894EXPORT_SYMBOL(dva_get_dsize_sync);
2895EXPORT_SYMBOL(bp_get_dsize_sync);
2896EXPORT_SYMBOL(bp_get_dsize);
2897EXPORT_SYMBOL(spa_has_slogs);
2898EXPORT_SYMBOL(spa_is_root);
2899EXPORT_SYMBOL(spa_writeable);
2900EXPORT_SYMBOL(spa_mode);
c28b2279 2901EXPORT_SYMBOL(spa_namespace_lock);
6cb8e530
PZ
2902EXPORT_SYMBOL(spa_trust_config);
2903EXPORT_SYMBOL(spa_missing_tvds_allowed);
2904EXPORT_SYMBOL(spa_set_missing_tvds);
f0ed6c74 2905EXPORT_SYMBOL(spa_state_to_name);
d2734cce
SD
2906EXPORT_SYMBOL(spa_importing_readonly_checkpoint);
2907EXPORT_SYMBOL(spa_min_claim_txg);
2908EXPORT_SYMBOL(spa_suspend_async_destroy);
2909EXPORT_SYMBOL(spa_has_checkpoint);
2910EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable);
cc92e9d0 2911
03fdcb9a
MM
2912ZFS_MODULE_PARAM(zfs, zfs_, flags, UINT, ZMOD_RW,
2913 "Set additional debugging flags");
0b39b9f9 2914
03fdcb9a
MM
2915ZFS_MODULE_PARAM(zfs, zfs_, recover, INT, ZMOD_RW,
2916 "Set to attempt to recover from fatal errors");
0b39b9f9 2917
03fdcb9a 2918ZFS_MODULE_PARAM(zfs, zfs_, free_leak_on_eio, INT, ZMOD_RW,
0b39b9f9
PS
2919 "Set to ignore IO errors during free and permanently leak the space");
2920
35aa9dc6 2921ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, checktime_ms, ULONG, ZMOD_RW,
03fdcb9a
MM
2922 "Dead I/O check interval in milliseconds");
2923
35aa9dc6 2924ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, enabled, INT, ZMOD_RW,
03fdcb9a
MM
2925 "Enable deadman timer");
2926
2927ZFS_MODULE_PARAM(zfs_spa, spa_, asize_inflation, INT, ZMOD_RW,
2928 "SPA size estimate multiplication factor");
2929
2930ZFS_MODULE_PARAM(zfs, zfs_, ddt_data_is_special, INT, ZMOD_RW,
2931 "Place DDT data into the special class");
2932
2933ZFS_MODULE_PARAM(zfs, zfs_, user_indirect_is_special, INT, ZMOD_RW,
2934 "Place user data indirect blocks into the special class");
2935
03fdcb9a 2936/* BEGIN CSTYLED */
e64e84ec
MM
2937ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, failmode,
2938 param_set_deadman_failmode, param_get_charp, ZMOD_RW,
2939 "Failmode for deadman timer");
2940
2a3aa5a1
MM
2941ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, synctime_ms,
2942 param_set_deadman_synctime, param_get_ulong, ZMOD_RW,
2943 "Pool sync expiration time in milliseconds");
2944
2945ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, ziotime_ms,
2946 param_set_deadman_ziotime, param_get_ulong, ZMOD_RW,
2947 "IO expiration time in milliseconds");
2948
03fdcb9a 2949ZFS_MODULE_PARAM(zfs, zfs_, special_class_metadata_reserve_pct, INT, ZMOD_RW,
1f02ecc5
D
2950 "Small file blocks in special vdevs depends on this much "
2951 "free space available");
02730c33 2952/* END CSTYLED */
2a3aa5a1 2953
7e3df9db 2954ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
2a3aa5a1 2955 param_get_int, ZMOD_RW, "Reserved free space in pool");