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) 2013 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
28 #include <sys/zfs_context.h>
29 #include <sys/spa_impl.h>
30 #include <sys/spa_boot.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/metaslab.h>
40 #include <sys/uberblock_impl.h>
43 #include <sys/unique.h>
44 #include <sys/dsl_pool.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/dsl_prop.h>
47 #include <sys/dsl_scan.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/metaslab_impl.h>
53 #include "zfeature_common.h"
58 * There are four basic locks for managing spa_t structures:
60 * spa_namespace_lock (global mutex)
62 * This lock must be acquired to do any of the following:
64 * - Lookup a spa_t by name
65 * - Add or remove a spa_t from the namespace
66 * - Increase spa_refcount from non-zero
67 * - Check if spa_refcount is zero
69 * - add/remove/attach/detach devices
70 * - Held for the duration of create/destroy/import/export
72 * It does not need to handle recursion. A create or destroy may
73 * reference objects (files or zvols) in other pools, but by
74 * definition they must have an existing reference, and will never need
75 * to lookup a spa_t by name.
77 * spa_refcount (per-spa refcount_t protected by mutex)
79 * This reference count keep track of any active users of the spa_t. The
80 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
81 * the refcount is never really 'zero' - opening a pool implicitly keeps
82 * some references in the DMU. Internally we check against spa_minref, but
83 * present the image of a zero/non-zero value to consumers.
85 * spa_config_lock[] (per-spa array of rwlocks)
87 * This protects the spa_t from config changes, and must be held in
88 * the following circumstances:
90 * - RW_READER to perform I/O to the spa
91 * - RW_WRITER to change the vdev config
93 * The locking order is fairly straightforward:
95 * spa_namespace_lock -> spa_refcount
97 * The namespace lock must be acquired to increase the refcount from 0
98 * or to check if it is zero.
100 * spa_refcount -> spa_config_lock[]
102 * There must be at least one valid reference on the spa_t to acquire
105 * spa_namespace_lock -> spa_config_lock[]
107 * The namespace lock must always be taken before the config lock.
110 * The spa_namespace_lock can be acquired directly and is globally visible.
112 * The namespace is manipulated using the following functions, all of which
113 * require the spa_namespace_lock to be held.
115 * spa_lookup() Lookup a spa_t by name.
117 * spa_add() Create a new spa_t in the namespace.
119 * spa_remove() Remove a spa_t from the namespace. This also
120 * frees up any memory associated with the spa_t.
122 * spa_next() Returns the next spa_t in the system, or the
123 * first if NULL is passed.
125 * spa_evict_all() Shutdown and remove all spa_t structures in
128 * spa_guid_exists() Determine whether a pool/device guid exists.
130 * The spa_refcount is manipulated using the following functions:
132 * spa_open_ref() Adds a reference to the given spa_t. Must be
133 * called with spa_namespace_lock held if the
134 * refcount is currently zero.
136 * spa_close() Remove a reference from the spa_t. This will
137 * not free the spa_t or remove it from the
138 * namespace. No locking is required.
140 * spa_refcount_zero() Returns true if the refcount is currently
141 * zero. Must be called with spa_namespace_lock
144 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
145 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
146 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
148 * To read the configuration, it suffices to hold one of these locks as reader.
149 * To modify the configuration, you must hold all locks as writer. To modify
150 * vdev state without altering the vdev tree's topology (e.g. online/offline),
151 * you must hold SCL_STATE and SCL_ZIO as writer.
153 * We use these distinct config locks to avoid recursive lock entry.
154 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
155 * block allocations (SCL_ALLOC), which may require reading space maps
156 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
158 * The spa config locks cannot be normal rwlocks because we need the
159 * ability to hand off ownership. For example, SCL_ZIO is acquired
160 * by the issuing thread and later released by an interrupt thread.
161 * They do, however, obey the usual write-wanted semantics to prevent
162 * writer (i.e. system administrator) starvation.
164 * The lock acquisition rules are as follows:
167 * Protects changes to the vdev tree topology, such as vdev
168 * add/remove/attach/detach. Protects the dirty config list
169 * (spa_config_dirty_list) and the set of spares and l2arc devices.
172 * Protects changes to pool state and vdev state, such as vdev
173 * online/offline/fault/degrade/clear. Protects the dirty state list
174 * (spa_state_dirty_list) and global pool state (spa_state).
177 * Protects changes to metaslab groups and classes.
178 * Held as reader by metaslab_alloc() and metaslab_claim().
181 * Held by bp-level zios (those which have no io_vd upon entry)
182 * to prevent changes to the vdev tree. The bp-level zio implicitly
183 * protects all of its vdev child zios, which do not hold SCL_ZIO.
186 * Protects changes to metaslab groups and classes.
187 * Held as reader by metaslab_free(). SCL_FREE is distinct from
188 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
189 * blocks in zio_done() while another i/o that holds either
190 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
193 * Held as reader to prevent changes to the vdev tree during trivial
194 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
195 * other locks, and lower than all of them, to ensure that it's safe
196 * to acquire regardless of caller context.
198 * In addition, the following rules apply:
200 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
201 * The lock ordering is SCL_CONFIG > spa_props_lock.
203 * (b) I/O operations on leaf vdevs. For any zio operation that takes
204 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
205 * or zio_write_phys() -- the caller must ensure that the config cannot
206 * cannot change in the interim, and that the vdev cannot be reopened.
207 * SCL_STATE as reader suffices for both.
209 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
211 * spa_vdev_enter() Acquire the namespace lock and the config lock
214 * spa_vdev_exit() Release the config lock, wait for all I/O
215 * to complete, sync the updated configs to the
216 * cache, and release the namespace lock.
218 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
219 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
220 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
222 * spa_rename() is also implemented within this file since it requires
223 * manipulation of the namespace.
226 static avl_tree_t spa_namespace_avl;
227 kmutex_t spa_namespace_lock;
228 static kcondvar_t spa_namespace_cv;
229 static int spa_active_count;
230 int spa_max_replication_override = SPA_DVAS_PER_BP;
232 static kmutex_t spa_spare_lock;
233 static avl_tree_t spa_spare_avl;
234 static kmutex_t spa_l2cache_lock;
235 static avl_tree_t spa_l2cache_avl;
237 kmem_cache_t *spa_buffer_pool;
241 /* Everything except dprintf and spa is on by default in debug builds */
242 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
247 TUNABLE_INT("debug.zfs_flags", &zfs_flags);
248 SYSCTL_INT(_debug, OID_AUTO, zfs_flags, CTLFLAG_RWTUN, &zfs_flags, 0,
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.
257 SYSCTL_DECL(_vfs_zfs);
258 TUNABLE_INT("vfs.zfs.recover", &zfs_recover);
259 SYSCTL_INT(_vfs_zfs, OID_AUTO, recover, CTLFLAG_RDTUN, &zfs_recover, 0,
260 "Try to recover from otherwise-fatal errors.");
263 * Expiration time in milliseconds. This value has two meanings. First it is
264 * used to determine when the spa_deadman() logic should fire. By default the
265 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
266 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
267 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
270 uint64_t zfs_deadman_synctime_ms = 1000000ULL;
271 TUNABLE_QUAD("vfs.zfs.deadman_synctime_ms", &zfs_deadman_synctime_ms);
272 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_synctime_ms, CTLFLAG_RDTUN,
273 &zfs_deadman_synctime_ms, 0,
274 "Stalled ZFS I/O expiration time in milliseconds");
277 * Check time in milliseconds. This defines the frequency at which we check
280 uint64_t zfs_deadman_checktime_ms = 5000ULL;
281 TUNABLE_QUAD("vfs.zfs.deadman_checktime_ms", &zfs_deadman_checktime_ms);
282 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_checktime_ms, CTLFLAG_RDTUN,
283 &zfs_deadman_checktime_ms, 0,
284 "Period of checks for stalled ZFS I/O in milliseconds");
287 * Default value of -1 for zfs_deadman_enabled is resolved in
290 int zfs_deadman_enabled = -1;
291 TUNABLE_INT("vfs.zfs.deadman_enabled", &zfs_deadman_enabled);
292 SYSCTL_INT(_vfs_zfs, OID_AUTO, deadman_enabled, CTLFLAG_RDTUN,
293 &zfs_deadman_enabled, 0, "Kernel panic on stalled ZFS I/O");
296 * The worst case is single-sector max-parity RAID-Z blocks, in which
297 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
298 * times the size; so just assume that. Add to this the fact that
299 * we can have up to 3 DVAs per bp, and one more factor of 2 because
300 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
302 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
304 int spa_asize_inflation = 24;
312 * If we are not i386 or amd64 or in a virtual machine,
313 * disable ZFS deadman thread by default
315 if (zfs_deadman_enabled == -1) {
316 #if defined(__amd64__) || defined(__i386__)
317 zfs_deadman_enabled = (vm_guest == VM_GUEST_NO) ? 1 : 0;
319 zfs_deadman_enabled = 0;
324 #endif /* !illumos */
327 * ==========================================================================
329 * ==========================================================================
332 spa_config_lock_init(spa_t *spa)
334 for (int i = 0; i < SCL_LOCKS; i++) {
335 spa_config_lock_t *scl = &spa->spa_config_lock[i];
336 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
337 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
338 refcount_create_untracked(&scl->scl_count);
339 scl->scl_writer = NULL;
340 scl->scl_write_wanted = 0;
345 spa_config_lock_destroy(spa_t *spa)
347 for (int i = 0; i < SCL_LOCKS; i++) {
348 spa_config_lock_t *scl = &spa->spa_config_lock[i];
349 mutex_destroy(&scl->scl_lock);
350 cv_destroy(&scl->scl_cv);
351 refcount_destroy(&scl->scl_count);
352 ASSERT(scl->scl_writer == NULL);
353 ASSERT(scl->scl_write_wanted == 0);
358 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
360 for (int i = 0; i < SCL_LOCKS; i++) {
361 spa_config_lock_t *scl = &spa->spa_config_lock[i];
362 if (!(locks & (1 << i)))
364 mutex_enter(&scl->scl_lock);
365 if (rw == RW_READER) {
366 if (scl->scl_writer || scl->scl_write_wanted) {
367 mutex_exit(&scl->scl_lock);
368 spa_config_exit(spa, locks ^ (1 << i), tag);
372 ASSERT(scl->scl_writer != curthread);
373 if (!refcount_is_zero(&scl->scl_count)) {
374 mutex_exit(&scl->scl_lock);
375 spa_config_exit(spa, locks ^ (1 << i), tag);
378 scl->scl_writer = curthread;
380 (void) refcount_add(&scl->scl_count, tag);
381 mutex_exit(&scl->scl_lock);
387 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
391 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
393 for (int i = 0; i < SCL_LOCKS; i++) {
394 spa_config_lock_t *scl = &spa->spa_config_lock[i];
395 if (scl->scl_writer == curthread)
396 wlocks_held |= (1 << i);
397 if (!(locks & (1 << i)))
399 mutex_enter(&scl->scl_lock);
400 if (rw == RW_READER) {
401 while (scl->scl_writer || scl->scl_write_wanted) {
402 cv_wait(&scl->scl_cv, &scl->scl_lock);
405 ASSERT(scl->scl_writer != curthread);
406 while (!refcount_is_zero(&scl->scl_count)) {
407 scl->scl_write_wanted++;
408 cv_wait(&scl->scl_cv, &scl->scl_lock);
409 scl->scl_write_wanted--;
411 scl->scl_writer = curthread;
413 (void) refcount_add(&scl->scl_count, tag);
414 mutex_exit(&scl->scl_lock);
416 ASSERT(wlocks_held <= locks);
420 spa_config_exit(spa_t *spa, int locks, void *tag)
422 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
423 spa_config_lock_t *scl = &spa->spa_config_lock[i];
424 if (!(locks & (1 << i)))
426 mutex_enter(&scl->scl_lock);
427 ASSERT(!refcount_is_zero(&scl->scl_count));
428 if (refcount_remove(&scl->scl_count, tag) == 0) {
429 ASSERT(scl->scl_writer == NULL ||
430 scl->scl_writer == curthread);
431 scl->scl_writer = NULL; /* OK in either case */
432 cv_broadcast(&scl->scl_cv);
434 mutex_exit(&scl->scl_lock);
439 spa_config_held(spa_t *spa, int locks, krw_t rw)
443 for (int i = 0; i < SCL_LOCKS; i++) {
444 spa_config_lock_t *scl = &spa->spa_config_lock[i];
445 if (!(locks & (1 << i)))
447 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
448 (rw == RW_WRITER && scl->scl_writer == curthread))
449 locks_held |= 1 << i;
456 * ==========================================================================
457 * SPA namespace functions
458 * ==========================================================================
462 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
463 * Returns NULL if no matching spa_t is found.
466 spa_lookup(const char *name)
468 static spa_t search; /* spa_t is large; don't allocate on stack */
473 ASSERT(MUTEX_HELD(&spa_namespace_lock));
475 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
478 * If it's a full dataset name, figure out the pool name and
481 cp = strpbrk(search.spa_name, "/@");
485 spa = avl_find(&spa_namespace_avl, &search, &where);
491 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
492 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
493 * looking for potentially hung I/Os.
496 spa_deadman(void *arg)
500 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
501 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
502 ++spa->spa_deadman_calls);
503 if (zfs_deadman_enabled)
504 vdev_deadman(spa->spa_root_vdev);
508 * Create an uninitialized spa_t with the given name. Requires
509 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
510 * exist by calling spa_lookup() first.
513 spa_add(const char *name, nvlist_t *config, const char *altroot)
516 spa_config_dirent_t *dp;
522 ASSERT(MUTEX_HELD(&spa_namespace_lock));
524 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
526 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
527 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
528 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
529 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
530 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
531 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
532 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
533 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
534 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
536 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
537 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
538 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
539 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
541 for (int t = 0; t < TXG_SIZE; t++)
542 bplist_create(&spa->spa_free_bplist[t]);
544 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
545 spa->spa_state = POOL_STATE_UNINITIALIZED;
546 spa->spa_freeze_txg = UINT64_MAX;
547 spa->spa_final_txg = UINT64_MAX;
548 spa->spa_load_max_txg = UINT64_MAX;
550 spa->spa_proc_state = SPA_PROC_NONE;
553 hdlr.cyh_func = spa_deadman;
555 hdlr.cyh_level = CY_LOW_LEVEL;
558 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
562 * This determines how often we need to check for hung I/Os after
563 * the cyclic has already fired. Since checking for hung I/Os is
564 * an expensive operation we don't want to check too frequently.
565 * Instead wait for 5 seconds before checking again.
567 when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
568 when.cyt_when = CY_INFINITY;
569 mutex_enter(&cpu_lock);
570 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
571 mutex_exit(&cpu_lock);
574 callout_init(&spa->spa_deadman_cycid, CALLOUT_MPSAFE);
577 refcount_create(&spa->spa_refcount);
578 spa_config_lock_init(spa);
580 avl_add(&spa_namespace_avl, spa);
583 * Set the alternate root, if there is one.
586 spa->spa_root = spa_strdup(altroot);
591 * Every pool starts with the default cachefile
593 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
594 offsetof(spa_config_dirent_t, scd_link));
596 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
597 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
598 list_insert_head(&spa->spa_config_list, dp);
600 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
603 if (config != NULL) {
606 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
608 VERIFY(nvlist_dup(features, &spa->spa_label_features,
612 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
615 if (spa->spa_label_features == NULL) {
616 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
620 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
626 * Removes a spa_t from the namespace, freeing up any memory used. Requires
627 * spa_namespace_lock. This is called only after the spa_t has been closed and
631 spa_remove(spa_t *spa)
633 spa_config_dirent_t *dp;
635 ASSERT(MUTEX_HELD(&spa_namespace_lock));
636 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
638 nvlist_free(spa->spa_config_splitting);
640 avl_remove(&spa_namespace_avl, spa);
641 cv_broadcast(&spa_namespace_cv);
644 spa_strfree(spa->spa_root);
648 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
649 list_remove(&spa->spa_config_list, dp);
650 if (dp->scd_path != NULL)
651 spa_strfree(dp->scd_path);
652 kmem_free(dp, sizeof (spa_config_dirent_t));
655 list_destroy(&spa->spa_config_list);
657 nvlist_free(spa->spa_label_features);
658 nvlist_free(spa->spa_load_info);
659 spa_config_set(spa, NULL);
662 mutex_enter(&cpu_lock);
663 if (spa->spa_deadman_cycid != CYCLIC_NONE)
664 cyclic_remove(spa->spa_deadman_cycid);
665 mutex_exit(&cpu_lock);
666 spa->spa_deadman_cycid = CYCLIC_NONE;
669 callout_drain(&spa->spa_deadman_cycid);
673 refcount_destroy(&spa->spa_refcount);
675 spa_config_lock_destroy(spa);
677 for (int t = 0; t < TXG_SIZE; t++)
678 bplist_destroy(&spa->spa_free_bplist[t]);
680 cv_destroy(&spa->spa_async_cv);
681 cv_destroy(&spa->spa_proc_cv);
682 cv_destroy(&spa->spa_scrub_io_cv);
683 cv_destroy(&spa->spa_suspend_cv);
685 mutex_destroy(&spa->spa_async_lock);
686 mutex_destroy(&spa->spa_errlist_lock);
687 mutex_destroy(&spa->spa_errlog_lock);
688 mutex_destroy(&spa->spa_history_lock);
689 mutex_destroy(&spa->spa_proc_lock);
690 mutex_destroy(&spa->spa_props_lock);
691 mutex_destroy(&spa->spa_scrub_lock);
692 mutex_destroy(&spa->spa_suspend_lock);
693 mutex_destroy(&spa->spa_vdev_top_lock);
695 kmem_free(spa, sizeof (spa_t));
699 * Given a pool, return the next pool in the namespace, or NULL if there is
700 * none. If 'prev' is NULL, return the first pool.
703 spa_next(spa_t *prev)
705 ASSERT(MUTEX_HELD(&spa_namespace_lock));
708 return (AVL_NEXT(&spa_namespace_avl, prev));
710 return (avl_first(&spa_namespace_avl));
714 * ==========================================================================
715 * SPA refcount functions
716 * ==========================================================================
720 * Add a reference to the given spa_t. Must have at least one reference, or
721 * have the namespace lock held.
724 spa_open_ref(spa_t *spa, void *tag)
726 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
727 MUTEX_HELD(&spa_namespace_lock));
728 (void) refcount_add(&spa->spa_refcount, tag);
732 * Remove a reference to the given spa_t. Must have at least one reference, or
733 * have the namespace lock held.
736 spa_close(spa_t *spa, void *tag)
738 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
739 MUTEX_HELD(&spa_namespace_lock));
740 (void) refcount_remove(&spa->spa_refcount, tag);
744 * Check to see if the spa refcount is zero. Must be called with
745 * spa_namespace_lock held. We really compare against spa_minref, which is the
746 * number of references acquired when opening a pool
749 spa_refcount_zero(spa_t *spa)
751 ASSERT(MUTEX_HELD(&spa_namespace_lock));
753 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
757 * ==========================================================================
758 * SPA spare and l2cache tracking
759 * ==========================================================================
763 * Hot spares and cache devices are tracked using the same code below,
764 * for 'auxiliary' devices.
767 typedef struct spa_aux {
775 spa_aux_compare(const void *a, const void *b)
777 const spa_aux_t *sa = a;
778 const spa_aux_t *sb = b;
780 if (sa->aux_guid < sb->aux_guid)
782 else if (sa->aux_guid > sb->aux_guid)
789 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
795 search.aux_guid = vd->vdev_guid;
796 if ((aux = avl_find(avl, &search, &where)) != NULL) {
799 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
800 aux->aux_guid = vd->vdev_guid;
802 avl_insert(avl, aux, where);
807 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
813 search.aux_guid = vd->vdev_guid;
814 aux = avl_find(avl, &search, &where);
818 if (--aux->aux_count == 0) {
819 avl_remove(avl, aux);
820 kmem_free(aux, sizeof (spa_aux_t));
821 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
822 aux->aux_pool = 0ULL;
827 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
829 spa_aux_t search, *found;
831 search.aux_guid = guid;
832 found = avl_find(avl, &search, NULL);
836 *pool = found->aux_pool;
843 *refcnt = found->aux_count;
848 return (found != NULL);
852 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
854 spa_aux_t search, *found;
857 search.aux_guid = vd->vdev_guid;
858 found = avl_find(avl, &search, &where);
859 ASSERT(found != NULL);
860 ASSERT(found->aux_pool == 0ULL);
862 found->aux_pool = spa_guid(vd->vdev_spa);
866 * Spares are tracked globally due to the following constraints:
868 * - A spare may be part of multiple pools.
869 * - A spare may be added to a pool even if it's actively in use within
871 * - A spare in use in any pool can only be the source of a replacement if
872 * the target is a spare in the same pool.
874 * We keep track of all spares on the system through the use of a reference
875 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
876 * spare, then we bump the reference count in the AVL tree. In addition, we set
877 * the 'vdev_isspare' member to indicate that the device is a spare (active or
878 * inactive). When a spare is made active (used to replace a device in the
879 * pool), we also keep track of which pool its been made a part of.
881 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
882 * called under the spa_namespace lock as part of vdev reconfiguration. The
883 * separate spare lock exists for the status query path, which does not need to
884 * be completely consistent with respect to other vdev configuration changes.
888 spa_spare_compare(const void *a, const void *b)
890 return (spa_aux_compare(a, b));
894 spa_spare_add(vdev_t *vd)
896 mutex_enter(&spa_spare_lock);
897 ASSERT(!vd->vdev_isspare);
898 spa_aux_add(vd, &spa_spare_avl);
899 vd->vdev_isspare = B_TRUE;
900 mutex_exit(&spa_spare_lock);
904 spa_spare_remove(vdev_t *vd)
906 mutex_enter(&spa_spare_lock);
907 ASSERT(vd->vdev_isspare);
908 spa_aux_remove(vd, &spa_spare_avl);
909 vd->vdev_isspare = B_FALSE;
910 mutex_exit(&spa_spare_lock);
914 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
918 mutex_enter(&spa_spare_lock);
919 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
920 mutex_exit(&spa_spare_lock);
926 spa_spare_activate(vdev_t *vd)
928 mutex_enter(&spa_spare_lock);
929 ASSERT(vd->vdev_isspare);
930 spa_aux_activate(vd, &spa_spare_avl);
931 mutex_exit(&spa_spare_lock);
935 * Level 2 ARC devices are tracked globally for the same reasons as spares.
936 * Cache devices currently only support one pool per cache device, and so
937 * for these devices the aux reference count is currently unused beyond 1.
941 spa_l2cache_compare(const void *a, const void *b)
943 return (spa_aux_compare(a, b));
947 spa_l2cache_add(vdev_t *vd)
949 mutex_enter(&spa_l2cache_lock);
950 ASSERT(!vd->vdev_isl2cache);
951 spa_aux_add(vd, &spa_l2cache_avl);
952 vd->vdev_isl2cache = B_TRUE;
953 mutex_exit(&spa_l2cache_lock);
957 spa_l2cache_remove(vdev_t *vd)
959 mutex_enter(&spa_l2cache_lock);
960 ASSERT(vd->vdev_isl2cache);
961 spa_aux_remove(vd, &spa_l2cache_avl);
962 vd->vdev_isl2cache = B_FALSE;
963 mutex_exit(&spa_l2cache_lock);
967 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
971 mutex_enter(&spa_l2cache_lock);
972 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
973 mutex_exit(&spa_l2cache_lock);
979 spa_l2cache_activate(vdev_t *vd)
981 mutex_enter(&spa_l2cache_lock);
982 ASSERT(vd->vdev_isl2cache);
983 spa_aux_activate(vd, &spa_l2cache_avl);
984 mutex_exit(&spa_l2cache_lock);
988 * ==========================================================================
990 * ==========================================================================
994 * Lock the given spa_t for the purpose of adding or removing a vdev.
995 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
996 * It returns the next transaction group for the spa_t.
999 spa_vdev_enter(spa_t *spa)
1001 mutex_enter(&spa->spa_vdev_top_lock);
1002 mutex_enter(&spa_namespace_lock);
1003 return (spa_vdev_config_enter(spa));
1007 * Internal implementation for spa_vdev_enter(). Used when a vdev
1008 * operation requires multiple syncs (i.e. removing a device) while
1009 * keeping the spa_namespace_lock held.
1012 spa_vdev_config_enter(spa_t *spa)
1014 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1016 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1018 return (spa_last_synced_txg(spa) + 1);
1022 * Used in combination with spa_vdev_config_enter() to allow the syncing
1023 * of multiple transactions without releasing the spa_namespace_lock.
1026 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1028 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1030 int config_changed = B_FALSE;
1032 ASSERT(txg > spa_last_synced_txg(spa));
1034 spa->spa_pending_vdev = NULL;
1037 * Reassess the DTLs.
1039 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1041 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1042 config_changed = B_TRUE;
1043 spa->spa_config_generation++;
1047 * Verify the metaslab classes.
1049 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1050 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1052 spa_config_exit(spa, SCL_ALL, spa);
1055 * Panic the system if the specified tag requires it. This
1056 * is useful for ensuring that configurations are updated
1059 if (zio_injection_enabled)
1060 zio_handle_panic_injection(spa, tag, 0);
1063 * Note: this txg_wait_synced() is important because it ensures
1064 * that there won't be more than one config change per txg.
1065 * This allows us to use the txg as the generation number.
1068 txg_wait_synced(spa->spa_dsl_pool, txg);
1071 ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
1072 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1074 spa_config_exit(spa, SCL_ALL, spa);
1078 * If the config changed, update the config cache.
1081 spa_config_sync(spa, B_FALSE, B_TRUE);
1085 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1086 * locking of spa_vdev_enter(), we also want make sure the transactions have
1087 * synced to disk, and then update the global configuration cache with the new
1091 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1093 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1094 mutex_exit(&spa_namespace_lock);
1095 mutex_exit(&spa->spa_vdev_top_lock);
1101 * Lock the given spa_t for the purpose of changing vdev state.
1104 spa_vdev_state_enter(spa_t *spa, int oplocks)
1106 int locks = SCL_STATE_ALL | oplocks;
1109 * Root pools may need to read of the underlying devfs filesystem
1110 * when opening up a vdev. Unfortunately if we're holding the
1111 * SCL_ZIO lock it will result in a deadlock when we try to issue
1112 * the read from the root filesystem. Instead we "prefetch"
1113 * the associated vnodes that we need prior to opening the
1114 * underlying devices and cache them so that we can prevent
1115 * any I/O when we are doing the actual open.
1117 if (spa_is_root(spa)) {
1118 int low = locks & ~(SCL_ZIO - 1);
1119 int high = locks & ~low;
1121 spa_config_enter(spa, high, spa, RW_WRITER);
1122 vdev_hold(spa->spa_root_vdev);
1123 spa_config_enter(spa, low, spa, RW_WRITER);
1125 spa_config_enter(spa, locks, spa, RW_WRITER);
1127 spa->spa_vdev_locks = locks;
1131 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1133 boolean_t config_changed = B_FALSE;
1135 if (vd != NULL || error == 0)
1136 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1140 vdev_state_dirty(vd->vdev_top);
1141 config_changed = B_TRUE;
1142 spa->spa_config_generation++;
1145 if (spa_is_root(spa))
1146 vdev_rele(spa->spa_root_vdev);
1148 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1149 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1152 * If anything changed, wait for it to sync. This ensures that,
1153 * from the system administrator's perspective, zpool(1M) commands
1154 * are synchronous. This is important for things like zpool offline:
1155 * when the command completes, you expect no further I/O from ZFS.
1158 txg_wait_synced(spa->spa_dsl_pool, 0);
1161 * If the config changed, update the config cache.
1163 if (config_changed) {
1164 mutex_enter(&spa_namespace_lock);
1165 spa_config_sync(spa, B_FALSE, B_TRUE);
1166 mutex_exit(&spa_namespace_lock);
1173 * ==========================================================================
1174 * Miscellaneous functions
1175 * ==========================================================================
1179 spa_activate_mos_feature(spa_t *spa, const char *feature)
1181 (void) nvlist_add_boolean(spa->spa_label_features, feature);
1182 vdev_config_dirty(spa->spa_root_vdev);
1186 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1188 (void) nvlist_remove_all(spa->spa_label_features, feature);
1189 vdev_config_dirty(spa->spa_root_vdev);
1196 spa_rename(const char *name, const char *newname)
1202 * Lookup the spa_t and grab the config lock for writing. We need to
1203 * actually open the pool so that we can sync out the necessary labels.
1204 * It's OK to call spa_open() with the namespace lock held because we
1205 * allow recursive calls for other reasons.
1207 mutex_enter(&spa_namespace_lock);
1208 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1209 mutex_exit(&spa_namespace_lock);
1213 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1215 avl_remove(&spa_namespace_avl, spa);
1216 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1217 avl_add(&spa_namespace_avl, spa);
1220 * Sync all labels to disk with the new names by marking the root vdev
1221 * dirty and waiting for it to sync. It will pick up the new pool name
1224 vdev_config_dirty(spa->spa_root_vdev);
1226 spa_config_exit(spa, SCL_ALL, FTAG);
1228 txg_wait_synced(spa->spa_dsl_pool, 0);
1231 * Sync the updated config cache.
1233 spa_config_sync(spa, B_FALSE, B_TRUE);
1235 spa_close(spa, FTAG);
1237 mutex_exit(&spa_namespace_lock);
1243 * Return the spa_t associated with given pool_guid, if it exists. If
1244 * device_guid is non-zero, determine whether the pool exists *and* contains
1245 * a device with the specified device_guid.
1248 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1251 avl_tree_t *t = &spa_namespace_avl;
1253 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1255 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1256 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1258 if (spa->spa_root_vdev == NULL)
1260 if (spa_guid(spa) == pool_guid) {
1261 if (device_guid == 0)
1264 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1265 device_guid) != NULL)
1269 * Check any devices we may be in the process of adding.
1271 if (spa->spa_pending_vdev) {
1272 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1273 device_guid) != NULL)
1283 * Determine whether a pool with the given pool_guid exists.
1286 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1288 return (spa_by_guid(pool_guid, device_guid) != NULL);
1292 spa_strdup(const char *s)
1298 new = kmem_alloc(len + 1, KM_SLEEP);
1306 spa_strfree(char *s)
1308 kmem_free(s, strlen(s) + 1);
1312 spa_get_random(uint64_t range)
1318 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1324 spa_generate_guid(spa_t *spa)
1326 uint64_t guid = spa_get_random(-1ULL);
1329 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1330 guid = spa_get_random(-1ULL);
1332 while (guid == 0 || spa_guid_exists(guid, 0))
1333 guid = spa_get_random(-1ULL);
1340 sprintf_blkptr(char *buf, const blkptr_t *bp)
1343 char *checksum = NULL;
1344 char *compress = NULL;
1347 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1348 dmu_object_byteswap_t bswap =
1349 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1350 (void) snprintf(type, sizeof (type), "bswap %s %s",
1351 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1352 "metadata" : "data",
1353 dmu_ot_byteswap[bswap].ob_name);
1355 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1358 checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1359 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1362 SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1366 spa_freeze(spa_t *spa)
1368 uint64_t freeze_txg = 0;
1370 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1371 if (spa->spa_freeze_txg == UINT64_MAX) {
1372 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1373 spa->spa_freeze_txg = freeze_txg;
1375 spa_config_exit(spa, SCL_ALL, FTAG);
1376 if (freeze_txg != 0)
1377 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1381 zfs_panic_recover(const char *fmt, ...)
1386 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1391 * This is a stripped-down version of strtoull, suitable only for converting
1392 * lowercase hexadecimal numbers that don't overflow.
1395 zfs_strtonum(const char *str, char **nptr)
1401 while ((c = *str) != '\0') {
1402 if (c >= '0' && c <= '9')
1404 else if (c >= 'a' && c <= 'f')
1405 digit = 10 + c - 'a';
1416 *nptr = (char *)str;
1422 * ==========================================================================
1423 * Accessor functions
1424 * ==========================================================================
1428 spa_shutting_down(spa_t *spa)
1430 return (spa->spa_async_suspended);
1434 spa_get_dsl(spa_t *spa)
1436 return (spa->spa_dsl_pool);
1440 spa_is_initializing(spa_t *spa)
1442 return (spa->spa_is_initializing);
1446 spa_get_rootblkptr(spa_t *spa)
1448 return (&spa->spa_ubsync.ub_rootbp);
1452 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1454 spa->spa_uberblock.ub_rootbp = *bp;
1458 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1460 if (spa->spa_root == NULL)
1463 (void) strncpy(buf, spa->spa_root, buflen);
1467 spa_sync_pass(spa_t *spa)
1469 return (spa->spa_sync_pass);
1473 spa_name(spa_t *spa)
1475 return (spa->spa_name);
1479 spa_guid(spa_t *spa)
1481 dsl_pool_t *dp = spa_get_dsl(spa);
1485 * If we fail to parse the config during spa_load(), we can go through
1486 * the error path (which posts an ereport) and end up here with no root
1487 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1490 if (spa->spa_root_vdev == NULL)
1491 return (spa->spa_config_guid);
1493 guid = spa->spa_last_synced_guid != 0 ?
1494 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1497 * Return the most recently synced out guid unless we're
1498 * in syncing context.
1500 if (dp && dsl_pool_sync_context(dp))
1501 return (spa->spa_root_vdev->vdev_guid);
1507 spa_load_guid(spa_t *spa)
1510 * This is a GUID that exists solely as a reference for the
1511 * purposes of the arc. It is generated at load time, and
1512 * is never written to persistent storage.
1514 return (spa->spa_load_guid);
1518 spa_last_synced_txg(spa_t *spa)
1520 return (spa->spa_ubsync.ub_txg);
1524 spa_first_txg(spa_t *spa)
1526 return (spa->spa_first_txg);
1530 spa_syncing_txg(spa_t *spa)
1532 return (spa->spa_syncing_txg);
1536 spa_state(spa_t *spa)
1538 return (spa->spa_state);
1542 spa_load_state(spa_t *spa)
1544 return (spa->spa_load_state);
1548 spa_freeze_txg(spa_t *spa)
1550 return (spa->spa_freeze_txg);
1555 spa_get_asize(spa_t *spa, uint64_t lsize)
1557 return (lsize * spa_asize_inflation);
1561 spa_get_dspace(spa_t *spa)
1563 return (spa->spa_dspace);
1567 spa_update_dspace(spa_t *spa)
1569 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1570 ddt_get_dedup_dspace(spa);
1574 * Return the failure mode that has been set to this pool. The default
1575 * behavior will be to block all I/Os when a complete failure occurs.
1578 spa_get_failmode(spa_t *spa)
1580 return (spa->spa_failmode);
1584 spa_suspended(spa_t *spa)
1586 return (spa->spa_suspended);
1590 spa_version(spa_t *spa)
1592 return (spa->spa_ubsync.ub_version);
1596 spa_deflate(spa_t *spa)
1598 return (spa->spa_deflate);
1602 spa_normal_class(spa_t *spa)
1604 return (spa->spa_normal_class);
1608 spa_log_class(spa_t *spa)
1610 return (spa->spa_log_class);
1614 spa_max_replication(spa_t *spa)
1617 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1618 * handle BPs with more than one DVA allocated. Set our max
1619 * replication level accordingly.
1621 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1623 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1627 spa_prev_software_version(spa_t *spa)
1629 return (spa->spa_prev_software_version);
1633 spa_deadman_synctime(spa_t *spa)
1635 return (spa->spa_deadman_synctime);
1639 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1641 uint64_t asize = DVA_GET_ASIZE(dva);
1642 uint64_t dsize = asize;
1644 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1646 if (asize != 0 && spa->spa_deflate) {
1647 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1648 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1655 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1659 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1660 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1666 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1670 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1672 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1673 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1675 spa_config_exit(spa, SCL_VDEV, FTAG);
1681 * ==========================================================================
1682 * Initialization and Termination
1683 * ==========================================================================
1687 spa_name_compare(const void *a1, const void *a2)
1689 const spa_t *s1 = a1;
1690 const spa_t *s2 = a2;
1693 s = strcmp(s1->spa_name, s2->spa_name);
1704 return (spa_active_count);
1714 EVENTHANDLER_DEFINE(mountroot, spa_boot_init, NULL, 0);
1720 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1721 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1722 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1723 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1725 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1726 offsetof(spa_t, spa_avl));
1728 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1729 offsetof(spa_aux_t, aux_avl));
1731 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1732 offsetof(spa_aux_t, aux_avl));
1734 spa_mode_global = mode;
1740 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1741 arc_procfd = open("/proc/self/ctl", O_WRONLY);
1742 if (arc_procfd == -1) {
1743 perror("could not enable watchpoints: "
1744 "opening /proc/self/ctl failed: ");
1750 #endif /* illumos */
1757 vdev_cache_stat_init();
1760 zpool_feature_init();
1767 #endif /* !illumos */
1777 vdev_cache_stat_fini();
1785 avl_destroy(&spa_namespace_avl);
1786 avl_destroy(&spa_spare_avl);
1787 avl_destroy(&spa_l2cache_avl);
1789 cv_destroy(&spa_namespace_cv);
1790 mutex_destroy(&spa_namespace_lock);
1791 mutex_destroy(&spa_spare_lock);
1792 mutex_destroy(&spa_l2cache_lock);
1796 * Return whether this pool has slogs. No locking needed.
1797 * It's not a problem if the wrong answer is returned as it's only for
1798 * performance and not correctness
1801 spa_has_slogs(spa_t *spa)
1803 return (spa->spa_log_class->mc_rotor != NULL);
1807 spa_get_log_state(spa_t *spa)
1809 return (spa->spa_log_state);
1813 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1815 spa->spa_log_state = state;
1819 spa_is_root(spa_t *spa)
1821 return (spa->spa_is_root);
1825 spa_writeable(spa_t *spa)
1827 return (!!(spa->spa_mode & FWRITE));
1831 spa_mode(spa_t *spa)
1833 return (spa->spa_mode);
1837 spa_bootfs(spa_t *spa)
1839 return (spa->spa_bootfs);
1843 spa_delegation(spa_t *spa)
1845 return (spa->spa_delegation);
1849 spa_meta_objset(spa_t *spa)
1851 return (spa->spa_meta_objset);
1855 spa_dedup_checksum(spa_t *spa)
1857 return (spa->spa_dedup_checksum);
1861 * Reset pool scan stat per scan pass (or reboot).
1864 spa_scan_stat_init(spa_t *spa)
1866 /* data not stored on disk */
1867 spa->spa_scan_pass_start = gethrestime_sec();
1868 spa->spa_scan_pass_exam = 0;
1869 vdev_scan_stat_init(spa->spa_root_vdev);
1873 * Get scan stats for zpool status reports
1876 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1878 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1880 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1881 return (SET_ERROR(ENOENT));
1882 bzero(ps, sizeof (pool_scan_stat_t));
1884 /* data stored on disk */
1885 ps->pss_func = scn->scn_phys.scn_func;
1886 ps->pss_start_time = scn->scn_phys.scn_start_time;
1887 ps->pss_end_time = scn->scn_phys.scn_end_time;
1888 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
1889 ps->pss_examined = scn->scn_phys.scn_examined;
1890 ps->pss_to_process = scn->scn_phys.scn_to_process;
1891 ps->pss_processed = scn->scn_phys.scn_processed;
1892 ps->pss_errors = scn->scn_phys.scn_errors;
1893 ps->pss_state = scn->scn_phys.scn_state;
1895 /* data not stored on disk */
1896 ps->pss_pass_start = spa->spa_scan_pass_start;
1897 ps->pss_pass_exam = spa->spa_scan_pass_exam;
1903 spa_debug_enabled(spa_t *spa)
1905 return (spa->spa_debug);