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 (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.
31 #include <sys/zfs_context.h>
32 #include <sys/spa_impl.h>
34 #include <sys/zio_checksum.h>
35 #include <sys/zio_compress.h>
37 #include <sys/dmu_tx.h>
40 #include <sys/vdev_impl.h>
41 #include <sys/vdev_initialize.h>
42 #include <sys/vdev_trim.h>
43 #include <sys/vdev_file.h>
44 #include <sys/vdev_raidz.h>
45 #include <sys/metaslab.h>
46 #include <sys/uberblock_impl.h>
49 #include <sys/unique.h>
50 #include <sys/dsl_pool.h>
51 #include <sys/dsl_dir.h>
52 #include <sys/dsl_prop.h>
53 #include <sys/fm/util.h>
54 #include <sys/dsl_scan.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/metaslab_impl.h>
59 #include <sys/kstat.h>
61 #include <sys/zfeature.h>
67 * There are four basic locks for managing spa_t structures:
69 * spa_namespace_lock (global mutex)
71 * This lock must be acquired to do any of the following:
73 * - Lookup a spa_t by name
74 * - Add or remove a spa_t from the namespace
75 * - Increase spa_refcount from non-zero
76 * - Check if spa_refcount is zero
78 * - add/remove/attach/detach devices
79 * - Held for the duration of create/destroy/import/export
81 * It does not need to handle recursion. A create or destroy may
82 * reference objects (files or zvols) in other pools, but by
83 * definition they must have an existing reference, and will never need
84 * to lookup a spa_t by name.
86 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
88 * This reference count keep track of any active users of the spa_t. The
89 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
90 * the refcount is never really 'zero' - opening a pool implicitly keeps
91 * some references in the DMU. Internally we check against spa_minref, but
92 * present the image of a zero/non-zero value to consumers.
94 * spa_config_lock[] (per-spa array of rwlocks)
96 * This protects the spa_t from config changes, and must be held in
97 * the following circumstances:
99 * - RW_READER to perform I/O to the spa
100 * - RW_WRITER to change the vdev config
102 * The locking order is fairly straightforward:
104 * spa_namespace_lock -> spa_refcount
106 * The namespace lock must be acquired to increase the refcount from 0
107 * or to check if it is zero.
109 * spa_refcount -> spa_config_lock[]
111 * There must be at least one valid reference on the spa_t to acquire
114 * spa_namespace_lock -> spa_config_lock[]
116 * The namespace lock must always be taken before the config lock.
119 * The spa_namespace_lock can be acquired directly and is globally visible.
121 * The namespace is manipulated using the following functions, all of which
122 * require the spa_namespace_lock to be held.
124 * spa_lookup() Lookup a spa_t by name.
126 * spa_add() Create a new spa_t in the namespace.
128 * spa_remove() Remove a spa_t from the namespace. This also
129 * frees up any memory associated with the spa_t.
131 * spa_next() Returns the next spa_t in the system, or the
132 * first if NULL is passed.
134 * spa_evict_all() Shutdown and remove all spa_t structures in
137 * spa_guid_exists() Determine whether a pool/device guid exists.
139 * The spa_refcount is manipulated using the following functions:
141 * spa_open_ref() Adds a reference to the given spa_t. Must be
142 * called with spa_namespace_lock held if the
143 * refcount is currently zero.
145 * spa_close() Remove a reference from the spa_t. This will
146 * not free the spa_t or remove it from the
147 * namespace. No locking is required.
149 * spa_refcount_zero() Returns true if the refcount is currently
150 * zero. Must be called with spa_namespace_lock
153 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
154 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
155 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
157 * To read the configuration, it suffices to hold one of these locks as reader.
158 * To modify the configuration, you must hold all locks as writer. To modify
159 * vdev state without altering the vdev tree's topology (e.g. online/offline),
160 * you must hold SCL_STATE and SCL_ZIO as writer.
162 * We use these distinct config locks to avoid recursive lock entry.
163 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
164 * block allocations (SCL_ALLOC), which may require reading space maps
165 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
167 * The spa config locks cannot be normal rwlocks because we need the
168 * ability to hand off ownership. For example, SCL_ZIO is acquired
169 * by the issuing thread and later released by an interrupt thread.
170 * They do, however, obey the usual write-wanted semantics to prevent
171 * writer (i.e. system administrator) starvation.
173 * The lock acquisition rules are as follows:
176 * Protects changes to the vdev tree topology, such as vdev
177 * add/remove/attach/detach. Protects the dirty config list
178 * (spa_config_dirty_list) and the set of spares and l2arc devices.
181 * Protects changes to pool state and vdev state, such as vdev
182 * online/offline/fault/degrade/clear. Protects the dirty state list
183 * (spa_state_dirty_list) and global pool state (spa_state).
186 * Protects changes to metaslab groups and classes.
187 * Held as reader by metaslab_alloc() and metaslab_claim().
190 * Held by bp-level zios (those which have no io_vd upon entry)
191 * to prevent changes to the vdev tree. The bp-level zio implicitly
192 * protects all of its vdev child zios, which do not hold SCL_ZIO.
195 * Protects changes to metaslab groups and classes.
196 * Held as reader by metaslab_free(). SCL_FREE is distinct from
197 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
198 * blocks in zio_done() while another i/o that holds either
199 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
202 * Held as reader to prevent changes to the vdev tree during trivial
203 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
204 * other locks, and lower than all of them, to ensure that it's safe
205 * to acquire regardless of caller context.
207 * In addition, the following rules apply:
209 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
210 * The lock ordering is SCL_CONFIG > spa_props_lock.
212 * (b) I/O operations on leaf vdevs. For any zio operation that takes
213 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
214 * or zio_write_phys() -- the caller must ensure that the config cannot
215 * cannot change in the interim, and that the vdev cannot be reopened.
216 * SCL_STATE as reader suffices for both.
218 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
220 * spa_vdev_enter() Acquire the namespace lock and the config lock
223 * spa_vdev_exit() Release the config lock, wait for all I/O
224 * to complete, sync the updated configs to the
225 * cache, and release the namespace lock.
227 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
228 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
229 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
232 static avl_tree_t spa_namespace_avl
;
233 kmutex_t spa_namespace_lock
;
234 static kcondvar_t spa_namespace_cv
;
235 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
237 static kmutex_t spa_spare_lock
;
238 static avl_tree_t spa_spare_avl
;
239 static kmutex_t spa_l2cache_lock
;
240 static avl_tree_t spa_l2cache_avl
;
242 kmem_cache_t
*spa_buffer_pool
;
247 * Everything except dprintf, set_error, spa, and indirect_remap is on
248 * by default in debug builds.
250 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SET_ERROR
|
251 ZFS_DEBUG_INDIRECT_REMAP
);
257 * zfs_recover can be set to nonzero to attempt to recover from
258 * otherwise-fatal errors, typically caused by on-disk corruption. When
259 * set, calls to zfs_panic_recover() will turn into warning messages.
260 * This should only be used as a last resort, as it typically results
261 * in leaked space, or worse.
263 int zfs_recover
= B_FALSE
;
266 * If destroy encounters an EIO while reading metadata (e.g. indirect
267 * blocks), space referenced by the missing metadata can not be freed.
268 * Normally this causes the background destroy to become "stalled", as
269 * it is unable to make forward progress. While in this stalled state,
270 * all remaining space to free from the error-encountering filesystem is
271 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
272 * permanently leak the space from indirect blocks that can not be read,
273 * and continue to free everything else that it can.
275 * The default, "stalling" behavior is useful if the storage partially
276 * fails (i.e. some but not all i/os fail), and then later recovers. In
277 * this case, we will be able to continue pool operations while it is
278 * partially failed, and when it recovers, we can continue to free the
279 * space, with no leaks. However, note that this case is actually
282 * Typically pools either (a) fail completely (but perhaps temporarily,
283 * e.g. a top-level vdev going offline), or (b) have localized,
284 * permanent errors (e.g. disk returns the wrong data due to bit flip or
285 * firmware bug). In case (a), this setting does not matter because the
286 * pool will be suspended and the sync thread will not be able to make
287 * forward progress regardless. In case (b), because the error is
288 * permanent, the best we can do is leak the minimum amount of space,
289 * which is what setting this flag will do. Therefore, it is reasonable
290 * for this flag to normally be set, but we chose the more conservative
291 * approach of not setting it, so that there is no possibility of
292 * leaking space in the "partial temporary" failure case.
294 int zfs_free_leak_on_eio
= B_FALSE
;
297 * Expiration time in milliseconds. This value has two meanings. First it is
298 * used to determine when the spa_deadman() logic should fire. By default the
299 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
300 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
301 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
302 * in one of three behaviors controlled by zfs_deadman_failmode.
304 unsigned long zfs_deadman_synctime_ms
= 600000ULL;
307 * This value controls the maximum amount of time zio_wait() will block for an
308 * outstanding IO. By default this is 300 seconds at which point the "hung"
309 * behavior will be applied as described for zfs_deadman_synctime_ms.
311 unsigned long zfs_deadman_ziotime_ms
= 300000ULL;
314 * Check time in milliseconds. This defines the frequency at which we check
317 unsigned long zfs_deadman_checktime_ms
= 60000ULL;
320 * By default the deadman is enabled.
322 int zfs_deadman_enabled
= 1;
325 * Controls the behavior of the deadman when it detects a "hung" I/O.
326 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
328 * wait - Wait for the "hung" I/O (default)
329 * continue - Attempt to recover from a "hung" I/O
330 * panic - Panic the system
332 char *zfs_deadman_failmode
= "wait";
335 * The worst case is single-sector max-parity RAID-Z blocks, in which
336 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
337 * times the size; so just assume that. Add to this the fact that
338 * we can have up to 3 DVAs per bp, and one more factor of 2 because
339 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
341 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
343 int spa_asize_inflation
= 24;
346 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
347 * the pool to be consumed. This ensures that we don't run the pool
348 * completely out of space, due to unaccounted changes (e.g. to the MOS).
349 * It also limits the worst-case time to allocate space. If we have
350 * less than this amount of free space, most ZPL operations (e.g. write,
351 * create) will return ENOSPC.
353 * Certain operations (e.g. file removal, most administrative actions) can
354 * use half the slop space. They will only return ENOSPC if less than half
355 * the slop space is free. Typically, once the pool has less than the slop
356 * space free, the user will use these operations to free up space in the pool.
357 * These are the operations that call dsl_pool_adjustedsize() with the netfree
358 * argument set to TRUE.
360 * Operations that are almost guaranteed to free up space in the absence of
361 * a pool checkpoint can use up to three quarters of the slop space
364 * A very restricted set of operations are always permitted, regardless of
365 * the amount of free space. These are the operations that call
366 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
367 * increase in the amount of space used, it is possible to run the pool
368 * completely out of space, causing it to be permanently read-only.
370 * Note that on very small pools, the slop space will be larger than
371 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
372 * but we never allow it to be more than half the pool size.
374 * See also the comments in zfs_space_check_t.
376 int spa_slop_shift
= 5;
377 uint64_t spa_min_slop
= 128 * 1024 * 1024;
378 int spa_allocators
= 4;
383 spa_load_failed(spa_t
*spa
, const char *fmt
, ...)
389 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
392 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa
->spa_name
,
393 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
398 spa_load_note(spa_t
*spa
, const char *fmt
, ...)
404 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
407 zfs_dbgmsg("spa_load(%s, config %s): %s", spa
->spa_name
,
408 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
412 * By default dedup and user data indirects land in the special class
414 int zfs_ddt_data_is_special
= B_TRUE
;
415 int zfs_user_indirect_is_special
= B_TRUE
;
418 * The percentage of special class final space reserved for metadata only.
419 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
420 * let metadata into the class.
422 int zfs_special_class_metadata_reserve_pct
= 25;
425 * ==========================================================================
427 * ==========================================================================
430 spa_config_lock_init(spa_t
*spa
)
432 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
433 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
434 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
435 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
436 zfs_refcount_create_untracked(&scl
->scl_count
);
437 scl
->scl_writer
= NULL
;
438 scl
->scl_write_wanted
= 0;
443 spa_config_lock_destroy(spa_t
*spa
)
445 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
446 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
447 mutex_destroy(&scl
->scl_lock
);
448 cv_destroy(&scl
->scl_cv
);
449 zfs_refcount_destroy(&scl
->scl_count
);
450 ASSERT(scl
->scl_writer
== NULL
);
451 ASSERT(scl
->scl_write_wanted
== 0);
456 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
458 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
459 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
460 if (!(locks
& (1 << i
)))
462 mutex_enter(&scl
->scl_lock
);
463 if (rw
== RW_READER
) {
464 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
465 mutex_exit(&scl
->scl_lock
);
466 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
471 ASSERT(scl
->scl_writer
!= curthread
);
472 if (!zfs_refcount_is_zero(&scl
->scl_count
)) {
473 mutex_exit(&scl
->scl_lock
);
474 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
478 scl
->scl_writer
= curthread
;
480 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
481 mutex_exit(&scl
->scl_lock
);
487 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
491 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
493 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
494 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
495 if (scl
->scl_writer
== curthread
)
496 wlocks_held
|= (1 << i
);
497 if (!(locks
& (1 << i
)))
499 mutex_enter(&scl
->scl_lock
);
500 if (rw
== RW_READER
) {
501 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
502 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
505 ASSERT(scl
->scl_writer
!= curthread
);
506 while (!zfs_refcount_is_zero(&scl
->scl_count
)) {
507 scl
->scl_write_wanted
++;
508 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
509 scl
->scl_write_wanted
--;
511 scl
->scl_writer
= curthread
;
513 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
514 mutex_exit(&scl
->scl_lock
);
516 ASSERT3U(wlocks_held
, <=, locks
);
520 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
522 for (int i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
523 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
524 if (!(locks
& (1 << i
)))
526 mutex_enter(&scl
->scl_lock
);
527 ASSERT(!zfs_refcount_is_zero(&scl
->scl_count
));
528 if (zfs_refcount_remove(&scl
->scl_count
, tag
) == 0) {
529 ASSERT(scl
->scl_writer
== NULL
||
530 scl
->scl_writer
== curthread
);
531 scl
->scl_writer
= NULL
; /* OK in either case */
532 cv_broadcast(&scl
->scl_cv
);
534 mutex_exit(&scl
->scl_lock
);
539 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
543 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
544 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
545 if (!(locks
& (1 << i
)))
547 if ((rw
== RW_READER
&&
548 !zfs_refcount_is_zero(&scl
->scl_count
)) ||
549 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
550 locks_held
|= 1 << i
;
557 * ==========================================================================
558 * SPA namespace functions
559 * ==========================================================================
563 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
564 * Returns NULL if no matching spa_t is found.
567 spa_lookup(const char *name
)
569 static spa_t search
; /* spa_t is large; don't allocate on stack */
574 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
576 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
579 * If it's a full dataset name, figure out the pool name and
582 cp
= strpbrk(search
.spa_name
, "/@#");
586 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
592 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
593 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
594 * looking for potentially hung I/Os.
597 spa_deadman(void *arg
)
601 /* Disable the deadman if the pool is suspended. */
602 if (spa_suspended(spa
))
605 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
606 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
607 ++spa
->spa_deadman_calls
);
608 if (zfs_deadman_enabled
)
609 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
611 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
612 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
613 MSEC_TO_TICK(zfs_deadman_checktime_ms
));
617 * Create an uninitialized spa_t with the given name. Requires
618 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
619 * exist by calling spa_lookup() first.
622 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
625 spa_config_dirent_t
*dp
;
627 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
629 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
631 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
632 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
633 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
634 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
635 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
636 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
637 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
638 mutex_init(&spa
->spa_cksum_tmpls_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
639 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
640 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
641 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
642 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
644 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
645 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
646 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
647 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
648 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
650 for (int t
= 0; t
< TXG_SIZE
; t
++)
651 bplist_create(&spa
->spa_free_bplist
[t
]);
653 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
654 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
655 spa
->spa_freeze_txg
= UINT64_MAX
;
656 spa
->spa_final_txg
= UINT64_MAX
;
657 spa
->spa_load_max_txg
= UINT64_MAX
;
659 spa
->spa_proc_state
= SPA_PROC_NONE
;
660 spa
->spa_trust_config
= B_TRUE
;
662 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
663 spa
->spa_deadman_ziotime
= MSEC2NSEC(zfs_deadman_ziotime_ms
);
664 spa_set_deadman_failmode(spa
, zfs_deadman_failmode
);
666 zfs_refcount_create(&spa
->spa_refcount
);
667 spa_config_lock_init(spa
);
670 avl_add(&spa_namespace_avl
, spa
);
673 * Set the alternate root, if there is one.
676 spa
->spa_root
= spa_strdup(altroot
);
678 spa
->spa_alloc_count
= spa_allocators
;
679 spa
->spa_alloc_locks
= kmem_zalloc(spa
->spa_alloc_count
*
680 sizeof (kmutex_t
), KM_SLEEP
);
681 spa
->spa_alloc_trees
= kmem_zalloc(spa
->spa_alloc_count
*
682 sizeof (avl_tree_t
), KM_SLEEP
);
683 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
684 mutex_init(&spa
->spa_alloc_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
685 avl_create(&spa
->spa_alloc_trees
[i
], zio_bookmark_compare
,
686 sizeof (zio_t
), offsetof(zio_t
, io_alloc_node
));
690 * Every pool starts with the default cachefile
692 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
693 offsetof(spa_config_dirent_t
, scd_link
));
695 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
696 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
697 list_insert_head(&spa
->spa_config_list
, dp
);
699 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
702 if (config
!= NULL
) {
705 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
707 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
711 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
714 if (spa
->spa_label_features
== NULL
) {
715 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
719 spa
->spa_min_ashift
= INT_MAX
;
720 spa
->spa_max_ashift
= 0;
722 /* Reset cached value */
723 spa
->spa_dedup_dspace
= ~0ULL;
726 * As a pool is being created, treat all features as disabled by
727 * setting SPA_FEATURE_DISABLED for all entries in the feature
730 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
731 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
734 list_create(&spa
->spa_leaf_list
, sizeof (vdev_t
),
735 offsetof(vdev_t
, vdev_leaf_node
));
741 * Removes a spa_t from the namespace, freeing up any memory used. Requires
742 * spa_namespace_lock. This is called only after the spa_t has been closed and
746 spa_remove(spa_t
*spa
)
748 spa_config_dirent_t
*dp
;
750 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
751 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
752 ASSERT3U(zfs_refcount_count(&spa
->spa_refcount
), ==, 0);
754 nvlist_free(spa
->spa_config_splitting
);
756 avl_remove(&spa_namespace_avl
, spa
);
757 cv_broadcast(&spa_namespace_cv
);
760 spa_strfree(spa
->spa_root
);
762 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
763 list_remove(&spa
->spa_config_list
, dp
);
764 if (dp
->scd_path
!= NULL
)
765 spa_strfree(dp
->scd_path
);
766 kmem_free(dp
, sizeof (spa_config_dirent_t
));
769 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
770 avl_destroy(&spa
->spa_alloc_trees
[i
]);
771 mutex_destroy(&spa
->spa_alloc_locks
[i
]);
773 kmem_free(spa
->spa_alloc_locks
, spa
->spa_alloc_count
*
775 kmem_free(spa
->spa_alloc_trees
, spa
->spa_alloc_count
*
776 sizeof (avl_tree_t
));
778 list_destroy(&spa
->spa_config_list
);
779 list_destroy(&spa
->spa_leaf_list
);
781 nvlist_free(spa
->spa_label_features
);
782 nvlist_free(spa
->spa_load_info
);
783 nvlist_free(spa
->spa_feat_stats
);
784 spa_config_set(spa
, NULL
);
786 zfs_refcount_destroy(&spa
->spa_refcount
);
788 spa_stats_destroy(spa
);
789 spa_config_lock_destroy(spa
);
791 for (int t
= 0; t
< TXG_SIZE
; t
++)
792 bplist_destroy(&spa
->spa_free_bplist
[t
]);
794 zio_checksum_templates_free(spa
);
796 cv_destroy(&spa
->spa_async_cv
);
797 cv_destroy(&spa
->spa_evicting_os_cv
);
798 cv_destroy(&spa
->spa_proc_cv
);
799 cv_destroy(&spa
->spa_scrub_io_cv
);
800 cv_destroy(&spa
->spa_suspend_cv
);
802 mutex_destroy(&spa
->spa_async_lock
);
803 mutex_destroy(&spa
->spa_errlist_lock
);
804 mutex_destroy(&spa
->spa_errlog_lock
);
805 mutex_destroy(&spa
->spa_evicting_os_lock
);
806 mutex_destroy(&spa
->spa_history_lock
);
807 mutex_destroy(&spa
->spa_proc_lock
);
808 mutex_destroy(&spa
->spa_props_lock
);
809 mutex_destroy(&spa
->spa_cksum_tmpls_lock
);
810 mutex_destroy(&spa
->spa_scrub_lock
);
811 mutex_destroy(&spa
->spa_suspend_lock
);
812 mutex_destroy(&spa
->spa_vdev_top_lock
);
813 mutex_destroy(&spa
->spa_feat_stats_lock
);
815 kmem_free(spa
, sizeof (spa_t
));
819 * Given a pool, return the next pool in the namespace, or NULL if there is
820 * none. If 'prev' is NULL, return the first pool.
823 spa_next(spa_t
*prev
)
825 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
828 return (AVL_NEXT(&spa_namespace_avl
, prev
));
830 return (avl_first(&spa_namespace_avl
));
834 * ==========================================================================
835 * SPA refcount functions
836 * ==========================================================================
840 * Add a reference to the given spa_t. Must have at least one reference, or
841 * have the namespace lock held.
844 spa_open_ref(spa_t
*spa
, void *tag
)
846 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
847 MUTEX_HELD(&spa_namespace_lock
));
848 (void) zfs_refcount_add(&spa
->spa_refcount
, tag
);
852 * Remove a reference to the given spa_t. Must have at least one reference, or
853 * have the namespace lock held.
856 spa_close(spa_t
*spa
, void *tag
)
858 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
859 MUTEX_HELD(&spa_namespace_lock
));
860 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
864 * Remove a reference to the given spa_t held by a dsl dir that is
865 * being asynchronously released. Async releases occur from a taskq
866 * performing eviction of dsl datasets and dirs. The namespace lock
867 * isn't held and the hold by the object being evicted may contribute to
868 * spa_minref (e.g. dataset or directory released during pool export),
869 * so the asserts in spa_close() do not apply.
872 spa_async_close(spa_t
*spa
, void *tag
)
874 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
878 * Check to see if the spa refcount is zero. Must be called with
879 * spa_namespace_lock held. We really compare against spa_minref, which is the
880 * number of references acquired when opening a pool
883 spa_refcount_zero(spa_t
*spa
)
885 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
887 return (zfs_refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
891 * ==========================================================================
892 * SPA spare and l2cache tracking
893 * ==========================================================================
897 * Hot spares and cache devices are tracked using the same code below,
898 * for 'auxiliary' devices.
901 typedef struct spa_aux
{
909 spa_aux_compare(const void *a
, const void *b
)
911 const spa_aux_t
*sa
= (const spa_aux_t
*)a
;
912 const spa_aux_t
*sb
= (const spa_aux_t
*)b
;
914 return (AVL_CMP(sa
->aux_guid
, sb
->aux_guid
));
918 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
924 search
.aux_guid
= vd
->vdev_guid
;
925 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
928 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
929 aux
->aux_guid
= vd
->vdev_guid
;
931 avl_insert(avl
, aux
, where
);
936 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
942 search
.aux_guid
= vd
->vdev_guid
;
943 aux
= avl_find(avl
, &search
, &where
);
947 if (--aux
->aux_count
== 0) {
948 avl_remove(avl
, aux
);
949 kmem_free(aux
, sizeof (spa_aux_t
));
950 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
951 aux
->aux_pool
= 0ULL;
956 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
958 spa_aux_t search
, *found
;
960 search
.aux_guid
= guid
;
961 found
= avl_find(avl
, &search
, NULL
);
965 *pool
= found
->aux_pool
;
972 *refcnt
= found
->aux_count
;
977 return (found
!= NULL
);
981 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
983 spa_aux_t search
, *found
;
986 search
.aux_guid
= vd
->vdev_guid
;
987 found
= avl_find(avl
, &search
, &where
);
988 ASSERT(found
!= NULL
);
989 ASSERT(found
->aux_pool
== 0ULL);
991 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
995 * Spares are tracked globally due to the following constraints:
997 * - A spare may be part of multiple pools.
998 * - A spare may be added to a pool even if it's actively in use within
1000 * - A spare in use in any pool can only be the source of a replacement if
1001 * the target is a spare in the same pool.
1003 * We keep track of all spares on the system through the use of a reference
1004 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1005 * spare, then we bump the reference count in the AVL tree. In addition, we set
1006 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1007 * inactive). When a spare is made active (used to replace a device in the
1008 * pool), we also keep track of which pool its been made a part of.
1010 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1011 * called under the spa_namespace lock as part of vdev reconfiguration. The
1012 * separate spare lock exists for the status query path, which does not need to
1013 * be completely consistent with respect to other vdev configuration changes.
1017 spa_spare_compare(const void *a
, const void *b
)
1019 return (spa_aux_compare(a
, b
));
1023 spa_spare_add(vdev_t
*vd
)
1025 mutex_enter(&spa_spare_lock
);
1026 ASSERT(!vd
->vdev_isspare
);
1027 spa_aux_add(vd
, &spa_spare_avl
);
1028 vd
->vdev_isspare
= B_TRUE
;
1029 mutex_exit(&spa_spare_lock
);
1033 spa_spare_remove(vdev_t
*vd
)
1035 mutex_enter(&spa_spare_lock
);
1036 ASSERT(vd
->vdev_isspare
);
1037 spa_aux_remove(vd
, &spa_spare_avl
);
1038 vd
->vdev_isspare
= B_FALSE
;
1039 mutex_exit(&spa_spare_lock
);
1043 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
1047 mutex_enter(&spa_spare_lock
);
1048 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
1049 mutex_exit(&spa_spare_lock
);
1055 spa_spare_activate(vdev_t
*vd
)
1057 mutex_enter(&spa_spare_lock
);
1058 ASSERT(vd
->vdev_isspare
);
1059 spa_aux_activate(vd
, &spa_spare_avl
);
1060 mutex_exit(&spa_spare_lock
);
1064 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1065 * Cache devices currently only support one pool per cache device, and so
1066 * for these devices the aux reference count is currently unused beyond 1.
1070 spa_l2cache_compare(const void *a
, const void *b
)
1072 return (spa_aux_compare(a
, b
));
1076 spa_l2cache_add(vdev_t
*vd
)
1078 mutex_enter(&spa_l2cache_lock
);
1079 ASSERT(!vd
->vdev_isl2cache
);
1080 spa_aux_add(vd
, &spa_l2cache_avl
);
1081 vd
->vdev_isl2cache
= B_TRUE
;
1082 mutex_exit(&spa_l2cache_lock
);
1086 spa_l2cache_remove(vdev_t
*vd
)
1088 mutex_enter(&spa_l2cache_lock
);
1089 ASSERT(vd
->vdev_isl2cache
);
1090 spa_aux_remove(vd
, &spa_l2cache_avl
);
1091 vd
->vdev_isl2cache
= B_FALSE
;
1092 mutex_exit(&spa_l2cache_lock
);
1096 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1100 mutex_enter(&spa_l2cache_lock
);
1101 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1102 mutex_exit(&spa_l2cache_lock
);
1108 spa_l2cache_activate(vdev_t
*vd
)
1110 mutex_enter(&spa_l2cache_lock
);
1111 ASSERT(vd
->vdev_isl2cache
);
1112 spa_aux_activate(vd
, &spa_l2cache_avl
);
1113 mutex_exit(&spa_l2cache_lock
);
1117 * ==========================================================================
1119 * ==========================================================================
1123 * Lock the given spa_t for the purpose of adding or removing a vdev.
1124 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1125 * It returns the next transaction group for the spa_t.
1128 spa_vdev_enter(spa_t
*spa
)
1130 mutex_enter(&spa
->spa_vdev_top_lock
);
1131 mutex_enter(&spa_namespace_lock
);
1133 vdev_autotrim_stop_all(spa
);
1135 return (spa_vdev_config_enter(spa
));
1139 * Internal implementation for spa_vdev_enter(). Used when a vdev
1140 * operation requires multiple syncs (i.e. removing a device) while
1141 * keeping the spa_namespace_lock held.
1144 spa_vdev_config_enter(spa_t
*spa
)
1146 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1148 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1150 return (spa_last_synced_txg(spa
) + 1);
1154 * Used in combination with spa_vdev_config_enter() to allow the syncing
1155 * of multiple transactions without releasing the spa_namespace_lock.
1158 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1160 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1162 int config_changed
= B_FALSE
;
1164 ASSERT(txg
> spa_last_synced_txg(spa
));
1166 spa
->spa_pending_vdev
= NULL
;
1169 * Reassess the DTLs.
1171 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1173 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1174 config_changed
= B_TRUE
;
1175 spa
->spa_config_generation
++;
1179 * Verify the metaslab classes.
1181 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1182 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1183 ASSERT(metaslab_class_validate(spa_special_class(spa
)) == 0);
1184 ASSERT(metaslab_class_validate(spa_dedup_class(spa
)) == 0);
1186 spa_config_exit(spa
, SCL_ALL
, spa
);
1189 * Panic the system if the specified tag requires it. This
1190 * is useful for ensuring that configurations are updated
1193 if (zio_injection_enabled
)
1194 zio_handle_panic_injection(spa
, tag
, 0);
1197 * Note: this txg_wait_synced() is important because it ensures
1198 * that there won't be more than one config change per txg.
1199 * This allows us to use the txg as the generation number.
1202 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1205 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1206 if (vd
->vdev_ops
->vdev_op_leaf
) {
1207 mutex_enter(&vd
->vdev_initialize_lock
);
1208 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
,
1210 mutex_exit(&vd
->vdev_initialize_lock
);
1212 mutex_enter(&vd
->vdev_trim_lock
);
1213 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, NULL
);
1214 mutex_exit(&vd
->vdev_trim_lock
);
1218 * The vdev may be both a leaf and top-level device.
1220 vdev_autotrim_stop_wait(vd
);
1222 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1224 spa_config_exit(spa
, SCL_ALL
, spa
);
1228 * If the config changed, update the config cache.
1231 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1235 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1236 * locking of spa_vdev_enter(), we also want make sure the transactions have
1237 * synced to disk, and then update the global configuration cache with the new
1241 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1243 vdev_autotrim_restart(spa
);
1245 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1246 mutex_exit(&spa_namespace_lock
);
1247 mutex_exit(&spa
->spa_vdev_top_lock
);
1253 * Lock the given spa_t for the purpose of changing vdev state.
1256 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1258 int locks
= SCL_STATE_ALL
| oplocks
;
1261 * Root pools may need to read of the underlying devfs filesystem
1262 * when opening up a vdev. Unfortunately if we're holding the
1263 * SCL_ZIO lock it will result in a deadlock when we try to issue
1264 * the read from the root filesystem. Instead we "prefetch"
1265 * the associated vnodes that we need prior to opening the
1266 * underlying devices and cache them so that we can prevent
1267 * any I/O when we are doing the actual open.
1269 if (spa_is_root(spa
)) {
1270 int low
= locks
& ~(SCL_ZIO
- 1);
1271 int high
= locks
& ~low
;
1273 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1274 vdev_hold(spa
->spa_root_vdev
);
1275 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1277 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1279 spa
->spa_vdev_locks
= locks
;
1283 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1285 boolean_t config_changed
= B_FALSE
;
1288 if (vd
== NULL
|| vd
== spa
->spa_root_vdev
) {
1289 vdev_top
= spa
->spa_root_vdev
;
1291 vdev_top
= vd
->vdev_top
;
1294 if (vd
!= NULL
|| error
== 0)
1295 vdev_dtl_reassess(vdev_top
, 0, 0, B_FALSE
);
1298 if (vd
!= spa
->spa_root_vdev
)
1299 vdev_state_dirty(vdev_top
);
1301 config_changed
= B_TRUE
;
1302 spa
->spa_config_generation
++;
1305 if (spa_is_root(spa
))
1306 vdev_rele(spa
->spa_root_vdev
);
1308 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1309 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1312 * If anything changed, wait for it to sync. This ensures that,
1313 * from the system administrator's perspective, zpool(1M) commands
1314 * are synchronous. This is important for things like zpool offline:
1315 * when the command completes, you expect no further I/O from ZFS.
1318 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1321 * If the config changed, update the config cache.
1323 if (config_changed
) {
1324 mutex_enter(&spa_namespace_lock
);
1325 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1326 mutex_exit(&spa_namespace_lock
);
1333 * ==========================================================================
1334 * Miscellaneous functions
1335 * ==========================================================================
1339 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1341 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1342 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1344 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1345 * dirty the vdev config because lock SCL_CONFIG is not held.
1346 * Thankfully, in this case we don't need to dirty the config
1347 * because it will be written out anyway when we finish
1348 * creating the pool.
1350 if (tx
->tx_txg
!= TXG_INITIAL
)
1351 vdev_config_dirty(spa
->spa_root_vdev
);
1356 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1358 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1359 vdev_config_dirty(spa
->spa_root_vdev
);
1363 * Return the spa_t associated with given pool_guid, if it exists. If
1364 * device_guid is non-zero, determine whether the pool exists *and* contains
1365 * a device with the specified device_guid.
1368 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1371 avl_tree_t
*t
= &spa_namespace_avl
;
1373 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1375 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1376 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1378 if (spa
->spa_root_vdev
== NULL
)
1380 if (spa_guid(spa
) == pool_guid
) {
1381 if (device_guid
== 0)
1384 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1385 device_guid
) != NULL
)
1389 * Check any devices we may be in the process of adding.
1391 if (spa
->spa_pending_vdev
) {
1392 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1393 device_guid
) != NULL
)
1403 * Determine whether a pool with the given pool_guid exists.
1406 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1408 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1412 spa_strdup(const char *s
)
1418 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1426 spa_strfree(char *s
)
1428 kmem_free(s
, strlen(s
) + 1);
1432 spa_get_random(uint64_t range
)
1441 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1447 spa_generate_guid(spa_t
*spa
)
1449 uint64_t guid
= spa_get_random(-1ULL);
1452 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1453 guid
= spa_get_random(-1ULL);
1455 while (guid
== 0 || spa_guid_exists(guid
, 0))
1456 guid
= spa_get_random(-1ULL);
1463 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1466 char *checksum
= NULL
;
1467 char *compress
= NULL
;
1470 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1471 dmu_object_byteswap_t bswap
=
1472 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1473 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1474 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1475 "metadata" : "data",
1476 dmu_ot_byteswap
[bswap
].ob_name
);
1478 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1481 if (!BP_IS_EMBEDDED(bp
)) {
1483 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1485 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1488 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1493 spa_freeze(spa_t
*spa
)
1495 uint64_t freeze_txg
= 0;
1497 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1498 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1499 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1500 spa
->spa_freeze_txg
= freeze_txg
;
1502 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1503 if (freeze_txg
!= 0)
1504 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1508 zfs_panic_recover(const char *fmt
, ...)
1513 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1518 * This is a stripped-down version of strtoull, suitable only for converting
1519 * lowercase hexadecimal numbers that don't overflow.
1522 zfs_strtonum(const char *str
, char **nptr
)
1528 while ((c
= *str
) != '\0') {
1529 if (c
>= '0' && c
<= '9')
1531 else if (c
>= 'a' && c
<= 'f')
1532 digit
= 10 + c
- 'a';
1543 *nptr
= (char *)str
;
1549 spa_activate_allocation_classes(spa_t
*spa
, dmu_tx_t
*tx
)
1552 * We bump the feature refcount for each special vdev added to the pool
1554 ASSERT(spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
));
1555 spa_feature_incr(spa
, SPA_FEATURE_ALLOCATION_CLASSES
, tx
);
1559 * ==========================================================================
1560 * Accessor functions
1561 * ==========================================================================
1565 spa_shutting_down(spa_t
*spa
)
1567 return (spa
->spa_async_suspended
);
1571 spa_get_dsl(spa_t
*spa
)
1573 return (spa
->spa_dsl_pool
);
1577 spa_is_initializing(spa_t
*spa
)
1579 return (spa
->spa_is_initializing
);
1583 spa_indirect_vdevs_loaded(spa_t
*spa
)
1585 return (spa
->spa_indirect_vdevs_loaded
);
1589 spa_get_rootblkptr(spa_t
*spa
)
1591 return (&spa
->spa_ubsync
.ub_rootbp
);
1595 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1597 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1601 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1603 if (spa
->spa_root
== NULL
)
1606 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1610 spa_sync_pass(spa_t
*spa
)
1612 return (spa
->spa_sync_pass
);
1616 spa_name(spa_t
*spa
)
1618 return (spa
->spa_name
);
1622 spa_guid(spa_t
*spa
)
1624 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1628 * If we fail to parse the config during spa_load(), we can go through
1629 * the error path (which posts an ereport) and end up here with no root
1630 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1633 if (spa
->spa_root_vdev
== NULL
)
1634 return (spa
->spa_config_guid
);
1636 guid
= spa
->spa_last_synced_guid
!= 0 ?
1637 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1640 * Return the most recently synced out guid unless we're
1641 * in syncing context.
1643 if (dp
&& dsl_pool_sync_context(dp
))
1644 return (spa
->spa_root_vdev
->vdev_guid
);
1650 spa_load_guid(spa_t
*spa
)
1653 * This is a GUID that exists solely as a reference for the
1654 * purposes of the arc. It is generated at load time, and
1655 * is never written to persistent storage.
1657 return (spa
->spa_load_guid
);
1661 spa_last_synced_txg(spa_t
*spa
)
1663 return (spa
->spa_ubsync
.ub_txg
);
1667 spa_first_txg(spa_t
*spa
)
1669 return (spa
->spa_first_txg
);
1673 spa_syncing_txg(spa_t
*spa
)
1675 return (spa
->spa_syncing_txg
);
1679 * Return the last txg where data can be dirtied. The final txgs
1680 * will be used to just clear out any deferred frees that remain.
1683 spa_final_dirty_txg(spa_t
*spa
)
1685 return (spa
->spa_final_txg
- TXG_DEFER_SIZE
);
1689 spa_state(spa_t
*spa
)
1691 return (spa
->spa_state
);
1695 spa_load_state(spa_t
*spa
)
1697 return (spa
->spa_load_state
);
1701 spa_freeze_txg(spa_t
*spa
)
1703 return (spa
->spa_freeze_txg
);
1707 * Return the inflated asize for a logical write in bytes. This is used by the
1708 * DMU to calculate the space a logical write will require on disk.
1709 * If lsize is smaller than the largest physical block size allocatable on this
1710 * pool we use its value instead, since the write will end up using the whole
1714 spa_get_worst_case_asize(spa_t
*spa
, uint64_t lsize
)
1717 return (0); /* No inflation needed */
1718 return (MAX(lsize
, 1 << spa
->spa_max_ashift
) * spa_asize_inflation
);
1722 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1723 * or at least 128MB, unless that would cause it to be more than half the
1726 * See the comment above spa_slop_shift for details.
1729 spa_get_slop_space(spa_t
*spa
)
1731 uint64_t space
= spa_get_dspace(spa
);
1732 return (MAX(space
>> spa_slop_shift
, MIN(space
>> 1, spa_min_slop
)));
1736 spa_get_dspace(spa_t
*spa
)
1738 return (spa
->spa_dspace
);
1742 spa_get_checkpoint_space(spa_t
*spa
)
1744 return (spa
->spa_checkpoint_info
.sci_dspace
);
1748 spa_update_dspace(spa_t
*spa
)
1750 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1751 ddt_get_dedup_dspace(spa
);
1752 if (spa
->spa_vdev_removal
!= NULL
) {
1754 * We can't allocate from the removing device, so
1755 * subtract its size. This prevents the DMU/DSL from
1756 * filling up the (now smaller) pool while we are in the
1757 * middle of removing the device.
1759 * Note that the DMU/DSL doesn't actually know or care
1760 * how much space is allocated (it does its own tracking
1761 * of how much space has been logically used). So it
1762 * doesn't matter that the data we are moving may be
1763 * allocated twice (on the old device and the new
1766 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1768 vdev_lookup_top(spa
, spa
->spa_vdev_removal
->svr_vdev_id
);
1769 spa
->spa_dspace
-= spa_deflate(spa
) ?
1770 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1771 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1776 * Return the failure mode that has been set to this pool. The default
1777 * behavior will be to block all I/Os when a complete failure occurs.
1780 spa_get_failmode(spa_t
*spa
)
1782 return (spa
->spa_failmode
);
1786 spa_suspended(spa_t
*spa
)
1788 return (spa
->spa_suspended
!= ZIO_SUSPEND_NONE
);
1792 spa_version(spa_t
*spa
)
1794 return (spa
->spa_ubsync
.ub_version
);
1798 spa_deflate(spa_t
*spa
)
1800 return (spa
->spa_deflate
);
1804 spa_normal_class(spa_t
*spa
)
1806 return (spa
->spa_normal_class
);
1810 spa_log_class(spa_t
*spa
)
1812 return (spa
->spa_log_class
);
1816 spa_special_class(spa_t
*spa
)
1818 return (spa
->spa_special_class
);
1822 spa_dedup_class(spa_t
*spa
)
1824 return (spa
->spa_dedup_class
);
1828 * Locate an appropriate allocation class
1831 spa_preferred_class(spa_t
*spa
, uint64_t size
, dmu_object_type_t objtype
,
1832 uint_t level
, uint_t special_smallblk
)
1834 if (DMU_OT_IS_ZIL(objtype
)) {
1835 if (spa
->spa_log_class
->mc_groups
!= 0)
1836 return (spa_log_class(spa
));
1838 return (spa_normal_class(spa
));
1841 boolean_t has_special_class
= spa
->spa_special_class
->mc_groups
!= 0;
1843 if (DMU_OT_IS_DDT(objtype
)) {
1844 if (spa
->spa_dedup_class
->mc_groups
!= 0)
1845 return (spa_dedup_class(spa
));
1846 else if (has_special_class
&& zfs_ddt_data_is_special
)
1847 return (spa_special_class(spa
));
1849 return (spa_normal_class(spa
));
1852 /* Indirect blocks for user data can land in special if allowed */
1853 if (level
> 0 && (DMU_OT_IS_FILE(objtype
) || objtype
== DMU_OT_ZVOL
)) {
1854 if (has_special_class
&& zfs_user_indirect_is_special
)
1855 return (spa_special_class(spa
));
1857 return (spa_normal_class(spa
));
1860 if (DMU_OT_IS_METADATA(objtype
) || level
> 0) {
1861 if (has_special_class
)
1862 return (spa_special_class(spa
));
1864 return (spa_normal_class(spa
));
1868 * Allow small file blocks in special class in some cases (like
1869 * for the dRAID vdev feature). But always leave a reserve of
1870 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1872 if (DMU_OT_IS_FILE(objtype
) &&
1873 has_special_class
&& size
<= special_smallblk
) {
1874 metaslab_class_t
*special
= spa_special_class(spa
);
1875 uint64_t alloc
= metaslab_class_get_alloc(special
);
1876 uint64_t space
= metaslab_class_get_space(special
);
1878 (space
* (100 - zfs_special_class_metadata_reserve_pct
))
1885 return (spa_normal_class(spa
));
1889 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1891 mutex_enter(&spa
->spa_evicting_os_lock
);
1892 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1893 mutex_exit(&spa
->spa_evicting_os_lock
);
1897 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1899 mutex_enter(&spa
->spa_evicting_os_lock
);
1900 list_remove(&spa
->spa_evicting_os_list
, os
);
1901 cv_broadcast(&spa
->spa_evicting_os_cv
);
1902 mutex_exit(&spa
->spa_evicting_os_lock
);
1906 spa_evicting_os_wait(spa_t
*spa
)
1908 mutex_enter(&spa
->spa_evicting_os_lock
);
1909 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1910 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1911 mutex_exit(&spa
->spa_evicting_os_lock
);
1913 dmu_buf_user_evict_wait();
1917 spa_max_replication(spa_t
*spa
)
1920 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1921 * handle BPs with more than one DVA allocated. Set our max
1922 * replication level accordingly.
1924 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1926 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1930 spa_prev_software_version(spa_t
*spa
)
1932 return (spa
->spa_prev_software_version
);
1936 spa_deadman_synctime(spa_t
*spa
)
1938 return (spa
->spa_deadman_synctime
);
1942 spa_get_autotrim(spa_t
*spa
)
1944 return (spa
->spa_autotrim
);
1948 spa_deadman_ziotime(spa_t
*spa
)
1950 return (spa
->spa_deadman_ziotime
);
1954 spa_get_deadman_failmode(spa_t
*spa
)
1956 return (spa
->spa_deadman_failmode
);
1960 spa_set_deadman_failmode(spa_t
*spa
, const char *failmode
)
1962 if (strcmp(failmode
, "wait") == 0)
1963 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1964 else if (strcmp(failmode
, "continue") == 0)
1965 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
1966 else if (strcmp(failmode
, "panic") == 0)
1967 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
1969 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1973 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1975 uint64_t asize
= DVA_GET_ASIZE(dva
);
1976 uint64_t dsize
= asize
;
1978 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1980 if (asize
!= 0 && spa
->spa_deflate
) {
1981 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1983 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
1984 vd
->vdev_deflate_ratio
;
1991 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1995 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1996 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2002 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
2006 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2008 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2009 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2011 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2017 spa_dirty_data(spa_t
*spa
)
2019 return (spa
->spa_dsl_pool
->dp_dirty_total
);
2023 * ==========================================================================
2024 * SPA Import Progress Routines
2025 * ==========================================================================
2028 typedef struct spa_import_progress
{
2029 uint64_t pool_guid
; /* unique id for updates */
2031 spa_load_state_t spa_load_state
;
2032 uint64_t mmp_sec_remaining
; /* MMP activity check */
2033 uint64_t spa_load_max_txg
; /* rewind txg */
2034 procfs_list_node_t smh_node
;
2035 } spa_import_progress_t
;
2037 spa_history_list_t
*spa_import_progress_list
= NULL
;
2040 spa_import_progress_show_header(struct seq_file
*f
)
2042 seq_printf(f
, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2043 "load_state", "multihost_secs", "max_txg",
2049 spa_import_progress_show(struct seq_file
*f
, void *data
)
2051 spa_import_progress_t
*sip
= (spa_import_progress_t
*)data
;
2053 seq_printf(f
, "%-20llu %-14llu %-14llu %-12llu %s\n",
2054 (u_longlong_t
)sip
->pool_guid
, (u_longlong_t
)sip
->spa_load_state
,
2055 (u_longlong_t
)sip
->mmp_sec_remaining
,
2056 (u_longlong_t
)sip
->spa_load_max_txg
,
2057 (sip
->pool_name
? sip
->pool_name
: "-"));
2062 /* Remove oldest elements from list until there are no more than 'size' left */
2064 spa_import_progress_truncate(spa_history_list_t
*shl
, unsigned int size
)
2066 spa_import_progress_t
*sip
;
2067 while (shl
->size
> size
) {
2068 sip
= list_remove_head(&shl
->procfs_list
.pl_list
);
2070 spa_strfree(sip
->pool_name
);
2071 kmem_free(sip
, sizeof (spa_import_progress_t
));
2075 IMPLY(size
== 0, list_is_empty(&shl
->procfs_list
.pl_list
));
2079 spa_import_progress_init(void)
2081 spa_import_progress_list
= kmem_zalloc(sizeof (spa_history_list_t
),
2084 spa_import_progress_list
->size
= 0;
2086 spa_import_progress_list
->procfs_list
.pl_private
=
2087 spa_import_progress_list
;
2089 procfs_list_install("zfs",
2092 &spa_import_progress_list
->procfs_list
,
2093 spa_import_progress_show
,
2094 spa_import_progress_show_header
,
2096 offsetof(spa_import_progress_t
, smh_node
));
2100 spa_import_progress_destroy(void)
2102 spa_history_list_t
*shl
= spa_import_progress_list
;
2103 procfs_list_uninstall(&shl
->procfs_list
);
2104 spa_import_progress_truncate(shl
, 0);
2105 kmem_free(shl
, sizeof (spa_history_list_t
));
2106 procfs_list_destroy(&shl
->procfs_list
);
2110 spa_import_progress_set_state(uint64_t pool_guid
,
2111 spa_load_state_t load_state
)
2113 spa_history_list_t
*shl
= spa_import_progress_list
;
2114 spa_import_progress_t
*sip
;
2120 mutex_enter(&shl
->procfs_list
.pl_lock
);
2121 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2122 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2123 if (sip
->pool_guid
== pool_guid
) {
2124 sip
->spa_load_state
= load_state
;
2129 mutex_exit(&shl
->procfs_list
.pl_lock
);
2135 spa_import_progress_set_max_txg(uint64_t pool_guid
, uint64_t load_max_txg
)
2137 spa_history_list_t
*shl
= spa_import_progress_list
;
2138 spa_import_progress_t
*sip
;
2144 mutex_enter(&shl
->procfs_list
.pl_lock
);
2145 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2146 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2147 if (sip
->pool_guid
== pool_guid
) {
2148 sip
->spa_load_max_txg
= load_max_txg
;
2153 mutex_exit(&shl
->procfs_list
.pl_lock
);
2159 spa_import_progress_set_mmp_check(uint64_t pool_guid
,
2160 uint64_t mmp_sec_remaining
)
2162 spa_history_list_t
*shl
= spa_import_progress_list
;
2163 spa_import_progress_t
*sip
;
2169 mutex_enter(&shl
->procfs_list
.pl_lock
);
2170 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2171 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2172 if (sip
->pool_guid
== pool_guid
) {
2173 sip
->mmp_sec_remaining
= mmp_sec_remaining
;
2178 mutex_exit(&shl
->procfs_list
.pl_lock
);
2184 * A new import is in progress, add an entry.
2187 spa_import_progress_add(spa_t
*spa
)
2189 spa_history_list_t
*shl
= spa_import_progress_list
;
2190 spa_import_progress_t
*sip
;
2191 char *poolname
= NULL
;
2193 sip
= kmem_zalloc(sizeof (spa_import_progress_t
), KM_SLEEP
);
2194 sip
->pool_guid
= spa_guid(spa
);
2196 (void) nvlist_lookup_string(spa
->spa_config
, ZPOOL_CONFIG_POOL_NAME
,
2198 if (poolname
== NULL
)
2199 poolname
= spa_name(spa
);
2200 sip
->pool_name
= spa_strdup(poolname
);
2201 sip
->spa_load_state
= spa_load_state(spa
);
2203 mutex_enter(&shl
->procfs_list
.pl_lock
);
2204 procfs_list_add(&shl
->procfs_list
, sip
);
2206 mutex_exit(&shl
->procfs_list
.pl_lock
);
2210 spa_import_progress_remove(uint64_t pool_guid
)
2212 spa_history_list_t
*shl
= spa_import_progress_list
;
2213 spa_import_progress_t
*sip
;
2215 mutex_enter(&shl
->procfs_list
.pl_lock
);
2216 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2217 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2218 if (sip
->pool_guid
== pool_guid
) {
2220 spa_strfree(sip
->pool_name
);
2221 list_remove(&shl
->procfs_list
.pl_list
, sip
);
2223 kmem_free(sip
, sizeof (spa_import_progress_t
));
2227 mutex_exit(&shl
->procfs_list
.pl_lock
);
2231 * ==========================================================================
2232 * Initialization and Termination
2233 * ==========================================================================
2237 spa_name_compare(const void *a1
, const void *a2
)
2239 const spa_t
*s1
= a1
;
2240 const spa_t
*s2
= a2
;
2243 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
2245 return (AVL_ISIGN(s
));
2257 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2258 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2259 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2260 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
2262 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
2263 offsetof(spa_t
, spa_avl
));
2265 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
2266 offsetof(spa_aux_t
, aux_avl
));
2268 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
2269 offsetof(spa_aux_t
, aux_avl
));
2271 spa_mode_global
= mode
;
2274 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
2275 struct sigaction sa
;
2277 sa
.sa_flags
= SA_SIGINFO
;
2278 sigemptyset(&sa
.sa_mask
);
2279 sa
.sa_sigaction
= arc_buf_sigsegv
;
2281 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
2282 perror("could not enable watchpoints: "
2283 "sigaction(SIGSEGV, ...) = ");
2291 zfs_refcount_init();
2294 metaslab_alloc_trace_init();
2299 vdev_cache_stat_init();
2300 vdev_mirror_stat_init();
2301 vdev_raidz_math_init();
2305 zpool_feature_init();
2310 spa_import_progress_init();
2321 vdev_cache_stat_fini();
2322 vdev_mirror_stat_fini();
2323 vdev_raidz_math_fini();
2328 metaslab_alloc_trace_fini();
2331 zfs_refcount_fini();
2335 spa_import_progress_destroy();
2337 avl_destroy(&spa_namespace_avl
);
2338 avl_destroy(&spa_spare_avl
);
2339 avl_destroy(&spa_l2cache_avl
);
2341 cv_destroy(&spa_namespace_cv
);
2342 mutex_destroy(&spa_namespace_lock
);
2343 mutex_destroy(&spa_spare_lock
);
2344 mutex_destroy(&spa_l2cache_lock
);
2348 * Return whether this pool has slogs. No locking needed.
2349 * It's not a problem if the wrong answer is returned as it's only for
2350 * performance and not correctness
2353 spa_has_slogs(spa_t
*spa
)
2355 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
2359 spa_get_log_state(spa_t
*spa
)
2361 return (spa
->spa_log_state
);
2365 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
2367 spa
->spa_log_state
= state
;
2371 spa_is_root(spa_t
*spa
)
2373 return (spa
->spa_is_root
);
2377 spa_writeable(spa_t
*spa
)
2379 return (!!(spa
->spa_mode
& FWRITE
) && spa
->spa_trust_config
);
2383 * Returns true if there is a pending sync task in any of the current
2384 * syncing txg, the current quiescing txg, or the current open txg.
2387 spa_has_pending_synctask(spa_t
*spa
)
2389 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
) ||
2390 !txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_early_sync_tasks
));
2394 spa_mode(spa_t
*spa
)
2396 return (spa
->spa_mode
);
2400 spa_bootfs(spa_t
*spa
)
2402 return (spa
->spa_bootfs
);
2406 spa_delegation(spa_t
*spa
)
2408 return (spa
->spa_delegation
);
2412 spa_meta_objset(spa_t
*spa
)
2414 return (spa
->spa_meta_objset
);
2418 spa_dedup_checksum(spa_t
*spa
)
2420 return (spa
->spa_dedup_checksum
);
2424 * Reset pool scan stat per scan pass (or reboot).
2427 spa_scan_stat_init(spa_t
*spa
)
2429 /* data not stored on disk */
2430 spa
->spa_scan_pass_start
= gethrestime_sec();
2431 if (dsl_scan_is_paused_scrub(spa
->spa_dsl_pool
->dp_scan
))
2432 spa
->spa_scan_pass_scrub_pause
= spa
->spa_scan_pass_start
;
2434 spa
->spa_scan_pass_scrub_pause
= 0;
2435 spa
->spa_scan_pass_scrub_spent_paused
= 0;
2436 spa
->spa_scan_pass_exam
= 0;
2437 spa
->spa_scan_pass_issued
= 0;
2438 vdev_scan_stat_init(spa
->spa_root_vdev
);
2442 * Get scan stats for zpool status reports
2445 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
2447 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
2449 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
2450 return (SET_ERROR(ENOENT
));
2451 bzero(ps
, sizeof (pool_scan_stat_t
));
2453 /* data stored on disk */
2454 ps
->pss_func
= scn
->scn_phys
.scn_func
;
2455 ps
->pss_state
= scn
->scn_phys
.scn_state
;
2456 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
2457 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
2458 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
2459 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
2460 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
2461 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
2462 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
2464 /* data not stored on disk */
2465 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
2466 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
2467 ps
->pss_pass_scrub_pause
= spa
->spa_scan_pass_scrub_pause
;
2468 ps
->pss_pass_scrub_spent_paused
= spa
->spa_scan_pass_scrub_spent_paused
;
2469 ps
->pss_pass_issued
= spa
->spa_scan_pass_issued
;
2471 scn
->scn_issued_before_pass
+ spa
->spa_scan_pass_issued
;
2477 spa_maxblocksize(spa_t
*spa
)
2479 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
2480 return (SPA_MAXBLOCKSIZE
);
2482 return (SPA_OLD_MAXBLOCKSIZE
);
2487 * Returns the txg that the last device removal completed. No indirect mappings
2488 * have been added since this txg.
2491 spa_get_last_removal_txg(spa_t
*spa
)
2494 uint64_t ret
= -1ULL;
2496 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2498 * sr_prev_indirect_vdev is only modified while holding all the
2499 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2502 vdevid
= spa
->spa_removing_phys
.sr_prev_indirect_vdev
;
2504 while (vdevid
!= -1ULL) {
2505 vdev_t
*vd
= vdev_lookup_top(spa
, vdevid
);
2506 vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
;
2508 ASSERT3P(vd
->vdev_ops
, ==, &vdev_indirect_ops
);
2511 * If the removal did not remap any data, we don't care.
2513 if (vdev_indirect_births_count(vib
) != 0) {
2514 ret
= vdev_indirect_births_last_entry_txg(vib
);
2518 vdevid
= vd
->vdev_indirect_config
.vic_prev_indirect_vdev
;
2520 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2523 spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
2529 spa_maxdnodesize(spa_t
*spa
)
2531 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2532 return (DNODE_MAX_SIZE
);
2534 return (DNODE_MIN_SIZE
);
2538 spa_multihost(spa_t
*spa
)
2540 return (spa
->spa_multihost
? B_TRUE
: B_FALSE
);
2544 spa_get_hostid(void)
2546 unsigned long myhostid
;
2549 myhostid
= zone_get_hostid(NULL
);
2552 * We're emulating the system's hostid in userland, so
2553 * we can't use zone_get_hostid().
2555 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2556 #endif /* _KERNEL */
2562 spa_trust_config(spa_t
*spa
)
2564 return (spa
->spa_trust_config
);
2568 spa_missing_tvds_allowed(spa_t
*spa
)
2570 return (spa
->spa_missing_tvds_allowed
);
2574 spa_set_missing_tvds(spa_t
*spa
, uint64_t missing
)
2576 spa
->spa_missing_tvds
= missing
;
2580 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2583 spa_state_to_name(spa_t
*spa
)
2585 ASSERT3P(spa
, !=, NULL
);
2588 * it is possible for the spa to exist, without root vdev
2589 * as the spa transitions during import/export
2591 vdev_t
*rvd
= spa
->spa_root_vdev
;
2593 return ("TRANSITIONING");
2595 vdev_state_t state
= rvd
->vdev_state
;
2596 vdev_aux_t aux
= rvd
->vdev_stat
.vs_aux
;
2598 if (spa_suspended(spa
) &&
2599 (spa_get_failmode(spa
) != ZIO_FAILURE_MODE_CONTINUE
))
2600 return ("SUSPENDED");
2603 case VDEV_STATE_CLOSED
:
2604 case VDEV_STATE_OFFLINE
:
2606 case VDEV_STATE_REMOVED
:
2608 case VDEV_STATE_CANT_OPEN
:
2609 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
2611 else if (aux
== VDEV_AUX_SPLIT_POOL
)
2615 case VDEV_STATE_FAULTED
:
2617 case VDEV_STATE_DEGRADED
:
2618 return ("DEGRADED");
2619 case VDEV_STATE_HEALTHY
:
2629 spa_top_vdevs_spacemap_addressable(spa_t
*spa
)
2631 vdev_t
*rvd
= spa
->spa_root_vdev
;
2632 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2633 if (!vdev_is_spacemap_addressable(rvd
->vdev_child
[c
]))
2640 spa_has_checkpoint(spa_t
*spa
)
2642 return (spa
->spa_checkpoint_txg
!= 0);
2646 spa_importing_readonly_checkpoint(spa_t
*spa
)
2648 return ((spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
) &&
2649 spa
->spa_mode
== FREAD
);
2653 spa_min_claim_txg(spa_t
*spa
)
2655 uint64_t checkpoint_txg
= spa
->spa_uberblock
.ub_checkpoint_txg
;
2657 if (checkpoint_txg
!= 0)
2658 return (checkpoint_txg
+ 1);
2660 return (spa
->spa_first_txg
);
2664 * If there is a checkpoint, async destroys may consume more space from
2665 * the pool instead of freeing it. In an attempt to save the pool from
2666 * getting suspended when it is about to run out of space, we stop
2667 * processing async destroys.
2670 spa_suspend_async_destroy(spa_t
*spa
)
2672 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2674 uint64_t unreserved
= dsl_pool_unreserved_space(dp
,
2675 ZFS_SPACE_CHECK_EXTRA_RESERVED
);
2676 uint64_t used
= dsl_dir_phys(dp
->dp_root_dir
)->dd_used_bytes
;
2677 uint64_t avail
= (unreserved
> used
) ? (unreserved
- used
) : 0;
2679 if (spa_has_checkpoint(spa
) && avail
== 0)
2685 #if defined(_KERNEL)
2687 #include <linux/mod_compat.h>
2690 param_set_deadman_failmode(const char *val
, zfs_kernel_param_t
*kp
)
2696 return (SET_ERROR(-EINVAL
));
2698 if ((p
= strchr(val
, '\n')) != NULL
)
2701 if (strcmp(val
, "wait") != 0 && strcmp(val
, "continue") != 0 &&
2702 strcmp(val
, "panic"))
2703 return (SET_ERROR(-EINVAL
));
2705 if (spa_mode_global
!= 0) {
2706 mutex_enter(&spa_namespace_lock
);
2707 while ((spa
= spa_next(spa
)) != NULL
)
2708 spa_set_deadman_failmode(spa
, val
);
2709 mutex_exit(&spa_namespace_lock
);
2712 return (param_set_charp(val
, kp
));
2716 param_set_deadman_ziotime(const char *val
, zfs_kernel_param_t
*kp
)
2721 error
= param_set_ulong(val
, kp
);
2723 return (SET_ERROR(error
));
2725 if (spa_mode_global
!= 0) {
2726 mutex_enter(&spa_namespace_lock
);
2727 while ((spa
= spa_next(spa
)) != NULL
)
2728 spa
->spa_deadman_ziotime
=
2729 MSEC2NSEC(zfs_deadman_ziotime_ms
);
2730 mutex_exit(&spa_namespace_lock
);
2737 param_set_deadman_synctime(const char *val
, zfs_kernel_param_t
*kp
)
2742 error
= param_set_ulong(val
, kp
);
2744 return (SET_ERROR(error
));
2746 if (spa_mode_global
!= 0) {
2747 mutex_enter(&spa_namespace_lock
);
2748 while ((spa
= spa_next(spa
)) != NULL
)
2749 spa
->spa_deadman_synctime
=
2750 MSEC2NSEC(zfs_deadman_synctime_ms
);
2751 mutex_exit(&spa_namespace_lock
);
2758 param_set_slop_shift(const char *buf
, zfs_kernel_param_t
*kp
)
2763 error
= kstrtoul(buf
, 0, &val
);
2765 return (SET_ERROR(error
));
2767 if (val
< 1 || val
> 31)
2768 return (SET_ERROR(-EINVAL
));
2770 error
= param_set_int(buf
, kp
);
2772 return (SET_ERROR(error
));
2777 /* Namespace manipulation */
2778 EXPORT_SYMBOL(spa_lookup
);
2779 EXPORT_SYMBOL(spa_add
);
2780 EXPORT_SYMBOL(spa_remove
);
2781 EXPORT_SYMBOL(spa_next
);
2783 /* Refcount functions */
2784 EXPORT_SYMBOL(spa_open_ref
);
2785 EXPORT_SYMBOL(spa_close
);
2786 EXPORT_SYMBOL(spa_refcount_zero
);
2788 /* Pool configuration lock */
2789 EXPORT_SYMBOL(spa_config_tryenter
);
2790 EXPORT_SYMBOL(spa_config_enter
);
2791 EXPORT_SYMBOL(spa_config_exit
);
2792 EXPORT_SYMBOL(spa_config_held
);
2794 /* Pool vdev add/remove lock */
2795 EXPORT_SYMBOL(spa_vdev_enter
);
2796 EXPORT_SYMBOL(spa_vdev_exit
);
2798 /* Pool vdev state change lock */
2799 EXPORT_SYMBOL(spa_vdev_state_enter
);
2800 EXPORT_SYMBOL(spa_vdev_state_exit
);
2802 /* Accessor functions */
2803 EXPORT_SYMBOL(spa_shutting_down
);
2804 EXPORT_SYMBOL(spa_get_dsl
);
2805 EXPORT_SYMBOL(spa_get_rootblkptr
);
2806 EXPORT_SYMBOL(spa_set_rootblkptr
);
2807 EXPORT_SYMBOL(spa_altroot
);
2808 EXPORT_SYMBOL(spa_sync_pass
);
2809 EXPORT_SYMBOL(spa_name
);
2810 EXPORT_SYMBOL(spa_guid
);
2811 EXPORT_SYMBOL(spa_last_synced_txg
);
2812 EXPORT_SYMBOL(spa_first_txg
);
2813 EXPORT_SYMBOL(spa_syncing_txg
);
2814 EXPORT_SYMBOL(spa_version
);
2815 EXPORT_SYMBOL(spa_state
);
2816 EXPORT_SYMBOL(spa_load_state
);
2817 EXPORT_SYMBOL(spa_freeze_txg
);
2818 EXPORT_SYMBOL(spa_get_dspace
);
2819 EXPORT_SYMBOL(spa_update_dspace
);
2820 EXPORT_SYMBOL(spa_deflate
);
2821 EXPORT_SYMBOL(spa_normal_class
);
2822 EXPORT_SYMBOL(spa_log_class
);
2823 EXPORT_SYMBOL(spa_special_class
);
2824 EXPORT_SYMBOL(spa_preferred_class
);
2825 EXPORT_SYMBOL(spa_max_replication
);
2826 EXPORT_SYMBOL(spa_prev_software_version
);
2827 EXPORT_SYMBOL(spa_get_failmode
);
2828 EXPORT_SYMBOL(spa_suspended
);
2829 EXPORT_SYMBOL(spa_bootfs
);
2830 EXPORT_SYMBOL(spa_delegation
);
2831 EXPORT_SYMBOL(spa_meta_objset
);
2832 EXPORT_SYMBOL(spa_maxblocksize
);
2833 EXPORT_SYMBOL(spa_maxdnodesize
);
2835 /* Miscellaneous support routines */
2836 EXPORT_SYMBOL(spa_guid_exists
);
2837 EXPORT_SYMBOL(spa_strdup
);
2838 EXPORT_SYMBOL(spa_strfree
);
2839 EXPORT_SYMBOL(spa_get_random
);
2840 EXPORT_SYMBOL(spa_generate_guid
);
2841 EXPORT_SYMBOL(snprintf_blkptr
);
2842 EXPORT_SYMBOL(spa_freeze
);
2843 EXPORT_SYMBOL(spa_upgrade
);
2844 EXPORT_SYMBOL(spa_evict_all
);
2845 EXPORT_SYMBOL(spa_lookup_by_guid
);
2846 EXPORT_SYMBOL(spa_has_spare
);
2847 EXPORT_SYMBOL(dva_get_dsize_sync
);
2848 EXPORT_SYMBOL(bp_get_dsize_sync
);
2849 EXPORT_SYMBOL(bp_get_dsize
);
2850 EXPORT_SYMBOL(spa_has_slogs
);
2851 EXPORT_SYMBOL(spa_is_root
);
2852 EXPORT_SYMBOL(spa_writeable
);
2853 EXPORT_SYMBOL(spa_mode
);
2854 EXPORT_SYMBOL(spa_namespace_lock
);
2855 EXPORT_SYMBOL(spa_trust_config
);
2856 EXPORT_SYMBOL(spa_missing_tvds_allowed
);
2857 EXPORT_SYMBOL(spa_set_missing_tvds
);
2858 EXPORT_SYMBOL(spa_state_to_name
);
2859 EXPORT_SYMBOL(spa_importing_readonly_checkpoint
);
2860 EXPORT_SYMBOL(spa_min_claim_txg
);
2861 EXPORT_SYMBOL(spa_suspend_async_destroy
);
2862 EXPORT_SYMBOL(spa_has_checkpoint
);
2863 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable
);
2866 module_param(zfs_flags
, uint
, 0644);
2867 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
2869 module_param(zfs_recover
, int, 0644);
2870 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
2872 module_param(zfs_free_leak_on_eio
, int, 0644);
2873 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
2874 "Set to ignore IO errors during free and permanently leak the space");
2876 module_param_call(zfs_deadman_synctime_ms
, param_set_deadman_synctime
,
2877 param_get_ulong
, &zfs_deadman_synctime_ms
, 0644);
2878 MODULE_PARM_DESC(zfs_deadman_synctime_ms
,
2879 "Pool sync expiration time in milliseconds");
2881 module_param_call(zfs_deadman_ziotime_ms
, param_set_deadman_ziotime
,
2882 param_get_ulong
, &zfs_deadman_ziotime_ms
, 0644);
2883 MODULE_PARM_DESC(zfs_deadman_ziotime_ms
,
2884 "IO expiration time in milliseconds");
2886 module_param(zfs_deadman_checktime_ms
, ulong
, 0644);
2887 MODULE_PARM_DESC(zfs_deadman_checktime_ms
,
2888 "Dead I/O check interval in milliseconds");
2890 module_param(zfs_deadman_enabled
, int, 0644);
2891 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
2893 module_param_call(zfs_deadman_failmode
, param_set_deadman_failmode
,
2894 param_get_charp
, &zfs_deadman_failmode
, 0644);
2895 MODULE_PARM_DESC(zfs_deadman_failmode
, "Failmode for deadman timer");
2897 module_param(spa_asize_inflation
, int, 0644);
2898 MODULE_PARM_DESC(spa_asize_inflation
,
2899 "SPA size estimate multiplication factor");
2901 module_param_call(spa_slop_shift
, param_set_slop_shift
, param_get_int
,
2902 &spa_slop_shift
, 0644);
2903 MODULE_PARM_DESC(spa_slop_shift
, "Reserved free space in pool");
2905 module_param(zfs_ddt_data_is_special
, int, 0644);
2906 MODULE_PARM_DESC(zfs_ddt_data_is_special
,
2907 "Place DDT data into the special class");
2909 module_param(zfs_user_indirect_is_special
, int, 0644);
2910 MODULE_PARM_DESC(zfs_user_indirect_is_special
,
2911 "Place user data indirect blocks into the special class");
2913 module_param(zfs_special_class_metadata_reserve_pct
, int, 0644);
2914 MODULE_PARM_DESC(zfs_special_class_metadata_reserve_pct
,
2915 "Small file blocks in special vdevs depends on this much "
2916 "free space available");