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, 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/spa_impl.h>
35 #include <sys/zio_checksum.h>
36 #include <sys/zio_compress.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/vdev_initialize.h>
43 #include <sys/vdev_trim.h>
44 #include <sys/vdev_file.h>
45 #include <sys/vdev_raidz.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/fm/util.h>
55 #include <sys/dsl_scan.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/metaslab_impl.h>
60 #include <sys/kstat.h>
62 #include <sys/btree.h>
63 #include <sys/zfeature.h>
65 #include <sys/zstd/zstd.h>
70 * There are three 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 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
240 static kmutex_t spa_spare_lock
;
241 static avl_tree_t spa_spare_avl
;
242 static kmutex_t spa_l2cache_lock
;
243 static avl_tree_t spa_l2cache_avl
;
245 kmem_cache_t
*spa_buffer_pool
;
246 spa_mode_t spa_mode_global
= SPA_MODE_UNINIT
;
250 * Everything except dprintf, set_error, spa, and indirect_remap is on
251 * by default in debug builds.
253 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SET_ERROR
|
254 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 int 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 int 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 600 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
305 * in one of three behaviors controlled by zfs_deadman_failmode.
307 unsigned long zfs_deadman_synctime_ms
= 600000UL;
310 * This value controls the maximum amount of time zio_wait() will block for an
311 * outstanding IO. By default this is 300 seconds at which point the "hung"
312 * behavior will be applied as described for zfs_deadman_synctime_ms.
314 unsigned long zfs_deadman_ziotime_ms
= 300000UL;
317 * Check time in milliseconds. This defines the frequency at which we check
320 unsigned long zfs_deadman_checktime_ms
= 60000UL;
323 * By default the deadman is enabled.
325 int zfs_deadman_enabled
= 1;
328 * Controls the behavior of the deadman when it detects a "hung" I/O.
329 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
331 * wait - Wait for the "hung" I/O (default)
332 * continue - Attempt to recover from a "hung" I/O
333 * panic - Panic the system
335 char *zfs_deadman_failmode
= "wait";
338 * The worst case is single-sector max-parity RAID-Z blocks, in which
339 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
340 * times the size; so just assume that. Add to this the fact that
341 * we can have up to 3 DVAs per bp, and one more factor of 2 because
342 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
344 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
346 int spa_asize_inflation
= 24;
349 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
350 * the pool to be consumed. This ensures that we don't run the pool
351 * completely out of space, due to unaccounted changes (e.g. to the MOS).
352 * It also limits the worst-case time to allocate space. If we have less than
353 * this amount of free space, most ZPL operations (e.g. write, create) will
354 * return ENOSPC. The ZIL metaslabs (spa_embedded_log_class) are also part of
355 * this 3.2% of space which can't be consumed by normal writes; the slop space
356 * "proper" (spa_get_slop_space()) is decreased by the embedded log space.
358 * Certain operations (e.g. file removal, most administrative actions) can
359 * use half the slop space. They will only return ENOSPC if less than half
360 * the slop space is free. Typically, once the pool has less than the slop
361 * space free, the user will use these operations to free up space in the pool.
362 * These are the operations that call dsl_pool_adjustedsize() with the netfree
363 * argument set to TRUE.
365 * Operations that are almost guaranteed to free up space in the absence of
366 * a pool checkpoint can use up to three quarters of the slop space
369 * A very restricted set of operations are always permitted, regardless of
370 * the amount of free space. These are the operations that call
371 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
372 * increase in the amount of space used, it is possible to run the pool
373 * completely out of space, causing it to be permanently read-only.
375 * Note that on very small pools, the slop space will be larger than
376 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
377 * but we never allow it to be more than half the pool size.
379 * See also the comments in zfs_space_check_t.
381 int spa_slop_shift
= 5;
382 uint64_t spa_min_slop
= 128 * 1024 * 1024;
383 int spa_allocators
= 4;
388 spa_load_failed(spa_t
*spa
, const char *fmt
, ...)
394 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
397 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa
->spa_name
,
398 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
403 spa_load_note(spa_t
*spa
, const char *fmt
, ...)
409 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
412 zfs_dbgmsg("spa_load(%s, config %s): %s", spa
->spa_name
,
413 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
417 * By default dedup and user data indirects land in the special class
419 int zfs_ddt_data_is_special
= B_TRUE
;
420 int zfs_user_indirect_is_special
= B_TRUE
;
423 * The percentage of special class final space reserved for metadata only.
424 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
425 * let metadata into the class.
427 int zfs_special_class_metadata_reserve_pct
= 25;
430 * ==========================================================================
432 * ==========================================================================
435 spa_config_lock_init(spa_t
*spa
)
437 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
438 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
439 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
440 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
441 zfs_refcount_create_untracked(&scl
->scl_count
);
442 scl
->scl_writer
= NULL
;
443 scl
->scl_write_wanted
= 0;
448 spa_config_lock_destroy(spa_t
*spa
)
450 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
451 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
452 mutex_destroy(&scl
->scl_lock
);
453 cv_destroy(&scl
->scl_cv
);
454 zfs_refcount_destroy(&scl
->scl_count
);
455 ASSERT(scl
->scl_writer
== NULL
);
456 ASSERT(scl
->scl_write_wanted
== 0);
461 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
463 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
464 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
465 if (!(locks
& (1 << i
)))
467 mutex_enter(&scl
->scl_lock
);
468 if (rw
== RW_READER
) {
469 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
470 mutex_exit(&scl
->scl_lock
);
471 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
476 ASSERT(scl
->scl_writer
!= curthread
);
477 if (!zfs_refcount_is_zero(&scl
->scl_count
)) {
478 mutex_exit(&scl
->scl_lock
);
479 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
483 scl
->scl_writer
= curthread
;
485 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
486 mutex_exit(&scl
->scl_lock
);
492 spa_config_enter(spa_t
*spa
, int locks
, const void *tag
, krw_t rw
)
496 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
498 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
499 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
500 if (scl
->scl_writer
== curthread
)
501 wlocks_held
|= (1 << i
);
502 if (!(locks
& (1 << i
)))
504 mutex_enter(&scl
->scl_lock
);
505 if (rw
== RW_READER
) {
506 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
507 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
510 ASSERT(scl
->scl_writer
!= curthread
);
511 while (!zfs_refcount_is_zero(&scl
->scl_count
)) {
512 scl
->scl_write_wanted
++;
513 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
514 scl
->scl_write_wanted
--;
516 scl
->scl_writer
= curthread
;
518 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
519 mutex_exit(&scl
->scl_lock
);
521 ASSERT3U(wlocks_held
, <=, locks
);
525 spa_config_exit(spa_t
*spa
, int locks
, const void *tag
)
527 for (int i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
528 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
529 if (!(locks
& (1 << i
)))
531 mutex_enter(&scl
->scl_lock
);
532 ASSERT(!zfs_refcount_is_zero(&scl
->scl_count
));
533 if (zfs_refcount_remove(&scl
->scl_count
, tag
) == 0) {
534 ASSERT(scl
->scl_writer
== NULL
||
535 scl
->scl_writer
== curthread
);
536 scl
->scl_writer
= NULL
; /* OK in either case */
537 cv_broadcast(&scl
->scl_cv
);
539 mutex_exit(&scl
->scl_lock
);
544 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
548 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
549 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
550 if (!(locks
& (1 << i
)))
552 if ((rw
== RW_READER
&&
553 !zfs_refcount_is_zero(&scl
->scl_count
)) ||
554 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
555 locks_held
|= 1 << i
;
562 * ==========================================================================
563 * SPA namespace functions
564 * ==========================================================================
568 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
569 * Returns NULL if no matching spa_t is found.
572 spa_lookup(const char *name
)
574 static spa_t search
; /* spa_t is large; don't allocate on stack */
579 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
581 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
584 * If it's a full dataset name, figure out the pool name and
587 cp
= strpbrk(search
.spa_name
, "/@#");
591 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
597 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
598 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
599 * looking for potentially hung I/Os.
602 spa_deadman(void *arg
)
606 /* Disable the deadman if the pool is suspended. */
607 if (spa_suspended(spa
))
610 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
611 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
612 ++spa
->spa_deadman_calls
);
613 if (zfs_deadman_enabled
)
614 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
616 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
617 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
618 MSEC_TO_TICK(zfs_deadman_checktime_ms
));
622 spa_log_sm_sort_by_txg(const void *va
, const void *vb
)
624 const spa_log_sm_t
*a
= va
;
625 const spa_log_sm_t
*b
= vb
;
627 return (TREE_CMP(a
->sls_txg
, b
->sls_txg
));
631 * Create an uninitialized spa_t with the given name. Requires
632 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
633 * exist by calling spa_lookup() first.
636 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
639 spa_config_dirent_t
*dp
;
641 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
643 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
645 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
646 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
647 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
648 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
649 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
650 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
651 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
652 mutex_init(&spa
->spa_cksum_tmpls_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
653 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
654 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
655 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
656 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
657 mutex_init(&spa
->spa_flushed_ms_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
658 mutex_init(&spa
->spa_activities_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
660 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
661 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
662 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
663 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
664 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
665 cv_init(&spa
->spa_activities_cv
, NULL
, CV_DEFAULT
, NULL
);
666 cv_init(&spa
->spa_waiters_cv
, NULL
, CV_DEFAULT
, NULL
);
668 for (int t
= 0; t
< TXG_SIZE
; t
++)
669 bplist_create(&spa
->spa_free_bplist
[t
]);
671 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
672 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
673 spa
->spa_freeze_txg
= UINT64_MAX
;
674 spa
->spa_final_txg
= UINT64_MAX
;
675 spa
->spa_load_max_txg
= UINT64_MAX
;
677 spa
->spa_proc_state
= SPA_PROC_NONE
;
678 spa
->spa_trust_config
= B_TRUE
;
679 spa
->spa_hostid
= zone_get_hostid(NULL
);
681 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
682 spa
->spa_deadman_ziotime
= MSEC2NSEC(zfs_deadman_ziotime_ms
);
683 spa_set_deadman_failmode(spa
, zfs_deadman_failmode
);
685 zfs_refcount_create(&spa
->spa_refcount
);
686 spa_config_lock_init(spa
);
689 avl_add(&spa_namespace_avl
, spa
);
692 * Set the alternate root, if there is one.
695 spa
->spa_root
= spa_strdup(altroot
);
697 spa
->spa_alloc_count
= spa_allocators
;
698 spa
->spa_alloc_locks
= kmem_zalloc(spa
->spa_alloc_count
*
699 sizeof (kmutex_t
), KM_SLEEP
);
700 spa
->spa_alloc_trees
= kmem_zalloc(spa
->spa_alloc_count
*
701 sizeof (avl_tree_t
), KM_SLEEP
);
702 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
703 mutex_init(&spa
->spa_alloc_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
704 avl_create(&spa
->spa_alloc_trees
[i
], zio_bookmark_compare
,
705 sizeof (zio_t
), offsetof(zio_t
, io_alloc_node
));
707 avl_create(&spa
->spa_metaslabs_by_flushed
, metaslab_sort_by_flushed
,
708 sizeof (metaslab_t
), offsetof(metaslab_t
, ms_spa_txg_node
));
709 avl_create(&spa
->spa_sm_logs_by_txg
, spa_log_sm_sort_by_txg
,
710 sizeof (spa_log_sm_t
), offsetof(spa_log_sm_t
, sls_node
));
711 list_create(&spa
->spa_log_summary
, sizeof (log_summary_entry_t
),
712 offsetof(log_summary_entry_t
, lse_node
));
715 * Every pool starts with the default cachefile
717 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
718 offsetof(spa_config_dirent_t
, scd_link
));
720 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
721 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
722 list_insert_head(&spa
->spa_config_list
, dp
);
724 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
727 if (config
!= NULL
) {
730 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
732 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
736 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
739 if (spa
->spa_label_features
== NULL
) {
740 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
744 spa
->spa_min_ashift
= INT_MAX
;
745 spa
->spa_max_ashift
= 0;
746 spa
->spa_min_alloc
= INT_MAX
;
748 /* Reset cached value */
749 spa
->spa_dedup_dspace
= ~0ULL;
752 * As a pool is being created, treat all features as disabled by
753 * setting SPA_FEATURE_DISABLED for all entries in the feature
756 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
757 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
760 list_create(&spa
->spa_leaf_list
, sizeof (vdev_t
),
761 offsetof(vdev_t
, vdev_leaf_node
));
767 * Removes a spa_t from the namespace, freeing up any memory used. Requires
768 * spa_namespace_lock. This is called only after the spa_t has been closed and
772 spa_remove(spa_t
*spa
)
774 spa_config_dirent_t
*dp
;
776 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
777 ASSERT(spa_state(spa
) == POOL_STATE_UNINITIALIZED
);
778 ASSERT3U(zfs_refcount_count(&spa
->spa_refcount
), ==, 0);
779 ASSERT0(spa
->spa_waiters
);
781 nvlist_free(spa
->spa_config_splitting
);
783 avl_remove(&spa_namespace_avl
, spa
);
784 cv_broadcast(&spa_namespace_cv
);
787 spa_strfree(spa
->spa_root
);
789 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
790 list_remove(&spa
->spa_config_list
, dp
);
791 if (dp
->scd_path
!= NULL
)
792 spa_strfree(dp
->scd_path
);
793 kmem_free(dp
, sizeof (spa_config_dirent_t
));
796 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
797 avl_destroy(&spa
->spa_alloc_trees
[i
]);
798 mutex_destroy(&spa
->spa_alloc_locks
[i
]);
800 kmem_free(spa
->spa_alloc_locks
, spa
->spa_alloc_count
*
802 kmem_free(spa
->spa_alloc_trees
, spa
->spa_alloc_count
*
803 sizeof (avl_tree_t
));
805 avl_destroy(&spa
->spa_metaslabs_by_flushed
);
806 avl_destroy(&spa
->spa_sm_logs_by_txg
);
807 list_destroy(&spa
->spa_log_summary
);
808 list_destroy(&spa
->spa_config_list
);
809 list_destroy(&spa
->spa_leaf_list
);
811 nvlist_free(spa
->spa_label_features
);
812 nvlist_free(spa
->spa_load_info
);
813 nvlist_free(spa
->spa_feat_stats
);
814 spa_config_set(spa
, NULL
);
816 zfs_refcount_destroy(&spa
->spa_refcount
);
818 spa_stats_destroy(spa
);
819 spa_config_lock_destroy(spa
);
821 for (int t
= 0; t
< TXG_SIZE
; t
++)
822 bplist_destroy(&spa
->spa_free_bplist
[t
]);
824 zio_checksum_templates_free(spa
);
826 cv_destroy(&spa
->spa_async_cv
);
827 cv_destroy(&spa
->spa_evicting_os_cv
);
828 cv_destroy(&spa
->spa_proc_cv
);
829 cv_destroy(&spa
->spa_scrub_io_cv
);
830 cv_destroy(&spa
->spa_suspend_cv
);
831 cv_destroy(&spa
->spa_activities_cv
);
832 cv_destroy(&spa
->spa_waiters_cv
);
834 mutex_destroy(&spa
->spa_flushed_ms_lock
);
835 mutex_destroy(&spa
->spa_async_lock
);
836 mutex_destroy(&spa
->spa_errlist_lock
);
837 mutex_destroy(&spa
->spa_errlog_lock
);
838 mutex_destroy(&spa
->spa_evicting_os_lock
);
839 mutex_destroy(&spa
->spa_history_lock
);
840 mutex_destroy(&spa
->spa_proc_lock
);
841 mutex_destroy(&spa
->spa_props_lock
);
842 mutex_destroy(&spa
->spa_cksum_tmpls_lock
);
843 mutex_destroy(&spa
->spa_scrub_lock
);
844 mutex_destroy(&spa
->spa_suspend_lock
);
845 mutex_destroy(&spa
->spa_vdev_top_lock
);
846 mutex_destroy(&spa
->spa_feat_stats_lock
);
847 mutex_destroy(&spa
->spa_activities_lock
);
849 kmem_free(spa
, sizeof (spa_t
));
853 * Given a pool, return the next pool in the namespace, or NULL if there is
854 * none. If 'prev' is NULL, return the first pool.
857 spa_next(spa_t
*prev
)
859 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
862 return (AVL_NEXT(&spa_namespace_avl
, prev
));
864 return (avl_first(&spa_namespace_avl
));
868 * ==========================================================================
869 * SPA refcount functions
870 * ==========================================================================
874 * Add a reference to the given spa_t. Must have at least one reference, or
875 * have the namespace lock held.
878 spa_open_ref(spa_t
*spa
, void *tag
)
880 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
881 MUTEX_HELD(&spa_namespace_lock
));
882 (void) zfs_refcount_add(&spa
->spa_refcount
, tag
);
886 * Remove a reference to the given spa_t. Must have at least one reference, or
887 * have the namespace lock held.
890 spa_close(spa_t
*spa
, void *tag
)
892 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
893 MUTEX_HELD(&spa_namespace_lock
));
894 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
898 * Remove a reference to the given spa_t held by a dsl dir that is
899 * being asynchronously released. Async releases occur from a taskq
900 * performing eviction of dsl datasets and dirs. The namespace lock
901 * isn't held and the hold by the object being evicted may contribute to
902 * spa_minref (e.g. dataset or directory released during pool export),
903 * so the asserts in spa_close() do not apply.
906 spa_async_close(spa_t
*spa
, void *tag
)
908 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
912 * Check to see if the spa refcount is zero. Must be called with
913 * spa_namespace_lock held. We really compare against spa_minref, which is the
914 * number of references acquired when opening a pool
917 spa_refcount_zero(spa_t
*spa
)
919 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
921 return (zfs_refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
925 * ==========================================================================
926 * SPA spare and l2cache tracking
927 * ==========================================================================
931 * Hot spares and cache devices are tracked using the same code below,
932 * for 'auxiliary' devices.
935 typedef struct spa_aux
{
943 spa_aux_compare(const void *a
, const void *b
)
945 const spa_aux_t
*sa
= (const spa_aux_t
*)a
;
946 const spa_aux_t
*sb
= (const spa_aux_t
*)b
;
948 return (TREE_CMP(sa
->aux_guid
, sb
->aux_guid
));
952 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
958 search
.aux_guid
= vd
->vdev_guid
;
959 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
962 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
963 aux
->aux_guid
= vd
->vdev_guid
;
965 avl_insert(avl
, aux
, where
);
970 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
976 search
.aux_guid
= vd
->vdev_guid
;
977 aux
= avl_find(avl
, &search
, &where
);
981 if (--aux
->aux_count
== 0) {
982 avl_remove(avl
, aux
);
983 kmem_free(aux
, sizeof (spa_aux_t
));
984 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
985 aux
->aux_pool
= 0ULL;
990 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
992 spa_aux_t search
, *found
;
994 search
.aux_guid
= guid
;
995 found
= avl_find(avl
, &search
, NULL
);
999 *pool
= found
->aux_pool
;
1006 *refcnt
= found
->aux_count
;
1011 return (found
!= NULL
);
1015 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
1017 spa_aux_t search
, *found
;
1020 search
.aux_guid
= vd
->vdev_guid
;
1021 found
= avl_find(avl
, &search
, &where
);
1022 ASSERT(found
!= NULL
);
1023 ASSERT(found
->aux_pool
== 0ULL);
1025 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
1029 * Spares are tracked globally due to the following constraints:
1031 * - A spare may be part of multiple pools.
1032 * - A spare may be added to a pool even if it's actively in use within
1034 * - A spare in use in any pool can only be the source of a replacement if
1035 * the target is a spare in the same pool.
1037 * We keep track of all spares on the system through the use of a reference
1038 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1039 * spare, then we bump the reference count in the AVL tree. In addition, we set
1040 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1041 * inactive). When a spare is made active (used to replace a device in the
1042 * pool), we also keep track of which pool its been made a part of.
1044 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1045 * called under the spa_namespace lock as part of vdev reconfiguration. The
1046 * separate spare lock exists for the status query path, which does not need to
1047 * be completely consistent with respect to other vdev configuration changes.
1051 spa_spare_compare(const void *a
, const void *b
)
1053 return (spa_aux_compare(a
, b
));
1057 spa_spare_add(vdev_t
*vd
)
1059 mutex_enter(&spa_spare_lock
);
1060 ASSERT(!vd
->vdev_isspare
);
1061 spa_aux_add(vd
, &spa_spare_avl
);
1062 vd
->vdev_isspare
= B_TRUE
;
1063 mutex_exit(&spa_spare_lock
);
1067 spa_spare_remove(vdev_t
*vd
)
1069 mutex_enter(&spa_spare_lock
);
1070 ASSERT(vd
->vdev_isspare
);
1071 spa_aux_remove(vd
, &spa_spare_avl
);
1072 vd
->vdev_isspare
= B_FALSE
;
1073 mutex_exit(&spa_spare_lock
);
1077 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
1081 mutex_enter(&spa_spare_lock
);
1082 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
1083 mutex_exit(&spa_spare_lock
);
1089 spa_spare_activate(vdev_t
*vd
)
1091 mutex_enter(&spa_spare_lock
);
1092 ASSERT(vd
->vdev_isspare
);
1093 spa_aux_activate(vd
, &spa_spare_avl
);
1094 mutex_exit(&spa_spare_lock
);
1098 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1099 * Cache devices currently only support one pool per cache device, and so
1100 * for these devices the aux reference count is currently unused beyond 1.
1104 spa_l2cache_compare(const void *a
, const void *b
)
1106 return (spa_aux_compare(a
, b
));
1110 spa_l2cache_add(vdev_t
*vd
)
1112 mutex_enter(&spa_l2cache_lock
);
1113 ASSERT(!vd
->vdev_isl2cache
);
1114 spa_aux_add(vd
, &spa_l2cache_avl
);
1115 vd
->vdev_isl2cache
= B_TRUE
;
1116 mutex_exit(&spa_l2cache_lock
);
1120 spa_l2cache_remove(vdev_t
*vd
)
1122 mutex_enter(&spa_l2cache_lock
);
1123 ASSERT(vd
->vdev_isl2cache
);
1124 spa_aux_remove(vd
, &spa_l2cache_avl
);
1125 vd
->vdev_isl2cache
= B_FALSE
;
1126 mutex_exit(&spa_l2cache_lock
);
1130 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1134 mutex_enter(&spa_l2cache_lock
);
1135 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1136 mutex_exit(&spa_l2cache_lock
);
1142 spa_l2cache_activate(vdev_t
*vd
)
1144 mutex_enter(&spa_l2cache_lock
);
1145 ASSERT(vd
->vdev_isl2cache
);
1146 spa_aux_activate(vd
, &spa_l2cache_avl
);
1147 mutex_exit(&spa_l2cache_lock
);
1151 * ==========================================================================
1153 * ==========================================================================
1157 * Lock the given spa_t for the purpose of adding or removing a vdev.
1158 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1159 * It returns the next transaction group for the spa_t.
1162 spa_vdev_enter(spa_t
*spa
)
1164 mutex_enter(&spa
->spa_vdev_top_lock
);
1165 mutex_enter(&spa_namespace_lock
);
1167 vdev_autotrim_stop_all(spa
);
1169 return (spa_vdev_config_enter(spa
));
1173 * The same as spa_vdev_enter() above but additionally takes the guid of
1174 * the vdev being detached. When there is a rebuild in process it will be
1175 * suspended while the vdev tree is modified then resumed by spa_vdev_exit().
1176 * The rebuild is canceled if only a single child remains after the detach.
1179 spa_vdev_detach_enter(spa_t
*spa
, uint64_t guid
)
1181 mutex_enter(&spa
->spa_vdev_top_lock
);
1182 mutex_enter(&spa_namespace_lock
);
1184 vdev_autotrim_stop_all(spa
);
1187 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
1189 vdev_rebuild_stop_wait(vd
->vdev_top
);
1193 return (spa_vdev_config_enter(spa
));
1197 * Internal implementation for spa_vdev_enter(). Used when a vdev
1198 * operation requires multiple syncs (i.e. removing a device) while
1199 * keeping the spa_namespace_lock held.
1202 spa_vdev_config_enter(spa_t
*spa
)
1204 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1206 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1208 return (spa_last_synced_txg(spa
) + 1);
1212 * Used in combination with spa_vdev_config_enter() to allow the syncing
1213 * of multiple transactions without releasing the spa_namespace_lock.
1216 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1218 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1220 int config_changed
= B_FALSE
;
1222 ASSERT(txg
> spa_last_synced_txg(spa
));
1224 spa
->spa_pending_vdev
= NULL
;
1227 * Reassess the DTLs.
1229 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
, B_FALSE
);
1231 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1232 config_changed
= B_TRUE
;
1233 spa
->spa_config_generation
++;
1237 * Verify the metaslab classes.
1239 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1240 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1241 ASSERT(metaslab_class_validate(spa_embedded_log_class(spa
)) == 0);
1242 ASSERT(metaslab_class_validate(spa_special_class(spa
)) == 0);
1243 ASSERT(metaslab_class_validate(spa_dedup_class(spa
)) == 0);
1245 spa_config_exit(spa
, SCL_ALL
, spa
);
1248 * Panic the system if the specified tag requires it. This
1249 * is useful for ensuring that configurations are updated
1252 if (zio_injection_enabled
)
1253 zio_handle_panic_injection(spa
, tag
, 0);
1256 * Note: this txg_wait_synced() is important because it ensures
1257 * that there won't be more than one config change per txg.
1258 * This allows us to use the txg as the generation number.
1261 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1264 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1265 if (vd
->vdev_ops
->vdev_op_leaf
) {
1266 mutex_enter(&vd
->vdev_initialize_lock
);
1267 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
,
1269 mutex_exit(&vd
->vdev_initialize_lock
);
1271 mutex_enter(&vd
->vdev_trim_lock
);
1272 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, NULL
);
1273 mutex_exit(&vd
->vdev_trim_lock
);
1277 * The vdev may be both a leaf and top-level device.
1279 vdev_autotrim_stop_wait(vd
);
1281 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1283 spa_config_exit(spa
, SCL_ALL
, spa
);
1287 * If the config changed, update the config cache.
1290 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1294 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1295 * locking of spa_vdev_enter(), we also want make sure the transactions have
1296 * synced to disk, and then update the global configuration cache with the new
1300 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1302 vdev_autotrim_restart(spa
);
1303 vdev_rebuild_restart(spa
);
1305 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1306 mutex_exit(&spa_namespace_lock
);
1307 mutex_exit(&spa
->spa_vdev_top_lock
);
1313 * Lock the given spa_t for the purpose of changing vdev state.
1316 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1318 int locks
= SCL_STATE_ALL
| oplocks
;
1321 * Root pools may need to read of the underlying devfs filesystem
1322 * when opening up a vdev. Unfortunately if we're holding the
1323 * SCL_ZIO lock it will result in a deadlock when we try to issue
1324 * the read from the root filesystem. Instead we "prefetch"
1325 * the associated vnodes that we need prior to opening the
1326 * underlying devices and cache them so that we can prevent
1327 * any I/O when we are doing the actual open.
1329 if (spa_is_root(spa
)) {
1330 int low
= locks
& ~(SCL_ZIO
- 1);
1331 int high
= locks
& ~low
;
1333 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1334 vdev_hold(spa
->spa_root_vdev
);
1335 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1337 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1339 spa
->spa_vdev_locks
= locks
;
1343 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1345 boolean_t config_changed
= B_FALSE
;
1348 if (vd
== NULL
|| vd
== spa
->spa_root_vdev
) {
1349 vdev_top
= spa
->spa_root_vdev
;
1351 vdev_top
= vd
->vdev_top
;
1354 if (vd
!= NULL
|| error
== 0)
1355 vdev_dtl_reassess(vdev_top
, 0, 0, B_FALSE
, B_FALSE
);
1358 if (vd
!= spa
->spa_root_vdev
)
1359 vdev_state_dirty(vdev_top
);
1361 config_changed
= B_TRUE
;
1362 spa
->spa_config_generation
++;
1365 if (spa_is_root(spa
))
1366 vdev_rele(spa
->spa_root_vdev
);
1368 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1369 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1372 * If anything changed, wait for it to sync. This ensures that,
1373 * from the system administrator's perspective, zpool(8) commands
1374 * are synchronous. This is important for things like zpool offline:
1375 * when the command completes, you expect no further I/O from ZFS.
1378 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1381 * If the config changed, update the config cache.
1383 if (config_changed
) {
1384 mutex_enter(&spa_namespace_lock
);
1385 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1386 mutex_exit(&spa_namespace_lock
);
1393 * ==========================================================================
1394 * Miscellaneous functions
1395 * ==========================================================================
1399 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1401 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1402 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1404 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1405 * dirty the vdev config because lock SCL_CONFIG is not held.
1406 * Thankfully, in this case we don't need to dirty the config
1407 * because it will be written out anyway when we finish
1408 * creating the pool.
1410 if (tx
->tx_txg
!= TXG_INITIAL
)
1411 vdev_config_dirty(spa
->spa_root_vdev
);
1416 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1418 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1419 vdev_config_dirty(spa
->spa_root_vdev
);
1423 * Return the spa_t associated with given pool_guid, if it exists. If
1424 * device_guid is non-zero, determine whether the pool exists *and* contains
1425 * a device with the specified device_guid.
1428 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1431 avl_tree_t
*t
= &spa_namespace_avl
;
1433 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1435 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1436 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1438 if (spa
->spa_root_vdev
== NULL
)
1440 if (spa_guid(spa
) == pool_guid
) {
1441 if (device_guid
== 0)
1444 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1445 device_guid
) != NULL
)
1449 * Check any devices we may be in the process of adding.
1451 if (spa
->spa_pending_vdev
) {
1452 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1453 device_guid
) != NULL
)
1463 * Determine whether a pool with the given pool_guid exists.
1466 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1468 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1472 spa_strdup(const char *s
)
1478 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1486 spa_strfree(char *s
)
1488 kmem_free(s
, strlen(s
) + 1);
1492 spa_get_random(uint64_t range
)
1501 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1507 spa_generate_guid(spa_t
*spa
)
1509 uint64_t guid
= spa_get_random(-1ULL);
1512 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1513 guid
= spa_get_random(-1ULL);
1515 while (guid
== 0 || spa_guid_exists(guid
, 0))
1516 guid
= spa_get_random(-1ULL);
1523 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1526 char *checksum
= NULL
;
1527 char *compress
= NULL
;
1530 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1531 dmu_object_byteswap_t bswap
=
1532 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1533 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1534 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1535 "metadata" : "data",
1536 dmu_ot_byteswap
[bswap
].ob_name
);
1538 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1541 if (!BP_IS_EMBEDDED(bp
)) {
1543 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1545 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1548 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1553 spa_freeze(spa_t
*spa
)
1555 uint64_t freeze_txg
= 0;
1557 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1558 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1559 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1560 spa
->spa_freeze_txg
= freeze_txg
;
1562 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1563 if (freeze_txg
!= 0)
1564 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1568 zfs_panic_recover(const char *fmt
, ...)
1573 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1578 * This is a stripped-down version of strtoull, suitable only for converting
1579 * lowercase hexadecimal numbers that don't overflow.
1582 zfs_strtonum(const char *str
, char **nptr
)
1588 while ((c
= *str
) != '\0') {
1589 if (c
>= '0' && c
<= '9')
1591 else if (c
>= 'a' && c
<= 'f')
1592 digit
= 10 + c
- 'a';
1603 *nptr
= (char *)str
;
1609 spa_activate_allocation_classes(spa_t
*spa
, dmu_tx_t
*tx
)
1612 * We bump the feature refcount for each special vdev added to the pool
1614 ASSERT(spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
));
1615 spa_feature_incr(spa
, SPA_FEATURE_ALLOCATION_CLASSES
, tx
);
1619 * ==========================================================================
1620 * Accessor functions
1621 * ==========================================================================
1625 spa_shutting_down(spa_t
*spa
)
1627 return (spa
->spa_async_suspended
);
1631 spa_get_dsl(spa_t
*spa
)
1633 return (spa
->spa_dsl_pool
);
1637 spa_is_initializing(spa_t
*spa
)
1639 return (spa
->spa_is_initializing
);
1643 spa_indirect_vdevs_loaded(spa_t
*spa
)
1645 return (spa
->spa_indirect_vdevs_loaded
);
1649 spa_get_rootblkptr(spa_t
*spa
)
1651 return (&spa
->spa_ubsync
.ub_rootbp
);
1655 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1657 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1661 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1663 if (spa
->spa_root
== NULL
)
1666 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1670 spa_sync_pass(spa_t
*spa
)
1672 return (spa
->spa_sync_pass
);
1676 spa_name(spa_t
*spa
)
1678 return (spa
->spa_name
);
1682 spa_guid(spa_t
*spa
)
1684 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1688 * If we fail to parse the config during spa_load(), we can go through
1689 * the error path (which posts an ereport) and end up here with no root
1690 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1693 if (spa
->spa_root_vdev
== NULL
)
1694 return (spa
->spa_config_guid
);
1696 guid
= spa
->spa_last_synced_guid
!= 0 ?
1697 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1700 * Return the most recently synced out guid unless we're
1701 * in syncing context.
1703 if (dp
&& dsl_pool_sync_context(dp
))
1704 return (spa
->spa_root_vdev
->vdev_guid
);
1710 spa_load_guid(spa_t
*spa
)
1713 * This is a GUID that exists solely as a reference for the
1714 * purposes of the arc. It is generated at load time, and
1715 * is never written to persistent storage.
1717 return (spa
->spa_load_guid
);
1721 spa_last_synced_txg(spa_t
*spa
)
1723 return (spa
->spa_ubsync
.ub_txg
);
1727 spa_first_txg(spa_t
*spa
)
1729 return (spa
->spa_first_txg
);
1733 spa_syncing_txg(spa_t
*spa
)
1735 return (spa
->spa_syncing_txg
);
1739 * Return the last txg where data can be dirtied. The final txgs
1740 * will be used to just clear out any deferred frees that remain.
1743 spa_final_dirty_txg(spa_t
*spa
)
1745 return (spa
->spa_final_txg
- TXG_DEFER_SIZE
);
1749 spa_state(spa_t
*spa
)
1751 return (spa
->spa_state
);
1755 spa_load_state(spa_t
*spa
)
1757 return (spa
->spa_load_state
);
1761 spa_freeze_txg(spa_t
*spa
)
1763 return (spa
->spa_freeze_txg
);
1767 * Return the inflated asize for a logical write in bytes. This is used by the
1768 * DMU to calculate the space a logical write will require on disk.
1769 * If lsize is smaller than the largest physical block size allocatable on this
1770 * pool we use its value instead, since the write will end up using the whole
1774 spa_get_worst_case_asize(spa_t
*spa
, uint64_t lsize
)
1777 return (0); /* No inflation needed */
1778 return (MAX(lsize
, 1 << spa
->spa_max_ashift
) * spa_asize_inflation
);
1782 * Return the amount of slop space in bytes. It is typically 1/32 of the pool
1783 * (3.2%), minus the embedded log space. On very small pools, it may be
1784 * slightly larger than this. The embedded log space is not included in
1785 * spa_dspace. By subtracting it, the usable space (per "zfs list") is a
1786 * constant 97% of the total space, regardless of metaslab size (assuming the
1787 * default spa_slop_shift=5 and a non-tiny pool).
1789 * See the comment above spa_slop_shift for more details.
1792 spa_get_slop_space(spa_t
*spa
)
1794 uint64_t space
= spa_get_dspace(spa
);
1795 uint64_t slop
= space
>> spa_slop_shift
;
1798 * Subtract the embedded log space, but no more than half the (3.2%)
1799 * unusable space. Note, the "no more than half" is only relevant if
1800 * zfs_embedded_slog_min_ms >> spa_slop_shift < 2, which is not true by
1803 uint64_t embedded_log
=
1804 metaslab_class_get_dspace(spa_embedded_log_class(spa
));
1805 slop
-= MIN(embedded_log
, slop
>> 1);
1808 * Slop space should be at least spa_min_slop, but no more than half
1811 slop
= MAX(slop
, MIN(space
>> 1, spa_min_slop
));
1816 spa_get_dspace(spa_t
*spa
)
1818 return (spa
->spa_dspace
);
1822 spa_get_checkpoint_space(spa_t
*spa
)
1824 return (spa
->spa_checkpoint_info
.sci_dspace
);
1828 spa_update_dspace(spa_t
*spa
)
1830 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1831 ddt_get_dedup_dspace(spa
);
1832 if (spa
->spa_vdev_removal
!= NULL
) {
1834 * We can't allocate from the removing device, so subtract
1835 * its size if it was included in dspace (i.e. if this is a
1836 * normal-class vdev, not special/dedup). This prevents the
1837 * DMU/DSL from filling up the (now smaller) pool while we
1838 * are in the middle of removing the device.
1840 * Note that the DMU/DSL doesn't actually know or care
1841 * how much space is allocated (it does its own tracking
1842 * of how much space has been logically used). So it
1843 * doesn't matter that the data we are moving may be
1844 * allocated twice (on the old device and the new
1847 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1849 vdev_lookup_top(spa
, spa
->spa_vdev_removal
->svr_vdev_id
);
1850 if (vd
->vdev_mg
->mg_class
== spa_normal_class(spa
)) {
1851 spa
->spa_dspace
-= spa_deflate(spa
) ?
1852 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1854 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1859 * Return the failure mode that has been set to this pool. The default
1860 * behavior will be to block all I/Os when a complete failure occurs.
1863 spa_get_failmode(spa_t
*spa
)
1865 return (spa
->spa_failmode
);
1869 spa_suspended(spa_t
*spa
)
1871 return (spa
->spa_suspended
!= ZIO_SUSPEND_NONE
);
1875 spa_version(spa_t
*spa
)
1877 return (spa
->spa_ubsync
.ub_version
);
1881 spa_deflate(spa_t
*spa
)
1883 return (spa
->spa_deflate
);
1887 spa_normal_class(spa_t
*spa
)
1889 return (spa
->spa_normal_class
);
1893 spa_log_class(spa_t
*spa
)
1895 return (spa
->spa_log_class
);
1899 spa_embedded_log_class(spa_t
*spa
)
1901 return (spa
->spa_embedded_log_class
);
1905 spa_special_class(spa_t
*spa
)
1907 return (spa
->spa_special_class
);
1911 spa_dedup_class(spa_t
*spa
)
1913 return (spa
->spa_dedup_class
);
1917 * Locate an appropriate allocation class
1920 spa_preferred_class(spa_t
*spa
, uint64_t size
, dmu_object_type_t objtype
,
1921 uint_t level
, uint_t special_smallblk
)
1924 * ZIL allocations determine their class in zio_alloc_zil().
1926 ASSERT(objtype
!= DMU_OT_INTENT_LOG
);
1928 boolean_t has_special_class
= spa
->spa_special_class
->mc_groups
!= 0;
1930 if (DMU_OT_IS_DDT(objtype
)) {
1931 if (spa
->spa_dedup_class
->mc_groups
!= 0)
1932 return (spa_dedup_class(spa
));
1933 else if (has_special_class
&& zfs_ddt_data_is_special
)
1934 return (spa_special_class(spa
));
1936 return (spa_normal_class(spa
));
1939 /* Indirect blocks for user data can land in special if allowed */
1940 if (level
> 0 && (DMU_OT_IS_FILE(objtype
) || objtype
== DMU_OT_ZVOL
)) {
1941 if (has_special_class
&& zfs_user_indirect_is_special
)
1942 return (spa_special_class(spa
));
1944 return (spa_normal_class(spa
));
1947 if (DMU_OT_IS_METADATA(objtype
) || level
> 0) {
1948 if (has_special_class
)
1949 return (spa_special_class(spa
));
1951 return (spa_normal_class(spa
));
1955 * Allow small file blocks in special class in some cases (like
1956 * for the dRAID vdev feature). But always leave a reserve of
1957 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1959 if (DMU_OT_IS_FILE(objtype
) &&
1960 has_special_class
&& size
<= special_smallblk
) {
1961 metaslab_class_t
*special
= spa_special_class(spa
);
1962 uint64_t alloc
= metaslab_class_get_alloc(special
);
1963 uint64_t space
= metaslab_class_get_space(special
);
1965 (space
* (100 - zfs_special_class_metadata_reserve_pct
))
1972 return (spa_normal_class(spa
));
1976 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1978 mutex_enter(&spa
->spa_evicting_os_lock
);
1979 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1980 mutex_exit(&spa
->spa_evicting_os_lock
);
1984 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1986 mutex_enter(&spa
->spa_evicting_os_lock
);
1987 list_remove(&spa
->spa_evicting_os_list
, os
);
1988 cv_broadcast(&spa
->spa_evicting_os_cv
);
1989 mutex_exit(&spa
->spa_evicting_os_lock
);
1993 spa_evicting_os_wait(spa_t
*spa
)
1995 mutex_enter(&spa
->spa_evicting_os_lock
);
1996 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1997 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1998 mutex_exit(&spa
->spa_evicting_os_lock
);
2000 dmu_buf_user_evict_wait();
2004 spa_max_replication(spa_t
*spa
)
2007 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
2008 * handle BPs with more than one DVA allocated. Set our max
2009 * replication level accordingly.
2011 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
2013 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
2017 spa_prev_software_version(spa_t
*spa
)
2019 return (spa
->spa_prev_software_version
);
2023 spa_deadman_synctime(spa_t
*spa
)
2025 return (spa
->spa_deadman_synctime
);
2029 spa_get_autotrim(spa_t
*spa
)
2031 return (spa
->spa_autotrim
);
2035 spa_deadman_ziotime(spa_t
*spa
)
2037 return (spa
->spa_deadman_ziotime
);
2041 spa_get_deadman_failmode(spa_t
*spa
)
2043 return (spa
->spa_deadman_failmode
);
2047 spa_set_deadman_failmode(spa_t
*spa
, const char *failmode
)
2049 if (strcmp(failmode
, "wait") == 0)
2050 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
2051 else if (strcmp(failmode
, "continue") == 0)
2052 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
2053 else if (strcmp(failmode
, "panic") == 0)
2054 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
2056 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
2060 spa_set_deadman_ziotime(hrtime_t ns
)
2064 if (spa_mode_global
!= SPA_MODE_UNINIT
) {
2065 mutex_enter(&spa_namespace_lock
);
2066 while ((spa
= spa_next(spa
)) != NULL
)
2067 spa
->spa_deadman_ziotime
= ns
;
2068 mutex_exit(&spa_namespace_lock
);
2073 spa_set_deadman_synctime(hrtime_t ns
)
2077 if (spa_mode_global
!= SPA_MODE_UNINIT
) {
2078 mutex_enter(&spa_namespace_lock
);
2079 while ((spa
= spa_next(spa
)) != NULL
)
2080 spa
->spa_deadman_synctime
= ns
;
2081 mutex_exit(&spa_namespace_lock
);
2086 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
2088 uint64_t asize
= DVA_GET_ASIZE(dva
);
2089 uint64_t dsize
= asize
;
2091 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2093 if (asize
!= 0 && spa
->spa_deflate
) {
2094 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
2096 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
2097 vd
->vdev_deflate_ratio
;
2104 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
2108 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2109 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2115 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
2119 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2121 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2122 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2124 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2130 spa_dirty_data(spa_t
*spa
)
2132 return (spa
->spa_dsl_pool
->dp_dirty_total
);
2136 * ==========================================================================
2137 * SPA Import Progress Routines
2138 * ==========================================================================
2141 typedef struct spa_import_progress
{
2142 uint64_t pool_guid
; /* unique id for updates */
2144 spa_load_state_t spa_load_state
;
2145 uint64_t mmp_sec_remaining
; /* MMP activity check */
2146 uint64_t spa_load_max_txg
; /* rewind txg */
2147 procfs_list_node_t smh_node
;
2148 } spa_import_progress_t
;
2150 spa_history_list_t
*spa_import_progress_list
= NULL
;
2153 spa_import_progress_show_header(struct seq_file
*f
)
2155 seq_printf(f
, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2156 "load_state", "multihost_secs", "max_txg",
2162 spa_import_progress_show(struct seq_file
*f
, void *data
)
2164 spa_import_progress_t
*sip
= (spa_import_progress_t
*)data
;
2166 seq_printf(f
, "%-20llu %-14llu %-14llu %-12llu %s\n",
2167 (u_longlong_t
)sip
->pool_guid
, (u_longlong_t
)sip
->spa_load_state
,
2168 (u_longlong_t
)sip
->mmp_sec_remaining
,
2169 (u_longlong_t
)sip
->spa_load_max_txg
,
2170 (sip
->pool_name
? sip
->pool_name
: "-"));
2175 /* Remove oldest elements from list until there are no more than 'size' left */
2177 spa_import_progress_truncate(spa_history_list_t
*shl
, unsigned int size
)
2179 spa_import_progress_t
*sip
;
2180 while (shl
->size
> size
) {
2181 sip
= list_remove_head(&shl
->procfs_list
.pl_list
);
2183 spa_strfree(sip
->pool_name
);
2184 kmem_free(sip
, sizeof (spa_import_progress_t
));
2188 IMPLY(size
== 0, list_is_empty(&shl
->procfs_list
.pl_list
));
2192 spa_import_progress_init(void)
2194 spa_import_progress_list
= kmem_zalloc(sizeof (spa_history_list_t
),
2197 spa_import_progress_list
->size
= 0;
2199 spa_import_progress_list
->procfs_list
.pl_private
=
2200 spa_import_progress_list
;
2202 procfs_list_install("zfs",
2206 &spa_import_progress_list
->procfs_list
,
2207 spa_import_progress_show
,
2208 spa_import_progress_show_header
,
2210 offsetof(spa_import_progress_t
, smh_node
));
2214 spa_import_progress_destroy(void)
2216 spa_history_list_t
*shl
= spa_import_progress_list
;
2217 procfs_list_uninstall(&shl
->procfs_list
);
2218 spa_import_progress_truncate(shl
, 0);
2219 procfs_list_destroy(&shl
->procfs_list
);
2220 kmem_free(shl
, sizeof (spa_history_list_t
));
2224 spa_import_progress_set_state(uint64_t pool_guid
,
2225 spa_load_state_t load_state
)
2227 spa_history_list_t
*shl
= spa_import_progress_list
;
2228 spa_import_progress_t
*sip
;
2234 mutex_enter(&shl
->procfs_list
.pl_lock
);
2235 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2236 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2237 if (sip
->pool_guid
== pool_guid
) {
2238 sip
->spa_load_state
= load_state
;
2243 mutex_exit(&shl
->procfs_list
.pl_lock
);
2249 spa_import_progress_set_max_txg(uint64_t pool_guid
, uint64_t load_max_txg
)
2251 spa_history_list_t
*shl
= spa_import_progress_list
;
2252 spa_import_progress_t
*sip
;
2258 mutex_enter(&shl
->procfs_list
.pl_lock
);
2259 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2260 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2261 if (sip
->pool_guid
== pool_guid
) {
2262 sip
->spa_load_max_txg
= load_max_txg
;
2267 mutex_exit(&shl
->procfs_list
.pl_lock
);
2273 spa_import_progress_set_mmp_check(uint64_t pool_guid
,
2274 uint64_t mmp_sec_remaining
)
2276 spa_history_list_t
*shl
= spa_import_progress_list
;
2277 spa_import_progress_t
*sip
;
2283 mutex_enter(&shl
->procfs_list
.pl_lock
);
2284 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2285 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2286 if (sip
->pool_guid
== pool_guid
) {
2287 sip
->mmp_sec_remaining
= mmp_sec_remaining
;
2292 mutex_exit(&shl
->procfs_list
.pl_lock
);
2298 * A new import is in progress, add an entry.
2301 spa_import_progress_add(spa_t
*spa
)
2303 spa_history_list_t
*shl
= spa_import_progress_list
;
2304 spa_import_progress_t
*sip
;
2305 char *poolname
= NULL
;
2307 sip
= kmem_zalloc(sizeof (spa_import_progress_t
), KM_SLEEP
);
2308 sip
->pool_guid
= spa_guid(spa
);
2310 (void) nvlist_lookup_string(spa
->spa_config
, ZPOOL_CONFIG_POOL_NAME
,
2312 if (poolname
== NULL
)
2313 poolname
= spa_name(spa
);
2314 sip
->pool_name
= spa_strdup(poolname
);
2315 sip
->spa_load_state
= spa_load_state(spa
);
2317 mutex_enter(&shl
->procfs_list
.pl_lock
);
2318 procfs_list_add(&shl
->procfs_list
, sip
);
2320 mutex_exit(&shl
->procfs_list
.pl_lock
);
2324 spa_import_progress_remove(uint64_t pool_guid
)
2326 spa_history_list_t
*shl
= spa_import_progress_list
;
2327 spa_import_progress_t
*sip
;
2329 mutex_enter(&shl
->procfs_list
.pl_lock
);
2330 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2331 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2332 if (sip
->pool_guid
== pool_guid
) {
2334 spa_strfree(sip
->pool_name
);
2335 list_remove(&shl
->procfs_list
.pl_list
, sip
);
2337 kmem_free(sip
, sizeof (spa_import_progress_t
));
2341 mutex_exit(&shl
->procfs_list
.pl_lock
);
2345 * ==========================================================================
2346 * Initialization and Termination
2347 * ==========================================================================
2351 spa_name_compare(const void *a1
, const void *a2
)
2353 const spa_t
*s1
= a1
;
2354 const spa_t
*s2
= a2
;
2357 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
2359 return (TREE_ISIGN(s
));
2369 spa_init(spa_mode_t mode
)
2371 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2372 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2373 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2374 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
2376 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
2377 offsetof(spa_t
, spa_avl
));
2379 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
2380 offsetof(spa_aux_t
, aux_avl
));
2382 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
2383 offsetof(spa_aux_t
, aux_avl
));
2385 spa_mode_global
= mode
;
2388 if (spa_mode_global
!= SPA_MODE_READ
&& dprintf_find_string("watch")) {
2389 struct sigaction sa
;
2391 sa
.sa_flags
= SA_SIGINFO
;
2392 sigemptyset(&sa
.sa_mask
);
2393 sa
.sa_sigaction
= arc_buf_sigsegv
;
2395 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
2396 perror("could not enable watchpoints: "
2397 "sigaction(SIGSEGV, ...) = ");
2405 zfs_refcount_init();
2408 metaslab_stat_init();
2413 vdev_cache_stat_init();
2414 vdev_mirror_stat_init();
2415 vdev_raidz_math_init();
2419 zpool_feature_init();
2424 spa_import_progress_init();
2435 vdev_cache_stat_fini();
2436 vdev_mirror_stat_fini();
2437 vdev_raidz_math_fini();
2442 metaslab_stat_fini();
2445 zfs_refcount_fini();
2449 spa_import_progress_destroy();
2451 avl_destroy(&spa_namespace_avl
);
2452 avl_destroy(&spa_spare_avl
);
2453 avl_destroy(&spa_l2cache_avl
);
2455 cv_destroy(&spa_namespace_cv
);
2456 mutex_destroy(&spa_namespace_lock
);
2457 mutex_destroy(&spa_spare_lock
);
2458 mutex_destroy(&spa_l2cache_lock
);
2462 * Return whether this pool has a dedicated slog device. No locking needed.
2463 * It's not a problem if the wrong answer is returned as it's only for
2464 * performance and not correctness.
2467 spa_has_slogs(spa_t
*spa
)
2469 return (spa
->spa_log_class
->mc_groups
!= 0);
2473 spa_get_log_state(spa_t
*spa
)
2475 return (spa
->spa_log_state
);
2479 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
2481 spa
->spa_log_state
= state
;
2485 spa_is_root(spa_t
*spa
)
2487 return (spa
->spa_is_root
);
2491 spa_writeable(spa_t
*spa
)
2493 return (!!(spa
->spa_mode
& SPA_MODE_WRITE
) && spa
->spa_trust_config
);
2497 * Returns true if there is a pending sync task in any of the current
2498 * syncing txg, the current quiescing txg, or the current open txg.
2501 spa_has_pending_synctask(spa_t
*spa
)
2503 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
) ||
2504 !txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_early_sync_tasks
));
2508 spa_mode(spa_t
*spa
)
2510 return (spa
->spa_mode
);
2514 spa_bootfs(spa_t
*spa
)
2516 return (spa
->spa_bootfs
);
2520 spa_delegation(spa_t
*spa
)
2522 return (spa
->spa_delegation
);
2526 spa_meta_objset(spa_t
*spa
)
2528 return (spa
->spa_meta_objset
);
2532 spa_dedup_checksum(spa_t
*spa
)
2534 return (spa
->spa_dedup_checksum
);
2538 * Reset pool scan stat per scan pass (or reboot).
2541 spa_scan_stat_init(spa_t
*spa
)
2543 /* data not stored on disk */
2544 spa
->spa_scan_pass_start
= gethrestime_sec();
2545 if (dsl_scan_is_paused_scrub(spa
->spa_dsl_pool
->dp_scan
))
2546 spa
->spa_scan_pass_scrub_pause
= spa
->spa_scan_pass_start
;
2548 spa
->spa_scan_pass_scrub_pause
= 0;
2549 spa
->spa_scan_pass_scrub_spent_paused
= 0;
2550 spa
->spa_scan_pass_exam
= 0;
2551 spa
->spa_scan_pass_issued
= 0;
2552 vdev_scan_stat_init(spa
->spa_root_vdev
);
2556 * Get scan stats for zpool status reports
2559 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
2561 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
2563 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
2564 return (SET_ERROR(ENOENT
));
2565 bzero(ps
, sizeof (pool_scan_stat_t
));
2567 /* data stored on disk */
2568 ps
->pss_func
= scn
->scn_phys
.scn_func
;
2569 ps
->pss_state
= scn
->scn_phys
.scn_state
;
2570 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
2571 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
2572 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
2573 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
2574 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
2575 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
2576 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
2578 /* data not stored on disk */
2579 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
2580 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
2581 ps
->pss_pass_scrub_pause
= spa
->spa_scan_pass_scrub_pause
;
2582 ps
->pss_pass_scrub_spent_paused
= spa
->spa_scan_pass_scrub_spent_paused
;
2583 ps
->pss_pass_issued
= spa
->spa_scan_pass_issued
;
2585 scn
->scn_issued_before_pass
+ spa
->spa_scan_pass_issued
;
2591 spa_maxblocksize(spa_t
*spa
)
2593 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
2594 return (SPA_MAXBLOCKSIZE
);
2596 return (SPA_OLD_MAXBLOCKSIZE
);
2601 * Returns the txg that the last device removal completed. No indirect mappings
2602 * have been added since this txg.
2605 spa_get_last_removal_txg(spa_t
*spa
)
2608 uint64_t ret
= -1ULL;
2610 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2612 * sr_prev_indirect_vdev is only modified while holding all the
2613 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2616 vdevid
= spa
->spa_removing_phys
.sr_prev_indirect_vdev
;
2618 while (vdevid
!= -1ULL) {
2619 vdev_t
*vd
= vdev_lookup_top(spa
, vdevid
);
2620 vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
;
2622 ASSERT3P(vd
->vdev_ops
, ==, &vdev_indirect_ops
);
2625 * If the removal did not remap any data, we don't care.
2627 if (vdev_indirect_births_count(vib
) != 0) {
2628 ret
= vdev_indirect_births_last_entry_txg(vib
);
2632 vdevid
= vd
->vdev_indirect_config
.vic_prev_indirect_vdev
;
2634 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2637 spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
2643 spa_maxdnodesize(spa_t
*spa
)
2645 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2646 return (DNODE_MAX_SIZE
);
2648 return (DNODE_MIN_SIZE
);
2652 spa_multihost(spa_t
*spa
)
2654 return (spa
->spa_multihost
? B_TRUE
: B_FALSE
);
2658 spa_get_hostid(spa_t
*spa
)
2660 return (spa
->spa_hostid
);
2664 spa_trust_config(spa_t
*spa
)
2666 return (spa
->spa_trust_config
);
2670 spa_missing_tvds_allowed(spa_t
*spa
)
2672 return (spa
->spa_missing_tvds_allowed
);
2676 spa_syncing_log_sm(spa_t
*spa
)
2678 return (spa
->spa_syncing_log_sm
);
2682 spa_set_missing_tvds(spa_t
*spa
, uint64_t missing
)
2684 spa
->spa_missing_tvds
= missing
;
2688 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2691 spa_state_to_name(spa_t
*spa
)
2693 ASSERT3P(spa
, !=, NULL
);
2696 * it is possible for the spa to exist, without root vdev
2697 * as the spa transitions during import/export
2699 vdev_t
*rvd
= spa
->spa_root_vdev
;
2701 return ("TRANSITIONING");
2703 vdev_state_t state
= rvd
->vdev_state
;
2704 vdev_aux_t aux
= rvd
->vdev_stat
.vs_aux
;
2706 if (spa_suspended(spa
) &&
2707 (spa_get_failmode(spa
) != ZIO_FAILURE_MODE_CONTINUE
))
2708 return ("SUSPENDED");
2711 case VDEV_STATE_CLOSED
:
2712 case VDEV_STATE_OFFLINE
:
2714 case VDEV_STATE_REMOVED
:
2716 case VDEV_STATE_CANT_OPEN
:
2717 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
2719 else if (aux
== VDEV_AUX_SPLIT_POOL
)
2723 case VDEV_STATE_FAULTED
:
2725 case VDEV_STATE_DEGRADED
:
2726 return ("DEGRADED");
2727 case VDEV_STATE_HEALTHY
:
2737 spa_top_vdevs_spacemap_addressable(spa_t
*spa
)
2739 vdev_t
*rvd
= spa
->spa_root_vdev
;
2740 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2741 if (!vdev_is_spacemap_addressable(rvd
->vdev_child
[c
]))
2748 spa_has_checkpoint(spa_t
*spa
)
2750 return (spa
->spa_checkpoint_txg
!= 0);
2754 spa_importing_readonly_checkpoint(spa_t
*spa
)
2756 return ((spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
) &&
2757 spa
->spa_mode
== SPA_MODE_READ
);
2761 spa_min_claim_txg(spa_t
*spa
)
2763 uint64_t checkpoint_txg
= spa
->spa_uberblock
.ub_checkpoint_txg
;
2765 if (checkpoint_txg
!= 0)
2766 return (checkpoint_txg
+ 1);
2768 return (spa
->spa_first_txg
);
2772 * If there is a checkpoint, async destroys may consume more space from
2773 * the pool instead of freeing it. In an attempt to save the pool from
2774 * getting suspended when it is about to run out of space, we stop
2775 * processing async destroys.
2778 spa_suspend_async_destroy(spa_t
*spa
)
2780 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2782 uint64_t unreserved
= dsl_pool_unreserved_space(dp
,
2783 ZFS_SPACE_CHECK_EXTRA_RESERVED
);
2784 uint64_t used
= dsl_dir_phys(dp
->dp_root_dir
)->dd_used_bytes
;
2785 uint64_t avail
= (unreserved
> used
) ? (unreserved
- used
) : 0;
2787 if (spa_has_checkpoint(spa
) && avail
== 0)
2793 #if defined(_KERNEL)
2796 param_set_deadman_failmode_common(const char *val
)
2802 return (SET_ERROR(EINVAL
));
2804 if ((p
= strchr(val
, '\n')) != NULL
)
2807 if (strcmp(val
, "wait") != 0 && strcmp(val
, "continue") != 0 &&
2808 strcmp(val
, "panic"))
2809 return (SET_ERROR(EINVAL
));
2811 if (spa_mode_global
!= SPA_MODE_UNINIT
) {
2812 mutex_enter(&spa_namespace_lock
);
2813 while ((spa
= spa_next(spa
)) != NULL
)
2814 spa_set_deadman_failmode(spa
, val
);
2815 mutex_exit(&spa_namespace_lock
);
2822 /* Namespace manipulation */
2823 EXPORT_SYMBOL(spa_lookup
);
2824 EXPORT_SYMBOL(spa_add
);
2825 EXPORT_SYMBOL(spa_remove
);
2826 EXPORT_SYMBOL(spa_next
);
2828 /* Refcount functions */
2829 EXPORT_SYMBOL(spa_open_ref
);
2830 EXPORT_SYMBOL(spa_close
);
2831 EXPORT_SYMBOL(spa_refcount_zero
);
2833 /* Pool configuration lock */
2834 EXPORT_SYMBOL(spa_config_tryenter
);
2835 EXPORT_SYMBOL(spa_config_enter
);
2836 EXPORT_SYMBOL(spa_config_exit
);
2837 EXPORT_SYMBOL(spa_config_held
);
2839 /* Pool vdev add/remove lock */
2840 EXPORT_SYMBOL(spa_vdev_enter
);
2841 EXPORT_SYMBOL(spa_vdev_exit
);
2843 /* Pool vdev state change lock */
2844 EXPORT_SYMBOL(spa_vdev_state_enter
);
2845 EXPORT_SYMBOL(spa_vdev_state_exit
);
2847 /* Accessor functions */
2848 EXPORT_SYMBOL(spa_shutting_down
);
2849 EXPORT_SYMBOL(spa_get_dsl
);
2850 EXPORT_SYMBOL(spa_get_rootblkptr
);
2851 EXPORT_SYMBOL(spa_set_rootblkptr
);
2852 EXPORT_SYMBOL(spa_altroot
);
2853 EXPORT_SYMBOL(spa_sync_pass
);
2854 EXPORT_SYMBOL(spa_name
);
2855 EXPORT_SYMBOL(spa_guid
);
2856 EXPORT_SYMBOL(spa_last_synced_txg
);
2857 EXPORT_SYMBOL(spa_first_txg
);
2858 EXPORT_SYMBOL(spa_syncing_txg
);
2859 EXPORT_SYMBOL(spa_version
);
2860 EXPORT_SYMBOL(spa_state
);
2861 EXPORT_SYMBOL(spa_load_state
);
2862 EXPORT_SYMBOL(spa_freeze_txg
);
2863 EXPORT_SYMBOL(spa_get_dspace
);
2864 EXPORT_SYMBOL(spa_update_dspace
);
2865 EXPORT_SYMBOL(spa_deflate
);
2866 EXPORT_SYMBOL(spa_normal_class
);
2867 EXPORT_SYMBOL(spa_log_class
);
2868 EXPORT_SYMBOL(spa_special_class
);
2869 EXPORT_SYMBOL(spa_preferred_class
);
2870 EXPORT_SYMBOL(spa_max_replication
);
2871 EXPORT_SYMBOL(spa_prev_software_version
);
2872 EXPORT_SYMBOL(spa_get_failmode
);
2873 EXPORT_SYMBOL(spa_suspended
);
2874 EXPORT_SYMBOL(spa_bootfs
);
2875 EXPORT_SYMBOL(spa_delegation
);
2876 EXPORT_SYMBOL(spa_meta_objset
);
2877 EXPORT_SYMBOL(spa_maxblocksize
);
2878 EXPORT_SYMBOL(spa_maxdnodesize
);
2880 /* Miscellaneous support routines */
2881 EXPORT_SYMBOL(spa_guid_exists
);
2882 EXPORT_SYMBOL(spa_strdup
);
2883 EXPORT_SYMBOL(spa_strfree
);
2884 EXPORT_SYMBOL(spa_get_random
);
2885 EXPORT_SYMBOL(spa_generate_guid
);
2886 EXPORT_SYMBOL(snprintf_blkptr
);
2887 EXPORT_SYMBOL(spa_freeze
);
2888 EXPORT_SYMBOL(spa_upgrade
);
2889 EXPORT_SYMBOL(spa_evict_all
);
2890 EXPORT_SYMBOL(spa_lookup_by_guid
);
2891 EXPORT_SYMBOL(spa_has_spare
);
2892 EXPORT_SYMBOL(dva_get_dsize_sync
);
2893 EXPORT_SYMBOL(bp_get_dsize_sync
);
2894 EXPORT_SYMBOL(bp_get_dsize
);
2895 EXPORT_SYMBOL(spa_has_slogs
);
2896 EXPORT_SYMBOL(spa_is_root
);
2897 EXPORT_SYMBOL(spa_writeable
);
2898 EXPORT_SYMBOL(spa_mode
);
2899 EXPORT_SYMBOL(spa_namespace_lock
);
2900 EXPORT_SYMBOL(spa_trust_config
);
2901 EXPORT_SYMBOL(spa_missing_tvds_allowed
);
2902 EXPORT_SYMBOL(spa_set_missing_tvds
);
2903 EXPORT_SYMBOL(spa_state_to_name
);
2904 EXPORT_SYMBOL(spa_importing_readonly_checkpoint
);
2905 EXPORT_SYMBOL(spa_min_claim_txg
);
2906 EXPORT_SYMBOL(spa_suspend_async_destroy
);
2907 EXPORT_SYMBOL(spa_has_checkpoint
);
2908 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable
);
2910 ZFS_MODULE_PARAM(zfs
, zfs_
, flags
, UINT
, ZMOD_RW
,
2911 "Set additional debugging flags");
2913 ZFS_MODULE_PARAM(zfs
, zfs_
, recover
, INT
, ZMOD_RW
,
2914 "Set to attempt to recover from fatal errors");
2916 ZFS_MODULE_PARAM(zfs
, zfs_
, free_leak_on_eio
, INT
, ZMOD_RW
,
2917 "Set to ignore IO errors during free and permanently leak the space");
2919 ZFS_MODULE_PARAM(zfs
, zfs_
, deadman_checktime_ms
, ULONG
, ZMOD_RW
,
2920 "Dead I/O check interval in milliseconds");
2922 ZFS_MODULE_PARAM(zfs
, zfs_
, deadman_enabled
, INT
, ZMOD_RW
,
2923 "Enable deadman timer");
2925 ZFS_MODULE_PARAM(zfs_spa
, spa_
, asize_inflation
, INT
, ZMOD_RW
,
2926 "SPA size estimate multiplication factor");
2928 ZFS_MODULE_PARAM(zfs
, zfs_
, ddt_data_is_special
, INT
, ZMOD_RW
,
2929 "Place DDT data into the special class");
2931 ZFS_MODULE_PARAM(zfs
, zfs_
, user_indirect_is_special
, INT
, ZMOD_RW
,
2932 "Place user data indirect blocks into the special class");
2935 ZFS_MODULE_PARAM_CALL(zfs_deadman
, zfs_deadman_
, failmode
,
2936 param_set_deadman_failmode
, param_get_charp
, ZMOD_RW
,
2937 "Failmode for deadman timer");
2939 ZFS_MODULE_PARAM_CALL(zfs_deadman
, zfs_deadman_
, synctime_ms
,
2940 param_set_deadman_synctime
, param_get_ulong
, ZMOD_RW
,
2941 "Pool sync expiration time in milliseconds");
2943 ZFS_MODULE_PARAM_CALL(zfs_deadman
, zfs_deadman_
, ziotime_ms
,
2944 param_set_deadman_ziotime
, param_get_ulong
, ZMOD_RW
,
2945 "IO expiration time in milliseconds");
2947 ZFS_MODULE_PARAM(zfs
, zfs_
, special_class_metadata_reserve_pct
, INT
, ZMOD_RW
,
2948 "Small file blocks in special vdevs depends on this much "
2949 "free space available");
2952 ZFS_MODULE_PARAM_CALL(zfs_spa
, spa_
, slop_shift
, param_set_slop_shift
,
2953 param_get_int
, ZMOD_RW
, "Reserved free space in pool");