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) 2012 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 #include <sys/zfs_context.h>
28 #include <sys/spa_impl.h>
30 #include <sys/zio_checksum.h>
31 #include <sys/zio_compress.h>
33 #include <sys/dmu_tx.h>
36 #include <sys/vdev_impl.h>
37 #include <sys/metaslab.h>
38 #include <sys/uberblock_impl.h>
41 #include <sys/unique.h>
42 #include <sys/dsl_pool.h>
43 #include <sys/dsl_dir.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dsl_scan.h>
46 #include <sys/fs/zfs.h>
47 #include <sys/metaslab_impl.h>
51 #include "zfeature_common.h"
56 * There are four basic locks for managing spa_t structures:
58 * spa_namespace_lock (global mutex)
60 * This lock must be acquired to do any of the following:
62 * - Lookup a spa_t by name
63 * - Add or remove a spa_t from the namespace
64 * - Increase spa_refcount from non-zero
65 * - Check if spa_refcount is zero
67 * - add/remove/attach/detach devices
68 * - Held for the duration of create/destroy/import/export
70 * It does not need to handle recursion. A create or destroy may
71 * reference objects (files or zvols) in other pools, but by
72 * definition they must have an existing reference, and will never need
73 * to lookup a spa_t by name.
75 * spa_refcount (per-spa refcount_t protected by mutex)
77 * This reference count keep track of any active users of the spa_t. The
78 * spa_t cannot be destroyed or freed while this is non-zero. Internally,
79 * the refcount is never really 'zero' - opening a pool implicitly keeps
80 * some references in the DMU. Internally we check against spa_minref, but
81 * present the image of a zero/non-zero value to consumers.
83 * spa_config_lock[] (per-spa array of rwlocks)
85 * This protects the spa_t from config changes, and must be held in
86 * the following circumstances:
88 * - RW_READER to perform I/O to the spa
89 * - RW_WRITER to change the vdev config
91 * The locking order is fairly straightforward:
93 * spa_namespace_lock -> spa_refcount
95 * The namespace lock must be acquired to increase the refcount from 0
96 * or to check if it is zero.
98 * spa_refcount -> spa_config_lock[]
100 * There must be at least one valid reference on the spa_t to acquire
103 * spa_namespace_lock -> spa_config_lock[]
105 * The namespace lock must always be taken before the config lock.
108 * The spa_namespace_lock can be acquired directly and is globally visible.
110 * The namespace is manipulated using the following functions, all of which
111 * require the spa_namespace_lock to be held.
113 * spa_lookup() Lookup a spa_t by name.
115 * spa_add() Create a new spa_t in the namespace.
117 * spa_remove() Remove a spa_t from the namespace. This also
118 * frees up any memory associated with the spa_t.
120 * spa_next() Returns the next spa_t in the system, or the
121 * first if NULL is passed.
123 * spa_evict_all() Shutdown and remove all spa_t structures in
126 * spa_guid_exists() Determine whether a pool/device guid exists.
128 * The spa_refcount is manipulated using the following functions:
130 * spa_open_ref() Adds a reference to the given spa_t. Must be
131 * called with spa_namespace_lock held if the
132 * refcount is currently zero.
134 * spa_close() Remove a reference from the spa_t. This will
135 * not free the spa_t or remove it from the
136 * namespace. No locking is required.
138 * spa_refcount_zero() Returns true if the refcount is currently
139 * zero. Must be called with spa_namespace_lock
142 * The spa_config_lock[] is an array of rwlocks, ordered as follows:
143 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV.
144 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}().
146 * To read the configuration, it suffices to hold one of these locks as reader.
147 * To modify the configuration, you must hold all locks as writer. To modify
148 * vdev state without altering the vdev tree's topology (e.g. online/offline),
149 * you must hold SCL_STATE and SCL_ZIO as writer.
151 * We use these distinct config locks to avoid recursive lock entry.
152 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces
153 * block allocations (SCL_ALLOC), which may require reading space maps
154 * from disk (dmu_read() -> zio_read() -> SCL_ZIO).
156 * The spa config locks cannot be normal rwlocks because we need the
157 * ability to hand off ownership. For example, SCL_ZIO is acquired
158 * by the issuing thread and later released by an interrupt thread.
159 * They do, however, obey the usual write-wanted semantics to prevent
160 * writer (i.e. system administrator) starvation.
162 * The lock acquisition rules are as follows:
165 * Protects changes to the vdev tree topology, such as vdev
166 * add/remove/attach/detach. Protects the dirty config list
167 * (spa_config_dirty_list) and the set of spares and l2arc devices.
170 * Protects changes to pool state and vdev state, such as vdev
171 * online/offline/fault/degrade/clear. Protects the dirty state list
172 * (spa_state_dirty_list) and global pool state (spa_state).
175 * Protects changes to metaslab groups and classes.
176 * Held as reader by metaslab_alloc() and metaslab_claim().
179 * Held by bp-level zios (those which have no io_vd upon entry)
180 * to prevent changes to the vdev tree. The bp-level zio implicitly
181 * protects all of its vdev child zios, which do not hold SCL_ZIO.
184 * Protects changes to metaslab groups and classes.
185 * Held as reader by metaslab_free(). SCL_FREE is distinct from
186 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free
187 * blocks in zio_done() while another i/o that holds either
188 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete.
191 * Held as reader to prevent changes to the vdev tree during trivial
192 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the
193 * other locks, and lower than all of them, to ensure that it's safe
194 * to acquire regardless of caller context.
196 * In addition, the following rules apply:
198 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list.
199 * The lock ordering is SCL_CONFIG > spa_props_lock.
201 * (b) I/O operations on leaf vdevs. For any zio operation that takes
202 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(),
203 * or zio_write_phys() -- the caller must ensure that the config cannot
204 * cannot change in the interim, and that the vdev cannot be reopened.
205 * SCL_STATE as reader suffices for both.
207 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit().
209 * spa_vdev_enter() Acquire the namespace lock and the config lock
212 * spa_vdev_exit() Release the config lock, wait for all I/O
213 * to complete, sync the updated configs to the
214 * cache, and release the namespace lock.
216 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit().
217 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual
218 * locking is, always, based on spa_namespace_lock and spa_config_lock[].
220 * spa_rename() is also implemented within this file since it requires
221 * manipulation of the namespace.
224 static avl_tree_t spa_namespace_avl;
225 kmutex_t spa_namespace_lock;
226 static kcondvar_t spa_namespace_cv;
227 static int spa_active_count;
228 int spa_max_replication_override = SPA_DVAS_PER_BP;
230 static kmutex_t spa_spare_lock;
231 static avl_tree_t spa_spare_avl;
232 static kmutex_t spa_l2cache_lock;
233 static avl_tree_t spa_l2cache_avl;
235 kmem_cache_t *spa_buffer_pool;
239 /* Everything except dprintf is on by default in debug builds */
240 int zfs_flags = ~ZFS_DEBUG_DPRINTF;
246 * zfs_recover can be set to nonzero to attempt to recover from
247 * otherwise-fatal errors, typically caused by on-disk corruption. When
248 * set, calls to zfs_panic_recover() will turn into warning messages.
251 SYSCTL_DECL(_vfs_zfs);
252 TUNABLE_INT("vfs.zfs.recover", &zfs_recover);
253 SYSCTL_INT(_vfs_zfs, OID_AUTO, recover, CTLFLAG_RDTUN, &zfs_recover, 0,
254 "Try to recover from otherwise-fatal errors.");
258 * ==========================================================================
260 * ==========================================================================
263 spa_config_lock_init(spa_t *spa)
265 for (int i = 0; i < SCL_LOCKS; i++) {
266 spa_config_lock_t *scl = &spa->spa_config_lock[i];
267 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL);
268 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL);
269 refcount_create(&scl->scl_count);
270 scl->scl_writer = NULL;
271 scl->scl_write_wanted = 0;
276 spa_config_lock_destroy(spa_t *spa)
278 for (int i = 0; i < SCL_LOCKS; i++) {
279 spa_config_lock_t *scl = &spa->spa_config_lock[i];
280 mutex_destroy(&scl->scl_lock);
281 cv_destroy(&scl->scl_cv);
282 refcount_destroy(&scl->scl_count);
283 ASSERT(scl->scl_writer == NULL);
284 ASSERT(scl->scl_write_wanted == 0);
289 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw)
291 for (int i = 0; i < SCL_LOCKS; i++) {
292 spa_config_lock_t *scl = &spa->spa_config_lock[i];
293 if (!(locks & (1 << i)))
295 mutex_enter(&scl->scl_lock);
296 if (rw == RW_READER) {
297 if (scl->scl_writer || scl->scl_write_wanted) {
298 mutex_exit(&scl->scl_lock);
299 spa_config_exit(spa, locks ^ (1 << i), tag);
303 ASSERT(scl->scl_writer != curthread);
304 if (!refcount_is_zero(&scl->scl_count)) {
305 mutex_exit(&scl->scl_lock);
306 spa_config_exit(spa, locks ^ (1 << i), tag);
309 scl->scl_writer = curthread;
311 (void) refcount_add(&scl->scl_count, tag);
312 mutex_exit(&scl->scl_lock);
318 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw)
322 for (int i = 0; i < SCL_LOCKS; i++) {
323 spa_config_lock_t *scl = &spa->spa_config_lock[i];
324 if (scl->scl_writer == curthread)
325 wlocks_held |= (1 << i);
326 if (!(locks & (1 << i)))
328 mutex_enter(&scl->scl_lock);
329 if (rw == RW_READER) {
330 while (scl->scl_writer || scl->scl_write_wanted) {
331 cv_wait(&scl->scl_cv, &scl->scl_lock);
334 ASSERT(scl->scl_writer != curthread);
335 while (!refcount_is_zero(&scl->scl_count)) {
336 scl->scl_write_wanted++;
337 cv_wait(&scl->scl_cv, &scl->scl_lock);
338 scl->scl_write_wanted--;
340 scl->scl_writer = curthread;
342 (void) refcount_add(&scl->scl_count, tag);
343 mutex_exit(&scl->scl_lock);
345 ASSERT(wlocks_held <= locks);
349 spa_config_exit(spa_t *spa, int locks, void *tag)
351 for (int i = SCL_LOCKS - 1; i >= 0; i--) {
352 spa_config_lock_t *scl = &spa->spa_config_lock[i];
353 if (!(locks & (1 << i)))
355 mutex_enter(&scl->scl_lock);
356 ASSERT(!refcount_is_zero(&scl->scl_count));
357 if (refcount_remove(&scl->scl_count, tag) == 0) {
358 ASSERT(scl->scl_writer == NULL ||
359 scl->scl_writer == curthread);
360 scl->scl_writer = NULL; /* OK in either case */
361 cv_broadcast(&scl->scl_cv);
363 mutex_exit(&scl->scl_lock);
368 spa_config_held(spa_t *spa, int locks, krw_t rw)
372 for (int i = 0; i < SCL_LOCKS; i++) {
373 spa_config_lock_t *scl = &spa->spa_config_lock[i];
374 if (!(locks & (1 << i)))
376 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) ||
377 (rw == RW_WRITER && scl->scl_writer == curthread))
378 locks_held |= 1 << i;
385 * ==========================================================================
386 * SPA namespace functions
387 * ==========================================================================
391 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held.
392 * Returns NULL if no matching spa_t is found.
395 spa_lookup(const char *name)
397 static spa_t search; /* spa_t is large; don't allocate on stack */
403 ASSERT(MUTEX_HELD(&spa_namespace_lock));
406 * If it's a full dataset name, figure out the pool name and
409 cp = strpbrk(name, "/@");
415 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name));
416 spa = avl_find(&spa_namespace_avl, &search, &where);
425 * Create an uninitialized spa_t with the given name. Requires
426 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already
427 * exist by calling spa_lookup() first.
430 spa_add(const char *name, nvlist_t *config, const char *altroot)
433 spa_config_dirent_t *dp;
435 ASSERT(MUTEX_HELD(&spa_namespace_lock));
437 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP);
439 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL);
440 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL);
441 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL);
442 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL);
443 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL);
444 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL);
445 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL);
446 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL);
447 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL);
449 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL);
450 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL);
451 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL);
452 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL);
454 for (int t = 0; t < TXG_SIZE; t++)
455 bplist_create(&spa->spa_free_bplist[t]);
457 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name));
458 spa->spa_state = POOL_STATE_UNINITIALIZED;
459 spa->spa_freeze_txg = UINT64_MAX;
460 spa->spa_final_txg = UINT64_MAX;
461 spa->spa_load_max_txg = UINT64_MAX;
463 spa->spa_proc_state = SPA_PROC_NONE;
465 refcount_create(&spa->spa_refcount);
466 spa_config_lock_init(spa);
468 avl_add(&spa_namespace_avl, spa);
471 * Set the alternate root, if there is one.
474 spa->spa_root = spa_strdup(altroot);
479 * Every pool starts with the default cachefile
481 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t),
482 offsetof(spa_config_dirent_t, scd_link));
484 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP);
485 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path);
486 list_insert_head(&spa->spa_config_list, dp);
488 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME,
491 if (config != NULL) {
494 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
496 VERIFY(nvlist_dup(features, &spa->spa_label_features,
500 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0);
503 if (spa->spa_label_features == NULL) {
504 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME,
512 * Removes a spa_t from the namespace, freeing up any memory used. Requires
513 * spa_namespace_lock. This is called only after the spa_t has been closed and
517 spa_remove(spa_t *spa)
519 spa_config_dirent_t *dp;
521 ASSERT(MUTEX_HELD(&spa_namespace_lock));
522 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
524 nvlist_free(spa->spa_config_splitting);
526 avl_remove(&spa_namespace_avl, spa);
527 cv_broadcast(&spa_namespace_cv);
530 spa_strfree(spa->spa_root);
534 while ((dp = list_head(&spa->spa_config_list)) != NULL) {
535 list_remove(&spa->spa_config_list, dp);
536 if (dp->scd_path != NULL)
537 spa_strfree(dp->scd_path);
538 kmem_free(dp, sizeof (spa_config_dirent_t));
541 list_destroy(&spa->spa_config_list);
543 nvlist_free(spa->spa_label_features);
544 nvlist_free(spa->spa_load_info);
545 spa_config_set(spa, NULL);
547 refcount_destroy(&spa->spa_refcount);
549 spa_config_lock_destroy(spa);
551 for (int t = 0; t < TXG_SIZE; t++)
552 bplist_destroy(&spa->spa_free_bplist[t]);
554 cv_destroy(&spa->spa_async_cv);
555 cv_destroy(&spa->spa_proc_cv);
556 cv_destroy(&spa->spa_scrub_io_cv);
557 cv_destroy(&spa->spa_suspend_cv);
559 mutex_destroy(&spa->spa_async_lock);
560 mutex_destroy(&spa->spa_errlist_lock);
561 mutex_destroy(&spa->spa_errlog_lock);
562 mutex_destroy(&spa->spa_history_lock);
563 mutex_destroy(&spa->spa_proc_lock);
564 mutex_destroy(&spa->spa_props_lock);
565 mutex_destroy(&spa->spa_scrub_lock);
566 mutex_destroy(&spa->spa_suspend_lock);
567 mutex_destroy(&spa->spa_vdev_top_lock);
569 kmem_free(spa, sizeof (spa_t));
573 * Given a pool, return the next pool in the namespace, or NULL if there is
574 * none. If 'prev' is NULL, return the first pool.
577 spa_next(spa_t *prev)
579 ASSERT(MUTEX_HELD(&spa_namespace_lock));
582 return (AVL_NEXT(&spa_namespace_avl, prev));
584 return (avl_first(&spa_namespace_avl));
588 * ==========================================================================
589 * SPA refcount functions
590 * ==========================================================================
594 * Add a reference to the given spa_t. Must have at least one reference, or
595 * have the namespace lock held.
598 spa_open_ref(spa_t *spa, void *tag)
600 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref ||
601 MUTEX_HELD(&spa_namespace_lock));
602 (void) refcount_add(&spa->spa_refcount, tag);
606 * Remove a reference to the given spa_t. Must have at least one reference, or
607 * have the namespace lock held.
610 spa_close(spa_t *spa, void *tag)
612 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref ||
613 MUTEX_HELD(&spa_namespace_lock));
614 (void) refcount_remove(&spa->spa_refcount, tag);
618 * Check to see if the spa refcount is zero. Must be called with
619 * spa_namespace_lock held. We really compare against spa_minref, which is the
620 * number of references acquired when opening a pool
623 spa_refcount_zero(spa_t *spa)
625 ASSERT(MUTEX_HELD(&spa_namespace_lock));
627 return (refcount_count(&spa->spa_refcount) == spa->spa_minref);
631 * ==========================================================================
632 * SPA spare and l2cache tracking
633 * ==========================================================================
637 * Hot spares and cache devices are tracked using the same code below,
638 * for 'auxiliary' devices.
641 typedef struct spa_aux {
649 spa_aux_compare(const void *a, const void *b)
651 const spa_aux_t *sa = a;
652 const spa_aux_t *sb = b;
654 if (sa->aux_guid < sb->aux_guid)
656 else if (sa->aux_guid > sb->aux_guid)
663 spa_aux_add(vdev_t *vd, avl_tree_t *avl)
669 search.aux_guid = vd->vdev_guid;
670 if ((aux = avl_find(avl, &search, &where)) != NULL) {
673 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP);
674 aux->aux_guid = vd->vdev_guid;
676 avl_insert(avl, aux, where);
681 spa_aux_remove(vdev_t *vd, avl_tree_t *avl)
687 search.aux_guid = vd->vdev_guid;
688 aux = avl_find(avl, &search, &where);
692 if (--aux->aux_count == 0) {
693 avl_remove(avl, aux);
694 kmem_free(aux, sizeof (spa_aux_t));
695 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) {
696 aux->aux_pool = 0ULL;
701 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl)
703 spa_aux_t search, *found;
705 search.aux_guid = guid;
706 found = avl_find(avl, &search, NULL);
710 *pool = found->aux_pool;
717 *refcnt = found->aux_count;
722 return (found != NULL);
726 spa_aux_activate(vdev_t *vd, avl_tree_t *avl)
728 spa_aux_t search, *found;
731 search.aux_guid = vd->vdev_guid;
732 found = avl_find(avl, &search, &where);
733 ASSERT(found != NULL);
734 ASSERT(found->aux_pool == 0ULL);
736 found->aux_pool = spa_guid(vd->vdev_spa);
740 * Spares are tracked globally due to the following constraints:
742 * - A spare may be part of multiple pools.
743 * - A spare may be added to a pool even if it's actively in use within
745 * - A spare in use in any pool can only be the source of a replacement if
746 * the target is a spare in the same pool.
748 * We keep track of all spares on the system through the use of a reference
749 * counted AVL tree. When a vdev is added as a spare, or used as a replacement
750 * spare, then we bump the reference count in the AVL tree. In addition, we set
751 * the 'vdev_isspare' member to indicate that the device is a spare (active or
752 * inactive). When a spare is made active (used to replace a device in the
753 * pool), we also keep track of which pool its been made a part of.
755 * The 'spa_spare_lock' protects the AVL tree. These functions are normally
756 * called under the spa_namespace lock as part of vdev reconfiguration. The
757 * separate spare lock exists for the status query path, which does not need to
758 * be completely consistent with respect to other vdev configuration changes.
762 spa_spare_compare(const void *a, const void *b)
764 return (spa_aux_compare(a, b));
768 spa_spare_add(vdev_t *vd)
770 mutex_enter(&spa_spare_lock);
771 ASSERT(!vd->vdev_isspare);
772 spa_aux_add(vd, &spa_spare_avl);
773 vd->vdev_isspare = B_TRUE;
774 mutex_exit(&spa_spare_lock);
778 spa_spare_remove(vdev_t *vd)
780 mutex_enter(&spa_spare_lock);
781 ASSERT(vd->vdev_isspare);
782 spa_aux_remove(vd, &spa_spare_avl);
783 vd->vdev_isspare = B_FALSE;
784 mutex_exit(&spa_spare_lock);
788 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt)
792 mutex_enter(&spa_spare_lock);
793 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl);
794 mutex_exit(&spa_spare_lock);
800 spa_spare_activate(vdev_t *vd)
802 mutex_enter(&spa_spare_lock);
803 ASSERT(vd->vdev_isspare);
804 spa_aux_activate(vd, &spa_spare_avl);
805 mutex_exit(&spa_spare_lock);
809 * Level 2 ARC devices are tracked globally for the same reasons as spares.
810 * Cache devices currently only support one pool per cache device, and so
811 * for these devices the aux reference count is currently unused beyond 1.
815 spa_l2cache_compare(const void *a, const void *b)
817 return (spa_aux_compare(a, b));
821 spa_l2cache_add(vdev_t *vd)
823 mutex_enter(&spa_l2cache_lock);
824 ASSERT(!vd->vdev_isl2cache);
825 spa_aux_add(vd, &spa_l2cache_avl);
826 vd->vdev_isl2cache = B_TRUE;
827 mutex_exit(&spa_l2cache_lock);
831 spa_l2cache_remove(vdev_t *vd)
833 mutex_enter(&spa_l2cache_lock);
834 ASSERT(vd->vdev_isl2cache);
835 spa_aux_remove(vd, &spa_l2cache_avl);
836 vd->vdev_isl2cache = B_FALSE;
837 mutex_exit(&spa_l2cache_lock);
841 spa_l2cache_exists(uint64_t guid, uint64_t *pool)
845 mutex_enter(&spa_l2cache_lock);
846 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl);
847 mutex_exit(&spa_l2cache_lock);
853 spa_l2cache_activate(vdev_t *vd)
855 mutex_enter(&spa_l2cache_lock);
856 ASSERT(vd->vdev_isl2cache);
857 spa_aux_activate(vd, &spa_l2cache_avl);
858 mutex_exit(&spa_l2cache_lock);
862 * ==========================================================================
864 * ==========================================================================
868 * Lock the given spa_t for the purpose of adding or removing a vdev.
869 * Grabs the global spa_namespace_lock plus the spa config lock for writing.
870 * It returns the next transaction group for the spa_t.
873 spa_vdev_enter(spa_t *spa)
875 mutex_enter(&spa->spa_vdev_top_lock);
876 mutex_enter(&spa_namespace_lock);
877 return (spa_vdev_config_enter(spa));
881 * Internal implementation for spa_vdev_enter(). Used when a vdev
882 * operation requires multiple syncs (i.e. removing a device) while
883 * keeping the spa_namespace_lock held.
886 spa_vdev_config_enter(spa_t *spa)
888 ASSERT(MUTEX_HELD(&spa_namespace_lock));
890 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
892 return (spa_last_synced_txg(spa) + 1);
896 * Used in combination with spa_vdev_config_enter() to allow the syncing
897 * of multiple transactions without releasing the spa_namespace_lock.
900 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag)
902 ASSERT(MUTEX_HELD(&spa_namespace_lock));
904 int config_changed = B_FALSE;
906 ASSERT(txg > spa_last_synced_txg(spa));
908 spa->spa_pending_vdev = NULL;
913 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE);
915 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) {
916 config_changed = B_TRUE;
917 spa->spa_config_generation++;
921 * Verify the metaslab classes.
923 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0);
924 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0);
926 spa_config_exit(spa, SCL_ALL, spa);
929 * Panic the system if the specified tag requires it. This
930 * is useful for ensuring that configurations are updated
933 if (zio_injection_enabled)
934 zio_handle_panic_injection(spa, tag, 0);
937 * Note: this txg_wait_synced() is important because it ensures
938 * that there won't be more than one config change per txg.
939 * This allows us to use the txg as the generation number.
942 txg_wait_synced(spa->spa_dsl_pool, txg);
945 ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0);
946 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER);
948 spa_config_exit(spa, SCL_ALL, spa);
952 * If the config changed, update the config cache.
955 spa_config_sync(spa, B_FALSE, B_TRUE);
959 * Unlock the spa_t after adding or removing a vdev. Besides undoing the
960 * locking of spa_vdev_enter(), we also want make sure the transactions have
961 * synced to disk, and then update the global configuration cache with the new
965 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error)
967 spa_vdev_config_exit(spa, vd, txg, error, FTAG);
968 mutex_exit(&spa_namespace_lock);
969 mutex_exit(&spa->spa_vdev_top_lock);
975 * Lock the given spa_t for the purpose of changing vdev state.
978 spa_vdev_state_enter(spa_t *spa, int oplocks)
980 int locks = SCL_STATE_ALL | oplocks;
983 * Root pools may need to read of the underlying devfs filesystem
984 * when opening up a vdev. Unfortunately if we're holding the
985 * SCL_ZIO lock it will result in a deadlock when we try to issue
986 * the read from the root filesystem. Instead we "prefetch"
987 * the associated vnodes that we need prior to opening the
988 * underlying devices and cache them so that we can prevent
989 * any I/O when we are doing the actual open.
991 if (spa_is_root(spa)) {
992 int low = locks & ~(SCL_ZIO - 1);
993 int high = locks & ~low;
995 spa_config_enter(spa, high, spa, RW_WRITER);
996 vdev_hold(spa->spa_root_vdev);
997 spa_config_enter(spa, low, spa, RW_WRITER);
999 spa_config_enter(spa, locks, spa, RW_WRITER);
1001 spa->spa_vdev_locks = locks;
1005 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error)
1007 boolean_t config_changed = B_FALSE;
1009 if (vd != NULL || error == 0)
1010 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev,
1014 vdev_state_dirty(vd->vdev_top);
1015 config_changed = B_TRUE;
1016 spa->spa_config_generation++;
1019 if (spa_is_root(spa))
1020 vdev_rele(spa->spa_root_vdev);
1022 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL);
1023 spa_config_exit(spa, spa->spa_vdev_locks, spa);
1026 * If anything changed, wait for it to sync. This ensures that,
1027 * from the system administrator's perspective, zpool(1M) commands
1028 * are synchronous. This is important for things like zpool offline:
1029 * when the command completes, you expect no further I/O from ZFS.
1032 txg_wait_synced(spa->spa_dsl_pool, 0);
1035 * If the config changed, update the config cache.
1037 if (config_changed) {
1038 mutex_enter(&spa_namespace_lock);
1039 spa_config_sync(spa, B_FALSE, B_TRUE);
1040 mutex_exit(&spa_namespace_lock);
1047 * ==========================================================================
1048 * Miscellaneous functions
1049 * ==========================================================================
1053 spa_activate_mos_feature(spa_t *spa, const char *feature)
1055 (void) nvlist_add_boolean(spa->spa_label_features, feature);
1056 vdev_config_dirty(spa->spa_root_vdev);
1060 spa_deactivate_mos_feature(spa_t *spa, const char *feature)
1062 (void) nvlist_remove_all(spa->spa_label_features, feature);
1063 vdev_config_dirty(spa->spa_root_vdev);
1070 spa_rename(const char *name, const char *newname)
1076 * Lookup the spa_t and grab the config lock for writing. We need to
1077 * actually open the pool so that we can sync out the necessary labels.
1078 * It's OK to call spa_open() with the namespace lock held because we
1079 * allow recursive calls for other reasons.
1081 mutex_enter(&spa_namespace_lock);
1082 if ((err = spa_open(name, &spa, FTAG)) != 0) {
1083 mutex_exit(&spa_namespace_lock);
1087 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1089 avl_remove(&spa_namespace_avl, spa);
1090 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name));
1091 avl_add(&spa_namespace_avl, spa);
1094 * Sync all labels to disk with the new names by marking the root vdev
1095 * dirty and waiting for it to sync. It will pick up the new pool name
1098 vdev_config_dirty(spa->spa_root_vdev);
1100 spa_config_exit(spa, SCL_ALL, FTAG);
1102 txg_wait_synced(spa->spa_dsl_pool, 0);
1105 * Sync the updated config cache.
1107 spa_config_sync(spa, B_FALSE, B_TRUE);
1109 spa_close(spa, FTAG);
1111 mutex_exit(&spa_namespace_lock);
1117 * Return the spa_t associated with given pool_guid, if it exists. If
1118 * device_guid is non-zero, determine whether the pool exists *and* contains
1119 * a device with the specified device_guid.
1122 spa_by_guid(uint64_t pool_guid, uint64_t device_guid)
1125 avl_tree_t *t = &spa_namespace_avl;
1127 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1129 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) {
1130 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1132 if (spa->spa_root_vdev == NULL)
1134 if (spa_guid(spa) == pool_guid) {
1135 if (device_guid == 0)
1138 if (vdev_lookup_by_guid(spa->spa_root_vdev,
1139 device_guid) != NULL)
1143 * Check any devices we may be in the process of adding.
1145 if (spa->spa_pending_vdev) {
1146 if (vdev_lookup_by_guid(spa->spa_pending_vdev,
1147 device_guid) != NULL)
1157 * Determine whether a pool with the given pool_guid exists.
1160 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid)
1162 return (spa_by_guid(pool_guid, device_guid) != NULL);
1166 spa_strdup(const char *s)
1172 new = kmem_alloc(len + 1, KM_SLEEP);
1180 spa_strfree(char *s)
1182 kmem_free(s, strlen(s) + 1);
1186 spa_get_random(uint64_t range)
1192 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t));
1198 spa_generate_guid(spa_t *spa)
1200 uint64_t guid = spa_get_random(-1ULL);
1203 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid))
1204 guid = spa_get_random(-1ULL);
1206 while (guid == 0 || spa_guid_exists(guid, 0))
1207 guid = spa_get_random(-1ULL);
1214 sprintf_blkptr(char *buf, const blkptr_t *bp)
1217 char *checksum = NULL;
1218 char *compress = NULL;
1221 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) {
1222 dmu_object_byteswap_t bswap =
1223 DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
1224 (void) snprintf(type, sizeof (type), "bswap %s %s",
1225 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ?
1226 "metadata" : "data",
1227 dmu_ot_byteswap[bswap].ob_name);
1229 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name,
1232 checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name;
1233 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
1236 SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress);
1240 spa_freeze(spa_t *spa)
1242 uint64_t freeze_txg = 0;
1244 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1245 if (spa->spa_freeze_txg == UINT64_MAX) {
1246 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE;
1247 spa->spa_freeze_txg = freeze_txg;
1249 spa_config_exit(spa, SCL_ALL, FTAG);
1250 if (freeze_txg != 0)
1251 txg_wait_synced(spa_get_dsl(spa), freeze_txg);
1255 zfs_panic_recover(const char *fmt, ...)
1260 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx);
1265 * This is a stripped-down version of strtoull, suitable only for converting
1266 * lowercase hexidecimal numbers that don't overflow.
1269 zfs_strtonum(const char *str, char **nptr)
1275 while ((c = *str) != '\0') {
1276 if (c >= '0' && c <= '9')
1278 else if (c >= 'a' && c <= 'f')
1279 digit = 10 + c - 'a';
1290 *nptr = (char *)str;
1296 * ==========================================================================
1297 * Accessor functions
1298 * ==========================================================================
1302 spa_shutting_down(spa_t *spa)
1304 return (spa->spa_async_suspended);
1308 spa_get_dsl(spa_t *spa)
1310 return (spa->spa_dsl_pool);
1314 spa_is_initializing(spa_t *spa)
1316 return (spa->spa_is_initializing);
1320 spa_get_rootblkptr(spa_t *spa)
1322 return (&spa->spa_ubsync.ub_rootbp);
1326 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp)
1328 spa->spa_uberblock.ub_rootbp = *bp;
1332 spa_altroot(spa_t *spa, char *buf, size_t buflen)
1334 if (spa->spa_root == NULL)
1337 (void) strncpy(buf, spa->spa_root, buflen);
1341 spa_sync_pass(spa_t *spa)
1343 return (spa->spa_sync_pass);
1347 spa_name(spa_t *spa)
1349 return (spa->spa_name);
1353 spa_guid(spa_t *spa)
1355 dsl_pool_t *dp = spa_get_dsl(spa);
1359 * If we fail to parse the config during spa_load(), we can go through
1360 * the error path (which posts an ereport) and end up here with no root
1361 * vdev. We stash the original pool guid in 'spa_config_guid' to handle
1364 if (spa->spa_root_vdev == NULL)
1365 return (spa->spa_config_guid);
1367 guid = spa->spa_last_synced_guid != 0 ?
1368 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;
1371 * Return the most recently synced out guid unless we're
1372 * in syncing context.
1374 if (dp && dsl_pool_sync_context(dp))
1375 return (spa->spa_root_vdev->vdev_guid);
1381 spa_load_guid(spa_t *spa)
1384 * This is a GUID that exists solely as a reference for the
1385 * purposes of the arc. It is generated at load time, and
1386 * is never written to persistent storage.
1388 return (spa->spa_load_guid);
1392 spa_last_synced_txg(spa_t *spa)
1394 return (spa->spa_ubsync.ub_txg);
1398 spa_first_txg(spa_t *spa)
1400 return (spa->spa_first_txg);
1404 spa_syncing_txg(spa_t *spa)
1406 return (spa->spa_syncing_txg);
1410 spa_state(spa_t *spa)
1412 return (spa->spa_state);
1416 spa_load_state(spa_t *spa)
1418 return (spa->spa_load_state);
1422 spa_freeze_txg(spa_t *spa)
1424 return (spa->spa_freeze_txg);
1429 spa_get_asize(spa_t *spa, uint64_t lsize)
1432 * The worst case is single-sector max-parity RAID-Z blocks, in which
1433 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1)
1434 * times the size; so just assume that. Add to this the fact that
1435 * we can have up to 3 DVAs per bp, and one more factor of 2 because
1436 * the block may be dittoed with up to 3 DVAs by ddt_sync().
1438 return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2);
1442 spa_get_dspace(spa_t *spa)
1444 return (spa->spa_dspace);
1448 spa_update_dspace(spa_t *spa)
1450 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) +
1451 ddt_get_dedup_dspace(spa);
1455 * Return the failure mode that has been set to this pool. The default
1456 * behavior will be to block all I/Os when a complete failure occurs.
1459 spa_get_failmode(spa_t *spa)
1461 return (spa->spa_failmode);
1465 spa_suspended(spa_t *spa)
1467 return (spa->spa_suspended);
1471 spa_version(spa_t *spa)
1473 return (spa->spa_ubsync.ub_version);
1477 spa_deflate(spa_t *spa)
1479 return (spa->spa_deflate);
1483 spa_normal_class(spa_t *spa)
1485 return (spa->spa_normal_class);
1489 spa_log_class(spa_t *spa)
1491 return (spa->spa_log_class);
1495 spa_max_replication(spa_t *spa)
1498 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to
1499 * handle BPs with more than one DVA allocated. Set our max
1500 * replication level accordingly.
1502 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS)
1504 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override));
1508 spa_prev_software_version(spa_t *spa)
1510 return (spa->spa_prev_software_version);
1514 dva_get_dsize_sync(spa_t *spa, const dva_t *dva)
1516 uint64_t asize = DVA_GET_ASIZE(dva);
1517 uint64_t dsize = asize;
1519 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1521 if (asize != 0 && spa->spa_deflate) {
1522 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva));
1523 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio;
1530 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp)
1534 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1535 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1541 bp_get_dsize(spa_t *spa, const blkptr_t *bp)
1545 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1547 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
1548 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]);
1550 spa_config_exit(spa, SCL_VDEV, FTAG);
1556 * ==========================================================================
1557 * Initialization and Termination
1558 * ==========================================================================
1562 spa_name_compare(const void *a1, const void *a2)
1564 const spa_t *s1 = a1;
1565 const spa_t *s2 = a2;
1568 s = strcmp(s1->spa_name, s2->spa_name);
1579 return (spa_active_count);
1591 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL);
1592 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL);
1593 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL);
1594 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL);
1596 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t),
1597 offsetof(spa_t, spa_avl));
1599 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t),
1600 offsetof(spa_aux_t, aux_avl));
1602 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t),
1603 offsetof(spa_aux_t, aux_avl));
1605 spa_mode_global = mode;
1609 if (spa_mode_global != FREAD && dprintf_find_string("watch")) {
1610 arc_procfd = open("/proc/self/ctl", O_WRONLY);
1611 if (arc_procfd == -1) {
1612 perror("could not enable watchpoints: "
1613 "opening /proc/self/ctl failed: ");
1619 #endif /* illumos */
1625 vdev_cache_stat_init();
1628 zpool_feature_init();
1640 vdev_cache_stat_fini();
1647 avl_destroy(&spa_namespace_avl);
1648 avl_destroy(&spa_spare_avl);
1649 avl_destroy(&spa_l2cache_avl);
1651 cv_destroy(&spa_namespace_cv);
1652 mutex_destroy(&spa_namespace_lock);
1653 mutex_destroy(&spa_spare_lock);
1654 mutex_destroy(&spa_l2cache_lock);
1658 * Return whether this pool has slogs. No locking needed.
1659 * It's not a problem if the wrong answer is returned as it's only for
1660 * performance and not correctness
1663 spa_has_slogs(spa_t *spa)
1665 return (spa->spa_log_class->mc_rotor != NULL);
1669 spa_get_log_state(spa_t *spa)
1671 return (spa->spa_log_state);
1675 spa_set_log_state(spa_t *spa, spa_log_state_t state)
1677 spa->spa_log_state = state;
1681 spa_is_root(spa_t *spa)
1683 return (spa->spa_is_root);
1687 spa_writeable(spa_t *spa)
1689 return (!!(spa->spa_mode & FWRITE));
1693 spa_mode(spa_t *spa)
1695 return (spa->spa_mode);
1699 spa_bootfs(spa_t *spa)
1701 return (spa->spa_bootfs);
1705 spa_delegation(spa_t *spa)
1707 return (spa->spa_delegation);
1711 spa_meta_objset(spa_t *spa)
1713 return (spa->spa_meta_objset);
1717 spa_dedup_checksum(spa_t *spa)
1719 return (spa->spa_dedup_checksum);
1723 * Reset pool scan stat per scan pass (or reboot).
1726 spa_scan_stat_init(spa_t *spa)
1728 /* data not stored on disk */
1729 spa->spa_scan_pass_start = gethrestime_sec();
1730 spa->spa_scan_pass_exam = 0;
1731 vdev_scan_stat_init(spa->spa_root_vdev);
1735 * Get scan stats for zpool status reports
1738 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps)
1740 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL;
1742 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE)
1744 bzero(ps, sizeof (pool_scan_stat_t));
1746 /* data stored on disk */
1747 ps->pss_func = scn->scn_phys.scn_func;
1748 ps->pss_start_time = scn->scn_phys.scn_start_time;
1749 ps->pss_end_time = scn->scn_phys.scn_end_time;
1750 ps->pss_to_examine = scn->scn_phys.scn_to_examine;
1751 ps->pss_examined = scn->scn_phys.scn_examined;
1752 ps->pss_to_process = scn->scn_phys.scn_to_process;
1753 ps->pss_processed = scn->scn_phys.scn_processed;
1754 ps->pss_errors = scn->scn_phys.scn_errors;
1755 ps->pss_state = scn->scn_phys.scn_state;
1757 /* data not stored on disk */
1758 ps->pss_pass_start = spa->spa_scan_pass_start;
1759 ps->pss_pass_exam = spa->spa_scan_pass_exam;
1765 spa_debug_enabled(spa_t *spa)
1767 return (spa->spa_debug);