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, 2015 by Delphix. All rights reserved.
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
32 #include <sys/spa_impl.h>
33 #include <sys/spa_boot.h>
35 #include <sys/zio_checksum.h>
36 #include <sys/zio_compress.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/metaslab.h>
43 #include <sys/uberblock_impl.h>
46 #include <sys/unique.h>
47 #include <sys/dsl_pool.h>
48 #include <sys/dsl_dir.h>
49 #include <sys/dsl_prop.h>
50 #include <sys/dsl_scan.h>
51 #include <sys/fs/zfs.h>
52 #include <sys/metaslab_impl.h>
56 #include <sys/zfeature.h>
58 #if defined(__FreeBSD__) && defined(_KERNEL)
59 #include <sys/types.h>
60 #include <sys/sysctl.h>
66 * There are four basic locks for managing spa_t structures:
68 * spa_namespace_lock (global mutex)
70 * This lock must be acquired to do any of the following:
72 * - Lookup a spa_t by name
73 * - Add or remove a spa_t from the namespace
74 * - Increase spa_refcount from non-zero
75 * - Check if spa_refcount is zero
77 * - add/remove/attach/detach devices
78 * - Held for the duration of create/destroy/import/export
80 * It does not need to handle recursion. A create or destroy may
81 * reference objects (files or zvols) in other pools, but by
82 * definition they must have an existing reference, and will never need
83 * to lookup a spa_t by name.
85 * spa_refcount (per-spa refcount_t protected by mutex)
87 * This reference count keep track of any active users of the spa_t. The
88 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
89 * the refcount is never really 'zero' - opening a pool implicitly keeps
90 * some references in the DMU. Internally we check against spa_minref, but
91 * present the image of a zero/non-zero value to consumers.
93 * spa_config_lock[] (per-spa array of rwlocks)
95 * This protects the spa_t from config changes, and must be held in
96 * the following circumstances:
98 * - RW_READER to perform I/O to the spa
99 * - RW_WRITER to change the vdev config
101 * The locking order is fairly straightforward:
103 * spa_namespace_lock -> spa_refcount
105 * The namespace lock must be acquired to increase the refcount from 0
106 * or to check if it is zero.
108 * spa_refcount -> spa_config_lock[]
110 * There must be at least one valid reference on the spa_t to acquire
113 * spa_namespace_lock -> spa_config_lock[]
115 * The namespace lock must always be taken before the config lock.
118 * The spa_namespace_lock can be acquired directly and is globally visible.
120 * The namespace is manipulated using the following functions, all of which
121 * require the spa_namespace_lock to be held.
123 * spa_lookup() Lookup a spa_t by name.
125 * spa_add() Create a new spa_t in the namespace.
127 * spa_remove() Remove a spa_t from the namespace. This also
128 * frees up any memory associated with the spa_t.
130 * spa_next() Returns the next spa_t in the system, or the
131 * first if NULL is passed.
133 * spa_evict_all() Shutdown and remove all spa_t structures in
136 * spa_guid_exists() Determine whether a pool/device guid exists.
138 * The spa_refcount is manipulated using the following functions:
140 * spa_open_ref() Adds a reference to the given spa_t. Must be
141 * called with spa_namespace_lock held if the
142 * refcount is currently zero.
144 * spa_close() Remove a reference from the spa_t. This will
145 * not free the spa_t or remove it from the
146 * namespace. No locking is required.
148 * spa_refcount_zero() Returns true if the refcount is currently
149 * zero. Must be called with spa_namespace_lock
152 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
153 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
154 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
156 * To read the configuration, it suffices to hold one of these locks as reader.
157 * To modify the configuration, you must hold all locks as writer. To modify
158 * vdev state without altering the vdev tree's topology (e.g. online/offline),
159 * you must hold SCL_STATE and SCL_ZIO as writer.
161 * We use these distinct config locks to avoid recursive lock entry.
162 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
163 * block allocations (SCL_ALLOC), which may require reading space maps
164 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
166 * The spa config locks cannot be normal rwlocks because we need the
167 * ability to hand off ownership. For example, SCL_ZIO is acquired
168 * by the issuing thread and later released by an interrupt thread.
169 * They do, however, obey the usual write-wanted semantics to prevent
170 * writer (i.e. system administrator) starvation.
172 * The lock acquisition rules are as follows:
175 * Protects changes to the vdev tree topology, such as vdev
176 * add/remove/attach/detach. Protects the dirty config list
177 * (spa_config_dirty_list) and the set of spares and l2arc devices.
180 * Protects changes to pool state and vdev state, such as vdev
181 * online/offline/fault/degrade/clear. Protects the dirty state list
182 * (spa_state_dirty_list) and global pool state (spa_state).
185 * Protects changes to metaslab groups and classes.
186 * Held as reader by metaslab_alloc() and metaslab_claim().
189 * Held by bp-level zios (those which have no io_vd upon entry)
190 * to prevent changes to the vdev tree. The bp-level zio implicitly
191 * protects all of its vdev child zios, which do not hold SCL_ZIO.
194 * Protects changes to metaslab groups and classes.
195 * Held as reader by metaslab_free(). SCL_FREE is distinct from
196 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
197 * blocks in zio_done() while another i/o that holds either
198 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
201 * Held as reader to prevent changes to the vdev tree during trivial
202 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
203 * other locks, and lower than all of them, to ensure that it's safe
204 * to acquire regardless of caller context.
206 * In addition, the following rules apply:
208 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
209 * The lock ordering is SCL_CONFIG > spa_props_lock.
211 * (b) I/O operations on leaf vdevs. For any zio operation that takes
212 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
213 * or zio_write_phys() -- the caller must ensure that the config cannot
214 * cannot change in the interim, and that the vdev cannot be reopened.
215 * SCL_STATE as reader suffices for both.
217 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
219 * spa_vdev_enter() Acquire the namespace lock and the config lock
222 * spa_vdev_exit() Release the config lock, wait for all I/O
223 * to complete, sync the updated configs to the
224 * cache, and release the namespace lock.
226 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
227 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
228 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
230 * spa_rename() is also implemented within this file since it requires
231 * manipulation of the namespace.
234 static avl_tree_t spa_namespace_avl;
235 kmutex_t spa_namespace_lock;
236 static kcondvar_t spa_namespace_cv;
237 static int spa_active_count;
238 int spa_max_replication_override = SPA_DVAS_PER_BP;
240 static kmutex_t spa_spare_lock;
241 static avl_tree_t spa_spare_avl;
242 static kmutex_t spa_l2cache_lock;
243 static avl_tree_t spa_l2cache_avl;
245 kmem_cache_t *spa_buffer_pool;
249 /* Everything except dprintf and spa is on by default in debug builds */
250 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA);
256 * zfs_recover can be set to nonzero to attempt to recover from
257 * otherwise-fatal errors, typically caused by on-disk corruption. When
258 * set, calls to zfs_panic_recover() will turn into warning messages.
259 * This should only be used as a last resort, as it typically results
260 * in leaked space, or worse.
262 boolean_t zfs_recover = B_FALSE;
265 * If destroy encounters an EIO while reading metadata (e.g. indirect
266 * blocks), space referenced by the missing metadata can not be freed.
267 * Normally this causes the background destroy to become "stalled", as
268 * it is unable to make forward progress. While in this stalled state,
269 * all remaining space to free from the error-encountering filesystem is
270 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
271 * permanently leak the space from indirect blocks that can not be read,
272 * and continue to free everything else that it can.
274 * The default, "stalling" behavior is useful if the storage partially
275 * fails (i.e. some but not all i/os fail), and then later recovers. In
276 * this case, we will be able to continue pool operations while it is
277 * partially failed, and when it recovers, we can continue to free the
278 * space, with no leaks. However, note that this case is actually
281 * Typically pools either (a) fail completely (but perhaps temporarily,
282 * e.g. a top-level vdev going offline), or (b) have localized,
283 * permanent errors (e.g. disk returns the wrong data due to bit flip or
284 * firmware bug). In case (a), this setting does not matter because the
285 * pool will be suspended and the sync thread will not be able to make
286 * forward progress regardless. In case (b), because the error is
287 * permanent, the best we can do is leak the minimum amount of space,
288 * which is what setting this flag will do. Therefore, it is reasonable
289 * for this flag to normally be set, but we chose the more conservative
290 * approach of not setting it, so that there is no possibility of
291 * leaking space in the "partial temporary" failure case.
293 boolean_t zfs_free_leak_on_eio = B_FALSE;
296 * Expiration time in milliseconds. This value has two meanings. First it is
297 * used to determine when the spa_deadman() logic should fire. By default the
298 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds.
299 * Secondly, the value determines if an I/O is considered "hung". Any I/O that
300 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting
303 uint64_t zfs_deadman_synctime_ms = 1000000ULL;
306 * Check time in milliseconds. This defines the frequency at which we check
309 uint64_t zfs_deadman_checktime_ms = 5000ULL;
312 * Default value of -1 for zfs_deadman_enabled is resolved in
315 int zfs_deadman_enabled = -1;
318 * The worst case is single-sector max-parity RAID-Z blocks, in which
319 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
320 * times the size; so just assume that. Add to this the fact that
321 * we can have up to 3 DVAs per bp, and one more factor of 2 because
322 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together,
324 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24
326 int spa_asize_inflation = 24;
328 #if defined(__FreeBSD__) && defined(_KERNEL)
329 SYSCTL_DECL(_vfs_zfs);
330 SYSCTL_INT(_vfs_zfs, OID_AUTO, recover, CTLFLAG_RWTUN, &zfs_recover, 0,
331 "Try to recover from otherwise-fatal errors.");
334 sysctl_vfs_zfs_debug_flags(SYSCTL_HANDLER_ARGS)
339 err = sysctl_handle_int(oidp, &val, 0, req);
340 if (err != 0 || req->newptr == NULL)
344 * ZFS_DEBUG_MODIFY must be enabled prior to boot so all
345 * arc buffers in the system have the necessary additional
346 * checksum data. However, it is safe to disable at any
349 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
350 val &= ~ZFS_DEBUG_MODIFY;
356 SYSCTL_PROC(_vfs_zfs, OID_AUTO, debug_flags,
357 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int),
358 sysctl_vfs_zfs_debug_flags, "IU", "Debug flags for ZFS testing.");
360 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_synctime_ms, CTLFLAG_RDTUN,
361 &zfs_deadman_synctime_ms, 0,
362 "Stalled ZFS I/O expiration time in milliseconds");
363 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_checktime_ms, CTLFLAG_RDTUN,
364 &zfs_deadman_checktime_ms, 0,
365 "Period of checks for stalled ZFS I/O in milliseconds");
366 SYSCTL_INT(_vfs_zfs, OID_AUTO, deadman_enabled, CTLFLAG_RDTUN,
367 &zfs_deadman_enabled, 0, "Kernel panic on stalled ZFS I/O");
368 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_asize_inflation, CTLFLAG_RWTUN,
369 &spa_asize_inflation, 0, "Worst case inflation factor for single sector writes");
378 * If we are not i386 or amd64 or in a virtual machine,
379 * disable ZFS deadman thread by default
381 if (zfs_deadman_enabled == -1) {
382 #if defined(__amd64__) || defined(__i386__)
383 zfs_deadman_enabled = (vm_guest == VM_GUEST_NO) ? 1 : 0;
385 zfs_deadman_enabled = 0;
390 #endif /* !illumos */
393 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in
394 * the pool to be consumed. This ensures that we don't run the pool
395 * completely out of space, due to unaccounted changes (e.g. to the MOS).
396 * It also limits the worst-case time to allocate space. If we have
397 * less than this amount of free space, most ZPL operations (e.g. write,
398 * create) will return ENOSPC.
400 * Certain operations (e.g. file removal, most administrative actions) can
401 * use half the slop space. They will only return ENOSPC if less than half
402 * the slop space is free. Typically, once the pool has less than the slop
403 * space free, the user will use these operations to free up space in the pool.
404 * These are the operations that call dsl_pool_adjustedsize() with the netfree
405 * argument set to TRUE.
407 * A very restricted set of operations are always permitted, regardless of
408 * the amount of free space. These are the operations that call
409 * dsl_sync_task(ZFS_SPACE_CHECK_NONE), e.g. "zfs destroy". If these
410 * operations result in a net increase in the amount of space used,
411 * it is possible to run the pool completely out of space, causing it to
412 * be permanently read-only.
414 * See also the comments in zfs_space_check_t.
416 int spa_slop_shift = 5;
417 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_slop_shift, CTLFLAG_RWTUN,
419 "Shift value of reserved space (1/(2^spa_slop_shift)).");
422 * ==========================================================================
424 * ==========================================================================
427 spa_config_lock_init(spa_t *spa)
429 for (int i = 0; i < SCL_LOCKS; i++) {
430 spa_config_lock_t *scl = &spa->spa_config_lock[i];
431 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
432 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
433 refcount_create_untracked(&scl->scl_count);
434 scl->scl_writer = NULL;
435 scl->scl_write_wanted = 0;
440 spa_config_lock_destroy(spa_t *spa)
442 for (int i = 0; i < SCL_LOCKS; i++) {
443 spa_config_lock_t *scl = &spa->spa_config_lock[i];
444 mutex_destroy(&scl->scl_lock);
445 cv_destroy(&scl->scl_cv);
446 refcount_destroy(&scl->scl_count);
447 ASSERT(scl->scl_writer == NULL);
448 ASSERT(scl->scl_write_wanted == 0);
453 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
455 for (int i = 0; i < SCL_LOCKS; i++) {
456 spa_config_lock_t *scl = &spa->spa_config_lock[i];
457 if (!(locks & (1 << i)))
459 mutex_enter(&scl->scl_lock);
460 if (rw == RW_READER) {
461 if (scl->scl_writer || scl->scl_write_wanted) {
462 mutex_exit(&scl->scl_lock);
463 spa_config_exit(spa, locks & ((1 << i) - 1),
468 ASSERT(scl->scl_writer != curthread);
469 if (!refcount_is_zero(&scl->scl_count)) {
470 mutex_exit(&scl->scl_lock);
471 spa_config_exit(spa, locks & ((1 << i) - 1),
475 scl->scl_writer = curthread;
477 (void) refcount_add(&scl->scl_count, tag);
478 mutex_exit(&scl->scl_lock);
484 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
488 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY);
490 for (int i = 0; i < SCL_LOCKS; i++) {
491 spa_config_lock_t *scl = &spa->spa_config_lock[i];
492 if (scl->scl_writer == curthread)
493 wlocks_held |= (1 << i);
494 if (!(locks & (1 << i)))
496 mutex_enter(&scl->scl_lock);
497 if (rw == RW_READER) {
498 while (scl->scl_writer || scl->scl_write_wanted) {
499 cv_wait(&scl->scl_cv, &scl->scl_lock);
502 ASSERT(scl->scl_writer != curthread);
503 while (!refcount_is_zero(&scl->scl_count)) {
504 scl->scl_write_wanted++;
505 cv_wait(&scl->scl_cv, &scl->scl_lock);
506 scl->scl_write_wanted--;
508 scl->scl_writer = curthread;
510 (void) refcount_add(&scl->scl_count, tag);
511 mutex_exit(&scl->scl_lock);
513 ASSERT(wlocks_held <= locks);
517 spa_config_exit(spa_t *spa, int locks, void *tag)
519 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
520 spa_config_lock_t *scl = &spa->spa_config_lock[i];
521 if (!(locks & (1 << i)))
523 mutex_enter(&scl->scl_lock);
524 ASSERT(!refcount_is_zero(&scl->scl_count));
525 if (refcount_remove(&scl->scl_count, tag) == 0) {
526 ASSERT(scl->scl_writer == NULL ||
527 scl->scl_writer == curthread);
528 scl->scl_writer = NULL; /* OK in either case */
529 cv_broadcast(&scl->scl_cv);
531 mutex_exit(&scl->scl_lock);
536 spa_config_held(spa_t *spa, int locks, krw_t rw)
540 for (int i = 0; i < SCL_LOCKS; i++) {
541 spa_config_lock_t *scl = &spa->spa_config_lock[i];
542 if (!(locks & (1 << i)))
544 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
545 (rw == RW_WRITER && scl->scl_writer == curthread))
546 locks_held |= 1 << i;
553 * ==========================================================================
554 * SPA namespace functions
555 * ==========================================================================
559 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
560 * Returns NULL if no matching spa_t is found.
563 spa_lookup(const char *name)
565 static spa_t search; /* spa_t is large; don't allocate on stack */
570 ASSERT(MUTEX_HELD(&spa_namespace_lock));
572 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
575 * If it's a full dataset name, figure out the pool name and
578 cp = strpbrk(search.spa_name, "/@#");
582 spa = avl_find(&spa_namespace_avl, &search, &where);
588 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms.
589 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues
590 * looking for potentially hung I/Os.
593 spa_deadman(void *arg)
598 * Disable the deadman timer if the pool is suspended.
600 if (spa_suspended(spa)) {
602 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
604 /* Nothing. just don't schedule any future callouts. */
609 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu",
610 (gethrtime() - spa->spa_sync_starttime) / NANOSEC,
611 ++spa->spa_deadman_calls);
612 if (zfs_deadman_enabled)
613 vdev_deadman(spa->spa_root_vdev);
616 callout_schedule(&spa->spa_deadman_cycid,
617 hz * zfs_deadman_checktime_ms / MILLISEC);
623 * Create an uninitialized spa_t with the given name. Requires
624 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
625 * exist by calling spa_lookup() first.
628 spa_add(const char *name, nvlist_t *config, const char *altroot)
631 spa_config_dirent_t *dp;
637 ASSERT(MUTEX_HELD(&spa_namespace_lock));
639 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
641 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
642 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
643 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
644 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL);
645 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
646 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
647 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
648 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL);
649 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
650 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
651 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
653 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
654 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL);
655 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
656 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
657 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
659 for (int t = 0; t < TXG_SIZE; t++)
660 bplist_create(&spa->spa_free_bplist[t]);
662 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
663 spa->spa_state = POOL_STATE_UNINITIALIZED;
664 spa->spa_freeze_txg = UINT64_MAX;
665 spa->spa_final_txg = UINT64_MAX;
666 spa->spa_load_max_txg = UINT64_MAX;
668 spa->spa_proc_state = SPA_PROC_NONE;
671 hdlr.cyh_func = spa_deadman;
673 hdlr.cyh_level = CY_LOW_LEVEL;
676 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms);
680 * This determines how often we need to check for hung I/Os after
681 * the cyclic has already fired. Since checking for hung I/Os is
682 * an expensive operation we don't want to check too frequently.
683 * Instead wait for 5 seconds before checking again.
685 when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms);
686 when.cyt_when = CY_INFINITY;
687 mutex_enter(&cpu_lock);
688 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when);
689 mutex_exit(&cpu_lock);
692 callout_init(&spa->spa_deadman_cycid, 1);
695 refcount_create(&spa->spa_refcount);
696 spa_config_lock_init(spa);
698 avl_add(&spa_namespace_avl, spa);
701 * Set the alternate root, if there is one.
704 spa->spa_root = spa_strdup(altroot);
709 * Every pool starts with the default cachefile
711 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
712 offsetof(spa_config_dirent_t, scd_link));
714 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
715 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
716 list_insert_head(&spa->spa_config_list, dp);
718 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
721 if (config != NULL) {
724 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
726 VERIFY(nvlist_dup(features, &spa->spa_label_features,
730 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
733 if (spa->spa_label_features == NULL) {
734 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
738 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0);
740 spa->spa_min_ashift = INT_MAX;
741 spa->spa_max_ashift = 0;
744 * As a pool is being created, treat all features as disabled by
745 * setting SPA_FEATURE_DISABLED for all entries in the feature
748 for (int i = 0; i < SPA_FEATURES; i++) {
749 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED;
756 * Removes a spa_t from the namespace, freeing up any memory used. Requires
757 * spa_namespace_lock. This is called only after the spa_t has been closed and
761 spa_remove(spa_t *spa)
763 spa_config_dirent_t *dp;
765 ASSERT(MUTEX_HELD(&spa_namespace_lock));
766 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
767 ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0);
769 nvlist_free(spa->spa_config_splitting);
771 avl_remove(&spa_namespace_avl, spa);
772 cv_broadcast(&spa_namespace_cv);
775 spa_strfree(spa->spa_root);
779 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
780 list_remove(&spa->spa_config_list, dp);
781 if (dp->scd_path != NULL)
782 spa_strfree(dp->scd_path);
783 kmem_free(dp, sizeof (spa_config_dirent_t));
786 list_destroy(&spa->spa_config_list);
788 nvlist_free(spa->spa_label_features);
789 nvlist_free(spa->spa_load_info);
790 spa_config_set(spa, NULL);
793 mutex_enter(&cpu_lock);
794 if (spa->spa_deadman_cycid != CYCLIC_NONE)
795 cyclic_remove(spa->spa_deadman_cycid);
796 mutex_exit(&cpu_lock);
797 spa->spa_deadman_cycid = CYCLIC_NONE;
800 callout_drain(&spa->spa_deadman_cycid);
804 refcount_destroy(&spa->spa_refcount);
806 spa_config_lock_destroy(spa);
808 for (int t = 0; t < TXG_SIZE; t++)
809 bplist_destroy(&spa->spa_free_bplist[t]);
811 zio_checksum_templates_free(spa);
813 cv_destroy(&spa->spa_async_cv);
814 cv_destroy(&spa->spa_evicting_os_cv);
815 cv_destroy(&spa->spa_proc_cv);
816 cv_destroy(&spa->spa_scrub_io_cv);
817 cv_destroy(&spa->spa_suspend_cv);
819 mutex_destroy(&spa->spa_async_lock);
820 mutex_destroy(&spa->spa_errlist_lock);
821 mutex_destroy(&spa->spa_errlog_lock);
822 mutex_destroy(&spa->spa_evicting_os_lock);
823 mutex_destroy(&spa->spa_history_lock);
824 mutex_destroy(&spa->spa_proc_lock);
825 mutex_destroy(&spa->spa_props_lock);
826 mutex_destroy(&spa->spa_cksum_tmpls_lock);
827 mutex_destroy(&spa->spa_scrub_lock);
828 mutex_destroy(&spa->spa_suspend_lock);
829 mutex_destroy(&spa->spa_vdev_top_lock);
831 kmem_free(spa, sizeof (spa_t));
835 * Given a pool, return the next pool in the namespace, or NULL if there is
836 * none. If 'prev' is NULL, return the first pool.
839 spa_next(spa_t *prev)
841 ASSERT(MUTEX_HELD(&spa_namespace_lock));
844 return (AVL_NEXT(&spa_namespace_avl, prev));
846 return (avl_first(&spa_namespace_avl));
850 * ==========================================================================
851 * SPA refcount functions
852 * ==========================================================================
856 * Add a reference to the given spa_t. Must have at least one reference, or
857 * have the namespace lock held.
860 spa_open_ref(spa_t *spa, void *tag)
862 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
863 MUTEX_HELD(&spa_namespace_lock));
864 (void) refcount_add(&spa->spa_refcount, tag);
868 * Remove a reference to the given spa_t. Must have at least one reference, or
869 * have the namespace lock held.
872 spa_close(spa_t *spa, void *tag)
874 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
875 MUTEX_HELD(&spa_namespace_lock));
876 (void) refcount_remove(&spa->spa_refcount, tag);
880 * Remove a reference to the given spa_t held by a dsl dir that is
881 * being asynchronously released. Async releases occur from a taskq
882 * performing eviction of dsl datasets and dirs. The namespace lock
883 * isn't held and the hold by the object being evicted may contribute to
884 * spa_minref (e.g. dataset or directory released during pool export),
885 * so the asserts in spa_close() do not apply.
888 spa_async_close(spa_t *spa, void *tag)
890 (void) refcount_remove(&spa->spa_refcount, tag);
894 * Check to see if the spa refcount is zero. Must be called with
895 * spa_namespace_lock held. We really compare against spa_minref, which is the
896 * number of references acquired when opening a pool
899 spa_refcount_zero(spa_t *spa)
901 ASSERT(MUTEX_HELD(&spa_namespace_lock));
903 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
907 * ==========================================================================
908 * SPA spare and l2cache tracking
909 * ==========================================================================
913 * Hot spares and cache devices are tracked using the same code below,
914 * for 'auxiliary' devices.
917 typedef struct spa_aux {
925 spa_aux_compare(const void *a, const void *b)
927 const spa_aux_t *sa = a;
928 const spa_aux_t *sb = b;
930 if (sa->aux_guid < sb->aux_guid)
932 else if (sa->aux_guid > sb->aux_guid)
939 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
945 search.aux_guid = vd->vdev_guid;
946 if ((aux = avl_find(avl, &search, &where)) != NULL) {
949 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
950 aux->aux_guid = vd->vdev_guid;
952 avl_insert(avl, aux, where);
957 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
963 search.aux_guid = vd->vdev_guid;
964 aux = avl_find(avl, &search, &where);
968 if (--aux->aux_count == 0) {
969 avl_remove(avl, aux);
970 kmem_free(aux, sizeof (spa_aux_t));
971 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
972 aux->aux_pool = 0ULL;
977 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
979 spa_aux_t search, *found;
981 search.aux_guid = guid;
982 found = avl_find(avl, &search, NULL);
986 *pool = found->aux_pool;
993 *refcnt = found->aux_count;
998 return (found != NULL);
1002 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
1004 spa_aux_t search, *found;
1007 search.aux_guid = vd->vdev_guid;
1008 found = avl_find(avl, &search, &where);
1009 ASSERT(found != NULL);
1010 ASSERT(found->aux_pool == 0ULL);
1012 found->aux_pool = spa_guid(vd->vdev_spa);
1016 * Spares are tracked globally due to the following constraints:
1018 * - A spare may be part of multiple pools.
1019 * - A spare may be added to a pool even if it's actively in use within
1021 * - A spare in use in any pool can only be the source of a replacement if
1022 * the target is a spare in the same pool.
1024 * We keep track of all spares on the system through the use of a reference
1025 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
1026 * spare, then we bump the reference count in the AVL tree. In addition, we set
1027 * the 'vdev_isspare' member to indicate that the device is a spare (active or
1028 * inactive). When a spare is made active (used to replace a device in the
1029 * pool), we also keep track of which pool its been made a part of.
1031 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
1032 * called under the spa_namespace lock as part of vdev reconfiguration. The
1033 * separate spare lock exists for the status query path, which does not need to
1034 * be completely consistent with respect to other vdev configuration changes.
1038 spa_spare_compare(const void *a, const void *b)
1040 return (spa_aux_compare(a, b));
1044 spa_spare_add(vdev_t *vd)
1046 mutex_enter(&spa_spare_lock);
1047 ASSERT(!vd->vdev_isspare);
1048 spa_aux_add(vd, &spa_spare_avl);
1049 vd->vdev_isspare = B_TRUE;
1050 mutex_exit(&spa_spare_lock);
1054 spa_spare_remove(vdev_t *vd)
1056 mutex_enter(&spa_spare_lock);
1057 ASSERT(vd->vdev_isspare);
1058 spa_aux_remove(vd, &spa_spare_avl);
1059 vd->vdev_isspare = B_FALSE;
1060 mutex_exit(&spa_spare_lock);
1064 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
1068 mutex_enter(&spa_spare_lock);
1069 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
1070 mutex_exit(&spa_spare_lock);
1076 spa_spare_activate(vdev_t *vd)
1078 mutex_enter(&spa_spare_lock);
1079 ASSERT(vd->vdev_isspare);
1080 spa_aux_activate(vd, &spa_spare_avl);
1081 mutex_exit(&spa_spare_lock);
1085 * Level 2 ARC devices are tracked globally for the same reasons as spares.
1086 * Cache devices currently only support one pool per cache device, and so
1087 * for these devices the aux reference count is currently unused beyond 1.
1091 spa_l2cache_compare(const void *a, const void *b)
1093 return (spa_aux_compare(a, b));
1097 spa_l2cache_add(vdev_t *vd)
1099 mutex_enter(&spa_l2cache_lock);
1100 ASSERT(!vd->vdev_isl2cache);
1101 spa_aux_add(vd, &spa_l2cache_avl);
1102 vd->vdev_isl2cache = B_TRUE;
1103 mutex_exit(&spa_l2cache_lock);
1107 spa_l2cache_remove(vdev_t *vd)
1109 mutex_enter(&spa_l2cache_lock);
1110 ASSERT(vd->vdev_isl2cache);
1111 spa_aux_remove(vd, &spa_l2cache_avl);
1112 vd->vdev_isl2cache = B_FALSE;
1113 mutex_exit(&spa_l2cache_lock);
1117 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
1121 mutex_enter(&spa_l2cache_lock);
1122 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
1123 mutex_exit(&spa_l2cache_lock);
1129 spa_l2cache_activate(vdev_t *vd)
1131 mutex_enter(&spa_l2cache_lock);
1132 ASSERT(vd->vdev_isl2cache);
1133 spa_aux_activate(vd, &spa_l2cache_avl);
1134 mutex_exit(&spa_l2cache_lock);
1138 * ==========================================================================
1140 * ==========================================================================
1144 * Lock the given spa_t for the purpose of adding or removing a vdev.
1145 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
1146 * It returns the next transaction group for the spa_t.
1149 spa_vdev_enter(spa_t *spa)
1151 mutex_enter(&spa->spa_vdev_top_lock);
1152 mutex_enter(&spa_namespace_lock);
1153 return (spa_vdev_config_enter(spa));
1157 * Internal implementation for spa_vdev_enter(). Used when a vdev
1158 * operation requires multiple syncs (i.e. removing a device) while
1159 * keeping the spa_namespace_lock held.
1162 spa_vdev_config_enter(spa_t *spa)
1164 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1166 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1168 return (spa_last_synced_txg(spa) + 1);
1172 * Used in combination with spa_vdev_config_enter() to allow the syncing
1173 * of multiple transactions without releasing the spa_namespace_lock.
1176 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
1178 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1180 int config_changed = B_FALSE;
1182 ASSERT(txg > spa_last_synced_txg(spa));
1184 spa->spa_pending_vdev = NULL;
1187 * Reassess the DTLs.
1189 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
1191 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
1192 config_changed = B_TRUE;
1193 spa->spa_config_generation++;
1197 * Verify the metaslab classes.
1199 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
1200 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
1202 spa_config_exit(spa, SCL_ALL, spa);
1205 * Panic the system if the specified tag requires it. This
1206 * is useful for ensuring that configurations are updated
1209 if (zio_injection_enabled)
1210 zio_handle_panic_injection(spa, tag, 0);
1213 * Note: this txg_wait_synced() is important because it ensures
1214 * that there won't be more than one config change per txg.
1215 * This allows us to use the txg as the generation number.
1218 txg_wait_synced(spa->spa_dsl_pool, txg);
1221 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL);
1222 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
1224 spa_config_exit(spa, SCL_ALL, spa);
1228 * If the config changed, update the config cache.
1231 spa_config_sync(spa, B_FALSE, B_TRUE);
1235 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
1236 * locking of spa_vdev_enter(), we also want make sure the transactions have
1237 * synced to disk, and then update the global configuration cache with the new
1241 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
1243 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
1244 mutex_exit(&spa_namespace_lock);
1245 mutex_exit(&spa->spa_vdev_top_lock);
1251 * Lock the given spa_t for the purpose of changing vdev state.
1254 spa_vdev_state_enter(spa_t *spa, int oplocks)
1256 int locks = SCL_STATE_ALL | oplocks;
1259 * Root pools may need to read of the underlying devfs filesystem
1260 * when opening up a vdev. Unfortunately if we're holding the
1261 * SCL_ZIO lock it will result in a deadlock when we try to issue
1262 * the read from the root filesystem. Instead we "prefetch"
1263 * the associated vnodes that we need prior to opening the
1264 * underlying devices and cache them so that we can prevent
1265 * any I/O when we are doing the actual open.
1267 if (spa_is_root(spa)) {
1268 int low = locks & ~(SCL_ZIO - 1);
1269 int high = locks & ~low;
1271 spa_config_enter(spa, high, spa, RW_WRITER);
1272 vdev_hold(spa->spa_root_vdev);
1273 spa_config_enter(spa, low, spa, RW_WRITER);
1275 spa_config_enter(spa, locks, spa, RW_WRITER);
1277 spa->spa_vdev_locks = locks;
1281 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1283 boolean_t config_changed = B_FALSE;
1285 if (vd != NULL || error == 0)
1286 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1290 vdev_state_dirty(vd->vdev_top);
1291 config_changed = B_TRUE;
1292 spa->spa_config_generation++;
1295 if (spa_is_root(spa))
1296 vdev_rele(spa->spa_root_vdev);
1298 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1299 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1302 * If anything changed, wait for it to sync. This ensures that,
1303 * from the system administrator's perspective, zpool(1M) commands
1304 * are synchronous. This is important for things like zpool offline:
1305 * when the command completes, you expect no further I/O from ZFS.
1308 txg_wait_synced(spa->spa_dsl_pool, 0);
1311 * If the config changed, update the config cache.
1313 if (config_changed) {
1314 mutex_enter(&spa_namespace_lock);
1315 spa_config_sync(spa, B_FALSE, B_TRUE);
1316 mutex_exit(&spa_namespace_lock);
1323 * ==========================================================================
1324 * Miscellaneous functions
1325 * ==========================================================================
1329 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx)
1331 if (!nvlist_exists(spa->spa_label_features, feature)) {
1332 fnvlist_add_boolean(spa->spa_label_features, feature);
1334 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't
1335 * dirty the vdev config because lock SCL_CONFIG is not held.
1336 * Thankfully, in this case we don't need to dirty the config
1337 * because it will be written out anyway when we finish
1338 * creating the pool.
1340 if (tx->tx_txg != TXG_INITIAL)
1341 vdev_config_dirty(spa->spa_root_vdev);
1346 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1348 if (nvlist_remove_all(spa->spa_label_features, feature) == 0)
1349 vdev_config_dirty(spa->spa_root_vdev);
1356 spa_rename(const char *name, const char *newname)
1362 * Lookup the spa_t and grab the config lock for writing. We need to
1363 * actually open the pool so that we can sync out the necessary labels.
1364 * It's OK to call spa_open() with the namespace lock held because we
1365 * allow recursive calls for other reasons.
1367 mutex_enter(&spa_namespace_lock);
1368 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1369 mutex_exit(&spa_namespace_lock);
1373 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1375 avl_remove(&spa_namespace_avl, spa);
1376 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1377 avl_add(&spa_namespace_avl, spa);
1380 * Sync all labels to disk with the new names by marking the root vdev
1381 * dirty and waiting for it to sync. It will pick up the new pool name
1384 vdev_config_dirty(spa->spa_root_vdev);
1386 spa_config_exit(spa, SCL_ALL, FTAG);
1388 txg_wait_synced(spa->spa_dsl_pool, 0);
1391 * Sync the updated config cache.
1393 spa_config_sync(spa, B_FALSE, B_TRUE);
1395 spa_close(spa, FTAG);
1397 mutex_exit(&spa_namespace_lock);
1403 * Return the spa_t associated with given pool_guid, if it exists. If
1404 * device_guid is non-zero, determine whether the pool exists *and* contains
1405 * a device with the specified device_guid.
1408 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1411 avl_tree_t *t = &spa_namespace_avl;
1413 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1415 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1416 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1418 if (spa->spa_root_vdev == NULL)
1420 if (spa_guid(spa) == pool_guid) {
1421 if (device_guid == 0)
1424 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1425 device_guid) != NULL)
1429 * Check any devices we may be in the process of adding.
1431 if (spa->spa_pending_vdev) {
1432 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1433 device_guid) != NULL)
1443 * Determine whether a pool with the given pool_guid exists.
1446 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1448 return (spa_by_guid(pool_guid, device_guid) != NULL);
1452 spa_strdup(const char *s)
1458 new = kmem_alloc(len + 1, KM_SLEEP);
1466 spa_strfree(char *s)
1468 kmem_free(s, strlen(s) + 1);
1472 spa_get_random(uint64_t range)
1478 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1484 spa_generate_guid(spa_t *spa)
1486 uint64_t guid = spa_get_random(-1ULL);
1489 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1490 guid = spa_get_random(-1ULL);
1492 while (guid == 0 || spa_guid_exists(guid, 0))
1493 guid = spa_get_random(-1ULL);
1500 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
1503 char *checksum = NULL;
1504 char *compress = NULL;
1507 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1508 dmu_object_byteswap_t bswap =
1509 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1510 (void) snprintf(type, sizeof (type), "bswap %s %s",
1511 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1512 "metadata" : "data",
1513 dmu_ot_byteswap[bswap].ob_name);
1515 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1518 if (!BP_IS_EMBEDDED(bp)) {
1520 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1522 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1525 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1530 spa_freeze(spa_t *spa)
1532 uint64_t freeze_txg = 0;
1534 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1535 if (spa->spa_freeze_txg == UINT64_MAX) {
1536 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1537 spa->spa_freeze_txg = freeze_txg;
1539 spa_config_exit(spa, SCL_ALL, FTAG);
1540 if (freeze_txg != 0)
1541 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1545 zfs_panic_recover(const char *fmt, ...)
1550 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1555 * This is a stripped-down version of strtoull, suitable only for converting
1556 * lowercase hexadecimal numbers that don't overflow.
1559 zfs_strtonum(const char *str, char **nptr)
1565 while ((c = *str) != '\0') {
1566 if (c >= '0' && c <= '9')
1568 else if (c >= 'a' && c <= 'f')
1569 digit = 10 + c - 'a';
1580 *nptr = (char *)str;
1586 * ==========================================================================
1587 * Accessor functions
1588 * ==========================================================================
1592 spa_shutting_down(spa_t *spa)
1594 return (spa->spa_async_suspended);
1598 spa_get_dsl(spa_t *spa)
1600 return (spa->spa_dsl_pool);
1604 spa_is_initializing(spa_t *spa)
1606 return (spa->spa_is_initializing);
1610 spa_get_rootblkptr(spa_t *spa)
1612 return (&spa->spa_ubsync.ub_rootbp);
1616 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1618 spa->spa_uberblock.ub_rootbp = *bp;
1622 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1624 if (spa->spa_root == NULL)
1627 (void) strncpy(buf, spa->spa_root, buflen);
1631 spa_sync_pass(spa_t *spa)
1633 return (spa->spa_sync_pass);
1637 spa_name(spa_t *spa)
1639 return (spa->spa_name);
1643 spa_guid(spa_t *spa)
1645 dsl_pool_t *dp = spa_get_dsl(spa);
1649 * If we fail to parse the config during spa_load(), we can go through
1650 * the error path (which posts an ereport) and end up here with no root
1651 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1654 if (spa->spa_root_vdev == NULL)
1655 return (spa->spa_config_guid);
1657 guid = spa->spa_last_synced_guid != 0 ?
1658 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1661 * Return the most recently synced out guid unless we're
1662 * in syncing context.
1664 if (dp && dsl_pool_sync_context(dp))
1665 return (spa->spa_root_vdev->vdev_guid);
1671 spa_load_guid(spa_t *spa)
1674 * This is a GUID that exists solely as a reference for the
1675 * purposes of the arc. It is generated at load time, and
1676 * is never written to persistent storage.
1678 return (spa->spa_load_guid);
1682 spa_last_synced_txg(spa_t *spa)
1684 return (spa->spa_ubsync.ub_txg);
1688 spa_first_txg(spa_t *spa)
1690 return (spa->spa_first_txg);
1694 spa_syncing_txg(spa_t *spa)
1696 return (spa->spa_syncing_txg);
1700 spa_state(spa_t *spa)
1702 return (spa->spa_state);
1706 spa_load_state(spa_t *spa)
1708 return (spa->spa_load_state);
1712 spa_freeze_txg(spa_t *spa)
1714 return (spa->spa_freeze_txg);
1719 spa_get_asize(spa_t *spa, uint64_t lsize)
1721 return (lsize * spa_asize_inflation);
1725 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%),
1728 * See the comment above spa_slop_shift for details.
1731 spa_get_slop_space(spa_t *spa) {
1732 uint64_t space = spa_get_dspace(spa);
1733 return (MAX(space >> spa_slop_shift, SPA_MINDEVSIZE >> 1));
1737 spa_get_dspace(spa_t *spa)
1739 return (spa->spa_dspace);
1743 spa_update_dspace(spa_t *spa)
1745 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1746 ddt_get_dedup_dspace(spa);
1750 * Return the failure mode that has been set to this pool. The default
1751 * behavior will be to block all I/Os when a complete failure occurs.
1754 spa_get_failmode(spa_t *spa)
1756 return (spa->spa_failmode);
1760 spa_suspended(spa_t *spa)
1762 return (spa->spa_suspended);
1766 spa_version(spa_t *spa)
1768 return (spa->spa_ubsync.ub_version);
1772 spa_deflate(spa_t *spa)
1774 return (spa->spa_deflate);
1778 spa_normal_class(spa_t *spa)
1780 return (spa->spa_normal_class);
1784 spa_log_class(spa_t *spa)
1786 return (spa->spa_log_class);
1790 spa_evicting_os_register(spa_t *spa, objset_t *os)
1792 mutex_enter(&spa->spa_evicting_os_lock);
1793 list_insert_head(&spa->spa_evicting_os_list, os);
1794 mutex_exit(&spa->spa_evicting_os_lock);
1798 spa_evicting_os_deregister(spa_t *spa, objset_t *os)
1800 mutex_enter(&spa->spa_evicting_os_lock);
1801 list_remove(&spa->spa_evicting_os_list, os);
1802 cv_broadcast(&spa->spa_evicting_os_cv);
1803 mutex_exit(&spa->spa_evicting_os_lock);
1807 spa_evicting_os_wait(spa_t *spa)
1809 mutex_enter(&spa->spa_evicting_os_lock);
1810 while (!list_is_empty(&spa->spa_evicting_os_list))
1811 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock);
1812 mutex_exit(&spa->spa_evicting_os_lock);
1814 dmu_buf_user_evict_wait();
1818 spa_max_replication(spa_t *spa)
1821 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1822 * handle BPs with more than one DVA allocated. Set our max
1823 * replication level accordingly.
1825 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1827 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1831 spa_prev_software_version(spa_t *spa)
1833 return (spa->spa_prev_software_version);
1837 spa_deadman_synctime(spa_t *spa)
1839 return (spa->spa_deadman_synctime);
1843 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1845 uint64_t asize = DVA_GET_ASIZE(dva);
1846 uint64_t dsize = asize;
1848 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1850 if (asize != 0 && spa->spa_deflate) {
1851 uint64_t vdev = DVA_GET_VDEV(dva);
1852 vdev_t *vd = vdev_lookup_top(spa, vdev);
1855 "dva_get_dsize_sync(): bad DVA %llu:%llu",
1856 (u_longlong_t)vdev, (u_longlong_t)asize);
1858 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1865 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1869 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1870 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1876 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1880 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1882 for (int d = 0; d < BP_GET_NDVAS(bp); d++)
1883 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1885 spa_config_exit(spa, SCL_VDEV, FTAG);
1891 * ==========================================================================
1892 * Initialization and Termination
1893 * ==========================================================================
1897 spa_name_compare(const void *a1, const void *a2)
1899 const spa_t *s1 = a1;
1900 const spa_t *s2 = a2;
1903 s = strcmp(s1->spa_name, s2->spa_name);
1914 return (spa_active_count);
1924 EVENTHANDLER_DEFINE(mountroot, spa_boot_init, NULL, 0);
1930 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1931 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1932 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1933 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1935 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1936 offsetof(spa_t, spa_avl));
1938 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1939 offsetof(spa_aux_t, aux_avl));
1941 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1942 offsetof(spa_aux_t, aux_avl));
1944 spa_mode_global = mode;
1950 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1951 arc_procfd = open("/proc/self/ctl", O_WRONLY);
1952 if (arc_procfd == -1) {
1953 perror("could not enable watchpoints: "
1954 "opening /proc/self/ctl failed: ");
1960 #endif /* illumos */
1968 vdev_cache_stat_init();
1971 zpool_feature_init();
1978 #endif /* !illumos */
1988 vdev_cache_stat_fini();
1997 avl_destroy(&spa_namespace_avl);
1998 avl_destroy(&spa_spare_avl);
1999 avl_destroy(&spa_l2cache_avl);
2001 cv_destroy(&spa_namespace_cv);
2002 mutex_destroy(&spa_namespace_lock);
2003 mutex_destroy(&spa_spare_lock);
2004 mutex_destroy(&spa_l2cache_lock);
2008 * Return whether this pool has slogs. No locking needed.
2009 * It's not a problem if the wrong answer is returned as it's only for
2010 * performance and not correctness
2013 spa_has_slogs(spa_t *spa)
2015 return (spa->spa_log_class->mc_rotor != NULL);
2019 spa_get_log_state(spa_t *spa)
2021 return (spa->spa_log_state);
2025 spa_set_log_state(spa_t *spa, spa_log_state_t state)
2027 spa->spa_log_state = state;
2031 spa_is_root(spa_t *spa)
2033 return (spa->spa_is_root);
2037 spa_writeable(spa_t *spa)
2039 return (!!(spa->spa_mode & FWRITE));
2043 * Returns true if there is a pending sync task in any of the current
2044 * syncing txg, the current quiescing txg, or the current open txg.
2047 spa_has_pending_synctask(spa_t *spa)
2049 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks));
2053 spa_mode(spa_t *spa)
2055 return (spa->spa_mode);
2059 spa_bootfs(spa_t *spa)
2061 return (spa->spa_bootfs);
2065 spa_delegation(spa_t *spa)
2067 return (spa->spa_delegation);
2071 spa_meta_objset(spa_t *spa)
2073 return (spa->spa_meta_objset);
2077 spa_dedup_checksum(spa_t *spa)
2079 return (spa->spa_dedup_checksum);
2083 * Reset pool scan stat per scan pass (or reboot).
2086 spa_scan_stat_init(spa_t *spa)
2088 /* data not stored on disk */
2089 spa->spa_scan_pass_start = gethrestime_sec();
2090 spa->spa_scan_pass_exam = 0;
2091 vdev_scan_stat_init(spa->spa_root_vdev);
2095 * Get scan stats for zpool status reports
2098 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
2100 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
2102 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
2103 return (SET_ERROR(ENOENT));
2104 bzero(ps, sizeof (pool_scan_stat_t));
2106 /* data stored on disk */
2107 ps->pss_func = scn->scn_phys.scn_func;
2108 ps->pss_start_time = scn->scn_phys.scn_start_time;
2109 ps->pss_end_time = scn->scn_phys.scn_end_time;
2110 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
2111 ps->pss_examined = scn->scn_phys.scn_examined;
2112 ps->pss_to_process = scn->scn_phys.scn_to_process;
2113 ps->pss_processed = scn->scn_phys.scn_processed;
2114 ps->pss_errors = scn->scn_phys.scn_errors;
2115 ps->pss_state = scn->scn_phys.scn_state;
2117 /* data not stored on disk */
2118 ps->pss_pass_start = spa->spa_scan_pass_start;
2119 ps->pss_pass_exam = spa->spa_scan_pass_exam;
2125 spa_debug_enabled(spa_t *spa)
2127 return (spa->spa_debug);
2131 spa_maxblocksize(spa_t *spa)
2133 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
2134 return (SPA_MAXBLOCKSIZE);
2136 return (SPA_OLD_MAXBLOCKSIZE);