zed: Allow autoreplace and fault LEDs for removed vdevs
[zfs.git] / module / zfs / spa_misc.c
blob3b355e0debcc623557c580ea31670854d4dccff6
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 * Copyright 2013 Saso Kiselkov. All rights reserved.
27 * Copyright (c) 2017 Datto Inc.
28 * Copyright (c) 2017, Intel Corporation.
29 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
32 #include <sys/zfs_context.h>
33 #include <sys/zfs_chksum.h>
34 #include <sys/spa_impl.h>
35 #include <sys/zio.h>
36 #include <sys/zio_checksum.h>
37 #include <sys/zio_compress.h>
38 #include <sys/dmu.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/zap.h>
41 #include <sys/zil.h>
42 #include <sys/vdev_impl.h>
43 #include <sys/vdev_initialize.h>
44 #include <sys/vdev_trim.h>
45 #include <sys/vdev_file.h>
46 #include <sys/vdev_raidz.h>
47 #include <sys/metaslab.h>
48 #include <sys/uberblock_impl.h>
49 #include <sys/txg.h>
50 #include <sys/avl.h>
51 #include <sys/unique.h>
52 #include <sys/dsl_pool.h>
53 #include <sys/dsl_dir.h>
54 #include <sys/dsl_prop.h>
55 #include <sys/fm/util.h>
56 #include <sys/dsl_scan.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/metaslab_impl.h>
59 #include <sys/arc.h>
60 #include <sys/brt.h>
61 #include <sys/ddt.h>
62 #include <sys/kstat.h>
63 #include "zfs_prop.h"
64 #include <sys/btree.h>
65 #include <sys/zfeature.h>
66 #include <sys/qat.h>
67 #include <sys/zstd/zstd.h>
70 * SPA locking
72 * There are three basic locks for managing spa_t structures:
74 * spa_namespace_lock (global mutex)
76 * This lock must be acquired to do any of the following:
78 * - Lookup a spa_t by name
79 * - Add or remove a spa_t from the namespace
80 * - Increase spa_refcount from non-zero
81 * - Check if spa_refcount is zero
82 * - Rename a spa_t
83 * - add/remove/attach/detach devices
84 * - Held for the duration of create/destroy/import/export
86 * It does not need to handle recursion. A create or destroy may
87 * reference objects (files or zvols) in other pools, but by
88 * definition they must have an existing reference, and will never need
89 * to lookup a spa_t by name.
91 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
93 * This reference count keep track of any active users of the spa_t. The
94 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
95 * the refcount is never really 'zero' - opening a pool implicitly keeps
96 * some references in the DMU. Internally we check against spa_minref, but
97 * present the image of a zero/non-zero value to consumers.
99 * spa_config_lock[] (per-spa array of rwlocks)
101 * This protects the spa_t from config changes, and must be held in
102 * the following circumstances:
104 * - RW_READER to perform I/O to the spa
105 * - RW_WRITER to change the vdev config
107 * The locking order is fairly straightforward:
109 * spa_namespace_lock -> spa_refcount
111 * The namespace lock must be acquired to increase the refcount from 0
112 * or to check if it is zero.
114 * spa_refcount -> spa_config_lock[]
116 * There must be at least one valid reference on the spa_t to acquire
117 * the config lock.
119 * spa_namespace_lock -> spa_config_lock[]
121 * The namespace lock must always be taken before the config lock.
124 * The spa_namespace_lock can be acquired directly and is globally visible.
126 * The namespace is manipulated using the following functions, all of which
127 * require the spa_namespace_lock to be held.
129 * spa_lookup() Lookup a spa_t by name.
131 * spa_add() Create a new spa_t in the namespace.
133 * spa_remove() Remove a spa_t from the namespace. This also
134 * frees up any memory associated with the spa_t.
136 * spa_next() Returns the next spa_t in the system, or the
137 * first if NULL is passed.
139 * spa_evict_all() Shutdown and remove all spa_t structures in
140 * the system.
142 * spa_guid_exists() Determine whether a pool/device guid exists.
144 * The spa_refcount is manipulated using the following functions:
146 * spa_open_ref() Adds a reference to the given spa_t. Must be
147 * called with spa_namespace_lock held if the
148 * refcount is currently zero.
150 * spa_close() Remove a reference from the spa_t. This will
151 * not free the spa_t or remove it from the
152 * namespace. No locking is required.
154 * spa_refcount_zero() Returns true if the refcount is currently
155 * zero. Must be called with spa_namespace_lock
156 * held.
158 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
159 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
160 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
162 * To read the configuration, it suffices to hold one of these locks as reader.
163 * To modify the configuration, you must hold all locks as writer. To modify
164 * vdev state without altering the vdev tree's topology (e.g. online/offline),
165 * you must hold SCL_STATE and SCL_ZIO as writer.
167 * We use these distinct config locks to avoid recursive lock entry.
168 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
169 * block allocations (SCL_ALLOC), which may require reading space maps
170 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
172 * The spa config locks cannot be normal rwlocks because we need the
173 * ability to hand off ownership. For example, SCL_ZIO is acquired
174 * by the issuing thread and later released by an interrupt thread.
175 * They do, however, obey the usual write-wanted semantics to prevent
176 * writer (i.e. system administrator) starvation.
178 * The lock acquisition rules are as follows:
180 * SCL_CONFIG
181 * Protects changes to the vdev tree topology, such as vdev
182 * add/remove/attach/detach. Protects the dirty config list
183 * (spa_config_dirty_list) and the set of spares and l2arc devices.
185 * SCL_STATE
186 * Protects changes to pool state and vdev state, such as vdev
187 * online/offline/fault/degrade/clear. Protects the dirty state list
188 * (spa_state_dirty_list) and global pool state (spa_state).
190 * SCL_ALLOC
191 * Protects changes to metaslab groups and classes.
192 * Held as reader by metaslab_alloc() and metaslab_claim().
194 * SCL_ZIO
195 * Held by bp-level zios (those which have no io_vd upon entry)
196 * to prevent changes to the vdev tree. The bp-level zio implicitly
197 * protects all of its vdev child zios, which do not hold SCL_ZIO.
199 * SCL_FREE
200 * Protects changes to metaslab groups and classes.
201 * Held as reader by metaslab_free(). SCL_FREE is distinct from
202 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
203 * blocks in zio_done() while another i/o that holds either
204 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
206 * SCL_VDEV
207 * Held as reader to prevent changes to the vdev tree during trivial
208 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
209 * other locks, and lower than all of them, to ensure that it's safe
210 * to acquire regardless of caller context.
212 * In addition, the following rules apply:
214 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
215 * The lock ordering is SCL_CONFIG > spa_props_lock.
217 * (b) I/O operations on leaf vdevs. For any zio operation that takes
218 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
219 * or zio_write_phys() -- the caller must ensure that the config cannot
220 * cannot change in the interim, and that the vdev cannot be reopened.
221 * SCL_STATE as reader suffices for both.
223 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
225 * spa_vdev_enter() Acquire the namespace lock and the config lock
226 * for writing.
228 * spa_vdev_exit() Release the config lock, wait for all I/O
229 * to complete, sync the updated configs to the
230 * cache, and release the namespace lock.
232 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
233 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
234 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
237 static avl_tree_t spa_namespace_avl;
238 kmutex_t spa_namespace_lock;
239 static kcondvar_t spa_namespace_cv;
240 static const int spa_max_replication_override = SPA_DVAS_PER_BP;
242 static kmutex_t spa_spare_lock;
243 static avl_tree_t spa_spare_avl;
244 static kmutex_t spa_l2cache_lock;
245 static avl_tree_t spa_l2cache_avl;
247 spa_mode_t spa_mode_global = SPA_MODE_UNINIT;
249 #ifdef ZFS_DEBUG
251 * Everything except dprintf, set_error, spa, and indirect_remap is on
252 * by default in debug builds.
254 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR |
255 ZFS_DEBUG_INDIRECT_REMAP);
256 #else
257 int zfs_flags = 0;
258 #endif
261 * zfs_recover can be set to nonzero to attempt to recover from
262 * otherwise-fatal errors, typically caused by on-disk corruption. When
263 * set, calls to zfs_panic_recover() will turn into warning messages.
264 * This should only be used as a last resort, as it typically results
265 * in leaked space, or worse.
267 int zfs_recover = B_FALSE;
270 * If destroy encounters an EIO while reading metadata (e.g. indirect
271 * blocks), space referenced by the missing metadata can not be freed.
272 * Normally this causes the background destroy to become "stalled", as
273 * it is unable to make forward progress. While in this stalled state,
274 * all remaining space to free from the error-encountering filesystem is
275 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
276 * permanently leak the space from indirect blocks that can not be read,
277 * and continue to free everything else that it can.
279 * The default, "stalling" behavior is useful if the storage partially
280 * fails (i.e. some but not all i/os fail), and then later recovers. In
281 * this case, we will be able to continue pool operations while it is
282 * partially failed, and when it recovers, we can continue to free the
283 * space, with no leaks. However, note that this case is actually
284 * fairly rare.
286 * Typically pools either (a) fail completely (but perhaps temporarily,
287 * e.g. a top-level vdev going offline), or (b) have localized,
288 * permanent errors (e.g. disk returns the wrong data due to bit flip or
289 * firmware bug). In case (a), this setting does not matter because the
290 * pool will be suspended and the sync thread will not be able to make
291 * forward progress regardless. In case (b), because the error is
292 * permanent, the best we can do is leak the minimum amount of space,
293 * which is what setting this flag will do. Therefore, it is reasonable
294 * for this flag to normally be set, but we chose the more conservative
295 * approach of not setting it, so that there is no possibility of
296 * leaking space in the "partial temporary" failure case.
298 int zfs_free_leak_on_eio = B_FALSE;
301 * Expiration time in milliseconds. This value has two meanings. First it is
302 * used to determine when the spa_deadman() logic should fire. By default the
303 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
304 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
305 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
306 * in one of three behaviors controlled by zfs_deadman_failmode.
308 uint64_t zfs_deadman_synctime_ms = 600000UL; /* 10 min. */
311 * This value controls the maximum amount of time zio_wait() will block for an
312 * outstanding IO. By default this is 300 seconds at which point the "hung"
313 * behavior will be applied as described for zfs_deadman_synctime_ms.
315 uint64_t zfs_deadman_ziotime_ms = 300000UL; /* 5 min. */
318 * Check time in milliseconds. This defines the frequency at which we check
319 * for hung I/O.
321 uint64_t zfs_deadman_checktime_ms = 60000UL; /* 1 min. */
324 * By default the deadman is enabled.
326 int zfs_deadman_enabled = B_TRUE;
329 * Controls the behavior of the deadman when it detects a "hung" I/O.
330 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
332 * wait - Wait for the "hung" I/O (default)
333 * continue - Attempt to recover from a "hung" I/O
334 * panic - Panic the system
336 const char *zfs_deadman_failmode = "wait";
339 * The worst case is single-sector max-parity RAID-Z blocks, in which
340 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
341 * times the size; so just assume that. Add to this the fact that
342 * we can have up to 3 DVAs per bp, and one more factor of 2 because
343 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
344 * the worst case is:
345 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
347 uint_t spa_asize_inflation = 24;
350 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
351 * the pool to be consumed (bounded by spa_max_slop). This ensures that we
352 * don't run the pool completely out of space, due to unaccounted changes (e.g.
353 * to the MOS). It also limits the worst-case time to allocate space. If we
354 * have less than this amount of free space, most ZPL operations (e.g. write,
355 * create) will return ENOSPC. The ZIL metaslabs (spa_embedded_log_class) are
356 * also part of this 3.2% of space which can't be consumed by normal writes;
357 * the slop space "proper" (spa_get_slop_space()) is decreased by the embedded
358 * log space.
360 * Certain operations (e.g. file removal, most administrative actions) can
361 * use half the slop space. They will only return ENOSPC if less than half
362 * the slop space is free. Typically, once the pool has less than the slop
363 * space free, the user will use these operations to free up space in the pool.
364 * These are the operations that call dsl_pool_adjustedsize() with the netfree
365 * argument set to TRUE.
367 * Operations that are almost guaranteed to free up space in the absence of
368 * a pool checkpoint can use up to three quarters of the slop space
369 * (e.g zfs destroy).
371 * A very restricted set of operations are always permitted, regardless of
372 * the amount of free space. These are the operations that call
373 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
374 * increase in the amount of space used, it is possible to run the pool
375 * completely out of space, causing it to be permanently read-only.
377 * Note that on very small pools, the slop space will be larger than
378 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
379 * but we never allow it to be more than half the pool size.
381 * Further, on very large pools, the slop space will be smaller than
382 * 3.2%, to avoid reserving much more space than we actually need; bounded
383 * by spa_max_slop (128GB).
385 * See also the comments in zfs_space_check_t.
387 uint_t spa_slop_shift = 5;
388 static const uint64_t spa_min_slop = 128ULL * 1024 * 1024;
389 static const uint64_t spa_max_slop = 128ULL * 1024 * 1024 * 1024;
390 static const int spa_allocators = 4;
393 void
394 spa_load_failed(spa_t *spa, const char *fmt, ...)
396 va_list adx;
397 char buf[256];
399 va_start(adx, fmt);
400 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
401 va_end(adx);
403 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
404 spa->spa_trust_config ? "trusted" : "untrusted", buf);
407 void
408 spa_load_note(spa_t *spa, const char *fmt, ...)
410 va_list adx;
411 char buf[256];
413 va_start(adx, fmt);
414 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
415 va_end(adx);
417 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
418 spa->spa_trust_config ? "trusted" : "untrusted", buf);
422 * By default dedup and user data indirects land in the special class
424 static int zfs_ddt_data_is_special = B_TRUE;
425 static int zfs_user_indirect_is_special = B_TRUE;
428 * The percentage of special class final space reserved for metadata only.
429 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
430 * let metadata into the class.
432 static uint_t zfs_special_class_metadata_reserve_pct = 25;
435 * ==========================================================================
436 * SPA config locking
437 * ==========================================================================
439 static void
440 spa_config_lock_init(spa_t *spa)
442 for (int i = 0; i < SCL_LOCKS; i++) {
443 spa_config_lock_t *scl = &spa->spa_config_lock[i];
444 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
445 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
446 scl->scl_writer = NULL;
447 scl->scl_write_wanted = 0;
448 scl->scl_count = 0;
452 static void
453 spa_config_lock_destroy(spa_t *spa)
455 for (int i = 0; i < SCL_LOCKS; i++) {
456 spa_config_lock_t *scl = &spa->spa_config_lock[i];
457 mutex_destroy(&scl->scl_lock);
458 cv_destroy(&scl->scl_cv);
459 ASSERT(scl->scl_writer == NULL);
460 ASSERT(scl->scl_write_wanted == 0);
461 ASSERT(scl->scl_count == 0);
466 spa_config_tryenter(spa_t *spa, int locks, const void *tag, krw_t rw)
468 for (int i = 0; i < SCL_LOCKS; i++) {
469 spa_config_lock_t *scl = &spa->spa_config_lock[i];
470 if (!(locks & (1 << i)))
471 continue;
472 mutex_enter(&scl->scl_lock);
473 if (rw == RW_READER) {
474 if (scl->scl_writer || scl->scl_write_wanted) {
475 mutex_exit(&scl->scl_lock);
476 spa_config_exit(spa, locks & ((1 << i) - 1),
477 tag);
478 return (0);
480 } else {
481 ASSERT(scl->scl_writer != curthread);
482 if (scl->scl_count != 0) {
483 mutex_exit(&scl->scl_lock);
484 spa_config_exit(spa, locks & ((1 << i) - 1),
485 tag);
486 return (0);
488 scl->scl_writer = curthread;
490 scl->scl_count++;
491 mutex_exit(&scl->scl_lock);
493 return (1);
496 static void
497 spa_config_enter_impl(spa_t *spa, int locks, const void *tag, krw_t rw,
498 int mmp_flag)
500 (void) tag;
501 int wlocks_held = 0;
503 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
505 for (int i = 0; i < SCL_LOCKS; i++) {
506 spa_config_lock_t *scl = &spa->spa_config_lock[i];
507 if (scl->scl_writer == curthread)
508 wlocks_held |= (1 << i);
509 if (!(locks & (1 << i)))
510 continue;
511 mutex_enter(&scl->scl_lock);
512 if (rw == RW_READER) {
513 while (scl->scl_writer ||
514 (!mmp_flag && scl->scl_write_wanted)) {
515 cv_wait(&scl->scl_cv, &scl->scl_lock);
517 } else {
518 ASSERT(scl->scl_writer != curthread);
519 while (scl->scl_count != 0) {
520 scl->scl_write_wanted++;
521 cv_wait(&scl->scl_cv, &scl->scl_lock);
522 scl->scl_write_wanted--;
524 scl->scl_writer = curthread;
526 scl->scl_count++;
527 mutex_exit(&scl->scl_lock);
529 ASSERT3U(wlocks_held, <=, locks);
532 void
533 spa_config_enter(spa_t *spa, int locks, const void *tag, krw_t rw)
535 spa_config_enter_impl(spa, locks, tag, rw, 0);
539 * The spa_config_enter_mmp() allows the mmp thread to cut in front of
540 * outstanding write lock requests. This is needed since the mmp updates are
541 * time sensitive and failure to service them promptly will result in a
542 * suspended pool. This pool suspension has been seen in practice when there is
543 * a single disk in a pool that is responding slowly and presumably about to
544 * fail.
547 void
548 spa_config_enter_mmp(spa_t *spa, int locks, const void *tag, krw_t rw)
550 spa_config_enter_impl(spa, locks, tag, rw, 1);
553 void
554 spa_config_exit(spa_t *spa, int locks, const void *tag)
556 (void) tag;
557 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
558 spa_config_lock_t *scl = &spa->spa_config_lock[i];
559 if (!(locks & (1 << i)))
560 continue;
561 mutex_enter(&scl->scl_lock);
562 ASSERT(scl->scl_count > 0);
563 if (--scl->scl_count == 0) {
564 ASSERT(scl->scl_writer == NULL ||
565 scl->scl_writer == curthread);
566 scl->scl_writer = NULL; /* OK in either case */
567 cv_broadcast(&scl->scl_cv);
569 mutex_exit(&scl->scl_lock);
574 spa_config_held(spa_t *spa, int locks, krw_t rw)
576 int locks_held = 0;
578 for (int i = 0; i < SCL_LOCKS; i++) {
579 spa_config_lock_t *scl = &spa->spa_config_lock[i];
580 if (!(locks & (1 << i)))
581 continue;
582 if ((rw == RW_READER && scl->scl_count != 0) ||
583 (rw == RW_WRITER && scl->scl_writer == curthread))
584 locks_held |= 1 << i;
587 return (locks_held);
591 * ==========================================================================
592 * SPA namespace functions
593 * ==========================================================================
597 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
598 * Returns NULL if no matching spa_t is found.
600 spa_t *
601 spa_lookup(const char *name)
603 static spa_t search; /* spa_t is large; don't allocate on stack */
604 spa_t *spa;
605 avl_index_t where;
606 char *cp;
608 ASSERT(MUTEX_HELD(&spa_namespace_lock));
610 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
613 * If it's a full dataset name, figure out the pool name and
614 * just use that.
616 cp = strpbrk(search.spa_name, "/@#");
617 if (cp != NULL)
618 *cp = '\0';
620 spa = avl_find(&spa_namespace_avl, &search, &where);
622 return (spa);
626 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
627 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
628 * looking for potentially hung I/Os.
630 void
631 spa_deadman(void *arg)
633 spa_t *spa = arg;
635 /* Disable the deadman if the pool is suspended. */
636 if (spa_suspended(spa))
637 return;
639 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
640 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
641 (u_longlong_t)++spa->spa_deadman_calls);
642 if (zfs_deadman_enabled)
643 vdev_deadman(spa->spa_root_vdev, FTAG);
645 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
646 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
647 MSEC_TO_TICK(zfs_deadman_checktime_ms));
650 static int
651 spa_log_sm_sort_by_txg(const void *va, const void *vb)
653 const spa_log_sm_t *a = va;
654 const spa_log_sm_t *b = vb;
656 return (TREE_CMP(a->sls_txg, b->sls_txg));
660 * Create an uninitialized spa_t with the given name. Requires
661 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
662 * exist by calling spa_lookup() first.
664 spa_t *
665 spa_add(const char *name, nvlist_t *config, const char *altroot)
667 spa_t *spa;
668 spa_config_dirent_t *dp;
670 ASSERT(MUTEX_HELD(&spa_namespace_lock));
672 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
674 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
675 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
676 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
677 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
678 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
679 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
680 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
681 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
682 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
683 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
684 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
685 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
686 mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL);
687 mutex_init(&spa->spa_activities_lock, NULL, MUTEX_DEFAULT, NULL);
689 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
690 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
691 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
692 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
693 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
694 cv_init(&spa->spa_activities_cv, NULL, CV_DEFAULT, NULL);
695 cv_init(&spa->spa_waiters_cv, NULL, CV_DEFAULT, NULL);
697 for (int t = 0; t < TXG_SIZE; t++)
698 bplist_create(&spa->spa_free_bplist[t]);
700 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
701 spa->spa_state = POOL_STATE_UNINITIALIZED;
702 spa->spa_freeze_txg = UINT64_MAX;
703 spa->spa_final_txg = UINT64_MAX;
704 spa->spa_load_max_txg = UINT64_MAX;
705 spa->spa_proc = &p0;
706 spa->spa_proc_state = SPA_PROC_NONE;
707 spa->spa_trust_config = B_TRUE;
708 spa->spa_hostid = zone_get_hostid(NULL);
710 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
711 spa->spa_deadman_ziotime = MSEC2NSEC(zfs_deadman_ziotime_ms);
712 spa_set_deadman_failmode(spa, zfs_deadman_failmode);
714 zfs_refcount_create(&spa->spa_refcount);
715 spa_config_lock_init(spa);
716 spa_stats_init(spa);
718 avl_add(&spa_namespace_avl, spa);
721 * Set the alternate root, if there is one.
723 if (altroot)
724 spa->spa_root = spa_strdup(altroot);
726 spa->spa_alloc_count = spa_allocators;
727 spa->spa_allocs = kmem_zalloc(spa->spa_alloc_count *
728 sizeof (spa_alloc_t), KM_SLEEP);
729 for (int i = 0; i < spa->spa_alloc_count; i++) {
730 mutex_init(&spa->spa_allocs[i].spaa_lock, NULL, MUTEX_DEFAULT,
731 NULL);
732 avl_create(&spa->spa_allocs[i].spaa_tree, zio_bookmark_compare,
733 sizeof (zio_t), offsetof(zio_t, io_queue_node.a));
735 avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
736 sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node));
737 avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg,
738 sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node));
739 list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t),
740 offsetof(log_summary_entry_t, lse_node));
743 * Every pool starts with the default cachefile
745 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
746 offsetof(spa_config_dirent_t, scd_link));
748 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
749 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
750 list_insert_head(&spa->spa_config_list, dp);
752 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
753 KM_SLEEP) == 0);
755 if (config != NULL) {
756 nvlist_t *features;
758 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
759 &features) == 0) {
760 VERIFY(nvlist_dup(features, &spa->spa_label_features,
761 0) == 0);
764 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
767 if (spa->spa_label_features == NULL) {
768 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
769 KM_SLEEP) == 0);
772 spa->spa_min_ashift = INT_MAX;
773 spa->spa_max_ashift = 0;
774 spa->spa_min_alloc = INT_MAX;
775 spa->spa_gcd_alloc = INT_MAX;
777 /* Reset cached value */
778 spa->spa_dedup_dspace = ~0ULL;
781 * As a pool is being created, treat all features as disabled by
782 * setting SPA_FEATURE_DISABLED for all entries in the feature
783 * refcount cache.
785 for (int i = 0; i < SPA_FEATURES; i++) {
786 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
789 list_create(&spa->spa_leaf_list, sizeof (vdev_t),
790 offsetof(vdev_t, vdev_leaf_node));
792 return (spa);
796 * Removes a spa_t from the namespace, freeing up any memory used. Requires
797 * spa_namespace_lock. This is called only after the spa_t has been closed and
798 * deactivated.
800 void
801 spa_remove(spa_t *spa)
803 spa_config_dirent_t *dp;
805 ASSERT(MUTEX_HELD(&spa_namespace_lock));
806 ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED);
807 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
808 ASSERT0(spa->spa_waiters);
810 nvlist_free(spa->spa_config_splitting);
812 avl_remove(&spa_namespace_avl, spa);
813 cv_broadcast(&spa_namespace_cv);
815 if (spa->spa_root)
816 spa_strfree(spa->spa_root);
818 while ((dp = list_remove_head(&spa->spa_config_list)) != NULL) {
819 if (dp->scd_path != NULL)
820 spa_strfree(dp->scd_path);
821 kmem_free(dp, sizeof (spa_config_dirent_t));
824 for (int i = 0; i < spa->spa_alloc_count; i++) {
825 avl_destroy(&spa->spa_allocs[i].spaa_tree);
826 mutex_destroy(&spa->spa_allocs[i].spaa_lock);
828 kmem_free(spa->spa_allocs, spa->spa_alloc_count *
829 sizeof (spa_alloc_t));
831 avl_destroy(&spa->spa_metaslabs_by_flushed);
832 avl_destroy(&spa->spa_sm_logs_by_txg);
833 list_destroy(&spa->spa_log_summary);
834 list_destroy(&spa->spa_config_list);
835 list_destroy(&spa->spa_leaf_list);
837 nvlist_free(spa->spa_label_features);
838 nvlist_free(spa->spa_load_info);
839 nvlist_free(spa->spa_feat_stats);
840 spa_config_set(spa, NULL);
842 zfs_refcount_destroy(&spa->spa_refcount);
844 spa_stats_destroy(spa);
845 spa_config_lock_destroy(spa);
847 for (int t = 0; t < TXG_SIZE; t++)
848 bplist_destroy(&spa->spa_free_bplist[t]);
850 zio_checksum_templates_free(spa);
852 cv_destroy(&spa->spa_async_cv);
853 cv_destroy(&spa->spa_evicting_os_cv);
854 cv_destroy(&spa->spa_proc_cv);
855 cv_destroy(&spa->spa_scrub_io_cv);
856 cv_destroy(&spa->spa_suspend_cv);
857 cv_destroy(&spa->spa_activities_cv);
858 cv_destroy(&spa->spa_waiters_cv);
860 mutex_destroy(&spa->spa_flushed_ms_lock);
861 mutex_destroy(&spa->spa_async_lock);
862 mutex_destroy(&spa->spa_errlist_lock);
863 mutex_destroy(&spa->spa_errlog_lock);
864 mutex_destroy(&spa->spa_evicting_os_lock);
865 mutex_destroy(&spa->spa_history_lock);
866 mutex_destroy(&spa->spa_proc_lock);
867 mutex_destroy(&spa->spa_props_lock);
868 mutex_destroy(&spa->spa_cksum_tmpls_lock);
869 mutex_destroy(&spa->spa_scrub_lock);
870 mutex_destroy(&spa->spa_suspend_lock);
871 mutex_destroy(&spa->spa_vdev_top_lock);
872 mutex_destroy(&spa->spa_feat_stats_lock);
873 mutex_destroy(&spa->spa_activities_lock);
875 kmem_free(spa, sizeof (spa_t));
879 * Given a pool, return the next pool in the namespace, or NULL if there is
880 * none. If 'prev' is NULL, return the first pool.
882 spa_t *
883 spa_next(spa_t *prev)
885 ASSERT(MUTEX_HELD(&spa_namespace_lock));
887 if (prev)
888 return (AVL_NEXT(&spa_namespace_avl, prev));
889 else
890 return (avl_first(&spa_namespace_avl));
894 * ==========================================================================
895 * SPA refcount functions
896 * ==========================================================================
900 * Add a reference to the given spa_t. Must have at least one reference, or
901 * have the namespace lock held.
903 void
904 spa_open_ref(spa_t *spa, const void *tag)
906 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
907 MUTEX_HELD(&spa_namespace_lock));
908 (void) zfs_refcount_add(&spa->spa_refcount, tag);
912 * Remove a reference to the given spa_t. Must have at least one reference, or
913 * have the namespace lock held.
915 void
916 spa_close(spa_t *spa, const void *tag)
918 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
919 MUTEX_HELD(&spa_namespace_lock));
920 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
924 * Remove a reference to the given spa_t held by a dsl dir that is
925 * being asynchronously released. Async releases occur from a taskq
926 * performing eviction of dsl datasets and dirs. The namespace lock
927 * isn't held and the hold by the object being evicted may contribute to
928 * spa_minref (e.g. dataset or directory released during pool export),
929 * so the asserts in spa_close() do not apply.
931 void
932 spa_async_close(spa_t *spa, const void *tag)
934 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
938 * Check to see if the spa refcount is zero. Must be called with
939 * spa_namespace_lock held. We really compare against spa_minref, which is the
940 * number of references acquired when opening a pool
942 boolean_t
943 spa_refcount_zero(spa_t *spa)
945 ASSERT(MUTEX_HELD(&spa_namespace_lock));
947 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
951 * ==========================================================================
952 * SPA spare and l2cache tracking
953 * ==========================================================================
957 * Hot spares and cache devices are tracked using the same code below,
958 * for 'auxiliary' devices.
961 typedef struct spa_aux {
962 uint64_t aux_guid;
963 uint64_t aux_pool;
964 avl_node_t aux_avl;
965 int aux_count;
966 } spa_aux_t;
968 static inline int
969 spa_aux_compare(const void *a, const void *b)
971 const spa_aux_t *sa = (const spa_aux_t *)a;
972 const spa_aux_t *sb = (const spa_aux_t *)b;
974 return (TREE_CMP(sa->aux_guid, sb->aux_guid));
977 static void
978 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
980 avl_index_t where;
981 spa_aux_t search;
982 spa_aux_t *aux;
984 search.aux_guid = vd->vdev_guid;
985 if ((aux = avl_find(avl, &search, &where)) != NULL) {
986 aux->aux_count++;
987 } else {
988 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
989 aux->aux_guid = vd->vdev_guid;
990 aux->aux_count = 1;
991 avl_insert(avl, aux, where);
995 static void
996 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
998 spa_aux_t search;
999 spa_aux_t *aux;
1000 avl_index_t where;
1002 search.aux_guid = vd->vdev_guid;
1003 aux = avl_find(avl, &search, &where);
1005 ASSERT(aux != NULL);
1007 if (--aux->aux_count == 0) {
1008 avl_remove(avl, aux);
1009 kmem_free(aux, sizeof (spa_aux_t));
1010 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
1011 aux->aux_pool = 0ULL;
1015 static boolean_t
1016 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
1018 spa_aux_t search, *found;
1020 search.aux_guid = guid;
1021 found = avl_find(avl, &search, NULL);
1023 if (pool) {
1024 if (found)
1025 *pool = found->aux_pool;
1026 else
1027 *pool = 0ULL;
1030 if (refcnt) {
1031 if (found)
1032 *refcnt = found->aux_count;
1033 else
1034 *refcnt = 0;
1037 return (found != NULL);
1040 static void
1041 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1043 spa_aux_t search, *found;
1044 avl_index_t where;
1046 search.aux_guid = vd->vdev_guid;
1047 found = avl_find(avl, &search, &where);
1048 ASSERT(found != NULL);
1049 ASSERT(found->aux_pool == 0ULL);
1051 found->aux_pool = spa_guid(vd->vdev_spa);
1055 * Spares are tracked globally due to the following constraints:
1057 * - A spare may be part of multiple pools.
1058 * - A spare may be added to a pool even if it's actively in use within
1059 * another pool.
1060 * - A spare in use in any pool can only be the source of a replacement if
1061 * the target is a spare in the same pool.
1063 * We keep track of all spares on the system through the use of a reference
1064 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1065 * spare, then we bump the reference count in the AVL tree. In addition, we set
1066 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1067 * inactive). When a spare is made active (used to replace a device in the
1068 * pool), we also keep track of which pool its been made a part of.
1070 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1071 * called under the spa_namespace lock as part of vdev reconfiguration. The
1072 * separate spare lock exists for the status query path, which does not need to
1073 * be completely consistent with respect to other vdev configuration changes.
1076 static int
1077 spa_spare_compare(const void *a, const void *b)
1079 return (spa_aux_compare(a, b));
1082 void
1083 spa_spare_add(vdev_t *vd)
1085 mutex_enter(&spa_spare_lock);
1086 ASSERT(!vd->vdev_isspare);
1087 spa_aux_add(vd, &spa_spare_avl);
1088 vd->vdev_isspare = B_TRUE;
1089 mutex_exit(&spa_spare_lock);
1092 void
1093 spa_spare_remove(vdev_t *vd)
1095 mutex_enter(&spa_spare_lock);
1096 ASSERT(vd->vdev_isspare);
1097 spa_aux_remove(vd, &spa_spare_avl);
1098 vd->vdev_isspare = B_FALSE;
1099 mutex_exit(&spa_spare_lock);
1102 boolean_t
1103 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
1105 boolean_t found;
1107 mutex_enter(&spa_spare_lock);
1108 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
1109 mutex_exit(&spa_spare_lock);
1111 return (found);
1114 void
1115 spa_spare_activate(vdev_t *vd)
1117 mutex_enter(&spa_spare_lock);
1118 ASSERT(vd->vdev_isspare);
1119 spa_aux_activate(vd, &spa_spare_avl);
1120 mutex_exit(&spa_spare_lock);
1124 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1125 * Cache devices currently only support one pool per cache device, and so
1126 * for these devices the aux reference count is currently unused beyond 1.
1129 static int
1130 spa_l2cache_compare(const void *a, const void *b)
1132 return (spa_aux_compare(a, b));
1135 void
1136 spa_l2cache_add(vdev_t *vd)
1138 mutex_enter(&spa_l2cache_lock);
1139 ASSERT(!vd->vdev_isl2cache);
1140 spa_aux_add(vd, &spa_l2cache_avl);
1141 vd->vdev_isl2cache = B_TRUE;
1142 mutex_exit(&spa_l2cache_lock);
1145 void
1146 spa_l2cache_remove(vdev_t *vd)
1148 mutex_enter(&spa_l2cache_lock);
1149 ASSERT(vd->vdev_isl2cache);
1150 spa_aux_remove(vd, &spa_l2cache_avl);
1151 vd->vdev_isl2cache = B_FALSE;
1152 mutex_exit(&spa_l2cache_lock);
1155 boolean_t
1156 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1158 boolean_t found;
1160 mutex_enter(&spa_l2cache_lock);
1161 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1162 mutex_exit(&spa_l2cache_lock);
1164 return (found);
1167 void
1168 spa_l2cache_activate(vdev_t *vd)
1170 mutex_enter(&spa_l2cache_lock);
1171 ASSERT(vd->vdev_isl2cache);
1172 spa_aux_activate(vd, &spa_l2cache_avl);
1173 mutex_exit(&spa_l2cache_lock);
1177 * ==========================================================================
1178 * SPA vdev locking
1179 * ==========================================================================
1183 * Lock the given spa_t for the purpose of adding or removing a vdev.
1184 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1185 * It returns the next transaction group for the spa_t.
1187 uint64_t
1188 spa_vdev_enter(spa_t *spa)
1190 mutex_enter(&spa->spa_vdev_top_lock);
1191 mutex_enter(&spa_namespace_lock);
1193 vdev_autotrim_stop_all(spa);
1195 return (spa_vdev_config_enter(spa));
1199 * The same as spa_vdev_enter() above but additionally takes the guid of
1200 * the vdev being detached. When there is a rebuild in process it will be
1201 * suspended while the vdev tree is modified then resumed by spa_vdev_exit().
1202 * The rebuild is canceled if only a single child remains after the detach.
1204 uint64_t
1205 spa_vdev_detach_enter(spa_t *spa, uint64_t guid)
1207 mutex_enter(&spa->spa_vdev_top_lock);
1208 mutex_enter(&spa_namespace_lock);
1210 vdev_autotrim_stop_all(spa);
1212 if (guid != 0) {
1213 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE);
1214 if (vd) {
1215 vdev_rebuild_stop_wait(vd->vdev_top);
1219 return (spa_vdev_config_enter(spa));
1223 * Internal implementation for spa_vdev_enter(). Used when a vdev
1224 * operation requires multiple syncs (i.e. removing a device) while
1225 * keeping the spa_namespace_lock held.
1227 uint64_t
1228 spa_vdev_config_enter(spa_t *spa)
1230 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1232 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1234 return (spa_last_synced_txg(spa) + 1);
1238 * Used in combination with spa_vdev_config_enter() to allow the syncing
1239 * of multiple transactions without releasing the spa_namespace_lock.
1241 void
1242 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error,
1243 const char *tag)
1245 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1247 int config_changed = B_FALSE;
1249 ASSERT(txg > spa_last_synced_txg(spa));
1251 spa->spa_pending_vdev = NULL;
1254 * Reassess the DTLs.
1256 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE, B_FALSE);
1258 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1259 config_changed = B_TRUE;
1260 spa->spa_config_generation++;
1264 * Verify the metaslab classes.
1266 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1267 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1268 ASSERT(metaslab_class_validate(spa_embedded_log_class(spa)) == 0);
1269 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1270 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
1272 spa_config_exit(spa, SCL_ALL, spa);
1275 * Panic the system if the specified tag requires it. This
1276 * is useful for ensuring that configurations are updated
1277 * transactionally.
1279 if (zio_injection_enabled)
1280 zio_handle_panic_injection(spa, tag, 0);
1283 * Note: this txg_wait_synced() is important because it ensures
1284 * that there won't be more than one config change per txg.
1285 * This allows us to use the txg as the generation number.
1287 if (error == 0)
1288 txg_wait_synced(spa->spa_dsl_pool, txg);
1290 if (vd != NULL) {
1291 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1292 if (vd->vdev_ops->vdev_op_leaf) {
1293 mutex_enter(&vd->vdev_initialize_lock);
1294 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED,
1295 NULL);
1296 mutex_exit(&vd->vdev_initialize_lock);
1298 mutex_enter(&vd->vdev_trim_lock);
1299 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL);
1300 mutex_exit(&vd->vdev_trim_lock);
1304 * The vdev may be both a leaf and top-level device.
1306 vdev_autotrim_stop_wait(vd);
1308 spa_config_enter(spa, SCL_STATE_ALL, spa, RW_WRITER);
1309 vdev_free(vd);
1310 spa_config_exit(spa, SCL_STATE_ALL, spa);
1314 * If the config changed, update the config cache.
1316 if (config_changed)
1317 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE);
1321 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1322 * locking of spa_vdev_enter(), we also want make sure the transactions have
1323 * synced to disk, and then update the global configuration cache with the new
1324 * information.
1327 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1329 vdev_autotrim_restart(spa);
1330 vdev_rebuild_restart(spa);
1332 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1333 mutex_exit(&spa_namespace_lock);
1334 mutex_exit(&spa->spa_vdev_top_lock);
1336 return (error);
1340 * Lock the given spa_t for the purpose of changing vdev state.
1342 void
1343 spa_vdev_state_enter(spa_t *spa, int oplocks)
1345 int locks = SCL_STATE_ALL | oplocks;
1348 * Root pools may need to read of the underlying devfs filesystem
1349 * when opening up a vdev. Unfortunately if we're holding the
1350 * SCL_ZIO lock it will result in a deadlock when we try to issue
1351 * the read from the root filesystem. Instead we "prefetch"
1352 * the associated vnodes that we need prior to opening the
1353 * underlying devices and cache them so that we can prevent
1354 * any I/O when we are doing the actual open.
1356 if (spa_is_root(spa)) {
1357 int low = locks & ~(SCL_ZIO - 1);
1358 int high = locks & ~low;
1360 spa_config_enter(spa, high, spa, RW_WRITER);
1361 vdev_hold(spa->spa_root_vdev);
1362 spa_config_enter(spa, low, spa, RW_WRITER);
1363 } else {
1364 spa_config_enter(spa, locks, spa, RW_WRITER);
1366 spa->spa_vdev_locks = locks;
1370 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1372 boolean_t config_changed = B_FALSE;
1373 vdev_t *vdev_top;
1375 if (vd == NULL || vd == spa->spa_root_vdev) {
1376 vdev_top = spa->spa_root_vdev;
1377 } else {
1378 vdev_top = vd->vdev_top;
1381 if (vd != NULL || error == 0)
1382 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE, B_FALSE);
1384 if (vd != NULL) {
1385 if (vd != spa->spa_root_vdev)
1386 vdev_state_dirty(vdev_top);
1388 config_changed = B_TRUE;
1389 spa->spa_config_generation++;
1392 if (spa_is_root(spa))
1393 vdev_rele(spa->spa_root_vdev);
1395 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1396 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1399 * If anything changed, wait for it to sync. This ensures that,
1400 * from the system administrator's perspective, zpool(8) commands
1401 * are synchronous. This is important for things like zpool offline:
1402 * when the command completes, you expect no further I/O from ZFS.
1404 if (vd != NULL)
1405 txg_wait_synced(spa->spa_dsl_pool, 0);
1408 * If the config changed, update the config cache.
1410 if (config_changed) {
1411 mutex_enter(&spa_namespace_lock);
1412 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
1413 mutex_exit(&spa_namespace_lock);
1416 return (error);
1420 * ==========================================================================
1421 * Miscellaneous functions
1422 * ==========================================================================
1425 void
1426 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1428 if (!nvlist_exists(spa->spa_label_features, feature)) {
1429 fnvlist_add_boolean(spa->spa_label_features, feature);
1431 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1432 * dirty the vdev config because lock SCL_CONFIG is not held.
1433 * Thankfully, in this case we don't need to dirty the config
1434 * because it will be written out anyway when we finish
1435 * creating the pool.
1437 if (tx->tx_txg != TXG_INITIAL)
1438 vdev_config_dirty(spa->spa_root_vdev);
1442 void
1443 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1445 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1446 vdev_config_dirty(spa->spa_root_vdev);
1450 * Return the spa_t associated with given pool_guid, if it exists. If
1451 * device_guid is non-zero, determine whether the pool exists *and* contains
1452 * a device with the specified device_guid.
1454 spa_t *
1455 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1457 spa_t *spa;
1458 avl_tree_t *t = &spa_namespace_avl;
1460 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1462 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1463 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1464 continue;
1465 if (spa->spa_root_vdev == NULL)
1466 continue;
1467 if (spa_guid(spa) == pool_guid) {
1468 if (device_guid == 0)
1469 break;
1471 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1472 device_guid) != NULL)
1473 break;
1476 * Check any devices we may be in the process of adding.
1478 if (spa->spa_pending_vdev) {
1479 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1480 device_guid) != NULL)
1481 break;
1486 return (spa);
1490 * Determine whether a pool with the given pool_guid exists.
1492 boolean_t
1493 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1495 return (spa_by_guid(pool_guid, device_guid) != NULL);
1498 char *
1499 spa_strdup(const char *s)
1501 size_t len;
1502 char *new;
1504 len = strlen(s);
1505 new = kmem_alloc(len + 1, KM_SLEEP);
1506 memcpy(new, s, len + 1);
1508 return (new);
1511 void
1512 spa_strfree(char *s)
1514 kmem_free(s, strlen(s) + 1);
1517 uint64_t
1518 spa_generate_guid(spa_t *spa)
1520 uint64_t guid;
1522 if (spa != NULL) {
1523 do {
1524 (void) random_get_pseudo_bytes((void *)&guid,
1525 sizeof (guid));
1526 } while (guid == 0 || spa_guid_exists(spa_guid(spa), guid));
1527 } else {
1528 do {
1529 (void) random_get_pseudo_bytes((void *)&guid,
1530 sizeof (guid));
1531 } while (guid == 0 || spa_guid_exists(guid, 0));
1534 return (guid);
1537 void
1538 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1540 char type[256];
1541 const char *checksum = NULL;
1542 const char *compress = NULL;
1544 if (bp != NULL) {
1545 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1546 dmu_object_byteswap_t bswap =
1547 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1548 (void) snprintf(type, sizeof (type), "bswap %s %s",
1549 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1550 "metadata" : "data",
1551 dmu_ot_byteswap[bswap].ob_name);
1552 } else {
1553 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1554 sizeof (type));
1556 if (!BP_IS_EMBEDDED(bp)) {
1557 checksum =
1558 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1560 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1563 SNPRINTF_BLKPTR(kmem_scnprintf, ' ', buf, buflen, bp, type, checksum,
1564 compress);
1567 void
1568 spa_freeze(spa_t *spa)
1570 uint64_t freeze_txg = 0;
1572 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1573 if (spa->spa_freeze_txg == UINT64_MAX) {
1574 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1575 spa->spa_freeze_txg = freeze_txg;
1577 spa_config_exit(spa, SCL_ALL, FTAG);
1578 if (freeze_txg != 0)
1579 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1582 void
1583 zfs_panic_recover(const char *fmt, ...)
1585 va_list adx;
1587 va_start(adx, fmt);
1588 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1589 va_end(adx);
1593 * This is a stripped-down version of strtoull, suitable only for converting
1594 * lowercase hexadecimal numbers that don't overflow.
1596 uint64_t
1597 zfs_strtonum(const char *str, char **nptr)
1599 uint64_t val = 0;
1600 char c;
1601 int digit;
1603 while ((c = *str) != '\0') {
1604 if (c >= '0' && c <= '9')
1605 digit = c - '0';
1606 else if (c >= 'a' && c <= 'f')
1607 digit = 10 + c - 'a';
1608 else
1609 break;
1611 val *= 16;
1612 val += digit;
1614 str++;
1617 if (nptr)
1618 *nptr = (char *)str;
1620 return (val);
1623 void
1624 spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1627 * We bump the feature refcount for each special vdev added to the pool
1629 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1630 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1634 * ==========================================================================
1635 * Accessor functions
1636 * ==========================================================================
1639 boolean_t
1640 spa_shutting_down(spa_t *spa)
1642 return (spa->spa_async_suspended);
1645 dsl_pool_t *
1646 spa_get_dsl(spa_t *spa)
1648 return (spa->spa_dsl_pool);
1651 boolean_t
1652 spa_is_initializing(spa_t *spa)
1654 return (spa->spa_is_initializing);
1657 boolean_t
1658 spa_indirect_vdevs_loaded(spa_t *spa)
1660 return (spa->spa_indirect_vdevs_loaded);
1663 blkptr_t *
1664 spa_get_rootblkptr(spa_t *spa)
1666 return (&spa->spa_ubsync.ub_rootbp);
1669 void
1670 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1672 spa->spa_uberblock.ub_rootbp = *bp;
1675 void
1676 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1678 if (spa->spa_root == NULL)
1679 buf[0] = '\0';
1680 else
1681 (void) strlcpy(buf, spa->spa_root, buflen);
1684 uint32_t
1685 spa_sync_pass(spa_t *spa)
1687 return (spa->spa_sync_pass);
1690 char *
1691 spa_name(spa_t *spa)
1693 return (spa->spa_name);
1696 uint64_t
1697 spa_guid(spa_t *spa)
1699 dsl_pool_t *dp = spa_get_dsl(spa);
1700 uint64_t guid;
1703 * If we fail to parse the config during spa_load(), we can go through
1704 * the error path (which posts an ereport) and end up here with no root
1705 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1706 * this case.
1708 if (spa->spa_root_vdev == NULL)
1709 return (spa->spa_config_guid);
1711 guid = spa->spa_last_synced_guid != 0 ?
1712 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1715 * Return the most recently synced out guid unless we're
1716 * in syncing context.
1718 if (dp && dsl_pool_sync_context(dp))
1719 return (spa->spa_root_vdev->vdev_guid);
1720 else
1721 return (guid);
1724 uint64_t
1725 spa_load_guid(spa_t *spa)
1728 * This is a GUID that exists solely as a reference for the
1729 * purposes of the arc. It is generated at load time, and
1730 * is never written to persistent storage.
1732 return (spa->spa_load_guid);
1735 uint64_t
1736 spa_last_synced_txg(spa_t *spa)
1738 return (spa->spa_ubsync.ub_txg);
1741 uint64_t
1742 spa_first_txg(spa_t *spa)
1744 return (spa->spa_first_txg);
1747 uint64_t
1748 spa_syncing_txg(spa_t *spa)
1750 return (spa->spa_syncing_txg);
1754 * Return the last txg where data can be dirtied. The final txgs
1755 * will be used to just clear out any deferred frees that remain.
1757 uint64_t
1758 spa_final_dirty_txg(spa_t *spa)
1760 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1763 pool_state_t
1764 spa_state(spa_t *spa)
1766 return (spa->spa_state);
1769 spa_load_state_t
1770 spa_load_state(spa_t *spa)
1772 return (spa->spa_load_state);
1775 uint64_t
1776 spa_freeze_txg(spa_t *spa)
1778 return (spa->spa_freeze_txg);
1782 * Return the inflated asize for a logical write in bytes. This is used by the
1783 * DMU to calculate the space a logical write will require on disk.
1784 * If lsize is smaller than the largest physical block size allocatable on this
1785 * pool we use its value instead, since the write will end up using the whole
1786 * block anyway.
1788 uint64_t
1789 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1791 if (lsize == 0)
1792 return (0); /* No inflation needed */
1793 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation);
1797 * Return the amount of slop space in bytes. It is typically 1/32 of the pool
1798 * (3.2%), minus the embedded log space. On very small pools, it may be
1799 * slightly larger than this. On very large pools, it will be capped to
1800 * the value of spa_max_slop. The embedded log space is not included in
1801 * spa_dspace. By subtracting it, the usable space (per "zfs list") is a
1802 * constant 97% of the total space, regardless of metaslab size (assuming the
1803 * default spa_slop_shift=5 and a non-tiny pool).
1805 * See the comment above spa_slop_shift for more details.
1807 uint64_t
1808 spa_get_slop_space(spa_t *spa)
1810 uint64_t space = 0;
1811 uint64_t slop = 0;
1814 * Make sure spa_dedup_dspace has been set.
1816 if (spa->spa_dedup_dspace == ~0ULL)
1817 spa_update_dspace(spa);
1820 * spa_get_dspace() includes the space only logically "used" by
1821 * deduplicated data, so since it's not useful to reserve more
1822 * space with more deduplicated data, we subtract that out here.
1824 space = spa_get_dspace(spa) - spa->spa_dedup_dspace;
1825 slop = MIN(space >> spa_slop_shift, spa_max_slop);
1828 * Subtract the embedded log space, but no more than half the (3.2%)
1829 * unusable space. Note, the "no more than half" is only relevant if
1830 * zfs_embedded_slog_min_ms >> spa_slop_shift < 2, which is not true by
1831 * default.
1833 uint64_t embedded_log =
1834 metaslab_class_get_dspace(spa_embedded_log_class(spa));
1835 slop -= MIN(embedded_log, slop >> 1);
1838 * Slop space should be at least spa_min_slop, but no more than half
1839 * the entire pool.
1841 slop = MAX(slop, MIN(space >> 1, spa_min_slop));
1842 return (slop);
1845 uint64_t
1846 spa_get_dspace(spa_t *spa)
1848 return (spa->spa_dspace);
1851 uint64_t
1852 spa_get_checkpoint_space(spa_t *spa)
1854 return (spa->spa_checkpoint_info.sci_dspace);
1857 void
1858 spa_update_dspace(spa_t *spa)
1860 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1861 ddt_get_dedup_dspace(spa) + brt_get_dspace(spa);
1862 if (spa->spa_nonallocating_dspace > 0) {
1864 * Subtract the space provided by all non-allocating vdevs that
1865 * contribute to dspace. If a file is overwritten, its old
1866 * blocks are freed and new blocks are allocated. If there are
1867 * no snapshots of the file, the available space should remain
1868 * the same. The old blocks could be freed from the
1869 * non-allocating vdev, but the new blocks must be allocated on
1870 * other (allocating) vdevs. By reserving the entire size of
1871 * the non-allocating vdevs (including allocated space), we
1872 * ensure that there will be enough space on the allocating
1873 * vdevs for this file overwrite to succeed.
1875 * Note that the DMU/DSL doesn't actually know or care
1876 * how much space is allocated (it does its own tracking
1877 * of how much space has been logically used). So it
1878 * doesn't matter that the data we are moving may be
1879 * allocated twice (on the old device and the new device).
1881 ASSERT3U(spa->spa_dspace, >=, spa->spa_nonallocating_dspace);
1882 spa->spa_dspace -= spa->spa_nonallocating_dspace;
1887 * Return the failure mode that has been set to this pool. The default
1888 * behavior will be to block all I/Os when a complete failure occurs.
1890 uint64_t
1891 spa_get_failmode(spa_t *spa)
1893 return (spa->spa_failmode);
1896 boolean_t
1897 spa_suspended(spa_t *spa)
1899 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1902 uint64_t
1903 spa_version(spa_t *spa)
1905 return (spa->spa_ubsync.ub_version);
1908 boolean_t
1909 spa_deflate(spa_t *spa)
1911 return (spa->spa_deflate);
1914 metaslab_class_t *
1915 spa_normal_class(spa_t *spa)
1917 return (spa->spa_normal_class);
1920 metaslab_class_t *
1921 spa_log_class(spa_t *spa)
1923 return (spa->spa_log_class);
1926 metaslab_class_t *
1927 spa_embedded_log_class(spa_t *spa)
1929 return (spa->spa_embedded_log_class);
1932 metaslab_class_t *
1933 spa_special_class(spa_t *spa)
1935 return (spa->spa_special_class);
1938 metaslab_class_t *
1939 spa_dedup_class(spa_t *spa)
1941 return (spa->spa_dedup_class);
1945 * Locate an appropriate allocation class
1947 metaslab_class_t *
1948 spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1949 uint_t level, uint_t special_smallblk)
1952 * ZIL allocations determine their class in zio_alloc_zil().
1954 ASSERT(objtype != DMU_OT_INTENT_LOG);
1956 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0;
1958 if (DMU_OT_IS_DDT(objtype)) {
1959 if (spa->spa_dedup_class->mc_groups != 0)
1960 return (spa_dedup_class(spa));
1961 else if (has_special_class && zfs_ddt_data_is_special)
1962 return (spa_special_class(spa));
1963 else
1964 return (spa_normal_class(spa));
1967 /* Indirect blocks for user data can land in special if allowed */
1968 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) {
1969 if (has_special_class && zfs_user_indirect_is_special)
1970 return (spa_special_class(spa));
1971 else
1972 return (spa_normal_class(spa));
1975 if (DMU_OT_IS_METADATA(objtype) || level > 0) {
1976 if (has_special_class)
1977 return (spa_special_class(spa));
1978 else
1979 return (spa_normal_class(spa));
1983 * Allow small file blocks in special class in some cases (like
1984 * for the dRAID vdev feature). But always leave a reserve of
1985 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1987 if (DMU_OT_IS_FILE(objtype) &&
1988 has_special_class && size <= special_smallblk) {
1989 metaslab_class_t *special = spa_special_class(spa);
1990 uint64_t alloc = metaslab_class_get_alloc(special);
1991 uint64_t space = metaslab_class_get_space(special);
1992 uint64_t limit =
1993 (space * (100 - zfs_special_class_metadata_reserve_pct))
1994 / 100;
1996 if (alloc < limit)
1997 return (special);
2000 return (spa_normal_class(spa));
2003 void
2004 spa_evicting_os_register(spa_t *spa, objset_t *os)
2006 mutex_enter(&spa->spa_evicting_os_lock);
2007 list_insert_head(&spa->spa_evicting_os_list, os);
2008 mutex_exit(&spa->spa_evicting_os_lock);
2011 void
2012 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
2014 mutex_enter(&spa->spa_evicting_os_lock);
2015 list_remove(&spa->spa_evicting_os_list, os);
2016 cv_broadcast(&spa->spa_evicting_os_cv);
2017 mutex_exit(&spa->spa_evicting_os_lock);
2020 void
2021 spa_evicting_os_wait(spa_t *spa)
2023 mutex_enter(&spa->spa_evicting_os_lock);
2024 while (!list_is_empty(&spa->spa_evicting_os_list))
2025 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
2026 mutex_exit(&spa->spa_evicting_os_lock);
2028 dmu_buf_user_evict_wait();
2032 spa_max_replication(spa_t *spa)
2035 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
2036 * handle BPs with more than one DVA allocated. Set our max
2037 * replication level accordingly.
2039 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
2040 return (1);
2041 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
2045 spa_prev_software_version(spa_t *spa)
2047 return (spa->spa_prev_software_version);
2050 uint64_t
2051 spa_deadman_synctime(spa_t *spa)
2053 return (spa->spa_deadman_synctime);
2056 spa_autotrim_t
2057 spa_get_autotrim(spa_t *spa)
2059 return (spa->spa_autotrim);
2062 uint64_t
2063 spa_deadman_ziotime(spa_t *spa)
2065 return (spa->spa_deadman_ziotime);
2068 uint64_t
2069 spa_get_deadman_failmode(spa_t *spa)
2071 return (spa->spa_deadman_failmode);
2074 void
2075 spa_set_deadman_failmode(spa_t *spa, const char *failmode)
2077 if (strcmp(failmode, "wait") == 0)
2078 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2079 else if (strcmp(failmode, "continue") == 0)
2080 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE;
2081 else if (strcmp(failmode, "panic") == 0)
2082 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
2083 else
2084 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT;
2087 void
2088 spa_set_deadman_ziotime(hrtime_t ns)
2090 spa_t *spa = NULL;
2092 if (spa_mode_global != SPA_MODE_UNINIT) {
2093 mutex_enter(&spa_namespace_lock);
2094 while ((spa = spa_next(spa)) != NULL)
2095 spa->spa_deadman_ziotime = ns;
2096 mutex_exit(&spa_namespace_lock);
2100 void
2101 spa_set_deadman_synctime(hrtime_t ns)
2103 spa_t *spa = NULL;
2105 if (spa_mode_global != SPA_MODE_UNINIT) {
2106 mutex_enter(&spa_namespace_lock);
2107 while ((spa = spa_next(spa)) != NULL)
2108 spa->spa_deadman_synctime = ns;
2109 mutex_exit(&spa_namespace_lock);
2113 uint64_t
2114 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
2116 uint64_t asize = DVA_GET_ASIZE(dva);
2117 uint64_t dsize = asize;
2119 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2121 if (asize != 0 && spa->spa_deflate) {
2122 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
2123 if (vd != NULL)
2124 dsize = (asize >> SPA_MINBLOCKSHIFT) *
2125 vd->vdev_deflate_ratio;
2128 return (dsize);
2131 uint64_t
2132 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
2134 uint64_t dsize = 0;
2136 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2137 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2139 return (dsize);
2142 uint64_t
2143 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2145 uint64_t dsize = 0;
2147 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2149 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2150 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2152 spa_config_exit(spa, SCL_VDEV, FTAG);
2154 return (dsize);
2157 uint64_t
2158 spa_dirty_data(spa_t *spa)
2160 return (spa->spa_dsl_pool->dp_dirty_total);
2164 * ==========================================================================
2165 * SPA Import Progress Routines
2166 * ==========================================================================
2169 typedef struct spa_import_progress {
2170 uint64_t pool_guid; /* unique id for updates */
2171 char *pool_name;
2172 spa_load_state_t spa_load_state;
2173 uint64_t mmp_sec_remaining; /* MMP activity check */
2174 uint64_t spa_load_max_txg; /* rewind txg */
2175 procfs_list_node_t smh_node;
2176 } spa_import_progress_t;
2178 spa_history_list_t *spa_import_progress_list = NULL;
2180 static int
2181 spa_import_progress_show_header(struct seq_file *f)
2183 seq_printf(f, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2184 "load_state", "multihost_secs", "max_txg",
2185 "pool_name");
2186 return (0);
2189 static int
2190 spa_import_progress_show(struct seq_file *f, void *data)
2192 spa_import_progress_t *sip = (spa_import_progress_t *)data;
2194 seq_printf(f, "%-20llu %-14llu %-14llu %-12llu %s\n",
2195 (u_longlong_t)sip->pool_guid, (u_longlong_t)sip->spa_load_state,
2196 (u_longlong_t)sip->mmp_sec_remaining,
2197 (u_longlong_t)sip->spa_load_max_txg,
2198 (sip->pool_name ? sip->pool_name : "-"));
2200 return (0);
2203 /* Remove oldest elements from list until there are no more than 'size' left */
2204 static void
2205 spa_import_progress_truncate(spa_history_list_t *shl, unsigned int size)
2207 spa_import_progress_t *sip;
2208 while (shl->size > size) {
2209 sip = list_remove_head(&shl->procfs_list.pl_list);
2210 if (sip->pool_name)
2211 spa_strfree(sip->pool_name);
2212 kmem_free(sip, sizeof (spa_import_progress_t));
2213 shl->size--;
2216 IMPLY(size == 0, list_is_empty(&shl->procfs_list.pl_list));
2219 static void
2220 spa_import_progress_init(void)
2222 spa_import_progress_list = kmem_zalloc(sizeof (spa_history_list_t),
2223 KM_SLEEP);
2225 spa_import_progress_list->size = 0;
2227 spa_import_progress_list->procfs_list.pl_private =
2228 spa_import_progress_list;
2230 procfs_list_install("zfs",
2231 NULL,
2232 "import_progress",
2233 0644,
2234 &spa_import_progress_list->procfs_list,
2235 spa_import_progress_show,
2236 spa_import_progress_show_header,
2237 NULL,
2238 offsetof(spa_import_progress_t, smh_node));
2241 static void
2242 spa_import_progress_destroy(void)
2244 spa_history_list_t *shl = spa_import_progress_list;
2245 procfs_list_uninstall(&shl->procfs_list);
2246 spa_import_progress_truncate(shl, 0);
2247 procfs_list_destroy(&shl->procfs_list);
2248 kmem_free(shl, sizeof (spa_history_list_t));
2252 spa_import_progress_set_state(uint64_t pool_guid,
2253 spa_load_state_t load_state)
2255 spa_history_list_t *shl = spa_import_progress_list;
2256 spa_import_progress_t *sip;
2257 int error = ENOENT;
2259 if (shl->size == 0)
2260 return (0);
2262 mutex_enter(&shl->procfs_list.pl_lock);
2263 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2264 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2265 if (sip->pool_guid == pool_guid) {
2266 sip->spa_load_state = load_state;
2267 error = 0;
2268 break;
2271 mutex_exit(&shl->procfs_list.pl_lock);
2273 return (error);
2277 spa_import_progress_set_max_txg(uint64_t pool_guid, uint64_t load_max_txg)
2279 spa_history_list_t *shl = spa_import_progress_list;
2280 spa_import_progress_t *sip;
2281 int error = ENOENT;
2283 if (shl->size == 0)
2284 return (0);
2286 mutex_enter(&shl->procfs_list.pl_lock);
2287 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2288 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2289 if (sip->pool_guid == pool_guid) {
2290 sip->spa_load_max_txg = load_max_txg;
2291 error = 0;
2292 break;
2295 mutex_exit(&shl->procfs_list.pl_lock);
2297 return (error);
2301 spa_import_progress_set_mmp_check(uint64_t pool_guid,
2302 uint64_t mmp_sec_remaining)
2304 spa_history_list_t *shl = spa_import_progress_list;
2305 spa_import_progress_t *sip;
2306 int error = ENOENT;
2308 if (shl->size == 0)
2309 return (0);
2311 mutex_enter(&shl->procfs_list.pl_lock);
2312 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2313 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2314 if (sip->pool_guid == pool_guid) {
2315 sip->mmp_sec_remaining = mmp_sec_remaining;
2316 error = 0;
2317 break;
2320 mutex_exit(&shl->procfs_list.pl_lock);
2322 return (error);
2326 * A new import is in progress, add an entry.
2328 void
2329 spa_import_progress_add(spa_t *spa)
2331 spa_history_list_t *shl = spa_import_progress_list;
2332 spa_import_progress_t *sip;
2333 const char *poolname = NULL;
2335 sip = kmem_zalloc(sizeof (spa_import_progress_t), KM_SLEEP);
2336 sip->pool_guid = spa_guid(spa);
2338 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME,
2339 &poolname);
2340 if (poolname == NULL)
2341 poolname = spa_name(spa);
2342 sip->pool_name = spa_strdup(poolname);
2343 sip->spa_load_state = spa_load_state(spa);
2345 mutex_enter(&shl->procfs_list.pl_lock);
2346 procfs_list_add(&shl->procfs_list, sip);
2347 shl->size++;
2348 mutex_exit(&shl->procfs_list.pl_lock);
2351 void
2352 spa_import_progress_remove(uint64_t pool_guid)
2354 spa_history_list_t *shl = spa_import_progress_list;
2355 spa_import_progress_t *sip;
2357 mutex_enter(&shl->procfs_list.pl_lock);
2358 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL;
2359 sip = list_prev(&shl->procfs_list.pl_list, sip)) {
2360 if (sip->pool_guid == pool_guid) {
2361 if (sip->pool_name)
2362 spa_strfree(sip->pool_name);
2363 list_remove(&shl->procfs_list.pl_list, sip);
2364 shl->size--;
2365 kmem_free(sip, sizeof (spa_import_progress_t));
2366 break;
2369 mutex_exit(&shl->procfs_list.pl_lock);
2373 * ==========================================================================
2374 * Initialization and Termination
2375 * ==========================================================================
2378 static int
2379 spa_name_compare(const void *a1, const void *a2)
2381 const spa_t *s1 = a1;
2382 const spa_t *s2 = a2;
2383 int s;
2385 s = strcmp(s1->spa_name, s2->spa_name);
2387 return (TREE_ISIGN(s));
2390 void
2391 spa_boot_init(void)
2393 spa_config_load();
2396 void
2397 spa_init(spa_mode_t mode)
2399 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2400 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2401 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2402 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2404 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2405 offsetof(spa_t, spa_avl));
2407 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2408 offsetof(spa_aux_t, aux_avl));
2410 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2411 offsetof(spa_aux_t, aux_avl));
2413 spa_mode_global = mode;
2415 #ifndef _KERNEL
2416 if (spa_mode_global != SPA_MODE_READ && dprintf_find_string("watch")) {
2417 struct sigaction sa;
2419 sa.sa_flags = SA_SIGINFO;
2420 sigemptyset(&sa.sa_mask);
2421 sa.sa_sigaction = arc_buf_sigsegv;
2423 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
2424 perror("could not enable watchpoints: "
2425 "sigaction(SIGSEGV, ...) = ");
2426 } else {
2427 arc_watch = B_TRUE;
2430 #endif
2432 fm_init();
2433 zfs_refcount_init();
2434 unique_init();
2435 zfs_btree_init();
2436 metaslab_stat_init();
2437 brt_init();
2438 ddt_init();
2439 zio_init();
2440 dmu_init();
2441 zil_init();
2442 vdev_mirror_stat_init();
2443 vdev_raidz_math_init();
2444 vdev_file_init();
2445 zfs_prop_init();
2446 chksum_init();
2447 zpool_prop_init();
2448 zpool_feature_init();
2449 spa_config_load();
2450 vdev_prop_init();
2451 l2arc_start();
2452 scan_init();
2453 qat_init();
2454 spa_import_progress_init();
2457 void
2458 spa_fini(void)
2460 l2arc_stop();
2462 spa_evict_all();
2464 vdev_file_fini();
2465 vdev_mirror_stat_fini();
2466 vdev_raidz_math_fini();
2467 chksum_fini();
2468 zil_fini();
2469 dmu_fini();
2470 zio_fini();
2471 ddt_fini();
2472 brt_fini();
2473 metaslab_stat_fini();
2474 zfs_btree_fini();
2475 unique_fini();
2476 zfs_refcount_fini();
2477 fm_fini();
2478 scan_fini();
2479 qat_fini();
2480 spa_import_progress_destroy();
2482 avl_destroy(&spa_namespace_avl);
2483 avl_destroy(&spa_spare_avl);
2484 avl_destroy(&spa_l2cache_avl);
2486 cv_destroy(&spa_namespace_cv);
2487 mutex_destroy(&spa_namespace_lock);
2488 mutex_destroy(&spa_spare_lock);
2489 mutex_destroy(&spa_l2cache_lock);
2493 * Return whether this pool has a dedicated slog device. No locking needed.
2494 * It's not a problem if the wrong answer is returned as it's only for
2495 * performance and not correctness.
2497 boolean_t
2498 spa_has_slogs(spa_t *spa)
2500 return (spa->spa_log_class->mc_groups != 0);
2503 spa_log_state_t
2504 spa_get_log_state(spa_t *spa)
2506 return (spa->spa_log_state);
2509 void
2510 spa_set_log_state(spa_t *spa, spa_log_state_t state)
2512 spa->spa_log_state = state;
2515 boolean_t
2516 spa_is_root(spa_t *spa)
2518 return (spa->spa_is_root);
2521 boolean_t
2522 spa_writeable(spa_t *spa)
2524 return (!!(spa->spa_mode & SPA_MODE_WRITE) && spa->spa_trust_config);
2528 * Returns true if there is a pending sync task in any of the current
2529 * syncing txg, the current quiescing txg, or the current open txg.
2531 boolean_t
2532 spa_has_pending_synctask(spa_t *spa)
2534 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2535 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
2538 spa_mode_t
2539 spa_mode(spa_t *spa)
2541 return (spa->spa_mode);
2544 uint64_t
2545 spa_bootfs(spa_t *spa)
2547 return (spa->spa_bootfs);
2550 uint64_t
2551 spa_delegation(spa_t *spa)
2553 return (spa->spa_delegation);
2556 objset_t *
2557 spa_meta_objset(spa_t *spa)
2559 return (spa->spa_meta_objset);
2562 enum zio_checksum
2563 spa_dedup_checksum(spa_t *spa)
2565 return (spa->spa_dedup_checksum);
2569 * Reset pool scan stat per scan pass (or reboot).
2571 void
2572 spa_scan_stat_init(spa_t *spa)
2574 /* data not stored on disk */
2575 spa->spa_scan_pass_start = gethrestime_sec();
2576 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2577 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2578 else
2579 spa->spa_scan_pass_scrub_pause = 0;
2581 if (dsl_errorscrub_is_paused(spa->spa_dsl_pool->dp_scan))
2582 spa->spa_scan_pass_errorscrub_pause = spa->spa_scan_pass_start;
2583 else
2584 spa->spa_scan_pass_errorscrub_pause = 0;
2586 spa->spa_scan_pass_scrub_spent_paused = 0;
2587 spa->spa_scan_pass_exam = 0;
2588 spa->spa_scan_pass_issued = 0;
2590 // error scrub stats
2591 spa->spa_scan_pass_errorscrub_spent_paused = 0;
2595 * Get scan stats for zpool status reports
2598 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2600 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2602 if (scn == NULL || (scn->scn_phys.scn_func == POOL_SCAN_NONE &&
2603 scn->errorscrub_phys.dep_func == POOL_SCAN_NONE))
2604 return (SET_ERROR(ENOENT));
2606 memset(ps, 0, sizeof (pool_scan_stat_t));
2608 /* data stored on disk */
2609 ps->pss_func = scn->scn_phys.scn_func;
2610 ps->pss_state = scn->scn_phys.scn_state;
2611 ps->pss_start_time = scn->scn_phys.scn_start_time;
2612 ps->pss_end_time = scn->scn_phys.scn_end_time;
2613 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2614 ps->pss_examined = scn->scn_phys.scn_examined;
2615 ps->pss_skipped = scn->scn_phys.scn_skipped;
2616 ps->pss_processed = scn->scn_phys.scn_processed;
2617 ps->pss_errors = scn->scn_phys.scn_errors;
2619 /* data not stored on disk */
2620 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2621 ps->pss_pass_start = spa->spa_scan_pass_start;
2622 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2623 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
2624 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2625 ps->pss_issued =
2626 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
2628 /* error scrub data stored on disk */
2629 ps->pss_error_scrub_func = scn->errorscrub_phys.dep_func;
2630 ps->pss_error_scrub_state = scn->errorscrub_phys.dep_state;
2631 ps->pss_error_scrub_start = scn->errorscrub_phys.dep_start_time;
2632 ps->pss_error_scrub_end = scn->errorscrub_phys.dep_end_time;
2633 ps->pss_error_scrub_examined = scn->errorscrub_phys.dep_examined;
2634 ps->pss_error_scrub_to_be_examined =
2635 scn->errorscrub_phys.dep_to_examine;
2637 /* error scrub data not stored on disk */
2638 ps->pss_pass_error_scrub_pause = spa->spa_scan_pass_errorscrub_pause;
2640 return (0);
2644 spa_maxblocksize(spa_t *spa)
2646 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2647 return (SPA_MAXBLOCKSIZE);
2648 else
2649 return (SPA_OLD_MAXBLOCKSIZE);
2654 * Returns the txg that the last device removal completed. No indirect mappings
2655 * have been added since this txg.
2657 uint64_t
2658 spa_get_last_removal_txg(spa_t *spa)
2660 uint64_t vdevid;
2661 uint64_t ret = -1ULL;
2663 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2665 * sr_prev_indirect_vdev is only modified while holding all the
2666 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2667 * examining it.
2669 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2671 while (vdevid != -1ULL) {
2672 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2673 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2675 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2678 * If the removal did not remap any data, we don't care.
2680 if (vdev_indirect_births_count(vib) != 0) {
2681 ret = vdev_indirect_births_last_entry_txg(vib);
2682 break;
2685 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2687 spa_config_exit(spa, SCL_VDEV, FTAG);
2689 IMPLY(ret != -1ULL,
2690 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2692 return (ret);
2696 spa_maxdnodesize(spa_t *spa)
2698 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2699 return (DNODE_MAX_SIZE);
2700 else
2701 return (DNODE_MIN_SIZE);
2704 boolean_t
2705 spa_multihost(spa_t *spa)
2707 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2710 uint32_t
2711 spa_get_hostid(spa_t *spa)
2713 return (spa->spa_hostid);
2716 boolean_t
2717 spa_trust_config(spa_t *spa)
2719 return (spa->spa_trust_config);
2722 uint64_t
2723 spa_missing_tvds_allowed(spa_t *spa)
2725 return (spa->spa_missing_tvds_allowed);
2728 space_map_t *
2729 spa_syncing_log_sm(spa_t *spa)
2731 return (spa->spa_syncing_log_sm);
2734 void
2735 spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2737 spa->spa_missing_tvds = missing;
2741 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2743 const char *
2744 spa_state_to_name(spa_t *spa)
2746 ASSERT3P(spa, !=, NULL);
2749 * it is possible for the spa to exist, without root vdev
2750 * as the spa transitions during import/export
2752 vdev_t *rvd = spa->spa_root_vdev;
2753 if (rvd == NULL) {
2754 return ("TRANSITIONING");
2756 vdev_state_t state = rvd->vdev_state;
2757 vdev_aux_t aux = rvd->vdev_stat.vs_aux;
2759 if (spa_suspended(spa) &&
2760 (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE))
2761 return ("SUSPENDED");
2763 switch (state) {
2764 case VDEV_STATE_CLOSED:
2765 case VDEV_STATE_OFFLINE:
2766 return ("OFFLINE");
2767 case VDEV_STATE_REMOVED:
2768 return ("REMOVED");
2769 case VDEV_STATE_CANT_OPEN:
2770 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
2771 return ("FAULTED");
2772 else if (aux == VDEV_AUX_SPLIT_POOL)
2773 return ("SPLIT");
2774 else
2775 return ("UNAVAIL");
2776 case VDEV_STATE_FAULTED:
2777 return ("FAULTED");
2778 case VDEV_STATE_DEGRADED:
2779 return ("DEGRADED");
2780 case VDEV_STATE_HEALTHY:
2781 return ("ONLINE");
2782 default:
2783 break;
2786 return ("UNKNOWN");
2789 boolean_t
2790 spa_top_vdevs_spacemap_addressable(spa_t *spa)
2792 vdev_t *rvd = spa->spa_root_vdev;
2793 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2794 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2795 return (B_FALSE);
2797 return (B_TRUE);
2800 boolean_t
2801 spa_has_checkpoint(spa_t *spa)
2803 return (spa->spa_checkpoint_txg != 0);
2806 boolean_t
2807 spa_importing_readonly_checkpoint(spa_t *spa)
2809 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
2810 spa->spa_mode == SPA_MODE_READ);
2813 uint64_t
2814 spa_min_claim_txg(spa_t *spa)
2816 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2818 if (checkpoint_txg != 0)
2819 return (checkpoint_txg + 1);
2821 return (spa->spa_first_txg);
2825 * If there is a checkpoint, async destroys may consume more space from
2826 * the pool instead of freeing it. In an attempt to save the pool from
2827 * getting suspended when it is about to run out of space, we stop
2828 * processing async destroys.
2830 boolean_t
2831 spa_suspend_async_destroy(spa_t *spa)
2833 dsl_pool_t *dp = spa_get_dsl(spa);
2835 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2836 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2837 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2838 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2840 if (spa_has_checkpoint(spa) && avail == 0)
2841 return (B_TRUE);
2843 return (B_FALSE);
2846 #if defined(_KERNEL)
2849 param_set_deadman_failmode_common(const char *val)
2851 spa_t *spa = NULL;
2852 char *p;
2854 if (val == NULL)
2855 return (SET_ERROR(EINVAL));
2857 if ((p = strchr(val, '\n')) != NULL)
2858 *p = '\0';
2860 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 &&
2861 strcmp(val, "panic"))
2862 return (SET_ERROR(EINVAL));
2864 if (spa_mode_global != SPA_MODE_UNINIT) {
2865 mutex_enter(&spa_namespace_lock);
2866 while ((spa = spa_next(spa)) != NULL)
2867 spa_set_deadman_failmode(spa, val);
2868 mutex_exit(&spa_namespace_lock);
2871 return (0);
2873 #endif
2875 /* Namespace manipulation */
2876 EXPORT_SYMBOL(spa_lookup);
2877 EXPORT_SYMBOL(spa_add);
2878 EXPORT_SYMBOL(spa_remove);
2879 EXPORT_SYMBOL(spa_next);
2881 /* Refcount functions */
2882 EXPORT_SYMBOL(spa_open_ref);
2883 EXPORT_SYMBOL(spa_close);
2884 EXPORT_SYMBOL(spa_refcount_zero);
2886 /* Pool configuration lock */
2887 EXPORT_SYMBOL(spa_config_tryenter);
2888 EXPORT_SYMBOL(spa_config_enter);
2889 EXPORT_SYMBOL(spa_config_exit);
2890 EXPORT_SYMBOL(spa_config_held);
2892 /* Pool vdev add/remove lock */
2893 EXPORT_SYMBOL(spa_vdev_enter);
2894 EXPORT_SYMBOL(spa_vdev_exit);
2896 /* Pool vdev state change lock */
2897 EXPORT_SYMBOL(spa_vdev_state_enter);
2898 EXPORT_SYMBOL(spa_vdev_state_exit);
2900 /* Accessor functions */
2901 EXPORT_SYMBOL(spa_shutting_down);
2902 EXPORT_SYMBOL(spa_get_dsl);
2903 EXPORT_SYMBOL(spa_get_rootblkptr);
2904 EXPORT_SYMBOL(spa_set_rootblkptr);
2905 EXPORT_SYMBOL(spa_altroot);
2906 EXPORT_SYMBOL(spa_sync_pass);
2907 EXPORT_SYMBOL(spa_name);
2908 EXPORT_SYMBOL(spa_guid);
2909 EXPORT_SYMBOL(spa_last_synced_txg);
2910 EXPORT_SYMBOL(spa_first_txg);
2911 EXPORT_SYMBOL(spa_syncing_txg);
2912 EXPORT_SYMBOL(spa_version);
2913 EXPORT_SYMBOL(spa_state);
2914 EXPORT_SYMBOL(spa_load_state);
2915 EXPORT_SYMBOL(spa_freeze_txg);
2916 EXPORT_SYMBOL(spa_get_dspace);
2917 EXPORT_SYMBOL(spa_update_dspace);
2918 EXPORT_SYMBOL(spa_deflate);
2919 EXPORT_SYMBOL(spa_normal_class);
2920 EXPORT_SYMBOL(spa_log_class);
2921 EXPORT_SYMBOL(spa_special_class);
2922 EXPORT_SYMBOL(spa_preferred_class);
2923 EXPORT_SYMBOL(spa_max_replication);
2924 EXPORT_SYMBOL(spa_prev_software_version);
2925 EXPORT_SYMBOL(spa_get_failmode);
2926 EXPORT_SYMBOL(spa_suspended);
2927 EXPORT_SYMBOL(spa_bootfs);
2928 EXPORT_SYMBOL(spa_delegation);
2929 EXPORT_SYMBOL(spa_meta_objset);
2930 EXPORT_SYMBOL(spa_maxblocksize);
2931 EXPORT_SYMBOL(spa_maxdnodesize);
2933 /* Miscellaneous support routines */
2934 EXPORT_SYMBOL(spa_guid_exists);
2935 EXPORT_SYMBOL(spa_strdup);
2936 EXPORT_SYMBOL(spa_strfree);
2937 EXPORT_SYMBOL(spa_generate_guid);
2938 EXPORT_SYMBOL(snprintf_blkptr);
2939 EXPORT_SYMBOL(spa_freeze);
2940 EXPORT_SYMBOL(spa_upgrade);
2941 EXPORT_SYMBOL(spa_evict_all);
2942 EXPORT_SYMBOL(spa_lookup_by_guid);
2943 EXPORT_SYMBOL(spa_has_spare);
2944 EXPORT_SYMBOL(dva_get_dsize_sync);
2945 EXPORT_SYMBOL(bp_get_dsize_sync);
2946 EXPORT_SYMBOL(bp_get_dsize);
2947 EXPORT_SYMBOL(spa_has_slogs);
2948 EXPORT_SYMBOL(spa_is_root);
2949 EXPORT_SYMBOL(spa_writeable);
2950 EXPORT_SYMBOL(spa_mode);
2951 EXPORT_SYMBOL(spa_namespace_lock);
2952 EXPORT_SYMBOL(spa_trust_config);
2953 EXPORT_SYMBOL(spa_missing_tvds_allowed);
2954 EXPORT_SYMBOL(spa_set_missing_tvds);
2955 EXPORT_SYMBOL(spa_state_to_name);
2956 EXPORT_SYMBOL(spa_importing_readonly_checkpoint);
2957 EXPORT_SYMBOL(spa_min_claim_txg);
2958 EXPORT_SYMBOL(spa_suspend_async_destroy);
2959 EXPORT_SYMBOL(spa_has_checkpoint);
2960 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable);
2962 ZFS_MODULE_PARAM(zfs, zfs_, flags, UINT, ZMOD_RW,
2963 "Set additional debugging flags");
2965 ZFS_MODULE_PARAM(zfs, zfs_, recover, INT, ZMOD_RW,
2966 "Set to attempt to recover from fatal errors");
2968 ZFS_MODULE_PARAM(zfs, zfs_, free_leak_on_eio, INT, ZMOD_RW,
2969 "Set to ignore IO errors during free and permanently leak the space");
2971 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, checktime_ms, U64, ZMOD_RW,
2972 "Dead I/O check interval in milliseconds");
2974 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, enabled, INT, ZMOD_RW,
2975 "Enable deadman timer");
2977 ZFS_MODULE_PARAM(zfs_spa, spa_, asize_inflation, UINT, ZMOD_RW,
2978 "SPA size estimate multiplication factor");
2980 ZFS_MODULE_PARAM(zfs, zfs_, ddt_data_is_special, INT, ZMOD_RW,
2981 "Place DDT data into the special class");
2983 ZFS_MODULE_PARAM(zfs, zfs_, user_indirect_is_special, INT, ZMOD_RW,
2984 "Place user data indirect blocks into the special class");
2986 /* BEGIN CSTYLED */
2987 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, failmode,
2988 param_set_deadman_failmode, param_get_charp, ZMOD_RW,
2989 "Failmode for deadman timer");
2991 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, synctime_ms,
2992 param_set_deadman_synctime, spl_param_get_u64, ZMOD_RW,
2993 "Pool sync expiration time in milliseconds");
2995 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, ziotime_ms,
2996 param_set_deadman_ziotime, spl_param_get_u64, ZMOD_RW,
2997 "IO expiration time in milliseconds");
2999 ZFS_MODULE_PARAM(zfs, zfs_, special_class_metadata_reserve_pct, UINT, ZMOD_RW,
3000 "Small file blocks in special vdevs depends on this much "
3001 "free space available");
3002 /* END CSTYLED */
3004 ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
3005 param_get_uint, ZMOD_RW, "Reserved free space in pool");