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.
255 * This should only be used as a last resort, as it typically results
256 * in leaked space, or worse.
259 SYSCTL_DECL(_vfs_zfs);
260 TUNABLE_INT("vfs.zfs.recover", &zfs_recover);
261 SYSCTL_INT(_vfs_zfs, OID_AUTO, recover, CTLFLAG_RDTUN, &zfs_recover, 0,
262 "Try to recover from otherwise-fatal errors.");
265 * Expiration time in milliseconds. This value has two meanings. First it is
266 * used to determine when the spa_deadman() logic should fire. By default the
267 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
268 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
269 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
272 uint64_t zfs_deadman_synctime_ms = 1000000ULL;
273 TUNABLE_QUAD("vfs.zfs.deadman_synctime_ms", &zfs_deadman_synctime_ms);
274 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_synctime_ms, CTLFLAG_RDTUN,
275 &zfs_deadman_synctime_ms, 0,
276 "Stalled ZFS I/O expiration time in milliseconds");
279 * Check time in milliseconds. This defines the frequency at which we check
282 uint64_t zfs_deadman_checktime_ms = 5000ULL;
283 TUNABLE_QUAD("vfs.zfs.deadman_checktime_ms", &zfs_deadman_checktime_ms);
284 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_checktime_ms, CTLFLAG_RDTUN,
285 &zfs_deadman_checktime_ms, 0,
286 "Period of checks for stalled ZFS I/O in milliseconds");
289 * Default value of -1 for zfs_deadman_enabled is resolved in
292 int zfs_deadman_enabled = -1;
293 TUNABLE_INT("vfs.zfs.deadman_enabled", &zfs_deadman_enabled);
294 SYSCTL_INT(_vfs_zfs, OID_AUTO, deadman_enabled, CTLFLAG_RDTUN,
295 &zfs_deadman_enabled, 0, "Kernel panic on stalled ZFS I/O");
298 * The worst case is single-sector max-parity RAID-Z blocks, in which
299 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
300 * times the size; so just assume that. Add to this the fact that
301 * we can have up to 3 DVAs per bp, and one more factor of 2 because
302 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
304 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
306 int spa_asize_inflation = 24;
314 * If we are not i386 or amd64 or in a virtual machine,
315 * disable ZFS deadman thread by default
317 if (zfs_deadman_enabled == -1) {
318 #if defined(__amd64__) || defined(__i386__)
319 zfs_deadman_enabled = (vm_guest == VM_GUEST_NO) ? 1 : 0;
321 zfs_deadman_enabled = 0;
326 #endif /* !illumos */
329 * ==========================================================================
331 * ==========================================================================
334 spa_config_lock_init(spa_t *spa)
336 for (int i = 0; i < SCL_LOCKS; i++) {
337 spa_config_lock_t *scl = &spa->spa_config_lock[i];
338 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
339 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
340 refcount_create_untracked(&scl->scl_count);
341 scl->scl_writer = NULL;
342 scl->scl_write_wanted = 0;
347 spa_config_lock_destroy(spa_t *spa)
349 for (int i = 0; i < SCL_LOCKS; i++) {
350 spa_config_lock_t *scl = &spa->spa_config_lock[i];
351 mutex_destroy(&scl->scl_lock);
352 cv_destroy(&scl->scl_cv);
353 refcount_destroy(&scl->scl_count);
354 ASSERT(scl->scl_writer == NULL);
355 ASSERT(scl->scl_write_wanted == 0);
360 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
362 for (int i = 0; i < SCL_LOCKS; i++) {
363 spa_config_lock_t *scl = &spa->spa_config_lock[i];
364 if (!(locks & (1 << i)))
366 mutex_enter(&scl->scl_lock);
367 if (rw == RW_READER) {
368 if (scl->scl_writer || scl->scl_write_wanted) {
369 mutex_exit(&scl->scl_lock);
370 spa_config_exit(spa, locks ^ (1 << i), tag);
374 ASSERT(scl->scl_writer != curthread);
375 if (!refcount_is_zero(&scl->scl_count)) {
376 mutex_exit(&scl->scl_lock);
377 spa_config_exit(spa, locks ^ (1 << i), tag);
380 scl->scl_writer = curthread;
382 (void) refcount_add(&scl->scl_count, tag);
383 mutex_exit(&scl->scl_lock);
389 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
393 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
395 for (int i = 0; i < SCL_LOCKS; i++) {
396 spa_config_lock_t *scl = &spa->spa_config_lock[i];
397 if (scl->scl_writer == curthread)
398 wlocks_held |= (1 << i);
399 if (!(locks & (1 << i)))
401 mutex_enter(&scl->scl_lock);
402 if (rw == RW_READER) {
403 while (scl->scl_writer || scl->scl_write_wanted) {
404 cv_wait(&scl->scl_cv, &scl->scl_lock);
407 ASSERT(scl->scl_writer != curthread);
408 while (!refcount_is_zero(&scl->scl_count)) {
409 scl->scl_write_wanted++;
410 cv_wait(&scl->scl_cv, &scl->scl_lock);
411 scl->scl_write_wanted--;
413 scl->scl_writer = curthread;
415 (void) refcount_add(&scl->scl_count, tag);
416 mutex_exit(&scl->scl_lock);
418 ASSERT(wlocks_held <= locks);
422 spa_config_exit(spa_t *spa, int locks, void *tag)
424 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
425 spa_config_lock_t *scl = &spa->spa_config_lock[i];
426 if (!(locks & (1 << i)))
428 mutex_enter(&scl->scl_lock);
429 ASSERT(!refcount_is_zero(&scl->scl_count));
430 if (refcount_remove(&scl->scl_count, tag) == 0) {
431 ASSERT(scl->scl_writer == NULL ||
432 scl->scl_writer == curthread);
433 scl->scl_writer = NULL; /* OK in either case */
434 cv_broadcast(&scl->scl_cv);
436 mutex_exit(&scl->scl_lock);
441 spa_config_held(spa_t *spa, int locks, krw_t rw)
445 for (int i = 0; i < SCL_LOCKS; i++) {
446 spa_config_lock_t *scl = &spa->spa_config_lock[i];
447 if (!(locks & (1 << i)))
449 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
450 (rw == RW_WRITER && scl->scl_writer == curthread))
451 locks_held |= 1 << i;
458 * ==========================================================================
459 * SPA namespace functions
460 * ==========================================================================
464 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
465 * Returns NULL if no matching spa_t is found.
468 spa_lookup(const char *name)
470 static spa_t search; /* spa_t is large; don't allocate on stack */
475 ASSERT(MUTEX_HELD(&spa_namespace_lock));
477 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
480 * If it's a full dataset name, figure out the pool name and
483 cp = strpbrk(search.spa_name, "/@");
487 spa = avl_find(&spa_namespace_avl, &search, &where);
493 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
494 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
495 * looking for potentially hung I/Os.
498 spa_deadman(void *arg)
503 * Disable the deadman timer if the pool is suspended.
505 if (spa_suspended(spa)) {
507 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
509 /* Nothing. just don't schedule any future callouts. */
514 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
515 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
516 ++spa->spa_deadman_calls);
517 if (zfs_deadman_enabled)
518 vdev_deadman(spa->spa_root_vdev);
522 * Create an uninitialized spa_t with the given name. Requires
523 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
524 * exist by calling spa_lookup() first.
527 spa_add(const char *name, nvlist_t *config, const char *altroot)
530 spa_config_dirent_t *dp;
536 ASSERT(MUTEX_HELD(&spa_namespace_lock));
538 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
540 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
541 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
542 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
543 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
544 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
545 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
546 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
547 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
548 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
550 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
551 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
552 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
553 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
555 for (int t = 0; t < TXG_SIZE; t++)
556 bplist_create(&spa->spa_free_bplist[t]);
558 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
559 spa->spa_state = POOL_STATE_UNINITIALIZED;
560 spa->spa_freeze_txg = UINT64_MAX;
561 spa->spa_final_txg = UINT64_MAX;
562 spa->spa_load_max_txg = UINT64_MAX;
564 spa->spa_proc_state = SPA_PROC_NONE;
567 hdlr.cyh_func = spa_deadman;
569 hdlr.cyh_level = CY_LOW_LEVEL;
572 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
576 * This determines how often we need to check for hung I/Os after
577 * the cyclic has already fired. Since checking for hung I/Os is
578 * an expensive operation we don't want to check too frequently.
579 * Instead wait for 5 seconds before checking again.
581 when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
582 when.cyt_when = CY_INFINITY;
583 mutex_enter(&cpu_lock);
584 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
585 mutex_exit(&cpu_lock);
588 callout_init(&spa->spa_deadman_cycid, CALLOUT_MPSAFE);
591 refcount_create(&spa->spa_refcount);
592 spa_config_lock_init(spa);
594 avl_add(&spa_namespace_avl, spa);
597 * Set the alternate root, if there is one.
600 spa->spa_root = spa_strdup(altroot);
605 * Every pool starts with the default cachefile
607 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
608 offsetof(spa_config_dirent_t, scd_link));
610 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
611 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
612 list_insert_head(&spa->spa_config_list, dp);
614 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
617 if (config != NULL) {
620 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
622 VERIFY(nvlist_dup(features, &spa->spa_label_features,
626 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
629 if (spa->spa_label_features == NULL) {
630 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
634 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
640 * Removes a spa_t from the namespace, freeing up any memory used. Requires
641 * spa_namespace_lock. This is called only after the spa_t has been closed and
645 spa_remove(spa_t *spa)
647 spa_config_dirent_t *dp;
649 ASSERT(MUTEX_HELD(&spa_namespace_lock));
650 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
652 nvlist_free(spa->spa_config_splitting);
654 avl_remove(&spa_namespace_avl, spa);
655 cv_broadcast(&spa_namespace_cv);
658 spa_strfree(spa->spa_root);
662 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
663 list_remove(&spa->spa_config_list, dp);
664 if (dp->scd_path != NULL)
665 spa_strfree(dp->scd_path);
666 kmem_free(dp, sizeof (spa_config_dirent_t));
669 list_destroy(&spa->spa_config_list);
671 nvlist_free(spa->spa_label_features);
672 nvlist_free(spa->spa_load_info);
673 spa_config_set(spa, NULL);
676 mutex_enter(&cpu_lock);
677 if (spa->spa_deadman_cycid != CYCLIC_NONE)
678 cyclic_remove(spa->spa_deadman_cycid);
679 mutex_exit(&cpu_lock);
680 spa->spa_deadman_cycid = CYCLIC_NONE;
683 callout_drain(&spa->spa_deadman_cycid);
687 refcount_destroy(&spa->spa_refcount);
689 spa_config_lock_destroy(spa);
691 for (int t = 0; t < TXG_SIZE; t++)
692 bplist_destroy(&spa->spa_free_bplist[t]);
694 cv_destroy(&spa->spa_async_cv);
695 cv_destroy(&spa->spa_proc_cv);
696 cv_destroy(&spa->spa_scrub_io_cv);
697 cv_destroy(&spa->spa_suspend_cv);
699 mutex_destroy(&spa->spa_async_lock);
700 mutex_destroy(&spa->spa_errlist_lock);
701 mutex_destroy(&spa->spa_errlog_lock);
702 mutex_destroy(&spa->spa_history_lock);
703 mutex_destroy(&spa->spa_proc_lock);
704 mutex_destroy(&spa->spa_props_lock);
705 mutex_destroy(&spa->spa_scrub_lock);
706 mutex_destroy(&spa->spa_suspend_lock);
707 mutex_destroy(&spa->spa_vdev_top_lock);
709 kmem_free(spa, sizeof (spa_t));
713 * Given a pool, return the next pool in the namespace, or NULL if there is
714 * none. If 'prev' is NULL, return the first pool.
717 spa_next(spa_t *prev)
719 ASSERT(MUTEX_HELD(&spa_namespace_lock));
722 return (AVL_NEXT(&spa_namespace_avl, prev));
724 return (avl_first(&spa_namespace_avl));
728 * ==========================================================================
729 * SPA refcount functions
730 * ==========================================================================
734 * Add a reference to the given spa_t. Must have at least one reference, or
735 * have the namespace lock held.
738 spa_open_ref(spa_t *spa, void *tag)
740 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
741 MUTEX_HELD(&spa_namespace_lock));
742 (void) refcount_add(&spa->spa_refcount, tag);
746 * Remove a reference to the given spa_t. Must have at least one reference, or
747 * have the namespace lock held.
750 spa_close(spa_t *spa, void *tag)
752 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
753 MUTEX_HELD(&spa_namespace_lock));
754 (void) refcount_remove(&spa->spa_refcount, tag);
758 * Check to see if the spa refcount is zero. Must be called with
759 * spa_namespace_lock held. We really compare against spa_minref, which is the
760 * number of references acquired when opening a pool
763 spa_refcount_zero(spa_t *spa)
765 ASSERT(MUTEX_HELD(&spa_namespace_lock));
767 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
771 * ==========================================================================
772 * SPA spare and l2cache tracking
773 * ==========================================================================
777 * Hot spares and cache devices are tracked using the same code below,
778 * for 'auxiliary' devices.
781 typedef struct spa_aux {
789 spa_aux_compare(const void *a, const void *b)
791 const spa_aux_t *sa = a;
792 const spa_aux_t *sb = b;
794 if (sa->aux_guid < sb->aux_guid)
796 else if (sa->aux_guid > sb->aux_guid)
803 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
809 search.aux_guid = vd->vdev_guid;
810 if ((aux = avl_find(avl, &search, &where)) != NULL) {
813 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
814 aux->aux_guid = vd->vdev_guid;
816 avl_insert(avl, aux, where);
821 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
827 search.aux_guid = vd->vdev_guid;
828 aux = avl_find(avl, &search, &where);
832 if (--aux->aux_count == 0) {
833 avl_remove(avl, aux);
834 kmem_free(aux, sizeof (spa_aux_t));
835 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
836 aux->aux_pool = 0ULL;
841 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
843 spa_aux_t search, *found;
845 search.aux_guid = guid;
846 found = avl_find(avl, &search, NULL);
850 *pool = found->aux_pool;
857 *refcnt = found->aux_count;
862 return (found != NULL);
866 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
868 spa_aux_t search, *found;
871 search.aux_guid = vd->vdev_guid;
872 found = avl_find(avl, &search, &where);
873 ASSERT(found != NULL);
874 ASSERT(found->aux_pool == 0ULL);
876 found->aux_pool = spa_guid(vd->vdev_spa);
880 * Spares are tracked globally due to the following constraints:
882 * - A spare may be part of multiple pools.
883 * - A spare may be added to a pool even if it's actively in use within
885 * - A spare in use in any pool can only be the source of a replacement if
886 * the target is a spare in the same pool.
888 * We keep track of all spares on the system through the use of a reference
889 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
890 * spare, then we bump the reference count in the AVL tree. In addition, we set
891 * the 'vdev_isspare' member to indicate that the device is a spare (active or
892 * inactive). When a spare is made active (used to replace a device in the
893 * pool), we also keep track of which pool its been made a part of.
895 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
896 * called under the spa_namespace lock as part of vdev reconfiguration. The
897 * separate spare lock exists for the status query path, which does not need to
898 * be completely consistent with respect to other vdev configuration changes.
902 spa_spare_compare(const void *a, const void *b)
904 return (spa_aux_compare(a, b));
908 spa_spare_add(vdev_t *vd)
910 mutex_enter(&spa_spare_lock);
911 ASSERT(!vd->vdev_isspare);
912 spa_aux_add(vd, &spa_spare_avl);
913 vd->vdev_isspare = B_TRUE;
914 mutex_exit(&spa_spare_lock);
918 spa_spare_remove(vdev_t *vd)
920 mutex_enter(&spa_spare_lock);
921 ASSERT(vd->vdev_isspare);
922 spa_aux_remove(vd, &spa_spare_avl);
923 vd->vdev_isspare = B_FALSE;
924 mutex_exit(&spa_spare_lock);
928 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
932 mutex_enter(&spa_spare_lock);
933 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
934 mutex_exit(&spa_spare_lock);
940 spa_spare_activate(vdev_t *vd)
942 mutex_enter(&spa_spare_lock);
943 ASSERT(vd->vdev_isspare);
944 spa_aux_activate(vd, &spa_spare_avl);
945 mutex_exit(&spa_spare_lock);
949 * Level 2 ARC devices are tracked globally for the same reasons as spares.
950 * Cache devices currently only support one pool per cache device, and so
951 * for these devices the aux reference count is currently unused beyond 1.
955 spa_l2cache_compare(const void *a, const void *b)
957 return (spa_aux_compare(a, b));
961 spa_l2cache_add(vdev_t *vd)
963 mutex_enter(&spa_l2cache_lock);
964 ASSERT(!vd->vdev_isl2cache);
965 spa_aux_add(vd, &spa_l2cache_avl);
966 vd->vdev_isl2cache = B_TRUE;
967 mutex_exit(&spa_l2cache_lock);
971 spa_l2cache_remove(vdev_t *vd)
973 mutex_enter(&spa_l2cache_lock);
974 ASSERT(vd->vdev_isl2cache);
975 spa_aux_remove(vd, &spa_l2cache_avl);
976 vd->vdev_isl2cache = B_FALSE;
977 mutex_exit(&spa_l2cache_lock);
981 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
985 mutex_enter(&spa_l2cache_lock);
986 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
987 mutex_exit(&spa_l2cache_lock);
993 spa_l2cache_activate(vdev_t *vd)
995 mutex_enter(&spa_l2cache_lock);
996 ASSERT(vd->vdev_isl2cache);
997 spa_aux_activate(vd, &spa_l2cache_avl);
998 mutex_exit(&spa_l2cache_lock);
1002 * ==========================================================================
1004 * ==========================================================================
1008 * Lock the given spa_t for the purpose of adding or removing a vdev.
1009 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1010 * It returns the next transaction group for the spa_t.
1013 spa_vdev_enter(spa_t *spa)
1015 mutex_enter(&spa->spa_vdev_top_lock);
1016 mutex_enter(&spa_namespace_lock);
1017 return (spa_vdev_config_enter(spa));
1021 * Internal implementation for spa_vdev_enter(). Used when a vdev
1022 * operation requires multiple syncs (i.e. removing a device) while
1023 * keeping the spa_namespace_lock held.
1026 spa_vdev_config_enter(spa_t *spa)
1028 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1030 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1032 return (spa_last_synced_txg(spa) + 1);
1036 * Used in combination with spa_vdev_config_enter() to allow the syncing
1037 * of multiple transactions without releasing the spa_namespace_lock.
1040 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1042 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1044 int config_changed = B_FALSE;
1046 ASSERT(txg > spa_last_synced_txg(spa));
1048 spa->spa_pending_vdev = NULL;
1051 * Reassess the DTLs.
1053 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1055 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1056 config_changed = B_TRUE;
1057 spa->spa_config_generation++;
1061 * Verify the metaslab classes.
1063 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1064 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1066 spa_config_exit(spa, SCL_ALL, spa);
1069 * Panic the system if the specified tag requires it. This
1070 * is useful for ensuring that configurations are updated
1073 if (zio_injection_enabled)
1074 zio_handle_panic_injection(spa, tag, 0);
1077 * Note: this txg_wait_synced() is important because it ensures
1078 * that there won't be more than one config change per txg.
1079 * This allows us to use the txg as the generation number.
1082 txg_wait_synced(spa->spa_dsl_pool, txg);
1085 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1086 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1088 spa_config_exit(spa, SCL_ALL, spa);
1092 * If the config changed, update the config cache.
1095 spa_config_sync(spa, B_FALSE, B_TRUE);
1099 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1100 * locking of spa_vdev_enter(), we also want make sure the transactions have
1101 * synced to disk, and then update the global configuration cache with the new
1105 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1107 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1108 mutex_exit(&spa_namespace_lock);
1109 mutex_exit(&spa->spa_vdev_top_lock);
1115 * Lock the given spa_t for the purpose of changing vdev state.
1118 spa_vdev_state_enter(spa_t *spa, int oplocks)
1120 int locks = SCL_STATE_ALL | oplocks;
1123 * Root pools may need to read of the underlying devfs filesystem
1124 * when opening up a vdev. Unfortunately if we're holding the
1125 * SCL_ZIO lock it will result in a deadlock when we try to issue
1126 * the read from the root filesystem. Instead we "prefetch"
1127 * the associated vnodes that we need prior to opening the
1128 * underlying devices and cache them so that we can prevent
1129 * any I/O when we are doing the actual open.
1131 if (spa_is_root(spa)) {
1132 int low = locks & ~(SCL_ZIO - 1);
1133 int high = locks & ~low;
1135 spa_config_enter(spa, high, spa, RW_WRITER);
1136 vdev_hold(spa->spa_root_vdev);
1137 spa_config_enter(spa, low, spa, RW_WRITER);
1139 spa_config_enter(spa, locks, spa, RW_WRITER);
1141 spa->spa_vdev_locks = locks;
1145 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1147 boolean_t config_changed = B_FALSE;
1149 if (vd != NULL || error == 0)
1150 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1154 vdev_state_dirty(vd->vdev_top);
1155 config_changed = B_TRUE;
1156 spa->spa_config_generation++;
1159 if (spa_is_root(spa))
1160 vdev_rele(spa->spa_root_vdev);
1162 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1163 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1166 * If anything changed, wait for it to sync. This ensures that,
1167 * from the system administrator's perspective, zpool(1M) commands
1168 * are synchronous. This is important for things like zpool offline:
1169 * when the command completes, you expect no further I/O from ZFS.
1172 txg_wait_synced(spa->spa_dsl_pool, 0);
1175 * If the config changed, update the config cache.
1177 if (config_changed) {
1178 mutex_enter(&spa_namespace_lock);
1179 spa_config_sync(spa, B_FALSE, B_TRUE);
1180 mutex_exit(&spa_namespace_lock);
1187 * ==========================================================================
1188 * Miscellaneous functions
1189 * ==========================================================================
1193 spa_activate_mos_feature(spa_t *spa, const char *feature)
1195 (void) nvlist_add_boolean(spa->spa_label_features, feature);
1196 vdev_config_dirty(spa->spa_root_vdev);
1200 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1202 (void) nvlist_remove_all(spa->spa_label_features, feature);
1203 vdev_config_dirty(spa->spa_root_vdev);
1210 spa_rename(const char *name, const char *newname)
1216 * Lookup the spa_t and grab the config lock for writing. We need to
1217 * actually open the pool so that we can sync out the necessary labels.
1218 * It's OK to call spa_open() with the namespace lock held because we
1219 * allow recursive calls for other reasons.
1221 mutex_enter(&spa_namespace_lock);
1222 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1223 mutex_exit(&spa_namespace_lock);
1227 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1229 avl_remove(&spa_namespace_avl, spa);
1230 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1231 avl_add(&spa_namespace_avl, spa);
1234 * Sync all labels to disk with the new names by marking the root vdev
1235 * dirty and waiting for it to sync. It will pick up the new pool name
1238 vdev_config_dirty(spa->spa_root_vdev);
1240 spa_config_exit(spa, SCL_ALL, FTAG);
1242 txg_wait_synced(spa->spa_dsl_pool, 0);
1245 * Sync the updated config cache.
1247 spa_config_sync(spa, B_FALSE, B_TRUE);
1249 spa_close(spa, FTAG);
1251 mutex_exit(&spa_namespace_lock);
1257 * Return the spa_t associated with given pool_guid, if it exists. If
1258 * device_guid is non-zero, determine whether the pool exists *and* contains
1259 * a device with the specified device_guid.
1262 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1265 avl_tree_t *t = &spa_namespace_avl;
1267 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1269 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1270 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1272 if (spa->spa_root_vdev == NULL)
1274 if (spa_guid(spa) == pool_guid) {
1275 if (device_guid == 0)
1278 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1279 device_guid) != NULL)
1283 * Check any devices we may be in the process of adding.
1285 if (spa->spa_pending_vdev) {
1286 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1287 device_guid) != NULL)
1297 * Determine whether a pool with the given pool_guid exists.
1300 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1302 return (spa_by_guid(pool_guid, device_guid) != NULL);
1306 spa_strdup(const char *s)
1312 new = kmem_alloc(len + 1, KM_SLEEP);
1320 spa_strfree(char *s)
1322 kmem_free(s, strlen(s) + 1);
1326 spa_get_random(uint64_t range)
1332 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1338 spa_generate_guid(spa_t *spa)
1340 uint64_t guid = spa_get_random(-1ULL);
1343 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1344 guid = spa_get_random(-1ULL);
1346 while (guid == 0 || spa_guid_exists(guid, 0))
1347 guid = spa_get_random(-1ULL);
1354 sprintf_blkptr(char *buf, const blkptr_t *bp)
1357 char *checksum = NULL;
1358 char *compress = NULL;
1361 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1362 dmu_object_byteswap_t bswap =
1363 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1364 (void) snprintf(type, sizeof (type), "bswap %s %s",
1365 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1366 "metadata" : "data",
1367 dmu_ot_byteswap[bswap].ob_name);
1369 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1372 checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1373 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1376 SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1380 spa_freeze(spa_t *spa)
1382 uint64_t freeze_txg = 0;
1384 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1385 if (spa->spa_freeze_txg == UINT64_MAX) {
1386 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1387 spa->spa_freeze_txg = freeze_txg;
1389 spa_config_exit(spa, SCL_ALL, FTAG);
1390 if (freeze_txg != 0)
1391 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1395 zfs_panic_recover(const char *fmt, ...)
1400 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1405 * This is a stripped-down version of strtoull, suitable only for converting
1406 * lowercase hexadecimal numbers that don't overflow.
1409 zfs_strtonum(const char *str, char **nptr)
1415 while ((c = *str) != '\0') {
1416 if (c >= '0' && c <= '9')
1418 else if (c >= 'a' && c <= 'f')
1419 digit = 10 + c - 'a';
1430 *nptr = (char *)str;
1436 * ==========================================================================
1437 * Accessor functions
1438 * ==========================================================================
1442 spa_shutting_down(spa_t *spa)
1444 return (spa->spa_async_suspended);
1448 spa_get_dsl(spa_t *spa)
1450 return (spa->spa_dsl_pool);
1454 spa_is_initializing(spa_t *spa)
1456 return (spa->spa_is_initializing);
1460 spa_get_rootblkptr(spa_t *spa)
1462 return (&spa->spa_ubsync.ub_rootbp);
1466 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1468 spa->spa_uberblock.ub_rootbp = *bp;
1472 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1474 if (spa->spa_root == NULL)
1477 (void) strncpy(buf, spa->spa_root, buflen);
1481 spa_sync_pass(spa_t *spa)
1483 return (spa->spa_sync_pass);
1487 spa_name(spa_t *spa)
1489 return (spa->spa_name);
1493 spa_guid(spa_t *spa)
1495 dsl_pool_t *dp = spa_get_dsl(spa);
1499 * If we fail to parse the config during spa_load(), we can go through
1500 * the error path (which posts an ereport) and end up here with no root
1501 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1504 if (spa->spa_root_vdev == NULL)
1505 return (spa->spa_config_guid);
1507 guid = spa->spa_last_synced_guid != 0 ?
1508 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1511 * Return the most recently synced out guid unless we're
1512 * in syncing context.
1514 if (dp && dsl_pool_sync_context(dp))
1515 return (spa->spa_root_vdev->vdev_guid);
1521 spa_load_guid(spa_t *spa)
1524 * This is a GUID that exists solely as a reference for the
1525 * purposes of the arc. It is generated at load time, and
1526 * is never written to persistent storage.
1528 return (spa->spa_load_guid);
1532 spa_last_synced_txg(spa_t *spa)
1534 return (spa->spa_ubsync.ub_txg);
1538 spa_first_txg(spa_t *spa)
1540 return (spa->spa_first_txg);
1544 spa_syncing_txg(spa_t *spa)
1546 return (spa->spa_syncing_txg);
1550 spa_state(spa_t *spa)
1552 return (spa->spa_state);
1556 spa_load_state(spa_t *spa)
1558 return (spa->spa_load_state);
1562 spa_freeze_txg(spa_t *spa)
1564 return (spa->spa_freeze_txg);
1569 spa_get_asize(spa_t *spa, uint64_t lsize)
1571 return (lsize * spa_asize_inflation);
1575 spa_get_dspace(spa_t *spa)
1577 return (spa->spa_dspace);
1581 spa_update_dspace(spa_t *spa)
1583 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1584 ddt_get_dedup_dspace(spa);
1588 * Return the failure mode that has been set to this pool. The default
1589 * behavior will be to block all I/Os when a complete failure occurs.
1592 spa_get_failmode(spa_t *spa)
1594 return (spa->spa_failmode);
1598 spa_suspended(spa_t *spa)
1600 return (spa->spa_suspended);
1604 spa_version(spa_t *spa)
1606 return (spa->spa_ubsync.ub_version);
1610 spa_deflate(spa_t *spa)
1612 return (spa->spa_deflate);
1616 spa_normal_class(spa_t *spa)
1618 return (spa->spa_normal_class);
1622 spa_log_class(spa_t *spa)
1624 return (spa->spa_log_class);
1628 spa_max_replication(spa_t *spa)
1631 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1632 * handle BPs with more than one DVA allocated. Set our max
1633 * replication level accordingly.
1635 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1637 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1641 spa_prev_software_version(spa_t *spa)
1643 return (spa->spa_prev_software_version);
1647 spa_deadman_synctime(spa_t *spa)
1649 return (spa->spa_deadman_synctime);
1653 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1655 uint64_t asize = DVA_GET_ASIZE(dva);
1656 uint64_t dsize = asize;
1658 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1660 if (asize != 0 && spa->spa_deflate) {
1661 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1662 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1669 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1673 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1674 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1680 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1684 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1686 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1687 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1689 spa_config_exit(spa, SCL_VDEV, FTAG);
1695 * ==========================================================================
1696 * Initialization and Termination
1697 * ==========================================================================
1701 spa_name_compare(const void *a1, const void *a2)
1703 const spa_t *s1 = a1;
1704 const spa_t *s2 = a2;
1707 s = strcmp(s1->spa_name, s2->spa_name);
1718 return (spa_active_count);
1728 EVENTHANDLER_DEFINE(mountroot, spa_boot_init, NULL, 0);
1734 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1735 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1736 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1737 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1739 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1740 offsetof(spa_t, spa_avl));
1742 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1743 offsetof(spa_aux_t, aux_avl));
1745 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1746 offsetof(spa_aux_t, aux_avl));
1748 spa_mode_global = mode;
1754 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1755 arc_procfd = open("/proc/self/ctl", O_WRONLY);
1756 if (arc_procfd == -1) {
1757 perror("could not enable watchpoints: "
1758 "opening /proc/self/ctl failed: ");
1764 #endif /* illumos */
1771 vdev_cache_stat_init();
1774 zpool_feature_init();
1781 #endif /* !illumos */
1791 vdev_cache_stat_fini();
1799 avl_destroy(&spa_namespace_avl);
1800 avl_destroy(&spa_spare_avl);
1801 avl_destroy(&spa_l2cache_avl);
1803 cv_destroy(&spa_namespace_cv);
1804 mutex_destroy(&spa_namespace_lock);
1805 mutex_destroy(&spa_spare_lock);
1806 mutex_destroy(&spa_l2cache_lock);
1810 * Return whether this pool has slogs. No locking needed.
1811 * It's not a problem if the wrong answer is returned as it's only for
1812 * performance and not correctness
1815 spa_has_slogs(spa_t *spa)
1817 return (spa->spa_log_class->mc_rotor != NULL);
1821 spa_get_log_state(spa_t *spa)
1823 return (spa->spa_log_state);
1827 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1829 spa->spa_log_state = state;
1833 spa_is_root(spa_t *spa)
1835 return (spa->spa_is_root);
1839 spa_writeable(spa_t *spa)
1841 return (!!(spa->spa_mode & FWRITE));
1845 spa_mode(spa_t *spa)
1847 return (spa->spa_mode);
1851 spa_bootfs(spa_t *spa)
1853 return (spa->spa_bootfs);
1857 spa_delegation(spa_t *spa)
1859 return (spa->spa_delegation);
1863 spa_meta_objset(spa_t *spa)
1865 return (spa->spa_meta_objset);
1869 spa_dedup_checksum(spa_t *spa)
1871 return (spa->spa_dedup_checksum);
1875 * Reset pool scan stat per scan pass (or reboot).
1878 spa_scan_stat_init(spa_t *spa)
1880 /* data not stored on disk */
1881 spa->spa_scan_pass_start = gethrestime_sec();
1882 spa->spa_scan_pass_exam = 0;
1883 vdev_scan_stat_init(spa->spa_root_vdev);
1887 * Get scan stats for zpool status reports
1890 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1892 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1894 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1895 return (SET_ERROR(ENOENT));
1896 bzero(ps, sizeof (pool_scan_stat_t));
1898 /* data stored on disk */
1899 ps->pss_func = scn->scn_phys.scn_func;
1900 ps->pss_start_time = scn->scn_phys.scn_start_time;
1901 ps->pss_end_time = scn->scn_phys.scn_end_time;
1902 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
1903 ps->pss_examined = scn->scn_phys.scn_examined;
1904 ps->pss_to_process = scn->scn_phys.scn_to_process;
1905 ps->pss_processed = scn->scn_phys.scn_processed;
1906 ps->pss_errors = scn->scn_phys.scn_errors;
1907 ps->pss_state = scn->scn_phys.scn_state;
1909 /* data not stored on disk */
1910 ps->pss_pass_start = spa->spa_scan_pass_start;
1911 ps->pss_pass_exam = spa->spa_scan_pass_exam;
1917 spa_debug_enabled(spa_t *spa)
1919 return (spa->spa_debug);