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 http://www.opensolaris.org/os/licensing.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright (c) 2017 Datto Inc.
30 * Copyright (c) 2017, Intel Corporation.
33 #include <sys/zfs_context.h>
34 #include <sys/spa_impl.h>
35 #include <sys/spa_boot.h>
37 #include <sys/zio_checksum.h>
38 #include <sys/zio_compress.h>
40 #include <sys/dmu_tx.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/vdev_file.h>
45 #include <sys/vdev_initialize.h>
46 #include <sys/metaslab.h>
47 #include <sys/uberblock_impl.h>
50 #include <sys/unique.h>
51 #include <sys/dsl_pool.h>
52 #include <sys/dsl_dir.h>
53 #include <sys/dsl_prop.h>
54 #include <sys/dsl_scan.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/metaslab_impl.h>
60 #include <sys/zfeature.h>
62 #if defined(__FreeBSD__) && defined(_KERNEL)
63 #include <sys/types.h>
64 #include <sys/sysctl.h>
70 * There are four basic locks for managing spa_t structures:
72 * spa_namespace_lock (global mutex)
74 * This lock must be acquired to do any of the following:
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
81 * - add/remove/attach/detach devices
82 * - Held for the duration of create/destroy/import/export
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.
89 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
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
94 * some references in the DMU. Internally we check against spa_minref, but
95 * present the image of a zero/non-zero value to consumers.
97 * spa_config_lock[] (per-spa array of rwlocks)
99 * This protects the spa_t from config changes, and must be held in
100 * the following circumstances:
102 * - RW_READER to perform I/O to the spa
103 * - RW_WRITER to change the vdev config
105 * The locking order is fairly straightforward:
107 * spa_namespace_lock -> spa_refcount
109 * The namespace lock must be acquired to increase the refcount from 0
110 * or to check if it is zero.
112 * spa_refcount -> spa_config_lock[]
114 * There must be at least one valid reference on the spa_t to acquire
117 * spa_namespace_lock -> spa_config_lock[]
119 * The namespace lock must always be taken before the config lock.
122 * The spa_namespace_lock can be acquired directly and is globally visible.
124 * The namespace is manipulated using the following functions, all of which
125 * require the spa_namespace_lock to be held.
127 * spa_lookup() Lookup a spa_t by name.
129 * spa_add() Create a new spa_t in the namespace.
131 * spa_remove() Remove a spa_t from the namespace. This also
132 * frees up any memory associated with the spa_t.
134 * spa_next() Returns the next spa_t in the system, or the
135 * first if NULL is passed.
137 * spa_evict_all() Shutdown and remove all spa_t structures in
140 * spa_guid_exists() Determine whether a pool/device guid exists.
142 * The spa_refcount is manipulated using the following functions:
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.
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.
152 * spa_refcount_zero() Returns true if the refcount is currently
153 * zero. Must be called with spa_namespace_lock
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}().
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.
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).
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.
176 * The lock acquisition rules are as follows:
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.
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).
189 * Protects changes to metaslab groups and classes.
190 * Held as reader by metaslab_alloc() and metaslab_claim().
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.
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.
205 * Held as reader to prevent changes to the vdev tree during trivial
206 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
207 * other locks, and lower than all of them, to ensure that it's safe
208 * to acquire regardless of caller context.
210 * In addition, the following rules apply:
212 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
213 * The lock ordering is SCL_CONFIG > spa_props_lock.
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.
221 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
223 * spa_vdev_enter() Acquire the namespace lock and the config lock
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.
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[].
235 static avl_tree_t spa_namespace_avl;
236 kmutex_t spa_namespace_lock;
237 static kcondvar_t spa_namespace_cv;
238 static int spa_active_count;
239 int spa_max_replication_override = SPA_DVAS_PER_BP;
241 static kmutex_t spa_spare_lock;
242 static avl_tree_t spa_spare_avl;
243 static kmutex_t spa_l2cache_lock;
244 static avl_tree_t spa_l2cache_avl;
246 kmem_cache_t *spa_buffer_pool;
251 * Everything except dprintf, spa, and indirect_remap is on by default
254 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP);
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.
266 boolean_t zfs_recover = B_FALSE;
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.
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
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.
297 boolean_t zfs_free_leak_on_eio = B_FALSE;
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
302 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
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
307 uint64_t zfs_deadman_synctime_ms = 1000000ULL;
310 * Check time in milliseconds. This defines the frequency at which we check
313 uint64_t zfs_deadman_checktime_ms = 5000ULL;
316 * Default value of -1 for zfs_deadman_enabled is resolved in
319 int zfs_deadman_enabled = -1;
322 * The worst case is single-sector max-parity RAID-Z blocks, in which
323 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
324 * times the size; so just assume that. Add to this the fact that
325 * we can have up to 3 DVAs per bp, and one more factor of 2 because
326 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
328 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
330 int spa_asize_inflation = 24;
332 #if defined(__FreeBSD__) && defined(_KERNEL)
333 SYSCTL_DECL(_vfs_zfs);
334 SYSCTL_INT(_vfs_zfs, OID_AUTO, recover, CTLFLAG_RWTUN, &zfs_recover, 0,
335 "Try to recover from otherwise-fatal errors.");
338 sysctl_vfs_zfs_debug_flags(SYSCTL_HANDLER_ARGS)
343 err = sysctl_handle_int(oidp, &val, 0, req);
344 if (err != 0 || req->newptr == NULL)
348 * ZFS_DEBUG_MODIFY must be enabled prior to boot so all
349 * arc buffers in the system have the necessary additional
350 * checksum data. However, it is safe to disable at any
353 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
354 val &= ~ZFS_DEBUG_MODIFY;
360 SYSCTL_PROC(_vfs_zfs, OID_AUTO, debugflags,
361 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int),
362 sysctl_vfs_zfs_debug_flags, "IU", "Debug flags for ZFS testing.");
364 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_synctime_ms, CTLFLAG_RWTUN,
365 &zfs_deadman_synctime_ms, 0,
366 "Stalled ZFS I/O expiration time in milliseconds");
367 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_checktime_ms, CTLFLAG_RWTUN,
368 &zfs_deadman_checktime_ms, 0,
369 "Period of checks for stalled ZFS I/O in milliseconds");
370 SYSCTL_INT(_vfs_zfs, OID_AUTO, deadman_enabled, CTLFLAG_RWTUN,
371 &zfs_deadman_enabled, 0, "Kernel panic on stalled ZFS I/O");
372 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_asize_inflation, CTLFLAG_RWTUN,
373 &spa_asize_inflation, 0, "Worst case inflation factor for single sector writes");
382 * If we are not i386 or amd64 or in a virtual machine,
383 * disable ZFS deadman thread by default
385 if (zfs_deadman_enabled == -1) {
386 #if defined(__amd64__) || defined(__i386__)
387 zfs_deadman_enabled = (vm_guest == VM_GUEST_NO) ? 1 : 0;
389 zfs_deadman_enabled = 0;
394 #endif /* !illumos */
397 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
398 * the pool to be consumed. This ensures that we don't run the pool
399 * completely out of space, due to unaccounted changes (e.g. to the MOS).
400 * It also limits the worst-case time to allocate space. If we have
401 * less than this amount of free space, most ZPL operations (e.g. write,
402 * create) will return ENOSPC.
404 * Certain operations (e.g. file removal, most administrative actions) can
405 * use half the slop space. They will only return ENOSPC if less than half
406 * the slop space is free. Typically, once the pool has less than the slop
407 * space free, the user will use these operations to free up space in the pool.
408 * These are the operations that call dsl_pool_adjustedsize() with the netfree
409 * argument set to TRUE.
411 * Operations that are almost guaranteed to free up space in the absence of
412 * a pool checkpoint can use up to three quarters of the slop space
415 * A very restricted set of operations are always permitted, regardless of
416 * the amount of free space. These are the operations that call
417 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
418 * increase in the amount of space used, it is possible to run the pool
419 * completely out of space, causing it to be permanently read-only.
421 * Note that on very small pools, the slop space will be larger than
422 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
423 * but we never allow it to be more than half the pool size.
425 * See also the comments in zfs_space_check_t.
427 int spa_slop_shift = 5;
428 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_slop_shift, CTLFLAG_RWTUN,
430 "Shift value of reserved space (1/(2^spa_slop_shift)).");
431 uint64_t spa_min_slop = 128 * 1024 * 1024;
432 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, spa_min_slop, CTLFLAG_RWTUN,
434 "Minimal value of reserved space");
436 int spa_allocators = 4;
438 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_allocators, CTLFLAG_RWTUN,
440 "Number of allocators per metaslab group");
444 spa_load_failed(spa_t *spa, const char *fmt, ...)
450 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
453 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name,
454 spa->spa_trust_config ? "trusted" : "untrusted", buf);
459 spa_load_note(spa_t *spa, const char *fmt, ...)
465 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
468 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name,
469 spa->spa_trust_config ? "trusted" : "untrusted", buf);
473 * By default dedup and user data indirects land in the special class
475 int zfs_ddt_data_is_special = B_TRUE;
476 int zfs_user_indirect_is_special = B_TRUE;
479 * The percentage of special class final space reserved for metadata only.
480 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
481 * let metadata into the class.
483 int zfs_special_class_metadata_reserve_pct = 25;
485 #if defined(__FreeBSD__) && defined(_KERNEL)
486 SYSCTL_INT(_vfs_zfs, OID_AUTO, ddt_data_is_special, CTLFLAG_RWTUN,
487 &zfs_ddt_data_is_special, 0,
488 "Whether DDT data is eligible for the special class vdevs");
489 SYSCTL_INT(_vfs_zfs, OID_AUTO, user_indirect_is_special, CTLFLAG_RWTUN,
490 &zfs_user_indirect_is_special, 0,
491 "Whether indirect blocks are eligible for the special class vdevs");
492 SYSCTL_INT(_vfs_zfs, OID_AUTO, special_class_metadata_reserve_pct,
493 CTLFLAG_RWTUN, &zfs_special_class_metadata_reserve_pct, 0,
494 "Percentage of space in the special class reserved solely for metadata");
498 * ==========================================================================
500 * ==========================================================================
503 spa_config_lock_init(spa_t *spa)
505 for (int i = 0; i < SCL_LOCKS; i++) {
506 spa_config_lock_t *scl = &spa->spa_config_lock[i];
507 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
508 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
509 zfs_refcount_create_untracked(&scl->scl_count);
510 scl->scl_writer = NULL;
511 scl->scl_write_wanted = 0;
516 spa_config_lock_destroy(spa_t *spa)
518 for (int i = 0; i < SCL_LOCKS; i++) {
519 spa_config_lock_t *scl = &spa->spa_config_lock[i];
520 mutex_destroy(&scl->scl_lock);
521 cv_destroy(&scl->scl_cv);
522 zfs_refcount_destroy(&scl->scl_count);
523 ASSERT(scl->scl_writer == NULL);
524 ASSERT(scl->scl_write_wanted == 0);
529 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
531 for (int i = 0; i < SCL_LOCKS; i++) {
532 spa_config_lock_t *scl = &spa->spa_config_lock[i];
533 if (!(locks & (1 << i)))
535 mutex_enter(&scl->scl_lock);
536 if (rw == RW_READER) {
537 if (scl->scl_writer || scl->scl_write_wanted) {
538 mutex_exit(&scl->scl_lock);
539 spa_config_exit(spa, locks & ((1 << i) - 1),
544 ASSERT(scl->scl_writer != curthread);
545 if (!zfs_refcount_is_zero(&scl->scl_count)) {
546 mutex_exit(&scl->scl_lock);
547 spa_config_exit(spa, locks & ((1 << i) - 1),
551 scl->scl_writer = curthread;
553 (void) zfs_refcount_add(&scl->scl_count, tag);
554 mutex_exit(&scl->scl_lock);
560 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
564 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
566 for (int i = 0; i < SCL_LOCKS; i++) {
567 spa_config_lock_t *scl = &spa->spa_config_lock[i];
568 if (scl->scl_writer == curthread)
569 wlocks_held |= (1 << i);
570 if (!(locks & (1 << i)))
572 mutex_enter(&scl->scl_lock);
573 if (rw == RW_READER) {
574 while (scl->scl_writer || scl->scl_write_wanted) {
575 cv_wait(&scl->scl_cv, &scl->scl_lock);
578 ASSERT(scl->scl_writer != curthread);
579 while (!zfs_refcount_is_zero(&scl->scl_count)) {
580 scl->scl_write_wanted++;
581 cv_wait(&scl->scl_cv, &scl->scl_lock);
582 scl->scl_write_wanted--;
584 scl->scl_writer = curthread;
586 (void) zfs_refcount_add(&scl->scl_count, tag);
587 mutex_exit(&scl->scl_lock);
589 ASSERT3U(wlocks_held, <=, locks);
593 spa_config_exit(spa_t *spa, int locks, void *tag)
595 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
596 spa_config_lock_t *scl = &spa->spa_config_lock[i];
597 if (!(locks & (1 << i)))
599 mutex_enter(&scl->scl_lock);
600 ASSERT(!zfs_refcount_is_zero(&scl->scl_count));
601 if (zfs_refcount_remove(&scl->scl_count, tag) == 0) {
602 ASSERT(scl->scl_writer == NULL ||
603 scl->scl_writer == curthread);
604 scl->scl_writer = NULL; /* OK in either case */
605 cv_broadcast(&scl->scl_cv);
607 mutex_exit(&scl->scl_lock);
612 spa_config_held(spa_t *spa, int locks, krw_t rw)
616 for (int i = 0; i < SCL_LOCKS; i++) {
617 spa_config_lock_t *scl = &spa->spa_config_lock[i];
618 if (!(locks & (1 << i)))
620 if ((rw == RW_READER &&
621 !zfs_refcount_is_zero(&scl->scl_count)) ||
622 (rw == RW_WRITER && scl->scl_writer == curthread))
623 locks_held |= 1 << i;
630 * ==========================================================================
631 * SPA namespace functions
632 * ==========================================================================
636 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
637 * Returns NULL if no matching spa_t is found.
640 spa_lookup(const char *name)
642 static spa_t search; /* spa_t is large; don't allocate on stack */
647 ASSERT(MUTEX_HELD(&spa_namespace_lock));
649 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
652 * If it's a full dataset name, figure out the pool name and
655 cp = strpbrk(search.spa_name, "/@#");
659 spa = avl_find(&spa_namespace_avl, &search, &where);
665 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
666 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
667 * looking for potentially hung I/Os.
670 spa_deadman(void *arg, int pending)
675 * Disable the deadman timer if the pool is suspended.
677 if (spa_suspended(spa)) {
679 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
681 /* Nothing. just don't schedule any future callouts. */
686 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
687 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
688 ++spa->spa_deadman_calls);
689 if (zfs_deadman_enabled)
690 vdev_deadman(spa->spa_root_vdev);
693 callout_schedule(&spa->spa_deadman_cycid,
694 hz * zfs_deadman_checktime_ms / MILLISEC);
699 #if defined(__FreeBSD__) && defined(_KERNEL)
701 spa_deadman_timeout(void *arg)
705 taskqueue_enqueue(taskqueue_thread, &spa->spa_deadman_task);
710 * Create an uninitialized spa_t with the given name. Requires
711 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
712 * exist by calling spa_lookup() first.
715 spa_add(const char *name, nvlist_t *config, const char *altroot)
718 spa_config_dirent_t *dp;
724 ASSERT(MUTEX_HELD(&spa_namespace_lock));
726 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
728 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
729 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
730 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
731 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
732 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
733 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
734 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
735 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
736 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
737 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
738 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
739 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
741 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
742 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
743 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
744 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
745 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
747 for (int t = 0; t < TXG_SIZE; t++)
748 bplist_create(&spa->spa_free_bplist[t]);
750 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
751 spa->spa_state = POOL_STATE_UNINITIALIZED;
752 spa->spa_freeze_txg = UINT64_MAX;
753 spa->spa_final_txg = UINT64_MAX;
754 spa->spa_load_max_txg = UINT64_MAX;
756 spa->spa_proc_state = SPA_PROC_NONE;
757 spa->spa_trust_config = B_TRUE;
760 hdlr.cyh_func = spa_deadman;
762 hdlr.cyh_level = CY_LOW_LEVEL;
765 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
769 * This determines how often we need to check for hung I/Os after
770 * the cyclic has already fired. Since checking for hung I/Os is
771 * an expensive operation we don't want to check too frequently.
772 * Instead wait for 5 seconds before checking again.
774 when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
775 when.cyt_when = CY_INFINITY;
776 mutex_enter(&cpu_lock);
777 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
778 mutex_exit(&cpu_lock);
782 * callout(9) does not provide a way to initialize a callout with
783 * a function and an argument, so we use callout_reset() to schedule
784 * the callout in the very distant future. Even if that event ever
785 * fires, it should be okayas we won't have any active zio-s.
786 * But normally spa_sync() will reschedule the callout with a proper
788 * callout(9) does not allow the callback function to sleep but
789 * vdev_deadman() needs to acquire vq_lock and illumos mutexes are
790 * emulated using sx(9). For this reason spa_deadman_timeout()
791 * will schedule spa_deadman() as task on a taskqueue that allows
794 TASK_INIT(&spa->spa_deadman_task, 0, spa_deadman, spa);
795 callout_init(&spa->spa_deadman_cycid, 1);
796 callout_reset_sbt(&spa->spa_deadman_cycid, SBT_MAX, 0,
797 spa_deadman_timeout, spa, 0);
800 zfs_refcount_create(&spa->spa_refcount);
801 spa_config_lock_init(spa);
803 avl_add(&spa_namespace_avl, spa);
806 * Set the alternate root, if there is one.
809 spa->spa_root = spa_strdup(altroot);
813 spa->spa_alloc_count = spa_allocators;
814 spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
815 sizeof (kmutex_t), KM_SLEEP);
816 spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
817 sizeof (avl_tree_t), KM_SLEEP);
818 for (int i = 0; i < spa->spa_alloc_count; i++) {
819 mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
820 avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
821 sizeof (zio_t), offsetof(zio_t, io_alloc_node));
825 * Every pool starts with the default cachefile
827 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
828 offsetof(spa_config_dirent_t, scd_link));
830 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
831 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
832 list_insert_head(&spa->spa_config_list, dp);
834 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
837 if (config != NULL) {
840 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
842 VERIFY(nvlist_dup(features, &spa->spa_label_features,
846 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
849 if (spa->spa_label_features == NULL) {
850 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
854 spa->spa_min_ashift = INT_MAX;
855 spa->spa_max_ashift = 0;
858 * As a pool is being created, treat all features as disabled by
859 * setting SPA_FEATURE_DISABLED for all entries in the feature
862 for (int i = 0; i < SPA_FEATURES; i++) {
863 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
866 list_create(&spa->spa_leaf_list, sizeof (vdev_t),
867 offsetof(vdev_t, vdev_leaf_node));
873 * Removes a spa_t from the namespace, freeing up any memory used. Requires
874 * spa_namespace_lock. This is called only after the spa_t has been closed and
878 spa_remove(spa_t *spa)
880 spa_config_dirent_t *dp;
882 ASSERT(MUTEX_HELD(&spa_namespace_lock));
883 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
884 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0);
886 nvlist_free(spa->spa_config_splitting);
888 avl_remove(&spa_namespace_avl, spa);
889 cv_broadcast(&spa_namespace_cv);
892 spa_strfree(spa->spa_root);
896 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
897 list_remove(&spa->spa_config_list, dp);
898 if (dp->scd_path != NULL)
899 spa_strfree(dp->scd_path);
900 kmem_free(dp, sizeof (spa_config_dirent_t));
903 for (int i = 0; i < spa->spa_alloc_count; i++) {
904 avl_destroy(&spa->spa_alloc_trees[i]);
905 mutex_destroy(&spa->spa_alloc_locks[i]);
907 kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count *
909 kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count *
910 sizeof (avl_tree_t));
912 list_destroy(&spa->spa_config_list);
913 list_destroy(&spa->spa_leaf_list);
915 nvlist_free(spa->spa_label_features);
916 nvlist_free(spa->spa_load_info);
917 nvlist_free(spa->spa_feat_stats);
918 spa_config_set(spa, NULL);
921 mutex_enter(&cpu_lock);
922 if (spa->spa_deadman_cycid != CYCLIC_NONE)
923 cyclic_remove(spa->spa_deadman_cycid);
924 mutex_exit(&cpu_lock);
925 spa->spa_deadman_cycid = CYCLIC_NONE;
928 callout_drain(&spa->spa_deadman_cycid);
929 taskqueue_drain(taskqueue_thread, &spa->spa_deadman_task);
933 zfs_refcount_destroy(&spa->spa_refcount);
935 spa_config_lock_destroy(spa);
937 for (int t = 0; t < TXG_SIZE; t++)
938 bplist_destroy(&spa->spa_free_bplist[t]);
940 zio_checksum_templates_free(spa);
942 cv_destroy(&spa->spa_async_cv);
943 cv_destroy(&spa->spa_evicting_os_cv);
944 cv_destroy(&spa->spa_proc_cv);
945 cv_destroy(&spa->spa_scrub_io_cv);
946 cv_destroy(&spa->spa_suspend_cv);
948 mutex_destroy(&spa->spa_async_lock);
949 mutex_destroy(&spa->spa_errlist_lock);
950 mutex_destroy(&spa->spa_errlog_lock);
951 mutex_destroy(&spa->spa_evicting_os_lock);
952 mutex_destroy(&spa->spa_history_lock);
953 mutex_destroy(&spa->spa_proc_lock);
954 mutex_destroy(&spa->spa_props_lock);
955 mutex_destroy(&spa->spa_cksum_tmpls_lock);
956 mutex_destroy(&spa->spa_scrub_lock);
957 mutex_destroy(&spa->spa_suspend_lock);
958 mutex_destroy(&spa->spa_vdev_top_lock);
959 mutex_destroy(&spa->spa_feat_stats_lock);
961 kmem_free(spa, sizeof (spa_t));
965 * Given a pool, return the next pool in the namespace, or NULL if there is
966 * none. If 'prev' is NULL, return the first pool.
969 spa_next(spa_t *prev)
971 ASSERT(MUTEX_HELD(&spa_namespace_lock));
974 return (AVL_NEXT(&spa_namespace_avl, prev));
976 return (avl_first(&spa_namespace_avl));
980 * ==========================================================================
981 * SPA refcount functions
982 * ==========================================================================
986 * Add a reference to the given spa_t. Must have at least one reference, or
987 * have the namespace lock held.
990 spa_open_ref(spa_t *spa, void *tag)
992 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
993 MUTEX_HELD(&spa_namespace_lock));
994 (void) zfs_refcount_add(&spa->spa_refcount, tag);
998 * Remove a reference to the given spa_t. Must have at least one reference, or
999 * have the namespace lock held.
1002 spa_close(spa_t *spa, void *tag)
1004 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref ||
1005 MUTEX_HELD(&spa_namespace_lock));
1006 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
1010 * Remove a reference to the given spa_t held by a dsl dir that is
1011 * being asynchronously released. Async releases occur from a taskq
1012 * performing eviction of dsl datasets and dirs. The namespace lock
1013 * isn't held and the hold by the object being evicted may contribute to
1014 * spa_minref (e.g. dataset or directory released during pool export),
1015 * so the asserts in spa_close() do not apply.
1018 spa_async_close(spa_t *spa, void *tag)
1020 (void) zfs_refcount_remove(&spa->spa_refcount, tag);
1024 * Check to see if the spa refcount is zero. Must be called with
1025 * spa_namespace_lock held. We really compare against spa_minref, which is the
1026 * number of references acquired when opening a pool
1029 spa_refcount_zero(spa_t *spa)
1031 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1033 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref);
1037 * ==========================================================================
1038 * SPA spare and l2cache tracking
1039 * ==========================================================================
1043 * Hot spares and cache devices are tracked using the same code below,
1044 * for 'auxiliary' devices.
1047 typedef struct spa_aux {
1055 spa_aux_compare(const void *a, const void *b)
1057 const spa_aux_t *sa = (const spa_aux_t *)a;
1058 const spa_aux_t *sb = (const spa_aux_t *)b;
1060 return (AVL_CMP(sa->aux_guid, sb->aux_guid));
1064 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
1070 search.aux_guid = vd->vdev_guid;
1071 if ((aux = avl_find(avl, &search, &where)) != NULL) {
1074 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
1075 aux->aux_guid = vd->vdev_guid;
1077 avl_insert(avl, aux, where);
1082 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
1088 search.aux_guid = vd->vdev_guid;
1089 aux = avl_find(avl, &search, &where);
1091 ASSERT(aux != NULL);
1093 if (--aux->aux_count == 0) {
1094 avl_remove(avl, aux);
1095 kmem_free(aux, sizeof (spa_aux_t));
1096 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
1097 aux->aux_pool = 0ULL;
1102 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
1104 spa_aux_t search, *found;
1106 search.aux_guid = guid;
1107 found = avl_find(avl, &search, NULL);
1111 *pool = found->aux_pool;
1118 *refcnt = found->aux_count;
1123 return (found != NULL);
1127 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1129 spa_aux_t search, *found;
1132 search.aux_guid = vd->vdev_guid;
1133 found = avl_find(avl, &search, &where);
1134 ASSERT(found != NULL);
1135 ASSERT(found->aux_pool == 0ULL);
1137 found->aux_pool = spa_guid(vd->vdev_spa);
1141 * Spares are tracked globally due to the following constraints:
1143 * - A spare may be part of multiple pools.
1144 * - A spare may be added to a pool even if it's actively in use within
1146 * - A spare in use in any pool can only be the source of a replacement if
1147 * the target is a spare in the same pool.
1149 * We keep track of all spares on the system through the use of a reference
1150 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1151 * spare, then we bump the reference count in the AVL tree. In addition, we set
1152 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1153 * inactive). When a spare is made active (used to replace a device in the
1154 * pool), we also keep track of which pool its been made a part of.
1156 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1157 * called under the spa_namespace lock as part of vdev reconfiguration. The
1158 * separate spare lock exists for the status query path, which does not need to
1159 * be completely consistent with respect to other vdev configuration changes.
1163 spa_spare_compare(const void *a, const void *b)
1165 return (spa_aux_compare(a, b));
1169 spa_spare_add(vdev_t *vd)
1171 mutex_enter(&spa_spare_lock);
1172 ASSERT(!vd->vdev_isspare);
1173 spa_aux_add(vd, &spa_spare_avl);
1174 vd->vdev_isspare = B_TRUE;
1175 mutex_exit(&spa_spare_lock);
1179 spa_spare_remove(vdev_t *vd)
1181 mutex_enter(&spa_spare_lock);
1182 ASSERT(vd->vdev_isspare);
1183 spa_aux_remove(vd, &spa_spare_avl);
1184 vd->vdev_isspare = B_FALSE;
1185 mutex_exit(&spa_spare_lock);
1189 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
1193 mutex_enter(&spa_spare_lock);
1194 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
1195 mutex_exit(&spa_spare_lock);
1201 spa_spare_activate(vdev_t *vd)
1203 mutex_enter(&spa_spare_lock);
1204 ASSERT(vd->vdev_isspare);
1205 spa_aux_activate(vd, &spa_spare_avl);
1206 mutex_exit(&spa_spare_lock);
1210 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1211 * Cache devices currently only support one pool per cache device, and so
1212 * for these devices the aux reference count is currently unused beyond 1.
1216 spa_l2cache_compare(const void *a, const void *b)
1218 return (spa_aux_compare(a, b));
1222 spa_l2cache_add(vdev_t *vd)
1224 mutex_enter(&spa_l2cache_lock);
1225 ASSERT(!vd->vdev_isl2cache);
1226 spa_aux_add(vd, &spa_l2cache_avl);
1227 vd->vdev_isl2cache = B_TRUE;
1228 mutex_exit(&spa_l2cache_lock);
1232 spa_l2cache_remove(vdev_t *vd)
1234 mutex_enter(&spa_l2cache_lock);
1235 ASSERT(vd->vdev_isl2cache);
1236 spa_aux_remove(vd, &spa_l2cache_avl);
1237 vd->vdev_isl2cache = B_FALSE;
1238 mutex_exit(&spa_l2cache_lock);
1242 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1246 mutex_enter(&spa_l2cache_lock);
1247 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1248 mutex_exit(&spa_l2cache_lock);
1254 spa_l2cache_activate(vdev_t *vd)
1256 mutex_enter(&spa_l2cache_lock);
1257 ASSERT(vd->vdev_isl2cache);
1258 spa_aux_activate(vd, &spa_l2cache_avl);
1259 mutex_exit(&spa_l2cache_lock);
1263 * ==========================================================================
1265 * ==========================================================================
1269 * Lock the given spa_t for the purpose of adding or removing a vdev.
1270 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1271 * It returns the next transaction group for the spa_t.
1274 spa_vdev_enter(spa_t *spa)
1276 mutex_enter(&spa->spa_vdev_top_lock);
1277 mutex_enter(&spa_namespace_lock);
1278 return (spa_vdev_config_enter(spa));
1282 * Internal implementation for spa_vdev_enter(). Used when a vdev
1283 * operation requires multiple syncs (i.e. removing a device) while
1284 * keeping the spa_namespace_lock held.
1287 spa_vdev_config_enter(spa_t *spa)
1289 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1291 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1293 return (spa_last_synced_txg(spa) + 1);
1297 * Used in combination with spa_vdev_config_enter() to allow the syncing
1298 * of multiple transactions without releasing the spa_namespace_lock.
1301 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1303 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1305 int config_changed = B_FALSE;
1307 ASSERT(txg > spa_last_synced_txg(spa));
1309 spa->spa_pending_vdev = NULL;
1312 * Reassess the DTLs.
1314 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1316 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1317 config_changed = B_TRUE;
1318 spa->spa_config_generation++;
1322 * Verify the metaslab classes.
1324 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1325 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1326 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0);
1327 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0);
1329 spa_config_exit(spa, SCL_ALL, spa);
1332 * Panic the system if the specified tag requires it. This
1333 * is useful for ensuring that configurations are updated
1336 if (zio_injection_enabled)
1337 zio_handle_panic_injection(spa, tag, 0);
1340 * Note: this txg_wait_synced() is important because it ensures
1341 * that there won't be more than one config change per txg.
1342 * This allows us to use the txg as the generation number.
1345 txg_wait_synced(spa->spa_dsl_pool, txg);
1348 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1349 if (vd->vdev_ops->vdev_op_leaf) {
1350 mutex_enter(&vd->vdev_initialize_lock);
1351 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED);
1352 mutex_exit(&vd->vdev_initialize_lock);
1355 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1357 spa_config_exit(spa, SCL_ALL, spa);
1361 * If the config changed, update the config cache.
1364 spa_write_cachefile(spa, B_FALSE, B_TRUE);
1368 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1369 * locking of spa_vdev_enter(), we also want make sure the transactions have
1370 * synced to disk, and then update the global configuration cache with the new
1374 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1376 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1377 mutex_exit(&spa_namespace_lock);
1378 mutex_exit(&spa->spa_vdev_top_lock);
1384 * Lock the given spa_t for the purpose of changing vdev state.
1387 spa_vdev_state_enter(spa_t *spa, int oplocks)
1389 int locks = SCL_STATE_ALL | oplocks;
1392 * Root pools may need to read of the underlying devfs filesystem
1393 * when opening up a vdev. Unfortunately if we're holding the
1394 * SCL_ZIO lock it will result in a deadlock when we try to issue
1395 * the read from the root filesystem. Instead we "prefetch"
1396 * the associated vnodes that we need prior to opening the
1397 * underlying devices and cache them so that we can prevent
1398 * any I/O when we are doing the actual open.
1400 if (spa_is_root(spa)) {
1401 int low = locks & ~(SCL_ZIO - 1);
1402 int high = locks & ~low;
1404 spa_config_enter(spa, high, spa, RW_WRITER);
1405 vdev_hold(spa->spa_root_vdev);
1406 spa_config_enter(spa, low, spa, RW_WRITER);
1408 spa_config_enter(spa, locks, spa, RW_WRITER);
1410 spa->spa_vdev_locks = locks;
1414 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1416 boolean_t config_changed = B_FALSE;
1418 if (vd != NULL || error == 0)
1419 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1423 vdev_state_dirty(vd->vdev_top);
1424 config_changed = B_TRUE;
1425 spa->spa_config_generation++;
1428 if (spa_is_root(spa))
1429 vdev_rele(spa->spa_root_vdev);
1431 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1432 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1435 * If anything changed, wait for it to sync. This ensures that,
1436 * from the system administrator's perspective, zpool(1M) commands
1437 * are synchronous. This is important for things like zpool offline:
1438 * when the command completes, you expect no further I/O from ZFS.
1441 txg_wait_synced(spa->spa_dsl_pool, 0);
1444 * If the config changed, update the config cache.
1446 if (config_changed) {
1447 mutex_enter(&spa_namespace_lock);
1448 spa_write_cachefile(spa, B_FALSE, B_TRUE);
1449 mutex_exit(&spa_namespace_lock);
1456 * ==========================================================================
1457 * Miscellaneous functions
1458 * ==========================================================================
1462 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1464 if (!nvlist_exists(spa->spa_label_features, feature)) {
1465 fnvlist_add_boolean(spa->spa_label_features, feature);
1467 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1468 * dirty the vdev config because lock SCL_CONFIG is not held.
1469 * Thankfully, in this case we don't need to dirty the config
1470 * because it will be written out anyway when we finish
1471 * creating the pool.
1473 if (tx->tx_txg != TXG_INITIAL)
1474 vdev_config_dirty(spa->spa_root_vdev);
1479 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1481 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1482 vdev_config_dirty(spa->spa_root_vdev);
1486 * Return the spa_t associated with given pool_guid, if it exists. If
1487 * device_guid is non-zero, determine whether the pool exists *and* contains
1488 * a device with the specified device_guid.
1491 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1494 avl_tree_t *t = &spa_namespace_avl;
1496 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1498 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1499 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1501 if (spa->spa_root_vdev == NULL)
1503 if (spa_guid(spa) == pool_guid) {
1504 if (device_guid == 0)
1507 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1508 device_guid) != NULL)
1512 * Check any devices we may be in the process of adding.
1514 if (spa->spa_pending_vdev) {
1515 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1516 device_guid) != NULL)
1526 * Determine whether a pool with the given pool_guid exists.
1529 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1531 return (spa_by_guid(pool_guid, device_guid) != NULL);
1535 spa_strdup(const char *s)
1541 new = kmem_alloc(len + 1, KM_SLEEP);
1549 spa_strfree(char *s)
1551 kmem_free(s, strlen(s) + 1);
1555 spa_get_random(uint64_t range)
1564 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1570 spa_generate_guid(spa_t *spa)
1572 uint64_t guid = spa_get_random(-1ULL);
1575 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1576 guid = spa_get_random(-1ULL);
1578 while (guid == 0 || spa_guid_exists(guid, 0))
1579 guid = spa_get_random(-1ULL);
1586 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1589 char *checksum = NULL;
1590 char *compress = NULL;
1593 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1594 dmu_object_byteswap_t bswap =
1595 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1596 (void) snprintf(type, sizeof (type), "bswap %s %s",
1597 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1598 "metadata" : "data",
1599 dmu_ot_byteswap[bswap].ob_name);
1601 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1604 if (!BP_IS_EMBEDDED(bp)) {
1606 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1608 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1611 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1616 spa_freeze(spa_t *spa)
1618 uint64_t freeze_txg = 0;
1620 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1621 if (spa->spa_freeze_txg == UINT64_MAX) {
1622 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1623 spa->spa_freeze_txg = freeze_txg;
1625 spa_config_exit(spa, SCL_ALL, FTAG);
1626 if (freeze_txg != 0)
1627 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1631 zfs_panic_recover(const char *fmt, ...)
1636 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1641 * This is a stripped-down version of strtoull, suitable only for converting
1642 * lowercase hexadecimal numbers that don't overflow.
1645 zfs_strtonum(const char *str, char **nptr)
1651 while ((c = *str) != '\0') {
1652 if (c >= '0' && c <= '9')
1654 else if (c >= 'a' && c <= 'f')
1655 digit = 10 + c - 'a';
1666 *nptr = (char *)str;
1672 spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx)
1675 * We bump the feature refcount for each special vdev added to the pool
1677 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES));
1678 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx);
1682 * ==========================================================================
1683 * Accessor functions
1684 * ==========================================================================
1688 spa_shutting_down(spa_t *spa)
1690 return (spa->spa_async_suspended);
1694 spa_get_dsl(spa_t *spa)
1696 return (spa->spa_dsl_pool);
1700 spa_is_initializing(spa_t *spa)
1702 return (spa->spa_is_initializing);
1706 spa_indirect_vdevs_loaded(spa_t *spa)
1708 return (spa->spa_indirect_vdevs_loaded);
1712 spa_get_rootblkptr(spa_t *spa)
1714 return (&spa->spa_ubsync.ub_rootbp);
1718 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1720 spa->spa_uberblock.ub_rootbp = *bp;
1724 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1726 if (spa->spa_root == NULL)
1729 (void) strncpy(buf, spa->spa_root, buflen);
1733 spa_sync_pass(spa_t *spa)
1735 return (spa->spa_sync_pass);
1739 spa_name(spa_t *spa)
1741 return (spa->spa_name);
1745 spa_guid(spa_t *spa)
1747 dsl_pool_t *dp = spa_get_dsl(spa);
1751 * If we fail to parse the config during spa_load(), we can go through
1752 * the error path (which posts an ereport) and end up here with no root
1753 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1756 if (spa->spa_root_vdev == NULL)
1757 return (spa->spa_config_guid);
1759 guid = spa->spa_last_synced_guid != 0 ?
1760 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1763 * Return the most recently synced out guid unless we're
1764 * in syncing context.
1766 if (dp && dsl_pool_sync_context(dp))
1767 return (spa->spa_root_vdev->vdev_guid);
1773 spa_load_guid(spa_t *spa)
1776 * This is a GUID that exists solely as a reference for the
1777 * purposes of the arc. It is generated at load time, and
1778 * is never written to persistent storage.
1780 return (spa->spa_load_guid);
1784 spa_last_synced_txg(spa_t *spa)
1786 return (spa->spa_ubsync.ub_txg);
1790 spa_first_txg(spa_t *spa)
1792 return (spa->spa_first_txg);
1796 spa_syncing_txg(spa_t *spa)
1798 return (spa->spa_syncing_txg);
1802 * Return the last txg where data can be dirtied. The final txgs
1803 * will be used to just clear out any deferred frees that remain.
1806 spa_final_dirty_txg(spa_t *spa)
1808 return (spa->spa_final_txg - TXG_DEFER_SIZE);
1812 spa_state(spa_t *spa)
1814 return (spa->spa_state);
1818 spa_load_state(spa_t *spa)
1820 return (spa->spa_load_state);
1824 spa_freeze_txg(spa_t *spa)
1826 return (spa->spa_freeze_txg);
1831 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1833 return (lsize * spa_asize_inflation);
1837 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1838 * or at least 128MB, unless that would cause it to be more than half the
1841 * See the comment above spa_slop_shift for details.
1844 spa_get_slop_space(spa_t *spa)
1846 uint64_t space = spa_get_dspace(spa);
1847 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
1851 spa_get_dspace(spa_t *spa)
1853 return (spa->spa_dspace);
1857 spa_get_checkpoint_space(spa_t *spa)
1859 return (spa->spa_checkpoint_info.sci_dspace);
1863 spa_update_dspace(spa_t *spa)
1865 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1866 ddt_get_dedup_dspace(spa);
1867 if (spa->spa_vdev_removal != NULL) {
1869 * We can't allocate from the removing device, so
1870 * subtract its size. This prevents the DMU/DSL from
1871 * filling up the (now smaller) pool while we are in the
1872 * middle of removing the device.
1874 * Note that the DMU/DSL doesn't actually know or care
1875 * how much space is allocated (it does its own tracking
1876 * of how much space has been logically used). So it
1877 * doesn't matter that the data we are moving may be
1878 * allocated twice (on the old device and the new
1881 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1883 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id);
1884 spa->spa_dspace -= spa_deflate(spa) ?
1885 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1886 spa_config_exit(spa, SCL_VDEV, FTAG);
1891 * Return the failure mode that has been set to this pool. The default
1892 * behavior will be to block all I/Os when a complete failure occurs.
1895 spa_get_failmode(spa_t *spa)
1897 return (spa->spa_failmode);
1901 spa_suspended(spa_t *spa)
1903 return (spa->spa_suspended != ZIO_SUSPEND_NONE);
1907 spa_version(spa_t *spa)
1909 return (spa->spa_ubsync.ub_version);
1913 spa_deflate(spa_t *spa)
1915 return (spa->spa_deflate);
1919 spa_normal_class(spa_t *spa)
1921 return (spa->spa_normal_class);
1925 spa_log_class(spa_t *spa)
1927 return (spa->spa_log_class);
1931 spa_special_class(spa_t *spa)
1933 return (spa->spa_special_class);
1937 spa_dedup_class(spa_t *spa)
1939 return (spa->spa_dedup_class);
1943 * Locate an appropriate allocation class
1946 spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype,
1947 uint_t level, uint_t special_smallblk)
1949 if (DMU_OT_IS_ZIL(objtype)) {
1950 if (spa->spa_log_class->mc_groups != 0)
1951 return (spa_log_class(spa));
1953 return (spa_normal_class(spa));
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));
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));
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));
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);
1993 (space * (100 - zfs_special_class_metadata_reserve_pct))
2000 return (spa_normal_class(spa));
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);
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);
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)
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);
2051 spa_deadman_synctime(spa_t *spa)
2053 return (spa->spa_deadman_synctime);
2057 spa_proc(spa_t *spa)
2059 return (spa->spa_proc);
2063 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
2065 uint64_t asize = DVA_GET_ASIZE(dva);
2066 uint64_t dsize = asize;
2068 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2070 if (asize != 0 && spa->spa_deflate) {
2071 uint64_t vdev = DVA_GET_VDEV(dva);
2072 vdev_t *vd = vdev_lookup_top(spa, vdev);
2075 "dva_get_dsize_sync(): bad DVA %llu:%llu",
2076 (u_longlong_t)vdev, (u_longlong_t)asize);
2078 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
2085 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
2089 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2090 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2096 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
2100 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2102 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
2103 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
2105 spa_config_exit(spa, SCL_VDEV, FTAG);
2111 spa_dirty_data(spa_t *spa)
2113 return (spa->spa_dsl_pool->dp_dirty_total);
2117 * ==========================================================================
2118 * Initialization and Termination
2119 * ==========================================================================
2123 spa_name_compare(const void *a1, const void *a2)
2125 const spa_t *s1 = a1;
2126 const spa_t *s2 = a2;
2129 s = strcmp(s1->spa_name, s2->spa_name);
2131 return (AVL_ISIGN(s));
2137 return (spa_active_count);
2147 EVENTHANDLER_DEFINE(mountroot, spa_boot_init, NULL, 0);
2153 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
2154 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
2155 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
2156 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
2158 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
2159 offsetof(spa_t, spa_avl));
2161 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
2162 offsetof(spa_aux_t, aux_avl));
2164 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
2165 offsetof(spa_aux_t, aux_avl));
2167 spa_mode_global = mode;
2173 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
2174 arc_procfd = open("/proc/self/ctl", O_WRONLY);
2175 if (arc_procfd == -1) {
2176 perror("could not enable watchpoints: "
2177 "opening /proc/self/ctl failed: ");
2183 #endif /* illumos */
2185 zfs_refcount_init();
2188 metaslab_alloc_trace_init();
2193 vdev_cache_stat_init();
2197 zpool_feature_init();
2201 dsl_scan_global_init();
2206 #endif /* !illumos */
2217 vdev_cache_stat_fini();
2222 metaslab_alloc_trace_fini();
2225 zfs_refcount_fini();
2228 avl_destroy(&spa_namespace_avl);
2229 avl_destroy(&spa_spare_avl);
2230 avl_destroy(&spa_l2cache_avl);
2232 cv_destroy(&spa_namespace_cv);
2233 mutex_destroy(&spa_namespace_lock);
2234 mutex_destroy(&spa_spare_lock);
2235 mutex_destroy(&spa_l2cache_lock);
2239 * Return whether this pool has slogs. No locking needed.
2240 * It's not a problem if the wrong answer is returned as it's only for
2241 * performance and not correctness
2244 spa_has_slogs(spa_t *spa)
2246 return (spa->spa_log_class->mc_rotor != NULL);
2250 spa_get_log_state(spa_t *spa)
2252 return (spa->spa_log_state);
2256 spa_set_log_state(spa_t *spa, spa_log_state_t state)
2258 spa->spa_log_state = state;
2262 spa_is_root(spa_t *spa)
2264 return (spa->spa_is_root);
2268 spa_writeable(spa_t *spa)
2270 return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config);
2274 * Returns true if there is a pending sync task in any of the current
2275 * syncing txg, the current quiescing txg, or the current open txg.
2278 spa_has_pending_synctask(spa_t *spa)
2280 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) ||
2281 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks));
2285 spa_mode(spa_t *spa)
2287 return (spa->spa_mode);
2291 spa_bootfs(spa_t *spa)
2293 return (spa->spa_bootfs);
2297 spa_delegation(spa_t *spa)
2299 return (spa->spa_delegation);
2303 spa_meta_objset(spa_t *spa)
2305 return (spa->spa_meta_objset);
2309 spa_dedup_checksum(spa_t *spa)
2311 return (spa->spa_dedup_checksum);
2315 * Reset pool scan stat per scan pass (or reboot).
2318 spa_scan_stat_init(spa_t *spa)
2320 /* data not stored on disk */
2321 spa->spa_scan_pass_start = gethrestime_sec();
2322 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan))
2323 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start;
2325 spa->spa_scan_pass_scrub_pause = 0;
2326 spa->spa_scan_pass_scrub_spent_paused = 0;
2327 spa->spa_scan_pass_exam = 0;
2328 spa->spa_scan_pass_issued = 0;
2329 vdev_scan_stat_init(spa->spa_root_vdev);
2333 * Get scan stats for zpool status reports
2336 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2338 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2340 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2341 return (SET_ERROR(ENOENT));
2342 bzero(ps, sizeof (pool_scan_stat_t));
2344 /* data stored on disk */
2345 ps->pss_func = scn->scn_phys.scn_func;
2346 ps->pss_state = scn->scn_phys.scn_state;
2347 ps->pss_start_time = scn->scn_phys.scn_start_time;
2348 ps->pss_end_time = scn->scn_phys.scn_end_time;
2349 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2350 ps->pss_to_process = scn->scn_phys.scn_to_process;
2351 ps->pss_processed = scn->scn_phys.scn_processed;
2352 ps->pss_errors = scn->scn_phys.scn_errors;
2353 ps->pss_examined = scn->scn_phys.scn_examined;
2355 scn->scn_issued_before_pass + spa->spa_scan_pass_issued;
2356 /* data not stored on disk */
2357 ps->pss_pass_start = spa->spa_scan_pass_start;
2358 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2359 ps->pss_pass_issued = spa->spa_scan_pass_issued;
2360 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause;
2361 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused;
2367 spa_maxblocksize(spa_t *spa)
2369 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2370 return (SPA_MAXBLOCKSIZE);
2372 return (SPA_OLD_MAXBLOCKSIZE);
2376 spa_maxdnodesize(spa_t *spa)
2378 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2379 return (DNODE_MAX_SIZE);
2381 return (DNODE_MIN_SIZE);
2385 spa_multihost(spa_t *spa)
2387 return (spa->spa_multihost ? B_TRUE : B_FALSE);
2391 spa_get_hostid(void)
2393 unsigned long myhostid;
2396 myhostid = zone_get_hostid(NULL);
2399 * We're emulating the system's hostid in userland, so
2400 * we can't use zone_get_hostid().
2402 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2403 #endif /* _KERNEL */
2409 * Returns the txg that the last device removal completed. No indirect mappings
2410 * have been added since this txg.
2413 spa_get_last_removal_txg(spa_t *spa)
2416 uint64_t ret = -1ULL;
2418 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2420 * sr_prev_indirect_vdev is only modified while holding all the
2421 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2424 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev;
2426 while (vdevid != -1ULL) {
2427 vdev_t *vd = vdev_lookup_top(spa, vdevid);
2428 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
2430 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
2433 * If the removal did not remap any data, we don't care.
2435 if (vdev_indirect_births_count(vib) != 0) {
2436 ret = vdev_indirect_births_last_entry_txg(vib);
2440 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev;
2442 spa_config_exit(spa, SCL_VDEV, FTAG);
2445 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
2451 spa_trust_config(spa_t *spa)
2453 return (spa->spa_trust_config);
2457 spa_missing_tvds_allowed(spa_t *spa)
2459 return (spa->spa_missing_tvds_allowed);
2463 spa_set_missing_tvds(spa_t *spa, uint64_t missing)
2465 spa->spa_missing_tvds = missing;
2469 spa_top_vdevs_spacemap_addressable(spa_t *spa)
2471 vdev_t *rvd = spa->spa_root_vdev;
2472 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2473 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c]))
2480 spa_has_checkpoint(spa_t *spa)
2482 return (spa->spa_checkpoint_txg != 0);
2486 spa_importing_readonly_checkpoint(spa_t *spa)
2488 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) &&
2489 spa->spa_mode == FREAD);
2493 spa_min_claim_txg(spa_t *spa)
2495 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg;
2497 if (checkpoint_txg != 0)
2498 return (checkpoint_txg + 1);
2500 return (spa->spa_first_txg);
2504 * If there is a checkpoint, async destroys may consume more space from
2505 * the pool instead of freeing it. In an attempt to save the pool from
2506 * getting suspended when it is about to run out of space, we stop
2507 * processing async destroys.
2510 spa_suspend_async_destroy(spa_t *spa)
2512 dsl_pool_t *dp = spa_get_dsl(spa);
2514 uint64_t unreserved = dsl_pool_unreserved_space(dp,
2515 ZFS_SPACE_CHECK_EXTRA_RESERVED);
2516 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes;
2517 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0;
2519 if (spa_has_checkpoint(spa) && avail == 0)