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 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 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 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 (!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) 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 (!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) 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(!refcount_is_zero(&scl
->scl_count
));
529 if (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
&& !refcount_is_zero(&scl
->scl_count
)) ||
549 (rw
== RW_WRITER
&& scl
->scl_writer
== curthread
))
550 locks_held
|= 1 << i
;
557 * ==========================================================================
558 * SPA namespace functions
559 * ==========================================================================
563 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
564 * Returns NULL if no matching spa_t is found.
567 spa_lookup(const char *name
)
569 static spa_t search
; /* spa_t is large; don't allocate on stack */
574 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
576 (void) strlcpy(search
.spa_name
, name
, sizeof (search
.spa_name
));
579 * If it's a full dataset name, figure out the pool name and
582 cp
= strpbrk(search
.spa_name
, "/@#");
586 spa
= avl_find(&spa_namespace_avl
, &search
, &where
);
592 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
593 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
594 * looking for potentially hung I/Os.
597 spa_deadman(void *arg
)
601 /* Disable the deadman if the pool is suspended. */
602 if (spa_suspended(spa
))
605 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
606 (gethrtime() - spa
->spa_sync_starttime
) / NANOSEC
,
607 ++spa
->spa_deadman_calls
);
608 if (zfs_deadman_enabled
)
609 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
611 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
612 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
613 MSEC_TO_TICK(zfs_deadman_checktime_ms
));
617 * Create an uninitialized spa_t with the given name. Requires
618 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
619 * exist by calling spa_lookup() first.
622 spa_add(const char *name
, nvlist_t
*config
, const char *altroot
)
625 spa_config_dirent_t
*dp
;
627 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
629 spa
= kmem_zalloc(sizeof (spa_t
), KM_SLEEP
);
631 mutex_init(&spa
->spa_async_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
632 mutex_init(&spa
->spa_errlist_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
633 mutex_init(&spa
->spa_errlog_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
634 mutex_init(&spa
->spa_evicting_os_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
635 mutex_init(&spa
->spa_history_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
636 mutex_init(&spa
->spa_proc_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
637 mutex_init(&spa
->spa_props_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
638 mutex_init(&spa
->spa_cksum_tmpls_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
639 mutex_init(&spa
->spa_scrub_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
640 mutex_init(&spa
->spa_suspend_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
641 mutex_init(&spa
->spa_vdev_top_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
642 mutex_init(&spa
->spa_feat_stats_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
644 cv_init(&spa
->spa_async_cv
, NULL
, CV_DEFAULT
, NULL
);
645 cv_init(&spa
->spa_evicting_os_cv
, NULL
, CV_DEFAULT
, NULL
);
646 cv_init(&spa
->spa_proc_cv
, NULL
, CV_DEFAULT
, NULL
);
647 cv_init(&spa
->spa_scrub_io_cv
, NULL
, CV_DEFAULT
, NULL
);
648 cv_init(&spa
->spa_suspend_cv
, NULL
, CV_DEFAULT
, NULL
);
650 for (int t
= 0; t
< TXG_SIZE
; t
++)
651 bplist_create(&spa
->spa_free_bplist
[t
]);
653 (void) strlcpy(spa
->spa_name
, name
, sizeof (spa
->spa_name
));
654 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
655 spa
->spa_freeze_txg
= UINT64_MAX
;
656 spa
->spa_final_txg
= UINT64_MAX
;
657 spa
->spa_load_max_txg
= UINT64_MAX
;
659 spa
->spa_proc_state
= SPA_PROC_NONE
;
660 spa
->spa_trust_config
= B_TRUE
;
662 spa
->spa_deadman_synctime
= MSEC2NSEC(zfs_deadman_synctime_ms
);
663 spa
->spa_deadman_ziotime
= MSEC2NSEC(zfs_deadman_ziotime_ms
);
664 spa_set_deadman_failmode(spa
, zfs_deadman_failmode
);
666 refcount_create(&spa
->spa_refcount
);
667 spa_config_lock_init(spa
);
670 avl_add(&spa_namespace_avl
, spa
);
673 * Set the alternate root, if there is one.
676 spa
->spa_root
= spa_strdup(altroot
);
678 spa
->spa_alloc_count
= spa_allocators
;
679 spa
->spa_alloc_locks
= kmem_zalloc(spa
->spa_alloc_count
*
680 sizeof (kmutex_t
), KM_SLEEP
);
681 spa
->spa_alloc_trees
= kmem_zalloc(spa
->spa_alloc_count
*
682 sizeof (avl_tree_t
), KM_SLEEP
);
683 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
684 mutex_init(&spa
->spa_alloc_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
685 avl_create(&spa
->spa_alloc_trees
[i
], zio_bookmark_compare
,
686 sizeof (zio_t
), offsetof(zio_t
, io_alloc_node
));
690 * Every pool starts with the default cachefile
692 list_create(&spa
->spa_config_list
, sizeof (spa_config_dirent_t
),
693 offsetof(spa_config_dirent_t
, scd_link
));
695 dp
= kmem_zalloc(sizeof (spa_config_dirent_t
), KM_SLEEP
);
696 dp
->scd_path
= altroot
? NULL
: spa_strdup(spa_config_path
);
697 list_insert_head(&spa
->spa_config_list
, dp
);
699 VERIFY(nvlist_alloc(&spa
->spa_load_info
, NV_UNIQUE_NAME
,
702 if (config
!= NULL
) {
705 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
707 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
,
711 VERIFY(nvlist_dup(config
, &spa
->spa_config
, 0) == 0);
714 if (spa
->spa_label_features
== NULL
) {
715 VERIFY(nvlist_alloc(&spa
->spa_label_features
, NV_UNIQUE_NAME
,
719 spa
->spa_min_ashift
= INT_MAX
;
720 spa
->spa_max_ashift
= 0;
722 /* Reset cached value */
723 spa
->spa_dedup_dspace
= ~0ULL;
726 * As a pool is being created, treat all features as disabled by
727 * setting SPA_FEATURE_DISABLED for all entries in the feature
730 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
731 spa
->spa_feat_refcount_cache
[i
] = SPA_FEATURE_DISABLED
;
738 * Removes a spa_t from the namespace, freeing up any memory used. Requires
739 * spa_namespace_lock. This is called only after the spa_t has been closed and
743 spa_remove(spa_t
*spa
)
745 spa_config_dirent_t
*dp
;
747 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
748 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
749 ASSERT3U(refcount_count(&spa
->spa_refcount
), ==, 0);
751 nvlist_free(spa
->spa_config_splitting
);
753 avl_remove(&spa_namespace_avl
, spa
);
754 cv_broadcast(&spa_namespace_cv
);
757 spa_strfree(spa
->spa_root
);
759 while ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
760 list_remove(&spa
->spa_config_list
, dp
);
761 if (dp
->scd_path
!= NULL
)
762 spa_strfree(dp
->scd_path
);
763 kmem_free(dp
, sizeof (spa_config_dirent_t
));
766 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
767 avl_destroy(&spa
->spa_alloc_trees
[i
]);
768 mutex_destroy(&spa
->spa_alloc_locks
[i
]);
770 kmem_free(spa
->spa_alloc_locks
, spa
->spa_alloc_count
*
772 kmem_free(spa
->spa_alloc_trees
, spa
->spa_alloc_count
*
773 sizeof (avl_tree_t
));
775 list_destroy(&spa
->spa_config_list
);
777 nvlist_free(spa
->spa_label_features
);
778 nvlist_free(spa
->spa_load_info
);
779 nvlist_free(spa
->spa_feat_stats
);
780 spa_config_set(spa
, NULL
);
782 refcount_destroy(&spa
->spa_refcount
);
784 spa_stats_destroy(spa
);
785 spa_config_lock_destroy(spa
);
787 for (int t
= 0; t
< TXG_SIZE
; t
++)
788 bplist_destroy(&spa
->spa_free_bplist
[t
]);
790 zio_checksum_templates_free(spa
);
792 cv_destroy(&spa
->spa_async_cv
);
793 cv_destroy(&spa
->spa_evicting_os_cv
);
794 cv_destroy(&spa
->spa_proc_cv
);
795 cv_destroy(&spa
->spa_scrub_io_cv
);
796 cv_destroy(&spa
->spa_suspend_cv
);
798 mutex_destroy(&spa
->spa_async_lock
);
799 mutex_destroy(&spa
->spa_errlist_lock
);
800 mutex_destroy(&spa
->spa_errlog_lock
);
801 mutex_destroy(&spa
->spa_evicting_os_lock
);
802 mutex_destroy(&spa
->spa_history_lock
);
803 mutex_destroy(&spa
->spa_proc_lock
);
804 mutex_destroy(&spa
->spa_props_lock
);
805 mutex_destroy(&spa
->spa_cksum_tmpls_lock
);
806 mutex_destroy(&spa
->spa_scrub_lock
);
807 mutex_destroy(&spa
->spa_suspend_lock
);
808 mutex_destroy(&spa
->spa_vdev_top_lock
);
809 mutex_destroy(&spa
->spa_feat_stats_lock
);
811 kmem_free(spa
, sizeof (spa_t
));
815 * Given a pool, return the next pool in the namespace, or NULL if there is
816 * none. If 'prev' is NULL, return the first pool.
819 spa_next(spa_t
*prev
)
821 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
824 return (AVL_NEXT(&spa_namespace_avl
, prev
));
826 return (avl_first(&spa_namespace_avl
));
830 * ==========================================================================
831 * SPA refcount functions
832 * ==========================================================================
836 * Add a reference to the given spa_t. Must have at least one reference, or
837 * have the namespace lock held.
840 spa_open_ref(spa_t
*spa
, void *tag
)
842 ASSERT(refcount_count(&spa
->spa_refcount
) >= spa
->spa_minref
||
843 MUTEX_HELD(&spa_namespace_lock
));
844 (void) refcount_add(&spa
->spa_refcount
, tag
);
848 * Remove a reference to the given spa_t. Must have at least one reference, or
849 * have the namespace lock held.
852 spa_close(spa_t
*spa
, void *tag
)
854 ASSERT(refcount_count(&spa
->spa_refcount
) > spa
->spa_minref
||
855 MUTEX_HELD(&spa_namespace_lock
));
856 (void) refcount_remove(&spa
->spa_refcount
, tag
);
860 * Remove a reference to the given spa_t held by a dsl dir that is
861 * being asynchronously released. Async releases occur from a taskq
862 * performing eviction of dsl datasets and dirs. The namespace lock
863 * isn't held and the hold by the object being evicted may contribute to
864 * spa_minref (e.g. dataset or directory released during pool export),
865 * so the asserts in spa_close() do not apply.
868 spa_async_close(spa_t
*spa
, void *tag
)
870 (void) refcount_remove(&spa
->spa_refcount
, tag
);
874 * Check to see if the spa refcount is zero. Must be called with
875 * spa_namespace_lock held. We really compare against spa_minref, which is the
876 * number of references acquired when opening a pool
879 spa_refcount_zero(spa_t
*spa
)
881 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
883 return (refcount_count(&spa
->spa_refcount
) == spa
->spa_minref
);
887 * ==========================================================================
888 * SPA spare and l2cache tracking
889 * ==========================================================================
893 * Hot spares and cache devices are tracked using the same code below,
894 * for 'auxiliary' devices.
897 typedef struct spa_aux
{
905 spa_aux_compare(const void *a
, const void *b
)
907 const spa_aux_t
*sa
= (const spa_aux_t
*)a
;
908 const spa_aux_t
*sb
= (const spa_aux_t
*)b
;
910 return (AVL_CMP(sa
->aux_guid
, sb
->aux_guid
));
914 spa_aux_add(vdev_t
*vd
, avl_tree_t
*avl
)
920 search
.aux_guid
= vd
->vdev_guid
;
921 if ((aux
= avl_find(avl
, &search
, &where
)) != NULL
) {
924 aux
= kmem_zalloc(sizeof (spa_aux_t
), KM_SLEEP
);
925 aux
->aux_guid
= vd
->vdev_guid
;
927 avl_insert(avl
, aux
, where
);
932 spa_aux_remove(vdev_t
*vd
, avl_tree_t
*avl
)
938 search
.aux_guid
= vd
->vdev_guid
;
939 aux
= avl_find(avl
, &search
, &where
);
943 if (--aux
->aux_count
== 0) {
944 avl_remove(avl
, aux
);
945 kmem_free(aux
, sizeof (spa_aux_t
));
946 } else if (aux
->aux_pool
== spa_guid(vd
->vdev_spa
)) {
947 aux
->aux_pool
= 0ULL;
952 spa_aux_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
, avl_tree_t
*avl
)
954 spa_aux_t search
, *found
;
956 search
.aux_guid
= guid
;
957 found
= avl_find(avl
, &search
, NULL
);
961 *pool
= found
->aux_pool
;
968 *refcnt
= found
->aux_count
;
973 return (found
!= NULL
);
977 spa_aux_activate(vdev_t
*vd
, avl_tree_t
*avl
)
979 spa_aux_t search
, *found
;
982 search
.aux_guid
= vd
->vdev_guid
;
983 found
= avl_find(avl
, &search
, &where
);
984 ASSERT(found
!= NULL
);
985 ASSERT(found
->aux_pool
== 0ULL);
987 found
->aux_pool
= spa_guid(vd
->vdev_spa
);
991 * Spares are tracked globally due to the following constraints:
993 * - A spare may be part of multiple pools.
994 * - A spare may be added to a pool even if it's actively in use within
996 * - A spare in use in any pool can only be the source of a replacement if
997 * the target is a spare in the same pool.
999 * We keep track of all spares on the system through the use of a reference
1000 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1001 * spare, then we bump the reference count in the AVL tree. In addition, we set
1002 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1003 * inactive). When a spare is made active (used to replace a device in the
1004 * pool), we also keep track of which pool its been made a part of.
1006 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1007 * called under the spa_namespace lock as part of vdev reconfiguration. The
1008 * separate spare lock exists for the status query path, which does not need to
1009 * be completely consistent with respect to other vdev configuration changes.
1013 spa_spare_compare(const void *a
, const void *b
)
1015 return (spa_aux_compare(a
, b
));
1019 spa_spare_add(vdev_t
*vd
)
1021 mutex_enter(&spa_spare_lock
);
1022 ASSERT(!vd
->vdev_isspare
);
1023 spa_aux_add(vd
, &spa_spare_avl
);
1024 vd
->vdev_isspare
= B_TRUE
;
1025 mutex_exit(&spa_spare_lock
);
1029 spa_spare_remove(vdev_t
*vd
)
1031 mutex_enter(&spa_spare_lock
);
1032 ASSERT(vd
->vdev_isspare
);
1033 spa_aux_remove(vd
, &spa_spare_avl
);
1034 vd
->vdev_isspare
= B_FALSE
;
1035 mutex_exit(&spa_spare_lock
);
1039 spa_spare_exists(uint64_t guid
, uint64_t *pool
, int *refcnt
)
1043 mutex_enter(&spa_spare_lock
);
1044 found
= spa_aux_exists(guid
, pool
, refcnt
, &spa_spare_avl
);
1045 mutex_exit(&spa_spare_lock
);
1051 spa_spare_activate(vdev_t
*vd
)
1053 mutex_enter(&spa_spare_lock
);
1054 ASSERT(vd
->vdev_isspare
);
1055 spa_aux_activate(vd
, &spa_spare_avl
);
1056 mutex_exit(&spa_spare_lock
);
1060 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1061 * Cache devices currently only support one pool per cache device, and so
1062 * for these devices the aux reference count is currently unused beyond 1.
1066 spa_l2cache_compare(const void *a
, const void *b
)
1068 return (spa_aux_compare(a
, b
));
1072 spa_l2cache_add(vdev_t
*vd
)
1074 mutex_enter(&spa_l2cache_lock
);
1075 ASSERT(!vd
->vdev_isl2cache
);
1076 spa_aux_add(vd
, &spa_l2cache_avl
);
1077 vd
->vdev_isl2cache
= B_TRUE
;
1078 mutex_exit(&spa_l2cache_lock
);
1082 spa_l2cache_remove(vdev_t
*vd
)
1084 mutex_enter(&spa_l2cache_lock
);
1085 ASSERT(vd
->vdev_isl2cache
);
1086 spa_aux_remove(vd
, &spa_l2cache_avl
);
1087 vd
->vdev_isl2cache
= B_FALSE
;
1088 mutex_exit(&spa_l2cache_lock
);
1092 spa_l2cache_exists(uint64_t guid
, uint64_t *pool
)
1096 mutex_enter(&spa_l2cache_lock
);
1097 found
= spa_aux_exists(guid
, pool
, NULL
, &spa_l2cache_avl
);
1098 mutex_exit(&spa_l2cache_lock
);
1104 spa_l2cache_activate(vdev_t
*vd
)
1106 mutex_enter(&spa_l2cache_lock
);
1107 ASSERT(vd
->vdev_isl2cache
);
1108 spa_aux_activate(vd
, &spa_l2cache_avl
);
1109 mutex_exit(&spa_l2cache_lock
);
1113 * ==========================================================================
1115 * ==========================================================================
1119 * Lock the given spa_t for the purpose of adding or removing a vdev.
1120 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1121 * It returns the next transaction group for the spa_t.
1124 spa_vdev_enter(spa_t
*spa
)
1126 mutex_enter(&spa
->spa_vdev_top_lock
);
1127 mutex_enter(&spa_namespace_lock
);
1128 return (spa_vdev_config_enter(spa
));
1132 * Internal implementation for spa_vdev_enter(). Used when a vdev
1133 * operation requires multiple syncs (i.e. removing a device) while
1134 * keeping the spa_namespace_lock held.
1137 spa_vdev_config_enter(spa_t
*spa
)
1139 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1141 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1143 return (spa_last_synced_txg(spa
) + 1);
1147 * Used in combination with spa_vdev_config_enter() to allow the syncing
1148 * of multiple transactions without releasing the spa_namespace_lock.
1151 spa_vdev_config_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
, char *tag
)
1153 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1155 int config_changed
= B_FALSE
;
1157 ASSERT(txg
> spa_last_synced_txg(spa
));
1159 spa
->spa_pending_vdev
= NULL
;
1162 * Reassess the DTLs.
1164 vdev_dtl_reassess(spa
->spa_root_vdev
, 0, 0, B_FALSE
);
1166 if (error
== 0 && !list_is_empty(&spa
->spa_config_dirty_list
)) {
1167 config_changed
= B_TRUE
;
1168 spa
->spa_config_generation
++;
1172 * Verify the metaslab classes.
1174 ASSERT(metaslab_class_validate(spa_normal_class(spa
)) == 0);
1175 ASSERT(metaslab_class_validate(spa_log_class(spa
)) == 0);
1176 ASSERT(metaslab_class_validate(spa_special_class(spa
)) == 0);
1177 ASSERT(metaslab_class_validate(spa_dedup_class(spa
)) == 0);
1179 spa_config_exit(spa
, SCL_ALL
, spa
);
1182 * Panic the system if the specified tag requires it. This
1183 * is useful for ensuring that configurations are updated
1186 if (zio_injection_enabled
)
1187 zio_handle_panic_injection(spa
, tag
, 0);
1190 * Note: this txg_wait_synced() is important because it ensures
1191 * that there won't be more than one config change per txg.
1192 * This allows us to use the txg as the generation number.
1195 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
1198 ASSERT(!vd
->vdev_detached
|| vd
->vdev_dtl_sm
== NULL
);
1199 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1201 spa_config_exit(spa
, SCL_ALL
, spa
);
1205 * If the config changed, update the config cache.
1208 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1212 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1213 * locking of spa_vdev_enter(), we also want make sure the transactions have
1214 * synced to disk, and then update the global configuration cache with the new
1218 spa_vdev_exit(spa_t
*spa
, vdev_t
*vd
, uint64_t txg
, int error
)
1220 spa_vdev_config_exit(spa
, vd
, txg
, error
, FTAG
);
1221 mutex_exit(&spa_namespace_lock
);
1222 mutex_exit(&spa
->spa_vdev_top_lock
);
1228 * Lock the given spa_t for the purpose of changing vdev state.
1231 spa_vdev_state_enter(spa_t
*spa
, int oplocks
)
1233 int locks
= SCL_STATE_ALL
| oplocks
;
1236 * Root pools may need to read of the underlying devfs filesystem
1237 * when opening up a vdev. Unfortunately if we're holding the
1238 * SCL_ZIO lock it will result in a deadlock when we try to issue
1239 * the read from the root filesystem. Instead we "prefetch"
1240 * the associated vnodes that we need prior to opening the
1241 * underlying devices and cache them so that we can prevent
1242 * any I/O when we are doing the actual open.
1244 if (spa_is_root(spa
)) {
1245 int low
= locks
& ~(SCL_ZIO
- 1);
1246 int high
= locks
& ~low
;
1248 spa_config_enter(spa
, high
, spa
, RW_WRITER
);
1249 vdev_hold(spa
->spa_root_vdev
);
1250 spa_config_enter(spa
, low
, spa
, RW_WRITER
);
1252 spa_config_enter(spa
, locks
, spa
, RW_WRITER
);
1254 spa
->spa_vdev_locks
= locks
;
1258 spa_vdev_state_exit(spa_t
*spa
, vdev_t
*vd
, int error
)
1260 boolean_t config_changed
= B_FALSE
;
1263 if (vd
== NULL
|| vd
== spa
->spa_root_vdev
) {
1264 vdev_top
= spa
->spa_root_vdev
;
1266 vdev_top
= vd
->vdev_top
;
1269 if (vd
!= NULL
|| error
== 0)
1270 vdev_dtl_reassess(vdev_top
, 0, 0, B_FALSE
);
1273 if (vd
!= spa
->spa_root_vdev
)
1274 vdev_state_dirty(vdev_top
);
1276 config_changed
= B_TRUE
;
1277 spa
->spa_config_generation
++;
1280 if (spa_is_root(spa
))
1281 vdev_rele(spa
->spa_root_vdev
);
1283 ASSERT3U(spa
->spa_vdev_locks
, >=, SCL_STATE_ALL
);
1284 spa_config_exit(spa
, spa
->spa_vdev_locks
, spa
);
1287 * If anything changed, wait for it to sync. This ensures that,
1288 * from the system administrator's perspective, zpool(1M) commands
1289 * are synchronous. This is important for things like zpool offline:
1290 * when the command completes, you expect no further I/O from ZFS.
1293 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1296 * If the config changed, update the config cache.
1298 if (config_changed
) {
1299 mutex_enter(&spa_namespace_lock
);
1300 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1301 mutex_exit(&spa_namespace_lock
);
1308 * ==========================================================================
1309 * Miscellaneous functions
1310 * ==========================================================================
1314 spa_activate_mos_feature(spa_t
*spa
, const char *feature
, dmu_tx_t
*tx
)
1316 if (!nvlist_exists(spa
->spa_label_features
, feature
)) {
1317 fnvlist_add_boolean(spa
->spa_label_features
, feature
);
1319 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1320 * dirty the vdev config because lock SCL_CONFIG is not held.
1321 * Thankfully, in this case we don't need to dirty the config
1322 * because it will be written out anyway when we finish
1323 * creating the pool.
1325 if (tx
->tx_txg
!= TXG_INITIAL
)
1326 vdev_config_dirty(spa
->spa_root_vdev
);
1331 spa_deactivate_mos_feature(spa_t
*spa
, const char *feature
)
1333 if (nvlist_remove_all(spa
->spa_label_features
, feature
) == 0)
1334 vdev_config_dirty(spa
->spa_root_vdev
);
1341 spa_rename(const char *name
, const char *newname
)
1347 * Lookup the spa_t and grab the config lock for writing. We need to
1348 * actually open the pool so that we can sync out the necessary labels.
1349 * It's OK to call spa_open() with the namespace lock held because we
1350 * allow recursive calls for other reasons.
1352 mutex_enter(&spa_namespace_lock
);
1353 if ((err
= spa_open(name
, &spa
, FTAG
)) != 0) {
1354 mutex_exit(&spa_namespace_lock
);
1358 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1360 avl_remove(&spa_namespace_avl
, spa
);
1361 (void) strlcpy(spa
->spa_name
, newname
, sizeof (spa
->spa_name
));
1362 avl_add(&spa_namespace_avl
, spa
);
1365 * Sync all labels to disk with the new names by marking the root vdev
1366 * dirty and waiting for it to sync. It will pick up the new pool name
1369 vdev_config_dirty(spa
->spa_root_vdev
);
1371 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1373 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1376 * Sync the updated config cache.
1378 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
1380 spa_close(spa
, FTAG
);
1382 mutex_exit(&spa_namespace_lock
);
1388 * Return the spa_t associated with given pool_guid, if it exists. If
1389 * device_guid is non-zero, determine whether the pool exists *and* contains
1390 * a device with the specified device_guid.
1393 spa_by_guid(uint64_t pool_guid
, uint64_t device_guid
)
1396 avl_tree_t
*t
= &spa_namespace_avl
;
1398 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1400 for (spa
= avl_first(t
); spa
!= NULL
; spa
= AVL_NEXT(t
, spa
)) {
1401 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
1403 if (spa
->spa_root_vdev
== NULL
)
1405 if (spa_guid(spa
) == pool_guid
) {
1406 if (device_guid
== 0)
1409 if (vdev_lookup_by_guid(spa
->spa_root_vdev
,
1410 device_guid
) != NULL
)
1414 * Check any devices we may be in the process of adding.
1416 if (spa
->spa_pending_vdev
) {
1417 if (vdev_lookup_by_guid(spa
->spa_pending_vdev
,
1418 device_guid
) != NULL
)
1428 * Determine whether a pool with the given pool_guid exists.
1431 spa_guid_exists(uint64_t pool_guid
, uint64_t device_guid
)
1433 return (spa_by_guid(pool_guid
, device_guid
) != NULL
);
1437 spa_strdup(const char *s
)
1443 new = kmem_alloc(len
+ 1, KM_SLEEP
);
1451 spa_strfree(char *s
)
1453 kmem_free(s
, strlen(s
) + 1);
1457 spa_get_random(uint64_t range
)
1466 (void) random_get_pseudo_bytes((void *)&r
, sizeof (uint64_t));
1472 spa_generate_guid(spa_t
*spa
)
1474 uint64_t guid
= spa_get_random(-1ULL);
1477 while (guid
== 0 || spa_guid_exists(spa_guid(spa
), guid
))
1478 guid
= spa_get_random(-1ULL);
1480 while (guid
== 0 || spa_guid_exists(guid
, 0))
1481 guid
= spa_get_random(-1ULL);
1488 snprintf_blkptr(char *buf
, size_t buflen
, const blkptr_t
*bp
)
1491 char *checksum
= NULL
;
1492 char *compress
= NULL
;
1495 if (BP_GET_TYPE(bp
) & DMU_OT_NEWTYPE
) {
1496 dmu_object_byteswap_t bswap
=
1497 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
1498 (void) snprintf(type
, sizeof (type
), "bswap %s %s",
1499 DMU_OT_IS_METADATA(BP_GET_TYPE(bp
)) ?
1500 "metadata" : "data",
1501 dmu_ot_byteswap
[bswap
].ob_name
);
1503 (void) strlcpy(type
, dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1506 if (!BP_IS_EMBEDDED(bp
)) {
1508 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
;
1510 compress
= zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
;
1513 SNPRINTF_BLKPTR(snprintf
, ' ', buf
, buflen
, bp
, type
, checksum
,
1518 spa_freeze(spa_t
*spa
)
1520 uint64_t freeze_txg
= 0;
1522 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1523 if (spa
->spa_freeze_txg
== UINT64_MAX
) {
1524 freeze_txg
= spa_last_synced_txg(spa
) + TXG_SIZE
;
1525 spa
->spa_freeze_txg
= freeze_txg
;
1527 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1528 if (freeze_txg
!= 0)
1529 txg_wait_synced(spa_get_dsl(spa
), freeze_txg
);
1533 zfs_panic_recover(const char *fmt
, ...)
1538 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
1543 * This is a stripped-down version of strtoull, suitable only for converting
1544 * lowercase hexadecimal numbers that don't overflow.
1547 zfs_strtonum(const char *str
, char **nptr
)
1553 while ((c
= *str
) != '\0') {
1554 if (c
>= '0' && c
<= '9')
1556 else if (c
>= 'a' && c
<= 'f')
1557 digit
= 10 + c
- 'a';
1568 *nptr
= (char *)str
;
1574 spa_activate_allocation_classes(spa_t
*spa
, dmu_tx_t
*tx
)
1577 * We bump the feature refcount for each special vdev added to the pool
1579 ASSERT(spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
));
1580 spa_feature_incr(spa
, SPA_FEATURE_ALLOCATION_CLASSES
, tx
);
1584 * ==========================================================================
1585 * Accessor functions
1586 * ==========================================================================
1590 spa_shutting_down(spa_t
*spa
)
1592 return (spa
->spa_async_suspended
);
1596 spa_get_dsl(spa_t
*spa
)
1598 return (spa
->spa_dsl_pool
);
1602 spa_is_initializing(spa_t
*spa
)
1604 return (spa
->spa_is_initializing
);
1608 spa_indirect_vdevs_loaded(spa_t
*spa
)
1610 return (spa
->spa_indirect_vdevs_loaded
);
1614 spa_get_rootblkptr(spa_t
*spa
)
1616 return (&spa
->spa_ubsync
.ub_rootbp
);
1620 spa_set_rootblkptr(spa_t
*spa
, const blkptr_t
*bp
)
1622 spa
->spa_uberblock
.ub_rootbp
= *bp
;
1626 spa_altroot(spa_t
*spa
, char *buf
, size_t buflen
)
1628 if (spa
->spa_root
== NULL
)
1631 (void) strncpy(buf
, spa
->spa_root
, buflen
);
1635 spa_sync_pass(spa_t
*spa
)
1637 return (spa
->spa_sync_pass
);
1641 spa_name(spa_t
*spa
)
1643 return (spa
->spa_name
);
1647 spa_guid(spa_t
*spa
)
1649 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1653 * If we fail to parse the config during spa_load(), we can go through
1654 * the error path (which posts an ereport) and end up here with no root
1655 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1658 if (spa
->spa_root_vdev
== NULL
)
1659 return (spa
->spa_config_guid
);
1661 guid
= spa
->spa_last_synced_guid
!= 0 ?
1662 spa
->spa_last_synced_guid
: spa
->spa_root_vdev
->vdev_guid
;
1665 * Return the most recently synced out guid unless we're
1666 * in syncing context.
1668 if (dp
&& dsl_pool_sync_context(dp
))
1669 return (spa
->spa_root_vdev
->vdev_guid
);
1675 spa_load_guid(spa_t
*spa
)
1678 * This is a GUID that exists solely as a reference for the
1679 * purposes of the arc. It is generated at load time, and
1680 * is never written to persistent storage.
1682 return (spa
->spa_load_guid
);
1686 spa_last_synced_txg(spa_t
*spa
)
1688 return (spa
->spa_ubsync
.ub_txg
);
1692 spa_first_txg(spa_t
*spa
)
1694 return (spa
->spa_first_txg
);
1698 spa_syncing_txg(spa_t
*spa
)
1700 return (spa
->spa_syncing_txg
);
1704 * Return the last txg where data can be dirtied. The final txgs
1705 * will be used to just clear out any deferred frees that remain.
1708 spa_final_dirty_txg(spa_t
*spa
)
1710 return (spa
->spa_final_txg
- TXG_DEFER_SIZE
);
1714 spa_state(spa_t
*spa
)
1716 return (spa
->spa_state
);
1720 spa_load_state(spa_t
*spa
)
1722 return (spa
->spa_load_state
);
1726 spa_freeze_txg(spa_t
*spa
)
1728 return (spa
->spa_freeze_txg
);
1732 * Return the inflated asize for a logical write in bytes. This is used by the
1733 * DMU to calculate the space a logical write will require on disk.
1734 * If lsize is smaller than the largest physical block size allocatable on this
1735 * pool we use its value instead, since the write will end up using the whole
1739 spa_get_worst_case_asize(spa_t
*spa
, uint64_t lsize
)
1742 return (0); /* No inflation needed */
1743 return (MAX(lsize
, 1 << spa
->spa_max_ashift
) * spa_asize_inflation
);
1747 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1748 * or at least 128MB, unless that would cause it to be more than half the
1751 * See the comment above spa_slop_shift for details.
1754 spa_get_slop_space(spa_t
*spa
)
1756 uint64_t space
= spa_get_dspace(spa
);
1757 return (MAX(space
>> spa_slop_shift
, MIN(space
>> 1, spa_min_slop
)));
1761 spa_get_dspace(spa_t
*spa
)
1763 return (spa
->spa_dspace
);
1767 spa_get_checkpoint_space(spa_t
*spa
)
1769 return (spa
->spa_checkpoint_info
.sci_dspace
);
1773 spa_update_dspace(spa_t
*spa
)
1775 spa
->spa_dspace
= metaslab_class_get_dspace(spa_normal_class(spa
)) +
1776 ddt_get_dedup_dspace(spa
);
1777 if (spa
->spa_vdev_removal
!= NULL
) {
1779 * We can't allocate from the removing device, so
1780 * subtract its size. This prevents the DMU/DSL from
1781 * filling up the (now smaller) pool while we are in the
1782 * middle of removing the device.
1784 * Note that the DMU/DSL doesn't actually know or care
1785 * how much space is allocated (it does its own tracking
1786 * of how much space has been logically used). So it
1787 * doesn't matter that the data we are moving may be
1788 * allocated twice (on the old device and the new
1791 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
1793 vdev_lookup_top(spa
, spa
->spa_vdev_removal
->svr_vdev_id
);
1794 spa
->spa_dspace
-= spa_deflate(spa
) ?
1795 vd
->vdev_stat
.vs_dspace
: vd
->vdev_stat
.vs_space
;
1796 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
1801 * Return the failure mode that has been set to this pool. The default
1802 * behavior will be to block all I/Os when a complete failure occurs.
1805 spa_get_failmode(spa_t
*spa
)
1807 return (spa
->spa_failmode
);
1811 spa_suspended(spa_t
*spa
)
1813 return (spa
->spa_suspended
!= ZIO_SUSPEND_NONE
);
1817 spa_version(spa_t
*spa
)
1819 return (spa
->spa_ubsync
.ub_version
);
1823 spa_deflate(spa_t
*spa
)
1825 return (spa
->spa_deflate
);
1829 spa_normal_class(spa_t
*spa
)
1831 return (spa
->spa_normal_class
);
1835 spa_log_class(spa_t
*spa
)
1837 return (spa
->spa_log_class
);
1841 spa_special_class(spa_t
*spa
)
1843 return (spa
->spa_special_class
);
1847 spa_dedup_class(spa_t
*spa
)
1849 return (spa
->spa_dedup_class
);
1853 * Locate an appropriate allocation class
1856 spa_preferred_class(spa_t
*spa
, uint64_t size
, dmu_object_type_t objtype
,
1857 uint_t level
, uint_t special_smallblk
)
1859 if (DMU_OT_IS_ZIL(objtype
)) {
1860 if (spa
->spa_log_class
->mc_groups
!= 0)
1861 return (spa_log_class(spa
));
1863 return (spa_normal_class(spa
));
1866 boolean_t has_special_class
= spa
->spa_special_class
->mc_groups
!= 0;
1868 if (DMU_OT_IS_DDT(objtype
)) {
1869 if (spa
->spa_dedup_class
->mc_groups
!= 0)
1870 return (spa_dedup_class(spa
));
1871 else if (has_special_class
&& zfs_ddt_data_is_special
)
1872 return (spa_special_class(spa
));
1874 return (spa_normal_class(spa
));
1877 /* Indirect blocks for user data can land in special if allowed */
1878 if (level
> 0 && (DMU_OT_IS_FILE(objtype
) || objtype
== DMU_OT_ZVOL
)) {
1879 if (has_special_class
&& zfs_user_indirect_is_special
)
1880 return (spa_special_class(spa
));
1882 return (spa_normal_class(spa
));
1885 if (DMU_OT_IS_METADATA(objtype
) || level
> 0) {
1886 if (has_special_class
)
1887 return (spa_special_class(spa
));
1889 return (spa_normal_class(spa
));
1893 * Allow small file blocks in special class in some cases (like
1894 * for the dRAID vdev feature). But always leave a reserve of
1895 * zfs_special_class_metadata_reserve_pct exclusively for metadata.
1897 if (DMU_OT_IS_FILE(objtype
) &&
1898 has_special_class
&& size
< special_smallblk
) {
1899 metaslab_class_t
*special
= spa_special_class(spa
);
1900 uint64_t alloc
= metaslab_class_get_alloc(special
);
1901 uint64_t space
= metaslab_class_get_space(special
);
1903 (space
* (100 - zfs_special_class_metadata_reserve_pct
))
1910 return (spa_normal_class(spa
));
1914 spa_evicting_os_register(spa_t
*spa
, objset_t
*os
)
1916 mutex_enter(&spa
->spa_evicting_os_lock
);
1917 list_insert_head(&spa
->spa_evicting_os_list
, os
);
1918 mutex_exit(&spa
->spa_evicting_os_lock
);
1922 spa_evicting_os_deregister(spa_t
*spa
, objset_t
*os
)
1924 mutex_enter(&spa
->spa_evicting_os_lock
);
1925 list_remove(&spa
->spa_evicting_os_list
, os
);
1926 cv_broadcast(&spa
->spa_evicting_os_cv
);
1927 mutex_exit(&spa
->spa_evicting_os_lock
);
1931 spa_evicting_os_wait(spa_t
*spa
)
1933 mutex_enter(&spa
->spa_evicting_os_lock
);
1934 while (!list_is_empty(&spa
->spa_evicting_os_list
))
1935 cv_wait(&spa
->spa_evicting_os_cv
, &spa
->spa_evicting_os_lock
);
1936 mutex_exit(&spa
->spa_evicting_os_lock
);
1938 dmu_buf_user_evict_wait();
1942 spa_max_replication(spa_t
*spa
)
1945 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1946 * handle BPs with more than one DVA allocated. Set our max
1947 * replication level accordingly.
1949 if (spa_version(spa
) < SPA_VERSION_DITTO_BLOCKS
)
1951 return (MIN(SPA_DVAS_PER_BP
, spa_max_replication_override
));
1955 spa_prev_software_version(spa_t
*spa
)
1957 return (spa
->spa_prev_software_version
);
1961 spa_deadman_synctime(spa_t
*spa
)
1963 return (spa
->spa_deadman_synctime
);
1967 spa_deadman_ziotime(spa_t
*spa
)
1969 return (spa
->spa_deadman_ziotime
);
1973 spa_get_deadman_failmode(spa_t
*spa
)
1975 return (spa
->spa_deadman_failmode
);
1979 spa_set_deadman_failmode(spa_t
*spa
, const char *failmode
)
1981 if (strcmp(failmode
, "wait") == 0)
1982 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1983 else if (strcmp(failmode
, "continue") == 0)
1984 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
1985 else if (strcmp(failmode
, "panic") == 0)
1986 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
1988 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_WAIT
;
1992 dva_get_dsize_sync(spa_t
*spa
, const dva_t
*dva
)
1994 uint64_t asize
= DVA_GET_ASIZE(dva
);
1995 uint64_t dsize
= asize
;
1997 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1999 if (asize
!= 0 && spa
->spa_deflate
) {
2000 vdev_t
*vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
2002 dsize
= (asize
>> SPA_MINBLOCKSHIFT
) *
2003 vd
->vdev_deflate_ratio
;
2010 bp_get_dsize_sync(spa_t
*spa
, const blkptr_t
*bp
)
2014 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2015 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2021 bp_get_dsize(spa_t
*spa
, const blkptr_t
*bp
)
2025 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2027 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++)
2028 dsize
+= dva_get_dsize_sync(spa
, &bp
->blk_dva
[d
]);
2030 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2036 spa_dirty_data(spa_t
*spa
)
2038 return (spa
->spa_dsl_pool
->dp_dirty_total
);
2042 * ==========================================================================
2043 * Initialization and Termination
2044 * ==========================================================================
2048 spa_name_compare(const void *a1
, const void *a2
)
2050 const spa_t
*s1
= a1
;
2051 const spa_t
*s2
= a2
;
2054 s
= strcmp(s1
->spa_name
, s2
->spa_name
);
2056 return (AVL_ISIGN(s
));
2068 mutex_init(&spa_namespace_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2069 mutex_init(&spa_spare_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2070 mutex_init(&spa_l2cache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2071 cv_init(&spa_namespace_cv
, NULL
, CV_DEFAULT
, NULL
);
2073 avl_create(&spa_namespace_avl
, spa_name_compare
, sizeof (spa_t
),
2074 offsetof(spa_t
, spa_avl
));
2076 avl_create(&spa_spare_avl
, spa_spare_compare
, sizeof (spa_aux_t
),
2077 offsetof(spa_aux_t
, aux_avl
));
2079 avl_create(&spa_l2cache_avl
, spa_l2cache_compare
, sizeof (spa_aux_t
),
2080 offsetof(spa_aux_t
, aux_avl
));
2082 spa_mode_global
= mode
;
2085 if (spa_mode_global
!= FREAD
&& dprintf_find_string("watch")) {
2086 struct sigaction sa
;
2088 sa
.sa_flags
= SA_SIGINFO
;
2089 sigemptyset(&sa
.sa_mask
);
2090 sa
.sa_sigaction
= arc_buf_sigsegv
;
2092 if (sigaction(SIGSEGV
, &sa
, NULL
) == -1) {
2093 perror("could not enable watchpoints: "
2094 "sigaction(SIGSEGV, ...) = ");
2105 metaslab_alloc_trace_init();
2110 vdev_cache_stat_init();
2111 vdev_mirror_stat_init();
2112 vdev_raidz_math_init();
2116 zpool_feature_init();
2131 vdev_cache_stat_fini();
2132 vdev_mirror_stat_fini();
2133 vdev_raidz_math_fini();
2138 metaslab_alloc_trace_fini();
2146 avl_destroy(&spa_namespace_avl
);
2147 avl_destroy(&spa_spare_avl
);
2148 avl_destroy(&spa_l2cache_avl
);
2150 cv_destroy(&spa_namespace_cv
);
2151 mutex_destroy(&spa_namespace_lock
);
2152 mutex_destroy(&spa_spare_lock
);
2153 mutex_destroy(&spa_l2cache_lock
);
2157 * Return whether this pool has slogs. No locking needed.
2158 * It's not a problem if the wrong answer is returned as it's only for
2159 * performance and not correctness
2162 spa_has_slogs(spa_t
*spa
)
2164 return (spa
->spa_log_class
->mc_rotor
!= NULL
);
2168 spa_get_log_state(spa_t
*spa
)
2170 return (spa
->spa_log_state
);
2174 spa_set_log_state(spa_t
*spa
, spa_log_state_t state
)
2176 spa
->spa_log_state
= state
;
2180 spa_is_root(spa_t
*spa
)
2182 return (spa
->spa_is_root
);
2186 spa_writeable(spa_t
*spa
)
2188 return (!!(spa
->spa_mode
& FWRITE
) && spa
->spa_trust_config
);
2192 * Returns true if there is a pending sync task in any of the current
2193 * syncing txg, the current quiescing txg, or the current open txg.
2196 spa_has_pending_synctask(spa_t
*spa
)
2198 return (!txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_sync_tasks
) ||
2199 !txg_all_lists_empty(&spa
->spa_dsl_pool
->dp_early_sync_tasks
));
2203 spa_mode(spa_t
*spa
)
2205 return (spa
->spa_mode
);
2209 spa_bootfs(spa_t
*spa
)
2211 return (spa
->spa_bootfs
);
2215 spa_delegation(spa_t
*spa
)
2217 return (spa
->spa_delegation
);
2221 spa_meta_objset(spa_t
*spa
)
2223 return (spa
->spa_meta_objset
);
2227 spa_dedup_checksum(spa_t
*spa
)
2229 return (spa
->spa_dedup_checksum
);
2233 * Reset pool scan stat per scan pass (or reboot).
2236 spa_scan_stat_init(spa_t
*spa
)
2238 /* data not stored on disk */
2239 spa
->spa_scan_pass_start
= gethrestime_sec();
2240 if (dsl_scan_is_paused_scrub(spa
->spa_dsl_pool
->dp_scan
))
2241 spa
->spa_scan_pass_scrub_pause
= spa
->spa_scan_pass_start
;
2243 spa
->spa_scan_pass_scrub_pause
= 0;
2244 spa
->spa_scan_pass_scrub_spent_paused
= 0;
2245 spa
->spa_scan_pass_exam
= 0;
2246 spa
->spa_scan_pass_issued
= 0;
2247 vdev_scan_stat_init(spa
->spa_root_vdev
);
2251 * Get scan stats for zpool status reports
2254 spa_scan_get_stats(spa_t
*spa
, pool_scan_stat_t
*ps
)
2256 dsl_scan_t
*scn
= spa
->spa_dsl_pool
? spa
->spa_dsl_pool
->dp_scan
: NULL
;
2258 if (scn
== NULL
|| scn
->scn_phys
.scn_func
== POOL_SCAN_NONE
)
2259 return (SET_ERROR(ENOENT
));
2260 bzero(ps
, sizeof (pool_scan_stat_t
));
2262 /* data stored on disk */
2263 ps
->pss_func
= scn
->scn_phys
.scn_func
;
2264 ps
->pss_state
= scn
->scn_phys
.scn_state
;
2265 ps
->pss_start_time
= scn
->scn_phys
.scn_start_time
;
2266 ps
->pss_end_time
= scn
->scn_phys
.scn_end_time
;
2267 ps
->pss_to_examine
= scn
->scn_phys
.scn_to_examine
;
2268 ps
->pss_examined
= scn
->scn_phys
.scn_examined
;
2269 ps
->pss_to_process
= scn
->scn_phys
.scn_to_process
;
2270 ps
->pss_processed
= scn
->scn_phys
.scn_processed
;
2271 ps
->pss_errors
= scn
->scn_phys
.scn_errors
;
2273 /* data not stored on disk */
2274 ps
->pss_pass_exam
= spa
->spa_scan_pass_exam
;
2275 ps
->pss_pass_start
= spa
->spa_scan_pass_start
;
2276 ps
->pss_pass_scrub_pause
= spa
->spa_scan_pass_scrub_pause
;
2277 ps
->pss_pass_scrub_spent_paused
= spa
->spa_scan_pass_scrub_spent_paused
;
2278 ps
->pss_pass_issued
= spa
->spa_scan_pass_issued
;
2280 scn
->scn_issued_before_pass
+ spa
->spa_scan_pass_issued
;
2286 spa_maxblocksize(spa_t
*spa
)
2288 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
))
2289 return (SPA_MAXBLOCKSIZE
);
2291 return (SPA_OLD_MAXBLOCKSIZE
);
2296 * Returns the txg that the last device removal completed. No indirect mappings
2297 * have been added since this txg.
2300 spa_get_last_removal_txg(spa_t
*spa
)
2303 uint64_t ret
= -1ULL;
2305 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2307 * sr_prev_indirect_vdev is only modified while holding all the
2308 * config locks, so it is sufficient to hold SCL_VDEV as reader when
2311 vdevid
= spa
->spa_removing_phys
.sr_prev_indirect_vdev
;
2313 while (vdevid
!= -1ULL) {
2314 vdev_t
*vd
= vdev_lookup_top(spa
, vdevid
);
2315 vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
;
2317 ASSERT3P(vd
->vdev_ops
, ==, &vdev_indirect_ops
);
2320 * If the removal did not remap any data, we don't care.
2322 if (vdev_indirect_births_count(vib
) != 0) {
2323 ret
= vdev_indirect_births_last_entry_txg(vib
);
2327 vdevid
= vd
->vdev_indirect_config
.vic_prev_indirect_vdev
;
2329 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2332 spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
2338 spa_maxdnodesize(spa_t
*spa
)
2340 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
))
2341 return (DNODE_MAX_SIZE
);
2343 return (DNODE_MIN_SIZE
);
2347 spa_multihost(spa_t
*spa
)
2349 return (spa
->spa_multihost
? B_TRUE
: B_FALSE
);
2353 spa_get_hostid(void)
2355 unsigned long myhostid
;
2358 myhostid
= zone_get_hostid(NULL
);
2361 * We're emulating the system's hostid in userland, so
2362 * we can't use zone_get_hostid().
2364 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2365 #endif /* _KERNEL */
2371 spa_trust_config(spa_t
*spa
)
2373 return (spa
->spa_trust_config
);
2377 spa_missing_tvds_allowed(spa_t
*spa
)
2379 return (spa
->spa_missing_tvds_allowed
);
2383 spa_set_missing_tvds(spa_t
*spa
, uint64_t missing
)
2385 spa
->spa_missing_tvds
= missing
;
2389 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc).
2392 spa_state_to_name(spa_t
*spa
)
2394 vdev_state_t state
= spa
->spa_root_vdev
->vdev_state
;
2395 vdev_aux_t aux
= spa
->spa_root_vdev
->vdev_stat
.vs_aux
;
2397 if (spa_suspended(spa
) &&
2398 (spa_get_failmode(spa
) != ZIO_FAILURE_MODE_CONTINUE
))
2399 return ("SUSPENDED");
2402 case VDEV_STATE_CLOSED
:
2403 case VDEV_STATE_OFFLINE
:
2405 case VDEV_STATE_REMOVED
:
2407 case VDEV_STATE_CANT_OPEN
:
2408 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
2410 else if (aux
== VDEV_AUX_SPLIT_POOL
)
2414 case VDEV_STATE_FAULTED
:
2416 case VDEV_STATE_DEGRADED
:
2417 return ("DEGRADED");
2418 case VDEV_STATE_HEALTHY
:
2428 spa_top_vdevs_spacemap_addressable(spa_t
*spa
)
2430 vdev_t
*rvd
= spa
->spa_root_vdev
;
2431 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2432 if (!vdev_is_spacemap_addressable(rvd
->vdev_child
[c
]))
2439 spa_has_checkpoint(spa_t
*spa
)
2441 return (spa
->spa_checkpoint_txg
!= 0);
2445 spa_importing_readonly_checkpoint(spa_t
*spa
)
2447 return ((spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
) &&
2448 spa
->spa_mode
== FREAD
);
2452 spa_min_claim_txg(spa_t
*spa
)
2454 uint64_t checkpoint_txg
= spa
->spa_uberblock
.ub_checkpoint_txg
;
2456 if (checkpoint_txg
!= 0)
2457 return (checkpoint_txg
+ 1);
2459 return (spa
->spa_first_txg
);
2463 * If there is a checkpoint, async destroys may consume more space from
2464 * the pool instead of freeing it. In an attempt to save the pool from
2465 * getting suspended when it is about to run out of space, we stop
2466 * processing async destroys.
2469 spa_suspend_async_destroy(spa_t
*spa
)
2471 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2473 uint64_t unreserved
= dsl_pool_unreserved_space(dp
,
2474 ZFS_SPACE_CHECK_EXTRA_RESERVED
);
2475 uint64_t used
= dsl_dir_phys(dp
->dp_root_dir
)->dd_used_bytes
;
2476 uint64_t avail
= (unreserved
> used
) ? (unreserved
- used
) : 0;
2478 if (spa_has_checkpoint(spa
) && avail
== 0)
2484 #if defined(_KERNEL)
2486 #include <linux/mod_compat.h>
2489 param_set_deadman_failmode(const char *val
, zfs_kernel_param_t
*kp
)
2495 return (SET_ERROR(-EINVAL
));
2497 if ((p
= strchr(val
, '\n')) != NULL
)
2500 if (strcmp(val
, "wait") != 0 && strcmp(val
, "continue") != 0 &&
2501 strcmp(val
, "panic"))
2502 return (SET_ERROR(-EINVAL
));
2504 if (spa_mode_global
!= 0) {
2505 mutex_enter(&spa_namespace_lock
);
2506 while ((spa
= spa_next(spa
)) != NULL
)
2507 spa_set_deadman_failmode(spa
, val
);
2508 mutex_exit(&spa_namespace_lock
);
2511 return (param_set_charp(val
, kp
));
2515 param_set_deadman_ziotime(const char *val
, zfs_kernel_param_t
*kp
)
2520 error
= param_set_ulong(val
, kp
);
2522 return (SET_ERROR(error
));
2524 if (spa_mode_global
!= 0) {
2525 mutex_enter(&spa_namespace_lock
);
2526 while ((spa
= spa_next(spa
)) != NULL
)
2527 spa
->spa_deadman_ziotime
=
2528 MSEC2NSEC(zfs_deadman_ziotime_ms
);
2529 mutex_exit(&spa_namespace_lock
);
2536 param_set_deadman_synctime(const char *val
, zfs_kernel_param_t
*kp
)
2541 error
= param_set_ulong(val
, kp
);
2543 return (SET_ERROR(error
));
2545 if (spa_mode_global
!= 0) {
2546 mutex_enter(&spa_namespace_lock
);
2547 while ((spa
= spa_next(spa
)) != NULL
)
2548 spa
->spa_deadman_synctime
=
2549 MSEC2NSEC(zfs_deadman_synctime_ms
);
2550 mutex_exit(&spa_namespace_lock
);
2556 /* Namespace manipulation */
2557 EXPORT_SYMBOL(spa_lookup
);
2558 EXPORT_SYMBOL(spa_add
);
2559 EXPORT_SYMBOL(spa_remove
);
2560 EXPORT_SYMBOL(spa_next
);
2562 /* Refcount functions */
2563 EXPORT_SYMBOL(spa_open_ref
);
2564 EXPORT_SYMBOL(spa_close
);
2565 EXPORT_SYMBOL(spa_refcount_zero
);
2567 /* Pool configuration lock */
2568 EXPORT_SYMBOL(spa_config_tryenter
);
2569 EXPORT_SYMBOL(spa_config_enter
);
2570 EXPORT_SYMBOL(spa_config_exit
);
2571 EXPORT_SYMBOL(spa_config_held
);
2573 /* Pool vdev add/remove lock */
2574 EXPORT_SYMBOL(spa_vdev_enter
);
2575 EXPORT_SYMBOL(spa_vdev_exit
);
2577 /* Pool vdev state change lock */
2578 EXPORT_SYMBOL(spa_vdev_state_enter
);
2579 EXPORT_SYMBOL(spa_vdev_state_exit
);
2581 /* Accessor functions */
2582 EXPORT_SYMBOL(spa_shutting_down
);
2583 EXPORT_SYMBOL(spa_get_dsl
);
2584 EXPORT_SYMBOL(spa_get_rootblkptr
);
2585 EXPORT_SYMBOL(spa_set_rootblkptr
);
2586 EXPORT_SYMBOL(spa_altroot
);
2587 EXPORT_SYMBOL(spa_sync_pass
);
2588 EXPORT_SYMBOL(spa_name
);
2589 EXPORT_SYMBOL(spa_guid
);
2590 EXPORT_SYMBOL(spa_last_synced_txg
);
2591 EXPORT_SYMBOL(spa_first_txg
);
2592 EXPORT_SYMBOL(spa_syncing_txg
);
2593 EXPORT_SYMBOL(spa_version
);
2594 EXPORT_SYMBOL(spa_state
);
2595 EXPORT_SYMBOL(spa_load_state
);
2596 EXPORT_SYMBOL(spa_freeze_txg
);
2597 EXPORT_SYMBOL(spa_get_dspace
);
2598 EXPORT_SYMBOL(spa_update_dspace
);
2599 EXPORT_SYMBOL(spa_deflate
);
2600 EXPORT_SYMBOL(spa_normal_class
);
2601 EXPORT_SYMBOL(spa_log_class
);
2602 EXPORT_SYMBOL(spa_special_class
);
2603 EXPORT_SYMBOL(spa_preferred_class
);
2604 EXPORT_SYMBOL(spa_max_replication
);
2605 EXPORT_SYMBOL(spa_prev_software_version
);
2606 EXPORT_SYMBOL(spa_get_failmode
);
2607 EXPORT_SYMBOL(spa_suspended
);
2608 EXPORT_SYMBOL(spa_bootfs
);
2609 EXPORT_SYMBOL(spa_delegation
);
2610 EXPORT_SYMBOL(spa_meta_objset
);
2611 EXPORT_SYMBOL(spa_maxblocksize
);
2612 EXPORT_SYMBOL(spa_maxdnodesize
);
2614 /* Miscellaneous support routines */
2615 EXPORT_SYMBOL(spa_rename
);
2616 EXPORT_SYMBOL(spa_guid_exists
);
2617 EXPORT_SYMBOL(spa_strdup
);
2618 EXPORT_SYMBOL(spa_strfree
);
2619 EXPORT_SYMBOL(spa_get_random
);
2620 EXPORT_SYMBOL(spa_generate_guid
);
2621 EXPORT_SYMBOL(snprintf_blkptr
);
2622 EXPORT_SYMBOL(spa_freeze
);
2623 EXPORT_SYMBOL(spa_upgrade
);
2624 EXPORT_SYMBOL(spa_evict_all
);
2625 EXPORT_SYMBOL(spa_lookup_by_guid
);
2626 EXPORT_SYMBOL(spa_has_spare
);
2627 EXPORT_SYMBOL(dva_get_dsize_sync
);
2628 EXPORT_SYMBOL(bp_get_dsize_sync
);
2629 EXPORT_SYMBOL(bp_get_dsize
);
2630 EXPORT_SYMBOL(spa_has_slogs
);
2631 EXPORT_SYMBOL(spa_is_root
);
2632 EXPORT_SYMBOL(spa_writeable
);
2633 EXPORT_SYMBOL(spa_mode
);
2634 EXPORT_SYMBOL(spa_namespace_lock
);
2635 EXPORT_SYMBOL(spa_trust_config
);
2636 EXPORT_SYMBOL(spa_missing_tvds_allowed
);
2637 EXPORT_SYMBOL(spa_set_missing_tvds
);
2638 EXPORT_SYMBOL(spa_state_to_name
);
2639 EXPORT_SYMBOL(spa_importing_readonly_checkpoint
);
2640 EXPORT_SYMBOL(spa_min_claim_txg
);
2641 EXPORT_SYMBOL(spa_suspend_async_destroy
);
2642 EXPORT_SYMBOL(spa_has_checkpoint
);
2643 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable
);
2646 module_param(zfs_flags
, uint
, 0644);
2647 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
2649 module_param(zfs_recover
, int, 0644);
2650 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
2652 module_param(zfs_free_leak_on_eio
, int, 0644);
2653 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
2654 "Set to ignore IO errors during free and permanently leak the space");
2656 module_param_call(zfs_deadman_synctime_ms
, param_set_deadman_synctime
,
2657 param_get_ulong
, &zfs_deadman_synctime_ms
, 0644);
2658 MODULE_PARM_DESC(zfs_deadman_synctime_ms
,
2659 "Pool sync expiration time in milliseconds");
2661 module_param_call(zfs_deadman_ziotime_ms
, param_set_deadman_ziotime
,
2662 param_get_ulong
, &zfs_deadman_ziotime_ms
, 0644);
2663 MODULE_PARM_DESC(zfs_deadman_ziotime_ms
,
2664 "IO expiration time in milliseconds");
2666 module_param(zfs_deadman_checktime_ms
, ulong
, 0644);
2667 MODULE_PARM_DESC(zfs_deadman_checktime_ms
,
2668 "Dead I/O check interval in milliseconds");
2670 module_param(zfs_deadman_enabled
, int, 0644);
2671 MODULE_PARM_DESC(zfs_deadman_enabled
, "Enable deadman timer");
2673 module_param_call(zfs_deadman_failmode
, param_set_deadman_failmode
,
2674 param_get_charp
, &zfs_deadman_failmode
, 0644);
2675 MODULE_PARM_DESC(zfs_deadman_failmode
, "Failmode for deadman timer");
2677 module_param(spa_asize_inflation
, int, 0644);
2678 MODULE_PARM_DESC(spa_asize_inflation
,
2679 "SPA size estimate multiplication factor");
2681 module_param(spa_slop_shift
, int, 0644);
2682 MODULE_PARM_DESC(spa_slop_shift
, "Reserved free space in pool");
2684 module_param(zfs_ddt_data_is_special
, int, 0644);
2685 MODULE_PARM_DESC(zfs_ddt_data_is_special
,
2686 "Place DDT data into the special class");
2688 module_param(zfs_user_indirect_is_special
, int, 0644);
2689 MODULE_PARM_DESC(zfs_user_indirect_is_special
,
2690 "Place user data indirect blocks into the special class");