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_file.h>
43 #include <sys/vdev_raidz.h>
44 #include <sys/metaslab.h>
45 #include <sys/uberblock_impl.h>
48 #include <sys/unique.h>
49 #include <sys/dsl_pool.h>
50 #include <sys/dsl_dir.h>
51 #include <sys/dsl_prop.h>
52 #include <sys/fm/util.h>
53 #include <sys/dsl_scan.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/metaslab_impl.h>
58 #include <sys/kstat.h>
60 #include <sys/zfeature.h>
66 * There are four basic locks for managing spa_t structures:
68 * spa_namespace_lock (global mutex)
70 * This lock must be acquired to do any of the following:
72 * - Lookup a spa_t by name
73 * - Add or remove a spa_t from the namespace
74 * - Increase spa_refcount from non-zero
75 * - Check if spa_refcount is zero
77 * - add/remove/attach/detach devices
78 * - Held for the duration of create/destroy/import/export
80 * It does not need to handle recursion. A create or destroy may
81 * reference objects (files or zvols) in other pools, but by
82 * definition they must have an existing reference, and will never need
83 * to lookup a spa_t by name.
85 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
87 * This reference count keep track of any active users of the spa_t. The
88 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
89 * the refcount is never really 'zero' - opening a pool implicitly keeps
90 * some references in the DMU. Internally we check against spa_minref, but
91 * present the image of a zero/non-zero value to consumers.
93 * spa_config_lock[] (per-spa array of rwlocks)
95 * This protects the spa_t from config changes, and must be held in
96 * the following circumstances:
98 * - RW_READER to perform I/O to the spa
99 * - RW_WRITER to change the vdev config
101 * The locking order is fairly straightforward:
103 * spa_namespace_lock -> spa_refcount
105 * The namespace lock must be acquired to increase the refcount from 0
106 * or to check if it is zero.
108 * spa_refcount -> spa_config_lock[]
110 * There must be at least one valid reference on the spa_t to acquire
113 * spa_namespace_lock -> spa_config_lock[]
115 * The namespace lock must always be taken before the config lock.
118 * The spa_namespace_lock can be acquired directly and is globally visible.
120 * The namespace is manipulated using the following functions, all of which
121 * require the spa_namespace_lock to be held.
123 * spa_lookup() Lookup a spa_t by name.
125 * spa_add() Create a new spa_t in the namespace.
127 * spa_remove() Remove a spa_t from the namespace. This also
128 * frees up any memory associated with the spa_t.
130 * spa_next() Returns the next spa_t in the system, or the
131 * first if NULL is passed.
133 * spa_evict_all() Shutdown and remove all spa_t structures in
136 * spa_guid_exists() Determine whether a pool/device guid exists.
138 * The spa_refcount is manipulated using the following functions:
140 * spa_open_ref() Adds a reference to the given spa_t. Must be
141 * called with spa_namespace_lock held if the
142 * refcount is currently zero.
144 * spa_close() Remove a reference from the spa_t. This will
145 * not free the spa_t or remove it from the
146 * namespace. No locking is required.
148 * spa_refcount_zero() Returns true if the refcount is currently
149 * zero. Must be called with spa_namespace_lock
152 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
153 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
154 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
156 * To read the configuration, it suffices to hold one of these locks as reader.
157 * To modify the configuration, you must hold all locks as writer. To modify
158 * vdev state without altering the vdev tree's topology (e.g. online/offline),
159 * you must hold SCL_STATE and SCL_ZIO as writer.
161 * We use these distinct config locks to avoid recursive lock entry.
162 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
163 * block allocations (SCL_ALLOC), which may require reading space maps
164 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
166 * The spa config locks cannot be normal rwlocks because we need the
167 * ability to hand off ownership. For example, SCL_ZIO is acquired
168 * by the issuing thread and later released by an interrupt thread.
169 * They do, however, obey the usual write-wanted semantics to prevent
170 * writer (i.e. system administrator) starvation.
172 * The lock acquisition rules are as follows:
175 * Protects changes to the vdev tree topology, such as vdev
176 * add/remove/attach/detach. Protects the dirty config list
177 * (spa_config_dirty_list) and the set of spares and l2arc devices.
180 * Protects changes to pool state and vdev state, such as vdev
181 * online/offline/fault/degrade/clear. Protects the dirty state list
182 * (spa_state_dirty_list) and global pool state (spa_state).
185 * Protects changes to metaslab groups and classes.
186 * Held as reader by metaslab_alloc() and metaslab_claim().
189 * Held by bp-level zios (those which have no io_vd upon entry)
190 * to prevent changes to the vdev tree. The bp-level zio implicitly
191 * protects all of its vdev child zios, which do not hold SCL_ZIO.
194 * Protects changes to metaslab groups and classes.
195 * Held as reader by metaslab_free(). SCL_FREE is distinct from
196 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
197 * blocks in zio_done() while another i/o that holds either
198 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
201 * Held as reader to prevent changes to the vdev tree during trivial
202 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
203 * other locks, and lower than all of them, to ensure that it's safe
204 * to acquire regardless of caller context.
206 * In addition, the following rules apply:
208 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
209 * The lock ordering is SCL_CONFIG > spa_props_lock.
211 * (b) I/O operations on leaf vdevs. For any zio operation that takes
212 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
213 * or zio_write_phys() -- the caller must ensure that the config cannot
214 * cannot change in the interim, and that the vdev cannot be reopened.
215 * SCL_STATE as reader suffices for both.
217 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
219 * spa_vdev_enter() Acquire the namespace lock and the config lock
222 * spa_vdev_exit() Release the config lock, wait for all I/O
223 * to complete, sync the updated configs to the
224 * cache, and release the namespace lock.
226 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
227 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
228 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
231 static avl_tree_t spa_namespace_avl
;
232 kmutex_t spa_namespace_lock
;
233 static kcondvar_t spa_namespace_cv
;
234 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
236 static kmutex_t spa_spare_lock
;
237 static avl_tree_t spa_spare_avl
;
238 static kmutex_t spa_l2cache_lock
;
239 static avl_tree_t spa_l2cache_avl
;
241 kmem_cache_t
*spa_buffer_pool
;
246 * Everything except dprintf, set_error, spa, and indirect_remap is on
247 * by default in debug builds.
249 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SET_ERROR
|
250 ZFS_DEBUG_INDIRECT_REMAP
);
256 * zfs_recover can be set to nonzero to attempt to recover from
257 * otherwise-fatal errors, typically caused by on-disk corruption. When
258 * set, calls to zfs_panic_recover() will turn into warning messages.
259 * This should only be used as a last resort, as it typically results
260 * in leaked space, or worse.
262 int zfs_recover
= B_FALSE
;
265 * If destroy encounters an EIO while reading metadata (e.g. indirect
266 * blocks), space referenced by the missing metadata can not be freed.
267 * Normally this causes the background destroy to become "stalled", as
268 * it is unable to make forward progress. While in this stalled state,
269 * all remaining space to free from the error-encountering filesystem is
270 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
271 * permanently leak the space from indirect blocks that can not be read,
272 * and continue to free everything else that it can.
274 * The default, "stalling" behavior is useful if the storage partially
275 * fails (i.e. some but not all i/os fail), and then later recovers. In
276 * this case, we will be able to continue pool operations while it is
277 * partially failed, and when it recovers, we can continue to free the
278 * space, with no leaks. However, note that this case is actually
281 * Typically pools either (a) fail completely (but perhaps temporarily,
282 * e.g. a top-level vdev going offline), or (b) have localized,
283 * permanent errors (e.g. disk returns the wrong data due to bit flip or
284 * firmware bug). In case (a), this setting does not matter because the
285 * pool will be suspended and the sync thread will not be able to make
286 * forward progress regardless. In case (b), because the error is
287 * permanent, the best we can do is leak the minimum amount of space,
288 * which is what setting this flag will do. Therefore, it is reasonable
289 * for this flag to normally be set, but we chose the more conservative
290 * approach of not setting it, so that there is no possibility of
291 * leaking space in the "partial temporary" failure case.
293 int zfs_free_leak_on_eio
= B_FALSE
;
296 * Expiration time in milliseconds. This value has two meanings. First it is
297 * used to determine when the spa_deadman() logic should fire. By default the
298 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
299 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
300 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
301 * in one of three behaviors controlled by zfs_deadman_failmode.
303 unsigned long zfs_deadman_synctime_ms
= 600000ULL;
306 * This value controls the maximum amount of time zio_wait() will block for an
307 * outstanding IO. By default this is 300 seconds at which point the "hung"
308 * behavior will be applied as described for zfs_deadman_synctime_ms.
310 unsigned long zfs_deadman_ziotime_ms
= 300000ULL;
313 * Check time in milliseconds. This defines the frequency at which we check
316 unsigned long zfs_deadman_checktime_ms
= 60000ULL;
319 * By default the deadman is enabled.
321 int zfs_deadman_enabled
= 1;
324 * Controls the behavior of the deadman when it detects a "hung" I/O.
325 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
327 * wait - Wait for the "hung" I/O (default)
328 * continue - Attempt to recover from a "hung" I/O
329 * panic - Panic the system
331 char *zfs_deadman_failmode
= "wait";
334 * The worst case is single-sector max-parity RAID-Z blocks, in which
335 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
336 * times the size; so just assume that. Add to this the fact that
337 * we can have up to 3 DVAs per bp, and one more factor of 2 because
338 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
340 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
342 int spa_asize_inflation
= 24;
345 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
346 * the pool to be consumed. This ensures that we don't run the pool
347 * completely out of space, due to unaccounted changes (e.g. to the MOS).
348 * It also limits the worst-case time to allocate space. If we have
349 * less than this amount of free space, most ZPL operations (e.g. write,
350 * create) will return ENOSPC.
352 * Certain operations (e.g. file removal, most administrative actions) can
353 * use half the slop space. They will only return ENOSPC if less than half
354 * the slop space is free. Typically, once the pool has less than the slop
355 * space free, the user will use these operations to free up space in the pool.
356 * These are the operations that call dsl_pool_adjustedsize() with the netfree
357 * argument set to TRUE.
359 * Operations that are almost guaranteed to free up space in the absence of
360 * a pool checkpoint can use up to three quarters of the slop space
363 * A very restricted set of operations are always permitted, regardless of
364 * the amount of free space. These are the operations that call
365 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
366 * increase in the amount of space used, it is possible to run the pool
367 * completely out of space, causing it to be permanently read-only.
369 * Note that on very small pools, the slop space will be larger than
370 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
371 * but we never allow it to be more than half the pool size.
373 * See also the comments in zfs_space_check_t.
375 int spa_slop_shift
= 5;
376 uint64_t spa_min_slop
= 128 * 1024 * 1024;
377 int spa_allocators
= 4;
382 spa_load_failed(spa_t
*spa
, const char *fmt
, ...)
388 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
391 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa
->spa_name
,
392 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
397 spa_load_note(spa_t
*spa
, const char *fmt
, ...)
403 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
406 zfs_dbgmsg("spa_load(%s, config %s): %s", spa
->spa_name
,
407 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
411 * By default dedup and user data indirects land in the special class
413 int zfs_ddt_data_is_special
= B_TRUE
;
414 int zfs_user_indirect_is_special
= B_TRUE
;
417 * The percentage of special class final space reserved for metadata only.
418 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
419 * let metadata into the class.
421 int zfs_special_class_metadata_reserve_pct
= 25;
424 * ==========================================================================
426 * ==========================================================================
429 spa_config_lock_init(spa_t
*spa
)
431 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
432 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
433 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
434 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
435 zfs_refcount_create_untracked(&scl
->scl_count
);
436 scl
->scl_writer
= NULL
;
437 scl
->scl_write_wanted
= 0;
442 spa_config_lock_destroy(spa_t
*spa
)
444 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
445 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
446 mutex_destroy(&scl
->scl_lock
);
447 cv_destroy(&scl
->scl_cv
);
448 zfs_refcount_destroy(&scl
->scl_count
);
449 ASSERT(scl
->scl_writer
== NULL
);
450 ASSERT(scl
->scl_write_wanted
== 0);
455 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
457 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
458 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
459 if (!(locks
& (1 << i
)))
461 mutex_enter(&scl
->scl_lock
);
462 if (rw
== RW_READER
) {
463 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
464 mutex_exit(&scl
->scl_lock
);
465 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
470 ASSERT(scl
->scl_writer
!= curthread
);
471 if (!zfs_refcount_is_zero(&scl
->scl_count
)) {
472 mutex_exit(&scl
->scl_lock
);
473 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
477 scl
->scl_writer
= curthread
;
479 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
480 mutex_exit(&scl
->scl_lock
);
486 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
490 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
492 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
493 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
494 if (scl
->scl_writer
== curthread
)
495 wlocks_held
|= (1 << i
);
496 if (!(locks
& (1 << i
)))
498 mutex_enter(&scl
->scl_lock
);
499 if (rw
== RW_READER
) {
500 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
501 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
504 ASSERT(scl
->scl_writer
!= curthread
);
505 while (!zfs_refcount_is_zero(&scl
->scl_count
)) {
506 scl
->scl_write_wanted
++;
507 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
508 scl
->scl_write_wanted
--;
510 scl
->scl_writer
= curthread
;
512 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
513 mutex_exit(&scl
->scl_lock
);
515 ASSERT3U(wlocks_held
, <=, locks
);
519 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
521 for (int i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
522 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
523 if (!(locks
& (1 << i
)))
525 mutex_enter(&scl
->scl_lock
);
526 ASSERT(!zfs_refcount_is_zero(&scl
->scl_count
));
527 if (zfs_refcount_remove(&scl
->scl_count
, tag
) == 0) {
528 ASSERT(scl
->scl_writer
== NULL
||
529 scl
->scl_writer
== curthread
);
530 scl
->scl_writer
= NULL
; /* OK in either case */
531 cv_broadcast(&scl
->scl_cv
);
533 mutex_exit(&scl
->scl_lock
);
538 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
542 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
543 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
544 if (!(locks
& (1 << i
)))
546 if ((rw
== RW_READER
&&
547 !zfs_refcount_is_zero(&scl
->scl_count
)) ||
548 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
549 locks_held
|= 1 << i
;
556 * ==========================================================================
557 * SPA namespace functions
558 * ==========================================================================
562 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
563 * Returns NULL if no matching spa_t is found.
566 spa_lookup(const char *name
)
568 static spa_t search
; /* spa_t is large; don't allocate on stack */
573 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
575 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
578 * If it's a full dataset name, figure out the pool name and
581 cp
= strpbrk(search
.spa_name
, "/@#");
585 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
591 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
592 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
593 * looking for potentially hung I/Os.
596 spa_deadman(void *arg
)
600 /* Disable the deadman if the pool is suspended. */
601 if (spa_suspended(spa
))
604 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
605 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
606 ++spa
->spa_deadman_calls
);
607 if (zfs_deadman_enabled
)
608 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
610 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
611 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
612 MSEC_TO_TICK(zfs_deadman_checktime_ms
));
616 * Create an uninitialized spa_t with the given name. Requires
617 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
618 * exist by calling spa_lookup() first.
621 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
624 spa_config_dirent_t
*dp
;
626 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
628 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
630 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
631 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
632 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
633 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
634 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
635 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
636 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
637 mutex_init(&spa
->spa_cksum_tmpls_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
638 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
639 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
640 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
641 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
643 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
644 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
645 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
646 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
647 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
649 for (int t
= 0; t
< TXG_SIZE
; t
++)
650 bplist_create(&spa
->spa_free_bplist
[t
]);
652 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
653 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
654 spa
->spa_freeze_txg
= UINT64_MAX
;
655 spa
->spa_final_txg
= UINT64_MAX
;
656 spa
->spa_load_max_txg
= UINT64_MAX
;
658 spa
->spa_proc_state
= SPA_PROC_NONE
;
659 spa
->spa_trust_config
= B_TRUE
;
661 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
662 spa
->spa_deadman_ziotime
= MSEC2NSEC(zfs_deadman_ziotime_ms
);
663 spa_set_deadman_failmode(spa
, zfs_deadman_failmode
);
665 zfs_refcount_create(&spa
->spa_refcount
);
666 spa_config_lock_init(spa
);
669 avl_add(&spa_namespace_avl
, spa
);
672 * Set the alternate root, if there is one.
675 spa
->spa_root
= spa_strdup(altroot
);
677 spa
->spa_alloc_count
= spa_allocators
;
678 spa
->spa_alloc_locks
= kmem_zalloc(spa
->spa_alloc_count
*
679 sizeof (kmutex_t
), KM_SLEEP
);
680 spa
->spa_alloc_trees
= kmem_zalloc(spa
->spa_alloc_count
*
681 sizeof (avl_tree_t
), KM_SLEEP
);
682 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
683 mutex_init(&spa
->spa_alloc_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
684 avl_create(&spa
->spa_alloc_trees
[i
], zio_bookmark_compare
,
685 sizeof (zio_t
), offsetof(zio_t
, io_alloc_node
));
689 * Every pool starts with the default cachefile
691 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
692 offsetof(spa_config_dirent_t
, scd_link
));
694 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
695 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
696 list_insert_head(&spa
->spa_config_list
, dp
);
698 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
701 if (config
!= NULL
) {
704 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
706 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
710 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
713 if (spa
->spa_label_features
== NULL
) {
714 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
718 spa
->spa_min_ashift
= INT_MAX
;
719 spa
->spa_max_ashift
= 0;
721 /* Reset cached value */
722 spa
->spa_dedup_dspace
= ~0ULL;
725 * As a pool is being created, treat all features as disabled by
726 * setting SPA_FEATURE_DISABLED for all entries in the feature
729 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
730 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
737 * Removes a spa_t from the namespace, freeing up any memory used. Requires
738 * spa_namespace_lock. This is called only after the spa_t has been closed and
742 spa_remove(spa_t
*spa
)
744 spa_config_dirent_t
*dp
;
746 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
747 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
748 ASSERT3U(zfs_refcount_count(&spa
->spa_refcount
), ==, 0);
750 nvlist_free(spa
->spa_config_splitting
);
752 avl_remove(&spa_namespace_avl
, spa
);
753 cv_broadcast(&spa_namespace_cv
);
756 spa_strfree(spa
->spa_root
);
758 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
759 list_remove(&spa
->spa_config_list
, dp
);
760 if (dp
->scd_path
!= NULL
)
761 spa_strfree(dp
->scd_path
);
762 kmem_free(dp
, sizeof (spa_config_dirent_t
));
765 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
766 avl_destroy(&spa
->spa_alloc_trees
[i
]);
767 mutex_destroy(&spa
->spa_alloc_locks
[i
]);
769 kmem_free(spa
->spa_alloc_locks
, spa
->spa_alloc_count
*
771 kmem_free(spa
->spa_alloc_trees
, spa
->spa_alloc_count
*
772 sizeof (avl_tree_t
));
774 list_destroy(&spa
->spa_config_list
);
776 nvlist_free(spa
->spa_label_features
);
777 nvlist_free(spa
->spa_load_info
);
778 nvlist_free(spa
->spa_feat_stats
);
779 spa_config_set(spa
, NULL
);
781 zfs_refcount_destroy(&spa
->spa_refcount
);
783 spa_stats_destroy(spa
);
784 spa_config_lock_destroy(spa
);
786 for (int t
= 0; t
< TXG_SIZE
; t
++)
787 bplist_destroy(&spa
->spa_free_bplist
[t
]);
789 zio_checksum_templates_free(spa
);
791 cv_destroy(&spa
->spa_async_cv
);
792 cv_destroy(&spa
->spa_evicting_os_cv
);
793 cv_destroy(&spa
->spa_proc_cv
);
794 cv_destroy(&spa
->spa_scrub_io_cv
);
795 cv_destroy(&spa
->spa_suspend_cv
);
797 mutex_destroy(&spa
->spa_async_lock
);
798 mutex_destroy(&spa
->spa_errlist_lock
);
799 mutex_destroy(&spa
->spa_errlog_lock
);
800 mutex_destroy(&spa
->spa_evicting_os_lock
);
801 mutex_destroy(&spa
->spa_history_lock
);
802 mutex_destroy(&spa
->spa_proc_lock
);
803 mutex_destroy(&spa
->spa_props_lock
);
804 mutex_destroy(&spa
->spa_cksum_tmpls_lock
);
805 mutex_destroy(&spa
->spa_scrub_lock
);
806 mutex_destroy(&spa
->spa_suspend_lock
);
807 mutex_destroy(&spa
->spa_vdev_top_lock
);
808 mutex_destroy(&spa
->spa_feat_stats_lock
);
810 kmem_free(spa
, sizeof (spa_t
));
814 * Given a pool, return the next pool in the namespace, or NULL if there is
815 * none. If 'prev' is NULL, return the first pool.
818 spa_next(spa_t
*prev
)
820 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
823 return (AVL_NEXT(&spa_namespace_avl
, prev
));
825 return (avl_first(&spa_namespace_avl
));
829 * ==========================================================================
830 * SPA refcount functions
831 * ==========================================================================
835 * Add a reference to the given spa_t. Must have at least one reference, or
836 * have the namespace lock held.
839 spa_open_ref(spa_t
*spa
, void *tag
)
841 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
842 MUTEX_HELD(&spa_namespace_lock
));
843 (void) zfs_refcount_add(&spa
->spa_refcount
, tag
);
847 * Remove a reference to the given spa_t. Must have at least one reference, or
848 * have the namespace lock held.
851 spa_close(spa_t
*spa
, void *tag
)
853 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
854 MUTEX_HELD(&spa_namespace_lock
));
855 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
859 * Remove a reference to the given spa_t held by a dsl dir that is
860 * being asynchronously released. Async releases occur from a taskq
861 * performing eviction of dsl datasets and dirs. The namespace lock
862 * isn't held and the hold by the object being evicted may contribute to
863 * spa_minref (e.g. dataset or directory released during pool export),
864 * so the asserts in spa_close() do not apply.
867 spa_async_close(spa_t
*spa
, void *tag
)
869 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
873 * Check to see if the spa refcount is zero. Must be called with
874 * spa_namespace_lock held. We really compare against spa_minref, which is the
875 * number of references acquired when opening a pool
878 spa_refcount_zero(spa_t
*spa
)
880 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
882 return (zfs_refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
886 * ==========================================================================
887 * SPA spare and l2cache tracking
888 * ==========================================================================
892 * Hot spares and cache devices are tracked using the same code below,
893 * for 'auxiliary' devices.
896 typedef struct spa_aux
{
904 spa_aux_compare(const void *a
, const void *b
)
906 const spa_aux_t
*sa
= (const spa_aux_t
*)a
;
907 const spa_aux_t
*sb
= (const spa_aux_t
*)b
;
909 return (AVL_CMP(sa
->aux_guid
, sb
->aux_guid
));
913 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
919 search
.aux_guid
= vd
->vdev_guid
;
920 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
923 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
924 aux
->aux_guid
= vd
->vdev_guid
;
926 avl_insert(avl
, aux
, where
);
931 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
937 search
.aux_guid
= vd
->vdev_guid
;
938 aux
= avl_find(avl
, &search
, &where
);
942 if (--aux
->aux_count
== 0) {
943 avl_remove(avl
, aux
);
944 kmem_free(aux
, sizeof (spa_aux_t
));
945 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
946 aux
->aux_pool
= 0ULL;
951 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
953 spa_aux_t search
, *found
;
955 search
.aux_guid
= guid
;
956 found
= avl_find(avl
, &search
, NULL
);
960 *pool
= found
->aux_pool
;
967 *refcnt
= found
->aux_count
;
972 return (found
!= NULL
);
976 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
978 spa_aux_t search
, *found
;
981 search
.aux_guid
= vd
->vdev_guid
;
982 found
= avl_find(avl
, &search
, &where
);
983 ASSERT(found
!= NULL
);
984 ASSERT(found
->aux_pool
== 0ULL);
986 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
990 * Spares are tracked globally due to the following constraints:
992 * - A spare may be part of multiple pools.
993 * - A spare may be added to a pool even if it's actively in use within
995 * - A spare in use in any pool can only be the source of a replacement if
996 * the target is a spare in the same pool.
998 * We keep track of all spares on the system through the use of a reference
999 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1000 * spare, then we bump the reference count in the AVL tree. In addition, we set
1001 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1002 * inactive). When a spare is made active (used to replace a device in the
1003 * pool), we also keep track of which pool its been made a part of.
1005 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1006 * called under the spa_namespace lock as part of vdev reconfiguration. The
1007 * separate spare lock exists for the status query path, which does not need to
1008 * be completely consistent with respect to other vdev configuration changes.
1012 spa_spare_compare(const void *a
, const void *b
)
1014 return (spa_aux_compare(a
, b
));
1018 spa_spare_add(vdev_t
*vd
)
1020 mutex_enter(&spa_spare_lock
);
1021 ASSERT(!vd
->vdev_isspare
);
1022 spa_aux_add(vd
, &spa_spare_avl
);
1023 vd
->vdev_isspare
= B_TRUE
;
1024 mutex_exit(&spa_spare_lock
);
1028 spa_spare_remove(vdev_t
*vd
)
1030 mutex_enter(&spa_spare_lock
);
1031 ASSERT(vd
->vdev_isspare
);
1032 spa_aux_remove(vd
, &spa_spare_avl
);
1033 vd
->vdev_isspare
= B_FALSE
;
1034 mutex_exit(&spa_spare_lock
);
1038 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
1042 mutex_enter(&spa_spare_lock
);
1043 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
1044 mutex_exit(&spa_spare_lock
);
1050 spa_spare_activate(vdev_t
*vd
)
1052 mutex_enter(&spa_spare_lock
);
1053 ASSERT(vd
->vdev_isspare
);
1054 spa_aux_activate(vd
, &spa_spare_avl
);
1055 mutex_exit(&spa_spare_lock
);
1059 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1060 * Cache devices currently only support one pool per cache device, and so
1061 * for these devices the aux reference count is currently unused beyond 1.
1065 spa_l2cache_compare(const void *a
, const void *b
)
1067 return (spa_aux_compare(a
, b
));
1071 spa_l2cache_add(vdev_t
*vd
)
1073 mutex_enter(&spa_l2cache_lock
);
1074 ASSERT(!vd
->vdev_isl2cache
);
1075 spa_aux_add(vd
, &spa_l2cache_avl
);
1076 vd
->vdev_isl2cache
= B_TRUE
;
1077 mutex_exit(&spa_l2cache_lock
);
1081 spa_l2cache_remove(vdev_t
*vd
)
1083 mutex_enter(&spa_l2cache_lock
);
1084 ASSERT(vd
->vdev_isl2cache
);
1085 spa_aux_remove(vd
, &spa_l2cache_avl
);
1086 vd
->vdev_isl2cache
= B_FALSE
;
1087 mutex_exit(&spa_l2cache_lock
);
1091 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1095 mutex_enter(&spa_l2cache_lock
);
1096 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1097 mutex_exit(&spa_l2cache_lock
);
1103 spa_l2cache_activate(vdev_t
*vd
)
1105 mutex_enter(&spa_l2cache_lock
);
1106 ASSERT(vd
->vdev_isl2cache
);
1107 spa_aux_activate(vd
, &spa_l2cache_avl
);
1108 mutex_exit(&spa_l2cache_lock
);
1112 * ==========================================================================
1114 * ==========================================================================
1118 * Lock the given spa_t for the purpose of adding or removing a vdev.
1119 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1120 * It returns the next transaction group for the spa_t.
1123 spa_vdev_enter(spa_t
*spa
)
1125 mutex_enter(&spa
->spa_vdev_top_lock
);
1126 mutex_enter(&spa_namespace_lock
);
1127 return (spa_vdev_config_enter(spa
));
1131 * Internal implementation for spa_vdev_enter(). Used when a vdev
1132 * operation requires multiple syncs (i.e. removing a device) while
1133 * keeping the spa_namespace_lock held.
1136 spa_vdev_config_enter(spa_t
*spa
)
1138 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1140 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1142 return (spa_last_synced_txg(spa
) + 1);
1146 * Used in combination with spa_vdev_config_enter() to allow the syncing
1147 * of multiple transactions without releasing the spa_namespace_lock.
1150 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1152 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1154 int config_changed
= B_FALSE
;
1156 ASSERT(txg
> spa_last_synced_txg(spa
));
1158 spa
->spa_pending_vdev
= NULL
;
1161 * Reassess the DTLs.
1163 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1165 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1166 config_changed
= B_TRUE
;
1167 spa
->spa_config_generation
++;
1171 * Verify the metaslab classes.
1173 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1174 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1175 ASSERT(metaslab_class_validate(spa_special_class(spa
)) == 0);
1176 ASSERT(metaslab_class_validate(spa_dedup_class(spa
)) == 0);
1178 spa_config_exit(spa
, SCL_ALL
, spa
);
1181 * Panic the system if the specified tag requires it. This
1182 * is useful for ensuring that configurations are updated
1185 if (zio_injection_enabled
)
1186 zio_handle_panic_injection(spa
, tag
, 0);
1189 * Note: this txg_wait_synced() is important because it ensures
1190 * that there won't be more than one config change per txg.
1191 * This allows us to use the txg as the generation number.
1194 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1197 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1198 if (vd
->vdev_ops
->vdev_op_leaf
) {
1199 mutex_enter(&vd
->vdev_initialize_lock
);
1200 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
,
1202 mutex_exit(&vd
->vdev_initialize_lock
);
1205 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1207 spa_config_exit(spa
, SCL_ALL
, spa
);
1211 * If the config changed, update the config cache.
1214 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1218 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1219 * locking of spa_vdev_enter(), we also want make sure the transactions have
1220 * synced to disk, and then update the global configuration cache with the new
1224 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1226 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1227 mutex_exit(&spa_namespace_lock
);
1228 mutex_exit(&spa
->spa_vdev_top_lock
);
1234 * Lock the given spa_t for the purpose of changing vdev state.
1237 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1239 int locks
= SCL_STATE_ALL
| oplocks
;
1242 * Root pools may need to read of the underlying devfs filesystem
1243 * when opening up a vdev. Unfortunately if we're holding the
1244 * SCL_ZIO lock it will result in a deadlock when we try to issue
1245 * the read from the root filesystem. Instead we "prefetch"
1246 * the associated vnodes that we need prior to opening the
1247 * underlying devices and cache them so that we can prevent
1248 * any I/O when we are doing the actual open.
1250 if (spa_is_root(spa
)) {
1251 int low
= locks
& ~(SCL_ZIO
- 1);
1252 int high
= locks
& ~low
;
1254 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1255 vdev_hold(spa
->spa_root_vdev
);
1256 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1258 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1260 spa
->spa_vdev_locks
= locks
;
1264 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1266 boolean_t config_changed
= B_FALSE
;
1269 if (vd
== NULL
|| vd
== spa
->spa_root_vdev
) {
1270 vdev_top
= spa
->spa_root_vdev
;
1272 vdev_top
= vd
->vdev_top
;
1275 if (vd
!= NULL
|| error
== 0)
1276 vdev_dtl_reassess(vdev_top
, 0, 0, B_FALSE
);
1279 if (vd
!= spa
->spa_root_vdev
)
1280 vdev_state_dirty(vdev_top
);
1282 config_changed
= B_TRUE
;
1283 spa
->spa_config_generation
++;
1286 if (spa_is_root(spa
))
1287 vdev_rele(spa
->spa_root_vdev
);
1289 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1290 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1293 * If anything changed, wait for it to sync. This ensures that,
1294 * from the system administrator's perspective, zpool(1M) commands
1295 * are synchronous. This is important for things like zpool offline:
1296 * when the command completes, you expect no further I/O from ZFS.
1299 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1302 * If the config changed, update the config cache.
1304 if (config_changed
) {
1305 mutex_enter(&spa_namespace_lock
);
1306 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1307 mutex_exit(&spa_namespace_lock
);
1314 * ==========================================================================
1315 * Miscellaneous functions
1316 * ==========================================================================
1320 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1322 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1323 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1325 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1326 * dirty the vdev config because lock SCL_CONFIG is not held.
1327 * Thankfully, in this case we don't need to dirty the config
1328 * because it will be written out anyway when we finish
1329 * creating the pool.
1331 if (tx
->tx_txg
!= TXG_INITIAL
)
1332 vdev_config_dirty(spa
->spa_root_vdev
);
1337 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1339 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1340 vdev_config_dirty(spa
->spa_root_vdev
);
1344 * Return the spa_t associated with given pool_guid, if it exists. If
1345 * device_guid is non-zero, determine whether the pool exists *and* contains
1346 * a device with the specified device_guid.
1349 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1352 avl_tree_t
*t
= &spa_namespace_avl
;
1354 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1356 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1357 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1359 if (spa
->spa_root_vdev
== NULL
)
1361 if (spa_guid(spa
) == pool_guid
) {
1362 if (device_guid
== 0)
1365 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1366 device_guid
) != NULL
)
1370 * Check any devices we may be in the process of adding.
1372 if (spa
->spa_pending_vdev
) {
1373 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1374 device_guid
) != NULL
)
1384 * Determine whether a pool with the given pool_guid exists.
1387 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1389 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1393 spa_strdup(const char *s
)
1399 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1407 spa_strfree(char *s
)
1409 kmem_free(s
, strlen(s
) + 1);
1413 spa_get_random(uint64_t range
)
1422 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1428 spa_generate_guid(spa_t
*spa
)
1430 uint64_t guid
= spa_get_random(-1ULL);
1433 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1434 guid
= spa_get_random(-1ULL);
1436 while (guid
== 0 || spa_guid_exists(guid
, 0))
1437 guid
= spa_get_random(-1ULL);
1444 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1447 char *checksum
= NULL
;
1448 char *compress
= NULL
;
1451 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1452 dmu_object_byteswap_t bswap
=
1453 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1454 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1455 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1456 "metadata" : "data",
1457 dmu_ot_byteswap
[bswap
].ob_name
);
1459 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1462 if (!BP_IS_EMBEDDED(bp
)) {
1464 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1466 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1469 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1474 spa_freeze(spa_t
*spa
)
1476 uint64_t freeze_txg
= 0;
1478 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1479 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1480 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1481 spa
->spa_freeze_txg
= freeze_txg
;
1483 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1484 if (freeze_txg
!= 0)
1485 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1489 zfs_panic_recover(const char *fmt
, ...)
1494 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1499 * This is a stripped-down version of strtoull, suitable only for converting
1500 * lowercase hexadecimal numbers that don't overflow.
1503 zfs_strtonum(const char *str
, char **nptr
)
1509 while ((c
= *str
) != '\0') {
1510 if (c
>= '0' && c
<= '9')
1512 else if (c
>= 'a' && c
<= 'f')
1513 digit
= 10 + c
- 'a';
1524 *nptr
= (char *)str
;
1530 spa_activate_allocation_classes(spa_t
*spa
, dmu_tx_t
*tx
)
1533 * We bump the feature refcount for each special vdev added to the pool
1535 ASSERT(spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
));
1536 spa_feature_incr(spa
, SPA_FEATURE_ALLOCATION_CLASSES
, tx
);
1540 * ==========================================================================
1541 * Accessor functions
1542 * ==========================================================================
1546 spa_shutting_down(spa_t
*spa
)
1548 return (spa
->spa_async_suspended
);
1552 spa_get_dsl(spa_t
*spa
)
1554 return (spa
->spa_dsl_pool
);
1558 spa_is_initializing(spa_t
*spa
)
1560 return (spa
->spa_is_initializing
);
1564 spa_indirect_vdevs_loaded(spa_t
*spa
)
1566 return (spa
->spa_indirect_vdevs_loaded
);
1570 spa_get_rootblkptr(spa_t
*spa
)
1572 return (&spa
->spa_ubsync
.ub_rootbp
);
1576 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1578 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1582 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1584 if (spa
->spa_root
== NULL
)
1587 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1591 spa_sync_pass(spa_t
*spa
)
1593 return (spa
->spa_sync_pass
);
1597 spa_name(spa_t
*spa
)
1599 return (spa
->spa_name
);
1603 spa_guid(spa_t
*spa
)
1605 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1609 * If we fail to parse the config during spa_load(), we can go through
1610 * the error path (which posts an ereport) and end up here with no root
1611 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1614 if (spa
->spa_root_vdev
== NULL
)
1615 return (spa
->spa_config_guid
);
1617 guid
= spa
->spa_last_synced_guid
!= 0 ?
1618 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1621 * Return the most recently synced out guid unless we're
1622 * in syncing context.
1624 if (dp
&& dsl_pool_sync_context(dp
))
1625 return (spa
->spa_root_vdev
->vdev_guid
);
1631 spa_load_guid(spa_t
*spa
)
1634 * This is a GUID that exists solely as a reference for the
1635 * purposes of the arc. It is generated at load time, and
1636 * is never written to persistent storage.
1638 return (spa
->spa_load_guid
);
1642 spa_last_synced_txg(spa_t
*spa
)
1644 return (spa
->spa_ubsync
.ub_txg
);
1648 spa_first_txg(spa_t
*spa
)
1650 return (spa
->spa_first_txg
);
1654 spa_syncing_txg(spa_t
*spa
)
1656 return (spa
->spa_syncing_txg
);
1660 * Return the last txg where data can be dirtied. The final txgs
1661 * will be used to just clear out any deferred frees that remain.
1664 spa_final_dirty_txg(spa_t
*spa
)
1666 return (spa
->spa_final_txg
- TXG_DEFER_SIZE
);
1670 spa_state(spa_t
*spa
)
1672 return (spa
->spa_state
);
1676 spa_load_state(spa_t
*spa
)
1678 return (spa
->spa_load_state
);
1682 spa_freeze_txg(spa_t
*spa
)
1684 return (spa
->spa_freeze_txg
);
1688 * Return the inflated asize for a logical write in bytes. This is used by the
1689 * DMU to calculate the space a logical write will require on disk.
1690 * If lsize is smaller than the largest physical block size allocatable on this
1691 * pool we use its value instead, since the write will end up using the whole
1695 spa_get_worst_case_asize(spa_t
*spa
, uint64_t lsize
)
1698 return (0); /* No inflation needed */
1699 return (MAX(lsize
, 1 << spa
->spa_max_ashift
) * spa_asize_inflation
);
1703 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1704 * or at least 128MB, unless that would cause it to be more than half the
1707 * See the comment above spa_slop_shift for details.
1710 spa_get_slop_space(spa_t
*spa
)
1712 uint64_t space
= spa_get_dspace(spa
);
1713 return (MAX(space
>> spa_slop_shift
, MIN(space
>> 1, spa_min_slop
)));
1717 spa_get_dspace(spa_t
*spa
)
1719 return (spa
->spa_dspace
);
1723 spa_get_checkpoint_space(spa_t
*spa
)
1725 return (spa
->spa_checkpoint_info
.sci_dspace
);
1729 spa_update_dspace(spa_t
*spa
)
1731 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1732 ddt_get_dedup_dspace(spa
);
1733 if (spa
->spa_vdev_removal
!= NULL
) {
1735 * We can't allocate from the removing device, so
1736 * subtract its size. This prevents the DMU/DSL from
1737 * filling up the (now smaller) pool while we are in the
1738 * middle of removing the device.
1740 * Note that the DMU/DSL doesn't actually know or care
1741 * how much space is allocated (it does its own tracking
1742 * of how much space has been logically used). So it
1743 * doesn't matter that the data we are moving may be
1744 * allocated twice (on the old device and the new
1747 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1749 vdev_lookup_top(spa
, spa
->spa_vdev_removal
->svr_vdev_id
);
1750 spa
->spa_dspace
-= spa_deflate(spa
) ?
1751 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1752 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1757 * Return the failure mode that has been set to this pool. The default
1758 * behavior will be to block all I/Os when a complete failure occurs.
1761 spa_get_failmode(spa_t
*spa
)
1763 return (spa
->spa_failmode
);
1767 spa_suspended(spa_t
*spa
)
1769 return (spa
->spa_suspended
!= ZIO_SUSPEND_NONE
);
1773 spa_version(spa_t
*spa
)
1775 return (spa
->spa_ubsync
.ub_version
);
1779 spa_deflate(spa_t
*spa
)
1781 return (spa
->spa_deflate
);
1785 spa_normal_class(spa_t
*spa
)
1787 return (spa
->spa_normal_class
);
1791 spa_log_class(spa_t
*spa
)
1793 return (spa
->spa_log_class
);
1797 spa_special_class(spa_t
*spa
)
1799 return (spa
->spa_special_class
);
1803 spa_dedup_class(spa_t
*spa
)
1805 return (spa
->spa_dedup_class
);
1809 * Locate an appropriate allocation class
1812 spa_preferred_class(spa_t
*spa
, uint64_t size
, dmu_object_type_t objtype
,
1813 uint_t level
, uint_t special_smallblk
)
1815 if (DMU_OT_IS_ZIL(objtype
)) {
1816 if (spa
->spa_log_class
->mc_groups
!= 0)
1817 return (spa_log_class(spa
));
1819 return (spa_normal_class(spa
));
1822 boolean_t has_special_class
= spa
->spa_special_class
->mc_groups
!= 0;
1824 if (DMU_OT_IS_DDT(objtype
)) {
1825 if (spa
->spa_dedup_class
->mc_groups
!= 0)
1826 return (spa_dedup_class(spa
));
1827 else if (has_special_class
&& zfs_ddt_data_is_special
)
1828 return (spa_special_class(spa
));
1830 return (spa_normal_class(spa
));
1833 /* Indirect blocks for user data can land in special if allowed */
1834 if (level
> 0 && (DMU_OT_IS_FILE(objtype
) || objtype
== DMU_OT_ZVOL
)) {
1835 if (has_special_class
&& zfs_user_indirect_is_special
)
1836 return (spa_special_class(spa
));
1838 return (spa_normal_class(spa
));
1841 if (DMU_OT_IS_METADATA(objtype
) || level
> 0) {
1842 if (has_special_class
)
1843 return (spa_special_class(spa
));
1845 return (spa_normal_class(spa
));
1849 * Allow small file blocks in special class in some cases (like
1850 * for the dRAID vdev feature). But always leave a reserve of
1851 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1853 if (DMU_OT_IS_FILE(objtype
) &&
1854 has_special_class
&& size
< special_smallblk
) {
1855 metaslab_class_t
*special
= spa_special_class(spa
);
1856 uint64_t alloc
= metaslab_class_get_alloc(special
);
1857 uint64_t space
= metaslab_class_get_space(special
);
1859 (space
* (100 - zfs_special_class_metadata_reserve_pct
))
1866 return (spa_normal_class(spa
));
1870 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1872 mutex_enter(&spa
->spa_evicting_os_lock
);
1873 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1874 mutex_exit(&spa
->spa_evicting_os_lock
);
1878 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1880 mutex_enter(&spa
->spa_evicting_os_lock
);
1881 list_remove(&spa
->spa_evicting_os_list
, os
);
1882 cv_broadcast(&spa
->spa_evicting_os_cv
);
1883 mutex_exit(&spa
->spa_evicting_os_lock
);
1887 spa_evicting_os_wait(spa_t
*spa
)
1889 mutex_enter(&spa
->spa_evicting_os_lock
);
1890 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1891 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1892 mutex_exit(&spa
->spa_evicting_os_lock
);
1894 dmu_buf_user_evict_wait();
1898 spa_max_replication(spa_t
*spa
)
1901 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1902 * handle BPs with more than one DVA allocated. Set our max
1903 * replication level accordingly.
1905 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1907 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1911 spa_prev_software_version(spa_t
*spa
)
1913 return (spa
->spa_prev_software_version
);
1917 spa_deadman_synctime(spa_t
*spa
)
1919 return (spa
->spa_deadman_synctime
);
1923 spa_deadman_ziotime(spa_t
*spa
)
1925 return (spa
->spa_deadman_ziotime
);
1929 spa_get_deadman_failmode(spa_t
*spa
)
1931 return (spa
->spa_deadman_failmode
);
1935 spa_set_deadman_failmode(spa_t
*spa
, const char *failmode
)
1937 if (strcmp(failmode
, "wait") == 0)
1938 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1939 else if (strcmp(failmode
, "continue") == 0)
1940 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
1941 else if (strcmp(failmode
, "panic") == 0)
1942 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
1944 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1948 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1950 uint64_t asize
= DVA_GET_ASIZE(dva
);
1951 uint64_t dsize
= asize
;
1953 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1955 if (asize
!= 0 && spa
->spa_deflate
) {
1956 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1958 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
1959 vd
->vdev_deflate_ratio
;
1966 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
1970 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1971 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1977 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
1981 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1983 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
1984 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
1986 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1992 spa_dirty_data(spa_t
*spa
)
1994 return (spa
->spa_dsl_pool
->dp_dirty_total
);
1998 * ==========================================================================
1999 * Initialization and Termination
2000 * ==========================================================================
2004 spa_name_compare(const void *a1
, const void *a2
)
2006 const spa_t
*s1
= a1
;
2007 const spa_t
*s2
= a2
;
2010 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
2012 return (AVL_ISIGN(s
));
2024 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2025 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2026 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2027 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
2029 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
2030 offsetof(spa_t
, spa_avl
));
2032 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
2033 offsetof(spa_aux_t
, aux_avl
));
2035 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
2036 offsetof(spa_aux_t
, aux_avl
));
2038 spa_mode_global
= mode
;
2041 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
2042 struct sigaction sa
;
2044 sa
.sa_flags
= SA_SIGINFO
;
2045 sigemptyset(&sa
.sa_mask
);
2046 sa
.sa_sigaction
= arc_buf_sigsegv
;
2048 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
2049 perror("could not enable watchpoints: "
2050 "sigaction(SIGSEGV, ...) = ");
2058 zfs_refcount_init();
2061 metaslab_alloc_trace_init();
2066 vdev_cache_stat_init();
2067 vdev_mirror_stat_init();
2068 vdev_raidz_math_init();
2072 zpool_feature_init();
2087 vdev_cache_stat_fini();
2088 vdev_mirror_stat_fini();
2089 vdev_raidz_math_fini();
2094 metaslab_alloc_trace_fini();
2097 zfs_refcount_fini();
2102 avl_destroy(&spa_namespace_avl
);
2103 avl_destroy(&spa_spare_avl
);
2104 avl_destroy(&spa_l2cache_avl
);
2106 cv_destroy(&spa_namespace_cv
);
2107 mutex_destroy(&spa_namespace_lock
);
2108 mutex_destroy(&spa_spare_lock
);
2109 mutex_destroy(&spa_l2cache_lock
);
2113 * Return whether this pool has slogs. No locking needed.
2114 * It's not a problem if the wrong answer is returned as it's only for
2115 * performance and not correctness
2118 spa_has_slogs(spa_t
*spa
)
2120 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
2124 spa_get_log_state(spa_t
*spa
)
2126 return (spa
->spa_log_state
);
2130 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
2132 spa
->spa_log_state
= state
;
2136 spa_is_root(spa_t
*spa
)
2138 return (spa
->spa_is_root
);
2142 spa_writeable(spa_t
*spa
)
2144 return (!!(spa
->spa_mode
& FWRITE
) && spa
->spa_trust_config
);
2148 * Returns true if there is a pending sync task in any of the current
2149 * syncing txg, the current quiescing txg, or the current open txg.
2152 spa_has_pending_synctask(spa_t
*spa
)
2154 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
) ||
2155 !txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_early_sync_tasks
));
2159 spa_mode(spa_t
*spa
)
2161 return (spa
->spa_mode
);
2165 spa_bootfs(spa_t
*spa
)
2167 return (spa
->spa_bootfs
);
2171 spa_delegation(spa_t
*spa
)
2173 return (spa
->spa_delegation
);
2177 spa_meta_objset(spa_t
*spa
)
2179 return (spa
->spa_meta_objset
);
2183 spa_dedup_checksum(spa_t
*spa
)
2185 return (spa
->spa_dedup_checksum
);
2189 * Reset pool scan stat per scan pass (or reboot).
2192 spa_scan_stat_init(spa_t
*spa
)
2194 /* data not stored on disk */
2195 spa
->spa_scan_pass_start
= gethrestime_sec();
2196 if (dsl_scan_is_paused_scrub(spa
->spa_dsl_pool
->dp_scan
))
2197 spa
->spa_scan_pass_scrub_pause
= spa
->spa_scan_pass_start
;
2199 spa
->spa_scan_pass_scrub_pause
= 0;
2200 spa
->spa_scan_pass_scrub_spent_paused
= 0;
2201 spa
->spa_scan_pass_exam
= 0;
2202 spa
->spa_scan_pass_issued
= 0;
2203 vdev_scan_stat_init(spa
->spa_root_vdev
);
2207 * Get scan stats for zpool status reports
2210 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
2212 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
2214 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
2215 return (SET_ERROR(ENOENT
));
2216 bzero(ps
, sizeof (pool_scan_stat_t
));
2218 /* data stored on disk */
2219 ps
->pss_func
= scn
->scn_phys
.scn_func
;
2220 ps
->pss_state
= scn
->scn_phys
.scn_state
;
2221 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
2222 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
2223 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
2224 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
2225 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
2226 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
2227 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
2229 /* data not stored on disk */
2230 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
2231 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
2232 ps
->pss_pass_scrub_pause
= spa
->spa_scan_pass_scrub_pause
;
2233 ps
->pss_pass_scrub_spent_paused
= spa
->spa_scan_pass_scrub_spent_paused
;
2234 ps
->pss_pass_issued
= spa
->spa_scan_pass_issued
;
2236 scn
->scn_issued_before_pass
+ spa
->spa_scan_pass_issued
;
2242 spa_maxblocksize(spa_t
*spa
)
2244 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
2245 return (SPA_MAXBLOCKSIZE
);
2247 return (SPA_OLD_MAXBLOCKSIZE
);
2252 * Returns the txg that the last device removal completed. No indirect mappings
2253 * have been added since this txg.
2256 spa_get_last_removal_txg(spa_t
*spa
)
2259 uint64_t ret
= -1ULL;
2261 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2263 * sr_prev_indirect_vdev is only modified while holding all the
2264 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2267 vdevid
= spa
->spa_removing_phys
.sr_prev_indirect_vdev
;
2269 while (vdevid
!= -1ULL) {
2270 vdev_t
*vd
= vdev_lookup_top(spa
, vdevid
);
2271 vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
;
2273 ASSERT3P(vd
->vdev_ops
, ==, &vdev_indirect_ops
);
2276 * If the removal did not remap any data, we don't care.
2278 if (vdev_indirect_births_count(vib
) != 0) {
2279 ret
= vdev_indirect_births_last_entry_txg(vib
);
2283 vdevid
= vd
->vdev_indirect_config
.vic_prev_indirect_vdev
;
2285 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2288 spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
2294 spa_maxdnodesize(spa_t
*spa
)
2296 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2297 return (DNODE_MAX_SIZE
);
2299 return (DNODE_MIN_SIZE
);
2303 spa_multihost(spa_t
*spa
)
2305 return (spa
->spa_multihost
? B_TRUE
: B_FALSE
);
2309 spa_get_hostid(void)
2311 unsigned long myhostid
;
2314 myhostid
= zone_get_hostid(NULL
);
2317 * We're emulating the system's hostid in userland, so
2318 * we can't use zone_get_hostid().
2320 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2321 #endif /* _KERNEL */
2327 spa_trust_config(spa_t
*spa
)
2329 return (spa
->spa_trust_config
);
2333 spa_missing_tvds_allowed(spa_t
*spa
)
2335 return (spa
->spa_missing_tvds_allowed
);
2339 spa_set_missing_tvds(spa_t
*spa
, uint64_t missing
)
2341 spa
->spa_missing_tvds
= missing
;
2345 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2348 spa_state_to_name(spa_t
*spa
)
2350 vdev_state_t state
= spa
->spa_root_vdev
->vdev_state
;
2351 vdev_aux_t aux
= spa
->spa_root_vdev
->vdev_stat
.vs_aux
;
2353 if (spa_suspended(spa
) &&
2354 (spa_get_failmode(spa
) != ZIO_FAILURE_MODE_CONTINUE
))
2355 return ("SUSPENDED");
2358 case VDEV_STATE_CLOSED
:
2359 case VDEV_STATE_OFFLINE
:
2361 case VDEV_STATE_REMOVED
:
2363 case VDEV_STATE_CANT_OPEN
:
2364 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
2366 else if (aux
== VDEV_AUX_SPLIT_POOL
)
2370 case VDEV_STATE_FAULTED
:
2372 case VDEV_STATE_DEGRADED
:
2373 return ("DEGRADED");
2374 case VDEV_STATE_HEALTHY
:
2384 spa_top_vdevs_spacemap_addressable(spa_t
*spa
)
2386 vdev_t
*rvd
= spa
->spa_root_vdev
;
2387 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2388 if (!vdev_is_spacemap_addressable(rvd
->vdev_child
[c
]))
2395 spa_has_checkpoint(spa_t
*spa
)
2397 return (spa
->spa_checkpoint_txg
!= 0);
2401 spa_importing_readonly_checkpoint(spa_t
*spa
)
2403 return ((spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
) &&
2404 spa
->spa_mode
== FREAD
);
2408 spa_min_claim_txg(spa_t
*spa
)
2410 uint64_t checkpoint_txg
= spa
->spa_uberblock
.ub_checkpoint_txg
;
2412 if (checkpoint_txg
!= 0)
2413 return (checkpoint_txg
+ 1);
2415 return (spa
->spa_first_txg
);
2419 * If there is a checkpoint, async destroys may consume more space from
2420 * the pool instead of freeing it. In an attempt to save the pool from
2421 * getting suspended when it is about to run out of space, we stop
2422 * processing async destroys.
2425 spa_suspend_async_destroy(spa_t
*spa
)
2427 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2429 uint64_t unreserved
= dsl_pool_unreserved_space(dp
,
2430 ZFS_SPACE_CHECK_EXTRA_RESERVED
);
2431 uint64_t used
= dsl_dir_phys(dp
->dp_root_dir
)->dd_used_bytes
;
2432 uint64_t avail
= (unreserved
> used
) ? (unreserved
- used
) : 0;
2434 if (spa_has_checkpoint(spa
) && avail
== 0)
2440 #if defined(_KERNEL)
2442 #include <linux/mod_compat.h>
2445 param_set_deadman_failmode(const char *val
, zfs_kernel_param_t
*kp
)
2451 return (SET_ERROR(-EINVAL
));
2453 if ((p
= strchr(val
, '\n')) != NULL
)
2456 if (strcmp(val
, "wait") != 0 && strcmp(val
, "continue") != 0 &&
2457 strcmp(val
, "panic"))
2458 return (SET_ERROR(-EINVAL
));
2460 if (spa_mode_global
!= 0) {
2461 mutex_enter(&spa_namespace_lock
);
2462 while ((spa
= spa_next(spa
)) != NULL
)
2463 spa_set_deadman_failmode(spa
, val
);
2464 mutex_exit(&spa_namespace_lock
);
2467 return (param_set_charp(val
, kp
));
2471 param_set_deadman_ziotime(const char *val
, zfs_kernel_param_t
*kp
)
2476 error
= param_set_ulong(val
, kp
);
2478 return (SET_ERROR(error
));
2480 if (spa_mode_global
!= 0) {
2481 mutex_enter(&spa_namespace_lock
);
2482 while ((spa
= spa_next(spa
)) != NULL
)
2483 spa
->spa_deadman_ziotime
=
2484 MSEC2NSEC(zfs_deadman_ziotime_ms
);
2485 mutex_exit(&spa_namespace_lock
);
2492 param_set_deadman_synctime(const char *val
, zfs_kernel_param_t
*kp
)
2497 error
= param_set_ulong(val
, kp
);
2499 return (SET_ERROR(error
));
2501 if (spa_mode_global
!= 0) {
2502 mutex_enter(&spa_namespace_lock
);
2503 while ((spa
= spa_next(spa
)) != NULL
)
2504 spa
->spa_deadman_synctime
=
2505 MSEC2NSEC(zfs_deadman_synctime_ms
);
2506 mutex_exit(&spa_namespace_lock
);
2513 param_set_slop_shift(const char *buf
, zfs_kernel_param_t
*kp
)
2518 error
= kstrtoul(buf
, 0, &val
);
2520 return (SET_ERROR(error
));
2522 if (val
< 1 || val
> 31)
2523 return (SET_ERROR(-EINVAL
));
2525 error
= param_set_int(buf
, kp
);
2527 return (SET_ERROR(error
));
2532 /* Namespace manipulation */
2533 EXPORT_SYMBOL(spa_lookup
);
2534 EXPORT_SYMBOL(spa_add
);
2535 EXPORT_SYMBOL(spa_remove
);
2536 EXPORT_SYMBOL(spa_next
);
2538 /* Refcount functions */
2539 EXPORT_SYMBOL(spa_open_ref
);
2540 EXPORT_SYMBOL(spa_close
);
2541 EXPORT_SYMBOL(spa_refcount_zero
);
2543 /* Pool configuration lock */
2544 EXPORT_SYMBOL(spa_config_tryenter
);
2545 EXPORT_SYMBOL(spa_config_enter
);
2546 EXPORT_SYMBOL(spa_config_exit
);
2547 EXPORT_SYMBOL(spa_config_held
);
2549 /* Pool vdev add/remove lock */
2550 EXPORT_SYMBOL(spa_vdev_enter
);
2551 EXPORT_SYMBOL(spa_vdev_exit
);
2553 /* Pool vdev state change lock */
2554 EXPORT_SYMBOL(spa_vdev_state_enter
);
2555 EXPORT_SYMBOL(spa_vdev_state_exit
);
2557 /* Accessor functions */
2558 EXPORT_SYMBOL(spa_shutting_down
);
2559 EXPORT_SYMBOL(spa_get_dsl
);
2560 EXPORT_SYMBOL(spa_get_rootblkptr
);
2561 EXPORT_SYMBOL(spa_set_rootblkptr
);
2562 EXPORT_SYMBOL(spa_altroot
);
2563 EXPORT_SYMBOL(spa_sync_pass
);
2564 EXPORT_SYMBOL(spa_name
);
2565 EXPORT_SYMBOL(spa_guid
);
2566 EXPORT_SYMBOL(spa_last_synced_txg
);
2567 EXPORT_SYMBOL(spa_first_txg
);
2568 EXPORT_SYMBOL(spa_syncing_txg
);
2569 EXPORT_SYMBOL(spa_version
);
2570 EXPORT_SYMBOL(spa_state
);
2571 EXPORT_SYMBOL(spa_load_state
);
2572 EXPORT_SYMBOL(spa_freeze_txg
);
2573 EXPORT_SYMBOL(spa_get_dspace
);
2574 EXPORT_SYMBOL(spa_update_dspace
);
2575 EXPORT_SYMBOL(spa_deflate
);
2576 EXPORT_SYMBOL(spa_normal_class
);
2577 EXPORT_SYMBOL(spa_log_class
);
2578 EXPORT_SYMBOL(spa_special_class
);
2579 EXPORT_SYMBOL(spa_preferred_class
);
2580 EXPORT_SYMBOL(spa_max_replication
);
2581 EXPORT_SYMBOL(spa_prev_software_version
);
2582 EXPORT_SYMBOL(spa_get_failmode
);
2583 EXPORT_SYMBOL(spa_suspended
);
2584 EXPORT_SYMBOL(spa_bootfs
);
2585 EXPORT_SYMBOL(spa_delegation
);
2586 EXPORT_SYMBOL(spa_meta_objset
);
2587 EXPORT_SYMBOL(spa_maxblocksize
);
2588 EXPORT_SYMBOL(spa_maxdnodesize
);
2590 /* Miscellaneous support routines */
2591 EXPORT_SYMBOL(spa_guid_exists
);
2592 EXPORT_SYMBOL(spa_strdup
);
2593 EXPORT_SYMBOL(spa_strfree
);
2594 EXPORT_SYMBOL(spa_get_random
);
2595 EXPORT_SYMBOL(spa_generate_guid
);
2596 EXPORT_SYMBOL(snprintf_blkptr
);
2597 EXPORT_SYMBOL(spa_freeze
);
2598 EXPORT_SYMBOL(spa_upgrade
);
2599 EXPORT_SYMBOL(spa_evict_all
);
2600 EXPORT_SYMBOL(spa_lookup_by_guid
);
2601 EXPORT_SYMBOL(spa_has_spare
);
2602 EXPORT_SYMBOL(dva_get_dsize_sync
);
2603 EXPORT_SYMBOL(bp_get_dsize_sync
);
2604 EXPORT_SYMBOL(bp_get_dsize
);
2605 EXPORT_SYMBOL(spa_has_slogs
);
2606 EXPORT_SYMBOL(spa_is_root
);
2607 EXPORT_SYMBOL(spa_writeable
);
2608 EXPORT_SYMBOL(spa_mode
);
2609 EXPORT_SYMBOL(spa_namespace_lock
);
2610 EXPORT_SYMBOL(spa_trust_config
);
2611 EXPORT_SYMBOL(spa_missing_tvds_allowed
);
2612 EXPORT_SYMBOL(spa_set_missing_tvds
);
2613 EXPORT_SYMBOL(spa_state_to_name
);
2614 EXPORT_SYMBOL(spa_importing_readonly_checkpoint
);
2615 EXPORT_SYMBOL(spa_min_claim_txg
);
2616 EXPORT_SYMBOL(spa_suspend_async_destroy
);
2617 EXPORT_SYMBOL(spa_has_checkpoint
);
2618 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable
);
2621 module_param(zfs_flags
, uint
, 0644);
2622 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
2624 module_param(zfs_recover
, int, 0644);
2625 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
2627 module_param(zfs_free_leak_on_eio
, int, 0644);
2628 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
2629 "Set to ignore IO errors during free and permanently leak the space");
2631 module_param_call(zfs_deadman_synctime_ms
, param_set_deadman_synctime
,
2632 param_get_ulong
, &zfs_deadman_synctime_ms
, 0644);
2633 MODULE_PARM_DESC(zfs_deadman_synctime_ms
,
2634 "Pool sync expiration time in milliseconds");
2636 module_param_call(zfs_deadman_ziotime_ms
, param_set_deadman_ziotime
,
2637 param_get_ulong
, &zfs_deadman_ziotime_ms
, 0644);
2638 MODULE_PARM_DESC(zfs_deadman_ziotime_ms
,
2639 "IO expiration time in milliseconds");
2641 module_param(zfs_deadman_checktime_ms
, ulong
, 0644);
2642 MODULE_PARM_DESC(zfs_deadman_checktime_ms
,
2643 "Dead I/O check interval in milliseconds");
2645 module_param(zfs_deadman_enabled
, int, 0644);
2646 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
2648 module_param_call(zfs_deadman_failmode
, param_set_deadman_failmode
,
2649 param_get_charp
, &zfs_deadman_failmode
, 0644);
2650 MODULE_PARM_DESC(zfs_deadman_failmode
, "Failmode for deadman timer");
2652 module_param(spa_asize_inflation
, int, 0644);
2653 MODULE_PARM_DESC(spa_asize_inflation
,
2654 "SPA size estimate multiplication factor");
2656 module_param_call(spa_slop_shift
, param_set_slop_shift
, param_get_int
,
2657 &spa_slop_shift
, 0644);
2658 MODULE_PARM_DESC(spa_slop_shift
, "Reserved free space in pool");
2660 module_param(zfs_ddt_data_is_special
, int, 0644);
2661 MODULE_PARM_DESC(zfs_ddt_data_is_special
,
2662 "Place DDT data into the special class");
2664 module_param(zfs_user_indirect_is_special
, int, 0644);
2665 MODULE_PARM_DESC(zfs_user_indirect_is_special
,
2666 "Place user data indirect blocks into the special class");