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, 2017 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.
29 #include <sys/zfs_context.h>
30 #include <sys/spa_impl.h>
32 #include <sys/zio_checksum.h>
33 #include <sys/zio_compress.h>
35 #include <sys/dmu_tx.h>
38 #include <sys/vdev_impl.h>
39 #include <sys/vdev_file.h>
40 #include <sys/vdev_raidz.h>
41 #include <sys/metaslab.h>
42 #include <sys/uberblock_impl.h>
45 #include <sys/unique.h>
46 #include <sys/dsl_pool.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/fm/util.h>
50 #include <sys/dsl_scan.h>
51 #include <sys/fs/zfs.h>
52 #include <sys/metaslab_impl.h>
55 #include <sys/kstat.h>
57 #include <sys/zfeature.h>
58 #include "qat_compress.h"
63 * There are four basic locks for managing spa_t structures:
65 * spa_namespace_lock (global mutex)
67 * This lock must be acquired to do any of the following:
69 * - Lookup a spa_t by name
70 * - Add or remove a spa_t from the namespace
71 * - Increase spa_refcount from non-zero
72 * - Check if spa_refcount is zero
74 * - add/remove/attach/detach devices
75 * - Held for the duration of create/destroy/import/export
77 * It does not need to handle recursion. A create or destroy may
78 * reference objects (files or zvols) in other pools, but by
79 * definition they must have an existing reference, and will never need
80 * to lookup a spa_t by name.
82 * spa_refcount (per-spa refcount_t protected by mutex)
84 * This reference count keep track of any active users of the spa_t. The
85 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
86 * the refcount is never really 'zero' - opening a pool implicitly keeps
87 * some references in the DMU. Internally we check against spa_minref, but
88 * present the image of a zero/non-zero value to consumers.
90 * spa_config_lock[] (per-spa array of rwlocks)
92 * This protects the spa_t from config changes, and must be held in
93 * the following circumstances:
95 * - RW_READER to perform I/O to the spa
96 * - RW_WRITER to change the vdev config
98 * The locking order is fairly straightforward:
100 * spa_namespace_lock -> spa_refcount
102 * The namespace lock must be acquired to increase the refcount from 0
103 * or to check if it is zero.
105 * spa_refcount -> spa_config_lock[]
107 * There must be at least one valid reference on the spa_t to acquire
110 * spa_namespace_lock -> spa_config_lock[]
112 * The namespace lock must always be taken before the config lock.
115 * The spa_namespace_lock can be acquired directly and is globally visible.
117 * The namespace is manipulated using the following functions, all of which
118 * require the spa_namespace_lock to be held.
120 * spa_lookup() Lookup a spa_t by name.
122 * spa_add() Create a new spa_t in the namespace.
124 * spa_remove() Remove a spa_t from the namespace. This also
125 * frees up any memory associated with the spa_t.
127 * spa_next() Returns the next spa_t in the system, or the
128 * first if NULL is passed.
130 * spa_evict_all() Shutdown and remove all spa_t structures in
133 * spa_guid_exists() Determine whether a pool/device guid exists.
135 * The spa_refcount is manipulated using the following functions:
137 * spa_open_ref() Adds a reference to the given spa_t. Must be
138 * called with spa_namespace_lock held if the
139 * refcount is currently zero.
141 * spa_close() Remove a reference from the spa_t. This will
142 * not free the spa_t or remove it from the
143 * namespace. No locking is required.
145 * spa_refcount_zero() Returns true if the refcount is currently
146 * zero. Must be called with spa_namespace_lock
149 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
150 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
151 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
153 * To read the configuration, it suffices to hold one of these locks as reader.
154 * To modify the configuration, you must hold all locks as writer. To modify
155 * vdev state without altering the vdev tree's topology (e.g. online/offline),
156 * you must hold SCL_STATE and SCL_ZIO as writer.
158 * We use these distinct config locks to avoid recursive lock entry.
159 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
160 * block allocations (SCL_ALLOC), which may require reading space maps
161 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
163 * The spa config locks cannot be normal rwlocks because we need the
164 * ability to hand off ownership. For example, SCL_ZIO is acquired
165 * by the issuing thread and later released by an interrupt thread.
166 * They do, however, obey the usual write-wanted semantics to prevent
167 * writer (i.e. system administrator) starvation.
169 * The lock acquisition rules are as follows:
172 * Protects changes to the vdev tree topology, such as vdev
173 * add/remove/attach/detach. Protects the dirty config list
174 * (spa_config_dirty_list) and the set of spares and l2arc devices.
177 * Protects changes to pool state and vdev state, such as vdev
178 * online/offline/fault/degrade/clear. Protects the dirty state list
179 * (spa_state_dirty_list) and global pool state (spa_state).
182 * Protects changes to metaslab groups and classes.
183 * Held as reader by metaslab_alloc() and metaslab_claim().
186 * Held by bp-level zios (those which have no io_vd upon entry)
187 * to prevent changes to the vdev tree. The bp-level zio implicitly
188 * protects all of its vdev child zios, which do not hold SCL_ZIO.
191 * Protects changes to metaslab groups and classes.
192 * Held as reader by metaslab_free(). SCL_FREE is distinct from
193 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
194 * blocks in zio_done() while another i/o that holds either
195 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
198 * Held as reader to prevent changes to the vdev tree during trivial
199 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
200 * other locks, and lower than all of them, to ensure that it's safe
201 * to acquire regardless of caller context.
203 * In addition, the following rules apply:
205 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
206 * The lock ordering is SCL_CONFIG > spa_props_lock.
208 * (b) I/O operations on leaf vdevs. For any zio operation that takes
209 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
210 * or zio_write_phys() -- the caller must ensure that the config cannot
211 * cannot change in the interim, and that the vdev cannot be reopened.
212 * SCL_STATE as reader suffices for both.
214 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
216 * spa_vdev_enter() Acquire the namespace lock and the config lock
219 * spa_vdev_exit() Release the config lock, wait for all I/O
220 * to complete, sync the updated configs to the
221 * cache, and release the namespace lock.
223 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
224 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
225 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
227 * spa_rename() is also implemented within this file since it requires
228 * manipulation of the namespace.
231 static avl_tree_t spa_namespace_avl;
232 kmutex_t spa_namespace_lock;
233 static kcondvar_t spa_namespace_cv;
234 int spa_max_replication_override = SPA_DVAS_PER_BP;
236 static kmutex_t spa_spare_lock;
237 static avl_tree_t spa_spare_avl;
238 static kmutex_t spa_l2cache_lock;
239 static avl_tree_t spa_l2cache_avl;
241 kmem_cache_t *spa_buffer_pool;
245 /* Everything except dprintf and spa is on by default in debug builds */
246 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
252 * zfs_recover can be set to nonzero to attempt to recover from
253 * otherwise-fatal errors, typically caused by on-disk corruption. When
254 * set, calls to zfs_panic_recover() will turn into warning messages.
255 * This should only be used as a last resort, as it typically results
256 * in leaked space, or worse.
258 int zfs_recover = B_FALSE;
261 * If destroy encounters an EIO while reading metadata (e.g. indirect
262 * blocks), space referenced by the missing metadata can not be freed.
263 * Normally this causes the background destroy to become "stalled", as
264 * it is unable to make forward progress. While in this stalled state,
265 * all remaining space to free from the error-encountering filesystem is
266 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
267 * permanently leak the space from indirect blocks that can not be read,
268 * and continue to free everything else that it can.
270 * The default, "stalling" behavior is useful if the storage partially
271 * fails (i.e. some but not all i/os fail), and then later recovers. In
272 * this case, we will be able to continue pool operations while it is
273 * partially failed, and when it recovers, we can continue to free the
274 * space, with no leaks. However, note that this case is actually
277 * Typically pools either (a) fail completely (but perhaps temporarily,
278 * e.g. a top-level vdev going offline), or (b) have localized,
279 * permanent errors (e.g. disk returns the wrong data due to bit flip or
280 * firmware bug). In case (a), this setting does not matter because the
281 * pool will be suspended and the sync thread will not be able to make
282 * forward progress regardless. In case (b), because the error is
283 * permanent, the best we can do is leak the minimum amount of space,
284 * which is what setting this flag will do. Therefore, it is reasonable
285 * for this flag to normally be set, but we chose the more conservative
286 * approach of not setting it, so that there is no possibility of
287 * leaking space in the "partial temporary" failure case.
289 int zfs_free_leak_on_eio = B_FALSE;
292 * Expiration time in milliseconds. This value has two meanings. First it is
293 * used to determine when the spa_deadman() logic should fire. By default the
294 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
295 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
296 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
299 unsigned long zfs_deadman_synctime_ms = 1000000ULL;
302 * Check time in milliseconds. This defines the frequency at which we check
305 unsigned long zfs_deadman_checktime_ms = 5000ULL;
308 * By default the deadman is enabled.
310 int zfs_deadman_enabled = 1;
313 * The worst case is single-sector max-parity RAID-Z blocks, in which
314 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
315 * times the size; so just assume that. Add to this the fact that
316 * we can have up to 3 DVAs per bp, and one more factor of 2 because
317 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
319 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
321 int spa_asize_inflation = 24;
324 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
325 * the pool to be consumed. This ensures that we don't run the pool
326 * completely out of space, due to unaccounted changes (e.g. to the MOS).
327 * It also limits the worst-case time to allocate space. If we have
328 * less than this amount of free space, most ZPL operations (e.g. write,
329 * create) will return ENOSPC.
331 * Certain operations (e.g. file removal, most administrative actions) can
332 * use half the slop space. They will only return ENOSPC if less than half
333 * the slop space is free. Typically, once the pool has less than the slop
334 * space free, the user will use these operations to free up space in the pool.
335 * These are the operations that call dsl_pool_adjustedsize() with the netfree
336 * argument set to TRUE.
338 * A very restricted set of operations are always permitted, regardless of
339 * the amount of free space. These are the operations that call
340 * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these
341 * operations result in a net increase in the amount of space used,
342 * it is possible to run the pool completely out of space, causing it to
343 * be permanently read-only.
345 * Note that on very small pools, the slop space will be larger than
346 * 3.2%, in an effort to have it be at least spa_min_slop (128MB),
347 * but we never allow it to be more than half the pool size.
349 * See also the comments in zfs_space_check_t.
351 int spa_slop_shift = 5;
352 uint64_t spa_min_slop = 128 * 1024 * 1024;
355 * ==========================================================================
357 * ==========================================================================
360 spa_config_lock_init(spa_t *spa)
364 for (i = 0; i < SCL_LOCKS; i++) {
365 spa_config_lock_t *scl = &spa->spa_config_lock[i];
366 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
367 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
368 refcount_create_untracked(&scl->scl_count);
369 scl->scl_writer = NULL;
370 scl->scl_write_wanted = 0;
375 spa_config_lock_destroy(spa_t *spa)
379 for (i = 0; i < SCL_LOCKS; i++) {
380 spa_config_lock_t *scl = &spa->spa_config_lock[i];
381 mutex_destroy(&scl->scl_lock);
382 cv_destroy(&scl->scl_cv);
383 refcount_destroy(&scl->scl_count);
384 ASSERT(scl->scl_writer == NULL);
385 ASSERT(scl->scl_write_wanted == 0);
390 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
394 for (i = 0; i < SCL_LOCKS; i++) {
395 spa_config_lock_t *scl = &spa->spa_config_lock[i];
396 if (!(locks & (1 << i)))
398 mutex_enter(&scl->scl_lock);
399 if (rw == RW_READER) {
400 if (scl->scl_writer || scl->scl_write_wanted) {
401 mutex_exit(&scl->scl_lock);
402 spa_config_exit(spa, locks & ((1 << i) - 1),
407 ASSERT(scl->scl_writer != curthread);
408 if (!refcount_is_zero(&scl->scl_count)) {
409 mutex_exit(&scl->scl_lock);
410 spa_config_exit(spa, locks & ((1 << i) - 1),
414 scl->scl_writer = curthread;
416 (void) refcount_add(&scl->scl_count, tag);
417 mutex_exit(&scl->scl_lock);
423 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
428 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
430 for (i = 0; i < SCL_LOCKS; i++) {
431 spa_config_lock_t *scl = &spa->spa_config_lock[i];
432 if (scl->scl_writer == curthread)
433 wlocks_held |= (1 << i);
434 if (!(locks & (1 << i)))
436 mutex_enter(&scl->scl_lock);
437 if (rw == RW_READER) {
438 while (scl->scl_writer || scl->scl_write_wanted) {
439 cv_wait(&scl->scl_cv, &scl->scl_lock);
442 ASSERT(scl->scl_writer != curthread);
443 while (!refcount_is_zero(&scl->scl_count)) {
444 scl->scl_write_wanted++;
445 cv_wait(&scl->scl_cv, &scl->scl_lock);
446 scl->scl_write_wanted--;
448 scl->scl_writer = curthread;
450 (void) refcount_add(&scl->scl_count, tag);
451 mutex_exit(&scl->scl_lock);
453 ASSERT(wlocks_held <= locks);
457 spa_config_exit(spa_t *spa, int locks, void *tag)
461 for (i = SCL_LOCKS - 1; i >= 0; i--) {
462 spa_config_lock_t *scl = &spa->spa_config_lock[i];
463 if (!(locks & (1 << i)))
465 mutex_enter(&scl->scl_lock);
466 ASSERT(!refcount_is_zero(&scl->scl_count));
467 if (refcount_remove(&scl->scl_count, tag) == 0) {
468 ASSERT(scl->scl_writer == NULL ||
469 scl->scl_writer == curthread);
470 scl->scl_writer = NULL; /* OK in either case */
471 cv_broadcast(&scl->scl_cv);
473 mutex_exit(&scl->scl_lock);
478 spa_config_held(spa_t *spa, int locks, krw_t rw)
480 int i, locks_held = 0;
482 for (i = 0; i < SCL_LOCKS; i++) {
483 spa_config_lock_t *scl = &spa->spa_config_lock[i];
484 if (!(locks & (1 << i)))
486 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
487 (rw == RW_WRITER && scl->scl_writer == curthread))
488 locks_held |= 1 << i;
495 * ==========================================================================
496 * SPA namespace functions
497 * ==========================================================================
501 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
502 * Returns NULL if no matching spa_t is found.
505 spa_lookup(const char *name)
507 static spa_t search; /* spa_t is large; don't allocate on stack */
512 ASSERT(MUTEX_HELD(&spa_namespace_lock));
514 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
517 * If it's a full dataset name, figure out the pool name and
520 cp = strpbrk(search.spa_name, "/@#");
524 spa = avl_find(&spa_namespace_avl, &search, &where);
530 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
531 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
532 * looking for potentially hung I/Os.
535 spa_deadman(void *arg)
539 /* Disable the deadman if the pool is suspended. */
540 if (spa_suspended(spa))
543 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
544 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
545 ++spa->spa_deadman_calls);
546 if (zfs_deadman_enabled)
547 vdev_deadman(spa->spa_root_vdev);
549 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq,
550 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
551 MSEC_TO_TICK(zfs_deadman_checktime_ms));
555 * Create an uninitialized spa_t with the given name. Requires
556 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
557 * exist by calling spa_lookup() first.
560 spa_add(const char *name, nvlist_t *config, const char *altroot)
563 spa_config_dirent_t *dp;
567 ASSERT(MUTEX_HELD(&spa_namespace_lock));
569 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
571 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
572 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
573 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
574 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
575 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
576 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
577 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
578 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
579 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
580 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
581 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
582 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL);
583 mutex_init(&spa->spa_alloc_lock, NULL, MUTEX_DEFAULT, NULL);
585 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
586 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
587 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
588 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
589 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
591 for (t = 0; t < TXG_SIZE; t++)
592 bplist_create(&spa->spa_free_bplist[t]);
594 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
595 spa->spa_state = POOL_STATE_UNINITIALIZED;
596 spa->spa_freeze_txg = UINT64_MAX;
597 spa->spa_final_txg = UINT64_MAX;
598 spa->spa_load_max_txg = UINT64_MAX;
600 spa->spa_proc_state = SPA_PROC_NONE;
602 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
604 refcount_create(&spa->spa_refcount);
605 spa_config_lock_init(spa);
608 avl_add(&spa_namespace_avl, spa);
611 * Set the alternate root, if there is one.
614 spa->spa_root = spa_strdup(altroot);
616 avl_create(&spa->spa_alloc_tree, zio_bookmark_compare,
617 sizeof (zio_t), offsetof(zio_t, io_alloc_node));
620 * Every pool starts with the default cachefile
622 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
623 offsetof(spa_config_dirent_t, scd_link));
625 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
626 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
627 list_insert_head(&spa->spa_config_list, dp);
629 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
632 if (config != NULL) {
635 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
637 VERIFY(nvlist_dup(features, &spa->spa_label_features,
641 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
644 if (spa->spa_label_features == NULL) {
645 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
649 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
651 spa->spa_min_ashift = INT_MAX;
652 spa->spa_max_ashift = 0;
654 /* Reset cached value */
655 spa->spa_dedup_dspace = ~0ULL;
658 * As a pool is being created, treat all features as disabled by
659 * setting SPA_FEATURE_DISABLED for all entries in the feature
662 for (i = 0; i < SPA_FEATURES; i++) {
663 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
670 * Removes a spa_t from the namespace, freeing up any memory used. Requires
671 * spa_namespace_lock. This is called only after the spa_t has been closed and
675 spa_remove(spa_t *spa)
677 spa_config_dirent_t *dp;
680 ASSERT(MUTEX_HELD(&spa_namespace_lock));
681 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
682 ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
684 nvlist_free(spa->spa_config_splitting);
686 avl_remove(&spa_namespace_avl, spa);
687 cv_broadcast(&spa_namespace_cv);
690 spa_strfree(spa->spa_root);
692 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
693 list_remove(&spa->spa_config_list, dp);
694 if (dp->scd_path != NULL)
695 spa_strfree(dp->scd_path);
696 kmem_free(dp, sizeof (spa_config_dirent_t));
699 avl_destroy(&spa->spa_alloc_tree);
700 list_destroy(&spa->spa_config_list);
702 nvlist_free(spa->spa_label_features);
703 nvlist_free(spa->spa_load_info);
704 nvlist_free(spa->spa_feat_stats);
705 spa_config_set(spa, NULL);
707 refcount_destroy(&spa->spa_refcount);
709 spa_stats_destroy(spa);
710 spa_config_lock_destroy(spa);
712 for (t = 0; t < TXG_SIZE; t++)
713 bplist_destroy(&spa->spa_free_bplist[t]);
715 zio_checksum_templates_free(spa);
717 cv_destroy(&spa->spa_async_cv);
718 cv_destroy(&spa->spa_evicting_os_cv);
719 cv_destroy(&spa->spa_proc_cv);
720 cv_destroy(&spa->spa_scrub_io_cv);
721 cv_destroy(&spa->spa_suspend_cv);
723 mutex_destroy(&spa->spa_alloc_lock);
724 mutex_destroy(&spa->spa_async_lock);
725 mutex_destroy(&spa->spa_errlist_lock);
726 mutex_destroy(&spa->spa_errlog_lock);
727 mutex_destroy(&spa->spa_evicting_os_lock);
728 mutex_destroy(&spa->spa_history_lock);
729 mutex_destroy(&spa->spa_proc_lock);
730 mutex_destroy(&spa->spa_props_lock);
731 mutex_destroy(&spa->spa_cksum_tmpls_lock);
732 mutex_destroy(&spa->spa_scrub_lock);
733 mutex_destroy(&spa->spa_suspend_lock);
734 mutex_destroy(&spa->spa_vdev_top_lock);
735 mutex_destroy(&spa->spa_feat_stats_lock);
737 kmem_free(spa, sizeof (spa_t));
741 * Given a pool, return the next pool in the namespace, or NULL if there is
742 * none. If 'prev' is NULL, return the first pool.
745 spa_next(spa_t *prev)
747 ASSERT(MUTEX_HELD(&spa_namespace_lock));
750 return (AVL_NEXT(&spa_namespace_avl, prev));
752 return (avl_first(&spa_namespace_avl));
756 * ==========================================================================
757 * SPA refcount functions
758 * ==========================================================================
762 * Add a reference to the given spa_t. Must have at least one reference, or
763 * have the namespace lock held.
766 spa_open_ref(spa_t *spa, void *tag)
768 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
769 MUTEX_HELD(&spa_namespace_lock));
770 (void) refcount_add(&spa->spa_refcount, tag);
774 * Remove a reference to the given spa_t. Must have at least one reference, or
775 * have the namespace lock held.
778 spa_close(spa_t *spa, void *tag)
780 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
781 MUTEX_HELD(&spa_namespace_lock));
782 (void) refcount_remove(&spa->spa_refcount, tag);
786 * Remove a reference to the given spa_t held by a dsl dir that is
787 * being asynchronously released. Async releases occur from a taskq
788 * performing eviction of dsl datasets and dirs. The namespace lock
789 * isn't held and the hold by the object being evicted may contribute to
790 * spa_minref (e.g. dataset or directory released during pool export),
791 * so the asserts in spa_close() do not apply.
794 spa_async_close(spa_t *spa, void *tag)
796 (void) refcount_remove(&spa->spa_refcount, tag);
800 * Check to see if the spa refcount is zero. Must be called with
801 * spa_namespace_lock held. We really compare against spa_minref, which is the
802 * number of references acquired when opening a pool
805 spa_refcount_zero(spa_t *spa)
807 ASSERT(MUTEX_HELD(&spa_namespace_lock));
809 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
813 * ==========================================================================
814 * SPA spare and l2cache tracking
815 * ==========================================================================
819 * Hot spares and cache devices are tracked using the same code below,
820 * for 'auxiliary' devices.
823 typedef struct spa_aux {
831 spa_aux_compare(const void *a, const void *b)
833 const spa_aux_t *sa = (const spa_aux_t *)a;
834 const spa_aux_t *sb = (const spa_aux_t *)b;
836 return (AVL_CMP(sa->aux_guid, sb->aux_guid));
840 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
846 search.aux_guid = vd->vdev_guid;
847 if ((aux = avl_find(avl, &search, &where)) != NULL) {
850 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
851 aux->aux_guid = vd->vdev_guid;
853 avl_insert(avl, aux, where);
858 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
864 search.aux_guid = vd->vdev_guid;
865 aux = avl_find(avl, &search, &where);
869 if (--aux->aux_count == 0) {
870 avl_remove(avl, aux);
871 kmem_free(aux, sizeof (spa_aux_t));
872 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
873 aux->aux_pool = 0ULL;
878 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
880 spa_aux_t search, *found;
882 search.aux_guid = guid;
883 found = avl_find(avl, &search, NULL);
887 *pool = found->aux_pool;
894 *refcnt = found->aux_count;
899 return (found != NULL);
903 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
905 spa_aux_t search, *found;
908 search.aux_guid = vd->vdev_guid;
909 found = avl_find(avl, &search, &where);
910 ASSERT(found != NULL);
911 ASSERT(found->aux_pool == 0ULL);
913 found->aux_pool = spa_guid(vd->vdev_spa);
917 * Spares are tracked globally due to the following constraints:
919 * - A spare may be part of multiple pools.
920 * - A spare may be added to a pool even if it's actively in use within
922 * - A spare in use in any pool can only be the source of a replacement if
923 * the target is a spare in the same pool.
925 * We keep track of all spares on the system through the use of a reference
926 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
927 * spare, then we bump the reference count in the AVL tree. In addition, we set
928 * the 'vdev_isspare' member to indicate that the device is a spare (active or
929 * inactive). When a spare is made active (used to replace a device in the
930 * pool), we also keep track of which pool its been made a part of.
932 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
933 * called under the spa_namespace lock as part of vdev reconfiguration. The
934 * separate spare lock exists for the status query path, which does not need to
935 * be completely consistent with respect to other vdev configuration changes.
939 spa_spare_compare(const void *a, const void *b)
941 return (spa_aux_compare(a, b));
945 spa_spare_add(vdev_t *vd)
947 mutex_enter(&spa_spare_lock);
948 ASSERT(!vd->vdev_isspare);
949 spa_aux_add(vd, &spa_spare_avl);
950 vd->vdev_isspare = B_TRUE;
951 mutex_exit(&spa_spare_lock);
955 spa_spare_remove(vdev_t *vd)
957 mutex_enter(&spa_spare_lock);
958 ASSERT(vd->vdev_isspare);
959 spa_aux_remove(vd, &spa_spare_avl);
960 vd->vdev_isspare = B_FALSE;
961 mutex_exit(&spa_spare_lock);
965 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
969 mutex_enter(&spa_spare_lock);
970 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
971 mutex_exit(&spa_spare_lock);
977 spa_spare_activate(vdev_t *vd)
979 mutex_enter(&spa_spare_lock);
980 ASSERT(vd->vdev_isspare);
981 spa_aux_activate(vd, &spa_spare_avl);
982 mutex_exit(&spa_spare_lock);
986 * Level 2 ARC devices are tracked globally for the same reasons as spares.
987 * Cache devices currently only support one pool per cache device, and so
988 * for these devices the aux reference count is currently unused beyond 1.
992 spa_l2cache_compare(const void *a, const void *b)
994 return (spa_aux_compare(a, b));
998 spa_l2cache_add(vdev_t *vd)
1000 mutex_enter(&spa_l2cache_lock);
1001 ASSERT(!vd->vdev_isl2cache);
1002 spa_aux_add(vd, &spa_l2cache_avl);
1003 vd->vdev_isl2cache = B_TRUE;
1004 mutex_exit(&spa_l2cache_lock);
1008 spa_l2cache_remove(vdev_t *vd)
1010 mutex_enter(&spa_l2cache_lock);
1011 ASSERT(vd->vdev_isl2cache);
1012 spa_aux_remove(vd, &spa_l2cache_avl);
1013 vd->vdev_isl2cache = B_FALSE;
1014 mutex_exit(&spa_l2cache_lock);
1018 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1022 mutex_enter(&spa_l2cache_lock);
1023 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1024 mutex_exit(&spa_l2cache_lock);
1030 spa_l2cache_activate(vdev_t *vd)
1032 mutex_enter(&spa_l2cache_lock);
1033 ASSERT(vd->vdev_isl2cache);
1034 spa_aux_activate(vd, &spa_l2cache_avl);
1035 mutex_exit(&spa_l2cache_lock);
1039 * ==========================================================================
1041 * ==========================================================================
1045 * Lock the given spa_t for the purpose of adding or removing a vdev.
1046 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1047 * It returns the next transaction group for the spa_t.
1050 spa_vdev_enter(spa_t *spa)
1052 mutex_enter(&spa->spa_vdev_top_lock);
1053 mutex_enter(&spa_namespace_lock);
1054 return (spa_vdev_config_enter(spa));
1058 * Internal implementation for spa_vdev_enter(). Used when a vdev
1059 * operation requires multiple syncs (i.e. removing a device) while
1060 * keeping the spa_namespace_lock held.
1063 spa_vdev_config_enter(spa_t *spa)
1065 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1067 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1069 return (spa_last_synced_txg(spa) + 1);
1073 * Used in combination with spa_vdev_config_enter() to allow the syncing
1074 * of multiple transactions without releasing the spa_namespace_lock.
1077 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1079 int config_changed = B_FALSE;
1081 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1082 ASSERT(txg > spa_last_synced_txg(spa));
1084 spa->spa_pending_vdev = NULL;
1087 * Reassess the DTLs.
1089 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1091 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1092 config_changed = B_TRUE;
1093 spa->spa_config_generation++;
1097 * Verify the metaslab classes.
1099 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1100 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1102 spa_config_exit(spa, SCL_ALL, spa);
1105 * Panic the system if the specified tag requires it. This
1106 * is useful for ensuring that configurations are updated
1109 if (zio_injection_enabled)
1110 zio_handle_panic_injection(spa, tag, 0);
1113 * Note: this txg_wait_synced() is important because it ensures
1114 * that there won't be more than one config change per txg.
1115 * This allows us to use the txg as the generation number.
1118 txg_wait_synced(spa->spa_dsl_pool, txg);
1121 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1122 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1124 spa_config_exit(spa, SCL_ALL, spa);
1128 * If the config changed, update the config cache.
1131 spa_config_sync(spa, B_FALSE, B_TRUE);
1135 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1136 * locking of spa_vdev_enter(), we also want make sure the transactions have
1137 * synced to disk, and then update the global configuration cache with the new
1141 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1143 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1144 mutex_exit(&spa_namespace_lock);
1145 mutex_exit(&spa->spa_vdev_top_lock);
1151 * Lock the given spa_t for the purpose of changing vdev state.
1154 spa_vdev_state_enter(spa_t *spa, int oplocks)
1156 int locks = SCL_STATE_ALL | oplocks;
1159 * Root pools may need to read of the underlying devfs filesystem
1160 * when opening up a vdev. Unfortunately if we're holding the
1161 * SCL_ZIO lock it will result in a deadlock when we try to issue
1162 * the read from the root filesystem. Instead we "prefetch"
1163 * the associated vnodes that we need prior to opening the
1164 * underlying devices and cache them so that we can prevent
1165 * any I/O when we are doing the actual open.
1167 if (spa_is_root(spa)) {
1168 int low = locks & ~(SCL_ZIO - 1);
1169 int high = locks & ~low;
1171 spa_config_enter(spa, high, spa, RW_WRITER);
1172 vdev_hold(spa->spa_root_vdev);
1173 spa_config_enter(spa, low, spa, RW_WRITER);
1175 spa_config_enter(spa, locks, spa, RW_WRITER);
1177 spa->spa_vdev_locks = locks;
1181 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1183 boolean_t config_changed = B_FALSE;
1185 if (vd != NULL || error == 0)
1186 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1190 vdev_state_dirty(vd->vdev_top);
1191 config_changed = B_TRUE;
1192 spa->spa_config_generation++;
1195 if (spa_is_root(spa))
1196 vdev_rele(spa->spa_root_vdev);
1198 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1199 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1202 * If anything changed, wait for it to sync. This ensures that,
1203 * from the system administrator's perspective, zpool(1M) commands
1204 * are synchronous. This is important for things like zpool offline:
1205 * when the command completes, you expect no further I/O from ZFS.
1208 txg_wait_synced(spa->spa_dsl_pool, 0);
1211 * If the config changed, update the config cache.
1213 if (config_changed) {
1214 mutex_enter(&spa_namespace_lock);
1215 spa_config_sync(spa, B_FALSE, B_TRUE);
1216 mutex_exit(&spa_namespace_lock);
1223 * ==========================================================================
1224 * Miscellaneous functions
1225 * ==========================================================================
1229 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1231 if (!nvlist_exists(spa->spa_label_features, feature)) {
1232 fnvlist_add_boolean(spa->spa_label_features, feature);
1234 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1235 * dirty the vdev config because lock SCL_CONFIG is not held.
1236 * Thankfully, in this case we don't need to dirty the config
1237 * because it will be written out anyway when we finish
1238 * creating the pool.
1240 if (tx->tx_txg != TXG_INITIAL)
1241 vdev_config_dirty(spa->spa_root_vdev);
1246 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1248 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1249 vdev_config_dirty(spa->spa_root_vdev);
1256 spa_rename(const char *name, const char *newname)
1262 * Lookup the spa_t and grab the config lock for writing. We need to
1263 * actually open the pool so that we can sync out the necessary labels.
1264 * It's OK to call spa_open() with the namespace lock held because we
1265 * allow recursive calls for other reasons.
1267 mutex_enter(&spa_namespace_lock);
1268 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1269 mutex_exit(&spa_namespace_lock);
1273 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1275 avl_remove(&spa_namespace_avl, spa);
1276 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1277 avl_add(&spa_namespace_avl, spa);
1280 * Sync all labels to disk with the new names by marking the root vdev
1281 * dirty and waiting for it to sync. It will pick up the new pool name
1284 vdev_config_dirty(spa->spa_root_vdev);
1286 spa_config_exit(spa, SCL_ALL, FTAG);
1288 txg_wait_synced(spa->spa_dsl_pool, 0);
1291 * Sync the updated config cache.
1293 spa_config_sync(spa, B_FALSE, B_TRUE);
1295 spa_close(spa, FTAG);
1297 mutex_exit(&spa_namespace_lock);
1303 * Return the spa_t associated with given pool_guid, if it exists. If
1304 * device_guid is non-zero, determine whether the pool exists *and* contains
1305 * a device with the specified device_guid.
1308 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1311 avl_tree_t *t = &spa_namespace_avl;
1313 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1315 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1316 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1318 if (spa->spa_root_vdev == NULL)
1320 if (spa_guid(spa) == pool_guid) {
1321 if (device_guid == 0)
1324 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1325 device_guid) != NULL)
1329 * Check any devices we may be in the process of adding.
1331 if (spa->spa_pending_vdev) {
1332 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1333 device_guid) != NULL)
1343 * Determine whether a pool with the given pool_guid exists.
1346 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1348 return (spa_by_guid(pool_guid, device_guid) != NULL);
1352 spa_strdup(const char *s)
1358 new = kmem_alloc(len + 1, KM_SLEEP);
1366 spa_strfree(char *s)
1368 kmem_free(s, strlen(s) + 1);
1372 spa_get_random(uint64_t range)
1378 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1384 spa_generate_guid(spa_t *spa)
1386 uint64_t guid = spa_get_random(-1ULL);
1389 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1390 guid = spa_get_random(-1ULL);
1392 while (guid == 0 || spa_guid_exists(guid, 0))
1393 guid = spa_get_random(-1ULL);
1400 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1403 char *checksum = NULL;
1404 char *compress = NULL;
1407 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1408 dmu_object_byteswap_t bswap =
1409 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1410 (void) snprintf(type, sizeof (type), "bswap %s %s",
1411 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1412 "metadata" : "data",
1413 dmu_ot_byteswap[bswap].ob_name);
1415 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1418 if (!BP_IS_EMBEDDED(bp)) {
1420 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1422 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1425 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1430 spa_freeze(spa_t *spa)
1432 uint64_t freeze_txg = 0;
1434 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1435 if (spa->spa_freeze_txg == UINT64_MAX) {
1436 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1437 spa->spa_freeze_txg = freeze_txg;
1439 spa_config_exit(spa, SCL_ALL, FTAG);
1440 if (freeze_txg != 0)
1441 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1445 zfs_panic_recover(const char *fmt, ...)
1450 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1455 * This is a stripped-down version of strtoull, suitable only for converting
1456 * lowercase hexadecimal numbers that don't overflow.
1459 strtonum(const char *str, char **nptr)
1465 while ((c = *str) != '\0') {
1466 if (c >= '0' && c <= '9')
1468 else if (c >= 'a' && c <= 'f')
1469 digit = 10 + c - 'a';
1480 *nptr = (char *)str;
1486 * ==========================================================================
1487 * Accessor functions
1488 * ==========================================================================
1492 spa_shutting_down(spa_t *spa)
1494 return (spa->spa_async_suspended);
1498 spa_get_dsl(spa_t *spa)
1500 return (spa->spa_dsl_pool);
1504 spa_is_initializing(spa_t *spa)
1506 return (spa->spa_is_initializing);
1510 spa_get_rootblkptr(spa_t *spa)
1512 return (&spa->spa_ubsync.ub_rootbp);
1516 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1518 spa->spa_uberblock.ub_rootbp = *bp;
1522 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1524 if (spa->spa_root == NULL)
1527 (void) strncpy(buf, spa->spa_root, buflen);
1531 spa_sync_pass(spa_t *spa)
1533 return (spa->spa_sync_pass);
1537 spa_name(spa_t *spa)
1539 return (spa->spa_name);
1543 spa_guid(spa_t *spa)
1545 dsl_pool_t *dp = spa_get_dsl(spa);
1549 * If we fail to parse the config during spa_load(), we can go through
1550 * the error path (which posts an ereport) and end up here with no root
1551 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1554 if (spa->spa_root_vdev == NULL)
1555 return (spa->spa_config_guid);
1557 guid = spa->spa_last_synced_guid != 0 ?
1558 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1561 * Return the most recently synced out guid unless we're
1562 * in syncing context.
1564 if (dp && dsl_pool_sync_context(dp))
1565 return (spa->spa_root_vdev->vdev_guid);
1571 spa_load_guid(spa_t *spa)
1574 * This is a GUID that exists solely as a reference for the
1575 * purposes of the arc. It is generated at load time, and
1576 * is never written to persistent storage.
1578 return (spa->spa_load_guid);
1582 spa_last_synced_txg(spa_t *spa)
1584 return (spa->spa_ubsync.ub_txg);
1588 spa_first_txg(spa_t *spa)
1590 return (spa->spa_first_txg);
1594 spa_syncing_txg(spa_t *spa)
1596 return (spa->spa_syncing_txg);
1600 spa_state(spa_t *spa)
1602 return (spa->spa_state);
1606 spa_load_state(spa_t *spa)
1608 return (spa->spa_load_state);
1612 spa_freeze_txg(spa_t *spa)
1614 return (spa->spa_freeze_txg);
1619 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize)
1621 return (lsize * spa_asize_inflation);
1625 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1626 * or at least 128MB, unless that would cause it to be more than half the
1629 * See the comment above spa_slop_shift for details.
1632 spa_get_slop_space(spa_t *spa)
1634 uint64_t space = spa_get_dspace(spa);
1635 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop)));
1639 spa_get_dspace(spa_t *spa)
1641 return (spa->spa_dspace);
1645 spa_update_dspace(spa_t *spa)
1647 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1648 ddt_get_dedup_dspace(spa);
1652 * Return the failure mode that has been set to this pool. The default
1653 * behavior will be to block all I/Os when a complete failure occurs.
1656 spa_get_failmode(spa_t *spa)
1658 return (spa->spa_failmode);
1662 spa_suspended(spa_t *spa)
1664 return (spa->spa_suspended);
1668 spa_version(spa_t *spa)
1670 return (spa->spa_ubsync.ub_version);
1674 spa_deflate(spa_t *spa)
1676 return (spa->spa_deflate);
1680 spa_normal_class(spa_t *spa)
1682 return (spa->spa_normal_class);
1686 spa_log_class(spa_t *spa)
1688 return (spa->spa_log_class);
1692 spa_evicting_os_register(spa_t *spa, objset_t *os)
1694 mutex_enter(&spa->spa_evicting_os_lock);
1695 list_insert_head(&spa->spa_evicting_os_list, os);
1696 mutex_exit(&spa->spa_evicting_os_lock);
1700 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1702 mutex_enter(&spa->spa_evicting_os_lock);
1703 list_remove(&spa->spa_evicting_os_list, os);
1704 cv_broadcast(&spa->spa_evicting_os_cv);
1705 mutex_exit(&spa->spa_evicting_os_lock);
1709 spa_evicting_os_wait(spa_t *spa)
1711 mutex_enter(&spa->spa_evicting_os_lock);
1712 while (!list_is_empty(&spa->spa_evicting_os_list))
1713 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1714 mutex_exit(&spa->spa_evicting_os_lock);
1716 dmu_buf_user_evict_wait();
1720 spa_max_replication(spa_t *spa)
1723 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1724 * handle BPs with more than one DVA allocated. Set our max
1725 * replication level accordingly.
1727 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1729 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1733 spa_prev_software_version(spa_t *spa)
1735 return (spa->spa_prev_software_version);
1739 spa_deadman_synctime(spa_t *spa)
1741 return (spa->spa_deadman_synctime);
1745 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1747 uint64_t asize = DVA_GET_ASIZE(dva);
1748 uint64_t dsize = asize;
1750 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1752 if (asize != 0 && spa->spa_deflate) {
1753 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1755 dsize = (asize >> SPA_MINBLOCKSHIFT) *
1756 vd->vdev_deflate_ratio;
1763 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1768 for (d = 0; d < BP_GET_NDVAS(bp); d++)
1769 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1775 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1780 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1782 for (d = 0; d < BP_GET_NDVAS(bp); d++)
1783 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1785 spa_config_exit(spa, SCL_VDEV, FTAG);
1791 * ==========================================================================
1792 * Initialization and Termination
1793 * ==========================================================================
1797 spa_name_compare(const void *a1, const void *a2)
1799 const spa_t *s1 = a1;
1800 const spa_t *s2 = a2;
1803 s = strcmp(s1->spa_name, s2->spa_name);
1805 return (AVL_ISIGN(s));
1817 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1818 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1819 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1820 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1822 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1823 offsetof(spa_t, spa_avl));
1825 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1826 offsetof(spa_aux_t, aux_avl));
1828 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1829 offsetof(spa_aux_t, aux_avl));
1831 spa_mode_global = mode;
1834 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1835 struct sigaction sa;
1837 sa.sa_flags = SA_SIGINFO;
1838 sigemptyset(&sa.sa_mask);
1839 sa.sa_sigaction = arc_buf_sigsegv;
1841 if (sigaction(SIGSEGV, &sa, NULL) == -1) {
1842 perror("could not enable watchpoints: "
1843 "sigaction(SIGSEGV, ...) = ");
1854 metaslab_alloc_trace_init();
1859 vdev_cache_stat_init();
1860 vdev_raidz_math_init();
1864 zpool_feature_init();
1878 vdev_cache_stat_fini();
1879 vdev_raidz_math_fini();
1884 metaslab_alloc_trace_fini();
1891 avl_destroy(&spa_namespace_avl);
1892 avl_destroy(&spa_spare_avl);
1893 avl_destroy(&spa_l2cache_avl);
1895 cv_destroy(&spa_namespace_cv);
1896 mutex_destroy(&spa_namespace_lock);
1897 mutex_destroy(&spa_spare_lock);
1898 mutex_destroy(&spa_l2cache_lock);
1902 * Return whether this pool has slogs. No locking needed.
1903 * It's not a problem if the wrong answer is returned as it's only for
1904 * performance and not correctness
1907 spa_has_slogs(spa_t *spa)
1909 return (spa->spa_log_class->mc_rotor != NULL);
1913 spa_get_log_state(spa_t *spa)
1915 return (spa->spa_log_state);
1919 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1921 spa->spa_log_state = state;
1925 spa_is_root(spa_t *spa)
1927 return (spa->spa_is_root);
1931 spa_writeable(spa_t *spa)
1933 return (!!(spa->spa_mode & FWRITE));
1937 * Returns true if there is a pending sync task in any of the current
1938 * syncing txg, the current quiescing txg, or the current open txg.
1941 spa_has_pending_synctask(spa_t *spa)
1943 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
1947 spa_mode(spa_t *spa)
1949 return (spa->spa_mode);
1953 spa_bootfs(spa_t *spa)
1955 return (spa->spa_bootfs);
1959 spa_delegation(spa_t *spa)
1961 return (spa->spa_delegation);
1965 spa_meta_objset(spa_t *spa)
1967 return (spa->spa_meta_objset);
1971 spa_dedup_checksum(spa_t *spa)
1973 return (spa->spa_dedup_checksum);
1977 * Reset pool scan stat per scan pass (or reboot).
1980 spa_scan_stat_init(spa_t *spa)
1982 /* data not stored on disk */
1983 spa->spa_scan_pass_start = gethrestime_sec();
1984 spa->spa_scan_pass_exam = 0;
1985 vdev_scan_stat_init(spa->spa_root_vdev);
1989 * Get scan stats for zpool status reports
1992 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1994 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1996 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1997 return (SET_ERROR(ENOENT));
1998 bzero(ps, sizeof (pool_scan_stat_t));
2000 /* data stored on disk */
2001 ps->pss_func = scn->scn_phys.scn_func;
2002 ps->pss_start_time = scn->scn_phys.scn_start_time;
2003 ps->pss_end_time = scn->scn_phys.scn_end_time;
2004 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2005 ps->pss_examined = scn->scn_phys.scn_examined;
2006 ps->pss_to_process = scn->scn_phys.scn_to_process;
2007 ps->pss_processed = scn->scn_phys.scn_processed;
2008 ps->pss_errors = scn->scn_phys.scn_errors;
2009 ps->pss_state = scn->scn_phys.scn_state;
2011 /* data not stored on disk */
2012 ps->pss_pass_start = spa->spa_scan_pass_start;
2013 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2019 spa_debug_enabled(spa_t *spa)
2021 return (spa->spa_debug);
2025 spa_maxblocksize(spa_t *spa)
2027 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2028 return (SPA_MAXBLOCKSIZE);
2030 return (SPA_OLD_MAXBLOCKSIZE);
2034 spa_maxdnodesize(spa_t *spa)
2036 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
2037 return (DNODE_MAX_SIZE);
2039 return (DNODE_MIN_SIZE);
2042 #if defined(_KERNEL) && defined(HAVE_SPL)
2043 /* Namespace manipulation */
2044 EXPORT_SYMBOL(spa_lookup);
2045 EXPORT_SYMBOL(spa_add);
2046 EXPORT_SYMBOL(spa_remove);
2047 EXPORT_SYMBOL(spa_next);
2049 /* Refcount functions */
2050 EXPORT_SYMBOL(spa_open_ref);
2051 EXPORT_SYMBOL(spa_close);
2052 EXPORT_SYMBOL(spa_refcount_zero);
2054 /* Pool configuration lock */
2055 EXPORT_SYMBOL(spa_config_tryenter);
2056 EXPORT_SYMBOL(spa_config_enter);
2057 EXPORT_SYMBOL(spa_config_exit);
2058 EXPORT_SYMBOL(spa_config_held);
2060 /* Pool vdev add/remove lock */
2061 EXPORT_SYMBOL(spa_vdev_enter);
2062 EXPORT_SYMBOL(spa_vdev_exit);
2064 /* Pool vdev state change lock */
2065 EXPORT_SYMBOL(spa_vdev_state_enter);
2066 EXPORT_SYMBOL(spa_vdev_state_exit);
2068 /* Accessor functions */
2069 EXPORT_SYMBOL(spa_shutting_down);
2070 EXPORT_SYMBOL(spa_get_dsl);
2071 EXPORT_SYMBOL(spa_get_rootblkptr);
2072 EXPORT_SYMBOL(spa_set_rootblkptr);
2073 EXPORT_SYMBOL(spa_altroot);
2074 EXPORT_SYMBOL(spa_sync_pass);
2075 EXPORT_SYMBOL(spa_name);
2076 EXPORT_SYMBOL(spa_guid);
2077 EXPORT_SYMBOL(spa_last_synced_txg);
2078 EXPORT_SYMBOL(spa_first_txg);
2079 EXPORT_SYMBOL(spa_syncing_txg);
2080 EXPORT_SYMBOL(spa_version);
2081 EXPORT_SYMBOL(spa_state);
2082 EXPORT_SYMBOL(spa_load_state);
2083 EXPORT_SYMBOL(spa_freeze_txg);
2084 EXPORT_SYMBOL(spa_get_dspace);
2085 EXPORT_SYMBOL(spa_update_dspace);
2086 EXPORT_SYMBOL(spa_deflate);
2087 EXPORT_SYMBOL(spa_normal_class);
2088 EXPORT_SYMBOL(spa_log_class);
2089 EXPORT_SYMBOL(spa_max_replication);
2090 EXPORT_SYMBOL(spa_prev_software_version);
2091 EXPORT_SYMBOL(spa_get_failmode);
2092 EXPORT_SYMBOL(spa_suspended);
2093 EXPORT_SYMBOL(spa_bootfs);
2094 EXPORT_SYMBOL(spa_delegation);
2095 EXPORT_SYMBOL(spa_meta_objset);
2096 EXPORT_SYMBOL(spa_maxblocksize);
2097 EXPORT_SYMBOL(spa_maxdnodesize);
2099 /* Miscellaneous support routines */
2100 EXPORT_SYMBOL(spa_rename);
2101 EXPORT_SYMBOL(spa_guid_exists);
2102 EXPORT_SYMBOL(spa_strdup);
2103 EXPORT_SYMBOL(spa_strfree);
2104 EXPORT_SYMBOL(spa_get_random);
2105 EXPORT_SYMBOL(spa_generate_guid);
2106 EXPORT_SYMBOL(snprintf_blkptr);
2107 EXPORT_SYMBOL(spa_freeze);
2108 EXPORT_SYMBOL(spa_upgrade);
2109 EXPORT_SYMBOL(spa_evict_all);
2110 EXPORT_SYMBOL(spa_lookup_by_guid);
2111 EXPORT_SYMBOL(spa_has_spare);
2112 EXPORT_SYMBOL(dva_get_dsize_sync);
2113 EXPORT_SYMBOL(bp_get_dsize_sync);
2114 EXPORT_SYMBOL(bp_get_dsize);
2115 EXPORT_SYMBOL(spa_has_slogs);
2116 EXPORT_SYMBOL(spa_is_root);
2117 EXPORT_SYMBOL(spa_writeable);
2118 EXPORT_SYMBOL(spa_mode);
2119 EXPORT_SYMBOL(spa_namespace_lock);
2122 module_param(zfs_flags, uint, 0644);
2123 MODULE_PARM_DESC(zfs_flags, "Set additional debugging flags");
2125 module_param(zfs_recover, int, 0644);
2126 MODULE_PARM_DESC(zfs_recover, "Set to attempt to recover from fatal errors");
2128 module_param(zfs_free_leak_on_eio, int, 0644);
2129 MODULE_PARM_DESC(zfs_free_leak_on_eio,
2130 "Set to ignore IO errors during free and permanently leak the space");
2132 module_param(zfs_deadman_synctime_ms, ulong, 0644);
2133 MODULE_PARM_DESC(zfs_deadman_synctime_ms, "Expiration time in milliseconds");
2135 module_param(zfs_deadman_checktime_ms, ulong, 0644);
2136 MODULE_PARM_DESC(zfs_deadman_checktime_ms,
2137 "Dead I/O check interval in milliseconds");
2139 module_param(zfs_deadman_enabled, int, 0644);
2140 MODULE_PARM_DESC(zfs_deadman_enabled, "Enable deadman timer");
2142 module_param(spa_asize_inflation, int, 0644);
2143 MODULE_PARM_DESC(spa_asize_inflation,
2144 "SPA size estimate multiplication factor");
2146 module_param(spa_slop_shift, int, 0644);
2147 MODULE_PARM_DESC(spa_slop_shift, "Reserved free space in pool");