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_file.h>
42 #include <sys/vdev_raidz.h>
43 #include <sys/metaslab.h>
44 #include <sys/uberblock_impl.h>
47 #include <sys/unique.h>
48 #include <sys/dsl_pool.h>
49 #include <sys/dsl_dir.h>
50 #include <sys/dsl_prop.h>
51 #include <sys/fm/util.h>
52 #include <sys/dsl_scan.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/metaslab_impl.h>
57 #include <sys/kstat.h>
59 #include <sys/zfeature.h>
65 * There are four basic locks for managing spa_t structures:
67 * spa_namespace_lock (global mutex)
69 * This lock must be acquired to do any of the following:
71 * - Lookup a spa_t by name
72 * - Add or remove a spa_t from the namespace
73 * - Increase spa_refcount from non-zero
74 * - Check if spa_refcount is zero
76 * - add/remove/attach/detach devices
77 * - Held for the duration of create/destroy/import/export
79 * It does not need to handle recursion. A create or destroy may
80 * reference objects (files or zvols) in other pools, but by
81 * definition they must have an existing reference, and will never need
82 * to lookup a spa_t by name.
84 * spa_refcount (per-spa zfs_refcount_t protected by mutex)
86 * This reference count keep track of any active users of the spa_t. The
87 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
88 * the refcount is never really 'zero' - opening a pool implicitly keeps
89 * some references in the DMU. Internally we check against spa_minref, but
90 * present the image of a zero/non-zero value to consumers.
92 * spa_config_lock[] (per-spa array of rwlocks)
94 * This protects the spa_t from config changes, and must be held in
95 * the following circumstances:
97 * - RW_READER to perform I/O to the spa
98 * - RW_WRITER to change the vdev config
100 * The locking order is fairly straightforward:
102 * spa_namespace_lock -> spa_refcount
104 * The namespace lock must be acquired to increase the refcount from 0
105 * or to check if it is zero.
107 * spa_refcount -> spa_config_lock[]
109 * There must be at least one valid reference on the spa_t to acquire
112 * spa_namespace_lock -> spa_config_lock[]
114 * The namespace lock must always be taken before the config lock.
117 * The spa_namespace_lock can be acquired directly and is globally visible.
119 * The namespace is manipulated using the following functions, all of which
120 * require the spa_namespace_lock to be held.
122 * spa_lookup() Lookup a spa_t by name.
124 * spa_add() Create a new spa_t in the namespace.
126 * spa_remove() Remove a spa_t from the namespace. This also
127 * frees up any memory associated with the spa_t.
129 * spa_next() Returns the next spa_t in the system, or the
130 * first if NULL is passed.
132 * spa_evict_all() Shutdown and remove all spa_t structures in
135 * spa_guid_exists() Determine whether a pool/device guid exists.
137 * The spa_refcount is manipulated using the following functions:
139 * spa_open_ref() Adds a reference to the given spa_t. Must be
140 * called with spa_namespace_lock held if the
141 * refcount is currently zero.
143 * spa_close() Remove a reference from the spa_t. This will
144 * not free the spa_t or remove it from the
145 * namespace. No locking is required.
147 * spa_refcount_zero() Returns true if the refcount is currently
148 * zero. Must be called with spa_namespace_lock
151 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
152 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
153 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
155 * To read the configuration, it suffices to hold one of these locks as reader.
156 * To modify the configuration, you must hold all locks as writer. To modify
157 * vdev state without altering the vdev tree's topology (e.g. online/offline),
158 * you must hold SCL_STATE and SCL_ZIO as writer.
160 * We use these distinct config locks to avoid recursive lock entry.
161 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
162 * block allocations (SCL_ALLOC), which may require reading space maps
163 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
165 * The spa config locks cannot be normal rwlocks because we need the
166 * ability to hand off ownership. For example, SCL_ZIO is acquired
167 * by the issuing thread and later released by an interrupt thread.
168 * They do, however, obey the usual write-wanted semantics to prevent
169 * writer (i.e. system administrator) starvation.
171 * The lock acquisition rules are as follows:
174 * Protects changes to the vdev tree topology, such as vdev
175 * add/remove/attach/detach. Protects the dirty config list
176 * (spa_config_dirty_list) and the set of spares and l2arc devices.
179 * Protects changes to pool state and vdev state, such as vdev
180 * online/offline/fault/degrade/clear. Protects the dirty state list
181 * (spa_state_dirty_list) and global pool state (spa_state).
184 * Protects changes to metaslab groups and classes.
185 * Held as reader by metaslab_alloc() and metaslab_claim().
188 * Held by bp-level zios (those which have no io_vd upon entry)
189 * to prevent changes to the vdev tree. The bp-level zio implicitly
190 * protects all of its vdev child zios, which do not hold SCL_ZIO.
193 * Protects changes to metaslab groups and classes.
194 * Held as reader by metaslab_free(). SCL_FREE is distinct from
195 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
196 * blocks in zio_done() while another i/o that holds either
197 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
200 * Held as reader to prevent changes to the vdev tree during trivial
201 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
202 * other locks, and lower than all of them, to ensure that it's safe
203 * to acquire regardless of caller context.
205 * In addition, the following rules apply:
207 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
208 * The lock ordering is SCL_CONFIG > spa_props_lock.
210 * (b) I/O operations on leaf vdevs. For any zio operation that takes
211 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
212 * or zio_write_phys() -- the caller must ensure that the config cannot
213 * cannot change in the interim, and that the vdev cannot be reopened.
214 * SCL_STATE as reader suffices for both.
216 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
218 * spa_vdev_enter() Acquire the namespace lock and the config lock
221 * spa_vdev_exit() Release the config lock, wait for all I/O
222 * to complete, sync the updated configs to the
223 * cache, and release the namespace lock.
225 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
226 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
227 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
229 * spa_rename() is also implemented within this file since it requires
230 * manipulation of the namespace.
233 static avl_tree_t spa_namespace_avl
;
234 kmutex_t spa_namespace_lock
;
235 static kcondvar_t spa_namespace_cv
;
236 int spa_max_replication_override
= SPA_DVAS_PER_BP
;
238 static kmutex_t spa_spare_lock
;
239 static avl_tree_t spa_spare_avl
;
240 static kmutex_t spa_l2cache_lock
;
241 static avl_tree_t spa_l2cache_avl
;
243 kmem_cache_t
*spa_buffer_pool
;
248 * Everything except dprintf, set_error, spa, and indirect_remap is on
249 * by default in debug builds.
251 int zfs_flags
= ~(ZFS_DEBUG_DPRINTF
| ZFS_DEBUG_SET_ERROR
|
252 ZFS_DEBUG_INDIRECT_REMAP
);
258 * zfs_recover can be set to nonzero to attempt to recover from
259 * otherwise-fatal errors, typically caused by on-disk corruption. When
260 * set, calls to zfs_panic_recover() will turn into warning messages.
261 * This should only be used as a last resort, as it typically results
262 * in leaked space, or worse.
264 int zfs_recover
= B_FALSE
;
267 * If destroy encounters an EIO while reading metadata (e.g. indirect
268 * blocks), space referenced by the missing metadata can not be freed.
269 * Normally this causes the background destroy to become "stalled", as
270 * it is unable to make forward progress. While in this stalled state,
271 * all remaining space to free from the error-encountering filesystem is
272 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
273 * permanently leak the space from indirect blocks that can not be read,
274 * and continue to free everything else that it can.
276 * The default, "stalling" behavior is useful if the storage partially
277 * fails (i.e. some but not all i/os fail), and then later recovers. In
278 * this case, we will be able to continue pool operations while it is
279 * partially failed, and when it recovers, we can continue to free the
280 * space, with no leaks. However, note that this case is actually
283 * Typically pools either (a) fail completely (but perhaps temporarily,
284 * e.g. a top-level vdev going offline), or (b) have localized,
285 * permanent errors (e.g. disk returns the wrong data due to bit flip or
286 * firmware bug). In case (a), this setting does not matter because the
287 * pool will be suspended and the sync thread will not be able to make
288 * forward progress regardless. In case (b), because the error is
289 * permanent, the best we can do is leak the minimum amount of space,
290 * which is what setting this flag will do. Therefore, it is reasonable
291 * for this flag to normally be set, but we chose the more conservative
292 * approach of not setting it, so that there is no possibility of
293 * leaking space in the "partial temporary" failure case.
295 int zfs_free_leak_on_eio
= B_FALSE
;
298 * Expiration time in milliseconds. This value has two meanings. First it is
299 * used to determine when the spa_deadman() logic should fire. By default the
300 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds.
301 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
302 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
303 * in one of three behaviors controlled by zfs_deadman_failmode.
305 unsigned long zfs_deadman_synctime_ms
= 600000ULL;
308 * This value controls the maximum amount of time zio_wait() will block for an
309 * outstanding IO. By default this is 300 seconds at which point the "hung"
310 * behavior will be applied as described for zfs_deadman_synctime_ms.
312 unsigned long zfs_deadman_ziotime_ms
= 300000ULL;
315 * Check time in milliseconds. This defines the frequency at which we check
318 unsigned long zfs_deadman_checktime_ms
= 60000ULL;
321 * By default the deadman is enabled.
323 int zfs_deadman_enabled
= 1;
326 * Controls the behavior of the deadman when it detects a "hung" I/O.
327 * Valid values are zfs_deadman_failmode=<wait|continue|panic>.
329 * wait - Wait for the "hung" I/O (default)
330 * continue - Attempt to recover from a "hung" I/O
331 * panic - Panic the system
333 char *zfs_deadman_failmode
= "wait";
336 * The worst case is single-sector max-parity RAID-Z blocks, in which
337 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
338 * times the size; so just assume that. Add to this the fact that
339 * we can have up to 3 DVAs per bp, and one more factor of 2 because
340 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
342 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
344 int spa_asize_inflation
= 24;
347 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
348 * the pool to be consumed. This ensures that we don't run the pool
349 * completely out of space, due to unaccounted changes (e.g. to the MOS).
350 * It also limits the worst-case time to allocate space. If we have
351 * less than this amount of free space, most ZPL operations (e.g. write,
352 * create) will return ENOSPC.
354 * Certain operations (e.g. file removal, most administrative actions) can
355 * use half the slop space. They will only return ENOSPC if less than half
356 * the slop space is free. Typically, once the pool has less than the slop
357 * space free, the user will use these operations to free up space in the pool.
358 * These are the operations that call dsl_pool_adjustedsize() with the netfree
359 * argument set to TRUE.
361 * Operations that are almost guaranteed to free up space in the absence of
362 * a pool checkpoint can use up to three quarters of the slop space
365 * A very restricted set of operations are always permitted, regardless of
366 * the amount of free space. These are the operations that call
367 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net
368 * increase in the amount of space used, it is possible to run the pool
369 * completely out of space, causing it to be permanently read-only.
371 * Note that on very small pools, the slop space will be larger than
372 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
373 * but we never allow it to be more than half the pool size.
375 * See also the comments in zfs_space_check_t.
377 int spa_slop_shift
= 5;
378 uint64_t spa_min_slop
= 128 * 1024 * 1024;
379 int spa_allocators
= 4;
384 spa_load_failed(spa_t
*spa
, const char *fmt
, ...)
390 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
393 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa
->spa_name
,
394 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
399 spa_load_note(spa_t
*spa
, const char *fmt
, ...)
405 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
408 zfs_dbgmsg("spa_load(%s, config %s): %s", spa
->spa_name
,
409 spa
->spa_trust_config
? "trusted" : "untrusted", buf
);
413 * By default dedup and user data indirects land in the special class
415 int zfs_ddt_data_is_special
= B_TRUE
;
416 int zfs_user_indirect_is_special
= B_TRUE
;
419 * The percentage of special class final space reserved for metadata only.
420 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only
421 * let metadata into the class.
423 int zfs_special_class_metadata_reserve_pct
= 25;
426 * ==========================================================================
428 * ==========================================================================
431 spa_config_lock_init(spa_t
*spa
)
433 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
434 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
435 mutex_init(&scl
->scl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
436 cv_init(&scl
->scl_cv
, NULL
, CV_DEFAULT
, NULL
);
437 zfs_refcount_create_untracked(&scl
->scl_count
);
438 scl
->scl_writer
= NULL
;
439 scl
->scl_write_wanted
= 0;
444 spa_config_lock_destroy(spa_t
*spa
)
446 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
447 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
448 mutex_destroy(&scl
->scl_lock
);
449 cv_destroy(&scl
->scl_cv
);
450 zfs_refcount_destroy(&scl
->scl_count
);
451 ASSERT(scl
->scl_writer
== NULL
);
452 ASSERT(scl
->scl_write_wanted
== 0);
457 spa_config_tryenter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
459 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
460 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
461 if (!(locks
& (1 << i
)))
463 mutex_enter(&scl
->scl_lock
);
464 if (rw
== RW_READER
) {
465 if (scl
->scl_writer
|| scl
->scl_write_wanted
) {
466 mutex_exit(&scl
->scl_lock
);
467 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
472 ASSERT(scl
->scl_writer
!= curthread
);
473 if (!zfs_refcount_is_zero(&scl
->scl_count
)) {
474 mutex_exit(&scl
->scl_lock
);
475 spa_config_exit(spa
, locks
& ((1 << i
) - 1),
479 scl
->scl_writer
= curthread
;
481 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
482 mutex_exit(&scl
->scl_lock
);
488 spa_config_enter(spa_t
*spa
, int locks
, void *tag
, krw_t rw
)
492 ASSERT3U(SCL_LOCKS
, <, sizeof (wlocks_held
) * NBBY
);
494 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
495 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
496 if (scl
->scl_writer
== curthread
)
497 wlocks_held
|= (1 << i
);
498 if (!(locks
& (1 << i
)))
500 mutex_enter(&scl
->scl_lock
);
501 if (rw
== RW_READER
) {
502 while (scl
->scl_writer
|| scl
->scl_write_wanted
) {
503 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
506 ASSERT(scl
->scl_writer
!= curthread
);
507 while (!zfs_refcount_is_zero(&scl
->scl_count
)) {
508 scl
->scl_write_wanted
++;
509 cv_wait(&scl
->scl_cv
, &scl
->scl_lock
);
510 scl
->scl_write_wanted
--;
512 scl
->scl_writer
= curthread
;
514 (void) zfs_refcount_add(&scl
->scl_count
, tag
);
515 mutex_exit(&scl
->scl_lock
);
517 ASSERT3U(wlocks_held
, <=, locks
);
521 spa_config_exit(spa_t
*spa
, int locks
, void *tag
)
523 for (int i
= SCL_LOCKS
- 1; i
>= 0; i
--) {
524 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
525 if (!(locks
& (1 << i
)))
527 mutex_enter(&scl
->scl_lock
);
528 ASSERT(!zfs_refcount_is_zero(&scl
->scl_count
));
529 if (zfs_refcount_remove(&scl
->scl_count
, tag
) == 0) {
530 ASSERT(scl
->scl_writer
== NULL
||
531 scl
->scl_writer
== curthread
);
532 scl
->scl_writer
= NULL
; /* OK in either case */
533 cv_broadcast(&scl
->scl_cv
);
535 mutex_exit(&scl
->scl_lock
);
540 spa_config_held(spa_t
*spa
, int locks
, krw_t rw
)
544 for (int i
= 0; i
< SCL_LOCKS
; i
++) {
545 spa_config_lock_t
*scl
= &spa
->spa_config_lock
[i
];
546 if (!(locks
& (1 << i
)))
548 if ((rw
== RW_READER
&&
549 !zfs_refcount_is_zero(&scl
->scl_count
)) ||
550 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
551 locks_held
|= 1 << i
;
558 * ==========================================================================
559 * SPA namespace functions
560 * ==========================================================================
564 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
565 * Returns NULL if no matching spa_t is found.
568 spa_lookup(const char *name
)
570 static spa_t search
; /* spa_t is large; don't allocate on stack */
575 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
577 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
580 * If it's a full dataset name, figure out the pool name and
583 cp
= strpbrk(search
.spa_name
, "/@#");
587 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
593 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
594 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
595 * looking for potentially hung I/Os.
598 spa_deadman(void *arg
)
602 /* Disable the deadman if the pool is suspended. */
603 if (spa_suspended(spa
))
606 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
607 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
608 ++spa
->spa_deadman_calls
);
609 if (zfs_deadman_enabled
)
610 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
612 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
613 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
614 MSEC_TO_TICK(zfs_deadman_checktime_ms
));
618 * Create an uninitialized spa_t with the given name. Requires
619 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
620 * exist by calling spa_lookup() first.
623 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
626 spa_config_dirent_t
*dp
;
628 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
630 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
632 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
633 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
634 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
635 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
636 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
637 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
638 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
639 mutex_init(&spa
->spa_cksum_tmpls_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
640 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
641 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
642 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
643 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
645 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
646 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
647 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
648 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
649 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
651 for (int t
= 0; t
< TXG_SIZE
; t
++)
652 bplist_create(&spa
->spa_free_bplist
[t
]);
654 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
655 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
656 spa
->spa_freeze_txg
= UINT64_MAX
;
657 spa
->spa_final_txg
= UINT64_MAX
;
658 spa
->spa_load_max_txg
= UINT64_MAX
;
660 spa
->spa_proc_state
= SPA_PROC_NONE
;
661 spa
->spa_trust_config
= B_TRUE
;
663 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
664 spa
->spa_deadman_ziotime
= MSEC2NSEC(zfs_deadman_ziotime_ms
);
665 spa_set_deadman_failmode(spa
, zfs_deadman_failmode
);
667 zfs_refcount_create(&spa
->spa_refcount
);
668 spa_config_lock_init(spa
);
671 avl_add(&spa_namespace_avl
, spa
);
674 * Set the alternate root, if there is one.
677 spa
->spa_root
= spa_strdup(altroot
);
679 spa
->spa_alloc_count
= spa_allocators
;
680 spa
->spa_alloc_locks
= kmem_zalloc(spa
->spa_alloc_count
*
681 sizeof (kmutex_t
), KM_SLEEP
);
682 spa
->spa_alloc_trees
= kmem_zalloc(spa
->spa_alloc_count
*
683 sizeof (avl_tree_t
), KM_SLEEP
);
684 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
685 mutex_init(&spa
->spa_alloc_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
686 avl_create(&spa
->spa_alloc_trees
[i
], zio_bookmark_compare
,
687 sizeof (zio_t
), offsetof(zio_t
, io_alloc_node
));
691 * Every pool starts with the default cachefile
693 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
694 offsetof(spa_config_dirent_t
, scd_link
));
696 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
697 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
698 list_insert_head(&spa
->spa_config_list
, dp
);
700 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
703 if (config
!= NULL
) {
706 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
708 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
712 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
715 if (spa
->spa_label_features
== NULL
) {
716 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
720 spa
->spa_min_ashift
= INT_MAX
;
721 spa
->spa_max_ashift
= 0;
723 /* Reset cached value */
724 spa
->spa_dedup_dspace
= ~0ULL;
727 * As a pool is being created, treat all features as disabled by
728 * setting SPA_FEATURE_DISABLED for all entries in the feature
731 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
732 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
739 * Removes a spa_t from the namespace, freeing up any memory used. Requires
740 * spa_namespace_lock. This is called only after the spa_t has been closed and
744 spa_remove(spa_t
*spa
)
746 spa_config_dirent_t
*dp
;
748 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
749 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
750 ASSERT3U(zfs_refcount_count(&spa
->spa_refcount
), ==, 0);
752 nvlist_free(spa
->spa_config_splitting
);
754 avl_remove(&spa_namespace_avl
, spa
);
755 cv_broadcast(&spa_namespace_cv
);
758 spa_strfree(spa
->spa_root
);
760 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
761 list_remove(&spa
->spa_config_list
, dp
);
762 if (dp
->scd_path
!= NULL
)
763 spa_strfree(dp
->scd_path
);
764 kmem_free(dp
, sizeof (spa_config_dirent_t
));
767 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
768 avl_destroy(&spa
->spa_alloc_trees
[i
]);
769 mutex_destroy(&spa
->spa_alloc_locks
[i
]);
771 kmem_free(spa
->spa_alloc_locks
, spa
->spa_alloc_count
*
773 kmem_free(spa
->spa_alloc_trees
, spa
->spa_alloc_count
*
774 sizeof (avl_tree_t
));
776 list_destroy(&spa
->spa_config_list
);
778 nvlist_free(spa
->spa_label_features
);
779 nvlist_free(spa
->spa_load_info
);
780 nvlist_free(spa
->spa_feat_stats
);
781 spa_config_set(spa
, NULL
);
783 zfs_refcount_destroy(&spa
->spa_refcount
);
785 spa_stats_destroy(spa
);
786 spa_config_lock_destroy(spa
);
788 for (int t
= 0; t
< TXG_SIZE
; t
++)
789 bplist_destroy(&spa
->spa_free_bplist
[t
]);
791 zio_checksum_templates_free(spa
);
793 cv_destroy(&spa
->spa_async_cv
);
794 cv_destroy(&spa
->spa_evicting_os_cv
);
795 cv_destroy(&spa
->spa_proc_cv
);
796 cv_destroy(&spa
->spa_scrub_io_cv
);
797 cv_destroy(&spa
->spa_suspend_cv
);
799 mutex_destroy(&spa
->spa_async_lock
);
800 mutex_destroy(&spa
->spa_errlist_lock
);
801 mutex_destroy(&spa
->spa_errlog_lock
);
802 mutex_destroy(&spa
->spa_evicting_os_lock
);
803 mutex_destroy(&spa
->spa_history_lock
);
804 mutex_destroy(&spa
->spa_proc_lock
);
805 mutex_destroy(&spa
->spa_props_lock
);
806 mutex_destroy(&spa
->spa_cksum_tmpls_lock
);
807 mutex_destroy(&spa
->spa_scrub_lock
);
808 mutex_destroy(&spa
->spa_suspend_lock
);
809 mutex_destroy(&spa
->spa_vdev_top_lock
);
810 mutex_destroy(&spa
->spa_feat_stats_lock
);
812 kmem_free(spa
, sizeof (spa_t
));
816 * Given a pool, return the next pool in the namespace, or NULL if there is
817 * none. If 'prev' is NULL, return the first pool.
820 spa_next(spa_t
*prev
)
822 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
825 return (AVL_NEXT(&spa_namespace_avl
, prev
));
827 return (avl_first(&spa_namespace_avl
));
831 * ==========================================================================
832 * SPA refcount functions
833 * ==========================================================================
837 * Add a reference to the given spa_t. Must have at least one reference, or
838 * have the namespace lock held.
841 spa_open_ref(spa_t
*spa
, void *tag
)
843 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
844 MUTEX_HELD(&spa_namespace_lock
));
845 (void) zfs_refcount_add(&spa
->spa_refcount
, tag
);
849 * Remove a reference to the given spa_t. Must have at least one reference, or
850 * have the namespace lock held.
853 spa_close(spa_t
*spa
, void *tag
)
855 ASSERT(zfs_refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
856 MUTEX_HELD(&spa_namespace_lock
));
857 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
861 * Remove a reference to the given spa_t held by a dsl dir that is
862 * being asynchronously released. Async releases occur from a taskq
863 * performing eviction of dsl datasets and dirs. The namespace lock
864 * isn't held and the hold by the object being evicted may contribute to
865 * spa_minref (e.g. dataset or directory released during pool export),
866 * so the asserts in spa_close() do not apply.
869 spa_async_close(spa_t
*spa
, void *tag
)
871 (void) zfs_refcount_remove(&spa
->spa_refcount
, tag
);
875 * Check to see if the spa refcount is zero. Must be called with
876 * spa_namespace_lock held. We really compare against spa_minref, which is the
877 * number of references acquired when opening a pool
880 spa_refcount_zero(spa_t
*spa
)
882 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
884 return (zfs_refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
888 * ==========================================================================
889 * SPA spare and l2cache tracking
890 * ==========================================================================
894 * Hot spares and cache devices are tracked using the same code below,
895 * for 'auxiliary' devices.
898 typedef struct spa_aux
{
906 spa_aux_compare(const void *a
, const void *b
)
908 const spa_aux_t
*sa
= (const spa_aux_t
*)a
;
909 const spa_aux_t
*sb
= (const spa_aux_t
*)b
;
911 return (AVL_CMP(sa
->aux_guid
, sb
->aux_guid
));
915 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
921 search
.aux_guid
= vd
->vdev_guid
;
922 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
925 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
926 aux
->aux_guid
= vd
->vdev_guid
;
928 avl_insert(avl
, aux
, where
);
933 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
939 search
.aux_guid
= vd
->vdev_guid
;
940 aux
= avl_find(avl
, &search
, &where
);
944 if (--aux
->aux_count
== 0) {
945 avl_remove(avl
, aux
);
946 kmem_free(aux
, sizeof (spa_aux_t
));
947 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
948 aux
->aux_pool
= 0ULL;
953 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
955 spa_aux_t search
, *found
;
957 search
.aux_guid
= guid
;
958 found
= avl_find(avl
, &search
, NULL
);
962 *pool
= found
->aux_pool
;
969 *refcnt
= found
->aux_count
;
974 return (found
!= NULL
);
978 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
980 spa_aux_t search
, *found
;
983 search
.aux_guid
= vd
->vdev_guid
;
984 found
= avl_find(avl
, &search
, &where
);
985 ASSERT(found
!= NULL
);
986 ASSERT(found
->aux_pool
== 0ULL);
988 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
992 * Spares are tracked globally due to the following constraints:
994 * - A spare may be part of multiple pools.
995 * - A spare may be added to a pool even if it's actively in use within
997 * - A spare in use in any pool can only be the source of a replacement if
998 * the target is a spare in the same pool.
1000 * We keep track of all spares on the system through the use of a reference
1001 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1002 * spare, then we bump the reference count in the AVL tree. In addition, we set
1003 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1004 * inactive). When a spare is made active (used to replace a device in the
1005 * pool), we also keep track of which pool its been made a part of.
1007 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1008 * called under the spa_namespace lock as part of vdev reconfiguration. The
1009 * separate spare lock exists for the status query path, which does not need to
1010 * be completely consistent with respect to other vdev configuration changes.
1014 spa_spare_compare(const void *a
, const void *b
)
1016 return (spa_aux_compare(a
, b
));
1020 spa_spare_add(vdev_t
*vd
)
1022 mutex_enter(&spa_spare_lock
);
1023 ASSERT(!vd
->vdev_isspare
);
1024 spa_aux_add(vd
, &spa_spare_avl
);
1025 vd
->vdev_isspare
= B_TRUE
;
1026 mutex_exit(&spa_spare_lock
);
1030 spa_spare_remove(vdev_t
*vd
)
1032 mutex_enter(&spa_spare_lock
);
1033 ASSERT(vd
->vdev_isspare
);
1034 spa_aux_remove(vd
, &spa_spare_avl
);
1035 vd
->vdev_isspare
= B_FALSE
;
1036 mutex_exit(&spa_spare_lock
);
1040 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
1044 mutex_enter(&spa_spare_lock
);
1045 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
1046 mutex_exit(&spa_spare_lock
);
1052 spa_spare_activate(vdev_t
*vd
)
1054 mutex_enter(&spa_spare_lock
);
1055 ASSERT(vd
->vdev_isspare
);
1056 spa_aux_activate(vd
, &spa_spare_avl
);
1057 mutex_exit(&spa_spare_lock
);
1061 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1062 * Cache devices currently only support one pool per cache device, and so
1063 * for these devices the aux reference count is currently unused beyond 1.
1067 spa_l2cache_compare(const void *a
, const void *b
)
1069 return (spa_aux_compare(a
, b
));
1073 spa_l2cache_add(vdev_t
*vd
)
1075 mutex_enter(&spa_l2cache_lock
);
1076 ASSERT(!vd
->vdev_isl2cache
);
1077 spa_aux_add(vd
, &spa_l2cache_avl
);
1078 vd
->vdev_isl2cache
= B_TRUE
;
1079 mutex_exit(&spa_l2cache_lock
);
1083 spa_l2cache_remove(vdev_t
*vd
)
1085 mutex_enter(&spa_l2cache_lock
);
1086 ASSERT(vd
->vdev_isl2cache
);
1087 spa_aux_remove(vd
, &spa_l2cache_avl
);
1088 vd
->vdev_isl2cache
= B_FALSE
;
1089 mutex_exit(&spa_l2cache_lock
);
1093 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1097 mutex_enter(&spa_l2cache_lock
);
1098 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1099 mutex_exit(&spa_l2cache_lock
);
1105 spa_l2cache_activate(vdev_t
*vd
)
1107 mutex_enter(&spa_l2cache_lock
);
1108 ASSERT(vd
->vdev_isl2cache
);
1109 spa_aux_activate(vd
, &spa_l2cache_avl
);
1110 mutex_exit(&spa_l2cache_lock
);
1114 * ==========================================================================
1116 * ==========================================================================
1120 * Lock the given spa_t for the purpose of adding or removing a vdev.
1121 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1122 * It returns the next transaction group for the spa_t.
1125 spa_vdev_enter(spa_t
*spa
)
1127 mutex_enter(&spa
->spa_vdev_top_lock
);
1128 mutex_enter(&spa_namespace_lock
);
1129 return (spa_vdev_config_enter(spa
));
1133 * Internal implementation for spa_vdev_enter(). Used when a vdev
1134 * operation requires multiple syncs (i.e. removing a device) while
1135 * keeping the spa_namespace_lock held.
1138 spa_vdev_config_enter(spa_t
*spa
)
1140 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1142 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1144 return (spa_last_synced_txg(spa
) + 1);
1148 * Used in combination with spa_vdev_config_enter() to allow the syncing
1149 * of multiple transactions without releasing the spa_namespace_lock.
1152 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1154 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1156 int config_changed
= B_FALSE
;
1158 ASSERT(txg
> spa_last_synced_txg(spa
));
1160 spa
->spa_pending_vdev
= NULL
;
1163 * Reassess the DTLs.
1165 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1167 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1168 config_changed
= B_TRUE
;
1169 spa
->spa_config_generation
++;
1173 * Verify the metaslab classes.
1175 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1176 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1177 ASSERT(metaslab_class_validate(spa_special_class(spa
)) == 0);
1178 ASSERT(metaslab_class_validate(spa_dedup_class(spa
)) == 0);
1180 spa_config_exit(spa
, SCL_ALL
, spa
);
1183 * Panic the system if the specified tag requires it. This
1184 * is useful for ensuring that configurations are updated
1187 if (zio_injection_enabled
)
1188 zio_handle_panic_injection(spa
, tag
, 0);
1191 * Note: this txg_wait_synced() is important because it ensures
1192 * that there won't be more than one config change per txg.
1193 * This allows us to use the txg as the generation number.
1196 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1199 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1200 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1202 spa_config_exit(spa
, SCL_ALL
, spa
);
1206 * If the config changed, update the config cache.
1209 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1213 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1214 * locking of spa_vdev_enter(), we also want make sure the transactions have
1215 * synced to disk, and then update the global configuration cache with the new
1219 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1221 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1222 mutex_exit(&spa_namespace_lock
);
1223 mutex_exit(&spa
->spa_vdev_top_lock
);
1229 * Lock the given spa_t for the purpose of changing vdev state.
1232 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1234 int locks
= SCL_STATE_ALL
| oplocks
;
1237 * Root pools may need to read of the underlying devfs filesystem
1238 * when opening up a vdev. Unfortunately if we're holding the
1239 * SCL_ZIO lock it will result in a deadlock when we try to issue
1240 * the read from the root filesystem. Instead we "prefetch"
1241 * the associated vnodes that we need prior to opening the
1242 * underlying devices and cache them so that we can prevent
1243 * any I/O when we are doing the actual open.
1245 if (spa_is_root(spa
)) {
1246 int low
= locks
& ~(SCL_ZIO
- 1);
1247 int high
= locks
& ~low
;
1249 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1250 vdev_hold(spa
->spa_root_vdev
);
1251 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1253 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1255 spa
->spa_vdev_locks
= locks
;
1259 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1261 boolean_t config_changed
= B_FALSE
;
1264 if (vd
== NULL
|| vd
== spa
->spa_root_vdev
) {
1265 vdev_top
= spa
->spa_root_vdev
;
1267 vdev_top
= vd
->vdev_top
;
1270 if (vd
!= NULL
|| error
== 0)
1271 vdev_dtl_reassess(vdev_top
, 0, 0, B_FALSE
);
1274 if (vd
!= spa
->spa_root_vdev
)
1275 vdev_state_dirty(vdev_top
);
1277 config_changed
= B_TRUE
;
1278 spa
->spa_config_generation
++;
1281 if (spa_is_root(spa
))
1282 vdev_rele(spa
->spa_root_vdev
);
1284 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1285 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1288 * If anything changed, wait for it to sync. This ensures that,
1289 * from the system administrator's perspective, zpool(1M) commands
1290 * are synchronous. This is important for things like zpool offline:
1291 * when the command completes, you expect no further I/O from ZFS.
1294 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1297 * If the config changed, update the config cache.
1299 if (config_changed
) {
1300 mutex_enter(&spa_namespace_lock
);
1301 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1302 mutex_exit(&spa_namespace_lock
);
1309 * ==========================================================================
1310 * Miscellaneous functions
1311 * ==========================================================================
1315 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1317 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1318 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1320 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1321 * dirty the vdev config because lock SCL_CONFIG is not held.
1322 * Thankfully, in this case we don't need to dirty the config
1323 * because it will be written out anyway when we finish
1324 * creating the pool.
1326 if (tx
->tx_txg
!= TXG_INITIAL
)
1327 vdev_config_dirty(spa
->spa_root_vdev
);
1332 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1334 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1335 vdev_config_dirty(spa
->spa_root_vdev
);
1342 spa_rename(const char *name
, const char *newname
)
1348 * Lookup the spa_t and grab the config lock for writing. We need to
1349 * actually open the pool so that we can sync out the necessary labels.
1350 * It's OK to call spa_open() with the namespace lock held because we
1351 * allow recursive calls for other reasons.
1353 mutex_enter(&spa_namespace_lock
);
1354 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1355 mutex_exit(&spa_namespace_lock
);
1359 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1361 avl_remove(&spa_namespace_avl
, spa
);
1362 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1363 avl_add(&spa_namespace_avl
, spa
);
1366 * Sync all labels to disk with the new names by marking the root vdev
1367 * dirty and waiting for it to sync. It will pick up the new pool name
1370 vdev_config_dirty(spa
->spa_root_vdev
);
1372 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1374 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1377 * Sync the updated config cache.
1379 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1381 spa_close(spa
, FTAG
);
1383 mutex_exit(&spa_namespace_lock
);
1389 * Return the spa_t associated with given pool_guid, if it exists. If
1390 * device_guid is non-zero, determine whether the pool exists *and* contains
1391 * a device with the specified device_guid.
1394 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1397 avl_tree_t
*t
= &spa_namespace_avl
;
1399 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1401 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1402 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1404 if (spa
->spa_root_vdev
== NULL
)
1406 if (spa_guid(spa
) == pool_guid
) {
1407 if (device_guid
== 0)
1410 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1411 device_guid
) != NULL
)
1415 * Check any devices we may be in the process of adding.
1417 if (spa
->spa_pending_vdev
) {
1418 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1419 device_guid
) != NULL
)
1429 * Determine whether a pool with the given pool_guid exists.
1432 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1434 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1438 spa_strdup(const char *s
)
1444 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1452 spa_strfree(char *s
)
1454 kmem_free(s
, strlen(s
) + 1);
1458 spa_get_random(uint64_t range
)
1467 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1473 spa_generate_guid(spa_t
*spa
)
1475 uint64_t guid
= spa_get_random(-1ULL);
1478 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1479 guid
= spa_get_random(-1ULL);
1481 while (guid
== 0 || spa_guid_exists(guid
, 0))
1482 guid
= spa_get_random(-1ULL);
1489 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1492 char *checksum
= NULL
;
1493 char *compress
= NULL
;
1496 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1497 dmu_object_byteswap_t bswap
=
1498 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1499 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1500 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1501 "metadata" : "data",
1502 dmu_ot_byteswap
[bswap
].ob_name
);
1504 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1507 if (!BP_IS_EMBEDDED(bp
)) {
1509 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1511 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1514 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1519 spa_freeze(spa_t
*spa
)
1521 uint64_t freeze_txg
= 0;
1523 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1524 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1525 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1526 spa
->spa_freeze_txg
= freeze_txg
;
1528 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1529 if (freeze_txg
!= 0)
1530 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1534 zfs_panic_recover(const char *fmt
, ...)
1539 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1544 * This is a stripped-down version of strtoull, suitable only for converting
1545 * lowercase hexadecimal numbers that don't overflow.
1548 zfs_strtonum(const char *str
, char **nptr
)
1554 while ((c
= *str
) != '\0') {
1555 if (c
>= '0' && c
<= '9')
1557 else if (c
>= 'a' && c
<= 'f')
1558 digit
= 10 + c
- 'a';
1569 *nptr
= (char *)str
;
1575 spa_activate_allocation_classes(spa_t
*spa
, dmu_tx_t
*tx
)
1578 * We bump the feature refcount for each special vdev added to the pool
1580 ASSERT(spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
));
1581 spa_feature_incr(spa
, SPA_FEATURE_ALLOCATION_CLASSES
, tx
);
1585 * ==========================================================================
1586 * Accessor functions
1587 * ==========================================================================
1591 spa_shutting_down(spa_t
*spa
)
1593 return (spa
->spa_async_suspended
);
1597 spa_get_dsl(spa_t
*spa
)
1599 return (spa
->spa_dsl_pool
);
1603 spa_is_initializing(spa_t
*spa
)
1605 return (spa
->spa_is_initializing
);
1609 spa_indirect_vdevs_loaded(spa_t
*spa
)
1611 return (spa
->spa_indirect_vdevs_loaded
);
1615 spa_get_rootblkptr(spa_t
*spa
)
1617 return (&spa
->spa_ubsync
.ub_rootbp
);
1621 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1623 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1627 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1629 if (spa
->spa_root
== NULL
)
1632 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1636 spa_sync_pass(spa_t
*spa
)
1638 return (spa
->spa_sync_pass
);
1642 spa_name(spa_t
*spa
)
1644 return (spa
->spa_name
);
1648 spa_guid(spa_t
*spa
)
1650 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1654 * If we fail to parse the config during spa_load(), we can go through
1655 * the error path (which posts an ereport) and end up here with no root
1656 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1659 if (spa
->spa_root_vdev
== NULL
)
1660 return (spa
->spa_config_guid
);
1662 guid
= spa
->spa_last_synced_guid
!= 0 ?
1663 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1666 * Return the most recently synced out guid unless we're
1667 * in syncing context.
1669 if (dp
&& dsl_pool_sync_context(dp
))
1670 return (spa
->spa_root_vdev
->vdev_guid
);
1676 spa_load_guid(spa_t
*spa
)
1679 * This is a GUID that exists solely as a reference for the
1680 * purposes of the arc. It is generated at load time, and
1681 * is never written to persistent storage.
1683 return (spa
->spa_load_guid
);
1687 spa_last_synced_txg(spa_t
*spa
)
1689 return (spa
->spa_ubsync
.ub_txg
);
1693 spa_first_txg(spa_t
*spa
)
1695 return (spa
->spa_first_txg
);
1699 spa_syncing_txg(spa_t
*spa
)
1701 return (spa
->spa_syncing_txg
);
1705 * Return the last txg where data can be dirtied. The final txgs
1706 * will be used to just clear out any deferred frees that remain.
1709 spa_final_dirty_txg(spa_t
*spa
)
1711 return (spa
->spa_final_txg
- TXG_DEFER_SIZE
);
1715 spa_state(spa_t
*spa
)
1717 return (spa
->spa_state
);
1721 spa_load_state(spa_t
*spa
)
1723 return (spa
->spa_load_state
);
1727 spa_freeze_txg(spa_t
*spa
)
1729 return (spa
->spa_freeze_txg
);
1733 * Return the inflated asize for a logical write in bytes. This is used by the
1734 * DMU to calculate the space a logical write will require on disk.
1735 * If lsize is smaller than the largest physical block size allocatable on this
1736 * pool we use its value instead, since the write will end up using the whole
1740 spa_get_worst_case_asize(spa_t
*spa
, uint64_t lsize
)
1743 return (0); /* No inflation needed */
1744 return (MAX(lsize
, 1 << spa
->spa_max_ashift
) * spa_asize_inflation
);
1748 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1749 * or at least 128MB, unless that would cause it to be more than half the
1752 * See the comment above spa_slop_shift for details.
1755 spa_get_slop_space(spa_t
*spa
)
1757 uint64_t space
= spa_get_dspace(spa
);
1758 return (MAX(space
>> spa_slop_shift
, MIN(space
>> 1, spa_min_slop
)));
1762 spa_get_dspace(spa_t
*spa
)
1764 return (spa
->spa_dspace
);
1768 spa_get_checkpoint_space(spa_t
*spa
)
1770 return (spa
->spa_checkpoint_info
.sci_dspace
);
1774 spa_update_dspace(spa_t
*spa
)
1776 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1777 ddt_get_dedup_dspace(spa
);
1778 if (spa
->spa_vdev_removal
!= NULL
) {
1780 * We can't allocate from the removing device, so
1781 * subtract its size. This prevents the DMU/DSL from
1782 * filling up the (now smaller) pool while we are in the
1783 * middle of removing the device.
1785 * Note that the DMU/DSL doesn't actually know or care
1786 * how much space is allocated (it does its own tracking
1787 * of how much space has been logically used). So it
1788 * doesn't matter that the data we are moving may be
1789 * allocated twice (on the old device and the new
1792 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1794 vdev_lookup_top(spa
, spa
->spa_vdev_removal
->svr_vdev_id
);
1795 spa
->spa_dspace
-= spa_deflate(spa
) ?
1796 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1797 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1802 * Return the failure mode that has been set to this pool. The default
1803 * behavior will be to block all I/Os when a complete failure occurs.
1806 spa_get_failmode(spa_t
*spa
)
1808 return (spa
->spa_failmode
);
1812 spa_suspended(spa_t
*spa
)
1814 return (spa
->spa_suspended
!= ZIO_SUSPEND_NONE
);
1818 spa_version(spa_t
*spa
)
1820 return (spa
->spa_ubsync
.ub_version
);
1824 spa_deflate(spa_t
*spa
)
1826 return (spa
->spa_deflate
);
1830 spa_normal_class(spa_t
*spa
)
1832 return (spa
->spa_normal_class
);
1836 spa_log_class(spa_t
*spa
)
1838 return (spa
->spa_log_class
);
1842 spa_special_class(spa_t
*spa
)
1844 return (spa
->spa_special_class
);
1848 spa_dedup_class(spa_t
*spa
)
1850 return (spa
->spa_dedup_class
);
1854 * Locate an appropriate allocation class
1857 spa_preferred_class(spa_t
*spa
, uint64_t size
, dmu_object_type_t objtype
,
1858 uint_t level
, uint_t special_smallblk
)
1860 if (DMU_OT_IS_ZIL(objtype
)) {
1861 if (spa
->spa_log_class
->mc_groups
!= 0)
1862 return (spa_log_class(spa
));
1864 return (spa_normal_class(spa
));
1867 boolean_t has_special_class
= spa
->spa_special_class
->mc_groups
!= 0;
1869 if (DMU_OT_IS_DDT(objtype
)) {
1870 if (spa
->spa_dedup_class
->mc_groups
!= 0)
1871 return (spa_dedup_class(spa
));
1872 else if (has_special_class
&& zfs_ddt_data_is_special
)
1873 return (spa_special_class(spa
));
1875 return (spa_normal_class(spa
));
1878 /* Indirect blocks for user data can land in special if allowed */
1879 if (level
> 0 && (DMU_OT_IS_FILE(objtype
) || objtype
== DMU_OT_ZVOL
)) {
1880 if (has_special_class
&& zfs_user_indirect_is_special
)
1881 return (spa_special_class(spa
));
1883 return (spa_normal_class(spa
));
1886 if (DMU_OT_IS_METADATA(objtype
) || level
> 0) {
1887 if (has_special_class
)
1888 return (spa_special_class(spa
));
1890 return (spa_normal_class(spa
));
1894 * Allow small file blocks in special class in some cases (like
1895 * for the dRAID vdev feature). But always leave a reserve of
1896 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1898 if (DMU_OT_IS_FILE(objtype
) &&
1899 has_special_class
&& size
< special_smallblk
) {
1900 metaslab_class_t
*special
= spa_special_class(spa
);
1901 uint64_t alloc
= metaslab_class_get_alloc(special
);
1902 uint64_t space
= metaslab_class_get_space(special
);
1904 (space
* (100 - zfs_special_class_metadata_reserve_pct
))
1911 return (spa_normal_class(spa
));
1915 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1917 mutex_enter(&spa
->spa_evicting_os_lock
);
1918 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1919 mutex_exit(&spa
->spa_evicting_os_lock
);
1923 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1925 mutex_enter(&spa
->spa_evicting_os_lock
);
1926 list_remove(&spa
->spa_evicting_os_list
, os
);
1927 cv_broadcast(&spa
->spa_evicting_os_cv
);
1928 mutex_exit(&spa
->spa_evicting_os_lock
);
1932 spa_evicting_os_wait(spa_t
*spa
)
1934 mutex_enter(&spa
->spa_evicting_os_lock
);
1935 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1936 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1937 mutex_exit(&spa
->spa_evicting_os_lock
);
1939 dmu_buf_user_evict_wait();
1943 spa_max_replication(spa_t
*spa
)
1946 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1947 * handle BPs with more than one DVA allocated. Set our max
1948 * replication level accordingly.
1950 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1952 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1956 spa_prev_software_version(spa_t
*spa
)
1958 return (spa
->spa_prev_software_version
);
1962 spa_deadman_synctime(spa_t
*spa
)
1964 return (spa
->spa_deadman_synctime
);
1968 spa_deadman_ziotime(spa_t
*spa
)
1970 return (spa
->spa_deadman_ziotime
);
1974 spa_get_deadman_failmode(spa_t
*spa
)
1976 return (spa
->spa_deadman_failmode
);
1980 spa_set_deadman_failmode(spa_t
*spa
, const char *failmode
)
1982 if (strcmp(failmode
, "wait") == 0)
1983 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1984 else if (strcmp(failmode
, "continue") == 0)
1985 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
1986 else if (strcmp(failmode
, "panic") == 0)
1987 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
1989 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1993 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1995 uint64_t asize
= DVA_GET_ASIZE(dva
);
1996 uint64_t dsize
= asize
;
1998 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
2000 if (asize
!= 0 && spa
->spa_deflate
) {
2001 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
2003 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
2004 vd
->vdev_deflate_ratio
;
2011 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
2015 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2016 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2022 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
2026 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2028 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2029 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2031 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2037 spa_dirty_data(spa_t
*spa
)
2039 return (spa
->spa_dsl_pool
->dp_dirty_total
);
2043 * ==========================================================================
2044 * Initialization and Termination
2045 * ==========================================================================
2049 spa_name_compare(const void *a1
, const void *a2
)
2051 const spa_t
*s1
= a1
;
2052 const spa_t
*s2
= a2
;
2055 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
2057 return (AVL_ISIGN(s
));
2069 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2070 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2071 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2072 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
2074 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
2075 offsetof(spa_t
, spa_avl
));
2077 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
2078 offsetof(spa_aux_t
, aux_avl
));
2080 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
2081 offsetof(spa_aux_t
, aux_avl
));
2083 spa_mode_global
= mode
;
2086 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
2087 struct sigaction sa
;
2089 sa
.sa_flags
= SA_SIGINFO
;
2090 sigemptyset(&sa
.sa_mask
);
2091 sa
.sa_sigaction
= arc_buf_sigsegv
;
2093 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
2094 perror("could not enable watchpoints: "
2095 "sigaction(SIGSEGV, ...) = ");
2103 zfs_refcount_init();
2106 metaslab_alloc_trace_init();
2111 vdev_cache_stat_init();
2112 vdev_mirror_stat_init();
2113 vdev_raidz_math_init();
2117 zpool_feature_init();
2132 vdev_cache_stat_fini();
2133 vdev_mirror_stat_fini();
2134 vdev_raidz_math_fini();
2139 metaslab_alloc_trace_fini();
2142 zfs_refcount_fini();
2147 avl_destroy(&spa_namespace_avl
);
2148 avl_destroy(&spa_spare_avl
);
2149 avl_destroy(&spa_l2cache_avl
);
2151 cv_destroy(&spa_namespace_cv
);
2152 mutex_destroy(&spa_namespace_lock
);
2153 mutex_destroy(&spa_spare_lock
);
2154 mutex_destroy(&spa_l2cache_lock
);
2158 * Return whether this pool has slogs. No locking needed.
2159 * It's not a problem if the wrong answer is returned as it's only for
2160 * performance and not correctness
2163 spa_has_slogs(spa_t
*spa
)
2165 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
2169 spa_get_log_state(spa_t
*spa
)
2171 return (spa
->spa_log_state
);
2175 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
2177 spa
->spa_log_state
= state
;
2181 spa_is_root(spa_t
*spa
)
2183 return (spa
->spa_is_root
);
2187 spa_writeable(spa_t
*spa
)
2189 return (!!(spa
->spa_mode
& FWRITE
) && spa
->spa_trust_config
);
2193 * Returns true if there is a pending sync task in any of the current
2194 * syncing txg, the current quiescing txg, or the current open txg.
2197 spa_has_pending_synctask(spa_t
*spa
)
2199 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
) ||
2200 !txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_early_sync_tasks
));
2204 spa_mode(spa_t
*spa
)
2206 return (spa
->spa_mode
);
2210 spa_bootfs(spa_t
*spa
)
2212 return (spa
->spa_bootfs
);
2216 spa_delegation(spa_t
*spa
)
2218 return (spa
->spa_delegation
);
2222 spa_meta_objset(spa_t
*spa
)
2224 return (spa
->spa_meta_objset
);
2228 spa_dedup_checksum(spa_t
*spa
)
2230 return (spa
->spa_dedup_checksum
);
2234 * Reset pool scan stat per scan pass (or reboot).
2237 spa_scan_stat_init(spa_t
*spa
)
2239 /* data not stored on disk */
2240 spa
->spa_scan_pass_start
= gethrestime_sec();
2241 if (dsl_scan_is_paused_scrub(spa
->spa_dsl_pool
->dp_scan
))
2242 spa
->spa_scan_pass_scrub_pause
= spa
->spa_scan_pass_start
;
2244 spa
->spa_scan_pass_scrub_pause
= 0;
2245 spa
->spa_scan_pass_scrub_spent_paused
= 0;
2246 spa
->spa_scan_pass_exam
= 0;
2247 spa
->spa_scan_pass_issued
= 0;
2248 vdev_scan_stat_init(spa
->spa_root_vdev
);
2252 * Get scan stats for zpool status reports
2255 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
2257 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
2259 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
2260 return (SET_ERROR(ENOENT
));
2261 bzero(ps
, sizeof (pool_scan_stat_t
));
2263 /* data stored on disk */
2264 ps
->pss_func
= scn
->scn_phys
.scn_func
;
2265 ps
->pss_state
= scn
->scn_phys
.scn_state
;
2266 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
2267 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
2268 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
2269 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
2270 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
2271 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
2272 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
2274 /* data not stored on disk */
2275 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
2276 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
2277 ps
->pss_pass_scrub_pause
= spa
->spa_scan_pass_scrub_pause
;
2278 ps
->pss_pass_scrub_spent_paused
= spa
->spa_scan_pass_scrub_spent_paused
;
2279 ps
->pss_pass_issued
= spa
->spa_scan_pass_issued
;
2281 scn
->scn_issued_before_pass
+ spa
->spa_scan_pass_issued
;
2287 spa_maxblocksize(spa_t
*spa
)
2289 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
2290 return (SPA_MAXBLOCKSIZE
);
2292 return (SPA_OLD_MAXBLOCKSIZE
);
2297 * Returns the txg that the last device removal completed. No indirect mappings
2298 * have been added since this txg.
2301 spa_get_last_removal_txg(spa_t
*spa
)
2304 uint64_t ret
= -1ULL;
2306 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2308 * sr_prev_indirect_vdev is only modified while holding all the
2309 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2312 vdevid
= spa
->spa_removing_phys
.sr_prev_indirect_vdev
;
2314 while (vdevid
!= -1ULL) {
2315 vdev_t
*vd
= vdev_lookup_top(spa
, vdevid
);
2316 vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
;
2318 ASSERT3P(vd
->vdev_ops
, ==, &vdev_indirect_ops
);
2321 * If the removal did not remap any data, we don't care.
2323 if (vdev_indirect_births_count(vib
) != 0) {
2324 ret
= vdev_indirect_births_last_entry_txg(vib
);
2328 vdevid
= vd
->vdev_indirect_config
.vic_prev_indirect_vdev
;
2330 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2333 spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
2339 spa_maxdnodesize(spa_t
*spa
)
2341 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2342 return (DNODE_MAX_SIZE
);
2344 return (DNODE_MIN_SIZE
);
2348 spa_multihost(spa_t
*spa
)
2350 return (spa
->spa_multihost
? B_TRUE
: B_FALSE
);
2354 spa_get_hostid(void)
2356 unsigned long myhostid
;
2359 myhostid
= zone_get_hostid(NULL
);
2362 * We're emulating the system's hostid in userland, so
2363 * we can't use zone_get_hostid().
2365 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2366 #endif /* _KERNEL */
2372 spa_trust_config(spa_t
*spa
)
2374 return (spa
->spa_trust_config
);
2378 spa_missing_tvds_allowed(spa_t
*spa
)
2380 return (spa
->spa_missing_tvds_allowed
);
2384 spa_set_missing_tvds(spa_t
*spa
, uint64_t missing
)
2386 spa
->spa_missing_tvds
= missing
;
2390 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2393 spa_state_to_name(spa_t
*spa
)
2395 vdev_state_t state
= spa
->spa_root_vdev
->vdev_state
;
2396 vdev_aux_t aux
= spa
->spa_root_vdev
->vdev_stat
.vs_aux
;
2398 if (spa_suspended(spa
) &&
2399 (spa_get_failmode(spa
) != ZIO_FAILURE_MODE_CONTINUE
))
2400 return ("SUSPENDED");
2403 case VDEV_STATE_CLOSED
:
2404 case VDEV_STATE_OFFLINE
:
2406 case VDEV_STATE_REMOVED
:
2408 case VDEV_STATE_CANT_OPEN
:
2409 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
2411 else if (aux
== VDEV_AUX_SPLIT_POOL
)
2415 case VDEV_STATE_FAULTED
:
2417 case VDEV_STATE_DEGRADED
:
2418 return ("DEGRADED");
2419 case VDEV_STATE_HEALTHY
:
2429 spa_top_vdevs_spacemap_addressable(spa_t
*spa
)
2431 vdev_t
*rvd
= spa
->spa_root_vdev
;
2432 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2433 if (!vdev_is_spacemap_addressable(rvd
->vdev_child
[c
]))
2440 spa_has_checkpoint(spa_t
*spa
)
2442 return (spa
->spa_checkpoint_txg
!= 0);
2446 spa_importing_readonly_checkpoint(spa_t
*spa
)
2448 return ((spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
) &&
2449 spa
->spa_mode
== FREAD
);
2453 spa_min_claim_txg(spa_t
*spa
)
2455 uint64_t checkpoint_txg
= spa
->spa_uberblock
.ub_checkpoint_txg
;
2457 if (checkpoint_txg
!= 0)
2458 return (checkpoint_txg
+ 1);
2460 return (spa
->spa_first_txg
);
2464 * If there is a checkpoint, async destroys may consume more space from
2465 * the pool instead of freeing it. In an attempt to save the pool from
2466 * getting suspended when it is about to run out of space, we stop
2467 * processing async destroys.
2470 spa_suspend_async_destroy(spa_t
*spa
)
2472 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2474 uint64_t unreserved
= dsl_pool_unreserved_space(dp
,
2475 ZFS_SPACE_CHECK_EXTRA_RESERVED
);
2476 uint64_t used
= dsl_dir_phys(dp
->dp_root_dir
)->dd_used_bytes
;
2477 uint64_t avail
= (unreserved
> used
) ? (unreserved
- used
) : 0;
2479 if (spa_has_checkpoint(spa
) && avail
== 0)
2485 #if defined(_KERNEL)
2487 #include <linux/mod_compat.h>
2490 param_set_deadman_failmode(const char *val
, zfs_kernel_param_t
*kp
)
2496 return (SET_ERROR(-EINVAL
));
2498 if ((p
= strchr(val
, '\n')) != NULL
)
2501 if (strcmp(val
, "wait") != 0 && strcmp(val
, "continue") != 0 &&
2502 strcmp(val
, "panic"))
2503 return (SET_ERROR(-EINVAL
));
2505 if (spa_mode_global
!= 0) {
2506 mutex_enter(&spa_namespace_lock
);
2507 while ((spa
= spa_next(spa
)) != NULL
)
2508 spa_set_deadman_failmode(spa
, val
);
2509 mutex_exit(&spa_namespace_lock
);
2512 return (param_set_charp(val
, kp
));
2516 param_set_deadman_ziotime(const char *val
, zfs_kernel_param_t
*kp
)
2521 error
= param_set_ulong(val
, kp
);
2523 return (SET_ERROR(error
));
2525 if (spa_mode_global
!= 0) {
2526 mutex_enter(&spa_namespace_lock
);
2527 while ((spa
= spa_next(spa
)) != NULL
)
2528 spa
->spa_deadman_ziotime
=
2529 MSEC2NSEC(zfs_deadman_ziotime_ms
);
2530 mutex_exit(&spa_namespace_lock
);
2537 param_set_deadman_synctime(const char *val
, zfs_kernel_param_t
*kp
)
2542 error
= param_set_ulong(val
, kp
);
2544 return (SET_ERROR(error
));
2546 if (spa_mode_global
!= 0) {
2547 mutex_enter(&spa_namespace_lock
);
2548 while ((spa
= spa_next(spa
)) != NULL
)
2549 spa
->spa_deadman_synctime
=
2550 MSEC2NSEC(zfs_deadman_synctime_ms
);
2551 mutex_exit(&spa_namespace_lock
);
2558 param_set_slop_shift(const char *buf
, zfs_kernel_param_t
*kp
)
2563 error
= kstrtoul(buf
, 0, &val
);
2565 return (SET_ERROR(error
));
2567 if (val
< 1 || val
> 31)
2568 return (SET_ERROR(-EINVAL
));
2570 error
= param_set_int(buf
, kp
);
2572 return (SET_ERROR(error
));
2577 /* Namespace manipulation */
2578 EXPORT_SYMBOL(spa_lookup
);
2579 EXPORT_SYMBOL(spa_add
);
2580 EXPORT_SYMBOL(spa_remove
);
2581 EXPORT_SYMBOL(spa_next
);
2583 /* Refcount functions */
2584 EXPORT_SYMBOL(spa_open_ref
);
2585 EXPORT_SYMBOL(spa_close
);
2586 EXPORT_SYMBOL(spa_refcount_zero
);
2588 /* Pool configuration lock */
2589 EXPORT_SYMBOL(spa_config_tryenter
);
2590 EXPORT_SYMBOL(spa_config_enter
);
2591 EXPORT_SYMBOL(spa_config_exit
);
2592 EXPORT_SYMBOL(spa_config_held
);
2594 /* Pool vdev add/remove lock */
2595 EXPORT_SYMBOL(spa_vdev_enter
);
2596 EXPORT_SYMBOL(spa_vdev_exit
);
2598 /* Pool vdev state change lock */
2599 EXPORT_SYMBOL(spa_vdev_state_enter
);
2600 EXPORT_SYMBOL(spa_vdev_state_exit
);
2602 /* Accessor functions */
2603 EXPORT_SYMBOL(spa_shutting_down
);
2604 EXPORT_SYMBOL(spa_get_dsl
);
2605 EXPORT_SYMBOL(spa_get_rootblkptr
);
2606 EXPORT_SYMBOL(spa_set_rootblkptr
);
2607 EXPORT_SYMBOL(spa_altroot
);
2608 EXPORT_SYMBOL(spa_sync_pass
);
2609 EXPORT_SYMBOL(spa_name
);
2610 EXPORT_SYMBOL(spa_guid
);
2611 EXPORT_SYMBOL(spa_last_synced_txg
);
2612 EXPORT_SYMBOL(spa_first_txg
);
2613 EXPORT_SYMBOL(spa_syncing_txg
);
2614 EXPORT_SYMBOL(spa_version
);
2615 EXPORT_SYMBOL(spa_state
);
2616 EXPORT_SYMBOL(spa_load_state
);
2617 EXPORT_SYMBOL(spa_freeze_txg
);
2618 EXPORT_SYMBOL(spa_get_dspace
);
2619 EXPORT_SYMBOL(spa_update_dspace
);
2620 EXPORT_SYMBOL(spa_deflate
);
2621 EXPORT_SYMBOL(spa_normal_class
);
2622 EXPORT_SYMBOL(spa_log_class
);
2623 EXPORT_SYMBOL(spa_special_class
);
2624 EXPORT_SYMBOL(spa_preferred_class
);
2625 EXPORT_SYMBOL(spa_max_replication
);
2626 EXPORT_SYMBOL(spa_prev_software_version
);
2627 EXPORT_SYMBOL(spa_get_failmode
);
2628 EXPORT_SYMBOL(spa_suspended
);
2629 EXPORT_SYMBOL(spa_bootfs
);
2630 EXPORT_SYMBOL(spa_delegation
);
2631 EXPORT_SYMBOL(spa_meta_objset
);
2632 EXPORT_SYMBOL(spa_maxblocksize
);
2633 EXPORT_SYMBOL(spa_maxdnodesize
);
2635 /* Miscellaneous support routines */
2636 EXPORT_SYMBOL(spa_rename
);
2637 EXPORT_SYMBOL(spa_guid_exists
);
2638 EXPORT_SYMBOL(spa_strdup
);
2639 EXPORT_SYMBOL(spa_strfree
);
2640 EXPORT_SYMBOL(spa_get_random
);
2641 EXPORT_SYMBOL(spa_generate_guid
);
2642 EXPORT_SYMBOL(snprintf_blkptr
);
2643 EXPORT_SYMBOL(spa_freeze
);
2644 EXPORT_SYMBOL(spa_upgrade
);
2645 EXPORT_SYMBOL(spa_evict_all
);
2646 EXPORT_SYMBOL(spa_lookup_by_guid
);
2647 EXPORT_SYMBOL(spa_has_spare
);
2648 EXPORT_SYMBOL(dva_get_dsize_sync
);
2649 EXPORT_SYMBOL(bp_get_dsize_sync
);
2650 EXPORT_SYMBOL(bp_get_dsize
);
2651 EXPORT_SYMBOL(spa_has_slogs
);
2652 EXPORT_SYMBOL(spa_is_root
);
2653 EXPORT_SYMBOL(spa_writeable
);
2654 EXPORT_SYMBOL(spa_mode
);
2655 EXPORT_SYMBOL(spa_namespace_lock
);
2656 EXPORT_SYMBOL(spa_trust_config
);
2657 EXPORT_SYMBOL(spa_missing_tvds_allowed
);
2658 EXPORT_SYMBOL(spa_set_missing_tvds
);
2659 EXPORT_SYMBOL(spa_state_to_name
);
2660 EXPORT_SYMBOL(spa_importing_readonly_checkpoint
);
2661 EXPORT_SYMBOL(spa_min_claim_txg
);
2662 EXPORT_SYMBOL(spa_suspend_async_destroy
);
2663 EXPORT_SYMBOL(spa_has_checkpoint
);
2664 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable
);
2667 module_param(zfs_flags
, uint
, 0644);
2668 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
2670 module_param(zfs_recover
, int, 0644);
2671 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
2673 module_param(zfs_free_leak_on_eio
, int, 0644);
2674 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
2675 "Set to ignore IO errors during free and permanently leak the space");
2677 module_param_call(zfs_deadman_synctime_ms
, param_set_deadman_synctime
,
2678 param_get_ulong
, &zfs_deadman_synctime_ms
, 0644);
2679 MODULE_PARM_DESC(zfs_deadman_synctime_ms
,
2680 "Pool sync expiration time in milliseconds");
2682 module_param_call(zfs_deadman_ziotime_ms
, param_set_deadman_ziotime
,
2683 param_get_ulong
, &zfs_deadman_ziotime_ms
, 0644);
2684 MODULE_PARM_DESC(zfs_deadman_ziotime_ms
,
2685 "IO expiration time in milliseconds");
2687 module_param(zfs_deadman_checktime_ms
, ulong
, 0644);
2688 MODULE_PARM_DESC(zfs_deadman_checktime_ms
,
2689 "Dead I/O check interval in milliseconds");
2691 module_param(zfs_deadman_enabled
, int, 0644);
2692 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
2694 module_param_call(zfs_deadman_failmode
, param_set_deadman_failmode
,
2695 param_get_charp
, &zfs_deadman_failmode
, 0644);
2696 MODULE_PARM_DESC(zfs_deadman_failmode
, "Failmode for deadman timer");
2698 module_param(spa_asize_inflation
, int, 0644);
2699 MODULE_PARM_DESC(spa_asize_inflation
,
2700 "SPA size estimate multiplication factor");
2702 module_param_call(spa_slop_shift
, param_set_slop_shift
, param_get_int
,
2703 &spa_slop_shift
, 0644);
2704 MODULE_PARM_DESC(spa_slop_shift
, "Reserved free space in pool");
2706 module_param(zfs_ddt_data_is_special
, int, 0644);
2707 MODULE_PARM_DESC(zfs_ddt_data_is_special
,
2708 "Place DDT data into the special class");
2710 module_param(zfs_user_indirect_is_special
, int, 0644);
2711 MODULE_PARM_DESC(zfs_user_indirect_is_special
,
2712 "Place user data indirect blocks into the special class");