4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 * Copyright 2013 Saso Kiselkov. All rights reserved.
27 * Copyright (c) 2017 Datto Inc.
28 * Copyright (c) 2017, Intel Corporation.
29 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
32 #include <sys/zfs_context.h>
33 #include <sys/spa_impl.h>
35 #include <sys/zio_checksum.h>
36 #include <sys/zio_compress.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/vdev_initialize.h>
43 #include <sys/vdev_trim.h>
44 #include <sys/vdev_file.h>
45 #include <sys/vdev_raidz.h>
46 #include <sys/metaslab.h>
47 #include <sys/uberblock_impl.h>
50 #include <sys/unique.h>
51 #include <sys/dsl_pool.h>
52 #include <sys/dsl_dir.h>
53 #include <sys/dsl_prop.h>
54 #include <sys/fm/util.h>
55 #include <sys/dsl_scan.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/metaslab_impl.h>
60 #include <sys/kstat.h>
62 #include <sys/btree.h>
63 #include <sys/zfeature.h>
69 * There are three basic locks for managing spa_t structures:
71 * spa_namespace_lock (global mutex)
73 * This lock must be acquired to do any of the following:
75 * - Lookup a spa_t by name
76 * - Add or remove a spa_t from the namespace
77 * - Increase spa_refcount from non-zero
78 * - Check if spa_refcount is zero
80 * - add/remove/attach/detach devices
81 * - Held for the duration of create/destroy/import/export
83 * It does not need to handle recursion. A create or destroy may
84 * reference objects (files or zvols) in other pools, but by
85 * definition they must have an existing reference, and will never need
86 * to lookup a spa_t by name.
88 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
90 * This reference count keep track of any active users of the spa_t. The
91 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
92 * the refcount is never really 'zero' - opening a pool implicitly keeps
93 * some references in the DMU. Internally we check against spa_minref, but
94 * present the image of a zero/non-zero value to consumers.
96 * spa_config_lock[] (per-spa array of rwlocks)
98 * This protects the spa_t from config changes, and must be held in
99 * the following circumstances:
101 * - RW_READER to perform I/O to the spa
102 * - RW_WRITER to change the vdev config
104 * The locking order is fairly straightforward:
106 * spa_namespace_lock -> spa_refcount
108 * The namespace lock must be acquired to increase the refcount from 0
109 * or to check if it is zero.
111 * spa_refcount -> spa_config_lock[]
113 * There must be at least one valid reference on the spa_t to acquire
116 * spa_namespace_lock -> spa_config_lock[]
118 * The namespace lock must always be taken before the config lock.
121 * The spa_namespace_lock can be acquired directly and is globally visible.
123 * The namespace is manipulated using the following functions, all of which
124 * require the spa_namespace_lock to be held.
126 * spa_lookup() Lookup a spa_t by name.
128 * spa_add() Create a new spa_t in the namespace.
130 * spa_remove() Remove a spa_t from the namespace. This also
131 * frees up any memory associated with the spa_t.
133 * spa_next() Returns the next spa_t in the system, or the
134 * first if NULL is passed.
136 * spa_evict_all() Shutdown and remove all spa_t structures in
139 * spa_guid_exists() Determine whether a pool/device guid exists.
141 * The spa_refcount is manipulated using the following functions:
143 * spa_open_ref() Adds a reference to the given spa_t. Must be
144 * called with spa_namespace_lock held if the
145 * refcount is currently zero.
147 * spa_close() Remove a reference from the spa_t. This will
148 * not free the spa_t or remove it from the
149 * namespace. No locking is required.
151 * spa_refcount_zero() Returns true if the refcount is currently
152 * zero. Must be called with spa_namespace_lock
155 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
156 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
157 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
159 * To read the configuration, it suffices to hold one of these locks as reader.
160 * To modify the configuration, you must hold all locks as writer. To modify
161 * vdev state without altering the vdev tree's topology (e.g. online/offline),
162 * you must hold SCL_STATE and SCL_ZIO as writer.
164 * We use these distinct config locks to avoid recursive lock entry.
165 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
166 * block allocations (SCL_ALLOC), which may require reading space maps
167 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
169 * The spa config locks cannot be normal rwlocks because we need the
170 * ability to hand off ownership. For example, SCL_ZIO is acquired
171 * by the issuing thread and later released by an interrupt thread.
172 * They do, however, obey the usual write-wanted semantics to prevent
173 * writer (i.e. system administrator) starvation.
175 * The lock acquisition rules are as follows:
178 * Protects changes to the vdev tree topology, such as vdev
179 * add/remove/attach/detach. Protects the dirty config list
180 * (spa_config_dirty_list) and the set of spares and l2arc devices.
183 * Protects changes to pool state and vdev state, such as vdev
184 * online/offline/fault/degrade/clear. Protects the dirty state list
185 * (spa_state_dirty_list) and global pool state (spa_state).
188 * Protects changes to metaslab groups and classes.
189 * Held as reader by metaslab_alloc() and metaslab_claim().
192 * Held by bp-level zios (those which have no io_vd upon entry)
193 * to prevent changes to the vdev tree. The bp-level zio implicitly
194 * protects all of its vdev child zios, which do not hold SCL_ZIO.
197 * Protects changes to metaslab groups and classes.
198 * Held as reader by metaslab_free(). SCL_FREE is distinct from
199 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
200 * blocks in zio_done() while another i/o that holds either
201 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
204 * Held as reader to prevent changes to the vdev tree during trivial
205 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
206 * other locks, and lower than all of them, to ensure that it's safe
207 * to acquire regardless of caller context.
209 * In addition, the following rules apply:
211 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
212 * The lock ordering is SCL_CONFIG > spa_props_lock.
214 * (b) I/O operations on leaf vdevs. For any zio operation that takes
215 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
216 * or zio_write_phys() -- the caller must ensure that the config cannot
217 * cannot change in the interim, and that the vdev cannot be reopened.
218 * SCL_STATE as reader suffices for both.
220 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
222 * spa_vdev_enter() Acquire the namespace lock and the config lock
225 * spa_vdev_exit() Release the config lock, wait for all I/O
226 * to complete, sync the updated configs to the
227 * cache, and release the namespace lock.
229 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
230 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
231 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
234 static avl_tree_t spa_namespace_avl
;
235 kmutex_t spa_namespace_lock
;
236 static kcondvar_t spa_namespace_cv
;
237 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
239 static kmutex_t spa_spare_lock
;
240 static avl_tree_t spa_spare_avl
;
241 static kmutex_t spa_l2cache_lock
;
242 static avl_tree_t spa_l2cache_avl
;
244 kmem_cache_t
*spa_buffer_pool
;
245 spa_mode_t spa_mode_global
= SPA_MODE_UNINIT
;
249 * Everything except dprintf, set_error, spa, and indirect_remap is on
250 * by default in debug builds.
252 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SET_ERROR
|
253 ZFS_DEBUG_INDIRECT_REMAP
);
259 * zfs_recover can be set to nonzero to attempt to recover from
260 * otherwise-fatal errors, typically caused by on-disk corruption. When
261 * set, calls to zfs_panic_recover() will turn into warning messages.
262 * This should only be used as a last resort, as it typically results
263 * in leaked space, or worse.
265 int zfs_recover
= B_FALSE
;
268 * If destroy encounters an EIO while reading metadata (e.g. indirect
269 * blocks), space referenced by the missing metadata can not be freed.
270 * Normally this causes the background destroy to become "stalled", as
271 * it is unable to make forward progress. While in this stalled state,
272 * all remaining space to free from the error-encountering filesystem is
273 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
274 * permanently leak the space from indirect blocks that can not be read,
275 * and continue to free everything else that it can.
277 * The default, "stalling" behavior is useful if the storage partially
278 * fails (i.e. some but not all i/os fail), and then later recovers. In
279 * this case, we will be able to continue pool operations while it is
280 * partially failed, and when it recovers, we can continue to free the
281 * space, with no leaks. However, note that this case is actually
284 * Typically pools either (a) fail completely (but perhaps temporarily,
285 * e.g. a top-level vdev going offline), or (b) have localized,
286 * permanent errors (e.g. disk returns the wrong data due to bit flip or
287 * firmware bug). In case (a), this setting does not matter because the
288 * pool will be suspended and the sync thread will not be able to make
289 * forward progress regardless. In case (b), because the error is
290 * permanent, the best we can do is leak the minimum amount of space,
291 * which is what setting this flag will do. Therefore, it is reasonable
292 * for this flag to normally be set, but we chose the more conservative
293 * approach of not setting it, so that there is no possibility of
294 * leaking space in the "partial temporary" failure case.
296 int zfs_free_leak_on_eio
= B_FALSE
;
299 * Expiration time in milliseconds. This value has two meanings. First it is
300 * used to determine when the spa_deadman() logic should fire. By default the
301 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
302 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
303 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
304 * in one of three behaviors controlled by zfs_deadman_failmode.
306 unsigned long zfs_deadman_synctime_ms
= 600000ULL;
309 * This value controls the maximum amount of time zio_wait() will block for an
310 * outstanding IO. By default this is 300 seconds at which point the "hung"
311 * behavior will be applied as described for zfs_deadman_synctime_ms.
313 unsigned long zfs_deadman_ziotime_ms
= 300000ULL;
316 * Check time in milliseconds. This defines the frequency at which we check
319 unsigned long zfs_deadman_checktime_ms
= 60000ULL;
322 * By default the deadman is enabled.
324 int zfs_deadman_enabled
= 1;
327 * Controls the behavior of the deadman when it detects a "hung" I/O.
328 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
330 * wait - Wait for the "hung" I/O (default)
331 * continue - Attempt to recover from a "hung" I/O
332 * panic - Panic the system
334 char *zfs_deadman_failmode
= "wait";
337 * The worst case is single-sector max-parity RAID-Z blocks, in which
338 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
339 * times the size; so just assume that. Add to this the fact that
340 * we can have up to 3 DVAs per bp, and one more factor of 2 because
341 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
343 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
345 int spa_asize_inflation
= 24;
348 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
349 * the pool to be consumed. This ensures that we don't run the pool
350 * completely out of space, due to unaccounted changes (e.g. to the MOS).
351 * It also limits the worst-case time to allocate space. If we have
352 * less than this amount of free space, most ZPL operations (e.g. write,
353 * create) will return ENOSPC.
355 * Certain operations (e.g. file removal, most administrative actions) can
356 * use half the slop space. They will only return ENOSPC if less than half
357 * the slop space is free. Typically, once the pool has less than the slop
358 * space free, the user will use these operations to free up space in the pool.
359 * These are the operations that call dsl_pool_adjustedsize() with the netfree
360 * argument set to TRUE.
362 * Operations that are almost guaranteed to free up space in the absence of
363 * a pool checkpoint can use up to three quarters of the slop space
366 * A very restricted set of operations are always permitted, regardless of
367 * the amount of free space. These are the operations that call
368 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
369 * increase in the amount of space used, it is possible to run the pool
370 * completely out of space, causing it to be permanently read-only.
372 * Note that on very small pools, the slop space will be larger than
373 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
374 * but we never allow it to be more than half the pool size.
376 * See also the comments in zfs_space_check_t.
378 int spa_slop_shift
= 5;
379 uint64_t spa_min_slop
= 128 * 1024 * 1024;
380 int spa_allocators
= 4;
385 spa_load_failed(spa_t
*spa
, const char *fmt
, ...)
391 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
394 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa
->spa_name
,
395 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
400 spa_load_note(spa_t
*spa
, const char *fmt
, ...)
406 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
409 zfs_dbgmsg("spa_load(%s, config %s): %s", spa
->spa_name
,
410 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
414 * By default dedup and user data indirects land in the special class
416 int zfs_ddt_data_is_special
= B_TRUE
;
417 int zfs_user_indirect_is_special
= B_TRUE
;
420 * The percentage of special class final space reserved for metadata only.
421 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
422 * let metadata into the class.
424 int zfs_special_class_metadata_reserve_pct
= 25;
427 * ==========================================================================
429 * ==========================================================================
432 spa_config_lock_init(spa_t
*spa
)
434 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
435 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
436 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
437 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
438 zfs_refcount_create_untracked(&scl
->scl_count
);
439 scl
->scl_writer
= NULL
;
440 scl
->scl_write_wanted
= 0;
445 spa_config_lock_destroy(spa_t
*spa
)
447 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
448 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
449 mutex_destroy(&scl
->scl_lock
);
450 cv_destroy(&scl
->scl_cv
);
451 zfs_refcount_destroy(&scl
->scl_count
);
452 ASSERT(scl
->scl_writer
== NULL
);
453 ASSERT(scl
->scl_write_wanted
== 0);
458 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
460 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
461 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
462 if (!(locks
& (1 << i
)))
464 mutex_enter(&scl
->scl_lock
);
465 if (rw
== RW_READER
) {
466 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
467 mutex_exit(&scl
->scl_lock
);
468 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
473 ASSERT(scl
->scl_writer
!= curthread
);
474 if (!zfs_refcount_is_zero(&scl
->scl_count
)) {
475 mutex_exit(&scl
->scl_lock
);
476 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
480 scl
->scl_writer
= curthread
;
482 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
483 mutex_exit(&scl
->scl_lock
);
489 spa_config_enter(spa_t
*spa
, int locks
, const void *tag
, krw_t rw
)
493 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
495 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
496 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
497 if (scl
->scl_writer
== curthread
)
498 wlocks_held
|= (1 << i
);
499 if (!(locks
& (1 << i
)))
501 mutex_enter(&scl
->scl_lock
);
502 if (rw
== RW_READER
) {
503 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
504 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
507 ASSERT(scl
->scl_writer
!= curthread
);
508 while (!zfs_refcount_is_zero(&scl
->scl_count
)) {
509 scl
->scl_write_wanted
++;
510 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
511 scl
->scl_write_wanted
--;
513 scl
->scl_writer
= curthread
;
515 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
516 mutex_exit(&scl
->scl_lock
);
518 ASSERT3U(wlocks_held
, <=, locks
);
522 spa_config_exit(spa_t
*spa
, int locks
, const void *tag
)
524 for (int i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
525 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
526 if (!(locks
& (1 << i
)))
528 mutex_enter(&scl
->scl_lock
);
529 ASSERT(!zfs_refcount_is_zero(&scl
->scl_count
));
530 if (zfs_refcount_remove(&scl
->scl_count
, tag
) == 0) {
531 ASSERT(scl
->scl_writer
== NULL
||
532 scl
->scl_writer
== curthread
);
533 scl
->scl_writer
= NULL
; /* OK in either case */
534 cv_broadcast(&scl
->scl_cv
);
536 mutex_exit(&scl
->scl_lock
);
541 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
545 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
546 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
547 if (!(locks
& (1 << i
)))
549 if ((rw
== RW_READER
&&
550 !zfs_refcount_is_zero(&scl
->scl_count
)) ||
551 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
552 locks_held
|= 1 << i
;
559 * ==========================================================================
560 * SPA namespace functions
561 * ==========================================================================
565 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
566 * Returns NULL if no matching spa_t is found.
569 spa_lookup(const char *name
)
571 static spa_t search
; /* spa_t is large; don't allocate on stack */
576 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
578 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
581 * If it's a full dataset name, figure out the pool name and
584 cp
= strpbrk(search
.spa_name
, "/@#");
588 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
594 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
595 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
596 * looking for potentially hung I/Os.
599 spa_deadman(void *arg
)
603 /* Disable the deadman if the pool is suspended. */
604 if (spa_suspended(spa
))
607 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
608 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
609 ++spa
->spa_deadman_calls
);
610 if (zfs_deadman_enabled
)
611 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
613 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
614 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
615 MSEC_TO_TICK(zfs_deadman_checktime_ms
));
619 spa_log_sm_sort_by_txg(const void *va
, const void *vb
)
621 const spa_log_sm_t
*a
= va
;
622 const spa_log_sm_t
*b
= vb
;
624 return (TREE_CMP(a
->sls_txg
, b
->sls_txg
));
628 * Create an uninitialized spa_t with the given name. Requires
629 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
630 * exist by calling spa_lookup() first.
633 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
636 spa_config_dirent_t
*dp
;
638 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
640 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
642 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
643 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
644 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
645 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
646 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
647 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
648 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
649 mutex_init(&spa
->spa_cksum_tmpls_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
650 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
651 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
652 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
653 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
654 mutex_init(&spa
->spa_flushed_ms_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
655 mutex_init(&spa
->spa_activities_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
657 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
658 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
659 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
660 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
661 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
662 cv_init(&spa
->spa_activities_cv
, NULL
, CV_DEFAULT
, NULL
);
663 cv_init(&spa
->spa_waiters_cv
, NULL
, CV_DEFAULT
, NULL
);
665 for (int t
= 0; t
< TXG_SIZE
; t
++)
666 bplist_create(&spa
->spa_free_bplist
[t
]);
668 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
669 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
670 spa
->spa_freeze_txg
= UINT64_MAX
;
671 spa
->spa_final_txg
= UINT64_MAX
;
672 spa
->spa_load_max_txg
= UINT64_MAX
;
674 spa
->spa_proc_state
= SPA_PROC_NONE
;
675 spa
->spa_trust_config
= B_TRUE
;
676 spa
->spa_hostid
= zone_get_hostid(NULL
);
678 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
679 spa
->spa_deadman_ziotime
= MSEC2NSEC(zfs_deadman_ziotime_ms
);
680 spa_set_deadman_failmode(spa
, zfs_deadman_failmode
);
682 zfs_refcount_create(&spa
->spa_refcount
);
683 spa_config_lock_init(spa
);
686 avl_add(&spa_namespace_avl
, spa
);
689 * Set the alternate root, if there is one.
692 spa
->spa_root
= spa_strdup(altroot
);
694 spa
->spa_alloc_count
= spa_allocators
;
695 spa
->spa_alloc_locks
= kmem_zalloc(spa
->spa_alloc_count
*
696 sizeof (kmutex_t
), KM_SLEEP
);
697 spa
->spa_alloc_trees
= kmem_zalloc(spa
->spa_alloc_count
*
698 sizeof (avl_tree_t
), KM_SLEEP
);
699 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
700 mutex_init(&spa
->spa_alloc_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
701 avl_create(&spa
->spa_alloc_trees
[i
], zio_bookmark_compare
,
702 sizeof (zio_t
), offsetof(zio_t
, io_alloc_node
));
704 avl_create(&spa
->spa_metaslabs_by_flushed
, metaslab_sort_by_flushed
,
705 sizeof (metaslab_t
), offsetof(metaslab_t
, ms_spa_txg_node
));
706 avl_create(&spa
->spa_sm_logs_by_txg
, spa_log_sm_sort_by_txg
,
707 sizeof (spa_log_sm_t
), offsetof(spa_log_sm_t
, sls_node
));
708 list_create(&spa
->spa_log_summary
, sizeof (log_summary_entry_t
),
709 offsetof(log_summary_entry_t
, lse_node
));
712 * Every pool starts with the default cachefile
714 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
715 offsetof(spa_config_dirent_t
, scd_link
));
717 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
718 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
719 list_insert_head(&spa
->spa_config_list
, dp
);
721 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
724 if (config
!= NULL
) {
727 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
729 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
733 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
736 if (spa
->spa_label_features
== NULL
) {
737 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
741 spa
->spa_min_ashift
= INT_MAX
;
742 spa
->spa_max_ashift
= 0;
744 /* Reset cached value */
745 spa
->spa_dedup_dspace
= ~0ULL;
748 * As a pool is being created, treat all features as disabled by
749 * setting SPA_FEATURE_DISABLED for all entries in the feature
752 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
753 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
756 list_create(&spa
->spa_leaf_list
, sizeof (vdev_t
),
757 offsetof(vdev_t
, vdev_leaf_node
));
763 * Removes a spa_t from the namespace, freeing up any memory used. Requires
764 * spa_namespace_lock. This is called only after the spa_t has been closed and
768 spa_remove(spa_t
*spa
)
770 spa_config_dirent_t
*dp
;
772 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
773 ASSERT(spa_state(spa
) == POOL_STATE_UNINITIALIZED
);
774 ASSERT3U(zfs_refcount_count(&spa
->spa_refcount
), ==, 0);
775 ASSERT0(spa
->spa_waiters
);
777 nvlist_free(spa
->spa_config_splitting
);
779 avl_remove(&spa_namespace_avl
, spa
);
780 cv_broadcast(&spa_namespace_cv
);
783 spa_strfree(spa
->spa_root
);
785 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
786 list_remove(&spa
->spa_config_list
, dp
);
787 if (dp
->scd_path
!= NULL
)
788 spa_strfree(dp
->scd_path
);
789 kmem_free(dp
, sizeof (spa_config_dirent_t
));
792 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
793 avl_destroy(&spa
->spa_alloc_trees
[i
]);
794 mutex_destroy(&spa
->spa_alloc_locks
[i
]);
796 kmem_free(spa
->spa_alloc_locks
, spa
->spa_alloc_count
*
798 kmem_free(spa
->spa_alloc_trees
, spa
->spa_alloc_count
*
799 sizeof (avl_tree_t
));
801 avl_destroy(&spa
->spa_metaslabs_by_flushed
);
802 avl_destroy(&spa
->spa_sm_logs_by_txg
);
803 list_destroy(&spa
->spa_log_summary
);
804 list_destroy(&spa
->spa_config_list
);
805 list_destroy(&spa
->spa_leaf_list
);
807 nvlist_free(spa
->spa_label_features
);
808 nvlist_free(spa
->spa_load_info
);
809 nvlist_free(spa
->spa_feat_stats
);
810 spa_config_set(spa
, NULL
);
812 zfs_refcount_destroy(&spa
->spa_refcount
);
814 spa_stats_destroy(spa
);
815 spa_config_lock_destroy(spa
);
817 for (int t
= 0; t
< TXG_SIZE
; t
++)
818 bplist_destroy(&spa
->spa_free_bplist
[t
]);
820 zio_checksum_templates_free(spa
);
822 cv_destroy(&spa
->spa_async_cv
);
823 cv_destroy(&spa
->spa_evicting_os_cv
);
824 cv_destroy(&spa
->spa_proc_cv
);
825 cv_destroy(&spa
->spa_scrub_io_cv
);
826 cv_destroy(&spa
->spa_suspend_cv
);
827 cv_destroy(&spa
->spa_activities_cv
);
828 cv_destroy(&spa
->spa_waiters_cv
);
830 mutex_destroy(&spa
->spa_flushed_ms_lock
);
831 mutex_destroy(&spa
->spa_async_lock
);
832 mutex_destroy(&spa
->spa_errlist_lock
);
833 mutex_destroy(&spa
->spa_errlog_lock
);
834 mutex_destroy(&spa
->spa_evicting_os_lock
);
835 mutex_destroy(&spa
->spa_history_lock
);
836 mutex_destroy(&spa
->spa_proc_lock
);
837 mutex_destroy(&spa
->spa_props_lock
);
838 mutex_destroy(&spa
->spa_cksum_tmpls_lock
);
839 mutex_destroy(&spa
->spa_scrub_lock
);
840 mutex_destroy(&spa
->spa_suspend_lock
);
841 mutex_destroy(&spa
->spa_vdev_top_lock
);
842 mutex_destroy(&spa
->spa_feat_stats_lock
);
843 mutex_destroy(&spa
->spa_activities_lock
);
845 kmem_free(spa
, sizeof (spa_t
));
849 * Given a pool, return the next pool in the namespace, or NULL if there is
850 * none. If 'prev' is NULL, return the first pool.
853 spa_next(spa_t
*prev
)
855 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
858 return (AVL_NEXT(&spa_namespace_avl
, prev
));
860 return (avl_first(&spa_namespace_avl
));
864 * ==========================================================================
865 * SPA refcount functions
866 * ==========================================================================
870 * Add a reference to the given spa_t. Must have at least one reference, or
871 * have the namespace lock held.
874 spa_open_ref(spa_t
*spa
, void *tag
)
876 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
877 MUTEX_HELD(&spa_namespace_lock
));
878 (void) zfs_refcount_add(&spa
->spa_refcount
, tag
);
882 * Remove a reference to the given spa_t. Must have at least one reference, or
883 * have the namespace lock held.
886 spa_close(spa_t
*spa
, void *tag
)
888 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
889 MUTEX_HELD(&spa_namespace_lock
));
890 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
894 * Remove a reference to the given spa_t held by a dsl dir that is
895 * being asynchronously released. Async releases occur from a taskq
896 * performing eviction of dsl datasets and dirs. The namespace lock
897 * isn't held and the hold by the object being evicted may contribute to
898 * spa_minref (e.g. dataset or directory released during pool export),
899 * so the asserts in spa_close() do not apply.
902 spa_async_close(spa_t
*spa
, void *tag
)
904 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
908 * Check to see if the spa refcount is zero. Must be called with
909 * spa_namespace_lock held. We really compare against spa_minref, which is the
910 * number of references acquired when opening a pool
913 spa_refcount_zero(spa_t
*spa
)
915 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
917 return (zfs_refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
921 * ==========================================================================
922 * SPA spare and l2cache tracking
923 * ==========================================================================
927 * Hot spares and cache devices are tracked using the same code below,
928 * for 'auxiliary' devices.
931 typedef struct spa_aux
{
939 spa_aux_compare(const void *a
, const void *b
)
941 const spa_aux_t
*sa
= (const spa_aux_t
*)a
;
942 const spa_aux_t
*sb
= (const spa_aux_t
*)b
;
944 return (TREE_CMP(sa
->aux_guid
, sb
->aux_guid
));
948 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
954 search
.aux_guid
= vd
->vdev_guid
;
955 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
958 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
959 aux
->aux_guid
= vd
->vdev_guid
;
961 avl_insert(avl
, aux
, where
);
966 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
972 search
.aux_guid
= vd
->vdev_guid
;
973 aux
= avl_find(avl
, &search
, &where
);
977 if (--aux
->aux_count
== 0) {
978 avl_remove(avl
, aux
);
979 kmem_free(aux
, sizeof (spa_aux_t
));
980 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
981 aux
->aux_pool
= 0ULL;
986 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
988 spa_aux_t search
, *found
;
990 search
.aux_guid
= guid
;
991 found
= avl_find(avl
, &search
, NULL
);
995 *pool
= found
->aux_pool
;
1002 *refcnt
= found
->aux_count
;
1007 return (found
!= NULL
);
1011 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
1013 spa_aux_t search
, *found
;
1016 search
.aux_guid
= vd
->vdev_guid
;
1017 found
= avl_find(avl
, &search
, &where
);
1018 ASSERT(found
!= NULL
);
1019 ASSERT(found
->aux_pool
== 0ULL);
1021 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
1025 * Spares are tracked globally due to the following constraints:
1027 * - A spare may be part of multiple pools.
1028 * - A spare may be added to a pool even if it's actively in use within
1030 * - A spare in use in any pool can only be the source of a replacement if
1031 * the target is a spare in the same pool.
1033 * We keep track of all spares on the system through the use of a reference
1034 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1035 * spare, then we bump the reference count in the AVL tree. In addition, we set
1036 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1037 * inactive). When a spare is made active (used to replace a device in the
1038 * pool), we also keep track of which pool its been made a part of.
1040 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1041 * called under the spa_namespace lock as part of vdev reconfiguration. The
1042 * separate spare lock exists for the status query path, which does not need to
1043 * be completely consistent with respect to other vdev configuration changes.
1047 spa_spare_compare(const void *a
, const void *b
)
1049 return (spa_aux_compare(a
, b
));
1053 spa_spare_add(vdev_t
*vd
)
1055 mutex_enter(&spa_spare_lock
);
1056 ASSERT(!vd
->vdev_isspare
);
1057 spa_aux_add(vd
, &spa_spare_avl
);
1058 vd
->vdev_isspare
= B_TRUE
;
1059 mutex_exit(&spa_spare_lock
);
1063 spa_spare_remove(vdev_t
*vd
)
1065 mutex_enter(&spa_spare_lock
);
1066 ASSERT(vd
->vdev_isspare
);
1067 spa_aux_remove(vd
, &spa_spare_avl
);
1068 vd
->vdev_isspare
= B_FALSE
;
1069 mutex_exit(&spa_spare_lock
);
1073 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
1077 mutex_enter(&spa_spare_lock
);
1078 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
1079 mutex_exit(&spa_spare_lock
);
1085 spa_spare_activate(vdev_t
*vd
)
1087 mutex_enter(&spa_spare_lock
);
1088 ASSERT(vd
->vdev_isspare
);
1089 spa_aux_activate(vd
, &spa_spare_avl
);
1090 mutex_exit(&spa_spare_lock
);
1094 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1095 * Cache devices currently only support one pool per cache device, and so
1096 * for these devices the aux reference count is currently unused beyond 1.
1100 spa_l2cache_compare(const void *a
, const void *b
)
1102 return (spa_aux_compare(a
, b
));
1106 spa_l2cache_add(vdev_t
*vd
)
1108 mutex_enter(&spa_l2cache_lock
);
1109 ASSERT(!vd
->vdev_isl2cache
);
1110 spa_aux_add(vd
, &spa_l2cache_avl
);
1111 vd
->vdev_isl2cache
= B_TRUE
;
1112 mutex_exit(&spa_l2cache_lock
);
1116 spa_l2cache_remove(vdev_t
*vd
)
1118 mutex_enter(&spa_l2cache_lock
);
1119 ASSERT(vd
->vdev_isl2cache
);
1120 spa_aux_remove(vd
, &spa_l2cache_avl
);
1121 vd
->vdev_isl2cache
= B_FALSE
;
1122 mutex_exit(&spa_l2cache_lock
);
1126 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1130 mutex_enter(&spa_l2cache_lock
);
1131 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1132 mutex_exit(&spa_l2cache_lock
);
1138 spa_l2cache_activate(vdev_t
*vd
)
1140 mutex_enter(&spa_l2cache_lock
);
1141 ASSERT(vd
->vdev_isl2cache
);
1142 spa_aux_activate(vd
, &spa_l2cache_avl
);
1143 mutex_exit(&spa_l2cache_lock
);
1147 * ==========================================================================
1149 * ==========================================================================
1153 * Lock the given spa_t for the purpose of adding or removing a vdev.
1154 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1155 * It returns the next transaction group for the spa_t.
1158 spa_vdev_enter(spa_t
*spa
)
1160 mutex_enter(&spa
->spa_vdev_top_lock
);
1161 mutex_enter(&spa_namespace_lock
);
1163 vdev_autotrim_stop_all(spa
);
1165 return (spa_vdev_config_enter(spa
));
1169 * Internal implementation for spa_vdev_enter(). Used when a vdev
1170 * operation requires multiple syncs (i.e. removing a device) while
1171 * keeping the spa_namespace_lock held.
1174 spa_vdev_config_enter(spa_t
*spa
)
1176 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1178 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1180 return (spa_last_synced_txg(spa
) + 1);
1184 * Used in combination with spa_vdev_config_enter() to allow the syncing
1185 * of multiple transactions without releasing the spa_namespace_lock.
1188 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1190 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1192 int config_changed
= B_FALSE
;
1194 ASSERT(txg
> spa_last_synced_txg(spa
));
1196 spa
->spa_pending_vdev
= NULL
;
1199 * Reassess the DTLs.
1201 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1203 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1204 config_changed
= B_TRUE
;
1205 spa
->spa_config_generation
++;
1209 * Verify the metaslab classes.
1211 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1212 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1213 ASSERT(metaslab_class_validate(spa_special_class(spa
)) == 0);
1214 ASSERT(metaslab_class_validate(spa_dedup_class(spa
)) == 0);
1216 spa_config_exit(spa
, SCL_ALL
, spa
);
1219 * Panic the system if the specified tag requires it. This
1220 * is useful for ensuring that configurations are updated
1223 if (zio_injection_enabled
)
1224 zio_handle_panic_injection(spa
, tag
, 0);
1227 * Note: this txg_wait_synced() is important because it ensures
1228 * that there won't be more than one config change per txg.
1229 * This allows us to use the txg as the generation number.
1232 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1235 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1236 if (vd
->vdev_ops
->vdev_op_leaf
) {
1237 mutex_enter(&vd
->vdev_initialize_lock
);
1238 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
,
1240 mutex_exit(&vd
->vdev_initialize_lock
);
1242 mutex_enter(&vd
->vdev_trim_lock
);
1243 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, NULL
);
1244 mutex_exit(&vd
->vdev_trim_lock
);
1248 * The vdev may be both a leaf and top-level device.
1250 vdev_autotrim_stop_wait(vd
);
1252 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1254 spa_config_exit(spa
, SCL_ALL
, spa
);
1258 * If the config changed, update the config cache.
1261 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1265 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1266 * locking of spa_vdev_enter(), we also want make sure the transactions have
1267 * synced to disk, and then update the global configuration cache with the new
1271 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1273 vdev_autotrim_restart(spa
);
1275 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1276 mutex_exit(&spa_namespace_lock
);
1277 mutex_exit(&spa
->spa_vdev_top_lock
);
1283 * Lock the given spa_t for the purpose of changing vdev state.
1286 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1288 int locks
= SCL_STATE_ALL
| oplocks
;
1291 * Root pools may need to read of the underlying devfs filesystem
1292 * when opening up a vdev. Unfortunately if we're holding the
1293 * SCL_ZIO lock it will result in a deadlock when we try to issue
1294 * the read from the root filesystem. Instead we "prefetch"
1295 * the associated vnodes that we need prior to opening the
1296 * underlying devices and cache them so that we can prevent
1297 * any I/O when we are doing the actual open.
1299 if (spa_is_root(spa
)) {
1300 int low
= locks
& ~(SCL_ZIO
- 1);
1301 int high
= locks
& ~low
;
1303 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1304 vdev_hold(spa
->spa_root_vdev
);
1305 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1307 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1309 spa
->spa_vdev_locks
= locks
;
1313 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1315 boolean_t config_changed
= B_FALSE
;
1318 if (vd
== NULL
|| vd
== spa
->spa_root_vdev
) {
1319 vdev_top
= spa
->spa_root_vdev
;
1321 vdev_top
= vd
->vdev_top
;
1324 if (vd
!= NULL
|| error
== 0)
1325 vdev_dtl_reassess(vdev_top
, 0, 0, B_FALSE
);
1328 if (vd
!= spa
->spa_root_vdev
)
1329 vdev_state_dirty(vdev_top
);
1331 config_changed
= B_TRUE
;
1332 spa
->spa_config_generation
++;
1335 if (spa_is_root(spa
))
1336 vdev_rele(spa
->spa_root_vdev
);
1338 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1339 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1342 * If anything changed, wait for it to sync. This ensures that,
1343 * from the system administrator's perspective, zpool(1M) commands
1344 * are synchronous. This is important for things like zpool offline:
1345 * when the command completes, you expect no further I/O from ZFS.
1348 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1351 * If the config changed, update the config cache.
1353 if (config_changed
) {
1354 mutex_enter(&spa_namespace_lock
);
1355 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1356 mutex_exit(&spa_namespace_lock
);
1363 * ==========================================================================
1364 * Miscellaneous functions
1365 * ==========================================================================
1369 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1371 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1372 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1374 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1375 * dirty the vdev config because lock SCL_CONFIG is not held.
1376 * Thankfully, in this case we don't need to dirty the config
1377 * because it will be written out anyway when we finish
1378 * creating the pool.
1380 if (tx
->tx_txg
!= TXG_INITIAL
)
1381 vdev_config_dirty(spa
->spa_root_vdev
);
1386 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1388 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1389 vdev_config_dirty(spa
->spa_root_vdev
);
1393 * Return the spa_t associated with given pool_guid, if it exists. If
1394 * device_guid is non-zero, determine whether the pool exists *and* contains
1395 * a device with the specified device_guid.
1398 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1401 avl_tree_t
*t
= &spa_namespace_avl
;
1403 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1405 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1406 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1408 if (spa
->spa_root_vdev
== NULL
)
1410 if (spa_guid(spa
) == pool_guid
) {
1411 if (device_guid
== 0)
1414 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1415 device_guid
) != NULL
)
1419 * Check any devices we may be in the process of adding.
1421 if (spa
->spa_pending_vdev
) {
1422 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1423 device_guid
) != NULL
)
1433 * Determine whether a pool with the given pool_guid exists.
1436 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1438 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1442 spa_strdup(const char *s
)
1448 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1456 spa_strfree(char *s
)
1458 kmem_free(s
, strlen(s
) + 1);
1462 spa_get_random(uint64_t range
)
1471 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1477 spa_generate_guid(spa_t
*spa
)
1479 uint64_t guid
= spa_get_random(-1ULL);
1482 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1483 guid
= spa_get_random(-1ULL);
1485 while (guid
== 0 || spa_guid_exists(guid
, 0))
1486 guid
= spa_get_random(-1ULL);
1493 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1496 char *checksum
= NULL
;
1497 char *compress
= NULL
;
1500 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1501 dmu_object_byteswap_t bswap
=
1502 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1503 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1504 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1505 "metadata" : "data",
1506 dmu_ot_byteswap
[bswap
].ob_name
);
1508 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1511 if (!BP_IS_EMBEDDED(bp
)) {
1513 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1515 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1518 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1523 spa_freeze(spa_t
*spa
)
1525 uint64_t freeze_txg
= 0;
1527 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1528 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1529 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1530 spa
->spa_freeze_txg
= freeze_txg
;
1532 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1533 if (freeze_txg
!= 0)
1534 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1538 zfs_panic_recover(const char *fmt
, ...)
1543 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1548 * This is a stripped-down version of strtoull, suitable only for converting
1549 * lowercase hexadecimal numbers that don't overflow.
1552 zfs_strtonum(const char *str
, char **nptr
)
1558 while ((c
= *str
) != '\0') {
1559 if (c
>= '0' && c
<= '9')
1561 else if (c
>= 'a' && c
<= 'f')
1562 digit
= 10 + c
- 'a';
1573 *nptr
= (char *)str
;
1579 spa_activate_allocation_classes(spa_t
*spa
, dmu_tx_t
*tx
)
1582 * We bump the feature refcount for each special vdev added to the pool
1584 ASSERT(spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
));
1585 spa_feature_incr(spa
, SPA_FEATURE_ALLOCATION_CLASSES
, tx
);
1589 * ==========================================================================
1590 * Accessor functions
1591 * ==========================================================================
1595 spa_shutting_down(spa_t
*spa
)
1597 return (spa
->spa_async_suspended
);
1601 spa_get_dsl(spa_t
*spa
)
1603 return (spa
->spa_dsl_pool
);
1607 spa_is_initializing(spa_t
*spa
)
1609 return (spa
->spa_is_initializing
);
1613 spa_indirect_vdevs_loaded(spa_t
*spa
)
1615 return (spa
->spa_indirect_vdevs_loaded
);
1619 spa_get_rootblkptr(spa_t
*spa
)
1621 return (&spa
->spa_ubsync
.ub_rootbp
);
1625 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1627 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1631 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1633 if (spa
->spa_root
== NULL
)
1636 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1640 spa_sync_pass(spa_t
*spa
)
1642 return (spa
->spa_sync_pass
);
1646 spa_name(spa_t
*spa
)
1648 return (spa
->spa_name
);
1652 spa_guid(spa_t
*spa
)
1654 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1658 * If we fail to parse the config during spa_load(), we can go through
1659 * the error path (which posts an ereport) and end up here with no root
1660 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1663 if (spa
->spa_root_vdev
== NULL
)
1664 return (spa
->spa_config_guid
);
1666 guid
= spa
->spa_last_synced_guid
!= 0 ?
1667 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1670 * Return the most recently synced out guid unless we're
1671 * in syncing context.
1673 if (dp
&& dsl_pool_sync_context(dp
))
1674 return (spa
->spa_root_vdev
->vdev_guid
);
1680 spa_load_guid(spa_t
*spa
)
1683 * This is a GUID that exists solely as a reference for the
1684 * purposes of the arc. It is generated at load time, and
1685 * is never written to persistent storage.
1687 return (spa
->spa_load_guid
);
1691 spa_last_synced_txg(spa_t
*spa
)
1693 return (spa
->spa_ubsync
.ub_txg
);
1697 spa_first_txg(spa_t
*spa
)
1699 return (spa
->spa_first_txg
);
1703 spa_syncing_txg(spa_t
*spa
)
1705 return (spa
->spa_syncing_txg
);
1709 * Return the last txg where data can be dirtied. The final txgs
1710 * will be used to just clear out any deferred frees that remain.
1713 spa_final_dirty_txg(spa_t
*spa
)
1715 return (spa
->spa_final_txg
- TXG_DEFER_SIZE
);
1719 spa_state(spa_t
*spa
)
1721 return (spa
->spa_state
);
1725 spa_load_state(spa_t
*spa
)
1727 return (spa
->spa_load_state
);
1731 spa_freeze_txg(spa_t
*spa
)
1733 return (spa
->spa_freeze_txg
);
1737 * Return the inflated asize for a logical write in bytes. This is used by the
1738 * DMU to calculate the space a logical write will require on disk.
1739 * If lsize is smaller than the largest physical block size allocatable on this
1740 * pool we use its value instead, since the write will end up using the whole
1744 spa_get_worst_case_asize(spa_t
*spa
, uint64_t lsize
)
1747 return (0); /* No inflation needed */
1748 return (MAX(lsize
, 1 << spa
->spa_max_ashift
) * spa_asize_inflation
);
1752 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1753 * or at least 128MB, unless that would cause it to be more than half the
1756 * See the comment above spa_slop_shift for details.
1759 spa_get_slop_space(spa_t
*spa
)
1761 uint64_t space
= spa_get_dspace(spa
);
1762 return (MAX(space
>> spa_slop_shift
, MIN(space
>> 1, spa_min_slop
)));
1766 spa_get_dspace(spa_t
*spa
)
1768 return (spa
->spa_dspace
);
1772 spa_get_checkpoint_space(spa_t
*spa
)
1774 return (spa
->spa_checkpoint_info
.sci_dspace
);
1778 spa_update_dspace(spa_t
*spa
)
1780 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1781 ddt_get_dedup_dspace(spa
);
1782 if (spa
->spa_vdev_removal
!= NULL
) {
1784 * We can't allocate from the removing device, so
1785 * subtract its size. This prevents the DMU/DSL from
1786 * filling up the (now smaller) pool while we are in the
1787 * middle of removing the device.
1789 * Note that the DMU/DSL doesn't actually know or care
1790 * how much space is allocated (it does its own tracking
1791 * of how much space has been logically used). So it
1792 * doesn't matter that the data we are moving may be
1793 * allocated twice (on the old device and the new
1796 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1798 vdev_lookup_top(spa
, spa
->spa_vdev_removal
->svr_vdev_id
);
1799 spa
->spa_dspace
-= spa_deflate(spa
) ?
1800 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1801 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1806 * Return the failure mode that has been set to this pool. The default
1807 * behavior will be to block all I/Os when a complete failure occurs.
1810 spa_get_failmode(spa_t
*spa
)
1812 return (spa
->spa_failmode
);
1816 spa_suspended(spa_t
*spa
)
1818 return (spa
->spa_suspended
!= ZIO_SUSPEND_NONE
);
1822 spa_version(spa_t
*spa
)
1824 return (spa
->spa_ubsync
.ub_version
);
1828 spa_deflate(spa_t
*spa
)
1830 return (spa
->spa_deflate
);
1834 spa_normal_class(spa_t
*spa
)
1836 return (spa
->spa_normal_class
);
1840 spa_log_class(spa_t
*spa
)
1842 return (spa
->spa_log_class
);
1846 spa_special_class(spa_t
*spa
)
1848 return (spa
->spa_special_class
);
1852 spa_dedup_class(spa_t
*spa
)
1854 return (spa
->spa_dedup_class
);
1858 * Locate an appropriate allocation class
1861 spa_preferred_class(spa_t
*spa
, uint64_t size
, dmu_object_type_t objtype
,
1862 uint_t level
, uint_t special_smallblk
)
1864 if (DMU_OT_IS_ZIL(objtype
)) {
1865 if (spa
->spa_log_class
->mc_groups
!= 0)
1866 return (spa_log_class(spa
));
1868 return (spa_normal_class(spa
));
1871 boolean_t has_special_class
= spa
->spa_special_class
->mc_groups
!= 0;
1873 if (DMU_OT_IS_DDT(objtype
)) {
1874 if (spa
->spa_dedup_class
->mc_groups
!= 0)
1875 return (spa_dedup_class(spa
));
1876 else if (has_special_class
&& zfs_ddt_data_is_special
)
1877 return (spa_special_class(spa
));
1879 return (spa_normal_class(spa
));
1882 /* Indirect blocks for user data can land in special if allowed */
1883 if (level
> 0 && (DMU_OT_IS_FILE(objtype
) || objtype
== DMU_OT_ZVOL
)) {
1884 if (has_special_class
&& zfs_user_indirect_is_special
)
1885 return (spa_special_class(spa
));
1887 return (spa_normal_class(spa
));
1890 if (DMU_OT_IS_METADATA(objtype
) || level
> 0) {
1891 if (has_special_class
)
1892 return (spa_special_class(spa
));
1894 return (spa_normal_class(spa
));
1898 * Allow small file blocks in special class in some cases (like
1899 * for the dRAID vdev feature). But always leave a reserve of
1900 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1902 if (DMU_OT_IS_FILE(objtype
) &&
1903 has_special_class
&& size
<= special_smallblk
) {
1904 metaslab_class_t
*special
= spa_special_class(spa
);
1905 uint64_t alloc
= metaslab_class_get_alloc(special
);
1906 uint64_t space
= metaslab_class_get_space(special
);
1908 (space
* (100 - zfs_special_class_metadata_reserve_pct
))
1915 return (spa_normal_class(spa
));
1919 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1921 mutex_enter(&spa
->spa_evicting_os_lock
);
1922 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1923 mutex_exit(&spa
->spa_evicting_os_lock
);
1927 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1929 mutex_enter(&spa
->spa_evicting_os_lock
);
1930 list_remove(&spa
->spa_evicting_os_list
, os
);
1931 cv_broadcast(&spa
->spa_evicting_os_cv
);
1932 mutex_exit(&spa
->spa_evicting_os_lock
);
1936 spa_evicting_os_wait(spa_t
*spa
)
1938 mutex_enter(&spa
->spa_evicting_os_lock
);
1939 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1940 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1941 mutex_exit(&spa
->spa_evicting_os_lock
);
1943 dmu_buf_user_evict_wait();
1947 spa_max_replication(spa_t
*spa
)
1950 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1951 * handle BPs with more than one DVA allocated. Set our max
1952 * replication level accordingly.
1954 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1956 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1960 spa_prev_software_version(spa_t
*spa
)
1962 return (spa
->spa_prev_software_version
);
1966 spa_deadman_synctime(spa_t
*spa
)
1968 return (spa
->spa_deadman_synctime
);
1972 spa_get_autotrim(spa_t
*spa
)
1974 return (spa
->spa_autotrim
);
1978 spa_deadman_ziotime(spa_t
*spa
)
1980 return (spa
->spa_deadman_ziotime
);
1984 spa_get_deadman_failmode(spa_t
*spa
)
1986 return (spa
->spa_deadman_failmode
);
1990 spa_set_deadman_failmode(spa_t
*spa
, const char *failmode
)
1992 if (strcmp(failmode
, "wait") == 0)
1993 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1994 else if (strcmp(failmode
, "continue") == 0)
1995 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
1996 else if (strcmp(failmode
, "panic") == 0)
1997 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
1999 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
2003 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
2005 uint64_t asize
= DVA_GET_ASIZE(dva
);
2006 uint64_t dsize
= asize
;
2008 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2010 if (asize
!= 0 && spa
->spa_deflate
) {
2011 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
2013 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
2014 vd
->vdev_deflate_ratio
;
2021 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
2025 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2026 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2032 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
2036 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2038 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2039 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2041 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2047 spa_dirty_data(spa_t
*spa
)
2049 return (spa
->spa_dsl_pool
->dp_dirty_total
);
2053 * ==========================================================================
2054 * SPA Import Progress Routines
2055 * ==========================================================================
2058 typedef struct spa_import_progress
{
2059 uint64_t pool_guid
; /* unique id for updates */
2061 spa_load_state_t spa_load_state
;
2062 uint64_t mmp_sec_remaining
; /* MMP activity check */
2063 uint64_t spa_load_max_txg
; /* rewind txg */
2064 procfs_list_node_t smh_node
;
2065 } spa_import_progress_t
;
2067 spa_history_list_t
*spa_import_progress_list
= NULL
;
2070 spa_import_progress_show_header(struct seq_file
*f
)
2072 seq_printf(f
, "%-20s %-14s %-14s %-12s %s\n", "pool_guid",
2073 "load_state", "multihost_secs", "max_txg",
2079 spa_import_progress_show(struct seq_file
*f
, void *data
)
2081 spa_import_progress_t
*sip
= (spa_import_progress_t
*)data
;
2083 seq_printf(f
, "%-20llu %-14llu %-14llu %-12llu %s\n",
2084 (u_longlong_t
)sip
->pool_guid
, (u_longlong_t
)sip
->spa_load_state
,
2085 (u_longlong_t
)sip
->mmp_sec_remaining
,
2086 (u_longlong_t
)sip
->spa_load_max_txg
,
2087 (sip
->pool_name
? sip
->pool_name
: "-"));
2092 /* Remove oldest elements from list until there are no more than 'size' left */
2094 spa_import_progress_truncate(spa_history_list_t
*shl
, unsigned int size
)
2096 spa_import_progress_t
*sip
;
2097 while (shl
->size
> size
) {
2098 sip
= list_remove_head(&shl
->procfs_list
.pl_list
);
2100 spa_strfree(sip
->pool_name
);
2101 kmem_free(sip
, sizeof (spa_import_progress_t
));
2105 IMPLY(size
== 0, list_is_empty(&shl
->procfs_list
.pl_list
));
2109 spa_import_progress_init(void)
2111 spa_import_progress_list
= kmem_zalloc(sizeof (spa_history_list_t
),
2114 spa_import_progress_list
->size
= 0;
2116 spa_import_progress_list
->procfs_list
.pl_private
=
2117 spa_import_progress_list
;
2119 procfs_list_install("zfs",
2122 &spa_import_progress_list
->procfs_list
,
2123 spa_import_progress_show
,
2124 spa_import_progress_show_header
,
2126 offsetof(spa_import_progress_t
, smh_node
));
2130 spa_import_progress_destroy(void)
2132 spa_history_list_t
*shl
= spa_import_progress_list
;
2133 procfs_list_uninstall(&shl
->procfs_list
);
2134 spa_import_progress_truncate(shl
, 0);
2135 procfs_list_destroy(&shl
->procfs_list
);
2136 kmem_free(shl
, sizeof (spa_history_list_t
));
2140 spa_import_progress_set_state(uint64_t pool_guid
,
2141 spa_load_state_t load_state
)
2143 spa_history_list_t
*shl
= spa_import_progress_list
;
2144 spa_import_progress_t
*sip
;
2150 mutex_enter(&shl
->procfs_list
.pl_lock
);
2151 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2152 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2153 if (sip
->pool_guid
== pool_guid
) {
2154 sip
->spa_load_state
= load_state
;
2159 mutex_exit(&shl
->procfs_list
.pl_lock
);
2165 spa_import_progress_set_max_txg(uint64_t pool_guid
, uint64_t load_max_txg
)
2167 spa_history_list_t
*shl
= spa_import_progress_list
;
2168 spa_import_progress_t
*sip
;
2174 mutex_enter(&shl
->procfs_list
.pl_lock
);
2175 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2176 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2177 if (sip
->pool_guid
== pool_guid
) {
2178 sip
->spa_load_max_txg
= load_max_txg
;
2183 mutex_exit(&shl
->procfs_list
.pl_lock
);
2189 spa_import_progress_set_mmp_check(uint64_t pool_guid
,
2190 uint64_t mmp_sec_remaining
)
2192 spa_history_list_t
*shl
= spa_import_progress_list
;
2193 spa_import_progress_t
*sip
;
2199 mutex_enter(&shl
->procfs_list
.pl_lock
);
2200 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2201 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2202 if (sip
->pool_guid
== pool_guid
) {
2203 sip
->mmp_sec_remaining
= mmp_sec_remaining
;
2208 mutex_exit(&shl
->procfs_list
.pl_lock
);
2214 * A new import is in progress, add an entry.
2217 spa_import_progress_add(spa_t
*spa
)
2219 spa_history_list_t
*shl
= spa_import_progress_list
;
2220 spa_import_progress_t
*sip
;
2221 char *poolname
= NULL
;
2223 sip
= kmem_zalloc(sizeof (spa_import_progress_t
), KM_SLEEP
);
2224 sip
->pool_guid
= spa_guid(spa
);
2226 (void) nvlist_lookup_string(spa
->spa_config
, ZPOOL_CONFIG_POOL_NAME
,
2228 if (poolname
== NULL
)
2229 poolname
= spa_name(spa
);
2230 sip
->pool_name
= spa_strdup(poolname
);
2231 sip
->spa_load_state
= spa_load_state(spa
);
2233 mutex_enter(&shl
->procfs_list
.pl_lock
);
2234 procfs_list_add(&shl
->procfs_list
, sip
);
2236 mutex_exit(&shl
->procfs_list
.pl_lock
);
2240 spa_import_progress_remove(uint64_t pool_guid
)
2242 spa_history_list_t
*shl
= spa_import_progress_list
;
2243 spa_import_progress_t
*sip
;
2245 mutex_enter(&shl
->procfs_list
.pl_lock
);
2246 for (sip
= list_tail(&shl
->procfs_list
.pl_list
); sip
!= NULL
;
2247 sip
= list_prev(&shl
->procfs_list
.pl_list
, sip
)) {
2248 if (sip
->pool_guid
== pool_guid
) {
2250 spa_strfree(sip
->pool_name
);
2251 list_remove(&shl
->procfs_list
.pl_list
, sip
);
2253 kmem_free(sip
, sizeof (spa_import_progress_t
));
2257 mutex_exit(&shl
->procfs_list
.pl_lock
);
2261 * ==========================================================================
2262 * Initialization and Termination
2263 * ==========================================================================
2267 spa_name_compare(const void *a1
, const void *a2
)
2269 const spa_t
*s1
= a1
;
2270 const spa_t
*s2
= a2
;
2273 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
2275 return (TREE_ISIGN(s
));
2285 spa_init(spa_mode_t mode
)
2287 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2288 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2289 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2290 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
2292 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
2293 offsetof(spa_t
, spa_avl
));
2295 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
2296 offsetof(spa_aux_t
, aux_avl
));
2298 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
2299 offsetof(spa_aux_t
, aux_avl
));
2301 spa_mode_global
= mode
;
2304 if (spa_mode_global
!= SPA_MODE_READ
&& dprintf_find_string("watch")) {
2305 struct sigaction sa
;
2307 sa
.sa_flags
= SA_SIGINFO
;
2308 sigemptyset(&sa
.sa_mask
);
2309 sa
.sa_sigaction
= arc_buf_sigsegv
;
2311 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
2312 perror("could not enable watchpoints: "
2313 "sigaction(SIGSEGV, ...) = ");
2321 zfs_refcount_init();
2324 metaslab_stat_init();
2329 vdev_cache_stat_init();
2330 vdev_mirror_stat_init();
2331 vdev_raidz_math_init();
2335 zpool_feature_init();
2340 spa_import_progress_init();
2351 vdev_cache_stat_fini();
2352 vdev_mirror_stat_fini();
2353 vdev_raidz_math_fini();
2358 metaslab_stat_fini();
2361 zfs_refcount_fini();
2365 spa_import_progress_destroy();
2367 avl_destroy(&spa_namespace_avl
);
2368 avl_destroy(&spa_spare_avl
);
2369 avl_destroy(&spa_l2cache_avl
);
2371 cv_destroy(&spa_namespace_cv
);
2372 mutex_destroy(&spa_namespace_lock
);
2373 mutex_destroy(&spa_spare_lock
);
2374 mutex_destroy(&spa_l2cache_lock
);
2378 * Return whether this pool has slogs. No locking needed.
2379 * It's not a problem if the wrong answer is returned as it's only for
2380 * performance and not correctness
2383 spa_has_slogs(spa_t
*spa
)
2385 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
2389 spa_get_log_state(spa_t
*spa
)
2391 return (spa
->spa_log_state
);
2395 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
2397 spa
->spa_log_state
= state
;
2401 spa_is_root(spa_t
*spa
)
2403 return (spa
->spa_is_root
);
2407 spa_writeable(spa_t
*spa
)
2409 return (!!(spa
->spa_mode
& SPA_MODE_WRITE
) && spa
->spa_trust_config
);
2413 * Returns true if there is a pending sync task in any of the current
2414 * syncing txg, the current quiescing txg, or the current open txg.
2417 spa_has_pending_synctask(spa_t
*spa
)
2419 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
) ||
2420 !txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_early_sync_tasks
));
2424 spa_mode(spa_t
*spa
)
2426 return (spa
->spa_mode
);
2430 spa_bootfs(spa_t
*spa
)
2432 return (spa
->spa_bootfs
);
2436 spa_delegation(spa_t
*spa
)
2438 return (spa
->spa_delegation
);
2442 spa_meta_objset(spa_t
*spa
)
2444 return (spa
->spa_meta_objset
);
2448 spa_dedup_checksum(spa_t
*spa
)
2450 return (spa
->spa_dedup_checksum
);
2454 * Reset pool scan stat per scan pass (or reboot).
2457 spa_scan_stat_init(spa_t
*spa
)
2459 /* data not stored on disk */
2460 spa
->spa_scan_pass_start
= gethrestime_sec();
2461 if (dsl_scan_is_paused_scrub(spa
->spa_dsl_pool
->dp_scan
))
2462 spa
->spa_scan_pass_scrub_pause
= spa
->spa_scan_pass_start
;
2464 spa
->spa_scan_pass_scrub_pause
= 0;
2465 spa
->spa_scan_pass_scrub_spent_paused
= 0;
2466 spa
->spa_scan_pass_exam
= 0;
2467 spa
->spa_scan_pass_issued
= 0;
2468 vdev_scan_stat_init(spa
->spa_root_vdev
);
2472 * Get scan stats for zpool status reports
2475 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
2477 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
2479 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
2480 return (SET_ERROR(ENOENT
));
2481 bzero(ps
, sizeof (pool_scan_stat_t
));
2483 /* data stored on disk */
2484 ps
->pss_func
= scn
->scn_phys
.scn_func
;
2485 ps
->pss_state
= scn
->scn_phys
.scn_state
;
2486 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
2487 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
2488 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
2489 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
2490 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
2491 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
2492 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
2494 /* data not stored on disk */
2495 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
2496 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
2497 ps
->pss_pass_scrub_pause
= spa
->spa_scan_pass_scrub_pause
;
2498 ps
->pss_pass_scrub_spent_paused
= spa
->spa_scan_pass_scrub_spent_paused
;
2499 ps
->pss_pass_issued
= spa
->spa_scan_pass_issued
;
2501 scn
->scn_issued_before_pass
+ spa
->spa_scan_pass_issued
;
2507 spa_maxblocksize(spa_t
*spa
)
2509 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
2510 return (SPA_MAXBLOCKSIZE
);
2512 return (SPA_OLD_MAXBLOCKSIZE
);
2517 * Returns the txg that the last device removal completed. No indirect mappings
2518 * have been added since this txg.
2521 spa_get_last_removal_txg(spa_t
*spa
)
2524 uint64_t ret
= -1ULL;
2526 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2528 * sr_prev_indirect_vdev is only modified while holding all the
2529 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2532 vdevid
= spa
->spa_removing_phys
.sr_prev_indirect_vdev
;
2534 while (vdevid
!= -1ULL) {
2535 vdev_t
*vd
= vdev_lookup_top(spa
, vdevid
);
2536 vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
;
2538 ASSERT3P(vd
->vdev_ops
, ==, &vdev_indirect_ops
);
2541 * If the removal did not remap any data, we don't care.
2543 if (vdev_indirect_births_count(vib
) != 0) {
2544 ret
= vdev_indirect_births_last_entry_txg(vib
);
2548 vdevid
= vd
->vdev_indirect_config
.vic_prev_indirect_vdev
;
2550 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2553 spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
2559 spa_maxdnodesize(spa_t
*spa
)
2561 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2562 return (DNODE_MAX_SIZE
);
2564 return (DNODE_MIN_SIZE
);
2568 spa_multihost(spa_t
*spa
)
2570 return (spa
->spa_multihost
? B_TRUE
: B_FALSE
);
2574 spa_get_hostid(spa_t
*spa
)
2576 return (spa
->spa_hostid
);
2580 spa_trust_config(spa_t
*spa
)
2582 return (spa
->spa_trust_config
);
2586 spa_missing_tvds_allowed(spa_t
*spa
)
2588 return (spa
->spa_missing_tvds_allowed
);
2592 spa_syncing_log_sm(spa_t
*spa
)
2594 return (spa
->spa_syncing_log_sm
);
2598 spa_set_missing_tvds(spa_t
*spa
, uint64_t missing
)
2600 spa
->spa_missing_tvds
= missing
;
2604 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2607 spa_state_to_name(spa_t
*spa
)
2609 ASSERT3P(spa
, !=, NULL
);
2612 * it is possible for the spa to exist, without root vdev
2613 * as the spa transitions during import/export
2615 vdev_t
*rvd
= spa
->spa_root_vdev
;
2617 return ("TRANSITIONING");
2619 vdev_state_t state
= rvd
->vdev_state
;
2620 vdev_aux_t aux
= rvd
->vdev_stat
.vs_aux
;
2622 if (spa_suspended(spa
) &&
2623 (spa_get_failmode(spa
) != ZIO_FAILURE_MODE_CONTINUE
))
2624 return ("SUSPENDED");
2627 case VDEV_STATE_CLOSED
:
2628 case VDEV_STATE_OFFLINE
:
2630 case VDEV_STATE_REMOVED
:
2632 case VDEV_STATE_CANT_OPEN
:
2633 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
2635 else if (aux
== VDEV_AUX_SPLIT_POOL
)
2639 case VDEV_STATE_FAULTED
:
2641 case VDEV_STATE_DEGRADED
:
2642 return ("DEGRADED");
2643 case VDEV_STATE_HEALTHY
:
2653 spa_top_vdevs_spacemap_addressable(spa_t
*spa
)
2655 vdev_t
*rvd
= spa
->spa_root_vdev
;
2656 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2657 if (!vdev_is_spacemap_addressable(rvd
->vdev_child
[c
]))
2664 spa_has_checkpoint(spa_t
*spa
)
2666 return (spa
->spa_checkpoint_txg
!= 0);
2670 spa_importing_readonly_checkpoint(spa_t
*spa
)
2672 return ((spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
) &&
2673 spa
->spa_mode
== SPA_MODE_READ
);
2677 spa_min_claim_txg(spa_t
*spa
)
2679 uint64_t checkpoint_txg
= spa
->spa_uberblock
.ub_checkpoint_txg
;
2681 if (checkpoint_txg
!= 0)
2682 return (checkpoint_txg
+ 1);
2684 return (spa
->spa_first_txg
);
2688 * If there is a checkpoint, async destroys may consume more space from
2689 * the pool instead of freeing it. In an attempt to save the pool from
2690 * getting suspended when it is about to run out of space, we stop
2691 * processing async destroys.
2694 spa_suspend_async_destroy(spa_t
*spa
)
2696 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2698 uint64_t unreserved
= dsl_pool_unreserved_space(dp
,
2699 ZFS_SPACE_CHECK_EXTRA_RESERVED
);
2700 uint64_t used
= dsl_dir_phys(dp
->dp_root_dir
)->dd_used_bytes
;
2701 uint64_t avail
= (unreserved
> used
) ? (unreserved
- used
) : 0;
2703 if (spa_has_checkpoint(spa
) && avail
== 0)
2709 #if defined(_KERNEL)
2712 param_set_deadman_failmode_common(const char *val
)
2718 return (SET_ERROR(EINVAL
));
2720 if ((p
= strchr(val
, '\n')) != NULL
)
2723 if (strcmp(val
, "wait") != 0 && strcmp(val
, "continue") != 0 &&
2724 strcmp(val
, "panic"))
2725 return (SET_ERROR(EINVAL
));
2727 if (spa_mode_global
!= SPA_MODE_UNINIT
) {
2728 mutex_enter(&spa_namespace_lock
);
2729 while ((spa
= spa_next(spa
)) != NULL
)
2730 spa_set_deadman_failmode(spa
, val
);
2731 mutex_exit(&spa_namespace_lock
);
2738 /* Namespace manipulation */
2739 EXPORT_SYMBOL(spa_lookup
);
2740 EXPORT_SYMBOL(spa_add
);
2741 EXPORT_SYMBOL(spa_remove
);
2742 EXPORT_SYMBOL(spa_next
);
2744 /* Refcount functions */
2745 EXPORT_SYMBOL(spa_open_ref
);
2746 EXPORT_SYMBOL(spa_close
);
2747 EXPORT_SYMBOL(spa_refcount_zero
);
2749 /* Pool configuration lock */
2750 EXPORT_SYMBOL(spa_config_tryenter
);
2751 EXPORT_SYMBOL(spa_config_enter
);
2752 EXPORT_SYMBOL(spa_config_exit
);
2753 EXPORT_SYMBOL(spa_config_held
);
2755 /* Pool vdev add/remove lock */
2756 EXPORT_SYMBOL(spa_vdev_enter
);
2757 EXPORT_SYMBOL(spa_vdev_exit
);
2759 /* Pool vdev state change lock */
2760 EXPORT_SYMBOL(spa_vdev_state_enter
);
2761 EXPORT_SYMBOL(spa_vdev_state_exit
);
2763 /* Accessor functions */
2764 EXPORT_SYMBOL(spa_shutting_down
);
2765 EXPORT_SYMBOL(spa_get_dsl
);
2766 EXPORT_SYMBOL(spa_get_rootblkptr
);
2767 EXPORT_SYMBOL(spa_set_rootblkptr
);
2768 EXPORT_SYMBOL(spa_altroot
);
2769 EXPORT_SYMBOL(spa_sync_pass
);
2770 EXPORT_SYMBOL(spa_name
);
2771 EXPORT_SYMBOL(spa_guid
);
2772 EXPORT_SYMBOL(spa_last_synced_txg
);
2773 EXPORT_SYMBOL(spa_first_txg
);
2774 EXPORT_SYMBOL(spa_syncing_txg
);
2775 EXPORT_SYMBOL(spa_version
);
2776 EXPORT_SYMBOL(spa_state
);
2777 EXPORT_SYMBOL(spa_load_state
);
2778 EXPORT_SYMBOL(spa_freeze_txg
);
2779 EXPORT_SYMBOL(spa_get_dspace
);
2780 EXPORT_SYMBOL(spa_update_dspace
);
2781 EXPORT_SYMBOL(spa_deflate
);
2782 EXPORT_SYMBOL(spa_normal_class
);
2783 EXPORT_SYMBOL(spa_log_class
);
2784 EXPORT_SYMBOL(spa_special_class
);
2785 EXPORT_SYMBOL(spa_preferred_class
);
2786 EXPORT_SYMBOL(spa_max_replication
);
2787 EXPORT_SYMBOL(spa_prev_software_version
);
2788 EXPORT_SYMBOL(spa_get_failmode
);
2789 EXPORT_SYMBOL(spa_suspended
);
2790 EXPORT_SYMBOL(spa_bootfs
);
2791 EXPORT_SYMBOL(spa_delegation
);
2792 EXPORT_SYMBOL(spa_meta_objset
);
2793 EXPORT_SYMBOL(spa_maxblocksize
);
2794 EXPORT_SYMBOL(spa_maxdnodesize
);
2796 /* Miscellaneous support routines */
2797 EXPORT_SYMBOL(spa_guid_exists
);
2798 EXPORT_SYMBOL(spa_strdup
);
2799 EXPORT_SYMBOL(spa_strfree
);
2800 EXPORT_SYMBOL(spa_get_random
);
2801 EXPORT_SYMBOL(spa_generate_guid
);
2802 EXPORT_SYMBOL(snprintf_blkptr
);
2803 EXPORT_SYMBOL(spa_freeze
);
2804 EXPORT_SYMBOL(spa_upgrade
);
2805 EXPORT_SYMBOL(spa_evict_all
);
2806 EXPORT_SYMBOL(spa_lookup_by_guid
);
2807 EXPORT_SYMBOL(spa_has_spare
);
2808 EXPORT_SYMBOL(dva_get_dsize_sync
);
2809 EXPORT_SYMBOL(bp_get_dsize_sync
);
2810 EXPORT_SYMBOL(bp_get_dsize
);
2811 EXPORT_SYMBOL(spa_has_slogs
);
2812 EXPORT_SYMBOL(spa_is_root
);
2813 EXPORT_SYMBOL(spa_writeable
);
2814 EXPORT_SYMBOL(spa_mode
);
2815 EXPORT_SYMBOL(spa_namespace_lock
);
2816 EXPORT_SYMBOL(spa_trust_config
);
2817 EXPORT_SYMBOL(spa_missing_tvds_allowed
);
2818 EXPORT_SYMBOL(spa_set_missing_tvds
);
2819 EXPORT_SYMBOL(spa_state_to_name
);
2820 EXPORT_SYMBOL(spa_importing_readonly_checkpoint
);
2821 EXPORT_SYMBOL(spa_min_claim_txg
);
2822 EXPORT_SYMBOL(spa_suspend_async_destroy
);
2823 EXPORT_SYMBOL(spa_has_checkpoint
);
2824 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable
);
2826 ZFS_MODULE_PARAM(zfs
, zfs_
, flags
, UINT
, ZMOD_RW
,
2827 "Set additional debugging flags");
2829 ZFS_MODULE_PARAM(zfs
, zfs_
, recover
, INT
, ZMOD_RW
,
2830 "Set to attempt to recover from fatal errors");
2832 ZFS_MODULE_PARAM(zfs
, zfs_
, free_leak_on_eio
, INT
, ZMOD_RW
,
2833 "Set to ignore IO errors during free and permanently leak the space");
2835 ZFS_MODULE_PARAM(zfs
, zfs_
, deadman_checktime_ms
, ULONG
, ZMOD_RW
,
2836 "Dead I/O check interval in milliseconds");
2838 ZFS_MODULE_PARAM(zfs
, zfs_
, deadman_enabled
, INT
, ZMOD_RW
,
2839 "Enable deadman timer");
2841 ZFS_MODULE_PARAM(zfs_spa
, spa_
, asize_inflation
, INT
, ZMOD_RW
,
2842 "SPA size estimate multiplication factor");
2844 ZFS_MODULE_PARAM(zfs
, zfs_
, ddt_data_is_special
, INT
, ZMOD_RW
,
2845 "Place DDT data into the special class");
2847 ZFS_MODULE_PARAM(zfs
, zfs_
, user_indirect_is_special
, INT
, ZMOD_RW
,
2848 "Place user data indirect blocks into the special class");
2851 ZFS_MODULE_PARAM_CALL(zfs_deadman
, zfs_deadman_
, failmode
,
2852 param_set_deadman_failmode
, param_get_charp
, ZMOD_RW
,
2853 "Failmode for deadman timer");
2855 ZFS_MODULE_PARAM_CALL(zfs_deadman
, zfs_deadman_
, synctime_ms
,
2856 param_set_deadman_synctime
, param_get_ulong
, ZMOD_RW
,
2857 "Pool sync expiration time in milliseconds");
2859 ZFS_MODULE_PARAM_CALL(zfs_deadman
, zfs_deadman_
, ziotime_ms
,
2860 param_set_deadman_ziotime
, param_get_ulong
, ZMOD_RW
,
2861 "IO expiration time in milliseconds");
2863 ZFS_MODULE_PARAM(zfs
, zfs_
, special_class_metadata_reserve_pct
, INT
, ZMOD_RW
,
2864 "Small file blocks in special vdevs depends on this much "
2865 "free space available");
2868 ZFS_MODULE_PARAM_CALL(spa
, spa_
, slop_shift
, param_set_slop_shift
,
2869 param_get_int
, ZMOD_RW
, "Reserved free space in pool");