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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab.h>
49 #include <sys/metaslab_impl.h>
50 #include <sys/uberblock_impl.h>
53 #include <sys/dmu_traverse.h>
54 #include <sys/dmu_objset.h>
55 #include <sys/unique.h>
56 #include <sys/dsl_pool.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/dsl_synctask.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/callb.h>
64 #include <sys/spa_boot.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/dsl_scan.h>
67 #include <sys/dmu_send.h>
68 #include <sys/dsl_destroy.h>
69 #include <sys/dsl_userhold.h>
70 #include <sys/zfeature.h>
72 #include <sys/trim_map.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
81 #include "zfs_comutil.h"
83 /* Check hostid on import? */
84 static int check_hostid = 1;
86 SYSCTL_DECL(_vfs_zfs);
87 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
88 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
89 "Check hostid on import?");
92 * The interval, in seconds, at which failed configuration cache file writes
95 static int zfs_ccw_retry_interval = 300;
97 typedef enum zti_modes {
98 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
99 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
100 ZTI_MODE_NULL, /* don't create a taskq */
104 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
105 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
106 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
108 #define ZTI_N(n) ZTI_P(n, 1)
109 #define ZTI_ONE ZTI_N(1)
111 typedef struct zio_taskq_info {
112 zti_modes_t zti_mode;
117 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
118 "issue", "issue_high", "intr", "intr_high"
122 * This table defines the taskq settings for each ZFS I/O type. When
123 * initializing a pool, we use this table to create an appropriately sized
124 * taskq. Some operations are low volume and therefore have a small, static
125 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
126 * macros. Other operations process a large amount of data; the ZTI_BATCH
127 * macro causes us to create a taskq oriented for throughput. Some operations
128 * are so high frequency and short-lived that the taskq itself can become a a
129 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
130 * additional degree of parallelism specified by the number of threads per-
131 * taskq and the number of taskqs; when dispatching an event in this case, the
132 * particular taskq is chosen at random.
134 * The different taskq priorities are to handle the different contexts (issue
135 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
136 * need to be handled with minimum delay.
138 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
139 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
140 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
141 { ZTI_N(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, /* READ */
142 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
143 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
144 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
145 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
148 static void spa_sync_version(void *arg, dmu_tx_t *tx);
149 static void spa_sync_props(void *arg, dmu_tx_t *tx);
150 static boolean_t spa_has_active_shared_spare(spa_t *spa);
151 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
152 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
154 static void spa_vdev_resilver_done(spa_t *spa);
156 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
158 id_t zio_taskq_psrset_bind = PS_NONE;
161 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
163 uint_t zio_taskq_basedc = 80; /* base duty cycle */
165 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
166 extern int zfs_sync_pass_deferred_free;
169 extern void spa_deadman(void *arg);
173 * This (illegal) pool name is used when temporarily importing a spa_t in order
174 * to get the vdev stats associated with the imported devices.
176 #define TRYIMPORT_NAME "$import"
179 * ==========================================================================
180 * SPA properties routines
181 * ==========================================================================
185 * Add a (source=src, propname=propval) list to an nvlist.
188 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
189 uint64_t intval, zprop_source_t src)
191 const char *propname = zpool_prop_to_name(prop);
194 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
195 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
198 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
200 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
202 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
203 nvlist_free(propval);
207 * Get property values from the spa configuration.
210 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
212 vdev_t *rvd = spa->spa_root_vdev;
213 dsl_pool_t *pool = spa->spa_dsl_pool;
217 uint64_t cap, version;
218 zprop_source_t src = ZPROP_SRC_NONE;
219 spa_config_dirent_t *dp;
221 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
224 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
225 size = metaslab_class_get_space(spa_normal_class(spa));
226 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
227 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
228 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
229 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
233 for (int c = 0; c < rvd->vdev_children; c++) {
234 vdev_t *tvd = rvd->vdev_child[c];
235 space += tvd->vdev_max_asize - tvd->vdev_asize;
237 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
240 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
241 (spa_mode(spa) == FREAD), src);
243 cap = (size == 0) ? 0 : (alloc * 100 / size);
244 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
246 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
247 ddt_get_pool_dedup_ratio(spa), src);
249 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
250 rvd->vdev_state, src);
252 version = spa_version(spa);
253 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
254 src = ZPROP_SRC_DEFAULT;
256 src = ZPROP_SRC_LOCAL;
257 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
261 dsl_dir_t *freedir = pool->dp_free_dir;
264 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
265 * when opening pools before this version freedir will be NULL.
267 if (freedir != NULL) {
268 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
269 freedir->dd_phys->dd_used_bytes, src);
271 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
276 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
278 if (spa->spa_comment != NULL) {
279 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
283 if (spa->spa_root != NULL)
284 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
287 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
288 if (dp->scd_path == NULL) {
289 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
290 "none", 0, ZPROP_SRC_LOCAL);
291 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
292 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
293 dp->scd_path, 0, ZPROP_SRC_LOCAL);
299 * Get zpool property values.
302 spa_prop_get(spa_t *spa, nvlist_t **nvp)
304 objset_t *mos = spa->spa_meta_objset;
309 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
311 mutex_enter(&spa->spa_props_lock);
314 * Get properties from the spa config.
316 spa_prop_get_config(spa, nvp);
318 /* If no pool property object, no more prop to get. */
319 if (mos == NULL || spa->spa_pool_props_object == 0) {
320 mutex_exit(&spa->spa_props_lock);
325 * Get properties from the MOS pool property object.
327 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
328 (err = zap_cursor_retrieve(&zc, &za)) == 0;
329 zap_cursor_advance(&zc)) {
332 zprop_source_t src = ZPROP_SRC_DEFAULT;
335 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
338 switch (za.za_integer_length) {
340 /* integer property */
341 if (za.za_first_integer !=
342 zpool_prop_default_numeric(prop))
343 src = ZPROP_SRC_LOCAL;
345 if (prop == ZPOOL_PROP_BOOTFS) {
347 dsl_dataset_t *ds = NULL;
349 dp = spa_get_dsl(spa);
350 dsl_pool_config_enter(dp, FTAG);
351 if (err = dsl_dataset_hold_obj(dp,
352 za.za_first_integer, FTAG, &ds)) {
353 dsl_pool_config_exit(dp, FTAG);
358 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
360 dsl_dataset_name(ds, strval);
361 dsl_dataset_rele(ds, FTAG);
362 dsl_pool_config_exit(dp, FTAG);
365 intval = za.za_first_integer;
368 spa_prop_add_list(*nvp, prop, strval, intval, src);
372 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
377 /* string property */
378 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
379 err = zap_lookup(mos, spa->spa_pool_props_object,
380 za.za_name, 1, za.za_num_integers, strval);
382 kmem_free(strval, za.za_num_integers);
385 spa_prop_add_list(*nvp, prop, strval, 0, src);
386 kmem_free(strval, za.za_num_integers);
393 zap_cursor_fini(&zc);
394 mutex_exit(&spa->spa_props_lock);
396 if (err && err != ENOENT) {
406 * Validate the given pool properties nvlist and modify the list
407 * for the property values to be set.
410 spa_prop_validate(spa_t *spa, nvlist_t *props)
413 int error = 0, reset_bootfs = 0;
415 boolean_t has_feature = B_FALSE;
418 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
420 char *strval, *slash, *check, *fname;
421 const char *propname = nvpair_name(elem);
422 zpool_prop_t prop = zpool_name_to_prop(propname);
426 if (!zpool_prop_feature(propname)) {
427 error = SET_ERROR(EINVAL);
432 * Sanitize the input.
434 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
435 error = SET_ERROR(EINVAL);
439 if (nvpair_value_uint64(elem, &intval) != 0) {
440 error = SET_ERROR(EINVAL);
445 error = SET_ERROR(EINVAL);
449 fname = strchr(propname, '@') + 1;
450 if (zfeature_lookup_name(fname, NULL) != 0) {
451 error = SET_ERROR(EINVAL);
455 has_feature = B_TRUE;
458 case ZPOOL_PROP_VERSION:
459 error = nvpair_value_uint64(elem, &intval);
461 (intval < spa_version(spa) ||
462 intval > SPA_VERSION_BEFORE_FEATURES ||
464 error = SET_ERROR(EINVAL);
467 case ZPOOL_PROP_DELEGATION:
468 case ZPOOL_PROP_AUTOREPLACE:
469 case ZPOOL_PROP_LISTSNAPS:
470 case ZPOOL_PROP_AUTOEXPAND:
471 error = nvpair_value_uint64(elem, &intval);
472 if (!error && intval > 1)
473 error = SET_ERROR(EINVAL);
476 case ZPOOL_PROP_BOOTFS:
478 * If the pool version is less than SPA_VERSION_BOOTFS,
479 * or the pool is still being created (version == 0),
480 * the bootfs property cannot be set.
482 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
483 error = SET_ERROR(ENOTSUP);
488 * Make sure the vdev config is bootable
490 if (!vdev_is_bootable(spa->spa_root_vdev)) {
491 error = SET_ERROR(ENOTSUP);
497 error = nvpair_value_string(elem, &strval);
503 if (strval == NULL || strval[0] == '\0') {
504 objnum = zpool_prop_default_numeric(
509 if (error = dmu_objset_hold(strval, FTAG, &os))
512 /* Must be ZPL and not gzip compressed. */
514 if (dmu_objset_type(os) != DMU_OST_ZFS) {
515 error = SET_ERROR(ENOTSUP);
517 dsl_prop_get_int_ds(dmu_objset_ds(os),
518 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
520 !BOOTFS_COMPRESS_VALID(compress)) {
521 error = SET_ERROR(ENOTSUP);
523 objnum = dmu_objset_id(os);
525 dmu_objset_rele(os, FTAG);
529 case ZPOOL_PROP_FAILUREMODE:
530 error = nvpair_value_uint64(elem, &intval);
531 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
532 intval > ZIO_FAILURE_MODE_PANIC))
533 error = SET_ERROR(EINVAL);
536 * This is a special case which only occurs when
537 * the pool has completely failed. This allows
538 * the user to change the in-core failmode property
539 * without syncing it out to disk (I/Os might
540 * currently be blocked). We do this by returning
541 * EIO to the caller (spa_prop_set) to trick it
542 * into thinking we encountered a property validation
545 if (!error && spa_suspended(spa)) {
546 spa->spa_failmode = intval;
547 error = SET_ERROR(EIO);
551 case ZPOOL_PROP_CACHEFILE:
552 if ((error = nvpair_value_string(elem, &strval)) != 0)
555 if (strval[0] == '\0')
558 if (strcmp(strval, "none") == 0)
561 if (strval[0] != '/') {
562 error = SET_ERROR(EINVAL);
566 slash = strrchr(strval, '/');
567 ASSERT(slash != NULL);
569 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
570 strcmp(slash, "/..") == 0)
571 error = SET_ERROR(EINVAL);
574 case ZPOOL_PROP_COMMENT:
575 if ((error = nvpair_value_string(elem, &strval)) != 0)
577 for (check = strval; *check != '\0'; check++) {
579 * The kernel doesn't have an easy isprint()
580 * check. For this kernel check, we merely
581 * check ASCII apart from DEL. Fix this if
582 * there is an easy-to-use kernel isprint().
584 if (*check >= 0x7f) {
585 error = SET_ERROR(EINVAL);
590 if (strlen(strval) > ZPROP_MAX_COMMENT)
594 case ZPOOL_PROP_DEDUPDITTO:
595 if (spa_version(spa) < SPA_VERSION_DEDUP)
596 error = SET_ERROR(ENOTSUP);
598 error = nvpair_value_uint64(elem, &intval);
600 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
601 error = SET_ERROR(EINVAL);
609 if (!error && reset_bootfs) {
610 error = nvlist_remove(props,
611 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
614 error = nvlist_add_uint64(props,
615 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
623 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
626 spa_config_dirent_t *dp;
628 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
632 dp = kmem_alloc(sizeof (spa_config_dirent_t),
635 if (cachefile[0] == '\0')
636 dp->scd_path = spa_strdup(spa_config_path);
637 else if (strcmp(cachefile, "none") == 0)
640 dp->scd_path = spa_strdup(cachefile);
642 list_insert_head(&spa->spa_config_list, dp);
644 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
648 spa_prop_set(spa_t *spa, nvlist_t *nvp)
651 nvpair_t *elem = NULL;
652 boolean_t need_sync = B_FALSE;
654 if ((error = spa_prop_validate(spa, nvp)) != 0)
657 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
658 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
660 if (prop == ZPOOL_PROP_CACHEFILE ||
661 prop == ZPOOL_PROP_ALTROOT ||
662 prop == ZPOOL_PROP_READONLY)
665 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
668 if (prop == ZPOOL_PROP_VERSION) {
669 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
671 ASSERT(zpool_prop_feature(nvpair_name(elem)));
672 ver = SPA_VERSION_FEATURES;
676 /* Save time if the version is already set. */
677 if (ver == spa_version(spa))
681 * In addition to the pool directory object, we might
682 * create the pool properties object, the features for
683 * read object, the features for write object, or the
684 * feature descriptions object.
686 error = dsl_sync_task(spa->spa_name, NULL,
687 spa_sync_version, &ver, 6);
698 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
706 * If the bootfs property value is dsobj, clear it.
709 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
711 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
712 VERIFY(zap_remove(spa->spa_meta_objset,
713 spa->spa_pool_props_object,
714 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
721 spa_change_guid_check(void *arg, dmu_tx_t *tx)
723 uint64_t *newguid = arg;
724 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
725 vdev_t *rvd = spa->spa_root_vdev;
728 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
729 vdev_state = rvd->vdev_state;
730 spa_config_exit(spa, SCL_STATE, FTAG);
732 if (vdev_state != VDEV_STATE_HEALTHY)
733 return (SET_ERROR(ENXIO));
735 ASSERT3U(spa_guid(spa), !=, *newguid);
741 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
743 uint64_t *newguid = arg;
744 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
746 vdev_t *rvd = spa->spa_root_vdev;
748 oldguid = spa_guid(spa);
750 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
751 rvd->vdev_guid = *newguid;
752 rvd->vdev_guid_sum += (*newguid - oldguid);
753 vdev_config_dirty(rvd);
754 spa_config_exit(spa, SCL_STATE, FTAG);
756 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
761 * Change the GUID for the pool. This is done so that we can later
762 * re-import a pool built from a clone of our own vdevs. We will modify
763 * the root vdev's guid, our own pool guid, and then mark all of our
764 * vdevs dirty. Note that we must make sure that all our vdevs are
765 * online when we do this, or else any vdevs that weren't present
766 * would be orphaned from our pool. We are also going to issue a
767 * sysevent to update any watchers.
770 spa_change_guid(spa_t *spa)
775 mutex_enter(&spa->spa_vdev_top_lock);
776 mutex_enter(&spa_namespace_lock);
777 guid = spa_generate_guid(NULL);
779 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
780 spa_change_guid_sync, &guid, 5);
783 spa_config_sync(spa, B_FALSE, B_TRUE);
784 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
787 mutex_exit(&spa_namespace_lock);
788 mutex_exit(&spa->spa_vdev_top_lock);
794 * ==========================================================================
795 * SPA state manipulation (open/create/destroy/import/export)
796 * ==========================================================================
800 spa_error_entry_compare(const void *a, const void *b)
802 spa_error_entry_t *sa = (spa_error_entry_t *)a;
803 spa_error_entry_t *sb = (spa_error_entry_t *)b;
806 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
807 sizeof (zbookmark_t));
818 * Utility function which retrieves copies of the current logs and
819 * re-initializes them in the process.
822 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
824 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
826 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
827 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
829 avl_create(&spa->spa_errlist_scrub,
830 spa_error_entry_compare, sizeof (spa_error_entry_t),
831 offsetof(spa_error_entry_t, se_avl));
832 avl_create(&spa->spa_errlist_last,
833 spa_error_entry_compare, sizeof (spa_error_entry_t),
834 offsetof(spa_error_entry_t, se_avl));
838 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
840 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
841 enum zti_modes mode = ztip->zti_mode;
842 uint_t value = ztip->zti_value;
843 uint_t count = ztip->zti_count;
844 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
847 boolean_t batch = B_FALSE;
849 if (mode == ZTI_MODE_NULL) {
851 tqs->stqs_taskq = NULL;
855 ASSERT3U(count, >, 0);
857 tqs->stqs_count = count;
858 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
862 ASSERT3U(value, >=, 1);
863 value = MAX(value, 1);
868 flags |= TASKQ_THREADS_CPU_PCT;
869 value = zio_taskq_batch_pct;
873 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
875 zio_type_name[t], zio_taskq_types[q], mode, value);
879 for (uint_t i = 0; i < count; i++) {
883 (void) snprintf(name, sizeof (name), "%s_%s_%u",
884 zio_type_name[t], zio_taskq_types[q], i);
886 (void) snprintf(name, sizeof (name), "%s_%s",
887 zio_type_name[t], zio_taskq_types[q]);
891 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
893 flags |= TASKQ_DC_BATCH;
895 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
896 spa->spa_proc, zio_taskq_basedc, flags);
899 pri_t pri = maxclsyspri;
901 * The write issue taskq can be extremely CPU
902 * intensive. Run it at slightly lower priority
903 * than the other taskqs.
905 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
908 tq = taskq_create_proc(name, value, pri, 50,
909 INT_MAX, spa->spa_proc, flags);
914 tqs->stqs_taskq[i] = tq;
919 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
921 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
923 if (tqs->stqs_taskq == NULL) {
924 ASSERT0(tqs->stqs_count);
928 for (uint_t i = 0; i < tqs->stqs_count; i++) {
929 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
930 taskq_destroy(tqs->stqs_taskq[i]);
933 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
934 tqs->stqs_taskq = NULL;
938 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
939 * Note that a type may have multiple discrete taskqs to avoid lock contention
940 * on the taskq itself. In that case we choose which taskq at random by using
941 * the low bits of gethrtime().
944 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
945 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
947 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
950 ASSERT3P(tqs->stqs_taskq, !=, NULL);
951 ASSERT3U(tqs->stqs_count, !=, 0);
953 if (tqs->stqs_count == 1) {
954 tq = tqs->stqs_taskq[0];
957 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
959 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
963 taskq_dispatch_ent(tq, func, arg, flags, ent);
967 spa_create_zio_taskqs(spa_t *spa)
969 for (int t = 0; t < ZIO_TYPES; t++) {
970 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
971 spa_taskqs_init(spa, t, q);
979 spa_thread(void *arg)
984 user_t *pu = PTOU(curproc);
986 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
989 ASSERT(curproc != &p0);
990 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
991 "zpool-%s", spa->spa_name);
992 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
995 /* bind this thread to the requested psrset */
996 if (zio_taskq_psrset_bind != PS_NONE) {
998 mutex_enter(&cpu_lock);
999 mutex_enter(&pidlock);
1000 mutex_enter(&curproc->p_lock);
1002 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1003 0, NULL, NULL) == 0) {
1004 curthread->t_bind_pset = zio_taskq_psrset_bind;
1007 "Couldn't bind process for zfs pool \"%s\" to "
1008 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1011 mutex_exit(&curproc->p_lock);
1012 mutex_exit(&pidlock);
1013 mutex_exit(&cpu_lock);
1019 if (zio_taskq_sysdc) {
1020 sysdc_thread_enter(curthread, 100, 0);
1024 spa->spa_proc = curproc;
1025 spa->spa_did = curthread->t_did;
1027 spa_create_zio_taskqs(spa);
1029 mutex_enter(&spa->spa_proc_lock);
1030 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1032 spa->spa_proc_state = SPA_PROC_ACTIVE;
1033 cv_broadcast(&spa->spa_proc_cv);
1035 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1036 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1037 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1038 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1040 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1041 spa->spa_proc_state = SPA_PROC_GONE;
1042 spa->spa_proc = &p0;
1043 cv_broadcast(&spa->spa_proc_cv);
1044 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1046 mutex_enter(&curproc->p_lock);
1049 #endif /* SPA_PROCESS */
1053 * Activate an uninitialized pool.
1056 spa_activate(spa_t *spa, int mode)
1058 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1060 spa->spa_state = POOL_STATE_ACTIVE;
1061 spa->spa_mode = mode;
1063 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1064 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1066 /* Try to create a covering process */
1067 mutex_enter(&spa->spa_proc_lock);
1068 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1069 ASSERT(spa->spa_proc == &p0);
1073 /* Only create a process if we're going to be around a while. */
1074 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1075 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1077 spa->spa_proc_state = SPA_PROC_CREATED;
1078 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1079 cv_wait(&spa->spa_proc_cv,
1080 &spa->spa_proc_lock);
1082 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1083 ASSERT(spa->spa_proc != &p0);
1084 ASSERT(spa->spa_did != 0);
1088 "Couldn't create process for zfs pool \"%s\"\n",
1093 #endif /* SPA_PROCESS */
1094 mutex_exit(&spa->spa_proc_lock);
1096 /* If we didn't create a process, we need to create our taskqs. */
1097 ASSERT(spa->spa_proc == &p0);
1098 if (spa->spa_proc == &p0) {
1099 spa_create_zio_taskqs(spa);
1103 * Start TRIM thread.
1105 trim_thread_create(spa);
1107 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1108 offsetof(vdev_t, vdev_config_dirty_node));
1109 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1110 offsetof(vdev_t, vdev_state_dirty_node));
1112 txg_list_create(&spa->spa_vdev_txg_list,
1113 offsetof(struct vdev, vdev_txg_node));
1115 avl_create(&spa->spa_errlist_scrub,
1116 spa_error_entry_compare, sizeof (spa_error_entry_t),
1117 offsetof(spa_error_entry_t, se_avl));
1118 avl_create(&spa->spa_errlist_last,
1119 spa_error_entry_compare, sizeof (spa_error_entry_t),
1120 offsetof(spa_error_entry_t, se_avl));
1124 * Opposite of spa_activate().
1127 spa_deactivate(spa_t *spa)
1129 ASSERT(spa->spa_sync_on == B_FALSE);
1130 ASSERT(spa->spa_dsl_pool == NULL);
1131 ASSERT(spa->spa_root_vdev == NULL);
1132 ASSERT(spa->spa_async_zio_root == NULL);
1133 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1136 * Stop TRIM thread in case spa_unload() wasn't called directly
1137 * before spa_deactivate().
1139 trim_thread_destroy(spa);
1141 txg_list_destroy(&spa->spa_vdev_txg_list);
1143 list_destroy(&spa->spa_config_dirty_list);
1144 list_destroy(&spa->spa_state_dirty_list);
1146 for (int t = 0; t < ZIO_TYPES; t++) {
1147 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1148 spa_taskqs_fini(spa, t, q);
1152 metaslab_class_destroy(spa->spa_normal_class);
1153 spa->spa_normal_class = NULL;
1155 metaslab_class_destroy(spa->spa_log_class);
1156 spa->spa_log_class = NULL;
1159 * If this was part of an import or the open otherwise failed, we may
1160 * still have errors left in the queues. Empty them just in case.
1162 spa_errlog_drain(spa);
1164 avl_destroy(&spa->spa_errlist_scrub);
1165 avl_destroy(&spa->spa_errlist_last);
1167 spa->spa_state = POOL_STATE_UNINITIALIZED;
1169 mutex_enter(&spa->spa_proc_lock);
1170 if (spa->spa_proc_state != SPA_PROC_NONE) {
1171 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1172 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1173 cv_broadcast(&spa->spa_proc_cv);
1174 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1175 ASSERT(spa->spa_proc != &p0);
1176 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1178 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1179 spa->spa_proc_state = SPA_PROC_NONE;
1181 ASSERT(spa->spa_proc == &p0);
1182 mutex_exit(&spa->spa_proc_lock);
1186 * We want to make sure spa_thread() has actually exited the ZFS
1187 * module, so that the module can't be unloaded out from underneath
1190 if (spa->spa_did != 0) {
1191 thread_join(spa->spa_did);
1194 #endif /* SPA_PROCESS */
1198 * Verify a pool configuration, and construct the vdev tree appropriately. This
1199 * will create all the necessary vdevs in the appropriate layout, with each vdev
1200 * in the CLOSED state. This will prep the pool before open/creation/import.
1201 * All vdev validation is done by the vdev_alloc() routine.
1204 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1205 uint_t id, int atype)
1211 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1214 if ((*vdp)->vdev_ops->vdev_op_leaf)
1217 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1220 if (error == ENOENT)
1226 return (SET_ERROR(EINVAL));
1229 for (int c = 0; c < children; c++) {
1231 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1239 ASSERT(*vdp != NULL);
1245 * Opposite of spa_load().
1248 spa_unload(spa_t *spa)
1252 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1257 trim_thread_destroy(spa);
1262 spa_async_suspend(spa);
1267 if (spa->spa_sync_on) {
1268 txg_sync_stop(spa->spa_dsl_pool);
1269 spa->spa_sync_on = B_FALSE;
1273 * Wait for any outstanding async I/O to complete.
1275 if (spa->spa_async_zio_root != NULL) {
1276 (void) zio_wait(spa->spa_async_zio_root);
1277 spa->spa_async_zio_root = NULL;
1280 bpobj_close(&spa->spa_deferred_bpobj);
1282 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1287 if (spa->spa_root_vdev)
1288 vdev_free(spa->spa_root_vdev);
1289 ASSERT(spa->spa_root_vdev == NULL);
1292 * Close the dsl pool.
1294 if (spa->spa_dsl_pool) {
1295 dsl_pool_close(spa->spa_dsl_pool);
1296 spa->spa_dsl_pool = NULL;
1297 spa->spa_meta_objset = NULL;
1304 * Drop and purge level 2 cache
1306 spa_l2cache_drop(spa);
1308 for (i = 0; i < spa->spa_spares.sav_count; i++)
1309 vdev_free(spa->spa_spares.sav_vdevs[i]);
1310 if (spa->spa_spares.sav_vdevs) {
1311 kmem_free(spa->spa_spares.sav_vdevs,
1312 spa->spa_spares.sav_count * sizeof (void *));
1313 spa->spa_spares.sav_vdevs = NULL;
1315 if (spa->spa_spares.sav_config) {
1316 nvlist_free(spa->spa_spares.sav_config);
1317 spa->spa_spares.sav_config = NULL;
1319 spa->spa_spares.sav_count = 0;
1321 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1322 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1323 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1325 if (spa->spa_l2cache.sav_vdevs) {
1326 kmem_free(spa->spa_l2cache.sav_vdevs,
1327 spa->spa_l2cache.sav_count * sizeof (void *));
1328 spa->spa_l2cache.sav_vdevs = NULL;
1330 if (spa->spa_l2cache.sav_config) {
1331 nvlist_free(spa->spa_l2cache.sav_config);
1332 spa->spa_l2cache.sav_config = NULL;
1334 spa->spa_l2cache.sav_count = 0;
1336 spa->spa_async_suspended = 0;
1338 if (spa->spa_comment != NULL) {
1339 spa_strfree(spa->spa_comment);
1340 spa->spa_comment = NULL;
1343 spa_config_exit(spa, SCL_ALL, FTAG);
1347 * Load (or re-load) the current list of vdevs describing the active spares for
1348 * this pool. When this is called, we have some form of basic information in
1349 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1350 * then re-generate a more complete list including status information.
1353 spa_load_spares(spa_t *spa)
1360 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1363 * First, close and free any existing spare vdevs.
1365 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1366 vd = spa->spa_spares.sav_vdevs[i];
1368 /* Undo the call to spa_activate() below */
1369 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1370 B_FALSE)) != NULL && tvd->vdev_isspare)
1371 spa_spare_remove(tvd);
1376 if (spa->spa_spares.sav_vdevs)
1377 kmem_free(spa->spa_spares.sav_vdevs,
1378 spa->spa_spares.sav_count * sizeof (void *));
1380 if (spa->spa_spares.sav_config == NULL)
1383 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1384 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1386 spa->spa_spares.sav_count = (int)nspares;
1387 spa->spa_spares.sav_vdevs = NULL;
1393 * Construct the array of vdevs, opening them to get status in the
1394 * process. For each spare, there is potentially two different vdev_t
1395 * structures associated with it: one in the list of spares (used only
1396 * for basic validation purposes) and one in the active vdev
1397 * configuration (if it's spared in). During this phase we open and
1398 * validate each vdev on the spare list. If the vdev also exists in the
1399 * active configuration, then we also mark this vdev as an active spare.
1401 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1403 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1404 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1405 VDEV_ALLOC_SPARE) == 0);
1408 spa->spa_spares.sav_vdevs[i] = vd;
1410 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1411 B_FALSE)) != NULL) {
1412 if (!tvd->vdev_isspare)
1416 * We only mark the spare active if we were successfully
1417 * able to load the vdev. Otherwise, importing a pool
1418 * with a bad active spare would result in strange
1419 * behavior, because multiple pool would think the spare
1420 * is actively in use.
1422 * There is a vulnerability here to an equally bizarre
1423 * circumstance, where a dead active spare is later
1424 * brought back to life (onlined or otherwise). Given
1425 * the rarity of this scenario, and the extra complexity
1426 * it adds, we ignore the possibility.
1428 if (!vdev_is_dead(tvd))
1429 spa_spare_activate(tvd);
1433 vd->vdev_aux = &spa->spa_spares;
1435 if (vdev_open(vd) != 0)
1438 if (vdev_validate_aux(vd) == 0)
1443 * Recompute the stashed list of spares, with status information
1446 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1447 DATA_TYPE_NVLIST_ARRAY) == 0);
1449 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1451 for (i = 0; i < spa->spa_spares.sav_count; i++)
1452 spares[i] = vdev_config_generate(spa,
1453 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1454 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1455 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1456 for (i = 0; i < spa->spa_spares.sav_count; i++)
1457 nvlist_free(spares[i]);
1458 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1462 * Load (or re-load) the current list of vdevs describing the active l2cache for
1463 * this pool. When this is called, we have some form of basic information in
1464 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1465 * then re-generate a more complete list including status information.
1466 * Devices which are already active have their details maintained, and are
1470 spa_load_l2cache(spa_t *spa)
1474 int i, j, oldnvdevs;
1476 vdev_t *vd, **oldvdevs, **newvdevs;
1477 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1479 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1481 if (sav->sav_config != NULL) {
1482 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1483 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1484 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1490 oldvdevs = sav->sav_vdevs;
1491 oldnvdevs = sav->sav_count;
1492 sav->sav_vdevs = NULL;
1496 * Process new nvlist of vdevs.
1498 for (i = 0; i < nl2cache; i++) {
1499 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1503 for (j = 0; j < oldnvdevs; j++) {
1505 if (vd != NULL && guid == vd->vdev_guid) {
1507 * Retain previous vdev for add/remove ops.
1515 if (newvdevs[i] == NULL) {
1519 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1520 VDEV_ALLOC_L2CACHE) == 0);
1525 * Commit this vdev as an l2cache device,
1526 * even if it fails to open.
1528 spa_l2cache_add(vd);
1533 spa_l2cache_activate(vd);
1535 if (vdev_open(vd) != 0)
1538 (void) vdev_validate_aux(vd);
1540 if (!vdev_is_dead(vd))
1541 l2arc_add_vdev(spa, vd);
1546 * Purge vdevs that were dropped
1548 for (i = 0; i < oldnvdevs; i++) {
1553 ASSERT(vd->vdev_isl2cache);
1555 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1556 pool != 0ULL && l2arc_vdev_present(vd))
1557 l2arc_remove_vdev(vd);
1558 vdev_clear_stats(vd);
1564 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1566 if (sav->sav_config == NULL)
1569 sav->sav_vdevs = newvdevs;
1570 sav->sav_count = (int)nl2cache;
1573 * Recompute the stashed list of l2cache devices, with status
1574 * information this time.
1576 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1577 DATA_TYPE_NVLIST_ARRAY) == 0);
1579 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1580 for (i = 0; i < sav->sav_count; i++)
1581 l2cache[i] = vdev_config_generate(spa,
1582 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1583 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1584 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1586 for (i = 0; i < sav->sav_count; i++)
1587 nvlist_free(l2cache[i]);
1589 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1593 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1596 char *packed = NULL;
1601 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1604 nvsize = *(uint64_t *)db->db_data;
1605 dmu_buf_rele(db, FTAG);
1607 packed = kmem_alloc(nvsize, KM_SLEEP);
1608 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1611 error = nvlist_unpack(packed, nvsize, value, 0);
1612 kmem_free(packed, nvsize);
1618 * Checks to see if the given vdev could not be opened, in which case we post a
1619 * sysevent to notify the autoreplace code that the device has been removed.
1622 spa_check_removed(vdev_t *vd)
1624 for (int c = 0; c < vd->vdev_children; c++)
1625 spa_check_removed(vd->vdev_child[c]);
1627 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1629 zfs_post_autoreplace(vd->vdev_spa, vd);
1630 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1635 * Validate the current config against the MOS config
1638 spa_config_valid(spa_t *spa, nvlist_t *config)
1640 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1643 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1645 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1646 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1648 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1651 * If we're doing a normal import, then build up any additional
1652 * diagnostic information about missing devices in this config.
1653 * We'll pass this up to the user for further processing.
1655 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1656 nvlist_t **child, *nv;
1659 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1661 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1663 for (int c = 0; c < rvd->vdev_children; c++) {
1664 vdev_t *tvd = rvd->vdev_child[c];
1665 vdev_t *mtvd = mrvd->vdev_child[c];
1667 if (tvd->vdev_ops == &vdev_missing_ops &&
1668 mtvd->vdev_ops != &vdev_missing_ops &&
1670 child[idx++] = vdev_config_generate(spa, mtvd,
1675 VERIFY(nvlist_add_nvlist_array(nv,
1676 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1677 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1678 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1680 for (int i = 0; i < idx; i++)
1681 nvlist_free(child[i]);
1684 kmem_free(child, rvd->vdev_children * sizeof (char **));
1688 * Compare the root vdev tree with the information we have
1689 * from the MOS config (mrvd). Check each top-level vdev
1690 * with the corresponding MOS config top-level (mtvd).
1692 for (int c = 0; c < rvd->vdev_children; c++) {
1693 vdev_t *tvd = rvd->vdev_child[c];
1694 vdev_t *mtvd = mrvd->vdev_child[c];
1697 * Resolve any "missing" vdevs in the current configuration.
1698 * If we find that the MOS config has more accurate information
1699 * about the top-level vdev then use that vdev instead.
1701 if (tvd->vdev_ops == &vdev_missing_ops &&
1702 mtvd->vdev_ops != &vdev_missing_ops) {
1704 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1708 * Device specific actions.
1710 if (mtvd->vdev_islog) {
1711 spa_set_log_state(spa, SPA_LOG_CLEAR);
1714 * XXX - once we have 'readonly' pool
1715 * support we should be able to handle
1716 * missing data devices by transitioning
1717 * the pool to readonly.
1723 * Swap the missing vdev with the data we were
1724 * able to obtain from the MOS config.
1726 vdev_remove_child(rvd, tvd);
1727 vdev_remove_child(mrvd, mtvd);
1729 vdev_add_child(rvd, mtvd);
1730 vdev_add_child(mrvd, tvd);
1732 spa_config_exit(spa, SCL_ALL, FTAG);
1734 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1737 } else if (mtvd->vdev_islog) {
1739 * Load the slog device's state from the MOS config
1740 * since it's possible that the label does not
1741 * contain the most up-to-date information.
1743 vdev_load_log_state(tvd, mtvd);
1748 spa_config_exit(spa, SCL_ALL, FTAG);
1751 * Ensure we were able to validate the config.
1753 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1757 * Check for missing log devices
1760 spa_check_logs(spa_t *spa)
1762 boolean_t rv = B_FALSE;
1764 switch (spa->spa_log_state) {
1765 case SPA_LOG_MISSING:
1766 /* need to recheck in case slog has been restored */
1767 case SPA_LOG_UNKNOWN:
1768 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1769 NULL, DS_FIND_CHILDREN) != 0);
1771 spa_set_log_state(spa, SPA_LOG_MISSING);
1778 spa_passivate_log(spa_t *spa)
1780 vdev_t *rvd = spa->spa_root_vdev;
1781 boolean_t slog_found = B_FALSE;
1783 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1785 if (!spa_has_slogs(spa))
1788 for (int c = 0; c < rvd->vdev_children; c++) {
1789 vdev_t *tvd = rvd->vdev_child[c];
1790 metaslab_group_t *mg = tvd->vdev_mg;
1792 if (tvd->vdev_islog) {
1793 metaslab_group_passivate(mg);
1794 slog_found = B_TRUE;
1798 return (slog_found);
1802 spa_activate_log(spa_t *spa)
1804 vdev_t *rvd = spa->spa_root_vdev;
1806 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1808 for (int c = 0; c < rvd->vdev_children; c++) {
1809 vdev_t *tvd = rvd->vdev_child[c];
1810 metaslab_group_t *mg = tvd->vdev_mg;
1812 if (tvd->vdev_islog)
1813 metaslab_group_activate(mg);
1818 spa_offline_log(spa_t *spa)
1822 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1823 NULL, DS_FIND_CHILDREN);
1826 * We successfully offlined the log device, sync out the
1827 * current txg so that the "stubby" block can be removed
1830 txg_wait_synced(spa->spa_dsl_pool, 0);
1836 spa_aux_check_removed(spa_aux_vdev_t *sav)
1840 for (i = 0; i < sav->sav_count; i++)
1841 spa_check_removed(sav->sav_vdevs[i]);
1845 spa_claim_notify(zio_t *zio)
1847 spa_t *spa = zio->io_spa;
1852 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1853 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1854 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1855 mutex_exit(&spa->spa_props_lock);
1858 typedef struct spa_load_error {
1859 uint64_t sle_meta_count;
1860 uint64_t sle_data_count;
1864 spa_load_verify_done(zio_t *zio)
1866 blkptr_t *bp = zio->io_bp;
1867 spa_load_error_t *sle = zio->io_private;
1868 dmu_object_type_t type = BP_GET_TYPE(bp);
1869 int error = zio->io_error;
1872 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1873 type != DMU_OT_INTENT_LOG)
1874 atomic_add_64(&sle->sle_meta_count, 1);
1876 atomic_add_64(&sle->sle_data_count, 1);
1878 zio_data_buf_free(zio->io_data, zio->io_size);
1883 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1884 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1886 if (!BP_IS_HOLE(bp)) {
1888 size_t size = BP_GET_PSIZE(bp);
1889 void *data = zio_data_buf_alloc(size);
1891 zio_nowait(zio_read(rio, spa, bp, data, size,
1892 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1893 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1894 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1900 spa_load_verify(spa_t *spa)
1903 spa_load_error_t sle = { 0 };
1904 zpool_rewind_policy_t policy;
1905 boolean_t verify_ok = B_FALSE;
1908 zpool_get_rewind_policy(spa->spa_config, &policy);
1910 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1913 rio = zio_root(spa, NULL, &sle,
1914 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1916 error = traverse_pool(spa, spa->spa_verify_min_txg,
1917 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1919 (void) zio_wait(rio);
1921 spa->spa_load_meta_errors = sle.sle_meta_count;
1922 spa->spa_load_data_errors = sle.sle_data_count;
1924 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1925 sle.sle_data_count <= policy.zrp_maxdata) {
1929 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1930 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1932 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1933 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1934 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1935 VERIFY(nvlist_add_int64(spa->spa_load_info,
1936 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1937 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1938 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1940 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1944 if (error != ENXIO && error != EIO)
1945 error = SET_ERROR(EIO);
1949 return (verify_ok ? 0 : EIO);
1953 * Find a value in the pool props object.
1956 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1958 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1959 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1963 * Find a value in the pool directory object.
1966 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1968 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1969 name, sizeof (uint64_t), 1, val));
1973 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1975 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1980 * Fix up config after a partly-completed split. This is done with the
1981 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1982 * pool have that entry in their config, but only the splitting one contains
1983 * a list of all the guids of the vdevs that are being split off.
1985 * This function determines what to do with that list: either rejoin
1986 * all the disks to the pool, or complete the splitting process. To attempt
1987 * the rejoin, each disk that is offlined is marked online again, and
1988 * we do a reopen() call. If the vdev label for every disk that was
1989 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1990 * then we call vdev_split() on each disk, and complete the split.
1992 * Otherwise we leave the config alone, with all the vdevs in place in
1993 * the original pool.
1996 spa_try_repair(spa_t *spa, nvlist_t *config)
2003 boolean_t attempt_reopen;
2005 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2008 /* check that the config is complete */
2009 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2010 &glist, &gcount) != 0)
2013 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2015 /* attempt to online all the vdevs & validate */
2016 attempt_reopen = B_TRUE;
2017 for (i = 0; i < gcount; i++) {
2018 if (glist[i] == 0) /* vdev is hole */
2021 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2022 if (vd[i] == NULL) {
2024 * Don't bother attempting to reopen the disks;
2025 * just do the split.
2027 attempt_reopen = B_FALSE;
2029 /* attempt to re-online it */
2030 vd[i]->vdev_offline = B_FALSE;
2034 if (attempt_reopen) {
2035 vdev_reopen(spa->spa_root_vdev);
2037 /* check each device to see what state it's in */
2038 for (extracted = 0, i = 0; i < gcount; i++) {
2039 if (vd[i] != NULL &&
2040 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2047 * If every disk has been moved to the new pool, or if we never
2048 * even attempted to look at them, then we split them off for
2051 if (!attempt_reopen || gcount == extracted) {
2052 for (i = 0; i < gcount; i++)
2055 vdev_reopen(spa->spa_root_vdev);
2058 kmem_free(vd, gcount * sizeof (vdev_t *));
2062 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2063 boolean_t mosconfig)
2065 nvlist_t *config = spa->spa_config;
2066 char *ereport = FM_EREPORT_ZFS_POOL;
2072 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2073 return (SET_ERROR(EINVAL));
2075 ASSERT(spa->spa_comment == NULL);
2076 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2077 spa->spa_comment = spa_strdup(comment);
2080 * Versioning wasn't explicitly added to the label until later, so if
2081 * it's not present treat it as the initial version.
2083 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2084 &spa->spa_ubsync.ub_version) != 0)
2085 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2087 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2088 &spa->spa_config_txg);
2090 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2091 spa_guid_exists(pool_guid, 0)) {
2092 error = SET_ERROR(EEXIST);
2094 spa->spa_config_guid = pool_guid;
2096 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2098 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2102 nvlist_free(spa->spa_load_info);
2103 spa->spa_load_info = fnvlist_alloc();
2105 gethrestime(&spa->spa_loaded_ts);
2106 error = spa_load_impl(spa, pool_guid, config, state, type,
2107 mosconfig, &ereport);
2110 spa->spa_minref = refcount_count(&spa->spa_refcount);
2112 if (error != EEXIST) {
2113 spa->spa_loaded_ts.tv_sec = 0;
2114 spa->spa_loaded_ts.tv_nsec = 0;
2116 if (error != EBADF) {
2117 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2120 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2127 * Load an existing storage pool, using the pool's builtin spa_config as a
2128 * source of configuration information.
2131 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2132 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2136 nvlist_t *nvroot = NULL;
2139 uberblock_t *ub = &spa->spa_uberblock;
2140 uint64_t children, config_cache_txg = spa->spa_config_txg;
2141 int orig_mode = spa->spa_mode;
2144 boolean_t missing_feat_write = B_FALSE;
2147 * If this is an untrusted config, access the pool in read-only mode.
2148 * This prevents things like resilvering recently removed devices.
2151 spa->spa_mode = FREAD;
2153 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2155 spa->spa_load_state = state;
2157 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2158 return (SET_ERROR(EINVAL));
2160 parse = (type == SPA_IMPORT_EXISTING ?
2161 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2164 * Create "The Godfather" zio to hold all async IOs
2166 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2167 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2170 * Parse the configuration into a vdev tree. We explicitly set the
2171 * value that will be returned by spa_version() since parsing the
2172 * configuration requires knowing the version number.
2174 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2175 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2176 spa_config_exit(spa, SCL_ALL, FTAG);
2181 ASSERT(spa->spa_root_vdev == rvd);
2183 if (type != SPA_IMPORT_ASSEMBLE) {
2184 ASSERT(spa_guid(spa) == pool_guid);
2188 * Try to open all vdevs, loading each label in the process.
2190 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2191 error = vdev_open(rvd);
2192 spa_config_exit(spa, SCL_ALL, FTAG);
2197 * We need to validate the vdev labels against the configuration that
2198 * we have in hand, which is dependent on the setting of mosconfig. If
2199 * mosconfig is true then we're validating the vdev labels based on
2200 * that config. Otherwise, we're validating against the cached config
2201 * (zpool.cache) that was read when we loaded the zfs module, and then
2202 * later we will recursively call spa_load() and validate against
2205 * If we're assembling a new pool that's been split off from an
2206 * existing pool, the labels haven't yet been updated so we skip
2207 * validation for now.
2209 if (type != SPA_IMPORT_ASSEMBLE) {
2210 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2211 error = vdev_validate(rvd, mosconfig);
2212 spa_config_exit(spa, SCL_ALL, FTAG);
2217 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2218 return (SET_ERROR(ENXIO));
2222 * Find the best uberblock.
2224 vdev_uberblock_load(rvd, ub, &label);
2227 * If we weren't able to find a single valid uberblock, return failure.
2229 if (ub->ub_txg == 0) {
2231 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2235 * If the pool has an unsupported version we can't open it.
2237 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2239 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2242 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2246 * If we weren't able to find what's necessary for reading the
2247 * MOS in the label, return failure.
2249 if (label == NULL || nvlist_lookup_nvlist(label,
2250 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2252 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2257 * Update our in-core representation with the definitive values
2260 nvlist_free(spa->spa_label_features);
2261 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2267 * Look through entries in the label nvlist's features_for_read. If
2268 * there is a feature listed there which we don't understand then we
2269 * cannot open a pool.
2271 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2272 nvlist_t *unsup_feat;
2274 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2277 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2279 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2280 if (!zfeature_is_supported(nvpair_name(nvp))) {
2281 VERIFY(nvlist_add_string(unsup_feat,
2282 nvpair_name(nvp), "") == 0);
2286 if (!nvlist_empty(unsup_feat)) {
2287 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2288 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2289 nvlist_free(unsup_feat);
2290 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2294 nvlist_free(unsup_feat);
2298 * If the vdev guid sum doesn't match the uberblock, we have an
2299 * incomplete configuration. We first check to see if the pool
2300 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2301 * If it is, defer the vdev_guid_sum check till later so we
2302 * can handle missing vdevs.
2304 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2305 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2306 rvd->vdev_guid_sum != ub->ub_guid_sum)
2307 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2309 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2310 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2311 spa_try_repair(spa, config);
2312 spa_config_exit(spa, SCL_ALL, FTAG);
2313 nvlist_free(spa->spa_config_splitting);
2314 spa->spa_config_splitting = NULL;
2318 * Initialize internal SPA structures.
2320 spa->spa_state = POOL_STATE_ACTIVE;
2321 spa->spa_ubsync = spa->spa_uberblock;
2322 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2323 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2324 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2325 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2326 spa->spa_claim_max_txg = spa->spa_first_txg;
2327 spa->spa_prev_software_version = ub->ub_software_version;
2329 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2331 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2332 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2334 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2335 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2337 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2338 boolean_t missing_feat_read = B_FALSE;
2339 nvlist_t *unsup_feat, *enabled_feat;
2341 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2342 &spa->spa_feat_for_read_obj) != 0) {
2343 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2346 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2347 &spa->spa_feat_for_write_obj) != 0) {
2348 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2351 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2352 &spa->spa_feat_desc_obj) != 0) {
2353 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2356 enabled_feat = fnvlist_alloc();
2357 unsup_feat = fnvlist_alloc();
2359 if (!spa_features_check(spa, B_FALSE,
2360 unsup_feat, enabled_feat))
2361 missing_feat_read = B_TRUE;
2363 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2364 if (!spa_features_check(spa, B_TRUE,
2365 unsup_feat, enabled_feat)) {
2366 missing_feat_write = B_TRUE;
2370 fnvlist_add_nvlist(spa->spa_load_info,
2371 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2373 if (!nvlist_empty(unsup_feat)) {
2374 fnvlist_add_nvlist(spa->spa_load_info,
2375 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2378 fnvlist_free(enabled_feat);
2379 fnvlist_free(unsup_feat);
2381 if (!missing_feat_read) {
2382 fnvlist_add_boolean(spa->spa_load_info,
2383 ZPOOL_CONFIG_CAN_RDONLY);
2387 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2388 * twofold: to determine whether the pool is available for
2389 * import in read-write mode and (if it is not) whether the
2390 * pool is available for import in read-only mode. If the pool
2391 * is available for import in read-write mode, it is displayed
2392 * as available in userland; if it is not available for import
2393 * in read-only mode, it is displayed as unavailable in
2394 * userland. If the pool is available for import in read-only
2395 * mode but not read-write mode, it is displayed as unavailable
2396 * in userland with a special note that the pool is actually
2397 * available for open in read-only mode.
2399 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2400 * missing a feature for write, we must first determine whether
2401 * the pool can be opened read-only before returning to
2402 * userland in order to know whether to display the
2403 * abovementioned note.
2405 if (missing_feat_read || (missing_feat_write &&
2406 spa_writeable(spa))) {
2407 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2412 * Load refcounts for ZFS features from disk into an in-memory
2413 * cache during SPA initialization.
2415 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2418 error = feature_get_refcount_from_disk(spa,
2419 &spa_feature_table[i], &refcount);
2421 spa->spa_feat_refcount_cache[i] = refcount;
2422 } else if (error == ENOTSUP) {
2423 spa->spa_feat_refcount_cache[i] =
2424 SPA_FEATURE_DISABLED;
2426 return (spa_vdev_err(rvd,
2427 VDEV_AUX_CORRUPT_DATA, EIO));
2432 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2433 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2434 &spa->spa_feat_enabled_txg_obj) != 0) {
2435 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2439 spa->spa_is_initializing = B_TRUE;
2440 error = dsl_pool_open(spa->spa_dsl_pool);
2441 spa->spa_is_initializing = B_FALSE;
2443 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2447 nvlist_t *policy = NULL, *nvconfig;
2449 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2450 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2452 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2453 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2455 unsigned long myhostid = 0;
2457 VERIFY(nvlist_lookup_string(nvconfig,
2458 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2461 myhostid = zone_get_hostid(NULL);
2464 * We're emulating the system's hostid in userland, so
2465 * we can't use zone_get_hostid().
2467 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2468 #endif /* _KERNEL */
2469 if (check_hostid && hostid != 0 && myhostid != 0 &&
2470 hostid != myhostid) {
2471 nvlist_free(nvconfig);
2472 cmn_err(CE_WARN, "pool '%s' could not be "
2473 "loaded as it was last accessed by "
2474 "another system (host: %s hostid: 0x%lx). "
2475 "See: http://illumos.org/msg/ZFS-8000-EY",
2476 spa_name(spa), hostname,
2477 (unsigned long)hostid);
2478 return (SET_ERROR(EBADF));
2481 if (nvlist_lookup_nvlist(spa->spa_config,
2482 ZPOOL_REWIND_POLICY, &policy) == 0)
2483 VERIFY(nvlist_add_nvlist(nvconfig,
2484 ZPOOL_REWIND_POLICY, policy) == 0);
2486 spa_config_set(spa, nvconfig);
2488 spa_deactivate(spa);
2489 spa_activate(spa, orig_mode);
2491 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2494 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2495 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2496 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2498 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2501 * Load the bit that tells us to use the new accounting function
2502 * (raid-z deflation). If we have an older pool, this will not
2505 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2506 if (error != 0 && error != ENOENT)
2507 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2509 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2510 &spa->spa_creation_version);
2511 if (error != 0 && error != ENOENT)
2512 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2515 * Load the persistent error log. If we have an older pool, this will
2518 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2519 if (error != 0 && error != ENOENT)
2520 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2522 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2523 &spa->spa_errlog_scrub);
2524 if (error != 0 && error != ENOENT)
2525 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2528 * Load the history object. If we have an older pool, this
2529 * will not be present.
2531 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2532 if (error != 0 && error != ENOENT)
2533 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2536 * If we're assembling the pool from the split-off vdevs of
2537 * an existing pool, we don't want to attach the spares & cache
2542 * Load any hot spares for this pool.
2544 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2545 if (error != 0 && error != ENOENT)
2546 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2547 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2548 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2549 if (load_nvlist(spa, spa->spa_spares.sav_object,
2550 &spa->spa_spares.sav_config) != 0)
2551 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2553 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2554 spa_load_spares(spa);
2555 spa_config_exit(spa, SCL_ALL, FTAG);
2556 } else if (error == 0) {
2557 spa->spa_spares.sav_sync = B_TRUE;
2561 * Load any level 2 ARC devices for this pool.
2563 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2564 &spa->spa_l2cache.sav_object);
2565 if (error != 0 && error != ENOENT)
2566 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2567 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2568 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2569 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2570 &spa->spa_l2cache.sav_config) != 0)
2571 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2573 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2574 spa_load_l2cache(spa);
2575 spa_config_exit(spa, SCL_ALL, FTAG);
2576 } else if (error == 0) {
2577 spa->spa_l2cache.sav_sync = B_TRUE;
2580 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2582 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2583 if (error && error != ENOENT)
2584 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2587 uint64_t autoreplace;
2589 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2590 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2591 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2592 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2593 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2594 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2595 &spa->spa_dedup_ditto);
2597 spa->spa_autoreplace = (autoreplace != 0);
2601 * If the 'autoreplace' property is set, then post a resource notifying
2602 * the ZFS DE that it should not issue any faults for unopenable
2603 * devices. We also iterate over the vdevs, and post a sysevent for any
2604 * unopenable vdevs so that the normal autoreplace handler can take
2607 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2608 spa_check_removed(spa->spa_root_vdev);
2610 * For the import case, this is done in spa_import(), because
2611 * at this point we're using the spare definitions from
2612 * the MOS config, not necessarily from the userland config.
2614 if (state != SPA_LOAD_IMPORT) {
2615 spa_aux_check_removed(&spa->spa_spares);
2616 spa_aux_check_removed(&spa->spa_l2cache);
2621 * Load the vdev state for all toplevel vdevs.
2626 * Propagate the leaf DTLs we just loaded all the way up the tree.
2628 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2629 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2630 spa_config_exit(spa, SCL_ALL, FTAG);
2633 * Load the DDTs (dedup tables).
2635 error = ddt_load(spa);
2637 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2639 spa_update_dspace(spa);
2642 * Validate the config, using the MOS config to fill in any
2643 * information which might be missing. If we fail to validate
2644 * the config then declare the pool unfit for use. If we're
2645 * assembling a pool from a split, the log is not transferred
2648 if (type != SPA_IMPORT_ASSEMBLE) {
2651 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2652 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2654 if (!spa_config_valid(spa, nvconfig)) {
2655 nvlist_free(nvconfig);
2656 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2659 nvlist_free(nvconfig);
2662 * Now that we've validated the config, check the state of the
2663 * root vdev. If it can't be opened, it indicates one or
2664 * more toplevel vdevs are faulted.
2666 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2667 return (SET_ERROR(ENXIO));
2669 if (spa_check_logs(spa)) {
2670 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2671 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2675 if (missing_feat_write) {
2676 ASSERT(state == SPA_LOAD_TRYIMPORT);
2679 * At this point, we know that we can open the pool in
2680 * read-only mode but not read-write mode. We now have enough
2681 * information and can return to userland.
2683 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2687 * We've successfully opened the pool, verify that we're ready
2688 * to start pushing transactions.
2690 if (state != SPA_LOAD_TRYIMPORT) {
2691 if (error = spa_load_verify(spa))
2692 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2696 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2697 spa->spa_load_max_txg == UINT64_MAX)) {
2699 int need_update = B_FALSE;
2701 ASSERT(state != SPA_LOAD_TRYIMPORT);
2704 * Claim log blocks that haven't been committed yet.
2705 * This must all happen in a single txg.
2706 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2707 * invoked from zil_claim_log_block()'s i/o done callback.
2708 * Price of rollback is that we abandon the log.
2710 spa->spa_claiming = B_TRUE;
2712 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2713 spa_first_txg(spa));
2714 (void) dmu_objset_find(spa_name(spa),
2715 zil_claim, tx, DS_FIND_CHILDREN);
2718 spa->spa_claiming = B_FALSE;
2720 spa_set_log_state(spa, SPA_LOG_GOOD);
2721 spa->spa_sync_on = B_TRUE;
2722 txg_sync_start(spa->spa_dsl_pool);
2725 * Wait for all claims to sync. We sync up to the highest
2726 * claimed log block birth time so that claimed log blocks
2727 * don't appear to be from the future. spa_claim_max_txg
2728 * will have been set for us by either zil_check_log_chain()
2729 * (invoked from spa_check_logs()) or zil_claim() above.
2731 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2734 * If the config cache is stale, or we have uninitialized
2735 * metaslabs (see spa_vdev_add()), then update the config.
2737 * If this is a verbatim import, trust the current
2738 * in-core spa_config and update the disk labels.
2740 if (config_cache_txg != spa->spa_config_txg ||
2741 state == SPA_LOAD_IMPORT ||
2742 state == SPA_LOAD_RECOVER ||
2743 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2744 need_update = B_TRUE;
2746 for (int c = 0; c < rvd->vdev_children; c++)
2747 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2748 need_update = B_TRUE;
2751 * Update the config cache asychronously in case we're the
2752 * root pool, in which case the config cache isn't writable yet.
2755 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2758 * Check all DTLs to see if anything needs resilvering.
2760 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2761 vdev_resilver_needed(rvd, NULL, NULL))
2762 spa_async_request(spa, SPA_ASYNC_RESILVER);
2765 * Log the fact that we booted up (so that we can detect if
2766 * we rebooted in the middle of an operation).
2768 spa_history_log_version(spa, "open");
2771 * Delete any inconsistent datasets.
2773 (void) dmu_objset_find(spa_name(spa),
2774 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2777 * Clean up any stale temporary dataset userrefs.
2779 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2786 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2788 int mode = spa->spa_mode;
2791 spa_deactivate(spa);
2793 spa->spa_load_max_txg--;
2795 spa_activate(spa, mode);
2796 spa_async_suspend(spa);
2798 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2802 * If spa_load() fails this function will try loading prior txg's. If
2803 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2804 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2805 * function will not rewind the pool and will return the same error as
2809 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2810 uint64_t max_request, int rewind_flags)
2812 nvlist_t *loadinfo = NULL;
2813 nvlist_t *config = NULL;
2814 int load_error, rewind_error;
2815 uint64_t safe_rewind_txg;
2818 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2819 spa->spa_load_max_txg = spa->spa_load_txg;
2820 spa_set_log_state(spa, SPA_LOG_CLEAR);
2822 spa->spa_load_max_txg = max_request;
2825 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2827 if (load_error == 0)
2830 if (spa->spa_root_vdev != NULL)
2831 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2833 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2834 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2836 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2837 nvlist_free(config);
2838 return (load_error);
2841 if (state == SPA_LOAD_RECOVER) {
2842 /* Price of rolling back is discarding txgs, including log */
2843 spa_set_log_state(spa, SPA_LOG_CLEAR);
2846 * If we aren't rolling back save the load info from our first
2847 * import attempt so that we can restore it after attempting
2850 loadinfo = spa->spa_load_info;
2851 spa->spa_load_info = fnvlist_alloc();
2854 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2855 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2856 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2857 TXG_INITIAL : safe_rewind_txg;
2860 * Continue as long as we're finding errors, we're still within
2861 * the acceptable rewind range, and we're still finding uberblocks
2863 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2864 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2865 if (spa->spa_load_max_txg < safe_rewind_txg)
2866 spa->spa_extreme_rewind = B_TRUE;
2867 rewind_error = spa_load_retry(spa, state, mosconfig);
2870 spa->spa_extreme_rewind = B_FALSE;
2871 spa->spa_load_max_txg = UINT64_MAX;
2873 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2874 spa_config_set(spa, config);
2876 if (state == SPA_LOAD_RECOVER) {
2877 ASSERT3P(loadinfo, ==, NULL);
2878 return (rewind_error);
2880 /* Store the rewind info as part of the initial load info */
2881 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2882 spa->spa_load_info);
2884 /* Restore the initial load info */
2885 fnvlist_free(spa->spa_load_info);
2886 spa->spa_load_info = loadinfo;
2888 return (load_error);
2895 * The import case is identical to an open except that the configuration is sent
2896 * down from userland, instead of grabbed from the configuration cache. For the
2897 * case of an open, the pool configuration will exist in the
2898 * POOL_STATE_UNINITIALIZED state.
2900 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2901 * the same time open the pool, without having to keep around the spa_t in some
2905 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2909 spa_load_state_t state = SPA_LOAD_OPEN;
2911 int locked = B_FALSE;
2912 int firstopen = B_FALSE;
2917 * As disgusting as this is, we need to support recursive calls to this
2918 * function because dsl_dir_open() is called during spa_load(), and ends
2919 * up calling spa_open() again. The real fix is to figure out how to
2920 * avoid dsl_dir_open() calling this in the first place.
2922 if (mutex_owner(&spa_namespace_lock) != curthread) {
2923 mutex_enter(&spa_namespace_lock);
2927 if ((spa = spa_lookup(pool)) == NULL) {
2929 mutex_exit(&spa_namespace_lock);
2930 return (SET_ERROR(ENOENT));
2933 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2934 zpool_rewind_policy_t policy;
2938 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2940 if (policy.zrp_request & ZPOOL_DO_REWIND)
2941 state = SPA_LOAD_RECOVER;
2943 spa_activate(spa, spa_mode_global);
2945 if (state != SPA_LOAD_RECOVER)
2946 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2948 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2949 policy.zrp_request);
2951 if (error == EBADF) {
2953 * If vdev_validate() returns failure (indicated by
2954 * EBADF), it indicates that one of the vdevs indicates
2955 * that the pool has been exported or destroyed. If
2956 * this is the case, the config cache is out of sync and
2957 * we should remove the pool from the namespace.
2960 spa_deactivate(spa);
2961 spa_config_sync(spa, B_TRUE, B_TRUE);
2964 mutex_exit(&spa_namespace_lock);
2965 return (SET_ERROR(ENOENT));
2970 * We can't open the pool, but we still have useful
2971 * information: the state of each vdev after the
2972 * attempted vdev_open(). Return this to the user.
2974 if (config != NULL && spa->spa_config) {
2975 VERIFY(nvlist_dup(spa->spa_config, config,
2977 VERIFY(nvlist_add_nvlist(*config,
2978 ZPOOL_CONFIG_LOAD_INFO,
2979 spa->spa_load_info) == 0);
2982 spa_deactivate(spa);
2983 spa->spa_last_open_failed = error;
2985 mutex_exit(&spa_namespace_lock);
2991 spa_open_ref(spa, tag);
2994 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2997 * If we've recovered the pool, pass back any information we
2998 * gathered while doing the load.
3000 if (state == SPA_LOAD_RECOVER) {
3001 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3002 spa->spa_load_info) == 0);
3006 spa->spa_last_open_failed = 0;
3007 spa->spa_last_ubsync_txg = 0;
3008 spa->spa_load_txg = 0;
3009 mutex_exit(&spa_namespace_lock);
3013 zvol_create_minors(spa->spa_name);
3024 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3027 return (spa_open_common(name, spapp, tag, policy, config));
3031 spa_open(const char *name, spa_t **spapp, void *tag)
3033 return (spa_open_common(name, spapp, tag, NULL, NULL));
3037 * Lookup the given spa_t, incrementing the inject count in the process,
3038 * preventing it from being exported or destroyed.
3041 spa_inject_addref(char *name)
3045 mutex_enter(&spa_namespace_lock);
3046 if ((spa = spa_lookup(name)) == NULL) {
3047 mutex_exit(&spa_namespace_lock);
3050 spa->spa_inject_ref++;
3051 mutex_exit(&spa_namespace_lock);
3057 spa_inject_delref(spa_t *spa)
3059 mutex_enter(&spa_namespace_lock);
3060 spa->spa_inject_ref--;
3061 mutex_exit(&spa_namespace_lock);
3065 * Add spares device information to the nvlist.
3068 spa_add_spares(spa_t *spa, nvlist_t *config)
3078 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3080 if (spa->spa_spares.sav_count == 0)
3083 VERIFY(nvlist_lookup_nvlist(config,
3084 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3085 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3086 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3088 VERIFY(nvlist_add_nvlist_array(nvroot,
3089 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3090 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3091 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3094 * Go through and find any spares which have since been
3095 * repurposed as an active spare. If this is the case, update
3096 * their status appropriately.
3098 for (i = 0; i < nspares; i++) {
3099 VERIFY(nvlist_lookup_uint64(spares[i],
3100 ZPOOL_CONFIG_GUID, &guid) == 0);
3101 if (spa_spare_exists(guid, &pool, NULL) &&
3103 VERIFY(nvlist_lookup_uint64_array(
3104 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3105 (uint64_t **)&vs, &vsc) == 0);
3106 vs->vs_state = VDEV_STATE_CANT_OPEN;
3107 vs->vs_aux = VDEV_AUX_SPARED;
3114 * Add l2cache device information to the nvlist, including vdev stats.
3117 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3120 uint_t i, j, nl2cache;
3127 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3129 if (spa->spa_l2cache.sav_count == 0)
3132 VERIFY(nvlist_lookup_nvlist(config,
3133 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3134 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3135 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3136 if (nl2cache != 0) {
3137 VERIFY(nvlist_add_nvlist_array(nvroot,
3138 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3139 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3140 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3143 * Update level 2 cache device stats.
3146 for (i = 0; i < nl2cache; i++) {
3147 VERIFY(nvlist_lookup_uint64(l2cache[i],
3148 ZPOOL_CONFIG_GUID, &guid) == 0);
3151 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3153 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3154 vd = spa->spa_l2cache.sav_vdevs[j];
3160 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3161 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3163 vdev_get_stats(vd, vs);
3169 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3175 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3176 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3178 /* We may be unable to read features if pool is suspended. */
3179 if (spa_suspended(spa))
3182 if (spa->spa_feat_for_read_obj != 0) {
3183 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3184 spa->spa_feat_for_read_obj);
3185 zap_cursor_retrieve(&zc, &za) == 0;
3186 zap_cursor_advance(&zc)) {
3187 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3188 za.za_num_integers == 1);
3189 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3190 za.za_first_integer));
3192 zap_cursor_fini(&zc);
3195 if (spa->spa_feat_for_write_obj != 0) {
3196 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3197 spa->spa_feat_for_write_obj);
3198 zap_cursor_retrieve(&zc, &za) == 0;
3199 zap_cursor_advance(&zc)) {
3200 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3201 za.za_num_integers == 1);
3202 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3203 za.za_first_integer));
3205 zap_cursor_fini(&zc);
3209 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3211 nvlist_free(features);
3215 spa_get_stats(const char *name, nvlist_t **config,
3216 char *altroot, size_t buflen)
3222 error = spa_open_common(name, &spa, FTAG, NULL, config);
3226 * This still leaves a window of inconsistency where the spares
3227 * or l2cache devices could change and the config would be
3228 * self-inconsistent.
3230 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3232 if (*config != NULL) {
3233 uint64_t loadtimes[2];
3235 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3236 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3237 VERIFY(nvlist_add_uint64_array(*config,
3238 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3240 VERIFY(nvlist_add_uint64(*config,
3241 ZPOOL_CONFIG_ERRCOUNT,
3242 spa_get_errlog_size(spa)) == 0);
3244 if (spa_suspended(spa))
3245 VERIFY(nvlist_add_uint64(*config,
3246 ZPOOL_CONFIG_SUSPENDED,
3247 spa->spa_failmode) == 0);
3249 spa_add_spares(spa, *config);
3250 spa_add_l2cache(spa, *config);
3251 spa_add_feature_stats(spa, *config);
3256 * We want to get the alternate root even for faulted pools, so we cheat
3257 * and call spa_lookup() directly.
3261 mutex_enter(&spa_namespace_lock);
3262 spa = spa_lookup(name);
3264 spa_altroot(spa, altroot, buflen);
3268 mutex_exit(&spa_namespace_lock);
3270 spa_altroot(spa, altroot, buflen);
3275 spa_config_exit(spa, SCL_CONFIG, FTAG);
3276 spa_close(spa, FTAG);
3283 * Validate that the auxiliary device array is well formed. We must have an
3284 * array of nvlists, each which describes a valid leaf vdev. If this is an
3285 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3286 * specified, as long as they are well-formed.
3289 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3290 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3291 vdev_labeltype_t label)
3298 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3301 * It's acceptable to have no devs specified.
3303 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3307 return (SET_ERROR(EINVAL));
3310 * Make sure the pool is formatted with a version that supports this
3313 if (spa_version(spa) < version)
3314 return (SET_ERROR(ENOTSUP));
3317 * Set the pending device list so we correctly handle device in-use
3320 sav->sav_pending = dev;
3321 sav->sav_npending = ndev;
3323 for (i = 0; i < ndev; i++) {
3324 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3328 if (!vd->vdev_ops->vdev_op_leaf) {
3330 error = SET_ERROR(EINVAL);
3335 * The L2ARC currently only supports disk devices in
3336 * kernel context. For user-level testing, we allow it.
3339 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3340 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3341 error = SET_ERROR(ENOTBLK);
3348 if ((error = vdev_open(vd)) == 0 &&
3349 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3350 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3351 vd->vdev_guid) == 0);
3357 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3364 sav->sav_pending = NULL;
3365 sav->sav_npending = 0;
3370 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3374 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3376 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3377 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3378 VDEV_LABEL_SPARE)) != 0) {
3382 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3383 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3384 VDEV_LABEL_L2CACHE));
3388 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3393 if (sav->sav_config != NULL) {
3399 * Generate new dev list by concatentating with the
3402 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3403 &olddevs, &oldndevs) == 0);
3405 newdevs = kmem_alloc(sizeof (void *) *
3406 (ndevs + oldndevs), KM_SLEEP);
3407 for (i = 0; i < oldndevs; i++)
3408 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3410 for (i = 0; i < ndevs; i++)
3411 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3414 VERIFY(nvlist_remove(sav->sav_config, config,
3415 DATA_TYPE_NVLIST_ARRAY) == 0);
3417 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3418 config, newdevs, ndevs + oldndevs) == 0);
3419 for (i = 0; i < oldndevs + ndevs; i++)
3420 nvlist_free(newdevs[i]);
3421 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3424 * Generate a new dev list.
3426 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3428 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3434 * Stop and drop level 2 ARC devices
3437 spa_l2cache_drop(spa_t *spa)
3441 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3443 for (i = 0; i < sav->sav_count; i++) {
3446 vd = sav->sav_vdevs[i];
3449 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3450 pool != 0ULL && l2arc_vdev_present(vd))
3451 l2arc_remove_vdev(vd);
3459 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3463 char *altroot = NULL;
3468 uint64_t txg = TXG_INITIAL;
3469 nvlist_t **spares, **l2cache;
3470 uint_t nspares, nl2cache;
3471 uint64_t version, obj;
3472 boolean_t has_features;
3475 * If this pool already exists, return failure.
3477 mutex_enter(&spa_namespace_lock);
3478 if (spa_lookup(pool) != NULL) {
3479 mutex_exit(&spa_namespace_lock);
3480 return (SET_ERROR(EEXIST));
3484 * Allocate a new spa_t structure.
3486 (void) nvlist_lookup_string(props,
3487 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3488 spa = spa_add(pool, NULL, altroot);
3489 spa_activate(spa, spa_mode_global);
3491 if (props && (error = spa_prop_validate(spa, props))) {
3492 spa_deactivate(spa);
3494 mutex_exit(&spa_namespace_lock);
3498 has_features = B_FALSE;
3499 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3500 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3501 if (zpool_prop_feature(nvpair_name(elem)))
3502 has_features = B_TRUE;
3505 if (has_features || nvlist_lookup_uint64(props,
3506 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3507 version = SPA_VERSION;
3509 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3511 spa->spa_first_txg = txg;
3512 spa->spa_uberblock.ub_txg = txg - 1;
3513 spa->spa_uberblock.ub_version = version;
3514 spa->spa_ubsync = spa->spa_uberblock;
3517 * Create "The Godfather" zio to hold all async IOs
3519 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3520 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3523 * Create the root vdev.
3525 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3527 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3529 ASSERT(error != 0 || rvd != NULL);
3530 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3532 if (error == 0 && !zfs_allocatable_devs(nvroot))
3533 error = SET_ERROR(EINVAL);
3536 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3537 (error = spa_validate_aux(spa, nvroot, txg,
3538 VDEV_ALLOC_ADD)) == 0) {
3539 for (int c = 0; c < rvd->vdev_children; c++) {
3540 vdev_ashift_optimize(rvd->vdev_child[c]);
3541 vdev_metaslab_set_size(rvd->vdev_child[c]);
3542 vdev_expand(rvd->vdev_child[c], txg);
3546 spa_config_exit(spa, SCL_ALL, FTAG);
3550 spa_deactivate(spa);
3552 mutex_exit(&spa_namespace_lock);
3557 * Get the list of spares, if specified.
3559 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3560 &spares, &nspares) == 0) {
3561 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3563 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3564 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3565 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3566 spa_load_spares(spa);
3567 spa_config_exit(spa, SCL_ALL, FTAG);
3568 spa->spa_spares.sav_sync = B_TRUE;
3572 * Get the list of level 2 cache devices, if specified.
3574 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3575 &l2cache, &nl2cache) == 0) {
3576 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3577 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3578 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3579 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3580 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3581 spa_load_l2cache(spa);
3582 spa_config_exit(spa, SCL_ALL, FTAG);
3583 spa->spa_l2cache.sav_sync = B_TRUE;
3586 spa->spa_is_initializing = B_TRUE;
3587 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3588 spa->spa_meta_objset = dp->dp_meta_objset;
3589 spa->spa_is_initializing = B_FALSE;
3592 * Create DDTs (dedup tables).
3596 spa_update_dspace(spa);
3598 tx = dmu_tx_create_assigned(dp, txg);
3601 * Create the pool config object.
3603 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3604 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3605 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3607 if (zap_add(spa->spa_meta_objset,
3608 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3609 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3610 cmn_err(CE_PANIC, "failed to add pool config");
3613 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3614 spa_feature_create_zap_objects(spa, tx);
3616 if (zap_add(spa->spa_meta_objset,
3617 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3618 sizeof (uint64_t), 1, &version, tx) != 0) {
3619 cmn_err(CE_PANIC, "failed to add pool version");
3622 /* Newly created pools with the right version are always deflated. */
3623 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3624 spa->spa_deflate = TRUE;
3625 if (zap_add(spa->spa_meta_objset,
3626 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3627 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3628 cmn_err(CE_PANIC, "failed to add deflate");
3633 * Create the deferred-free bpobj. Turn off compression
3634 * because sync-to-convergence takes longer if the blocksize
3637 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3638 dmu_object_set_compress(spa->spa_meta_objset, obj,
3639 ZIO_COMPRESS_OFF, tx);
3640 if (zap_add(spa->spa_meta_objset,
3641 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3642 sizeof (uint64_t), 1, &obj, tx) != 0) {
3643 cmn_err(CE_PANIC, "failed to add bpobj");
3645 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3646 spa->spa_meta_objset, obj));
3649 * Create the pool's history object.
3651 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3652 spa_history_create_obj(spa, tx);
3655 * Set pool properties.
3657 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3658 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3659 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3660 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3662 if (props != NULL) {
3663 spa_configfile_set(spa, props, B_FALSE);
3664 spa_sync_props(props, tx);
3669 spa->spa_sync_on = B_TRUE;
3670 txg_sync_start(spa->spa_dsl_pool);
3673 * We explicitly wait for the first transaction to complete so that our
3674 * bean counters are appropriately updated.
3676 txg_wait_synced(spa->spa_dsl_pool, txg);
3678 spa_config_sync(spa, B_FALSE, B_TRUE);
3680 spa_history_log_version(spa, "create");
3682 spa->spa_minref = refcount_count(&spa->spa_refcount);
3684 mutex_exit(&spa_namespace_lock);
3692 * Get the root pool information from the root disk, then import the root pool
3693 * during the system boot up time.
3695 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3698 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3701 nvlist_t *nvtop, *nvroot;
3704 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3708 * Add this top-level vdev to the child array.
3710 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3712 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3714 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3717 * Put this pool's top-level vdevs into a root vdev.
3719 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3720 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3721 VDEV_TYPE_ROOT) == 0);
3722 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3723 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3724 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3728 * Replace the existing vdev_tree with the new root vdev in
3729 * this pool's configuration (remove the old, add the new).
3731 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3732 nvlist_free(nvroot);
3737 * Walk the vdev tree and see if we can find a device with "better"
3738 * configuration. A configuration is "better" if the label on that
3739 * device has a more recent txg.
3742 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3744 for (int c = 0; c < vd->vdev_children; c++)
3745 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3747 if (vd->vdev_ops->vdev_op_leaf) {
3751 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3755 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3759 * Do we have a better boot device?
3761 if (label_txg > *txg) {
3770 * Import a root pool.
3772 * For x86. devpath_list will consist of devid and/or physpath name of
3773 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3774 * The GRUB "findroot" command will return the vdev we should boot.
3776 * For Sparc, devpath_list consists the physpath name of the booting device
3777 * no matter the rootpool is a single device pool or a mirrored pool.
3779 * "/pci@1f,0/ide@d/disk@0,0:a"
3782 spa_import_rootpool(char *devpath, char *devid)
3785 vdev_t *rvd, *bvd, *avd = NULL;
3786 nvlist_t *config, *nvtop;
3792 * Read the label from the boot device and generate a configuration.
3794 config = spa_generate_rootconf(devpath, devid, &guid);
3795 #if defined(_OBP) && defined(_KERNEL)
3796 if (config == NULL) {
3797 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3799 get_iscsi_bootpath_phy(devpath);
3800 config = spa_generate_rootconf(devpath, devid, &guid);
3804 if (config == NULL) {
3805 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3807 return (SET_ERROR(EIO));
3810 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3812 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3814 mutex_enter(&spa_namespace_lock);
3815 if ((spa = spa_lookup(pname)) != NULL) {
3817 * Remove the existing root pool from the namespace so that we
3818 * can replace it with the correct config we just read in.
3823 spa = spa_add(pname, config, NULL);
3824 spa->spa_is_root = B_TRUE;
3825 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3828 * Build up a vdev tree based on the boot device's label config.
3830 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3832 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3833 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3834 VDEV_ALLOC_ROOTPOOL);
3835 spa_config_exit(spa, SCL_ALL, FTAG);
3837 mutex_exit(&spa_namespace_lock);
3838 nvlist_free(config);
3839 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3845 * Get the boot vdev.
3847 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3848 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3849 (u_longlong_t)guid);
3850 error = SET_ERROR(ENOENT);
3855 * Determine if there is a better boot device.
3858 spa_alt_rootvdev(rvd, &avd, &txg);
3860 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3861 "try booting from '%s'", avd->vdev_path);
3862 error = SET_ERROR(EINVAL);
3867 * If the boot device is part of a spare vdev then ensure that
3868 * we're booting off the active spare.
3870 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3871 !bvd->vdev_isspare) {
3872 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3873 "try booting from '%s'",
3875 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3876 error = SET_ERROR(EINVAL);
3882 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3884 spa_config_exit(spa, SCL_ALL, FTAG);
3885 mutex_exit(&spa_namespace_lock);
3887 nvlist_free(config);
3893 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3897 spa_generate_rootconf(const char *name)
3899 nvlist_t **configs, **tops;
3901 nvlist_t *best_cfg, *nvtop, *nvroot;
3910 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3913 ASSERT3U(count, !=, 0);
3915 for (i = 0; i < count; i++) {
3918 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3920 if (txg > best_txg) {
3922 best_cfg = configs[i];
3927 * Multi-vdev root pool configuration discovery is not supported yet.
3930 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3932 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3935 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3936 for (i = 0; i < nchildren; i++) {
3939 if (configs[i] == NULL)
3941 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
3943 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
3945 for (i = 0; holes != NULL && i < nholes; i++) {
3948 if (tops[holes[i]] != NULL)
3950 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
3951 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
3952 VDEV_TYPE_HOLE) == 0);
3953 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
3955 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
3958 for (i = 0; i < nchildren; i++) {
3959 if (tops[i] != NULL)
3961 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
3962 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
3963 VDEV_TYPE_MISSING) == 0);
3964 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
3966 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
3971 * Create pool config based on the best vdev config.
3973 nvlist_dup(best_cfg, &config, KM_SLEEP);
3976 * Put this pool's top-level vdevs into a root vdev.
3978 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3980 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3981 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3982 VDEV_TYPE_ROOT) == 0);
3983 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3984 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3985 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3986 tops, nchildren) == 0);
3989 * Replace the existing vdev_tree with the new root vdev in
3990 * this pool's configuration (remove the old, add the new).
3992 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3995 * Drop vdev config elements that should not be present at pool level.
3997 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
3998 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4000 for (i = 0; i < count; i++)
4001 nvlist_free(configs[i]);
4002 kmem_free(configs, count * sizeof(void *));
4003 for (i = 0; i < nchildren; i++)
4004 nvlist_free(tops[i]);
4005 kmem_free(tops, nchildren * sizeof(void *));
4006 nvlist_free(nvroot);
4011 spa_import_rootpool(const char *name)
4014 vdev_t *rvd, *bvd, *avd = NULL;
4015 nvlist_t *config, *nvtop;
4021 * Read the label from the boot device and generate a configuration.
4023 config = spa_generate_rootconf(name);
4025 mutex_enter(&spa_namespace_lock);
4026 if (config != NULL) {
4027 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4028 &pname) == 0 && strcmp(name, pname) == 0);
4029 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4032 if ((spa = spa_lookup(pname)) != NULL) {
4034 * Remove the existing root pool from the namespace so
4035 * that we can replace it with the correct config
4040 spa = spa_add(pname, config, NULL);
4043 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4044 * via spa_version().
4046 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4047 &spa->spa_ubsync.ub_version) != 0)
4048 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4049 } else if ((spa = spa_lookup(name)) == NULL) {
4050 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4054 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4056 spa->spa_is_root = B_TRUE;
4057 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4060 * Build up a vdev tree based on the boot device's label config.
4062 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4064 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4065 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4066 VDEV_ALLOC_ROOTPOOL);
4067 spa_config_exit(spa, SCL_ALL, FTAG);
4069 mutex_exit(&spa_namespace_lock);
4070 nvlist_free(config);
4071 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4076 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4078 spa_config_exit(spa, SCL_ALL, FTAG);
4079 mutex_exit(&spa_namespace_lock);
4081 nvlist_free(config);
4089 * Import a non-root pool into the system.
4092 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4095 char *altroot = NULL;
4096 spa_load_state_t state = SPA_LOAD_IMPORT;
4097 zpool_rewind_policy_t policy;
4098 uint64_t mode = spa_mode_global;
4099 uint64_t readonly = B_FALSE;
4102 nvlist_t **spares, **l2cache;
4103 uint_t nspares, nl2cache;
4106 * If a pool with this name exists, return failure.
4108 mutex_enter(&spa_namespace_lock);
4109 if (spa_lookup(pool) != NULL) {
4110 mutex_exit(&spa_namespace_lock);
4111 return (SET_ERROR(EEXIST));
4115 * Create and initialize the spa structure.
4117 (void) nvlist_lookup_string(props,
4118 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4119 (void) nvlist_lookup_uint64(props,
4120 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4123 spa = spa_add(pool, config, altroot);
4124 spa->spa_import_flags = flags;
4127 * Verbatim import - Take a pool and insert it into the namespace
4128 * as if it had been loaded at boot.
4130 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4132 spa_configfile_set(spa, props, B_FALSE);
4134 spa_config_sync(spa, B_FALSE, B_TRUE);
4136 mutex_exit(&spa_namespace_lock);
4140 spa_activate(spa, mode);
4143 * Don't start async tasks until we know everything is healthy.
4145 spa_async_suspend(spa);
4147 zpool_get_rewind_policy(config, &policy);
4148 if (policy.zrp_request & ZPOOL_DO_REWIND)
4149 state = SPA_LOAD_RECOVER;
4152 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4153 * because the user-supplied config is actually the one to trust when
4156 if (state != SPA_LOAD_RECOVER)
4157 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4159 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4160 policy.zrp_request);
4163 * Propagate anything learned while loading the pool and pass it
4164 * back to caller (i.e. rewind info, missing devices, etc).
4166 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4167 spa->spa_load_info) == 0);
4169 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4171 * Toss any existing sparelist, as it doesn't have any validity
4172 * anymore, and conflicts with spa_has_spare().
4174 if (spa->spa_spares.sav_config) {
4175 nvlist_free(spa->spa_spares.sav_config);
4176 spa->spa_spares.sav_config = NULL;
4177 spa_load_spares(spa);
4179 if (spa->spa_l2cache.sav_config) {
4180 nvlist_free(spa->spa_l2cache.sav_config);
4181 spa->spa_l2cache.sav_config = NULL;
4182 spa_load_l2cache(spa);
4185 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4188 error = spa_validate_aux(spa, nvroot, -1ULL,
4191 error = spa_validate_aux(spa, nvroot, -1ULL,
4192 VDEV_ALLOC_L2CACHE);
4193 spa_config_exit(spa, SCL_ALL, FTAG);
4196 spa_configfile_set(spa, props, B_FALSE);
4198 if (error != 0 || (props && spa_writeable(spa) &&
4199 (error = spa_prop_set(spa, props)))) {
4201 spa_deactivate(spa);
4203 mutex_exit(&spa_namespace_lock);
4207 spa_async_resume(spa);
4210 * Override any spares and level 2 cache devices as specified by
4211 * the user, as these may have correct device names/devids, etc.
4213 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4214 &spares, &nspares) == 0) {
4215 if (spa->spa_spares.sav_config)
4216 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4217 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4219 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4220 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4221 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4222 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4223 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4224 spa_load_spares(spa);
4225 spa_config_exit(spa, SCL_ALL, FTAG);
4226 spa->spa_spares.sav_sync = B_TRUE;
4228 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4229 &l2cache, &nl2cache) == 0) {
4230 if (spa->spa_l2cache.sav_config)
4231 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4232 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4234 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4235 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4236 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4237 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4238 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4239 spa_load_l2cache(spa);
4240 spa_config_exit(spa, SCL_ALL, FTAG);
4241 spa->spa_l2cache.sav_sync = B_TRUE;
4245 * Check for any removed devices.
4247 if (spa->spa_autoreplace) {
4248 spa_aux_check_removed(&spa->spa_spares);
4249 spa_aux_check_removed(&spa->spa_l2cache);
4252 if (spa_writeable(spa)) {
4254 * Update the config cache to include the newly-imported pool.
4256 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4260 * It's possible that the pool was expanded while it was exported.
4261 * We kick off an async task to handle this for us.
4263 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4265 mutex_exit(&spa_namespace_lock);
4266 spa_history_log_version(spa, "import");
4270 zvol_create_minors(pool);
4277 spa_tryimport(nvlist_t *tryconfig)
4279 nvlist_t *config = NULL;
4285 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4288 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4292 * Create and initialize the spa structure.
4294 mutex_enter(&spa_namespace_lock);
4295 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4296 spa_activate(spa, FREAD);
4299 * Pass off the heavy lifting to spa_load().
4300 * Pass TRUE for mosconfig because the user-supplied config
4301 * is actually the one to trust when doing an import.
4303 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4306 * If 'tryconfig' was at least parsable, return the current config.
4308 if (spa->spa_root_vdev != NULL) {
4309 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4310 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4312 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4314 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4315 spa->spa_uberblock.ub_timestamp) == 0);
4316 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4317 spa->spa_load_info) == 0);
4320 * If the bootfs property exists on this pool then we
4321 * copy it out so that external consumers can tell which
4322 * pools are bootable.
4324 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4325 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4328 * We have to play games with the name since the
4329 * pool was opened as TRYIMPORT_NAME.
4331 if (dsl_dsobj_to_dsname(spa_name(spa),
4332 spa->spa_bootfs, tmpname) == 0) {
4334 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4336 cp = strchr(tmpname, '/');
4338 (void) strlcpy(dsname, tmpname,
4341 (void) snprintf(dsname, MAXPATHLEN,
4342 "%s/%s", poolname, ++cp);
4344 VERIFY(nvlist_add_string(config,
4345 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4346 kmem_free(dsname, MAXPATHLEN);
4348 kmem_free(tmpname, MAXPATHLEN);
4352 * Add the list of hot spares and level 2 cache devices.
4354 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4355 spa_add_spares(spa, config);
4356 spa_add_l2cache(spa, config);
4357 spa_config_exit(spa, SCL_CONFIG, FTAG);
4361 spa_deactivate(spa);
4363 mutex_exit(&spa_namespace_lock);
4369 * Pool export/destroy
4371 * The act of destroying or exporting a pool is very simple. We make sure there
4372 * is no more pending I/O and any references to the pool are gone. Then, we
4373 * update the pool state and sync all the labels to disk, removing the
4374 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4375 * we don't sync the labels or remove the configuration cache.
4378 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4379 boolean_t force, boolean_t hardforce)
4386 if (!(spa_mode_global & FWRITE))
4387 return (SET_ERROR(EROFS));
4389 mutex_enter(&spa_namespace_lock);
4390 if ((spa = spa_lookup(pool)) == NULL) {
4391 mutex_exit(&spa_namespace_lock);
4392 return (SET_ERROR(ENOENT));
4396 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4397 * reacquire the namespace lock, and see if we can export.
4399 spa_open_ref(spa, FTAG);
4400 mutex_exit(&spa_namespace_lock);
4401 spa_async_suspend(spa);
4402 mutex_enter(&spa_namespace_lock);
4403 spa_close(spa, FTAG);
4406 * The pool will be in core if it's openable,
4407 * in which case we can modify its state.
4409 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4411 * Objsets may be open only because they're dirty, so we
4412 * have to force it to sync before checking spa_refcnt.
4414 txg_wait_synced(spa->spa_dsl_pool, 0);
4417 * A pool cannot be exported or destroyed if there are active
4418 * references. If we are resetting a pool, allow references by
4419 * fault injection handlers.
4421 if (!spa_refcount_zero(spa) ||
4422 (spa->spa_inject_ref != 0 &&
4423 new_state != POOL_STATE_UNINITIALIZED)) {
4424 spa_async_resume(spa);
4425 mutex_exit(&spa_namespace_lock);
4426 return (SET_ERROR(EBUSY));
4430 * A pool cannot be exported if it has an active shared spare.
4431 * This is to prevent other pools stealing the active spare
4432 * from an exported pool. At user's own will, such pool can
4433 * be forcedly exported.
4435 if (!force && new_state == POOL_STATE_EXPORTED &&
4436 spa_has_active_shared_spare(spa)) {
4437 spa_async_resume(spa);
4438 mutex_exit(&spa_namespace_lock);
4439 return (SET_ERROR(EXDEV));
4443 * We want this to be reflected on every label,
4444 * so mark them all dirty. spa_unload() will do the
4445 * final sync that pushes these changes out.
4447 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4448 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4449 spa->spa_state = new_state;
4450 spa->spa_final_txg = spa_last_synced_txg(spa) +
4452 vdev_config_dirty(spa->spa_root_vdev);
4453 spa_config_exit(spa, SCL_ALL, FTAG);
4457 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4459 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4461 spa_deactivate(spa);
4464 if (oldconfig && spa->spa_config)
4465 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4467 if (new_state != POOL_STATE_UNINITIALIZED) {
4469 spa_config_sync(spa, B_TRUE, B_TRUE);
4472 mutex_exit(&spa_namespace_lock);
4478 * Destroy a storage pool.
4481 spa_destroy(char *pool)
4483 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4488 * Export a storage pool.
4491 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4492 boolean_t hardforce)
4494 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4499 * Similar to spa_export(), this unloads the spa_t without actually removing it
4500 * from the namespace in any way.
4503 spa_reset(char *pool)
4505 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4510 * ==========================================================================
4511 * Device manipulation
4512 * ==========================================================================
4516 * Add a device to a storage pool.
4519 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4523 vdev_t *rvd = spa->spa_root_vdev;
4525 nvlist_t **spares, **l2cache;
4526 uint_t nspares, nl2cache;
4528 ASSERT(spa_writeable(spa));
4530 txg = spa_vdev_enter(spa);
4532 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4533 VDEV_ALLOC_ADD)) != 0)
4534 return (spa_vdev_exit(spa, NULL, txg, error));
4536 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4538 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4542 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4546 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4547 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4549 if (vd->vdev_children != 0 &&
4550 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4551 return (spa_vdev_exit(spa, vd, txg, error));
4554 * We must validate the spares and l2cache devices after checking the
4555 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4557 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4558 return (spa_vdev_exit(spa, vd, txg, error));
4561 * Transfer each new top-level vdev from vd to rvd.
4563 for (int c = 0; c < vd->vdev_children; c++) {
4566 * Set the vdev id to the first hole, if one exists.
4568 for (id = 0; id < rvd->vdev_children; id++) {
4569 if (rvd->vdev_child[id]->vdev_ishole) {
4570 vdev_free(rvd->vdev_child[id]);
4574 tvd = vd->vdev_child[c];
4575 vdev_remove_child(vd, tvd);
4577 vdev_add_child(rvd, tvd);
4578 vdev_config_dirty(tvd);
4582 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4583 ZPOOL_CONFIG_SPARES);
4584 spa_load_spares(spa);
4585 spa->spa_spares.sav_sync = B_TRUE;
4588 if (nl2cache != 0) {
4589 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4590 ZPOOL_CONFIG_L2CACHE);
4591 spa_load_l2cache(spa);
4592 spa->spa_l2cache.sav_sync = B_TRUE;
4596 * We have to be careful when adding new vdevs to an existing pool.
4597 * If other threads start allocating from these vdevs before we
4598 * sync the config cache, and we lose power, then upon reboot we may
4599 * fail to open the pool because there are DVAs that the config cache
4600 * can't translate. Therefore, we first add the vdevs without
4601 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4602 * and then let spa_config_update() initialize the new metaslabs.
4604 * spa_load() checks for added-but-not-initialized vdevs, so that
4605 * if we lose power at any point in this sequence, the remaining
4606 * steps will be completed the next time we load the pool.
4608 (void) spa_vdev_exit(spa, vd, txg, 0);
4610 mutex_enter(&spa_namespace_lock);
4611 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4612 mutex_exit(&spa_namespace_lock);
4618 * Attach a device to a mirror. The arguments are the path to any device
4619 * in the mirror, and the nvroot for the new device. If the path specifies
4620 * a device that is not mirrored, we automatically insert the mirror vdev.
4622 * If 'replacing' is specified, the new device is intended to replace the
4623 * existing device; in this case the two devices are made into their own
4624 * mirror using the 'replacing' vdev, which is functionally identical to
4625 * the mirror vdev (it actually reuses all the same ops) but has a few
4626 * extra rules: you can't attach to it after it's been created, and upon
4627 * completion of resilvering, the first disk (the one being replaced)
4628 * is automatically detached.
4631 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4633 uint64_t txg, dtl_max_txg;
4634 vdev_t *rvd = spa->spa_root_vdev;
4635 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4637 char *oldvdpath, *newvdpath;
4641 ASSERT(spa_writeable(spa));
4643 txg = spa_vdev_enter(spa);
4645 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4648 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4650 if (!oldvd->vdev_ops->vdev_op_leaf)
4651 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4653 pvd = oldvd->vdev_parent;
4655 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4656 VDEV_ALLOC_ATTACH)) != 0)
4657 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4659 if (newrootvd->vdev_children != 1)
4660 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4662 newvd = newrootvd->vdev_child[0];
4664 if (!newvd->vdev_ops->vdev_op_leaf)
4665 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4667 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4668 return (spa_vdev_exit(spa, newrootvd, txg, error));
4671 * Spares can't replace logs
4673 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4674 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4678 * For attach, the only allowable parent is a mirror or the root
4681 if (pvd->vdev_ops != &vdev_mirror_ops &&
4682 pvd->vdev_ops != &vdev_root_ops)
4683 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4685 pvops = &vdev_mirror_ops;
4688 * Active hot spares can only be replaced by inactive hot
4691 if (pvd->vdev_ops == &vdev_spare_ops &&
4692 oldvd->vdev_isspare &&
4693 !spa_has_spare(spa, newvd->vdev_guid))
4694 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4697 * If the source is a hot spare, and the parent isn't already a
4698 * spare, then we want to create a new hot spare. Otherwise, we
4699 * want to create a replacing vdev. The user is not allowed to
4700 * attach to a spared vdev child unless the 'isspare' state is
4701 * the same (spare replaces spare, non-spare replaces
4704 if (pvd->vdev_ops == &vdev_replacing_ops &&
4705 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4706 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4707 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4708 newvd->vdev_isspare != oldvd->vdev_isspare) {
4709 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4712 if (newvd->vdev_isspare)
4713 pvops = &vdev_spare_ops;
4715 pvops = &vdev_replacing_ops;
4719 * Make sure the new device is big enough.
4721 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4722 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4725 * The new device cannot have a higher alignment requirement
4726 * than the top-level vdev.
4728 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4729 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4732 * If this is an in-place replacement, update oldvd's path and devid
4733 * to make it distinguishable from newvd, and unopenable from now on.
4735 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4736 spa_strfree(oldvd->vdev_path);
4737 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4739 (void) sprintf(oldvd->vdev_path, "%s/%s",
4740 newvd->vdev_path, "old");
4741 if (oldvd->vdev_devid != NULL) {
4742 spa_strfree(oldvd->vdev_devid);
4743 oldvd->vdev_devid = NULL;
4747 /* mark the device being resilvered */
4748 newvd->vdev_resilver_txg = txg;
4751 * If the parent is not a mirror, or if we're replacing, insert the new
4752 * mirror/replacing/spare vdev above oldvd.
4754 if (pvd->vdev_ops != pvops)
4755 pvd = vdev_add_parent(oldvd, pvops);
4757 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4758 ASSERT(pvd->vdev_ops == pvops);
4759 ASSERT(oldvd->vdev_parent == pvd);
4762 * Extract the new device from its root and add it to pvd.
4764 vdev_remove_child(newrootvd, newvd);
4765 newvd->vdev_id = pvd->vdev_children;
4766 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4767 vdev_add_child(pvd, newvd);
4769 tvd = newvd->vdev_top;
4770 ASSERT(pvd->vdev_top == tvd);
4771 ASSERT(tvd->vdev_parent == rvd);
4773 vdev_config_dirty(tvd);
4776 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4777 * for any dmu_sync-ed blocks. It will propagate upward when
4778 * spa_vdev_exit() calls vdev_dtl_reassess().
4780 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4782 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4783 dtl_max_txg - TXG_INITIAL);
4785 if (newvd->vdev_isspare) {
4786 spa_spare_activate(newvd);
4787 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4790 oldvdpath = spa_strdup(oldvd->vdev_path);
4791 newvdpath = spa_strdup(newvd->vdev_path);
4792 newvd_isspare = newvd->vdev_isspare;
4795 * Mark newvd's DTL dirty in this txg.
4797 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4800 * Schedule the resilver to restart in the future. We do this to
4801 * ensure that dmu_sync-ed blocks have been stitched into the
4802 * respective datasets.
4804 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4809 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4811 spa_history_log_internal(spa, "vdev attach", NULL,
4812 "%s vdev=%s %s vdev=%s",
4813 replacing && newvd_isspare ? "spare in" :
4814 replacing ? "replace" : "attach", newvdpath,
4815 replacing ? "for" : "to", oldvdpath);
4817 spa_strfree(oldvdpath);
4818 spa_strfree(newvdpath);
4820 if (spa->spa_bootfs)
4821 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4827 * Detach a device from a mirror or replacing vdev.
4829 * If 'replace_done' is specified, only detach if the parent
4830 * is a replacing vdev.
4833 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4837 vdev_t *rvd = spa->spa_root_vdev;
4838 vdev_t *vd, *pvd, *cvd, *tvd;
4839 boolean_t unspare = B_FALSE;
4840 uint64_t unspare_guid = 0;
4843 ASSERT(spa_writeable(spa));
4845 txg = spa_vdev_enter(spa);
4847 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4850 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4852 if (!vd->vdev_ops->vdev_op_leaf)
4853 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4855 pvd = vd->vdev_parent;
4858 * If the parent/child relationship is not as expected, don't do it.
4859 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4860 * vdev that's replacing B with C. The user's intent in replacing
4861 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4862 * the replace by detaching C, the expected behavior is to end up
4863 * M(A,B). But suppose that right after deciding to detach C,
4864 * the replacement of B completes. We would have M(A,C), and then
4865 * ask to detach C, which would leave us with just A -- not what
4866 * the user wanted. To prevent this, we make sure that the
4867 * parent/child relationship hasn't changed -- in this example,
4868 * that C's parent is still the replacing vdev R.
4870 if (pvd->vdev_guid != pguid && pguid != 0)
4871 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4874 * Only 'replacing' or 'spare' vdevs can be replaced.
4876 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4877 pvd->vdev_ops != &vdev_spare_ops)
4878 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4880 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4881 spa_version(spa) >= SPA_VERSION_SPARES);
4884 * Only mirror, replacing, and spare vdevs support detach.
4886 if (pvd->vdev_ops != &vdev_replacing_ops &&
4887 pvd->vdev_ops != &vdev_mirror_ops &&
4888 pvd->vdev_ops != &vdev_spare_ops)
4889 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4892 * If this device has the only valid copy of some data,
4893 * we cannot safely detach it.
4895 if (vdev_dtl_required(vd))
4896 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4898 ASSERT(pvd->vdev_children >= 2);
4901 * If we are detaching the second disk from a replacing vdev, then
4902 * check to see if we changed the original vdev's path to have "/old"
4903 * at the end in spa_vdev_attach(). If so, undo that change now.
4905 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4906 vd->vdev_path != NULL) {
4907 size_t len = strlen(vd->vdev_path);
4909 for (int c = 0; c < pvd->vdev_children; c++) {
4910 cvd = pvd->vdev_child[c];
4912 if (cvd == vd || cvd->vdev_path == NULL)
4915 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4916 strcmp(cvd->vdev_path + len, "/old") == 0) {
4917 spa_strfree(cvd->vdev_path);
4918 cvd->vdev_path = spa_strdup(vd->vdev_path);
4925 * If we are detaching the original disk from a spare, then it implies
4926 * that the spare should become a real disk, and be removed from the
4927 * active spare list for the pool.
4929 if (pvd->vdev_ops == &vdev_spare_ops &&
4931 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4935 * Erase the disk labels so the disk can be used for other things.
4936 * This must be done after all other error cases are handled,
4937 * but before we disembowel vd (so we can still do I/O to it).
4938 * But if we can't do it, don't treat the error as fatal --
4939 * it may be that the unwritability of the disk is the reason
4940 * it's being detached!
4942 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4945 * Remove vd from its parent and compact the parent's children.
4947 vdev_remove_child(pvd, vd);
4948 vdev_compact_children(pvd);
4951 * Remember one of the remaining children so we can get tvd below.
4953 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4956 * If we need to remove the remaining child from the list of hot spares,
4957 * do it now, marking the vdev as no longer a spare in the process.
4958 * We must do this before vdev_remove_parent(), because that can
4959 * change the GUID if it creates a new toplevel GUID. For a similar
4960 * reason, we must remove the spare now, in the same txg as the detach;
4961 * otherwise someone could attach a new sibling, change the GUID, and
4962 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4965 ASSERT(cvd->vdev_isspare);
4966 spa_spare_remove(cvd);
4967 unspare_guid = cvd->vdev_guid;
4968 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4969 cvd->vdev_unspare = B_TRUE;
4973 * If the parent mirror/replacing vdev only has one child,
4974 * the parent is no longer needed. Remove it from the tree.
4976 if (pvd->vdev_children == 1) {
4977 if (pvd->vdev_ops == &vdev_spare_ops)
4978 cvd->vdev_unspare = B_FALSE;
4979 vdev_remove_parent(cvd);
4984 * We don't set tvd until now because the parent we just removed
4985 * may have been the previous top-level vdev.
4987 tvd = cvd->vdev_top;
4988 ASSERT(tvd->vdev_parent == rvd);
4991 * Reevaluate the parent vdev state.
4993 vdev_propagate_state(cvd);
4996 * If the 'autoexpand' property is set on the pool then automatically
4997 * try to expand the size of the pool. For example if the device we
4998 * just detached was smaller than the others, it may be possible to
4999 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5000 * first so that we can obtain the updated sizes of the leaf vdevs.
5002 if (spa->spa_autoexpand) {
5004 vdev_expand(tvd, txg);
5007 vdev_config_dirty(tvd);
5010 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5011 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5012 * But first make sure we're not on any *other* txg's DTL list, to
5013 * prevent vd from being accessed after it's freed.
5015 vdpath = spa_strdup(vd->vdev_path);
5016 for (int t = 0; t < TXG_SIZE; t++)
5017 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5018 vd->vdev_detached = B_TRUE;
5019 vdev_dirty(tvd, VDD_DTL, vd, txg);
5021 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5023 /* hang on to the spa before we release the lock */
5024 spa_open_ref(spa, FTAG);
5026 error = spa_vdev_exit(spa, vd, txg, 0);
5028 spa_history_log_internal(spa, "detach", NULL,
5030 spa_strfree(vdpath);
5033 * If this was the removal of the original device in a hot spare vdev,
5034 * then we want to go through and remove the device from the hot spare
5035 * list of every other pool.
5038 spa_t *altspa = NULL;
5040 mutex_enter(&spa_namespace_lock);
5041 while ((altspa = spa_next(altspa)) != NULL) {
5042 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5046 spa_open_ref(altspa, FTAG);
5047 mutex_exit(&spa_namespace_lock);
5048 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5049 mutex_enter(&spa_namespace_lock);
5050 spa_close(altspa, FTAG);
5052 mutex_exit(&spa_namespace_lock);
5054 /* search the rest of the vdevs for spares to remove */
5055 spa_vdev_resilver_done(spa);
5058 /* all done with the spa; OK to release */
5059 mutex_enter(&spa_namespace_lock);
5060 spa_close(spa, FTAG);
5061 mutex_exit(&spa_namespace_lock);
5067 * Split a set of devices from their mirrors, and create a new pool from them.
5070 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5071 nvlist_t *props, boolean_t exp)
5074 uint64_t txg, *glist;
5076 uint_t c, children, lastlog;
5077 nvlist_t **child, *nvl, *tmp;
5079 char *altroot = NULL;
5080 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5081 boolean_t activate_slog;
5083 ASSERT(spa_writeable(spa));
5085 txg = spa_vdev_enter(spa);
5087 /* clear the log and flush everything up to now */
5088 activate_slog = spa_passivate_log(spa);
5089 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5090 error = spa_offline_log(spa);
5091 txg = spa_vdev_config_enter(spa);
5094 spa_activate_log(spa);
5097 return (spa_vdev_exit(spa, NULL, txg, error));
5099 /* check new spa name before going any further */
5100 if (spa_lookup(newname) != NULL)
5101 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5104 * scan through all the children to ensure they're all mirrors
5106 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5107 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5109 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5111 /* first, check to ensure we've got the right child count */
5112 rvd = spa->spa_root_vdev;
5114 for (c = 0; c < rvd->vdev_children; c++) {
5115 vdev_t *vd = rvd->vdev_child[c];
5117 /* don't count the holes & logs as children */
5118 if (vd->vdev_islog || vd->vdev_ishole) {
5126 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5127 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5129 /* next, ensure no spare or cache devices are part of the split */
5130 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5131 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5132 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5134 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5135 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5137 /* then, loop over each vdev and validate it */
5138 for (c = 0; c < children; c++) {
5139 uint64_t is_hole = 0;
5141 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5145 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5146 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5149 error = SET_ERROR(EINVAL);
5154 /* which disk is going to be split? */
5155 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5157 error = SET_ERROR(EINVAL);
5161 /* look it up in the spa */
5162 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5163 if (vml[c] == NULL) {
5164 error = SET_ERROR(ENODEV);
5168 /* make sure there's nothing stopping the split */
5169 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5170 vml[c]->vdev_islog ||
5171 vml[c]->vdev_ishole ||
5172 vml[c]->vdev_isspare ||
5173 vml[c]->vdev_isl2cache ||
5174 !vdev_writeable(vml[c]) ||
5175 vml[c]->vdev_children != 0 ||
5176 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5177 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5178 error = SET_ERROR(EINVAL);
5182 if (vdev_dtl_required(vml[c])) {
5183 error = SET_ERROR(EBUSY);
5187 /* we need certain info from the top level */
5188 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5189 vml[c]->vdev_top->vdev_ms_array) == 0);
5190 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5191 vml[c]->vdev_top->vdev_ms_shift) == 0);
5192 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5193 vml[c]->vdev_top->vdev_asize) == 0);
5194 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5195 vml[c]->vdev_top->vdev_ashift) == 0);
5199 kmem_free(vml, children * sizeof (vdev_t *));
5200 kmem_free(glist, children * sizeof (uint64_t));
5201 return (spa_vdev_exit(spa, NULL, txg, error));
5204 /* stop writers from using the disks */
5205 for (c = 0; c < children; c++) {
5207 vml[c]->vdev_offline = B_TRUE;
5209 vdev_reopen(spa->spa_root_vdev);
5212 * Temporarily record the splitting vdevs in the spa config. This
5213 * will disappear once the config is regenerated.
5215 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5216 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5217 glist, children) == 0);
5218 kmem_free(glist, children * sizeof (uint64_t));
5220 mutex_enter(&spa->spa_props_lock);
5221 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5223 mutex_exit(&spa->spa_props_lock);
5224 spa->spa_config_splitting = nvl;
5225 vdev_config_dirty(spa->spa_root_vdev);
5227 /* configure and create the new pool */
5228 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5229 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5230 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5231 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5232 spa_version(spa)) == 0);
5233 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5234 spa->spa_config_txg) == 0);
5235 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5236 spa_generate_guid(NULL)) == 0);
5237 (void) nvlist_lookup_string(props,
5238 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5240 /* add the new pool to the namespace */
5241 newspa = spa_add(newname, config, altroot);
5242 newspa->spa_config_txg = spa->spa_config_txg;
5243 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5245 /* release the spa config lock, retaining the namespace lock */
5246 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5248 if (zio_injection_enabled)
5249 zio_handle_panic_injection(spa, FTAG, 1);
5251 spa_activate(newspa, spa_mode_global);
5252 spa_async_suspend(newspa);
5255 /* mark that we are creating new spa by splitting */
5256 newspa->spa_splitting_newspa = B_TRUE;
5258 /* create the new pool from the disks of the original pool */
5259 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5261 newspa->spa_splitting_newspa = B_FALSE;
5266 /* if that worked, generate a real config for the new pool */
5267 if (newspa->spa_root_vdev != NULL) {
5268 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5269 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5270 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5271 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5272 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5277 if (props != NULL) {
5278 spa_configfile_set(newspa, props, B_FALSE);
5279 error = spa_prop_set(newspa, props);
5284 /* flush everything */
5285 txg = spa_vdev_config_enter(newspa);
5286 vdev_config_dirty(newspa->spa_root_vdev);
5287 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5289 if (zio_injection_enabled)
5290 zio_handle_panic_injection(spa, FTAG, 2);
5292 spa_async_resume(newspa);
5294 /* finally, update the original pool's config */
5295 txg = spa_vdev_config_enter(spa);
5296 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5297 error = dmu_tx_assign(tx, TXG_WAIT);
5300 for (c = 0; c < children; c++) {
5301 if (vml[c] != NULL) {
5304 spa_history_log_internal(spa, "detach", tx,
5305 "vdev=%s", vml[c]->vdev_path);
5309 vdev_config_dirty(spa->spa_root_vdev);
5310 spa->spa_config_splitting = NULL;
5314 (void) spa_vdev_exit(spa, NULL, txg, 0);
5316 if (zio_injection_enabled)
5317 zio_handle_panic_injection(spa, FTAG, 3);
5319 /* split is complete; log a history record */
5320 spa_history_log_internal(newspa, "split", NULL,
5321 "from pool %s", spa_name(spa));
5323 kmem_free(vml, children * sizeof (vdev_t *));
5325 /* if we're not going to mount the filesystems in userland, export */
5327 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5334 spa_deactivate(newspa);
5337 txg = spa_vdev_config_enter(spa);
5339 /* re-online all offlined disks */
5340 for (c = 0; c < children; c++) {
5342 vml[c]->vdev_offline = B_FALSE;
5344 vdev_reopen(spa->spa_root_vdev);
5346 nvlist_free(spa->spa_config_splitting);
5347 spa->spa_config_splitting = NULL;
5348 (void) spa_vdev_exit(spa, NULL, txg, error);
5350 kmem_free(vml, children * sizeof (vdev_t *));
5355 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5357 for (int i = 0; i < count; i++) {
5360 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5363 if (guid == target_guid)
5371 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5372 nvlist_t *dev_to_remove)
5374 nvlist_t **newdev = NULL;
5377 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5379 for (int i = 0, j = 0; i < count; i++) {
5380 if (dev[i] == dev_to_remove)
5382 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5385 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5386 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5388 for (int i = 0; i < count - 1; i++)
5389 nvlist_free(newdev[i]);
5392 kmem_free(newdev, (count - 1) * sizeof (void *));
5396 * Evacuate the device.
5399 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5404 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5405 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5406 ASSERT(vd == vd->vdev_top);
5409 * Evacuate the device. We don't hold the config lock as writer
5410 * since we need to do I/O but we do keep the
5411 * spa_namespace_lock held. Once this completes the device
5412 * should no longer have any blocks allocated on it.
5414 if (vd->vdev_islog) {
5415 if (vd->vdev_stat.vs_alloc != 0)
5416 error = spa_offline_log(spa);
5418 error = SET_ERROR(ENOTSUP);
5425 * The evacuation succeeded. Remove any remaining MOS metadata
5426 * associated with this vdev, and wait for these changes to sync.
5428 ASSERT0(vd->vdev_stat.vs_alloc);
5429 txg = spa_vdev_config_enter(spa);
5430 vd->vdev_removing = B_TRUE;
5431 vdev_dirty_leaves(vd, VDD_DTL, txg);
5432 vdev_config_dirty(vd);
5433 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5439 * Complete the removal by cleaning up the namespace.
5442 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5444 vdev_t *rvd = spa->spa_root_vdev;
5445 uint64_t id = vd->vdev_id;
5446 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5448 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5449 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5450 ASSERT(vd == vd->vdev_top);
5453 * Only remove any devices which are empty.
5455 if (vd->vdev_stat.vs_alloc != 0)
5458 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5460 if (list_link_active(&vd->vdev_state_dirty_node))
5461 vdev_state_clean(vd);
5462 if (list_link_active(&vd->vdev_config_dirty_node))
5463 vdev_config_clean(vd);
5468 vdev_compact_children(rvd);
5470 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5471 vdev_add_child(rvd, vd);
5473 vdev_config_dirty(rvd);
5476 * Reassess the health of our root vdev.
5482 * Remove a device from the pool -
5484 * Removing a device from the vdev namespace requires several steps
5485 * and can take a significant amount of time. As a result we use
5486 * the spa_vdev_config_[enter/exit] functions which allow us to
5487 * grab and release the spa_config_lock while still holding the namespace
5488 * lock. During each step the configuration is synced out.
5490 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5494 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5497 metaslab_group_t *mg;
5498 nvlist_t **spares, **l2cache, *nv;
5500 uint_t nspares, nl2cache;
5502 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5504 ASSERT(spa_writeable(spa));
5507 txg = spa_vdev_enter(spa);
5509 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5511 if (spa->spa_spares.sav_vdevs != NULL &&
5512 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5513 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5514 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5516 * Only remove the hot spare if it's not currently in use
5519 if (vd == NULL || unspare) {
5520 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5521 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5522 spa_load_spares(spa);
5523 spa->spa_spares.sav_sync = B_TRUE;
5525 error = SET_ERROR(EBUSY);
5527 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5528 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5529 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5530 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5532 * Cache devices can always be removed.
5534 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5535 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5536 spa_load_l2cache(spa);
5537 spa->spa_l2cache.sav_sync = B_TRUE;
5538 } else if (vd != NULL && vd->vdev_islog) {
5540 ASSERT(vd == vd->vdev_top);
5543 * XXX - Once we have bp-rewrite this should
5544 * become the common case.
5550 * Stop allocating from this vdev.
5552 metaslab_group_passivate(mg);
5555 * Wait for the youngest allocations and frees to sync,
5556 * and then wait for the deferral of those frees to finish.
5558 spa_vdev_config_exit(spa, NULL,
5559 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5562 * Attempt to evacuate the vdev.
5564 error = spa_vdev_remove_evacuate(spa, vd);
5566 txg = spa_vdev_config_enter(spa);
5569 * If we couldn't evacuate the vdev, unwind.
5572 metaslab_group_activate(mg);
5573 return (spa_vdev_exit(spa, NULL, txg, error));
5577 * Clean up the vdev namespace.
5579 spa_vdev_remove_from_namespace(spa, vd);
5581 } else if (vd != NULL) {
5583 * Normal vdevs cannot be removed (yet).
5585 error = SET_ERROR(ENOTSUP);
5588 * There is no vdev of any kind with the specified guid.
5590 error = SET_ERROR(ENOENT);
5594 return (spa_vdev_exit(spa, NULL, txg, error));
5600 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5601 * currently spared, so we can detach it.
5604 spa_vdev_resilver_done_hunt(vdev_t *vd)
5606 vdev_t *newvd, *oldvd;
5608 for (int c = 0; c < vd->vdev_children; c++) {
5609 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5615 * Check for a completed replacement. We always consider the first
5616 * vdev in the list to be the oldest vdev, and the last one to be
5617 * the newest (see spa_vdev_attach() for how that works). In
5618 * the case where the newest vdev is faulted, we will not automatically
5619 * remove it after a resilver completes. This is OK as it will require
5620 * user intervention to determine which disk the admin wishes to keep.
5622 if (vd->vdev_ops == &vdev_replacing_ops) {
5623 ASSERT(vd->vdev_children > 1);
5625 newvd = vd->vdev_child[vd->vdev_children - 1];
5626 oldvd = vd->vdev_child[0];
5628 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5629 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5630 !vdev_dtl_required(oldvd))
5635 * Check for a completed resilver with the 'unspare' flag set.
5637 if (vd->vdev_ops == &vdev_spare_ops) {
5638 vdev_t *first = vd->vdev_child[0];
5639 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5641 if (last->vdev_unspare) {
5644 } else if (first->vdev_unspare) {
5651 if (oldvd != NULL &&
5652 vdev_dtl_empty(newvd, DTL_MISSING) &&
5653 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5654 !vdev_dtl_required(oldvd))
5658 * If there are more than two spares attached to a disk,
5659 * and those spares are not required, then we want to
5660 * attempt to free them up now so that they can be used
5661 * by other pools. Once we're back down to a single
5662 * disk+spare, we stop removing them.
5664 if (vd->vdev_children > 2) {
5665 newvd = vd->vdev_child[1];
5667 if (newvd->vdev_isspare && last->vdev_isspare &&
5668 vdev_dtl_empty(last, DTL_MISSING) &&
5669 vdev_dtl_empty(last, DTL_OUTAGE) &&
5670 !vdev_dtl_required(newvd))
5679 spa_vdev_resilver_done(spa_t *spa)
5681 vdev_t *vd, *pvd, *ppvd;
5682 uint64_t guid, sguid, pguid, ppguid;
5684 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5686 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5687 pvd = vd->vdev_parent;
5688 ppvd = pvd->vdev_parent;
5689 guid = vd->vdev_guid;
5690 pguid = pvd->vdev_guid;
5691 ppguid = ppvd->vdev_guid;
5694 * If we have just finished replacing a hot spared device, then
5695 * we need to detach the parent's first child (the original hot
5698 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5699 ppvd->vdev_children == 2) {
5700 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5701 sguid = ppvd->vdev_child[1]->vdev_guid;
5703 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5705 spa_config_exit(spa, SCL_ALL, FTAG);
5706 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5708 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5710 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5713 spa_config_exit(spa, SCL_ALL, FTAG);
5717 * Update the stored path or FRU for this vdev.
5720 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5724 boolean_t sync = B_FALSE;
5726 ASSERT(spa_writeable(spa));
5728 spa_vdev_state_enter(spa, SCL_ALL);
5730 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5731 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5733 if (!vd->vdev_ops->vdev_op_leaf)
5734 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5737 if (strcmp(value, vd->vdev_path) != 0) {
5738 spa_strfree(vd->vdev_path);
5739 vd->vdev_path = spa_strdup(value);
5743 if (vd->vdev_fru == NULL) {
5744 vd->vdev_fru = spa_strdup(value);
5746 } else if (strcmp(value, vd->vdev_fru) != 0) {
5747 spa_strfree(vd->vdev_fru);
5748 vd->vdev_fru = spa_strdup(value);
5753 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5757 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5759 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5763 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5765 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5769 * ==========================================================================
5771 * ==========================================================================
5775 spa_scan_stop(spa_t *spa)
5777 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5778 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5779 return (SET_ERROR(EBUSY));
5780 return (dsl_scan_cancel(spa->spa_dsl_pool));
5784 spa_scan(spa_t *spa, pool_scan_func_t func)
5786 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5788 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5789 return (SET_ERROR(ENOTSUP));
5792 * If a resilver was requested, but there is no DTL on a
5793 * writeable leaf device, we have nothing to do.
5795 if (func == POOL_SCAN_RESILVER &&
5796 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5797 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5801 return (dsl_scan(spa->spa_dsl_pool, func));
5805 * ==========================================================================
5806 * SPA async task processing
5807 * ==========================================================================
5811 spa_async_remove(spa_t *spa, vdev_t *vd)
5813 if (vd->vdev_remove_wanted) {
5814 vd->vdev_remove_wanted = B_FALSE;
5815 vd->vdev_delayed_close = B_FALSE;
5816 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5819 * We want to clear the stats, but we don't want to do a full
5820 * vdev_clear() as that will cause us to throw away
5821 * degraded/faulted state as well as attempt to reopen the
5822 * device, all of which is a waste.
5824 vd->vdev_stat.vs_read_errors = 0;
5825 vd->vdev_stat.vs_write_errors = 0;
5826 vd->vdev_stat.vs_checksum_errors = 0;
5828 vdev_state_dirty(vd->vdev_top);
5831 for (int c = 0; c < vd->vdev_children; c++)
5832 spa_async_remove(spa, vd->vdev_child[c]);
5836 spa_async_probe(spa_t *spa, vdev_t *vd)
5838 if (vd->vdev_probe_wanted) {
5839 vd->vdev_probe_wanted = B_FALSE;
5840 vdev_reopen(vd); /* vdev_open() does the actual probe */
5843 for (int c = 0; c < vd->vdev_children; c++)
5844 spa_async_probe(spa, vd->vdev_child[c]);
5848 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5854 if (!spa->spa_autoexpand)
5857 for (int c = 0; c < vd->vdev_children; c++) {
5858 vdev_t *cvd = vd->vdev_child[c];
5859 spa_async_autoexpand(spa, cvd);
5862 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5865 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5866 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5868 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5869 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5871 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5872 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5875 kmem_free(physpath, MAXPATHLEN);
5879 spa_async_thread(void *arg)
5884 ASSERT(spa->spa_sync_on);
5886 mutex_enter(&spa->spa_async_lock);
5887 tasks = spa->spa_async_tasks;
5888 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
5889 mutex_exit(&spa->spa_async_lock);
5892 * See if the config needs to be updated.
5894 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5895 uint64_t old_space, new_space;
5897 mutex_enter(&spa_namespace_lock);
5898 old_space = metaslab_class_get_space(spa_normal_class(spa));
5899 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5900 new_space = metaslab_class_get_space(spa_normal_class(spa));
5901 mutex_exit(&spa_namespace_lock);
5904 * If the pool grew as a result of the config update,
5905 * then log an internal history event.
5907 if (new_space != old_space) {
5908 spa_history_log_internal(spa, "vdev online", NULL,
5909 "pool '%s' size: %llu(+%llu)",
5910 spa_name(spa), new_space, new_space - old_space);
5914 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5915 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5916 spa_async_autoexpand(spa, spa->spa_root_vdev);
5917 spa_config_exit(spa, SCL_CONFIG, FTAG);
5921 * See if any devices need to be probed.
5923 if (tasks & SPA_ASYNC_PROBE) {
5924 spa_vdev_state_enter(spa, SCL_NONE);
5925 spa_async_probe(spa, spa->spa_root_vdev);
5926 (void) spa_vdev_state_exit(spa, NULL, 0);
5930 * If any devices are done replacing, detach them.
5932 if (tasks & SPA_ASYNC_RESILVER_DONE)
5933 spa_vdev_resilver_done(spa);
5936 * Kick off a resilver.
5938 if (tasks & SPA_ASYNC_RESILVER)
5939 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5942 * Let the world know that we're done.
5944 mutex_enter(&spa->spa_async_lock);
5945 spa->spa_async_thread = NULL;
5946 cv_broadcast(&spa->spa_async_cv);
5947 mutex_exit(&spa->spa_async_lock);
5952 spa_async_thread_vd(void *arg)
5957 ASSERT(spa->spa_sync_on);
5959 mutex_enter(&spa->spa_async_lock);
5960 tasks = spa->spa_async_tasks;
5962 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
5963 mutex_exit(&spa->spa_async_lock);
5966 * See if any devices need to be marked REMOVED.
5968 if (tasks & SPA_ASYNC_REMOVE) {
5969 spa_vdev_state_enter(spa, SCL_NONE);
5970 spa_async_remove(spa, spa->spa_root_vdev);
5971 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5972 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5973 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5974 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5975 (void) spa_vdev_state_exit(spa, NULL, 0);
5979 * Let the world know that we're done.
5981 mutex_enter(&spa->spa_async_lock);
5982 tasks = spa->spa_async_tasks;
5983 if ((tasks & SPA_ASYNC_REMOVE) != 0)
5985 spa->spa_async_thread_vd = NULL;
5986 cv_broadcast(&spa->spa_async_cv);
5987 mutex_exit(&spa->spa_async_lock);
5992 spa_async_suspend(spa_t *spa)
5994 mutex_enter(&spa->spa_async_lock);
5995 spa->spa_async_suspended++;
5996 while (spa->spa_async_thread != NULL &&
5997 spa->spa_async_thread_vd != NULL)
5998 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5999 mutex_exit(&spa->spa_async_lock);
6003 spa_async_resume(spa_t *spa)
6005 mutex_enter(&spa->spa_async_lock);
6006 ASSERT(spa->spa_async_suspended != 0);
6007 spa->spa_async_suspended--;
6008 mutex_exit(&spa->spa_async_lock);
6012 spa_async_tasks_pending(spa_t *spa)
6014 uint_t non_config_tasks;
6016 boolean_t config_task_suspended;
6018 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6020 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6021 if (spa->spa_ccw_fail_time == 0) {
6022 config_task_suspended = B_FALSE;
6024 config_task_suspended =
6025 (gethrtime() - spa->spa_ccw_fail_time) <
6026 (zfs_ccw_retry_interval * NANOSEC);
6029 return (non_config_tasks || (config_task && !config_task_suspended));
6033 spa_async_dispatch(spa_t *spa)
6035 mutex_enter(&spa->spa_async_lock);
6036 if (spa_async_tasks_pending(spa) &&
6037 !spa->spa_async_suspended &&
6038 spa->spa_async_thread == NULL &&
6040 spa->spa_async_thread = thread_create(NULL, 0,
6041 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6042 mutex_exit(&spa->spa_async_lock);
6046 spa_async_dispatch_vd(spa_t *spa)
6048 mutex_enter(&spa->spa_async_lock);
6049 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6050 !spa->spa_async_suspended &&
6051 spa->spa_async_thread_vd == NULL &&
6053 spa->spa_async_thread_vd = thread_create(NULL, 0,
6054 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6055 mutex_exit(&spa->spa_async_lock);
6059 spa_async_request(spa_t *spa, int task)
6061 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6062 mutex_enter(&spa->spa_async_lock);
6063 spa->spa_async_tasks |= task;
6064 mutex_exit(&spa->spa_async_lock);
6065 spa_async_dispatch_vd(spa);
6069 * ==========================================================================
6070 * SPA syncing routines
6071 * ==========================================================================
6075 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6078 bpobj_enqueue(bpo, bp, tx);
6083 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6087 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6088 BP_GET_PSIZE(bp), zio->io_flags));
6093 * Note: this simple function is not inlined to make it easier to dtrace the
6094 * amount of time spent syncing frees.
6097 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6099 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6100 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6101 VERIFY(zio_wait(zio) == 0);
6105 * Note: this simple function is not inlined to make it easier to dtrace the
6106 * amount of time spent syncing deferred frees.
6109 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6111 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6112 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6113 spa_free_sync_cb, zio, tx), ==, 0);
6114 VERIFY0(zio_wait(zio));
6119 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6121 char *packed = NULL;
6126 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6129 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6130 * information. This avoids the dmu_buf_will_dirty() path and
6131 * saves us a pre-read to get data we don't actually care about.
6133 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6134 packed = kmem_alloc(bufsize, KM_SLEEP);
6136 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6138 bzero(packed + nvsize, bufsize - nvsize);
6140 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6142 kmem_free(packed, bufsize);
6144 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6145 dmu_buf_will_dirty(db, tx);
6146 *(uint64_t *)db->db_data = nvsize;
6147 dmu_buf_rele(db, FTAG);
6151 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6152 const char *config, const char *entry)
6162 * Update the MOS nvlist describing the list of available devices.
6163 * spa_validate_aux() will have already made sure this nvlist is
6164 * valid and the vdevs are labeled appropriately.
6166 if (sav->sav_object == 0) {
6167 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6168 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6169 sizeof (uint64_t), tx);
6170 VERIFY(zap_update(spa->spa_meta_objset,
6171 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6172 &sav->sav_object, tx) == 0);
6175 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6176 if (sav->sav_count == 0) {
6177 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6179 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6180 for (i = 0; i < sav->sav_count; i++)
6181 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6182 B_FALSE, VDEV_CONFIG_L2CACHE);
6183 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6184 sav->sav_count) == 0);
6185 for (i = 0; i < sav->sav_count; i++)
6186 nvlist_free(list[i]);
6187 kmem_free(list, sav->sav_count * sizeof (void *));
6190 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6191 nvlist_free(nvroot);
6193 sav->sav_sync = B_FALSE;
6197 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6201 if (list_is_empty(&spa->spa_config_dirty_list))
6204 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6206 config = spa_config_generate(spa, spa->spa_root_vdev,
6207 dmu_tx_get_txg(tx), B_FALSE);
6210 * If we're upgrading the spa version then make sure that
6211 * the config object gets updated with the correct version.
6213 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6214 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6215 spa->spa_uberblock.ub_version);
6217 spa_config_exit(spa, SCL_STATE, FTAG);
6219 if (spa->spa_config_syncing)
6220 nvlist_free(spa->spa_config_syncing);
6221 spa->spa_config_syncing = config;
6223 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6227 spa_sync_version(void *arg, dmu_tx_t *tx)
6229 uint64_t *versionp = arg;
6230 uint64_t version = *versionp;
6231 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6234 * Setting the version is special cased when first creating the pool.
6236 ASSERT(tx->tx_txg != TXG_INITIAL);
6238 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6239 ASSERT(version >= spa_version(spa));
6241 spa->spa_uberblock.ub_version = version;
6242 vdev_config_dirty(spa->spa_root_vdev);
6243 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6247 * Set zpool properties.
6250 spa_sync_props(void *arg, dmu_tx_t *tx)
6252 nvlist_t *nvp = arg;
6253 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6254 objset_t *mos = spa->spa_meta_objset;
6255 nvpair_t *elem = NULL;
6257 mutex_enter(&spa->spa_props_lock);
6259 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6261 char *strval, *fname;
6263 const char *propname;
6264 zprop_type_t proptype;
6267 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6270 * We checked this earlier in spa_prop_validate().
6272 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6274 fname = strchr(nvpair_name(elem), '@') + 1;
6275 VERIFY0(zfeature_lookup_name(fname, &fid));
6277 spa_feature_enable(spa, fid, tx);
6278 spa_history_log_internal(spa, "set", tx,
6279 "%s=enabled", nvpair_name(elem));
6282 case ZPOOL_PROP_VERSION:
6283 intval = fnvpair_value_uint64(elem);
6285 * The version is synced seperatly before other
6286 * properties and should be correct by now.
6288 ASSERT3U(spa_version(spa), >=, intval);
6291 case ZPOOL_PROP_ALTROOT:
6293 * 'altroot' is a non-persistent property. It should
6294 * have been set temporarily at creation or import time.
6296 ASSERT(spa->spa_root != NULL);
6299 case ZPOOL_PROP_READONLY:
6300 case ZPOOL_PROP_CACHEFILE:
6302 * 'readonly' and 'cachefile' are also non-persisitent
6306 case ZPOOL_PROP_COMMENT:
6307 strval = fnvpair_value_string(elem);
6308 if (spa->spa_comment != NULL)
6309 spa_strfree(spa->spa_comment);
6310 spa->spa_comment = spa_strdup(strval);
6312 * We need to dirty the configuration on all the vdevs
6313 * so that their labels get updated. It's unnecessary
6314 * to do this for pool creation since the vdev's
6315 * configuratoin has already been dirtied.
6317 if (tx->tx_txg != TXG_INITIAL)
6318 vdev_config_dirty(spa->spa_root_vdev);
6319 spa_history_log_internal(spa, "set", tx,
6320 "%s=%s", nvpair_name(elem), strval);
6324 * Set pool property values in the poolprops mos object.
6326 if (spa->spa_pool_props_object == 0) {
6327 spa->spa_pool_props_object =
6328 zap_create_link(mos, DMU_OT_POOL_PROPS,
6329 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6333 /* normalize the property name */
6334 propname = zpool_prop_to_name(prop);
6335 proptype = zpool_prop_get_type(prop);
6337 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6338 ASSERT(proptype == PROP_TYPE_STRING);
6339 strval = fnvpair_value_string(elem);
6340 VERIFY0(zap_update(mos,
6341 spa->spa_pool_props_object, propname,
6342 1, strlen(strval) + 1, strval, tx));
6343 spa_history_log_internal(spa, "set", tx,
6344 "%s=%s", nvpair_name(elem), strval);
6345 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6346 intval = fnvpair_value_uint64(elem);
6348 if (proptype == PROP_TYPE_INDEX) {
6350 VERIFY0(zpool_prop_index_to_string(
6351 prop, intval, &unused));
6353 VERIFY0(zap_update(mos,
6354 spa->spa_pool_props_object, propname,
6355 8, 1, &intval, tx));
6356 spa_history_log_internal(spa, "set", tx,
6357 "%s=%lld", nvpair_name(elem), intval);
6359 ASSERT(0); /* not allowed */
6363 case ZPOOL_PROP_DELEGATION:
6364 spa->spa_delegation = intval;
6366 case ZPOOL_PROP_BOOTFS:
6367 spa->spa_bootfs = intval;
6369 case ZPOOL_PROP_FAILUREMODE:
6370 spa->spa_failmode = intval;
6372 case ZPOOL_PROP_AUTOEXPAND:
6373 spa->spa_autoexpand = intval;
6374 if (tx->tx_txg != TXG_INITIAL)
6375 spa_async_request(spa,
6376 SPA_ASYNC_AUTOEXPAND);
6378 case ZPOOL_PROP_DEDUPDITTO:
6379 spa->spa_dedup_ditto = intval;
6388 mutex_exit(&spa->spa_props_lock);
6392 * Perform one-time upgrade on-disk changes. spa_version() does not
6393 * reflect the new version this txg, so there must be no changes this
6394 * txg to anything that the upgrade code depends on after it executes.
6395 * Therefore this must be called after dsl_pool_sync() does the sync
6399 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6401 dsl_pool_t *dp = spa->spa_dsl_pool;
6403 ASSERT(spa->spa_sync_pass == 1);
6405 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6407 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6408 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6409 dsl_pool_create_origin(dp, tx);
6411 /* Keeping the origin open increases spa_minref */
6412 spa->spa_minref += 3;
6415 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6416 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6417 dsl_pool_upgrade_clones(dp, tx);
6420 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6421 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6422 dsl_pool_upgrade_dir_clones(dp, tx);
6424 /* Keeping the freedir open increases spa_minref */
6425 spa->spa_minref += 3;
6428 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6429 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6430 spa_feature_create_zap_objects(spa, tx);
6432 rrw_exit(&dp->dp_config_rwlock, FTAG);
6436 * Sync the specified transaction group. New blocks may be dirtied as
6437 * part of the process, so we iterate until it converges.
6440 spa_sync(spa_t *spa, uint64_t txg)
6442 dsl_pool_t *dp = spa->spa_dsl_pool;
6443 objset_t *mos = spa->spa_meta_objset;
6444 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6445 vdev_t *rvd = spa->spa_root_vdev;
6450 VERIFY(spa_writeable(spa));
6453 * Lock out configuration changes.
6455 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6457 spa->spa_syncing_txg = txg;
6458 spa->spa_sync_pass = 0;
6461 * If there are any pending vdev state changes, convert them
6462 * into config changes that go out with this transaction group.
6464 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6465 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6467 * We need the write lock here because, for aux vdevs,
6468 * calling vdev_config_dirty() modifies sav_config.
6469 * This is ugly and will become unnecessary when we
6470 * eliminate the aux vdev wart by integrating all vdevs
6471 * into the root vdev tree.
6473 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6474 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6475 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6476 vdev_state_clean(vd);
6477 vdev_config_dirty(vd);
6479 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6480 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6482 spa_config_exit(spa, SCL_STATE, FTAG);
6484 tx = dmu_tx_create_assigned(dp, txg);
6486 spa->spa_sync_starttime = gethrtime();
6488 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6489 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6492 callout_reset(&spa->spa_deadman_cycid,
6493 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6498 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6499 * set spa_deflate if we have no raid-z vdevs.
6501 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6502 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6505 for (i = 0; i < rvd->vdev_children; i++) {
6506 vd = rvd->vdev_child[i];
6507 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6510 if (i == rvd->vdev_children) {
6511 spa->spa_deflate = TRUE;
6512 VERIFY(0 == zap_add(spa->spa_meta_objset,
6513 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6514 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6519 * If anything has changed in this txg, or if someone is waiting
6520 * for this txg to sync (eg, spa_vdev_remove()), push the
6521 * deferred frees from the previous txg. If not, leave them
6522 * alone so that we don't generate work on an otherwise idle
6525 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6526 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6527 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6528 ((dsl_scan_active(dp->dp_scan) ||
6529 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6530 spa_sync_deferred_frees(spa, tx);
6534 * Iterate to convergence.
6537 int pass = ++spa->spa_sync_pass;
6539 spa_sync_config_object(spa, tx);
6540 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6541 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6542 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6543 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6544 spa_errlog_sync(spa, txg);
6545 dsl_pool_sync(dp, txg);
6547 if (pass < zfs_sync_pass_deferred_free) {
6548 spa_sync_frees(spa, free_bpl, tx);
6550 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6551 &spa->spa_deferred_bpobj, tx);
6555 dsl_scan_sync(dp, tx);
6557 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6561 spa_sync_upgrades(spa, tx);
6563 } while (dmu_objset_is_dirty(mos, txg));
6566 * Rewrite the vdev configuration (which includes the uberblock)
6567 * to commit the transaction group.
6569 * If there are no dirty vdevs, we sync the uberblock to a few
6570 * random top-level vdevs that are known to be visible in the
6571 * config cache (see spa_vdev_add() for a complete description).
6572 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6576 * We hold SCL_STATE to prevent vdev open/close/etc.
6577 * while we're attempting to write the vdev labels.
6579 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6581 if (list_is_empty(&spa->spa_config_dirty_list)) {
6582 vdev_t *svd[SPA_DVAS_PER_BP];
6584 int children = rvd->vdev_children;
6585 int c0 = spa_get_random(children);
6587 for (int c = 0; c < children; c++) {
6588 vd = rvd->vdev_child[(c0 + c) % children];
6589 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6591 svd[svdcount++] = vd;
6592 if (svdcount == SPA_DVAS_PER_BP)
6595 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6597 error = vdev_config_sync(svd, svdcount, txg,
6600 error = vdev_config_sync(rvd->vdev_child,
6601 rvd->vdev_children, txg, B_FALSE);
6603 error = vdev_config_sync(rvd->vdev_child,
6604 rvd->vdev_children, txg, B_TRUE);
6608 spa->spa_last_synced_guid = rvd->vdev_guid;
6610 spa_config_exit(spa, SCL_STATE, FTAG);
6614 zio_suspend(spa, NULL);
6615 zio_resume_wait(spa);
6620 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6623 callout_drain(&spa->spa_deadman_cycid);
6628 * Clear the dirty config list.
6630 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6631 vdev_config_clean(vd);
6634 * Now that the new config has synced transactionally,
6635 * let it become visible to the config cache.
6637 if (spa->spa_config_syncing != NULL) {
6638 spa_config_set(spa, spa->spa_config_syncing);
6639 spa->spa_config_txg = txg;
6640 spa->spa_config_syncing = NULL;
6643 spa->spa_ubsync = spa->spa_uberblock;
6645 dsl_pool_sync_done(dp, txg);
6648 * Update usable space statistics.
6650 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6651 vdev_sync_done(vd, txg);
6653 spa_update_dspace(spa);
6656 * It had better be the case that we didn't dirty anything
6657 * since vdev_config_sync().
6659 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6660 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6661 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6663 spa->spa_sync_pass = 0;
6665 spa_config_exit(spa, SCL_CONFIG, FTAG);
6667 spa_handle_ignored_writes(spa);
6670 * If any async tasks have been requested, kick them off.
6672 spa_async_dispatch(spa);
6673 spa_async_dispatch_vd(spa);
6677 * Sync all pools. We don't want to hold the namespace lock across these
6678 * operations, so we take a reference on the spa_t and drop the lock during the
6682 spa_sync_allpools(void)
6685 mutex_enter(&spa_namespace_lock);
6686 while ((spa = spa_next(spa)) != NULL) {
6687 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6688 !spa_writeable(spa) || spa_suspended(spa))
6690 spa_open_ref(spa, FTAG);
6691 mutex_exit(&spa_namespace_lock);
6692 txg_wait_synced(spa_get_dsl(spa), 0);
6693 mutex_enter(&spa_namespace_lock);
6694 spa_close(spa, FTAG);
6696 mutex_exit(&spa_namespace_lock);
6700 * ==========================================================================
6701 * Miscellaneous routines
6702 * ==========================================================================
6706 * Remove all pools in the system.
6714 * Remove all cached state. All pools should be closed now,
6715 * so every spa in the AVL tree should be unreferenced.
6717 mutex_enter(&spa_namespace_lock);
6718 while ((spa = spa_next(NULL)) != NULL) {
6720 * Stop async tasks. The async thread may need to detach
6721 * a device that's been replaced, which requires grabbing
6722 * spa_namespace_lock, so we must drop it here.
6724 spa_open_ref(spa, FTAG);
6725 mutex_exit(&spa_namespace_lock);
6726 spa_async_suspend(spa);
6727 mutex_enter(&spa_namespace_lock);
6728 spa_close(spa, FTAG);
6730 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6732 spa_deactivate(spa);
6736 mutex_exit(&spa_namespace_lock);
6740 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6745 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6749 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6750 vd = spa->spa_l2cache.sav_vdevs[i];
6751 if (vd->vdev_guid == guid)
6755 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6756 vd = spa->spa_spares.sav_vdevs[i];
6757 if (vd->vdev_guid == guid)
6766 spa_upgrade(spa_t *spa, uint64_t version)
6768 ASSERT(spa_writeable(spa));
6770 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6773 * This should only be called for a non-faulted pool, and since a
6774 * future version would result in an unopenable pool, this shouldn't be
6777 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6778 ASSERT(version >= spa->spa_uberblock.ub_version);
6780 spa->spa_uberblock.ub_version = version;
6781 vdev_config_dirty(spa->spa_root_vdev);
6783 spa_config_exit(spa, SCL_ALL, FTAG);
6785 txg_wait_synced(spa_get_dsl(spa), 0);
6789 spa_has_spare(spa_t *spa, uint64_t guid)
6793 spa_aux_vdev_t *sav = &spa->spa_spares;
6795 for (i = 0; i < sav->sav_count; i++)
6796 if (sav->sav_vdevs[i]->vdev_guid == guid)
6799 for (i = 0; i < sav->sav_npending; i++) {
6800 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6801 &spareguid) == 0 && spareguid == guid)
6809 * Check if a pool has an active shared spare device.
6810 * Note: reference count of an active spare is 2, as a spare and as a replace
6813 spa_has_active_shared_spare(spa_t *spa)
6817 spa_aux_vdev_t *sav = &spa->spa_spares;
6819 for (i = 0; i < sav->sav_count; i++) {
6820 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6821 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6830 * Post a sysevent corresponding to the given event. The 'name' must be one of
6831 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6832 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6833 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6834 * or zdb as real changes.
6837 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6841 sysevent_attr_list_t *attr = NULL;
6842 sysevent_value_t value;
6845 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6848 value.value_type = SE_DATA_TYPE_STRING;
6849 value.value.sv_string = spa_name(spa);
6850 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6853 value.value_type = SE_DATA_TYPE_UINT64;
6854 value.value.sv_uint64 = spa_guid(spa);
6855 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6859 value.value_type = SE_DATA_TYPE_UINT64;
6860 value.value.sv_uint64 = vd->vdev_guid;
6861 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6865 if (vd->vdev_path) {
6866 value.value_type = SE_DATA_TYPE_STRING;
6867 value.value.sv_string = vd->vdev_path;
6868 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6869 &value, SE_SLEEP) != 0)
6874 if (sysevent_attach_attributes(ev, attr) != 0)
6878 (void) log_sysevent(ev, SE_SLEEP, &eid);
6882 sysevent_free_attr(attr);